From 0407685ecf571f7e6cf27fc803320a3eb1774652 Mon Sep 17 00:00:00 2001 From: ed Date: Wed, 22 Dec 2021 09:37:09 +0100 Subject: [PATCH] decoding --- src/core/lib/Gsm7.mjs | 107 ++++++++++++++++--------------- src/core/operations/FromGSM7.mjs | 1 - tests/operations/tests/Gsm7.mjs | 16 +++-- 3 files changed, 67 insertions(+), 57 deletions(-) diff --git a/src/core/lib/Gsm7.mjs b/src/core/lib/Gsm7.mjs index c6343fa9..bdcd493e 100644 --- a/src/core/lib/Gsm7.mjs +++ b/src/core/lib/Gsm7.mjs @@ -641,7 +641,7 @@ export function toGsm7(text, charset, extension, CRpad) { } codePoints.push(c); } - + // optional step #2: final CR to cope with unexpected encoding of 0x00 or to affirm a wanted final CR if (CRpad) { if ((codePoints.length % 8 == 7) || ((codePoints.length % 8 == 0) && codePoints[-1] == 0x0d)) { @@ -673,60 +673,63 @@ export function toGsm7(text, charset, extension, CRpad) { /** * GSM-7 decode the input SMS as text * - * @param {string} data - * @param {string} [alphabet="A-Za-z0-9+/="] - * @param {string} [returnType="string"] - Either "string" or "byteArray" - * @param {boolean} [removeNonAlphChars=true] - * @returns {byteArray} + * @param {ArrayBuffer} sms + * @param {array} [charset] + * @param {array} [extension] + * @param {boolean} [CRpad=true] + * @returns {string} */ -export function fromGsm7(sms, alphabet=charsets['Default'], extension=extensions['Default'], CRpad=true) { - console.log("\n----------") - console.log(text); - //console.log(charset.join("")); - //console.log(extension.join("")); - console.log(CRpad); - console.log("------------\n") +export function fromGsm7(sms, charset, extension, CRpad) { + if (sms.length == 0) return ""; + if (charset == 'Default') { + charset = CHARSET_OPTIONS[0].value; + } + if (extension == 'Default') { + extension = EXTENSION_OPTIONS[0].value; + } - return ""; -/* - # step #1: 7bit unpacking - codepoints = [] - previousbits = 0 - for i,b in enumerate(sms): - mod = i % 7 - c = previousbits + ((b << mod) & 0x7f) - previousbits = (b >> (7-mod)) - codepoints.append(c) - if mod == 6: - c = previousbits - previousbits = 0 - codepoints.append(c) - if not codepoints: - return '' + // step #1: 7bit unpacking + let codePoints = []; + let previousBits = 0; + for (let i = 0; i < sms.length; i++) { + let b = sms[i]; + let mod = i % 7; + let c = previousBits + ((b << mod) & 0x7f); + previousBits = b >> (7-mod); + codePoints.push(c); + if (mod==6) { + c = previousBits; + previousBits = 0; + codePoints.push(c); + } + } - # optional step #2: remove final CR when on octet boundary - if CRpad: - if ((len(codepoints) % 8 == 0) and codepoints[-1] == 0x0d): - codepoints.pop() + // optional step #2: remove final CR when on octet boundary + if (CRpad) { + if ((codePoints.length % 8 == 0) && codePoints[-1] == 0x0d) { + codePoints.pop(); + } + } - # step #3: decoding with given charset and extension - text = [] - esc = False - for c in codepoints: - if esc: - char = extension[c] - if char == '\x00': - char = charset[c] - elif char == '\x1b': - char = ' ' - text.append(char) - esc = False - elif c == 0x1b: - esc = True - else: - text.append(charset[c]) - return ''.join(text) -*/ - + // step #3: decoding with given charset and extension + let text = []; + let esc = false; + for (let c of codePoints) { + if (esc) { + let char = extension[c]; + if (char=='\x00') { + char = charset[c]; + } else if (char=='\x1b') { + char = ' '; + } + text.push(char); + esc = false; + } else if (c==0x1b) { + esc = true; + } else { + text.push(charset[c]); + } + } + return text.join(""); } diff --git a/src/core/operations/FromGSM7.mjs b/src/core/operations/FromGSM7.mjs index 7981cdb1..794e2389 100644 --- a/src/core/operations/FromGSM7.mjs +++ b/src/core/operations/FromGSM7.mjs @@ -53,7 +53,6 @@ class FromGSM7 extends Operation { return fromGsm7(input, charset, extension, CRpad); } - } export default FromGSM7; diff --git a/tests/operations/tests/Gsm7.mjs b/tests/operations/tests/Gsm7.mjs index ec919bd6..ad2e6623 100644 --- a/tests/operations/tests/Gsm7.mjs +++ b/tests/operations/tests/Gsm7.mjs @@ -17,10 +17,14 @@ TestRegister.addTests([ op: "To GSM-7", args: ["Default", "Default", false], }, + { + op: "To Hex", + args: ["Space", 0], + } ], }, { - name: "To GSM-7: a real SMS", + name: "To GSM-7: a real SMS sent over IP", input: "a long sms with escapes [{ ~}) should be enough to validate encoding & packing", expectedOutput: "61 10 fb ed 3e 83 e6 ed 39 e8 9e a6 a3 41 e5 f9 38 0c 2f cf 41 1b de 06 05 da f4 36 a9 14 68 8e 7e d7 d9 64 90 b8 0c 2a bb df f5 33 1a 44 7f 83 ec 61 76 9a 1c a6 97 41 65 f7 f8 4d 4e bb cf 20 13 08 1e 1e af d3 ee 33", recipeConfig: [ @@ -52,7 +56,7 @@ TestRegister.addTests([ { name: "To GSM-7: padding a 7 chars SMS", input: "7Chars.", - expectedOutput: "b7 21 3a 2c 9f bb 00", + expectedOutput: "b7 21 3a 2c 9f bb 1a", recipeConfig: [ { op: "To GSM-7", @@ -69,9 +73,13 @@ TestRegister.addTests([ input: "", expectedOutput: "", recipeConfig: [ + { + op: "From Hex", + args: ["Auto"], + }, { op: "From GSM-7", - args: [], + args: ["Default", "Default", true], }, ], }, @@ -86,7 +94,7 @@ TestRegister.addTests([ }, { op: "From GSM-7", - args: [], + args: ["Default", "Default", true], } ], }