From e44911d4eb1cd9e41fbf71a614fb07273ef35ecb Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Tue, 5 Sep 2023 22:19:48 -0800 Subject: [PATCH] fetch works --- build.zig | 12 ++ src/bun.js/node/node_fs_stat_watcher.zig | 8 +- src/bun.zig | 6 + src/deps/zlib.shared.zig | 53 ++++++ src/deps/zlib.win32.zig | 211 +++++++++++++++++++++++ src/http_client_async.zig | 4 +- src/zlib.posix.zig | 90 ++++++++++ src/zlib.zig | 129 +++----------- 8 files changed, 402 insertions(+), 111 deletions(-) create mode 100644 src/deps/zlib.shared.zig create mode 100644 src/deps/zlib.win32.zig create mode 100644 src/zlib.posix.zig diff --git a/build.zig b/build.zig index 03bb363076..a65fd7bbc9 100644 --- a/build.zig +++ b/build.zig @@ -52,6 +52,18 @@ fn addInternalPackages(b: *Build, step: *CompileStep, _: std.mem.Allocator, _: [ step.addModule("async_io", io); + step.addModule("zlib-internal", brk: { + if (target.isWindows()) { + break :brk b.createModule(.{ + .source_file = FileSource.relative("src/deps/zlib.win32.zig") + }); + } + + break :brk b.createModule(.{ + .source_file = FileSource.relative("src/deps/zlib.posix.zig") + }); + }); + var async_: *Module = brk: { if (target.isDarwin() or target.isLinux() or target.isFreeBSD()) { break :brk b.createModule(.{ diff --git a/src/bun.js/node/node_fs_stat_watcher.zig b/src/bun.js/node/node_fs_stat_watcher.zig index c2690c2000..7eeb8bb530 100644 --- a/src/bun.js/node/node_fs_stat_watcher.zig +++ b/src/bun.js/node/node_fs_stat_watcher.zig @@ -156,7 +156,7 @@ pub const StatWatcher = struct { globalThis: *JSC.JSGlobalObject, js_this: JSC.JSValue, - poll_ref: JSC.PollRef = .{}, + poll_ref: bun.Async.KeepAlive = .{}, last_stat: bun.Stat, last_jsvalue: JSC.Strong, @@ -197,6 +197,12 @@ pub const StatWatcher = struct { global_this: JSC.C.JSContextRef, pub fn fromJS(ctx: JSC.C.JSContextRef, arguments: *ArgumentsSlice, exception: JSC.C.ExceptionRef) ?Arguments { + if (comptime Environment.isWindows) { + bun.todo(@src(), void{}); + ctx.throwTODO("Windows support not implemented yet! Sorry!!"); + return null; + } + const vm = ctx.vm(); const path = PathLike.fromJS(ctx, arguments, exception) orelse { if (exception.* == null) { diff --git a/src/bun.zig b/src/bun.zig index 0d2e492a46..a1345dd228 100644 --- a/src/bun.zig +++ b/src/bun.zig @@ -1741,6 +1741,12 @@ const WindowsStat = extern struct { pub fn atime(this: *const WindowsStat) std.c.timespec { return this.atim; } + + pub fn birthtime(this: *const WindowsStat) std.c.timespec { + return this.atim; + } + + }; pub const Stat = if (Environment.isPosix) std.os.Stat else WindowsStat; diff --git a/src/deps/zlib.shared.zig b/src/deps/zlib.shared.zig new file mode 100644 index 0000000000..e23b9d43e3 --- /dev/null +++ b/src/deps/zlib.shared.zig @@ -0,0 +1,53 @@ + +// #define Z_BINARY 0 +// #define Z_TEXT 1 +// #define Z_ASCII Z_TEXT /* for compatibility with 1.2.2 and earlier */ +// #define Z_UNKNOWN 2 +pub const DataType = enum(c_int) { + Binary = 0, + Text = 1, + Unknown = 2, +}; + +// #define Z_OK 0 +// #define Z_STREAM_END 1 +// #define Z_NEED_DICT 2 +// #define Z_ERRNO (-1) +// #define Z_STREAM_ERROR (-2) +// #define Z_DATA_ERROR (-3) +// #define Z_MEM_ERROR (-4) +// #define Z_BUF_ERROR (-5) +// #define Z_VERSION_ERROR (-6) +pub const ReturnCode = enum(c_int) { + Ok = 0, + StreamEnd = 1, + NeedDict = 2, + ErrNo = -1, + StreamError = -2, + DataError = -3, + MemError = -4, + BufError = -5, + VersionError = -6, +}; + +// #define Z_NO_FLUSH 0 +// #define Z_PARTIAL_FLUSH 1 +// #define Z_SYNC_FLUSH 2 +// #define Z_FULL_FLUSH 3 +// #define Z_FINISH 4 +// #define Z_BLOCK 5 +// #define Z_TREES 6 +pub const FlushValue = enum(c_int) { + NoFlush = 0, + PartialFlush = 1, + /// Z_SYNC_FLUSH requests that inflate() flush as much output as possible to the output buffer + SyncFlush = 2, + FullFlush = 3, + Finish = 4, + + /// Z_BLOCK requests that inflate() stop if and when it gets to the next / deflate block boundary When decoding the zlib or gzip format, this will / cause inflate() to return immediately after the header and before the / first block. When doing a raw inflate, inflate() will go ahead and / process the first block, and will return when it gets to the end of that / block, or when it runs out of data. / The Z_BLOCK option assists in appending to or combining deflate streams. / To assist in this, on return inflate() always sets strm->data_type to the / number of unused bits in the last byte taken from strm->next_in, plus 64 / if inflate() is currently decoding the last block in the deflate stream, / plus 128 if inflate() returned immediately after decoding an end-of-block / code or decoding the complete header up to just before the first byte of / the deflate stream. The end-of-block will not be indicated until all of / the uncompressed data from that block has been written to strm->next_out. / The number of unused bits may in general be greater than seven, except / when bit 7 of data_type is set, in which case the number of unused bits / will be less than eight. data_type is set as noted here every time / inflate() returns for all flush options, and so can be used to determine / the amount of currently consumed input in bits. + Block = 5, + + /// The Z_TREES option behaves as Z_BLOCK does, but it also returns when the end of each deflate block header is reached, before any actual data in that block is decoded. This allows the caller to determine the length of the deflate block header for later use in random access within a deflate block. 256 is added to the value of strm->data_type when inflate() returns immediately after reaching the end of the deflate block header. + Trees = 6, +}; \ No newline at end of file diff --git a/src/deps/zlib.win32.zig b/src/deps/zlib.win32.zig new file mode 100644 index 0000000000..d98d5c4eae --- /dev/null +++ b/src/deps/zlib.win32.zig @@ -0,0 +1,211 @@ +// zig translate-c -I${VCPKG_ROOT}/installed/x64-windows/include/ ${VCPKG_ROOT}/current/installed/x64-windows/include/zlib.h -target x86_64-windows-msvc -lc > src/deps/zlib.win32.zig +pub const rsize_t = usize; +pub const _ino_t = c_ushort; +pub const ino_t = _ino_t; +pub const _dev_t = c_uint; +pub const dev_t = _dev_t; +pub const _off_t = c_long; +pub const off_t = _off_t; +const voidpf = *anyopaque; +const Bytef = [*]u8; +const Byte = u8; +const uInt = c_uint; +const uLong = c_ulong; + const z_size_t = usize; + const intf = ReturnCode; + const uIntf = uInt; + const uLongf = uLong; + const voidpc = ?*const anyopaque; + const voidp = ?*anyopaque; +pub const alloc_func = ?*const fn (voidpf, uInt, uInt) callconv(.C) voidpf; +pub const free_func = ?*const fn (voidpf, voidpf) callconv(.C) void; +pub const z_alloc_func = alloc_func; +pub const z_free_func = free_func; +pub const struct_internal_state = opaque {}; +pub const struct_z_stream_s = extern struct { + next_in: ?[*]const u8, + avail_in: uInt, + total_in: uLong, + next_out: ?[*]u8, + avail_out: uInt, + total_out: uLong, + err_msg: ?[*:0]const u8, + internal_state: ?*struct_internal_state, + alloc_func: alloc_func, + free_func: free_func, + @"user_data": voidpf, + data_type: DataType, + adler: uLong, + reserved: uLong, +}; +pub const z_stream = struct_z_stream_s; +pub const z_streamp = ?*z_stream; +pub const struct_gz_header_s = extern struct { + text: c_int, + time: uLong, + xflags: c_int, + os: c_int, + extra: [*c]Bytef, + extra_len: uInt, + extra_max: uInt, + name: [*c]Bytef, + name_max: uInt, + comment: [*c]Bytef, + comm_max: uInt, + hcrc: c_int, + done: c_int, +}; +pub const gz_header = struct_gz_header_s; +pub const gz_headerp = [*c]gz_header; +pub extern fn zlibVersion() [*c]const u8; +pub extern fn deflate(strm: z_streamp, flush: FlushValue) ReturnCode; +pub extern fn deflateEnd(strm: z_streamp) ReturnCode; +pub extern fn inflate(strm: z_streamp, flush: FlushValue) ReturnCode; +pub extern fn inflateEnd(strm: z_streamp) ReturnCode; +pub extern fn deflateSetDictionary(strm: z_streamp, dictionary: [*c]const Bytef, dictLength: uInt) ReturnCode; +pub extern fn deflateGetDictionary(strm: z_streamp, dictionary: [*c]Bytef, dictLength: [*c]uInt) ReturnCode; +pub extern fn deflateCopy(dest: z_streamp, source: z_streamp) ReturnCode; +pub extern fn deflateReset(strm: z_streamp) ReturnCode; +pub extern fn deflateParams(strm: z_streamp, level: c_int, strategy: c_int) ReturnCode; +pub extern fn deflateTune(strm: z_streamp, good_length: c_int, max_lazy: c_int, nice_length: c_int, max_chain: c_int) ReturnCode; +pub extern fn deflateBound(strm: z_streamp, sourceLen: uLong) uLong; +pub extern fn deflatePending(strm: z_streamp, pending: [*c]c_uint, bits: [*c]c_int) ReturnCode; +pub extern fn deflatePrime(strm: z_streamp, bits: c_int, value: c_int) ReturnCode; +pub extern fn deflateSetHeader(strm: z_streamp, head: gz_headerp) ReturnCode; +pub extern fn inflateSetDictionary(strm: z_streamp, dictionary: [*c]const Bytef, dictLength: uInt) ReturnCode; +pub extern fn inflateGetDictionary(strm: z_streamp, dictionary: [*c]Bytef, dictLength: [*c]uInt) ReturnCode; +pub extern fn inflateSync(strm: z_streamp) ReturnCode; +pub extern fn inflateCopy(dest: z_streamp, source: z_streamp) ReturnCode; +pub extern fn inflateReset(strm: z_streamp) ReturnCode; +pub extern fn inflateReset2(strm: z_streamp, windowBits: c_int) ReturnCode; +pub extern fn inflatePrime(strm: z_streamp, bits: c_int, value: c_int) ReturnCode; +pub extern fn inflateMark(strm: z_streamp) c_long; +pub extern fn inflateGetHeader(strm: z_streamp, head: gz_headerp) ReturnCode; +pub const in_func = ?*const fn (?*anyopaque, [*c][*c]u8) callconv(.C) c_uint; +pub const out_func = ?*const fn (?*anyopaque, [*c]u8, c_uint) callconv(.C) ReturnCode; +pub extern fn inflateBack(strm: z_streamp, in: in_func, in_desc: ?*anyopaque, out: out_func, out_desc: ?*anyopaque) ReturnCode; +pub extern fn inflateBackEnd(strm: z_streamp) ReturnCode; +pub extern fn zlibCompileFlags() uLong; +pub extern fn compress(dest: [*c]Bytef, destLen: [*c]uLongf, source: [*c]const Bytef, sourceLen: uLong) ReturnCode; +pub extern fn compress2(dest: [*c]Bytef, destLen: [*c]uLongf, source: [*c]const Bytef, sourceLen: uLong, level: c_int) ReturnCode; +pub extern fn compressBound(sourceLen: uLong) uLong; +pub extern fn uncompress(dest: [*c]Bytef, destLen: [*c]uLongf, source: [*c]const Bytef, sourceLen: uLong) ReturnCode; +pub extern fn uncompress2(dest: [*c]Bytef, destLen: [*c]uLongf, source: [*c]const Bytef, sourceLen: [*c]uLong) ReturnCode; +pub const struct_gzFile_s = extern struct { + have: c_uint, + next: [*c]u8, + pos: c_longlong, +}; +pub const gzFile = [*c]struct_gzFile_s; +pub extern fn gzdopen(fd: c_int, mode: [*c]const u8) gzFile; +pub extern fn gzbuffer(file: gzFile, size: c_uint) ReturnCode; +pub extern fn gzsetparams(file: gzFile, level: c_int, strategy: c_int) ReturnCode; +pub extern fn gzread(file: gzFile, buf: voidp, len: c_uint) ReturnCode; +pub extern fn gzfread(buf: voidp, size: z_size_t, nitems: z_size_t, file: gzFile) z_size_t; +pub extern fn gzwrite(file: gzFile, buf: voidpc, len: c_uint) ReturnCode; +pub extern fn gzfwrite(buf: voidpc, size: z_size_t, nitems: z_size_t, file: gzFile) z_size_t; +pub extern fn gzprintf(file: gzFile, format: [*c]const u8, ...) ReturnCode; +pub extern fn gzputs(file: gzFile, s: [*c]const u8) ReturnCode; +pub extern fn gzgets(file: gzFile, buf: [*c]u8, len: c_int) [*c]u8; +pub extern fn gzputc(file: gzFile, c: c_int) ReturnCode; +pub extern fn gzgetc(file: gzFile) ReturnCode; +pub extern fn gzungetc(c: c_int, file: gzFile) ReturnCode; +pub extern fn gzflush(file: gzFile, flush: FlushValue) ReturnCode; +pub extern fn gzrewind(file: gzFile) ReturnCode; +pub extern fn gzeof(file: gzFile) ReturnCode; +pub extern fn gzdirect(file: gzFile) ReturnCode; +pub extern fn gzclose(file: gzFile) ReturnCode; +pub extern fn gzclose_r(file: gzFile) ReturnCode; +pub extern fn gzclose_w(file: gzFile) ReturnCode; +pub extern fn gzerror(file: gzFile, errnum: [*c]c_int) [*c]const u8; +pub extern fn gzclearerr(file: gzFile) void; +pub extern fn adler32(adler: uLong, buf: [*c]const Bytef, len: uInt) uLong; +pub extern fn adler32_z(adler: uLong, buf: [*c]const Bytef, len: z_size_t) uLong; +pub extern fn crc32(crc: uLong, buf: [*c]const Bytef, len: uInt) uLong; +pub extern fn crc32_z(crc: uLong, buf: [*c]const Bytef, len: z_size_t) uLong; +pub extern fn crc32_combine_op(crc1: uLong, crc2: uLong, op: uLong) uLong; +pub extern fn deflateInit_(strm: z_streamp, level: c_int, version: [*c]const u8, stream_size: c_int) ReturnCode; +pub extern fn inflateInit_(strm: z_streamp, version: [*c]const u8, stream_size: c_int) ReturnCode; +pub extern fn deflateInit2_(strm: z_streamp, level: c_int, method: c_int, windowBits: c_int, memLevel: c_int, strategy: c_int, version: [*c]const u8, stream_size: c_int) ReturnCode; +pub extern fn inflateInit2_(strm: z_streamp, windowBits: c_int, version: [*c]const u8, stream_size: c_int) ReturnCode; +pub extern fn inflateBackInit_(strm: z_streamp, windowBits: c_int, window: [*c]u8, version: [*c]const u8, stream_size: c_int) ReturnCode; +pub extern fn gzgetc_(file: gzFile) ReturnCode; +pub extern fn gzopen([*c]const u8, [*c]const u8) gzFile; +pub extern fn gzseek(gzFile, c_long, c_int) c_long; +pub extern fn gztell(gzFile) c_long; +pub extern fn gzoffset(gzFile) c_long; +pub extern fn adler32_combine(uLong, uLong, c_long) uLong; +pub extern fn crc32_combine(uLong, uLong, c_long) uLong; +pub extern fn crc32_combine_gen(c_long) uLong; +pub extern fn zError(c_int) [*c]const u8; +pub extern fn inflateSyncPoint(z_streamp) ReturnCode; +// pub extern fn get_crc_table() [*c]const z_crc_t; +pub extern fn inflateUndermine(z_streamp, c_int) ReturnCode; +pub extern fn inflateValidate(z_streamp, c_int) ReturnCode; +pub extern fn inflateCodesUsed(z_streamp) c_ulong; +pub extern fn inflateResetKeep(z_streamp) ReturnCode; +pub extern fn deflateResetKeep(z_streamp) ReturnCode; + +pub const z_off_t = c_long; +pub const ZLIB_VERSION = "1.2.13"; +pub const ZLIB_VERNUM = @as(c_int, 0x12d0); +pub const ZLIB_VER_MAJOR = @as(c_int, 1); +pub const ZLIB_VER_MINOR = @as(c_int, 2); +pub const ZLIB_VER_REVISION = @as(c_int, 13); +pub const ZLIB_VER_SUBREVISION = @as(c_int, 0); +pub const Z_NO_FLUSH = @as(c_int, 0); +pub const Z_PARTIAL_FLUSH = @as(c_int, 1); +pub const Z_SYNC_FLUSH = @as(c_int, 2); +pub const Z_FULL_FLUSH = @as(c_int, 3); +pub const Z_FINISH = @as(c_int, 4); +pub const Z_BLOCK = @as(c_int, 5); +pub const Z_TREES = @as(c_int, 6); +pub const Z_OK = @as(c_int, 0); +pub const Z_STREAM_END = @as(c_int, 1); +pub const Z_NEED_DICT = @as(c_int, 2); +pub const Z_ERRNO = -@as(c_int, 1); +pub const Z_STREAM_ERROR = -@as(c_int, 2); +pub const Z_DATA_ERROR = -@as(c_int, 3); +pub const Z_MEM_ERROR = -@as(c_int, 4); +pub const Z_BUF_ERROR = -@as(c_int, 5); +pub const Z_VERSION_ERROR = -@as(c_int, 6); +pub const Z_NO_COMPRESSION = @as(c_int, 0); +pub const Z_BEST_SPEED = @as(c_int, 1); +pub const Z_BEST_COMPRESSION = @as(c_int, 9); +pub const Z_DEFAULT_COMPRESSION = -@as(c_int, 1); +pub const Z_FILTERED = @as(c_int, 1); +pub const Z_HUFFMAN_ONLY = @as(c_int, 2); +pub const Z_RLE = @as(c_int, 3); +pub const Z_FIXED = @as(c_int, 4); +pub const Z_DEFAULT_STRATEGY = @as(c_int, 0); +pub const Z_BINARY = @as(c_int, 0); +pub const Z_TEXT = @as(c_int, 1); +pub const Z_ASCII = Z_TEXT; +pub const Z_UNKNOWN = @as(c_int, 2); +pub const Z_DEFLATED = @as(c_int, 8); +pub const Z_NULL = @as(c_int, 0); +pub inline fn deflateInit(strm: anytype, level: anytype) ReturnCode { + return deflateInit_(strm, level, zlibVersion(), @import("std").zig.c_translation.cast(c_int, @import("std").zig.c_translation.sizeof(z_stream))); +} +pub inline fn inflateInit(strm: anytype) ReturnCode { + return inflateInit_(strm, zlibVersion(), @import("std").zig.c_translation.cast(c_int, @import("std").zig.c_translation.sizeof(z_stream))); +} +pub inline fn deflateInit2(strm: anytype, level: anytype, method: anytype, windowBits: anytype, memLevel: anytype, strategy: anytype) ReturnCode { + return deflateInit2_(strm, level, method, windowBits, memLevel, strategy, zlibVersion(), @import("std").zig.c_translation.cast(c_int, @import("std").zig.c_translation.sizeof(z_stream))); +} +pub inline fn inflateInit2(strm: anytype, windowBits: anytype) ReturnCode { + return inflateInit2_(strm, windowBits, zlibVersion(), @import("std").zig.c_translation.cast(c_int, @import("std").zig.c_translation.sizeof(z_stream))); +} +pub inline fn inflateBackInit(strm: anytype, windowBits: anytype, window: anytype) ReturnCode { + return inflateBackInit_(strm, windowBits, window, zlibVersion(), @import("std").zig.c_translation.cast(c_int, @import("std").zig.c_translation.sizeof(z_stream))); +} +pub const internal_state = struct_internal_state; +pub const z_stream_s = struct_z_stream_s; +pub const zStream_struct = struct_z_stream_s; +pub const gz_header_s = struct_gz_header_s; +pub const gzFile_s = struct_gzFile_s; + + +pub const DataType = @import("./zlib-shared.zig").DataType; +pub const FlushValue = @import("./zlib-shared.zig").FlushValue; +pub const ReturnCode = @import("./zlib-shared.zig").ReturnCode; \ No newline at end of file diff --git a/src/http_client_async.zig b/src/http_client_async.zig index 5c7dc42091..bbf7e07b83 100644 --- a/src/http_client_async.zig +++ b/src/http_client_async.zig @@ -1140,10 +1140,10 @@ pub const InternalState = struct { body_out_str.list.expandToCapacity(); } reader.list = body_out_str.list; - reader.zlib.next_out = &body_out_str.list.items[initial]; + reader.zlib.next_out = @ptrCast(&body_out_str.list.items[initial]); reader.zlib.avail_out = @as(u32, @truncate(body_out_str.list.capacity - initial)); // we reset the total out so we can track how much we decompressed this time - reader.zlib.total_out = initial; + reader.zlib.total_out = @truncate(initial); } else { reader = try Zlib.ZlibReaderArrayList.initWithOptionsAndListAllocator( buffer, diff --git a/src/zlib.posix.zig b/src/zlib.posix.zig new file mode 100644 index 0000000000..e8f65a1c82 --- /dev/null +++ b/src/zlib.posix.zig @@ -0,0 +1,90 @@ +const uInt = c_uint; +const uLong = c_ulong; +pub const struct_internal_state = extern struct { + dummy: c_int, +}; + +// https://zlib.net/manual.html#Stream +const Byte = u8; +const Bytef = Byte; +const charf = u8; +const intf = c_int; +const uIntf = uInt; +const uLongf = uLong; +const voidpc = ?*const anyopaque; +const voidpf = ?*anyopaque; +const voidp = ?*anyopaque; +const z_crc_t = c_uint; + +// typedef voidpf (*alloc_func) OF((voidpf opaque, uInt items, uInt size)); +// typedef void (*free_func) OF((voidpf opaque, voidpf address)); + +pub const z_alloc_fn = ?*const fn (*anyopaque, uInt, uInt) callconv(.C) voidpf; +pub const z_free_fn = ?*const fn (*anyopaque, *anyopaque) callconv(.C) void; + + +pub const zStream_struct = extern struct { + /// next input byte + next_in: [*c]const u8, + /// number of bytes available at next_in + avail_in: uInt, + /// total number of input bytes read so far + total_in: uLong, + + /// next output byte will go here + next_out: [*c]u8, + /// remaining free space at next_out + avail_out: uInt, + /// total number of bytes output so far + total_out: uLong, + + /// last error message, NULL if no error + err_msg: [*c]const u8, + /// not visible by applications + internal_state: ?*struct_internal_state, + + /// used to allocate the internal state + alloc_func: z_alloc_fn, + /// used to free the internal state + free_func: z_free_fn, + /// private data object passed to zalloc and zfree + user_data: *anyopaque, + + /// best guess about the data type: binary or text for deflate, or the decoding state for inflate + data_type: DataType, + + ///Adler-32 or CRC-32 value of the uncompressed data + adler: uLong, + /// reserved for future use + reserved: uLong, +}; + +pub const z_stream = zStream_struct; +pub const z_streamp = *z_stream; + +pub const DataType = @import("./zlib-shared.zig").DataType; +pub const FlushValue = @import("./zlib-shared.zig").FlushValue; +pub const ReturnCode = @import("./zlib-shared.zig").ReturnCode; +pub extern fn zlibVersion() [*c]const u8; + +pub extern fn deflateInit_(strm: z_streamp, level: c_int, version: [*c]const u8, stream_size: c_int) ReturnCode; +pub extern fn inflateInit_(strm: z_streamp, version: [*c]const u8, stream_size: c_int) ReturnCode; +pub extern fn deflateInit2_(strm: z_streamp, level: c_int, method: c_int, windowBits: c_int, memLevel: c_int, strategy: c_int, version: [*c]const u8, stream_size: c_int) ReturnCode; +pub extern fn inflateInit2_(strm: z_streamp, windowBits: c_int, version: [*c]const u8, stream_size: c_int) ReturnCode; +pub extern fn inflateBackInit_(strm: z_streamp, windowBits: c_int, window: [*c]u8, version: [*c]const u8, stream_size: c_int) ReturnCode; + +pub inline fn deflateInit(strm: anytype, level: anytype) ReturnCode { + return deflateInit_(strm, level, zlibVersion(), @import("std").zig.c_translation.cast(c_int, @import("std").zig.c_translation.sizeof(z_stream))); +} +pub inline fn inflateInit(strm: anytype) ReturnCode { + return inflateInit_(strm, zlibVersion(), @import("std").zig.c_translation.cast(c_int, @import("std").zig.c_translation.sizeof(z_stream))); +} +pub inline fn deflateInit2(strm: anytype, level: anytype, method: anytype, windowBits: anytype, memLevel: anytype, strategy: anytype) ReturnCode { + return deflateInit2_(strm, level, method, windowBits, memLevel, strategy, zlibVersion(), @import("std").zig.c_translation.cast(c_int, @import("std").zig.c_translation.sizeof(z_stream))); +} +pub inline fn inflateInit2(strm: anytype, windowBits: anytype) ReturnCode { + return inflateInit2_(strm, windowBits, zlibVersion(), @import("std").zig.c_translation.cast(c_int, @import("std").zig.c_translation.sizeof(z_stream))); +} +pub inline fn inflateBackInit(strm: anytype, windowBits: anytype, window: anytype) ReturnCode { + return inflateBackInit_(strm, windowBits, window, zlibVersion(), @import("std").zig.c_translation.cast(c_int, @import("std").zig.c_translation.sizeof(z_stream))); +} \ No newline at end of file diff --git a/src/zlib.zig b/src/zlib.zig index 090d0f3a03..b88e0a7be3 100644 --- a/src/zlib.zig +++ b/src/zlib.zig @@ -65,12 +65,12 @@ const z_crc_t = c_uint; // typedef voidpf (*alloc_func) OF((voidpf opaque, uInt items, uInt size)); // typedef void (*free_func) OF((voidpf opaque, voidpf address)); -pub const z_alloc_fn = ?*const fn (*anyopaque, uInt, uInt) callconv(.C) voidpf; -pub const z_free_fn = ?*const fn (*anyopaque, *anyopaque) callconv(.C) void; +const zStream_struct = @import("zlib-internal").zStream_struct; +const z_stream = @import("zlib-internal").z_stream; +const z_streamp = @import("zlib-internal").z_streamp; + + -pub const struct_internal_state = extern struct { - dummy: c_int, -}; // typedef struct z_stream_s { // z_const Bytef *next_in; /* next input byte */ // uInt avail_in; /* number of bytes available at next_in */ @@ -93,97 +93,10 @@ pub const struct_internal_state = extern struct { // uLong reserved; /* reserved for future use */ // } z_stream; -pub const zStream_struct = extern struct { - /// next input byte - next_in: [*c]const u8, - /// number of bytes available at next_in - avail_in: uInt, - /// total number of input bytes read so far - total_in: uLong, - /// next output byte will go here - next_out: [*c]u8, - /// remaining free space at next_out - avail_out: uInt, - /// total number of bytes output so far - total_out: uLong, - - /// last error message, NULL if no error - err_msg: [*c]const u8, - /// not visible by applications - internal_state: ?*struct_internal_state, - - /// used to allocate the internal state - alloc_func: z_alloc_fn, - /// used to free the internal state - free_func: z_free_fn, - /// private data object passed to zalloc and zfree - user_data: *anyopaque, - - /// best guess about the data type: binary or text for deflate, or the decoding state for inflate - data_type: DataType, - - ///Adler-32 or CRC-32 value of the uncompressed data - adler: uLong, - /// reserved for future use - reserved: uLong, -}; - -pub const z_stream = zStream_struct; -pub const z_streamp = *z_stream; - -// #define Z_BINARY 0 -// #define Z_TEXT 1 -// #define Z_ASCII Z_TEXT /* for compatibility with 1.2.2 and earlier */ -// #define Z_UNKNOWN 2 -pub const DataType = enum(c_int) { - Binary = 0, - Text = 1, - Unknown = 2, -}; - -// #define Z_OK 0 -// #define Z_STREAM_END 1 -// #define Z_NEED_DICT 2 -// #define Z_ERRNO (-1) -// #define Z_STREAM_ERROR (-2) -// #define Z_DATA_ERROR (-3) -// #define Z_MEM_ERROR (-4) -// #define Z_BUF_ERROR (-5) -// #define Z_VERSION_ERROR (-6) -pub const ReturnCode = enum(c_int) { - Ok = 0, - StreamEnd = 1, - NeedDict = 2, - ErrNo = -1, - StreamError = -2, - DataError = -3, - MemError = -4, - BufError = -5, - VersionError = -6, -}; - -// #define Z_NO_FLUSH 0 -// #define Z_PARTIAL_FLUSH 1 -// #define Z_SYNC_FLUSH 2 -// #define Z_FULL_FLUSH 3 -// #define Z_FINISH 4 -// #define Z_BLOCK 5 -// #define Z_TREES 6 -pub const FlushValue = enum(c_int) { - NoFlush = 0, - PartialFlush = 1, - /// Z_SYNC_FLUSH requests that inflate() flush as much output as possible to the output buffer - SyncFlush = 2, - FullFlush = 3, - Finish = 4, - - /// Z_BLOCK requests that inflate() stop if and when it gets to the next / deflate block boundary When decoding the zlib or gzip format, this will / cause inflate() to return immediately after the header and before the / first block. When doing a raw inflate, inflate() will go ahead and / process the first block, and will return when it gets to the end of that / block, or when it runs out of data. / The Z_BLOCK option assists in appending to or combining deflate streams. / To assist in this, on return inflate() always sets strm->data_type to the / number of unused bits in the last byte taken from strm->next_in, plus 64 / if inflate() is currently decoding the last block in the deflate stream, / plus 128 if inflate() returned immediately after decoding an end-of-block / code or decoding the complete header up to just before the first byte of / the deflate stream. The end-of-block will not be indicated until all of / the uncompressed data from that block has been written to strm->next_out. / The number of unused bits may in general be greater than seven, except / when bit 7 of data_type is set, in which case the number of unused bits / will be less than eight. data_type is set as noted here every time / inflate() returns for all flush options, and so can be used to determine / the amount of currently consumed input in bits. - Block = 5, - - /// The Z_TREES option behaves as Z_BLOCK does, but it also returns when the end of each deflate block header is reached, before any actual data in that block is decoded. This allows the caller to determine the length of the deflate block header for later use in random access within a deflate block. 256 is added to the value of strm->data_type when inflate() returns immediately after reaching the end of the deflate block header. - Trees = 6, -}; +const DataType = @import("zlib-internal").DataType; +const FlushValue = @import("zlib-internal").FlushValue; +const ReturnCode = @import("zlib-internal").ReturnCode; // ZEXTERN int ZEXPORT inflateInit OF((z_streamp strm)); @@ -475,12 +388,12 @@ pub const ZlibReaderArrayList = struct { zlib_reader.zlib = zStream_struct{ .next_in = input.ptr, - .avail_in = @as(uInt, @intCast(input.len)), - .total_in = @as(uInt, @intCast(input.len)), + .avail_in = @truncate(input.len), + .total_in = @truncate(input.len), .next_out = zlib_reader.list.items.ptr, - .avail_out = @as(u32, @intCast(zlib_reader.list.items.len)), - .total_out = zlib_reader.list.items.len, + .avail_out = @truncate(zlib_reader.list.items.len), + .total_out = @truncate(zlib_reader.list.items.len), .err_msg = null, .alloc_func = ZlibReader.alloc, @@ -562,8 +475,8 @@ pub const ZlibReaderArrayList = struct { const initial = this.list.items.len; try this.list.ensureUnusedCapacity(this.list_allocator, 4096); this.list.expandToCapacity(); - this.zlib.next_out = &this.list.items[initial]; - this.zlib.avail_out = @as(u32, @intCast(this.list.items.len - initial)); + this.zlib.next_out = @ptrCast(&this.list.items[initial]); + this.zlib.avail_out = @truncate(this.list.items.len -| initial); } if (this.zlib.avail_in == 0) { @@ -870,12 +783,12 @@ pub const ZlibCompressorArrayList = struct { zlib_reader.zlib = zStream_struct{ .next_in = input.ptr, - .avail_in = @as(uInt, @intCast(input.len)), - .total_in = @as(uInt, @intCast(input.len)), + .avail_in = @truncate(input.len), + .total_in = @truncate(input.len), .next_out = zlib_reader.list.items.ptr, - .avail_out = @as(u32, @intCast(zlib_reader.list.items.len)), - .total_out = zlib_reader.list.items.len, + .avail_out = @truncate(zlib_reader.list.items.len), + .total_out = @truncate(zlib_reader.list.items.len), .err_msg = null, .alloc_func = ZlibCompressor.alloc, @@ -902,7 +815,7 @@ pub const ZlibCompressorArrayList = struct { ReturnCode.Ok => { try zlib_reader.list.ensureTotalCapacityPrecise(list_allocator, deflateBound(&zlib_reader.zlib, input.len)); zlib_reader.list_ptr.* = zlib_reader.list; - zlib_reader.zlib.avail_out = @as(uInt, @truncate(zlib_reader.list.capacity)); + zlib_reader.zlib.avail_out = @truncate(zlib_reader.list.capacity); zlib_reader.zlib.next_out = zlib_reader.list.items.ptr; return zlib_reader; @@ -969,8 +882,8 @@ pub const ZlibCompressorArrayList = struct { const initial = this.list.items.len; try this.list.ensureUnusedCapacity(this.list_allocator, 4096); this.list.expandToCapacity(); - this.zlib.next_out = &this.list.items[initial]; - this.zlib.avail_out = @as(u32, @intCast(this.list.items.len - initial)); + this.zlib.next_out = @ptrCast(&this.list.items[initial]); + this.zlib.avail_out = @truncate(this.list.items.len -| initial); } if (this.zlib.avail_out == 0) {