aboutsummaryrefslogtreecommitdiff
path: root/C/Util/Lzma
diff options
context:
space:
mode:
Diffstat (limited to 'C/Util/Lzma')
-rw-r--r--C/Util/Lzma/LzmaUtil.c286
-rw-r--r--C/Util/Lzma/LzmaUtil.dsp172
-rw-r--r--C/Util/Lzma/LzmaUtil.dsw29
-rw-r--r--C/Util/Lzma/makefile30
-rw-r--r--C/Util/Lzma/makefile.gcc21
5 files changed, 538 insertions, 0 deletions
diff --git a/C/Util/Lzma/LzmaUtil.c b/C/Util/Lzma/LzmaUtil.c
new file mode 100644
index 0000000..62a5907
--- /dev/null
+++ b/C/Util/Lzma/LzmaUtil.c
@@ -0,0 +1,286 @@
1/* LzmaUtil.c -- Test application for LZMA compression
22021-11-01 : Igor Pavlov : Public domain */
3
4#include "../../Precomp.h"
5
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9
10#include "../../CpuArch.h"
11
12#include "../../Alloc.h"
13#include "../../7zFile.h"
14#include "../../7zVersion.h"
15#include "../../LzFind.h"
16#include "../../LzmaDec.h"
17#include "../../LzmaEnc.h"
18
19static const char * const kCantReadMessage = "Cannot read input file";
20static const char * const kCantWriteMessage = "Cannot write output file";
21static const char * const kCantAllocateMessage = "Cannot allocate memory";
22static const char * const kDataErrorMessage = "Data error";
23
24static void PrintHelp(char *buffer)
25{
26 strcat(buffer,
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}
32
33static int PrintError(char *buffer, const char *message)
34{
35 strcat(buffer, "\nError: ");
36 strcat(buffer, message);
37 strcat(buffer, "\n");
38 return 1;
39}
40
41static int PrintError_WRes(char *buffer, const char *message, WRes wres)
42{
43 strcat(buffer, "\nError: ");
44 strcat(buffer, message);
45 sprintf(buffer + strlen(buffer), "\nSystem error code: %d", (unsigned)wres);
46 #ifndef _WIN32
47 {
48 const char *s = strerror(wres);
49 if (s)
50 sprintf(buffer + strlen(buffer), " : %s", s);
51 }
52 #endif
53 strcat(buffer, "\n");
54 return 1;
55}
56
57static int PrintErrorNumber(char *buffer, SRes val)
58{
59 sprintf(buffer + strlen(buffer), "\n7-Zip error code: %d\n", (unsigned)val);
60 return 1;
61}
62
63static int PrintUserError(char *buffer)
64{
65 return PrintError(buffer, "Incorrect command");
66}
67
68
69#define IN_BUF_SIZE (1 << 16)
70#define OUT_BUF_SIZE (1 << 16)
71
72
73static SRes Decode2(CLzmaDec *state, ISeqOutStream *outStream, ISeqInStream *inStream,
74 UInt64 unpackSize)
75{
76 int thereIsSize = (unpackSize != (UInt64)(Int64)-1);
77 Byte inBuf[IN_BUF_SIZE];
78 Byte outBuf[OUT_BUF_SIZE];
79 size_t inPos = 0, inSize = 0, outPos = 0;
80 LzmaDec_Init(state);
81 for (;;)
82 {
83 if (inPos == inSize)
84 {
85 inSize = IN_BUF_SIZE;
86 RINOK(inStream->Read(inStream, inBuf, &inSize));
87 inPos = 0;
88 }
89 {
90 SRes res;
91 SizeT inProcessed = inSize - inPos;
92 SizeT outProcessed = OUT_BUF_SIZE - outPos;
93 ELzmaFinishMode finishMode = LZMA_FINISH_ANY;
94 ELzmaStatus status;
95 if (thereIsSize && outProcessed > unpackSize)
96 {
97 outProcessed = (SizeT)unpackSize;
98 finishMode = LZMA_FINISH_END;
99 }
100
101 res = LzmaDec_DecodeToBuf(state, outBuf + outPos, &outProcessed,
102 inBuf + inPos, &inProcessed, finishMode, &status);
103 inPos += inProcessed;
104 outPos += outProcessed;
105 unpackSize -= outProcessed;
106
107 if (outStream)
108 if (outStream->Write(outStream, outBuf, outPos) != outPos)
109 return SZ_ERROR_WRITE;
110
111 outPos = 0;
112
113 if (res != SZ_OK || (thereIsSize && unpackSize == 0))
114 return res;
115
116 if (inProcessed == 0 && outProcessed == 0)
117 {
118 if (thereIsSize || status != LZMA_STATUS_FINISHED_WITH_MARK)
119 return SZ_ERROR_DATA;
120 return res;
121 }
122 }
123 }
124}
125
126
127static SRes Decode(ISeqOutStream *outStream, ISeqInStream *inStream)
128{
129 UInt64 unpackSize;
130 int i;
131 SRes res = 0;
132
133 CLzmaDec state;
134
135 /* header: 5 bytes of LZMA properties and 8 bytes of uncompressed size */
136 unsigned char header[LZMA_PROPS_SIZE + 8];
137
138 /* Read and parse header */
139
140 RINOK(SeqInStream_Read(inStream, header, sizeof(header)));
141
142 unpackSize = 0;
143 for (i = 0; i < 8; i++)
144 unpackSize += (UInt64)header[LZMA_PROPS_SIZE + i] << (i * 8);
145
146 LzmaDec_Construct(&state);
147 RINOK(LzmaDec_Allocate(&state, header, LZMA_PROPS_SIZE, &g_Alloc));
148 res = Decode2(&state, outStream, inStream, unpackSize);
149 LzmaDec_Free(&state, &g_Alloc);
150 return res;
151}
152
153static SRes Encode(ISeqOutStream *outStream, ISeqInStream *inStream, UInt64 fileSize, char *rs)
154{
155 CLzmaEncHandle enc;
156 SRes res;
157 CLzmaEncProps props;
158
159 UNUSED_VAR(rs);
160
161 enc = LzmaEnc_Create(&g_Alloc);
162 if (enc == 0)
163 return SZ_ERROR_MEM;
164
165 LzmaEncProps_Init(&props);
166 res = LzmaEnc_SetProps(enc, &props);
167
168 if (res == SZ_OK)
169 {
170 Byte header[LZMA_PROPS_SIZE + 8];
171 size_t headerSize = LZMA_PROPS_SIZE;
172 int i;
173
174 res = LzmaEnc_WriteProperties(enc, header, &headerSize);
175 for (i = 0; i < 8; i++)
176 header[headerSize++] = (Byte)(fileSize >> (8 * i));
177 if (outStream->Write(outStream, header, headerSize) != headerSize)
178 res = SZ_ERROR_WRITE;
179 else
180 {
181 if (res == SZ_OK)
182 res = LzmaEnc_Encode(enc, outStream, inStream, NULL, &g_Alloc, &g_Alloc);
183 }
184 }
185 LzmaEnc_Destroy(enc, &g_Alloc, &g_Alloc);
186 return res;
187}
188
189
190static int main2(int numArgs, const char *args[], char *rs)
191{
192 CFileSeqInStream inStream;
193 CFileOutStream outStream;
194 char c;
195 int res;
196 int encodeMode;
197 BoolInt useOutFile = False;
198
199 LzFindPrepare();
200
201 FileSeqInStream_CreateVTable(&inStream);
202 File_Construct(&inStream.file);
203 inStream.wres = 0;
204
205 FileOutStream_CreateVTable(&outStream);
206 File_Construct(&outStream.file);
207 outStream.wres = 0;
208
209 if (numArgs == 1)
210 {
211 PrintHelp(rs);
212 return 0;
213 }
214
215 if (numArgs < 3 || numArgs > 4 || strlen(args[1]) != 1)
216 return PrintUserError(rs);
217
218 c = args[1][0];
219 encodeMode = (c == 'e' || c == 'E');
220 if (!encodeMode && c != 'd' && c != 'D')
221 return PrintUserError(rs);
222
223 {
224 size_t t4 = sizeof(UInt32);
225 size_t t8 = sizeof(UInt64);
226 if (t4 != 4 || t8 != 8)
227 return PrintError(rs, "Incorrect UInt32 or UInt64");
228 }
229
230 {
231 WRes wres = InFile_Open(&inStream.file, args[2]);
232 if (wres != 0)
233 return PrintError_WRes(rs, "Cannot open input file", wres);
234 }
235
236 if (numArgs > 3)
237 {
238 WRes wres;
239 useOutFile = True;
240 wres = OutFile_Open(&outStream.file, args[3]);
241 if (wres != 0)
242 return PrintError_WRes(rs, "Cannot open output file", wres);
243 }
244 else if (encodeMode)
245 PrintUserError(rs);
246
247 if (encodeMode)
248 {
249 UInt64 fileSize;
250 WRes wres = File_GetLength(&inStream.file, &fileSize);
251 if (wres != 0)
252 return PrintError_WRes(rs, "Cannot get file length", wres);
253 res = Encode(&outStream.vt, &inStream.vt, fileSize, rs);
254 }
255 else
256 {
257 res = Decode(&outStream.vt, useOutFile ? &inStream.vt : NULL);
258 }
259
260 if (useOutFile)
261 File_Close(&outStream.file);
262 File_Close(&inStream.file);
263
264 if (res != SZ_OK)
265 {
266 if (res == SZ_ERROR_MEM)
267 return PrintError(rs, kCantAllocateMessage);
268 else if (res == SZ_ERROR_DATA)
269 return PrintError(rs, kDataErrorMessage);
270 else if (res == SZ_ERROR_WRITE)
271 return PrintError_WRes(rs, kCantWriteMessage, outStream.wres);
272 else if (res == SZ_ERROR_READ)
273 return PrintError_WRes(rs, kCantReadMessage, inStream.wres);
274 return PrintErrorNumber(rs, res);
275 }
276 return 0;
277}
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
new file mode 100644
index 0000000..4e38e4a
--- /dev/null
+++ b/C/Util/Lzma/LzmaUtil.dsp
@@ -0,0 +1,172 @@
1# Microsoft Developer Studio Project File - Name="LzmaUtil" - Package Owner=<4>
2# Microsoft Developer Studio Generated Build File, Format Version 6.00
3# ** DO NOT EDIT **
4
5# TARGTYPE "Win32 (x86) Console Application" 0x0103
6
7CFG=LzmaUtil - Win32 Debug
8!MESSAGE This is not a valid makefile. To build this project using NMAKE,
9!MESSAGE use the Export Makefile command and run
10!MESSAGE
11!MESSAGE NMAKE /f "LzmaUtil.mak".
12!MESSAGE
13!MESSAGE You can specify a configuration when running NMAKE
14!MESSAGE by defining the macro CFG on the command line. For example:
15!MESSAGE
16!MESSAGE NMAKE /f "LzmaUtil.mak" CFG="LzmaUtil - Win32 Debug"
17!MESSAGE
18!MESSAGE Possible choices for configuration are:
19!MESSAGE
20!MESSAGE "LzmaUtil - Win32 Release" (based on "Win32 (x86) Console Application")
21!MESSAGE "LzmaUtil - Win32 Debug" (based on "Win32 (x86) Console Application")
22!MESSAGE
23
24# Begin Project
25# PROP AllowPerConfigDependencies 0
26# PROP Scc_ProjName ""
27# PROP Scc_LocalPath ""
28CPP=cl.exe
29RSC=rc.exe
30
31!IF "$(CFG)" == "LzmaUtil - Win32 Release"
32
33# PROP BASE Use_MFC 0
34# PROP BASE Use_Debug_Libraries 0
35# PROP BASE Output_Dir "Release"
36# PROP BASE Intermediate_Dir "Release"
37# PROP BASE Target_Dir ""
38# PROP Use_MFC 0
39# PROP Use_Debug_Libraries 0
40# PROP Output_Dir "Release"
41# PROP Intermediate_Dir "Release"
42# PROP Ignore_Export_Lib 0
43# PROP Target_Dir ""
44# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
45# ADD CPP /nologo /MT /W4 /WX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /FD /c
46# SUBTRACT CPP /YX
47# ADD BASE RSC /l 0x419 /d "NDEBUG"
48# ADD RSC /l 0x419 /d "NDEBUG"
49BSC32=bscmake.exe
50# ADD BASE BSC32 /nologo
51# ADD BSC32 /nologo
52LINK32=link.exe
53# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
54# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386 /out:"c:\util\7lzma.exe"
55
56!ELSEIF "$(CFG)" == "LzmaUtil - Win32 Debug"
57
58# PROP BASE Use_MFC 0
59# PROP BASE Use_Debug_Libraries 1
60# PROP BASE Output_Dir "Debug"
61# PROP BASE Intermediate_Dir "Debug"
62# PROP BASE Target_Dir ""
63# PROP Use_MFC 0
64# PROP Use_Debug_Libraries 1
65# PROP Output_Dir "Debug"
66# PROP Intermediate_Dir "Debug"
67# PROP Ignore_Export_Lib 0
68# PROP Target_Dir ""
69# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c
70# ADD CPP /nologo /MTd /W4 /WX /Gm /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /FD /GZ /c
71# SUBTRACT CPP /YX
72# ADD BASE RSC /l 0x419 /d "_DEBUG"
73# ADD RSC /l 0x419 /d "_DEBUG"
74BSC32=bscmake.exe
75# ADD BASE BSC32 /nologo
76# ADD BSC32 /nologo
77LINK32=link.exe
78# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept
79# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /out:"c:\util\7lzma.exe" /pdbtype:sept
80
81!ENDIF
82
83# Begin Target
84
85# Name "LzmaUtil - Win32 Release"
86# Name "LzmaUtil - Win32 Debug"
87# Begin Source File
88
89SOURCE=..\..\7zFile.c
90# End Source File
91# Begin Source File
92
93SOURCE=..\..\7zFile.h
94# End Source File
95# Begin Source File
96
97SOURCE=..\..\7zStream.c
98# End Source File
99# Begin Source File
100
101SOURCE=..\..\7zTypes.h
102# End Source File
103# Begin Source File
104
105SOURCE=..\..\7zVersion.h
106# End Source File
107# Begin Source File
108
109SOURCE=..\..\Alloc.c
110# End Source File
111# Begin Source File
112
113SOURCE=..\..\Alloc.h
114# End Source File
115# Begin Source File
116
117SOURCE=..\..\CpuArch.h
118# End Source File
119# Begin Source File
120
121SOURCE=..\..\LzFind.c
122# End Source File
123# Begin Source File
124
125SOURCE=..\..\LzFind.h
126# End Source File
127# Begin Source File
128
129SOURCE=..\..\LzFindMt.c
130# End Source File
131# Begin Source File
132
133SOURCE=..\..\LzFindMt.h
134# End Source File
135# Begin Source File
136
137SOURCE=..\..\LzFindOpt.c
138# End Source File
139# Begin Source File
140
141SOURCE=..\..\LzHash.h
142# End Source File
143# Begin Source File
144
145SOURCE=..\..\LzmaDec.c
146# End Source File
147# Begin Source File
148
149SOURCE=..\..\LzmaDec.h
150# End Source File
151# Begin Source File
152
153SOURCE=..\..\LzmaEnc.c
154# End Source File
155# Begin Source File
156
157SOURCE=..\..\LzmaEnc.h
158# End Source File
159# Begin Source File
160
161SOURCE=.\LzmaUtil.c
162# End Source File
163# Begin Source File
164
165SOURCE=..\..\Threads.c
166# End Source File
167# Begin Source File
168
169SOURCE=..\..\Threads.h
170# End Source File
171# End Target
172# End Project
diff --git a/C/Util/Lzma/LzmaUtil.dsw b/C/Util/Lzma/LzmaUtil.dsw
new file mode 100644
index 0000000..c52eaf6
--- /dev/null
+++ b/C/Util/Lzma/LzmaUtil.dsw
@@ -0,0 +1,29 @@
1Microsoft Developer Studio Workspace File, Format Version 6.00
2# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!
3
4###############################################################################
5
6Project: "LzmaUtil"=.\LzmaUtil.dsp - Package Owner=<4>
7
8Package=<5>
9{{{
10}}}
11
12Package=<4>
13{{{
14}}}
15
16###############################################################################
17
18Global:
19
20Package=<5>
21{{{
22}}}
23
24Package=<3>
25{{{
26}}}
27
28###############################################################################
29
diff --git a/C/Util/Lzma/makefile b/C/Util/Lzma/makefile
new file mode 100644
index 0000000..7813bdb
--- /dev/null
+++ b/C/Util/Lzma/makefile
@@ -0,0 +1,30 @@
1# MY_STATIC_LINK=1
2PROG = LZMAc.exe
3
4CFLAGS = $(CFLAGS) \
5
6LIB_OBJS = \
7 $O\LzmaUtil.obj \
8
9C_OBJS = \
10 $O\Alloc.obj \
11 $O\CpuArch.obj \
12 $O\LzFind.obj \
13 $O\LzFindMt.obj \
14 $O\LzFindOpt.obj \
15 $O\LzmaDec.obj \
16 $O\LzmaEnc.obj \
17 $O\7zFile.obj \
18 $O\7zStream.obj \
19 $O\Threads.obj \
20
21OBJS = \
22 $(LIB_OBJS) \
23 $(C_OBJS) \
24
25!include "../../../CPP/Build.mak"
26
27$(LIB_OBJS): $(*B).c
28 $(COMPL_O2)
29$(C_OBJS): ../../$(*B).c
30 $(COMPL_O2)
diff --git a/C/Util/Lzma/makefile.gcc b/C/Util/Lzma/makefile.gcc
new file mode 100644
index 0000000..2acb0b8
--- /dev/null
+++ b/C/Util/Lzma/makefile.gcc
@@ -0,0 +1,21 @@
1PROG = 7lzma
2
3include ../../../CPP/7zip/LzmaDec_gcc.mak
4
5
6OBJS = \
7 $(LZMA_DEC_OPT_OBJS) \
8 $O/7zFile.o \
9 $O/7zStream.o \
10 $O/Alloc.o \
11 $O/CpuArch.o \
12 $O/LzFind.o \
13 $O/LzFindMt.o \
14 $O/LzFindOpt.o \
15 $O/LzmaDec.o \
16 $O/LzmaEnc.o \
17 $O/LzmaUtil.o \
18 $O/Threads.o \
19
20
21include ../../7zip_gcc_c.mak