Coverage Report

Created: 2026-12-01 06:38

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/xpdf-4.06/xpdf/CMap.cc
Line
Count
Source
1
//========================================================================
2
//
3
// CMap.cc
4
//
5
// Copyright 2001-2003 Glyph & Cog, LLC
6
//
7
//========================================================================
8
9
#include <aconf.h>
10
11
#include <stdio.h>
12
#include <stdlib.h>
13
#include <string.h>
14
#include <ctype.h>
15
#include "gmem.h"
16
#include "gmempp.h"
17
#include "gfile.h"
18
#include "GString.h"
19
#include "GHash.h"
20
#include "Error.h"
21
#include "GlobalParams.h"
22
#include "PSTokenizer.h"
23
#include "Object.h"
24
#include "Stream.h"
25
#include "CMap.h"
26
27
//------------------------------------------------------------------------
28
29
struct CMapVectorEntry {
30
  GBool isVector;
31
  union {
32
    CMapVectorEntry *vector;
33
    CID cid;
34
  };
35
};
36
37
//------------------------------------------------------------------------
38
39
0
static int getCharFromFile(void *data) {
40
0
  return fgetc((FILE *)data);
41
0
}
42
43
702k
static int getCharFromStream(void *data) {
44
702k
  return ((Stream *)data)->getChar();
45
702k
}
46
47
//------------------------------------------------------------------------
48
49
CMap *CMap::parse(CMapCache *cache, GString *collectionA, Object *obj,
50
5.45k
      GHash *usedCMaps) {
51
5.45k
  CMap *cMap;
52
5.45k
  GString *cMapNameA;
53
54
5.45k
  if (obj->isName()) {
55
4.63k
    cMapNameA = new GString(obj->getName());
56
4.63k
    if (!(cMap = globalParams->getCMap(collectionA, cMapNameA))) {
57
230
      error(errSyntaxError, -1,
58
230
      "Unknown CMap '{0:t}' for character collection '{1:t}'",
59
230
      cMapNameA, collectionA);
60
230
    }
61
4.63k
    delete cMapNameA;
62
4.63k
  } else if (obj->isStream()) {
63
755
    GHash *newUsedCMaps = NULL;
64
755
    if (!usedCMaps) {
65
755
      usedCMaps = newUsedCMaps = new GHash(gTrue);
66
755
    }
67
755
    if (!(cMap = CMap::parse(NULL, collectionA, obj->getStream(),
68
755
           usedCMaps))) {
69
0
      error(errSyntaxError, -1, "Invalid CMap in Type 0 font");
70
0
    }
71
755
    if (newUsedCMaps) {
72
755
      delete newUsedCMaps;
73
755
    }
74
755
  } else {
75
61
    error(errSyntaxError, -1, "Invalid Encoding in Type 0 font");
76
61
    return NULL;
77
61
  }
78
5.39k
  return cMap;
79
5.45k
}
80
81
CMap *CMap::parse(CMapCache *cache, GString *collectionA,
82
3.45k
      GString *cMapNameA, GHash *usedCMaps) {
83
3.45k
  FILE *f;
84
3.45k
  CMap *cMap;
85
86
3.45k
  if (!(f = globalParams->findCMapFile(collectionA, cMapNameA))) {
87
88
    // Check for an identity CMap.
89
3.45k
    if (!cMapNameA->cmp("Identity") || !cMapNameA->cmp("Identity-H")) {
90
3.19k
      return new CMap(collectionA->copy(), cMapNameA->copy(), 0);
91
3.19k
    }
92
263
    if (!cMapNameA->cmp("Identity-V")) {
93
33
      return new CMap(collectionA->copy(), cMapNameA->copy(), 1);
94
33
    }
95
96
230
    error(errSyntaxError, -1,
97
230
    "Couldn't find '{0:t}' CMap file for '{1:t}' collection",
98
230
    cMapNameA, collectionA);
99
230
    return NULL;
100
263
  }
101
102
0
  GHash *newUsedCMaps = NULL;
103
0
  if (!usedCMaps) {
104
0
    usedCMaps = newUsedCMaps = new GHash(gTrue);
105
0
  }
106
107
0
  cMap = new CMap(collectionA->copy(), cMapNameA->copy());
108
0
  cMap->parse2(cache, &getCharFromFile, f, usedCMaps);
109
110
0
  if (newUsedCMaps) {
111
0
    delete newUsedCMaps;
112
0
  }
113
114
0
  fclose(f);
115
116
0
  return cMap;
117
3.45k
}
118
119
CMap *CMap::parse(CMapCache *cache, GString *collectionA, Stream *str,
120
755
      GHash *usedCMaps) {
121
755
  Object obj1;
122
755
  CMap *cMap;
123
124
  // check for a loop
125
755
  if (usedCMaps) {
126
755
    GString *name;
127
755
    if (str->getDict()->lookup("CMapName", &obj1)->isName()) {
128
43
      name = new GString(obj1.getName());
129
712
    } else {
130
712
      name = new GString();
131
712
    }
132
755
    obj1.free();
133
755
    if (usedCMaps->lookupInt(name)) {
134
0
      error(errSyntaxError, -1, "Loop in usecmap");
135
0
      delete name;
136
0
      return NULL;
137
0
    }
138
755
    usedCMaps->add(name, 1);
139
755
  }
140
141
755
  cMap = new CMap(collectionA->copy(), NULL);
142
143
755
  if (!str->getDict()->lookup("UseCMap", &obj1)->isNull()) {
144
0
    cMap->useCMap(cache, &obj1, usedCMaps);
145
0
  }
146
755
  obj1.free();
147
148
755
  str->reset();
149
755
  cMap->parse2(cache, &getCharFromStream, str, usedCMaps);
150
755
  str->close();
151
755
  return cMap;
152
755
}
153
154
void CMap::parse2(CMapCache *cache, int (*getCharFunc)(void *), void *data,
155
755
      GHash *usedCMaps) {
156
755
  PSTokenizer *pst;
157
755
  char tok1[256], tok2[256], tok3[256];
158
755
  int n1, n2, n3;
159
755
  Guint start, end, code;
160
161
755
  pst = new PSTokenizer(getCharFunc, data);
162
755
  pst->getToken(tok1, sizeof(tok1), &n1);
163
34.8k
  while (pst->getToken(tok2, sizeof(tok2), &n2)) {
164
34.1k
    if (!strcmp(tok2, "usecmap")) {
165
0
      if (tok1[0] == '/') {
166
0
  useCMap(cache, tok1 + 1, usedCMaps);
167
0
      }
168
0
      pst->getToken(tok1, sizeof(tok1), &n1);
169
34.1k
    } else if (!strcmp(tok1, "/WMode")) {
170
224
      wMode = atoi(tok2);
171
224
      pst->getToken(tok1, sizeof(tok1), &n1);
172
33.9k
    } else if (!strcmp(tok2, "begincidchar")) {
173
6.53k
      while (pst->getToken(tok1, sizeof(tok1), &n1)) {
174
6.51k
  if (!strcmp(tok1, "endcidchar")) {
175
8
    break;
176
8
  }
177
6.51k
  if (!pst->getToken(tok2, sizeof(tok2), &n2) ||
178
6.48k
      !strcmp(tok2, "endcidchar")) {
179
38
    error(errSyntaxError, -1, "Illegal entry in cidchar block in CMap");
180
38
    break;
181
38
  }
182
6.47k
  if (!(tok1[0] == '<' && tok1[n1 - 1] == '>' &&
183
4.13k
        n1 >= 4 && (n1 & 1) == 0)) {
184
2.55k
    error(errSyntaxError, -1, "Illegal entry in cidchar block in CMap");
185
2.55k
    continue;
186
2.55k
  }
187
3.91k
  tok1[n1 - 1] = '\0';
188
3.91k
  if (sscanf(tok1 + 1, "%x", &code) != 1) {
189
78
    error(errSyntaxError, -1, "Illegal entry in cidchar block in CMap");
190
78
    continue;
191
78
  }
192
3.83k
  n1 = (n1 - 2) / 2;
193
3.83k
  addCIDs(code, code, n1, (CID)atoi(tok2));
194
3.83k
      }
195
63
      pst->getToken(tok1, sizeof(tok1), &n1);
196
33.8k
    } else if (!strcmp(tok2, "begincidrange")) {
197
8.73k
      while (pst->getToken(tok1, sizeof(tok1), &n1)) {
198
8.72k
  if (!strcmp(tok1, "endcidrange")) {
199
42
    break;
200
42
  }
201
8.68k
  if (!pst->getToken(tok2, sizeof(tok2), &n2) ||
202
8.67k
      !strcmp(tok2, "endcidrange") ||
203
8.64k
      !pst->getToken(tok3, sizeof(tok3), &n3) ||
204
8.62k
      !strcmp(tok3, "endcidrange")) {
205
80
    error(errSyntaxError, -1, "Illegal entry in cidrange block in CMap");
206
80
    break;
207
80
  }
208
8.60k
  if (tok1[0] == '<' && tok2[0] == '<' &&
209
6.09k
      n1 == n2 && n1 >= 4 && (n1 & 1) == 0) {
210
4.72k
    tok1[n1 - 1] = tok2[n1 - 1] = '\0';
211
4.72k
    sscanf(tok1 + 1, "%x", &start);
212
4.72k
    sscanf(tok2 + 1, "%x", &end);
213
4.72k
    n1 = (n1 - 2) / 2;
214
4.72k
    addCIDs(start, end, n1, (CID)atoi(tok3));
215
4.72k
  }
216
8.60k
      }
217
137
      pst->getToken(tok1, sizeof(tok1), &n1);
218
33.7k
    } else {
219
33.7k
      strcpy(tok1, tok2);
220
33.7k
    }
221
34.1k
  }
222
755
  delete pst;
223
755
}
224
225
755
CMap::CMap(GString *collectionA, GString *cMapNameA) {
226
755
  int i;
227
228
755
  collection = collectionA;
229
755
  cMapName = cMapNameA;
230
755
  isIdent = gFalse;
231
755
  wMode = 0;
232
755
  vector = (CMapVectorEntry *)gmallocn(256, sizeof(CMapVectorEntry));
233
194k
  for (i = 0; i < 256; ++i) {
234
193k
    vector[i].isVector = gFalse;
235
193k
    vector[i].cid = 0;
236
193k
  }
237
755
  refCnt = 1;
238
755
}
239
240
3.22k
CMap::CMap(GString *collectionA, GString *cMapNameA, int wModeA) {
241
3.22k
  collection = collectionA;
242
3.22k
  cMapName = cMapNameA;
243
3.22k
  isIdent = gTrue;
244
3.22k
  wMode = wModeA;
245
3.22k
  vector = NULL;
246
3.22k
  refCnt = 1;
247
3.22k
}
248
249
0
void CMap::useCMap(CMapCache *cache, char *useName, GHash *usedCMaps) {
250
0
  GString *useNameStr;
251
0
  CMap *subCMap;
252
253
  // check for a loop
254
0
  if (usedCMaps) {
255
0
    if (usedCMaps->lookupInt(useName)) {
256
0
      error(errSyntaxError, -1, "Loop in usecmap");
257
0
      return;
258
0
    }
259
0
    usedCMaps->add(new GString(useName), 1);
260
0
  }
261
262
0
  useNameStr = new GString(useName);
263
  // if cache is non-NULL, we already have a lock, and we can use
264
  // CMapCache::getCMap() directly; otherwise, we need to use
265
  // GlobalParams::getCMap() in order to acqure the lock
266
0
  if (cache) {
267
0
    subCMap = cache->getCMap(collection, useNameStr, usedCMaps);
268
0
  } else {
269
0
    subCMap = globalParams->getCMap(collection, useNameStr);
270
0
  }
271
0
  delete useNameStr;
272
0
  if (!subCMap) {
273
0
    return;
274
0
  }
275
0
  isIdent = subCMap->isIdent;
276
0
  if (subCMap->vector) {
277
0
    copyVector(vector, subCMap->vector);
278
0
  }
279
0
  subCMap->decRefCnt();
280
0
}
281
282
0
void CMap::useCMap(CMapCache *cache, Object *obj, GHash *usedCMaps) {
283
0
  CMap *subCMap;
284
285
0
  subCMap = CMap::parse(cache, collection, obj, usedCMaps);
286
0
  if (!subCMap) {
287
0
    return;
288
0
  }
289
0
  isIdent = subCMap->isIdent;
290
0
  if (subCMap->vector) {
291
0
    copyVector(vector, subCMap->vector);
292
0
  }
293
0
  subCMap->decRefCnt();
294
0
}
295
296
0
void CMap::copyVector(CMapVectorEntry *dest, CMapVectorEntry *src) {
297
0
  int i, j;
298
299
0
  for (i = 0; i < 256; ++i) {
300
0
    if (src[i].isVector) {
301
0
      if (!dest[i].isVector) {
302
0
  dest[i].isVector = gTrue;
303
0
  dest[i].vector =
304
0
    (CMapVectorEntry *)gmallocn(256, sizeof(CMapVectorEntry));
305
0
  for (j = 0; j < 256; ++j) {
306
0
    dest[i].vector[j].isVector = gFalse;
307
0
    dest[i].vector[j].cid = 0;
308
0
  }
309
0
      }
310
0
      copyVector(dest[i].vector, src[i].vector);
311
0
    } else {
312
0
      if (dest[i].isVector) {
313
0
  error(errSyntaxError, -1, "Collision in usecmap");
314
0
      } else {
315
0
  dest[i].cid = src[i].cid;
316
0
      }
317
0
    }
318
0
  }
319
0
}
320
321
8.56k
void CMap::addCIDs(Guint start, Guint end, Guint nBytes, CID firstCID) {
322
8.56k
  CMapVectorEntry *vec;
323
8.56k
  int byte, byte0, byte1;
324
8.56k
  Guint start1, end1, i, j, k;
325
326
8.56k
  start1 = start & 0xffffff00;
327
8.56k
  end1 = end & 0xffffff00;
328
87.3k
  for (i = start1; i <= end1; i += 0x100) {
329
78.7k
    vec = vector;
330
223k
    for (j = nBytes - 1; j >= 1; --j) {
331
144k
      byte = (i >> (8 * j)) & 0xff;
332
144k
      if (!vec[byte].isVector) {
333
70.0k
  vec[byte].isVector = gTrue;
334
70.0k
  vec[byte].vector =
335
70.0k
      (CMapVectorEntry *)gmallocn(256, sizeof(CMapVectorEntry));
336
17.9M
  for (k = 0; k < 256; ++k) {
337
17.9M
    vec[byte].vector[k].isVector = gFalse;
338
17.9M
    vec[byte].vector[k].cid = 0;
339
17.9M
  }
340
70.0k
      }
341
144k
      vec = vec[byte].vector;
342
144k
    }
343
78.7k
    byte0 = (i < start) ? (start & 0xff) : 0;
344
78.7k
    byte1 = (i + 0xff > end) ? (end & 0xff) : 0xff;
345
18.1M
    for (byte = byte0; byte <= byte1; ++byte) {
346
18.0M
      if (vec[byte].isVector) {
347
5.17k
  error(errSyntaxError, -1, "Invalid CID ({0:x} [{1:d} bytes]) in CMap",
348
5.17k
        i, nBytes);
349
18.0M
      } else {
350
18.0M
  vec[byte].cid = firstCID + ((i + byte) - start);
351
18.0M
      }
352
18.0M
    }
353
78.7k
  }
354
8.56k
}
355
356
3.98k
CMap::~CMap() {
357
3.98k
  delete collection;
358
3.98k
  if (cMapName) {
359
3.22k
    delete cMapName;
360
3.22k
  }
361
3.98k
  if (vector) {
362
755
    freeCMapVector(vector);
363
755
  }
364
3.98k
}
365
366
70.7k
void CMap::freeCMapVector(CMapVectorEntry *vec) {
367
70.7k
  int i;
368
369
18.1M
  for (i = 0; i < 256; ++i) {
370
18.1M
    if (vec[i].isVector) {
371
70.0k
      freeCMapVector(vec[i].vector);
372
70.0k
    }
373
18.1M
  }
374
70.7k
  gfree(vec);
375
70.7k
}
376
377
4.40k
void CMap::incRefCnt() {
378
4.40k
#if MULTITHREADED
379
4.40k
  gAtomicIncrement(&refCnt);
380
#else
381
  ++refCnt;
382
#endif
383
4.40k
}
384
385
8.38k
void CMap::decRefCnt() {
386
8.38k
  GBool done;
387
388
8.38k
#if MULTITHREADED
389
8.38k
  done = gAtomicDecrement(&refCnt) == 0;
390
#else
391
  done = --refCnt == 0;
392
#endif
393
8.38k
  if (done) {
394
3.98k
    delete this;
395
3.98k
  }
396
8.38k
}
397
398
1.19k
GBool CMap::match(GString *collectionA, GString *cMapNameA) {
399
1.19k
  return !collection->cmp(collectionA) && !cMapName->cmp(cMapNameA);
400
1.19k
}
401
402
263k
CID CMap::getCID(char *s, int len, CharCode *c, int *nUsed) {
403
263k
  CMapVectorEntry *vec;
404
263k
  CharCode cc;
405
263k
  int n, i;
406
407
263k
  vec = vector;
408
263k
  cc = 0;
409
263k
  n = 0;
410
264k
  while (vec && n < len) {
411
3.43k
    i = s[n++] & 0xff;
412
3.43k
    cc = (cc << 8) | i;
413
3.43k
    if (!vec[i].isVector) {
414
2.43k
      *c = cc;
415
2.43k
      *nUsed = n;
416
2.43k
      return vec[i].cid;
417
2.43k
    }
418
998
    vec = vec[i].vector;
419
998
  }
420
261k
  if (isIdent && len >= 2) {
421
    // identity CMap
422
249k
    *nUsed = 2;
423
249k
    *c = cc = ((s[0] & 0xff) << 8) + (s[1] & 0xff);
424
249k
    return cc;
425
249k
  }
426
12.2k
  *nUsed = 1;
427
12.2k
  *c = s[0] & 0xff;
428
12.2k
  return 0;
429
261k
}
430
431
//------------------------------------------------------------------------
432
433
19.3k
CMapCache::CMapCache() {
434
19.3k
  int i;
435
436
96.5k
  for (i = 0; i < cMapCacheSize; ++i) {
437
77.2k
    cache[i] = NULL;
438
77.2k
  }
439
19.3k
}
440
441
19.3k
CMapCache::~CMapCache() {
442
19.3k
  int i;
443
444
96.5k
  for (i = 0; i < cMapCacheSize; ++i) {
445
77.2k
    if (cache[i]) {
446
3.22k
      cache[i]->decRefCnt();
447
3.22k
    }
448
77.2k
  }
449
19.3k
}
450
451
CMap *CMapCache::getCMap(GString *collection, GString *cMapName,
452
4.63k
       GHash *usedCMaps) {
453
4.63k
  CMap *cmap;
454
4.63k
  int i, j;
455
456
4.63k
  if (cache[0] && cache[0]->match(collection, cMapName)) {
457
1.17k
    cache[0]->incRefCnt();
458
1.17k
    return cache[0];
459
1.17k
  }
460
13.8k
  for (i = 1; i < cMapCacheSize; ++i) {
461
10.3k
    if (cache[i] && cache[i]->match(collection, cMapName)) {
462
2
      cmap = cache[i];
463
4
      for (j = i; j >= 1; --j) {
464
2
  cache[j] = cache[j - 1];
465
2
      }
466
2
      cache[0] = cmap;
467
2
      cmap->incRefCnt();
468
2
      return cmap;
469
2
    }
470
10.3k
  }
471
3.45k
  if ((cmap = CMap::parse(this, collection, cMapName, usedCMaps))) {
472
3.22k
    if (cache[cMapCacheSize - 1]) {
473
0
      cache[cMapCacheSize - 1]->decRefCnt();
474
0
    }
475
12.9k
    for (j = cMapCacheSize - 1; j >= 1; --j) {
476
9.67k
      cache[j] = cache[j - 1];
477
9.67k
    }
478
3.22k
    cache[0] = cmap;
479
3.22k
    cmap->incRefCnt();
480
3.22k
    return cmap;
481
3.22k
  }
482
230
  return NULL;
483
3.45k
}