zzz.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * ZZZip/include/zzz.h
  3. * https://gitlab.com/bztsrc/zzzip
  4. *
  5. * Copyright (C) 2021 bzt (bztsrc@gitlab)
  6. *
  7. * Permission is hereby granted, free of charge, to any person
  8. * obtaining a copy of this software and associated documentation
  9. * files (the "Software"), to deal in the Software without
  10. * restriction, including without limitation the rights to use, copy,
  11. * modify, merge, publish, distribute, sublicense, and/or sell copies
  12. * of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be
  16. * included in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  22. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  23. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  25. * DEALINGS IN THE SOFTWARE.
  26. *
  27. * @brief ZZZip library API
  28. *
  29. */
  30. #ifndef _ZZZ_H_
  31. #define _ZZZ_H_
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif
  35. #include <stdint.h>
  36. /* file format and API version */
  37. #define ZZZ_VERSION 0x00
  38. #ifndef _MSC_VER
  39. #define _inline __inline__
  40. #define _pack __attribute__((packed))
  41. #define _unused __attribute__((unused))
  42. #pragma pack(push)
  43. #pragma pack(1)
  44. #else
  45. #define _inline
  46. #define _pack
  47. #define _unused __pragma(warning(suppress:4100))
  48. #endif
  49. /* file magics */
  50. #define ZZZ_MAGIC "ZZz\032" /* magic for encryption or entity blocks */
  51. #define ZZZ_MAGEND "ZEnd" /* magic for terminating end block */
  52. /* encryption methods */
  53. enum {
  54. ZZZ_ENC_AES_256_CBC, /* military grade NIST AES */
  55. ZZZ_ENC_SHA_256_XOR, /* simple symmetric xor cipher */
  56. ZZZ_ENC_LAST
  57. };
  58. /* encryption block */
  59. typedef struct {
  60. uint8_t magic[4]; /* magic bytes, ZZZ_MAGIC same as in entity block */
  61. uint16_t size; /* size of the block, indirectly defines number of encryption filters used */
  62. uint16_t mbz; /* file name length, must be zero */
  63. uint32_t passwdcrc; /* CRC of the password */
  64. uint8_t padding; /* content padding in power of 2 */
  65. uint8_t enctype[256]; /* encryption method(s) (could be more, but there must be at least one) */
  66. } _pack zzz_enc_block_t;
  67. /* OS-independent file types */
  68. #define ZZZ_TYPE_REG 0x00 /* regular file */
  69. #define ZZZ_TYPE_LNK 0x10 /* hard link */
  70. #define ZZZ_TYPE_SYM 0x20 /* symbolic link */
  71. #define ZZZ_TYPE_UNI 0x30 /* directory union */
  72. #define ZZZ_TYPE_CHR 0x40 /* character device */
  73. #define ZZZ_TYPE_BLK 0x50 /* block device */
  74. #define ZZZ_TYPE_DIR 0x60 /* directory */
  75. #define ZZZ_TYPE_FIFO 0x70 /* named FIFO */
  76. #define ZZZ_TYPE_LAST 0x80
  77. #define ZZZ_FILE_TYPE(x) ((x) & 0xF0) /* use on zzz_block_t.type */
  78. #define ZZZ_NUM_FILTER(x) ((x) & 0x0F) /* use on zzz_block_t.type */
  79. /* steal one bit from file name size and add it to header size */
  80. #define ZZZ_HEADER_MAXSIZE 131072
  81. #define ZZZ_HEADER_SIZE(x) (int)(((((zzz_block_t*)(x))->namelen & 0x8000) << 1) | ((zzz_block_t*)(x))->size)
  82. #define ZZZ_FILENAME_LEN(x) (int)(((zzz_block_t*)(x))->namelen & 0x7FFF)
  83. /* entity block header */
  84. typedef struct {
  85. uint8_t magic[4]; /* magic bytes, ZZZ_MAGIC */
  86. uint16_t size; /* size of the header */
  87. uint16_t namelen; /* file name length, must not be zero */
  88. uint16_t myear; /* file modification year */
  89. uint8_t mmon; /* file modification month 1 - 12 */
  90. uint8_t mday; /* file modification day 1 - 31 */
  91. uint8_t mhour; /* file modification hour 0 - 23 */
  92. uint8_t mmin; /* file modification minute 0 - 59 */
  93. uint8_t msec; /* file modification second 0 - 60 */
  94. uint8_t type; /* block type, file type and number of filters */
  95. uint64_t uncompressed; /* uncompressed file size */
  96. int64_t uncompressed_hi;
  97. uint64_t contentsize; /* content size or compressed file size (without optional padding) */
  98. int64_t contentsize_hi;
  99. } _pack zzz_block_t;
  100. extern char c_assert1[sizeof(zzz_block_t) == 48 ? 1 : -1];
  101. /* filters */
  102. enum {
  103. ZZZ_FILTER_ASCII, /* newline conversion */
  104. ZZZ_FILTER_CODE, /* bcj executable code conversion */
  105. ZZZ_FILTER_ZLIB, /* zlib deflate */
  106. ZZZ_FILTER_BZIP2, /* bzip2 */
  107. ZZZ_FILTER_LZMA, /* lzma */
  108. ZZZ_FILTER_XZ, /* lzma2, xz */
  109. ZZZ_FILTER_PPMD, /* PPMdH, similar to rar */
  110. ZZZ_FILTER_ZSTD /* ZStandard */
  111. };
  112. /* one filter descriptor */
  113. typedef struct {
  114. uint8_t method; /* one of the ZZZ_FILTER_x define above */
  115. uint8_t level; /* compression level, filter specific */
  116. } _pack zzz_filter_t;
  117. /* extra field types */
  118. enum {
  119. ZZZ_EXTRA_PAD = 0, /* padding */
  120. ZZZ_EXTRA_COMMENT, /* UTF-8 comment */
  121. ZZZ_EXTRA_MIME, /* mime type, with optional ";charset=x" */
  122. ZZZ_EXTRA_ICON, /* PNG or SVG icon */
  123. ZZZ_EXTRA_META, /* meta info (XAttr) */
  124. ZZZ_EXTRA_TIME, /* POSIX timestamps */
  125. ZZZ_EXTRA_ACCESS, /* POSIX mode, uid, gid, username and groupname */
  126. ZZZ_EXTRA_NFSACL, /* POSIX NFSv4 ACL */
  127. ZZZ_EXTRA_TEXTACL, /* text-based ACL (OS/2 and POSIX 1003e draft std 17) */
  128. ZZZ_EXTRA_UUIDACL, /* UUID ACL */
  129. /* other, OS-Specific information */
  130. ZZZ_EXTRA_OS_OS2 = 0x10,
  131. ZZZ_EXTRA_OS_VMS,
  132. ZZZ_EXTRA_OS_MAC,
  133. ZZZ_EXTRA_OS_WIN,
  134. ZZZ_EXTRA_OS_T4,
  135. ZZZ_EXTRA_OS_OS390,
  136. ZZZ_EXTRA_OS_AS400,
  137. ZZZ_EXTRA_OS_MORPH,
  138. ZZZ_EXTRA_OS_HAIKU
  139. };
  140. /* extra field descriptor */
  141. typedef struct {
  142. uint8_t type; /* one of the ZZZ_EXTRA_x define above */
  143. uint8_t rev; /* most significant bit means big-endian for OS-Specific fields */
  144. uint16_t size; /* size of the extra field */
  145. } _pack zzz_extra_t;
  146. /* end block */
  147. typedef struct {
  148. uint8_t magic[4]; /* magic bytes, ZZZ_MAGEND */
  149. uint16_t size; /* size of the block, always 48 */
  150. uint16_t filetypes; /* bitmask for stored file types */
  151. uint16_t ayear; /* archive creation year */
  152. uint8_t amon; /* archive creation month 1 - 12 */
  153. uint8_t aday; /* archive creation day 1 - 31 */
  154. uint8_t ahour; /* archive creation hour 0 - 23 */
  155. uint8_t amin; /* archive creation minute 0 - 59 */
  156. uint8_t asec; /* archive creation second 0 - 60 */
  157. uint8_t revision; /* ZZZ_VERSION */
  158. uint64_t uncompressed; /* total of uncompressed sizes in entity blocks */
  159. int64_t uncompressed_hi;
  160. uint64_t numblk; /* total number of blocks in the archive */
  161. uint32_t filters; /* bitmask of used filters */
  162. uint32_t acrc; /* archive CRC */
  163. } _pack zzz_end_t;
  164. extern char c_assert2[sizeof(zzz_end_t) == 48 ? 1 : -1];
  165. #ifdef _MSC_VER
  166. #pragma pack(pop)
  167. #endif
  168. /* in-memory meta descriptor */
  169. typedef struct {
  170. uint16_t size; /* data size */
  171. char *name; /* UTF-8 key */
  172. uint8_t *data; /* binary data */
  173. } zzz_meta_t;
  174. /* in-memory extra field descriptor */
  175. typedef struct {
  176. uint8_t type; /* one of the ZZZ_EXTRA_x define above */
  177. uint8_t rev; /* most significant bit means big-endian for OS-Specific fields */
  178. uint16_t size; /* size of the the extra field's data */
  179. union {
  180. uint8_t *any; /* used for more types */
  181. struct {
  182. uint16_t len; /* number of meta key-value pairs */
  183. zzz_meta_t *data; /* key-value pairs */
  184. } meta;
  185. struct {
  186. uint64_t mtime; /* nanosec modification timestamp */
  187. uint64_t atime; /* nanosec access timestamp */
  188. uint64_t ctime; /* nanosec status change timestamp */
  189. uint64_t btime; /* nanosec birth (creation) timestamp */
  190. } time;
  191. struct {
  192. uint32_t mode; /* POSIX access right bits */
  193. uint64_t uid; /* owner numeric id */
  194. uint64_t gid; /* group numeric id */
  195. char *user; /* owner name */
  196. char *group; /* group name */
  197. } access;
  198. } attr;
  199. } zzz_attr_t;
  200. /* in-memory entity */
  201. typedef struct {
  202. zzz_block_t header; /* copy of the block header, machine native endian */
  203. zzz_filter_t filter[16]; /* used compression filters */
  204. char *filename; /* filename */
  205. char *target; /* target for links */
  206. int numextra; /* number of extra fields */
  207. zzz_attr_t *extra; /* extra fields */
  208. uint32_t devmaj, devmin; /* device major and minor for device files */
  209. uint32_t origcrc; /* uncompressed file's CRC */
  210. uint32_t blkcrc; /* entity block's CRC */
  211. } zzz_entity_t;
  212. /* return codes */
  213. enum {
  214. ZZZ_OK, /* 0 success */
  215. ZZZ_ENCRYPTED, /* 1 got an encryption block when reading */
  216. ZZZ_END, /* 2 last block reached */
  217. ZZZ_ERR_BADINP, /* 3 bad input parameter */
  218. ZZZ_ERR_NOMEM, /* 4 not enough memory */
  219. ZZZ_ERR_HDRTOOBIG, /* 5 header too big (max 128k) */
  220. ZZZ_ERR_IO, /* 6 file input output error */
  221. ZZZ_ERR_CORRUPT, /* 7 corrupt file format or bad CRC */
  222. ZZZ_ERR_UNSUPPORTED /* 8 unsupported filter/type in archive */
  223. };
  224. #define ZZZ_BUFSIZE 1048576 /* read and write buffer size (1M) */
  225. /* parameter for zzz_entity_file's text argument */
  226. enum {
  227. ZZZ_BINARY,
  228. ZZZ_TEXT
  229. };
  230. /* common.c */
  231. uint32_t zzz_crc32(uint32_t crc, uint8_t *buf, uint64_t len);
  232. char *zzz_realpath(const char *path);
  233. int zzz_entity_extra(void *ctx, int type, uint8_t *buf, uint16_t size);
  234. void zzz_entity_free(zzz_entity_t *ent);
  235. /* create.c */
  236. void *zzz_create_file(char *fn);
  237. void *zzz_create_stream(int fd);
  238. void *zzz_create_mem();
  239. int zzz_compression(void *ctx, int method, int level);
  240. int zzz_encrypt(void *ctx, int method, uint8_t *password, int pwdlen);
  241. int zzz_entity_file(void *ctx, char *fn, int text, int64_t size);
  242. int zzz_entity_hardlink(void *ctx, char *fn, char *target);
  243. int zzz_entity_symlink(void *ctx, char *fn, char *target);
  244. int zzz_entity_union(void *ctx, char *fn);
  245. int zzz_entity_union_target(void *ctx, char *target);
  246. int zzz_entity_chrdev(void *ctx, char *fn, uint32_t devmaj, uint32_t devmin);
  247. int zzz_entity_blkdev(void *ctx, char *fn, uint32_t devmaj, uint32_t devmin);
  248. int zzz_entity_dir(void *ctx, char *fn);
  249. int zzz_entity_fifo(void *ctx, char *fn);
  250. int zzz_entity_mtime(void *ctx, uint16_t year, uint8_t mon, uint8_t day, uint8_t hour, uint8_t min, uint8_t sec);
  251. int zzz_entity_mtime_t(void *ctx, uint64_t t);
  252. int zzz_entity_extra_comment(void *ctx, char *comment);
  253. int zzz_entity_extra_mime(void *ctx, char *mime);
  254. int zzz_entity_extra_icon(void *ctx, uint8_t *buf, uint16_t size);
  255. int zzz_entity_extra_meta(void *ctx, char *key, uint8_t *value, uint16_t size);
  256. int zzz_entity_extra_time(void *ctx, uint64_t mtime, uint64_t atime, uint64_t ctime, uint64_t btime);
  257. int zzz_entity_extra_access(void *ctx, uint32_t mode, char *user, uint64_t uid, char *group, uint64_t gid);
  258. int zzz_entity_extra_acl_nfs(void *ctx, char *acl);
  259. int zzz_entity_extra_acl_text(void *ctx, char *acl);
  260. int zzz_entity_extra_acl_uuid(void *ctx, uint8_t *acl, uint16_t size);
  261. int zzz_entity_extra_os_tag(void *ctx, int ostype, uint16_t tag, uint8_t *buf, uint16_t size);
  262. int zzz_entity_data(void *ctx, uint8_t *buf, uint64_t size);
  263. int zzz_entity_flush(void *ctx);
  264. int zzz_finish(void *ctx, uint8_t **mem, uint64_t *size);
  265. /* extract.c */
  266. void *zzz_open_file(char *fn);
  267. void *zzz_open_stream(int fd);
  268. void *zzz_open_mem(uint8_t *mem, uint64_t size);
  269. int zzz_decrypt(void *ctx, uint8_t *password, int pwdlen);
  270. int zzz_read_header(void *ctx, zzz_entity_t **ent);
  271. int zzz_read_data(void *ctx, uint8_t *buf, uint64_t *size);
  272. int zzz_close(void *ctx);
  273. #ifdef __cplusplus
  274. }
  275. #endif
  276. #endif /* _ZZZ_H_ */