mirror of
https://github.com/gchq/CyberChef.git
synced 2025-05-09 07:45:00 -04:00
decoding
This commit is contained in:
parent
d92ff10585
commit
0407685ecf
3 changed files with 67 additions and 57 deletions
|
@ -641,7 +641,7 @@ export function toGsm7(text, charset, extension, CRpad) {
|
||||||
}
|
}
|
||||||
codePoints.push(c);
|
codePoints.push(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
// optional step #2: final CR to cope with unexpected encoding of 0x00 or to affirm a wanted final CR
|
// optional step #2: final CR to cope with unexpected encoding of 0x00 or to affirm a wanted final CR
|
||||||
if (CRpad) {
|
if (CRpad) {
|
||||||
if ((codePoints.length % 8 == 7) || ((codePoints.length % 8 == 0) && codePoints[-1] == 0x0d)) {
|
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
|
* GSM-7 decode the input SMS as text
|
||||||
*
|
*
|
||||||
* @param {string} data
|
* @param {ArrayBuffer} sms
|
||||||
* @param {string} [alphabet="A-Za-z0-9+/="]
|
* @param {array} [charset]
|
||||||
* @param {string} [returnType="string"] - Either "string" or "byteArray"
|
* @param {array} [extension]
|
||||||
* @param {boolean} [removeNonAlphChars=true]
|
* @param {boolean} [CRpad=true]
|
||||||
* @returns {byteArray}
|
* @returns {string}
|
||||||
*/
|
*/
|
||||||
export function fromGsm7(sms, alphabet=charsets['Default'], extension=extensions['Default'], CRpad=true) {
|
export function fromGsm7(sms, charset, extension, CRpad) {
|
||||||
console.log("\n----------")
|
if (sms.length == 0) return "";
|
||||||
console.log(text);
|
if (charset == 'Default') {
|
||||||
//console.log(charset.join(""));
|
charset = CHARSET_OPTIONS[0].value;
|
||||||
//console.log(extension.join(""));
|
}
|
||||||
console.log(CRpad);
|
if (extension == 'Default') {
|
||||||
console.log("------------\n")
|
extension = EXTENSION_OPTIONS[0].value;
|
||||||
|
}
|
||||||
|
|
||||||
return "";
|
// step #1: 7bit unpacking
|
||||||
/*
|
let codePoints = [];
|
||||||
# step #1: 7bit unpacking
|
let previousBits = 0;
|
||||||
codepoints = []
|
for (let i = 0; i < sms.length; i++) {
|
||||||
previousbits = 0
|
let b = sms[i];
|
||||||
for i,b in enumerate(sms):
|
let mod = i % 7;
|
||||||
mod = i % 7
|
let c = previousBits + ((b << mod) & 0x7f);
|
||||||
c = previousbits + ((b << mod) & 0x7f)
|
previousBits = b >> (7-mod);
|
||||||
previousbits = (b >> (7-mod))
|
codePoints.push(c);
|
||||||
codepoints.append(c)
|
if (mod==6) {
|
||||||
if mod == 6:
|
c = previousBits;
|
||||||
c = previousbits
|
previousBits = 0;
|
||||||
previousbits = 0
|
codePoints.push(c);
|
||||||
codepoints.append(c)
|
}
|
||||||
if not codepoints:
|
}
|
||||||
return ''
|
|
||||||
|
|
||||||
# optional step #2: remove final CR when on octet boundary
|
// optional step #2: remove final CR when on octet boundary
|
||||||
if CRpad:
|
if (CRpad) {
|
||||||
if ((len(codepoints) % 8 == 0) and codepoints[-1] == 0x0d):
|
if ((codePoints.length % 8 == 0) && codePoints[-1] == 0x0d) {
|
||||||
codepoints.pop()
|
codePoints.pop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
# step #3: decoding with given charset and extension
|
// step #3: decoding with given charset and extension
|
||||||
text = []
|
let text = [];
|
||||||
esc = False
|
let esc = false;
|
||||||
for c in codepoints:
|
for (let c of codePoints) {
|
||||||
if esc:
|
if (esc) {
|
||||||
char = extension[c]
|
let char = extension[c];
|
||||||
if char == '\x00':
|
if (char=='\x00') {
|
||||||
char = charset[c]
|
char = charset[c];
|
||||||
elif char == '\x1b':
|
} else if (char=='\x1b') {
|
||||||
char = ' '
|
char = ' ';
|
||||||
text.append(char)
|
}
|
||||||
esc = False
|
text.push(char);
|
||||||
elif c == 0x1b:
|
esc = false;
|
||||||
esc = True
|
} else if (c==0x1b) {
|
||||||
else:
|
esc = true;
|
||||||
text.append(charset[c])
|
} else {
|
||||||
return ''.join(text)
|
text.push(charset[c]);
|
||||||
*/
|
}
|
||||||
|
}
|
||||||
|
return text.join("");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -53,7 +53,6 @@ class FromGSM7 extends Operation {
|
||||||
|
|
||||||
return fromGsm7(input, charset, extension, CRpad);
|
return fromGsm7(input, charset, extension, CRpad);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default FromGSM7;
|
export default FromGSM7;
|
||||||
|
|
|
@ -17,10 +17,14 @@ TestRegister.addTests([
|
||||||
op: "To GSM-7",
|
op: "To GSM-7",
|
||||||
args: ["Default", "Default", false],
|
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",
|
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",
|
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: [
|
recipeConfig: [
|
||||||
|
@ -52,7 +56,7 @@ TestRegister.addTests([
|
||||||
{
|
{
|
||||||
name: "To GSM-7: padding a 7 chars SMS",
|
name: "To GSM-7: padding a 7 chars SMS",
|
||||||
input: "7Chars.",
|
input: "7Chars.",
|
||||||
expectedOutput: "b7 21 3a 2c 9f bb 00",
|
expectedOutput: "b7 21 3a 2c 9f bb 1a",
|
||||||
recipeConfig: [
|
recipeConfig: [
|
||||||
{
|
{
|
||||||
op: "To GSM-7",
|
op: "To GSM-7",
|
||||||
|
@ -69,9 +73,13 @@ TestRegister.addTests([
|
||||||
input: "",
|
input: "",
|
||||||
expectedOutput: "",
|
expectedOutput: "",
|
||||||
recipeConfig: [
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
op: "From Hex",
|
||||||
|
args: ["Auto"],
|
||||||
|
},
|
||||||
{
|
{
|
||||||
op: "From GSM-7",
|
op: "From GSM-7",
|
||||||
args: [],
|
args: ["Default", "Default", true],
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
@ -86,7 +94,7 @@ TestRegister.addTests([
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
op: "From GSM-7",
|
op: "From GSM-7",
|
||||||
args: [],
|
args: ["Default", "Default", true],
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue