Debian Codegraph @

podofo/3rdparty/adobe/afdko/source/ctutil/ctutil.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 * CoreType Utilities Library
7 */
8
9 #include <string.h>
10 #include <stdlib.h>
11 #include <ctype.h>
12 #include <stdio.h>
13 #include <math.h>
14 #include <stdint.h>
15 #include "ctutil.h"
16
17 /* Exchange 2 values of size "s" pointed to by "a" and "b". */
18 #define MAX_BUF 256
19 #define EXCH(a, b, s) \
20 do { \
21 char tmp[MAX_BUF]; \
22 size_t n; \
23 char *p = (a); \
24 char *q = (b); \
25 for (n = (s); n > MAX_BUF; n -= MAX_BUF) { \
26 memcpy(tmp, p, MAX_BUF); \
27 memcpy(p, q, MAX_BUF); \
28 memcpy(q, tmp, MAX_BUF); \
29 p += MAX_BUF; \
30 q += MAX_BUF; \
31 } \
32 memcpy(tmp, p, n); \
33 memcpy(p, q, n); \
34 memcpy(q, tmp, n); \
35 } while (0)
36
37 /* Sort array using quicksort algorithm. Algorithm adapted from the Quicksort
38 chapter of "Algorithms in C" by Robert Sedgewick. */
39 static void quicksort(char *pl, char *pr, int size,
40 int(CTL_CDECL *cmp)(const void *first,
41 const void *second, void *ctx),
42 void *ctx) {
43 while (pr - pl > 0) {
44 char *pi = pl - size;
45 char *pj = pr;
46
47 for (;;) {
48 while (cmp(pi += size, pr, ctx) < 0) {
49 }
50 while (cmp(pj -= size, pr, ctx) > 0) {
51 if (pj == pl) {
52 break; /* Avoid overrun */
53 }
54 }
55 if (pi >= pj) {
56 break; /* Pointers crossed */
57 }
58 EXCH(pi, pj, size);
59 }
60
61 /* Move partition value to final position */
62 if (pi != pr) {
63 EXCH(pi, pr, size);
64 }
65
66 /* Sort subfiles l..i-1 and i+1..r; recurse on smallest subfile */
67 pj = pi - size;
68 pi += size;
69 if (pj - pl < pr - pi) {
70 if (pj - pl > 0) {
71 quicksort(pl, pj, size, cmp, ctx);
72 }
73 pl = pi;
74 } else {
75 if (pr - pi > 0) {
76 quicksort(pi, pr, size, cmp, ctx);
77 }
78 pr = pj;
79 }
80 }
81 }
82
83 /* Sort array. Replacement for ANSI C qsort() when compare function requires
84 access to context but can't use global variables for reentrancy reasons. */
85 void ctuQSort(void *base, size_t count, size_t size,
86 int(CTL_CDECL *cmp)(const void *first, const void *second,
87 void *ctx),
88 void *ctx) {
89 /* 64-bit warning fixed by cast here */
90 quicksort((char *)base, (char *)base + (count - 1) * size, (int)size, cmp, ctx);
91 }
92
93 /* Binary search for key. Returns 1 if found else 0. Found or insertion
94 position returned via "index" parameter. */
95 int ctuLookup(const void *key, const void *base, size_t count, size_t size,
96 int(CTL_CDECL *cmp)(const void *key, const void *value,
97 void *ctx),
98 size_t *index, void *ctx) {
99 long lo = 0;
100 /* 64-bit warning fixed by cast here */
101 long hi = (long)(count - 1);
102
103 while (lo <= hi) {
104 long mid = (lo + hi) / 2;
105 int match = cmp(key, (char *)base + mid * size, ctx);
106 if (match > 0) {
107 lo = mid + 1;
108 } else if (match < 0) {
109 hi = mid - 1;
110 } else {
111 /* Found */
112 *index = mid;
113 return 1;
114 }
115 }
116
117 /* Not found */
118 *index = lo;
119 return 0;
120 }
121
122 /* Convert ANSI standard date/time format to Apple LongDateTime format.
123 Algorithm adapted from standard Julian Day calculation. */
124 void ctuANSITime2LongDateTime(struct tm *ansi, ctuLongDateTime ldt) {
125 unsigned long elapsed;
126 int month = ansi->tm_mon + 1;
127 int year = ansi->tm_year;
128
129 if (month < 3) {
130 /* Jan and Feb become months 13 and 14 of the previous year */
131 month += 12;
132 year--;
133 }
134
135 /* Calculate elapsed seconds since 1 Jan 1904 00:00:00 */
136 elapsed = (((year - 4L) * 365 + /* Add years (as days) */
137 year / 4 + /* Add leap year days */
138 (306 * (month + 1)) / 10 - 64 + /* Add month days */
139 ansi->tm_mday) * /* Add days */
140 24 * 60 * 60 + /* Convert days to secs */
141 ansi->tm_hour * 60 * 60 + /* Add hours (as secs) */
142 ansi->tm_min * 60 + /* Add minutes (as secs) */
143 ansi->tm_sec); /* Add seconds */
144
145 /* Set value */
146 ldt[0] = ldt[1] = ldt[2] = ldt[3] = 0;
147 ldt[4] = (char)(elapsed >> 24);
148 ldt[5] = (char)(elapsed >> 16);
149 ldt[6] = (char)(elapsed >> 8);
150 ldt[7] = (char)elapsed;
151 }
152
153 /* Convert Apple LongDateTime format to ANSI standard date/time format.
154 Algorithm adapted from standard Julian Day calculation. */
155 void ctuLongDateTime2ANSITime(struct tm *ansi, ctuLongDateTime ldt) {
156 uint32_t elapsed = (ldt[4] << 24 |
157 ldt[5] << 16 |
158 ldt[6] << 8 |
159 ldt[7]);
160 int A = elapsed / (24 * 60 * 60L);
161 int B = A + 1524;
162 int C = ((B - 122.1) / 365.25);
163 int D = (C * 365.25);
164 int E = ((B - D) / 30.6001);
165 int F = (E * 30.6001);
166 int secs = elapsed - A * (24 * 60 * 60L);
167 if (E > 13) {
168 ansi->tm_year = C + 1;
169 ansi->tm_mon = E - 14;
170 ansi->tm_yday = B - D - 429;
171 } else {
172 ansi->tm_year = C;
173 ansi->tm_mon = E - 2;
174 ansi->tm_yday = B - D - 64;
175 }
176 ansi->tm_mday = B - D - F;
177 ansi->tm_hour = secs / (60 * 60);
178 secs -= ansi->tm_hour * (60 * 60);
179 ansi->tm_min = secs / 60;
180 ansi->tm_sec = secs - ansi->tm_min * 60;
181 ansi->tm_wday = (A + 5) % 7;
182 ansi->tm_isdst = 0;
183 }
184
185 /* Count bits in a long word */
186 int ctuCountBits(long value) {
187 int count;
188 for (count = 0; value; count++) {
189 value &= value - 1;
190 }
191 return count;
192 }
193
194 /* Convert string to double in thread safe and locale-independent manner. */
195 double ctuStrtod(const char *s, char **endptr) {
196 char *ep;
197 double result = strtod(s, &ep);
198
199 if (*ep == '.') {
200 /* Parse stopped on period; may be using non-C locale */
201 char *p;
202 size_t length;
203
204 /* Assume the period was the decimal point and parse rest of number.
205 Note, it is possible that the period terminated a number that didn't
206 contain a decimal point, e.g. "1E2.". In this rare case the number
207 was already parsed correctly, the following repeated conversions
208 will have no effect, and the correct value will be returned. */
209 (void)strtod(ep + 1, &p);
210 length = p - s;
211
212 if (length < 50) {
213 int i;
214 char tmp[50];
215
216 /* Make modifiable copy of string */
217 memcpy(tmp, s, length);
218 tmp[length] = '\0';
219 p = ep - s + tmp;
220
221 /* Try conversion several times in case locale switched on
222 another thread */
223 for (i = 0; i < 6; i++) {
224 /* Cycle between period and comma */
225 *p = (*p == '.') ? ',' : '.';
226 result = strtod(tmp, &ep);
227 if (ep == tmp + length) {
228 /* Reached end of number; conversion succeeded */
229 ep = (char *)(s + length);
230 break;
231 }
232 }
233 }
234 }
235
236 if (endptr != NULL) {
237 *endptr = ep;
238 }
239
240 return result;
241 }
242
243 /* Convert double in a locale-independent manner. */
244 void ctuDtostr(char *buf, size_t bufLen, double value, int width, int precision) {
245 char *p;
246 if (width == 0 && precision == 0) {
247 SPRINTF_S(buf, bufLen, "%.12lf", value);
248 } else if (width == 0 && precision > 0) {
249 SPRINTF_S(buf, bufLen, "%.*lf", precision, value);
250 } else if (width > 0 && precision == 0) {
251 SPRINTF_S(buf, bufLen, "%*lf", width, value);
252 } else {
253 SPRINTF_S(buf, bufLen, "%*.*lf", width, precision, value);
254 }
255 p = strchr(buf, ',');
256 if (p != NULL) {
257 /* Non-C locale in use; convert to C locale convention */
258 *p = '.';
259 }
260 p = strchr(buf, '.');
261 if (p != NULL) {
262 /* Have decimal point. Remove trailing zeros.*/
263 int l = (int)strlen(p);
264 p += l - 1;
265 while (*p == '0') {
266 *p = '\0';
267 p--;
268 }
269 if (*p == '.') {
270 *p = '\0';
271 }
272 }
273 }
274
275 /* Get version numbers of libraries. */
276 void ctuGetVersion(ctlVersionCallbacks *cb) {
277 if (cb->called & 1 << CTU_LIB_ID) {
278 return; /* Already enumerated */
279 }
280 cb->getversion(cb, CTU_VERSION, "ctutil");
281
282 /* Record this call */
283 cb->called |= 1 << CTU_LIB_ID;
284 }
285
286 char *CTL_SPLIT_VERSION(char *version_buf, int version) {
287 sprintf(version_buf, "%d.%d.%d", ((version) >> 16 & 0xff),
288 ((version) >> 8 & 0xff),
289 ((version)&0xff));
290 return version_buf;
291 }
292
293 #if !defined(_UCRT)
294 float roundf(float x) {
295 float val = (float)((x < 0) ? (ceil((x)-0.5)) : (floor((x) + 0.5)));
296 return val;
297 }
298 #endif