2 Copyright 1998-2003 Victor Wagner
3 Copyright 2003 Alex Ott
4 This file is released under the GPL. Details can be
5 found in the file COPYING accompanying this distribution.
15 char *charset_path=CHARSETPATH;
16 char *source_csname=SOURCE_CHARSET, *dest_csname=TARGET_CHARSET;
17 short int * source_charset;
19 char bad_char[]=UNKNOWN_CHAR;
20 CHARSET target_charset;
21 /************************************************************************/
22 /* Converts char in input charset into unicode representation */
23 /* Should be converted to macro */
24 /************************************************************************/
25 int to_unicode (short int *charset, int c) {
28 /************************************************************************/
29 /* Search inverse charset record for given unicode char and returns */
30 /* 0-255 char value if found, -1 otherwise */
31 /************************************************************************/
32 int from_unicode (CHARSET charset, int u) {
34 /* This is really assignment, not comparation */
35 if ((p=charset[(unsigned)u>>8])) {
41 /************************************************************************/
42 /* Converts direct (charset -> unicode) to reverse map */
43 /************************************************************************/
44 CHARSET make_reverse_map(short int *charset) {
45 CHARSET newmap=calloc(sizeof(short int *), 256);
55 newmap[j] = malloc(sizeof(short int *)*256);
57 fprintf(stderr,"Insufficient memory for charset\n");
60 for (l=0,p=newmap[j];l<256;l++,p++) *p=-1;
68 /************************************************************************/
69 /* Reads charset file (as got from ftp.unicode.org) and returns array of*/
70 /* 256 short ints (malloced) mapping from charset t unicode */
71 /************************************************************************/
72 short int * read_charset(const char *filename) {
75 short int *new=calloc(sizeof(short int),256);
78 path= find_file(stradd(filename,CHARSET_EXT),charset_path);
80 fprintf(stderr,"Cannot load charset %s - file not found\n",filename);
90 setvbuf(f,input_buffer,_IOFBF,FILE_BUFFER);
96 if (fscanf(f,"%i %li",&c,&uc)==2) {
97 if (c<0||c>255||uc<0||(uc>0xFEFE&& uc!=0xFFFE)) {
98 fprintf(stderr,"Invalid charset file %s\n",path);
104 while((fgetc(f)!='\n')&&!feof(f)) ;
112 /************************************************************************/
113 /* Reads 8-bit char and convers it from source charset */
114 /************************************************************************/
116 int get_8bit_char (FILE *f,long *offset,long fileend)
119 if (catdoc_read(&buf, 1, 1, f)==0) return EOF;
121 return to_unicode(source_charset,buf);
125 /************************************************************************/
126 /* Reads 16-bit unicode value. MS-Word runs on LSB-first machine only, */
127 /* so read lsb first always and don't care about proper bit order */
128 /************************************************************************/
130 int get_utf16lsb (FILE *f,long *offset,long fileend) {
131 unsigned char buf[2];
133 result=catdoc_read(buf, 1, 2, f);
142 return ((int)buf[1])|(((int)buf[0])<<8);
145 /************************************************************************/
146 /* Reads 16-bit unicode value written in MSB order. For processing
147 * non-word files . */
148 /************************************************************************/
149 int get_utf16msb (FILE *f,long *offset,long fileend) {
150 unsigned char buf[2];
152 result=catdoc_read(buf, 1, 2, f);
161 return ((int)buf[0])|(((int)buf[1])<<8);
164 int get_utf8 (FILE *f,long *offset,long fileend) {
165 unsigned char buf[3];
168 result=catdoc_read(buf, 1, 1, f);
173 if (result==0) return EOF;
179 return 0xfeff; /*skip corrupted sequebces*/
181 if (catdoc_read(buf+1, 1, 1, f)<=0) return EOF;
182 return ((c & 0x1F)<<6 | ((char)buf[1] & 0x3F));
185 if (catdoc_read(buf+1, 1, 2, f)<=2) return (int)EOF;
186 return ((c & 0x0F)<<12)|
187 ((buf[1] & 0x3f)<<6)|
193 /**************************************************************************/
194 /* Converts unicode char to output charset sequence. Coversion have */
195 /* three steps: 1. Replacement map is searched for the character in case */
196 /* it is not allowed for output format (% in TeX, < in HTML */
197 /* 2. target charset is searched for this unicode char, if it wasn't */
198 /* replaced. If not found, then 3. Substitution map is searched */
199 /**************************************************************************/
200 char *convert_char(int uc) {
201 static char plain_char[]="a"; /*placeholder for one-char sequences */
202 static char hexbuf[8];
205 if ((mapped=map_subst(spec_chars,uc))) return mapped;
206 if (target_charset) {
207 c =from_unicode(target_charset,uc);
212 if ((mapped = map_subst(replacements,uc))) return mapped;
213 if (unknown_as_hex) {
214 sprintf(hexbuf,"\\x%04X",(unsigned)uc);
215 /* This sprintf is safe, becouse uc is unicode character code,
216 which cannot be greater than 0xFFFE. It is ensured by routines
223 /* NULL target charset means UTF-8 output */
227 /******************************************************************/
228 /* Converts given unicode character to the utf-8 sequence */
229 /* in the static string buffer. Buffer wouldbe overwritten upon */
231 /******************************************************************/
232 char *to_utf8(unsigned int uc) {
233 static char utfbuffer[4]; /* it shouldn't overflow becouse we never deal
234 with chars greater than 65535*/
241 utfbuffer[count++]=0xC0 | (uc >> 6);
243 utfbuffer[count++]=0xE0 | (uc >>12);
244 utfbuffer[count++]=0x80 | ((uc >>6) &0x3F);
246 utfbuffer[count++]=0x80 | (uc & 0x3F);
257 struct cp_map cp_to_charset [] = {
259 {10001,"mac-japanese"},
260 {10002,"mac-tchinese"},
261 {10003,"mac-korean"},
262 {10004,"mac-arabic"},
263 {10005,"mac-hebrew"},
264 {10006,"mac-greek1"},
265 {10007,"mac-cyrillic"},
266 {10008,"mac-schinese"},
267 {10010,"mac-romania"},
268 {10017,"mac-ukraine"},
270 {10029,"mac-centeuro"},
271 {10079,"mac-iselandic"},
272 {10081,"mac-turkish"},
273 {10082,"mac-croatia"},
287 const char *charset_from_codepage(unsigned int codepage) {
289 static char buffer[7];
291 if (codepage==1200||codepage==1201) {
295 if (codepage<10000) {
296 sprintf(buffer,"cp%d",codepage);
299 for (cp = cp_to_charset;cp->codepage!=0&& cp->codepage!=codepage;cp++);
300 return cp->charset_name;