22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
ctx.input = ctx.buffer[:0]
// Forward declaration as it is required for recursion
var write func(data []byte) error
write = func(data []byte) error {
var (
err error
blockSize int = 8
bufferLength int = len(ctx.input)
)
// Force a flush if we are called with no data to write
if len(data) == 0 {
// Nothing to flush if the buffer is empty though
|
<
|
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
ctx.input = ctx.buffer[:0]
// Forward declaration as it is required for recursion
var write func(data []byte) error
write = func(data []byte) error {
var (
blockSize int = 8
bufferLength int = len(ctx.input)
)
// Force a flush if we are called with no data to write
if len(data) == 0 {
// Nothing to flush if the buffer is empty though
|
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
|
// ... otherwise just return
return nil
}
// The current buffer + new data overflow the block size
// Complete the block, flush it ...
ctx.input = append(ctx.input, data[:blockSize-bufferLength]...)
err = write(nil)
if err != nil {
return err
}
// ... and stage the rest of the data in the buffer
ctx.input = append(ctx.input, data[blockSize-bufferLength:]...)
return nil
}
var buf []byte = make([]byte, 1, blockSize+1)
for block := 0; block < len(data)/blockSize; block++ {
for i := 0; i < blockSize; i++ {
var current byte = data[(block*blockSize)+i]
if ctx.table[ctx.hash] == current {
// Guess was right - don't output
buf[0] |= 1 << uint(i)
} else {
// Guess was wrong, output char
ctx.table[ctx.hash] = current
buf = append(buf, current)
}
ctx.hash = (ctx.hash << 4) ^ uint16(current)
}
_, err = writer.Write(buf)
if err != nil {
return err
}
// Reset the flags and buffer for the next iteration
buf[0] ^= buf[0]
buf = buf[:1]
}
var remaining int = len(data) % blockSize
if remaining > 0 {
ctx.input = ctx.buffer[:remaining]
copy(ctx.input, data[len(data)-remaining:])
} else {
ctx.input = ctx.buffer[:0]
}
return nil
|
|
<
>
|
<
<
|
<
|
|
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
|
// ... otherwise just return
return nil
}
// The current buffer + new data overflow the block size
// Complete the block, flush it ...
ctx.input = append(ctx.input, data[:blockSize-bufferLength]...)
if err := write(nil); err != nil {
return err
}
// ... and stage the rest of the data in the buffer
ctx.input = append(ctx.input, data[blockSize-bufferLength:]...)
return nil
}
// TODO allocate this on ctx.buffer ...
var buf []byte = make([]byte, 1, blockSize+1)
for block := 0; block < len(data)/blockSize; block++ {
for i := 0; i < blockSize; i++ {
var current byte = data[(block*blockSize)+i]
if ctx.table[ctx.hash] == current {
// Guess was right - don't output
buf[0] |= 1 << uint(i)
} else {
// Guess was wrong, output char
ctx.table[ctx.hash] = current
buf = append(buf, current)
}
ctx.hash = (ctx.hash << 4) ^ uint16(current)
}
if _, err := writer.Write(buf); err != nil {
return err
}
// Reset the flags and buffer for the next iteration
buf, buf[0] = buf[:1], 0
}
if remaining := len(data) % blockSize; remaining > 0 {
ctx.input = ctx.buffer[:remaining]
copy(ctx.input, data[len(data)-remaining:])
} else {
ctx.input = ctx.buffer[:0]
}
return nil
|