aboutsummaryrefslogtreecommitdiff
path: root/src/liblzma/common/code.c
blob: 0e3929b6c226d0d57cf453f4937595ffd93ef769 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
///////////////////////////////////////////////////////////////////////////////
//
/// \file       code.c
/// \brief      zlib-like API wrapper for liblzma's internal API
//
//  Copyright (C) 2007 Lasse Collin
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
///////////////////////////////////////////////////////////////////////////////

#include "common.h"


LZMA_API const lzma_stream LZMA_STREAM_INIT_VAR = {
	.next_in = NULL,
	.avail_in = 0,
	.total_in = 0,
	.next_out = NULL,
	.avail_out = 0,
	.total_out = 0,
	.allocator = NULL,
	.internal = NULL,
};


extern lzma_ret
lzma_strm_init(lzma_stream *strm)
{
	if (strm == NULL)
		return LZMA_PROG_ERROR;

	if (strm->internal == NULL) {
		strm->internal = lzma_alloc(sizeof(lzma_internal),
				strm->allocator);
		if (strm->internal == NULL)
			return LZMA_MEM_ERROR;

		strm->internal->next = LZMA_NEXT_CODER_INIT;
	}

	strm->internal->supported_actions[LZMA_RUN] = false;
	strm->internal->supported_actions[LZMA_SYNC_FLUSH] = false;
	strm->internal->supported_actions[LZMA_FULL_FLUSH] = false;
	strm->internal->supported_actions[LZMA_FINISH] = false;
	strm->internal->sequence = ISEQ_RUN;

	strm->total_in = 0;
	strm->total_out = 0;

	return LZMA_OK;
}


extern LZMA_API lzma_ret
lzma_code(lzma_stream *strm, lzma_action action)
{
	// Sanity checks
	if ((strm->next_in == NULL && strm->avail_in != 0)
			|| (strm->next_out == NULL && strm->avail_out != 0)
			|| strm->internal == NULL
			|| strm->internal->next.code == NULL
			|| (unsigned int)(action) > LZMA_FINISH
			|| !strm->internal->supported_actions[action])
		return LZMA_PROG_ERROR;

	switch (strm->internal->sequence) {
	case ISEQ_RUN:
		switch (action) {
		case LZMA_RUN:
			break;

		case LZMA_SYNC_FLUSH:
			strm->internal->sequence = ISEQ_SYNC_FLUSH;
			break;

		case LZMA_FULL_FLUSH:
			strm->internal->sequence = ISEQ_FULL_FLUSH;
			break;

		case LZMA_FINISH:
			strm->internal->sequence = ISEQ_FINISH;
			break;
		}

		break;

	case ISEQ_SYNC_FLUSH:
		if (action != LZMA_SYNC_FLUSH)
			return LZMA_PROG_ERROR;

		// Check that application doesn't change avail_in once
		// LZMA_SYNC_FLUSH has been used.
		if (strm->internal->avail_in != strm->avail_in)
			return LZMA_DATA_ERROR;

		break;

	case ISEQ_FULL_FLUSH:
		if (action != LZMA_FULL_FLUSH)
			return LZMA_PROG_ERROR;

		// Check that application doesn't change avail_in once
		// LZMA_FULL_FLUSH has been used.
		if (strm->internal->avail_in != strm->avail_in)
			return LZMA_DATA_ERROR;

		break;

	case ISEQ_FINISH:
		if (action != LZMA_FINISH)
			return LZMA_PROG_ERROR;

		if (strm->internal->avail_in != strm->avail_in)
			return LZMA_DATA_ERROR;

		break;

	case ISEQ_END:
		return LZMA_STREAM_END;

	case ISEQ_ERROR:
	default:
		return LZMA_PROG_ERROR;
	}

	size_t in_pos = 0;
	size_t out_pos = 0;
	lzma_ret ret = strm->internal->next.code(
			strm->internal->next.coder, strm->allocator,
			strm->next_in, &in_pos, strm->avail_in,
			strm->next_out, &out_pos, strm->avail_out, action);

	strm->next_in += in_pos;
	strm->avail_in -= in_pos;
	strm->total_in += in_pos;

	strm->next_out += out_pos;
	strm->avail_out -= out_pos;
	strm->total_out += out_pos;

	strm->internal->avail_in = strm->avail_in;

	switch (ret) {
	case LZMA_OK:
		// Don't return LZMA_BUF_ERROR when it happens the first time.
		// This is to avoid returning LZMA_BUF_ERROR when avail_out
		// was zero but still there was no more data left to written
		// to next_out.
		if (out_pos == 0 && in_pos == 0) {
			if (strm->internal->allow_buf_error)
				ret = LZMA_BUF_ERROR;
			else
				strm->internal->allow_buf_error = true;
		} else {
			strm->internal->allow_buf_error = false;
		}
		break;

	case LZMA_STREAM_END:
		if (strm->internal->sequence == ISEQ_SYNC_FLUSH
				|| strm->internal->sequence == ISEQ_FULL_FLUSH)
			strm->internal->sequence = ISEQ_RUN;
		else
			strm->internal->sequence = ISEQ_END;
		break;

	case LZMA_UNSUPPORTED_CHECK:
		strm->internal->allow_buf_error = false;
		break;

	default:
		// All the other errors are fatal; coding cannot be continued.
		strm->internal->sequence = ISEQ_ERROR;
		break;
	}

	return ret;
}


extern LZMA_API void
lzma_end(lzma_stream *strm)
{
	if (strm != NULL && strm->internal != NULL) {
		if (strm->internal->next.end != NULL)
			strm->internal->next.end(strm->internal->next.coder,
					strm->allocator);

		lzma_free(strm->internal, strm->allocator);
		strm->internal = NULL;
	}

	return;
}