From 1a1d7e05257894c5fe405cfddc6db6be4e5d5387 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Wed, 23 Oct 2019 20:54:32 +1100 Subject: [PATCH] AP_ROMFS: support uncompressed romfs data --- libraries/AP_ROMFS/AP_ROMFS.cpp | 24 ++++++++++++++++++------ libraries/AP_ROMFS/AP_ROMFS.h | 9 ++++++--- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/libraries/AP_ROMFS/AP_ROMFS.cpp b/libraries/AP_ROMFS/AP_ROMFS.cpp index dc207e9c57..73d7f37155 100644 --- a/libraries/AP_ROMFS/AP_ROMFS.cpp +++ b/libraries/AP_ROMFS/AP_ROMFS.cpp @@ -45,7 +45,7 @@ const uint8_t *AP_ROMFS::find_file(const char *name, uint32_t &size) data after use. The next byte after the file data is guaranteed to be null */ -uint8_t *AP_ROMFS::find_decompress(const char *name, uint32_t &size) +const 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); @@ -53,6 +53,9 @@ uint8_t *AP_ROMFS::find_decompress(const char *name, uint32_t &size) return nullptr; } +#ifdef HAL_ROMFS_UNCOMPRESSED + return compressed_data; +#else // 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; @@ -67,7 +70,7 @@ uint8_t *AP_ROMFS::find_decompress(const char *name, uint32_t &size) TINF_DATA *d = (TINF_DATA *)malloc(sizeof(TINF_DATA)); if (!d) { - free(decompressed_data); + ::free(decompressed_data); return nullptr; } uzlib_uncompress_init(d, NULL, 0); @@ -78,8 +81,8 @@ uint8_t *AP_ROMFS::find_decompress(const char *name, uint32_t &size) // assume gzip format int res = uzlib_gzip_parse_header(d); if (res != TINF_OK) { - free(decompressed_data); - free(d); + ::free(decompressed_data); + ::free(d); return nullptr; } @@ -90,13 +93,22 @@ uint8_t *AP_ROMFS::find_decompress(const char *name, uint32_t &size) // ROMFS data res = uzlib_uncompress(d); - free(d); + ::free(d); if (res != TINF_OK) { - free(decompressed_data); + ::free(decompressed_data); return nullptr; } size = decompressed_size; return decompressed_data; +#endif +} + +// free returned data +void AP_ROMFS::free(const uint8_t *data) +{ +#ifndef HAL_ROMFS_UNCOMPRESSED + ::free(const_cast(data)); +#endif } diff --git a/libraries/AP_ROMFS/AP_ROMFS.h b/libraries/AP_ROMFS/AP_ROMFS.h index ae88e9bb62..213f033d24 100644 --- a/libraries/AP_ROMFS/AP_ROMFS.h +++ b/libraries/AP_ROMFS/AP_ROMFS.h @@ -9,10 +9,13 @@ class AP_ROMFS { public: // find a file and de-compress, assumning gzip format. The // decompressed data will be allocated with malloc(). You must - // call free on the return value after use. The next byte after + // call AP_ROMFS::free() on the return value after use. The next byte after // the file data is guaranteed to be null. - static uint8_t *find_decompress(const char *name, uint32_t &size); - + static const uint8_t *find_decompress(const char *name, uint32_t &size); + + // free returned data + static void free(const uint8_t *data); + private: // find an embedded file static const uint8_t *find_file(const char *name, uint32_t &size);