Skip to content

Commit

Permalink
improve binary escaping
Browse files Browse the repository at this point in the history
  • Loading branch information
lonnywong committed Oct 6, 2023
1 parent 70ee441 commit b746472
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 19 deletions.
7 changes: 3 additions & 4 deletions src/comm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ export const isRunningInBrowser = (function() {
require("fs");
return false;
}
} catch (err) {
return true;
}
} catch (err) { }
return true;
})();

export function strToUint8(str: string): Uint8Array {
Expand Down Expand Up @@ -139,7 +138,7 @@ export type OpenSaveFile = (
saveParam: any,
fileName: string,
directory: boolean,
overwrite: boolean
overwrite: boolean,
) => Promise<TrzszFileWriter>;

export interface ProgressCallback {
Expand Down
2 changes: 1 addition & 1 deletion src/escape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function getEscapeChars(escapeAll: boolean): Array<string[]> {
["\x7e", "\xee\x31"],
];
if (escapeAll) {
const chars = "\x02\x10\x1b\x1d\x9d";
const chars = "\x02\x0d\x10\x11\x13\x18\x1b\x1d\x8d\x90\x91\x93\x9d";
for (let i = 0; i < chars.length; i++) {
escapeChars.push([chars[i], "\xee" + String.fromCharCode(0x41 + i)]);
}
Expand Down
20 changes: 14 additions & 6 deletions test/escape.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,23 @@ test("escape chars to codes", () => {
test("escape data", () => {
const escapeChars = getEscapeChars(true);
const escapeCodes = escapeCharsToCodes(escapeChars);
const data = strToUint8("\xee\x7e\x02\x10A");
expect(escapeData(data, [])).toStrictEqual(strToUint8("\xee\x7e\x02\x10A"));
expect(escapeData(data, escapeCodes)).toStrictEqual(strToUint8("\xee\xee\xee\x31\xee\x41\xee\x42A"));
const data = strToUint8("\xee\x7e\x02\x0d\x10\x11\x13\x18\x1b\x1d\x8d\x90\x91\x93\x9dA");
expect(escapeData(data, [])).toStrictEqual(
strToUint8("\xee\x7e\x02\x0d\x10\x11\x13\x18\x1b\x1d\x8d\x90\x91\x93\x9dA"),
);
expect(escapeData(data, escapeCodes)).toStrictEqual(
strToUint8("\xee\xee\xee1\xeeA\xeeB\xeeC\xeeD\xeeE\xeeF\xeeG\xeeH\xeeI\xeeJ\xeeK\xeeL\xeeMA"),
);
});

test("unescape data", () => {
const escapeChars = getEscapeChars(true);
const escapeCodes = escapeCharsToCodes(escapeChars);
const data = strToUint8("\xee\xee\xee\x31\xee\x41\xee\x42A");
expect(unescapeData(data, [])).toStrictEqual(strToUint8("\xee\xee\xee\x31\xee\x41\xee\x42A"));
expect(unescapeData(data, escapeCodes)).toStrictEqual(strToUint8("\xee\x7e\x02\x10A"));
const data = strToUint8("\xee\xee\xee1\xeeA\xeeB\xeeC\xeeD\xeeE\xeeF\xeeG\xeeH\xeeI\xeeJ\xeeK\xeeL\xeeMA");
expect(unescapeData(data, [])).toStrictEqual(
strToUint8("\xee\xee\xee1\xeeA\xeeB\xeeC\xeeD\xeeE\xeeF\xeeG\xeeH\xeeI\xeeJ\xeeK\xeeL\xeeMA"),
);
expect(unescapeData(data, escapeCodes)).toStrictEqual(
strToUint8("\xee\x7e\x02\x0d\x10\x11\x13\x18\x1b\x1d\x8d\x90\x91\x93\x9dA"),
);
});
26 changes: 18 additions & 8 deletions test/transfer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,9 @@ test("upload files using binary mode", async () => {
file.readFile.mockReturnValueOnce(strToUint8("\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A"));

trzsz.addReceivedData(
"junk\r\n#CFG:eJxNzzsOwjAMgOGrRJ47OBULbLxOESKUFgORoFR5DAXB2XEokrP5/+TBfsHNDRdYKRgnaBR0fnBh4k4hEzfF3o10" +
"7K8uRFZj4JARicruPP3bNsrAR1jPUgJb4bWwRuFNxZ3wtuKT8E54WfEerC0v5HP0T+JrNbYLhuTv9MjpB/j+AivBQi0=\n"
"junk\r\n#CFG:eJxF0LsOwjAMBdB/8ZwhKQwlG+/3F4QIpcWFIigoaQZA8O20FNmb75FlXfkFF1cdQcM5gICsrJx/gK59RAEYcnfHfX5y" +
"PoA2BnZRSsRmrxv+0QoDH0L1y+0sE8Jhh55gRFtKEo4ZFeGEsUc4ZUwJZ4wZ4ZzxQLggTBmXhAOutGLkSmtGrrRh5JtbsLb5aixC+UTQS" +
"iZ9AXV5xVus2yjfX3gwanQ=\n",
);
const config = await trzsz.recvConfig();
expect(config.binary).toBe(true);
Expand Down Expand Up @@ -276,8 +277,9 @@ test("download files using binary mode", async () => {
const trzsz = new TrzszTransfer(writer);

trzsz.addReceivedData(
"junk\r\n#CFG:eJxNzzsOwjAMgOGrRJ47OBULbLxOESKUFgORoFR5DAXB2XEokrP5/+TBfsHNDRdYKRgnaBR0fnBh4k4hEzfF3o10" +
"7K8uRFZj4JARicruPP3bNsrAR1jPUgJb4bWwRuFNxZ3wtuKT8E54WfEerC0v5HP0T+JrNbYLhuTv9MjpB/j+AivBQi0=\n"
"junk\r\n#CFG:eJxF0LsOwjAMBdB/8ZwhKQwlG+/3F4QIpcWFIigoaQZA8O20FNmb75FlXfkFF1cdQcM5gICsrJx/gK59RAEYcnfHfX5y" +
"PoA2BnZRSsRmrxv+0QoDH0L1y+0sE8Jhh55gRFtKEo4ZFeGEsUc4ZUwJZ4wZ4ZzxQLggTBmXhAOutGLkSmtGrrRh5JtbsLb5aixC+UTQS" +
"iZ9AXV5xVus2yjfX3gwanQ=\n",
);
const config = await trzsz.recvConfig();
expect(config.binary).toBe(true);
Expand Down Expand Up @@ -568,10 +570,18 @@ test("send config to remote", async () => {
expect(cfgStr).toContain('["\\u00ee","\\u00ee\\u00ee"]');
expect(cfgStr).toContain('["~","\\u00ee1"]');
expect(cfgStr).toContain('["\\u0002","\\u00eeA"]');
expect(cfgStr).toContain('["\\u0010","\\u00eeB"]');
expect(cfgStr).toContain('["\\u001b","\\u00eeC"]');
expect(cfgStr).toContain('["\\u001d","\\u00eeD"]');
expect(cfgStr).toContain('["\\u009d","\\u00eeE"]');
expect(cfgStr).toContain('["\\r","\\u00eeB"]');
expect(cfgStr).toContain('["\\u0010","\\u00eeC"]');
expect(cfgStr).toContain('["\\u0011","\\u00eeD"]');
expect(cfgStr).toContain('["\\u0013","\\u00eeE"]');
expect(cfgStr).toContain('["\\u0018","\\u00eeF"]');
expect(cfgStr).toContain('["\\u001b","\\u00eeG"]');
expect(cfgStr).toContain('["\\u001d","\\u00eeH"]');
expect(cfgStr).toContain('["\\u008d","\\u00eeI"]');
expect(cfgStr).toContain('["\\u0090","\\u00eeJ"]');
expect(cfgStr).toContain('["\\u0091","\\u00eeK"]');
expect(cfgStr).toContain('["\\u0093","\\u00eeL"]');
expect(cfgStr).toContain('["\\u009d","\\u00eeM"]');
const config = JSON.parse(cfgStr);
expect(config.quiet).toBe(true);
expect(config.binary).toBe(true);
Expand Down

0 comments on commit b746472

Please sign in to comment.