Merge pull request #721 from bjornbytes/file

File Object
This commit is contained in:
Bjorn 2023-11-22 16:48:20 -08:00 committed by GitHub
commit 3a16e0c290
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 1821 additions and 559 deletions

View File

@ -295,7 +295,6 @@ endif()
set(LOVR_SRC
src/core/fs.c
src/core/zip.c
src/api/api.c
src/api/l_lovr.c
src/util.c
@ -419,6 +418,8 @@ if(LOVR_ENABLE_FILESYSTEM)
target_sources(lovr PRIVATE
src/modules/filesystem/filesystem.c
src/api/l_filesystem.c
src/api/l_filesystem_file.c
src/lib/miniz/miniz_tinfl.c
)
else()
target_compile_definitions(lovr PRIVATE LOVR_DISABLE_FILESYSTEM)

View File

@ -400,7 +400,6 @@ src = {
'src/core/fs.c',
('src/core/os_%s.c'):format(target),
'src/core/spv.c',
'src/core/zip.c',
'src/api/api.c',
'src/api/l_lovr.c'
}
@ -443,6 +442,7 @@ else
end
src += 'src/lib/stb/*.c'
src += 'src/lib/miniz/*.c'
src += (config.modules.audio or config.modules.data) and 'src/lib/miniaudio/*.c' or nil
src += config.modules.data and 'src/lib/jsmn/*.c' or nil
src += config.modules.data and 'src/lib/minimp3/*.c' or nil

View File

@ -45,6 +45,7 @@ extern StringEntry lovrJointType[];
extern StringEntry lovrKeyboardKey[];
extern StringEntry lovrMeshStorage[];
extern StringEntry lovrModelDrawMode[];
extern StringEntry lovrOpenMode[];
extern StringEntry lovrOriginType[];
extern StringEntry lovrPassType[];
extern StringEntry lovrPermission[];

View File

@ -5,8 +5,15 @@
#include <stdlib.h>
#include <string.h>
StringEntry lovrOpenMode[] = {
[OPEN_READ] = ENTRY("r"),
[OPEN_WRITE] = ENTRY("w"),
[OPEN_APPEND] = ENTRY("a"),
{ 0 }
};
void* luax_readfile(const char* filename, size_t* bytesRead) {
return lovrFilesystemRead(filename, -1, bytesRead);
return lovrFilesystemRead(filename, bytesRead);
}
bool luax_writefile(const char* filename, const void* data, size_t size) {
@ -111,7 +118,6 @@ static int l_lovrFilesystemGetAppdataDirectory(lua_State* L) {
return 1;
}
static int l_lovrFilesystemGetDirectoryItems(lua_State* L) {
const char* path = luaL_checkstring(L, 1);
lua_settop(L, 1);
@ -275,9 +281,9 @@ static int l_lovrFilesystemMount(lua_State* L) {
}
static int l_lovrFilesystemNewBlob(lua_State* L) {
size_t size;
const char* path = luaL_checkstring(L, 1);
uint8_t* data = luax_readfile(path, &size);
size_t size;
void* data = luax_readfile(path, &size);
lovrAssert(data, "Could not load file '%s'", path);
Blob* blob = lovrBlobCreate(data, size, path);
luax_pushtype(L, Blob, blob);
@ -287,18 +293,15 @@ static int l_lovrFilesystemNewBlob(lua_State* L) {
static int l_lovrFilesystemRead(lua_State* L) {
const char* path = luaL_checkstring(L, 1);
lua_Integer luaSize = luaL_optinteger(L, 2, -1);
size_t size = MAX(luaSize, -1);
size_t bytesRead;
void* content = lovrFilesystemRead(path, size, &bytesRead);
if (!content) {
size_t size;
void* data = lovrFilesystemRead(path, &size);
if (!data) {
lua_pushnil(L);
return 1;
}
lua_pushlstring(L, content, bytesRead);
lua_pushinteger(L, bytesRead);
free(content);
return 2;
lua_pushlstring(L, data, size);
free(data);
return 1;
}
static int l_lovrFilesystemRemove(lua_State* L) {
@ -343,6 +346,22 @@ static int l_lovrFilesystemWrite(lua_State* L) {
return 1;
}
static int l_lovrFilesystemNewFile(lua_State* L) {
const char* path = luaL_checkstring(L, 1);
OpenMode mode = luax_checkenum(L, 2, OpenMode, NULL);
const char* error;
File* file = lovrFileCreate(path, mode, &error);
if (file) {
luax_pushtype(L, File, file);
lovrRelease(file, lovrFileDestroy);
return 1;
} else {
lua_pushnil(L);
lua_pushstring(L, error);
return 2;
}
}
static const luaL_Reg lovrFilesystem[] = {
{ "append", l_lovrFilesystemAppend },
{ "createDirectory", l_lovrFilesystemCreateDirectory },
@ -370,6 +389,7 @@ static const luaL_Reg lovrFilesystem[] = {
{ "setIdentity", l_lovrFilesystemSetIdentity },
{ "unmount", l_lovrFilesystemUnmount },
{ "write", l_lovrFilesystemWrite },
{ "newFile", l_lovrFilesystemNewFile },
{ NULL, NULL }
};
@ -538,6 +558,8 @@ static int libLoaderAllInOne(lua_State* L) {
return libLoaderCommon(L, allInOneFlag);
}
extern const luaL_Reg lovrFile[];
int luaopen_lovr_filesystem(lua_State* L) {
const char* archive = NULL;
@ -555,6 +577,7 @@ int luaopen_lovr_filesystem(lua_State* L) {
lua_newtable(L);
luax_register(L, lovrFilesystem);
luax_registertype(L, File);
luax_registerloader(L, luaLoader, 2);
luax_registerloader(L, libLoader, 3);
luax_registerloader(L, libLoaderAllInOne, 4);

132
src/api/l_filesystem_file.c Normal file
View File

@ -0,0 +1,132 @@
#include "api.h"
#include "data/blob.h"
#include "filesystem/filesystem.h"
#include "util.h"
#include <stdlib.h>
static int l_lovrFileGetMode(lua_State* L) {
File* file = luax_checktype(L, 1, File);
OpenMode mode = lovrFileGetMode(file);
luax_pushenum(L, OpenMode, mode);
return 1;
}
static int l_lovrFileGetPath(lua_State* L) {
File* file = luax_checktype(L, 1, File);
const char* path = lovrFileGetPath(file);
lua_pushstring(L, path);
return 1;
}
static int l_lovrFileGetSize(lua_State* L) {
File* file = luax_checktype(L, 1, File);
uint64_t size = lovrFileGetSize(file);
if (size >= 1ull << 53) {
lua_pushnil(L);
} else {
lua_pushinteger(L, size);
}
return 1;
}
static int l_lovrFileRead(lua_State* L) {
File* file = luax_checktype(L, 1, File);
size_t size;
if (lua_type(L, 2) == LUA_TNUMBER) {
lua_Number n = lua_tonumber(L, 2);
lovrCheck(n >= 0, "Number of bytes to read can not be negative");
lovrCheck(n < 9007199254740992.0, "Number of bytes to read must be less than 2^53");
size = (size_t) n;
} else {
size = lovrFileGetSize(file) - lovrFileTell(file);
}
size_t count;
void* data = malloc(size);
lovrAssert(data, "Out of memory");
bool success = lovrFileRead(file, data, size, &count);
if (success) {
lua_pushlstring(L, data, count);
lua_pushnumber(L, count);
free(data);
return 2;
} else {
lua_pushnil(L);
free(data);
return 1;
}
}
static int l_lovrFileWrite(lua_State* L) {
File* file = luax_checktype(L, 1, File);
const void* data;
size_t size;
Blob* blob = luax_totype(L, 2, Blob);
if (blob) {
data = blob->data;
size = blob->size;
} else if (lua_type(L, 2) == LUA_TSTRING) {
data = lua_tolstring(L, 2, &size);
} else {
return luax_typeerror(L, 2, "string or Blob");
}
if (lua_type(L, 3) == LUA_TNUMBER) {
lua_Number n = lua_tonumber(L, 2);
lovrCheck(n >= 0, "Number of bytes to write can not be negative");
lovrCheck(n < 9007199254740992.0, "Number of bytes to write must be less than 2^53");
lovrCheck(n <= size, "Number of bytes to write is bigger than the size of the source");
size = (size_t) n;
}
size_t count;
bool success = lovrFileWrite(file, data, size, &count);
if (success && count == size) {
lua_pushboolean(L, true);
} else {
lua_pushboolean(L, false);
}
return 1;
}
static int l_lovrFileSeek(lua_State* L) {
File* file = luax_checktype(L, 1, File);
lua_Number offset = luaL_checknumber(L, 2);
lovrCheck(offset >= 0 && offset < 9007199254740992.0, "Invalid seek position");
bool success = lovrFileSeek(file, offset);
lua_pushboolean(L, success);
return 1;
}
static int l_lovrFileTell(lua_State* L) {
File* file = luax_checktype(L, 1, File);
uint64_t offset = lovrFileTell(file);
if (offset >= 1ull << 53) {
lua_pushnil(L);
} else {
lua_pushinteger(L, offset);
}
return 1;
}
static int l_lovrFileIsEOF(lua_State* L) {
File* file = luax_checktype(L, 1, File);
OpenMode mode = lovrFileGetMode(file);
if (mode == OPEN_READ) {
uint64_t offset = lovrFileTell(file);
uint64_t extent = lovrFileGetSize(file);
lua_pushboolean(L, offset >= extent);
} else {
lua_pushboolean(L, false);
}
return 1;
}
const luaL_Reg lovrFile[] = {
{ "getMode", l_lovrFileGetMode },
{ "getPath", l_lovrFileGetPath },
{ "getSize", l_lovrFileGetSize },
{ "read", l_lovrFileRead },
{ "write", l_lovrFileWrite },
{ "seek", l_lovrFileSeek },
{ "tell", l_lovrFileTell },
{ "isEOF", l_lovrFileIsEOF },
{ NULL, NULL }
};

View File

@ -6,7 +6,7 @@
#define FS_PATH_MAX 1024
bool fs_open(const char* path, OpenMode mode, fs_handle* file) {
bool fs_open(const char* path, char mode, fs_handle* file) {
WCHAR wpath[FS_PATH_MAX];
if (!MultiByteToWideChar(CP_UTF8, 0, path, -1, wpath, FS_PATH_MAX)) {
return false;
@ -15,9 +15,9 @@ bool fs_open(const char* path, OpenMode mode, fs_handle* file) {
DWORD access;
DWORD creation;
switch (mode) {
case OPEN_READ: access = GENERIC_READ; creation = OPEN_EXISTING; break;
case OPEN_WRITE: access = GENERIC_WRITE; creation = CREATE_ALWAYS; break;
case OPEN_APPEND: access = GENERIC_WRITE; creation = OPEN_ALWAYS; break;
case 'r': access = GENERIC_READ; creation = OPEN_EXISTING; break;
case 'w': access = GENERIC_WRITE; creation = CREATE_ALWAYS; break;
case 'a': access = GENERIC_WRITE; creation = OPEN_ALWAYS; break;
default: return false;
}
@ -27,7 +27,7 @@ bool fs_open(const char* path, OpenMode mode, fs_handle* file) {
return false;
}
if (mode == OPEN_APPEND && SetFilePointer(file->handle, 0, NULL, FILE_END) == INVALID_SET_FILE_POINTER) {
if (mode == 'a' && SetFilePointer(file->handle, 0, NULL, FILE_END) == INVALID_SET_FILE_POINTER) {
CloseHandle(file->handle);
return false;
}
@ -39,20 +39,36 @@ bool fs_close(fs_handle file) {
return CloseHandle(file.handle);
}
bool fs_read(fs_handle file, void* buffer, size_t* bytes) {
DWORD bytes32 = *bytes > UINT32_MAX ? UINT32_MAX : (DWORD) *bytes;
bool success = ReadFile(file.handle, buffer, bytes32, &bytes32, NULL);
*bytes = bytes32;
bool fs_read(fs_handle file, void* data, size_t size, size_t* count) {
DWORD bytes32 = size > UINT32_MAX ? UINT32_MAX : (DWORD) size;
bool success = ReadFile(file.handle, data, bytes32, &bytes32, NULL);
*count = bytes32;
return success;
}
bool fs_write(fs_handle file, const void* buffer, size_t* bytes) {
DWORD bytes32 = *bytes > UINT32_MAX ? UINT32_MAX : (DWORD) *bytes;
bool success = WriteFile(file.handle, buffer, bytes32, &bytes32, NULL);
*bytes = bytes32;
bool fs_write(fs_handle file, const void* data, size_t size, size_t* count) {
DWORD bytes32 = size > UINT32_MAX ? UINT32_MAX : (DWORD) size;
bool success = WriteFile(file.handle, data, bytes32, &bytes32, NULL);
*count = bytes32;
return success;
}
bool fs_seek(fs_handle file, uint64_t offset) {
LARGE_INTEGER n = { .QuadPart = offset };
return SetFilePointerEx(file.handle, n, NULL, FILE_BEGIN);
}
bool fs_fstat(fs_handle file, FileInfo* info) {
LARGE_INTEGER size;
if (!GetFileSizeEx(file.handle, &size)) {
return false;
}
info->size = size.QuadPart;
info->lastModified = 0;
info->type = FILE_REGULAR;
return true;
}
void* fs_map(const char* path, size_t* size) {
WCHAR wpath[FS_PATH_MAX];
if (!MultiByteToWideChar(CP_UTF8, 0, path, -1, wpath, FS_PATH_MAX)) {
@ -175,48 +191,65 @@ bool fs_list(const char* path, fs_list_cb* callback, void* context) {
#include <limits.h>
#include <unistd.h>
bool fs_open(const char* path, OpenMode mode, fs_handle* file) {
bool fs_open(const char* path, char mode, fs_handle* file) {
int flags;
switch (mode) {
case OPEN_READ: flags = O_RDONLY; break;
case OPEN_WRITE: flags = O_WRONLY | O_CREAT | O_TRUNC; break;
case OPEN_APPEND: flags = O_APPEND | O_WRONLY | O_CREAT; break;
case 'r': flags = O_RDONLY; break;
case 'w': flags = O_WRONLY | O_CREAT | O_TRUNC; break;
case 'a': flags = O_APPEND | O_WRONLY | O_CREAT; break;
default: return false;
}
struct stat stats;
file->fd = open(path, flags, S_IRUSR | S_IWUSR);
return file->fd >= 0;
return file->fd >= 0 && !fstat(file->fd, &stats) && !S_ISDIR(stats.st_mode);
}
bool fs_close(fs_handle file) {
return close(file.fd) == 0;
}
bool fs_read(fs_handle file, void* buffer, size_t* bytes) {
ssize_t result = read(file.fd, buffer, *bytes);
bool fs_read(fs_handle file, void* data, size_t size, size_t* count) {
ssize_t result = read(file.fd, data, size);
if (result < 0 || result > SSIZE_MAX) {
*bytes = 0;
*count = 0;
return false;
} else {
*bytes = (uint32_t) result;
*count = result;
return true;
}
}
bool fs_write(fs_handle file, const void* buffer, size_t* bytes) {
ssize_t result = write(file.fd, buffer, *bytes);
bool fs_write(fs_handle file, const void* data, size_t size, size_t* count) {
ssize_t result = write(file.fd, data, size);
if (result < 0 || result > SSIZE_MAX) {
*bytes = 0;
*count = 0;
return false;
} else {
*bytes = (uint32_t) result;
*count = (size_t) result;
return true;
}
}
bool fs_seek(fs_handle file, uint64_t offset) {
return lseek(file.fd, (off_t) offset, SEEK_SET) != (off_t) -1;
}
bool fs_fstat(fs_handle file, FileInfo* info) {
struct stat stats;
if (fstat(file.fd, &stats)) {
return false;
}
info->size = (uint64_t) stats.st_size;
info->lastModified = (uint64_t) stats.st_mtime;
info->type = S_ISDIR(stats.st_mode) ? FILE_DIRECTORY : FILE_REGULAR;
return true;
}
void* fs_map(const char* path, size_t* size) {
FileInfo info;
fs_handle file;
if (!fs_stat(path, &info) || !fs_open(path, OPEN_READ, &file)) {
if (!fs_stat(path, &info) || !fs_open(path, 'r', &file)) {
return NULL;
}
*size = info.size;
@ -235,12 +268,9 @@ bool fs_stat(const char* path, FileInfo* info) {
return false;
}
if (info) {
info->size = (uint64_t) stats.st_size;
info->lastModified = (uint64_t) stats.st_mtime;
info->type = S_ISDIR(stats.st_mode) ? FILE_DIRECTORY : FILE_REGULAR;
}
info->size = (uint64_t) stats.st_size;
info->lastModified = (uint64_t) stats.st_mtime;
info->type = S_ISDIR(stats.st_mode) ? FILE_DIRECTORY : FILE_REGULAR;
return true;
}

View File

@ -4,12 +4,6 @@
#pragma once
typedef enum {
OPEN_READ,
OPEN_WRITE,
OPEN_APPEND
} OpenMode;
typedef enum {
FILE_DIRECTORY,
FILE_REGULAR
@ -24,10 +18,12 @@ typedef struct {
typedef void fs_list_cb(void*, const char*);
typedef union { int fd; void* handle; } fs_handle;
bool fs_open(const char* path, OpenMode mode, fs_handle* file);
bool fs_open(const char* path, char mode, fs_handle* file);
bool fs_close(fs_handle file);
bool fs_read(fs_handle file, void* buffer, size_t* bytes);
bool fs_write(fs_handle file, const void* buffer, size_t* bytes);
bool fs_read(fs_handle file, void* data, size_t size, size_t* count);
bool fs_write(fs_handle file, const void* data, size_t size, size_t* count);
bool fs_seek(fs_handle file, uint64_t offset);
bool fs_fstat(fs_handle file, FileInfo* info);
void* fs_map(const char* path, size_t* size);
bool fs_unmap(void* data, size_t size);
bool fs_stat(const char* path, FileInfo* info);

View File

@ -1,84 +0,0 @@
#include "zip.h"
#include <string.h>
static uint16_t readu16(const uint8_t* p) { uint16_t x; memcpy(&x, p, sizeof(x)); return x; }
static uint32_t readu32(const uint8_t* p) { uint32_t x; memcpy(&x, p, sizeof(x)); return x; }
bool zip_open(zip_state* zip) {
const uint8_t* p = zip->data + zip->size - 22;
if (zip->size < 22 || readu32(p) != 0x06054b50) {
return false;
}
zip->count = readu16(p + 10);
zip->cursor = readu32(p + 16);
zip->base = 0;
if (zip->cursor + 4 > zip->size) {
return false;
}
// See if the central directory starts where the endOfCentralDirectory said it would.
// If it doesn't, then it might be a self-extracting archive with broken offsets (common).
// In this case, assume the central directory is directly adjacent to the endOfCentralDirectory,
// located at (offsetOfEndOfCentralDirectory (aka size - 22) - sizeOfCentralDirectory).
// If we find a central directory there, then compute a "base" offset that equals the difference
// between where it is and where it was supposed to be, and apply this offset to everything else.
if (readu32(zip->data + zip->cursor) != 0x02014b50) {
size_t offsetOfEndOfCentralDirectory = zip->size - 22;
size_t sizeOfCentralDirectory = readu32(p + 12);
size_t centralDirectoryOffset = offsetOfEndOfCentralDirectory - sizeOfCentralDirectory;
if (sizeOfCentralDirectory > offsetOfEndOfCentralDirectory || centralDirectoryOffset + 4 > zip->size) {
return false;
}
if (readu32(zip->data + centralDirectoryOffset) != 0x02014b50) {
return false;
}
zip->base = centralDirectoryOffset - zip->cursor;
zip->cursor = centralDirectoryOffset;
}
return true;
}
bool zip_next(zip_state* zip, zip_file* file) {
const uint8_t* p = zip->data + zip->cursor;
if (zip->cursor + 46 > zip->size || readu32(p) != 0x02014b50) {
return false;
}
file->mtime = readu16(p + 12);
file->mdate = readu16(p + 14);
file->csize = readu32(p + 20);
file->size = readu32(p + 24);
file->length = readu16(p + 28);
file->offset = readu32(p + 42) + zip->base;
file->name = (const char*) (p + 46);
zip->cursor += 46 + file->length + readu16(p + 30) + readu16(p + 32);
return zip->cursor < zip->size;
}
void* zip_load(zip_state* zip, size_t offset, bool* compressed) {
if (zip->size < 30 || offset > zip->size - 30) {
return NULL;
}
uint8_t* p = zip->data + offset;
if (readu32(p) != 0x04034b50) {
return NULL;
}
uint16_t compression = readu16(p + 8);
if (compression != 0 && compression != 8) {
return false;
}
*compressed = (compression == 8);
uint32_t skip = readu16(p + 26) + readu16(p + 28);
return p + 30 + skip;
}

View File

@ -1,36 +0,0 @@
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
// Status:
// - Little endian only
// - Zip64 is not supported
// - Self-extracting archives are supported
// - Supports store and deflate compression
// - No comment allowed at the end of archive (file comments are okay)
// - No multi-disk archives
// - No encryption
#pragma once
typedef struct {
uint8_t* data;
size_t size;
size_t base;
size_t cursor;
uint64_t count;
} zip_state;
typedef struct {
uint64_t offset;
uint64_t csize;
uint64_t size;
const char* name;
uint16_t length;
uint16_t mdate;
uint16_t mtime;
} zip_file;
bool zip_open(zip_state* zip);
bool zip_next(zip_state* zip, zip_file* info);
void* zip_load(zip_state* zip, size_t offset, bool* compressed);

704
src/lib/miniz/miniz_tinfl.c Normal file
View File

@ -0,0 +1,704 @@
/**************************************************************************
*
* Copyright 2013-2014 RAD Game Tools and Valve Software
* Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
* All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
**************************************************************************/
#include "miniz_tinfl.h"
#ifndef MINIZ_NO_INFLATE_APIS
#ifdef __cplusplus
extern "C" {
#endif
/* ------------------- Low-level Decompression (completely independent from all compression API's) */
#define TINFL_MEMCPY(d, s, l) memcpy(d, s, l)
#define TINFL_MEMSET(p, c, l) memset(p, c, l)
#define TINFL_CR_BEGIN \
switch (r->m_state) \
{ \
case 0:
#define TINFL_CR_RETURN(state_index, result) \
do \
{ \
status = result; \
r->m_state = state_index; \
goto common_exit; \
case state_index:; \
} \
MZ_MACRO_END
#define TINFL_CR_RETURN_FOREVER(state_index, result) \
do \
{ \
for (;;) \
{ \
TINFL_CR_RETURN(state_index, result); \
} \
} \
MZ_MACRO_END
#define TINFL_CR_FINISH }
#define TINFL_GET_BYTE(state_index, c) \
do \
{ \
while (pIn_buf_cur >= pIn_buf_end) \
{ \
TINFL_CR_RETURN(state_index, (decomp_flags & TINFL_FLAG_HAS_MORE_INPUT) ? TINFL_STATUS_NEEDS_MORE_INPUT : TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS); \
} \
c = *pIn_buf_cur++; \
} \
MZ_MACRO_END
#define TINFL_NEED_BITS(state_index, n) \
do \
{ \
mz_uint c; \
TINFL_GET_BYTE(state_index, c); \
bit_buf |= (((tinfl_bit_buf_t)c) << num_bits); \
num_bits += 8; \
} while (num_bits < (mz_uint)(n))
#define TINFL_SKIP_BITS(state_index, n) \
do \
{ \
if (num_bits < (mz_uint)(n)) \
{ \
TINFL_NEED_BITS(state_index, n); \
} \
bit_buf >>= (n); \
num_bits -= (n); \
} \
MZ_MACRO_END
#define TINFL_GET_BITS(state_index, b, n) \
do \
{ \
if (num_bits < (mz_uint)(n)) \
{ \
TINFL_NEED_BITS(state_index, n); \
} \
b = bit_buf & ((1 << (n)) - 1); \
bit_buf >>= (n); \
num_bits -= (n); \
} \
MZ_MACRO_END
/* TINFL_HUFF_BITBUF_FILL() is only used rarely, when the number of bytes remaining in the input buffer falls below 2. */
/* It reads just enough bytes from the input stream that are needed to decode the next Huffman code (and absolutely no more). It works by trying to fully decode a */
/* Huffman code by using whatever bits are currently present in the bit buffer. If this fails, it reads another byte, and tries again until it succeeds or until the */
/* bit buffer contains >=15 bits (deflate's max. Huffman code size). */
#define TINFL_HUFF_BITBUF_FILL(state_index, pLookUp, pTree) \
do \
{ \
temp = pLookUp[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]; \
if (temp >= 0) \
{ \
code_len = temp >> 9; \
if ((code_len) && (num_bits >= code_len)) \
break; \
} \
else if (num_bits > TINFL_FAST_LOOKUP_BITS) \
{ \
code_len = TINFL_FAST_LOOKUP_BITS; \
do \
{ \
temp = pTree[~temp + ((bit_buf >> code_len++) & 1)]; \
} while ((temp < 0) && (num_bits >= (code_len + 1))); \
if (temp >= 0) \
break; \
} \
TINFL_GET_BYTE(state_index, c); \
bit_buf |= (((tinfl_bit_buf_t)c) << num_bits); \
num_bits += 8; \
} while (num_bits < 15);
/* TINFL_HUFF_DECODE() decodes the next Huffman coded symbol. It's more complex than you would initially expect because the zlib API expects the decompressor to never read */
/* beyond the final byte of the deflate stream. (In other words, when this macro wants to read another byte from the input, it REALLY needs another byte in order to fully */
/* decode the next Huffman code.) Handling this properly is particularly important on raw deflate (non-zlib) streams, which aren't followed by a byte aligned adler-32. */
/* The slow path is only executed at the very end of the input buffer. */
/* v1.16: The original macro handled the case at the very end of the passed-in input buffer, but we also need to handle the case where the user passes in 1+zillion bytes */
/* following the deflate data and our non-conservative read-ahead path won't kick in here on this code. This is much trickier. */
#define TINFL_HUFF_DECODE(state_index, sym, pLookUp, pTree) \
do \
{ \
int temp; \
mz_uint code_len, c; \
if (num_bits < 15) \
{ \
if ((pIn_buf_end - pIn_buf_cur) < 2) \
{ \
TINFL_HUFF_BITBUF_FILL(state_index, pLookUp, pTree); \
} \
else \
{ \
bit_buf |= (((tinfl_bit_buf_t)pIn_buf_cur[0]) << num_bits) | (((tinfl_bit_buf_t)pIn_buf_cur[1]) << (num_bits + 8)); \
pIn_buf_cur += 2; \
num_bits += 16; \
} \
} \
if ((temp = pLookUp[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0) \
code_len = temp >> 9, temp &= 511; \
else \
{ \
code_len = TINFL_FAST_LOOKUP_BITS; \
do \
{ \
temp = pTree[~temp + ((bit_buf >> code_len++) & 1)]; \
} while (temp < 0); \
} \
sym = temp; \
bit_buf >>= code_len; \
num_bits -= code_len; \
} \
MZ_MACRO_END
static void tinfl_clear_tree(tinfl_decompressor *r)
{
if (r->m_type == 0)
MZ_CLEAR_ARR(r->m_tree_0);
else if (r->m_type == 1)
MZ_CLEAR_ARR(r->m_tree_1);
else
MZ_CLEAR_ARR(r->m_tree_2);
}
tinfl_status tinfl_decompress(tinfl_decompressor *r, const mz_uint8 *pIn_buf_next, size_t *pIn_buf_size, mz_uint8 *pOut_buf_start, mz_uint8 *pOut_buf_next, size_t *pOut_buf_size, const mz_uint32 decomp_flags)
{
static const mz_uint16 s_length_base[31] = { 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0 };
static const mz_uint8 s_length_extra[31] = { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 0, 0 };
static const mz_uint16 s_dist_base[32] = { 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577, 0, 0 };
static const mz_uint8 s_dist_extra[32] = { 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13 };
static const mz_uint8 s_length_dezigzag[19] = { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 };
static const mz_uint16 s_min_table_sizes[3] = { 257, 1, 4 };
mz_int16 *pTrees[3];
mz_uint8 *pCode_sizes[3];
tinfl_status status = TINFL_STATUS_FAILED;
mz_uint32 num_bits, dist, counter, num_extra;
tinfl_bit_buf_t bit_buf;
const mz_uint8 *pIn_buf_cur = pIn_buf_next, *const pIn_buf_end = pIn_buf_next + *pIn_buf_size;
mz_uint8 *pOut_buf_cur = pOut_buf_next, *const pOut_buf_end = pOut_buf_next ? pOut_buf_next + *pOut_buf_size : NULL;
size_t out_buf_size_mask = (decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF) ? (size_t)-1 : ((pOut_buf_next - pOut_buf_start) + *pOut_buf_size) - 1, dist_from_out_buf_start;
/* Ensure the output buffer's size is a power of 2, unless the output buffer is large enough to hold the entire output file (in which case it doesn't matter). */
if ((!pOut_buf_start) || (!pOut_buf_next) || (!pIn_buf_size) || (!pOut_buf_size))
{
return TINFL_STATUS_BAD_PARAM;
}
if (((out_buf_size_mask + 1) & out_buf_size_mask) || (pOut_buf_next < pOut_buf_start))
{
*pIn_buf_size = *pOut_buf_size = 0;
return TINFL_STATUS_BAD_PARAM;
}
pTrees[0] = r->m_tree_0;
pTrees[1] = r->m_tree_1;
pTrees[2] = r->m_tree_2;
pCode_sizes[0] = r->m_code_size_0;
pCode_sizes[1] = r->m_code_size_1;
pCode_sizes[2] = r->m_code_size_2;
num_bits = r->m_num_bits;
bit_buf = r->m_bit_buf;
dist = r->m_dist;
counter = r->m_counter;
num_extra = r->m_num_extra;
dist_from_out_buf_start = r->m_dist_from_out_buf_start;
TINFL_CR_BEGIN
bit_buf = num_bits = dist = counter = num_extra = r->m_zhdr0 = r->m_zhdr1 = 0;
r->m_z_adler32 = r->m_check_adler32 = 1;
if (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER)
{
TINFL_GET_BYTE(1, r->m_zhdr0);
TINFL_GET_BYTE(2, r->m_zhdr1);
counter = (((r->m_zhdr0 * 256 + r->m_zhdr1) % 31 != 0) || (r->m_zhdr1 & 32) || ((r->m_zhdr0 & 15) != 8));
if (!(decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF))
counter |= (((1U << (8U + (r->m_zhdr0 >> 4))) > 32768U) || ((out_buf_size_mask + 1) < (size_t)((size_t)1 << (8U + (r->m_zhdr0 >> 4)))));
if (counter)
{
TINFL_CR_RETURN_FOREVER(36, TINFL_STATUS_FAILED);
}
}
do
{
TINFL_GET_BITS(3, r->m_final, 3);
r->m_type = r->m_final >> 1;
if (r->m_type == 0)
{
TINFL_SKIP_BITS(5, num_bits & 7);
for (counter = 0; counter < 4; ++counter)
{
if (num_bits)
TINFL_GET_BITS(6, r->m_raw_header[counter], 8);
else
TINFL_GET_BYTE(7, r->m_raw_header[counter]);
}
if ((counter = (r->m_raw_header[0] | (r->m_raw_header[1] << 8))) != (mz_uint)(0xFFFF ^ (r->m_raw_header[2] | (r->m_raw_header[3] << 8))))
{
TINFL_CR_RETURN_FOREVER(39, TINFL_STATUS_FAILED);
}
while ((counter) && (num_bits))
{
TINFL_GET_BITS(51, dist, 8);
while (pOut_buf_cur >= pOut_buf_end)
{
TINFL_CR_RETURN(52, TINFL_STATUS_HAS_MORE_OUTPUT);
}
*pOut_buf_cur++ = (mz_uint8)dist;
counter--;
}
while (counter)
{
size_t n;
while (pOut_buf_cur >= pOut_buf_end)
{
TINFL_CR_RETURN(9, TINFL_STATUS_HAS_MORE_OUTPUT);
}
while (pIn_buf_cur >= pIn_buf_end)
{
TINFL_CR_RETURN(38, (decomp_flags & TINFL_FLAG_HAS_MORE_INPUT) ? TINFL_STATUS_NEEDS_MORE_INPUT : TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS);
}
n = MZ_MIN(MZ_MIN((size_t)(pOut_buf_end - pOut_buf_cur), (size_t)(pIn_buf_end - pIn_buf_cur)), counter);
TINFL_MEMCPY(pOut_buf_cur, pIn_buf_cur, n);
pIn_buf_cur += n;
pOut_buf_cur += n;
counter -= (mz_uint)n;
}
}
else if (r->m_type == 3)
{
TINFL_CR_RETURN_FOREVER(10, TINFL_STATUS_FAILED);
}
else
{
if (r->m_type == 1)
{
mz_uint8 *p = r->m_code_size_0;
mz_uint i;
r->m_table_sizes[0] = 288;
r->m_table_sizes[1] = 32;
TINFL_MEMSET(r->m_code_size_1, 5, 32);
for (i = 0; i <= 143; ++i)
*p++ = 8;
for (; i <= 255; ++i)
*p++ = 9;
for (; i <= 279; ++i)
*p++ = 7;
for (; i <= 287; ++i)
*p++ = 8;
}
else
{
for (counter = 0; counter < 3; counter++)
{
TINFL_GET_BITS(11, r->m_table_sizes[counter], "\05\05\04"[counter]);
r->m_table_sizes[counter] += s_min_table_sizes[counter];
}
MZ_CLEAR_ARR(r->m_code_size_2);
for (counter = 0; counter < r->m_table_sizes[2]; counter++)
{
mz_uint s;
TINFL_GET_BITS(14, s, 3);
r->m_code_size_2[s_length_dezigzag[counter]] = (mz_uint8)s;
}
r->m_table_sizes[2] = 19;
}
for (; (int)r->m_type >= 0; r->m_type--)
{
int tree_next, tree_cur;
mz_int16 *pLookUp;
mz_int16 *pTree;
mz_uint8 *pCode_size;
mz_uint i, j, used_syms, total, sym_index, next_code[17], total_syms[16];
pLookUp = r->m_look_up[r->m_type];
pTree = pTrees[r->m_type];
pCode_size = pCode_sizes[r->m_type];
MZ_CLEAR_ARR(total_syms);
TINFL_MEMSET(pLookUp, 0, sizeof(r->m_look_up[0]));
tinfl_clear_tree(r);
for (i = 0; i < r->m_table_sizes[r->m_type]; ++i)
total_syms[pCode_size[i]]++;
used_syms = 0, total = 0;
next_code[0] = next_code[1] = 0;
for (i = 1; i <= 15; ++i)
{
used_syms += total_syms[i];
next_code[i + 1] = (total = ((total + total_syms[i]) << 1));
}
if ((65536 != total) && (used_syms > 1))
{
TINFL_CR_RETURN_FOREVER(35, TINFL_STATUS_FAILED);
}
for (tree_next = -1, sym_index = 0; sym_index < r->m_table_sizes[r->m_type]; ++sym_index)
{
mz_uint rev_code = 0, l, cur_code, code_size = pCode_size[sym_index];
if (!code_size)
continue;
cur_code = next_code[code_size]++;
for (l = code_size; l > 0; l--, cur_code >>= 1)
rev_code = (rev_code << 1) | (cur_code & 1);
if (code_size <= TINFL_FAST_LOOKUP_BITS)
{
mz_int16 k = (mz_int16)((code_size << 9) | sym_index);
while (rev_code < TINFL_FAST_LOOKUP_SIZE)
{
pLookUp[rev_code] = k;
rev_code += (1 << code_size);
}
continue;
}
if (0 == (tree_cur = pLookUp[rev_code & (TINFL_FAST_LOOKUP_SIZE - 1)]))
{
pLookUp[rev_code & (TINFL_FAST_LOOKUP_SIZE - 1)] = (mz_int16)tree_next;
tree_cur = tree_next;
tree_next -= 2;
}
rev_code >>= (TINFL_FAST_LOOKUP_BITS - 1);
for (j = code_size; j > (TINFL_FAST_LOOKUP_BITS + 1); j--)
{
tree_cur -= ((rev_code >>= 1) & 1);
if (!pTree[-tree_cur - 1])
{
pTree[-tree_cur - 1] = (mz_int16)tree_next;
tree_cur = tree_next;
tree_next -= 2;
}
else
tree_cur = pTree[-tree_cur - 1];
}
tree_cur -= ((rev_code >>= 1) & 1);
pTree[-tree_cur - 1] = (mz_int16)sym_index;
}
if (r->m_type == 2)
{
for (counter = 0; counter < (r->m_table_sizes[0] + r->m_table_sizes[1]);)
{
mz_uint s;
TINFL_HUFF_DECODE(16, dist, r->m_look_up[2], r->m_tree_2);
if (dist < 16)
{
r->m_len_codes[counter++] = (mz_uint8)dist;
continue;
}
if ((dist == 16) && (!counter))
{
TINFL_CR_RETURN_FOREVER(17, TINFL_STATUS_FAILED);
}
num_extra = "\02\03\07"[dist - 16];
TINFL_GET_BITS(18, s, num_extra);
s += "\03\03\013"[dist - 16];
TINFL_MEMSET(r->m_len_codes + counter, (dist == 16) ? r->m_len_codes[counter - 1] : 0, s);
counter += s;
}
if ((r->m_table_sizes[0] + r->m_table_sizes[1]) != counter)
{
TINFL_CR_RETURN_FOREVER(21, TINFL_STATUS_FAILED);
}
TINFL_MEMCPY(r->m_code_size_0, r->m_len_codes, r->m_table_sizes[0]);
TINFL_MEMCPY(r->m_code_size_1, r->m_len_codes + r->m_table_sizes[0], r->m_table_sizes[1]);
}
}
for (;;)
{
mz_uint8 *pSrc;
for (;;)
{
if (((pIn_buf_end - pIn_buf_cur) < 4) || ((pOut_buf_end - pOut_buf_cur) < 2))
{
TINFL_HUFF_DECODE(23, counter, r->m_look_up[0], r->m_tree_0);
if (counter >= 256)
break;
while (pOut_buf_cur >= pOut_buf_end)
{
TINFL_CR_RETURN(24, TINFL_STATUS_HAS_MORE_OUTPUT);
}
*pOut_buf_cur++ = (mz_uint8)counter;
}
else
{
int sym2;
mz_uint code_len;
#if TINFL_USE_64BIT_BITBUF
if (num_bits < 30)
{
bit_buf |= (((tinfl_bit_buf_t)MZ_READ_LE32(pIn_buf_cur)) << num_bits);
pIn_buf_cur += 4;
num_bits += 32;
}
#else
if (num_bits < 15)
{
bit_buf |= (((tinfl_bit_buf_t)MZ_READ_LE16(pIn_buf_cur)) << num_bits);
pIn_buf_cur += 2;
num_bits += 16;
}
#endif
if ((sym2 = r->m_look_up[0][bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0)
code_len = sym2 >> 9;
else
{
code_len = TINFL_FAST_LOOKUP_BITS;
do
{
sym2 = r->m_tree_0[~sym2 + ((bit_buf >> code_len++) & 1)];
} while (sym2 < 0);
}
counter = sym2;
bit_buf >>= code_len;
num_bits -= code_len;
if (counter & 256)
break;
#if !TINFL_USE_64BIT_BITBUF
if (num_bits < 15)
{
bit_buf |= (((tinfl_bit_buf_t)MZ_READ_LE16(pIn_buf_cur)) << num_bits);
pIn_buf_cur += 2;
num_bits += 16;
}
#endif
if ((sym2 = r->m_look_up[0][bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0)
code_len = sym2 >> 9;
else
{
code_len = TINFL_FAST_LOOKUP_BITS;
do
{
sym2 = r->m_tree_0[~sym2 + ((bit_buf >> code_len++) & 1)];
} while (sym2 < 0);
}
bit_buf >>= code_len;
num_bits -= code_len;
pOut_buf_cur[0] = (mz_uint8)counter;
if (sym2 & 256)
{
pOut_buf_cur++;
counter = sym2;
break;
}
pOut_buf_cur[1] = (mz_uint8)sym2;
pOut_buf_cur += 2;
}
}
if ((counter &= 511) == 256)
break;
num_extra = s_length_extra[counter - 257];
counter = s_length_base[counter - 257];
if (num_extra)
{
mz_uint extra_bits;
TINFL_GET_BITS(25, extra_bits, num_extra);
counter += extra_bits;
}
TINFL_HUFF_DECODE(26, dist, r->m_look_up[1], r->m_tree_1);
num_extra = s_dist_extra[dist];
dist = s_dist_base[dist];
if (num_extra)
{
mz_uint extra_bits;
TINFL_GET_BITS(27, extra_bits, num_extra);
dist += extra_bits;
}
dist_from_out_buf_start = pOut_buf_cur - pOut_buf_start;
if ((dist == 0 || dist > dist_from_out_buf_start || dist_from_out_buf_start == 0) && (decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF))
{
TINFL_CR_RETURN_FOREVER(37, TINFL_STATUS_FAILED);
}
pSrc = pOut_buf_start + ((dist_from_out_buf_start - dist) & out_buf_size_mask);
if ((MZ_MAX(pOut_buf_cur, pSrc) + counter) > pOut_buf_end)
{
while (counter--)
{
while (pOut_buf_cur >= pOut_buf_end)
{
TINFL_CR_RETURN(53, TINFL_STATUS_HAS_MORE_OUTPUT);
}
*pOut_buf_cur++ = pOut_buf_start[(dist_from_out_buf_start++ - dist) & out_buf_size_mask];
}
continue;
}
#if MINIZ_USE_UNALIGNED_LOADS_AND_STORES
else if ((counter >= 9) && (counter <= dist))
{
const mz_uint8 *pSrc_end = pSrc + (counter & ~7);
do
{
#ifdef MINIZ_UNALIGNED_USE_MEMCPY
memcpy(pOut_buf_cur, pSrc, sizeof(mz_uint32)*2);
#else
((mz_uint32 *)pOut_buf_cur)[0] = ((const mz_uint32 *)pSrc)[0];
((mz_uint32 *)pOut_buf_cur)[1] = ((const mz_uint32 *)pSrc)[1];
#endif
pOut_buf_cur += 8;
} while ((pSrc += 8) < pSrc_end);
if ((counter &= 7) < 3)
{
if (counter)
{
pOut_buf_cur[0] = pSrc[0];
if (counter > 1)
pOut_buf_cur[1] = pSrc[1];
pOut_buf_cur += counter;
}
continue;
}
}
#endif
while(counter>2)
{
pOut_buf_cur[0] = pSrc[0];
pOut_buf_cur[1] = pSrc[1];
pOut_buf_cur[2] = pSrc[2];
pOut_buf_cur += 3;
pSrc += 3;
counter -= 3;
}
if (counter > 0)
{
pOut_buf_cur[0] = pSrc[0];
if (counter > 1)
pOut_buf_cur[1] = pSrc[1];
pOut_buf_cur += counter;
}
}
}
} while (!(r->m_final & 1));
/* Ensure byte alignment and put back any bytes from the bitbuf if we've looked ahead too far on gzip, or other Deflate streams followed by arbitrary data. */
/* I'm being super conservative here. A number of simplifications can be made to the byte alignment part, and the Adler32 check shouldn't ever need to worry about reading from the bitbuf now. */
TINFL_SKIP_BITS(32, num_bits & 7);
while ((pIn_buf_cur > pIn_buf_next) && (num_bits >= 8))
{
--pIn_buf_cur;
num_bits -= 8;
}
bit_buf &= ~(~(tinfl_bit_buf_t)0 << num_bits);
MZ_ASSERT(!num_bits); /* if this assert fires then we've read beyond the end of non-deflate/zlib streams with following data (such as gzip streams). */
if (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER)
{
for (counter = 0; counter < 4; ++counter)
{
mz_uint s;
if (num_bits)
TINFL_GET_BITS(41, s, 8);
else
TINFL_GET_BYTE(42, s);
r->m_z_adler32 = (r->m_z_adler32 << 8) | s;
}
}
TINFL_CR_RETURN_FOREVER(34, TINFL_STATUS_DONE);
TINFL_CR_FINISH
common_exit:
/* As long as we aren't telling the caller that we NEED more input to make forward progress: */
/* Put back any bytes from the bitbuf in case we've looked ahead too far on gzip, or other Deflate streams followed by arbitrary data. */
/* We need to be very careful here to NOT push back any bytes we definitely know we need to make forward progress, though, or we'll lock the caller up into an inf loop. */
if ((status != TINFL_STATUS_NEEDS_MORE_INPUT) && (status != TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS))
{
while ((pIn_buf_cur > pIn_buf_next) && (num_bits >= 8))
{
--pIn_buf_cur;
num_bits -= 8;
}
}
r->m_num_bits = num_bits;
r->m_bit_buf = bit_buf & ~(~(tinfl_bit_buf_t)0 << num_bits);
r->m_dist = dist;
r->m_counter = counter;
r->m_num_extra = num_extra;
r->m_dist_from_out_buf_start = dist_from_out_buf_start;
*pIn_buf_size = pIn_buf_cur - pIn_buf_next;
*pOut_buf_size = pOut_buf_cur - pOut_buf_next;
if ((decomp_flags & (TINFL_FLAG_PARSE_ZLIB_HEADER | TINFL_FLAG_COMPUTE_ADLER32)) && (status >= 0))
{
const mz_uint8 *ptr = pOut_buf_next;
size_t buf_len = *pOut_buf_size;
mz_uint32 i, s1 = r->m_check_adler32 & 0xffff, s2 = r->m_check_adler32 >> 16;
size_t block_len = buf_len % 5552;
while (buf_len)
{
for (i = 0; i + 7 < block_len; i += 8, ptr += 8)
{
s1 += ptr[0], s2 += s1;
s1 += ptr[1], s2 += s1;
s1 += ptr[2], s2 += s1;
s1 += ptr[3], s2 += s1;
s1 += ptr[4], s2 += s1;
s1 += ptr[5], s2 += s1;
s1 += ptr[6], s2 += s1;
s1 += ptr[7], s2 += s1;
}
for (; i < block_len; ++i)
s1 += *ptr++, s2 += s1;
s1 %= 65521U, s2 %= 65521U;
buf_len -= block_len;
block_len = 5552;
}
r->m_check_adler32 = (s2 << 16) + s1;
if ((status == TINFL_STATUS_DONE) && (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER) && (r->m_check_adler32 != r->m_z_adler32))
status = TINFL_STATUS_ADLER32_MISMATCH;
}
return status;
}
size_t tinfl_decompress_mem_to_mem(void *pOut_buf, size_t out_buf_len, const void *pSrc_buf, size_t src_buf_len, int flags)
{
tinfl_decompressor decomp;
tinfl_status status;
tinfl_init(&decomp);
status = tinfl_decompress(&decomp, (const mz_uint8 *)pSrc_buf, &src_buf_len, (mz_uint8 *)pOut_buf, (mz_uint8 *)pOut_buf, &out_buf_len, (flags & ~TINFL_FLAG_HAS_MORE_INPUT) | TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF);
return (status != TINFL_STATUS_DONE) ? TINFL_DECOMPRESS_MEM_TO_MEM_FAILED : out_buf_len;
}
#ifndef MINIZ_NO_MALLOC
tinfl_decompressor *tinfl_decompressor_alloc(void)
{
tinfl_decompressor *pDecomp = (tinfl_decompressor *)MZ_MALLOC(sizeof(tinfl_decompressor));
if (pDecomp)
tinfl_init(pDecomp);
return pDecomp;
}
void tinfl_decompressor_free(tinfl_decompressor *pDecomp)
{
MZ_FREE(pDecomp);
}
#endif
#ifdef __cplusplus
}
#endif
#endif /*#ifndef MINIZ_NO_INFLATE_APIS*/

218
src/lib/miniz/miniz_tinfl.h Normal file
View File

@ -0,0 +1,218 @@
#pragma once
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
/* ------------------- Types and macros */
typedef unsigned char mz_uint8;
typedef signed short mz_int16;
typedef unsigned short mz_uint16;
typedef unsigned int mz_uint32;
typedef unsigned int mz_uint;
typedef int64_t mz_int64;
typedef uint64_t mz_uint64;
typedef int mz_bool;
#define MZ_FALSE (0)
#define MZ_TRUE (1)
/* Works around MSVC's spammy "warning C4127: conditional expression is constant" message. */
#ifdef _MSC_VER
#define MZ_MACRO_END while (0, 0)
#else
#define MZ_MACRO_END while (0)
#endif
#define MZ_ASSERT(x) assert(x)
#ifdef MINIZ_NO_MALLOC
#define MZ_MALLOC(x) NULL
#define MZ_FREE(x) (void)x, ((void)0)
#define MZ_REALLOC(p, x) NULL
#else
#define MZ_MALLOC(x) malloc(x)
#define MZ_FREE(x) free(x)
#define MZ_REALLOC(p, x) realloc(p, x)
#endif
#define MZ_MAX(a, b) (((a) > (b)) ? (a) : (b))
#define MZ_MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MZ_CLEAR_OBJ(obj) memset(&(obj), 0, sizeof(obj))
#define MZ_CLEAR_ARR(obj) memset((obj), 0, sizeof(obj))
#define MZ_CLEAR_PTR(obj) memset((obj), 0, sizeof(*obj))
#if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN
#define MZ_READ_LE16(p) *((const mz_uint16 *)(p))
#define MZ_READ_LE32(p) *((const mz_uint32 *)(p))
#else
#define MZ_READ_LE16(p) ((mz_uint32)(((const mz_uint8 *)(p))[0]) | ((mz_uint32)(((const mz_uint8 *)(p))[1]) << 8U))
#define MZ_READ_LE32(p) ((mz_uint32)(((const mz_uint8 *)(p))[0]) | ((mz_uint32)(((const mz_uint8 *)(p))[1]) << 8U) | ((mz_uint32)(((const mz_uint8 *)(p))[2]) << 16U) | ((mz_uint32)(((const mz_uint8 *)(p))[3]) << 24U))
#endif
#define MZ_READ_LE64(p) (((mz_uint64)MZ_READ_LE32(p)) | (((mz_uint64)MZ_READ_LE32((const mz_uint8 *)(p) + sizeof(mz_uint32))) << 32U))
#ifdef _MSC_VER
#define MZ_FORCEINLINE __forceinline
#elif defined(__GNUC__)
#define MZ_FORCEINLINE __inline__ __attribute__((__always_inline__))
#else
#define MZ_FORCEINLINE inline
#endif
#ifdef __cplusplus
extern "C" {
#endif
#define MZ_UINT16_MAX (0xFFFFU)
#define MZ_UINT32_MAX (0xFFFFFFFFU)
#ifdef __cplusplus
}
#endif
/* ------------------- Low-level Decompression API Definitions */
#ifndef MINIZ_NO_INFLATE_APIS
#ifdef __cplusplus
extern "C" {
#endif
/* Decompression flags used by tinfl_decompress(). */
/* TINFL_FLAG_PARSE_ZLIB_HEADER: If set, the input has a valid zlib header and ends with an adler32 checksum (it's a valid zlib stream). Otherwise, the input is a raw deflate stream. */
/* TINFL_FLAG_HAS_MORE_INPUT: If set, there are more input bytes available beyond the end of the supplied input buffer. If clear, the input buffer contains all remaining input. */
/* TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF: If set, the output buffer is large enough to hold the entire decompressed stream. If clear, the output buffer is at least the size of the dictionary (typically 32KB). */
/* TINFL_FLAG_COMPUTE_ADLER32: Force adler-32 checksum computation of the decompressed bytes. */
enum
{
TINFL_FLAG_PARSE_ZLIB_HEADER = 1,
TINFL_FLAG_HAS_MORE_INPUT = 2,
TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF = 4,
TINFL_FLAG_COMPUTE_ADLER32 = 8
};
/* High level decompression functions: */
/* tinfl_decompress_mem_to_heap() decompresses a block in memory to a heap block allocated via malloc(). */
/* On entry: */
/* pSrc_buf, src_buf_len: Pointer and size of the Deflate or zlib source data to decompress. */
/* On return: */
/* Function returns a pointer to the decompressed data, or NULL on failure. */
/* *pOut_len will be set to the decompressed data's size, which could be larger than src_buf_len on uncompressible data. */
/* The caller must call mz_free() on the returned block when it's no longer needed. */
void *tinfl_decompress_mem_to_heap(const void *pSrc_buf, size_t src_buf_len, size_t *pOut_len, int flags);
/* tinfl_decompress_mem_to_mem() decompresses a block in memory to another block in memory. */
/* Returns TINFL_DECOMPRESS_MEM_TO_MEM_FAILED on failure, or the number of bytes written on success. */
#define TINFL_DECOMPRESS_MEM_TO_MEM_FAILED ((size_t)(-1))
size_t tinfl_decompress_mem_to_mem(void *pOut_buf, size_t out_buf_len, const void *pSrc_buf, size_t src_buf_len, int flags);
/* tinfl_decompress_mem_to_callback() decompresses a block in memory to an internal 32KB buffer, and a user provided callback function will be called to flush the buffer. */
/* Returns 1 on success or 0 on failure. */
typedef int (*tinfl_put_buf_func_ptr)(const void *pBuf, int len, void *pUser);
int tinfl_decompress_mem_to_callback(const void *pIn_buf, size_t *pIn_buf_size, tinfl_put_buf_func_ptr pPut_buf_func, void *pPut_buf_user, int flags);
struct tinfl_decompressor_tag;
typedef struct tinfl_decompressor_tag tinfl_decompressor;
#ifndef MINIZ_NO_MALLOC
/* Allocate the tinfl_decompressor structure in C so that */
/* non-C language bindings to tinfl_ API don't need to worry about */
/* structure size and allocation mechanism. */
tinfl_decompressor *tinfl_decompressor_alloc(void);
void tinfl_decompressor_free(tinfl_decompressor *pDecomp);
#endif
/* Max size of LZ dictionary. */
#define TINFL_LZ_DICT_SIZE 32768
/* Return status. */
typedef enum {
/* This flags indicates the inflator needs 1 or more input bytes to make forward progress, but the caller is indicating that no more are available. The compressed data */
/* is probably corrupted. If you call the inflator again with more bytes it'll try to continue processing the input but this is a BAD sign (either the data is corrupted or you called it incorrectly). */
/* If you call it again with no input you'll just get TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS again. */
TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS = -4,
/* This flag indicates that one or more of the input parameters was obviously bogus. (You can try calling it again, but if you get this error the calling code is wrong.) */
TINFL_STATUS_BAD_PARAM = -3,
/* This flags indicate the inflator is finished but the adler32 check of the uncompressed data didn't match. If you call it again it'll return TINFL_STATUS_DONE. */
TINFL_STATUS_ADLER32_MISMATCH = -2,
/* This flags indicate the inflator has somehow failed (bad code, corrupted input, etc.). If you call it again without resetting via tinfl_init() it it'll just keep on returning the same status failure code. */
TINFL_STATUS_FAILED = -1,
/* Any status code less than TINFL_STATUS_DONE must indicate a failure. */
/* This flag indicates the inflator has returned every byte of uncompressed data that it can, has consumed every byte that it needed, has successfully reached the end of the deflate stream, and */
/* if zlib headers and adler32 checking enabled that it has successfully checked the uncompressed data's adler32. If you call it again you'll just get TINFL_STATUS_DONE over and over again. */
TINFL_STATUS_DONE = 0,
/* This flag indicates the inflator MUST have more input data (even 1 byte) before it can make any more forward progress, or you need to clear the TINFL_FLAG_HAS_MORE_INPUT */
/* flag on the next call if you don't have any more source data. If the source data was somehow corrupted it's also possible (but unlikely) for the inflator to keep on demanding input to */
/* proceed, so be sure to properly set the TINFL_FLAG_HAS_MORE_INPUT flag. */
TINFL_STATUS_NEEDS_MORE_INPUT = 1,
/* This flag indicates the inflator definitely has 1 or more bytes of uncompressed data available, but it cannot write this data into the output buffer. */
/* Note if the source compressed data was corrupted it's possible for the inflator to return a lot of uncompressed data to the caller. I've been assuming you know how much uncompressed data to expect */
/* (either exact or worst case) and will stop calling the inflator and fail after receiving too much. In pure streaming scenarios where you have no idea how many bytes to expect this may not be possible */
/* so I may need to add some code to address this. */
TINFL_STATUS_HAS_MORE_OUTPUT = 2
} tinfl_status;
/* Initializes the decompressor to its initial state. */
#define tinfl_init(r) \
do \
{ \
(r)->m_state = 0; \
} \
MZ_MACRO_END
#define tinfl_get_adler32(r) (r)->m_check_adler32
/* Main low-level decompressor coroutine function. This is the only function actually needed for decompression. All the other functions are just high-level helpers for improved usability. */
/* This is a universal API, i.e. it can be used as a building block to build any desired higher level decompression API. In the limit case, it can be called once per every byte input or output. */
tinfl_status tinfl_decompress(tinfl_decompressor *r, const mz_uint8 *pIn_buf_next, size_t *pIn_buf_size, mz_uint8 *pOut_buf_start, mz_uint8 *pOut_buf_next, size_t *pOut_buf_size, const mz_uint32 decomp_flags);
/* Internal/private bits follow. */
enum
{
TINFL_MAX_HUFF_TABLES = 3,
TINFL_MAX_HUFF_SYMBOLS_0 = 288,
TINFL_MAX_HUFF_SYMBOLS_1 = 32,
TINFL_MAX_HUFF_SYMBOLS_2 = 19,
TINFL_FAST_LOOKUP_BITS = 10,
TINFL_FAST_LOOKUP_SIZE = 1 << TINFL_FAST_LOOKUP_BITS
};
#if MINIZ_HAS_64BIT_REGISTERS
#define TINFL_USE_64BIT_BITBUF 1
#else
#define TINFL_USE_64BIT_BITBUF 0
#endif
#if TINFL_USE_64BIT_BITBUF
typedef mz_uint64 tinfl_bit_buf_t;
#define TINFL_BITBUF_SIZE (64)
#else
typedef mz_uint32 tinfl_bit_buf_t;
#define TINFL_BITBUF_SIZE (32)
#endif
struct tinfl_decompressor_tag
{
mz_uint32 m_state, m_num_bits, m_zhdr0, m_zhdr1, m_z_adler32, m_final, m_type, m_check_adler32, m_dist, m_counter, m_num_extra, m_table_sizes[TINFL_MAX_HUFF_TABLES];
tinfl_bit_buf_t m_bit_buf;
size_t m_dist_from_out_buf_start;
mz_int16 m_look_up[TINFL_MAX_HUFF_TABLES][TINFL_FAST_LOOKUP_SIZE];
mz_int16 m_tree_0[TINFL_MAX_HUFF_SYMBOLS_0 * 2];
mz_int16 m_tree_1[TINFL_MAX_HUFF_SYMBOLS_1 * 2];
mz_int16 m_tree_2[TINFL_MAX_HUFF_SYMBOLS_2 * 2];
mz_uint8 m_code_size_0[TINFL_MAX_HUFF_SYMBOLS_0];
mz_uint8 m_code_size_1[TINFL_MAX_HUFF_SYMBOLS_1];
mz_uint8 m_code_size_2[TINFL_MAX_HUFF_SYMBOLS_2];
mz_uint8 m_raw_header[4], m_len_codes[TINFL_MAX_HUFF_SYMBOLS_0 + TINFL_MAX_HUFF_SYMBOLS_1 + 137];
};
#ifdef __cplusplus
}
#endif
#endif /*#ifndef MINIZ_NO_INFLATE_APIS*/

File diff suppressed because it is too large Load Diff

View File

@ -12,6 +12,9 @@
#define LOVR_PATH_SEP '/'
#endif
typedef struct Archive Archive;
typedef struct File File;
bool lovrFilesystemInit(const char* archive);
void lovrFilesystemDestroy(void);
const char* lovrFilesystemGetSource(void);
@ -23,7 +26,7 @@ bool lovrFilesystemIsFile(const char* path);
bool lovrFilesystemIsDirectory(const char* path);
uint64_t lovrFilesystemGetSize(const char* path);
uint64_t lovrFilesystemGetLastModified(const char* path);
void* lovrFilesystemRead(const char* path, size_t bytes, size_t* bytesRead);
void* lovrFilesystemRead(const char* path, size_t* size);
void lovrFilesystemGetDirectoryItems(const char* path, void (*callback)(void* context, const char* path), void* context);
const char* lovrFilesystemGetIdentity(void);
bool lovrFilesystemSetIdentity(const char* identity, bool precedence);
@ -37,3 +40,26 @@ size_t lovrFilesystemGetUserDirectory(char* buffer, size_t size);
size_t lovrFilesystemGetWorkingDirectory(char* buffer, size_t size);
const char* lovrFilesystemGetRequirePath(void);
void lovrFilesystemSetRequirePath(const char* requirePath);
// Archive
Archive* lovrArchiveCreate(const char* path, const char* mountpoint, const char* root);
void lovrArchiveDestroy(void* ref);
// File
typedef enum {
OPEN_READ,
OPEN_WRITE,
OPEN_APPEND
} OpenMode;
File* lovrFileCreate(const char* path, OpenMode mode, const char** error);
void lovrFileDestroy(void* ref);
const char* lovrFileGetPath(File* file);
OpenMode lovrFileGetMode(File* file);
uint64_t lovrFileGetSize(File* file);
bool lovrFileRead(File* file, void* data, size_t size, size_t* count);
bool lovrFileWrite(File* file, const void* data, size_t size, size_t* count);
bool lovrFileSeek(File* file, uint64_t offset);
uint64_t lovrFileTell(File* file);