Debian Codegraph @

src/libchdr_codec_zlib.c

raw symbols SCIP
1 #include "codec_zlib.h"
2
3 #include <stddef.h>
4 #include <stdlib.h>
5 #include <string.h>
6
7 static voidpf zlib_fast_alloc(voidpf opaque, zlib_alloc_size items, zlib_alloc_size size);
8 static void zlib_fast_free(voidpf opaque, voidpf address);
9 static void zlib_allocator_free(voidpf opaque);
10
11 /*-------------------------------------------------
12 zlib_codec_init - initialize the ZLIB codec
13 -------------------------------------------------*/
14
15 chd_error zlib_codec_init(void *codec, uint32_t hunkbytes)
16 {
17 int zerr;
18 chd_error err;
19 zlib_codec_data *data = (zlib_codec_data*)codec;
20
21 (void)hunkbytes;
22
23 /* clear the buffers */
24 memset(data, 0, sizeof(zlib_codec_data));
25
26 /* init the inflater first */
27 data->inflater.next_in = (Bytef *)data; /* bogus, but that's ok */
28 data->inflater.avail_in = 0;
29 data->inflater.zalloc = zlib_fast_alloc;
30 data->inflater.zfree = zlib_fast_free;
31 data->inflater.opaque = &data->allocator;
32 zerr = inflateInit2(&data->inflater, -MAX_WBITS);
33
34 /* convert errors */
35 if (zerr == Z_MEM_ERROR)
36 err = CHDERR_OUT_OF_MEMORY;
37 else if (zerr != Z_OK)
38 err = CHDERR_CODEC_ERROR;
39 else
40 err = CHDERR_NONE;
41
42 return err;
43 }
44
45 /*-------------------------------------------------
46 zlib_codec_free - free data for the ZLIB
47 codec
48 -------------------------------------------------*/
49
50 void zlib_codec_free(void *codec)
51 {
52 zlib_codec_data *data = (zlib_codec_data *)codec;
53
54 /* deinit the streams */
55 if (data != NULL)
56 {
57 inflateEnd(&data->inflater);
58
59 /* free our fast memory */
60 zlib_allocator_free(&data->allocator);
61 }
62 }
63
64 /*-------------------------------------------------
65 zlib_codec_decompress - decompress data using
66 the ZLIB codec
67 -------------------------------------------------*/
68
69 chd_error zlib_codec_decompress(void *codec, const uint8_t *src, uint32_t complen, uint8_t *dest, uint32_t destlen)
70 {
71 zlib_codec_data *data = (zlib_codec_data *)codec;
72 int zerr;
73
74 /* reset the decompressor */
75 data->inflater.next_in = (Bytef *)src;
76 data->inflater.avail_in = complen;
77 data->inflater.total_in = 0;
78 data->inflater.next_out = (Bytef *)dest;
79 data->inflater.avail_out = destlen;
80 data->inflater.total_out = 0;
81 zerr = inflateReset(&data->inflater);
82 if (zerr != Z_OK)
83 return CHDERR_DECOMPRESSION_ERROR;
84
85 /* do it */
86 zerr = inflate(&data->inflater, Z_FINISH);
87 if (data->inflater.total_out != destlen)
88 return CHDERR_DECOMPRESSION_ERROR;
89
90 return CHDERR_NONE;
91 }
92
93 /*-------------------------------------------------
94 zlib_fast_alloc - fast malloc for ZLIB, which
95 allocates and frees memory frequently
96 -------------------------------------------------*/
97
98 /* Huge alignment values for possible SIMD optimization by compiler (NEON, SSE, AVX) */
99 #define ZLIB_MIN_ALIGNMENT_BITS 512
100 #define ZLIB_MIN_ALIGNMENT_BYTES (ZLIB_MIN_ALIGNMENT_BITS / 8)
101
102 static voidpf zlib_fast_alloc(voidpf opaque, zlib_alloc_size items, zlib_alloc_size size)
103 {
104 zlib_allocator *alloc = (zlib_allocator *)opaque;
105 uintptr_t paddr = 0;
106 uint32_t *ptr;
107 int i;
108
109 /* compute the size, rounding to the nearest 1k */
110 size = (size * items + 0x3ff) & ~0x3ff;
111
112 /* reuse a hunk if we can */
113 for (i = 0; i < MAX_ZLIB_ALLOCS; i++)
114 {
115 ptr = alloc->allocptr[i];
116 if (ptr && size == *ptr)
117 {
118 /* set the low bit of the size so we don't match next time */
119 *ptr |= 1;
120
121 /* return aligned block address */
122 return (voidpf)(alloc->allocptr2[i]);
123 }
124 }
125
126 /* alloc a new one */
127 ptr = (uint32_t *)malloc(size + sizeof(uint32_t) + ZLIB_MIN_ALIGNMENT_BYTES);
128 if (!ptr)
129 return NULL;
130
131 /* put it into the list */
132 for (i = 0; i < MAX_ZLIB_ALLOCS; i++)
133 if (!alloc->allocptr[i])
134 {
135 alloc->allocptr[i] = ptr;
136 paddr = (((uintptr_t)ptr) + sizeof(uint32_t) + (ZLIB_MIN_ALIGNMENT_BYTES-1)) & (~(ZLIB_MIN_ALIGNMENT_BYTES-1));
137 alloc->allocptr2[i] = (uint32_t*)paddr;
138 break;
139 }
140
141 /* set the low bit of the size so we don't match next time */
142 *ptr = size | 1;
143
144 /* return aligned block address */
145 return (voidpf)paddr;
146 }
147
148 /*-------------------------------------------------
149 zlib_fast_free - fast free for ZLIB, which
150 allocates and frees memory frequently
151 -------------------------------------------------*/
152
153 static void zlib_fast_free(voidpf opaque, voidpf address)
154 {
155 zlib_allocator *alloc = (zlib_allocator *)opaque;
156 uint32_t *ptr = (uint32_t *)address;
157 int i;
158
159 /* find the hunk */
160 for (i = 0; i < MAX_ZLIB_ALLOCS; i++)
161 if (ptr == alloc->allocptr2[i])
162 {
163 /* clear the low bit of the size to allow matches */
164 *(alloc->allocptr[i]) &= ~1;
165 return;
166 }
167 }
168
169 /*-------------------------------------------------
170 zlib_allocator_free
171 -------------------------------------------------*/
172 static void zlib_allocator_free(voidpf opaque)
173 {
174 zlib_allocator *alloc = (zlib_allocator *)opaque;
175 int i;
176
177 for (i = 0; i < MAX_ZLIB_ALLOCS; i++)
178 if (alloc->allocptr[i])
179 free(alloc->allocptr[i]);
180 }