Merge branch 'bigbreak' of https://github.com/Araq/Nimrod into bigbreak

This commit is contained in:
Araq 2014-09-17 23:14:13 +02:00
commit 47d755b418

View file

@ -14,32 +14,32 @@ type
Ulong* = int Ulong* = int
Ulongf* = int Ulongf* = int
Pulongf* = ptr Ulongf Pulongf* = ptr Ulongf
z_off_t* = int32 ZOffT* = int32
pbyte* = cstring Pbyte* = cstring
pbytef* = cstring Pbytef* = cstring
TAllocfunc* = proc (p: pointer, items: uInt, size: uInt): pointer{.cdecl.} TAllocfunc* = proc (p: pointer, items: Uint, size: Uint): pointer{.cdecl.}
TFreeFunc* = proc (p: pointer, address: pointer){.cdecl.} TFreeFunc* = proc (p: pointer, address: pointer){.cdecl.}
TInternalState*{.final, pure.} = object TInternalState*{.final, pure.} = object
PInternalState* = ptr TInternalstate PInternalState* = ptr TInternalState
TZStream*{.final, pure.} = object TZStream*{.final, pure.} = object
next_in*: pbytef nextIn*: Pbytef
avail_in*: uInt availIn*: Uint
total_in*: uLong totalIn*: Ulong
next_out*: pbytef nextOut*: Pbytef
avail_out*: uInt availOut*: Uint
total_out*: uLong totalOut*: Ulong
msg*: pbytef msg*: Pbytef
state*: PInternalState state*: PInternalState
zalloc*: TAllocFunc zalloc*: TAllocfunc
zfree*: TFreeFunc zfree*: TFreeFunc
opaque*: pointer opaque*: pointer
data_type*: int32 dataType*: int32
adler*: uLong adler*: Ulong
reserved*: uLong reserved*: Ulong
TZStreamRec* = TZStream TZStreamRec* = TZStream
PZstream* = ptr TZStream PZstream* = ptr TZStream
gzFile* = pointer GzFile* = pointer
const const
Z_NO_FLUSH* = 0 Z_NO_FLUSH* = 0
@ -78,79 +78,79 @@ proc inflate*(strm: var TZStream, flush: int32): int32{.cdecl, dynlib: libz,
importc: "inflate".} importc: "inflate".}
proc inflateEnd*(strm: var TZStream): int32{.cdecl, dynlib: libz, proc inflateEnd*(strm: var TZStream): int32{.cdecl, dynlib: libz,
importc: "inflateEnd".} importc: "inflateEnd".}
proc deflateSetDictionary*(strm: var TZStream, dictionary: pbytef, proc deflateSetDictionary*(strm: var TZStream, dictionary: Pbytef,
dictLength: uInt): int32{.cdecl, dynlib: libz, dictLength: Uint): int32{.cdecl, dynlib: libz,
importc: "deflateSetDictionary".} importc: "deflateSetDictionary".}
proc deflateCopy*(dest, source: var TZstream): int32{.cdecl, dynlib: libz, proc deflateCopy*(dest, source: var TZStream): int32{.cdecl, dynlib: libz,
importc: "deflateCopy".} importc: "deflateCopy".}
proc deflateReset*(strm: var TZStream): int32{.cdecl, dynlib: libz, proc deflateReset*(strm: var TZStream): int32{.cdecl, dynlib: libz,
importc: "deflateReset".} importc: "deflateReset".}
proc deflateParams*(strm: var TZStream, level: int32, strategy: int32): int32{. proc deflateParams*(strm: var TZStream, level: int32, strategy: int32): int32{.
cdecl, dynlib: libz, importc: "deflateParams".} cdecl, dynlib: libz, importc: "deflateParams".}
proc inflateSetDictionary*(strm: var TZStream, dictionary: pbytef, proc inflateSetDictionary*(strm: var TZStream, dictionary: Pbytef,
dictLength: uInt): int32{.cdecl, dynlib: libz, dictLength: Uint): int32{.cdecl, dynlib: libz,
importc: "inflateSetDictionary".} importc: "inflateSetDictionary".}
proc inflateSync*(strm: var TZStream): int32{.cdecl, dynlib: libz, proc inflateSync*(strm: var TZStream): int32{.cdecl, dynlib: libz,
importc: "inflateSync".} importc: "inflateSync".}
proc inflateReset*(strm: var TZStream): int32{.cdecl, dynlib: libz, proc inflateReset*(strm: var TZStream): int32{.cdecl, dynlib: libz,
importc: "inflateReset".} importc: "inflateReset".}
proc compress*(dest: pbytef, destLen: puLongf, source: pbytef, sourceLen: uLong): cint{. proc compress*(dest: Pbytef, destLen: Pulongf, source: Pbytef, sourceLen: Ulong): cint{.
cdecl, dynlib: libz, importc: "compress".} cdecl, dynlib: libz, importc: "compress".}
proc compress2*(dest: pbytef, destLen: puLongf, source: pbytef, proc compress2*(dest: Pbytef, destLen: Pulongf, source: Pbytef,
sourceLen: uLong, level: cint): cint{.cdecl, dynlib: libz, sourceLen: Ulong, level: cint): cint{.cdecl, dynlib: libz,
importc: "compress2".} importc: "compress2".}
proc uncompress*(dest: pbytef, destLen: puLongf, source: pbytef, proc uncompress*(dest: Pbytef, destLen: Pulongf, source: Pbytef,
sourceLen: uLong): cint{.cdecl, dynlib: libz, sourceLen: Ulong): cint{.cdecl, dynlib: libz,
importc: "uncompress".} importc: "uncompress".}
proc compressBound*(sourceLen: uLong): uLong {.cdecl, dynlib: libz, importc.} proc compressBound*(sourceLen: Ulong): Ulong {.cdecl, dynlib: libz, importc.}
proc gzopen*(path: cstring, mode: cstring): gzFile{.cdecl, dynlib: libz, proc gzopen*(path: cstring, mode: cstring): GzFile{.cdecl, dynlib: libz,
importc: "gzopen".} importc: "gzopen".}
proc gzdopen*(fd: int32, mode: cstring): gzFile{.cdecl, dynlib: libz, proc gzdopen*(fd: int32, mode: cstring): GzFile{.cdecl, dynlib: libz,
importc: "gzdopen".} importc: "gzdopen".}
proc gzsetparams*(thefile: gzFile, level: int32, strategy: int32): int32{.cdecl, proc gzsetparams*(thefile: GzFile, level: int32, strategy: int32): int32{.cdecl,
dynlib: libz, importc: "gzsetparams".} dynlib: libz, importc: "gzsetparams".}
proc gzread*(thefile: gzFile, buf: pointer, length: int): int32{.cdecl, proc gzread*(thefile: GzFile, buf: pointer, length: int): int32{.cdecl,
dynlib: libz, importc: "gzread".} dynlib: libz, importc: "gzread".}
proc gzwrite*(thefile: gzFile, buf: pointer, length: int): int32{.cdecl, proc gzwrite*(thefile: GzFile, buf: pointer, length: int): int32{.cdecl,
dynlib: libz, importc: "gzwrite".} dynlib: libz, importc: "gzwrite".}
proc gzprintf*(thefile: gzFile, format: pbytef): int32{.varargs, cdecl, proc gzprintf*(thefile: GzFile, format: Pbytef): int32{.varargs, cdecl,
dynlib: libz, importc: "gzprintf".} dynlib: libz, importc: "gzprintf".}
proc gzputs*(thefile: gzFile, s: pbytef): int32{.cdecl, dynlib: libz, proc gzputs*(thefile: GzFile, s: Pbytef): int32{.cdecl, dynlib: libz,
importc: "gzputs".} importc: "gzputs".}
proc gzgets*(thefile: gzFile, buf: pbytef, length: int32): pbytef{.cdecl, proc gzgets*(thefile: GzFile, buf: Pbytef, length: int32): Pbytef{.cdecl,
dynlib: libz, importc: "gzgets".} dynlib: libz, importc: "gzgets".}
proc gzputc*(thefile: gzFile, c: char): char{.cdecl, dynlib: libz, proc gzputc*(thefile: GzFile, c: char): char{.cdecl, dynlib: libz,
importc: "gzputc".} importc: "gzputc".}
proc gzgetc*(thefile: gzFile): char{.cdecl, dynlib: libz, importc: "gzgetc".} proc gzgetc*(thefile: GzFile): char{.cdecl, dynlib: libz, importc: "gzgetc".}
proc gzflush*(thefile: gzFile, flush: int32): int32{.cdecl, dynlib: libz, proc gzflush*(thefile: GzFile, flush: int32): int32{.cdecl, dynlib: libz,
importc: "gzflush".} importc: "gzflush".}
proc gzseek*(thefile: gzFile, offset: z_off_t, whence: int32): z_off_t{.cdecl, proc gzseek*(thefile: GzFile, offset: ZOffT, whence: int32): ZOffT{.cdecl,
dynlib: libz, importc: "gzseek".} dynlib: libz, importc: "gzseek".}
proc gzrewind*(thefile: gzFile): int32{.cdecl, dynlib: libz, importc: "gzrewind".} proc gzrewind*(thefile: GzFile): int32{.cdecl, dynlib: libz, importc: "gzrewind".}
proc gztell*(thefile: gzFile): z_off_t{.cdecl, dynlib: libz, importc: "gztell".} proc gztell*(thefile: GzFile): ZOffT{.cdecl, dynlib: libz, importc: "gztell".}
proc gzeof*(thefile: gzFile): int {.cdecl, dynlib: libz, importc: "gzeof".} proc gzeof*(thefile: GzFile): int {.cdecl, dynlib: libz, importc: "gzeof".}
proc gzclose*(thefile: gzFile): int32{.cdecl, dynlib: libz, importc: "gzclose".} proc gzclose*(thefile: GzFile): int32{.cdecl, dynlib: libz, importc: "gzclose".}
proc gzerror*(thefile: gzFile, errnum: var int32): pbytef{.cdecl, dynlib: libz, proc gzerror*(thefile: GzFile, errnum: var int32): Pbytef{.cdecl, dynlib: libz,
importc: "gzerror".} importc: "gzerror".}
proc adler32*(adler: uLong, buf: pbytef, length: uInt): uLong{.cdecl, proc adler32*(adler: Ulong, buf: Pbytef, length: Uint): Ulong{.cdecl,
dynlib: libz, importc: "adler32".} dynlib: libz, importc: "adler32".}
## **Warning**: Adler-32 requires at least a few hundred bytes to get rolling. ## **Warning**: Adler-32 requires at least a few hundred bytes to get rolling.
proc crc32*(crc: uLong, buf: pbytef, length: uInt): uLong{.cdecl, dynlib: libz, proc crc32*(crc: Ulong, buf: Pbytef, length: Uint): Ulong{.cdecl, dynlib: libz,
importc: "crc32".} importc: "crc32".}
proc deflateInitu*(strm: var TZStream, level: int32, version: cstring, proc deflateInitu*(strm: var TZStream, level: int32, version: cstring,
stream_size: int32): int32{.cdecl, dynlib: libz, streamSize: int32): int32{.cdecl, dynlib: libz,
importc: "deflateInit_".} importc: "deflateInit_".}
proc inflateInitu*(strm: var TZStream, version: cstring, proc inflateInitu*(strm: var TZStream, version: cstring,
stream_size: int32): int32 {. streamSize: int32): int32 {.
cdecl, dynlib: libz, importc: "inflateInit_".} cdecl, dynlib: libz, importc: "inflateInit_".}
proc deflateInit*(strm: var TZStream, level: int32): int32 proc deflateInit*(strm: var TZStream, level: int32): int32
proc inflateInit*(strm: var TZStream): int32 proc inflateInit*(strm: var TZStream): int32
proc deflateInit2u*(strm: var TZStream, level: int32, `method`: int32, proc deflateInit2u*(strm: var TZStream, level: int32, `method`: int32,
windowBits: int32, memLevel: int32, strategy: int32, windowBits: int32, memLevel: int32, strategy: int32,
version: cstring, stream_size: int32): int32 {.cdecl, version: cstring, streamSize: int32): int32 {.cdecl,
dynlib: libz, importc: "deflateInit2_".} dynlib: libz, importc: "deflateInit2_".}
proc inflateInit2u*(strm: var TZStream, windowBits: int32, version: cstring, proc inflateInit2u*(strm: var TZStream, windowBits: int32, version: cstring,
stream_size: int32): int32{.cdecl, dynlib: libz, streamSize: int32): int32{.cdecl, dynlib: libz,
importc: "inflateInit2_".} importc: "inflateInit2_".}
proc deflateInit2*(strm: var TZStream, proc deflateInit2*(strm: var TZStream,
level, `method`, windowBits, memLevel, level, `method`, windowBits, memLevel,
@ -159,29 +159,29 @@ proc inflateInit2*(strm: var TZStream, windowBits: int32): int32
proc zError*(err: int32): cstring{.cdecl, dynlib: libz, importc: "zError".} proc zError*(err: int32): cstring{.cdecl, dynlib: libz, importc: "zError".}
proc inflateSyncPoint*(z: PZstream): int32{.cdecl, dynlib: libz, proc inflateSyncPoint*(z: PZstream): int32{.cdecl, dynlib: libz,
importc: "inflateSyncPoint".} importc: "inflateSyncPoint".}
proc get_crc_table*(): pointer{.cdecl, dynlib: libz, importc: "get_crc_table".} proc getCrcTable*(): pointer{.cdecl, dynlib: libz, importc: "get_crc_table".}
proc deflateInit(strm: var TZStream, level: int32): int32 = proc deflateInit(strm: var TZStream, level: int32): int32 =
result = deflateInitu(strm, level, ZLIB_VERSION(), sizeof(TZStream).cint) result = deflateInitu(strm, level, zlibVersion(), sizeof(TZStream).cint)
proc inflateInit(strm: var TZStream): int32 = proc inflateInit(strm: var TZStream): int32 =
result = inflateInitu(strm, ZLIB_VERSION(), sizeof(TZStream).cint) result = inflateInitu(strm, zlibVersion(), sizeof(TZStream).cint)
proc deflateInit2(strm: var TZStream, proc deflateInit2(strm: var TZStream,
level, `method`, windowBits, memLevel, level, `method`, windowBits, memLevel,
strategy: int32): int32 = strategy: int32): int32 =
result = deflateInit2u(strm, level, `method`, windowBits, memLevel, result = deflateInit2u(strm, level, `method`, windowBits, memLevel,
strategy, ZLIB_VERSION(), sizeof(TZStream).cint) strategy, zlibVersion(), sizeof(TZStream).cint)
proc inflateInit2(strm: var TZStream, windowBits: int32): int32 = proc inflateInit2(strm: var TZStream, windowBits: int32): int32 =
result = inflateInit2u(strm, windowBits, ZLIB_VERSION(), result = inflateInit2u(strm, windowBits, zlibVersion(),
sizeof(TZStream).cint) sizeof(TZStream).cint)
proc zlibAllocMem*(AppData: Pointer, Items, Size: int): Pointer {.cdecl.} = proc zlibAllocMem*(appData: pointer, items, size: int): pointer {.cdecl.} =
result = Alloc(Items * Size) result = alloc(items * size)
proc zlibFreeMem*(AppData, `Block`: Pointer) {.cdecl.} = proc zlibFreeMem*(appData, `block`: pointer) {.cdecl.} =
dealloc(`Block`) dealloc(`block`)
proc uncompress*(sourceBuf: cstring, sourceLen: int): string = proc uncompress*(sourceBuf: cstring, sourceLen: int): string =
## Given a deflated cstring returns its inflated version. ## Given a deflated cstring returns its inflated version.
@ -202,7 +202,7 @@ proc uncompress*(sourceBuf: cstring, sourceLen: int): string =
var z: TZStream var z: TZStream
# Initialize input. # Initialize input.
z.next_in = sourceBuf z.nextIn = sourceBuf
# Input left to decompress. # Input left to decompress.
var left = zlib.Uint(sourceLen) var left = zlib.Uint(sourceLen)
@ -220,12 +220,12 @@ proc uncompress*(sourceBuf: cstring, sourceLen: int): string =
var decompressed = newStringOfCap(space) var decompressed = newStringOfCap(space)
# Initialize output. # Initialize output.
z.next_out = addr(decompressed[0]) z.nextOut = addr(decompressed[0])
# Output generated so far. # Output generated so far.
var have = 0 var have = 0
# Set up for gzip decoding. # Set up for gzip decoding.
z.avail_in = 0; z.availIn = 0;
var status = inflateInit2(z, (15+16)) var status = inflateInit2(z, (15+16))
if status != Z_OK: if status != Z_OK:
# Out of memory. # Out of memory.
@ -241,10 +241,10 @@ proc uncompress*(sourceBuf: cstring, sourceLen: int): string =
discard inflateReset(z) discard inflateReset(z)
# Provide input for inflate. # Provide input for inflate.
if z.avail_in == 0: if z.availIn == 0:
# This only makes sense in the C version using unsigned values. # This only makes sense in the C version using unsigned values.
z.avail_in = left z.availIn = left
left -= z.avail_in left -= z.availIn
# Decompress the available input. # Decompress the available input.
while true: while true:
@ -260,15 +260,15 @@ proc uncompress*(sourceBuf: cstring, sourceLen: int): string =
# Increase space. # Increase space.
decompressed.setLen(space) decompressed.setLen(space)
# Update output pointer (might have moved). # Update output pointer (might have moved).
z.next_out = addr(decompressed[have]) z.nextOut = addr(decompressed[have])
# Provide output space for inflate. # Provide output space for inflate.
z.avail_out = zlib.Uint(space - have) z.availOut = zlib.Uint(space - have)
have += z.avail_out; have += z.availOut;
# Inflate and update the decompressed size. # Inflate and update the decompressed size.
status = inflate(z, Z_SYNC_FLUSH); status = inflate(z, Z_SYNC_FLUSH);
have -= z.avail_out; have -= z.availOut;
# Bail out if any errors. # Bail out if any errors.
if status != Z_OK and status != Z_BUF_ERROR and status != Z_STREAM_END: if status != Z_OK and status != Z_BUF_ERROR and status != Z_STREAM_END:
@ -279,10 +279,10 @@ proc uncompress*(sourceBuf: cstring, sourceLen: int): string =
# Repeat until all output is generated from provided input (note # Repeat until all output is generated from provided input (note
# that even if z.avail_in is zero, there may still be pending # that even if z.avail_in is zero, there may still be pending
# output -- we're not done until the output buffer isn't filled) # output -- we're not done until the output buffer isn't filled)
if z.avail_out != 0: if z.availOut != 0:
break break
# Continue until all input consumed. # Continue until all input consumed.
if left == 0 and z.avail_in == 0: if left == 0 and z.availIn == 0:
break break
# Verify that the input is a valid gzip stream. # Verify that the input is a valid gzip stream.