2 *******************************************************************************
4 * Copyright (C) 1999-2007, 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"
39 // general definitions ----------------------------------------------------- ***
41 #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
43 /* UDataInfo cf. udata.h */
44 static const UDataInfo dataInfo
={
45 (uint16_t)sizeof(UDataInfo
),
50 (uint8_t)sizeof(UChar
),
53 {0x43, 0x6d, 0x6e, 0x44}, /* dataFormat="CmnD" */
54 {1, 0, 0, 0}, /* formatVersion */
55 {3, 0, 0, 0} /* dataVersion */
59 static void U_CALLCONV
60 printPackageError(void *context
, const char *fmt
, va_list args
) {
61 vfprintf((FILE *)context
, fmt
, args
);
66 readSwapUInt16(uint16_t x
) {
67 return (uint16_t)((x
<<8)|(x
>>8));
70 // platform types ---------------------------------------------------------- ***
72 static const char *types
="lb?e";
74 enum { TYPE_L
, TYPE_B
, TYPE_LE
, TYPE_E
, TYPE_COUNT
};
77 makeTypeEnum(uint8_t charset
, UBool isBigEndian
) {
78 return 2*(int32_t)charset
+isBigEndian
;
82 makeTypeEnum(char type
) {
84 type
== 'l' ? TYPE_L
:
85 type
== 'b' ? TYPE_B
:
86 type
== 'e' ? TYPE_E
:
91 makeTypeLetter(uint8_t charset
, UBool isBigEndian
) {
92 return types
[makeTypeEnum(charset
, isBigEndian
)];
96 makeTypeLetter(int32_t typeEnum
) {
97 return types
[typeEnum
];
101 makeTypeProps(char type
, uint8_t &charset
, UBool
&isBigEndian
) {
102 int32_t typeEnum
=makeTypeEnum(type
);
103 charset
=(uint8_t)(typeEnum
>>1);
104 isBigEndian
=(UBool
)(typeEnum
&1);
107 U_CFUNC
const UDataInfo
*
108 getDataInfo(const uint8_t *data
, int32_t length
,
109 int32_t &infoLength
, int32_t &headerLength
,
110 UErrorCode
*pErrorCode
) {
111 const DataHeader
*pHeader
;
112 const UDataInfo
*pInfo
;
114 if(pErrorCode
==NULL
|| U_FAILURE(*pErrorCode
)) {
118 (length
>=0 && length
<(int32_t)sizeof(DataHeader
))
120 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
124 pHeader
=(const DataHeader
*)data
;
125 pInfo
=&pHeader
->info
;
126 if( (length
>=0 && length
<(int32_t)sizeof(DataHeader
)) ||
127 pHeader
->dataHeader
.magic1
!=0xda ||
128 pHeader
->dataHeader
.magic2
!=0x27 ||
129 pInfo
->sizeofUChar
!=2
131 *pErrorCode
=U_UNSUPPORTED_ERROR
;
135 if(pInfo
->isBigEndian
==U_IS_BIG_ENDIAN
) {
136 headerLength
=pHeader
->dataHeader
.headerSize
;
137 infoLength
=pInfo
->size
;
139 headerLength
=readSwapUInt16(pHeader
->dataHeader
.headerSize
);
140 infoLength
=readSwapUInt16(pInfo
->size
);
143 if( headerLength
<(int32_t)sizeof(DataHeader
) ||
144 infoLength
<(int32_t)sizeof(UDataInfo
) ||
145 headerLength
<(int32_t)(sizeof(pHeader
->dataHeader
)+infoLength
) ||
146 (length
>=0 && length
<headerLength
)
148 *pErrorCode
=U_UNSUPPORTED_ERROR
;
156 getTypeEnumForInputData(const uint8_t *data
, int32_t length
,
157 UErrorCode
*pErrorCode
) {
158 const UDataInfo
*pInfo
;
159 int32_t infoLength
, headerLength
;
161 /* getDataInfo() checks for illegal arguments */
162 pInfo
=getDataInfo(data
, length
, infoLength
, headerLength
, pErrorCode
);
167 return makeTypeEnum(pInfo
->charsetFamily
, (UBool
)pInfo
->isBigEndian
);
170 // file handling ----------------------------------------------------------- ***
173 extractPackageName(const char *filename
,
174 char pkg
[], int32_t capacity
) {
175 const char *basename
;
178 basename
=findBasename(filename
);
179 len
=(int32_t)strlen(basename
)-4; /* -4: subtract the length of ".dat" */
181 if(len
<=0 || 0!=strcmp(basename
+len
, ".dat")) {
182 fprintf(stderr
, "icupkg: \"%s\" is not recognized as a package filename (must end with .dat)\n",
184 exit(U_ILLEGAL_ARGUMENT_ERROR
);
188 fprintf(stderr
, "icupkg: the package name \"%s\" is too long (>=%ld)\n",
189 basename
, (long)capacity
);
190 exit(U_ILLEGAL_ARGUMENT_ERROR
);
193 memcpy(pkg
, basename
, len
);
198 getFileLength(FILE *f
) {
201 fseek(f
, 0, SEEK_END
);
202 length
=(int32_t)ftell(f
);
203 fseek(f
, 0, SEEK_SET
);
208 * Turn tree separators and alternate file separators into normal file separators.
210 #if U_TREE_ENTRY_SEP_CHAR==U_FILE_SEP_CHAR && U_FILE_ALT_SEP_CHAR==U_FILE_SEP_CHAR
211 #define treeToPath(s)
214 treeToPath(char *s
) {
217 for(t
=s
; *t
!=0; ++t
) {
218 if(*t
==U_TREE_ENTRY_SEP_CHAR
|| *t
==U_FILE_ALT_SEP_CHAR
) {
226 * Turn file separators into tree separators.
228 #if U_TREE_ENTRY_SEP_CHAR==U_FILE_SEP_CHAR && U_FILE_ALT_SEP_CHAR==U_FILE_SEP_CHAR
229 #define pathToTree(s)
232 pathToTree(char *s
) {
235 for(t
=s
; *t
!=0; ++t
) {
236 if(*t
==U_FILE_SEP_CHAR
|| *t
==U_FILE_ALT_SEP_CHAR
) {
237 *t
=U_TREE_ENTRY_SEP_CHAR
;
244 * Prepend the path (if any) to the name and run the name through treeToName().
247 makeFullFilename(const char *path
, const char *name
,
248 char *filename
, int32_t capacity
) {
251 // prepend the path unless NULL or empty
252 if(path
!=NULL
&& path
[0]!=0) {
253 if((int32_t)(strlen(path
)+1)>=capacity
) {
254 fprintf(stderr
, "pathname too long: \"%s\"\n", path
);
255 exit(U_BUFFER_OVERFLOW_ERROR
);
257 strcpy(filename
, path
);
259 // make sure the path ends with a file separator
260 s
=strchr(filename
, 0);
261 if(*(s
-1)!=U_FILE_SEP_CHAR
&& *(s
-1)!=U_FILE_ALT_SEP_CHAR
) {
262 *s
++=U_FILE_SEP_CHAR
;
268 // turn the name into a filename, turn tree separators into file separators
269 if((int32_t)((s
-filename
)+strlen(name
))>=capacity
) {
270 fprintf(stderr
, "path/filename too long: \"%s%s\"\n", filename
, name
);
271 exit(U_BUFFER_OVERFLOW_ERROR
);
278 makeFullFilenameAndDirs(const char *path
, const char *name
,
279 char *filename
, int32_t capacity
) {
281 UErrorCode errorCode
;
283 makeFullFilename(path
, name
, filename
, capacity
);
285 // make tree directories
286 errorCode
=U_ZERO_ERROR
;
287 sep
=strchr(filename
, 0)-strlen(name
);
288 while((sep
=strchr(sep
, U_FILE_SEP_CHAR
))!=NULL
) {
290 *sep
=0; // truncate temporarily
291 uprv_mkdir(filename
, &errorCode
);
292 if(U_FAILURE(errorCode
)) {
293 fprintf(stderr
, "icupkg: unable to create tree directory \"%s\"\n", filename
);
294 exit(U_FILE_ACCESS_ERROR
);
297 *sep
++=U_FILE_SEP_CHAR
; // restore file separator character
302 readFile(const char *path
, const char *name
, int32_t &length
, char &type
) {
306 UErrorCode errorCode
;
307 int32_t fileLength
, typeEnum
;
309 makeFullFilename(path
, name
, filename
, (int32_t)sizeof(filename
));
311 /* open the input file, get its length, allocate memory for it, read the file */
312 file
=fopen(filename
, "rb");
314 fprintf(stderr
, "icupkg: unable to open input file \"%s\"\n", filename
);
315 exit(U_FILE_ACCESS_ERROR
);
318 /* get the file length */
319 fileLength
=getFileLength(file
);
320 if(ferror(file
) || fileLength
<=0) {
321 fprintf(stderr
, "icupkg: empty input file \"%s\"\n", filename
);
323 exit(U_FILE_ACCESS_ERROR
);
326 /* allocate the buffer, pad to multiple of 16 */
327 length
=(fileLength
+0xf)&~0xf;
328 data
=(uint8_t *)malloc(length
);
331 exit(U_MEMORY_ALLOCATION_ERROR
);
335 if(fileLength
!=(int32_t)fread(data
, 1, fileLength
, file
)) {
336 fprintf(stderr
, "icupkg: error reading \"%s\"\n", filename
);
339 exit(U_FILE_ACCESS_ERROR
);
342 /* pad the file to a multiple of 16 using the usual padding byte */
343 if(fileLength
<length
) {
344 memset(data
+fileLength
, 0xaa, length
-fileLength
);
349 // minimum check for ICU-format data
350 errorCode
=U_ZERO_ERROR
;
351 typeEnum
=getTypeEnumForInputData(data
, length
, &errorCode
);
352 if(typeEnum
<0 || U_FAILURE(errorCode
)) {
353 fprintf(stderr
, "icupkg: not an ICU data file: \"%s\"\n", filename
);
355 exit(U_INVALID_FORMAT_ERROR
);
357 type
=makeTypeLetter(typeEnum
);
362 // .dat package file representation ---------------------------------------- ***
366 static int32_t U_CALLCONV
367 compareItems(const void * /*context*/, const void *left
, const void *right
) {
370 return (int32_t)strcmp(((Item
*)left
)->name
, ((Item
*)right
)->name
);
381 inCharset
=U_CHARSET_FAMILY
;
382 inIsBigEndian
=U_IS_BIG_ENDIAN
;
385 inStringTop
=outStringTop
=0;
388 findPrefix
=findSuffix
=NULL
;
389 findPrefixLength
=findSuffixLength
=0;
392 // create a header for an empty package
394 pHeader
=(DataHeader
*)header
;
395 pHeader
->dataHeader
.magic1
=0xda;
396 pHeader
->dataHeader
.magic2
=0x27;
397 memcpy(&pHeader
->info
, &dataInfo
, sizeof(dataInfo
));
398 headerLength
=(int32_t)(4+sizeof(dataInfo
));
399 if(headerLength
&0xf) {
400 /* NUL-pad the header to a multiple of 16 */
401 int32_t length
=(headerLength
+0xf)&~0xf;
402 memset(header
+headerLength
, 0, length
-headerLength
);
405 pHeader
->dataHeader
.headerSize
=(uint16_t)headerLength
;
408 Package::~Package() {
413 for(index
=0; index
<itemCount
; ++index
) {
414 if(items
[index
].isDataOwned
) {
415 free(items
[index
].data
);
421 Package::readPackage(const char *filename
) {
423 const UDataInfo
*pInfo
;
424 UErrorCode errorCode
;
426 const uint8_t *inBytes
;
428 int32_t length
, offset
, i
;
429 int32_t itemLength
, typeEnum
;
432 const UDataOffsetTOCEntry
*inEntries
;
434 extractPackageName(filename
, inPkgName
, (int32_t)sizeof(inPkgName
));
437 inData
=readFile(NULL
, filename
, inLength
, type
);
441 * swap the header - even if the swapping itself is a no-op
442 * because it tells us the header length
444 errorCode
=U_ZERO_ERROR
;
445 makeTypeProps(type
, inCharset
, inIsBigEndian
);
446 ds
=udata_openSwapper(inIsBigEndian
, inCharset
, U_IS_BIG_ENDIAN
, U_CHARSET_FAMILY
, &errorCode
);
447 if(U_FAILURE(errorCode
)) {
448 fprintf(stderr
, "icupkg: udata_openSwapper(\"%s\") failed - %s\n",
449 filename
, u_errorName(errorCode
));
453 ds
->printError
=printPackageError
;
454 ds
->printErrorContext
=stderr
;
456 headerLength
=sizeof(header
);
457 if(length
<headerLength
) {
460 headerLength
=udata_swapDataHeader(ds
, inData
, headerLength
, header
, &errorCode
);
461 if(U_FAILURE(errorCode
)) {
465 /* check data format and format version */
466 pInfo
=(const UDataInfo
*)((const char *)inData
+4);
468 pInfo
->dataFormat
[0]==0x43 && /* dataFormat="CmnD" */
469 pInfo
->dataFormat
[1]==0x6d &&
470 pInfo
->dataFormat
[2]==0x6e &&
471 pInfo
->dataFormat
[3]==0x44 &&
472 pInfo
->formatVersion
[0]==1
474 fprintf(stderr
, "icupkg: data format %02x.%02x.%02x.%02x (format version %02x) is not recognized as an ICU .dat package\n",
475 pInfo
->dataFormat
[0], pInfo
->dataFormat
[1],
476 pInfo
->dataFormat
[2], pInfo
->dataFormat
[3],
477 pInfo
->formatVersion
[0]);
478 exit(U_UNSUPPORTED_ERROR
);
480 inIsBigEndian
=(UBool
)pInfo
->isBigEndian
;
481 inCharset
=pInfo
->charsetFamily
;
483 inBytes
=(const uint8_t *)inData
+headerLength
;
484 inEntries
=(const UDataOffsetTOCEntry
*)(inBytes
+4);
486 /* check that the itemCount fits, then the ToC table, then at least the header of the last item */
487 length
-=headerLength
;
489 /* itemCount does not fit */
492 itemCount
=udata_readInt32(ds
, *(const int32_t *)inBytes
);
495 } else if(length
<(4+8*itemCount
)) {
496 /* ToC table does not fit */
499 /* offset of the last item plus at least 20 bytes for its header */
500 offset
=20+(int32_t)ds
->readUInt32(inEntries
[itemCount
-1].dataOffset
);
504 fprintf(stderr
, "icupkg: too few bytes (%ld after header) for a .dat package\n",
506 exit(U_INDEX_OUTOFBOUNDS_ERROR
);
508 /* do not modify the package length variable until the last item's length is set */
511 char prefix
[MAX_PKG_NAME_LENGTH
+4];
512 char *s
, *inItemStrings
;
513 int32_t inPkgNameLength
, prefixLength
, stringsOffset
;
515 if(itemCount
>MAX_FILE_COUNT
) {
516 fprintf(stderr
, "icupkg: too many items, maximum is %d\n", MAX_FILE_COUNT
);
517 exit(U_BUFFER_OVERFLOW_ERROR
);
520 /* swap the item name strings */
521 stringsOffset
=4+8*itemCount
;
522 itemLength
=(int32_t)(ds
->readUInt32(inEntries
[0].dataOffset
))-stringsOffset
;
524 // don't include padding bytes at the end of the item names
525 while(itemLength
>0 && inBytes
[stringsOffset
+itemLength
-1]!=0) {
529 if((inStringTop
+itemLength
)>STRING_STORE_SIZE
) {
530 fprintf(stderr
, "icupkg: total length of item name strings too long\n");
531 exit(U_BUFFER_OVERFLOW_ERROR
);
534 inItemStrings
=inStrings
+inStringTop
;
535 ds
->swapInvChars(ds
, inBytes
+stringsOffset
, itemLength
, inItemStrings
, &errorCode
);
536 if(U_FAILURE(errorCode
)) {
537 fprintf(stderr
, "icupkg failed to swap the input .dat package item name strings\n");
538 exit(U_INVALID_FORMAT_ERROR
);
540 inStringTop
+=itemLength
;
542 // reset the Item entries
543 memset(items
, 0, itemCount
*sizeof(Item
));
545 inPkgNameLength
=strlen(inPkgName
);
546 memcpy(prefix
, inPkgName
, inPkgNameLength
);
547 prefixLength
=inPkgNameLength
;
550 * Get the common prefix of the items.
551 * New-style ICU .dat packages use tree separators ('/') between package names,
552 * tree names, and item names,
553 * while old-style ICU .dat packages (before multi-tree support)
554 * use an underscore ('_') between package and item names.
556 offset
=(int32_t)ds
->readUInt32(inEntries
[0].nameOffset
)-stringsOffset
;
557 s
=inItemStrings
+offset
;
558 if( (int32_t)strlen(s
)>=(inPkgNameLength
+2) &&
559 0==memcmp(s
, inPkgName
, inPkgNameLength
) &&
560 s
[inPkgNameLength
]=='_'
562 // old-style .dat package
563 prefix
[prefixLength
++]='_';
565 // new-style .dat package
566 prefix
[prefixLength
++]=U_TREE_ENTRY_SEP_CHAR
;
567 // if it turns out to not contain U_TREE_ENTRY_SEP_CHAR
568 // then the test in the loop below will fail
570 prefix
[prefixLength
]=0;
572 /* read the ToC table */
573 for(i
=0; i
<itemCount
; ++i
) {
574 // skip the package part of the item name, error if it does not match the actual package name
575 // or if nothing follows the package name
576 offset
=(int32_t)ds
->readUInt32(inEntries
[i
].nameOffset
)-stringsOffset
;
577 s
=inItemStrings
+offset
;
578 if(0!=strncmp(s
, prefix
, prefixLength
) || s
[prefixLength
]==0) {
579 fprintf(stderr
, "icupkg: input .dat item name \"%s\" does not start with \"%s\"\n",
581 exit(U_UNSUPPORTED_ERROR
);
583 items
[i
].name
=s
+prefixLength
;
585 // set the item's data
586 items
[i
].data
=(uint8_t *)inBytes
+ds
->readUInt32(inEntries
[i
].dataOffset
);
588 items
[i
-1].length
=(int32_t)(items
[i
].data
-items
[i
-1].data
);
590 // set the previous item's platform type
591 typeEnum
=getTypeEnumForInputData(items
[i
-1].data
, items
[i
-1].length
, &errorCode
);
592 if(typeEnum
<0 || U_FAILURE(errorCode
)) {
593 fprintf(stderr
, "icupkg: not an ICU data file: item \"%s\" in \"%s\"\n", items
[i
-1].name
, filename
);
594 exit(U_INVALID_FORMAT_ERROR
);
596 items
[i
-1].type
=makeTypeLetter(typeEnum
);
598 items
[i
].isDataOwned
=FALSE
;
600 // set the last item's length
601 items
[itemCount
-1].length
=length
-ds
->readUInt32(inEntries
[itemCount
-1].dataOffset
);
603 // set the last item's platform type
604 typeEnum
=getTypeEnumForInputData(items
[itemCount
-1].data
, items
[itemCount
-1].length
, &errorCode
);
605 if(typeEnum
<0 || U_FAILURE(errorCode
)) {
606 fprintf(stderr
, "icupkg: not an ICU data file: item \"%s\" in \"%s\"\n", items
[i
-1].name
, filename
);
607 exit(U_INVALID_FORMAT_ERROR
);
609 items
[itemCount
-1].type
=makeTypeLetter(typeEnum
);
611 if(type
!=U_ICUDATA_TYPE_LETTER
[0]) {
612 // sort the item names for the local charset
617 udata_closeSwapper(ds
);
621 Package::getInType() {
622 return makeTypeLetter(inCharset
, inIsBigEndian
);
626 Package::writePackage(const char *filename
, char outType
, const char *comment
) {
627 char prefix
[MAX_PKG_NAME_LENGTH
+4];
628 UDataOffsetTOCEntry entry
;
629 UDataSwapper
*dsLocalToOut
, *ds
[TYPE_COUNT
];
633 UErrorCode errorCode
;
634 int32_t i
, length
, prefixLength
, maxItemLength
, basenameOffset
, offset
, outInt32
;
636 UBool outIsBigEndian
;
638 extractPackageName(filename
, prefix
, MAX_PKG_NAME_LENGTH
);
640 // if there is an explicit comment, then use it, else use what's in the current header
642 /* get the header size minus the current comment */
646 pHeader
=(DataHeader
*)header
;
647 headerLength
=4+pHeader
->info
.size
;
648 length
=(int32_t)strlen(comment
);
649 if((int32_t)(headerLength
+length
)>=(int32_t)sizeof(header
)) {
650 fprintf(stderr
, "icupkg: comment too long\n");
651 exit(U_BUFFER_OVERFLOW_ERROR
);
653 memcpy(header
+headerLength
, comment
, length
+1);
654 headerLength
+=length
;
655 if(headerLength
&0xf) {
656 /* NUL-pad the header to a multiple of 16 */
657 length
=(headerLength
+0xf)&~0xf;
658 memset(header
+headerLength
, 0, length
-headerLength
);
661 pHeader
->dataHeader
.headerSize
=(uint16_t)headerLength
;
664 makeTypeProps(outType
, outCharset
, outIsBigEndian
);
666 // open (TYPE_COUNT-2) swappers
667 // one is a no-op for local type==outType
668 // one type (TYPE_LE) is bogus
669 errorCode
=U_ZERO_ERROR
;
670 i
=makeTypeEnum(outType
);
671 ds
[TYPE_B
]= i
==TYPE_B
? NULL
: udata_openSwapper(TRUE
, U_ASCII_FAMILY
, outIsBigEndian
, outCharset
, &errorCode
);
672 ds
[TYPE_L
]= i
==TYPE_L
? NULL
: udata_openSwapper(FALSE
, U_ASCII_FAMILY
, outIsBigEndian
, outCharset
, &errorCode
);
674 ds
[TYPE_E
]= i
==TYPE_E
? NULL
: udata_openSwapper(TRUE
, U_EBCDIC_FAMILY
, outIsBigEndian
, outCharset
, &errorCode
);
675 if(U_FAILURE(errorCode
)) {
676 fprintf(stderr
, "icupkg: udata_openSwapper() failed - %s\n", u_errorName(errorCode
));
679 for(i
=0; i
<TYPE_COUNT
; ++i
) {
681 ds
[i
]->printError
=printPackageError
;
682 ds
[i
]->printErrorContext
=stderr
;
686 dsLocalToOut
=ds
[makeTypeEnum(U_CHARSET_FAMILY
, U_IS_BIG_ENDIAN
)];
688 // create the file and write its contents
689 file
=fopen(filename
, "wb");
691 fprintf(stderr
, "icupkg: unable to create file \"%s\"\n", filename
);
692 exit(U_FILE_ACCESS_ERROR
);
695 // swap and write the header
696 if(dsLocalToOut
!=NULL
) {
697 udata_swapDataHeader(dsLocalToOut
, header
, headerLength
, header
, &errorCode
);
698 if(U_FAILURE(errorCode
)) {
699 fprintf(stderr
, "icupkg: udata_swapDataHeader(local to out) failed - %s\n", u_errorName(errorCode
));
703 length
=(int32_t)fwrite(header
, 1, headerLength
, file
);
704 if(length
!=headerLength
) {
705 fprintf(stderr
, "icupkg: unable to write complete header to file \"%s\"\n", filename
);
706 exit(U_FILE_ACCESS_ERROR
);
709 // prepare and swap the package name with a tree separator
710 // for prepending to item names
711 strcat(prefix
, U_TREE_ENTRY_SEP_STRING
);
712 prefixLength
=(int32_t)strlen(prefix
);
713 if(dsLocalToOut
!=NULL
) {
714 dsLocalToOut
->swapInvChars(dsLocalToOut
, prefix
, prefixLength
, prefix
, &errorCode
);
715 if(U_FAILURE(errorCode
)) {
716 fprintf(stderr
, "icupkg: swapInvChars(output package name) failed - %s\n", u_errorName(errorCode
));
720 // swap and sort the item names (sorting needs to be done in the output charset)
721 dsLocalToOut
->swapInvChars(dsLocalToOut
, inStrings
, inStringTop
, inStrings
, &errorCode
);
722 if(U_FAILURE(errorCode
)) {
723 fprintf(stderr
, "icupkg: swapInvChars(item names) failed - %s\n", u_errorName(errorCode
));
729 // create the output item names in sorted order, with the package name prepended to each
730 for(i
=0; i
<itemCount
; ++i
) {
731 length
=(int32_t)strlen(items
[i
].name
);
732 name
=allocString(FALSE
, length
+prefixLength
);
733 memcpy(name
, prefix
, prefixLength
);
734 memcpy(name
+prefixLength
, items
[i
].name
, length
+1);
738 // calculate offsets for item names and items, pad to 16-align items
739 // align only the first item; each item's length is a multiple of 16
740 basenameOffset
=4+8*itemCount
;
741 offset
=basenameOffset
+outStringTop
;
742 if((length
=(offset
&15))!=0) {
744 memset(allocString(FALSE
, length
-1), 0xaa, length
);
748 // write the table of contents
749 // first the itemCount
751 if(dsLocalToOut
!=NULL
) {
752 dsLocalToOut
->swapArray32(dsLocalToOut
, &outInt32
, 4, &outInt32
, &errorCode
);
753 if(U_FAILURE(errorCode
)) {
754 fprintf(stderr
, "icupkg: swapArray32(item count) failed - %s\n", u_errorName(errorCode
));
758 length
=(int32_t)fwrite(&outInt32
, 1, 4, file
);
760 fprintf(stderr
, "icupkg: unable to write complete item count to file \"%s\"\n", filename
);
761 exit(U_FILE_ACCESS_ERROR
);
764 // then write the item entries (and collect the maxItemLength)
766 for(i
=0; i
<itemCount
; ++i
) {
767 entry
.nameOffset
=(uint32_t)(basenameOffset
+(items
[i
].name
-outStrings
));
768 entry
.dataOffset
=(uint32_t)offset
;
769 if(dsLocalToOut
!=NULL
) {
770 dsLocalToOut
->swapArray32(dsLocalToOut
, &entry
, 8, &entry
, &errorCode
);
771 if(U_FAILURE(errorCode
)) {
772 fprintf(stderr
, "icupkg: swapArray32(item entry %ld) failed - %s\n", (long)i
, u_errorName(errorCode
));
776 length
=(int32_t)fwrite(&entry
, 1, 8, file
);
778 fprintf(stderr
, "icupkg: unable to write complete item entry %ld to file \"%s\"\n", (long)i
, filename
);
779 exit(U_FILE_ACCESS_ERROR
);
782 length
=items
[i
].length
;
783 if(length
>maxItemLength
) {
784 maxItemLength
=length
;
789 // write the item names
790 length
=(int32_t)fwrite(outStrings
, 1, outStringTop
, file
);
791 if(length
!=outStringTop
) {
792 fprintf(stderr
, "icupkg: unable to write complete item names to file \"%s\"\n", filename
);
793 exit(U_FILE_ACCESS_ERROR
);
797 for(pItem
=items
, i
=0; i
<itemCount
; ++pItem
, ++i
) {
798 int32_t type
=makeTypeEnum(pItem
->type
);
800 // swap each item from its platform properties to the desired ones
803 pItem
->data
, pItem
->length
, pItem
->data
,
805 if(U_FAILURE(errorCode
)) {
806 fprintf(stderr
, "icupkg: udata_swap(item %ld) failed - %s\n", (long)i
, u_errorName(errorCode
));
810 length
=(int32_t)fwrite(pItem
->data
, 1, pItem
->length
, file
);
811 if(length
!=pItem
->length
) {
812 fprintf(stderr
, "icupkg: unable to write complete item %ld to file \"%s\"\n", (long)i
, filename
);
813 exit(U_FILE_ACCESS_ERROR
);
818 fprintf(stderr
, "icupkg: unable to write complete file \"%s\"\n", filename
);
819 exit(U_FILE_ACCESS_ERROR
);
823 for(i
=0; i
<TYPE_COUNT
; ++i
) {
824 udata_closeSwapper(ds
[i
]);
829 Package::findItem(const char *name
, int32_t length
) const {
830 int32_t i
, start
, limit
;
833 /* do a binary search for the string */
839 result
=strncmp(name
, items
[i
].name
, length
);
841 result
=strcmp(name
, items
[i
].name
);
848 * if we compared just prefixes, then we may need to back up
849 * to the first item with this prefix
851 while(i
>0 && 0==strncmp(name
, items
[i
-1].name
, length
)) {
856 } else if(result
<0) {
858 } else /* result>0 */ {
863 return ~start
; /* not found, return binary-not of the insertion point */
867 Package::findItems(const char *pattern
) {
870 if(pattern
==NULL
|| *pattern
==0) {
879 wild
=strchr(pattern
, '*');
882 findPrefixLength
=(int32_t)strlen(pattern
);
885 findPrefixLength
=(int32_t)(wild
-pattern
);
887 findSuffixLength
=(int32_t)strlen(findSuffix
);
888 if(NULL
!=strchr(findSuffix
, '*')) {
889 // two or more wildcards
890 fprintf(stderr
, "icupkg: syntax error (more than one '*') in item pattern \"%s\"\n", pattern
);
895 if(findPrefixLength
==0) {
898 findNextIndex
=findItem(findPrefix
, findPrefixLength
);
903 Package::findNextItem() {
904 const char *name
, *middle
, *treeSep
;
905 int32_t index
, nameLength
, middleLength
;
907 if(findNextIndex
<0) {
911 while(findNextIndex
<itemCount
) {
912 index
=findNextIndex
++;
913 name
=items
[index
].name
;
914 nameLength
=(int32_t)strlen(name
);
915 if(nameLength
<(findPrefixLength
+findSuffixLength
)) {
916 // item name too short for prefix & suffix
919 if(findPrefixLength
>0 && 0!=memcmp(findPrefix
, name
, findPrefixLength
)) {
920 // left the range of names with this prefix
923 middle
=name
+findPrefixLength
;
924 middleLength
=nameLength
-findPrefixLength
-findSuffixLength
;
925 if(findSuffixLength
>0 && 0!=memcmp(findSuffix
, name
+(nameLength
-findSuffixLength
), findSuffixLength
)) {
926 // suffix does not match
929 // prefix & suffix match
931 if(matchMode
&MATCH_NOSLASH
) {
932 treeSep
=strchr(middle
, U_TREE_ENTRY_SEP_CHAR
);
933 if(treeSep
!=NULL
&& (treeSep
-middle
)<middleLength
) {
934 // the middle (matching the * wildcard) contains a tree separator /
939 // found a matching item
949 Package::setMatchMode(uint32_t mode
) {
954 Package::addItem(const char *name
) {
955 addItem(name
, NULL
, 0, FALSE
, U_ICUDATA_TYPE_LETTER
[0]);
959 Package::addItem(const char *name
, uint8_t *data
, int32_t length
, UBool isDataOwned
, char type
) {
962 index
=findItem(name
);
964 // new item, make space at the insertion point
965 if(itemCount
>=MAX_FILE_COUNT
) {
966 fprintf(stderr
, "icupkg: too many items, maximum is %d\n", MAX_FILE_COUNT
);
967 exit(U_BUFFER_OVERFLOW_ERROR
);
969 // move the following items down
971 if(index
<itemCount
) {
972 memmove(items
+index
+1, items
+index
, (itemCount
-index
)*sizeof(Item
));
976 // reset this Item entry
977 memset(items
+index
, 0, sizeof(Item
));
979 // copy the item's name
980 items
[index
].name
=allocString(TRUE
, strlen(name
));
981 strcpy(items
[index
].name
, name
);
982 pathToTree(items
[index
].name
);
984 // same-name item found, replace it
985 if(items
[index
].isDataOwned
) {
986 free(items
[index
].data
);
989 // keep the item's name since it is the same
992 // set the item's data
993 items
[index
].data
=data
;
994 items
[index
].length
=length
;
995 items
[index
].isDataOwned
=isDataOwned
;
996 items
[index
].type
=type
;
1000 Package::addFile(const char *filesPath
, const char *name
) {
1005 data
=readFile(filesPath
, name
, length
, type
);
1006 // readFile() exits the tool if it fails
1007 addItem(name
, data
, length
, TRUE
, type
);
1011 Package::addItems(const Package
&listPkg
) {
1015 for(pItem
=listPkg
.items
, i
=0; i
<listPkg
.itemCount
; ++pItem
, ++i
) {
1016 addItem(pItem
->name
, pItem
->data
, pItem
->length
, FALSE
, pItem
->type
);
1021 Package::removeItem(int32_t index
) {
1024 if(items
[index
].isDataOwned
) {
1025 free(items
[index
].data
);
1028 // move the following items up
1029 if((index
+1)<itemCount
) {
1030 memmove(items
+index
, items
+index
+1, (itemCount
-(index
+1))*sizeof(Item
));
1034 if(index
<=findNextIndex
) {
1041 Package::removeItems(const char *pattern
) {
1045 while((index
=findNextItem())>=0) {
1051 Package::removeItems(const Package
&listPkg
) {
1055 for(pItem
=listPkg
.items
, i
=0; i
<listPkg
.itemCount
; ++pItem
, ++i
) {
1056 removeItems(pItem
->name
);
1061 Package::extractItem(const char *filesPath
, const char *outName
, int32_t index
, char outType
) {
1062 char filename
[1024];
1067 uint8_t itemCharset
, outCharset
;
1068 UBool itemIsBigEndian
, outIsBigEndian
;
1070 if(index
<0 || itemCount
<=index
) {
1075 // swap the data to the outType
1076 // outType==0: don't swap
1077 if(outType
!=0 && pItem
->type
!=outType
) {
1079 UErrorCode errorCode
=U_ZERO_ERROR
;
1080 makeTypeProps(pItem
->type
, itemCharset
, itemIsBigEndian
);
1081 makeTypeProps(outType
, outCharset
, outIsBigEndian
);
1082 ds
=udata_openSwapper(itemIsBigEndian
, itemCharset
, outIsBigEndian
, outCharset
, &errorCode
);
1083 if(U_FAILURE(errorCode
)) {
1084 fprintf(stderr
, "icupkg: udata_openSwapper(item %ld) failed - %s\n",
1085 (long)index
, u_errorName(errorCode
));
1089 ds
->printError
=printPackageError
;
1090 ds
->printErrorContext
=stderr
;
1092 // swap the item from its platform properties to the desired ones
1093 udata_swap(ds
, pItem
->data
, pItem
->length
, pItem
->data
, &errorCode
);
1094 if(U_FAILURE(errorCode
)) {
1095 fprintf(stderr
, "icupkg: udata_swap(item %ld) failed - %s\n", (long)index
, u_errorName(errorCode
));
1098 udata_closeSwapper(ds
);
1101 // create the file and write its contents
1102 makeFullFilenameAndDirs(filesPath
, outName
, filename
, (int32_t)sizeof(filename
));
1103 file
=fopen(filename
, "wb");
1105 fprintf(stderr
, "icupkg: unable to create file \"%s\"\n", filename
);
1106 exit(U_FILE_ACCESS_ERROR
);
1108 fileLength
=(int32_t)fwrite(pItem
->data
, 1, pItem
->length
, file
);
1110 if(ferror(file
) || fileLength
!=pItem
->length
) {
1111 fprintf(stderr
, "icupkg: unable to write complete file \"%s\"\n", filename
);
1112 exit(U_FILE_ACCESS_ERROR
);
1118 Package::extractItem(const char *filesPath
, int32_t index
, char outType
) {
1119 extractItem(filesPath
, items
[index
].name
, index
, outType
);
1123 Package::extractItems(const char *filesPath
, const char *pattern
, char outType
) {
1127 while((index
=findNextItem())>=0) {
1128 extractItem(filesPath
, index
, outType
);
1133 Package::extractItems(const char *filesPath
, const Package
&listPkg
, char outType
) {
1137 for(pItem
=listPkg
.items
, i
=0; i
<listPkg
.itemCount
; ++pItem
, ++i
) {
1138 extractItems(filesPath
, pItem
->name
, outType
);
1143 Package::getItemCount() const {
1148 Package::getItem(int32_t idx
) const {
1149 if (0 <= idx
&& idx
< itemCount
) {
1156 Package::checkDependency(void *context
, const char *itemName
, const char *targetName
) {
1157 // check dependency: make sure the target item is in the package
1158 Package
*me
=(Package
*)context
;
1159 if(me
->findItem(targetName
)<0) {
1160 me
->isMissingItems
=TRUE
;
1161 fprintf(stderr
, "Item %s depends on missing item %s\n", itemName
, targetName
);
1166 Package::checkDependencies() {
1167 isMissingItems
=FALSE
;
1168 enumDependencies(this, checkDependency
);
1169 return (UBool
)!isMissingItems
;
1173 Package::enumDependencies(void *context
, CheckDependency check
) {
1176 for(i
=0; i
<itemCount
; ++i
) {
1177 enumDependencies(items
+i
, context
, check
);
1182 Package::allocString(UBool in
, int32_t length
) {
1195 if(top
>STRING_STORE_SIZE
) {
1196 fprintf(stderr
, "icupkg: string storage overflow\n");
1197 exit(U_BUFFER_OVERFLOW_ERROR
);
1208 Package::sortItems() {
1209 UErrorCode errorCode
=U_ZERO_ERROR
;
1210 uprv_sortArray(items
, itemCount
, (int32_t)sizeof(Item
), compareItems
, NULL
, FALSE
, &errorCode
);
1211 if(U_FAILURE(errorCode
)) {
1212 fprintf(stderr
, "icupkg: sorting item names failed - %s\n", u_errorName(errorCode
));