Debian Codegraph @

podofo/3rdparty/adobe/afdko/source/cffwrite/cffwrite_sindex.c

raw
1 /* Copyright 2014 Adobe Systems Incorporated (http://www.adobe.com/). All Rights Reserved.
2 This software is licensed as OpenSource, under the Apache License, Version 2.0.
3 This license is available at: http://opensource.org/licenses/Apache-2.0. */
4 /***********************************************************************/
5
6 #include "cffwrite_sindex.h"
7
8 #include "ctutil.h"
9
10 #include <string.h>
11 #include <stdlib.h>
12
13 /* Standard strings */
14 typedef struct /* Standard string record */
15 {
16 SID sid;
17 char *string;
18 } StdRec;
19
20 static const StdRec std2sid[] = /* Map standard string to SID */
21 {
22 #include "stdstr0.h"
23 };
24 #define STD_STR_CNT ARRAY_LEN(std2sid)
25
26 static char *sid2std[] = /* Map SID to standard string */
27 {
28 #include "stdstr1.h"
29 };
30
31 typedef struct /* Custom string record */
32 {
33 long iString; /* String buffer index */
34 SID sid;
35 } CustomRec;
36
37 /* Module context */
38 struct sindexCtx_ {
39 dnaDCL(CustomRec, custom); /* Custom string records */
40 dnaDCL(char, strings); /* Custom string buffer */
41 dnaDCL(unsigned short, byName); /* Custom strings by name */
42 SID nextid; /* Next custom id */
43 cfwCtx g; /* Package context */
44 };
45
46 /* Initialize module. */
47 void cfwSindexNew(cfwCtx g) {
48 sindexCtx h = (sindexCtx)cfwMemNew(g, sizeof(struct sindexCtx_));
49
50 /* Link contexts */
51 h->g = g;
52 g->ctx.sindex = h;
53
54 dnaINIT(g->ctx.dnaSafe, h->custom, 260, 1000);
55 dnaINIT(g->ctx.dnaSafe, h->strings, 1500, 3000);
56 dnaINIT(g->ctx.dnaSafe, h->byName, 260, 1000);
57 h->nextid = STD_STR_CNT;
58 }
59
60 /* Get module into reusable state. */
61 void cfwSindexReuse(cfwCtx g) {
62 sindexCtx h = g->ctx.sindex;
63
64 h->custom.cnt = 0;
65 h->strings.cnt = 0;
66 h->byName.cnt = 0;
67 h->nextid = STD_STR_CNT;
68 }
69
70 /* Free resources. */
71 void cfwSindexFree(cfwCtx g) {
72 sindexCtx h = g->ctx.sindex;
73
74 if (h == NULL) {
75 return;
76 }
77
78 dnaFREE(h->custom);
79 dnaFREE(h->strings);
80 dnaFREE(h->byName);
81
82 cfwMemFree(g, h);
83 g->ctx.sindex = NULL;
84 }
85
86 /* Match standard string. */
87 static int CTL_CDECL matchStdStr(const void *key, const void *value) {
88 return strcmp((char *)key, ((StdRec *)value)->string);
89 }
90
91 /* Match non-standard string. */
92 static int CTL_CDECL matchNonStdStr(const void *key, const void *value,
93 void *ctx) {
94 sindexCtx h = (sindexCtx)ctx;
95 return strcmp((char *)key, &h->strings.array
96 [h->custom.array[*(unsigned short *)value].iString]);
97 }
98
99 int cfwSindexInvalidString(char *string) {
100 return (string == NULL || *string == '\0');
101 }
102
103 /* Add string. If standard string return its SID, otherwise if in table return
104 existing record index, else add to table and return new record index. If
105 string is empty return SRI_UNDEF. */
106 SRI cfwSindexAddString(cfwCtx g, char *string) {
107 sindexCtx h = g->ctx.sindex;
108 size_t index;
109 StdRec *std;
110
111 if (cfwSindexInvalidString(string)) {
112 return SRI_UNDEF; /* Reject invalid strings */
113 }
114 std = (StdRec *)bsearch(string, std2sid, STD_STR_CNT,
115 sizeof(StdRec), matchStdStr);
116 if (std != NULL) {
117 /* Standard string; return its SID */
118 return std->sid;
119 }
120
121 /* Search custom strings */
122 if (ctuLookup(string, h->byName.array, h->byName.cnt,
123 sizeof(h->byName.array[0]), matchNonStdStr, &index, h)) {
124 /* Match found; return custom record index */
125 return h->byName.array[index] + STD_STR_CNT;
126 } else {
127 /* Not found; add to table */
128 CustomRec *custom;
129 size_t stringLen;
130
131 unsigned short *_new = &dnaGROW(h->byName, h->byName.cnt)[index];
132
133 /* Make and fill hole */
134 memmove(_new + 1, _new,
135 (h->byName.cnt++ - index) * sizeof(h->byName.array[0]));
136 *_new = (unsigned short)h->custom.cnt;
137
138 /* Allocate and fill new record */
139 custom = dnaNEXT(h->custom);
140 custom->iString = h->strings.cnt;
141 custom->sid = SID_UNDEF;
142
143 /* Save string */
144 /* 64-bit warning fixed by cast here */
145 stringLen = (long)strlen(string) + 1;
146 STRCPY_S(dnaEXTEND(h->strings, (long)stringLen), stringLen, string);
147
148 return *_new + STD_STR_CNT;
149 }
150 }
151
152 /* Get string from SRI. */
153 char *cfwSindexGetString(cfwCtx g, SRI index) {
154 sindexCtx h = g->ctx.sindex;
155 if (index < STD_STR_CNT) {
156 return sid2std[index];
157 } else {
158 if (index - STD_STR_CNT >= h->custom.cnt) {
159 static char null_str[1] = {'\0'};
160 return null_str;
161 }
162 return &h->strings.array[h->custom.array[index - STD_STR_CNT].iString];
163 }
164 }
165
166 /* Assign the next custom SID to the specified custom string. */
167 SID cfwSindexAssignSID(cfwCtx g, SRI index) {
168 sindexCtx h = g->ctx.sindex;
169 if (index < STD_STR_CNT) {
170 return index;
171 } else if (index - STD_STR_CNT >= h->custom.cnt) {
172 return SID_UNDEF;
173 } else {
174 CustomRec *custom = &h->custom.array[index - STD_STR_CNT];
175 if (custom->sid == SID_UNDEF) {
176 custom->sid = h->nextid++;
177 }
178 return custom->sid;
179 }
180 }
181
182 /* Return table size. */
183 long cfwSindexSize(cfwCtx g) {
184 sindexCtx h = g->ctx.sindex;
185 long i;
186 long cnt = 0;
187 long size = 0;
188 for (i = 0; i < h->custom.cnt; i++) {
189 CustomRec *custom = &h->custom.array[i];
190 if (custom->sid != SID_UNDEF) {
191 cnt++;
192 /* 64-bit warning fixed by cast here */
193 size += (long)strlen(&h->strings.array[custom->iString]);
194 }
195 }
196 return INDEX_SIZE(cnt, size);
197 }
198
199 /* Compare custom string ids. */
200 static int CTL_CDECL cmpCustomIds(const void *first, const void *second) {
201 SID a = ((CustomRec *)first)->sid;
202 SID b = ((CustomRec *)second)->sid;
203 if (a < b) {
204 return -1;
205 } else if (a > b) {
206 return 1;
207 } else {
208 return 0;
209 }
210 }
211
212 /* Write string table. */
213 void cfwSindexWrite(cfwCtx g) {
214 sindexCtx h = g->ctx.sindex;
215 long i;
216 Offset offset;
217 INDEX index;
218
219 /* Sort custom records into SID order */
220 qsort(h->custom.array, h->custom.cnt, sizeof(CustomRec), cmpCustomIds);
221
222 /* Remove unused strings */
223 index.datasize = 0;
224 for (i = 0; i < h->custom.cnt; i++) {
225 CustomRec *custom = &h->custom.array[i];
226 if (custom->sid == SID_UNDEF) {
227 break;
228 } else {
229 /* 64-bit warning fixed by cast here */
230 index.datasize += (long)strlen(&h->strings.array[custom->iString]);
231 }
232 }
233 h->custom.cnt = i;
234
235 /* Construct header */
236 index.count = (unsigned short)h->custom.cnt;
237 index.offSize = INDEX_OFF_SIZE(index.datasize);
238
239 cfwWrite2(g, (unsigned short)index.count);
240
241 if (index.count == 0) {
242 return; /* Empty table just has zero count */
243 }
244 cfwWrite1(g, index.offSize);
245
246 /* Write string offset array */
247 offset = 1;
248 cfwWriteN(g, index.offSize, offset);
249 for (i = 0; i < h->custom.cnt; i++) {
250 /* 64-bit warning fixed by cast here */
251 offset += (Offset)strlen(&h->strings.array[h->custom.array[i].iString]);
252 cfwWriteN(g, index.offSize, offset);
253 }
254
255 /* Write string data */
256 for (i = 0; i < h->custom.cnt; i++) {
257 char *string = &h->strings.array[h->custom.array[i].iString];
258 cfwWrite(g, strlen(string), string);
259 }
260 }