Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 34b13402bd3d1feb14731397ecbb004f85715933 @planetbeing committed Apr 30, 2009
Showing with 586 additions and 0 deletions.
  1. +33 −0 CMakeLists.txt
  2. +7 −0 README
  3. +102 −0 include/common.h
  4. +84 −0 include/partial/partial.h
  5. +317 −0 partial.c
  6. +43 −0 test.c
@@ -0,0 +1,33 @@
+INCLUDE(FindCURL)
+INCLUDE(FindZLIB)
+
+cmake_minimum_required(VERSION 2.6)
+
+SET(BUILD_PARTIAL TRUE)
+
+IF(NOT ZLIB_FOUND)
+ message(STATUS "zlib is required for partial!")
+ SET(BUILD_PARTIAL FALSE)
+ENDIF(NOT ZLIB_FOUND)
+
+IF(NOT CURL_FOUND)
+ message(STATUS "curl is required for partial!")
+ SET(BUILD_PARTIAL FALSE)
+ENDIF(NOT CURL_FOUND)
+
+IF(BUILD_PARTIAL)
+ include_directories(${ZLIB_INCLUDE_DIR})
+ include_directories(${CURL_INCLUDE_DIR})
+ include_directories(include)
+
+ IF(WIN32)
+ add_definitions(-DCURL_STATICLIB)
+ ENDIF(WIN32)
+
+ add_library(partial partial.c)
+ target_link_libraries(partial ${CURL_LIBRARIES} ${ZLIB_LIBRARIES})
+
+ add_executable(test test.c)
+ target_link_libraries(test partial)
+ENDIF(BUILD_PARTIAL)
+
@@ -0,0 +1,7 @@
+Use cmake 2.6 to create the Makefiles for this.
+
+Requires libcurl 7.19.4 for file:// URL support.
+
+test.c contains an example that is designed for a simple test.zip containing
+only a text.txt.
+
@@ -0,0 +1,102 @@
+#ifndef COMMON_H
+#define COMMON_H
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include <sys/types.h>
+
+#ifdef WIN32
+#define fseeko fseeko64
+#define ftello ftello64
+#define off_t off64_t
+#define mkdir(x, y) mkdir(x)
+#define PATH_SEPARATOR "\\"
+#else
+#define PATH_SEPARATOR "/"
+#endif
+
+#define TRUE 1
+#define FALSE 0
+
+#define FLIPENDIAN(x) flipEndian((unsigned char *)(&(x)), sizeof(x))
+#define FLIPENDIANLE(x) flipEndianLE((unsigned char *)(&(x)), sizeof(x))
+
+#define IS_BIG_ENDIAN 0
+#define IS_LITTLE_ENDIAN 1
+
+#define TIME_OFFSET_FROM_UNIX 2082844800L
+#define APPLE_TO_UNIX_TIME(x) ((x) - TIME_OFFSET_FROM_UNIX)
+#define UNIX_TO_APPLE_TIME(x) ((x) + TIME_OFFSET_FROM_UNIX)
+
+#define ASSERT(x, m) if(!(x)) { fflush(stdout); fprintf(stderr, "error: %s\n", m); perror("error"); fflush(stderr); exit(1); }
+
+extern char endianness;
+
+static inline void flipEndian(unsigned char* x, int length) {
+ int i;
+ unsigned char tmp;
+
+ if(endianness == IS_BIG_ENDIAN) {
+ return;
+ } else {
+ for(i = 0; i < (length / 2); i++) {
+ tmp = x[i];
+ x[i] = x[length - i - 1];
+ x[length - i - 1] = tmp;
+ }
+ }
+}
+
+static inline void flipEndianLE(unsigned char* x, int length) {
+ int i;
+ unsigned char tmp;
+
+ if(endianness == IS_LITTLE_ENDIAN) {
+ return;
+ } else {
+ for(i = 0; i < (length / 2); i++) {
+ tmp = x[i];
+ x[i] = x[length - i - 1];
+ x[length - i - 1] = tmp;
+ }
+ }
+}
+
+static inline void hexToBytes(const char* hex, uint8_t** buffer, size_t* bytes) {
+ *bytes = strlen(hex) / 2;
+ *buffer = (uint8_t*) malloc(*bytes);
+ size_t i;
+ for(i = 0; i < *bytes; i++) {
+ uint32_t byte;
+ sscanf(hex, "%2x", &byte);
+ (*buffer)[i] = byte;
+ hex += 2;
+ }
+}
+
+static inline void hexToInts(const char* hex, unsigned int** buffer, size_t* bytes) {
+ *bytes = strlen(hex) / 2;
+ *buffer = (unsigned int*) malloc((*bytes) * sizeof(int));
+ size_t i;
+ for(i = 0; i < *bytes; i++) {
+ sscanf(hex, "%2x", &((*buffer)[i]));
+ hex += 2;
+ }
+}
+
+struct io_func_struct;
+
+typedef int (*readFunc)(struct io_func_struct* io, off_t location, size_t size, void *buffer);
+typedef int (*writeFunc)(struct io_func_struct* io, off_t location, size_t size, void *buffer);
+typedef void (*closeFunc)(struct io_func_struct* io);
+
+typedef struct io_func_struct {
+ void* data;
+ readFunc read;
+ writeFunc write;
+ closeFunc close;
+} io_func;
+
+#endif
@@ -0,0 +1,84 @@
+#include <inttypes.h>
+#include <curl/curl.h>
+
+typedef struct EndOfCD {
+ uint32_t signature;
+ uint16_t diskNo;
+ uint16_t CDDiskNo;
+ uint16_t CDDiskEntries;
+ uint16_t CDEntries;
+ uint32_t CDSize;
+ uint32_t CDOffset;
+ uint16_t lenComment;
+} __attribute__ ((packed)) EndOfCD;
+
+typedef struct CDFile {
+ uint32_t signature;
+ uint16_t version;
+ uint16_t versionExtract;
+ uint16_t flags;
+ uint16_t method;
+ uint16_t modTime;
+ uint16_t modDate;
+ uint32_t crc32;
+ uint32_t compressedSize;
+ uint32_t size;
+ uint16_t lenFileName;
+ uint16_t lenExtra;
+ uint16_t lenComment;
+ uint16_t diskStart;
+ uint16_t internalAttr;
+ uint32_t externalAttr;
+ uint32_t offset;
+} __attribute__ ((packed)) CDFile;
+
+typedef struct LocalFile {
+ uint32_t signature;
+ uint16_t versionExtract;
+ uint16_t flags;
+ uint16_t method;
+ uint16_t modTime;
+ uint16_t modDate;
+ uint32_t crc32;
+ uint32_t compressedSize;
+ uint32_t size;
+ uint16_t lenFileName;
+ uint16_t lenExtra;
+} __attribute__ ((packed)) LocalFile;
+
+typedef struct ZipInfo ZipInfo;
+
+typedef void (*PartialZipProgressCallback)(ZipInfo* info, CDFile* file, size_t progress);
+
+struct ZipInfo {
+ char* url;
+ uint64_t length;
+ CURL* hIPSW;
+ char* centralDirectory;
+ size_t centralDirectoryRecvd;
+ EndOfCD* centralDirectoryDesc;
+ char centralDirectoryEnd[0xffff + sizeof(EndOfCD)];
+ size_t centralDirectoryEndRecvd;
+ PartialZipProgressCallback progressCallback;
+};
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+ ZipInfo* PartialZipInit(const char* url);
+
+ CDFile* PartialZipFindFile(ZipInfo* info, const char* fileName);
+
+ CDFile* PartialZipListFiles(ZipInfo* info);
+
+ unsigned char* PartialZipGetFile(ZipInfo* info, CDFile* file);
+
+ void PartialZipRelease(ZipInfo* info);
+
+ void PartialZipSetProgressCallback(ZipInfo* info, PartialZipProgressCallback progressCallback);
+
+#ifdef __cplusplus
+}
+#endif
+
Oops, something went wrong. Retry.

0 comments on commit 34b1340

Please sign in to comment.