disable Nimble dependencies for appveyor
This commit is contained in:
parent
bf0993c835
commit
d2208091fa
43 changed files with 27230 additions and 7 deletions
18
tests/deps/zip-0.2.1/zip.nimble
Normal file
18
tests/deps/zip-0.2.1/zip.nimble
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
# Package
|
||||
|
||||
version = "0.2.1"
|
||||
author = "Anonymous"
|
||||
description = "Wrapper for the zip library"
|
||||
license = "MIT"
|
||||
|
||||
skipDirs = @["tests"]
|
||||
|
||||
# Dependencies
|
||||
|
||||
requires "nim >= 0.10.0"
|
||||
|
||||
task tests, "Run lib tests":
|
||||
withDir "tests":
|
||||
exec "nim c -r ziptests"
|
||||
exec "nim c -r zlibtests"
|
||||
exec "nim c -r gziptests"
|
||||
97
tests/deps/zip-0.2.1/zip/gzipfiles.nim
Normal file
97
tests/deps/zip-0.2.1/zip/gzipfiles.nim
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
import os
|
||||
import zlib
|
||||
import streams
|
||||
export streams
|
||||
|
||||
## This module implements a gzipfile stream for reading, writing, appending.
|
||||
|
||||
type
|
||||
GzFileStream* = ref object of Stream
|
||||
mode: FileMode
|
||||
f: GzFile
|
||||
|
||||
const SEEK_SET = 0.int32 # Seek from beginning of file.
|
||||
|
||||
proc fsClose(s: Stream) =
|
||||
if not GzFileStream(s).f.isNil:
|
||||
discard gzclose(GzFileStream(s).f)
|
||||
GzFileStream(s).f = nil
|
||||
|
||||
proc fsFlush(s: Stream) =
|
||||
# compiler flushFile also discard c_fflush
|
||||
discard gzflush(GzFileStream(s).f, Z_FINISH)
|
||||
|
||||
proc fsAtEnd(s: Stream): bool =
|
||||
result = gzeof(GzFileStream(s).f) == 1
|
||||
|
||||
proc fsSetPosition(s: Stream, pos: int) =
|
||||
if gzseek(GzFileStream(s).f, pos.ZOffT, SEEK_SET) == -1:
|
||||
if GzFileStream(s).mode in {fmWrite, fmAppend}:
|
||||
raise newException(IOError, "error in gzip stream while seeking! (file is in write/append mode!")
|
||||
else:
|
||||
raise newException(IOError, "error in gzip stream while seeking!")
|
||||
|
||||
proc fsGetPosition(s: Stream): int =
|
||||
result = gztell(GzFileStream(s).f).int
|
||||
|
||||
proc fsReadData(s: Stream, buffer: pointer, bufLen: int): int =
|
||||
result = gzread(GzFileStream(s).f, buffer, bufLen).int
|
||||
if result == -1:
|
||||
if GzFileStream(s).mode in {fmWrite, fmAppend}:
|
||||
raise newException(IOError, "cannot read data from write-only gzip stream!")
|
||||
else:
|
||||
raise newException(IOError, "cannot read from stream!")
|
||||
|
||||
proc fsPeekData(s: Stream, buffer: pointer, bufLen: int): int =
|
||||
let gz = GzFileStream(s)
|
||||
if gz.mode in {fmWrite, fmAppend}:
|
||||
raise newException(IOError, "cannot peek data from write-only gzip stream!")
|
||||
let pos = int(gztell(gz.f))
|
||||
result = fsReadData(s, buffer, bufLen)
|
||||
fsSetPosition(s, pos)
|
||||
|
||||
proc fsWriteData(s: Stream, buffer: pointer, bufLen: int) =
|
||||
if gzwrite(GzFileStream(s).f, buffer, bufLen).int != bufLen:
|
||||
if GzFileStream(s).mode in {fmWrite, fmAppend}:
|
||||
raise newException(IOError, "cannot write data to gzip stream!")
|
||||
else:
|
||||
raise newException(IOError, "cannot write data to read-only gzip stream!")
|
||||
|
||||
|
||||
proc newGzFileStream*(filename: string; mode=fmRead; level=Z_DEFAULT_COMPRESSION): GzFileStream =
|
||||
## Opens a Gzipfile as a file stream. `mode` can be
|
||||
## ``fmRead``, ``fmWrite`` or ``fmAppend``.
|
||||
##
|
||||
## Compression level can be set with ``level`` argument. Currently
|
||||
## ``Z_DEFAULT_COMPRESSION`` is 6.
|
||||
##
|
||||
## Note: ``level`` is ignored if ``mode`` is `fmRead`
|
||||
##
|
||||
## Note: There is only partial support for file seeking
|
||||
## - in fmRead mode, seeking randomly inside the gzip
|
||||
## file will lead to poor performance.
|
||||
## - in fmWrite, fmAppend mode, only forward seeking
|
||||
## is supported.
|
||||
new(result)
|
||||
case mode
|
||||
of fmRead: result.f = gzopen(filename, "rb")
|
||||
of fmWrite: result.f = gzopen(filename, "wb")
|
||||
of fmAppend: result.f = gzopen(filename, "ab")
|
||||
else: raise newException(IOError, "unsupported file mode '" & $mode &
|
||||
"' for GzFileStream!")
|
||||
if result.f.isNil:
|
||||
let err = osLastError()
|
||||
if err != OSErrorCode(0'i32):
|
||||
raiseOSError(err)
|
||||
if mode in {fmWrite, fmAppend}:
|
||||
discard gzsetparams(result.f, level.int32, Z_DEFAULT_STRATEGY.int32)
|
||||
|
||||
result.mode = mode
|
||||
result.closeImpl = fsClose
|
||||
result.atEndImpl = fsAtEnd
|
||||
result.setPositionImpl = fsSetPosition
|
||||
result.getPositionImpl = fsGetPosition
|
||||
result.readDataImpl = fsReadData
|
||||
result.peekDataImpl = fsPeekData
|
||||
result.writeDataImpl = fsWriteData
|
||||
result.flushImpl = fsFlush
|
||||
252
tests/deps/zip-0.2.1/zip/libzip.nim
Normal file
252
tests/deps/zip-0.2.1/zip/libzip.nim
Normal file
|
|
@ -0,0 +1,252 @@
|
|||
#
|
||||
#
|
||||
# Nim's Runtime Library
|
||||
# (c) Copyright 2013 Andreas Rumpf
|
||||
#
|
||||
# See the file "copying.txt", included in this
|
||||
# distribution, for details about the copyright.
|
||||
#
|
||||
|
||||
## Interface to the `libzip <http://www.nih.at/libzip/index.html>`_ library by
|
||||
## Dieter Baron and Thomas Klausner. This version links
|
||||
## against ``libzip2.so.2`` unless you define the symbol ``useLibzipSrc``; then
|
||||
## it is compiled against some old ``libizp_all.c`` file.
|
||||
|
||||
#
|
||||
# zip.h -- exported declarations.
|
||||
# Copyright (C) 1999-2008 Dieter Baron and Thomas Klausner
|
||||
#
|
||||
# This file is part of libzip, a library to manipulate ZIP archives.
|
||||
# The authors can be contacted at <libzip@nih.at>
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without
|
||||
# modification, are permitted provided that the following conditions
|
||||
# are met:
|
||||
# 1. Redistributions of source code must retain the above copyright
|
||||
# notice, this list of conditions and the following disclaimer.
|
||||
# 2. Redistributions in binary form must reproduce the above copyright
|
||||
# notice, this list of conditions and the following disclaimer in
|
||||
# the documentation and/or other materials provided with the
|
||||
# distribution.
|
||||
# 3. The names of the authors may not be used to endorse or promote
|
||||
# products derived from this software without specific prior
|
||||
# written permission.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
#
|
||||
|
||||
import times
|
||||
|
||||
when defined(unix) and not defined(useLibzipSrc):
|
||||
when defined(macosx):
|
||||
{.pragma: mydll, dynlib: "libzip(|2|4).dylib".}
|
||||
else:
|
||||
{.pragma: mydll, dynlib: "libzip(|2).so(|.4|.2|.1|.0)".}
|
||||
else:
|
||||
when defined(unix):
|
||||
{.passl: "-lz".}
|
||||
{.compile: "zip/private/libzip_all.c".}
|
||||
{.pragma: mydll.}
|
||||
|
||||
type
|
||||
ZipSourceCmd* = int32
|
||||
|
||||
ZipSourceCallback* = proc (state: pointer, data: pointer, length: int,
|
||||
cmd: ZipSourceCmd): int {.cdecl.}
|
||||
PZipStat* = ptr ZipStat
|
||||
ZipStat* = object ## the 'zip_stat' struct
|
||||
name*: cstring ## name of the file
|
||||
index*: int32 ## index within archive
|
||||
crc*: int32 ## crc of file data
|
||||
mtime*: Time ## modification time
|
||||
size*: int ## size of file (uncompressed)
|
||||
compSize*: int ## size of file (compressed)
|
||||
compMethod*: int16 ## compression method used
|
||||
encryptionMethod*: int16 ## encryption method used
|
||||
|
||||
Zip = object
|
||||
ZipSource = object
|
||||
ZipFile = object
|
||||
|
||||
PZip* = ptr Zip ## represents a zip archive
|
||||
PZipFile* = ptr ZipFile ## represents a file within an archive
|
||||
PZipSource* = ptr ZipSource ## represents a source for an archive
|
||||
{.deprecated: [TZipSourceCmd: ZipSourceCmd, TZipStat: ZipStat, TZip: Zip,
|
||||
TZipSourceCallback: ZipSourceCallback, TZipSource: ZipSource,
|
||||
TZipFile: ZipFile].}
|
||||
|
||||
# flags for zip_name_locate, zip_fopen, zip_stat, ...
|
||||
const
|
||||
ZIP_CREATE* = 1'i32
|
||||
ZIP_EXCL* = 2'i32
|
||||
ZIP_CHECKCONS* = 4'i32
|
||||
ZIP_FL_NOCASE* = 1'i32 ## ignore case on name lookup
|
||||
ZIP_FL_NODIR* = 2'i32 ## ignore directory component
|
||||
ZIP_FL_COMPRESSED* = 4'i32 ## read compressed data
|
||||
ZIP_FL_UNCHANGED* = 8'i32 ## use original data, ignoring changes
|
||||
ZIP_FL_RECOMPRESS* = 16'i32 ## force recompression of data
|
||||
|
||||
const # archive global flags flags
|
||||
ZIP_AFL_TORRENT* = 1'i32 ## torrent zipped
|
||||
|
||||
const # libzip error codes
|
||||
ZIP_ER_OK* = 0'i32 ## N No error
|
||||
ZIP_ER_MULTIDISK* = 1'i32 ## N Multi-disk zip archives not supported
|
||||
ZIP_ER_RENAME* = 2'i32 ## S Renaming temporary file failed
|
||||
ZIP_ER_CLOSE* = 3'i32 ## S Closing zip archive failed
|
||||
ZIP_ER_SEEK* = 4'i32 ## S Seek error
|
||||
ZIP_ER_READ* = 5'i32 ## S Read error
|
||||
ZIP_ER_WRITE* = 6'i32 ## S Write error
|
||||
ZIP_ER_CRC* = 7'i32 ## N CRC error
|
||||
ZIP_ER_ZIPCLOSED* = 8'i32 ## N Containing zip archive was closed
|
||||
ZIP_ER_NOENT* = 9'i32 ## N No such file
|
||||
ZIP_ER_EXISTS* = 10'i32 ## N File already exists
|
||||
ZIP_ER_OPEN* = 11'i32 ## S Can't open file
|
||||
ZIP_ER_TMPOPEN* = 12'i32 ## S Failure to create temporary file
|
||||
ZIP_ER_ZLIB* = 13'i32 ## Z Zlib error
|
||||
ZIP_ER_MEMORY* = 14'i32 ## N Malloc failure
|
||||
ZIP_ER_CHANGED* = 15'i32 ## N Entry has been changed
|
||||
ZIP_ER_COMPNOTSUPP* = 16'i32 ## N Compression method not supported
|
||||
ZIP_ER_EOF* = 17'i32 ## N Premature EOF
|
||||
ZIP_ER_INVAL* = 18'i32 ## N Invalid argument
|
||||
ZIP_ER_NOZIP* = 19'i32 ## N Not a zip archive
|
||||
ZIP_ER_INTERNAL* = 20'i32 ## N Internal error
|
||||
ZIP_ER_INCONS* = 21'i32 ## N Zip archive inconsistent
|
||||
ZIP_ER_REMOVE* = 22'i32 ## S Can't remove file
|
||||
ZIP_ER_DELETED* = 23'i32 ## N Entry has been deleted
|
||||
|
||||
const # type of system error value
|
||||
ZIP_ET_NONE* = 0'i32 ## sys_err unused
|
||||
ZIP_ET_SYS* = 1'i32 ## sys_err is errno
|
||||
ZIP_ET_ZLIB* = 2'i32 ## sys_err is zlib error code
|
||||
|
||||
const # compression methods
|
||||
ZIP_CM_DEFAULT* = -1'i32 ## better of deflate or store
|
||||
ZIP_CM_STORE* = 0'i32 ## stored (uncompressed)
|
||||
ZIP_CM_SHRINK* = 1'i32 ## shrunk
|
||||
ZIP_CM_REDUCE_1* = 2'i32 ## reduced with factor 1
|
||||
ZIP_CM_REDUCE_2* = 3'i32 ## reduced with factor 2
|
||||
ZIP_CM_REDUCE_3* = 4'i32 ## reduced with factor 3
|
||||
ZIP_CM_REDUCE_4* = 5'i32 ## reduced with factor 4
|
||||
ZIP_CM_IMPLODE* = 6'i32 ## imploded
|
||||
## 7 - Reserved for Tokenizing compression algorithm
|
||||
ZIP_CM_DEFLATE* = 8'i32 ## deflated
|
||||
ZIP_CM_DEFLATE64* = 9'i32 ## deflate64
|
||||
ZIP_CM_PKWARE_IMPLODE* = 10'i32 ## PKWARE imploding
|
||||
## 11 - Reserved by PKWARE
|
||||
ZIP_CM_BZIP2* = 12'i32 ## compressed using BZIP2 algorithm
|
||||
## 13 - Reserved by PKWARE
|
||||
ZIP_CM_LZMA* = 14'i32 ## LZMA (EFS)
|
||||
## 15-17 - Reserved by PKWARE
|
||||
ZIP_CM_TERSE* = 18'i32 ## compressed using IBM TERSE (new)
|
||||
ZIP_CM_LZ77* = 19'i32 ## IBM LZ77 z Architecture (PFS)
|
||||
ZIP_CM_WAVPACK* = 97'i32 ## WavPack compressed data
|
||||
ZIP_CM_PPMD* = 98'i32 ## PPMd version I, Rev 1
|
||||
|
||||
const # encryption methods
|
||||
ZIP_EM_NONE* = 0'i32 ## not encrypted
|
||||
ZIP_EM_TRAD_PKWARE* = 1'i32 ## traditional PKWARE encryption
|
||||
|
||||
const
|
||||
ZIP_EM_UNKNOWN* = 0x0000FFFF'i32 ## unknown algorithm
|
||||
|
||||
const
|
||||
ZIP_SOURCE_OPEN* = 0'i32 ## prepare for reading
|
||||
ZIP_SOURCE_READ* = 1'i32 ## read data
|
||||
ZIP_SOURCE_CLOSE* = 2'i32 ## reading is done
|
||||
ZIP_SOURCE_STAT* = 3'i32 ## get meta information
|
||||
ZIP_SOURCE_ERROR* = 4'i32 ## get error information
|
||||
constZIP_SOURCE_FREE* = 5'i32 ## cleanup and free resources
|
||||
ZIP_SOURCE_SUPPORTS* = 14'i32 ## check supported commands
|
||||
|
||||
proc zip_add*(para1: PZip, para2: cstring, para3: PZipSource): int32 {.cdecl,
|
||||
importc: "zip_add", mydll.}
|
||||
proc zip_add_dir*(para1: PZip, para2: cstring): int32 {.cdecl,
|
||||
importc: "zip_add_dir", mydll.}
|
||||
proc zip_close*(para1: PZip) {.cdecl, importc: "zip_close", mydll.}
|
||||
proc zip_delete*(para1: PZip, para2: int32): int32 {.cdecl, mydll,
|
||||
importc: "zip_delete".}
|
||||
proc zip_error_clear*(para1: PZip) {.cdecl, importc: "zip_error_clear", mydll.}
|
||||
proc zip_error_get*(para1: PZip, para2: ptr int32, para3: ptr int32) {.cdecl,
|
||||
importc: "zip_error_get", mydll.}
|
||||
proc zip_error_get_sys_type*(para1: int32): int32 {.cdecl, mydll,
|
||||
importc: "zip_error_get_sys_type".}
|
||||
proc zip_error_to_str*(para1: cstring, para2: int, para3: int32,
|
||||
para4: int32): int32 {.cdecl, mydll,
|
||||
importc: "zip_error_to_str".}
|
||||
proc zip_fclose*(para1: PZipFile) {.cdecl, mydll,
|
||||
importc: "zip_fclose".}
|
||||
proc zip_file_error_clear*(para1: PZipFile) {.cdecl, mydll,
|
||||
importc: "zip_file_error_clear".}
|
||||
proc zip_file_error_get*(para1: PZipFile, para2: ptr int32, para3: ptr int32) {.
|
||||
cdecl, mydll, importc: "zip_file_error_get".}
|
||||
proc zip_file_strerror*(para1: PZipFile): cstring {.cdecl, mydll,
|
||||
importc: "zip_file_strerror".}
|
||||
proc zip_fopen*(para1: PZip, para2: cstring, para3: int32): PZipFile {.cdecl,
|
||||
mydll, importc: "zip_fopen".}
|
||||
proc zip_fopen_index*(para1: PZip, para2: int32, para3: int32): PZipFile {.
|
||||
cdecl, mydll, importc: "zip_fopen_index".}
|
||||
proc zip_fread*(para1: PZipFile, para2: pointer, para3: int): int {.
|
||||
cdecl, mydll, importc: "zip_fread".}
|
||||
proc zip_get_archive_comment*(para1: PZip, para2: ptr int32, para3: int32): cstring {.
|
||||
cdecl, mydll, importc: "zip_get_archive_comment".}
|
||||
proc zip_get_archive_flag*(para1: PZip, para2: int32, para3: int32): int32 {.
|
||||
cdecl, mydll, importc: "zip_get_archive_flag".}
|
||||
proc zip_get_file_comment*(para1: PZip, para2: int32, para3: ptr int32,
|
||||
para4: int32): cstring {.cdecl, mydll,
|
||||
importc: "zip_get_file_comment".}
|
||||
proc zip_get_name*(para1: PZip, para2: int32, para3: int32): cstring {.cdecl,
|
||||
mydll, importc: "zip_get_name".}
|
||||
proc zip_get_num_files*(para1: PZip): int32 {.cdecl,
|
||||
mydll, importc: "zip_get_num_files".}
|
||||
proc zip_name_locate*(para1: PZip, para2: cstring, para3: int32): int32 {.cdecl,
|
||||
mydll, importc: "zip_name_locate".}
|
||||
proc zip_open*(para1: cstring, para2: int32, para3: ptr int32): PZip {.cdecl,
|
||||
mydll, importc: "zip_open".}
|
||||
proc zip_rename*(para1: PZip, para2: int32, para3: cstring): int32 {.cdecl,
|
||||
mydll, importc: "zip_rename".}
|
||||
proc zip_replace*(para1: PZip, para2: int32, para3: PZipSource): int32 {.cdecl,
|
||||
mydll, importc: "zip_replace".}
|
||||
proc zip_set_archive_comment*(para1: PZip, para2: cstring, para3: int32): int32 {.
|
||||
cdecl, mydll, importc: "zip_set_archive_comment".}
|
||||
proc zip_set_archive_flag*(para1: PZip, para2: int32, para3: int32): int32 {.
|
||||
cdecl, mydll, importc: "zip_set_archive_flag".}
|
||||
proc zip_set_file_comment*(para1: PZip, para2: int32, para3: cstring,
|
||||
para4: int32): int32 {.cdecl, mydll,
|
||||
importc: "zip_set_file_comment".}
|
||||
proc zip_source_buffer*(para1: PZip, para2: pointer, para3: int, para4: int32): PZipSource {.
|
||||
cdecl, mydll, importc: "zip_source_buffer".}
|
||||
proc zip_source_file*(para1: PZip, para2: cstring, para3: int, para4: int): PZipSource {.
|
||||
cdecl, mydll, importc: "zip_source_file".}
|
||||
proc zip_source_filep*(para1: PZip, para2: File, para3: int, para4: int): PZipSource {.
|
||||
cdecl, mydll, importc: "zip_source_filep".}
|
||||
proc zip_source_free*(para1: PZipSource) {.cdecl, mydll,
|
||||
importc: "zip_source_free".}
|
||||
proc zip_source_function*(para1: PZip, para2: ZipSourceCallback,
|
||||
para3: pointer): PZipSource {.cdecl, mydll,
|
||||
importc: "zip_source_function".}
|
||||
proc zip_source_zip*(para1: PZip, para2: PZip, para3: int32, para4: int32,
|
||||
para5: int, para6: int): PZipSource {.cdecl, mydll,
|
||||
importc: "zip_source_zip".}
|
||||
proc zip_stat*(para1: PZip, para2: cstring, para3: int32, para4: PZipStat): int32 {.
|
||||
cdecl, mydll, importc: "zip_stat".}
|
||||
proc zip_stat_index*(para1: PZip, para2: int32, para3: int32, para4: PZipStat): int32 {.
|
||||
cdecl, mydll, importc: "zip_stat_index".}
|
||||
proc zip_stat_init*(para1: PZipStat) {.cdecl, mydll, importc: "zip_stat_init".}
|
||||
proc zip_strerror*(para1: PZip): cstring {.cdecl, mydll, importc: "zip_strerror".}
|
||||
proc zip_unchange*(para1: PZip, para2: int32): int32 {.cdecl, mydll,
|
||||
importc: "zip_unchange".}
|
||||
proc zip_unchange_all*(para1: PZip): int32 {.cdecl, mydll,
|
||||
importc: "zip_unchange_all".}
|
||||
proc zip_unchange_archive*(para1: PZip): int32 {.cdecl, mydll,
|
||||
importc: "zip_unchange_archive".}
|
||||
4193
tests/deps/zip-0.2.1/zip/private/libzip_all.c
Normal file
4193
tests/deps/zip-0.2.1/zip/private/libzip_all.c
Normal file
File diff suppressed because it is too large
Load diff
193
tests/deps/zip-0.2.1/zip/zipfiles.nim
Normal file
193
tests/deps/zip-0.2.1/zip/zipfiles.nim
Normal file
|
|
@ -0,0 +1,193 @@
|
|||
#
|
||||
#
|
||||
# Nim's Runtime Library
|
||||
# (c) Copyright 2012 Andreas Rumpf
|
||||
#
|
||||
# See the file "copying.txt", included in this
|
||||
# distribution, for details about the copyright.
|
||||
#
|
||||
|
||||
## This module implements a zip archive creator/reader/modifier.
|
||||
|
||||
import
|
||||
streams, libzip, times, os, strutils
|
||||
|
||||
const BufSize = 8 * 1024
|
||||
|
||||
type
|
||||
ZipArchive* = object of RootObj ## represents a zip archive
|
||||
mode: FileMode
|
||||
w: PZip
|
||||
{.deprecated: [TZipArchive: ZipArchive].}
|
||||
|
||||
proc zipError(z: var ZipArchive) =
|
||||
var e: ref IOError
|
||||
new(e)
|
||||
e.msg = $zip_strerror(z.w)
|
||||
raise e
|
||||
|
||||
proc open*(z: var ZipArchive, filename: string, mode: FileMode = fmRead): bool =
|
||||
## Opens a zip file for reading, writing or appending. All file modes are
|
||||
## supported. Returns true iff successful, false otherwise.
|
||||
var err, flags: int32
|
||||
case mode
|
||||
of fmRead, fmReadWriteExisting, fmAppend: flags = 0
|
||||
of fmWrite:
|
||||
if existsFile(filename): removeFile(filename)
|
||||
flags = ZIP_CREATE or ZIP_EXCL
|
||||
of fmReadWrite: flags = ZIP_CREATE
|
||||
z.w = zip_open(filename, flags, addr(err))
|
||||
z.mode = mode
|
||||
result = z.w != nil
|
||||
|
||||
proc close*(z: var ZipArchive) =
|
||||
## Closes a zip file.
|
||||
zip_close(z.w)
|
||||
|
||||
proc createDir*(z: var ZipArchive, dir: string) =
|
||||
## Creates a directory within the `z` archive. This does not fail if the
|
||||
## directory already exists. Note that for adding a file like
|
||||
## ``"path1/path2/filename"`` it is not necessary
|
||||
## to create the ``"path/path2"`` subdirectories - it will be done
|
||||
## automatically by ``addFile``.
|
||||
assert(z.mode != fmRead)
|
||||
discard zip_add_dir(z.w, dir)
|
||||
zip_error_clear(z.w)
|
||||
|
||||
proc addFile*(z: var ZipArchive, dest, src: string) =
|
||||
## Adds the file `src` to the archive `z` with the name `dest`. `dest`
|
||||
## may contain a path that will be created.
|
||||
assert(z.mode != fmRead)
|
||||
if not fileExists(src):
|
||||
raise newException(IOError, "File '" & src & "' does not exist")
|
||||
var zipsrc = zip_source_file(z.w, src, 0, -1)
|
||||
if zipsrc == nil:
|
||||
#echo("Dest: " & dest)
|
||||
#echo("Src: " & src)
|
||||
zipError(z)
|
||||
if zip_add(z.w, dest, zipsrc) < 0'i32:
|
||||
zip_source_free(zipsrc)
|
||||
zipError(z)
|
||||
|
||||
proc addFile*(z: var ZipArchive, file: string) =
|
||||
## A shortcut for ``addFile(z, file, file)``, i.e. the name of the source is
|
||||
## the name of the destination.
|
||||
addFile(z, file, file)
|
||||
|
||||
proc mySourceCallback(state, data: pointer, len: int,
|
||||
cmd: ZipSourceCmd): int {.cdecl.} =
|
||||
var src = cast[Stream](state)
|
||||
case cmd
|
||||
of ZIP_SOURCE_OPEN:
|
||||
if src.setPositionImpl != nil: setPosition(src, 0) # reset
|
||||
of ZIP_SOURCE_READ:
|
||||
result = readData(src, data, len)
|
||||
of ZIP_SOURCE_CLOSE: close(src)
|
||||
of ZIP_SOURCE_STAT:
|
||||
var stat = cast[PZipStat](data)
|
||||
zip_stat_init(stat)
|
||||
stat.size = high(int32)-1 # we don't know the size
|
||||
stat.mtime = getTime()
|
||||
result = sizeof(ZipStat)
|
||||
of ZIP_SOURCE_ERROR:
|
||||
var err = cast[ptr array[0..1, cint]](data)
|
||||
err[0] = ZIP_ER_INTERNAL
|
||||
err[1] = 0
|
||||
result = 2*sizeof(cint)
|
||||
of constZIP_SOURCE_FREE: GC_unref(src)
|
||||
of ZIP_SOURCE_SUPPORTS:
|
||||
# By default a read-only source is supported, which suits us.
|
||||
result = -1
|
||||
else:
|
||||
# An unknown command, failing
|
||||
result = -1
|
||||
|
||||
proc addFile*(z: var ZipArchive, dest: string, src: Stream) =
|
||||
## Adds a file named with `dest` to the archive `z`. `dest`
|
||||
## may contain a path. The file's content is read from the `src` stream.
|
||||
assert(z.mode != fmRead)
|
||||
GC_ref(src)
|
||||
var zipsrc = zip_source_function(z.w, mySourceCallback, cast[pointer](src))
|
||||
if zipsrc == nil: zipError(z)
|
||||
if zip_add(z.w, dest, zipsrc) < 0'i32:
|
||||
zip_source_free(zipsrc)
|
||||
zipError(z)
|
||||
|
||||
# -------------- zip file stream ---------------------------------------------
|
||||
|
||||
type
|
||||
TZipFileStream = object of StreamObj
|
||||
f: PZipFile
|
||||
atEnd: bool
|
||||
|
||||
PZipFileStream* =
|
||||
ref TZipFileStream ## a reader stream of a file within a zip archive
|
||||
|
||||
proc fsClose(s: Stream) = zip_fclose(PZipFileStream(s).f)
|
||||
proc fsAtEnd(s: Stream): bool = PZipFileStream(s).atEnd
|
||||
proc fsReadData(s: Stream, buffer: pointer, bufLen: int): int =
|
||||
result = zip_fread(PZipFileStream(s).f, buffer, bufLen)
|
||||
if result == 0:
|
||||
PZipFileStream(s).atEnd = true
|
||||
|
||||
proc newZipFileStream(f: PZipFile): PZipFileStream =
|
||||
new(result)
|
||||
result.f = f
|
||||
result.atEnd = false
|
||||
result.closeImpl = fsClose
|
||||
result.readDataImpl = fsReadData
|
||||
result.atEndImpl = fsAtEnd
|
||||
# other methods are nil!
|
||||
|
||||
# ----------------------------------------------------------------------------
|
||||
|
||||
proc getStream*(z: var ZipArchive, filename: string): PZipFileStream =
|
||||
## returns a stream that can be used to read the file named `filename`
|
||||
## from the archive `z`. Returns nil in case of an error.
|
||||
## The returned stream does not support the `setPosition`, `getPosition`,
|
||||
## `writeData` or `atEnd` methods.
|
||||
var x = zip_fopen(z.w, filename, 0'i32)
|
||||
if x != nil: result = newZipFileStream(x)
|
||||
|
||||
iterator walkFiles*(z: var ZipArchive): string =
|
||||
## walks over all files in the archive `z` and returns the filename
|
||||
## (including the path).
|
||||
var i = 0'i32
|
||||
var num = zip_get_num_files(z.w)
|
||||
while i < num:
|
||||
yield $zip_get_name(z.w, i, 0'i32)
|
||||
inc(i)
|
||||
|
||||
proc extractFile*(z: var ZipArchive, srcFile: string, dest: Stream) =
|
||||
## extracts a file from the zip archive `z` to the destination stream.
|
||||
var buf: array[BufSize, byte]
|
||||
var strm = getStream(z, srcFile)
|
||||
while true:
|
||||
let bytesRead = strm.readData(addr(buf[0]), buf.len)
|
||||
if bytesRead <= 0: break
|
||||
dest.writeData(addr(buf[0]), bytesRead)
|
||||
|
||||
dest.flush()
|
||||
strm.close()
|
||||
|
||||
proc extractFile*(z: var ZipArchive, srcFile: string, dest: string) =
|
||||
## extracts a file from the zip archive `z` to the destination filename.
|
||||
var file = newFileStream(dest, fmWrite)
|
||||
if file.isNil:
|
||||
raise newException(IOError, "Failed to create output file: " & dest)
|
||||
extractFile(z, srcFile, file)
|
||||
file.close()
|
||||
|
||||
proc extractAll*(z: var ZipArchive, dest: string) =
|
||||
## extracts all files from archive `z` to the destination directory.
|
||||
createDir(dest)
|
||||
for file in walkFiles(z):
|
||||
if file.contains("/"):
|
||||
createDir(dest / file[0..file.rfind("/")])
|
||||
extractFile(z, file, dest / file)
|
||||
|
||||
when not defined(testing) and isMainModule:
|
||||
var zip: ZipArchive
|
||||
if not zip.open("nim-0.11.0.zip"):
|
||||
raise newException(IOError, "opening zip failed")
|
||||
zip.extractAll("test")
|
||||
425
tests/deps/zip-0.2.1/zip/zlib.nim
Normal file
425
tests/deps/zip-0.2.1/zip/zlib.nim
Normal file
|
|
@ -0,0 +1,425 @@
|
|||
# Converted from Pascal
|
||||
|
||||
## Interface to the zlib http://www.zlib.net/ compression library.
|
||||
|
||||
when defined(windows):
|
||||
const libz = "zlib1.dll"
|
||||
elif defined(macosx):
|
||||
const libz = "libz.dylib"
|
||||
else:
|
||||
const libz = "libz.so.1"
|
||||
|
||||
type
|
||||
Uint* = int32
|
||||
Ulong* = int
|
||||
Ulongf* = int
|
||||
Pulongf* = ptr Ulongf
|
||||
ZOffT* = int32
|
||||
Pbyte* = cstring
|
||||
Pbytef* = cstring
|
||||
Allocfunc* = proc (p: pointer, items: Uint, size: Uint): pointer{.cdecl.}
|
||||
FreeFunc* = proc (p: pointer, address: pointer){.cdecl.}
|
||||
InternalState*{.final, pure.} = object
|
||||
PInternalState* = ptr InternalState
|
||||
ZStream*{.final, pure.} = object
|
||||
nextIn*: Pbytef
|
||||
availIn*: Uint
|
||||
totalIn*: Ulong
|
||||
nextOut*: Pbytef
|
||||
availOut*: Uint
|
||||
totalOut*: Ulong
|
||||
msg*: Pbytef
|
||||
state*: PInternalState
|
||||
zalloc*: Allocfunc
|
||||
zfree*: FreeFunc
|
||||
opaque*: pointer
|
||||
dataType*: int32
|
||||
adler*: Ulong
|
||||
reserved*: Ulong
|
||||
|
||||
ZStreamRec* = ZStream
|
||||
PZstream* = ptr ZStream
|
||||
GzFile* = pointer
|
||||
ZStreamHeader* = enum
|
||||
DETECT_STREAM,
|
||||
RAW_DEFLATE,
|
||||
ZLIB_STREAM,
|
||||
GZIP_STREAM
|
||||
|
||||
ZlibStreamError* = object of Exception
|
||||
|
||||
{.deprecated: [TInternalState: InternalState, TAllocfunc: Allocfunc,
|
||||
TFreeFunc: FreeFunc, TZStream: ZStream, TZStreamRec: ZStreamRec].}
|
||||
|
||||
const
|
||||
Z_NO_FLUSH* = 0
|
||||
Z_PARTIAL_FLUSH* = 1
|
||||
Z_SYNC_FLUSH* = 2
|
||||
Z_FULL_FLUSH* = 3
|
||||
Z_FINISH* = 4
|
||||
Z_OK* = 0
|
||||
Z_STREAM_END* = 1
|
||||
Z_NEED_DICT* = 2
|
||||
Z_ERRNO* = -1
|
||||
Z_STREAM_ERROR* = -2
|
||||
Z_DATA_ERROR* = -3
|
||||
Z_MEM_ERROR* = -4
|
||||
Z_BUF_ERROR* = -5
|
||||
Z_VERSION_ERROR* = -6
|
||||
Z_NO_COMPRESSION* = 0
|
||||
Z_BEST_SPEED* = 1
|
||||
Z_BEST_COMPRESSION* = 9
|
||||
Z_DEFAULT_COMPRESSION* = -1
|
||||
Z_FILTERED* = 1
|
||||
Z_HUFFMAN_ONLY* = 2
|
||||
Z_DEFAULT_STRATEGY* = 0
|
||||
Z_BINARY* = 0
|
||||
Z_ASCII* = 1
|
||||
Z_UNKNOWN* = 2
|
||||
Z_DEFLATED* = 8
|
||||
Z_NULL* = 0
|
||||
Z_MEM_LEVEL* = 8
|
||||
MAX_WBITS* = 15
|
||||
|
||||
proc zlibVersion*(): cstring{.cdecl, dynlib: libz, importc: "zlibVersion".}
|
||||
proc deflate*(strm: var ZStream, flush: int32): int32{.cdecl, dynlib: libz,
|
||||
importc: "deflate".}
|
||||
proc deflateEnd*(strm: var ZStream): int32{.cdecl, dynlib: libz,
|
||||
importc: "deflateEnd".}
|
||||
proc inflate*(strm: var ZStream, flush: int32): int32{.cdecl, dynlib: libz,
|
||||
importc: "inflate".}
|
||||
proc inflateEnd*(strm: var ZStream): int32{.cdecl, dynlib: libz,
|
||||
importc: "inflateEnd".}
|
||||
proc deflateSetDictionary*(strm: var ZStream, dictionary: Pbytef,
|
||||
dictLength: Uint): int32{.cdecl, dynlib: libz,
|
||||
importc: "deflateSetDictionary".}
|
||||
proc deflateCopy*(dest, source: var ZStream): int32{.cdecl, dynlib: libz,
|
||||
importc: "deflateCopy".}
|
||||
proc deflateReset*(strm: var ZStream): int32{.cdecl, dynlib: libz,
|
||||
importc: "deflateReset".}
|
||||
proc deflateParams*(strm: var ZStream, level: int32, strategy: int32): int32{.
|
||||
cdecl, dynlib: libz, importc: "deflateParams".}
|
||||
proc inflateSetDictionary*(strm: var ZStream, dictionary: Pbytef,
|
||||
dictLength: Uint): int32{.cdecl, dynlib: libz,
|
||||
importc: "inflateSetDictionary".}
|
||||
proc inflateSync*(strm: var ZStream): int32{.cdecl, dynlib: libz,
|
||||
importc: "inflateSync".}
|
||||
proc inflateReset*(strm: var ZStream): int32{.cdecl, dynlib: libz,
|
||||
importc: "inflateReset".}
|
||||
proc compress*(dest: Pbytef, destLen: Pulongf, source: Pbytef, sourceLen: Ulong): cint{.
|
||||
cdecl, dynlib: libz, importc: "compress".}
|
||||
proc compress2*(dest: Pbytef, destLen: Pulongf, source: Pbytef,
|
||||
sourceLen: Ulong, level: cint): cint{.cdecl, dynlib: libz,
|
||||
importc: "compress2".}
|
||||
proc uncompress*(dest: Pbytef, destLen: Pulongf, source: Pbytef,
|
||||
sourceLen: Ulong): cint{.cdecl, dynlib: libz,
|
||||
importc: "uncompress".}
|
||||
proc compressBound*(sourceLen: Ulong): Ulong {.cdecl, dynlib: libz, importc.}
|
||||
proc gzopen*(path: cstring, mode: cstring): GzFile{.cdecl, dynlib: libz,
|
||||
importc: "gzopen".}
|
||||
proc gzdopen*(fd: int32, mode: cstring): GzFile{.cdecl, dynlib: libz,
|
||||
importc: "gzdopen".}
|
||||
proc gzsetparams*(thefile: GzFile, level: int32, strategy: int32): int32{.cdecl,
|
||||
dynlib: libz, importc: "gzsetparams".}
|
||||
proc gzread*(thefile: GzFile, buf: pointer, length: int): int32{.cdecl,
|
||||
dynlib: libz, importc: "gzread".}
|
||||
proc gzwrite*(thefile: GzFile, buf: pointer, length: int): int32{.cdecl,
|
||||
dynlib: libz, importc: "gzwrite".}
|
||||
proc gzprintf*(thefile: GzFile, format: Pbytef): int32{.varargs, cdecl,
|
||||
dynlib: libz, importc: "gzprintf".}
|
||||
proc gzputs*(thefile: GzFile, s: Pbytef): int32{.cdecl, dynlib: libz,
|
||||
importc: "gzputs".}
|
||||
proc gzgets*(thefile: GzFile, buf: Pbytef, length: int32): Pbytef{.cdecl,
|
||||
dynlib: libz, importc: "gzgets".}
|
||||
proc gzputc*(thefile: GzFile, c: char): char{.cdecl, dynlib: libz,
|
||||
importc: "gzputc".}
|
||||
proc gzgetc*(thefile: GzFile): char{.cdecl, dynlib: libz, importc: "gzgetc".}
|
||||
proc gzflush*(thefile: GzFile, flush: int32): int32{.cdecl, dynlib: libz,
|
||||
importc: "gzflush".}
|
||||
proc gzseek*(thefile: GzFile, offset: ZOffT, whence: int32): ZOffT{.cdecl,
|
||||
dynlib: libz, importc: "gzseek".}
|
||||
proc gzrewind*(thefile: GzFile): int32{.cdecl, dynlib: libz, importc: "gzrewind".}
|
||||
proc gztell*(thefile: GzFile): ZOffT{.cdecl, dynlib: libz, importc: "gztell".}
|
||||
proc gzeof*(thefile: GzFile): int {.cdecl, dynlib: libz, importc: "gzeof".}
|
||||
proc gzclose*(thefile: GzFile): int32{.cdecl, dynlib: libz, importc: "gzclose".}
|
||||
proc gzerror*(thefile: GzFile, errnum: var int32): Pbytef{.cdecl, dynlib: libz,
|
||||
importc: "gzerror".}
|
||||
proc adler32*(adler: Ulong, buf: Pbytef, length: Uint): Ulong{.cdecl,
|
||||
dynlib: libz, importc: "adler32".}
|
||||
## **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,
|
||||
importc: "crc32".}
|
||||
proc deflateInitu*(strm: var ZStream, level: int32, version: cstring,
|
||||
streamSize: int32): int32{.cdecl, dynlib: libz,
|
||||
importc: "deflateInit_".}
|
||||
proc inflateInitu*(strm: var ZStream, version: cstring,
|
||||
streamSize: int32): int32 {.
|
||||
cdecl, dynlib: libz, importc: "inflateInit_".}
|
||||
proc deflateInit*(strm: var ZStream, level: int32): int32
|
||||
proc inflateInit*(strm: var ZStream): int32
|
||||
proc deflateInit2u*(strm: var ZStream, level: int32, `method`: int32,
|
||||
windowBits: int32, memLevel: int32, strategy: int32,
|
||||
version: cstring, streamSize: int32): int32 {.cdecl,
|
||||
dynlib: libz, importc: "deflateInit2_".}
|
||||
proc inflateInit2u*(strm: var ZStream, windowBits: int32, version: cstring,
|
||||
streamSize: int32): int32{.cdecl, dynlib: libz,
|
||||
importc: "inflateInit2_".}
|
||||
proc deflateInit2*(strm: var ZStream,
|
||||
level, `method`, windowBits, memLevel,
|
||||
strategy: int32): int32
|
||||
proc inflateInit2*(strm: var ZStream, windowBits: int32): int32
|
||||
proc zError*(err: int32): cstring{.cdecl, dynlib: libz, importc: "zError".}
|
||||
proc inflateSyncPoint*(z: PZstream): int32{.cdecl, dynlib: libz,
|
||||
importc: "inflateSyncPoint".}
|
||||
proc getCrcTable*(): pointer{.cdecl, dynlib: libz, importc: "get_crc_table".}
|
||||
|
||||
proc deflateBound*(strm: var ZStream, sourceLen: ULong): ULong {.cdecl,
|
||||
dynlib: libz, importc: "deflateBound".}
|
||||
|
||||
proc deflateInit(strm: var ZStream, level: int32): int32 =
|
||||
result = deflateInitu(strm, level, zlibVersion(), sizeof(ZStream).cint)
|
||||
|
||||
proc inflateInit(strm: var ZStream): int32 =
|
||||
result = inflateInitu(strm, zlibVersion(), sizeof(ZStream).cint)
|
||||
|
||||
proc deflateInit2(strm: var ZStream,
|
||||
level, `method`, windowBits, memLevel,
|
||||
strategy: int32): int32 =
|
||||
result = deflateInit2u(strm, level, `method`, windowBits, memLevel,
|
||||
strategy, zlibVersion(), sizeof(ZStream).cint)
|
||||
|
||||
proc inflateInit2(strm: var ZStream, windowBits: int32): int32 =
|
||||
result = inflateInit2u(strm, windowBits, zlibVersion(),
|
||||
sizeof(ZStream).cint)
|
||||
|
||||
proc zlibAllocMem*(appData: pointer, items, size: int): pointer {.cdecl.} =
|
||||
result = alloc(items * size)
|
||||
|
||||
proc zlibFreeMem*(appData, `block`: pointer) {.cdecl.} =
|
||||
dealloc(`block`)
|
||||
|
||||
|
||||
proc compress*(sourceBuf: cstring; sourceLen: int; level=Z_DEFAULT_COMPRESSION; stream=GZIP_STREAM): string =
|
||||
## Given a cstring, returns its deflated version with an optional header.
|
||||
##
|
||||
## Valid argument for ``stream`` are
|
||||
## - ``ZLIB_STREAM`` - add a zlib header and footer.
|
||||
## - ``GZIP_STREAM`` - add a basic gzip header and footer.
|
||||
## - ``RAW_DEFLATE`` - no header is generated.
|
||||
##
|
||||
## Passing a nil cstring will crash this proc in release mode and assert in
|
||||
## debug mode.
|
||||
##
|
||||
## Compression level can be set with ``level`` argument. Currently
|
||||
## ``Z_DEFAULT_COMPRESSION`` is 6.
|
||||
##
|
||||
## Returns "" on failure.
|
||||
assert(not sourceBuf.isNil)
|
||||
assert(sourceLen >= 0)
|
||||
|
||||
var z: ZStream
|
||||
var windowBits = MAX_WBITS
|
||||
case (stream)
|
||||
of RAW_DEFLATE: windowBits = -MAX_WBITS
|
||||
of GZIP_STREAM: windowBits = MAX_WBITS + 16
|
||||
of ZLIB_STREAM, DETECT_STREAM:
|
||||
discard # DETECT_STREAM defaults to ZLIB_STREAM
|
||||
|
||||
var status = deflateInit2(z, level.int32, Z_DEFLATED.int32,
|
||||
windowBits.int32, Z_MEM_LEVEL.int32,
|
||||
Z_DEFAULT_STRATEGY.int32)
|
||||
case status
|
||||
of Z_OK: discard
|
||||
of Z_MEM_ERROR: raise newException(OutOfMemError, "")
|
||||
of Z_STREAM_ERROR: raise newException(ZlibStreamError, "invalid zlib stream parameter!")
|
||||
of Z_VERSION_ERROR: raise newException(ZlibStreamError, "zlib version mismatch!")
|
||||
else: raise newException(ZlibStreamError, "Unkown error(" & $status & ") : " & $z.msg)
|
||||
|
||||
let space = deflateBound(z, sourceLen)
|
||||
var compressed = newStringOfCap(space)
|
||||
z.next_in = sourceBuf
|
||||
z.avail_in = sourceLen.Uint
|
||||
z.next_out = addr(compressed[0])
|
||||
z.avail_out = space.Uint
|
||||
|
||||
status = deflate(z, Z_FINISH)
|
||||
if status != Z_STREAM_END:
|
||||
discard deflateEnd(z) # cleanup allocated ressources
|
||||
raise newException(ZlibStreamError, "Invalid stream state(" & $status & ") : " & $z.msg)
|
||||
|
||||
status = deflateEnd(z)
|
||||
if status != Z_OK: # cleanup allocated ressources
|
||||
raise newException(ZlibStreamError, "Invalid stream state(" & $status & ") : " & $z.msg)
|
||||
|
||||
compressed.setLen(z.total_out)
|
||||
swap(result, compressed)
|
||||
|
||||
proc compress*(input: string; level=Z_DEFAULT_COMPRESSION; stream=GZIP_STREAM): string =
|
||||
## Given a string, returns its deflated version with an optional header.
|
||||
##
|
||||
## Valid arguments for ``stream`` are
|
||||
## - ``ZLIB_STREAM`` - add a zlib header and footer.
|
||||
## - ``GZIP_STREAM`` - add a basic gzip header and footer.
|
||||
## - ``RAW_DEFLATE`` - no header is generated.
|
||||
##
|
||||
## Compression level can be set with ``level`` argument. Currently
|
||||
## ``Z_DEFAULT_COMPRESSION`` is 6.
|
||||
##
|
||||
## Returns "" on failure.
|
||||
result = compress(input, input.len, level, stream)
|
||||
|
||||
proc uncompress*(sourceBuf: cstring, sourceLen: Natural; stream=DETECT_STREAM): string =
|
||||
## Given a deflated buffer returns its inflated content as a string.
|
||||
##
|
||||
## Valid arguments for ``stream`` are
|
||||
## - ``DETECT_STREAM`` - detect if zlib or gzip header is present
|
||||
## and decompress stream. Fail on raw deflate stream.
|
||||
## - ``ZLIB_STREAM`` - decompress a zlib stream.
|
||||
## - ``GZIP_STREAM`` - decompress a gzip stream.
|
||||
## - ``RAW_DEFLATE`` - decompress a raw deflate stream.
|
||||
##
|
||||
## Passing a nil cstring will crash this proc in release mode and assert in
|
||||
## debug mode.
|
||||
##
|
||||
## Returns "" on problems. Failure is a very loose concept, it could be you
|
||||
## passing a non deflated string, or it could mean not having enough memory
|
||||
## for the inflated version.
|
||||
##
|
||||
## The uncompression algorithm is based on http://zlib.net/zpipe.c.
|
||||
assert(not sourceBuf.isNil)
|
||||
assert(sourceLen >= 0)
|
||||
var z: ZStream
|
||||
var decompressed: string = ""
|
||||
var sbytes = 0
|
||||
var wbytes = 0
|
||||
## allocate inflate state
|
||||
|
||||
z.availIn = 0
|
||||
var wbits = case (stream)
|
||||
of RAW_DEFLATE: -MAX_WBITS
|
||||
of ZLIB_STREAM: MAX_WBITS
|
||||
of GZIP_STREAM: MAX_WBITS + 16
|
||||
of DETECT_STREAM: MAX_WBITS + 32
|
||||
|
||||
var status = inflateInit2(z, wbits.int32)
|
||||
|
||||
case status
|
||||
of Z_OK: discard
|
||||
of Z_MEM_ERROR: raise newException(OutOfMemError, "")
|
||||
of Z_STREAM_ERROR: raise newException(ZlibStreamError, "invalid zlib stream parameter!")
|
||||
of Z_VERSION_ERROR: raise newException(ZlibStreamError, "zlib version mismatch!")
|
||||
else: raise newException(ZlibStreamError, "Unkown error(" & $status & ") : " & $z.msg)
|
||||
|
||||
# run loop until all input is consumed.
|
||||
# handle concatenated deflated stream with header.
|
||||
while true:
|
||||
z.availIn = (sourceLen - sbytes).int32
|
||||
|
||||
# no more input available
|
||||
if (sourceLen - sbytes) <= 0: break
|
||||
z.nextIn = sourceBuf[sbytes].unsafeaddr
|
||||
|
||||
# run inflate() on available input until output buffer is full
|
||||
while true:
|
||||
# if written bytes >= output size : resize output
|
||||
if wbytes >= decompressed.len:
|
||||
let cur_outlen = decompressed.len
|
||||
let new_outlen = if decompressed.len == 0: sourceLen*2 else: decompressed.len*2
|
||||
if new_outlen < cur_outlen: # unsigned integer overflow, buffer too large
|
||||
discard inflateEnd(z);
|
||||
raise newException(OverflowError, "zlib stream decompressed size is too large! (size > " & $int.high & ")")
|
||||
|
||||
decompressed.setLen(new_outlen)
|
||||
|
||||
# available space for decompression
|
||||
let space = decompressed.len - wbytes
|
||||
z.availOut = space.Uint
|
||||
z.nextOut = decompressed[wbytes].addr
|
||||
|
||||
status = inflate(z, Z_NO_FLUSH)
|
||||
if status.int8 notin {Z_OK.int8, Z_STREAM_END.int8, Z_BUF_ERROR.int8}:
|
||||
discard inflateEnd(z)
|
||||
case status
|
||||
of Z_MEM_ERROR: raise newException(OutOfMemError, "")
|
||||
of Z_DATA_ERROR: raise newException(ZlibStreamError, "invalid zlib stream parameter!")
|
||||
else: raise newException(ZlibStreamError, "Unkown error(" & $status & ") : " & $z.msg)
|
||||
|
||||
# add written bytes, if any.
|
||||
wbytes += space - z.availOut.int
|
||||
|
||||
# may need more input
|
||||
if not (z.availOut == 0): break
|
||||
|
||||
# inflate() says stream is done
|
||||
if (status == Z_STREAM_END):
|
||||
# may have another stream concatenated
|
||||
if z.availIn != 0:
|
||||
sbytes = sourceLen - z.availIn # add consumed bytes
|
||||
if inflateReset(z) != Z_OK: # reset zlib struct and try again
|
||||
raise newException(ZlibStreamError, "Invalid stream state(" & $status & ") : " & $z.msg)
|
||||
else:
|
||||
break # end of decompression
|
||||
|
||||
# clean up and don't care about any error
|
||||
discard inflateEnd(z)
|
||||
|
||||
if status != Z_STREAM_END:
|
||||
raise newException(ZlibStreamError, "Invalid stream state(" & $status & ") : " & $z.msg)
|
||||
|
||||
decompressed.setLen(wbytes)
|
||||
swap(result, decompressed)
|
||||
|
||||
|
||||
proc uncompress*(sourceBuf: string; stream=DETECT_STREAM): string =
|
||||
## Given a GZIP-ed string return its inflated content.
|
||||
##
|
||||
## Valid arguments for ``stream`` are
|
||||
## - ``DETECT_STREAM`` - detect if zlib or gzip header is present
|
||||
## and decompress stream. Fail on raw deflate stream.
|
||||
## - ``ZLIB_STREAM`` - decompress a zlib stream.
|
||||
## - ``GZIP_STREAM`` - decompress a gzip stream.
|
||||
## - ``RAW_DEFLATE`` - decompress a raw deflate stream.
|
||||
##
|
||||
## Returns "" on failure.
|
||||
result = uncompress(sourceBuf, sourceBuf.len, stream)
|
||||
|
||||
|
||||
|
||||
proc deflate*(buffer: var string; level=Z_DEFAULT_COMPRESSION; stream=GZIP_STREAM): bool {.discardable.} =
|
||||
## Convenience proc which deflates a string and insert an optional header/footer.
|
||||
##
|
||||
## Valid arguments for ``stream`` are
|
||||
## - ``ZLIB_STREAM`` - add a zlib header and footer.
|
||||
## - ``GZIP_STREAM`` - add a basic gzip header and footer.
|
||||
## - ``RAW_DEFLATE`` - no header is generated.
|
||||
##
|
||||
## Compression level can be set with ``level`` argument. Currently
|
||||
## ``Z_DEFAULT_COMPRESSION`` is 6.
|
||||
##
|
||||
## Returns true if `buffer` was successfully deflated otherwise the buffer is untouched.
|
||||
|
||||
var temp = compress(addr(buffer[0]), buffer.len, level, stream)
|
||||
if temp.len != 0:
|
||||
swap(buffer, temp)
|
||||
result = true
|
||||
|
||||
proc inflate*(buffer: var string; stream=DETECT_STREAM): bool {.discardable.} =
|
||||
## Convenience proc which inflates a string containing compressed data
|
||||
## with an optional header.
|
||||
##
|
||||
## Valid argument for ``stream`` are:
|
||||
## - ``DETECT_STREAM`` - detect if zlib or gzip header is present
|
||||
## and decompress stream. Fail on raw deflate stream.
|
||||
## - ``ZLIB_STREAM`` - decompress a zlib stream.
|
||||
## - ``GZIP_STREAM`` - decompress a gzip stream.
|
||||
## - ``RAW_DEFLATE`` - decompress a raw deflate stream.
|
||||
##
|
||||
## It is ok to pass a buffer which doesn't contain deflated data,
|
||||
## in this case the proc won't modify the buffer.
|
||||
##
|
||||
## Returns true if `buffer` was successfully inflated.
|
||||
|
||||
var temp = uncompress(addr(buffer[0]), buffer.len, stream)
|
||||
if temp.len != 0:
|
||||
swap(buffer, temp)
|
||||
result = true
|
||||
176
tests/deps/zip-0.2.1/zip/zzip.nim
Normal file
176
tests/deps/zip-0.2.1/zip/zzip.nim
Normal file
|
|
@ -0,0 +1,176 @@
|
|||
#
|
||||
#
|
||||
# Nim's Runtime Library
|
||||
# (c) Copyright 2008 Andreas Rumpf
|
||||
#
|
||||
# See the file "copying.txt", included in this
|
||||
# distribution, for details about the copyright.
|
||||
#
|
||||
|
||||
## This module is an interface to the zzip library.
|
||||
|
||||
# Author:
|
||||
# Guido Draheim <guidod@gmx.de>
|
||||
# Tomi Ollila <Tomi.Ollila@iki.fi>
|
||||
# Copyright (c) 1999,2000,2001,2002,2003,2004 Guido Draheim
|
||||
# All rights reserved,
|
||||
# usage allowed under the restrictions of the
|
||||
# Lesser GNU General Public License
|
||||
# or alternatively the restrictions
|
||||
# of the Mozilla Public License 1.1
|
||||
|
||||
when defined(windows):
|
||||
const
|
||||
dllname = "zzip.dll"
|
||||
else:
|
||||
const
|
||||
dllname = "libzzip.so"
|
||||
|
||||
type
|
||||
TZZipError* = int32 # Name conflict if we drop the `T`
|
||||
|
||||
const
|
||||
ZZIP_ERROR* = -4096'i32
|
||||
ZZIP_NO_ERROR* = 0'i32 # no error, may be used if user sets it.
|
||||
ZZIP_OUTOFMEM* = ZZIP_ERROR - 20'i32 # out of memory
|
||||
ZZIP_DIR_OPEN* = ZZIP_ERROR - 21'i32 # failed to open zipfile, see errno for details
|
||||
ZZIP_DIR_STAT* = ZZIP_ERROR - 22'i32 # failed to fstat zipfile, see errno for details
|
||||
ZZIP_DIR_SEEK* = ZZIP_ERROR - 23'i32 # failed to lseek zipfile, see errno for details
|
||||
ZZIP_DIR_READ* = ZZIP_ERROR - 24'i32 # failed to read zipfile, see errno for details
|
||||
ZZIP_DIR_TOO_SHORT* = ZZIP_ERROR - 25'i32
|
||||
ZZIP_DIR_EDH_MISSING* = ZZIP_ERROR - 26'i32
|
||||
ZZIP_DIRSIZE* = ZZIP_ERROR - 27'i32
|
||||
ZZIP_ENOENT* = ZZIP_ERROR - 28'i32
|
||||
ZZIP_UNSUPP_COMPR* = ZZIP_ERROR - 29'i32
|
||||
ZZIP_CORRUPTED* = ZZIP_ERROR - 31'i32
|
||||
ZZIP_UNDEF* = ZZIP_ERROR - 32'i32
|
||||
ZZIP_DIR_LARGEFILE* = ZZIP_ERROR - 33'i32
|
||||
|
||||
ZZIP_CASELESS* = 1'i32 shl 12'i32
|
||||
ZZIP_NOPATHS* = 1'i32 shl 13'i32
|
||||
ZZIP_PREFERZIP* = 1'i32 shl 14'i32
|
||||
ZZIP_ONLYZIP* = 1'i32 shl 16'i32
|
||||
ZZIP_FACTORY* = 1'i32 shl 17'i32
|
||||
ZZIP_ALLOWREAL* = 1'i32 shl 18'i32
|
||||
ZZIP_THREADED* = 1'i32 shl 19'i32
|
||||
|
||||
type
|
||||
ZZipDir* {.final, pure.} = object
|
||||
ZZipFile* {.final, pure.} = object
|
||||
ZZipPluginIO* {.final, pure.} = object
|
||||
|
||||
ZZipDirent* {.final, pure.} = object
|
||||
d_compr*: int32 ## compression method
|
||||
d_csize*: int32 ## compressed size
|
||||
st_size*: int32 ## file size / decompressed size
|
||||
d_name*: cstring ## file name / strdupped name
|
||||
|
||||
ZZipStat* = ZZipDirent
|
||||
{.deprecated: [TZZipDir: ZzipDir, TZZipFile: ZzipFile,
|
||||
TZZipPluginIO: ZzipPluginIO, TZZipDirent: ZzipDirent,
|
||||
TZZipStat: ZZipStat].}
|
||||
|
||||
proc zzip_strerror*(errcode: int32): cstring {.cdecl, dynlib: dllname,
|
||||
importc: "zzip_strerror".}
|
||||
proc zzip_strerror_of*(dir: ptr ZZipDir): cstring {.cdecl, dynlib: dllname,
|
||||
importc: "zzip_strerror_of".}
|
||||
proc zzip_errno*(errcode: int32): int32 {.cdecl, dynlib: dllname,
|
||||
importc: "zzip_errno".}
|
||||
|
||||
proc zzip_geterror*(dir: ptr ZZipDir): int32 {.cdecl, dynlib: dllname,
|
||||
importc: "zzip_error".}
|
||||
proc zzip_seterror*(dir: ptr ZZipDir, errcode: int32) {.cdecl, dynlib: dllname,
|
||||
importc: "zzip_seterror".}
|
||||
proc zzip_compr_str*(compr: int32): cstring {.cdecl, dynlib: dllname,
|
||||
importc: "zzip_compr_str".}
|
||||
proc zzip_dirhandle*(fp: ptr ZZipFile): ptr ZZipDir {.cdecl, dynlib: dllname,
|
||||
importc: "zzip_dirhandle".}
|
||||
proc zzip_dirfd*(dir: ptr ZZipDir): int32 {.cdecl, dynlib: dllname,
|
||||
importc: "zzip_dirfd".}
|
||||
proc zzip_dir_real*(dir: ptr ZZipDir): int32 {.cdecl, dynlib: dllname,
|
||||
importc: "zzip_dir_real".}
|
||||
proc zzip_file_real*(fp: ptr ZZipFile): int32 {.cdecl, dynlib: dllname,
|
||||
importc: "zzip_file_real".}
|
||||
proc zzip_realdir*(dir: ptr ZZipDir): pointer {.cdecl, dynlib: dllname,
|
||||
importc: "zzip_realdir".}
|
||||
proc zzip_realfd*(fp: ptr ZZipFile): int32 {.cdecl, dynlib: dllname,
|
||||
importc: "zzip_realfd".}
|
||||
|
||||
proc zzip_dir_alloc*(fileext: cstringArray): ptr ZZipDir {.cdecl,
|
||||
dynlib: dllname, importc: "zzip_dir_alloc".}
|
||||
proc zzip_dir_free*(para1: ptr ZZipDir): int32 {.cdecl, dynlib: dllname,
|
||||
importc: "zzip_dir_free".}
|
||||
|
||||
proc zzip_dir_fdopen*(fd: int32, errcode_p: ptr TZZipError): ptr ZZipDir {.cdecl,
|
||||
dynlib: dllname, importc: "zzip_dir_fdopen".}
|
||||
proc zzip_dir_open*(filename: cstring, errcode_p: ptr TZZipError): ptr ZZipDir {.
|
||||
cdecl, dynlib: dllname, importc: "zzip_dir_open".}
|
||||
proc zzip_dir_close*(dir: ptr ZZipDir) {.cdecl, dynlib: dllname,
|
||||
importc: "zzip_dir_close".}
|
||||
proc zzip_dir_read*(dir: ptr ZZipDir, dirent: ptr ZZipDirent): int32 {.cdecl,
|
||||
dynlib: dllname, importc: "zzip_dir_read".}
|
||||
|
||||
proc zzip_opendir*(filename: cstring): ptr ZZipDir {.cdecl, dynlib: dllname,
|
||||
importc: "zzip_opendir".}
|
||||
proc zzip_closedir*(dir: ptr ZZipDir) {.cdecl, dynlib: dllname,
|
||||
importc: "zzip_closedir".}
|
||||
proc zzip_readdir*(dir: ptr ZZipDir): ptr ZZipDirent {.cdecl, dynlib: dllname,
|
||||
importc: "zzip_readdir".}
|
||||
proc zzip_rewinddir*(dir: ptr ZZipDir) {.cdecl, dynlib: dllname,
|
||||
importc: "zzip_rewinddir".}
|
||||
proc zzip_telldir*(dir: ptr ZZipDir): int {.cdecl, dynlib: dllname,
|
||||
importc: "zzip_telldir".}
|
||||
proc zzip_seekdir*(dir: ptr ZZipDir, offset: int) {.cdecl, dynlib: dllname,
|
||||
importc: "zzip_seekdir".}
|
||||
|
||||
proc zzip_file_open*(dir: ptr ZZipDir, name: cstring, flags: int32): ptr ZZipFile {.
|
||||
cdecl, dynlib: dllname, importc: "zzip_file_open".}
|
||||
proc zzip_file_close*(fp: ptr ZZipFile) {.cdecl, dynlib: dllname,
|
||||
importc: "zzip_file_close".}
|
||||
proc zzip_file_read*(fp: ptr ZZipFile, buf: pointer, length: int): int {.
|
||||
cdecl, dynlib: dllname, importc: "zzip_file_read".}
|
||||
proc zzip_open*(name: cstring, flags: int32): ptr ZZipFile {.cdecl,
|
||||
dynlib: dllname, importc: "zzip_open".}
|
||||
proc zzip_close*(fp: ptr ZZipFile) {.cdecl, dynlib: dllname,
|
||||
importc: "zzip_close".}
|
||||
proc zzip_read*(fp: ptr ZZipFile, buf: pointer, length: int): int {.
|
||||
cdecl, dynlib: dllname, importc: "zzip_read".}
|
||||
|
||||
proc zzip_freopen*(name: cstring, mode: cstring, para3: ptr ZZipFile): ptr ZZipFile {.
|
||||
cdecl, dynlib: dllname, importc: "zzip_freopen".}
|
||||
proc zzip_fopen*(name: cstring, mode: cstring): ptr ZZipFile {.cdecl,
|
||||
dynlib: dllname, importc: "zzip_fopen".}
|
||||
proc zzip_fread*(p: pointer, size: int, nmemb: int,
|
||||
file: ptr ZZipFile): int {.cdecl, dynlib: dllname,
|
||||
importc: "zzip_fread".}
|
||||
proc zzip_fclose*(fp: ptr ZZipFile) {.cdecl, dynlib: dllname,
|
||||
importc: "zzip_fclose".}
|
||||
|
||||
proc zzip_rewind*(fp: ptr ZZipFile): int32 {.cdecl, dynlib: dllname,
|
||||
importc: "zzip_rewind".}
|
||||
proc zzip_seek*(fp: ptr ZZipFile, offset: int, whence: int32): int {.
|
||||
cdecl, dynlib: dllname, importc: "zzip_seek".}
|
||||
proc zzip_tell*(fp: ptr ZZipFile): int {.cdecl, dynlib: dllname,
|
||||
importc: "zzip_tell".}
|
||||
|
||||
proc zzip_dir_stat*(dir: ptr ZZipDir, name: cstring, zs: ptr ZZipStat,
|
||||
flags: int32): int32 {.cdecl, dynlib: dllname,
|
||||
importc: "zzip_dir_stat".}
|
||||
proc zzip_file_stat*(fp: ptr ZZipFile, zs: ptr ZZipStat): int32 {.cdecl,
|
||||
dynlib: dllname, importc: "zzip_file_stat".}
|
||||
proc zzip_fstat*(fp: ptr ZZipFile, zs: ptr ZZipStat): int32 {.cdecl, dynlib: dllname,
|
||||
importc: "zzip_fstat".}
|
||||
|
||||
proc zzip_open_shared_io*(stream: ptr ZZipFile, name: cstring,
|
||||
o_flags: int32, o_modes: int32, ext: cstringArray,
|
||||
io: ptr ZZipPluginIO): ptr ZZipFile {.cdecl,
|
||||
dynlib: dllname, importc: "zzip_open_shared_io".}
|
||||
proc zzip_open_ext_io*(name: cstring, o_flags: int32, o_modes: int32,
|
||||
ext: cstringArray, io: ptr ZZipPluginIO): ptr ZZipFile {.
|
||||
cdecl, dynlib: dllname, importc: "zzip_open_ext_io".}
|
||||
proc zzip_opendir_ext_io*(name: cstring, o_modes: int32,
|
||||
ext: cstringArray, io: ptr ZZipPluginIO): ptr ZZipDir {.
|
||||
cdecl, dynlib: dllname, importc: "zzip_opendir_ext_io".}
|
||||
proc zzip_dir_open_ext_io*(filename: cstring, errcode_p: ptr TZZipError,
|
||||
ext: cstringArray, io: ptr ZZipPluginIO): ptr ZZipDir {.
|
||||
cdecl, dynlib: dllname, importc: "zzip_dir_open_ext_io".}
|
||||
Loading…
Add table
Add a link
Reference in a new issue