1
1
const std = @import ("std" );
2
2
const Allocator = std .mem .Allocator ;
3
- const crc32 = std .hash .crc ;
3
+ const crc32 = std .hash .crc . Crc32Iscsi ;
4
4
const mem = std .mem ;
5
5
const testing = std .testing ;
6
6
@@ -38,8 +38,9 @@ const SnappyError = error{
38
38
// Perform the CRC hash per the snappy documentation. We must use wrapping addition since this is
39
39
// the default behavior in other languages.
40
40
fn crc (b : []const u8 ) u32 {
41
- const c = crc32 .Crc32SmallWithPoly (.Castagnoli );
42
- const hash = c .hash (b );
41
+ var c = crc32 .init ();
42
+ c .update (b );
43
+ const hash = c .final ();
43
44
return @as (u32 , hash >> 15 | hash << 17 ) +% 0xa282ead8 ;
44
45
}
45
46
@@ -172,7 +173,7 @@ fn runDecode(dst: []u8, src: []const u8) u8 {
172
173
return 1 ;
173
174
}
174
175
175
- mem .copy (u8 , dst [d .. ], src [s .. s + @as (usize , @intCast (length ))]);
176
+ std . mem .copyForwards (u8 , dst [d .. ], src [s .. s + @as (usize , @intCast (length ))]);
176
177
const l = @as (usize , @intCast (length ));
177
178
d += l ;
178
179
s += l ;
@@ -217,14 +218,14 @@ fn runDecode(dst: []u8, src: []const u8) u8 {
217
218
218
219
if (offset >= length ) {
219
220
const upper_bound = d - @as (usize , @intCast (offset )) + @as (usize , @intCast (length ));
220
- mem .copy (u8 , dst [d .. d + @as (usize , @intCast (length ))], dst [d - @as (usize , @intCast (offset )) .. upper_bound ]);
221
+ std . mem .copyForwards (u8 , dst [d .. d + @as (usize , @intCast (length ))], dst [d - @as (usize , @intCast (offset )) .. upper_bound ]);
221
222
d += @as (usize , @intCast (length ));
222
223
continue ;
223
224
}
224
225
225
226
var a = dst [d .. d + @as (usize , @intCast (length ))];
226
227
var b = dst [d - @as (usize , @intCast (offset )) .. ];
227
- var aLen = a .len ;
228
+ const aLen = a .len ;
228
229
b = b [0.. aLen ];
229
230
for (a , 0.. ) | _ , i | {
230
231
a [i ] = b [i ];
@@ -244,11 +245,11 @@ fn runDecode(dst: []u8, src: []const u8) u8 {
244
245
pub fn decode (allocator : Allocator , src : []const u8 ) ! []u8 {
245
246
const block = try decodedLen (src );
246
247
247
- var dst = try allocator .alloc (u8 , block .blockLen );
248
+ const dst = try allocator .alloc (u8 , block .blockLen );
248
249
errdefer allocator .free (dst );
249
250
250
251
// Skip past how many bytes we read to get the length.
251
- var s = src [block .headerLen .. ];
252
+ const s = src [block .headerLen .. ];
252
253
253
254
if (runDecode (dst , s ) != 0 ) {
254
255
return SnappyError .Corrupt ;
@@ -278,7 +279,7 @@ fn emitLiteral(dst: []u8, lit: []const u8) usize {
278
279
i = 3 ;
279
280
},
280
281
}
281
- mem .copy (u8 , dst [i .. ], lit );
282
+ std . mem .copyForwards (u8 , dst [i .. ], lit );
282
283
283
284
return i + @min (dst .len , lit .len );
284
285
}
@@ -346,7 +347,7 @@ fn encodeBlock(dst: []u8, src: []u8) usize {
346
347
}
347
348
348
349
var table = mem .zeroes ([maxTableSize ]u16 );
349
- var sLimit = src .len - inputMargin ;
350
+ const sLimit = src .len - inputMargin ;
350
351
var nextEmit : usize = 0 ;
351
352
var s : usize = 1 ;
352
353
var nextHash = snappyHash (load32 (src , @as (isize , @intCast (s ))), shift );
@@ -358,7 +359,7 @@ fn encodeBlock(dst: []u8, src: []u8) usize {
358
359
359
360
inner : while (true ) {
360
361
s = nextS ;
361
- var bytesBetweenHashLookups = skip >> 5 ;
362
+ const bytesBetweenHashLookups = skip >> 5 ;
362
363
nextS = s + @as (usize , @intCast (bytesBetweenHashLookups ));
363
364
skip += bytesBetweenHashLookups ;
364
365
if (nextS > sLimit ) {
@@ -375,7 +376,7 @@ fn encodeBlock(dst: []u8, src: []u8) usize {
375
376
d += emitLiteral (dst [d .. ], src [nextEmit .. s ]);
376
377
377
378
while (true ) {
378
- var base = s ;
379
+ const base = s ;
379
380
s += 4 ;
380
381
var i = @as (usize , @intCast (candidate + 4 ));
381
382
while (s < src .len and src [i ] == src [s ]) {
@@ -389,10 +390,10 @@ fn encodeBlock(dst: []u8, src: []u8) usize {
389
390
break :outer ;
390
391
}
391
392
392
- var x = load64 (src , @as (isize , @intCast (s - 1 )));
393
- var prevHash = snappyHash (@as (u32 , @truncate (x >> 0 )), shift );
393
+ const x = load64 (src , @as (isize , @intCast (s - 1 )));
394
+ const prevHash = snappyHash (@as (u32 , @truncate (x >> 0 )), shift );
394
395
table [prevHash & tableMask ] = @as (u16 , @intCast (s - 1 ));
395
- var currHash = snappyHash (@as (u32 , @truncate (x >> 8 )), shift );
396
+ const currHash = snappyHash (@as (u32 , @truncate (x >> 8 )), shift );
396
397
candidate = @as (isize , @intCast (table [currHash & tableMask ]));
397
398
table [currHash & tableMask ] = @as (u16 , @intCast (s ));
398
399
if (@as (u32 , @truncate (x >> 8 )) != load32 (src , candidate )) {
@@ -425,7 +426,7 @@ pub fn encode(allocator: Allocator, src: []u8) ![]u8 {
425
426
426
427
while (mutSrc .len > 0 ) {
427
428
var p = try allocator .alloc (u8 , mutSrc .len );
428
- mem .copy (u8 , p , mutSrc );
429
+ std . mem .copyForwards (u8 , p , mutSrc );
429
430
var empty = [_ ]u8 {};
430
431
mutSrc = empty [0.. ];
431
432
if (p .len > maxBlockSize ) {
@@ -440,8 +441,8 @@ pub fn encode(allocator: Allocator, src: []u8) ![]u8 {
440
441
allocator .free (p );
441
442
}
442
443
443
- var output = try allocator .alloc (u8 , d );
444
- mem .copy (u8 , output , dst [0.. d ]);
444
+ const output = try allocator .alloc (u8 , d );
445
+ std . mem .copyForwards (u8 , output , dst [0.. d ]);
445
446
allocator .free (dst );
446
447
447
448
return output ;
@@ -463,7 +464,8 @@ pub fn maxEncodedLen(srcLen: usize) isize {
463
464
}
464
465
465
466
test "snappy crc" {
466
- try testing .expect (crc ("snappy" ) == 0x293d0c23 );
467
+ const snappycrc = crc ("snappy" );
468
+ try testing .expect (snappycrc == 0x293d0c23 );
467
469
}
468
470
469
471
test "decoding variable integers" {
@@ -481,17 +483,18 @@ test "simple encode" {
481
483
const allocator = testing .allocator ;
482
484
483
485
var input : [4 ]u8 = [_ ]u8 { 't' , 'h' , 'i' , 's' };
484
- var i : []u8 = & input ;
485
- var output = try encode (allocator , i );
486
+ const i : []u8 = & input ;
487
+ const output = try encode (allocator , i );
488
+
486
489
defer allocator .free (output );
487
490
488
491
try testing .expectEqualSlices (u8 , output , "\x04\x0c this" );
489
492
}
490
493
491
494
test "simple decode" {
492
495
const allocator = testing .allocator ;
493
-
494
- const decoded = try decode (allocator , " \x19\x1c oh snap, \x05\x06 ,py is cool! \x0a " );
496
+ const encodedbytes = " \x19\x1c oh snap, \x05\x06 ,py is cool! \x0a " ;
497
+ const decoded = try decode (allocator , encodedbytes );
495
498
defer allocator .free (decoded );
496
499
497
500
try testing .expectEqualSlices (u8 , decoded , "oh snap, snappy is cool!\n " );
0 commit comments