2001-03-13 00:58:51 +00:00
|
|
|
/*
|
|
|
|
*******************************************************************************
|
|
|
|
*
|
2005-02-17 00:19:44 +00:00
|
|
|
* Copyright (C) 2000-2005, International Business Machines
|
2001-03-13 00:58:51 +00:00
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
*
|
|
|
|
*******************************************************************************
|
|
|
|
* file name: decmn.c
|
|
|
|
* encoding: US-ASCII
|
|
|
|
* tab size: 8 (not used)
|
|
|
|
* indentation:4
|
|
|
|
*
|
|
|
|
* created on: 2001mar05
|
|
|
|
* created by: Markus W. Scherer
|
2001-11-10 05:32:54 +00:00
|
|
|
* changes by: Yves Arrouye
|
2001-03-13 00:58:51 +00:00
|
|
|
*
|
|
|
|
* This tool takes an ICU common data file (icuxyz.dat),
|
|
|
|
* outputs a list of components,
|
|
|
|
* and writes one file per packaged data piece in the common file.
|
|
|
|
* This can be used to add, remove, or replace data.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "unicode/utypes.h"
|
2004-09-23 00:24:36 +00:00
|
|
|
#include "unicode/putil.h"
|
2001-03-13 00:58:51 +00:00
|
|
|
#include "unicode/udata.h"
|
2001-11-10 05:32:54 +00:00
|
|
|
#include "uoptions.h"
|
|
|
|
#include "cstring.h"
|
2001-03-13 00:58:51 +00:00
|
|
|
|
|
|
|
static uint8_t buffer[100000], buffer2[128*1024];
|
|
|
|
|
2001-11-10 05:32:54 +00:00
|
|
|
static const char *pname;
|
|
|
|
|
|
|
|
static UOption options[]={
|
|
|
|
/*0*/ UOPTION_HELP_H,
|
|
|
|
/*1*/ UOPTION_HELP_QUESTION_MARK,
|
|
|
|
/*2*/ UOPTION_DESTDIR,
|
|
|
|
/*3*/ UOPTION_DEF(0, 'n', UOPT_NO_ARG),
|
|
|
|
/*4*/ UOPTION_DEF("comment", 'C', UOPT_NO_ARG),
|
|
|
|
};
|
|
|
|
|
2001-03-13 00:58:51 +00:00
|
|
|
static int
|
|
|
|
compareFiles(const void *file1, const void *file2) {
|
|
|
|
/* sort by file offset */
|
|
|
|
int32_t diff=*((int32_t *)file1+1)-*((int32_t *)file2+1);
|
|
|
|
if(diff!=0) {
|
|
|
|
return (int)(diff>>15)|1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2001-11-10 05:32:54 +00:00
|
|
|
copyFile(FILE *in, int32_t offset, int32_t size, const char *dir, const char *name) {
|
2001-03-13 00:58:51 +00:00
|
|
|
FILE *out;
|
|
|
|
int32_t length;
|
2001-11-10 05:32:54 +00:00
|
|
|
char path[512], *p;
|
2001-03-13 00:58:51 +00:00
|
|
|
|
|
|
|
if(0!=fseek(in, offset, SEEK_SET)) {
|
2001-11-10 05:32:54 +00:00
|
|
|
fprintf(stderr, "%s: cannot seek to position %ld for file \"%s\"\n", pname,
|
2001-03-27 21:38:04 +00:00
|
|
|
(long)offset, name);
|
2001-03-13 00:58:51 +00:00
|
|
|
return 4;
|
|
|
|
}
|
2001-11-10 05:32:54 +00:00
|
|
|
|
|
|
|
uprv_strcpy(path, dir);
|
|
|
|
p = path + strlen(path);
|
|
|
|
if (p[-1] != U_FILE_SEP_CHAR) {
|
2001-11-10 05:43:59 +00:00
|
|
|
*p++ = U_FILE_SEP_CHAR;
|
2001-11-10 05:32:54 +00:00
|
|
|
}
|
|
|
|
uprv_strcpy(p, name);
|
2001-11-12 19:15:55 +00:00
|
|
|
|
2001-11-10 05:32:54 +00:00
|
|
|
out=fopen(path, "wb");
|
2001-03-13 00:58:51 +00:00
|
|
|
if(out==NULL) {
|
2001-11-10 05:32:54 +00:00
|
|
|
fprintf(stderr, "%s: unable to open output file \"%s\"\n", pname, path);
|
2001-03-13 00:58:51 +00:00
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* copy the contents into the new, separate file */
|
|
|
|
while(size>sizeof(buffer2)) {
|
|
|
|
length=(int32_t)fread(buffer2, 1, sizeof(buffer2), in);
|
|
|
|
if(length<=0) {
|
2001-11-10 05:32:54 +00:00
|
|
|
fprintf(stderr, "%s: read error while copying output file \"%s\"\n", pname, path);
|
2001-03-13 00:58:51 +00:00
|
|
|
fclose(out);
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
if(length!=(int32_t)fwrite(buffer2, 1, length, out)) {
|
2001-11-10 05:32:54 +00:00
|
|
|
fprintf(stderr, "%s: write error while copying output file \"%s\"\n", pname, path);
|
2001-03-13 00:58:51 +00:00
|
|
|
fclose(out);
|
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
size-=length;
|
|
|
|
}
|
|
|
|
while(size>0) {
|
|
|
|
length=(int32_t)fread(buffer2, 1, size, in);
|
|
|
|
if(length<=0) {
|
2001-11-10 05:32:54 +00:00
|
|
|
fprintf(stderr, "%s: read error while copying output file \"%s\"\n", pname, path);
|
2001-03-13 00:58:51 +00:00
|
|
|
fclose(out);
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
if(length!=(int32_t)fwrite(buffer2, 1, length, out)) {
|
2001-11-10 05:32:54 +00:00
|
|
|
fprintf(stderr, "%s: write error while copying output file \"%s\"\n", pname, path);
|
2001-03-13 00:58:51 +00:00
|
|
|
fclose(out);
|
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
size-=length;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(out);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern int
|
2001-11-10 05:32:54 +00:00
|
|
|
main(int argc, char *argv[]) {
|
2001-03-13 00:58:51 +00:00
|
|
|
FILE *in;
|
|
|
|
UDataInfo *info;
|
|
|
|
uint8_t *base;
|
|
|
|
int32_t *p;
|
|
|
|
int32_t i, length, count, baseOffset;
|
2001-11-10 05:32:54 +00:00
|
|
|
int result, ishelp = 0;
|
|
|
|
|
2002-03-15 23:41:40 +00:00
|
|
|
U_MAIN_INIT_ARGS(argc, argv);
|
|
|
|
|
2001-11-10 05:32:54 +00:00
|
|
|
pname = uprv_strchr(*argv, U_FILE_SEP_CHAR);
|
2005-02-17 00:19:44 +00:00
|
|
|
#ifdef U_WINDOWS
|
2001-11-10 05:32:54 +00:00
|
|
|
if (!pname) {
|
2001-11-10 05:43:59 +00:00
|
|
|
pname = uprv_strchr(*argv, '/');
|
2001-11-10 05:32:54 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (pname) {
|
2001-11-10 05:43:59 +00:00
|
|
|
++pname;
|
2001-11-10 05:32:54 +00:00
|
|
|
} else {
|
2001-11-10 05:43:59 +00:00
|
|
|
pname = argv[0];
|
2001-11-10 05:32:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
options[2].value = ".";
|
2001-03-13 00:58:51 +00:00
|
|
|
|
2001-11-10 05:32:54 +00:00
|
|
|
argc = u_parseArgs(argc, argv, sizeof(options) / sizeof(*options), options);
|
2001-11-12 19:15:55 +00:00
|
|
|
ishelp = options[0].doesOccur || options[1].doesOccur;
|
|
|
|
if (ishelp || argc != 2) {
|
2001-03-13 00:58:51 +00:00
|
|
|
fprintf(stderr,
|
2001-11-10 05:32:54 +00:00
|
|
|
"%csage: %s [ -h, -?, --help ] [ -n ] [ -C, --comment ] [ -d, --destdir destination ] archive\n", ishelp ? 'U' : 'u', pname);
|
2001-11-10 05:43:59 +00:00
|
|
|
if (ishelp) {
|
|
|
|
fprintf(stderr, "\nOptions: -h, -?, --help print this message and exit\n"
|
|
|
|
" -n do not create files\n"
|
|
|
|
" -C, --comment print the comment embedded in the file and exit\n"
|
|
|
|
" -d, --destdir destination create files in destination\n");
|
|
|
|
}
|
2001-11-10 05:32:54 +00:00
|
|
|
|
|
|
|
return ishelp ? 0 : 1;
|
2001-03-13 00:58:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
in=fopen(argv[1], "rb");
|
|
|
|
if(in==NULL) {
|
2001-11-10 05:32:54 +00:00
|
|
|
fprintf(stderr, "%s: unable to open input file \"%s\"\n", pname, argv[1]);
|
2001-03-13 00:58:51 +00:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* read the beginning of the file */
|
|
|
|
length=(int32_t)fread(buffer, 1, sizeof(buffer), in);
|
|
|
|
if(length<20) {
|
2001-11-10 05:32:54 +00:00
|
|
|
fprintf(stderr, "%s: input file too short\n", pname);
|
2001-03-13 00:58:51 +00:00
|
|
|
fclose(in);
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check the validity of the file */
|
|
|
|
if(buffer[2]!=0xda || buffer[3]!=0x27) {
|
2001-11-10 05:32:54 +00:00
|
|
|
fprintf(stderr, "%s: not an ICU data file\n", pname);
|
2001-03-13 00:58:51 +00:00
|
|
|
fclose(in);
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check the platform properties for the file */
|
|
|
|
info=(UDataInfo *)(buffer+4);
|
|
|
|
if(info->isBigEndian!=U_IS_BIG_ENDIAN) {
|
2001-11-10 05:32:54 +00:00
|
|
|
fprintf(stderr, "%s: the file is in the wrong byte endianness\n", pname);
|
2001-03-13 00:58:51 +00:00
|
|
|
fclose(in);
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
if(info->charsetFamily!=U_CHARSET_FAMILY) {
|
2001-11-10 05:32:54 +00:00
|
|
|
fprintf(stderr, "%s: the file is not built for this machine's charset family\n", pname);
|
2001-03-13 00:58:51 +00:00
|
|
|
fclose(in);
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check that this is a common data file */
|
|
|
|
if(info->dataFormat[0]!=0x43 || info->dataFormat[1]!=0x6d || info->dataFormat[2]!=0x6e || info->dataFormat[3]!=0x44) {
|
2001-11-10 05:32:54 +00:00
|
|
|
fprintf(stderr, "%s: this file is not a common data (archive) file\n", pname);
|
2001-03-13 00:58:51 +00:00
|
|
|
fclose(in);
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check for version 1 */
|
|
|
|
if(info->formatVersion[0]!=1) {
|
2001-11-10 05:32:54 +00:00
|
|
|
fprintf(stderr, "%s: the format version %d.%d.%d.%d is not known\n", pname,
|
2001-03-13 00:58:51 +00:00
|
|
|
info->formatVersion[0], info->formatVersion[1], info->formatVersion[2], info->formatVersion[3]);
|
|
|
|
fclose(in);
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
2001-11-10 05:32:54 +00:00
|
|
|
/* do we want to show the comment, and is there a comment? */
|
|
|
|
if (options[4].doesOccur && *(uint16_t *)buffer>4+info->size) {
|
2003-05-29 17:29:39 +00:00
|
|
|
printf("%s\n", (const char *)(buffer+4+info->size));
|
2001-11-10 05:43:59 +00:00
|
|
|
return 0;
|
2001-03-13 00:58:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* output all filenames */
|
|
|
|
baseOffset=*(uint16_t *)buffer;
|
|
|
|
base=buffer+baseOffset;
|
|
|
|
p=(int32_t *)base;
|
|
|
|
count=*p++;
|
2001-11-10 05:32:54 +00:00
|
|
|
/* printf("files[%ld]\n", (long)count); */
|
2001-03-13 00:58:51 +00:00
|
|
|
for(i=0; i<count; ++i) {
|
2002-02-07 02:50:13 +00:00
|
|
|
printf("%s%c%s\n", options[2].value, U_FILE_SEP_CHAR, base+*p);
|
2001-03-13 00:58:51 +00:00
|
|
|
p+=2;
|
|
|
|
}
|
2001-11-10 05:32:54 +00:00
|
|
|
/* puts("endfiles"); */
|
|
|
|
|
2001-11-10 05:43:59 +00:00
|
|
|
if (options[3].doesOccur) { /* Do not extract. */
|
|
|
|
return 0;
|
2001-11-10 05:32:54 +00:00
|
|
|
}
|
2001-03-13 00:58:51 +00:00
|
|
|
|
|
|
|
/* sort all files by their offsets in the common file */
|
|
|
|
qsort(base+4, count, 8, compareFiles);
|
|
|
|
|
|
|
|
/* write all files except the last one */
|
|
|
|
p=(int32_t *)(base+4);
|
|
|
|
--count;
|
|
|
|
for(i=0; i<count; ++i) {
|
|
|
|
/* the size is the difference between this file's offset and the next one's */
|
2001-11-10 05:32:54 +00:00
|
|
|
result=copyFile(in, baseOffset+p[1], p[3]-p[1], options[2].value, (const char *)(base+*p));
|
2001-03-13 00:58:51 +00:00
|
|
|
if(result!=0) {
|
|
|
|
fclose(in);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
p+=2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* write the last file */
|
|
|
|
if(count>=0) {
|
|
|
|
/* the size is the number of bytes to the end of the common file */
|
|
|
|
if(0!=fseek(in, 0, SEEK_END)) {
|
2001-11-10 05:32:54 +00:00
|
|
|
fprintf(stderr, "%s: unable to seek to the end of the common file\n", pname);
|
2001-03-13 00:58:51 +00:00
|
|
|
return 4;
|
|
|
|
}
|
2001-11-10 05:32:54 +00:00
|
|
|
result=copyFile(in, baseOffset+p[1], (int32_t)ftell(in)-baseOffset-p[1], options[2].value, (const char *)(base+*p));
|
2001-03-13 00:58:51 +00:00
|
|
|
if(result!=0) {
|
|
|
|
fclose(in);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(in);
|
|
|
|
return 0;
|
|
|
|
}
|
2001-11-10 05:43:59 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Hey, Emacs, please set the following:
|
|
|
|
*
|
|
|
|
* Local Variables:
|
|
|
|
* indent-tabs-mode: nil
|
|
|
|
* End:
|
|
|
|
*
|
|
|
|
*/
|