libfragmentzip.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //
  2. // libfragmentzip.h
  3. // libfragmentzip
  4. //
  5. // Created by tihmstar on 24.12.16.
  6. // Copyright © 2016 tihmstar. All rights reserved.
  7. //
  8. #ifndef libfragmentzip_h
  9. #define libfragmentzip_h
  10. #include <curl/curl.h>
  11. #include <stdlib.h>
  12. #include <stdint.h>
  13. #include <sys/types.h>
  14. #ifdef _WIN32
  15. #define STATIC_INLINE static __inline
  16. #define ATTRIBUTE_PACKED
  17. #pragma pack(push)
  18. #pragma pack(1)
  19. #else
  20. #define STATIC_INLINE static inline
  21. #define ATTRIBUTE_PACKED __attribute__ ((packed))
  22. #endif
  23. #define makeBE32(a) makeEndian((char *)(&(a)), 4, 1)
  24. #define makeLE32(a) makeEndian((char *)(&(a)), 4, 0)
  25. #define makeBE16(a) makeEndian((char *)(&(a)), 2, 1)
  26. #define makeLE16(a) makeEndian((char *)(&(a)), 2, 0)
  27. #define fragmentzip_nextCD(cd) ((fragmentzip_cd *)(cd->filename+cd->len_filename+cd->len_extra_field+cd->len_file_comment))
  28. #ifdef __cplusplus
  29. extern "C"
  30. {
  31. #else
  32. typedef enum{
  33. false = 0,
  34. true = 1
  35. }bool;
  36. #endif
  37. typedef struct{
  38. uint32_t signature;
  39. uint16_t version;
  40. uint16_t flags;
  41. uint16_t compression;
  42. uint16_t modtime;
  43. uint16_t moddate;
  44. uint32_t crc32;
  45. uint32_t size_compressed;
  46. uint32_t size_uncompressed;
  47. uint16_t len_filename;
  48. uint16_t len_extra_field;
  49. char filename[1]; //variable length
  50. // char extra_field[]; //variable length
  51. } ATTRIBUTE_PACKED fragentzip_local_file;
  52. typedef struct{
  53. uint32_t crc32;
  54. uint32_t size_compressed;
  55. uint32_t size_uncompressed;
  56. } ATTRIBUTE_PACKED fragmentzip_data_descriptor;
  57. typedef struct{
  58. uint32_t signature;
  59. uint16_t disk_cur_number;
  60. uint16_t disk_cd_start_number;
  61. uint16_t cd_disk_number;
  62. uint16_t cd_entries;
  63. uint32_t cd_size;
  64. uint32_t cd_start_offset;
  65. uint16_t comment_len;
  66. } ATTRIBUTE_PACKED fragmentzip_end_of_cd;
  67. typedef struct{
  68. uint32_t signature;
  69. uint16_t version;
  70. uint16_t pkzip_version_needed;
  71. uint16_t flags;
  72. uint16_t compression;
  73. uint16_t modtime;
  74. uint16_t moddate;
  75. uint32_t crc32;
  76. uint32_t size_compressed;
  77. uint32_t size_uncompressed;
  78. uint16_t len_filename;
  79. uint16_t len_extra_field;
  80. uint16_t len_file_comment;
  81. uint16_t disk_num;
  82. uint16_t internal_attribute;
  83. uint32_t external_attribute;
  84. uint32_t local_header_offset;
  85. char filename[1]; //variable length
  86. // char extra_field[]; //variable length
  87. // char file_comment[]; //variable length
  88. } ATTRIBUTE_PACKED fragmentzip_cd;
  89. typedef struct fragmentzip_info{
  90. char *url;
  91. CURL *mcurl;
  92. uint64_t length;
  93. fragmentzip_cd *cd;
  94. fragmentzip_end_of_cd *cd_end;
  95. } fragmentzip_t;
  96. STATIC_INLINE bool isBigEndian(){
  97. static const uint32_t tst = 0x41424344;
  98. return (bool)__builtin_expect(((char*)&tst)[0] == 0x41,0);
  99. }
  100. STATIC_INLINE void makeEndian(char * buf, unsigned int size, bool big){
  101. if (isBigEndian() != big){
  102. switch (size) {
  103. case 2:
  104. buf[0] ^= buf[1];
  105. buf[1] ^= buf[0];
  106. buf[0] ^= buf[1];
  107. break;
  108. case 4:
  109. buf[0] ^= buf[3];
  110. buf[3] ^= buf[0];
  111. buf[0] ^= buf[3];
  112. buf[2] ^= buf[1];
  113. buf[1] ^= buf[2];
  114. buf[2] ^= buf[1];
  115. break;
  116. default:
  117. printf("[FATAL] operation not supported\n");
  118. exit(1);
  119. break;
  120. }
  121. }
  122. }
  123. typedef void (*fragmentzip_process_callback_t)(unsigned int progress);
  124. fragmentzip_t *fragmentzip_open(const char *url);
  125. fragmentzip_t *fragmentzip_open_extended(const char *url, CURL *mcurl); //pass custom CURL with web auth by basic/digest or cookies
  126. int fragmentzip_download_file(fragmentzip_t *info, const char *remotepath, const char *savepath, fragmentzip_process_callback_t callback);
  127. void fragmentzip_close(fragmentzip_t *info);
  128. fragmentzip_cd *fragmentzip_getCDForPath(fragmentzip_t *info, const char *path);
  129. #ifdef __cplusplus
  130. }
  131. #endif
  132. #endif /* libfragmentzip_h */