diff options
author | Damien George <damien@micropython.org> | 2021-07-09 12:38:25 +1000 |
---|---|---|
committer | Damien George <damien@micropython.org> | 2021-07-12 17:07:36 +1000 |
commit | 4d546713ec8858cbf908de45de11cbfc46a20971 (patch) | |
tree | fda10704c197bf3500f09297d29239c15f878fab /shared/memzip/memzip.c | |
parent | 925878b2f83e09987bd1688746b92898ee8bbcac (diff) | |
download | micropython-4d546713ec8858cbf908de45de11cbfc46a20971.tar.gz micropython-4d546713ec8858cbf908de45de11cbfc46a20971.zip |
shared: Introduce new top-level dir and move 1st party lib code there.
This commit moves all first-party code developed for this project from lib/
to shared/, so that lib/ now only contains third-party code.
The following directories are moved as-is from lib to shared:
lib/libc -> shared/libc
lib/memzip -> shared/memzip
lib/netutils -> shared/netutils
lib/timeutils -> shared/timeutils
lib/upytesthelper -> shared/upytesthelper
All files in lib/embed/ have been moved to shared/libc/.
lib/mp-readline has been moved to shared/readline.
lib/utils has been moved to shared/runtime, with the exception of
lib/utils/printf.c which has been moved to shared/libc/printf.c.
Signed-off-by: Damien George <damien@micropython.org>
Diffstat (limited to 'shared/memzip/memzip.c')
-rw-r--r-- | shared/memzip/memzip.c | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/shared/memzip/memzip.c b/shared/memzip/memzip.c new file mode 100644 index 0000000000..3fbea8e1e9 --- /dev/null +++ b/shared/memzip/memzip.c @@ -0,0 +1,106 @@ +#include <stdint.h> +#include <stdlib.h> +#include <string.h> +#include "py/mpconfig.h" +#include "py/misc.h" +#include "memzip.h" + +extern uint8_t memzip_data[]; + +const MEMZIP_FILE_HDR *memzip_find_file_header(const char *filename) { + + const MEMZIP_FILE_HDR *file_hdr = (const MEMZIP_FILE_HDR *)memzip_data; + uint8_t *mem_data; + + /* Zip file filenames don't have a leading /, so we strip it off */ + + if (*filename == '/') { + filename++; + } + while (file_hdr->signature == MEMZIP_FILE_HEADER_SIGNATURE) { + const char *file_hdr_filename = (const char *)&file_hdr[1]; + mem_data = (uint8_t *)file_hdr_filename; + mem_data += file_hdr->filename_len; + mem_data += file_hdr->extra_len; + if (!strncmp(file_hdr_filename, filename, file_hdr->filename_len)) { + /* We found a match */ + return file_hdr; + } + mem_data += file_hdr->uncompressed_size; + file_hdr = (const MEMZIP_FILE_HDR *)mem_data; + } + return NULL; +} + +bool memzip_is_dir(const char *filename) { + const MEMZIP_FILE_HDR *file_hdr = (const MEMZIP_FILE_HDR *)memzip_data; + uint8_t *mem_data; + + if (strcmp(filename, "/") == 0) { + // The root directory is a directory. + return true; + } + + // Zip filenames don't have a leading /, so we strip it off + if (*filename == '/') { + filename++; + } + size_t filename_len = strlen(filename); + + while (file_hdr->signature == MEMZIP_FILE_HEADER_SIGNATURE) { + const char *file_hdr_filename = (const char *)&file_hdr[1]; + if (filename_len < file_hdr->filename_len && + strncmp(file_hdr_filename, filename, filename_len) == 0 && + file_hdr_filename[filename_len] == '/') { + return true; + } + + mem_data = (uint8_t *)file_hdr_filename; + mem_data += file_hdr->filename_len; + mem_data += file_hdr->extra_len; + mem_data += file_hdr->uncompressed_size; + file_hdr = (const MEMZIP_FILE_HDR *)mem_data; + } + return NULL; + +} + +MEMZIP_RESULT memzip_locate(const char *filename, void **data, size_t *len) +{ + const MEMZIP_FILE_HDR *file_hdr = memzip_find_file_header(filename); + if (file_hdr == NULL) { + return MZ_NO_FILE; + } + if (file_hdr->compression_method != 0) { + return MZ_FILE_COMPRESSED; + } + + uint8_t *mem_data; + mem_data = (uint8_t *)&file_hdr[1]; + mem_data += file_hdr->filename_len; + mem_data += file_hdr->extra_len; + + *data = mem_data; + *len = file_hdr->uncompressed_size; + return MZ_OK; +} + +MEMZIP_RESULT memzip_stat(const char *path, MEMZIP_FILE_INFO *info) { + const MEMZIP_FILE_HDR *file_hdr = memzip_find_file_header(path); + if (file_hdr == NULL) { + if (memzip_is_dir(path)) { + info->file_size = 0; + info->last_mod_date = 0; + info->last_mod_time = 0; + info->is_dir = 1; + return MZ_OK; + } + return MZ_NO_FILE; + } + info->file_size = file_hdr->uncompressed_size; + info->last_mod_date = file_hdr->last_mod_date; + info->last_mod_time = file_hdr->last_mod_time; + info->is_dir = 0; + + return MZ_OK; +} |