Browse code

compiles, upx-lzma b0rked

aCaB authored on 2009/08/05 23:51:09
Showing 4 changed files
... ...
@@ -25,55 +25,42 @@
25 25
 #endif
26 26
 
27 27
 #include "lzma_iface.h"
28
-#include "7z/LzmaDec.h"
29
-#include "cltypes.h"
30
-#include "others.h"
31 28
 
32 29
 static void *__wrap_alloc(void *unused, size_t size) { 
33 30
     unused = unused;
34 31
     return cli_malloc(size);
35 32
 }
36
-static void *__wrap_free(void *unused, void *freeme) {
33
+static void __wrap_free(void *unused, void *freeme) {
37 34
     unused = unused;
38 35
     free(freeme);
39 36
 }
40 37
 static ISzAlloc g_Alloc = { __wrap_alloc, __wrap_free };
41 38
 
42
-struct CLI_LZMA {
43
-    CLzmaDec state;
44
-    unsigned char header[LZMA_PROPS_SIZE];
45
-    unsigned int p_cnt;
46
-    unsigned int s_cnt;
47
-    unsigned int freeme;
48
-    uint64_t usize;
49
-    ELzmaFinishMode finish;
50
-};
51
-
52
-static unsigned char lzma_getbyte(CLI_LZMA *L, int *fail) {
53
-    unsigned char *c = (unsigned char *)L->next_in;
54
-    if(!c || !L->avail_in) {
39
+
40
+static unsigned char lzma_getbyte(struct CLI_LZMA *L, int *fail) {
41
+    unsigned char c;
42
+    if(!L->next_in || !L->avail_in) {
55 43
 	*fail = 1;
56 44
 	return 0;
57 45
     }
58 46
     *fail = 0;
59
-    L->next_in = &c[1];
47
+    c = L->next_in[0];
48
+    L->next_in++;
60 49
     L->avail_in--;
61
-    return *c;
50
+    return c;
62 51
 }
63 52
     
64 53
 
65
-int cli_LzmaInit(CLI_LZMA **Lp, uint64_t size_override) {
66
-    CLI_LZMA *L = *Lp;
54
+int cli_LzmaInit(struct CLI_LZMA *L, uint64_t size_override) {
67 55
     int fail;
68 56
 
69
-    if(!L) {
70
-	*Lp = L = cli_calloc(sizeof(*L), 1);
71
-	if(!L) return CL_EMEM;
57
+    if(!L->init) {
72 58
 	L->p_cnt = LZMA_PROPS_SIZE;
73 59
 	if(size_override)
74 60
 	    L->usize = size_override;
75 61
 	else
76 62
 	    L->s_cnt = 8;
63
+	L->init = 1;
77 64
     } else if(size_override)
78 65
 	cli_warnmsg("cli_LzmaInit: ignoring late size override\n");
79 66
 
... ...
@@ -95,42 +82,36 @@ int cli_LzmaInit(CLI_LZMA **Lp, uint64_t size_override) {
95 95
     LzmaDec_Construct(&L->state);
96 96
     if(LzmaDec_Allocate(&L->state, L->header, LZMA_PROPS_SIZE, &g_Alloc) != SZ_OK)
97 97
 	return CL_EMEM;
98
-    LzmaDec_Init(&state);
98
+    LzmaDec_Init(&L->state);
99 99
 
100 100
     L->freeme = 1;
101
-    if(~L-usize) L->finish = LZMA_FINISH_END;
101
+    if(~L->usize) L->finish = LZMA_FINISH_END;
102 102
     else L->finish = LZMA_FINISH_ANY;
103 103
     return LZMA_RESULT_OK;
104 104
 }
105 105
 	
106 106
 
107
-void cli_LzmaShutdown(CLI_LZMA **Lp) {
108
-    CLI_LZMA *L;
109
-
110
-    if(!Lp) return;
111
-    L = *Lp;
107
+void cli_LzmaShutdown(struct CLI_LZMA *L) {
112 108
     if(L->freeme)
113 109
 	LzmaDec_Free(&L->state, &g_Alloc);
114
-    free(L);
115
-    *Lp = NULL;
116 110
     return;
117 111
 }
118 112
 
119 113
 
120
-int cli_LzmaDecode(CLI_LZMA **Lp) {
121
-    CLI_LZMA *L = *Lp;
122
-
123
-    if(!L->freeme) return cli_LzmaInit(LP, 0);
124
-
114
+int cli_LzmaDecode(struct CLI_LZMA *L) {
125 115
     SRes res;
126
-    SizeT outbytes = L->avail_out;
127
-    SizeT inbytes = L->avail_in;
116
+    SizeT outbytes, inbytes;
128 117
     ELzmaStatus status;
118
+
119
+    if(!L->freeme) return cli_LzmaInit(L, 0);
120
+
121
+    outbytes = L->avail_out;
122
+    inbytes = L->avail_in;
129 123
     res = LzmaDec_DecodeToBuf(&L->state, L->next_out, &outbytes, L->next_in, &inbytes, L->finish, &status);
130 124
     L->next_in += inbytes;
131 125
     L->next_out += outbytes;
132 126
     L->usize -= outbytes;
133
-
127
+    return 0; /* FIXMELZMA */
134 128
 }
135 129
 
136 130
 /* int cli_LzmaInitUPX(CLI_LZMA **Lp, uint32_t dictsz) { */
... ...
@@ -23,21 +23,37 @@
23 23
 #ifndef __LZMA_IFACE_H
24 24
 #define __LZMA_IFACE_H
25 25
 
26
+#include "7z/LzmaDec.h"
26 27
 #include "cltypes.h"
28
+#include "others.h"
29
+
30
+struct CLI_LZMA {
31
+    CLzmaDec state;
32
+    unsigned char header[LZMA_PROPS_SIZE];
33
+    unsigned int p_cnt;
34
+    unsigned int s_cnt;
35
+    unsigned int freeme;
36
+    unsigned int init;
37
+    uint64_t usize;
38
+    ELzmaFinishMode finish;
39
+    unsigned char *next_in;
40
+    unsigned char *next_out;
41
+    SizeT avail_in;
42
+    SizeT avail_out;
43
+};
27 44
 
28
-typedef struct CLI_LZMA_tag CLI_LZMA;
29 45
 
30 46
 struct stream_state {
31
-	uint32_t avail_in;
32
-	unsigned char *next_in;
33
-	uint32_t avail_out;
34
-	unsigned char *next_out;
47
+    uint32_t avail_in;
48
+    unsigned char *next_in;
49
+    uint32_t avail_out;
50
+    unsigned char *next_out;
35 51
 };
36 52
 
37
-int cli_LzmaInit(CLI_LZMA **, uint64_t);
38
-void cli_LzmaShutdown(CLI_LZMA **);
39
-int cli_LzmaDecode(CLI_LZMA **, struct stream_state*);
40
-int cli_LzmaInitUPX(CLI_LZMA **, uint32_t);
53
+int cli_LzmaInit(struct CLI_LZMA *, uint64_t);
54
+void cli_LzmaShutdown(struct CLI_LZMA *);
55
+int cli_LzmaDecode(struct CLI_LZMA *);
56
+/* int cli_LzmaInitUPX(struct CLI_LZMA **, uint32_t); FIXMELZMA */
41 57
 
42 58
 #define LZMA_STREAM_END 2
43 59
 #define LZMA_RESULT_OK 0
... ...
@@ -76,7 +76,7 @@ struct nsis_st {
76 76
   uint8_t eof;
77 77
   struct stream_state nsis;
78 78
   nsis_bzstream bz;
79
-  CLI_LZMA* lz;
79
+  struct CLI_LZMA lz;
80 80
 /*   z_stream z; */
81 81
   nsis_z_stream z;
82 82
   unsigned char *freeme;
... ...
@@ -97,6 +97,7 @@ static int nsis_init(struct nsis_st *n) {
97 97
     n->freecomp=1;
98 98
     break;
99 99
   case COMP_LZMA:
100
+    memset(&n->bz, 0, sizeof(struct CLI_LZMA));
100 101
     cli_LzmaInit(&n->lz, 0xffffffffffffffffULL);
101 102
     n->freecomp=1;
102 103
     break;
... ...
@@ -150,13 +151,21 @@ static int nsis_decomp(struct nsis_st *n) {
150 150
     n->nsis.next_out = n->bz.next_out;
151 151
     break;
152 152
   case COMP_LZMA:
153
-    switch (cli_LzmaDecode(&n->lz, &n->nsis)) {
153
+    n->lz.avail_in = n->nsis.avail_in;
154
+    n->lz.next_in = n->nsis.next_in;
155
+    n->lz.avail_out = n->nsis.avail_out;
156
+    n->lz.next_out = n->nsis.next_out;
157
+    switch (cli_LzmaDecode(&n->lz)) {
154 158
     case LZMA_RESULT_OK:
155 159
       ret = CL_SUCCESS;
156 160
       break;
157 161
     case LZMA_STREAM_END:
158 162
       ret = CL_BREAK;
159 163
     }
164
+    n->nsis.avail_in = n->lz.avail_in;
165
+    n->nsis.next_in = n->lz.next_in;
166
+    n->nsis.avail_out = n->lz.avail_out;
167
+    n->nsis.next_out = n->lz.next_out;
160 168
     break;
161 169
   case COMP_ZLIB:
162 170
     n->z.avail_in = n->nsis.avail_in;
... ...
@@ -523,22 +523,23 @@ int upx_inflate2e(char *src, uint32_t ssize, char *dst, uint32_t *dsize, uint32_
523 523
 }
524 524
 
525 525
 int upx_inflatelzma(char *src, uint32_t ssize, char *dst, uint32_t *dsize, uint32_t upx0, uint32_t upx1, uint32_t ep) {
526
-  CLI_LZMA *lz = NULL;
526
+  struct CLI_LZMA l;
527 527
   struct stream_state s;
528 528
   uint32_t magic[]={0xb16,0xb1e,0};
529 529
 
530
-  cli_LzmaInitUPX(&lz, *dsize);
530
+  memset(&l, 0, sizeof(l));
531
+  //cli_LzmaInitUPX(&lz, *dsize); /* FIXMELZMA: make func and check return value */
531 532
   s.avail_in = ssize;
532 533
   s.avail_out = *dsize;
533 534
   s.next_in = (unsigned char*)src+2;
534 535
   s.next_out = (unsigned char*)dst;
535 536
 
536
-  if(cli_LzmaDecode(&lz, &s)==LZMA_RESULT_DATA_ERROR) {
537
+  if(cli_LzmaDecode(&l)==LZMA_RESULT_DATA_ERROR) {
537 538
 /*     __asm__ __volatile__("int3"); */
538
-    cli_LzmaShutdown(&lz);
539
+    cli_LzmaShutdown(&l);
539 540
     return -1;
540 541
   }
541
-  cli_LzmaShutdown(&lz);
542
+  cli_LzmaShutdown(&l);
542 543
 
543 544
   return pefromupx (src, ssize, dst, dsize, ep, upx0, upx1, magic, *dsize);
544 545
 }