aboutsummaryrefslogtreecommitdiff
path: root/C/Util/Lzma
diff options
context:
space:
mode:
authorIgor Pavlov <87184205+ip7z@users.noreply.github.com>2023-06-21 00:00:00 +0000
committerIgor Pavlov <87184205+ip7z@users.noreply.github.com>2023-12-17 14:59:19 +0500
commit5b39dc76f1bc82f941d5c800ab9f34407a06b53a (patch)
treefe5e17420300b715021a76328444088d32047963 /C/Util/Lzma
parent93be7d4abfd4233228f58ee1fbbcd76d91be66a4 (diff)
download7zip-5b39dc76f1bc82f941d5c800ab9f34407a06b53a.tar.gz
7zip-5b39dc76f1bc82f941d5c800ab9f34407a06b53a.tar.bz2
7zip-5b39dc76f1bc82f941d5c800ab9f34407a06b53a.zip
23.0123.01
Diffstat (limited to 'C/Util/Lzma')
-rw-r--r--C/Util/Lzma/LzmaUtil.c145
-rw-r--r--C/Util/Lzma/LzmaUtil.dsp16
-rw-r--r--C/Util/Lzma/Precomp.h14
3 files changed, 116 insertions, 59 deletions
diff --git a/C/Util/Lzma/LzmaUtil.c b/C/Util/Lzma/LzmaUtil.c
index 62a5907..b9b974b 100644
--- a/C/Util/Lzma/LzmaUtil.c
+++ b/C/Util/Lzma/LzmaUtil.c
@@ -1,7 +1,7 @@
1/* LzmaUtil.c -- Test application for LZMA compression 1/* LzmaUtil.c -- Test application for LZMA compression
22021-11-01 : Igor Pavlov : Public domain */ 22023-03-07 : Igor Pavlov : Public domain */
3 3
4#include "../../Precomp.h" 4#include "Precomp.h"
5 5
6#include <stdio.h> 6#include <stdio.h>
7#include <stdlib.h> 7#include <stdlib.h>
@@ -21,48 +21,80 @@ static const char * const kCantWriteMessage = "Cannot write output file";
21static const char * const kCantAllocateMessage = "Cannot allocate memory"; 21static const char * const kCantAllocateMessage = "Cannot allocate memory";
22static const char * const kDataErrorMessage = "Data error"; 22static const char * const kDataErrorMessage = "Data error";
23 23
24static void PrintHelp(char *buffer) 24static void Print(const char *s)
25{ 25{
26 strcat(buffer, 26 fputs(s, stdout);
27 "\nLZMA-C " MY_VERSION_CPU " : " MY_COPYRIGHT_DATE "\n\n"
28 "Usage: lzma <e|d> inputFile outputFile\n"
29 " e: encode file\n"
30 " d: decode file\n");
31} 27}
32 28
33static int PrintError(char *buffer, const char *message) 29static void PrintHelp(void)
34{ 30{
35 strcat(buffer, "\nError: "); 31 Print(
36 strcat(buffer, message); 32 "\n" "LZMA-C " MY_VERSION_CPU " : " MY_COPYRIGHT_DATE
37 strcat(buffer, "\n"); 33 "\n"
34 "\n" "Usage: lzma <e|d> inputFile outputFile"
35 "\n" " e: encode file"
36 "\n" " d: decode file"
37 "\n");
38}
39
40static int PrintError(const char *message)
41{
42 Print("\nError: ");
43 Print(message);
44 Print("\n");
38 return 1; 45 return 1;
39} 46}
40 47
41static int PrintError_WRes(char *buffer, const char *message, WRes wres) 48#define CONVERT_INT_TO_STR(charType, tempSize) \
49 unsigned char temp[tempSize]; unsigned i = 0; \
50 while (val >= 10) { temp[i++] = (unsigned char)('0' + (unsigned)(val % 10)); val /= 10; } \
51 *s++ = (charType)('0' + (unsigned)val); \
52 while (i != 0) { i--; *s++ = (charType)temp[i]; } \
53 *s = 0; \
54 return s;
55
56static char * Convert_unsigned_To_str(unsigned val, char *s)
42{ 57{
43 strcat(buffer, "\nError: "); 58 CONVERT_INT_TO_STR(char, 32)
44 strcat(buffer, message); 59}
45 sprintf(buffer + strlen(buffer), "\nSystem error code: %d", (unsigned)wres); 60
61static void Print_unsigned(unsigned code)
62{
63 char str[32];
64 Convert_unsigned_To_str(code, str);
65 Print(str);
66}
67
68static int PrintError_WRes(const char *message, WRes wres)
69{
70 PrintError(message);
71 Print("\nSystem error code: ");
72 Print_unsigned((unsigned)wres);
46 #ifndef _WIN32 73 #ifndef _WIN32
47 { 74 {
48 const char *s = strerror(wres); 75 const char *s = strerror(wres);
49 if (s) 76 if (s)
50 sprintf(buffer + strlen(buffer), " : %s", s); 77 {
78 Print(" : ");
79 Print(s);
80 }
51 } 81 }
52 #endif 82 #endif
53 strcat(buffer, "\n"); 83 Print("\n");
54 return 1; 84 return 1;
55} 85}
56 86
57static int PrintErrorNumber(char *buffer, SRes val) 87static int PrintErrorNumber(SRes val)
58{ 88{
59 sprintf(buffer + strlen(buffer), "\n7-Zip error code: %d\n", (unsigned)val); 89 Print("\n7-Zip error code: ");
90 Print_unsigned((unsigned)val);
91 Print("\n");
60 return 1; 92 return 1;
61} 93}
62 94
63static int PrintUserError(char *buffer) 95static int PrintUserError(void)
64{ 96{
65 return PrintError(buffer, "Incorrect command"); 97 return PrintError("Incorrect command");
66} 98}
67 99
68 100
@@ -70,10 +102,10 @@ static int PrintUserError(char *buffer)
70#define OUT_BUF_SIZE (1 << 16) 102#define OUT_BUF_SIZE (1 << 16)
71 103
72 104
73static SRes Decode2(CLzmaDec *state, ISeqOutStream *outStream, ISeqInStream *inStream, 105static SRes Decode2(CLzmaDec *state, ISeqOutStreamPtr outStream, ISeqInStreamPtr inStream,
74 UInt64 unpackSize) 106 UInt64 unpackSize)
75{ 107{
76 int thereIsSize = (unpackSize != (UInt64)(Int64)-1); 108 const int thereIsSize = (unpackSize != (UInt64)(Int64)-1);
77 Byte inBuf[IN_BUF_SIZE]; 109 Byte inBuf[IN_BUF_SIZE];
78 Byte outBuf[OUT_BUF_SIZE]; 110 Byte outBuf[OUT_BUF_SIZE];
79 size_t inPos = 0, inSize = 0, outPos = 0; 111 size_t inPos = 0, inSize = 0, outPos = 0;
@@ -83,7 +115,7 @@ static SRes Decode2(CLzmaDec *state, ISeqOutStream *outStream, ISeqInStream *inS
83 if (inPos == inSize) 115 if (inPos == inSize)
84 { 116 {
85 inSize = IN_BUF_SIZE; 117 inSize = IN_BUF_SIZE;
86 RINOK(inStream->Read(inStream, inBuf, &inSize)); 118 RINOK(inStream->Read(inStream, inBuf, &inSize))
87 inPos = 0; 119 inPos = 0;
88 } 120 }
89 { 121 {
@@ -124,7 +156,7 @@ static SRes Decode2(CLzmaDec *state, ISeqOutStream *outStream, ISeqInStream *inS
124} 156}
125 157
126 158
127static SRes Decode(ISeqOutStream *outStream, ISeqInStream *inStream) 159static SRes Decode(ISeqOutStreamPtr outStream, ISeqInStreamPtr inStream)
128{ 160{
129 UInt64 unpackSize; 161 UInt64 unpackSize;
130 int i; 162 int i;
@@ -137,27 +169,29 @@ static SRes Decode(ISeqOutStream *outStream, ISeqInStream *inStream)
137 169
138 /* Read and parse header */ 170 /* Read and parse header */
139 171
140 RINOK(SeqInStream_Read(inStream, header, sizeof(header))); 172 {
141 173 size_t size = sizeof(header);
174 RINOK(SeqInStream_ReadMax(inStream, header, &size))
175 if (size != sizeof(header))
176 return SZ_ERROR_INPUT_EOF;
177 }
142 unpackSize = 0; 178 unpackSize = 0;
143 for (i = 0; i < 8; i++) 179 for (i = 0; i < 8; i++)
144 unpackSize += (UInt64)header[LZMA_PROPS_SIZE + i] << (i * 8); 180 unpackSize += (UInt64)header[LZMA_PROPS_SIZE + i] << (i * 8);
145 181
146 LzmaDec_Construct(&state); 182 LzmaDec_CONSTRUCT(&state)
147 RINOK(LzmaDec_Allocate(&state, header, LZMA_PROPS_SIZE, &g_Alloc)); 183 RINOK(LzmaDec_Allocate(&state, header, LZMA_PROPS_SIZE, &g_Alloc))
148 res = Decode2(&state, outStream, inStream, unpackSize); 184 res = Decode2(&state, outStream, inStream, unpackSize);
149 LzmaDec_Free(&state, &g_Alloc); 185 LzmaDec_Free(&state, &g_Alloc);
150 return res; 186 return res;
151} 187}
152 188
153static SRes Encode(ISeqOutStream *outStream, ISeqInStream *inStream, UInt64 fileSize, char *rs) 189static SRes Encode(ISeqOutStreamPtr outStream, ISeqInStreamPtr inStream, UInt64 fileSize)
154{ 190{
155 CLzmaEncHandle enc; 191 CLzmaEncHandle enc;
156 SRes res; 192 SRes res;
157 CLzmaEncProps props; 193 CLzmaEncProps props;
158 194
159 UNUSED_VAR(rs);
160
161 enc = LzmaEnc_Create(&g_Alloc); 195 enc = LzmaEnc_Create(&g_Alloc);
162 if (enc == 0) 196 if (enc == 0)
163 return SZ_ERROR_MEM; 197 return SZ_ERROR_MEM;
@@ -187,7 +221,7 @@ static SRes Encode(ISeqOutStream *outStream, ISeqInStream *inStream, UInt64 file
187} 221}
188 222
189 223
190static int main2(int numArgs, const char *args[], char *rs) 224int Z7_CDECL main(int numArgs, const char *args[])
191{ 225{
192 CFileSeqInStream inStream; 226 CFileSeqInStream inStream;
193 CFileOutStream outStream; 227 CFileOutStream outStream;
@@ -208,29 +242,31 @@ static int main2(int numArgs, const char *args[], char *rs)
208 242
209 if (numArgs == 1) 243 if (numArgs == 1)
210 { 244 {
211 PrintHelp(rs); 245 PrintHelp();
212 return 0; 246 return 0;
213 } 247 }
214 248
215 if (numArgs < 3 || numArgs > 4 || strlen(args[1]) != 1) 249 if (numArgs < 3 || numArgs > 4 || strlen(args[1]) != 1)
216 return PrintUserError(rs); 250 return PrintUserError();
217 251
218 c = args[1][0]; 252 c = args[1][0];
219 encodeMode = (c == 'e' || c == 'E'); 253 encodeMode = (c == 'e' || c == 'E');
220 if (!encodeMode && c != 'd' && c != 'D') 254 if (!encodeMode && c != 'd' && c != 'D')
221 return PrintUserError(rs); 255 return PrintUserError();
222 256
257 /*
223 { 258 {
224 size_t t4 = sizeof(UInt32); 259 size_t t4 = sizeof(UInt32);
225 size_t t8 = sizeof(UInt64); 260 size_t t8 = sizeof(UInt64);
226 if (t4 != 4 || t8 != 8) 261 if (t4 != 4 || t8 != 8)
227 return PrintError(rs, "Incorrect UInt32 or UInt64"); 262 return PrintError("Incorrect UInt32 or UInt64");
228 } 263 }
264 */
229 265
230 { 266 {
231 WRes wres = InFile_Open(&inStream.file, args[2]); 267 const WRes wres = InFile_Open(&inStream.file, args[2]);
232 if (wres != 0) 268 if (wres != 0)
233 return PrintError_WRes(rs, "Cannot open input file", wres); 269 return PrintError_WRes("Cannot open input file", wres);
234 } 270 }
235 271
236 if (numArgs > 3) 272 if (numArgs > 3)
@@ -239,18 +275,18 @@ static int main2(int numArgs, const char *args[], char *rs)
239 useOutFile = True; 275 useOutFile = True;
240 wres = OutFile_Open(&outStream.file, args[3]); 276 wres = OutFile_Open(&outStream.file, args[3]);
241 if (wres != 0) 277 if (wres != 0)
242 return PrintError_WRes(rs, "Cannot open output file", wres); 278 return PrintError_WRes("Cannot open output file", wres);
243 } 279 }
244 else if (encodeMode) 280 else if (encodeMode)
245 PrintUserError(rs); 281 PrintUserError();
246 282
247 if (encodeMode) 283 if (encodeMode)
248 { 284 {
249 UInt64 fileSize; 285 UInt64 fileSize;
250 WRes wres = File_GetLength(&inStream.file, &fileSize); 286 const WRes wres = File_GetLength(&inStream.file, &fileSize);
251 if (wres != 0) 287 if (wres != 0)
252 return PrintError_WRes(rs, "Cannot get file length", wres); 288 return PrintError_WRes("Cannot get file length", wres);
253 res = Encode(&outStream.vt, &inStream.vt, fileSize, rs); 289 res = Encode(&outStream.vt, &inStream.vt, fileSize);
254 } 290 }
255 else 291 else
256 { 292 {
@@ -264,23 +300,14 @@ static int main2(int numArgs, const char *args[], char *rs)
264 if (res != SZ_OK) 300 if (res != SZ_OK)
265 { 301 {
266 if (res == SZ_ERROR_MEM) 302 if (res == SZ_ERROR_MEM)
267 return PrintError(rs, kCantAllocateMessage); 303 return PrintError(kCantAllocateMessage);
268 else if (res == SZ_ERROR_DATA) 304 else if (res == SZ_ERROR_DATA)
269 return PrintError(rs, kDataErrorMessage); 305 return PrintError(kDataErrorMessage);
270 else if (res == SZ_ERROR_WRITE) 306 else if (res == SZ_ERROR_WRITE)
271 return PrintError_WRes(rs, kCantWriteMessage, outStream.wres); 307 return PrintError_WRes(kCantWriteMessage, outStream.wres);
272 else if (res == SZ_ERROR_READ) 308 else if (res == SZ_ERROR_READ)
273 return PrintError_WRes(rs, kCantReadMessage, inStream.wres); 309 return PrintError_WRes(kCantReadMessage, inStream.wres);
274 return PrintErrorNumber(rs, res); 310 return PrintErrorNumber(res);
275 } 311 }
276 return 0; 312 return 0;
277} 313}
278
279
280int MY_CDECL main(int numArgs, const char *args[])
281{
282 char rs[1000] = { 0 };
283 int res = main2(numArgs, args, rs);
284 fputs(rs, stdout);
285 return res;
286}
diff --git a/C/Util/Lzma/LzmaUtil.dsp b/C/Util/Lzma/LzmaUtil.dsp
index 4e38e4a..e2e7d42 100644
--- a/C/Util/Lzma/LzmaUtil.dsp
+++ b/C/Util/Lzma/LzmaUtil.dsp
@@ -106,6 +106,10 @@ SOURCE=..\..\7zVersion.h
106# End Source File 106# End Source File
107# Begin Source File 107# Begin Source File
108 108
109SOURCE=..\..\7zWindows.h
110# End Source File
111# Begin Source File
112
109SOURCE=..\..\Alloc.c 113SOURCE=..\..\Alloc.c
110# End Source File 114# End Source File
111# Begin Source File 115# Begin Source File
@@ -114,6 +118,10 @@ SOURCE=..\..\Alloc.h
114# End Source File 118# End Source File
115# Begin Source File 119# Begin Source File
116 120
121SOURCE=..\..\Compiler.h
122# End Source File
123# Begin Source File
124
117SOURCE=..\..\CpuArch.h 125SOURCE=..\..\CpuArch.h
118# End Source File 126# End Source File
119# Begin Source File 127# Begin Source File
@@ -162,6 +170,14 @@ SOURCE=.\LzmaUtil.c
162# End Source File 170# End Source File
163# Begin Source File 171# Begin Source File
164 172
173SOURCE=..\..\Precomp.h
174# End Source File
175# Begin Source File
176
177SOURCE=.\Precomp.h
178# End Source File
179# Begin Source File
180
165SOURCE=..\..\Threads.c 181SOURCE=..\..\Threads.c
166# End Source File 182# End Source File
167# Begin Source File 183# Begin Source File
diff --git a/C/Util/Lzma/Precomp.h b/C/Util/Lzma/Precomp.h
new file mode 100644
index 0000000..bc8fa21
--- /dev/null
+++ b/C/Util/Lzma/Precomp.h
@@ -0,0 +1,14 @@
1/* Precomp.h -- StdAfx
22023-03-04 : Igor Pavlov : Public domain */
3
4#ifndef ZIP7_INC_PRECOMP_H
5#define ZIP7_INC_PRECOMP_H
6
7#if defined(_MSC_VER) && _MSC_VER >= 1800
8#pragma warning(disable : 4464) // relative include path contains '..'
9#endif
10
11#include "../../Compiler.h"
12#include "../../7zTypes.h"
13
14#endif