Fix OTS warning about `maxp.maxSizeOfInstructions`.
[ttfautohint.git] / lib / taname.c
blobcc328c263718bae9741bc2a29cb588c23f292045
1 /* taname.c */
3 /*
4 * Copyright (C) 2012-2022 by Werner Lemberg.
6 * This file is part of the ttfautohint library, and may only be used,
7 * modified, and distributed under the terms given in `COPYING'. By
8 * continuing to use, modify, or distribute this file you indicate that you
9 * have read `COPYING' and understand and accept it fully.
11 * The file `COPYING' mentioned in the previous paragraph is distributed
12 * with the ttfautohint library.
16 #include <string.h>
17 #include "ta.h"
20 typedef struct Lang_Tag_Record_
22 FT_UShort len;
23 FT_Byte* str;
24 } Lang_Tag_Record;
26 typedef struct Name_Record_
28 FT_UShort platform_id;
29 FT_UShort encoding_id;
30 FT_UShort language_id;
31 FT_UShort name_id;
33 FT_UShort len;
34 FT_Byte* str;
35 } Name_Record;
37 typedef struct Naming_Table_
39 FT_UShort format;
40 FT_UShort string_offset;
42 FT_UShort name_count;
43 Name_Record* name_records;
45 FT_UShort lang_tag_count;
46 Lang_Tag_Record* lang_tag_records;
47 } Naming_Table;
50 static FT_Error
51 parse_name_header(FT_Byte** curp,
52 Naming_Table* n,
53 FT_ULong buf_len,
54 FT_Byte** start,
55 FT_Byte** end)
57 FT_Byte* buf = *curp;
58 FT_Byte* p;
59 FT_Byte* startp;
60 FT_Byte* endp;
63 p = *curp;
65 n->format = NEXT_USHORT(p);
66 n->name_count = NEXT_USHORT(p);
67 n->string_offset = NEXT_USHORT(p);
69 n->name_records = NULL;
70 n->lang_tag_records = NULL;
72 /* all name strings must be between `startp' and `endp' */
73 startp = buf + 6 + 12 * n->name_count;
74 endp = buf + buf_len;
76 /* format 1 also has language tag records */
77 if (n->format == 1)
79 FT_Byte* q;
82 q = startp;
83 if (q + 2 > endp)
84 return FT_Err_Invalid_Table;
86 n->lang_tag_count = NEXT_USHORT(q);
88 startp += 2 + 4 * n->lang_tag_count;
90 else
91 n->lang_tag_count = 0;
93 if (startp > endp)
94 return FT_Err_Invalid_Table;
96 *start = startp;
97 *end = endp;
98 *curp = p;
100 return TA_Err_Ok;
104 static FT_Error
105 parse_name_records(FT_Byte** curp,
106 Naming_Table* n,
107 FT_Byte* buf,
108 FT_Byte* startp,
109 FT_Byte* endp,
110 FONT* font)
112 FT_UShort i, count;
113 FT_Byte* p;
116 p = *curp;
118 if (!n->name_count)
119 return TA_Err_Ok;
121 /* allocate name records array */
122 n->name_records = (Name_Record*)calloc(1, n->name_count
123 * sizeof (Name_Record));
124 if (!n->name_records)
125 return FT_Err_Out_Of_Memory;
127 /* walk over all name records */
128 count = 0;
129 for (i = 0; i < n->name_count; i++)
131 Name_Record* r = &n->name_records[count];
133 FT_UShort offset;
134 FT_Byte* s;
135 FT_UShort l;
138 r->platform_id = NEXT_USHORT(p);
139 r->encoding_id = NEXT_USHORT(p);
140 r->language_id = NEXT_USHORT(p);
141 r->name_id = NEXT_USHORT(p);
143 r->len = NEXT_USHORT(p);
145 offset = NEXT_USHORT(p);
147 s = buf + n->string_offset + offset;
149 /* ignore invalid entries */
150 if (s < startp || s + r->len > endp)
151 continue;
153 /* mac encoding or non-Unicode Windows encoding? */
154 if (r->platform_id == 1
155 || (r->platform_id == 3 && !(r->encoding_id == 1
156 || r->encoding_id == 10)))
158 /* one-byte or multi-byte encodings */
160 /* skip everything after a NULL byte (if any) */
161 for (l = 0; l < r->len; l++)
162 if (!s[l])
163 break;
165 /* ignore zero-length entries */
166 if (!l)
167 continue;
169 r->len = l;
171 else
173 /* (two-byte) UTF-16BE for everything else */
175 /* we need an even number of bytes */
176 r->len &= ~1U;
178 /* ignore entries which contain only NULL bytes */
179 for (l = 0; l < r->len; l++)
180 if (s[l])
181 break;
182 if (l == r->len)
183 continue;
186 /* this string gets probably modified */
187 /* by the `info' or `info_post' callbacks, */
188 /* so we have to call the allocation function provided by the user */
189 r->str = (FT_Byte*)font->allocate(r->len);
190 if (!r->str)
191 return FT_Err_Out_Of_Memory;
192 memcpy(r->str, s, r->len);
194 if (font->info)
196 /* we ignore the return value of `font->info' */
197 font->info(r->platform_id,
198 r->encoding_id,
199 r->language_id,
200 r->name_id,
201 &r->len,
202 &r->str,
203 font->info_data);
206 count++;
209 /* let the user modify `name' table entries */
210 if (font->info && font->info_post)
211 /* we ignore the return value of `font->info_post' */
212 font->info_post(font->info_data);
214 /* shrink name record array if necessary */
215 n->name_records = (Name_Record*)realloc(n->name_records,
216 count * sizeof (Name_Record));
217 n->name_count = count;
219 return TA_Err_Ok;
223 static FT_Error
224 parse_lang_tag_records(FT_Byte** curp,
225 Naming_Table* n,
226 FT_Byte* buf,
227 FT_Byte* startp,
228 FT_Byte* endp)
230 FT_UShort i;
231 FT_Byte* p;
234 p = *curp;
236 if (!n->lang_tag_count)
237 return TA_Err_Ok;
239 /* allocate language tags array */
240 n->lang_tag_records = (Lang_Tag_Record*)calloc(
241 1, n->lang_tag_count * sizeof (Lang_Tag_Record));
242 if (!n->lang_tag_records)
243 return FT_Err_Out_Of_Memory;
245 /* walk over all language tag records (if any) */
246 for (i = 0; i < n->lang_tag_count; i++)
248 Lang_Tag_Record* r = &n->lang_tag_records[i];
250 FT_UShort offset;
251 FT_Byte* s;
254 r->len = NEXT_USHORT(p);
256 offset = NEXT_USHORT(p);
258 s = buf + n->string_offset + offset;
260 /* ignore an invalid entry -- */
261 /* contrary to name records, we can't simply remove it */
262 /* because references to it should still work */
263 /* (we don't apply more fixes */
264 /* since ttfautohint is not a TrueType sanitizing tool) */
265 if (s < startp || s + r->len > endp)
266 continue;
268 /* we don't massage the data since we only make a copy */
269 r->str = (FT_Byte*)malloc(r->len);
270 if (!r->str)
271 return FT_Err_Out_Of_Memory;
273 memcpy(r->str, s, r->len);
276 return TA_Err_Ok;
280 /* we build a non-optimized `name' table, this is, */
281 /* we don't fold strings `foo' and `foobar' into one string */
283 static FT_Error
284 build_name_table(Naming_Table* n,
285 SFNT_Table* name_table)
287 FT_Byte* buf_new;
288 FT_Byte* buf_new_resized;
289 FT_ULong buf_new_len;
290 FT_ULong len;
292 FT_Byte* p;
293 FT_Byte* q;
294 FT_Byte* base;
296 FT_UShort i;
297 FT_UShort string_offset;
298 FT_ULong data_offset;
299 FT_ULong data_len;
302 /* we reallocate the array to its real size later on */
303 buf_new_len= 6 + 12 * n->name_count;
304 if (n->format == 1)
305 buf_new_len += 2 + 4 * n->lang_tag_count;
307 buf_new = (FT_Byte*)malloc(buf_new_len);
308 if (!buf_new)
309 return FT_Err_Out_Of_Memory;
311 /* note that the OpenType specification says that `string_offset' is the */
312 /* `offset to the start of string storage (from start of table)', */
313 /* but this isn't enforced by the major rendering engines */
314 /* as long as the final string offsets are valid */
315 string_offset = (buf_new_len <= 0xFFFF) ? (FT_UShort)buf_new_len : 0xFFFF;
317 p = buf_new;
319 *(p++) = HIGH(n->format);
320 *(p++) = LOW(n->format);
321 *(p++) = HIGH(n->name_count);
322 *(p++) = LOW(n->name_count);
323 *(p++) = HIGH(string_offset);
324 *(p++) = LOW(string_offset);
326 /* first pass */
328 data_len = 0;
329 for (i = 0; i < n->name_count; i++)
331 Name_Record* r = &n->name_records[i];
334 *(p++) = HIGH(r->platform_id);
335 *(p++) = LOW(r->platform_id);
336 *(p++) = HIGH(r->encoding_id);
337 *(p++) = LOW(r->encoding_id);
338 *(p++) = HIGH(r->language_id);
339 *(p++) = LOW(r->language_id);
340 *(p++) = HIGH(r->name_id);
341 *(p++) = LOW(r->name_id);
343 *(p++) = HIGH(r->len);
344 *(p++) = LOW(r->len);
346 /* the offset field gets filled in later */
347 p += 2;
349 data_len += r->len;
352 if (n->format == 1)
354 *(p++) = HIGH(n->lang_tag_count);
355 *(p++) = LOW(n->lang_tag_count);
357 for (i = 0; i < n->lang_tag_count; i++)
359 Lang_Tag_Record* r = &n->lang_tag_records[i];
362 *(p++) = HIGH(r->len);
363 *(p++) = LOW(r->len);
365 /* the offset field gets filled in later */
366 p += 2;
368 data_len += r->len;
372 if (buf_new_len + data_len > 2 * 0xFFFF)
374 /* the table would become too large, so we do nothing */
375 free(buf_new);
376 return TA_Err_Ok;
379 data_offset = buf_new_len;
381 /* reallocate the buffer to fit its real size */
382 buf_new_len += data_len;
383 /* make the allocated buffer length a multiple of 4 */
384 len = (buf_new_len + 3) & ~3U;
386 buf_new_resized = (FT_Byte*)realloc(buf_new, len);
387 if (!buf_new_resized)
389 free(buf_new);
390 return FT_Err_Out_Of_Memory;
392 buf_new = buf_new_resized;
394 base = buf_new + string_offset;
395 p = buf_new + data_offset;
397 /* second pass */
399 /* the first name record offset */
400 q = &buf_new[6 + 10];
401 for (i = 0; i < n->name_count; i++)
403 Name_Record* r = &n->name_records[i];
406 /* fill in offset */
407 *q = HIGH(p - base);
408 *(q + 1) = LOW(p - base);
410 /* copy string */
411 memcpy(p, r->str, r->len);
413 p += r->len;
414 q += 12;
417 if (n->format == 1)
419 /* the first language tag record offset */
420 q = &buf_new[6 + 12 * n->name_count + 2 + 2];
421 for (i = 0; i < n->lang_tag_count; i++)
423 Lang_Tag_Record* r = &n->lang_tag_records[i];
426 /* fill in offset */
427 *q = HIGH(p - base);
428 *(q + 1) = LOW(p - base);
430 /* copy string */
431 memcpy(p, r->str, r->len);
433 p += r->len;
434 q += 4;
438 /* pad end of buffer with zeros */
439 switch (buf_new_len % 4)
441 case 1:
442 *(p++) = 0x00;
443 /* fall through */
444 case 2:
445 *(p++) = 0x00;
446 /* fall through */
447 case 3:
448 *(p++) = 0x00;
449 /* fall through */
450 default:
451 break;
454 /* we are done; replace the old buffer with the new one */
455 free(name_table->buf);
457 name_table->buf = buf_new;
458 name_table->len = buf_new_len;
460 return TA_Err_Ok;
464 /* we handle the `name' table as optional; */
465 /* if there are problems not related to allocation, */
466 /* simply return (or continue, if possible) without signaling an error, */
467 /* and the original `name' table is not modified */
469 FT_Error
470 TA_sfnt_update_name_table(SFNT* sfnt,
471 FONT* font)
473 FT_Error error;
475 SFNT_Table* name_table;
476 FT_Byte* buf;
477 FT_ULong buf_len;
479 Naming_Table n;
481 FT_Byte* p;
482 FT_Byte* startp;
483 FT_Byte* endp;
485 FT_UShort i;
488 if (sfnt->name_idx == MISSING)
489 return TA_Err_Ok;
491 name_table = &font->tables[sfnt->name_idx];
492 buf = name_table->buf;
493 buf_len = name_table->len;
495 if (name_table->processed)
496 return TA_Err_Ok;
498 p = buf;
500 error = parse_name_header(&p, &n, buf_len, &startp, &endp);
501 if (error)
502 return TA_Err_Ok;
504 /* due to the structure of the `name' table, */
505 /* we must parse it completely, apply our changes, */
506 /* and rebuild it from scratch */
507 error = parse_name_records(&p, &n, buf, startp, endp, font);
508 if (error)
509 goto Exit;
511 error = parse_lang_tag_records(&p, &n, buf, startp, endp);
512 if (error)
513 goto Exit;
515 error = build_name_table(&n, name_table);
516 if (error)
517 goto Exit;
519 name_table->checksum = TA_table_compute_checksum(name_table->buf,
520 name_table->len);
522 Exit:
523 for (i = 0; i < n.name_count; i++)
524 font->deallocate(n.name_records[i].str);
525 for (i = 0; i < n.lang_tag_count; i++)
526 free(n.lang_tag_records[i].str);
528 free(n.name_records);
529 free(n.lang_tag_records);
531 name_table->processed = 1;
533 return error;
536 /* end of taname.c */