1 /* Copyright (C) 2001-2019 Artifex Software, Inc.
2    All Rights Reserved.
3 
4    This software is provided AS-IS with no warranty, either express or
5    implied.
6 
7    This software is distributed under license and may not be copied,
8    modified or distributed except as expressly authorized under the terms
9    of the license contained in the file LICENSE in this distribution.
10 
11    Refer to licensing information at http://www.artifex.com or contact
12    Artifex Software, Inc.,  1305 Grant Avenue - Suite 200, Novato,
13    CA 94945, U.S.A., +1(415)492-9861, for further information.
14 */
15 
16 
17 /* Standard definitions for Ghostscript code not needing arch.h */
18 
19 #ifndef stdpre_INCLUDED
20 #  define stdpre_INCLUDED
21 
22 /* We do not use FILE * as a rule. Instead we use gp_file * and
23  * gp_ prefixed versions of fgetc, fread, fwrite, fseek, ftell
24  * fclose, etc.
25  */
26 #ifndef gp_file_DEFINED
27 #  define gp_file_DEFINED
28 typedef struct gp_file_s gp_file;
29 #endif
30 
31 /* if we define _LARGEFILE64_SOURCE zlib tries to include unistd.h */
32 #ifndef _MSC_VER
33 /* Ghostscript uses transitional LFS functions. */
34 #define _LARGEFILE64_SOURCE 1
35 #endif
36 
37 #ifndef _FILE_OFFSET_BITS
38 #define _FILE_OFFSET_BITS 64
39 #endif
40 
41 /*
42  * Here we deal with the vagaries of various C compilers.  We assume that:
43  *      ANSI-standard Unix compilers define __STDC__.
44  *      gcc defines __GNUC__.
45  *      Borland Turbo C and Turbo C++ define __MSDOS__ and __TURBOC__.
46  *      Borland C++ defines __BORLANDC__, __MSDOS__, and __TURBOC__.
47  *      Microsoft C/C++ defines _MSC_VER and _MSDOS.
48  *      Watcom C defines __WATCOMC__ and MSDOS.
49  *      MetroWerks C defines __MWERKS__.
50  *
51  * We arrange to define __MSDOS__ on all the MS-DOS platforms.
52  */
53 #if (defined(MSDOS) || defined(_MSDOS)) && !defined(__MSDOS__)
54 #  define __MSDOS__
55 #endif
56 /*
57  * Also, not used much here, but used in other header files, we assume:
58  *      Unix System V environments define SYSV.
59  *      The SCO ODT compiler defines M_SYSV and M_SYS3.
60  *      VMS systems define VMS.
61  *      OSF/1 compilers define __osf__ or __OSF__.
62  *        (The VMS and OSF/1 C compilers handle prototypes and const,
63  *        but do not define __STDC__.)
64  *      bsd 4.2 or 4.3 systems define BSD4_2.
65  *      POSIX-compliant environments define _POSIX_SOURCE.
66  *      Motorola 88K BCS/OCS systems defined m88k.
67  *
68  * We make fairly heroic efforts to confine all uses of these flags to
69  * header files, and never to use them in code.
70  */
71 #if defined(__osf__) && !defined(__OSF__)
72 #  define __OSF__		/* */
73 #endif
74 #if defined(M_SYSV) && !defined(SYSV)
75 #  define SYSV			/* */
76 #endif
77 #if defined(M_SYS3) && !defined(__SVR3)
78 #  define __SVR3		/* */
79 #endif
80 
81 #if defined(__STDC__) || defined(__MSDOS__) || defined(__convex__) || defined(VMS) || defined(__OSF__) || defined(__WIN32__) || defined(__IBMC__) || defined(M_UNIX) || defined(__GNUC__) || defined(__BORLANDC__)
82 # if !(defined(M_XENIX) && !defined(__GNUC__))	/* SCO Xenix cc is broken */
83 #  define __PROTOTYPES__	/* */
84 # endif
85 #endif
86 
87 /* Define dummy values for __FILE__ and __LINE__ if the compiler */
88 /* doesn't provide these.  Note that places that use __FILE__ */
89 /* must check explicitly for a null pointer. */
90 #ifndef __FILE__
91 #  define __FILE__ NULL
92 #endif
93 #ifndef __LINE__
94 #  define __LINE__ 0
95 #endif
96 
97 /* Disable 'const' and 'volatile' if the compiler can't handle them. */
98 #ifndef __PROTOTYPES__
99 #  undef const
100 #  define const			/* */
101 #  undef volatile
102 #  define volatile		/* */
103 #endif
104 
105 /* restrict is standard in C99, but not in all C++ compilers. */
106 #if defined(__STDC_VERSION__) && __STDC_VERSION__ == 199901L /* C99 */
107 # if defined(HAVE_RESTRICT) && HAVE_RESTRICT==1
108 #  define gs_restrict restrict
109 # else /* defined(HAVE_RESTRICT) && HAVE_RESTRICT==1 */
110 #  define gs_restrict
111 # endif /* defined(HAVE_RESTRICT) && HAVE_RESTRICT==1 */
112 #elif defined(_MSC_VER) && _MSC_VER >= 1500 /* MSVC 9 or newer */
113 # define gs_restrict __restrict
114 #elif __GNUC__ >= 3 /* GCC 3 or newer */
115 # if defined(HAVE_RESTRICT) && HAVE_RESTRICT==1
116 #  define gs_restrict __restrict
117 # else /* defined(HAVE_RESTRICT) && HAVE_RESTRICT==1 */
118 #  define gs_restrict
119 # endif /* defined(HAVE_RESTRICT) && HAVE_RESTRICT==1 */
120 #else /* Unknown or ancient */
121 # define gs_restrict
122 #endif
123 
124 
125 
126 /* Ensure we have a definition of 'inline', even if that means
127  * disabling it if the compiler can't handle it. */
128 #ifdef __cplusplus
129  /* inline will already be defined within C++ */
130 #elif defined (__STDC_VERSION_) && (__STDC_VERSION__ >= 199901L)
131  /* inline will already be defined within C99 */
132 #elif defined(_MSC_VER) && (_MSC_VER >= 1500) /* MSVC 9 or newer */
133  #define inline __inline
134 #elif defined(__GNUC__) && (__GNUC__ >= 3) /* GCC 3 or newer */
135  /* Define inline as __inline__ so -pedantic won't produce a warning. */
136  #undef inline
137  #define inline __inline__
138 #elif defined(__DECC)
139  #undef inline
140  #define inline __inline
141 #elif !(defined(__MWERKS__) || defined(inline))
142  /* Unknown or ancient - disable it */
143  #define inline
144 #endif
145 
146 /* Define ourselves a 'forceinline' we can use to more forcefully
147  * tell the compiler to inline something. On all but MSVC this can
148  * drop back to inline. */
149 #ifdef _MSC_VER
150 #define forceinline __forceinline
151 #else
152 #define forceinline inline
153 #endif
154 
155 /*
156  * Provide a way to include inline procedures in header files, regardless of
157  * whether the compiler (A) doesn't support inline at all, (B) supports it
158  * but also always compiles a closed copy, (C) supports it but somehow only
159  * includes a single closed copy in the executable, or (D) supports it and
160  * also supports a different syntax if no closed copy is desired.
161  *
162  * The code that appears just after this comment indicates which compilers
163  * are of which kind.  (Eventually this might be determined automatically.)
164  *	(A) and (B) require nothing here.
165  *	(C) requires
166  *		#define extern_inline inline
167  *	(D) requires
168  *		#define extern_inline extern inline  // or whatever
169  * Note that for case (B), the procedure will only be declared inline in
170  * the .c file where its closed copy is compiled.
171  */
172 #ifdef __GNUC__
173 #  define extern_inline extern inline
174 #endif
175 
176 /*
177  * To include an inline procedure xyz in a header file abc.h, use the
178  * following template in the header file:
179 
180 extern_inline int xyz(<<parameters>>)
181 #if HAVE_EXTERN_INLINE || defined(INLINE_INCLUDE_xyz)
182 {
183     <<body>>
184 }
185 #else
186 ;
187 #endif
188 
189  * And use the following in whichever .c file takes responsibility for
190  * including the closed copy of xyz:
191 
192 #define EXTERN_INCLUDE_xyz	// must precede all #includes
193 #include "abc.h"
194 
195  * The definitions of the EXTERN_INCLUDE_ macros must precede *all* includes
196  * because there is no way to know whether some other .h file #includes abc.h
197  * indirectly, and because of the protection against double #includes, the
198  * EXTERN_INCLUDE_s must be defined before the first inclusion of abc.h.
199  */
200 
201 /*
202  * The following is generic code that does not need per-compiler
203  * customization.
204  */
205 #ifdef extern_inline
206 #  define HAVE_EXTERN_INLINE 1
207 #else
208 #  define extern_inline /* */
209 #  define HAVE_EXTERN_INLINE 0
210 #endif
211 
212 /*
213  * Some compilers give a warning if a function call that returns a value
214  * is used as a statement; a few compilers give an error for the construct
215  * (void)0, which is contrary to the ANSI standard.  Since we don't know of
216  * any compilers that do both, we define a macro here for discarding
217  * the value of an expression statement, which can be defined as either
218  * including or not including the cast.  (We don't conditionalize this here,
219  * because no commercial compiler gives the error on (void)0, although
220  * some give warnings.)  */
221 #define DISCARD(expr) ((void)(expr))
222 /* Backward compatibility */
223 #define discard(expr) DISCARD(expr)
224 
225 /*
226  * Some versions of the Watcom compiler give a "Comparison result always
227  * 0/1" message that we want to suppress because it gets in the way of
228  * meaningful warnings.
229  */
230 #ifdef __WATCOMC__
231 #  pragma disable_message(124);
232 #endif
233 
234 /*
235  * Some versions of gcc have a bug such that after
236         byte *p;
237         ...
238         x = *(long *)p;
239  * the compiler then thinks that p always points to long-aligned data.
240  * Detect this here so it can be handled appropriately in the few places
241  * that (we think) matter.
242  */
243 #ifdef __GNUC__
244 # if __GNUC__ == 2 & (7 < __GNUC_MINOR__ <= 95)
245 #  define ALIGNMENT_ALIASING_BUG
246 # endif
247 #endif
248 
249 /*
250  * The SVR4.2 C compiler incorrectly considers the result of << and >>
251  * to be unsigned if the left operand is signed and the right operand is
252  * unsigned.  We believe this only causes trouble in Ghostscript code when
253  * the right operand is a sizeof(...), which is unsigned for this compiler.
254  * Therefore, we replace the relevant uses of sizeof with size_of:
255  */
256 #define size_of(x) ((int)(sizeof(x)))
257 
258 /*
259  * far_data was formerly used for static data that had to be assigned its
260  * own segment on PCs with 64K segments.  This was supported in Borland C++,
261  * but none of the other compilers.  Since we no longer support
262  * small-segment systems, far_data is vacuous.
263  */
264 #undef far_data
265 #define far_data /* */
266 
267 /*
268  * Get the number of elements of a statically dimensioned array.
269  * Note that this also works on array members of structures.
270  */
271 #define countof(a) (sizeof(a) / sizeof((a)[0]))
272 #define count_of(a) (size_of(a) / size_of((a)[0]))
273 
274 /*
275  * Get the offset of a structure member.  Amazingly enough, the simpler
276  * definition works on all compilers except for one broken MIPS compiler
277  * and the IBM RS/6000.  Unfortunately, because of these two compilers,
278  * we have to use the more complex definition.  Even more unfortunately,
279  * the more complex definition doesn't work on the MetroWerks
280  * CodeWarrior compiler (Macintosh and BeOS).
281  */
282 #ifdef __MWERKS__
283 #define offset_of(type, memb)\
284  ((int) &((type *) 0)->memb)
285 #else
286 #define offset_of(type, memb)\
287  ((int) ( (char *)&((type *)0)->memb - (char *)((type *)0) ))
288 #endif
289 
290 /*
291  * Get the alignment of a pointer modulo a given power of 2.
292  * There is no portable way to do this, but the following definition
293  * works on all reasonable systems.
294  */
295 #define ALIGNMENT_MOD(ptr, modu)\
296   ((uint)( ((const char *)(ptr) - (const char *)0) & ((modu) - 1) ))
297 
298 /* Define short names for the unsigned types. */
299 typedef unsigned char byte;
300 typedef unsigned char uchar;
301 typedef unsigned short ushort;
302 typedef unsigned int uint;
303 typedef unsigned long ulong;
304 
305 /* And for signed char */
306 typedef signed char schar;
307 
308 /* Since sys/types.h may define one or more of these (depending on
309  * the platform), we have to take steps to prevent name clashes.
310  * Unfortunately this can clobber valid definitions for the size-
311  * specific types, but there's no simple solution.
312  *
313  * NOTE: This requires that you include std.h *before* any other
314  * header file that includes sys/types.h.
315  *
316  */
317 #define bool bool_		/* (maybe not needed) */
318 #define uchar uchar_
319 #define uint uint_
320 #define ushort ushort_
321 #define ulong ulong_
322 #define schar schar_
323 #include <sys/types.h>
324 #undef bool
325 #undef uchar
326 #undef uint
327 #undef ushort
328 #undef ulong
329 #undef schar
330 
331 /*
332  * Define a Boolean type.  Even though we would like it to be
333  * unsigned char, it pretty well has to be int, because
334  * that's what all the relational operators and && and || produce.
335  * We can't make it an enumerated type, because ints don't coerce
336  * freely to enums (although the opposite is true).
337  * Unfortunately, at least some C++ compilers have a built-in bool type,
338  * and the MetroWerks C++ compiler insists that bool be equivalent to
339  * unsigned char.
340  */
341 #ifndef __cplusplus
342 #ifdef __BEOS__
343 typedef unsigned char bool;
344 #else
345 typedef int bool;
346 #endif
347 #endif
348 /*
349  * Older versions of MetroWerks CodeWarrior defined true and false, but they're now
350  * an enum in the (MacOS) Universal Interfaces. The only way around this is to escape
351  * our own definitions wherever MacTypes.h is included.
352  */
353 #if defined(_MSC_VER) && _MSC_VER >= 1900
354 /* VS 2014 defines bool already, but has it as _Bool (a 1 byte thing).
355  * We can't live with that. */
356 #undef false
357 #define false ((bool)0)
358 #undef true
359 #define true ((bool)1)
360 #else
361 #ifndef __MACOS__
362 #undef false
363 #define false ((bool)0)
364 #undef true
365 #define true ((bool)1)
366 #endif /* __MACOS__ */
367 #endif
368 
369 /*
370  * Compilers disagree as to whether macros used in macro arguments
371  * should be expanded at the time of the call, or at the time of
372  * final expansion.  Even authoritative documents disagree: the ANSI
373  * standard says the former, but Harbison and Steele's book says the latter.
374  * In order to work around this discrepancy, we have to do some very
375  * ugly things in a couple of places.  We mention it here because
376  * it might well trip up future developers.
377  */
378 
379 /*
380  * Define the type to be used for ordering pointers (<, >=, etc.).
381  * The Borland and Microsoft large models only compare the offset part
382  * of segmented pointers.  Semantically, the right type to use for the
383  * comparison is char huge *, but we have no idea how expensive comparing
384  * such pointers is, and any type that compares all the bits of the pointer,
385  * gives the right result for pointers in the same segment, and keeps
386  * different segments disjoint will do.
387  */
388 #if defined(__TURBOC__) || defined(_MSC_VER)
389 typedef unsigned long ptr_ord_t;
390 #else
391 typedef const char *ptr_ord_t;
392 #endif
393 /* Define all the pointer comparison operations. */
394 #define _PTR_CMP(p1, rel, p2)  ((ptr_ord_t)(p1) rel (ptr_ord_t)(p2))
395 #define PTR_LE(p1, p2) _PTR_CMP(p1, <=, p2)
396 #define PTR_LT(p1, p2) _PTR_CMP(p1, <, p2)
397 #define PTR_GE(p1, p2) _PTR_CMP(p1, >=, p2)
398 #define PTR_GT(p1, p2) _PTR_CMP(p1, >, p2)
399 #define PTR_BETWEEN(ptr, lo, hi)\
400   (PTR_GE(ptr, lo) && PTR_LT(ptr, hi))
401 
402 /* Define  min and max, but make sure to use the identical definition */
403 /* to the one that all the compilers seem to have.... */
404 #ifndef min
405 #  define min(a, b) (((a) < (b)) ? (a) : (b))
406 #endif
407 #ifndef max
408 #  define max(a, b) (((a) > (b)) ? (a) : (b))
409 #endif
410 
411 /* Define a standard way to round values to a (constant) modulus. */
412 #define ROUND_DOWN(value, modulus)\
413   ( (modulus) & ((modulus) - 1) ?	/* not a power of 2 */\
414     (value) - (value) % (modulus) :\
415     (value) & -(modulus) )
416 #define ROUND_UP(value, modulus)\
417   ( (modulus) & ((modulus) - 1) ?	/* not a power of 2 */\
418     ((value) + ((modulus) - 1)) / (modulus) * (modulus) :\
419     ((value) + ((modulus) - 1)) & -(modulus) )
420 /* Backward compatibility */
421 #define round_up(v, m) ROUND_UP(v, m)
422 #define round_down(v, m) ROUND_DOWN(v, m)
423 
424 /*
425  * Because of C's strange insistence that ; is a terminator and not a
426  * separator, compound statements {...} are not syntactically equivalent to
427  * single statements.  Therefore, we define here a compound-statement
428  * construct that *is* syntactically equivalent to a single statement.
429  * Usage is
430  *      BEGIN
431  *        ...statements...
432  *      END
433  */
434 #define BEGIN	do {
435 #define END	} while (0)
436 
437 /*
438  * Define a handy macro for a statement that does nothing.
439  * We can't just use an empty statement, since this upsets some compilers.
440  */
441 #ifndef DO_NOTHING
442 #  define DO_NOTHING BEGIN END
443 #endif
444 
445 /*
446  * For accountability, debugging, and error messages, we pass a client
447  * identification string to alloc and free, and possibly other places as
448  * well.  Define the type for these strings.
449  */
450 typedef const char *client_name_t;
451 /****** WHAT TO DO ABOUT client_name_string ? ******/
452 #define client_name_string(cname) (cname)
453 
454 /*
455  * Define success and failure codes for 'exit'.  The only system on which
456  * they are different is VMS with older DEC C versions.  We aren't sure
457  * in what version DEC C started being compatible with the rest of the
458  * world, and we don't know what the story is with VAX C.  If you have
459  * problems, uncomment the following line or add -DOLD_VMS_C to the C
460  * command line.
461  */
462 /*#define OLD_VMS_C*/
463 #if defined(VMS)
464 #  define exit_FAILED 18
465 #  if (defined(OLD_VMS_C) || !defined(__DECC))
466 #    define exit_OK 1
467 #  else
468 #    define exit_OK 0
469 #  endif
470 #else
471 #  define exit_OK 0
472 #  define exit_FAILED 1
473 #endif
474 
475 #endif /* stdpre_INCLUDED */
476