Fix zip dictionary allocation
This commit is contained in:
parent
bab374a675
commit
ef705d3ac6
@ -529,10 +529,24 @@ bool ZipFile::readFileToStream(const char* filename, Print& out, const size_t ch
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (fileStat.method == MZ_DEFLATED) {
|
if (fileStat.method == MZ_DEFLATED) {
|
||||||
// Setup inflator
|
// Allocate largest buffer first to maximize chance of finding contiguous block
|
||||||
|
// Dictionary buffer (32KB) - needed for DEFLATE sliding window
|
||||||
|
const auto outputBuffer = static_cast<uint8_t*>(malloc(TINFL_LZ_DICT_SIZE));
|
||||||
|
if (!outputBuffer) {
|
||||||
|
Serial.printf("[%lu] [ZIP] Failed to allocate memory for dictionary (need %d bytes)\n", millis(),
|
||||||
|
TINFL_LZ_DICT_SIZE);
|
||||||
|
if (!wasOpen) {
|
||||||
|
close();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
memset(outputBuffer, 0, TINFL_LZ_DICT_SIZE);
|
||||||
|
|
||||||
|
// Setup inflator (~11KB)
|
||||||
const auto inflator = static_cast<tinfl_decompressor*>(malloc(sizeof(tinfl_decompressor)));
|
const auto inflator = static_cast<tinfl_decompressor*>(malloc(sizeof(tinfl_decompressor)));
|
||||||
if (!inflator) {
|
if (!inflator) {
|
||||||
Serial.printf("[%lu] [ZIP] Failed to allocate memory for inflator\n", millis());
|
Serial.printf("[%lu] [ZIP] Failed to allocate memory for inflator\n", millis());
|
||||||
|
free(outputBuffer);
|
||||||
if (!wasOpen) {
|
if (!wasOpen) {
|
||||||
close();
|
close();
|
||||||
}
|
}
|
||||||
@ -541,29 +555,18 @@ bool ZipFile::readFileToStream(const char* filename, Print& out, const size_t ch
|
|||||||
memset(inflator, 0, sizeof(tinfl_decompressor));
|
memset(inflator, 0, sizeof(tinfl_decompressor));
|
||||||
tinfl_init(inflator);
|
tinfl_init(inflator);
|
||||||
|
|
||||||
// Setup file read buffer
|
// Setup file read buffer (smallest allocation last)
|
||||||
const auto fileReadBuffer = static_cast<uint8_t*>(malloc(chunkSize));
|
const auto fileReadBuffer = static_cast<uint8_t*>(malloc(chunkSize));
|
||||||
if (!fileReadBuffer) {
|
if (!fileReadBuffer) {
|
||||||
Serial.printf("[%lu] [ZIP] Failed to allocate memory for zip file read buffer\n", millis());
|
Serial.printf("[%lu] [ZIP] Failed to allocate memory for zip file read buffer\n", millis());
|
||||||
free(inflator);
|
free(inflator);
|
||||||
|
free(outputBuffer);
|
||||||
if (!wasOpen) {
|
if (!wasOpen) {
|
||||||
close();
|
close();
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const auto outputBuffer = static_cast<uint8_t*>(malloc(TINFL_LZ_DICT_SIZE));
|
|
||||||
if (!outputBuffer) {
|
|
||||||
Serial.printf("[%lu] [ZIP] Failed to allocate memory for dictionary\n", millis());
|
|
||||||
free(inflator);
|
|
||||||
free(fileReadBuffer);
|
|
||||||
if (!wasOpen) {
|
|
||||||
close();
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
memset(outputBuffer, 0, TINFL_LZ_DICT_SIZE);
|
|
||||||
|
|
||||||
size_t fileRemainingBytes = deflatedDataSize;
|
size_t fileRemainingBytes = deflatedDataSize;
|
||||||
size_t processedOutputBytes = 0;
|
size_t processedOutputBytes = 0;
|
||||||
size_t fileReadBufferFilledBytes = 0;
|
size_t fileReadBufferFilledBytes = 0;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user