MCPcopy Create free account
hub / github.com/cryptii/cryptii / performDecode

Method performDecode

src/Encoder/BaudotCode.js:221–256  ·  view source on GitHub ↗

* Performs decode on given content. * @protected * @param {Chain} content * @return {number[]|string|Uint8Array|Chain|Promise} Decoded content

(content)

Source from the content-addressed store, hash-verified

219 * @return {number[]|string|Uint8Array|Chain|Promise} Decoded content
220 */
221 performDecode (content) {
222 // Retrieve character set
223 const variant = this.getSettingValue('variant')
224 const characterSet = this.getVariantCharacterSet(variant)
225 const switchToFigures = characterSet.indexOf('FS') % 32
226 const switchToLetters = characterSet.indexOf('LS') % 32
227
228 // Compose 5-bit tape from byte array
229 const tape = ArrayUtil.resizeBitSizedArray(content.getBytes(), 8, 5, true)
230 const result = new Array(tape.length)
231
232 // Go through tape and map each character to Unicode code points
233 let char, codePoint
234 let figureSet = false
235 let j = 0
236
237 for (let i = 0; i < tape.length; i++) {
238 char = tape[i]
239
240 if (!figureSet && char === switchToFigures) {
241 // Switch to figure set
242 figureSet = true
243 } else if (figureSet && char === switchToLetters) {
244 // Switch to letter set
245 figureSet = false
246 } else {
247 // Map tape character
248 codePoint = characterSet[char + (figureSet ? 32 : 0)]
249 if (codePoint !== null) {
250 result[j++] = codePoint
251 }
252 }
253 }
254
255 return result.slice(0, j)
256 }
257
258 /**
259 * Composes the character set for the given variant name.

Callers

nothing calls this directly

Calls 5

getSettingValueMethod · 0.80
indexOfMethod · 0.80
resizeBitSizedArrayMethod · 0.80
getBytesMethod · 0.80

Tested by

no test coverage detected