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_p.h"
8 #include "zutil.h"
9
10 z_const char * const PREFIX(z_errmsg)[10] = {
11 (z_const char *)"need dictionary", /* Z_NEED_DICT 2 */
12 (z_const char *)"stream end", /* Z_STREAM_END 1 */
13 (z_const char *)"", /* Z_OK 0 */
14 (z_const char *)"file error", /* Z_ERRNO (-1) */
15 (z_const char *)"stream error", /* Z_STREAM_ERROR (-2) */
16 (z_const char *)"data error", /* Z_DATA_ERROR (-3) */
17 (z_const char *)"insufficient memory", /* Z_MEM_ERROR (-4) */
18 (z_const char *)"buffer error", /* Z_BUF_ERROR (-5) */
19 (z_const char *)"incompatible version",/* Z_VERSION_ERROR (-6) */
20 (z_const char *)""
21 };
22
23 const char zlibng_string[] =
24 " zlib-ng 2.1.0.devel forked from zlib";
25
26 #ifdef ZLIB_COMPAT
zlibVersion(void)27 const char * Z_EXPORT zlibVersion(void) {
28 return ZLIB_VERSION;
29 }
30 #endif
31
zlibng_version(void)32 const char * Z_EXPORT zlibng_version(void) {
33 return ZLIBNG_VERSION;
34 }
35
PREFIX(zlibCompileFlags)36 unsigned long Z_EXPORT PREFIX(zlibCompileFlags)(void) {
37 unsigned long flags;
38
39 flags = 0;
40 switch ((int)(sizeof(unsigned int))) {
41 case 2: break;
42 case 4: flags += 1; break;
43 case 8: flags += 2; break;
44 default: flags += 3;
45 }
46 switch ((int)(sizeof(unsigned long))) {
47 case 2: break;
48 case 4: flags += 1 << 2; break;
49 case 8: flags += 2 << 2; break;
50 default: flags += 3 << 2;
51 }
52 switch ((int)(sizeof(void *))) {
53 case 2: break;
54 case 4: flags += 1 << 4; break;
55 case 8: flags += 2 << 4; break;
56 default: flags += 3 << 4;
57 }
58 switch ((int)(sizeof(z_off_t))) {
59 case 2: break;
60 case 4: flags += 1 << 6; break;
61 case 8: flags += 2 << 6; break;
62 default: flags += 3 << 6;
63 }
64 #ifdef ZLIB_DEBUG
65 flags += 1 << 8;
66 #endif
67 #ifdef ZLIB_WINAPI
68 flags += 1 << 10;
69 #endif
70 /* Bit 13 reserved for DYNAMIC_CRC_TABLE */
71 #ifdef NO_GZCOMPRESS
72 flags += 1L << 16;
73 #endif
74 #ifdef NO_GZIP
75 flags += 1L << 17;
76 #endif
77 #ifdef PKZIP_BUG_WORKAROUND
78 flags += 1L << 20;
79 #endif
80 return flags;
81 }
82
83 #ifdef ZLIB_DEBUG
84 # include <stdlib.h>
85 # ifndef verbose
86 # define verbose 0
87 # endif
88 int Z_INTERNAL z_verbose = verbose;
89
z_error(char * m)90 void Z_INTERNAL z_error(char *m) {
91 fprintf(stderr, "%s\n", m);
92 exit(1);
93 }
94 #endif
95
96 /* exported to allow conversion of error code to string for compress() and
97 * uncompress()
98 */
PREFIX(zError)99 const char * Z_EXPORT PREFIX(zError)(int err) {
100 return ERR_MSG(err);
101 }
102
zng_calloc(void * opaque,unsigned items,unsigned size)103 void Z_INTERNAL *zng_calloc(void *opaque, unsigned items, unsigned size) {
104 Z_UNUSED(opaque);
105 return zng_alloc((size_t)items * (size_t)size);
106 }
107
zng_cfree(void * opaque,void * ptr)108 void Z_INTERNAL zng_cfree(void *opaque, void *ptr) {
109 Z_UNUSED(opaque);
110 zng_free(ptr);
111 }
112
113 /* Since we support custom memory allocators, some which might not align memory as we expect,
114 * we have to ask for extra memory and return an aligned pointer. */
zng_alloc_aligned(zng_calloc_func zalloc,void * opaque,unsigned items,unsigned size,unsigned align)115 void Z_INTERNAL *zng_alloc_aligned(zng_calloc_func zalloc, void *opaque, unsigned items, unsigned size, unsigned align) {
116 uintptr_t return_ptr, original_ptr;
117 uint32_t alloc_size, align_diff;
118 void *ptr;
119
120 /* If no custom calloc function used then call zlib-ng's aligned calloc */
121 if (zalloc == zng_calloc)
122 return zng_calloc(opaque, items, size);
123
124 /* Allocate enough memory for proper alignment and to store the original memory pointer */
125 alloc_size = sizeof(void *) + (items * size) + align;
126 ptr = zalloc(opaque, 1, alloc_size);
127 if (!ptr)
128 return NULL;
129
130 /* Calculate return pointer address with space enough to store original pointer */
131 align_diff = align - ((uintptr_t)ptr % align);
132 return_ptr = (uintptr_t)ptr + align_diff;
133 if (align_diff < sizeof(void *))
134 return_ptr += align;
135
136 /* Store the original pointer for free() */
137 original_ptr = return_ptr - sizeof(void *);
138 memcpy((void *)original_ptr, &ptr, sizeof(void *));
139
140 /* Return properly aligned pointer in allocation */
141 return (void *)return_ptr;
142 }
143
zng_free_aligned(zng_cfree_func zfree,void * opaque,void * ptr)144 void Z_INTERNAL zng_free_aligned(zng_cfree_func zfree, void *opaque, void *ptr) {
145 /* If no custom cfree function used then call zlib-ng's aligned cfree */
146 if (zfree == zng_cfree) {
147 zng_cfree(opaque, ptr);
148 return;
149 }
150 if (!ptr)
151 return;
152
153 /* Calculate offset to original memory allocation pointer */
154 void *original_ptr = (void *)((uintptr_t)ptr - sizeof(void *));
155 void *free_ptr = *(void **)original_ptr;
156
157 /* Free original memory allocation */
158 zfree(opaque, free_ptr);
159 }
160