wok view lzma/stuff/lzlib.u @ rev 3940

add zile: a lightweight Emacs clone.
author Dominique Corbex <domcox@users.sourceforge.net>
date Sat Aug 22 12:55:55 2009 +0200 (2009-08-22)
parents
children
line source
1 --- CPP/7zip/Compress/LZMA_Alone/makefile.gcc
2 +++ CPP/7zip/Compress/LZMA_Alone/makefile.gcc
3 @@ -1,6 +1,6 @@
4 PROG = lzma
5 -CXX = g++ -O2 -Wall
6 -CXX_C = gcc -O2 -Wall
7 +CXX = g++ -s -O2 -Wall
8 +CXX_C = gcc -s -O2 -Wall
9 LIB = -lm
10 RM = rm -f
11 CFLAGS = -c
12 @@ -46,8 +46,47 @@
13 LzmaDecode.o \
14 LzmaRamDecode.o \
16 +LIB = liblz.so.1.0.0
17 +LIB_OBJS = \
18 + LzmaRamDecode.o \
19 + LzmaDecode.o \
20 + BranchX86.o \
21 + Wrapper.o \
22 +
23 +SHARED_OBJS = \
24 + LzmaAlone.o \
25 + LzmaBench.o \
26 + LzmaBenchCon.o \
27 + LZMADecoder.o \
28 + LZMAEncoder.o \
29 + LzmaRam.o \
30 + InBuffer.o \
31 + OutBuffer.o \
32 + CRC.o \
33 + IntToString.o \
34 + MyString.o \
35 + StringConvert.o \
36 + StringToInt.o \
37 + MyVector.o \
38 + 7zCrc.o \
39 + Alloc.o \
40 + MatchFinder.o \
41 + StreamUtils.o \
42 + LZOutWindow.o \
43 + RangeCoderBit.o \
44 + FileStreams.o \
45 + $(FILE_IO).o \
46 + CommandLineParser.o \
47 +
48 +all: $(PROG) $(LIB)
49 +
50 +$(LIB): $(LIB_OBJS)
51 + $(CXX) -shared -Wl,-soname -Wl,liblz.so.1 -o $(LIB) $(LIB_OBJS) -lz
52 + $(CXX) $(LDFLAGS) -o lzma-shared $(SHARED_OBJS) liblz.so.1.0.0 $(LIB) $(LIB2)
53 +
54 +Wrapper.o: Wrapper.c
55 + $(CXX_C) $(CFLAGS) Wrapper.c
57 -all: $(PROG)
59 $(PROG): $(OBJS)
60 $(CXX) -o $(PROG) $(LDFLAGS) $(OBJS) $(LIB) $(LIB2)
62 --- CPP/7zip/Compress/LZMA_Alone/lzlib.h
63 +++ CPP/7zip/Compress/LZMA_Alone/lzlib.h
64 @@ -0,0 +1,15 @@
65 +#ifndef LZLIB_H
66 +#define LZLIB_H
67 +#include <zlib.h>
68 +typedef struct {
69 + int handlertype;
70 + unsigned long lzsize;
71 + int fd;
72 + gzFile gzfd;
73 +} *lzFile;
74 +
75 +extern lzFile lzdopen(int fd, const char *mode);
76 +extern lzFile lzopen(const char *path, const char *mode);
77 +extern void *lzgrab(lzFile file, unsigned long *size);
78 +extern int lzclose(lzFile file);
79 +#endif
81 --- CPP/7zip/Compress/LZMA_Alone/Wrapper.c
82 +++ CPP/7zip/Compress/LZMA_Alone/Wrapper.c
83 @@ -0,0 +1,119 @@
84 +#include <stdlib.h>
85 +#include <unistd.h>
86 +#include <sys/types.h>
87 +#include <sys/stat.h>
88 +#include <fcntl.h>
89 +#include "lzlib.h"
90 +#include "LzmaRamDecode.h"
91 +
92 +#define LZ_READ 0
93 +#define LZ_GZREAD 1
94 +#define LZ_LZREAD 2
95 +
96 +#define LZMA_PROP 0x5d
97 +
98 +static int fullread(int fd, unsigned char *buffer, size_t len)
99 +{
100 + int i, n;
101 + for (n = 0; (i = read(fd, buffer + n, len - n)) > 0; n += i);
102 + return n;
103 +}
104 +
105 +lzFile lzdopen(int fd, const char *mode)
106 +{
107 + unsigned char tmp[13];
108 + int n;
109 + lzFile lzfd;
110 +
111 + if (fd < 0) return NULL;
112 + n = fullread(fd, tmp, sizeof(tmp));
113 + lzfd = malloc(sizeof(*lzfd));
114 + if (!lzfd) return NULL;
115 + lzfd->handlertype = LZ_READ;
116 + if (n == sizeof(tmp) && tmp[0] == LZMA_PROP &&
117 + !LzmaRamGetUncompressedSize(tmp-1, sizeof(tmp)+1, &lzfd->lzsize))
118 + lzfd->handlertype = LZ_LZREAD;
119 + else if (n > 2 && tmp[0] == 0x1F && tmp[1] == 0x8B)
120 + lzfd->handlertype = LZ_GZREAD;
121 + lzfd->fd = fd;
122 + lseek(fd, 0, SEEK_SET);
123 + if (lzfd->handlertype != LZ_LZREAD) {
124 + if (lzfd->handlertype == LZ_GZREAD) {
125 + lzfd->gzfd = gzdopen(fd, mode);
126 + if (lzfd->gzfd == Z_NULL) {
127 + free(lzfd);
128 + return NULL;
129 + }
130 + }
131 + }
132 + return lzfd;
133 +}
134 +
135 +lzFile lzopen(const char *path, const char *mode)
136 +{
137 + int fd = open(path, O_RDONLY);
138 + return lzdopen(fd, mode);
139 +}
140 +
141 +static int lzread(lzFile file, void *buf, unsigned len)
142 +{
143 + if (file->handlertype == LZ_GZREAD)
144 + return gzread(file->gzfd, buf, len);
145 + return read(file->fd, buf, len);
146 +}
147 +
148 +void *lzgrab(lzFile file, unsigned long *size)
149 +{
150 + unsigned int n, max;
151 + unsigned char *output;
152 +
153 + if (!file) return NULL;
154 + if (file->handlertype == LZ_LZREAD) {
155 + unsigned char *input;
156 + size_t outsize;
157 +
158 + output = malloc(file->lzsize + file->lzsize/20); // 105%
159 + if (!output) return NULL;
160 + output[0] = 0;
161 + max = 1 + fullread(file->fd, output+1, file->lzsize);
162 + input = realloc(output, max);
163 + output = malloc(file->lzsize);
164 + if (!output || LzmaRamDecompress(input, max,
165 + output, file->lzsize, &outsize, malloc, free)) {
166 + if (output) free(output);
167 + free(input);
168 + return NULL;
169 + }
170 + free(input);
171 + *size = outsize;
172 + return output;
173 + }
174 + max = 16384;
175 + output = malloc(max);
176 + if (!output) return NULL;
177 + *size = 0;
178 + while ((n = lzread(file, output + *size, max - *size)) > 0) {
179 + *size += n;
180 + if (*size == max) {
181 + output = realloc(output, max *= 2);
182 + if (!output) return NULL;
183 + }
184 + }
185 + if (n < 0) {
186 + free(output);
187 + return NULL;
188 + }
189 + return realloc(output, *size);
190 +}
191 +
192 +int lzclose(lzFile file)
193 +{
194 + int status = -1;
195 + if (file) {
196 + if (file->handlertype == LZ_GZREAD)
197 + status = gzclose(file->gzfd);
198 + else status = close(file->fd);
199 + free(file);
200 + }
201 + return status;
202 +}