@@ -15,36 +15,33 @@ interface EncodingMeta {
15
15
16
16
interface EncodingBlock extends EncodingMeta {
17
17
indices : number [ ]
18
- data : Uint8Array
18
+ data : Uint32Array
19
19
}
20
20
21
- export function blockToBinary ( block : EncodingBlock ) : Uint8Array {
21
+ export function blockToBinary ( block : EncodingBlock ) : Uint32Array {
22
22
const { k, length, sum, indices, data } = block
23
- const header = new Uint32Array ( [
23
+ const header = [
24
24
indices . length ,
25
25
...indices ,
26
26
k ,
27
27
length ,
28
28
sum ,
29
- ] )
30
- const binary = new Uint8Array ( header . length * 4 + data . length )
31
- let offset = 0
32
- binary . set ( new Uint8Array ( header . buffer ) , offset )
33
- offset += header . length * 4
34
- binary . set ( data , offset )
29
+ ]
30
+ const binary = new Uint32Array ( header . length + data . length )
31
+ binary . set ( header )
32
+ binary . set ( data , header . length )
35
33
return binary
36
34
}
37
35
38
- export function binaryToBlock ( binary : Uint8Array ) : EncodingBlock {
39
- const degree = new Uint32Array ( binary . buffer , 0 , 4 ) [ 0 ] !
40
- const headerRest = Array . from ( new Uint32Array ( binary . buffer , 4 , degree + 3 ) )
41
- const indices = headerRest . slice ( 0 , degree )
36
+ export function binaryToBlock ( binary : Uint32Array ) : EncodingBlock {
37
+ const degree = binary [ 0 ] !
38
+ const indices = Array . from ( binary . slice ( 1 , degree + 1 ) )
42
39
const [
43
40
k ,
44
41
length ,
45
42
sum ,
46
- ] = headerRest . slice ( degree ) as [ number , number , number ]
47
- const data = binary . slice ( 4 * ( degree + 4 ) )
43
+ ] = Array . from ( binary . slice ( degree + 1 ) ) as [ number , number , number ]
44
+ const data = binary . slice ( degree + 1 + 3 )
48
45
return {
49
46
k,
50
47
length,
@@ -55,7 +52,7 @@ export function binaryToBlock(binary: Uint8Array): EncodingBlock {
55
52
}
56
53
57
54
// CRC32 checksum
58
- function checksum ( data : Uint8Array ) : number {
55
+ function checksum ( data : Uint32Array ) : number {
59
56
let crc = 0xFFFFFFFF
60
57
for ( let i = 0 ; i < data . length ; i ++ ) {
61
58
crc = crc ^ data [ i ] !
@@ -92,25 +89,25 @@ function getRandomIndices(k: number, degree: number): number[] {
92
89
return Array . from ( indices )
93
90
}
94
91
95
- function xorUint8Array ( a : Uint8Array , b : Uint8Array ) : Uint8Array {
96
- const result = new Uint8Array ( a . length )
92
+ function xorUint32Array ( a : Uint32Array , b : Uint32Array ) : Uint32Array {
93
+ const result = new Uint32Array ( a . length )
97
94
for ( let i = 0 ; i < a . length ; i ++ ) {
98
95
result [ i ] = a [ i ] ! ^ b [ i ] !
99
96
}
100
97
return result
101
98
}
102
99
103
- function sliceData ( data : Uint8Array , blockSize : number ) : Uint8Array [ ] {
104
- const blocks : Uint8Array [ ] = [ ]
100
+ function sliceData ( data : Uint32Array , blockSize : number ) : Uint32Array [ ] {
101
+ const blocks : Uint32Array [ ] = [ ]
105
102
for ( let i = 0 ; i < data . length ; i += blockSize ) {
106
- const block = new Uint8Array ( blockSize )
103
+ const block = new Uint32Array ( blockSize )
107
104
block . set ( data . slice ( i , i + blockSize ) )
108
105
blocks . push ( block )
109
106
}
110
107
return blocks
111
108
}
112
109
113
- export function * encodeFountain ( data : Uint8Array , indiceSize : number ) : Generator < EncodingBlock > {
110
+ export function * encodeFountain ( data : Uint32Array , indiceSize : number ) : Generator < EncodingBlock > {
114
111
const sum = checksum ( data )
115
112
const indices = sliceData ( data , indiceSize )
116
113
const k = indices . length
@@ -123,10 +120,10 @@ export function *encodeFountain(data: Uint8Array, indiceSize: number): Generator
123
120
while ( true ) {
124
121
const degree = getRandomDegree ( k )
125
122
const selectedIndices = getRandomIndices ( k , degree )
126
- let encodedData = new Uint8Array ( indiceSize )
123
+ let encodedData = new Uint32Array ( indiceSize )
127
124
128
125
for ( const index of selectedIndices ) {
129
- encodedData = xorUint8Array ( encodedData , indices [ index ] ! )
126
+ encodedData = xorUint32Array ( encodedData , indices [ index ] ! )
130
127
}
131
128
132
129
yield {
@@ -142,7 +139,7 @@ export function createDecoder(blocks?: EncodingBlock[]) {
142
139
}
143
140
144
141
export class LtDecoder {
145
- public decodedData : ( Uint8Array | undefined ) [ ] = [ ]
142
+ public decodedData : ( Uint32Array | undefined ) [ ] = [ ]
146
143
public decodedCount = 0
147
144
public encodedBlocks : Set < EncodingBlock > = new Set ( )
148
145
public meta : EncodingBlock = undefined !
@@ -172,7 +169,7 @@ export class LtDecoder {
172
169
173
170
for ( const index of indices ) {
174
171
if ( this . decodedData [ index ] != null ) {
175
- data = xorUint8Array ( data , this . decodedData [ index ] ! )
172
+ data = xorUint32Array ( data , this . decodedData [ index ] ! )
176
173
indices = indices . filter ( i => i !== index )
177
174
}
178
175
}
@@ -190,16 +187,16 @@ export class LtDecoder {
190
187
return this . decodedCount === this . meta . k
191
188
}
192
189
193
- getDecoded ( ) : Uint8Array | undefined {
190
+ getDecoded ( ) : Uint32Array | undefined {
194
191
if ( this . decodedCount !== this . meta . k ) {
195
192
return
196
193
}
197
194
if ( this . decodedData . some ( block => block == null ) ) {
198
195
return
199
196
}
200
197
const indiceSize = this . meta . data . length
201
- const blocks = this . decodedData as Uint8Array [ ]
202
- const decodedData = new Uint8Array ( this . meta . length )
198
+ const blocks = this . decodedData as Uint32Array [ ]
199
+ const decodedData = new Uint32Array ( this . meta . length )
203
200
blocks . forEach ( ( block , i ) => {
204
201
const start = i * indiceSize
205
202
if ( start + indiceSize > decodedData . length ) {
@@ -219,8 +216,8 @@ export class LtDecoder {
219
216
}
220
217
}
221
218
222
- export function tringToUint8Array ( str : string ) : Uint8Array {
223
- const data = new Uint8Array ( str . length )
219
+ export function tringToUint32Array ( str : string ) : Uint32Array {
220
+ const data = new Uint32Array ( str . length )
224
221
for ( let i = 0 ; i < str . length ; i ++ ) {
225
222
data [ i ] = str . charCodeAt ( i )
226
223
}
0 commit comments