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

Method performDecode

src/Encoder/PolybiusSquare.js:168–238  ·  view source on GitHub ↗

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

(content)

Source from the content-addressed store, hash-verified

166 * @return {number[]|string|Uint8Array|Chain} Decoded content
167 */
168 performDecode (content) {
169 const alphabet = this.getSettingValue('alphabet').getCodePoints()
170 const separator = this.getSettingValue('separator').getCodePoints()
171 const includeForeignChars = this.getSettingValue('includeForeignChars')
172
173 const rows = this.getSettingValue('rows').getCodePoints()
174 const columns = this.getSettingValue('columns').getCodePoints()
175
176 // Ignore all separators inside the given content
177 const input = ArrayUtil.replaceSlice(content.getCodePoints(), separator)
178
179 const alphabetLength = alphabet.length
180 const width = columns.length
181 const inputLength = input.length
182 const result = new Array(inputLength)
183
184 let i = 0
185 let j = 0
186 let row, column, index
187
188 while (i < inputLength) {
189 // Find the next valid row character
190 row = null
191 while (row === null && i < inputLength) {
192 index = rows.indexOf(input[i])
193 if (index !== -1) {
194 // Found row character
195 row = index
196 } else if (includeForeignChars) {
197 // Include foreign character
198 result[j++] = input[i]
199 }
200 i++
201 }
202
203 // Find the next valid column character
204 column = null
205 while (column === null && i < inputLength) {
206 index = rows.indexOf(input[i])
207 if (index !== -1) {
208 // Found column character
209 column = index
210 } else if (includeForeignChars) {
211 // Include foreign character
212 result[j++] = input[i]
213 }
214 i++
215 }
216
217 if (row !== null && column !== null) {
218 // Decode square coordinates
219 index = row * width + column
220
221 // Make sure the given square cell is defined by the alphabet
222 if (index >= alphabetLength) {
223 throw new InvalidInputError(
224 `Polybius square cell at coordinates ${row},${column} are not ` +
225 'defined by the alphabet.')

Callers

nothing calls this directly

Calls 4

getCodePointsMethod · 0.80
getSettingValueMethod · 0.80
replaceSliceMethod · 0.80
indexOfMethod · 0.80

Tested by

no test coverage detected