wok annotate lzma/stuff/lzlib.u @ rev 5072

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