Browse code

fix lzma return codes, update nsis/lzma

aCaB authored on 2009/08/06 03:51:02
Showing 3 changed files
... ...
@@ -81,12 +81,10 @@ int cli_LzmaInit(struct CLI_LZMA *L, uint64_t size_override) {
81 81
 
82 82
     LzmaDec_Construct(&L->state);
83 83
     if(LzmaDec_Allocate(&L->state, L->header, LZMA_PROPS_SIZE, &g_Alloc) != SZ_OK)
84
-	return CL_EMEM;
84
+	return LZMA_RESULT_DATA_ERROR;
85 85
     LzmaDec_Init(&L->state);
86 86
 
87 87
     L->freeme = 1;
88
-    if(~L->usize) L->finish = LZMA_FINISH_END;
89
-    else L->finish = LZMA_FINISH_ANY;
90 88
     return LZMA_RESULT_OK;
91 89
 }
92 90
 	
... ...
@@ -102,16 +100,29 @@ int cli_LzmaDecode(struct CLI_LZMA *L) {
102 102
     SRes res;
103 103
     SizeT outbytes, inbytes;
104 104
     ELzmaStatus status;
105
+    ELzmaFinishMode finish;
105 106
 
106 107
     if(!L->freeme) return cli_LzmaInit(L, 0);
107 108
 
108
-    outbytes = L->avail_out;
109 109
     inbytes = L->avail_in;
110
-    res = LzmaDec_DecodeToBuf(&L->state, L->next_out, &outbytes, L->next_in, &inbytes, L->finish, &status);
110
+    if(~L->usize && L->avail_out > L->usize) {
111
+	outbytes = L->usize;
112
+	finish = LZMA_FINISH_END;
113
+    } else {
114
+	outbytes = L->avail_out;
115
+	finish = LZMA_FINISH_ANY;
116
+    }
117
+    res = LzmaDec_DecodeToBuf(&L->state, L->next_out, &outbytes, L->next_in, &inbytes, finish, &status);
118
+    L->avail_in -= inbytes;
111 119
     L->next_in += inbytes;
120
+    L->avail_out -= outbytes;
112 121
     L->next_out += outbytes;
113
-    L->usize -= outbytes;
114
-    return 0; /* FIXMELZMA */
122
+    if(~L->usize) L->usize -= outbytes;
123
+    if(res != SZ_OK)
124
+	return LZMA_RESULT_DATA_ERROR;
125
+    if(!L->usize || status == LZMA_STATUS_FINISHED_WITH_MARK)
126
+	return LZMA_STREAM_END;
127
+    return LZMA_RESULT_OK;
115 128
 }
116 129
 
117 130
 /* int cli_LzmaInitUPX(CLI_LZMA **Lp, uint32_t dictsz) { */
... ...
@@ -35,7 +35,6 @@ struct CLI_LZMA {
35 35
     unsigned int freeme;
36 36
     unsigned int init;
37 37
     uint64_t usize;
38
-    ELzmaFinishMode finish;
39 38
     unsigned char *next_in;
40 39
     unsigned char *next_out;
41 40
     SizeT avail_in;
... ...
@@ -97,8 +97,9 @@ 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));
101
-    cli_LzmaInit(&n->lz, 0xffffffffffffffffULL);
100
+    memset(&n->lz, 0, sizeof(struct CLI_LZMA));
101
+    if(cli_LzmaInit(&n->lz, 0xffffffffffffffffULL)!=LZMA_RESULT_OK)
102
+      return CL_EUNPACK;
102 103
     n->freecomp=1;
103 104
     break;
104 105
   case COMP_ZLIB: