]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/common/common.c
Fix a design error in liblzma API.
[icculus/xz.git] / src / liblzma / common / common.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       common.h
4 /// \brief      Common functions needed in many places in liblzma
5 //
6 //  Author:     Lasse Collin
7 //
8 //  This file has been put into the public domain.
9 //  You can do whatever you want with this file.
10 //
11 ///////////////////////////////////////////////////////////////////////////////
12
13 #include "common.h"
14
15
16 /////////////
17 // Version //
18 /////////////
19
20 extern LZMA_API(uint32_t)
21 lzma_version_number(void)
22 {
23         return LZMA_VERSION;
24 }
25
26
27 extern LZMA_API(const char *)
28 lzma_version_string(void)
29 {
30         return LZMA_VERSION_STRING;
31 }
32
33
34 ///////////////////////
35 // Memory allocation //
36 ///////////////////////
37
38 extern void * lzma_attribute((malloc))
39 lzma_alloc(size_t size, lzma_allocator *allocator)
40 {
41         // Some malloc() variants return NULL if called with size == 0.
42         if (size == 0)
43                 size = 1;
44
45         void *ptr;
46
47         if (allocator != NULL && allocator->alloc != NULL)
48                 ptr = allocator->alloc(allocator->opaque, 1, size);
49         else
50                 ptr = malloc(size);
51
52         return ptr;
53 }
54
55
56 extern void
57 lzma_free(void *ptr, lzma_allocator *allocator)
58 {
59         if (allocator != NULL && allocator->free != NULL)
60                 allocator->free(allocator->opaque, ptr);
61         else
62                 free(ptr);
63
64         return;
65 }
66
67
68 //////////
69 // Misc //
70 //////////
71
72 extern size_t
73 lzma_bufcpy(const uint8_t *restrict in, size_t *restrict in_pos,
74                 size_t in_size, uint8_t *restrict out,
75                 size_t *restrict out_pos, size_t out_size)
76 {
77         const size_t in_avail = in_size - *in_pos;
78         const size_t out_avail = out_size - *out_pos;
79         const size_t copy_size = MIN(in_avail, out_avail);
80
81         memcpy(out + *out_pos, in + *in_pos, copy_size);
82
83         *in_pos += copy_size;
84         *out_pos += copy_size;
85
86         return copy_size;
87 }
88
89
90 extern lzma_ret
91 lzma_next_filter_init(lzma_next_coder *next, lzma_allocator *allocator,
92                 const lzma_filter_info *filters)
93 {
94         lzma_next_coder_init(filters[0].init, next, allocator);
95         next->id = filters[0].id;
96         return filters[0].init == NULL
97                         ? LZMA_OK : filters[0].init(next, allocator, filters);
98 }
99
100
101 extern lzma_ret
102 lzma_next_filter_update(lzma_next_coder *next, lzma_allocator *allocator,
103                 const lzma_filter *reversed_filters)
104 {
105         // Check that the application isn't trying to change the Filter ID.
106         // End of filters is indicated with LZMA_VLI_UNKNOWN in both
107         // reversed_filters[0].id and next->id.
108         if (reversed_filters[0].id != next->id)
109                 return LZMA_PROG_ERROR;
110
111         if (reversed_filters[0].id == LZMA_VLI_UNKNOWN)
112                 return LZMA_OK;
113
114         assert(next->update != NULL);
115         return next->update(next->coder, allocator, NULL, reversed_filters);
116 }
117
118
119 extern void
120 lzma_next_end(lzma_next_coder *next, lzma_allocator *allocator)
121 {
122         if (next->init != (uintptr_t)(NULL)) {
123                 // To avoid tiny end functions that simply call
124                 // lzma_free(coder, allocator), we allow leaving next->end
125                 // NULL and call lzma_free() here.
126                 if (next->end != NULL)
127                         next->end(next->coder, allocator);
128                 else
129                         lzma_free(next->coder, allocator);
130
131                 // Reset the variables so the we don't accidentally think
132                 // that it is an already initialized coder.
133                 *next = LZMA_NEXT_CODER_INIT;
134         }
135
136         return;
137 }
138
139
140 //////////////////////////////////////
141 // External to internal API wrapper //
142 //////////////////////////////////////
143
144 extern lzma_ret
145 lzma_strm_init(lzma_stream *strm)
146 {
147         if (strm == NULL)
148                 return LZMA_PROG_ERROR;
149
150         if (strm->internal == NULL) {
151                 strm->internal = lzma_alloc(sizeof(lzma_internal),
152                                 strm->allocator);
153                 if (strm->internal == NULL)
154                         return LZMA_MEM_ERROR;
155
156                 strm->internal->next = LZMA_NEXT_CODER_INIT;
157         }
158
159         strm->internal->supported_actions[LZMA_RUN] = false;
160         strm->internal->supported_actions[LZMA_SYNC_FLUSH] = false;
161         strm->internal->supported_actions[LZMA_FULL_FLUSH] = false;
162         strm->internal->supported_actions[LZMA_FINISH] = false;
163         strm->internal->sequence = ISEQ_RUN;
164
165         strm->total_in = 0;
166         strm->total_out = 0;
167
168         return LZMA_OK;
169 }
170
171
172 extern LZMA_API(lzma_ret)
173 lzma_code(lzma_stream *strm, lzma_action action)
174 {
175         // Sanity checks
176         if ((strm->next_in == NULL && strm->avail_in != 0)
177                         || (strm->next_out == NULL && strm->avail_out != 0)
178                         || strm->internal == NULL
179                         || strm->internal->next.code == NULL
180                         || (unsigned int)(action) > LZMA_FINISH
181                         || !strm->internal->supported_actions[action])
182                 return LZMA_PROG_ERROR;
183
184         switch (strm->internal->sequence) {
185         case ISEQ_RUN:
186                 switch (action) {
187                 case LZMA_RUN:
188                         break;
189
190                 case LZMA_SYNC_FLUSH:
191                         strm->internal->sequence = ISEQ_SYNC_FLUSH;
192                         break;
193
194                 case LZMA_FULL_FLUSH:
195                         strm->internal->sequence = ISEQ_FULL_FLUSH;
196                         break;
197
198                 case LZMA_FINISH:
199                         strm->internal->sequence = ISEQ_FINISH;
200                         break;
201                 }
202
203                 break;
204
205         case ISEQ_SYNC_FLUSH:
206                 // The same action must be used until we return
207                 // LZMA_STREAM_END, and the amount of input must not change.
208                 if (action != LZMA_SYNC_FLUSH
209                                 || strm->internal->avail_in != strm->avail_in)
210                         return LZMA_PROG_ERROR;
211
212                 break;
213
214         case ISEQ_FULL_FLUSH:
215                 if (action != LZMA_FULL_FLUSH
216                                 || strm->internal->avail_in != strm->avail_in)
217                         return LZMA_PROG_ERROR;
218
219                 break;
220
221         case ISEQ_FINISH:
222                 if (action != LZMA_FINISH
223                                 || strm->internal->avail_in != strm->avail_in)
224                         return LZMA_PROG_ERROR;
225
226                 break;
227
228         case ISEQ_END:
229                 return LZMA_STREAM_END;
230
231         case ISEQ_ERROR:
232         default:
233                 return LZMA_PROG_ERROR;
234         }
235
236         size_t in_pos = 0;
237         size_t out_pos = 0;
238         lzma_ret ret = strm->internal->next.code(
239                         strm->internal->next.coder, strm->allocator,
240                         strm->next_in, &in_pos, strm->avail_in,
241                         strm->next_out, &out_pos, strm->avail_out, action);
242
243         strm->next_in += in_pos;
244         strm->avail_in -= in_pos;
245         strm->total_in += in_pos;
246
247         strm->next_out += out_pos;
248         strm->avail_out -= out_pos;
249         strm->total_out += out_pos;
250
251         strm->internal->avail_in = strm->avail_in;
252
253         switch (ret) {
254         case LZMA_OK:
255                 // Don't return LZMA_BUF_ERROR when it happens the first time.
256                 // This is to avoid returning LZMA_BUF_ERROR when avail_out
257                 // was zero but still there was no more data left to written
258                 // to next_out.
259                 if (out_pos == 0 && in_pos == 0) {
260                         if (strm->internal->allow_buf_error)
261                                 ret = LZMA_BUF_ERROR;
262                         else
263                                 strm->internal->allow_buf_error = true;
264                 } else {
265                         strm->internal->allow_buf_error = false;
266                 }
267                 break;
268
269         case LZMA_STREAM_END:
270                 if (strm->internal->sequence == ISEQ_SYNC_FLUSH
271                                 || strm->internal->sequence == ISEQ_FULL_FLUSH)
272                         strm->internal->sequence = ISEQ_RUN;
273                 else
274                         strm->internal->sequence = ISEQ_END;
275
276         // Fall through
277
278         case LZMA_NO_CHECK:
279         case LZMA_UNSUPPORTED_CHECK:
280         case LZMA_GET_CHECK:
281         case LZMA_MEMLIMIT_ERROR:
282                 // Something else than LZMA_OK, but not a fatal error,
283                 // that is, coding may be continued (except if ISEQ_END).
284                 strm->internal->allow_buf_error = false;
285                 break;
286
287         default:
288                 // All the other errors are fatal; coding cannot be continued.
289                 assert(ret != LZMA_BUF_ERROR);
290                 strm->internal->sequence = ISEQ_ERROR;
291                 break;
292         }
293
294         return ret;
295 }
296
297
298 extern LZMA_API(void)
299 lzma_end(lzma_stream *strm)
300 {
301         if (strm != NULL && strm->internal != NULL) {
302                 lzma_next_end(&strm->internal->next, strm->allocator);
303                 lzma_free(strm->internal, strm->allocator);
304                 strm->internal = NULL;
305         }
306
307         return;
308 }
309
310
311 extern LZMA_API(lzma_check)
312 lzma_get_check(const lzma_stream *strm)
313 {
314         // Return LZMA_CHECK_NONE if we cannot know the check type.
315         // It's a bug in the application if this happens.
316         if (strm->internal->next.get_check == NULL)
317                 return LZMA_CHECK_NONE;
318
319         return strm->internal->next.get_check(strm->internal->next.coder);
320 }
321
322
323 extern LZMA_API(uint64_t)
324 lzma_memusage(const lzma_stream *strm)
325 {
326         uint64_t memusage;
327         uint64_t old_memlimit;
328
329         if (strm == NULL || strm->internal == NULL
330                         || strm->internal->next.memconfig == NULL
331                         || strm->internal->next.memconfig(
332                                 strm->internal->next.coder,
333                                 &memusage, &old_memlimit, 0) != LZMA_OK)
334                 return 0;
335
336         return memusage;
337 }
338
339
340 extern LZMA_API(uint64_t)
341 lzma_memlimit_get(const lzma_stream *strm)
342 {
343         uint64_t old_memlimit;
344         uint64_t memusage;
345
346         if (strm == NULL || strm->internal == NULL
347                         || strm->internal->next.memconfig == NULL
348                         || strm->internal->next.memconfig(
349                                 strm->internal->next.coder,
350                                 &memusage, &old_memlimit, 0) != LZMA_OK)
351                 return 0;
352
353         return old_memlimit;
354 }
355
356
357 extern LZMA_API(lzma_ret)
358 lzma_memlimit_set(lzma_stream *strm, uint64_t new_memlimit)
359 {
360         // Dummy variables to simplify memconfig functions
361         uint64_t old_memlimit;
362         uint64_t memusage;
363
364         if (strm == NULL || strm->internal == NULL
365                         || strm->internal->next.memconfig == NULL)
366                 return LZMA_PROG_ERROR;
367
368         if (new_memlimit != 0 && new_memlimit < LZMA_MEMUSAGE_BASE)
369                 return LZMA_MEMLIMIT_ERROR;
370
371         return strm->internal->next.memconfig(strm->internal->next.coder,
372                         &memusage, &old_memlimit, new_memlimit);
373 }