51 const uint32_t maximum_buffer_len = decompressed_data_limit +
SWF_HEADER_LEN;
52 uint32_t requested_len = out_buffer->
size;
54 if (requested_len >= maximum_buffer_len)
57 if (requested_len == 0) {
59 }
else if (requested_len > maximum_buffer_len / 2) {
60 requested_len = maximum_buffer_len;
72 static uint32_t FileSwfDecompressionOutputCapacity(
87 uint32_t decompressed_data_limit, uint32_t *decompressed_data_produced)
90 *decompressed_data_produced = 0;
92 memset(&infstream, 0,
sizeof(infstream));
93 infstream.zalloc = Z_NULL;
94 infstream.zfree = Z_NULL;
95 infstream.opaque = Z_NULL;
97 infstream.avail_in = (uInt)compressed_data_len;
98 infstream.next_in = (Bytef *)compressed_data;
100 int result = inflateInit(&infstream);
101 if (result != Z_OK) {
107 uint32_t output_capacity =
108 FileSwfDecompressionOutputCapacity(out_buffer, decompressed_data_limit);
110 infstream.avail_out = (uInt)(output_capacity - infstream.total_out);
113 result = inflate(&infstream, Z_NO_FLUSH);
114 *decompressed_data_produced = (uint32_t)infstream.total_out;
116 if (result == Z_STREAM_END || *decompressed_data_produced == decompressed_data_limit)
120 if (result == Z_OK && infstream.avail_out == 0) {
121 if (!FileSwfDecompressionBufferGrow(det_ctx, out_buffer, decompressed_data_limit)) {
129 if ((result == Z_OK || result == Z_BUF_ERROR) && infstream.avail_in == 0)
132 if (result == Z_DATA_ERROR) {
134 }
else if (result == Z_STREAM_ERROR) {
136 }
else if (result == Z_BUF_ERROR) {
144 inflateEnd(&infstream);
154 static uint8_t *FileSwfLzmaOutputGrow(
void *output, uint32_t min_size)
161 uint32_t decompressed_data_limit, uint32_t *decompressed_data_produced)
163 *decompressed_data_produced = 0;
165 size_t inprocessed = compressed_data_len;
166 size_t outprocessed = 0;
167 int ret = lzma_decompress(compressed_data, &inprocessed, out_buffer,
SWF_HEADER_LEN,
168 decompressed_data_limit, &outprocessed, FileSwfLzmaOutputGrow,
170 *decompressed_data_produced = (uint32_t)outprocessed;
172 if (ret == LzmaOk || ret == LzmaOutputFull) {
174 ret == LzmaOutputFull && *decompressed_data_produced != decompressed_data_limit);
178 if (ret == LzmaOutputAllocError) {
180 }
else if (ret == LzmaIoError) {
182 }
else if (ret == LzmaHeaderTooShortError) {
184 }
else if (ret == LzmaError) {
186 }
else if (ret == LzmaMemoryError) {
188 }
else if (ret == LzmaXzError) {