Debian Codegraph @

texk/web2c/luatexdir/utils/utils.c

raw
1 /*
2
3 Copyright 1996-2006 Han The Thanh <[email protected]>
4 Copyright 2006-2012 Taco Hoekwater <[email protected]>
5
6 This file is part of LuaTeX.
7
8 LuaTeX is free software; you can redistribute it and/or modify it under the terms
9 of the GNU General Public License as published by the Free Software Foundation;
10 either version 2 of the License, or (at your option) any later version.
11
12 LuaTeX is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
14 PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU General Public License along with
17 LuaTeX; if not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "ptexlib.h"
22
23 /*tex
24
25 This is a trick to load mingw32's io.h early, using a macro redefinition of
26 |eof()|.
27
28 */
29
30 #include <kpathsea/config.h>
31 #include "sys/types.h"
32 #include <kpathsea/c-stat.h>
33 #include <kpathsea/c-fopen.h>
34 #include <string.h>
35 #include <time.h>
36
37 /*tex For |DBL_EPSILON|: */
38
39 #include <float.h>
40
41 #include "zlib.h"
42 #include "md5.h"
43
44 #include "lua/luatex-api.h"
45 #include "luatex_svnversion.h"
46
47 #include "png.h"
48 #include "mplib.h"
49 #ifdef LUATEX_HARFBUZZ_ENABLED
50 #include "hb.h"
51 #endif
52
53
54
55 #define check_nprintf(size_get, size_want) \
56 if ((unsigned)(size_get) >= (unsigned)(size_want)) \
57 formatted_error("internal","snprintf failed: file %s, line %d", __FILE__, __LINE__);
58
59 char *cur_file_name = NULL;
60 static char print_buf[PRINTF_BUF_SIZE];
61 int epochseconds;
62 int microseconds;
63
64 typedef char char_entry;
65 define_array(char);
66
67 #define SUBSET_TAG_LENGTH 6
68
69 void make_subset_tag(fd_entry * fd)
70 {
71 int i, j = 0, a[SUBSET_TAG_LENGTH];
72 md5_state_t pms;
73 char *glyph;
74 glw_entry *glw_glyph;
75 struct avl_traverser t;
76 md5_byte_t digest[16];
77 void **aa;
78 static struct avl_table *st_tree = NULL;
79 if (st_tree == NULL)
80 st_tree = avl_create(comp_string_entry, NULL, &avl_xallocator);
81 assert(fd != NULL);
82 assert(fd->gl_tree != NULL);
83 assert(fd->fontname != NULL);
84 // assert(fd->subset_tag == NULL);
85 if (fd->subset_tag != NULL) {
86 return;
87 }
88 fd->subset_tag = xtalloc(SUBSET_TAG_LENGTH + 1, char);
89 do {
90 md5_init(&pms);
91 avl_t_init(&t, fd->gl_tree);
92 if (is_cidkeyed(fd->fm)) { /* |glw_entry| items */
93 for (glw_glyph = (glw_entry *) avl_t_first(&t, fd->gl_tree);
94 glw_glyph != NULL; glw_glyph = (glw_entry *) avl_t_next(&t)) {
95 glyph = malloc(24);
96 sprintf(glyph, "%05u%05u ", glw_glyph->id, glw_glyph->wd);
97 md5_append(&pms, (md5_byte_t *) glyph, (int) strlen(glyph));
98 free(glyph);
99 }
100 } else {
101 for (glyph = (char *) avl_t_first(&t, fd->gl_tree); glyph != NULL;
102 glyph = (char *) avl_t_next(&t)) {
103 md5_append(&pms, (md5_byte_t *) glyph, (int) strlen(glyph));
104 md5_append(&pms, (const md5_byte_t *) " ", 1);
105 }
106 }
107 md5_append(&pms, (md5_byte_t *) fd->fontname,
108 (int) strlen(fd->fontname));
109 md5_append(&pms, (md5_byte_t *) & j, sizeof(int)); /* to resolve collision */
110 md5_finish(&pms, digest);
111 for (a[0] = 0, i = 0; i < 13; i++)
112 a[0] += digest[i];
113 for (i = 1; i < SUBSET_TAG_LENGTH; i++)
114 a[i] = a[i - 1] - digest[i - 1] + digest[(i + 12) % 16];
115 for (i = 0; i < SUBSET_TAG_LENGTH; i++)
116 fd->subset_tag[i] = (char) (a[i] % 26 + 'A');
117 fd->subset_tag[SUBSET_TAG_LENGTH] = '\0';
118 j++;
119 assert(j < 100);
120 }
121 while ((char *) avl_find(st_tree, fd->subset_tag) != NULL);
122 aa = avl_probe(st_tree, fd->subset_tag);
123 assert(aa != NULL);
124 if (j > 2)
125 formatted_warning("subsets","subset-tag collision, resolved in round %d",j);
126 }
127
128 __attribute__ ((format(printf, 1, 2)))
129 void tex_printf(const char *fmt, ...)
130 {
131 va_list args;
132 va_start(args, fmt);
133 vsnprintf(print_buf, PRINTF_BUF_SIZE, fmt, args);
134 tprint(print_buf);
135 xfflush(stdout);
136 va_end(args);
137 }
138
139 size_t xfwrite(void *ptr, size_t size, size_t nmemb, FILE * stream)
140 {
141 if (fwrite(ptr, size, nmemb, stream) != nmemb)
142 formatted_error("file io","fwrite() failed");
143 return nmemb;
144 }
145
146 int xfflush(FILE * stream)
147 {
148 if (fflush(stream) != 0)
149 formatted_error("file io","fflush() failed (%s)", strerror(errno));
150 return 0;
151 }
152
153 int xgetc(FILE * stream)
154 {
155 int c = getc(stream);
156 if (c < 0 && c != EOF)
157 formatted_error("file io","getc() failed (%s)", strerror(errno));
158 return c;
159 }
160
161 int xputc(int c, FILE * stream)
162 {
163 int i = putc(c, stream);
164 if (i < 0)
165 formatted_error("file io","putc() failed (%s)", strerror(errno));
166 return i;
167 }
168
169 scaled ext_xn_over_d(scaled x, scaled n, scaled d)
170 {
171 double r = (((double) x) * ((double) n)) / ((double) d);
172 if (r > DBL_EPSILON)
173 r += 0.5;
174 else
175 r -= 0.5;
176 if (r >= (double) max_integer || r <= -(double) max_integer)
177 normal_warning("internal","arithmetic number too big");
178 return (scaled) r;
179 }
180
181 /*tex
182
183 This function strips trailing zeros in string with numbers; leading zeros are
184 not stripped (as in real life), It's not used.
185
186 */
187
188 #if 0
189 char *stripzeros(char *a)
190 {
191 enum { NONUM, DOTNONUM, INT, DOT, LEADDOT, FRAC } s = NONUM, t = NONUM;
192 char *p, *q, *r;
193 for (p = q = r = a; *p != '\0';) {
194 switch (s) {
195 case NONUM:
196 if (*p >= '0' && *p <= '9')
197 s = INT;
198 else if (*p == '.')
199 s = LEADDOT;
200 break;
201 case DOTNONUM:
202 if (*p != '.' && (*p < '0' || *p > '9'))
203 s = NONUM;
204 break;
205 case INT:
206 if (*p == '.')
207 s = DOT;
208 else if (*p < '0' || *p > '9')
209 s = NONUM;
210 break;
211 case DOT:
212 case LEADDOT:
213 if (*p >= '0' && *p <= '9')
214 s = FRAC;
215 else if (*p == '.')
216 s = DOTNONUM;
217 else
218 s = NONUM;
219 break;
220 case FRAC:
221 if (*p == '.')
222 s = DOTNONUM;
223 else if (*p < '0' || *p > '9')
224 s = NONUM;
225 break;
226 default:;
227 }
228 switch (s) {
229 case DOT:
230 r = q;
231 break;
232 case LEADDOT:
233 r = q + 1;
234 break;
235 case FRAC:
236 if (*p > '0')
237 r = q + 1;
238 break;
239 case NONUM:
240 if ((t == FRAC || t == DOT) && r != a) {
241 q = r--;
242 if (*r == '.') /* was a LEADDOT */
243 *r = '0';
244 r = a;
245 }
246 break;
247 default:;
248 }
249 *q++ = *p++;
250 t = s;
251 }
252 *q = '\0';
253 return a;
254 }
255 #endif
256
257 void initversionstring(char **versions)
258 {
259
260 #ifdef LuajitTeX
261 #define LUA_VER_STRING LUAJIT_VERSION
262 #else
263 #define LUA_VER_STRING "lua version " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR "." LUA_VERSION_RELEASE
264 #endif
265 #define STR(tok) STR2(tok)
266 #define STR2(tok) #tok
267
268 const_string fmt =
269 #ifdef LUATEX_HARFBUZZ_ENABLED
270 "Compiled with libharfbuzz %s; using %s\n"
271 #endif
272 "Compiled with libpng %s; using %s\n"
273 "Compiled with %s\n" /* Lua or LuaJIT */
274 "Compiled with mplib version %s\n"
275 "Compiled with zlib %s; using %s\n"
276 "\nDevelopment id: %s\n";
277 size_t len = strlen(fmt)
278 #ifdef LUATEX_HARFBUZZ_ENABLED
279 + strlen(HB_VERSION_STRING) + strlen(hb_version_string())
280 #endif
281 + strlen(PNG_LIBPNG_VER_STRING) + strlen(png_libpng_ver)
282 + strlen(LUA_VER_STRING)
283 + strlen(mp_metapost_version())
284 + strlen(ZLIB_VERSION) + strlen(zlib_version)
285 + strlen(STR(luatex_svn_revision))
286 + 1;
287
288 /*tex
289 The size of |len| will be more than enough, because of the placeholder
290 chars in fmt that get replaced by the arguments.
291 */
292 *versions = xmalloc(len);
293 sprintf(*versions, fmt,
294 #ifdef LUATEX_HARFBUZZ_ENABLED
295 HB_VERSION_STRING,hb_version_string(),
296 #endif
297 PNG_LIBPNG_VER_STRING, png_libpng_ver, LUA_VER_STRING,
298 mp_metapost_version(),
299 ZLIB_VERSION, zlib_version,STR(luatex_svn_revision));
300
301 #undef STR2
302 #undef STR
303 #undef LUA_VER_STRING
304
305 }
306
307 void check_buffer_overflow(int wsize)
308 {
309 if (wsize > buf_size) {
310 int nsize = buf_size + buf_size / 5 + 5;
311 if (nsize < wsize) {
312 nsize = wsize + 5;
313 }
314 buffer = (unsigned char *) xreallocarray(buffer, char, (unsigned) nsize);
315 buf_size = nsize;
316 }
317 }
318
319 /*tex
320
321 The return value is a decimal number with the point |dd| places from the
322 back, |scaled_out| is the number of scaled points corresponding to that.
323
324 */
325
326 #define max_integer 0x7FFFFFFF
327
328 scaled divide_scaled(scaled s, scaled m, int dd)
329 {
330 register scaled q;
331 register scaled r;
332 int i;
333 int sign = 1;
334 if (s < 0) {
335 sign = -sign;
336 s = -s;
337 }
338 if (m < 0) {
339 sign = -sign;
340 m = -m;
341 }
342 if (m == 0) {
343 normal_error("arithmetic", "divided by zero");
344 } else if (m >= (max_integer / 10)) {
345 normal_error("arithmetic", "number too big");
346 }
347 q = s / m;
348 r = s % m;
349 for (i = 1; i <= (int) dd; i++) {
350 q = 10 * q + (10 * r) / m;
351 r = (10 * r) % m;
352 }
353 /*tex Rounding: */
354 if (2 * r >= m) {
355 q++;
356 }
357 return sign * q;
358 }
359
360 #ifdef _WIN32
361 #undef floor
362 #define floor win32_floor
363 #endif
364
365 /*tex
366
367 The same function, but using doubles instead of integers (faster).
368
369 */
370
371 scaled divide_scaled_n(double sd, double md, double n)
372 {
373 double dd, di = 0.0;
374 dd = sd / md * n;
375 if (dd > 0.0)
376 di = floor(dd + 0.5);
377 else if (dd < 0.0)
378 di = -floor((-dd) + 0.5);
379 return (scaled) di;
380 }
381
382 int do_zround(double r)
383 {
384 int i;
385 if (r > 2147483647.0)
386 i = 2147483647;
387 else if (r < -2147483647.0)
388 i = -2147483647;
389 else if (r >= 0.0)
390 i = (int) (r + 0.5);
391 else
392 i = (int) (r - 0.5);
393 return i;
394 }
395
396
397 /*tex
398
399 Old MSVC doesn't have |rint|.
400
401 */
402
403 #if defined(_MSC_VER) && _MSC_VER <= 1600
404
405 # include <math.h>
406
407 double rint(double x)
408 {
409 return floor(x+0.5);
410 }
411
412 #endif
413
414 /*tex
415
416 We replace |tmpfile| on \MSWINDOWS:
417
418 */
419
420 #if defined(_WIN32)
421
422 /*
423
424 _cairo_win_tmpfile (void) - replace tmpfile() on Windows
425 extracted from cairo-misc.c in cairo - a vector graphics library
426 with display and print output
427
428 the functiion name is changed from _cairo_win32_tmpfile (void) to
429 _cairo_win_tmpfile (void)
430
431 Copyright 2002 University of Southern California
432 Copyright 2005 Red Hat, Inc.
433 Copyright 2007 Adrian Johnson
434
435 This library is free software; you can redistribute it and/or modify it
436 either under the terms of the GNU Lesser General Public License version 2.1
437 as published by the Free Software Foundation (the "LGPL") or, at your option,
438 under the terms of the Mozilla Public License Version 1.1 (the "MPL"). If you
439 do not alter this notice, a recipient may use your version of this file under
440 either the MPL or the LGPL.
441
442 You should have received a copy of the LGPL along with this library in the
443 file COPYING-LGPL-2.1; if not, write to the Free Software Foundation, Inc.,
444 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA You should have
445 received a copy of the MPL along with this library in the file
446 COPYING-MPL-1.1
447
448 The contents of this file are subject to the Mozilla Public License Version
449 1.1 (the "License"); you may not use this file except in compliance with the
450 License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
451
452 This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
453 KIND, either express or implied. See the LGPL or the MPL for the specific
454 language governing rights and limitations.
455
456 The Original Code is the cairo graphics library. The Initial Developer of the
457 Original Code is University of Southern California. Contributor(s):
458
459 Carl D. Worth <[email protected]>
460 Adrian Johnson <[email protected]>
461
462 */
463
464 #include <stdio.h>
465 #define WIN32_LEAN_AND_MEAN
466
467 /*tex
468
469 We require \MSWINDOWS\ 2000 features such as |ETO_PDY|. We probably can now
470 assume that all \MSWINDOWS\ versions are recent.
471
472 */
473
474 #if !defined(WINVER) || (WINVER < 0x0500)
475 # define WINVER 0x0500
476 #endif
477 #if !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0500)
478 # define _WIN32_WINNT 0x0500
479 #endif
480
481 #include <windows.h>
482 #include <io.h>
483
484 /*tex
485
486 On \MSWINDOWS\ |tmpfile| creates the file in the root directory. This may
487 fail due to unsufficient privileges. However, this isn't a problem on
488 \MSWINDOWS\ CE so we don't use it there. Who is actually using CE anyway?
489
490 */
491
492 FILE * _cairo_win_tmpfile (void)
493 {
494 DWORD path_len;
495 WCHAR path_name[MAX_PATH + 1];
496 WCHAR file_name[MAX_PATH + 1];
497 HANDLE handle;
498 int fd;
499 FILE *fp;
500 path_len = GetTempPathW (MAX_PATH, path_name);
501 if (path_len <= 0 || path_len >= MAX_PATH)
502 return NULL;
503 if (GetTempFileNameW (path_name, L"ps_", 0, file_name) == 0)
504 return NULL;
505 handle = CreateFileW (file_name,
506 GENERIC_READ | GENERIC_WRITE,
507 0,
508 NULL,
509 CREATE_ALWAYS,
510 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE,
511 NULL);
512 if (handle == INVALID_HANDLE_VALUE) {
513 DeleteFileW (file_name);
514 return NULL;
515 }
516 fd = _open_osfhandle((intptr_t) handle, 0);
517 if (fd < 0) {
518 CloseHandle (handle);
519 return NULL;
520 }
521 fp = _fdopen(fd, "w+b");
522 if (fp == NULL) {
523 _close(fd);
524 return NULL;
525 }
526 return fp;
527 }
528
529 #endif