about summary refs log tree commit diff stats
path: root/src/bindings/zlib.nim
blob: 00acb1cae8ae85bee8024e639a6ccdb7a3b1bfd3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const zlib = (func(): string =
  let res = staticExec("pkg-config --libs --silence-errors zlib")
  if res != "":
    return res
)()
when zlib == "":
  error("zlib not found")

{.passL: zlib.}

const
  Z_NO_FLUSH* = cint(0)
  Z_PARTIAL_FLUSH* = cint(1)
  Z_SYNC_FLUSH* = cint(2)
  Z_FULL_FLUSH* = cint(3)
  Z_FINISH* = cint(4)
  Z_BLOCK* = cint(5)
  Z_TREES* = cint(6)

const
  Z_OK* = cint(0)
  Z_STREAM_END* = cint(1)
  Z_NEED_DICT* = cint(2)
  Z_ERRNO* = cint(-1)
  Z_STREAM_ERROR* = cint(-2)
  Z_DATA_ERROR* = cint(-3)
  Z_MEM_ERROR* = cint(-4)
  Z_BUF_ERROR* = cint(-5)
  Z_VERSION_ERROR* = cint(-6)

const
  Z_BINARY* = cint(0)
  Z_TEXT* = cint(1)
  Z_ASCII* = Z_TEXT
  Z_UNKNOWN* = cint(2)

type
  alloc_func* {.importc, header: "zlib.h".} = proc (opaque: pointer,
    items: cuint, size: cuint): pointer {.cdecl.}

  free_func* {.importc, header: "zlib.h".} = proc (opaque: pointer,
    address: pointer) {.cdecl.}

  internal_state* {.importc, header: "zlib.h".} = object

  z_stream* {.importc, header: "zlib.h".} = object
    next_in*: ptr uint8 # next input byte
    avail_in*: cuint # number of bytes available in next_in
    total_in*: culong # total number of input bytes read so far

    next_out*: ptr uint8 # next output byte will go here
    avail_out*: cuint # remaining free space at next_out
    total_out*: culong # total number of bytes output so far

    msg*: cstring # last error message, NULL if no error
    state*: ptr internal_state # not visible by applications

    zalloc*: alloc_func # used to allocate the internal state
    zfree*: free_func # used to free the internal state 
    opaque*: pointer # private data object passed to zalloc and zfree 

    data_type*: cint # best guess about the data type: binary or text
                     # for deflate, or the decoding state for inflate 
    adler*: culong # Adler-32 or CRC-32 value of the uncompressed data 
    reserved*: culong # reserved for future use 

  z_streamp* = ptr z_stream

{.push header: "zlib.h", importc, cdecl.}
proc inflateInit*(strm: z_streamp): cint
proc inflate*(strm: z_streamp, flush: cint): cint
proc inflateEnd*(strm: z_streamp): cint
proc compress*(dest: ptr uint8, destLen: ptr culong, source: ptr uint8,
  sourceLen: culong): cint
proc compressBound*(sourceLen: culong): culong
proc uncompress*(dest: ptr uint8, destLen: ptr culong, source: ptr uint8,
  sourceLen: culong): cint
proc crc32*(crc: culong, buf: ptr uint8, len: cuint): culong
{.pop.}