wok view lzav-dev/stuff/lzav.c @ rev 25842
remove web-tweetdeck
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Thu May 15 11:37:45 2025 +0000 (3 months ago) |
parents | |
children |
line source
1 #include "lzav.h"
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <unistd.h>
6 #define BLKSZ 0x10000
7 #define COMPRESS 0
8 #define HICOMPRESS 1
9 #define DECOMPRESS 2
11 static int src_len, dst_len;
12 static char *src_buf, *dst_buf;
14 static void fail(int code)
15 {
16 fprintf(stderr,"Error %d \n",code);
17 if (src_buf) free(src_buf);
18 if (dst_buf) free(dst_buf);
19 exit(code);
20 }
22 void inline load(void)
23 {
24 int n, max = BLKSZ;
26 src_len = 0;
27 src_buf = malloc(max);
28 while (src_buf != NULL) {
29 n = read(0, src_buf + src_len, max - src_len);
30 if (n <= 0) break;
31 src_len += n;
32 if (src_len == max) {
33 max += BLKSZ;
34 src_buf = realloc(src_buf, max);
35 }
36 }
37 if (src_buf == NULL) fail(1);
38 if (n < 0) fail(2);
39 }
41 inline void store(void)
42 {
43 int n, i;
44 for (n = 0; n < dst_len; n += i) {
45 i = write(1, dst_buf + n, dst_len - n);
46 if (i < 0) fail(4);
47 }
48 }
50 static void header(void)
51 {
52 int len = src_len;
53 LZAV_IEC32( len );
54 write(1,"LZAV",4);
55 write(1,&len,4);
56 }
58 int main(int argc, char *argv[])
59 {
60 int mode = COMPRESS;
61 int i, j, max_len;
62 for (i = 1; i < argc; i++) {
63 char c;
64 for (j = 0; argv[i][j] == '-'; j++);
65 switch (argv[i][j]) {
66 case 'd':
67 mode = DECOMPRESS;
68 break;
69 case 'e':
70 mode = COMPRESS;
71 break;
72 case 'h':
73 case 'x':
74 mode = HICOMPRESS;
75 break;
76 default:
77 fprintf(stderr,"Usage: %s [-d|-c|-x] < input > output\nDecompress, compress or hi-compress",argv[0]);
78 }
79 }
80 load();
81 switch (mode) {
82 case DECOMPRESS :
83 dst_len = *(int *)(src_buf+4);
84 LZAV_IEC32(dst_len);
85 src_buf += 8; src_len -= 8;
86 dst_buf = malloc( dst_len );
87 if (dst_buf == NULL) fail(3);
88 dst_len = lzav_decompress( src_buf, dst_buf, src_len, dst_len );
89 if( dst_len < 0 ) fail(5);
90 break;
91 case COMPRESS :
92 max_len = lzav_compress_bound( src_len );
93 dst_buf = malloc( max_len );
94 if (dst_buf == NULL) fail(3);
95 dst_len = lzav_compress_default( src_buf, dst_buf, src_len, max_len );
96 if( dst_len == 0 && src_len != 0 ) fail(5);
97 header();
98 break;
99 case HICOMPRESS :
100 max_len = lzav_compress_bound_hi( src_len );
101 dst_buf = malloc( max_len );
102 if (dst_buf == NULL) fail(3);
103 dst_len = lzav_compress_hi( src_buf, dst_buf, src_len, max_len );
104 if( dst_len == 0 && src_len != 0 ) fail(5);
105 header();
106 break;
107 }
108 store();
109 }