Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
7858 views
1
#ifndef MUPDF_FITZ_SYSTEM_H
2
#define MUPDF_FITZ_SYSTEM_H
3
4
/*
5
Include the standard libc headers.
6
*/
7
8
#include <stdio.h>
9
#include <stdlib.h>
10
#include <stddef.h>
11
#include <stdarg.h>
12
#include <string.h>
13
#include <math.h>
14
15
#include <assert.h>
16
#include <errno.h>
17
#include <limits.h> /* INT_MAX & co */
18
#include <float.h> /* FLT_EPSILON, FLT_MAX & co */
19
#include <fcntl.h> /* O_RDONLY & co */
20
#include <time.h>
21
22
#include <setjmp.h>
23
24
#include "mupdf/memento.h"
25
26
#define nelem(x) (sizeof(x)/sizeof((x)[0]))
27
28
#ifndef M_PI
29
#define M_PI 3.14159265358979323846
30
#endif
31
32
#ifndef M_SQRT2
33
#define M_SQRT2 1.41421356237309504880
34
#endif
35
36
/*
37
Some differences in libc can be smoothed over
38
*/
39
40
#ifdef __APPLE__
41
#define HAVE_SIGSETJMP
42
#elif defined(__unix)
43
#define HAVE_SIGSETJMP
44
#endif
45
46
/*
47
Where possible (i.e. on platforms on which they are provided), use
48
sigsetjmp/siglongjmp in preference to setjmp/longjmp. We don't alter
49
signal handlers within mupdf, so there is no need for us to
50
store/restore them - hence we use the non-restoring variants. This
51
makes a large speed difference on MacOSX (and probably other
52
platforms too.
53
*/
54
#ifdef HAVE_SIGSETJMP
55
#define fz_setjmp(BUF) sigsetjmp(BUF, 0)
56
#define fz_longjmp(BUF,VAL) siglongjmp(BUF, VAL)
57
#define fz_jmp_buf sigjmp_buf
58
#else
59
#define fz_setjmp(BUF) setjmp(BUF)
60
#define fz_longjmp(BUF,VAL) longjmp(BUF,VAL)
61
#define fz_jmp_buf jmp_buf
62
#endif
63
64
#ifdef _MSC_VER /* Microsoft Visual C */
65
66
/* MSVC up to VS2012 */
67
#if _MSC_VER < 1800
68
#define va_copy(a, oa) do { a=oa; } while (0)
69
#define va_copy_end(a) do {} while(0)
70
#else
71
#define va_copy_end(a) va_end(a)
72
#endif
73
74
typedef signed char int8_t;
75
typedef short int int16_t;
76
typedef int int32_t;
77
typedef __int64 int64_t;
78
79
typedef unsigned char uint8_t;
80
typedef unsigned short int uint16_t;
81
typedef unsigned int uint32_t;
82
typedef unsigned __int64 uint64_t;
83
84
#pragma warning( disable: 4244 ) /* conversion from X to Y, possible loss of data */
85
#pragma warning( disable: 4701 ) /* Potentially uninitialized local variable 'name' used */
86
#pragma warning( disable: 4996 ) /* 'function': was declared deprecated */
87
88
#include <io.h>
89
90
struct timeval;
91
struct timezone;
92
int gettimeofday(struct timeval *tv, struct timezone *tz);
93
94
#define snprintf _snprintf
95
#if _MSC_VER < 1800
96
#define isnan(x) _isnan(x)
97
#define isinf(x) (!_finite(x))
98
#endif
99
#define hypotf _hypotf
100
101
#define fopen fz_fopen_utf8
102
103
FILE *fz_fopen_utf8(const char *name, const char *mode);
104
105
#define fopen fz_fopen_utf8
106
107
char *fz_utf8_from_wchar(const wchar_t *s);
108
wchar_t *fz_wchar_from_utf8(const char *s);
109
110
FILE *fz_fopen_utf8(const char *name, const char *mode);
111
char **fz_argv_from_wargv(int argc, wchar_t **wargv);
112
void fz_free_argv(int argc, char **argv);
113
114
#else /* Unix or close enough */
115
116
#include <stdint.h>
117
#include <unistd.h>
118
119
#ifndef O_BINARY
120
#define O_BINARY 0
121
#endif
122
123
#define va_copy_end(a) va_end(a)
124
125
#endif
126
127
#ifdef __ANDROID__
128
#include <android/log.h>
129
#define LOG_TAG "libmupdf"
130
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
131
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
132
#else
133
#define LOGI(...) do {} while(0)
134
#define LOGE(...) do {} while(0)
135
#endif
136
137
/*
138
Variadic macros, inline and restrict keywords
139
140
inline is standard in C++, so don't touch the definition in this case.
141
For some compilers we can enable it within C too.
142
*/
143
144
#ifndef __cplusplus
145
#if __STDC_VERSION__ == 199901L /* C99 */
146
#elif _MSC_VER >= 1500 /* MSVC 9 or newer */
147
#define inline __inline
148
#elif __GNUC__ >= 3 /* GCC 3 or newer */
149
#define inline __inline
150
#else /* Unknown or ancient */
151
#define inline
152
#endif
153
#endif
154
155
/*
156
restrict is standard in C99, but not in all C++ compilers. Enable
157
where possible, disable if in doubt.
158
*/
159
#if __STDC_VERSION__ == 199901L /* C99 */
160
#elif _MSC_VER >= 1500 /* MSVC 9 or newer */
161
#define restrict __restrict
162
#elif __GNUC__ >= 3 /* GCC 3 or newer */
163
#define restrict __restrict
164
#else /* Unknown or ancient */
165
#define restrict
166
#endif
167
168
/* noreturn is a GCC extension */
169
#ifdef __GNUC__
170
#define FZ_NORETURN __attribute__((noreturn))
171
#else
172
#ifdef _MSC_VER
173
#define FZ_NORETURN __declspec(noreturn)
174
#else
175
#define FZ_NORETURN
176
#endif
177
#endif
178
179
/*
180
GCC can do type checking of printf strings
181
*/
182
183
#ifndef __printflike
184
#if __GNUC__ > 2 || __GNUC__ == 2 && __GNUC_MINOR__ >= 7
185
#define __printflike(fmtarg, firstvararg) \
186
__attribute__((__format__ (__printf__, fmtarg, firstvararg)))
187
#else
188
#define __printflike(fmtarg, firstvararg)
189
#endif
190
#endif
191
192
/*
193
Shut the compiler up about unused variables
194
*/
195
#define UNUSED(x) do { x = x; } while (0)
196
197
/* ARM assembly specific defines */
198
199
#ifdef ARCH_ARM
200
#ifdef NDK_PROFILER
201
extern void __gnu_mcount_nc(void);
202
#define ENTER_PG "push {lr}\nbl __gnu_mcount_nc\n"
203
#else
204
#define ENTER_PG
205
#endif
206
207
/* If we're compiling as thumb code, then we need to tell the compiler
208
* to enter and exit ARM mode around our assembly sections. If we move
209
* the ARM functions to a separate file and arrange for it to be compiled
210
* without thumb mode, we can save some time on entry.
211
*/
212
/* This is slightly suboptimal; __thumb__ and __thumb2__ become defined
213
* and undefined by #pragma arm/#pragma thumb - but we can't define a
214
* macro to track that. */
215
#if defined(__thumb__) || defined(__thumb2__)
216
#define ENTER_ARM ".balign 4\nmov r12,pc\nbx r12\n0:.arm\n" ENTER_PG
217
#define ENTER_THUMB "9:.thumb\n" ENTER_PG
218
#else
219
#define ENTER_ARM
220
#define ENTER_THUMB
221
#endif
222
#endif
223
224
#ifdef CLUSTER
225
#define LOCAL_TRIG_FNS
226
#endif
227
228
#ifdef LOCAL_TRIG_FNS
229
/*
230
* Trig functions
231
*/
232
static float
233
my_atan_table[258] =
234
{
235
0.0000000000f, 0.00390623013f,0.00781234106f,0.0117182136f,
236
0.0156237286f, 0.0195287670f, 0.0234332099f, 0.0273369383f,
237
0.0312398334f, 0.0351417768f, 0.0390426500f, 0.0429423347f,
238
0.0468407129f, 0.0507376669f, 0.0546330792f, 0.0585268326f,
239
0.0624188100f, 0.0663088949f, 0.0701969711f, 0.0740829225f,
240
0.0779666338f, 0.0818479898f, 0.0857268758f, 0.0896031775f,
241
0.0934767812f, 0.0973475735f, 0.1012154420f, 0.1050802730f,
242
0.1089419570f, 0.1128003810f, 0.1166554350f, 0.1205070100f,
243
0.1243549950f, 0.1281992810f, 0.1320397620f, 0.1358763280f,
244
0.1397088740f, 0.1435372940f, 0.1473614810f, 0.1511813320f,
245
0.1549967420f, 0.1588076080f, 0.1626138290f, 0.1664153010f,
246
0.1702119250f, 0.1740036010f, 0.1777902290f, 0.1815717110f,
247
0.1853479500f, 0.1891188490f, 0.1928843120f, 0.1966442450f,
248
0.2003985540f, 0.2041471450f, 0.2078899270f, 0.2116268090f,
249
0.2153577000f, 0.2190825110f, 0.2228011540f, 0.2265135410f,
250
0.2302195870f, 0.2339192060f, 0.2376123140f, 0.2412988270f,
251
0.2449786630f, 0.2486517410f, 0.2523179810f, 0.2559773030f,
252
0.2596296290f, 0.2632748830f, 0.2669129880f, 0.2705438680f,
253
0.2741674510f, 0.2777836630f, 0.2813924330f, 0.2849936890f,
254
0.2885873620f, 0.2921733830f, 0.2957516860f, 0.2993222020f,
255
0.3028848680f, 0.3064396190f, 0.3099863910f, 0.3135251230f,
256
0.3170557530f, 0.3205782220f, 0.3240924700f, 0.3275984410f,
257
0.3310960770f, 0.3345853220f, 0.3380661230f, 0.3415384250f,
258
0.3450021770f, 0.3484573270f, 0.3519038250f, 0.3553416220f,
259
0.3587706700f, 0.3621909220f, 0.3656023320f, 0.3690048540f,
260
0.3723984470f, 0.3757830650f, 0.3791586690f, 0.3825252170f,
261
0.3858826690f, 0.3892309880f, 0.3925701350f, 0.3959000740f,
262
0.3992207700f, 0.4025321870f, 0.4058342930f, 0.4091270550f,
263
0.4124104420f, 0.4156844220f, 0.4189489670f, 0.4222040480f,
264
0.4254496370f, 0.4286857080f, 0.4319122350f, 0.4351291940f,
265
0.4383365600f, 0.4415343100f, 0.4447224240f, 0.4479008790f,
266
0.4510696560f, 0.4542287350f, 0.4573780990f, 0.4605177290f,
267
0.4636476090f, 0.4667677240f, 0.4698780580f, 0.4729785980f,
268
0.4760693300f, 0.4791502430f, 0.4822213240f, 0.4852825630f,
269
0.4883339510f, 0.4913754780f, 0.4944071350f, 0.4974289160f,
270
0.5004408130f, 0.5034428210f, 0.5064349340f, 0.5094171490f,
271
0.5123894600f, 0.5153518660f, 0.5183043630f, 0.5212469510f,
272
0.5241796290f, 0.5271023950f, 0.5300152510f, 0.5329181980f,
273
0.5358112380f, 0.5386943730f, 0.5415676050f, 0.5444309400f,
274
0.5472843810f, 0.5501279330f, 0.5529616020f, 0.5557853940f,
275
0.5585993150f, 0.5614033740f, 0.5641975770f, 0.5669819340f,
276
0.5697564530f, 0.5725211450f, 0.5752760180f, 0.5780210840f,
277
0.5807563530f, 0.5834818390f, 0.5861975510f, 0.5889035040f,
278
0.5915997100f, 0.5942861830f, 0.5969629370f, 0.5996299860f,
279
0.6022873460f, 0.6049350310f, 0.6075730580f, 0.6102014430f,
280
0.6128202020f, 0.6154293530f, 0.6180289120f, 0.6206188990f,
281
0.6231993300f, 0.6257702250f, 0.6283316020f, 0.6308834820f,
282
0.6334258830f, 0.6359588250f, 0.6384823300f, 0.6409964180f,
283
0.6435011090f, 0.6459964250f, 0.6484823880f, 0.6509590190f,
284
0.6534263410f, 0.6558843770f, 0.6583331480f, 0.6607726790f,
285
0.6632029930f, 0.6656241120f, 0.6680360620f, 0.6704388650f,
286
0.6728325470f, 0.6752171330f, 0.6775926450f, 0.6799591110f,
287
0.6823165550f, 0.6846650020f, 0.6870044780f, 0.6893350100f,
288
0.6916566220f, 0.6939693410f, 0.6962731940f, 0.6985682070f,
289
0.7008544080f, 0.7031318220f, 0.7054004770f, 0.7076604000f,
290
0.7099116190f, 0.7121541600f, 0.7143880520f, 0.7166133230f,
291
0.7188300000f, 0.7210381110f, 0.7232376840f, 0.7254287490f,
292
0.7276113330f, 0.7297854640f, 0.7319511710f, 0.7341084830f,
293
0.7362574290f, 0.7383980370f, 0.7405303370f, 0.7426543560f,
294
0.7447701260f, 0.7468776740f, 0.7489770290f, 0.7510682220f,
295
0.7531512810f, 0.7552262360f, 0.7572931160f, 0.7593519510f,
296
0.7614027700f, 0.7634456020f, 0.7654804790f, 0.7675074280f,
297
0.7695264800f, 0.7715376650f, 0.7735410110f, 0.7755365500f,
298
0.7775243100f, 0.7795043220f, 0.7814766150f, 0.7834412190f,
299
0.7853981630f, 0.7853981630f /* Extended by 1 for interpolation */
300
};
301
302
static inline float my_sinf(float x)
303
{
304
float x2, xn;
305
int i;
306
/* Map x into the -PI to PI range. We could do this using:
307
* x = fmodf(x, (float)(2.0 * M_PI));
308
* but that's C99, and seems to misbehave with negative numbers
309
* on some platforms. */
310
x -= (float)M_PI;
311
i = x / (float)(2.0f * M_PI);
312
x -= i * (float)(2.0f * M_PI);
313
if (x < 0.0f)
314
x += (float)(2.0f * M_PI);
315
x -= (float)M_PI;
316
if (x <= (float)(-M_PI/2.0))
317
x = -(float)M_PI-x;
318
else if (x >= (float)(M_PI/2.0))
319
x = (float)M_PI-x;
320
x2 = x*x;
321
xn = x*x2/6.0f;
322
x -= xn;
323
xn *= x2/20.0f;
324
x += xn;
325
xn *= x2/42.0f;
326
x -= xn;
327
xn *= x2/72.0f;
328
x += xn;
329
return x;
330
}
331
332
static inline float my_atan2f(float o, float a)
333
{
334
int negate = 0, flip = 0, i;
335
float r, s;
336
if (o == 0.0f)
337
{
338
if (a > 0)
339
return 0.0f;
340
else
341
return (float)M_PI;
342
}
343
if (o < 0)
344
o = -o, negate = 1;
345
if (a < 0)
346
a = -a, flip = 1;
347
if (o < a)
348
i = (int)(65536.0f*o/a + 0.5f);
349
else
350
i = (int)(65536.0f*a/o + 0.5f);
351
r = my_atan_table[i>>8];
352
s = my_atan_table[(i>>8)+1];
353
r += (s-r)*(i&255)/256.0f;
354
if (o >= a)
355
r = (float)(M_PI/2.0f) - r;
356
if (flip)
357
r = (float)M_PI - r;
358
if (negate)
359
r = -r;
360
return r;
361
}
362
363
#define sinf(x) my_sinf(x)
364
#define cosf(x) my_sinf(((float)(M_PI/2.0f)) + (x))
365
#define atan2f(x,y) my_atan2f((x),(y))
366
#endif
367
368
int fz_strcasecmp(const char *a, const char *b);
369
370
#endif
371
372