• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* zutil.c -- target dependent utility functions for the compression library
2  * Copyright (C) 1995-2017 Jean-loup Gailly
3  * For conditions of distribution and use, see copyright notice in zlib.h
4  */
5 
6 #include "zbuild.h"
7 #include "zutil.h"
8 #ifdef WITH_GZFILEOP
9 #  include "gzguts.h"
10 #endif
11 
12 z_const char * const PREFIX(z_errmsg)[10] = {
13     (z_const char *)"need dictionary",     /* Z_NEED_DICT       2  */
14     (z_const char *)"stream end",          /* Z_STREAM_END      1  */
15     (z_const char *)"",                    /* Z_OK              0  */
16     (z_const char *)"file error",          /* Z_ERRNO         (-1) */
17     (z_const char *)"stream error",        /* Z_STREAM_ERROR  (-2) */
18     (z_const char *)"data error",          /* Z_DATA_ERROR    (-3) */
19     (z_const char *)"insufficient memory", /* Z_MEM_ERROR     (-4) */
20     (z_const char *)"buffer error",        /* Z_BUF_ERROR     (-5) */
21     (z_const char *)"incompatible version",/* Z_VERSION_ERROR (-6) */
22     (z_const char *)""
23 };
24 
25 const char zlibng_string[] =
26     " zlib-ng 1.9.9 forked from zlib 1.2.12.f ";
27 
28 #ifdef ZLIB_COMPAT
zlibVersion(void)29 const char * Z_EXPORT zlibVersion(void) {
30     return ZLIB_VERSION;
31 }
32 #endif
33 
zlibng_version(void)34 const char * Z_EXPORT zlibng_version(void) {
35     return ZLIBNG_VERSION;
36 }
37 
PREFIX(zlibCompileFlags)38 unsigned long Z_EXPORT PREFIX(zlibCompileFlags)(void) {
39     unsigned long flags;
40 
41     flags = 0;
42     switch ((int)(sizeof(unsigned int))) {
43     case 2:     break;
44     case 4:     flags += 1;     break;
45     case 8:     flags += 2;     break;
46     default:    flags += 3;
47     }
48     switch ((int)(sizeof(unsigned long))) {
49     case 2:     break;
50     case 4:     flags += 1 << 2;        break;
51     case 8:     flags += 2 << 2;        break;
52     default:    flags += 3 << 2;
53     }
54     switch ((int)(sizeof(void *))) {
55     case 2:     break;
56     case 4:     flags += 1 << 4;        break;
57     case 8:     flags += 2 << 4;        break;
58     default:    flags += 3 << 4;
59     }
60     switch ((int)(sizeof(z_off_t))) {
61     case 2:     break;
62     case 4:     flags += 1 << 6;        break;
63     case 8:     flags += 2 << 6;        break;
64     default:    flags += 3 << 6;
65     }
66 #ifdef ZLIB_DEBUG
67     flags += 1 << 8;
68 #endif
69 #ifdef ZLIB_WINAPI
70     flags += 1 << 10;
71 #endif
72     /* Bit 13 reserved for DYNAMIC_CRC_TABLE */
73 #ifdef NO_GZCOMPRESS
74     flags += 1L << 16;
75 #endif
76 #ifdef NO_GZIP
77     flags += 1L << 17;
78 #endif
79 #ifdef PKZIP_BUG_WORKAROUND
80     flags += 1L << 20;
81 #endif
82     return flags;
83 }
84 
85 #ifdef ZLIB_DEBUG
86 #  include <stdlib.h>
87 #  ifndef verbose
88 #    define verbose 0
89 #  endif
90 int Z_INTERNAL z_verbose = verbose;
91 
z_error(char * m)92 void Z_INTERNAL z_error(char *m) {
93     fprintf(stderr, "%s\n", m);
94     exit(1);
95 }
96 #endif
97 
98 /* exported to allow conversion of error code to string for compress() and
99  * uncompress()
100  */
PREFIX(zError)101 const char * Z_EXPORT PREFIX(zError)(int err) {
102     return ERR_MSG(err);
103 }
104 
105 #ifndef MY_ZCALLOC /* Any system without a special alloc function */
106 
107 #ifndef UNALIGNED_OK
108 #  include <malloc.h>
109 #  if defined(_WIN32)
110 #    define zng_align_alloc(align, size) _aligned_malloc(size, align)
111 #    define zng_align_free(ptr)          _aligned_free(ptr)
112 #  else
113 #    define zng_align_alloc              memalign
114 #    define zng_align_free(ptr)          free(ptr)
115 #  endif
116 #endif
117 
zng_calloc(void * opaque,unsigned items,unsigned size)118 void Z_INTERNAL *zng_calloc(void *opaque, unsigned items, unsigned size)
119 {
120     (void)opaque;
121 #ifndef UNALIGNED_OK
122     return zng_align_alloc(16, items * size);
123 #else
124     return sizeof(unsigned int) > 2 ? (void *)malloc(items * size) :
125                               (void *)calloc(items, size);
126 #endif
127 }
128 
zng_cfree(void * opaque,void * ptr)129 void Z_INTERNAL zng_cfree(void *opaque, void *ptr) {
130     (void)opaque;
131 #ifndef UNALIGNED_OK
132     zng_align_free(ptr);
133 #else
134     free(ptr);
135 #endif
136 }
137 
138 #endif /* MY_ZCALLOC */
139