RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / scripts / squashfs / lzma / C / 7zip / Compress / LZMA_C / LzmaStateTest.c
blobfa791d3580c1e44d467c4bc15aea5ad2a7c6837f
1 /*
2 LzmaStateTest.c
3 Test application for LZMA Decoder (State version)
5 This file written and distributed to public domain by Igor Pavlov.
6 This file is part of LZMA SDK 4.26 (2005-08-02)
7 */
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
13 #include "LzmaStateDecode.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 #define kInBufferSize (1 << 15)
20 #define kOutBufferSize (1 << 15)
22 unsigned char g_InBuffer[kInBufferSize];
23 unsigned char g_OutBuffer[kOutBufferSize];
25 size_t MyReadFile(FILE *file, void *data, size_t size)
26 { return fread(data, 1, size, file); }
28 int MyReadFileAndCheck(FILE *file, void *data, size_t size)
29 { return (MyReadFile(file, data, size) == size); }
31 int PrintError(char *buffer, const char *message)
33 sprintf(buffer + strlen(buffer), "\nError: ");
34 sprintf(buffer + strlen(buffer), message);
35 return 1;
38 int main3(FILE *inFile, FILE *outFile, char *rs)
40 /* We use two 32-bit integers to construct 64-bit integer for file size.
41 You can remove outSizeHigh, if you don't need >= 4GB supporting,
42 or you can use UInt64 outSize, if your compiler supports 64-bit integers*/
43 UInt32 outSize = 0;
44 UInt32 outSizeHigh = 0;
46 int waitEOS = 1;
47 /* waitEOS = 1, if there is no uncompressed size in headers,
48 so decoder will wait EOS (End of Stream Marker) in compressed stream */
50 int i;
51 int res = 0;
52 CLzmaDecoderState state; /* it's about 140 bytes structure, if int is 32-bit */
53 unsigned char properties[LZMA_PROPERTIES_SIZE];
54 SizeT inAvail = 0;
55 unsigned char *inBuffer = 0;
57 if (sizeof(UInt32) < 4)
58 return PrintError(rs, "LZMA decoder needs correct UInt32");
60 /* Read LZMA properties for compressed stream */
62 if (!MyReadFileAndCheck(inFile, properties, sizeof(properties)))
63 return PrintError(rs, kCantReadMessage);
65 /* Read uncompressed size */
67 for (i = 0; i < 8; i++)
69 unsigned char b;
70 if (!MyReadFileAndCheck(inFile, &b, 1))
71 return PrintError(rs, kCantReadMessage);
72 if (b != 0xFF)
73 waitEOS = 0;
74 if (i < 4)
75 outSize += (UInt32)(b) << (i * 8);
76 else
77 outSizeHigh += (UInt32)(b) << ((i - 4) * 8);
80 /* Decode LZMA properties and allocate memory */
82 if (LzmaDecodeProperties(&state.Properties, properties, LZMA_PROPERTIES_SIZE) != LZMA_RESULT_OK)
83 return PrintError(rs, "Incorrect stream properties");
84 state.Probs = (CProb *)malloc(LzmaGetNumProbs(&state.Properties) * sizeof(CProb));
85 if (state.Probs == 0)
86 return PrintError(rs, kCantAllocateMessage);
88 if (state.Properties.DictionarySize == 0)
89 state.Dictionary = 0;
90 else
92 state.Dictionary = (unsigned char *)malloc(state.Properties.DictionarySize);
93 if (state.Dictionary == 0)
95 free(state.Probs);
96 return PrintError(rs, kCantAllocateMessage);
100 /* Decompress */
102 LzmaDecoderInit(&state);
106 SizeT inProcessed, outProcessed;
107 int finishDecoding;
108 UInt32 outAvail = kOutBufferSize;
109 if (!waitEOS && outSizeHigh == 0 && outAvail > outSize)
110 outAvail = outSize;
111 if (inAvail == 0)
113 inAvail = (SizeT)MyReadFile(inFile, g_InBuffer, kInBufferSize);
114 inBuffer = g_InBuffer;
116 finishDecoding = (inAvail == 0);
117 res = LzmaDecode(&state,
118 inBuffer, inAvail, &inProcessed,
119 g_OutBuffer, outAvail, &outProcessed,
120 finishDecoding);
121 if (res != 0)
123 sprintf(rs + strlen(rs), "\nDecoding error = %d\n", res);
124 res = 1;
125 break;
127 inAvail -= inProcessed;
128 inBuffer += inProcessed;
130 if (outFile != 0)
131 if (fwrite(g_OutBuffer, 1, outProcessed, outFile) != outProcessed)
133 PrintError(rs, kCantWriteMessage);
134 res = 1;
135 break;
138 if (outSize < outProcessed)
139 outSizeHigh--;
140 outSize -= (UInt32)outProcessed;
141 outSize &= 0xFFFFFFFF;
143 if (outProcessed == 0 && finishDecoding)
145 if (!waitEOS && (outSize != 0 || outSizeHigh != 0))
146 res = 1;
147 break;
150 while ((outSize != 0 && outSizeHigh == 0) || outSizeHigh != 0 || waitEOS);
152 free(state.Dictionary);
153 free(state.Probs);
154 return res;
157 int main2(int numArgs, const char *args[], char *rs)
159 FILE *inFile = 0;
160 FILE *outFile = 0;
161 int res;
163 sprintf(rs + strlen(rs), "\nLZMA Decoder 4.26 Copyright (c) 1999-2005 Igor Pavlov 2005-08-02\n");
164 if (numArgs < 2 || numArgs > 3)
166 sprintf(rs + strlen(rs), "\nUsage: lzmadec file.lzma [outFile]\n");
167 return 1;
170 inFile = fopen(args[1], "rb");
171 if (inFile == 0)
172 return PrintError(rs, "Can not open input file");
174 if (numArgs > 2)
176 outFile = fopen(args[2], "wb+");
177 if (outFile == 0)
178 return PrintError(rs, "Can not open output file");
181 res = main3(inFile, outFile, rs);
183 if (outFile != 0)
184 fclose(outFile);
185 fclose(inFile);
186 return res;
189 int main(int numArgs, const char *args[])
191 char rs[800] = { 0 };
192 int res = main2(numArgs, args, rs);
193 printf(rs);
194 return res;