AP_ROMFS: added support for compressed files in ROMFS

This commit is contained in:
Andrew Tridgell
2018-07-09 19:41:56 +10:00
parent 3fc6968700
commit c3db5c3da2
5 changed files with 906 additions and 1 deletions
+59
View File
@@ -17,6 +17,7 @@
*/
#include "AP_ROMFS.h"
#include "tinf.h"
#ifdef HAL_HAVE_AP_ROMFS_EMBEDDED_H
#include <ap_romfs_embedded.h>
@@ -37,3 +38,61 @@ const uint8_t *AP_ROMFS::find_file(const char *name, uint32_t &size)
}
return nullptr;
}
/*
find a compressed file and uncompress it. Space for decompressed
data comes from malloc. Caller must be careful to free the resulting
data after use.
*/
uint8_t *AP_ROMFS::find_decompress(const char *name, uint32_t &size)
{
uint32_t compressed_size;
const uint8_t *compressed_data = find_file(name, compressed_size);
if (!compressed_data) {
return nullptr;
}
// last 4 bytes of gzip file are length of decompressed data
const uint8_t *p = &compressed_data[compressed_size-4];
uint32_t decompressed_size = p[0] | p[1] << 8 | p[2] << 16 | p[3] << 24;
uint8_t *decompressed_data = (uint8_t *)malloc(decompressed_size);
if (!decompressed_data) {
return nullptr;
}
TINF_DATA *d = (TINF_DATA *)malloc(sizeof(TINF_DATA));
if (!d) {
free(decompressed_data);
return nullptr;
}
uzlib_uncompress_init(d, NULL, 0);
d->source = compressed_data;
d->source_limit = compressed_data + compressed_size - 4;
// assume gzip format
int res = uzlib_gzip_parse_header(d);
if (res != TINF_OK) {
free(decompressed_data);
free(d);
return nullptr;
}
d->dest = decompressed_data;
d->destSize = decompressed_size;
// we don't check CRC, as it just wastes flash space for constant
// ROMFS data
res = uzlib_uncompress(d);
free(d);
if (res != TINF_OK) {
free(decompressed_data);
return nullptr;
}
size = decompressed_size;
return decompressed_data;
}