2 *******************************************************************************
4 * Copyright (C) 1999-2014, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
8 * file name: package.cpp
10 * tab size: 8 (not used)
13 * created on: 2005aug25
14 * created by: Markus W. Scherer
16 * Read, modify, and write ICU .dat data package files.
17 * This is an integral part of the icupkg tool, moved to the toolutil library
18 * because parts of tool implementations tend to be later shared by
20 * Subsumes functionality and implementation code from
21 * gencmn, decmn, and icuswap tools.
24 #include "unicode/utypes.h"
25 #include "unicode/putil.h"
26 #include "unicode/udata.h"
41 static const int32_t kItemsChunk
= 256; /* How much to increase the filesarray by each time */
43 // general definitions ----------------------------------------------------- ***
45 /* UDataInfo cf. udata.h */
46 static const UDataInfo dataInfo
={
47 (uint16_t)sizeof(UDataInfo
),
52 (uint8_t)sizeof(UChar
),
55 {0x43, 0x6d, 0x6e, 0x44}, /* dataFormat="CmnD" */
56 {1, 0, 0, 0}, /* formatVersion */
57 {3, 0, 0, 0} /* dataVersion */
61 static void U_CALLCONV
62 printPackageError(void *context
, const char *fmt
, va_list args
) {
63 vfprintf((FILE *)context
, fmt
, args
);
68 readSwapUInt16(uint16_t x
) {
69 return (uint16_t)((x
<<8)|(x
>>8));
72 // platform types ---------------------------------------------------------- ***
74 static const char *types
="lb?e";
76 enum { TYPE_L
, TYPE_B
, TYPE_LE
, TYPE_E
, TYPE_COUNT
};
79 makeTypeEnum(uint8_t charset
, UBool isBigEndian
) {
80 return 2*(int32_t)charset
+isBigEndian
;
84 makeTypeEnum(char type
) {
86 type
== 'l' ? TYPE_L
:
87 type
== 'b' ? TYPE_B
:
88 type
== 'e' ? TYPE_E
:
93 makeTypeLetter(uint8_t charset
, UBool isBigEndian
) {
94 return types
[makeTypeEnum(charset
, isBigEndian
)];
98 makeTypeLetter(int32_t typeEnum
) {
99 return types
[typeEnum
];
103 makeTypeProps(char type
, uint8_t &charset
, UBool
&isBigEndian
) {
104 int32_t typeEnum
=makeTypeEnum(type
);
105 charset
=(uint8_t)(typeEnum
>>1);
106 isBigEndian
=(UBool
)(typeEnum
&1);
109 U_CFUNC
const UDataInfo
*
110 getDataInfo(const uint8_t *data
, int32_t length
,
111 int32_t &infoLength
, int32_t &headerLength
,
112 UErrorCode
*pErrorCode
) {
113 const DataHeader
*pHeader
;
114 const UDataInfo
*pInfo
;
116 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
120 (length
>=0 && length
<(int32_t)sizeof(DataHeader
))
122 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
126 pHeader
=(const DataHeader
*)data
;
127 pInfo
=&pHeader
->info
;
128 if( (length
>=0 && length
<(int32_t)sizeof(DataHeader
)) ||
129 pHeader
->dataHeader
.magic1
!=0xda ||
130 pHeader
->dataHeader
.magic2
!=0x27 ||
131 pInfo
->sizeofUChar
!=2
133 *pErrorCode
=U_UNSUPPORTED_ERROR
;
137 if(pInfo
->isBigEndian
==U_IS_BIG_ENDIAN
) {
138 headerLength
=pHeader
->dataHeader
.headerSize
;
139 infoLength
=pInfo
->size
;
141 headerLength
=readSwapUInt16(pHeader
->dataHeader
.headerSize
);
142 infoLength
=readSwapUInt16(pInfo
->size
);
145 if( headerLength
<(int32_t)sizeof(DataHeader
) ||
146 infoLength
<(int32_t)sizeof(UDataInfo
) ||
147 headerLength
<(int32_t)(sizeof(pHeader
->dataHeader
)+infoLength
) ||
148 (length
>=0 && length
<headerLength
)
150 *pErrorCode
=U_UNSUPPORTED_ERROR
;
158 getTypeEnumForInputData(const uint8_t *data
, int32_t length
,
159 UErrorCode
*pErrorCode
) {
160 const UDataInfo
*pInfo
;
161 int32_t infoLength
, headerLength
;
163 /* getDataInfo() checks for illegal arguments */
164 pInfo
=getDataInfo(data
, length
, infoLength
, headerLength
, pErrorCode
);
169 return makeTypeEnum(pInfo
->charsetFamily
, (UBool
)pInfo
->isBigEndian
);
172 // file handling ----------------------------------------------------------- ***
175 extractPackageName(const char *filename
,
176 char pkg
[], int32_t capacity
) {
177 const char *basename
;
180 basename
=findBasename(filename
);
181 len
=(int32_t)strlen(basename
)-4; /* -4: subtract the length of ".dat" */
183 if(len
<=0 || 0!=strcmp(basename
+len
, ".dat")) {
184 fprintf(stderr
, "icupkg: \"%s\" is not recognized as a package filename (must end with .dat)\n",
186 exit(U_ILLEGAL_ARGUMENT_ERROR
);
190 fprintf(stderr
, "icupkg: the package name \"%s\" is too long (>=%ld)\n",
191 basename
, (long)capacity
);
192 exit(U_ILLEGAL_ARGUMENT_ERROR
);
195 memcpy(pkg
, basename
, len
);
200 getFileLength(FILE *f
) {
203 fseek(f
, 0, SEEK_END
);
204 length
=(int32_t)ftell(f
);
205 fseek(f
, 0, SEEK_SET
);
210 * Turn tree separators and alternate file separators into normal file separators.
212 #if U_TREE_ENTRY_SEP_CHAR==U_FILE_SEP_CHAR && U_FILE_ALT_SEP_CHAR==U_FILE_SEP_CHAR
213 #define treeToPath(s)
216 treeToPath(char *s
) {
219 for(t
=s
; *t
!=0; ++t
) {
220 if(*t
==U_TREE_ENTRY_SEP_CHAR
|| *t
==U_FILE_ALT_SEP_CHAR
) {
228 * Turn file separators into tree separators.
230 #if U_TREE_ENTRY_SEP_CHAR==U_FILE_SEP_CHAR && U_FILE_ALT_SEP_CHAR==U_FILE_SEP_CHAR
231 #define pathToTree(s)
234 pathToTree(char *s
) {
237 for(t
=s
; *t
!=0; ++t
) {
238 if(*t
==U_FILE_SEP_CHAR
|| *t
==U_FILE_ALT_SEP_CHAR
) {
239 *t
=U_TREE_ENTRY_SEP_CHAR
;
246 * Prepend the path (if any) to the name and run the name through treeToName().
249 makeFullFilename(const char *path
, const char *name
,
250 char *filename
, int32_t capacity
) {
253 // prepend the path unless NULL or empty
254 if(path
!=NULL
&& path
[0]!=0) {
255 if((int32_t)(strlen(path
)+1)>=capacity
) {
256 fprintf(stderr
, "pathname too long: \"%s\"\n", path
);
257 exit(U_BUFFER_OVERFLOW_ERROR
);
259 strcpy(filename
, path
);
261 // make sure the path ends with a file separator
262 s
=strchr(filename
, 0);
263 if(*(s
-1)!=U_FILE_SEP_CHAR
&& *(s
-1)!=U_FILE_ALT_SEP_CHAR
) {
264 *s
++=U_FILE_SEP_CHAR
;
270 // turn the name into a filename, turn tree separators into file separators
271 if((int32_t)((s
-filename
)+strlen(name
))>=capacity
) {
272 fprintf(stderr
, "path/filename too long: \"%s%s\"\n", filename
, name
);
273 exit(U_BUFFER_OVERFLOW_ERROR
);
280 makeFullFilenameAndDirs(const char *path
, const char *name
,
281 char *filename
, int32_t capacity
) {
283 UErrorCode errorCode
;
285 makeFullFilename(path
, name
, filename
, capacity
);
287 // make tree directories
288 errorCode
=U_ZERO_ERROR
;
289 sep
=strchr(filename
, 0)-strlen(name
);
290 while((sep
=strchr(sep
, U_FILE_SEP_CHAR
))!=NULL
) {
292 *sep
=0; // truncate temporarily
293 uprv_mkdir(filename
, &errorCode
);
294 if(U_FAILURE(errorCode
)) {
295 fprintf(stderr
, "icupkg: unable to create tree directory \"%s\"\n", filename
);
296 exit(U_FILE_ACCESS_ERROR
);
299 *sep
++=U_FILE_SEP_CHAR
; // restore file separator character
304 readFile(const char *path
, const char *name
, int32_t &length
, char &type
) {
308 UErrorCode errorCode
;
309 int32_t fileLength
, typeEnum
;
311 makeFullFilename(path
, name
, filename
, (int32_t)sizeof(filename
));
313 /* open the input file, get its length, allocate memory for it, read the file */
314 file
=fopen(filename
, "rb");
316 fprintf(stderr
, "icupkg: unable to open input file \"%s\"\n", filename
);
317 exit(U_FILE_ACCESS_ERROR
);
320 /* get the file length */
321 fileLength
=getFileLength(file
);
322 if(ferror(file
) || fileLength
<=0) {
323 fprintf(stderr
, "icupkg: empty input file \"%s\"\n", filename
);
325 exit(U_FILE_ACCESS_ERROR
);
328 /* allocate the buffer, pad to multiple of 16 */
329 length
=(fileLength
+0xf)&~0xf;
330 data
=(uint8_t *)uprv_malloc(length
);
333 fprintf(stderr
, "icupkg: malloc error allocating %d bytes.\n", (int)length
);
334 exit(U_MEMORY_ALLOCATION_ERROR
);
338 if(fileLength
!=(int32_t)fread(data
, 1, fileLength
, file
)) {
339 fprintf(stderr
, "icupkg: error reading \"%s\"\n", filename
);
342 exit(U_FILE_ACCESS_ERROR
);
345 /* pad the file to a multiple of 16 using the usual padding byte */
346 if(fileLength
<length
) {
347 memset(data
+fileLength
, 0xaa, length
-fileLength
);
352 // minimum check for ICU-format data
353 errorCode
=U_ZERO_ERROR
;
354 typeEnum
=getTypeEnumForInputData(data
, length
, &errorCode
);
355 if(typeEnum
<0 || U_FAILURE(errorCode
)) {
356 fprintf(stderr
, "icupkg: not an ICU data file: \"%s\"\n", filename
);
358 #if !UCONFIG_NO_LEGACY_CONVERSION
359 exit(U_INVALID_FORMAT_ERROR
);
361 fprintf(stderr
, "U_INVALID_FORMAT_ERROR occurred but UCONFIG_NO_LEGACY_CONVERSION is on so this is expected.\n");
365 type
=makeTypeLetter(typeEnum
);
370 // .dat package file representation ---------------------------------------- ***
374 static int32_t U_CALLCONV
375 compareItems(const void * /*context*/, const void *left
, const void *right
) {
378 return (int32_t)strcmp(((Item
*)left
)->name
, ((Item
*)right
)->name
);
386 : doAutoPrefix(FALSE
), prefixEndsWithType(FALSE
) {
391 inCharset
=U_CHARSET_FAMILY
;
392 inIsBigEndian
=U_IS_BIG_ENDIAN
;
398 inStringTop
=outStringTop
=0;
401 findPrefix
=findSuffix
=NULL
;
402 findPrefixLength
=findSuffixLength
=0;
405 // create a header for an empty package
407 pHeader
=(DataHeader
*)header
;
408 pHeader
->dataHeader
.magic1
=0xda;
409 pHeader
->dataHeader
.magic2
=0x27;
410 memcpy(&pHeader
->info
, &dataInfo
, sizeof(dataInfo
));
411 headerLength
=(int32_t)(4+sizeof(dataInfo
));
412 if(headerLength
&0xf) {
413 /* NUL-pad the header to a multiple of 16 */
414 int32_t length
=(headerLength
+0xf)&~0xf;
415 memset(header
+headerLength
, 0, length
-headerLength
);
418 pHeader
->dataHeader
.headerSize
=(uint16_t)headerLength
;
421 Package::~Package() {
426 for(idx
=0; idx
<itemCount
; ++idx
) {
427 if(items
[idx
].isDataOwned
) {
428 free(items
[idx
].data
);
432 uprv_free((void*)items
);
436 Package::setPrefix(const char *p
) {
437 if(strlen(p
)>=sizeof(pkgPrefix
)) {
438 fprintf(stderr
, "icupkg: --toc_prefix %s too long\n", p
);
439 exit(U_ILLEGAL_ARGUMENT_ERROR
);
441 strcpy(pkgPrefix
, p
);
445 Package::readPackage(const char *filename
) {
447 const UDataInfo
*pInfo
;
448 UErrorCode errorCode
;
450 const uint8_t *inBytes
;
452 int32_t length
, offset
, i
;
453 int32_t itemLength
, typeEnum
;
456 const UDataOffsetTOCEntry
*inEntries
;
458 extractPackageName(filename
, inPkgName
, (int32_t)sizeof(inPkgName
));
461 inData
=readFile(NULL
, filename
, inLength
, type
);
465 * swap the header - even if the swapping itself is a no-op
466 * because it tells us the header length
468 errorCode
=U_ZERO_ERROR
;
469 makeTypeProps(type
, inCharset
, inIsBigEndian
);
470 ds
=udata_openSwapper(inIsBigEndian
, inCharset
, U_IS_BIG_ENDIAN
, U_CHARSET_FAMILY
, &errorCode
);
471 if(U_FAILURE(errorCode
)) {
472 fprintf(stderr
, "icupkg: udata_openSwapper(\"%s\") failed - %s\n",
473 filename
, u_errorName(errorCode
));
477 ds
->printError
=printPackageError
;
478 ds
->printErrorContext
=stderr
;
480 headerLength
=sizeof(header
);
481 if(length
<headerLength
) {
484 headerLength
=udata_swapDataHeader(ds
, inData
, headerLength
, header
, &errorCode
);
485 if(U_FAILURE(errorCode
)) {
489 /* check data format and format version */
490 pInfo
=(const UDataInfo
*)((const char *)inData
+4);
492 pInfo
->dataFormat
[0]==0x43 && /* dataFormat="CmnD" */
493 pInfo
->dataFormat
[1]==0x6d &&
494 pInfo
->dataFormat
[2]==0x6e &&
495 pInfo
->dataFormat
[3]==0x44 &&
496 pInfo
->formatVersion
[0]==1
498 fprintf(stderr
, "icupkg: data format %02x.%02x.%02x.%02x (format version %02x) is not recognized as an ICU .dat package\n",
499 pInfo
->dataFormat
[0], pInfo
->dataFormat
[1],
500 pInfo
->dataFormat
[2], pInfo
->dataFormat
[3],
501 pInfo
->formatVersion
[0]);
502 exit(U_UNSUPPORTED_ERROR
);
504 inIsBigEndian
=(UBool
)pInfo
->isBigEndian
;
505 inCharset
=pInfo
->charsetFamily
;
507 inBytes
=(const uint8_t *)inData
+headerLength
;
508 inEntries
=(const UDataOffsetTOCEntry
*)(inBytes
+4);
510 /* check that the itemCount fits, then the ToC table, then at least the header of the last item */
511 length
-=headerLength
;
513 /* itemCount does not fit */
516 itemCount
=udata_readInt32(ds
, *(const int32_t *)inBytes
);
517 setItemCapacity(itemCount
); /* resize so there's space */
520 } else if(length
<(4+8*itemCount
)) {
521 /* ToC table does not fit */
524 /* offset of the last item plus at least 20 bytes for its header */
525 offset
=20+(int32_t)ds
->readUInt32(inEntries
[itemCount
-1].dataOffset
);
529 fprintf(stderr
, "icupkg: too few bytes (%ld after header) for a .dat package\n",
531 exit(U_INDEX_OUTOFBOUNDS_ERROR
);
533 /* do not modify the package length variable until the last item's length is set */
537 fprintf(stderr
, "icupkg: --auto_toc_prefix[_with_type] but the input package is empty\n");
538 exit(U_INVALID_FORMAT_ERROR
);
541 char prefix
[MAX_PKG_NAME_LENGTH
+4];
542 char *s
, *inItemStrings
;
544 if(itemCount
>itemMax
) {
545 fprintf(stderr
, "icupkg: too many items, maximum is %d\n", itemMax
);
546 exit(U_BUFFER_OVERFLOW_ERROR
);
549 /* swap the item name strings */
550 int32_t stringsOffset
=4+8*itemCount
;
551 itemLength
=(int32_t)(ds
->readUInt32(inEntries
[0].dataOffset
))-stringsOffset
;
553 // don't include padding bytes at the end of the item names
554 while(itemLength
>0 && inBytes
[stringsOffset
+itemLength
-1]!=0) {
558 if((inStringTop
+itemLength
)>STRING_STORE_SIZE
) {
559 fprintf(stderr
, "icupkg: total length of item name strings too long\n");
560 exit(U_BUFFER_OVERFLOW_ERROR
);
563 inItemStrings
=inStrings
+inStringTop
;
564 ds
->swapInvChars(ds
, inBytes
+stringsOffset
, itemLength
, inItemStrings
, &errorCode
);
565 if(U_FAILURE(errorCode
)) {
566 fprintf(stderr
, "icupkg failed to swap the input .dat package item name strings\n");
567 exit(U_INVALID_FORMAT_ERROR
);
569 inStringTop
+=itemLength
;
571 // reset the Item entries
572 memset(items
, 0, itemCount
*sizeof(Item
));
575 * Get the common prefix of the items.
576 * New-style ICU .dat packages use tree separators ('/') between package names,
577 * tree names, and item names,
578 * while old-style ICU .dat packages (before multi-tree support)
579 * use an underscore ('_') between package and item names.
581 offset
=(int32_t)ds
->readUInt32(inEntries
[0].nameOffset
)-stringsOffset
;
582 s
=inItemStrings
+offset
; // name of the first entry
583 int32_t prefixLength
;
585 // Use the first entry's prefix. Must be a new-style package.
586 const char *prefixLimit
=strchr(s
, U_TREE_ENTRY_SEP_CHAR
);
587 if(prefixLimit
==NULL
) {
589 "icupkg: --auto_toc_prefix[_with_type] but "
590 "the first entry \"%s\" does not contain a '%c'\n",
591 s
, U_TREE_ENTRY_SEP_CHAR
);
592 exit(U_INVALID_FORMAT_ERROR
);
594 prefixLength
=(int32_t)(prefixLimit
-s
);
595 if(prefixLength
==0 || prefixLength
>=UPRV_LENGTHOF(pkgPrefix
)) {
597 "icupkg: --auto_toc_prefix[_with_type] but "
598 "the prefix of the first entry \"%s\" is empty or too long\n",
600 exit(U_INVALID_FORMAT_ERROR
);
602 if(prefixEndsWithType
&& s
[prefixLength
-1]!=type
) {
604 "icupkg: --auto_toc_prefix_with_type but "
605 "the prefix of the first entry \"%s\" does not end with '%c'\n",
607 exit(U_INVALID_FORMAT_ERROR
);
609 memcpy(pkgPrefix
, s
, prefixLength
);
610 pkgPrefix
[prefixLength
]=0;
611 memcpy(prefix
, s
, ++prefixLength
); // include the /
613 // Use the package basename as prefix.
614 int32_t inPkgNameLength
=strlen(inPkgName
);
615 memcpy(prefix
, inPkgName
, inPkgNameLength
);
616 prefixLength
=inPkgNameLength
;
618 if( (int32_t)strlen(s
)>=(inPkgNameLength
+2) &&
619 0==memcmp(s
, inPkgName
, inPkgNameLength
) &&
620 s
[inPkgNameLength
]=='_'
622 // old-style .dat package
623 prefix
[prefixLength
++]='_';
625 // new-style .dat package
626 prefix
[prefixLength
++]=U_TREE_ENTRY_SEP_CHAR
;
627 // if it turns out to not contain U_TREE_ENTRY_SEP_CHAR
628 // then the test in the loop below will fail
631 prefix
[prefixLength
]=0;
633 /* read the ToC table */
634 for(i
=0; i
<itemCount
; ++i
) {
635 // skip the package part of the item name, error if it does not match the actual package name
636 // or if nothing follows the package name
637 offset
=(int32_t)ds
->readUInt32(inEntries
[i
].nameOffset
)-stringsOffset
;
638 s
=inItemStrings
+offset
;
639 if(0!=strncmp(s
, prefix
, prefixLength
) || s
[prefixLength
]==0) {
640 fprintf(stderr
, "icupkg: input .dat item name \"%s\" does not start with \"%s\"\n",
642 exit(U_INVALID_FORMAT_ERROR
);
644 items
[i
].name
=s
+prefixLength
;
646 // set the item's data
647 items
[i
].data
=(uint8_t *)inBytes
+ds
->readUInt32(inEntries
[i
].dataOffset
);
649 items
[i
-1].length
=(int32_t)(items
[i
].data
-items
[i
-1].data
);
651 // set the previous item's platform type
652 typeEnum
=getTypeEnumForInputData(items
[i
-1].data
, items
[i
-1].length
, &errorCode
);
653 if(typeEnum
<0 || U_FAILURE(errorCode
)) {
654 fprintf(stderr
, "icupkg: not an ICU data file: item \"%s\" in \"%s\"\n", items
[i
-1].name
, filename
);
655 exit(U_INVALID_FORMAT_ERROR
);
657 items
[i
-1].type
=makeTypeLetter(typeEnum
);
659 items
[i
].isDataOwned
=FALSE
;
661 // set the last item's length
662 items
[itemCount
-1].length
=length
-ds
->readUInt32(inEntries
[itemCount
-1].dataOffset
);
664 // set the last item's platform type
665 typeEnum
=getTypeEnumForInputData(items
[itemCount
-1].data
, items
[itemCount
-1].length
, &errorCode
);
666 if(typeEnum
<0 || U_FAILURE(errorCode
)) {
667 fprintf(stderr
, "icupkg: not an ICU data file: item \"%s\" in \"%s\"\n", items
[i
-1].name
, filename
);
668 exit(U_INVALID_FORMAT_ERROR
);
670 items
[itemCount
-1].type
=makeTypeLetter(typeEnum
);
672 if(type
!=U_ICUDATA_TYPE_LETTER
[0]) {
673 // sort the item names for the local charset
678 udata_closeSwapper(ds
);
682 Package::getInType() {
683 return makeTypeLetter(inCharset
, inIsBigEndian
);
687 Package::writePackage(const char *filename
, char outType
, const char *comment
) {
688 char prefix
[MAX_PKG_NAME_LENGTH
+4];
689 UDataOffsetTOCEntry entry
;
690 UDataSwapper
*dsLocalToOut
, *ds
[TYPE_COUNT
];
694 UErrorCode errorCode
;
695 int32_t i
, length
, prefixLength
, maxItemLength
, basenameOffset
, offset
, outInt32
;
697 UBool outIsBigEndian
;
699 extractPackageName(filename
, prefix
, MAX_PKG_NAME_LENGTH
);
701 // if there is an explicit comment, then use it, else use what's in the current header
703 /* get the header size minus the current comment */
707 pHeader
=(DataHeader
*)header
;
708 headerLength
=4+pHeader
->info
.size
;
709 length
=(int32_t)strlen(comment
);
710 if((int32_t)(headerLength
+length
)>=(int32_t)sizeof(header
)) {
711 fprintf(stderr
, "icupkg: comment too long\n");
712 exit(U_BUFFER_OVERFLOW_ERROR
);
714 memcpy(header
+headerLength
, comment
, length
+1);
715 headerLength
+=length
;
716 if(headerLength
&0xf) {
717 /* NUL-pad the header to a multiple of 16 */
718 length
=(headerLength
+0xf)&~0xf;
719 memset(header
+headerLength
, 0, length
-headerLength
);
722 pHeader
->dataHeader
.headerSize
=(uint16_t)headerLength
;
725 makeTypeProps(outType
, outCharset
, outIsBigEndian
);
727 // open (TYPE_COUNT-2) swappers
728 // one is a no-op for local type==outType
729 // one type (TYPE_LE) is bogus
730 errorCode
=U_ZERO_ERROR
;
731 i
=makeTypeEnum(outType
);
732 ds
[TYPE_B
]= i
==TYPE_B
? NULL
: udata_openSwapper(TRUE
, U_ASCII_FAMILY
, outIsBigEndian
, outCharset
, &errorCode
);
733 ds
[TYPE_L
]= i
==TYPE_L
? NULL
: udata_openSwapper(FALSE
, U_ASCII_FAMILY
, outIsBigEndian
, outCharset
, &errorCode
);
735 ds
[TYPE_E
]= i
==TYPE_E
? NULL
: udata_openSwapper(TRUE
, U_EBCDIC_FAMILY
, outIsBigEndian
, outCharset
, &errorCode
);
736 if(U_FAILURE(errorCode
)) {
737 fprintf(stderr
, "icupkg: udata_openSwapper() failed - %s\n", u_errorName(errorCode
));
740 for(i
=0; i
<TYPE_COUNT
; ++i
) {
742 ds
[i
]->printError
=printPackageError
;
743 ds
[i
]->printErrorContext
=stderr
;
747 dsLocalToOut
=ds
[makeTypeEnum(U_CHARSET_FAMILY
, U_IS_BIG_ENDIAN
)];
749 // create the file and write its contents
750 file
=fopen(filename
, "wb");
752 fprintf(stderr
, "icupkg: unable to create file \"%s\"\n", filename
);
753 exit(U_FILE_ACCESS_ERROR
);
756 // swap and write the header
757 if(dsLocalToOut
!=NULL
) {
758 udata_swapDataHeader(dsLocalToOut
, header
, headerLength
, header
, &errorCode
);
759 if(U_FAILURE(errorCode
)) {
760 fprintf(stderr
, "icupkg: udata_swapDataHeader(local to out) failed - %s\n", u_errorName(errorCode
));
764 length
=(int32_t)fwrite(header
, 1, headerLength
, file
);
765 if(length
!=headerLength
) {
766 fprintf(stderr
, "icupkg: unable to write complete header to file \"%s\"\n", filename
);
767 exit(U_FILE_ACCESS_ERROR
);
770 // prepare and swap the package name with a tree separator
771 // for prepending to item names
772 if(pkgPrefix
[0]==0) {
773 prefixLength
=(int32_t)strlen(prefix
);
775 prefixLength
=(int32_t)strlen(pkgPrefix
);
776 memcpy(prefix
, pkgPrefix
, prefixLength
);
777 if(prefixEndsWithType
) {
778 prefix
[prefixLength
-1]=outType
;
781 prefix
[prefixLength
++]=U_TREE_ENTRY_SEP_CHAR
;
782 prefix
[prefixLength
]=0;
783 if(dsLocalToOut
!=NULL
) {
784 dsLocalToOut
->swapInvChars(dsLocalToOut
, prefix
, prefixLength
, prefix
, &errorCode
);
785 if(U_FAILURE(errorCode
)) {
786 fprintf(stderr
, "icupkg: swapInvChars(output package name) failed - %s\n", u_errorName(errorCode
));
790 // swap and sort the item names (sorting needs to be done in the output charset)
791 dsLocalToOut
->swapInvChars(dsLocalToOut
, inStrings
, inStringTop
, inStrings
, &errorCode
);
792 if(U_FAILURE(errorCode
)) {
793 fprintf(stderr
, "icupkg: swapInvChars(item names) failed - %s\n", u_errorName(errorCode
));
799 // create the output item names in sorted order, with the package name prepended to each
800 for(i
=0; i
<itemCount
; ++i
) {
801 length
=(int32_t)strlen(items
[i
].name
);
802 name
=allocString(FALSE
, length
+prefixLength
);
803 memcpy(name
, prefix
, prefixLength
);
804 memcpy(name
+prefixLength
, items
[i
].name
, length
+1);
808 // calculate offsets for item names and items, pad to 16-align items
809 // align only the first item; each item's length is a multiple of 16
810 basenameOffset
=4+8*itemCount
;
811 offset
=basenameOffset
+outStringTop
;
812 if((length
=(offset
&15))!=0) {
814 memset(allocString(FALSE
, length
-1), 0xaa, length
);
818 // write the table of contents
819 // first the itemCount
821 if(dsLocalToOut
!=NULL
) {
822 dsLocalToOut
->swapArray32(dsLocalToOut
, &outInt32
, 4, &outInt32
, &errorCode
);
823 if(U_FAILURE(errorCode
)) {
824 fprintf(stderr
, "icupkg: swapArray32(item count) failed - %s\n", u_errorName(errorCode
));
828 length
=(int32_t)fwrite(&outInt32
, 1, 4, file
);
830 fprintf(stderr
, "icupkg: unable to write complete item count to file \"%s\"\n", filename
);
831 exit(U_FILE_ACCESS_ERROR
);
834 // then write the item entries (and collect the maxItemLength)
836 for(i
=0; i
<itemCount
; ++i
) {
837 entry
.nameOffset
=(uint32_t)(basenameOffset
+(items
[i
].name
-outStrings
));
838 entry
.dataOffset
=(uint32_t)offset
;
839 if(dsLocalToOut
!=NULL
) {
840 dsLocalToOut
->swapArray32(dsLocalToOut
, &entry
, 8, &entry
, &errorCode
);
841 if(U_FAILURE(errorCode
)) {
842 fprintf(stderr
, "icupkg: swapArray32(item entry %ld) failed - %s\n", (long)i
, u_errorName(errorCode
));
846 length
=(int32_t)fwrite(&entry
, 1, 8, file
);
848 fprintf(stderr
, "icupkg: unable to write complete item entry %ld to file \"%s\"\n", (long)i
, filename
);
849 exit(U_FILE_ACCESS_ERROR
);
852 length
=items
[i
].length
;
853 if(length
>maxItemLength
) {
854 maxItemLength
=length
;
859 // write the item names
860 length
=(int32_t)fwrite(outStrings
, 1, outStringTop
, file
);
861 if(length
!=outStringTop
) {
862 fprintf(stderr
, "icupkg: unable to write complete item names to file \"%s\"\n", filename
);
863 exit(U_FILE_ACCESS_ERROR
);
867 for(pItem
=items
, i
=0; i
<itemCount
; ++pItem
, ++i
) {
868 int32_t type
=makeTypeEnum(pItem
->type
);
870 // swap each item from its platform properties to the desired ones
873 pItem
->data
, pItem
->length
, pItem
->data
,
875 if(U_FAILURE(errorCode
)) {
876 fprintf(stderr
, "icupkg: udata_swap(item %ld) failed - %s\n", (long)i
, u_errorName(errorCode
));
880 length
=(int32_t)fwrite(pItem
->data
, 1, pItem
->length
, file
);
881 if(length
!=pItem
->length
) {
882 fprintf(stderr
, "icupkg: unable to write complete item %ld to file \"%s\"\n", (long)i
, filename
);
883 exit(U_FILE_ACCESS_ERROR
);
888 fprintf(stderr
, "icupkg: unable to write complete file \"%s\"\n", filename
);
889 exit(U_FILE_ACCESS_ERROR
);
893 for(i
=0; i
<TYPE_COUNT
; ++i
) {
894 udata_closeSwapper(ds
[i
]);
899 Package::findItem(const char *name
, int32_t length
) const {
900 int32_t i
, start
, limit
;
903 /* do a binary search for the string */
909 result
=strncmp(name
, items
[i
].name
, length
);
911 result
=strcmp(name
, items
[i
].name
);
918 * if we compared just prefixes, then we may need to back up
919 * to the first item with this prefix
921 while(i
>0 && 0==strncmp(name
, items
[i
-1].name
, length
)) {
926 } else if(result
<0) {
928 } else /* result>0 */ {
933 return ~start
; /* not found, return binary-not of the insertion point */
937 Package::findItems(const char *pattern
) {
940 if(pattern
==NULL
|| *pattern
==0) {
949 wild
=strchr(pattern
, '*');
952 findPrefixLength
=(int32_t)strlen(pattern
);
955 findPrefixLength
=(int32_t)(wild
-pattern
);
957 findSuffixLength
=(int32_t)strlen(findSuffix
);
958 if(NULL
!=strchr(findSuffix
, '*')) {
959 // two or more wildcards
960 fprintf(stderr
, "icupkg: syntax error (more than one '*') in item pattern \"%s\"\n", pattern
);
965 if(findPrefixLength
==0) {
968 findNextIndex
=findItem(findPrefix
, findPrefixLength
);
973 Package::findNextItem() {
974 const char *name
, *middle
, *treeSep
;
975 int32_t idx
, nameLength
, middleLength
;
977 if(findNextIndex
<0) {
981 while(findNextIndex
<itemCount
) {
983 name
=items
[idx
].name
;
984 nameLength
=(int32_t)strlen(name
);
985 if(nameLength
<(findPrefixLength
+findSuffixLength
)) {
986 // item name too short for prefix & suffix
989 if(findPrefixLength
>0 && 0!=memcmp(findPrefix
, name
, findPrefixLength
)) {
990 // left the range of names with this prefix
993 middle
=name
+findPrefixLength
;
994 middleLength
=nameLength
-findPrefixLength
-findSuffixLength
;
995 if(findSuffixLength
>0 && 0!=memcmp(findSuffix
, name
+(nameLength
-findSuffixLength
), findSuffixLength
)) {
996 // suffix does not match
999 // prefix & suffix match
1001 if(matchMode
&MATCH_NOSLASH
) {
1002 treeSep
=strchr(middle
, U_TREE_ENTRY_SEP_CHAR
);
1003 if(treeSep
!=NULL
&& (treeSep
-middle
)<middleLength
) {
1004 // the middle (matching the * wildcard) contains a tree separator /
1009 // found a matching item
1019 Package::setMatchMode(uint32_t mode
) {
1024 Package::addItem(const char *name
) {
1025 addItem(name
, NULL
, 0, FALSE
, U_ICUDATA_TYPE_LETTER
[0]);
1029 Package::addItem(const char *name
, uint8_t *data
, int32_t length
, UBool isDataOwned
, char type
) {
1034 // new item, make space at the insertion point
1035 ensureItemCapacity();
1036 // move the following items down
1039 memmove(items
+idx
+1, items
+idx
, (itemCount
-idx
)*sizeof(Item
));
1043 // reset this Item entry
1044 memset(items
+idx
, 0, sizeof(Item
));
1046 // copy the item's name
1047 items
[idx
].name
=allocString(TRUE
, strlen(name
));
1048 strcpy(items
[idx
].name
, name
);
1049 pathToTree(items
[idx
].name
);
1051 // same-name item found, replace it
1052 if(items
[idx
].isDataOwned
) {
1053 free(items
[idx
].data
);
1056 // keep the item's name since it is the same
1059 // set the item's data
1060 items
[idx
].data
=data
;
1061 items
[idx
].length
=length
;
1062 items
[idx
].isDataOwned
=isDataOwned
;
1063 items
[idx
].type
=type
;
1067 Package::addFile(const char *filesPath
, const char *name
) {
1072 data
=readFile(filesPath
, name
, length
, type
);
1073 // readFile() exits the tool if it fails
1074 addItem(name
, data
, length
, TRUE
, type
);
1078 Package::addItems(const Package
&listPkg
) {
1082 for(pItem
=listPkg
.items
, i
=0; i
<listPkg
.itemCount
; ++pItem
, ++i
) {
1083 addItem(pItem
->name
, pItem
->data
, pItem
->length
, FALSE
, pItem
->type
);
1088 Package::removeItem(int32_t idx
) {
1091 if(items
[idx
].isDataOwned
) {
1092 free(items
[idx
].data
);
1095 // move the following items up
1096 if((idx
+1)<itemCount
) {
1097 memmove(items
+idx
, items
+idx
+1, (itemCount
-(idx
+1))*sizeof(Item
));
1101 if(idx
<=findNextIndex
) {
1108 Package::removeItems(const char *pattern
) {
1112 while((idx
=findNextItem())>=0) {
1118 Package::removeItems(const Package
&listPkg
) {
1122 for(pItem
=listPkg
.items
, i
=0; i
<listPkg
.itemCount
; ++pItem
, ++i
) {
1123 removeItems(pItem
->name
);
1128 Package::extractItem(const char *filesPath
, const char *outName
, int32_t idx
, char outType
) {
1129 char filename
[1024];
1134 uint8_t itemCharset
, outCharset
;
1135 UBool itemIsBigEndian
, outIsBigEndian
;
1137 if(idx
<0 || itemCount
<=idx
) {
1142 // swap the data to the outType
1143 // outType==0: don't swap
1144 if(outType
!=0 && pItem
->type
!=outType
) {
1146 UErrorCode errorCode
=U_ZERO_ERROR
;
1147 makeTypeProps(pItem
->type
, itemCharset
, itemIsBigEndian
);
1148 makeTypeProps(outType
, outCharset
, outIsBigEndian
);
1149 ds
=udata_openSwapper(itemIsBigEndian
, itemCharset
, outIsBigEndian
, outCharset
, &errorCode
);
1150 if(U_FAILURE(errorCode
)) {
1151 fprintf(stderr
, "icupkg: udata_openSwapper(item %ld) failed - %s\n",
1152 (long)idx
, u_errorName(errorCode
));
1156 ds
->printError
=printPackageError
;
1157 ds
->printErrorContext
=stderr
;
1159 // swap the item from its platform properties to the desired ones
1160 udata_swap(ds
, pItem
->data
, pItem
->length
, pItem
->data
, &errorCode
);
1161 if(U_FAILURE(errorCode
)) {
1162 fprintf(stderr
, "icupkg: udata_swap(item %ld) failed - %s\n", (long)idx
, u_errorName(errorCode
));
1165 udata_closeSwapper(ds
);
1166 pItem
->type
=outType
;
1169 // create the file and write its contents
1170 makeFullFilenameAndDirs(filesPath
, outName
, filename
, (int32_t)sizeof(filename
));
1171 file
=fopen(filename
, "wb");
1173 fprintf(stderr
, "icupkg: unable to create file \"%s\"\n", filename
);
1174 exit(U_FILE_ACCESS_ERROR
);
1176 fileLength
=(int32_t)fwrite(pItem
->data
, 1, pItem
->length
, file
);
1178 if(ferror(file
) || fileLength
!=pItem
->length
) {
1179 fprintf(stderr
, "icupkg: unable to write complete file \"%s\"\n", filename
);
1180 exit(U_FILE_ACCESS_ERROR
);
1186 Package::extractItem(const char *filesPath
, int32_t idx
, char outType
) {
1187 extractItem(filesPath
, items
[idx
].name
, idx
, outType
);
1191 Package::extractItems(const char *filesPath
, const char *pattern
, char outType
) {
1195 while((idx
=findNextItem())>=0) {
1196 extractItem(filesPath
, idx
, outType
);
1201 Package::extractItems(const char *filesPath
, const Package
&listPkg
, char outType
) {
1205 for(pItem
=listPkg
.items
, i
=0; i
<listPkg
.itemCount
; ++pItem
, ++i
) {
1206 extractItems(filesPath
, pItem
->name
, outType
);
1211 Package::getItemCount() const {
1216 Package::getItem(int32_t idx
) const {
1217 if (0 <= idx
&& idx
< itemCount
) {
1224 Package::checkDependency(void *context
, const char *itemName
, const char *targetName
) {
1225 // check dependency: make sure the target item is in the package
1226 Package
*me
=(Package
*)context
;
1227 if(me
->findItem(targetName
)<0) {
1228 me
->isMissingItems
=TRUE
;
1229 fprintf(stderr
, "Item %s depends on missing item %s\n", itemName
, targetName
);
1234 Package::checkDependencies() {
1235 isMissingItems
=FALSE
;
1236 enumDependencies(this, checkDependency
);
1237 return (UBool
)!isMissingItems
;
1241 Package::enumDependencies(void *context
, CheckDependency check
) {
1244 for(i
=0; i
<itemCount
; ++i
) {
1245 enumDependencies(items
+i
, context
, check
);
1250 Package::allocString(UBool in
, int32_t length
) {
1263 if(top
>STRING_STORE_SIZE
) {
1264 fprintf(stderr
, "icupkg: string storage overflow\n");
1265 exit(U_BUFFER_OVERFLOW_ERROR
);
1276 Package::sortItems() {
1277 UErrorCode errorCode
=U_ZERO_ERROR
;
1278 uprv_sortArray(items
, itemCount
, (int32_t)sizeof(Item
), compareItems
, NULL
, FALSE
, &errorCode
);
1279 if(U_FAILURE(errorCode
)) {
1280 fprintf(stderr
, "icupkg: sorting item names failed - %s\n", u_errorName(errorCode
));
1285 void Package::setItemCapacity(int32_t max
)
1290 Item
*newItems
= (Item
*)uprv_malloc(max
* sizeof(items
[0]));
1291 Item
*oldItems
= items
;
1292 if(newItems
== NULL
) {
1293 fprintf(stderr
, "icupkg: Out of memory trying to allocate %lu bytes for %d items\n",
1294 (unsigned long)max
*sizeof(items
[0]), max
);
1295 exit(U_MEMORY_ALLOCATION_ERROR
);
1297 if(items
&& itemCount
>0) {
1298 uprv_memcpy(newItems
, items
, itemCount
*sizeof(items
[0]));
1302 uprv_free(oldItems
);
1305 void Package::ensureItemCapacity()
1307 if((itemCount
+1)>itemMax
) {
1308 setItemCapacity(itemCount
+kItemsChunk
);