forked from oven-sh/bun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrotli.zig
287 lines (235 loc) · 9.44 KB
/
brotli.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
const bun = @import("root").bun;
const std = @import("std");
pub const c = struct {
pub usingnamespace @import("./deps/brotli_decoder.zig");
pub usingnamespace @import("./deps/brotli_encoder.zig");
};
const BrotliDecoder = c.BrotliDecoder;
const BrotliEncoder = c.BrotliEncoder;
const mimalloc = bun.Mimalloc;
pub const BrotliAllocator = struct {
pub fn alloc(_: ?*anyopaque, len: usize) callconv(.C) *anyopaque {
if (bun.heap_breakdown.enabled) {
const zone = bun.heap_breakdown.getZone("brotli");
return zone.malloc_zone_malloc(len) orelse bun.outOfMemory();
}
return mimalloc.mi_malloc(len) orelse bun.outOfMemory();
}
pub fn free(_: ?*anyopaque, data: ?*anyopaque) callconv(.C) void {
if (bun.heap_breakdown.enabled) {
const zone = bun.heap_breakdown.getZone("brotli");
zone.malloc_zone_free(data);
return;
}
mimalloc.mi_free(data);
}
};
pub const DecoderOptions = struct {
pub const Params = std.enums.EnumFieldStruct(c.BrotliDecoderParameter, bool, false);
params: Params = Params{
.LARGE_WINDOW = true,
.DISABLE_RING_BUFFER_REALLOCATION = false,
},
};
pub const BrotliReaderArrayList = struct {
pub const State = enum {
Uninitialized,
Inflating,
End,
Error,
};
input: []const u8,
list: std.ArrayListUnmanaged(u8),
list_allocator: std.mem.Allocator,
list_ptr: *std.ArrayListUnmanaged(u8),
brotli: *BrotliDecoder,
state: State = State.Uninitialized,
total_out: usize = 0,
total_in: usize = 0,
flushOp: BrotliEncoder.Operation,
finishFlushOp: BrotliEncoder.Operation,
fullFlushOp: BrotliEncoder.Operation,
pub usingnamespace bun.New(BrotliReaderArrayList);
pub fn newWithOptions(input: []const u8, list: *std.ArrayListUnmanaged(u8), allocator: std.mem.Allocator, options: DecoderOptions) !*BrotliReaderArrayList {
return BrotliReaderArrayList.new(try initWithOptions(input, list, allocator, options, .process, .finish, .flush));
}
pub fn initWithOptions(
input: []const u8,
list: *std.ArrayListUnmanaged(u8),
allocator: std.mem.Allocator,
options: DecoderOptions,
flushOp: BrotliEncoder.Operation,
finishFlushOp: BrotliEncoder.Operation,
fullFlushOp: BrotliEncoder.Operation,
) !BrotliReaderArrayList {
if (!BrotliDecoder.initializeBrotli()) {
return error.BrotliFailedToLoad;
}
var brotli = BrotliDecoder.createInstance(&BrotliAllocator.alloc, &BrotliAllocator.free, null) orelse return error.BrotliFailedToCreateInstance;
if (options.params.LARGE_WINDOW)
_ = brotli.setParameter(c.BrotliDecoderParameter.LARGE_WINDOW, 1);
if (options.params.DISABLE_RING_BUFFER_REALLOCATION)
_ = brotli.setParameter(c.BrotliDecoderParameter.DISABLE_RING_BUFFER_REALLOCATION, 1);
bun.assert(list.items.ptr != input.ptr);
return .{
.input = input,
.list_ptr = list,
.list = list.*,
.list_allocator = allocator,
.brotli = brotli,
.flushOp = flushOp,
.finishFlushOp = finishFlushOp,
.fullFlushOp = fullFlushOp,
};
}
pub fn end(this: *BrotliReaderArrayList) void {
this.state = .End;
}
pub fn readAll(this: *BrotliReaderArrayList, is_done: bool) !void {
defer this.list_ptr.* = this.list;
if (this.state == .End or this.state == .Error) {
return;
}
bun.assert(this.list.items.ptr != this.input.ptr);
while (this.state == State.Uninitialized or this.state == State.Inflating) {
var unused_capacity = this.list.unusedCapacitySlice();
if (unused_capacity.len < 4096) {
try this.list.ensureUnusedCapacity(this.list_allocator, 4096);
unused_capacity = this.list.unusedCapacitySlice();
}
bun.assert(unused_capacity.len > 0);
var next_in = this.input[this.total_in..];
var in_remaining = next_in.len;
var out_remaining = unused_capacity.len;
// https://github.com/google/brotli/blob/fef82ea10435abb1500b615b1b2c6175d429ec6c/go/cbrotli/reader.go#L15-L27
const result = this.brotli.decompressStream(
&in_remaining,
@ptrCast(&next_in),
&out_remaining,
@ptrCast(&unused_capacity.ptr),
null,
);
const bytes_written = unused_capacity.len -| out_remaining;
const bytes_read = next_in.len -| in_remaining;
this.list.items.len += bytes_written;
this.total_in += bytes_read;
switch (result) {
.success => {
if (comptime bun.Environment.allow_assert) {
bun.assert(this.brotli.isFinished());
}
this.end();
return;
},
.err => {
this.state = .Error;
if (comptime bun.Environment.allow_assert) {
const code = this.brotli.getErrorCode();
bun.Output.debugWarn("Brotli error: {s} ({d})", .{ @tagName(code), @intFromEnum(code) });
}
return error.BrotliDecompressionError;
},
.needs_more_input => {
if (in_remaining > 0) {
@panic("Brotli wants more data");
}
this.state = .Inflating;
if (is_done) {
this.state = .Error;
}
return error.ShortRead;
},
.needs_more_output => {
try this.list.ensureTotalCapacity(this.list_allocator, this.list.capacity + 4096);
this.state = .Inflating;
},
}
}
}
pub fn deinit(this: *BrotliReaderArrayList) void {
this.brotli.destroyInstance();
this.destroy();
}
};
pub const BrotliCompressionStream = struct {
pub const State = enum {
Inflating,
End,
Error,
};
brotli: *BrotliEncoder,
state: State = State.Inflating,
total_out: usize = 0,
total_in: usize = 0,
flushOp: BrotliEncoder.Operation,
finishFlushOp: BrotliEncoder.Operation,
fullFlushOp: BrotliEncoder.Operation,
pub fn init(
flushOp: BrotliEncoder.Operation,
finishFlushOp: BrotliEncoder.Operation,
fullFlushOp: BrotliEncoder.Operation,
) !BrotliCompressionStream {
const instance = BrotliEncoder.createInstance(&BrotliAllocator.alloc, &BrotliAllocator.free, null) orelse return error.BrotliFailedToCreateInstance;
return BrotliCompressionStream{
.brotli = instance,
.flushOp = flushOp,
.finishFlushOp = finishFlushOp,
.fullFlushOp = fullFlushOp,
};
}
pub fn writeChunk(this: *BrotliCompressionStream, input: []const u8, last: bool) ![]const u8 {
this.total_in += input.len;
const result = this.brotli.compressStream(if (last) this.finishFlushOp else this.flushOp, input);
if (!result.success) {
this.state = .Error;
return error.BrotliCompressionError;
}
return result.output;
}
pub fn write(this: *BrotliCompressionStream, input: []const u8, last: bool) ![]const u8 {
if (this.state == .End or this.state == .Error) {
return "";
}
return this.writeChunk(input, last);
}
pub fn end(this: *BrotliCompressionStream) ![]const u8 {
defer this.state = .End;
return try this.write("", true);
}
pub fn deinit(this: *BrotliCompressionStream) void {
this.brotli.destroyInstance();
}
fn NewWriter(comptime InputWriter: type) type {
return struct {
compressor: *BrotliCompressionStream,
input_writer: InputWriter,
const Self = @This();
pub const WriteError = error{BrotliCompressionError} || InputWriter.Error;
pub const Writer = std.io.Writer(@This(), WriteError, Self.write);
pub fn init(compressor: *BrotliCompressionStream, input_writer: InputWriter) Self {
return Self{
.compressor = compressor,
.input_writer = input_writer,
};
}
pub fn write(self: Self, to_compress: []const u8) WriteError!usize {
const decompressed = try self.compressor.write(to_compress, false);
try self.input_writer.writeAll(decompressed);
return to_compress.len;
}
pub fn end(self: Self) !usize {
const decompressed = try self.compressor.end();
try self.input_writer.writeAll(decompressed);
}
pub fn writer(self: Self) Writer {
return Writer{ .context = self };
}
};
}
pub fn writerContext(this: *BrotliCompressionStream, writable: anytype) NewWriter(@TypeOf(writable)) {
return NewWriter(@TypeOf(writable)).init(this, writable);
}
pub fn writer(this: *BrotliCompressionStream, writable: anytype) NewWriter(@TypeOf(writable)).Writer {
return this.writerContext(writable).writer();
}
};