-
Notifications
You must be signed in to change notification settings - Fork 3
/
lz4.dart
288 lines (247 loc) · 9.67 KB
/
lz4.dart
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
288
import 'dart:ffi';
import 'dart:typed_data';
import 'package:collection/collection.dart';
import 'package:dart_native_compression/utils/uint8_list_utils.dart';
import 'package:ffi/ffi.dart';
typedef GetVersionNumberNative = Int32 Function();
typedef GetVersionNumber = int Function();
typedef GetVersionStringNative = Pointer<Utf8> Function();
typedef GetVersionString = Pointer<Utf8> Function();
typedef GetFrameVersionNumberNative = Uint64 Function();
typedef GetFrameVersionNumber = int Function();
typedef GetCompressFrameBoundNative = Uint64 Function(Uint64);
typedef GetCompressFrameBound = int Function(int);
typedef CompressFrameNative = Uint64 Function(
Pointer<Uint8>, Uint64, Pointer<Uint8>, Uint64);
typedef CompressFrame = int Function(Pointer<Uint8>, int, Pointer<Uint8>, int);
typedef DecompressFrameNative = Uint64 Function(
Pointer, Pointer<Uint8>, Pointer<Uint64>, Pointer<Uint8>, Pointer<Uint64>);
typedef DecompressFrame = int Function(
Pointer, Pointer<Uint8>, Pointer<Uint64>, Pointer<Uint8>, Pointer<Uint64>);
/// Lz4 utility class
class Lz4Lib {
/// Construct Lz4Lib with DynamicLibrary
Lz4Lib(DynamicLibrary lib) {
getVersionNumber =
lib.lookupFunction<GetVersionNumberNative, GetVersionNumber>(
'ffi_lz4_version_number');
_getVersionString =
lib.lookupFunction<GetVersionStringNative, GetVersionString>(
'ffi_lz4_version_string');
getFrameVersionNumber =
lib.lookupFunction<GetFrameVersionNumberNative, GetFrameVersionNumber>(
'ffi_lz4f_get_version');
getCompressFrameBound =
lib.lookupFunction<GetCompressFrameBoundNative, GetCompressFrameBound>(
'ffi_lz4f_compress_frame_bound');
_compressFrame = lib.lookupFunction<CompressFrameNative, CompressFrame>(
'ffi_lz4f_compress_frame');
_createDecompressionContext =
lib.lookupFunction<Uint64 Function(Pointer), int Function(Pointer)>(
'ffi_lz4f_create_decompression_context');
_freeDecompressionContext =
lib.lookupFunction<Uint64 Function(Pointer), int Function(Pointer)>(
'ffi_lz4f_free_decompression_context');
_getFrameHeaderSize = lib.lookupFunction<
Uint64 Function(Pointer<Uint8>, Uint64),
int Function(Pointer<Uint8>, int)>('ffi_lz4f_header_size');
_decompressFrame =
lib.lookupFunction<DecompressFrameNative, DecompressFrame>(
'ffi_lz4f_decompress');
}
/// Get lz4 version number
late GetVersionNumber getVersionNumber;
late GetVersionString _getVersionString;
/// Get lz4 version string
String getVersionString() {
final ptr = _getVersionString();
return ptr.toDartString();
}
/// Get lz4 frame version number
late GetFrameVersionNumber getFrameVersionNumber;
/// Get compression frame bound
late GetCompressFrameBound getCompressFrameBound;
late CompressFrame _compressFrame;
/// Compression data into lz4 frame
Uint8List compressFrame(Uint8List data) {
final bound = getCompressFrameBound(data.length);
final srcBuffer = data.getPointer();
try {
final dstBuffer = malloc.allocate<Uint8>(bound);
final compressedLength =
_compressFrame(dstBuffer, bound, srcBuffer, data.length);
return dstBuffer.asTypedList(compressedLength);
} finally {
malloc.free(srcBuffer);
}
}
late int Function(Pointer) _createDecompressionContext;
late int Function(Pointer) _freeDecompressionContext;
late int Function(Pointer<Uint8>, int) _getFrameHeaderSize;
late DecompressFrame _decompressFrame;
/// Decompression data from lz4 frame
Uint8List decompressFrame(Uint8List data) {
if (!ListEquality().equals(_magickHeader, data.sublist(0, 4)) ||
data.length < 7) {
throw Exception('Invalid data');
}
final srcBuffer = data.getPointer();
final estimateDstBufferSize =
_validateFrameAndGetEstimatedDecodeBufferSize(data, srcBuffer);
final contextPtr = malloc.allocate<Uint64>(1);
_createDecompressionContext(contextPtr);
final context = Pointer.fromAddress(contextPtr[0]);
final dstSizePtr = malloc.allocate<Uint64>(1);
final srcSizePtr = malloc.allocate<Uint64>(1);
final dstBuffer = malloc.allocate<Uint8>(estimateDstBufferSize);
try {
srcSizePtr.value = data.length;
int srcPtrOffset = 0;
int nextSrcSize = 0;
final decompressed = BytesBuilder(copy: true);
do {
dstSizePtr.value = estimateDstBufferSize;
nextSrcSize = _decompressFrame(context, dstBuffer, dstSizePtr,
srcBuffer.elementAt(srcPtrOffset), srcSizePtr);
final srcSize = srcSizePtr.value;
srcPtrOffset += srcSize;
final dstSize = dstSizePtr.value;
decompressed.add(dstBuffer.asTypedList(dstSize));
} while (nextSrcSize > 0);
return decompressed.takeBytes();
} finally {
_freeDecompressionContext(context);
malloc.free(contextPtr);
malloc.free(dstSizePtr);
malloc.free(srcSizePtr);
malloc.free(srcBuffer);
malloc.free(dstBuffer);
}
}
/// Decompression data from lz4 frame with stream api
/// https://github.com/lz4/lz4/blob/dev/doc/lz4_Frame_format.md
Stream<Uint8List> decompressFrameStream(Stream<Uint8List> stream) async* {
final contextPtr = malloc.allocate<Uint64>(1);
_createDecompressionContext(contextPtr);
final context = Pointer.fromAddress(contextPtr[0]);
final dstSizePtr = malloc.allocate<Uint64>(1);
final srcSizePtr = malloc.allocate<Uint64>(1);
var estimateDstBufferSize = 0;
Pointer<Uint8>? dstBuffer;
int nextSrcSize = 0;
NativeBytesBuilder? sourceBufferBuilder;
List<NativeBytesBuilder> danglePointers = [];
try {
var remainder = BytesBuilder(copy: true);
await for (final chunk in stream) {
if (sourceBufferBuilder == null) {
sourceBufferBuilder = NativeBytesBuilder(chunk.length);
sourceBufferBuilder.add(chunk);
estimateDstBufferSize = _validateFrameAndGetEstimatedDecodeBufferSize(
chunk, sourceBufferBuilder.ptr);
dstBuffer = malloc.allocate<Uint8>(estimateDstBufferSize);
} else {
var r = sourceBufferBuilder.add(chunk);
if (r != null && r.isNotEmpty) {
remainder.add(r);
}
}
while (sourceBufferBuilder!.length >= nextSrcSize) {
srcSizePtr.value = sourceBufferBuilder.length;
dstSizePtr.value = estimateDstBufferSize;
nextSrcSize = _decompressFrame(context, dstBuffer!, dstSizePtr,
sourceBufferBuilder.ptr, srcSizePtr);
final dstSize = dstSizePtr.value;
if (dstSize > 0) {
final decompressedChunkBuilder = BytesBuilder(copy: true);
decompressedChunkBuilder.add(dstBuffer.asTypedList(dstSize));
yield decompressedChunkBuilder.takeBytes();
}
if (nextSrcSize > 0) {
final consumedSrcSize = srcSizePtr.value;
if (consumedSrcSize < sourceBufferBuilder.length) {
if (consumedSrcSize + nextSrcSize <
sourceBufferBuilder.capacity) {
danglePointers.add(sourceBufferBuilder);
sourceBufferBuilder =
sourceBufferBuilder.shift(consumedSrcSize);
} else {
remainder.add(
sourceBufferBuilder.asTypedList().sublist(consumedSrcSize));
sourceBufferBuilder.free();
sourceBufferBuilder = NativeBytesBuilder(nextSrcSize);
}
} else {
sourceBufferBuilder.free();
sourceBufferBuilder = NativeBytesBuilder(nextSrcSize);
}
if (remainder.length > 0) {
final r = sourceBufferBuilder.add(remainder.takeBytes());
remainder.clear();
if (r != null && r.isNotEmpty) {
remainder.add(r);
}
}
} else {
return;
}
}
}
} finally {
_freeDecompressionContext(context);
malloc.free(contextPtr);
malloc.free(dstSizePtr);
malloc.free(srcSizePtr);
if (dstBuffer != null) {
malloc.free(dstBuffer);
}
if (sourceBufferBuilder != null) {
sourceBufferBuilder.free();
}
for (final p in danglePointers) {
p.free();
}
}
}
final List<int> _magickHeader = [4, 34, 77, 24];
final Map<int, int> _blockSizeTable = {
4: 1024 * 64,
5: 1024 * 256,
6: 1024 * 1024,
7: 1024 * 1024 * 4,
};
int _validateFrameAndGetEstimatedDecodeBufferSize(
Uint8List chunk, Pointer<Uint8> chunkPtr) {
if (!ListEquality().equals(_magickHeader, chunk.sublist(0, 4)) ||
chunk.length < 7) {
throw Exception('First chunk too small');
}
var estimateDstBufferSize = 0;
final flagByte = chunk[4];
final hasContentSize = flagByte & 0x08 > 0;
final hasDictId = flagByte & 0x01 > 0;
final headerSize = _getFrameHeaderSize(chunkPtr, chunk.length);
if (hasContentSize) {
var contentSizeBytes = headerSize - 3;
if (hasDictId) {
contentSizeBytes -= 4;
}
if (contentSizeBytes > 0) {
for (var i = 0; i < contentSizeBytes; i++) {
estimateDstBufferSize += (chunk[6 + i] << (8 * i));
}
}
}
final blockMaxSizeKey = chunk[5] >> 4 & 0x07;
if (_blockSizeTable.containsKey(blockMaxSizeKey)) {
final blockMaxSize = _blockSizeTable[blockMaxSizeKey]!;
if (estimateDstBufferSize == 0 || estimateDstBufferSize > blockMaxSize) {
estimateDstBufferSize = blockMaxSize;
}
}
if (estimateDstBufferSize == 0) {
estimateDstBufferSize = _blockSizeTable[7]!;
}
return estimateDstBufferSize;
}
}