zlib.odin 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. package vendor_zlib
  2. import "core:c"
  3. when ODIN_OS == .Windows {
  4. foreign import zlib "libz.lib"
  5. } else when ODIN_OS == .Linux {
  6. foreign import zlib "system:z"
  7. } else {
  8. foreign import zlib "system:z"
  9. }
  10. VERSION :: "1.2.12"
  11. VERNUM :: 0x12c0
  12. VER_MAJOR :: 1
  13. VER_MINOR :: 2
  14. VER_REVISION :: 12
  15. VER_SUBREVISION :: 0
  16. voidp :: rawptr
  17. voidpf :: rawptr
  18. voidpc :: rawptr
  19. Byte :: c.uchar
  20. Bytef :: c.uchar
  21. uInt :: c.uint
  22. uIntf :: c.uint
  23. uLong :: c.ulong
  24. uLongf :: c.ulong
  25. size_t :: c.size_t
  26. off_t :: c.long
  27. off64_t :: i64
  28. crc_t :: u32
  29. alloc_func :: proc "c" (opaque: voidp, items: uInt, size: uInt) -> voidpf
  30. free_func :: proc "c" (opaque: voidp, address: voidpf)
  31. in_func :: proc "c" (rawptr, [^][^]c.uchar) -> c.uint
  32. out_func :: proc "c" (rawptr, [^]c.uchar, c.uint) -> c.int
  33. gzFile_s :: struct {
  34. have: c.uint,
  35. next: [^]c.uchar,
  36. pos: off64_t,
  37. }
  38. gzFile :: ^gzFile_s
  39. z_stream_s :: struct {
  40. next_in: ^Bytef,
  41. avail_in: uInt,
  42. total_in: uLong,
  43. next_out: ^Bytef,
  44. avail_out: uInt,
  45. total_out: uLong,
  46. msg: [^]c.char,
  47. state: rawptr,
  48. zalloc: alloc_func,
  49. zfree: free_func,
  50. opaque: voidpf,
  51. data_type: c.int,
  52. adler: uLong,
  53. reserved: uLong,
  54. }
  55. z_stream :: z_stream_s
  56. z_streamp :: ^z_stream
  57. gz_header_s :: struct {
  58. text: c.int,
  59. time: uLong,
  60. xflags: c.int,
  61. os: c.int,
  62. extra: [^]Bytef,
  63. extra_len: uInt,
  64. extra_max: uInt,
  65. name: [^]Bytef,
  66. name_max: uInt,
  67. comment: [^]Bytef,
  68. comm_max: uInt,
  69. hcrc: c.int,
  70. done: c.int,
  71. }
  72. gz_header :: gz_header_s
  73. gz_headerp :: ^gz_header
  74. // Allowed flush values; see deflate() and inflate() below for details
  75. NO_FLUSH :: 0
  76. PARTIAL_FLUSH :: 1
  77. SYNC_FLUSH :: 2
  78. FULL_FLUSH :: 3
  79. FINISH :: 4
  80. BLOCK :: 5
  81. TREES :: 6
  82. // Return codes for the compression/decompression functions. Negative values are
  83. // errors, positive values are used for special but normal events.
  84. OK :: 0
  85. STREAM_END :: 1
  86. NEED_DICT :: 2
  87. ERRNO :: -1
  88. STREAM_ERROR :: -2
  89. DATA_ERROR :: -3
  90. MEM_ERROR :: -4
  91. BUF_ERROR :: -5
  92. VERSION_ERROR :: -6
  93. // compression levels
  94. NO_COMPRESSION :: 0
  95. BEST_SPEED :: 1
  96. BEST_COMPRESSION :: 9
  97. DEFAULT_COMPRESSION :: -1
  98. // compression strategy; see deflateInit2() below for details
  99. FILTERED :: 1
  100. HUFFMAN_ONLY :: 2
  101. RLE :: 3
  102. FIXED :: 4
  103. DEFAULT_STRATEGY :: 0
  104. // Possible values of the data_type field for deflate()
  105. BINARY :: 0
  106. TEXT :: 1
  107. ASCII :: TEXT // for compatibility with 1.2.2 and earlier
  108. UNKNOWN :: 2
  109. // The deflate compression method (the only one supported in this version)
  110. DEFLATED :: 8
  111. NULL :: 0 // for initializing zalloc, zfree, opaque
  112. version :: Version // for compatibility with versions < 1.0.2
  113. @(default_calling_convention="c")
  114. foreign zlib {
  115. // becase zlib.zlibVersion would be silly to write
  116. @(link_prefix="zlib")
  117. Version :: proc() -> cstring ---
  118. deflate :: proc(strm: z_streamp, flush: c.int) -> c.int ---
  119. deflateEnd :: proc(strm: z_streamp) -> c.int ---
  120. inflate :: proc(strm: z_streamp, flush: c.int) -> c.int ---
  121. inflateEnd :: proc(strm: z_streamp) -> c.int ---
  122. deflateSetDictionary :: proc(strm: z_streamp, dictionary: [^]Bytef, dictLength: uInt) -> c.int ---
  123. deflateGetDictionary :: proc(strm: z_streamp, dictionary: [^]Bytef, dictLength: ^uInt) -> c.int ---
  124. deflateCopy :: proc(dest, source: z_streamp) -> c.int ---
  125. deflateReset :: proc(strm: z_streamp) -> c.int ---
  126. deflateParams :: proc(strm: z_streamp, level, strategy: c.int) -> c.int ---
  127. deflateTune :: proc(strm: z_streamp, good_length, max_lazy, nice_length, max_chain: c.int) -> c.int ---
  128. deflateBound :: proc(strm: z_streamp, sourceLen: uLong) -> uLong ---
  129. deflatePending :: proc(strm: z_streamp, pending: [^]c.uint, bits: [^]c.int) -> c.int ---
  130. deflatePrime :: proc(strm: z_streamp, bits, value: c.int) -> c.int ---
  131. deflateSetHeader :: proc(strm: z_streamp, head: gz_headerp) -> c.int ---
  132. inflateSetDictionary :: proc(strm: z_streamp, dictionary: [^]Bytef, dictLength: uInt) -> c.int ---
  133. inflateGetDictionary :: proc(strm: z_streamp, dictionary: [^]Bytef, dictLength: ^uInt) -> c.int ---
  134. inflateSync :: proc(strm: z_streamp) -> c.int ---
  135. inflateCopy :: proc(dest, source: z_streamp) -> c.int ---
  136. inflateReset :: proc(strm: z_streamp) -> c.int ---
  137. inflateReset2 :: proc(strm: z_streamp, windowBits: c.int) -> c.int ---
  138. inflatePrime :: proc(strm: z_streamp, bits, value: c.int) -> c.int ---
  139. inflateMark :: proc(strm: z_streamp) -> c.long ---
  140. inflateGetHeader :: proc(strm: z_streamp, head: gz_headerp) -> c.int ---
  141. inflateBack :: proc(strm: z_streamp, _in: in_func, in_desc: rawptr, out: out_func, out_desc: rawptr) -> c.int ---
  142. inflateBackEnd :: proc(strm: z_streamp) -> c.int ---
  143. zlibCompileFlags :: proc() -> uLong ---
  144. compress :: proc(dest: [^]Bytef, destLen: ^uLongf, source: [^]Bytef, sourceLen: uLong) -> c.int ---
  145. compress2 :: proc(dest: [^]Bytef, destLen: ^uLongf, source: [^]Bytef, sourceLen: uLong, level: c.int) -> c.int ---
  146. compressBound :: proc(sourceLen: uLong) -> uLong ---
  147. uncompress :: proc(dest: [^]Bytef, destLen: ^uLongf, source: [^]Bytef, sourceLen: uLong) -> c.int ---
  148. uncompress2 :: proc(dest: [^]Bytef, destLen: ^uLongf, source: [^]Bytef, sourceLen: ^uLong) -> c.int ---
  149. gzdopen :: proc(fd: c.int, mode: cstring) -> gzFile ---
  150. gzbuffer :: proc(file: gzFile, size: c.uint) -> c.int ---
  151. gzsetparams :: proc(file: gzFile, level, strategy: c.int) -> c.int ---
  152. gzread :: proc(file: gzFile, buf: voidp, len: c.uint) -> c.int ---
  153. gzfread :: proc(buf: voidp, size, nitems: size_t, file: gzFile) -> size_t ---
  154. gzwrite :: proc(file: gzFile, buf: voidpc, len: c.uint) -> c.int ---
  155. gzfwrite :: proc(buf: voidpc, size, nitems: size_t, file: gzFile) -> size_t ---
  156. gzprintf :: proc(file: gzFile, format: cstring, #c_vararg args: ..any) -> c.int ---
  157. gzputs :: proc(file: gzFile, s: cstring) -> c.int ---
  158. gzgets :: proc(file: gzFile, buf: [^]c.char, len: c.int) -> [^]c.char ---
  159. gzputc :: proc(file: gzFile, ch: c.int) -> c.int ---
  160. gzgetc_ :: proc(file: gzFile) -> c.int --- // backwards compat, not the same as gzget
  161. gzungetc :: proc(ch: c.int, file: gzFile) -> c.int ---
  162. gzflush :: proc(file: gzFile, flush: c.int) -> c.int ---
  163. gzrewind :: proc(file: gzFile) -> c.int ---
  164. gzeof :: proc(file: gzFile) -> c.int ---
  165. gzdirect :: proc(file: gzFile) -> c.int ---
  166. gzclose :: proc(file: gzFile) -> c.int ---
  167. gzclose_r :: proc(file: gzFile) -> c.int ---
  168. gzclose_w :: proc(file: gzFile) -> c.int ---
  169. gzerror :: proc(file: gzFile, errnum: ^c.int) -> cstring ---
  170. gzclearerr :: proc(file: gzFile) ---
  171. adler32 :: proc(adler: uLong, buf: [^]Bytef, len: uInt) -> uLong ---
  172. adler32_z :: proc(adler: uLong, buf: [^]Bytef, len: size_t) -> uLong ---
  173. crc32 :: proc(crc: uLong, buf: [^]Bytef, len: uInt) -> uLong ---
  174. crc32_z :: proc(crc: uLong, buf: [^]Bytef, len: size_t) -> uLong ---
  175. crc32_combine_op :: proc(crc1, crc2, op: uLong) -> uLong ---
  176. gzopen64 :: proc(cstring, cstring) -> gzFile ---
  177. gzseek64 :: proc(gzFile, off64_t, c.int) -> off64_t ---
  178. gztell64 :: proc(gzFile) -> off64_t ---
  179. gzoffset64 :: proc(gzFile) -> off64_t ---
  180. adler32_combine64 :: proc(uLong, uLong, off64_t) -> uLong ---
  181. crc32_combine64 :: proc(uLong, uLong, off64_t) -> uLong ---
  182. crc32_combine_gen64 :: proc(off64_t) -> uLong ---
  183. adler32_combine :: proc(uLong, uLong, off_t) -> uLong ---
  184. crc32_combine :: proc(uLong, uLong, off_t) -> uLong ---
  185. crc32_combine_gen :: proc(off_t) -> uLong ---
  186. zError :: proc(c.int) -> cstring ---
  187. inflateSyncPoint :: proc(z_streamp) -> c.int ---
  188. get_crc_table :: proc() -> [^]crc_t ---
  189. inflateUndermine :: proc(z_streamp, c.int) -> c.int ---
  190. inflateValidate :: proc(z_streamp, c.int) -> c.int ---
  191. inflateCodesUsed :: proc(z_streamp) -> c.ulong ---
  192. inflateResetKeep :: proc(z_streamp) -> c.int ---
  193. deflateResetKeep :: proc(z_streamp) -> c.int ---
  194. }
  195. // Make these private since we create wrappers below passing in version and size
  196. // of the stream structure like zlib.h does
  197. @(private)
  198. @(default_calling_convention="c")
  199. foreign zlib {
  200. deflateInit_ :: proc(strm: z_streamp, level: c.int, version: cstring, stream_size: c.int) -> c.int ---
  201. inflateInit_ :: proc(strm: z_streamp, level: c.int, version: cstring, stream_size: c.int) -> c.int ---
  202. deflateInit2_ :: proc(strm: z_streamp, level, method, windowBits, memLevel, strategy: c.int, version: cstring, stream_size: c.int) -> c.int ---
  203. inflateInit2_ :: proc(strm: z_streamp, windowBits: c.int, version: cstring, stream_size: c.int) -> c.int ---
  204. inflateBackInit_ :: proc(strm: z_streamp, windowBits: c.int, window: [^]c.uchar, version: cstring, stream_size: c.int) -> c.int ---
  205. // see below for explanation
  206. @(link_name="gzgetc")
  207. gzgetc_unique :: proc(file: gzFile) -> c.int ---
  208. }
  209. deflateInit :: #force_inline proc "c" (strm: z_streamp, level: c.int) -> c.int {
  210. return deflateInit_(strm, level, VERSION, c.int(size_of(z_stream)))
  211. }
  212. inflateInit :: #force_inline proc "c" (strm: z_streamp, level: c.int) -> c.int {
  213. return inflateInit_(strm, level, VERSION, c.int(size_of(z_stream)))
  214. }
  215. deflateInit2 :: #force_inline proc "c" (strm: z_streamp, level, method, windowBits, memLevel, strategy: c.int) -> c.int {
  216. return deflateInit2_(strm, level, method, windowBits, memLevel, strategy, VERSION, c.int(size_of(z_stream)))
  217. }
  218. inflateInit2 :: #force_inline proc "c" (strm: z_streamp, windowBits: c.int) -> c.int {
  219. return inflateInit2_(strm, windowBits, VERSION, c.int(size_of(z_stream)))
  220. }
  221. inflateBackInit :: #force_inline proc "c" (strm: z_streamp, windowBits: c.int, window: [^]c.uchar) -> c.int {
  222. return inflateBackInit_(strm, windowBits, window, VERSION, c.int(size_of(z_stream)))
  223. }
  224. // zlib.h redefines gzgetc with a macro and uses (gzgetc)(g) to invoke it from
  225. // inside the same macro (preventing macro expansion), in Odin we give that a
  226. // unique name using link_prefix then implement the body of the macro in our own
  227. // procedure calling the unique named gzgetc instead.
  228. gzgetc :: #force_inline proc(file: gzFile) -> c.int {
  229. if file.have != 0 {
  230. file.have -= 1
  231. file.pos += 1
  232. ch := c.int(file.next[0])
  233. file.next = &file.next[1]
  234. return ch
  235. }
  236. return gzgetc_unique(file)
  237. }