@@ -6,11 +6,9 @@ class CharSet {
66 this . ndxFn = ndxFn
77 this . len = chars . length
88 this . entropyPerChar = Math . floor ( Math . log2 ( this . len ) )
9-
109 if ( this . entropyPerChar != Math . log2 ( this . len ) ) {
1110 throw new Error ( 'EntropyString only supports CharSets with a power of 2 characters' )
1211 }
13-
1412 this . charsPerChunk = lcm ( this . entropyPerChar , 8 ) / this . entropyPerChar
1513 }
1614
@@ -20,7 +18,6 @@ class CharSet {
2018 if ( len != this . len ) {
2119 throw new Error ( 'Invalid character count' )
2220 }
23-
2421 // Ensure no repeated characters
2522 for ( let i = 0 ; i < len ; i ++ ) {
2623 let c = chars . charAt ( i )
@@ -30,7 +27,6 @@ class CharSet {
3027 }
3128 }
3229 }
33-
3430 this . chars = chars
3531 }
3632}
@@ -44,42 +40,46 @@ const _ndx32 = (chunk, slice, bytes) => {
4440}
4541
4642const _ndx16 = ( chunk , slice , bytes ) => {
47- return ( ( bytes [ chunk ] << ( 4 * slice ) ) & 0xff ) >> 4
43+ return _ndxDiv ( chunk , slice , bytes , 4 )
4844}
49-
45+
5046const _ndx8 = ( chunk , slice , bytes ) => {
5147 return _ndxGen ( chunk , slice , bytes , 3 )
5248}
5349
5450const _ndx4 = ( chunk , slice , bytes ) => {
55- return ( ( bytes [ chunk ] << ( 2 * slice ) ) & 0xff ) >> 6
51+ return _ndxDiv ( chunk , slice , bytes , 2 )
5652}
5753
5854const _ndx2 = ( chunk , slice , bytes ) => {
59- return ( ( bytes [ chunk ] << slice ) & 0xff ) >> 7
55+ return _ndxDiv ( chunk , slice , bytes , 1 )
6056}
6157
62- const _ndxGen = ( chunk , slice , bytes , bitsPerSlice ) => {
58+ const _ndxGen = ( chunk , slice , bytes , entropyPerChar ) => {
6359 let bitsPerByte = 8
64- let slicesPerChunk = lcm ( bitsPerSlice , bitsPerByte ) / bitsPerByte
65-
60+ let slicesPerChunk = lcm ( entropyPerChar , bitsPerByte ) / bitsPerByte
6661 let bNum = chunk * slicesPerChunk
67-
68- let rShift = bitsPerByte - bitsPerSlice
6962
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
7266
7367 let ndx = ( ( bytes [ bNum + lOffset ] << lShift ) & 0xff ) >> rShift
7468
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
7771 if ( rShift < rShiftIt ) {
7872 ndx += bytes [ bNum + rOffset ] >> rShiftIt
7973 }
8074 return ndx
8175}
8276
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+
8383const charSet64 =
8484 new CharSet ( "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_" , _ndx64 )
8585const charSet32 =
0 commit comments