File: combine.go

package info (click to toggle)
golang-github-dsnet-golib 0.0~git20171103.1ea1667-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 240 kB
  • sloc: makefile: 2
file content (220 lines) | stat: -rw-r--r-- 6,363 bytes parent folder | download | duplicates (2)
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
// Copyright 2015, Joe Tsai. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE.md file.

// Package hashmerge provides functionality for merging hashes.
package hashmerge

// The origin of the CombineAdler32, CombineCRC32, and CombineCRC64 functions
// in this package is the adler32_combine, crc32_combine, gf2_matrix_times,
// and gf2_matrix_square functions found in the zlib library and was translated
// from C to Go. Thanks goes to the authors of zlib:
//	Mark Adler and Jean-loup Gailly.
//
// See the following:
//	http://www.zlib.net/
//	https://github.com/madler/zlib/blob/master/adler32.c
//	https://github.com/madler/zlib/blob/master/crc32.c
//	https://stackoverflow.com/questions/23122312/crc-calculation-of-a-mostly-static-data-stream/23126768#23126768
//
// ====================================================
// Copyright (C) 1995-2013 Jean-loup Gailly and Mark Adler
//
// This software is provided 'as-is', without any express or implied
// warranty.  In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
//    claim that you wrote the original software. If you use this software
//    in a product, an acknowledgment in the product documentation would be
//    appreciated but is not required.
// 2. Altered source versions must be plainly marked as such, and must not be
//    misrepresented as being the original software.
// 3. This notice may not be removed or altered from any source distribution.
//
// Jean-loup Gailly        Mark Adler
// jloup@gzip.org          madler@alumni.caltech.edu
// ====================================================

// CombineAdler32 combines two Adler-32 checksums together.
// Let AB be the string concatenation of two strings A and B. Then Combine
// computes the checksum of AB given only the checksum of A, the checksum of B,
// and the length of B:
//	adler32.Checksum(AB) == CombineAdler32(adler32.Checksum(A), adler32.Checksum(B), len(B))
func CombineAdler32(adler1, adler2 uint32, len2 int64) uint32 {
	if len2 < 0 {
		panic("hashmerge: negative length")
	}

	const mod = 65521
	var sum1, sum2, rem uint32
	rem = uint32(len2 % mod)
	sum1 = adler1 & 0xffff
	sum2 = rem * sum1
	sum2 %= mod
	sum1 += (adler2 & 0xffff) + mod - 1
	sum2 += (adler1 >> 16) + (adler2 >> 16) + mod - rem
	if sum1 >= mod {
		sum1 -= mod
	}
	if sum1 >= mod {
		sum1 -= mod
	}
	if sum2 >= mod<<1 {
		sum2 -= mod << 1
	}
	if sum2 >= mod {
		sum2 -= mod
	}
	return sum1 | (sum2 << 16)
}

// CombineCRC32 combines two CRC-32 checksums together.
// Let AB be the string concatenation of two strings A and B. Then Combine
// computes the checksum of AB given only the checksum of A, the checksum of B,
// and the length of B:
//	tab := crc32.MakeTable(poly)
//	crc32.Checksum(AB, tab) == CombineCRC32(poly, crc32.Checksum(A, tab), crc32.Checksum(B, tab), len(B))
func CombineCRC32(poly, crc1, crc2 uint32, len2 int64) uint32 {
	if len2 < 0 {
		panic("hashmerge: negative length")
	}

	// Translation of gf2_matrix_times from zlib.
	var matrixMult = func(mat *[32]uint32, vec uint32) uint32 {
		var sum uint32
		for n := 0; n < 32 && vec > 0; n++ {
			if vec&1 > 0 {
				sum ^= mat[n]
			}
			vec >>= 1
		}
		return sum
	}

	// Translation of gf2_matrix_square from zlib.
	var matrixSquare = func(square, mat *[32]uint32) {
		for n := 0; n < 32; n++ {
			square[n] = matrixMult(mat, mat[n])
		}
	}

	// Even and odd power-of-two zeros operators.
	var even, odd [32]uint32

	// Put operator for one zero bit in odd.
	var row uint32 = 1
	odd[0] = poly
	for n := 1; n < 32; n++ {
		odd[n] = row
		row <<= 1
	}

	// Put operator for two zero bits in even.
	matrixSquare(&even, &odd)

	// Put operator for four zero bits in odd.
	matrixSquare(&odd, &even)

	// Apply len2 zeros to crc1 (first square will put the operator for one
	// zero byte, eight zero bits, in even).
	for {
		// Apply zeros operator for this bit of len2.
		matrixSquare(&even, &odd)
		if len2&1 > 0 {
			crc1 = matrixMult(&even, crc1)
		}
		len2 >>= 1
		if len2 == 0 {
			break
		}

		// Another iteration of the loop with odd and even swapped.
		matrixSquare(&odd, &even)
		if len2&1 > 0 {
			crc1 = matrixMult(&odd, crc1)
		}
		len2 >>= 1
		if len2 == 0 {
			break
		}
	}
	return crc1 ^ crc2
}

// CombineCRC64 combines two CRC-64 checksums together.
// Let AB be the string concatenation of two strings A and B. Then Combine
// computes the checksum of AB given only the checksum of A, the checksum of B,
// and the length of B:
//	tab := crc64.MakeTable(poly)
//	crc64.Checksum(AB, tab) == CombineCRC64(poly, crc64.Checksum(A, tab), crc64.Checksum(B, tab), len(B))
func CombineCRC64(poly, crc1, crc2 uint64, len2 int64) uint64 {
	if len2 < 0 {
		panic("hashmerge: negative length")
	}

	// Translation of gf2_matrix_times from zlib.
	var matrixMult = func(mat *[64]uint64, vec uint64) uint64 {
		var sum uint64
		for n := 0; n < 64 && vec > 0; n++ {
			if vec&1 > 0 {
				sum ^= mat[n]
			}
			vec >>= 1
		}
		return sum
	}

	// Translation of gf2_matrix_square from zlib.
	var matrixSquare = func(square, mat *[64]uint64) {
		for n := 0; n < 64; n++ {
			square[n] = matrixMult(mat, mat[n])
		}
	}

	// Even and odd power-of-two zeros operators.
	var even, odd [64]uint64

	// Put operator for one zero bit in odd.
	var row uint64 = 1
	odd[0] = poly
	for n := 1; n < 64; n++ {
		odd[n] = row
		row <<= 1
	}

	// Put operator for two zero bits in even.
	matrixSquare(&even, &odd)

	// Put operator for four zero bits in odd.
	matrixSquare(&odd, &even)

	// Apply len2 zeros to crc1 (first square will put the operator for one
	// zero byte, eight zero bits, in even).
	for {
		// Apply zeros operator for this bit of len2.
		matrixSquare(&even, &odd)
		if len2&1 > 0 {
			crc1 = matrixMult(&even, crc1)
		}
		len2 >>= 1
		if len2 == 0 {
			break
		}

		// Another iteration of the loop with odd and even swapped.
		matrixSquare(&odd, &even)
		if len2&1 > 0 {
			crc1 = matrixMult(&odd, crc1)
		}
		len2 >>= 1
		if len2 == 0 {
			break
		}
	}
	return crc1 ^ crc2
}