diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..460ecb9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*build* +*_export.h +.idea diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..15b4860 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,98 @@ +cmake_minimum_required(VERSION 3.0) +project(stdarc_c VERSION 0.0.1 LANGUAGES C) + +set(CMAKE_C_STANDARD 90) +cmake_policy(SET CMP0063 NEW) +set(CMAKE_C_VISIBILITY_PRESET hidden) +set(CMAKE_VISIBILITY_INLINES_HIDDEN YES) + +option(BUILD_SHARED_LIBS "Build using shared libraries" OFF) +if (BUILD_SHARED_LIBS) + set(LIBRARY_TYPE_FLAG "SHARED") +else () + set(LIBRARY_TYPE_FLAG "STATIC") +endif () + +# control where the static and shared libraries are built so that on windows +# we don't need to tinker with the path to run the executable +set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}") +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}") +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}") + +add_library("${PROJECT_NAME}_compiler_flags" INTERFACE) +target_compile_features("${PROJECT_NAME}_compiler_flags" INTERFACE "c_std_${CMAKE_C_STANDARD}") + +set(gcc_like "$") +set(msvc "$") +# -g -fsanitize=address -fno-omit-frame-pointer +target_compile_options( + "${PROJECT_NAME}_compiler_flags" + INTERFACE + "$<${gcc_like}:$>" + "$<${msvc}:$>" +) +if (CMAKE_C_COMPILER_ID STREQUAL "Clang" OR CMAKE_C_COMPILER_ID STREQUAL "AppleClang") + set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address") + set(CMAKE_LINKER_FLAGS_DEBUG "${CMAKE_LINKER_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address") +endif() + +# configure a header file to pass the version number only +configure_file( + "${CMAKE_CURRENT_SOURCE_DIR}/cmake/config.h.in" + "${PROJECT_NAME}Config.h" +) + +add_subdirectory("src") + +option(BUILD_DEMOS "Build demos" ON) +if (BUILD_DEMOS) + add_subdirectory("demo") +endif (BUILD_DEMOS) + +install( + FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.h" + DESTINATION "include" +) + +include(InstallRequiredSystemLibraries) +set(CPACK_BUNDLE_NAME "${PROJECT_NAME}") +set(CPACK_PACKAGE_VENDOR "r-lyeh") +set(CPACK_PACKAGE_DESCRIPTION "Small file archivers (zip, pak, tar), virtual filesystem (vfs) and dir listing in a single-file header.") +set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Collection of small public domain file archivers") +if (APPLE) + set(CPACK_BUNDLE_PLIST "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Info.plist") + set(CPACK_BUNDLE_ICON "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Info.plist") + set(CPACK_PACKAGE_ICON "${CMAKE_CURRENT_SOURCE_DIR}/cmake/CustomVolumeIcon.icns") +endif() +set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE") +set(CPACK_PACKAGE_VERSION_MAJOR "${${PROJECT_NAME}_VERSION_MAJOR}") +set(CPACK_PACKAGE_VERSION_MINOR "${${PROJECT_NAME}_VERSION_MINOR}") +set(CPACK_RESOURCE_FILE_README "${CMAKE_CURRENT_SOURCE_DIR}/cmake/README.txt") +set(CPACK_RESOURCE_FILE_WELCOME "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Welcome.txt") +set(CPACK_PACKAGE_CONTACT "https://github.com/r-lyeh/stdarc.c") + +include(CPack) +include(CMakePackageConfigHelpers) + +option(BUILD_SHARED_LIBS "Build using shared libraries" ON) +if (BUILD_SHARED_LIBS) + set(LIBRARY_TYPE_FLAG "SHARED") +else () + set(LIBRARY_TYPE_FLAG "STATIC") +endif () + +# generate the config file that is includes the exports +configure_package_config_file( + "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Config.cmake.in" + "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" + INSTALL_DESTINATION "lib/cmake/example" + NO_SET_AND_CHECK_MACRO + NO_CHECK_REQUIRED_COMPONENTS_MACRO +) + +# generate the version file for the config file +write_basic_package_version_file( + "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" + VERSION "${${PROJECT_NAME}_VERSION_MAJOR}.${${PROJECT_NAME}_VERSION_MINOR}" + COMPATIBILITY AnyNewerVersion +) diff --git a/cmake/BundleIcon.icns b/cmake/BundleIcon.icns new file mode 100644 index 0000000..8808dd6 Binary files /dev/null and b/cmake/BundleIcon.icns differ diff --git a/cmake/CTestConfig.cmake b/cmake/CTestConfig.cmake new file mode 100644 index 0000000..ec92397 --- /dev/null +++ b/cmake/CTestConfig.cmake @@ -0,0 +1,7 @@ +set(CTEST_PROJECT_NAME "stdarc_c") +set(CTEST_NIGHTLY_START_TIME "00:00:00 EST") + +set(CTEST_DROP_METHOD "http") +set(CTEST_DROP_SITE "my.cdash.org") +set(CTEST_DROP_LOCATION "/submit.php?project=stdarc_c") +set(CTEST_DROP_SITE_CDASH TRUE) diff --git a/cmake/Config.cmake.in b/cmake/Config.cmake.in new file mode 100644 index 0000000..3048dc6 --- /dev/null +++ b/cmake/Config.cmake.in @@ -0,0 +1,4 @@ + +@PACKAGE_INIT@ + +include ( "${CMAKE_CURRENT_LIST_DIR}/libstdarc_cTargets.cmake" ) diff --git a/cmake/CustomVolumeIcon.icns b/cmake/CustomVolumeIcon.icns new file mode 100644 index 0000000..3862a51 Binary files /dev/null and b/cmake/CustomVolumeIcon.icns differ diff --git a/cmake/Info.plist b/cmake/Info.plist new file mode 100644 index 0000000..e5a7d00 --- /dev/null +++ b/cmake/Info.plist @@ -0,0 +1,14 @@ + + + + + CFBundleExecutable + BundleGeneratorTest + CFBundleIconFile + BundleGeneratorTest.icns + CFBundleInfoDictionaryVersion + 6.0 + CFBundlePackageType + APPL + + diff --git a/cmake/MultiCPackConfig.cmake b/cmake/MultiCPackConfig.cmake new file mode 100644 index 0000000..e8f89de --- /dev/null +++ b/cmake/MultiCPackConfig.cmake @@ -0,0 +1,6 @@ +include("release/CPackConfig.cmake") + +set(CPACK_INSTALL_CMAKE_PROJECTS + "debug;stdarc_c;ALL;/" + "release;stdarc_c;ALL;/" +) diff --git a/cmake/README.txt b/cmake/README.txt new file mode 100644 index 0000000..48ff566 --- /dev/null +++ b/cmake/README.txt @@ -0,0 +1 @@ +Small file archivers (zip, pak, tar), virtual filesystem (vfs) and dir listing in a single-file header. diff --git a/cmake/Welcome.txt b/cmake/Welcome.txt new file mode 100644 index 0000000..e76cf2d --- /dev/null +++ b/cmake/Welcome.txt @@ -0,0 +1,3 @@ +Small file archivers (zip, pak, tar), virtual filesystem (vfs) and dir listing in a single-file header. + +https://github.com/r-lyeh/stdarc.c diff --git a/cmake/config.h.in b/cmake/config.h.in new file mode 100644 index 0000000..b4c9a53 --- /dev/null +++ b/cmake/config.h.in @@ -0,0 +1,9 @@ +#ifndef stdarc_c_CONFIG_H +#define stdarc_c_CONFIG_H + +#define stdarc_c_VERSION_MAJOR @stdarc_c_VERSION_MAJOR@ +#define stdarc_c_VERSION_MINOR @stdarc_c_VERSION_MINOR@ +#define stdarc_c_VERSION_PATCH @stdarc_c_VERSION_PATCH@ +#define stdarc_c_VERSION "@stdarc_c_VERSION@" + +#endif /* stdarc_c_CONFIG_H */ diff --git a/demo/CMakeLists.txt b/demo/CMakeLists.txt new file mode 100644 index 0000000..d5d9413 --- /dev/null +++ b/demo/CMakeLists.txt @@ -0,0 +1,35 @@ +set(exec_name_prefix "stdarc") + + +foreach(name "dir" "pak" "tar" "vfs" "zip") + set(EXEC_NAME "${exec_name_prefix}_${name}") + set(Source_Files "demo_${name}.c") + source_group("Source Files" FILES "${Source_Files}") + + add_library("${EXEC_NAME}" "${LIBRARY_TYPE_FLAG}" "${Source_Files}") + + target_include_directories( + "${EXEC_NAME}" + PUBLIC + "$" + "$" + ) + set_target_properties( + "${EXEC_NAME}" + PROPERTIES + LINKER_LANGUAGE + C + ) + target_link_libraries("${EXEC_NAME}" PRIVATE "${LIBRARY_NAME}") + + # install rules + + set(installable_libs "${EXEC_NAME}" "${PROJECT_NAME}_compiler_flags") + + if (TARGET "${DEPENDANT_LIBRARY}") + list(APPEND installable_libs "${DEPENDANT_LIBRARY}") + endif () + install(TARGETS ${installable_libs} + DESTINATION "bin" + EXPORT "${EXEC_NAME}Targets") +endforeach() diff --git a/demo/demo_dir.c b/demo/demo_dir.c index bf17fb2..3bb0039 100644 --- a/demo/demo_dir.c +++ b/demo/demo_dir.c @@ -8,7 +8,8 @@ int main(int argc, char **argv) { printf("list contents of %s ...\n", fname); dir *t = dir_open(fname, "rb"); if( t ) { - for( unsigned i = 0 ; i < dir_count(t); ++i ) { + unsigned i; + for( i = 0 ; i < dir_count(t); ++i ) { printf("Y %3d) %11u %s\t%s\n", i+1, dir_size(t,i), dir_name(t,i), dir_file(t,i) ? "" : ""); } dir_close(t); diff --git a/demo/demo_pak.c b/demo/demo_pak.c index f800723..c7fc9eb 100644 --- a/demo/demo_pak.c +++ b/demo/demo_pak.c @@ -10,7 +10,8 @@ int main(int argc, char **argv) { pak *z = pak_open("demo.pak", "a+b"); if( z ) { const char *fname = __FILE__; - for( FILE *fp = fopen(fname, "rb"); fp; fclose(fp), fp = 0) { + FILE *fp; + for( fp = fopen(fname, "rb"); fp; fclose(fp), fp = 0 ) { pak_append_file(z, fname, fp); } pak_close(z); @@ -21,7 +22,8 @@ int main(int argc, char **argv) { printf("testing files in %s ...\n", fname); z = pak_open(fname, "rb"); if( z ) { - for( unsigned i = 0 ; i < pak_count(z); ++i ) { + unsigned i; + for( i = 0 ; i < pak_count(z); ++i ) { printf(" %d) ", i+1); printf("@%08x ", pak_offset(z,i)); printf("%11u ", pak_size(z,i)); diff --git a/demo/demo_tar.c b/demo/demo_tar.c index ab53b8b..b7e3b14 100644 --- a/demo/demo_tar.c +++ b/demo/demo_tar.c @@ -8,7 +8,8 @@ int main(int argc, char **argv) { printf("list contents of %s ...\n", fname); tar *t = tar_open(fname, "rb"); if( t ) { - for( unsigned i = 0 ; i < tar_count(t); ++i ) { + unsigned i; + for( i = 0 ; i < tar_count(t); ++i ) { printf(" %d) @%08x %11u %s ", i+1, tar_offset(t,i), tar_size(t,i), tar_name(t,i)); void *data = tar_extract(t,i); printf("\r%c\n", data ? 'Y':'N'); // use data here: "%.*s\n",tar_size(t,i),(char*)data diff --git a/demo/demo_zip.c b/demo/demo_zip.c index 79c1aaa..7e692d4 100644 --- a/demo/demo_zip.c +++ b/demo/demo_zip.c @@ -13,7 +13,8 @@ int main(int argc, const char **argv) { zip *z = zip_open("demo.zip", "a+b"); if( z ) { // compress with DEFLATE|6. Other compressors are also supported (try LZMA|5, ULZ|9, LZ4X|3, etc.) - for( FILE *myfile = fopen(__FILE__, "rb"); myfile; fclose(myfile), myfile = 0) { + FILE *myfile; + for( myfile = fopen(__FILE__, "rb"); myfile; fclose(myfile), myfile = 0 ) { zip_append_file(z, __FILE__, myfile, 6); } zip_close(z); @@ -26,7 +27,8 @@ int main(int argc, const char **argv) { printf("testing files in %s ...\n", infile); z = zip_open(infile, "rb"); if( z ) { - for( unsigned i = 0 ; i < zip_count(z); ++i ) { + unsigned i; + for( i = 0 ; i < zip_count(z); ++i ) { printf(" %d) ", i+1); printf("[%08X] ", zip_hash(z,i)); printf("$%02X ", zip_codec(z,i)); diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..e5bacb0 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,51 @@ +set(LIBRARY_NAME "stdarc") + +set(Header_Files "arc.h") +source_group("Header Files" FILES "${Header_Files}") + +set(Source_Files + "dir.c" + "pak.c" + "tar.c" + "vfs.c" + "zip.c" + ) +source_group("Source Files" FILES "${Source_Files}") + +add_library("${LIBRARY_NAME}" "${LIBRARY_TYPE_FLAG}" "${Header_Files}" "${Source_Files}") + +target_include_directories( + "${LIBRARY_NAME}" + PUBLIC + "$" + "$" +) +set_target_properties( + "${LIBRARY_NAME}" + PROPERTIES + LINKER_LANGUAGE + C +) + +# Symbol exporter +include(GenerateExportHeader) +set(_export_file "${CMAKE_CURRENT_SOURCE_DIR}/${LIBRARY_NAME}_export.h") +generate_export_header("${LIBRARY_NAME}" EXPORT_FILE_NAME "${_export_file}") + +################# +# install rules # +################# + +# setup the version numbering +set_property(TARGET "${LIBRARY_NAME}" PROPERTY VERSION "1.0.0") +set_property(TARGET "${LIBRARY_NAME}" PROPERTY SOVERSION "1") + +set(installable_libs "${LIBRARY_NAME}" "${PROJECT_NAME}_compiler_flags") +install(FILES "${Header_Files}" "${_export_file}" DESTINATION "include") + +if (TARGET "${DEPENDANT_LIBRARY}") + list(APPEND installable_libs "${DEPENDANT_LIBRARY}") +endif () +install(TARGETS ${installable_libs} + DESTINATION "bin" + EXPORT "${LIBRARY_NAME}Targets") diff --git a/src/dir.c b/src/dir.c index 11c46ec..cfb67c0 100644 --- a/src/dir.c +++ b/src/dir.c @@ -10,10 +10,10 @@ dir *dir_open(const char *filename, const char *mode); // recursive 'r' int dir_find(dir*, const char *entryname); // returns entry number; or <0 if not found. unsigned dir_count(dir*); - char* dir_name(dir*, unsigned index); - unsigned dir_size(dir*, unsigned index); - unsigned dir_file(dir*, unsigned index); // dir_isfile? bool? - void* dir_read(dir*, unsigned index); // must free() after use + char* dir_name(dir*, unsigned int index); + unsigned int dir_size(dir*, unsigned int index); + unsigned int dir_file(dir*, unsigned int index); // dir_isfile? bool? + void* dir_read(dir*, unsigned int index); // must free() after use void dir_close(dir*); @@ -71,31 +71,39 @@ int dir_yield(dir *d, const char *pathfile, char *name, int namelen) { #ifdef _WIN32 WIN32_FIND_DATAA fdata = { 0 }; snprintf(name, namelen, "%s/*", pathfile); - for( HANDLE h = FindFirstFileA(name, &fdata ); h != INVALID_HANDLE_VALUE; (ok = FindClose( h ), h = INVALID_HANDLE_VALUE, 1)) { - for( int next = 1; next; next = FindNextFileA(h, &fdata) != 0 ) { - if( fdata.cFileName[0] == '.' ) continue; - int is_dir = (fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) > 0; - snprintf(name, namelen, "%s/%s%s", pathfile, fdata.cFileName, is_dir ? "/" : ""); - struct stat st; if( !is_dir ) if(stat(name, &st) < 0) continue; - // add - dir_entry de = { STRDUP(name), is_dir ? 0 : st.st_size, is_dir }; - d->entry = (dir_entry*)REALLOC(d->entry, ++d->count * sizeof(dir_entry)); - d->entry[d->count-1] = de; + HANDLE h; + { + for( h = FindFirstFileA(name, &fdata ); h != INVALID_HANDLE_VALUE; (ok = FindClose( h ), h = INVALID_HANDLE_VALUE, 1) ) { + int next; + for( next = 1; next; next = FindNextFileA(h, &fdata) != 0 ) { + if( fdata.cFileName[0] == '.' ) continue; + int is_dir = (fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) > 0; + snprintf(name, namelen, "%s/%s%s", pathfile, fdata.cFileName, is_dir ? "/" : ""); + struct stat st; if( !is_dir ) if(stat(name, &st) < 0) continue; + // add + dir_entry de = { STRDUP(name), is_dir ? 0 : st.st_size, is_dir }; + d->entry = (dir_entry*)REALLOC(d->entry, ++d->count * sizeof(dir_entry)); + d->entry[d->count-1] = de; + } } } #else snprintf(name, namelen, "%s/", pathfile); - for( DIR *dir = opendir(name); dir; ok = (closedir(dir), dir = 0, 1)) { - for( struct dirent *ep; ep = readdir(dir); ) { - if( ep->d_name[0] == '.' ) continue; - snprintf(name, namelen, "%s/%s", pathfile, ep->d_name); - struct stat st; if( stat(name, &st) < 0 ) continue; - DIR *tmp = opendir(ep->d_name); int is_dir = !!tmp; if(tmp) closedir(tmp); - // if( is_dir && recursive ) { dir_yield(d,name); } - // add - dir_entry de = { STRDUP(name), is_dir ? 0 : st.st_size, is_dir }; - d->entry = (dir_entry*)REALLOC(d->entry, ++d->count * sizeof(dir_entry)); - d->entry[d->count-1] = de; + DIR *dir; + { + for( dir = opendir(name); dir; ok = (closedir(dir), dir = 0, 1) ) { + struct dirent *ep; + for( ; ep = readdir(dir); ) { + if( ep->d_name[0] == '.' ) continue; + snprintf(name, namelen, "%s/%s", pathfile, ep->d_name); + struct stat st; if( stat(name, &st) < 0 ) continue; + DIR *tmp = opendir(ep->d_name); int is_dir = !!tmp; if(tmp) closedir(tmp); + // if( is_dir && recursive ) { dir_yield(d,name); } + // add + dir_entry de = { STRDUP(name), is_dir ? 0 : st.st_size, is_dir }; + d->entry = (dir_entry*)REALLOC(d->entry, ++d->count * sizeof(dir_entry)); + d->entry[d->count-1] = de; + } } } #endif @@ -107,8 +115,14 @@ dir *dir_open(const char *pathfile, const char *mode) { dir *d = (dir*)REALLOC(0, sizeof(dir)), zero = {0}; *d = zero; char *clean = STRDUP( pathfile ); - for( int i = 0; clean[i]; ++i ) if(clean[i] == '\\') clean[i] = '/'; - for( int len = strlen(clean); clean[--len] == '/'; ) clean[len] = '\0'; + { + int i; + for( i = 0; clean[i]; ++i ) if(clean[i] == '\\') clean[i] = '/'; + } + { + size_t len; + for( len = strlen(clean); clean[--len] == '/'; ) clean[len] = '\0'; + } char buffer[2048]; dir_yield(d, clean, buffer, 2048); @@ -118,7 +132,8 @@ dir *dir_open(const char *pathfile, const char *mode) { } int dir_find(dir *d, const char *entryname) { - for( int i = d->count; --i >= 0; ) { // in case of several copies, grab most recent file (last coincidence) + unsigned i; + for( i = d->count; --i >= 0; ) { // in case of several copies, grab most recent file (last coincidence) if( 0 == strcmp(entryname, d->entry[i].filename)) return i; } return -1; @@ -128,22 +143,23 @@ unsigned dir_count(dir *d) { return d ? d->count : 0; } -char* dir_name(dir *d, unsigned index) { +char* dir_name(dir *d, unsigned int index) { return d && index < d->count ? d->entry[index].filename : 0; } -unsigned dir_size(dir *d, unsigned index) { +unsigned int dir_size(dir *d, unsigned int index) { return d && index < d->count ? (unsigned)d->entry[index].size : 0; } -unsigned dir_file(dir *d, unsigned index) { +unsigned int dir_file(dir *d, unsigned int index) { return d && index < d->count ? (unsigned)!d->entry[index].is_dir : 0; } void *dir_read(dir *d, unsigned index) { if( d && index < d->count ) { void *data = 0; - for( FILE *fp = fopen(d->entry[index].filename, "rb"); fp; fclose(fp), fp = 0) { + FILE *fp; + for( fp = fopen(d->entry[index].filename, "rb"); fp; fclose(fp), fp = 0 ) { size_t len = d->entry[index].size; data = REALLOC(0, len); if( data && fread(data, 1, len, fp) != len ) { @@ -156,8 +172,11 @@ void *dir_read(dir *d, unsigned index) { } void dir_close(dir *d) { - for( int i = 0; i < d->count; ++i) { - REALLOC(d->entry[i].filename, 0); + { + unsigned i; + for( i = 0; i < d->count; ++i ) { + REALLOC(d->entry[i].filename, 0); + } } dir zero = {0}; *d = zero; @@ -168,7 +187,8 @@ void dir_close(dir *d) { int main( int argc, char **argv ) { dir *d = dir_open(argc > 1 ? argv[1] : "./", "rb"); if( d ) { - for( int i = 0; i < dir_count(d); ++i ) { + int i; + for( i = 0; i < dir_count(d); ++i ) { if( dir_file(d,i) ) printf("%3d) %11d %s\n", i + 1, dir_size(d,i), dir_name(d,i)); else diff --git a/src/pak.c b/src/pak.c index 319f2f6..74cc10b 100644 --- a/src/pak.c +++ b/src/pak.c @@ -17,21 +17,23 @@ #ifndef PAK_H #define PAK_H +#include + typedef struct pak pak; pak* pak_open(const char *fname, const char *mode /*a,r,w*/); // (w)rite or (a)ppend modes only int pak_append_file(pak*, const char *filename, FILE *in); - int pak_append_data(pak*, const char *filename, const void *in, unsigned inlen); + int pak_append_data(pak*, const char *filename, const void *in, unsigned int inlen); // (r)ead only mode int pak_find(pak*,const char *fname); // return <0 if error; index otherwise. - unsigned pak_count(pak*); - unsigned pak_size(pak*,unsigned index); - unsigned pak_offset(pak*, unsigned index); - char *pak_name(pak*,unsigned index); - void *pak_extract(pak*, unsigned index); // must free() after use + unsigned int pak_count(pak*); + unsigned int pak_size(pak*,unsigned int index); + unsigned int pak_offset(pak*, unsigned int index); + char *pak_name(pak*,unsigned int index); + void *pak_extract(pak*, unsigned int index); // must free() after use void pak_close(pak*); @@ -45,6 +47,7 @@ void pak_close(pak*); #include #include #include +#include #ifndef REALLOC #define REALLOC realloc @@ -92,7 +95,7 @@ typedef struct pak { FILE *in, *out; int dummy; pak_file *entries; - unsigned count; + unsigned int count; } pak; pak *pak_open(const char *fname, const char *mode) { @@ -135,10 +138,13 @@ pak *pak_open(const char *fname, const char *mode) { goto fail; } - for( unsigned i = 0; i < num_files; ++i ) { - pak_file *e = &p->entries[i]; - e->offset = ltoh32(e->offset); - e->size = ltoh32(e->size); + { + unsigned i; + for( i = 0; i < num_files; ++i ) { + pak_file *e = &p->entries[i]; + e->offset = ltoh32(e->offset); + e->size = ltoh32(e->size); + } } if( mode[0] == 'a' ) { @@ -183,7 +189,7 @@ fail:; return NULL; } -int pak_append_data(pak *p, const char *filename, const void *in, unsigned inlen) { +int pak_append_data(pak *p, const char *filename, const void *in, unsigned int inlen) { if(!p->out) return ERR(0, "read-only pak file"); // index meta @@ -226,17 +232,20 @@ void pak_close(pak *p) { if(p->out) { // write toc uint32_t seek = 0 + 12, dirpos = (uint32_t)ftell(p->out), dirlen = p->count * 64; - for(unsigned i = 0; i < p->count; ++i) { - pak_file *e = &p->entries[i]; - // write name (truncated if needed), and trailing zeros - char zero[56] = {0}; - int namelen = strlen(e->name); - fwrite( e->name, 1, namelen >= 56 ? 55 : namelen, p->out ); - fwrite( zero, 1, namelen >= 56 ? 1 : 56 - namelen, p->out ); - // write offset + length pair - uint32_t pseek = htol32(seek); fwrite( &pseek, 1,4, p->out ); - uint32_t psize = htol32(e->size); fwrite( &psize, 1,4, p->out ); - seek += e->size; + { + unsigned i; + for( i = 0; i < p->count; ++i ) { + pak_file *e = &p->entries[i]; + // write name (truncated if needed), and trailing zeros + char zero[56] = {0}; + int namelen = strlen(e->name); + fwrite( e->name, 1, namelen >= 56 ? 55 : namelen, p->out ); + fwrite( zero, 1, namelen >= 56 ? 1 : 56 - namelen, p->out ); + // write offset + length pair + uint32_t pseek = htol32(seek); fwrite( &pseek, 1,4, p->out ); + uint32_t psize = htol32(e->size); fwrite( &psize, 1,4, p->out ); + seek += e->size; + } } // patch header @@ -251,8 +260,11 @@ void pak_close(pak *p) { if(p->out) fclose(p->out); // clean up - for(unsigned i = 0; i < p->count; ++i) { - pak_file *e = &p->entries[i]; + { + unsigned i; + for( i = 0; i < p->count; ++i ) { + pak_file *e = &p->entries[i]; + } } REALLOC(p->entries, 0); @@ -264,14 +276,17 @@ void pak_close(pak *p) { int pak_find(pak *p, const char *filename) { if( p->in ) { - for( int i = p->count; --i >= 0; ) { - if(!strcmp(p->entries[i].name, filename)) return i; + { + unsigned i; + for( i = p->count; --i >= 0; ) { + if(!strcmp(p->entries[i].name, filename)) return i; + } } } return -1; } -unsigned pak_count(pak *p) { +unsigned int pak_count(pak *p) { return p->in ? p->count : 0; } @@ -328,14 +343,17 @@ int main(int argc, char **argv) { printf("listing %s archive ...\n", fname); p = pak_open(fname, "rb"); if( p ) { - for( unsigned i = 0; i < pak_count(p); ++i ) { - printf(" %d) @%08x %11u %s ", i+1, pak_offset(p,i), pak_size(p,i), pak_name(p,i)); - void *data = pak_extract(p,i); - printf("\r%c\n", data ? 'Y':'N'); - if(argc > 2 && data) - if(i == pak_find(p,argv[2])) - printf("%.*s\n", (int)pak_size(p,i), (char*)data); - free(data); + { + unsigned i; + for( i = 0; i < pak_count(p); ++i ) { + printf(" %d) @%08x %11u %s ", i+1, pak_offset(p,i), pak_size(p,i), pak_name(p,i)); + void *data = pak_extract(p,i); + printf("\r%c\n", data ? 'Y':'N'); + if(argc > 2 && data) + if(i == pak_find(p,argv[2])) + printf("%.*s\n", (int)pak_size(p,i), (char*)data); + free(data); + } } pak_close(p); } diff --git a/src/tar.c b/src/tar.c index 9eb352b..6a80ce3 100644 --- a/src/tar.c +++ b/src/tar.c @@ -8,12 +8,12 @@ typedef struct tar tar; tar *tar_open(const char *filename, const char *mode); - int tar_find(tar*, const char *entryname); // returns entry number; or <0 if not found. - unsigned tar_count(tar*); - char* tar_name(tar*, unsigned index); - unsigned tar_size(tar*, unsigned index); - unsigned tar_offset(tar*, unsigned index); - void* tar_extract(tar*, unsigned index); // must free() after use + unsigned int tar_find(tar*, const char *entryname); // returns entry number; or <0 if not found. + unsigned int tar_count(tar*); + char* tar_name(tar*, unsigned int index); + unsigned tar_size(tar*, unsigned int index); + unsigned tar_offset(tar*, unsigned int index); + void* tar_extract(tar*, unsigned int index); // must free() after use void tar_close(tar *t); @@ -42,10 +42,10 @@ void tar_close(tar *t); struct tar { FILE *in; - unsigned count; + unsigned int count; struct tar_entry { char *filename; - unsigned size; + unsigned int size; size_t offset; } *entries; }; @@ -60,9 +60,9 @@ uint64_t tar__octal( const char *src, const char *eof ) { return sum; } -typedef int (*tar_callback)(const char *filename, unsigned inlen, size_t offset, void *userdata); +typedef int (*tar_callback)(const char *filename, unsigned int inlen, size_t offset, void *userdata); -int tar__push_entry(const char *filename, unsigned inlen, size_t offset, void *userdata) { +int tar__push_entry(const char *filename, unsigned int inlen, size_t offset, void *userdata) { tar *t = (tar *)userdata; unsigned index = t->count; @@ -141,14 +141,17 @@ tar *tar_open(const char *filename, const char *mode) { return t; } -int tar_find(tar *t, const char *entryname) { - if( t->in ) for( int i = t->count; --i >= 0; ) { // in case of several copies, grab most recent file (last coincidence) - if( 0 == strcmp(entryname, t->entries[i].filename)) return i; +unsigned int tar_find(tar *t, const char *entryname) { + if( t->in ) { + int i; + for( i = t->count; --i >= 0; ) { // in case of several copies, grab most recent file (last coincidence) + if( 0 == strcmp(entryname, t->entries[i].filename)) return i; + } } return -1; } -unsigned tar_count(tar *t) { +unsigned int tar_count(tar *t) { return t ? t->count : 0; } @@ -156,7 +159,7 @@ char* tar_name(tar *t, unsigned index) { return t && index < t->count ? t->entries[index].filename : 0; } -unsigned tar_size(tar *t, unsigned index) { +unsigned int tar_size(tar *t, unsigned int index) { return t && index < t->count ? t->entries[index].size : 0; } @@ -177,8 +180,11 @@ void *tar_extract(tar *t, unsigned index) { void tar_close(tar *t) { fclose(t->in); - for( int i = 0; i < t->count; ++i) { - REALLOC(t->entries[i].filename, 0); + { + unsigned i; + for( i = 0; i < t->count; ++i ) { + REALLOC(t->entries[i].filename, 0); + } } tar zero = {0}; *t = zero; @@ -190,7 +196,8 @@ int main( int argc, char **argv ) { if(argc <= 1) exit(printf("%s file.tar [file_to_view]\n", argv[0])); tar *t = tar_open(argv[1], "rb"); if( t ) { - for( int i = 0; i < tar_count(t); ++i ) { + unsigned i; + for( i = 0; i < tar_count(t); ++i ) { printf("%d) %s (%u bytes)\n", i+1, tar_name(t,i), tar_size(t,i)); char *data = tar_extract(t,i); if(argc>2) if(0==strcmp(argv[2],tar_name(t,i))) printf("%.*s\n", tar_size(t,i), data); diff --git a/src/vfs.c b/src/vfs.c index f4691a5..511ac65 100644 --- a/src/vfs.c +++ b/src/vfs.c @@ -4,7 +4,7 @@ // - note: vfs_mount() order matters (the most recent the higher priority). void vfs_mount(const char *path); // zipfile or directory/with/trailing/slash/ -char* vfs_load(const char *filename, int *size); // must free() after use +char* vfs_load(const char *filename, unsigned int *size); // must free() after use // ----------------------------------------------------------------------------- @@ -55,15 +55,16 @@ void vfs_mount(const char *path) { dir_head->is_directory = is_directory; } -char *vfs_load(const char *filename, int *size) { // must free() after use +char *vfs_load(const char *filename, unsigned int *size) { // must free() after use char *data = NULL; - for(vfs_dir *dir = dir_head; dir && !data; dir = dir->next) { + vfs_dir *dir; + for( dir = dir_head; dir && !data; dir = dir->next ) { if( dir->is_directory ) { char buf[512]; snprintf(buf, sizeof(buf), "%s%s", dir->path, filename); data = vfs_read_file(buf, size); } else { - int index = zip_find(dir->archive, filename); + unsigned int index = zip_find(dir->archive, filename); data = zip_extract(dir->archive, index); if( size ) *size = zip_size(dir->archive, index); } diff --git a/src/zip.c b/src/zip.c index b15830c..cb671e5 100644 --- a/src/zip.c +++ b/src/zip.c @@ -11,6 +11,7 @@ #ifndef ZIP_H #define ZIP_H + #include #include @@ -22,19 +23,19 @@ zip* zip_open(const char *file, const char *mode /*r,w,a*/); bool zip_append_file(zip*, const char *entryname, FILE *in, unsigned compr_level); // only for (r)ead mode - int zip_find(zip*, const char *entryname); // convert entry to index. returns <0 if not found. - unsigned zip_count(zip*); - char* zip_name(zip*, unsigned index); - char* zip_modt(zip*, unsigned index); - unsigned zip_size(zip*, unsigned index); - unsigned zip_hash(zip*, unsigned index); - bool zip_file(zip*, unsigned index); // is_file? (dir if name ends with '/'; file otherwise) - bool zip_test(zip*, unsigned index); - unsigned zip_codec(zip*, unsigned index); - unsigned zip_offset(zip*, unsigned index); - void* zip_extract(zip*, unsigned index); // must free() after use - bool zip_extract_file(zip*, unsigned index, FILE *out); - unsigned zip_extract_data(zip*, unsigned index, void *out, unsigned outlen); + unsigned int zip_find(zip*, const char *entryname); // convert entry to index. returns <0 if not found. + unsigned int zip_count(zip*); + char* zip_name(zip*, unsigned int index); + char* zip_modt(zip*, unsigned int index); + unsigned int zip_size(zip*, unsigned int index); + unsigned int zip_hash(zip*, unsigned int index); + bool zip_file(zip*, unsigned int index); // is_file? (dir if name ends with '/'; file otherwise) + bool zip_test(zip*, unsigned int index); + unsigned int zip_codec(zip*, unsigned int index); + unsigned int zip_offset(zip*, unsigned int index); + void* zip_extract(zip*, unsigned int index); // must free() after use + bool zip_extract_file(zip*, unsigned int index, FILE *out); + unsigned int zip_extract_data(zip*, unsigned int index, void *out, unsigned int outlen); void zip_close(zip*); @@ -49,6 +50,7 @@ void zip_close(zip*); #include #include #include +#include #ifndef REALLOC #define REALLOC realloc @@ -186,7 +188,7 @@ int jzReadEndRecord(FILE *fp, JZEndRecord *endRecord) { } // Naively assume signature can only be found in one place... - for(i = readBytes - sizeof(JZEndRecord); i >= 0; i--) { + for( i = readBytes - sizeof(JZEndRecord); i >= 0; i-- ) { er = (JZEndRecord *)(jzBuffer + i); if(er->signature == 0x06054B50) break; @@ -225,84 +227,87 @@ int jzReadCentralDirectory(FILE *fp, JZEndRecord *endRecord, JZRecordCallback ca return ERR(JZ_ERRNO, "Cannot seek in zip file!"); } - for(int i=0; inumEntries; i++) { - PRINTF("%d)\n@-> %lu %#lx\n", i+1, (unsigned long)ftell(fp), (unsigned long)ftell(fp)); - long offset = ftell(fp); // store current position + { + uint16_t i; + for( i=0; inumEntries; i++ ) { + PRINTF("%d)\n@-> %lu %#lx\n", i+1, (unsigned long)ftell(fp), (unsigned long)ftell(fp)); + long offset = ftell(fp); // store current position - if(fread(&fileHeader, 1, sizeof(JZGlobalFileHeader), fp) < sizeof(JZGlobalFileHeader)) { - return ERR(JZ_ERRNO, "Couldn't read file header #%d!", i); - } + if(fread(&fileHeader, 1, sizeof(JZGlobalFileHeader), fp) < sizeof(JZGlobalFileHeader)) { + return ERR(JZ_ERRNO, "Couldn't read file header #%d!", i); + } - JZGlobalFileHeader *g = &fileHeader, copy = *g; - PRINTF("\tsignature: %u %#x\n", g->signature, g->signature); // 0x02014B50 - PRINTF("\tversionMadeBy: %u %#x\n", g->versionMadeBy, g->versionMadeBy); // unsupported - PRINTF("\tversionNeededToExtract: %u %#x\n", g->versionNeededToExtract, g->versionNeededToExtract); // unsupported - PRINTF("\tgeneralPurposeBitFlag: %u %#x\n", g->generalPurposeBitFlag, g->generalPurposeBitFlag); // unsupported - PRINTF("\tcompressionMethod: %u %#x\n", g->compressionMethod, g->compressionMethod); // 0-store,8-deflate - PRINTF("\tlastModFileTime: %u %#x\n", g->lastModFileTime, g->lastModFileTime); - PRINTF("\tlastModFileDate: %u %#x\n", g->lastModFileDate, g->lastModFileDate); - PRINTF("\tcrc32: %#x\n", g->crc32); - PRINTF("\tcompressedSize: %u\n", g->compressedSize); - PRINTF("\tuncompressedSize: %u\n", g->uncompressedSize); - PRINTF("\tfileNameLength: %u\n", g->fileNameLength); - PRINTF("\textraFieldLength: %u\n", g->extraFieldLength); // unsupported - PRINTF("\tfileCommentLength: %u\n", g->fileCommentLength); // unsupported - PRINTF("\tdiskNumberStart: %u\n", g->diskNumberStart); // unsupported - PRINTF("\tinternalFileAttributes: %#x\n", g->internalFileAttributes); // unsupported - PRINTF("\texternalFileAttributes: %#x\n", g->externalFileAttributes); // unsupported - PRINTF("\trelativeOffsetOflocalHeader: %u %#x\n", g->relativeOffsetOflocalHeader, g->relativeOffsetOflocalHeader); - - if(fileHeader.signature != 0x02014B50) { - return ERR(JZ_ERRNO, "Invalid file header signature %#x #%d!", fileHeader.signature, i); - } + JZGlobalFileHeader *g = &fileHeader, copy = *g; + PRINTF("\tsignature: %u %#x\n", g->signature, g->signature); // 0x02014B50 + PRINTF("\tversionMadeBy: %u %#x\n", g->versionMadeBy, g->versionMadeBy); // unsupported + PRINTF("\tversionNeededToExtract: %u %#x\n", g->versionNeededToExtract, g->versionNeededToExtract); // unsupported + PRINTF("\tgeneralPurposeBitFlag: %u %#x\n", g->generalPurposeBitFlag, g->generalPurposeBitFlag); // unsupported + PRINTF("\tcompressionMethod: %u %#x\n", g->compressionMethod, g->compressionMethod); // 0-store,8-deflate + PRINTF("\tlastModFileTime: %u %#x\n", g->lastModFileTime, g->lastModFileTime); + PRINTF("\tlastModFileDate: %u %#x\n", g->lastModFileDate, g->lastModFileDate); + PRINTF("\tcrc32: %#x\n", g->crc32); + PRINTF("\tcompressedSize: %u\n", g->compressedSize); + PRINTF("\tuncompressedSize: %u\n", g->uncompressedSize); + PRINTF("\tfileNameLength: %u\n", g->fileNameLength); + PRINTF("\textraFieldLength: %u\n", g->extraFieldLength); // unsupported + PRINTF("\tfileCommentLength: %u\n", g->fileCommentLength); // unsupported + PRINTF("\tdiskNumberStart: %u\n", g->diskNumberStart); // unsupported + PRINTF("\tinternalFileAttributes: %#x\n", g->internalFileAttributes); // unsupported + PRINTF("\texternalFileAttributes: %#x\n", g->externalFileAttributes); // unsupported + PRINTF("\trelativeOffsetOflocalHeader: %u %#x\n", g->relativeOffsetOflocalHeader, g->relativeOffsetOflocalHeader); + + if(fileHeader.signature != 0x02014B50) { + return ERR(JZ_ERRNO, "Invalid file header signature %#x #%d!", fileHeader.signature, i); + } - if(fileHeader.fileNameLength + 1 >= JZ_BUFFER_SIZE) { - return ERR(JZ_ERRNO, "Too long file name %u #%d!", fileHeader.fileNameLength, i); - } + if(fileHeader.fileNameLength + 1 >= JZ_BUFFER_SIZE) { + return ERR(JZ_ERRNO, "Too long file name %u #%d!", fileHeader.fileNameLength, i); + } - // filename - char jzFilename[JZ_BUFFER_SIZE/3]; - if(fread(jzFilename, 1, fileHeader.fileNameLength, fp) < fileHeader.fileNameLength) { - return ERR(JZ_ERRNO, "Couldn't read filename #%d!", i); - } - jzFilename[fileHeader.fileNameLength] = '\0'; // NULL terminate + // filename + char jzFilename[JZ_BUFFER_SIZE/3]; + if(fread(jzFilename, 1, fileHeader.fileNameLength, fp) < fileHeader.fileNameLength) { + return ERR(JZ_ERRNO, "Couldn't read filename #%d!", i); + } + jzFilename[fileHeader.fileNameLength] = '\0'; // NULL terminate - // extra block - unsigned char jzExtra[JZ_BUFFER_SIZE/3]; - if(fread(jzExtra, 1, fileHeader.extraFieldLength, fp) < fileHeader.extraFieldLength) { - return ERR(JZ_ERRNO, "Couldn't read extra block #%d!", i); - } + // extra block + unsigned char jzExtra[JZ_BUFFER_SIZE/3]; + if(fread(jzExtra, 1, fileHeader.extraFieldLength, fp) < fileHeader.extraFieldLength) { + return ERR(JZ_ERRNO, "Couldn't read extra block #%d!", i); + } - // comment block - char jzComment[JZ_BUFFER_SIZE/3]; - if(fread(jzComment, 1, fileHeader.fileCommentLength, fp) < fileHeader.fileCommentLength) { - return ERR(JZ_ERRNO, "Couldn't read comment block #%d!", i); - } - jzComment[fileHeader.fileCommentLength] = '\0'; // NULL terminate + // comment block + char jzComment[JZ_BUFFER_SIZE/3]; + if(fread(jzComment, 1, fileHeader.fileCommentLength, fp) < fileHeader.fileCommentLength) { + return ERR(JZ_ERRNO, "Couldn't read comment block #%d!", i); + } + jzComment[fileHeader.fileCommentLength] = '\0'; // NULL terminate - // seek to local file header, then skip file header + filename + extra field length - if(fseek(fp, fileHeader.relativeOffsetOflocalHeader + sizeof_JZLocalFileHeader - 2 - 2, SEEK_SET)) { - return ERR(JZ_ERRNO, "Cannot seek in file!"); - } + // seek to local file header, then skip file header + filename + extra field length + if(fseek(fp, fileHeader.relativeOffsetOflocalHeader + sizeof_JZLocalFileHeader - 2 - 2, SEEK_SET)) { + return ERR(JZ_ERRNO, "Cannot seek in file!"); + } - if(fread(&fileHeader.fileNameLength, 1, 2, fp) < 2) { - return ERR(JZ_ERRNO, "Couldn't read local filename #%d!", i); - } - if(fread(&fileHeader.extraFieldLength, 1, 2, fp) < 2) { - return ERR(JZ_ERRNO, "Couldn't read local extrafield #%d!", i); - } - if(fseek(fp, fileHeader.relativeOffsetOflocalHeader + sizeof_JZLocalFileHeader + fileHeader.fileNameLength + fileHeader.extraFieldLength, SEEK_SET)) { - return ERR(JZ_ERRNO, "Cannot seek in file!"); - } + if(fread(&fileHeader.fileNameLength, 1, 2, fp) < 2) { + return ERR(JZ_ERRNO, "Couldn't read local filename #%d!", i); + } + if(fread(&fileHeader.extraFieldLength, 1, 2, fp) < 2) { + return ERR(JZ_ERRNO, "Couldn't read local extrafield #%d!", i); + } + if(fseek(fp, fileHeader.relativeOffsetOflocalHeader + sizeof_JZLocalFileHeader + fileHeader.fileNameLength + fileHeader.extraFieldLength, SEEK_SET)) { + return ERR(JZ_ERRNO, "Cannot seek in file!"); + } - PRINTF("@-> %lu %#lx\n---\n", (unsigned long)ftell(fp), (unsigned long)ftell(fp)); + PRINTF("@-> %lu %#lx\n---\n", (unsigned long)ftell(fp), (unsigned long)ftell(fp)); - if( JZ_OK != callback(fp, i, &fileHeader, jzFilename, jzExtra, jzComment, user_data) ) - break; // keep going while callback returns ok + if( JZ_OK != callback(fp, i, &fileHeader, jzFilename, jzExtra, jzComment, user_data) ) + break; // keep going while callback returns ok - fseek(fp, offset, SEEK_SET); // return to position - fseek(fp, sizeof(JZGlobalFileHeader) + copy.fileNameLength, SEEK_CUR); // skip entry - fseek(fp, copy.extraFieldLength + copy.fileCommentLength, SEEK_CUR); // skip entry + fseek(fp, offset, SEEK_SET); // return to position + fseek(fp, sizeof(JZGlobalFileHeader) + copy.fileNameLength, SEEK_CUR); // skip entry + fseek(fp, copy.extraFieldLength + copy.fileCommentLength, SEEK_CUR); // skip entry + } } return JZ_OK; @@ -359,13 +364,20 @@ struct zip { uint32_t zip__crc32(uint32_t crc, const void *data, size_t n_bytes) { // CRC32 routine is from Björn Samuelsson's public domain implementation at http://home.thep.lu.se/~bjorn/crc/ static uint32_t table[256] = {0}; - if(!*table) for(uint32_t i = 0; i < 0x100; ++i) { - uint32_t r = i; - for(int j = 0; j < 8; ++j) r = (r & 1 ? 0 : (uint32_t)0xEDB88320L) ^ r >> 1; - table[i] = r ^ (uint32_t)0xFF000000L; + if(!*table) { + uint32_t i; + for( i = 0; i < 0x100; ++i ) { + uint32_t r = i; + unsigned short j; + for( j = 0; j < 8; ++j ) + r = (r & 1 ? 0 : (uint32_t)0xEDB88320L) ^ r >> 1; + table[i] = r ^ (uint32_t)0xFF000000L; + } } - for(size_t i = 0; i < n_bytes; ++i) { - crc = table[(uint8_t)crc ^ ((uint8_t*)data)[i]] ^ crc >> 8; + { + size_t i; + for( i = 0; i < n_bytes; ++i ) + crc = table[(uint8_t)crc ^ ((uint8_t*)data)[i]] ^ crc >> 8; } return crc; } @@ -392,9 +404,12 @@ int zip__callback(FILE *fp, int idx, JZGlobalFileHeader *header, char *filename, // zip read -int zip_find(zip *z, const char *entryname) { - if( z->in ) for( int i = z->count; --i >= 0; ) { // in case of several copies, grab most recent file (last coincidence) - if( 0 == strcmp(entryname, z->entries[i].filename)) return i; +unsigned int zip_find(zip *z, const char *entryname) { + if( z->in ) { + int i; + for( i = z->count; --i >= 0; ) { // in case of several copies, grab most recent file (last coincidence) + if( 0 == strcmp(entryname, z->entries[i].filename)) return i; + } } return -1; } @@ -635,13 +650,16 @@ void zip_close(zip* z) { end.numEntries = z->count; end.centralDirectoryOffset = ftell(z->out); // flush global directory: global file+filename each - for(unsigned i = 0; i < z->count; i++) { - struct zip_entry *h = &z->entries[i]; - JZGlobalFileHeader *g = &h->header; - fwrite(g, 1, sizeof(JZGlobalFileHeader), z->out); - fwrite(h->filename, 1, g->fileNameLength, z->out); - fwrite(h->extra, 1, g->extraFieldLength, z->out); - fwrite(h->comment, 1, g->fileCommentLength, z->out); + { + unsigned i; + for( i = 0; i < z->count; i++ ) { + struct zip_entry *h = &z->entries[i]; + JZGlobalFileHeader *g = &h->header; + fwrite(g, 1, sizeof(JZGlobalFileHeader), z->out); + fwrite(h->filename, 1, g->fileNameLength, z->out); + fwrite(h->extra, 1, g->extraFieldLength, z->out); + fwrite(h->comment, 1, g->fileCommentLength, z->out); + } } end.centralDirectorySize = ftell(z->out) - end.centralDirectoryOffset; end.zipCommentLength = 0; @@ -652,10 +670,13 @@ void zip_close(zip* z) { if( z->out ) fclose(z->out); if( z->in ) fclose(z->in); // clean up - for(unsigned i = 0; i < z->count; ++i ) { - REALLOC(z->entries[i].filename, 0); - if(z->entries[i].extra) REALLOC(z->entries[i].extra, 0); - if(z->entries[i].comment) REALLOC(z->entries[i].comment, 0); + { + unsigned i; + for( i = 0; i < z->count; ++i ) { + REALLOC(z->entries[i].filename, 0); + if(z->entries[i].extra) REALLOC(z->entries[i].extra, 0); + if(z->entries[i].comment) REALLOC(z->entries[i].comment, 0); + } } if(z->entries) REALLOC(z->entries, 0); zip zero = {0}; *z = zero; REALLOC(z, 0); diff --git a/stdarc.c b/stdarc.c index e620110..3ae9a3f 100644 --- a/stdarc.c +++ b/stdarc.c @@ -37,22 +37,22 @@ typedef struct zip zip; zip* zip_open(const char *file, const char *mode /*r,w,a*/); // only for (w)rite or (a)ppend mode - bool zip_append_file(zip*, const char *entryname, FILE *in, unsigned compr_level); + bool zip_append_file(zip*, const char *entryname, FILE *in, unsigned int compr_level); // only for (r)ead mode - int zip_find(zip*, const char *entryname); // convert entry to index. returns <0 if not found. - unsigned zip_count(zip*); - char* zip_name(zip*, unsigned index); - char* zip_modt(zip*, unsigned index); - unsigned zip_size(zip*, unsigned index); - unsigned zip_hash(zip*, unsigned index); - bool zip_file(zip*, unsigned index); // is_file? (dir if name ends with '/'; file otherwise) - bool zip_test(zip*, unsigned index); - unsigned zip_codec(zip*, unsigned index); - unsigned zip_offset(zip*, unsigned index); - void* zip_extract(zip*, unsigned index); // must free() after use - bool zip_extract_file(zip*, unsigned index, FILE *out); - unsigned zip_extract_data(zip*, unsigned index, void *out, unsigned outlen); + unsigned int zip_find(zip*, const char *entryname); // convert entry to index. returns <0 if not found. + unsigned int zip_count(zip*); + char* zip_name(zip*, unsigned int index); + char* zip_modt(zip*, unsigned int index); + unsigned int zip_size(zip*, unsigned int index); + unsigned int zip_hash(zip*, unsigned int index); + bool zip_file(zip*, unsigned int index); // is_file? (dir if name ends with '/'; file otherwise) + bool zip_test(zip*, unsigned int index); + unsigned int zip_codec(zip*, unsigned int index); + unsigned int zip_offset(zip*, unsigned int index); + void* zip_extract(zip*, unsigned int index); // must free() after use + bool zip_extract_file(zip*, unsigned int index, FILE *out); + unsigned int zip_extract_data(zip*, unsigned int index, void *out, unsigned int outlen); void zip_close(zip*); @@ -202,7 +202,7 @@ int jzReadEndRecord(FILE *fp, JZEndRecord *endRecord) { } // Naively assume signature can only be found in one place... - for(i = readBytes - sizeof(JZEndRecord); i >= 0; i--) { + for( i = readBytes - sizeof(JZEndRecord); i >= 0; i-- ) { er = (JZEndRecord *)(jzBuffer + i); if(er->signature == 0x06054B50) break; @@ -241,84 +241,92 @@ int jzReadCentralDirectory(FILE *fp, JZEndRecord *endRecord, JZRecordCallback ca return ERR(JZ_ERRNO, "Cannot seek in zip file!"); } - for(int i=0; inumEntries; i++) { - PRINTF("%d)\n@-> %lu %#lx\n", i+1, (unsigned long)ftell(fp), (unsigned long)ftell(fp)); - long offset = ftell(fp); // store current position + { + uint16_t i; + for (i = 0; i < endRecord->numEntries; i++) { + PRINTF("%d)\n@-> %lu %#lx\n", i + 1, (unsigned long) ftell(fp), (unsigned long) ftell(fp)); + long offset = ftell(fp); // store current position - if(fread(&fileHeader, 1, sizeof(JZGlobalFileHeader), fp) < sizeof(JZGlobalFileHeader)) { - return ERR(JZ_ERRNO, "Couldn't read file header #%d!", i); - } + if (fread(&fileHeader, 1, sizeof(JZGlobalFileHeader), fp) < sizeof(JZGlobalFileHeader)) { + return ERR(JZ_ERRNO, "Couldn't read file header #%d!", i); + } - JZGlobalFileHeader *g = &fileHeader, copy = *g; - PRINTF("\tsignature: %u %#x\n", g->signature, g->signature); // 0x02014B50 - PRINTF("\tversionMadeBy: %u %#x\n", g->versionMadeBy, g->versionMadeBy); // unsupported - PRINTF("\tversionNeededToExtract: %u %#x\n", g->versionNeededToExtract, g->versionNeededToExtract); // unsupported - PRINTF("\tgeneralPurposeBitFlag: %u %#x\n", g->generalPurposeBitFlag, g->generalPurposeBitFlag); // unsupported - PRINTF("\tcompressionMethod: %u %#x\n", g->compressionMethod, g->compressionMethod); // 0-store,8-deflate - PRINTF("\tlastModFileTime: %u %#x\n", g->lastModFileTime, g->lastModFileTime); - PRINTF("\tlastModFileDate: %u %#x\n", g->lastModFileDate, g->lastModFileDate); - PRINTF("\tcrc32: %#x\n", g->crc32); - PRINTF("\tcompressedSize: %u\n", g->compressedSize); - PRINTF("\tuncompressedSize: %u\n", g->uncompressedSize); - PRINTF("\tfileNameLength: %u\n", g->fileNameLength); - PRINTF("\textraFieldLength: %u\n", g->extraFieldLength); // unsupported - PRINTF("\tfileCommentLength: %u\n", g->fileCommentLength); // unsupported - PRINTF("\tdiskNumberStart: %u\n", g->diskNumberStart); // unsupported - PRINTF("\tinternalFileAttributes: %#x\n", g->internalFileAttributes); // unsupported - PRINTF("\texternalFileAttributes: %#x\n", g->externalFileAttributes); // unsupported - PRINTF("\trelativeOffsetOflocalHeader: %u %#x\n", g->relativeOffsetOflocalHeader, g->relativeOffsetOflocalHeader); - - if(fileHeader.signature != 0x02014B50) { - return ERR(JZ_ERRNO, "Invalid file header signature %#x #%d!", fileHeader.signature, i); - } + JZGlobalFileHeader *g = &fileHeader, copy = *g; + PRINTF("\tsignature: %u %#x\n", g->signature, g->signature); // 0x02014B50 + PRINTF("\tversionMadeBy: %u %#x\n", g->versionMadeBy, g->versionMadeBy); // unsupported + PRINTF("\tversionNeededToExtract: %u %#x\n", g->versionNeededToExtract, + g->versionNeededToExtract); // unsupported + PRINTF("\tgeneralPurposeBitFlag: %u %#x\n", g->generalPurposeBitFlag, + g->generalPurposeBitFlag); // unsupported + PRINTF("\tcompressionMethod: %u %#x\n", g->compressionMethod, g->compressionMethod); // 0-store,8-deflate + PRINTF("\tlastModFileTime: %u %#x\n", g->lastModFileTime, g->lastModFileTime); + PRINTF("\tlastModFileDate: %u %#x\n", g->lastModFileDate, g->lastModFileDate); + PRINTF("\tcrc32: %#x\n", g->crc32); + PRINTF("\tcompressedSize: %u\n", g->compressedSize); + PRINTF("\tuncompressedSize: %u\n", g->uncompressedSize); + PRINTF("\tfileNameLength: %u\n", g->fileNameLength); + PRINTF("\textraFieldLength: %u\n", g->extraFieldLength); // unsupported + PRINTF("\tfileCommentLength: %u\n", g->fileCommentLength); // unsupported + PRINTF("\tdiskNumberStart: %u\n", g->diskNumberStart); // unsupported + PRINTF("\tinternalFileAttributes: %#x\n", g->internalFileAttributes); // unsupported + PRINTF("\texternalFileAttributes: %#x\n", g->externalFileAttributes); // unsupported + PRINTF("\trelativeOffsetOflocalHeader: %u %#x\n", g->relativeOffsetOflocalHeader, + g->relativeOffsetOflocalHeader); + + if (fileHeader.signature != 0x02014B50) { + return ERR(JZ_ERRNO, "Invalid file header signature %#x #%d!", fileHeader.signature, i); + } - if(fileHeader.fileNameLength + 1 >= JZ_BUFFER_SIZE) { - return ERR(JZ_ERRNO, "Too long file name %u #%d!", fileHeader.fileNameLength, i); - } + if (fileHeader.fileNameLength + 1 >= JZ_BUFFER_SIZE) { + return ERR(JZ_ERRNO, "Too long file name %u #%d!", fileHeader.fileNameLength, i); + } - // filename - char jzFilename[JZ_BUFFER_SIZE/3]; - if(fread(jzFilename, 1, fileHeader.fileNameLength, fp) < fileHeader.fileNameLength) { - return ERR(JZ_ERRNO, "Couldn't read filename #%d!", i); - } - jzFilename[fileHeader.fileNameLength] = '\0'; // NULL terminate + // filename + char jzFilename[JZ_BUFFER_SIZE / 3]; + if (fread(jzFilename, 1, fileHeader.fileNameLength, fp) < fileHeader.fileNameLength) { + return ERR(JZ_ERRNO, "Couldn't read filename #%d!", i); + } + jzFilename[fileHeader.fileNameLength] = '\0'; // NULL terminate - // extra block - unsigned char jzExtra[JZ_BUFFER_SIZE/3]; - if(fread(jzExtra, 1, fileHeader.extraFieldLength, fp) < fileHeader.extraFieldLength) { - return ERR(JZ_ERRNO, "Couldn't read extra block #%d!", i); - } + // extra block + unsigned char jzExtra[JZ_BUFFER_SIZE / 3]; + if (fread(jzExtra, 1, fileHeader.extraFieldLength, fp) < fileHeader.extraFieldLength) { + return ERR(JZ_ERRNO, "Couldn't read extra block #%d!", i); + } - // comment block - char jzComment[JZ_BUFFER_SIZE/3]; - if(fread(jzComment, 1, fileHeader.fileCommentLength, fp) < fileHeader.fileCommentLength) { - return ERR(JZ_ERRNO, "Couldn't read comment block #%d!", i); - } - jzComment[fileHeader.fileCommentLength] = '\0'; // NULL terminate + // comment block + char jzComment[JZ_BUFFER_SIZE / 3]; + if (fread(jzComment, 1, fileHeader.fileCommentLength, fp) < fileHeader.fileCommentLength) { + return ERR(JZ_ERRNO, "Couldn't read comment block #%d!", i); + } + jzComment[fileHeader.fileCommentLength] = '\0'; // NULL terminate - // seek to local file header, then skip file header + filename + extra field length - if(fseek(fp, fileHeader.relativeOffsetOflocalHeader + sizeof_JZLocalFileHeader - 2 - 2, SEEK_SET)) { - return ERR(JZ_ERRNO, "Cannot seek in file!"); - } + // seek to local file header, then skip file header + filename + extra field length + if (fseek(fp, fileHeader.relativeOffsetOflocalHeader + sizeof_JZLocalFileHeader - 2 - 2, SEEK_SET)) { + return ERR(JZ_ERRNO, "Cannot seek in file!"); + } - if(fread(&fileHeader.fileNameLength, 1, 2, fp) < 2) { - return ERR(JZ_ERRNO, "Couldn't read local filename #%d!", i); - } - if(fread(&fileHeader.extraFieldLength, 1, 2, fp) < 2) { - return ERR(JZ_ERRNO, "Couldn't read local extrafield #%d!", i); - } - if(fseek(fp, fileHeader.relativeOffsetOflocalHeader + sizeof_JZLocalFileHeader + fileHeader.fileNameLength + fileHeader.extraFieldLength, SEEK_SET)) { - return ERR(JZ_ERRNO, "Cannot seek in file!"); - } + if (fread(&fileHeader.fileNameLength, 1, 2, fp) < 2) { + return ERR(JZ_ERRNO, "Couldn't read local filename #%d!", i); + } + if (fread(&fileHeader.extraFieldLength, 1, 2, fp) < 2) { + return ERR(JZ_ERRNO, "Couldn't read local extrafield #%d!", i); + } + if (fseek(fp, + fileHeader.relativeOffsetOflocalHeader + sizeof_JZLocalFileHeader + fileHeader.fileNameLength + + fileHeader.extraFieldLength, SEEK_SET)) { + return ERR(JZ_ERRNO, "Cannot seek in file!"); + } - PRINTF("@-> %lu %#lx\n---\n", (unsigned long)ftell(fp), (unsigned long)ftell(fp)); + PRINTF("@-> %lu %#lx\n---\n", (unsigned long) ftell(fp), (unsigned long) ftell(fp)); - if( JZ_OK != callback(fp, i, &fileHeader, jzFilename, jzExtra, jzComment, user_data) ) - break; // keep going while callback returns ok + if (JZ_OK != callback(fp, i, &fileHeader, jzFilename, jzExtra, jzComment, user_data)) + break; // keep going while callback returns ok - fseek(fp, offset, SEEK_SET); // return to position - fseek(fp, sizeof(JZGlobalFileHeader) + copy.fileNameLength, SEEK_CUR); // skip entry - fseek(fp, copy.extraFieldLength + copy.fileCommentLength, SEEK_CUR); // skip entry + fseek(fp, offset, SEEK_SET); // return to position + fseek(fp, sizeof(JZGlobalFileHeader) + copy.fileNameLength, SEEK_CUR); // skip entry + fseek(fp, copy.extraFieldLength + copy.fileCommentLength, SEEK_CUR); // skip entry + } } return JZ_OK; @@ -375,13 +383,20 @@ struct zip { uint32_t zip__crc32(uint32_t crc, const void *data, size_t n_bytes) { // CRC32 routine is from Björn Samuelsson's public domain implementation at http://home.thep.lu.se/~bjorn/crc/ static uint32_t table[256] = {0}; - if(!*table) for(uint32_t i = 0; i < 0x100; ++i) { - uint32_t r = i; - for(int j = 0; j < 8; ++j) r = (r & 1 ? 0 : (uint32_t)0xEDB88320L) ^ r >> 1; - table[i] = r ^ (uint32_t)0xFF000000L; + if(!*table) { + uint32_t i; + for (i = 0; i < 0x100; ++i) { + uint32_t r = i; + unsigned short j; + for (j = 0; j < 8; ++j) r = (r & 1 ? 0 : (uint32_t) 0xEDB88320L) ^ r >> 1; + table[i] = r ^ (uint32_t) 0xFF000000L; + } } - for(size_t i = 0; i < n_bytes; ++i) { - crc = table[(uint8_t)crc ^ ((uint8_t*)data)[i]] ^ crc >> 8; + { + size_t i; + for (i = 0; i < n_bytes; ++i) { + crc = table[(uint8_t) crc ^ ((uint8_t *) data)[i]] ^ crc >> 8; + } } return crc; } @@ -408,9 +423,12 @@ int zip__callback(FILE *fp, int idx, JZGlobalFileHeader *header, char *filename, // zip read -int zip_find(zip *z, const char *entryname) { - if( z->in ) for( int i = z->count; --i >= 0; ) { // in case of several copies, grab most recent file (last coincidence) - if( 0 == strcmp(entryname, z->entries[i].filename)) return i; +unsigned int zip_find(zip *z, const char *entryname) { + if( z->in ) { + unsigned i; + for ( i = z->count; --i >= 0; ) { // in case of several copies, grab most recent file (last coincidence) + if (0 == strcmp(entryname, z->entries[i].filename)) return i; + } } return -1; } @@ -651,13 +669,16 @@ void zip_close(zip* z) { end.numEntries = z->count; end.centralDirectoryOffset = ftell(z->out); // flush global directory: global file+filename each - for(unsigned i = 0; i < z->count; i++) { - struct zip_entry *h = &z->entries[i]; - JZGlobalFileHeader *g = &h->header; - fwrite(g, 1, sizeof(JZGlobalFileHeader), z->out); - fwrite(h->filename, 1, g->fileNameLength, z->out); - fwrite(h->extra, 1, g->extraFieldLength, z->out); - fwrite(h->comment, 1, g->fileCommentLength, z->out); + { + unsigned i; + for (i = 0; i < z->count; i++) { + struct zip_entry *h = &z->entries[i]; + JZGlobalFileHeader *g = &h->header; + fwrite(g, 1, sizeof(JZGlobalFileHeader), z->out); + fwrite(h->filename, 1, g->fileNameLength, z->out); + fwrite(h->extra, 1, g->extraFieldLength, z->out); + fwrite(h->comment, 1, g->fileCommentLength, z->out); + } } end.centralDirectorySize = ftell(z->out) - end.centralDirectoryOffset; end.zipCommentLength = 0; @@ -668,10 +689,13 @@ void zip_close(zip* z) { if( z->out ) fclose(z->out); if( z->in ) fclose(z->in); // clean up - for(unsigned i = 0; i < z->count; ++i ) { - REALLOC(z->entries[i].filename, 0); - if(z->entries[i].extra) REALLOC(z->entries[i].extra, 0); - if(z->entries[i].comment) REALLOC(z->entries[i].comment, 0); + { + unsigned i; + for (i = 0; i < z->count; ++i) { + REALLOC(z->entries[i].filename, 0); + if (z->entries[i].extra) REALLOC(z->entries[i].extra, 0); + if (z->entries[i].comment) REALLOC(z->entries[i].comment, 0); + } } if(z->entries) REALLOC(z->entries, 0); zip zero = {0}; *z = zero; REALLOC(z, 0); @@ -690,7 +714,7 @@ typedef struct tar tar; tar *tar_open(const char *filename, const char *mode); - int tar_find(tar*, const char *entryname); // returns entry number; or <0 if not found. + unsigned int tar_find(tar*, const char *entryname); // returns entry number; or <0 if not found. unsigned tar_count(tar*); char* tar_name(tar*, unsigned index); unsigned tar_size(tar*, unsigned index); @@ -823,14 +847,17 @@ tar *tar_open(const char *filename, const char *mode) { return t; } -int tar_find(tar *t, const char *entryname) { - if( t->in ) for( int i = t->count; --i >= 0; ) { // in case of several copies, grab most recent file (last coincidence) - if( 0 == strcmp(entryname, t->entries[i].filename)) return i; +unsigned int tar_find(tar *t, const char *entryname) { + if( t->in ) { + unsigned i; + for ( i = t->count; --i >= 0; ) { // in case of several copies, grab most recent file (last coincidence) + if (0 == strcmp(entryname, t->entries[i].filename)) return i; + } } return -1; } -unsigned tar_count(tar *t) { +unsigned int tar_count(tar *t) { return t ? t->count : 0; } @@ -859,12 +886,17 @@ void *tar_extract(tar *t, unsigned index) { void tar_close(tar *t) { fclose(t->in); - for( int i = 0; i < t->count; ++i) { - REALLOC(t->entries[i].filename, 0); + { + unsigned i; + for ( i = 0; i < t->count; ++i ) { + REALLOC(t->entries[i].filename, 0); + } + } + { + tar zero = {0}; + *t = zero; + REALLOC(t, 0); } - tar zero = {0}; - *t = zero; - REALLOC(t, 0); } #ifdef TAR_DEMO @@ -872,7 +904,8 @@ int main( int argc, char **argv ) { if(argc <= 1) exit(printf("%s file.tar [file_to_view]\n", argv[0])); tar *t = tar_open(argv[1], "rb"); if( t ) { - for( int i = 0; i < tar_count(t); ++i ) { + unsigned i; + for( i = 0; i < tar_count(t); ++i ) { printf("%d) %s (%u bytes)\n", i+1, tar_name(t,i), tar_size(t,i)); char *data = tar_extract(t,i); if(argc>2) if(0==strcmp(argv[2],tar_name(t,i))) printf("%.*s\n", tar_size(t,i), data); @@ -1023,10 +1056,13 @@ pak *pak_open(const char *fname, const char *mode) { goto fail; } - for( unsigned i = 0; i < num_files; ++i ) { - pak_file *e = &p->entries[i]; - e->offset = ltoh32(e->offset); - e->size = ltoh32(e->size); + { + unsigned i; + for ( i = 0; i < num_files; ++i ) { + pak_file *e = &p->entries[i]; + e->offset = ltoh32(e->offset); + e->size = ltoh32(e->size); + } } if( mode[0] == 'a' ) { @@ -1114,17 +1150,22 @@ void pak_close(pak *p) { if(p->out) { // write toc uint32_t seek = 0 + 12, dirpos = (uint32_t)ftell(p->out), dirlen = p->count * 64; - for(unsigned i = 0; i < p->count; ++i) { - pak_file *e = &p->entries[i]; - // write name (truncated if needed), and trailing zeros - char zero[56] = {0}; - int namelen = strlen(e->name); - fwrite( e->name, 1, namelen >= 56 ? 55 : namelen, p->out ); - fwrite( zero, 1, namelen >= 56 ? 1 : 56 - namelen, p->out ); - // write offset + length pair - uint32_t pseek = htol32(seek); fwrite( &pseek, 1,4, p->out ); - uint32_t psize = htol32(e->size); fwrite( &psize, 1,4, p->out ); - seek += e->size; + { + unsigned i; + for ( i = 0; i < p->count; ++i ) { + pak_file *e = &p->entries[i]; + // write name (truncated if needed), and trailing zeros + char zero[56] = {0}; + int namelen = strlen(e->name); + fwrite(e->name, 1, namelen >= 56 ? 55 : namelen, p->out); + fwrite(zero, 1, namelen >= 56 ? 1 : 56 - namelen, p->out); + // write offset + length pair + uint32_t pseek = htol32(seek); + fwrite(&pseek, 1, 4, p->out); + uint32_t psize = htol32(e->size); + fwrite(&psize, 1, 4, p->out); + seek += e->size; + } } // patch header @@ -1139,8 +1180,11 @@ void pak_close(pak *p) { if(p->out) fclose(p->out); // clean up - for(unsigned i = 0; i < p->count; ++i) { - pak_file *e = &p->entries[i]; + { + unsigned i; + for ( i = 0; i < p->count; ++i ) { + pak_file *e = &p->entries[i]; + } } REALLOC(p->entries, 0); @@ -1152,18 +1196,19 @@ void pak_close(pak *p) { int pak_find(pak *p, const char *filename) { if( p->in ) { - for( int i = p->count; --i >= 0; ) { + unsigned i; + for( i = p->count; --i >= 0; ) { if(!strcmp(p->entries[i].name, filename)) return i; } } return -1; } -unsigned pak_count(pak *p) { +unsigned int pak_count(pak *p) { return p->in ? p->count : 0; } -unsigned pak_offset(pak *p, unsigned index) { +unsigned int pak_offset(pak *p, unsigned index) { return p->in && index < p->count ? p->entries[index].offset : 0; } @@ -1216,7 +1261,8 @@ int main(int argc, char **argv) { printf("listing %s archive ...\n", fname); p = pak_open(fname, "rb"); if( p ) { - for( unsigned i = 0; i < pak_count(p); ++i ) { + unsigned i; + for( i = 0; i < pak_count(p); ++i ) { printf(" %d) @%08x %11u %s ", i+1, pak_offset(p,i), pak_size(p,i), pak_name(p,i)); void *data = pak_extract(p,i); printf("\r%c\n", data ? 'Y':'N'); @@ -1240,7 +1286,7 @@ int main(int argc, char **argv) { // - note: vfs_mount() order matters (the most recent the higher priority). void vfs_mount(const char *path); // zipfile or directory/with/trailing/slash/ -char* vfs_load(const char *filename, int *size); // must free() after use +char* vfs_load(const char *filename, unsigned int *size); // must free() after use // ----------------------------------------------------------------------------- @@ -1291,15 +1337,16 @@ void vfs_mount(const char *path) { dir_head->is_directory = is_directory; } -char *vfs_load(const char *filename, int *size) { // must free() after use +char *vfs_load(const char *filename, unsigned int *size) { // must free() after use char *data = NULL; - for(vfs_dir *dir = dir_head; dir && !data; dir = dir->next) { + vfs_dir *dir; + for( dir = dir_head; dir && !data; dir = dir->next ) { if( dir->is_directory ) { char buf[512]; snprintf(buf, sizeof(buf), "%s%s", dir->path, filename); data = vfs_read_file(buf, size); } else { - int index = zip_find(dir->archive, filename); + unsigned int index = zip_find(dir->archive, filename); data = zip_extract(dir->archive, index); if( size ) *size = zip_size(dir->archive, index); } @@ -1394,22 +1441,28 @@ int dir_yield(dir *d, const char *pathfile, char *name, int namelen) { #ifdef _WIN32 WIN32_FIND_DATAA fdata = { 0 }; snprintf(name, namelen, "%s/*", pathfile); - for( HANDLE h = FindFirstFileA(name, &fdata ); h != INVALID_HANDLE_VALUE; (ok = FindClose( h ), h = INVALID_HANDLE_VALUE, 1)) { - for( int next = 1; next; next = FindNextFileA(h, &fdata) != 0 ) { - if( fdata.cFileName[0] == '.' ) continue; - int is_dir = (fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) > 0; - snprintf(name, namelen, "%s/%s%s", pathfile, fdata.cFileName, is_dir ? "/" : ""); - struct stat st; if( !is_dir ) if(stat(name, &st) < 0) continue; - // add - dir_entry de = { STRDUP(name), is_dir ? 0 : st.st_size, is_dir }; - d->entry = (dir_entry*)REALLOC(d->entry, ++d->count * sizeof(dir_entry)); - d->entry[d->count-1] = de; + { + HANDLE h; + for( h = FindFirstFileA(name, &fdata ); h != INVALID_HANDLE_VALUE; (ok = FindClose( h ), h = INVALID_HANDLE_VALUE, 1) ) { + int next; + for( next = 1; next; next = FindNextFileA(h, &fdata) != 0 ) { + if( fdata.cFileName[0] == '.' ) continue; + int is_dir = (fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) > 0; + snprintf(name, namelen, "%s/%s%s", pathfile, fdata.cFileName, is_dir ? "/" : ""); + struct stat st; if( !is_dir ) if(stat(name, &st) < 0) continue; + // add + dir_entry de = { STRDUP(name), is_dir ? 0 : st.st_size, is_dir }; + d->entry = (dir_entry*)REALLOC(d->entry, ++d->count * sizeof(dir_entry)); + d->entry[d->count-1] = de; + } } } #else snprintf(name, namelen, "%s/", pathfile); - for( DIR *dir = opendir(name); dir; ok = (closedir(dir), dir = 0, 1)) { - for( struct dirent *ep; ep = readdir(dir); ) { + DIR *dir; + for( dir = opendir(name); dir; ok = (closedir(dir), dir = 0, 1) ) { + struct dirent *ep; + while( (ep = readdir(dir)) ) { if( ep->d_name[0] == '.' ) continue; snprintf(name, namelen, "%s/%s", pathfile, ep->d_name); struct stat st; if( stat(name, &st) < 0 ) continue; @@ -1430,10 +1483,16 @@ dir *dir_open(const char *pathfile, const char *mode) { dir *d = (dir*)REALLOC(0, sizeof(dir)), zero = {0}; *d = zero; char *clean = STRDUP( pathfile ); - for( int i = 0; clean[i]; ++i ) if(clean[i] == '\\') clean[i] = '/'; - for( int len = strlen(clean); clean[--len] == '/'; ) clean[len] = '\0'; - char buffer[2048]; + { + int i; + for (i = 0; clean[i]; ++i) if (clean[i] == '\\') clean[i] = '/'; + } + { + size_t len; + for (len = strlen(clean); clean[--len] == '/';) clean[len] = '\0'; + } + dir_yield(d, clean, buffer, 2048); REALLOC(clean, 0); @@ -1441,7 +1500,8 @@ dir *dir_open(const char *pathfile, const char *mode) { } int dir_find(dir *d, const char *entryname) { - for( int i = d->count; --i >= 0; ) { // in case of several copies, grab most recent file (last coincidence) + int i; + for( i = d->count; --i >= 0; ) { // in case of several copies, grab most recent file (last coincidence) if( 0 == strcmp(entryname, d->entry[i].filename)) return i; } return -1; @@ -1466,7 +1526,8 @@ unsigned dir_file(dir *d, unsigned index) { void *dir_read(dir *d, unsigned index) { if( d && index < d->count ) { void *data = 0; - for( FILE *fp = fopen(d->entry[index].filename, "rb"); fp; fclose(fp), fp = 0) { + FILE *fp; + for( fp = fopen(d->entry[index].filename, "rb"); fp; fclose(fp), fp = 0 ) { size_t len = d->entry[index].size; data = REALLOC(0, len); if( data && fread(data, 1, len, fp) != len ) { @@ -1479,7 +1540,8 @@ void *dir_read(dir *d, unsigned index) { } void dir_close(dir *d) { - for( int i = 0; i < d->count; ++i) { + int i; + for( i = 0; i < d->count; ++i ) { REALLOC(d->entry[i].filename, 0); } dir zero = {0}; @@ -1491,7 +1553,8 @@ void dir_close(dir *d) { int main( int argc, char **argv ) { dir *d = dir_open(argc > 1 ? argv[1] : "./", "rb"); if( d ) { - for( int i = 0; i < dir_count(d); ++i ) { + int i; + for( i = 0; i < dir_count(d); ++i ) { if( dir_file(d,i) ) printf("%3d) %11d %s\n", i + 1, dir_size(d,i), dir_name(d,i)); else