Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore bits that were introduced as padding in transformUnitSize #41

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions extensions/essentials/src/binary-slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ const execute: OperationExecuteExport = (request) => {

// Transform the array of bytes to an array of bits and slice them
// precisely to create the bit slice
const bitSlice = transformUnitSize(byteSlice, 8, 1).slice(
const bitSlice = transformUnitSize(byteSlice, 8, 1, true).slice(
bitSliceArgs.start - byteSliceStart * 8,
bitSliceArgs.end - byteSliceStart * 8
)

// Fill up the first byte with zero bits and turn the bits back into bytes
const paddingBits = bitSlice.length % 8 > 0 ? 8 - bitSlice.length % 8 : 0
const sliceBitsPadded = new Array(paddingBits).fill(0).concat(bitSlice)
const sliceBytes = transformUnitSize(sliceBitsPadded, 1, 8)
const sliceBytes = transformUnitSize(sliceBitsPadded, 8, 1, false)
slice = new Uint8Array(sliceBytes).buffer
break
}
Expand Down
4 changes: 2 additions & 2 deletions extensions/essentials/src/binary-to-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ const execute: OperationExecuteExport = (request) => {
))

// Transform unit size (encode)
const encodedUnits = transformUnitSize(units, inUnitSize, outUnitSize)
const encodedUnits = transformUnitSize(units, inUnitSize, outUnitSize, true)
const encodedCodePoints = encodedUnits.map(unit => alphabet[unit])

// Apply padding
Expand Down Expand Up @@ -195,7 +195,7 @@ const execute: OperationExecuteExport = (request) => {
encodedUnits = encodedUnits.slice(0, j)

// Transform unit size (decode)
const units = transformUnitSize(encodedUnits, outUnitSize, inUnitSize)
const units = transformUnitSize(encodedUnits, inUnitSize, outUnitSize, false)
const buffer = Uint8Array.from(units).buffer

// Add an issue if foreign characters were encountered
Expand Down
18 changes: 16 additions & 2 deletions extensions/essentials/src/lib/binary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ export const isBigEndianEnvironment = (): boolean =>
export const transformUnitSize = (
inputUnits: number[],
inUnitSize: number,
outUnitSize: number
outUnitSize: number,
forward: boolean
): number[] => {
if (inUnitSize === outUnitSize) {
return inputUnits
Expand All @@ -84,11 +85,18 @@ export const transformUnitSize = (
return []
}

// Some bits may not hold any information when decoding, see issue #40
if (!forward) {
[inUnitSize, outUnitSize] = [outUnitSize, inUnitSize]
}

const commonSize = lcm(inUnitSize, outUnitSize)
const inUnits = commonSize / inUnitSize
const outUnits = commonSize / outUnitSize

const outputUnitsLength = Math.ceil((outUnits * inputUnits.length) / inUnits)
const outputUnitsLength = forward
? Math.ceil((outUnits * inputUnits.length) / inUnits)
: Math.floor((outUnits * inputUnits.length) / inUnits)
const outputUnits = new Array<number>(outputUnitsLength)

let remainingInBits, inUnit, moveBits
Expand All @@ -104,6 +112,12 @@ export const transformUnitSize = (
// If the current out unit is full, move to the next one
if (remainingOutBits === 0) {
outputUnits[o++] = outUnit
// If the remaining input bits contain no information, don't add a null unit (only occurs when decoding)
// TODO: This works, but I, anderium, don't like that this `if` executes on each iteration.
if (o === outputUnitsLength) {
o--
break
}
outUnit = 0
remainingOutBits = outUnitSize
}
Expand Down