Debian Codegraph @

src/libchdr_codec_lzma.c

raw symbols SCIP
1 #include "codec_lzma.h"
2
3 #include <stddef.h>
4 #include <stdlib.h>
5 #include <string.h>
6
7 /***************************************************************************
8 * LZMA ALLOCATOR HELPER
9 ***************************************************************************
10 */
11
12 static void *lzma_fast_alloc(ISzAllocPtr p, size_t size);
13 static void lzma_fast_free(ISzAllocPtr p, void *address);
14
15 /*-------------------------------------------------
16 * lzma_allocator_init
17 *-------------------------------------------------
18 */
19
20 static void lzma_allocator_init(void* p)
21 {
22 lzma_allocator *codec = (lzma_allocator *)(p);
23
24 /* reset pointer list */
25 memset(codec->allocptr, 0, sizeof(codec->allocptr));
26 memset(codec->allocptr2, 0, sizeof(codec->allocptr2));
27 codec->base.Alloc = lzma_fast_alloc;
28 codec->base.Free = lzma_fast_free;
29 }
30
31 /*-------------------------------------------------
32 * lzma_allocator_free
33 *-------------------------------------------------
34 */
35
36 static void lzma_allocator_free(void* p )
37 {
38 int i;
39 lzma_allocator *codec = (lzma_allocator *)(p);
40
41 /* free our memory */
42 for (i = 0 ; i < MAX_LZMA_ALLOCS ; i++)
43 {
44 if (codec->allocptr[i] != NULL)
45 free(codec->allocptr[i]);
46 }
47 }
48
49 /*-------------------------------------------------
50 * lzma_fast_alloc - fast malloc for lzma, which
51 * allocates and frees memory frequently
52 *-------------------------------------------------
53 */
54
55 /* Huge alignment values for possible SIMD optimization by compiler (NEON, SSE, AVX) */
56 #define LZMA_MIN_ALIGNMENT_BITS 512
57 #define LZMA_MIN_ALIGNMENT_BYTES (LZMA_MIN_ALIGNMENT_BITS / 8)
58
59 static void *lzma_fast_alloc(ISzAllocPtr p, size_t size)
60 {
61 int scan;
62 uint32_t *addr = NULL;
63 lzma_allocator *codec = (lzma_allocator *)(p);
64 uintptr_t vaddr = 0;
65
66 /* compute the size, rounding to the nearest 1k */
67 size = (size + 0x3ff) & ~0x3ff;
68
69 /* reuse a hunk if we can */
70 for (scan = 0; scan < MAX_LZMA_ALLOCS; scan++)
71 {
72 uint32_t *ptr = codec->allocptr[scan];
73 if (ptr != NULL && size == *ptr)
74 {
75 /* set the low bit of the size so we don't match next time */
76 *ptr |= 1;
77
78 /* return aligned address of the block */
79 return codec->allocptr2[scan];
80 }
81 }
82
83 /* alloc a new one and put it into the list */
84 addr = (uint32_t *)malloc(size + sizeof(uint32_t) + LZMA_MIN_ALIGNMENT_BYTES);
85 if (addr==NULL)
86 return NULL;
87 for (scan = 0; scan < MAX_LZMA_ALLOCS; scan++)
88 {
89 if (codec->allocptr[scan] == NULL)
90 {
91 /* store block address */
92 codec->allocptr[scan] = addr;
93
94 /* compute aligned address, store it */
95 vaddr = (uintptr_t)addr;
96 vaddr = (vaddr + sizeof(uint32_t) + (LZMA_MIN_ALIGNMENT_BYTES-1)) & (~(LZMA_MIN_ALIGNMENT_BYTES-1));
97 codec->allocptr2[scan] = (uint32_t*)vaddr;
98 break;
99 }
100 }
101
102 /* set the low bit of the size so we don't match next time */
103 *addr = size | 1;
104
105 /* return aligned address */
106 return (void*)vaddr;
107 }
108
109 /*-------------------------------------------------
110 * lzma_fast_free - fast free for lzma, which
111 * allocates and frees memory frequently
112 *-------------------------------------------------
113 */
114
115 static void lzma_fast_free(ISzAllocPtr p, void *address)
116 {
117 int scan;
118 uint32_t *ptr = NULL;
119 lzma_allocator *codec = NULL;
120
121 if (address == NULL)
122 return;
123
124 codec = (lzma_allocator *)(p);
125
126 /* find the hunk */
127 ptr = (uint32_t *)address;
128 for (scan = 0; scan < MAX_LZMA_ALLOCS; scan++)
129 {
130 if (ptr == codec->allocptr2[scan])
131 {
132 /* clear the low bit of the size to allow matches */
133 *codec->allocptr[scan] &= ~1;
134 return;
135 }
136 }
137 }
138
139 /***************************************************************************
140 * LZMA DECOMPRESSOR
141 ***************************************************************************
142 */
143
144 /*-------------------------------------------------
145 * lzma_compute_aligned_dictionary_size
146 * Based on LzmaEncProps_Normalize, LzmaEnc_SetProps, LzmaEnc_WriteProperties.
147 *-------------------------------------------------
148 */
149
150 static uint32_t lzma_compute_aligned_dictionary_size(uint32_t hunkbytes)
151 {
152 const unsigned int level = 9;
153 const uint32_t reduceSize = hunkbytes;
154
155 uint32_t dictSize, alignedDictSize;
156
157 /* LzmaEncProps_Normalize */
158 dictSize = level <= 4 ?
159 (uint32_t)1 << (level * 2 + 16) :
160 level <= sizeof(size_t) / 2 + 4 ?
161 (uint32_t)1 << (level + 20) :
162 (uint32_t)1 << (sizeof(size_t) / 2 + 24);
163
164 if (dictSize > reduceSize)
165 {
166 const uint32_t kReduceMin = (uint32_t)1 << 12;
167 const uint32_t max = MIN(kReduceMin, reduceSize);
168
169 dictSize = MAX(max, dictSize);
170 }
171
172 /* LzmaEnc_SetProps */
173 dictSize = MIN((uint32_t)15 << 28, dictSize); /* kLzmaMaxHistorySize */
174
175 /* LzmaEnc_WriteProperties */
176 /* we write aligned dictionary value to properties for lzma decoder */
177 if (dictSize >= ((uint32_t)1 << 21))
178 {
179 const uint32_t kDictMask = ((uint32_t)1 << 20) - 1;
180
181 alignedDictSize = (dictSize + kDictMask) & ~kDictMask;
182 alignedDictSize = MIN(dictSize, alignedDictSize);
183 }
184 else
185 {
186 unsigned int i = 11 * 2;
187
188 do
189 {
190 alignedDictSize = (uint32_t)(2 + (i & 1)) << (i >> 1);
191 i++;
192 }
193 while (alignedDictSize < dictSize);
194 }
195
196 return alignedDictSize;
197 }
198
199 /*-------------------------------------------------
200 * lzma_codec_init - constructor
201 *-------------------------------------------------
202 */
203
204 chd_error lzma_codec_init(void* codec, uint32_t hunkbytes)
205 {
206 lzma_codec_data* lzma_codec = (lzma_codec_data*) codec;
207 lzma_allocator* alloc = &lzma_codec->allocator;
208 const uint32_t alignedDictSize = lzma_compute_aligned_dictionary_size(hunkbytes);
209
210 unsigned int i;
211 Byte decoder_props[LZMA_PROPS_SIZE];
212
213 decoder_props[0] = 93;
214 for (i = 0; i < LZMA_PROPS_SIZE - 1; ++i)
215 decoder_props[1 + i] = (alignedDictSize >> (8 * i)) & 0xFF;
216
217 lzma_allocator_init(alloc);
218
219 /* construct the decoder */
220 LzmaDec_Construct(&lzma_codec->decoder);
221
222 /* do memory allocations */
223 if (LzmaDec_Allocate(&lzma_codec->decoder, decoder_props, LZMA_PROPS_SIZE, &alloc->base) != SZ_OK)
224 return CHDERR_DECOMPRESSION_ERROR;
225
226 /* Okay */
227 return CHDERR_NONE;
228 }
229
230 /*-------------------------------------------------
231 * lzma_codec_free
232 *-------------------------------------------------
233 */
234
235 void lzma_codec_free(void* codec)
236 {
237 lzma_codec_data* lzma_codec = (lzma_codec_data*) codec;
238
239 /* free memory */
240 LzmaDec_Free(&lzma_codec->decoder, &lzma_codec->allocator.base);
241 lzma_allocator_free(&lzma_codec->allocator);
242 }
243
244 /*-------------------------------------------------
245 * decompress - decompress data using the LZMA
246 * codec
247 *-------------------------------------------------
248 */
249
250 chd_error lzma_codec_decompress(void* codec, const uint8_t *src, uint32_t complen, uint8_t *dest, uint32_t destlen)
251 {
252 ELzmaStatus status;
253 SRes res;
254 SizeT consumedlen, decodedlen;
255 /* initialize */
256 lzma_codec_data* lzma_codec = (lzma_codec_data*) codec;
257 LzmaDec_Init(&lzma_codec->decoder);
258
259 /* decode */
260 consumedlen = complen;
261 decodedlen = destlen;
262 res = LzmaDec_DecodeToBuf(&lzma_codec->decoder, dest, &decodedlen, src, &consumedlen, LZMA_FINISH_END, &status);
263 if ((res != SZ_OK && res != LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK) || consumedlen != complen || decodedlen != destlen)
264 return CHDERR_DECOMPRESSION_ERROR;
265 return CHDERR_NONE;
266 }