Skip to content

Commit 851f837

Browse files
committed
Add bytesNeeded
1 parent fc65c3a commit 851f837

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

lib/charSet.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import lcm from './lcm'
22
import WeakMap from 'weak-map'
33

44
const propMap = new WeakMap()
5-
const bitsPerByte = 8
5+
6+
const BITS_PER_BYTE = 8
67

78
export default class CharSet {
89
constructor(chars) {
@@ -28,7 +29,7 @@ export default class CharSet {
2829
bitsPerChar,
2930
length,
3031
ndxFn: _ndxFn(bitsPerChar),
31-
charsPerChunk: lcm(bitsPerChar, bitsPerByte) / bitsPerChar
32+
charsPerChunk: lcm(bitsPerChar, BITS_PER_BYTE) / bitsPerChar
3233
}
3334
propMap.set(this, privProps)
3435
}
@@ -57,38 +58,44 @@ export default class CharSet {
5758
return propMap.get(this).ndxFn
5859
}
5960

61+
bytesNeeded(entropyBits) {
62+
const count = Math.ceil(entropyBits / this.bitsPerChar())
63+
return Math.ceil(count * this.bitsPerChar() / BITS_PER_BYTE)
64+
}
65+
6066
// Aliases
6167
chars() { return this.getChars() }
6268
ndxFn() { return this.getNdxFn() }
69+
bitsPerChar() { return this.getBitsPerChar() }
6370
}
6471

6572
const _ndxFn = (bitsPerChar) => {
66-
// If bitsPerBytes is a multiple of bitsPerChar, we can slice off an integer number
73+
// If BITS_PER_BYTEs is a multiple of bitsPerChar, we can slice off an integer number
6774
// of chars per byte.
68-
if (lcm(bitsPerChar, bitsPerByte) === bitsPerByte) {
75+
if (lcm(bitsPerChar, BITS_PER_BYTE) === BITS_PER_BYTE) {
6976
return function(chunk, slice, bytes) {
7077
let lShift = bitsPerChar
71-
let rShift = bitsPerByte - bitsPerChar
78+
let rShift = BITS_PER_BYTE - bitsPerChar
7279
return ((bytes[chunk]<<(lShift*slice))&0xff)>>rShift
7380
}
7481
}
7582
// Otherwise, while slicing off bits per char, we will possibly straddle a couple
7683
// of bytes, so a bit more work is involved
7784
else {
7885
return function(chunk, slice, bytes) {
79-
let slicesPerChunk = lcm(bitsPerChar, bitsPerByte) / bitsPerByte
86+
let slicesPerChunk = lcm(bitsPerChar, BITS_PER_BYTE) / BITS_PER_BYTE
8087
let bNum = chunk * slicesPerChunk
8188

82-
let offset = (slice*bitsPerChar)/bitsPerByte
89+
let offset = (slice*bitsPerChar)/BITS_PER_BYTE
8390
let lOffset = Math.floor(offset)
8491
let rOffset = Math.ceil(offset)
8592

86-
let rShift = bitsPerByte - bitsPerChar
87-
let lShift = (slice*bitsPerChar) % bitsPerByte
93+
let rShift = BITS_PER_BYTE - bitsPerChar
94+
let lShift = (slice*bitsPerChar) % BITS_PER_BYTE
8895

8996
let ndx = ((bytes[bNum+lOffset]<<lShift)&0xff)>>rShift
9097

91-
let rShiftIt = ((rOffset+1)*bitsPerByte - (slice+1)*bitsPerChar) % bitsPerByte
98+
let rShiftIt = ((rOffset+1)*BITS_PER_BYTE - (slice+1)*bitsPerChar) % BITS_PER_BYTE
9299
if (rShift < rShiftIt) {
93100
ndx += bytes[bNum+rOffset]>>rShiftIt
94101
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy