Debian Codegraph @

texk/dvipdfm-x/cmap_read.c

raw
1 /* This is dvipdfmx, an eXtended version of dvipdfm by Mark A. Wicks.
2
3 Copyright (C) 2002-2021 by Jin-Hwan Cho and Shunsaku Hirata,
4 the dvipdfmx project team.
5
6 Copyright (C) 1998, 1999 by Mark A. Wicks <[email protected]>
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <string.h>
28
29 #include "system.h"
30 #include "mem.h"
31 #include "error.h"
32
33 #include "dpxconf.h"
34 #include "dpxutil.h"
35 #include "pst.h"
36
37 #include "cmap_p.h"
38 #include "cmap.h"
39
40 #include "cmap_read.h"
41
42 #define CMAP_PARSE_DEBUG_STR "CMap_parse:"
43 #define CMAP_PARSE_DEBUG 3
44
45 #ifdef HAVE_DPXFILE
46 #include "dpxfile.h"
47 #else
48 #include "mfileio.h"
49
50 typedef struct {
51 unsigned char *cursor;
52 unsigned char *endptr;
53
54 unsigned char *buf;
55 size_t max;
56 FILE *fp;
57 size_t unread;
58 } ifreader;
59
60 static ifreader *ifreader_create (FILE *fp, size_t remain, size_t bufsize);
61 static size_t ifreader_read (ifreader *reader, size_t size);
62 static void ifreader_destroy (ifreader *reader);
63
64 static ifreader *
65 ifreader_create (FILE *fp, size_t size, size_t bufsize)
66 {
67 ifreader *reader;
68
69 reader = NEW(1, ifreader);
70 reader->buf = NEW(bufsize+1, unsigned char);
71 reader->max = bufsize;
72 reader->fp = fp;
73 reader->unread = size;
74
75 reader->cursor = reader->endptr = reader->buf;
76 *reader->endptr = 0;
77
78 return reader;
79 }
80
81 static void
82 ifreader_destroy (ifreader *reader)
83 {
84 ASSERT(reader);
85 if (reader->buf)
86 RELEASE(reader->buf);
87 RELEASE(reader);
88 }
89
90
91 static size_t
92 ifreader_read (ifreader *reader, size_t size)
93 {
94 size_t bytesread = 0, bytesrem = 0;
95
96 ASSERT(reader);
97 bytesrem = (size_t) reader->endptr - (size_t) reader->cursor;
98 if (size > reader->max) {
99 reader->buf = RENEW(reader->buf, size+1, unsigned char);
100 reader->max = size;
101 }
102 if (reader->unread > 0 && bytesrem < size) {
103 bytesread = MIN(reader->max - bytesrem, reader->unread);
104 memmove(reader->buf, reader->cursor, bytesrem);
105 reader->cursor = reader->buf;
106 reader->endptr = reader->buf + bytesrem;
107 if (fread(reader->endptr, 1, bytesread, reader->fp) != bytesread)
108 ERROR("Reading file failed.");
109 reader->endptr += bytesread;
110 reader->unread -= bytesread;
111 }
112
113 *reader->endptr = 0;
114
115 return bytesread + bytesrem;
116 }
117 #define ifreader_need(f,s) ifreader_read((f),(s))
118
119 #endif /* HAVE_DPXFILE */
120
121 static int check_next_token (ifreader *input, const char *key);
122 static int get_coderange (ifreader *input, unsigned char *codeLo, unsigned char *codeHi, int *dim, int maxlen);
123
124 static int handle_codearray (CMap *cmap, ifreader *input, unsigned char *codeLo, int dim, int count);
125 static int do_codespacerange (CMap *cmap, ifreader *input, int count);
126 static int do_notdefrange (CMap *cmap, ifreader *input, int count);
127 static int do_bfrange (CMap *cmap, ifreader *input, int count);
128 static int do_cidrange (CMap *cmap, ifreader *input, int count);
129 static int do_notdefchar (CMap *cmap, ifreader *input, int count);
130 static int do_bfchar (CMap *cmap, ifreader *input, int count);
131 static int do_cidchar (CMap *cmap, ifreader *input, int count);
132
133 #define TOKEN_LEN_MAX 127
134
135 static int
136 check_next_token (ifreader *input, const char *key)
137 {
138 int cmp;
139 pst_obj *token;
140 char *str;
141
142 if (ifreader_need(input, strlen(key)) == 0)
143 return -1;
144 if ((token = pst_get_token(&(input->cursor), input->endptr)) == NULL)
145 return -1;
146
147 str = (char *) pst_getSV(token);
148 cmp = strcmp(str, key) ? -1 : 0;
149 if (str)
150 RELEASE(str);
151 pst_release_obj(token);
152
153 return cmp;
154 }
155
156 static int
157 get_coderange (ifreader *input,
158 unsigned char *codeLo, unsigned char *codeHi, int *dim, int maxlen)
159 {
160 pst_obj *tok1, *tok2;
161 int dim1, dim2;
162
163 if ((tok1 = pst_get_token(&(input->cursor), input->endptr)) == NULL)
164 return -1;
165 if ((tok2 = pst_get_token(&(input->cursor), input->endptr)) == NULL) {
166 pst_release_obj(tok1);
167 return -1;
168 }
169
170 if (!PST_STRINGTYPE(tok1) || !PST_STRINGTYPE(tok2)) {
171 pst_release_obj(tok1);
172 pst_release_obj(tok2);
173 return -1;
174 }
175
176 dim1 = pst_length_of(tok1);
177 dim2 = pst_length_of(tok2);
178 if (dim1 != dim2 || dim1 > maxlen) {
179 pst_release_obj(tok1);
180 pst_release_obj(tok2);
181 return -1;
182 }
183
184 memcpy(codeLo, pst_data_ptr(tok1), dim1);
185 memcpy(codeHi, pst_data_ptr(tok2), dim2);
186 pst_release_obj(tok1);
187 pst_release_obj(tok2);
188
189 *dim = dim1;
190 return 0;
191 }
192
193 static int
194 do_codespacerange (CMap *cmap, ifreader *input, int count)
195 {
196 unsigned char codeLo[TOKEN_LEN_MAX], codeHi[TOKEN_LEN_MAX];
197 int dim;
198
199 while (count-- > 0) {
200 if (get_coderange(input, codeLo, codeHi, &dim, TOKEN_LEN_MAX) < 0)
201 return -1;
202 CMap_add_codespacerange(cmap, codeLo, codeHi, dim);
203 }
204
205 return check_next_token(input, "endcodespacerange");
206 }
207
208 /*
209 * bfrange
210 * <codeLo> <codeHi> [destCode1 destCode2 ...]
211 */
212 static int
213 handle_codearray (CMap *cmap, ifreader *input, unsigned char *codeLo, int dim, int count)
214 {
215 pst_obj *tok = NULL;
216
217 if (dim < 1)
218 ERROR("Invalid code range.");
219 while (count-- > 0) {
220 if ((tok = pst_get_token(&(input->cursor), input->endptr)) == NULL)
221 return -1;
222 else if (PST_STRINGTYPE(tok)) {
223 CMap_add_bfchar(cmap, codeLo, dim, (unsigned char *) pst_data_ptr(tok), pst_length_of(tok));
224 } else if (PST_MARKTYPE(tok) || !PST_NAMETYPE(tok))
225 ERROR("%s: Invalid CMap mapping record.", CMAP_PARSE_DEBUG_STR);
226 else
227 ERROR("%s: Mapping to charName not supported.", CMAP_PARSE_DEBUG_STR);
228 pst_release_obj(tok);
229 codeLo[dim-1] += 1;
230 }
231
232 return check_next_token(input, "]");
233 }
234
235 static int
236 do_notdefrange (CMap *cmap, ifreader *input, int count)
237 {
238 pst_obj *tok;
239 unsigned char codeLo[TOKEN_LEN_MAX], codeHi[TOKEN_LEN_MAX];
240 int dstCID;
241 int dim;
242
243 while (count-- > 0) {
244 if (ifreader_need(input, TOKEN_LEN_MAX*3) == 0)
245 return -1;
246 if (get_coderange(input, codeLo, codeHi, &dim, TOKEN_LEN_MAX) < 0 ||
247 (tok = pst_get_token(&(input->cursor), input->endptr)) == NULL)
248 return -1;
249 if (PST_INTEGERTYPE(tok)) {
250 dstCID = pst_getIV(tok);
251 if (dstCID >= 0 && dstCID <= CID_MAX)
252 CMap_add_notdefrange(cmap, codeLo, codeHi, dim, (CID) dstCID);
253 } else
254 WARN("%s: Invalid CMap mapping record. (ignored)", CMAP_PARSE_DEBUG_STR);
255 pst_release_obj(tok);
256 }
257
258 return check_next_token(input, "endnotdefrange");
259 }
260
261 static int
262 do_bfrange (CMap *cmap, ifreader *input, int count)
263 {
264 pst_obj *tok;
265 unsigned char codeLo[TOKEN_LEN_MAX], codeHi[TOKEN_LEN_MAX];
266 int srcdim;
267
268 while (count-- > 0) {
269 if (ifreader_need(input, TOKEN_LEN_MAX*3) == 0)
270 return -1;
271 if (get_coderange(input, codeLo, codeHi, &srcdim, TOKEN_LEN_MAX) < 0 ||
272 (tok = pst_get_token(&(input->cursor), input->endptr)) == NULL)
273 return -1;
274 if (PST_STRINGTYPE(tok)) {
275 CMap_add_bfrange(cmap, codeLo, codeHi, srcdim,
276 (unsigned char *) pst_data_ptr(tok), pst_length_of(tok));
277 } else if (PST_MARKTYPE(tok)) {
278 if (handle_codearray(cmap, input, codeLo, srcdim,
279 codeHi[srcdim-1] - codeLo[srcdim-1] + 1) < 0) {
280 pst_release_obj(tok);
281 return -1;
282 }
283 } else
284 WARN("%s: Invalid CMap mapping record. (ignored)", CMAP_PARSE_DEBUG_STR);
285 pst_release_obj(tok);
286 }
287
288 return check_next_token(input, "endbfrange");
289 }
290
291 static int
292 do_cidrange (CMap *cmap, ifreader *input, int count)
293 {
294 pst_obj *tok;
295 unsigned char codeLo[TOKEN_LEN_MAX], codeHi[TOKEN_LEN_MAX];
296 int dstCID;
297 int dim;
298
299 while (count-- > 0) {
300 if (ifreader_need(input, TOKEN_LEN_MAX*3) == 0)
301 return -1;
302 if (get_coderange(input, codeLo, codeHi, &dim, TOKEN_LEN_MAX) < 0 ||
303 (tok = pst_get_token(&(input->cursor), input->endptr)) == NULL)
304 return -1;
305 if (PST_INTEGERTYPE(tok)) {
306 dstCID = pst_getIV(tok);
307 if (dstCID >= 0 && dstCID <= CID_MAX)
308 CMap_add_cidrange(cmap, codeLo, codeHi, dim, (CID) dstCID);
309 } else
310 WARN("%s: Invalid CMap mapping record. (ignored)", CMAP_PARSE_DEBUG_STR);
311 pst_release_obj(tok);
312 }
313
314 return check_next_token(input, "endcidrange");
315 }
316
317 static int
318 do_notdefchar (CMap *cmap, ifreader *input, int count)
319 {
320 pst_obj *tok1, *tok2;
321 int dstCID;
322
323 while (count-- > 0) {
324 if (ifreader_need(input, TOKEN_LEN_MAX*2) == 0)
325 return -1;
326 if ((tok1 = pst_get_token(&(input->cursor), input->endptr)) == NULL)
327 return -1;
328 if ((tok2 = pst_get_token(&(input->cursor), input->endptr)) == NULL) {
329 pst_release_obj(tok1);
330 return -1;
331 }
332 if (PST_STRINGTYPE(tok1) && PST_INTEGERTYPE(tok2)) {
333 dstCID = pst_getIV(tok2);
334 if (dstCID >= 0 && dstCID <= CID_MAX)
335 CMap_add_notdefchar(cmap, pst_data_ptr(tok1), pst_length_of(tok1), (CID) dstCID);
336 } else
337 WARN("%s: Invalid CMap mapping record. (ignored)", CMAP_PARSE_DEBUG_STR);
338 pst_release_obj(tok1);
339 pst_release_obj(tok2);
340 }
341
342 return check_next_token(input, "endnotdefchar");
343 }
344
345 static int
346 do_bfchar (CMap *cmap, ifreader *input, int count)
347 {
348 pst_obj *tok1, *tok2;
349
350 while (count-- > 0) {
351 if (ifreader_need(input, TOKEN_LEN_MAX*2) == 0)
352 return -1;
353 if ((tok1 = pst_get_token(&(input->cursor), input->endptr)) == NULL)
354 return -1;
355 if ((tok2 = pst_get_token(&(input->cursor), input->endptr)) == NULL) {
356 pst_release_obj(tok1);
357 return -1;
358 }
359 /* We only support single CID font as descendant font, charName should not come here. */
360 if (PST_STRINGTYPE(tok1) && PST_STRINGTYPE(tok2)) {
361 CMap_add_bfchar(cmap,
362 (unsigned char *) pst_data_ptr(tok1), pst_length_of(tok1),
363 (unsigned char *) pst_data_ptr(tok2), pst_length_of(tok2));
364 } else if (PST_NAMETYPE(tok2))
365 ERROR("%s: Mapping to charName not supported.", CMAP_PARSE_DEBUG_STR);
366 else
367 WARN("%s: Invalid CMap mapping record. (ignored)", CMAP_PARSE_DEBUG_STR);
368 pst_release_obj(tok1);
369 pst_release_obj(tok2);
370 }
371
372 return check_next_token(input, "endbfchar");
373 }
374
375 static int
376 do_cidchar (CMap *cmap, ifreader *input, int count)
377 {
378 pst_obj *tok1, *tok2;
379 int dstCID;
380
381 while (count-- > 0) {
382 if (ifreader_need(input, TOKEN_LEN_MAX*2) == 0)
383 return -1;
384 if ((tok1 = pst_get_token(&(input->cursor), input->endptr)) == NULL)
385 return -1;
386 if ((tok2 = pst_get_token(&(input->cursor), input->endptr)) == NULL) {
387 pst_release_obj(tok1);
388 return -1;
389 }
390 if (PST_STRINGTYPE(tok1) && PST_INTEGERTYPE(tok2)) {
391 dstCID = pst_getIV(tok2);
392 if (dstCID >= 0 && dstCID <= CID_MAX)
393 CMap_add_cidchar(cmap, pst_data_ptr(tok1), pst_length_of(tok1), (CID) dstCID);
394 } else
395 WARN("%s: Invalid CMap mapping record. (ignored)", CMAP_PARSE_DEBUG_STR);
396 pst_release_obj(tok1);
397 pst_release_obj(tok2);
398 }
399
400 return check_next_token(input, "endcidchar");
401 }
402
403
404 #define MATCH_NAME(t,n) (PST_NAMETYPE((t)) && (pst_length_of((t)) == strlen((n))) && !memcmp(pst_data_ptr((t)),(n),strlen((n))))
405 #define MATCH_OP(t,n) (PST_UNKNOWNTYPE((t)) && (pst_length_of((t)) == strlen((n))) && !memcmp(pst_data_ptr((t)),(n),strlen((n))))
406
407 static int
408 do_cidsysteminfo (CMap *cmap, ifreader *input)
409 {
410 pst_obj *tok1, *tok2;
411 CIDSysInfo csi = {NULL, NULL, -1};
412 int simpledict = 0;
413 int error = 0;
414
415 ifreader_need(input, TOKEN_LEN_MAX*2);
416 /*
417 * Assuming /CIDSystemInfo 3 dict dup begin .... end def
418 * or /CIDSystemInfo << ... >> def
419 */
420 while ((tok1 = pst_get_token(&(input->cursor), input->endptr)) != NULL) {
421 if (PST_MARKTYPE(tok1)) {
422 simpledict = 1;
423 pst_release_obj(tok1);
424 break;
425 } else if (MATCH_OP(tok1, "begin")) {
426 simpledict = 0;
427 pst_release_obj(tok1);
428 break;
429 } else {
430 pst_release_obj(tok1);
431 /* continue */
432 }
433 }
434 tok1 = tok2 = NULL;
435 while (!error &&
436 (tok1 = pst_get_token(&(input->cursor), input->endptr)) != NULL) {
437 if (MATCH_OP(tok1, ">>") && simpledict) {
438 pst_release_obj(tok1);
439 break;
440 } else if (MATCH_OP(tok1, "end") && !simpledict) {
441 pst_release_obj(tok1);
442 break;
443 } else if (MATCH_NAME(tok1, "Registry") &&
444 (tok2 = pst_get_token(&(input->cursor), input->endptr)) != NULL) {
445 if (!PST_STRINGTYPE(tok2))
446 error = -1;
447 else if (!simpledict &&
448 check_next_token(input, "def"))
449 error = -1;
450 if (!error)
451 csi.registry = (char *) pst_getSV(tok2);
452 } else if (MATCH_NAME(tok1, "Ordering") &&
453 (tok2 = pst_get_token(&(input->cursor), input->endptr)) != NULL) {
454 if (!PST_STRINGTYPE(tok2))
455 error = -1;
456 else if (!simpledict &&
457 check_next_token(input, "def"))
458 error = -1;
459 if (!error)
460 csi.ordering = (char *) pst_getSV(tok2);
461 } else if (MATCH_NAME(tok1, "Supplement") &&
462 (tok2 = pst_get_token(&(input->cursor), input->endptr)) != NULL) {
463 if (!PST_INTEGERTYPE(tok2))
464 error = -1;
465 else if (!simpledict &&
466 check_next_token(input, "def"))
467 error = -1;
468 if (!error)
469 csi.supplement = pst_getIV(tok2);
470 }
471 if (tok2)
472 pst_release_obj(tok2);
473 if (tok1)
474 pst_release_obj(tok1);
475 tok1 = tok2 = NULL;
476 }
477 if (!error &&
478 check_next_token(input, "def"))
479 error = -1;
480
481 if (!error &&
482 csi.registry && csi.ordering &&
483 csi.supplement >= 0) {
484 CMap_set_CIDSysInfo(cmap, &csi);
485 }
486
487 if (csi.registry)
488 RELEASE(csi.registry);
489 if (csi.ordering)
490 RELEASE(csi.ordering);
491
492 return error;
493 }
494
495 #define INPUT_BUF_SIZE 4096
496 #define CMAP_SIG_MAX 64
497 int
498 CMap_parse_check_sig (FILE *fp)
499 {
500 int result = -1;
501 char sig[CMAP_SIG_MAX+1];
502
503 if (!fp)
504 return -1;
505
506 rewind(fp);
507 if (fread(sig, sizeof(char), CMAP_SIG_MAX, fp) != CMAP_SIG_MAX)
508 result = -1;
509 else {
510 sig[CMAP_SIG_MAX] = 0;
511 if (strncmp(sig, "%!PS", 4))
512 result = -1;
513 else if (strstr(sig+4, "Resource-CMap"))
514 result = 0;
515 }
516 rewind(fp);
517
518 return result;
519 }
520
521 int
522 CMap_parse (CMap *cmap, FILE *fp)
523 {
524 pst_obj *tok1, *tok2;
525 ifreader *input;
526 int status = 0, tmpint = -1;
527
528 ASSERT(cmap && fp);
529
530 input = ifreader_create(fp, file_size(fp), INPUT_BUF_SIZE-1);
531
532 while (status >= 0) {
533 tok1 = tok2 = NULL;
534 ifreader_read(input, INPUT_BUF_SIZE/2);
535 tok1 = pst_get_token(&(input->cursor), input->endptr);
536 if (tok1 == NULL)
537 break;
538 else if (MATCH_NAME(tok1, "CMapName")) {
539 if ((tok2 = pst_get_token(&(input->cursor), input->endptr)) == NULL ||
540 !(PST_NAMETYPE(tok2) || PST_STRINGTYPE(tok2)) ||
541 check_next_token(input, "def") < 0)
542 status = -1;
543 else
544 CMap_set_name(cmap, pst_data_ptr(tok2));
545 } else if (MATCH_NAME(tok1, "CMapType")) {
546 if ((tok2 = pst_get_token(&(input->cursor), input->endptr)) == NULL ||
547 !PST_INTEGERTYPE(tok2) ||
548 check_next_token(input, "def") < 0)
549 status = -1;
550 else
551 CMap_set_type(cmap, pst_getIV(tok2));
552 } else if (MATCH_NAME(tok1, "WMode")) {
553 if ((tok2 = pst_get_token(&(input->cursor), input->endptr)) == NULL ||
554 !PST_INTEGERTYPE(tok2) ||
555 check_next_token(input, "def") < 0)
556 status = -1;
557 else
558 CMap_set_wmode(cmap, pst_getIV(tok2));
559 } else if (MATCH_NAME(tok1, "CIDSystemInfo")) {
560 status = do_cidsysteminfo(cmap, input);
561 } else if (MATCH_NAME(tok1, "Version") ||
562 MATCH_NAME(tok1, "UIDOffset") ||
563 MATCH_NAME(tok1, "XUID")) {
564 /* Ignore */
565 } else if (PST_NAMETYPE(tok1)) {
566 /* Possibly usecmap comes next */
567 if ((tok2 = pst_get_token(&(input->cursor), input->endptr)) != NULL &&
568 MATCH_OP(tok2, "usecmap")) {
569 int id;
570 CMap *ucmap;
571 id = CMap_cache_find(pst_data_ptr(tok1));
572 if (id < 0)
573 status = -1;
574 else {
575 ucmap = CMap_cache_get(id);
576 CMap_set_usecmap(cmap, ucmap);
577 }
578 }
579 } else if (MATCH_OP(tok1, "begincodespacerange")) {
580 status = do_codespacerange(cmap, input, tmpint);
581 } else if (MATCH_OP(tok1, "beginnotdefrange")) {
582 status = do_notdefrange(cmap, input, tmpint);
583 } else if (MATCH_OP(tok1, "beginnotdefchar")) {
584 status = do_notdefchar(cmap, input, tmpint);
585 } else if (MATCH_OP(tok1, "beginbfrange")) {
586 status = do_bfrange(cmap, input, tmpint);
587 } else if (MATCH_OP(tok1, "beginbfchar")) {
588 status = do_bfchar(cmap, input, tmpint);
589 } else if (MATCH_OP(tok1, "begincidrange")) {
590 status = do_cidrange(cmap, input, tmpint);
591 } else if (MATCH_OP(tok1, "begincidchar")) {
592 status = do_cidchar(cmap, input, tmpint);
593 } else if (PST_INTEGERTYPE(tok1)) {
594 tmpint = pst_getIV(tok1);
595 } /* else Simply ignore */
596 if (tok1)
597 pst_release_obj(tok1);
598 if (tok2)
599 pst_release_obj(tok2);
600 }
601
602 ifreader_destroy(input);
603
604 return (status < 0) ? -1 : CMap_is_valid(cmap);
605 }