| 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 |
|
| 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 |
|
| 24 |
memset(data, 0, sizeof(zlib_codec_data)); |
| 25 |
|
| 26 |
|
| 27 |
data->inflater.next_in = (Bytef *)data; |
| 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 |
|
| 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 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
void zlib_codec_free(void *codec) |
| 51 |
{ |
| 52 |
zlib_codec_data *data = (zlib_codec_data *)codec; |
| 53 |
|
| 54 |
|
| 55 |
if (data != NULL) |
| 56 |
{ |
| 57 |
inflateEnd(&data->inflater); |
| 58 |
|
| 59 |
|
| 60 |
zlib_allocator_free(&data->allocator); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 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 |
|
| 110 |
size = (size * items + 0x3ff) & ~0x3ff; |
| 111 |
|
| 112 |
|
| 113 |
for (i = 0; i < MAX_ZLIB_ALLOCS; i++) |
| 114 |
{ |
| 115 |
ptr = alloc->allocptr[i]; |
| 116 |
if (ptr && size == *ptr) |
| 117 |
{ |
| 118 |
|
| 119 |
*ptr |= 1; |
| 120 |
|
| 121 |
|
| 122 |
return (voidpf)(alloc->allocptr2[i]); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
ptr = (uint32_t *)malloc(size + sizeof(uint32_t) + ZLIB_MIN_ALIGNMENT_BYTES); |
| 128 |
if (!ptr) |
| 129 |
return NULL; |
| 130 |
|
| 131 |
|
| 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 |
|
| 142 |
*ptr = size | 1; |
| 143 |
|
| 144 |
|
| 145 |
return (voidpf)paddr; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 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 |
|
| 160 |
for (i = 0; i < MAX_ZLIB_ALLOCS; i++) |
| 161 |
if (ptr == alloc->allocptr2[i]) |
| 162 |
{ |
| 163 |
|
| 164 |
*(alloc->allocptr[i]) &= ~1; |
| 165 |
return; |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 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 |
} |