@@ -6,11 +6,9 @@ class CharSet {
6
6
this . ndxFn = ndxFn
7
7
this . len = chars . length
8
8
this . entropyPerChar = Math . floor ( Math . log2 ( this . len ) )
9
-
10
9
if ( this . entropyPerChar != Math . log2 ( this . len ) ) {
11
10
throw new Error ( 'EntropyString only supports CharSets with a power of 2 characters' )
12
11
}
13
-
14
12
this . charsPerChunk = lcm ( this . entropyPerChar , 8 ) / this . entropyPerChar
15
13
}
16
14
@@ -20,7 +18,6 @@ class CharSet {
20
18
if ( len != this . len ) {
21
19
throw new Error ( 'Invalid character count' )
22
20
}
23
-
24
21
// Ensure no repeated characters
25
22
for ( let i = 0 ; i < len ; i ++ ) {
26
23
let c = chars . charAt ( i )
@@ -30,7 +27,6 @@ class CharSet {
30
27
}
31
28
}
32
29
}
33
-
34
30
this . chars = chars
35
31
}
36
32
}
@@ -44,42 +40,46 @@ const _ndx32 = (chunk, slice, bytes) => {
44
40
}
45
41
46
42
const _ndx16 = ( chunk , slice , bytes ) => {
47
- return ( ( bytes [ chunk ] << ( 4 * slice ) ) & 0xff ) >> 4
43
+ return _ndxDiv ( chunk , slice , bytes , 4 )
48
44
}
49
-
45
+
50
46
const _ndx8 = ( chunk , slice , bytes ) => {
51
47
return _ndxGen ( chunk , slice , bytes , 3 )
52
48
}
53
49
54
50
const _ndx4 = ( chunk , slice , bytes ) => {
55
- return ( ( bytes [ chunk ] << ( 2 * slice ) ) & 0xff ) >> 6
51
+ return _ndxDiv ( chunk , slice , bytes , 2 )
56
52
}
57
53
58
54
const _ndx2 = ( chunk , slice , bytes ) => {
59
- return ( ( bytes [ chunk ] << slice ) & 0xff ) >> 7
55
+ return _ndxDiv ( chunk , slice , bytes , 1 )
60
56
}
61
57
62
- const _ndxGen = ( chunk , slice , bytes , bitsPerSlice ) => {
58
+ const _ndxGen = ( chunk , slice , bytes , entropyPerChar ) => {
63
59
let bitsPerByte = 8
64
- let slicesPerChunk = lcm ( bitsPerSlice , bitsPerByte ) / bitsPerByte
65
-
60
+ let slicesPerChunk = lcm ( entropyPerChar , bitsPerByte ) / bitsPerByte
66
61
let bNum = chunk * slicesPerChunk
67
-
68
- let rShift = bitsPerByte - bitsPerSlice
69
62
70
- let lOffset = Math . floor ( ( slice * bitsPerSlice ) / bitsPerByte )
71
- let lShift = ( slice * bitsPerSlice ) % bitsPerByte
63
+ let rShift = bitsPerByte - entropyPerChar
64
+ let lOffset = Math . floor ( ( slice * entropyPerChar ) / bitsPerByte )
65
+ let lShift = ( slice * entropyPerChar ) % bitsPerByte
72
66
73
67
let ndx = ( ( bytes [ bNum + lOffset ] << lShift ) & 0xff ) >> rShift
74
68
75
- let rOffset = Math . ceil ( ( slice * bitsPerSlice ) / bitsPerByte )
76
- let rShiftIt = ( ( rOffset + 1 ) * bitsPerByte - ( slice + 1 ) * bitsPerSlice ) % bitsPerByte
69
+ let rOffset = Math . ceil ( ( slice * entropyPerChar ) / bitsPerByte )
70
+ let rShiftIt = ( ( rOffset + 1 ) * bitsPerByte - ( slice + 1 ) * entropyPerChar ) % bitsPerByte
77
71
if ( rShift < rShiftIt ) {
78
72
ndx += bytes [ bNum + rOffset ] >> rShiftIt
79
73
}
80
74
return ndx
81
75
}
82
76
77
+ const _ndxDiv = ( chunk , slice , bytes , entropyPerChar ) => {
78
+ let lShift = entropyPerChar
79
+ let rShift = 8 - entropyPerChar
80
+ return ( ( bytes [ chunk ] << ( lShift * slice ) ) & 0xff ) >> rShift
81
+ }
82
+
83
83
const charSet64 =
84
84
new CharSet ( "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_" , _ndx64 )
85
85
const charSet32 =
0 commit comments