|
| 1 | +/** |
| 2 | + * @author linuxgemini [[email protected]] |
| 3 | + * @copyright Crown Copyright 2024 |
| 4 | + * @license Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import Utils from "../Utils.mjs"; |
| 8 | +import OperationError from "../errors/OperationError.mjs"; |
| 9 | +import { fromHex, toHex } from "./Hex.mjs"; |
| 10 | + |
| 11 | +/** |
| 12 | + * Modhex alphabet. |
| 13 | + */ |
| 14 | +const MODHEX_ALPHABET = "cbdefghijklnrtuv"; |
| 15 | + |
| 16 | + |
| 17 | +/** |
| 18 | + * Modhex alphabet map. |
| 19 | + */ |
| 20 | +const MODHEX_ALPHABET_MAP = MODHEX_ALPHABET.split(""); |
| 21 | + |
| 22 | + |
| 23 | +/** |
| 24 | + * Hex alphabet to substitute Modhex. |
| 25 | + */ |
| 26 | +const HEX_ALPHABET = "0123456789abcdef"; |
| 27 | + |
| 28 | + |
| 29 | +/** |
| 30 | + * Hex alphabet map to substitute Modhex. |
| 31 | + */ |
| 32 | +const HEX_ALPHABET_MAP = HEX_ALPHABET.split(""); |
| 33 | + |
| 34 | + |
| 35 | +/** |
| 36 | + * Convert a byte array into a modhex string. |
| 37 | + * |
| 38 | + * @param {byteArray|Uint8Array|ArrayBuffer} data |
| 39 | + * @param {string} [delim=" "] |
| 40 | + * @param {number} [padding=2] |
| 41 | + * @returns {string} |
| 42 | + * |
| 43 | + * @example |
| 44 | + * // returns "cl bf bu" |
| 45 | + * toModhex([10,20,30]); |
| 46 | + * |
| 47 | + * // returns "cl:bf:bu" |
| 48 | + * toModhex([10,20,30], ":"); |
| 49 | + */ |
| 50 | +export function toModhex(data, delim=" ", padding=2, extraDelim="", lineSize=0) { |
| 51 | + if (!data) return ""; |
| 52 | + if (data instanceof ArrayBuffer) data = new Uint8Array(data); |
| 53 | + |
| 54 | + const regularHexString = toHex(data, "", padding, "", 0); |
| 55 | + |
| 56 | + let modhexString = ""; |
| 57 | + for (const letter of regularHexString.split("")) { |
| 58 | + modhexString += MODHEX_ALPHABET_MAP[HEX_ALPHABET_MAP.indexOf(letter)]; |
| 59 | + } |
| 60 | + |
| 61 | + let output = ""; |
| 62 | + const groupingRegexp = new RegExp(`.{1,${padding}}`, "g"); |
| 63 | + const groupedModhex = modhexString.match(groupingRegexp); |
| 64 | + |
| 65 | + for (let i = 0; i < groupedModhex.length; i++) { |
| 66 | + const group = groupedModhex[i]; |
| 67 | + output += group + delim; |
| 68 | + |
| 69 | + if (extraDelim) { |
| 70 | + output += extraDelim; |
| 71 | + } |
| 72 | + // Add LF after each lineSize amount of bytes but not at the end |
| 73 | + if ((i !== groupedModhex.length - 1) && ((i + 1) % lineSize === 0)) { |
| 74 | + output += "\n"; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + // Remove the extraDelim at the end (if there is one) |
| 79 | + // and remove the delim at the end, but if it's prepended there's nothing to remove |
| 80 | + const rTruncLen = extraDelim.length + delim.length; |
| 81 | + if (rTruncLen) { |
| 82 | + // If rTruncLen === 0 then output.slice(0,0) will be returned, which is nothing |
| 83 | + return output.slice(0, -rTruncLen); |
| 84 | + } else { |
| 85 | + return output; |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | + |
| 90 | +/** |
| 91 | + * Convert a byte array into a modhex string as efficiently as possible with no options. |
| 92 | + * |
| 93 | + * @param {byteArray|Uint8Array|ArrayBuffer} data |
| 94 | + * @returns {string} |
| 95 | + * |
| 96 | + * @example |
| 97 | + * // returns "clbfbu" |
| 98 | + * toModhexFast([10,20,30]); |
| 99 | + */ |
| 100 | +export function toModhexFast(data) { |
| 101 | + if (!data) return ""; |
| 102 | + if (data instanceof ArrayBuffer) data = new Uint8Array(data); |
| 103 | + |
| 104 | + const output = []; |
| 105 | + |
| 106 | + for (let i = 0; i < data.length; i++) { |
| 107 | + output.push(MODHEX_ALPHABET_MAP[(data[i] >> 4) & 0xf]); |
| 108 | + output.push(MODHEX_ALPHABET_MAP[data[i] & 0xf]); |
| 109 | + } |
| 110 | + return output.join(""); |
| 111 | +} |
| 112 | + |
| 113 | + |
| 114 | +/** |
| 115 | + * Convert a modhex string into a byte array. |
| 116 | + * |
| 117 | + * @param {string} data |
| 118 | + * @param {string} [delim] |
| 119 | + * @param {number} [byteLen=2] |
| 120 | + * @returns {byteArray} |
| 121 | + * |
| 122 | + * @example |
| 123 | + * // returns [10,20,30] |
| 124 | + * fromModhex("cl bf bu"); |
| 125 | + * |
| 126 | + * // returns [10,20,30] |
| 127 | + * fromModhex("cl:bf:bu", "Colon"); |
| 128 | + */ |
| 129 | +export function fromModhex(data, delim="Auto", byteLen=2) { |
| 130 | + if (byteLen < 1 || Math.round(byteLen) !== byteLen) |
| 131 | + throw new OperationError("Byte length must be a positive integer"); |
| 132 | + |
| 133 | + // The `.replace(/\s/g, "")` an interesting workaround: Hex "multiline" tests aren't actually |
| 134 | + // multiline. Tests for Modhex fixes that, thus exposing the issue. |
| 135 | + data = data.toLowerCase().replace(/\s/g, ""); |
| 136 | + |
| 137 | + if (delim !== "None") { |
| 138 | + const delimRegex = delim === "Auto" ? /[^cbdefghijklnrtuv]/gi : Utils.regexRep(delim); |
| 139 | + data = data.split(delimRegex); |
| 140 | + } else { |
| 141 | + data = [data]; |
| 142 | + } |
| 143 | + |
| 144 | + let regularHexString = ""; |
| 145 | + for (let i = 0; i < data.length; i++) { |
| 146 | + for (const letter of data[i].split("")) { |
| 147 | + regularHexString += HEX_ALPHABET_MAP[MODHEX_ALPHABET_MAP.indexOf(letter)]; |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + const output = fromHex(regularHexString, "None", byteLen); |
| 152 | + return output; |
| 153 | +} |
| 154 | + |
| 155 | + |
| 156 | +/** |
| 157 | + * To Modhex delimiters. |
| 158 | + */ |
| 159 | +export const TO_MODHEX_DELIM_OPTIONS = ["Space", "Percent", "Comma", "Semi-colon", "Colon", "Line feed", "CRLF", "None"]; |
| 160 | + |
| 161 | + |
| 162 | +/** |
| 163 | + * From Modhex delimiters. |
| 164 | + */ |
| 165 | +export const FROM_MODHEX_DELIM_OPTIONS = ["Auto"].concat(TO_MODHEX_DELIM_OPTIONS); |
0 commit comments