1 /***************************************************************************
5 MAME Compressed Hunks of Data file format
7 ****************************************************************************
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are
16 * Redistributions of source code must retain the above copyright
17 notice, this list of conditions and the following disclaimer.
18 * Redistributions in binary form must reproduce the above copyright
19 notice, this list of conditions and the following disclaimer in
20 the documentation and/or other materials provided with the
22 * Neither the name 'MAME' nor the names of its contributors may be
23 used to endorse or promote products derived from this software
24 without specific prior written permission.
26 THIS SOFTWARE IS PROVIDED BY AARON GILES ''AS IS'' AND ANY EXPRESS OR
27 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
28 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29 DISCLAIMED. IN NO EVENT SHALL AARON GILES BE LIABLE FOR ANY DIRECT,
30 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
35 IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 POSSIBILITY OF SUCH DAMAGE.
38 ***************************************************************************/
46 #include <libchdr/chd.h>
47 #include <libchdr/minmax.h>
48 #include <libchdr/cdrom.h>
49 #include <libchdr/lzma.h>
50 #include <libchdr/huffman.h>
52 #include <retro_inline.h>
53 #include <streams/file_stream.h>
58 /***************************************************************************
59 * LZMA ALLOCATOR HELPER
60 ***************************************************************************
63 /*-------------------------------------------------
64 * lzma_fast_alloc - fast malloc for lzma, which
65 * allocates and frees memory frequently
66 *-------------------------------------------------
69 /* Huge alignment values for possible SIMD optimization by compiler (NEON, SSE, AVX) */
70 #define LZMA_MIN_ALIGNMENT_BITS 512
71 #define LZMA_MIN_ALIGNMENT_BYTES (LZMA_MIN_ALIGNMENT_BITS / 8)
73 static void *lzma_fast_alloc(void *p, size_t size)
76 uint32_t *addr = NULL;
77 lzma_allocator *codec = (lzma_allocator *)(p);
80 /* compute the size, rounding to the nearest 1k */
81 size = (size + 0x3ff) & ~0x3ff;
83 /* reuse a hunk if we can */
84 for (scan = 0; scan < MAX_LZMA_ALLOCS; scan++)
86 uint32_t *ptr = codec->allocptr[scan];
87 if (ptr != NULL && size == *ptr)
89 /* set the low bit of the size so we don't match next time */
92 /* return aligned address of the block */
93 return codec->allocptr2[scan];
97 /* alloc a new one and put it into the list */
98 addr = (uint32_t *)malloc(size + sizeof(uint32_t) + LZMA_MIN_ALIGNMENT_BYTES);
101 for (scan = 0; scan < MAX_LZMA_ALLOCS; scan++)
103 if (codec->allocptr[scan] == NULL)
105 /* store block address */
106 codec->allocptr[scan] = addr;
108 /* compute aligned address, store it */
109 vaddr = (uintptr_t)addr;
110 vaddr = (vaddr + sizeof(uint32_t) + (LZMA_MIN_ALIGNMENT_BYTES-1)) & (~(LZMA_MIN_ALIGNMENT_BYTES-1));
111 codec->allocptr2[scan] = (uint32_t*)vaddr;
116 /* set the low bit of the size so we don't match next time */
119 /* return aligned address */
123 /*-------------------------------------------------
124 * lzma_fast_free - fast free for lzma, which
125 * allocates and frees memory frequently
126 *-------------------------------------------------
128 static void lzma_fast_free(void *p, void *address)
131 uint32_t *ptr = NULL;
132 lzma_allocator *codec = NULL;
137 codec = (lzma_allocator *)(p);
140 ptr = (uint32_t *)address;
141 for (scan = 0; scan < MAX_LZMA_ALLOCS; scan++)
143 if (ptr == codec->allocptr2[scan])
145 /* clear the low bit of the size to allow matches */
146 *codec->allocptr[scan] &= ~1;
152 /*-------------------------------------------------
153 * lzma_allocator_init
154 *-------------------------------------------------
157 void lzma_allocator_init(void* p)
159 lzma_allocator *codec = (lzma_allocator *)(p);
161 /* reset pointer list */
162 memset(codec->allocptr, 0, sizeof(codec->allocptr));
163 codec->Alloc = lzma_fast_alloc;
164 codec->Free = lzma_fast_free;
167 /*-------------------------------------------------
168 * lzma_allocator_free
169 *-------------------------------------------------
172 void lzma_allocator_free(void* p )
174 lzma_allocator *codec = (lzma_allocator *)(p);
176 /* free our memory */
178 for (i = 0 ; i < MAX_LZMA_ALLOCS ; i++)
180 if (codec->allocptr[i] != NULL)
181 free(codec->allocptr[i]);
185 /***************************************************************************
187 ***************************************************************************
190 /*-------------------------------------------------
191 * lzma_codec_init - constructor
192 *-------------------------------------------------
195 chd_error lzma_codec_init(void* codec, uint32_t hunkbytes)
197 CLzmaEncProps encoder_props;
199 uint8_t decoder_props[LZMA_PROPS_SIZE];
200 lzma_allocator* alloc;
202 lzma_codec_data* lzma_codec = (lzma_codec_data*) codec;
204 /* construct the decoder */
205 LzmaDec_Construct(&lzma_codec->decoder);
207 /* FIXME: this code is written in a way that makes it impossible to safely upgrade the LZMA SDK
208 * This code assumes that the current version of the encoder imposes the same requirements on the
209 * decoder as the encoder used to produce the file. This is not necessarily true. The format
210 * needs to be changed so the encoder properties are written to the file.
212 * configure the properties like the compressor did */
213 LzmaEncProps_Init(&encoder_props);
214 encoder_props.level = 9;
215 encoder_props.reduceSize = hunkbytes;
216 LzmaEncProps_Normalize(&encoder_props);
218 /* convert to decoder properties */
219 alloc = &lzma_codec->allocator;
220 lzma_allocator_init(alloc);
221 enc = LzmaEnc_Create((ISzAlloc*)alloc);
223 return CHDERR_DECOMPRESSION_ERROR;
224 if (LzmaEnc_SetProps(enc, &encoder_props) != SZ_OK)
226 LzmaEnc_Destroy(enc, (ISzAlloc*)&alloc, (ISzAlloc*)&alloc);
227 return CHDERR_DECOMPRESSION_ERROR;
229 props_size = sizeof(decoder_props);
230 if (LzmaEnc_WriteProperties(enc, decoder_props, &props_size) != SZ_OK)
232 LzmaEnc_Destroy(enc, (ISzAlloc*)alloc, (ISzAlloc*)alloc);
233 return CHDERR_DECOMPRESSION_ERROR;
235 LzmaEnc_Destroy(enc, (ISzAlloc*)alloc, (ISzAlloc*)alloc);
237 /* do memory allocations */
238 if (LzmaDec_Allocate(&lzma_codec->decoder, decoder_props, LZMA_PROPS_SIZE, (ISzAlloc*)alloc) != SZ_OK)
239 return CHDERR_DECOMPRESSION_ERROR;
245 /*-------------------------------------------------
247 *-------------------------------------------------
250 void lzma_codec_free(void* codec)
252 lzma_codec_data* lzma_codec = (lzma_codec_data*) codec;
253 lzma_allocator* alloc = &lzma_codec->allocator;
256 LzmaDec_Free(&lzma_codec->decoder, (ISzAlloc*)&lzma_codec->allocator);
257 lzma_allocator_free(alloc);
260 /*-------------------------------------------------
261 * decompress - decompress data using the LZMA
263 *-------------------------------------------------
266 chd_error lzma_codec_decompress(void* codec, const uint8_t *src, uint32_t complen, uint8_t *dest, uint32_t destlen)
270 size_t consumedlen, decodedlen;
272 lzma_codec_data* lzma_codec = (lzma_codec_data*) codec;
273 LzmaDec_Init(&lzma_codec->decoder);
276 consumedlen = complen;
277 decodedlen = destlen;
278 res = LzmaDec_DecodeToBuf(&lzma_codec->decoder, dest, &decodedlen, src, &consumedlen, LZMA_FINISH_END, &status);
279 if ((res != SZ_OK && res != LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK) || consumedlen != complen || decodedlen != destlen)
280 return CHDERR_DECOMPRESSION_ERROR;
285 chd_error cdlz_codec_init(void* codec, uint32_t hunkbytes)
288 cdlz_codec_data* cdlz = (cdlz_codec_data*) codec;
290 /* allocate buffer */
291 cdlz->buffer = (uint8_t*)malloc(sizeof(uint8_t) * hunkbytes);
292 if (cdlz->buffer == NULL)
293 return CHDERR_OUT_OF_MEMORY;
295 ret = lzma_codec_init(&cdlz->base_decompressor, (hunkbytes / CD_FRAME_SIZE) * CD_MAX_SECTOR_DATA);
296 if (ret != CHDERR_NONE)
300 ret = zlib_codec_init(&cdlz->subcode_decompressor, (hunkbytes / CD_FRAME_SIZE) * CD_MAX_SECTOR_DATA);
301 if (ret != CHDERR_NONE)
308 void cdlz_codec_free(void* codec)
310 cdlz_codec_data* cdlz = (cdlz_codec_data*) codec;
312 lzma_codec_free(&cdlz->base_decompressor);
314 zlib_codec_free(&cdlz->subcode_decompressor);
320 chd_error cdlz_codec_decompress(void *codec, const uint8_t *src, uint32_t complen, uint8_t *dest, uint32_t destlen)
322 #ifdef WANT_RAW_DATA_SECTOR
326 cdlz_codec_data* cdlz = (cdlz_codec_data*)codec;
328 /* determine header bytes */
329 uint32_t frames = destlen / CD_FRAME_SIZE;
330 uint32_t complen_bytes = (destlen < 65536) ? 2 : 3;
331 uint32_t ecc_bytes = (frames + 7) / 8;
332 uint32_t header_bytes = ecc_bytes + complen_bytes;
334 /* extract compressed length of base */
335 uint32_t complen_base = (src[ecc_bytes + 0] << 8) | src[ecc_bytes + 1];
336 if (complen_bytes > 2)
337 complen_base = (complen_base << 8) | src[ecc_bytes + 2];
339 /* reset and decode */
340 lzma_codec_decompress(&cdlz->base_decompressor, &src[header_bytes], complen_base, &cdlz->buffer[0], frames * CD_MAX_SECTOR_DATA);
342 if (header_bytes + complen_base >= complen)
343 return CHDERR_DECOMPRESSION_ERROR;
344 zlib_codec_decompress(&cdlz->subcode_decompressor, &src[header_bytes + complen_base], complen - complen_base - header_bytes, &cdlz->buffer[frames * CD_MAX_SECTOR_DATA], frames * CD_MAX_SUBCODE_DATA);
347 /* reassemble the data */
348 for (framenum = 0; framenum < frames; framenum++)
350 memcpy(&dest[framenum * CD_FRAME_SIZE], &cdlz->buffer[framenum * CD_MAX_SECTOR_DATA], CD_MAX_SECTOR_DATA);
352 memcpy(&dest[framenum * CD_FRAME_SIZE + CD_MAX_SECTOR_DATA], &cdlz->buffer[frames * CD_MAX_SECTOR_DATA + framenum * CD_MAX_SUBCODE_DATA], CD_MAX_SUBCODE_DATA);
355 #ifdef WANT_RAW_DATA_SECTOR
356 /* reconstitute the ECC data and sync header */
357 sector = (uint8_t *)&dest[framenum * CD_FRAME_SIZE];
358 if ((src[framenum / 8] & (1 << (framenum % 8))) != 0)
360 memcpy(sector, s_cd_sync_header, sizeof(s_cd_sync_header));
361 ecc_generate(sector);