Some fixes found by cppcheck.
[ttfautohint.git] / lib / tatables.c
blob85cb129e7d2546d40047c9b9a87b58d60d9145a2
1 /* tatables.c */
3 /*
4 * Copyright (C) 2011-2019 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 <stdlib.h>
18 #include "ta.h"
21 FT_Error
22 TA_sfnt_add_table_info(SFNT* sfnt)
24 SFNT_Table_Info* table_infos_new;
27 sfnt->num_table_infos++;
28 table_infos_new =
29 (SFNT_Table_Info*)realloc(sfnt->table_infos, sfnt->num_table_infos
30 * sizeof (SFNT_Table_Info));
31 if (!table_infos_new)
33 sfnt->num_table_infos--;
34 return FT_Err_Out_Of_Memory;
36 else
37 sfnt->table_infos = table_infos_new;
39 sfnt->table_infos[sfnt->num_table_infos - 1] = MISSING;
41 return TA_Err_Ok;
45 FT_ULong
46 TA_table_compute_checksum(FT_Byte* buf,
47 FT_ULong len)
49 FT_Byte* end_buf = buf + len;
50 FT_ULong checksum = 0;
53 /* we expect that the length of `buf' is a multiple of 4 */
55 while (buf < end_buf)
56 checksum += NEXT_ULONG(buf);
58 return checksum;
62 FT_Error
63 TA_font_add_table(FONT* font,
64 SFNT_Table_Info* table_info,
65 FT_ULong tag,
66 FT_ULong len,
67 FT_Byte* buf)
69 SFNT_Table* tables_new;
70 SFNT_Table* table_last;
73 font->num_tables++;
74 tables_new = (SFNT_Table*)realloc(font->tables,
75 font->num_tables * sizeof (SFNT_Table));
76 if (!tables_new)
78 font->num_tables--;
79 return FT_Err_Out_Of_Memory;
81 else
82 font->tables = tables_new;
84 table_last = &font->tables[font->num_tables - 1];
86 table_last->tag = tag;
87 table_last->len = len;
88 table_last->buf = buf;
89 table_last->checksum = TA_table_compute_checksum(buf, len);
90 table_last->offset = 0; /* set in `TA_font_compute_table_offsets' */
91 table_last->data = NULL;
92 table_last->processed = 0;
94 /* link table and table info */
95 *table_info = font->num_tables - 1;
97 return TA_Err_Ok;
101 void
102 TA_sfnt_sort_table_info(SFNT* sfnt,
103 FONT* font)
105 /* Looking into an arbitrary TTF (with a `DSIG' table), tags */
106 /* starting with an uppercase letter are sorted before lowercase */
107 /* letters. In other words, the alphabetical ordering (as */
108 /* mandated by signing a font) is a simple numeric comparison of */
109 /* the 32bit tag values. */
111 SFNT_Table* tables = font->tables;
113 SFNT_Table_Info* table_infos = sfnt->table_infos;
114 FT_ULong num_table_infos = sfnt->num_table_infos;
116 FT_ULong i;
117 FT_ULong j;
120 /* bubble sort */
121 for (i = 1; i < num_table_infos; i++)
123 for (j = i; j > 0; j--)
125 SFNT_Table_Info swap;
126 FT_ULong tag1;
127 FT_ULong tag2;
130 tag1 = (table_infos[j] == MISSING)
132 : tables[table_infos[j]].tag;
133 tag2 = (table_infos[j - 1] == MISSING)
135 : tables[table_infos[j - 1]].tag;
137 if (tag1 > tag2)
138 break;
140 swap = table_infos[j];
141 table_infos[j] = table_infos[j - 1];
142 table_infos[j - 1] = swap;
148 void
149 TA_font_compute_table_offsets(FONT* font,
150 FT_ULong start)
152 FT_ULong i;
153 FT_ULong offset = start;
156 for (i = 0; i < font->num_tables; i++)
158 SFNT_Table* table = &font->tables[i];
161 table->offset = offset;
163 /* table offsets must be multiples of 4; */
164 /* this also fits the actual buffer lengths */
165 offset += (table->len + 3) & ~3U;
169 /* end of tatables.c */