2014-01-08 04:00:22 -05:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-07-15 01:19:27 -04:00
|
|
|
#include "mpconfigport.h"
|
2014-06-16 01:33:14 -04:00
|
|
|
#include "misc.h"
|
2014-01-08 04:00:22 -05:00
|
|
|
#include "memzip.h"
|
|
|
|
|
|
|
|
extern uint8_t _staticfs[];
|
|
|
|
|
2014-06-16 01:33:14 -04:00
|
|
|
const MEMZIP_FILE_HDR *memzip_find_file_header(const char *filename) {
|
|
|
|
|
2014-01-08 04:00:22 -05:00
|
|
|
const MEMZIP_FILE_HDR *file_hdr = (const MEMZIP_FILE_HDR *)_staticfs;
|
|
|
|
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 */
|
2014-06-16 01:33:14 -04:00
|
|
|
return file_hdr;
|
|
|
|
}
|
|
|
|
mem_data += file_hdr->uncompressed_size;
|
|
|
|
file_hdr = (const MEMZIP_FILE_HDR *)mem_data;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-01-08 04:00:22 -05:00
|
|
|
|
2014-06-16 01:33:14 -04:00
|
|
|
bool memzip_is_dir(const char *filename) {
|
|
|
|
const MEMZIP_FILE_HDR *file_hdr = (const MEMZIP_FILE_HDR *)_staticfs;
|
|
|
|
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;
|
2014-01-08 04:00:22 -05:00
|
|
|
}
|
2014-06-16 01:33:14 -04:00
|
|
|
|
|
|
|
mem_data = (uint8_t *)file_hdr_filename;
|
|
|
|
mem_data += file_hdr->filename_len;
|
|
|
|
mem_data += file_hdr->extra_len;
|
2014-01-08 04:00:22 -05:00
|
|
|
mem_data += file_hdr->uncompressed_size;
|
|
|
|
file_hdr = (const MEMZIP_FILE_HDR *)mem_data;
|
|
|
|
}
|
2014-06-16 01:33:14 -04:00
|
|
|
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;
|
2014-01-08 04:00:22 -05:00
|
|
|
}
|