3 Test application for LZMA Decoder
5 This file written and distributed to public domain by Igor Pavlov.
6 This file is part of LZMA SDK 4.26 (2005-08-05)
13 #include "LzmaDecode.h"
15 const char *kCantReadMessage = "Can not read input file";
16 const char *kCantWriteMessage = "Can not write output file";
17 const char *kCantAllocateMessage = "Can not allocate memory";
19 size_t MyReadFile(FILE *file, void *data, size_t size)
23 return fread(data, 1, size, file);
26 int MyReadFileAndCheck(FILE *file, void *data, size_t size)
27 { return (MyReadFile(file, data, size) == size);}
29 size_t MyWriteFile(FILE *file, const void *data, size_t size)
33 return fwrite(data, 1, size, file);
36 int MyWriteFileAndCheck(FILE *file, const void *data, size_t size)
37 { return (MyWriteFile(file, data, size) == size); }
40 #define kInBufferSize (1 << 15)
41 typedef struct _CBuffer
43 ILzmaInCallback InCallback;
45 unsigned char Buffer[kInBufferSize];
48 int LzmaReadCompressed(void *object, const unsigned char **buffer, SizeT *size)
50 CBuffer *b = (CBuffer *)object;
52 *size = (SizeT)MyReadFile(b->File, b->Buffer, kInBufferSize);
53 return LZMA_RESULT_OK;
60 #define kOutBufferSize (1 << 15)
61 unsigned char g_OutBuffer[kOutBufferSize];
64 int PrintError(char *buffer, const char *message)
66 sprintf(buffer + strlen(buffer), "\nError: ");
67 sprintf(buffer + strlen(buffer), message);
71 int main3(FILE *inFile, FILE *outFile, char *rs)
73 /* We use two 32-bit integers to construct 64-bit integer for file size.
74 You can remove outSizeHigh, if you don't need >= 4GB supporting,
75 or you can use UInt64 outSize, if your compiler supports 64-bit integers*/
77 UInt32 outSizeHigh = 0;
78 #ifndef _LZMA_OUT_READ
80 unsigned char *outStream;
84 /* waitEOS = 1, if there is no uncompressed size in headers,
85 so decoder will wait EOS (End of Stream Marker) in compressed stream */
89 unsigned char *inStream;
92 CLzmaDecoderState state; /* it's about 24-80 bytes structure, if int is 32-bit */
93 unsigned char properties[LZMA_PROPERTIES_SIZE];
98 g_InBuffer.File = inFile;
101 if (sizeof(UInt32) < 4)
102 return PrintError(rs, "LZMA decoder needs correct UInt32");
107 fseek(inFile, 0, SEEK_END);
108 length = ftell(inFile);
109 fseek(inFile, 0, SEEK_SET);
110 if ((long)(SizeT)length != length)
111 return PrintError(rs, "Too big compressed stream");
112 compressedSize = (SizeT)(length - (LZMA_PROPERTIES_SIZE + 8));
116 /* Read LZMA properties for compressed stream */
118 if (!MyReadFileAndCheck(inFile, properties, sizeof(properties)))
119 return PrintError(rs, kCantReadMessage);
121 /* Read uncompressed size */
125 for (i = 0; i < 8; i++)
128 if (!MyReadFileAndCheck(inFile, &b, 1))
129 return PrintError(rs, kCantReadMessage);
133 outSize += (UInt32)(b) << (i * 8);
135 outSizeHigh += (UInt32)(b) << ((i - 4) * 8);
138 #ifndef _LZMA_OUT_READ
140 return PrintError(rs, "Stream with EOS marker is not supported");
141 outSizeFull = (SizeT)outSize;
142 if (sizeof(SizeT) >= 8)
143 outSizeFull |= (((SizeT)outSizeHigh << 16) << 16);
144 else if (outSizeHigh != 0 || (UInt32)(SizeT)outSize != outSize)
145 return PrintError(rs, "Too big uncompressed stream");
149 /* Decode LZMA properties and allocate memory */
151 if (LzmaDecodeProperties(&state.Properties, properties, LZMA_PROPERTIES_SIZE) != LZMA_RESULT_OK)
152 return PrintError(rs, "Incorrect stream properties");
153 state.Probs = (CProb *)malloc(LzmaGetNumProbs(&state.Properties) * sizeof(CProb));
155 #ifdef _LZMA_OUT_READ
156 if (state.Properties.DictionarySize == 0)
157 state.Dictionary = 0;
159 state.Dictionary = (unsigned char *)malloc(state.Properties.DictionarySize);
161 if (outSizeFull == 0)
164 outStream = (unsigned char *)malloc(outSizeFull);
168 if (compressedSize == 0)
171 inStream = (unsigned char *)malloc(compressedSize);
175 #ifdef _LZMA_OUT_READ
176 || (state.Dictionary == 0 && state.Properties.DictionarySize != 0)
178 || (outStream == 0 && outSizeFull != 0)
181 || (inStream == 0 && compressedSize != 0)
186 #ifdef _LZMA_OUT_READ
187 free(state.Dictionary);
194 return PrintError(rs, kCantAllocateMessage);
200 g_InBuffer.InCallback.Read = LzmaReadCompressed;
202 if (!MyReadFileAndCheck(inFile, inStream, compressedSize))
203 return PrintError(rs, kCantReadMessage);
206 #ifdef _LZMA_OUT_READ
209 SizeT inAvail = compressedSize;
210 const unsigned char *inBuffer = inStream;
212 LzmaDecoderInit(&state);
219 SizeT outAvail = kOutBufferSize;
220 if (!waitEOS && outSizeHigh == 0 && outAvail > outSize)
221 outAvail = (SizeT)outSize;
222 res = LzmaDecode(&state,
224 &g_InBuffer.InCallback,
226 inBuffer, inAvail, &inProcessed,
228 g_OutBuffer, outAvail, &outProcessed);
231 sprintf(rs + strlen(rs), "\nDecoding error = %d\n", res);
236 inAvail -= inProcessed;
237 inBuffer += inProcessed;
241 if (!MyWriteFileAndCheck(outFile, g_OutBuffer, (size_t)outProcessed))
243 PrintError(rs, kCantWriteMessage);
248 if (outSize < outProcessed)
250 outSize -= (UInt32)outProcessed;
251 outSize &= 0xFFFFFFFF;
253 if (outProcessed == 0)
255 if (!waitEOS && (outSize != 0 || outSizeHigh != 0))
260 while ((outSize != 0 && outSizeHigh == 0) || outSizeHigh != 0 || waitEOS);
269 res = LzmaDecode(&state,
271 &g_InBuffer.InCallback,
273 inStream, compressedSize, &inProcessed,
275 outStream, outSizeFull, &outProcessed);
278 sprintf(rs + strlen(rs), "\nDecoding error = %d\n", res);
281 else if (outFile != 0)
283 if (!MyWriteFileAndCheck(outFile, outStream, (size_t)outProcessed))
285 PrintError(rs, kCantWriteMessage);
293 #ifdef _LZMA_OUT_READ
294 free(state.Dictionary);
304 int main2(int numArgs, const char *args[], char *rs)
310 sprintf(rs + strlen(rs), "\nLZMA Decoder 4.26 Copyright (c) 1999-2005 Igor Pavlov 2005-08-05\n");
311 if (numArgs < 2 || numArgs > 3)
313 sprintf(rs + strlen(rs), "\nUsage: lzmadec file.lzma [outFile]\n");
317 inFile = fopen(args[1], "rb");
319 return PrintError(rs, "Can not open input file");
323 outFile = fopen(args[2], "wb+");
325 return PrintError(rs, "Can not open output file");
328 res = main3(inFile, outFile, rs);
336 int main(int numArgs, const char *args[])
338 char rs[800] = { 0 };
339 int res = main2(numArgs, args, rs);