From 3a1a6a94d26a96ee5d02c23845a7511813992ee5 Mon Sep 17 00:00:00 2001 From: n1073645 Date: Mon, 16 Dec 2019 17:05:06 +0000 Subject: [PATCH 1/5] Sets the gzip comment bitfield --- src/core/operations/Gunzip.mjs | 10 ++++++---- src/core/operations/Gzip.mjs | 16 ++++++++++------ 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/core/operations/Gunzip.mjs b/src/core/operations/Gunzip.mjs index 07b1d6c2..ace795be 100644 --- a/src/core/operations/Gunzip.mjs +++ b/src/core/operations/Gunzip.mjs @@ -5,9 +5,11 @@ */ import Operation from "../Operation.mjs"; -import zlibAndGzip from "zlibjs/bin/zlib_and_gzip.min.js"; +// import zlibAndGzip from "zlibjs/bin/zlib_and_gzip.min.js"; +import gunzip from "zlibjs/bin/gunzip.min.js"; -const Zlib = zlibAndGzip.Zlib; +// const Zlib = zlibAndGzip.Zlib; +const Zlib = gunzip.Zlib; /** * Gunzip operation @@ -42,8 +44,8 @@ class Gunzip extends Operation { * @returns {File} */ run(input, args) { - const gunzip = new Zlib.Gunzip(new Uint8Array(input)); - return new Uint8Array(gunzip.decompress()).buffer; + const gzipObj = new Zlib.Gunzip(new Uint8Array(input)); + return new Uint8Array(gzipObj.decompress()).buffer; } } diff --git a/src/core/operations/Gzip.mjs b/src/core/operations/Gzip.mjs index 5f9fa474..b0ed9b53 100644 --- a/src/core/operations/Gzip.mjs +++ b/src/core/operations/Gzip.mjs @@ -6,9 +6,10 @@ import Operation from "../Operation.mjs"; import {COMPRESSION_TYPE, ZLIB_COMPRESSION_TYPE_LOOKUP} from "../lib/Zlib.mjs"; -import zlibAndGzip from "zlibjs/bin/zlib_and_gzip.min.js"; +// import zlibAndGzip from "zlibjs/bin/zlib_and_gzip.min.js"; +import gzip from "zlibjs/bin/gzip.min.js"; -const Zlib = zlibAndGzip.Zlib; +const Zlib = gzip.Zlib; /** * Gzip operation @@ -73,12 +74,15 @@ class Gzip extends Operation { options.filename = filename; } if (comment.length) { - options.flags.fcommenct = true; + options.flags.comment = true; options.comment = comment; } - - const gzip = new Zlib.Gzip(new Uint8Array(input), options); - return new Uint8Array(gzip.compress()).buffer; + const gzipObj = new Zlib.Gzip(new Uint8Array(input), options); + const compressed = new Uint8Array(gzipObj.compress()); + if (options.flags.comment) { + compressed[3] |= 0x10; + } + return compressed.buffer; } } From 5fd2512a9b353dd2dce1d50408c7d4ad997384d4 Mon Sep 17 00:00:00 2001 From: n1073645 Date: Tue, 17 Dec 2019 12:15:11 +0000 Subject: [PATCH 2/5] Gzip tests added --- src/core/operations/Gzip.mjs | 3 +- tests/operations/index.mjs | 1 + tests/operations/tests/Gzip.mjs | 89 +++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 tests/operations/tests/Gzip.mjs diff --git a/src/core/operations/Gzip.mjs b/src/core/operations/Gzip.mjs index b0ed9b53..093ae6a4 100644 --- a/src/core/operations/Gzip.mjs +++ b/src/core/operations/Gzip.mjs @@ -6,7 +6,6 @@ import Operation from "../Operation.mjs"; import {COMPRESSION_TYPE, ZLIB_COMPRESSION_TYPE_LOOKUP} from "../lib/Zlib.mjs"; -// import zlibAndGzip from "zlibjs/bin/zlib_and_gzip.min.js"; import gzip from "zlibjs/bin/gzip.min.js"; const Zlib = gzip.Zlib; @@ -79,7 +78,7 @@ class Gzip extends Operation { } const gzipObj = new Zlib.Gzip(new Uint8Array(input), options); const compressed = new Uint8Array(gzipObj.compress()); - if (options.flags.comment) { + if (options.flags.comment && !(compressed[3] & 0x10)) { compressed[3] |= 0x10; } return compressed.buffer; diff --git a/tests/operations/index.mjs b/tests/operations/index.mjs index c54fa7ef..09ebf0fc 100644 --- a/tests/operations/index.mjs +++ b/tests/operations/index.mjs @@ -41,6 +41,7 @@ import "./tests/DateTime.mjs"; import "./tests/ExtractEmailAddresses.mjs"; import "./tests/Fork.mjs"; import "./tests/FromDecimal.mjs"; +import "./tests/Gzip.mjs"; import "./tests/Hash.mjs"; import "./tests/HaversineDistance.mjs"; import "./tests/Hexdump.mjs"; diff --git a/tests/operations/tests/Gzip.mjs b/tests/operations/tests/Gzip.mjs new file mode 100644 index 00000000..2b055ae8 --- /dev/null +++ b/tests/operations/tests/Gzip.mjs @@ -0,0 +1,89 @@ +/** + * Gzip tests. + * + * @author n1073645 [n1073645@gmail.com] + * + * @copyright Crown Copyright 2019 + * @license Apache-2.0 + */ + +import TestRegister from "../../lib/TestRegister.mjs"; + +TestRegister.addTests([ + { + name: "Gzip: No comment, no checksum and no filename", + input: "The quick brown fox jumped over the slow dog", + expectedOutput: "0dc9dd0180200804e0556ea8262848fb3dc588c6a7e76faa8aeedb726036c68d951f76bf9a0af8aae1f97d9c0c084b02509cbf8c2c000000", + recipeConfig: [ + { + op: "Gzip", + args: ["Dynamic Huffman Coding", "", "", false] + }, + { + op: "Drop bytes", + args: [0, 10, false] + }, + { + op: "To Hex", + args: ["None"] + } + ] + }, + { + name: "Gzip: No comment, no checksum and has a filename", + input: "The quick brown fox jumped over the slow dog", + expectedOutput: "636f6d6d656e74000dc9dd0180200804e0556ea8262848fb3dc588c6a7e76faa8aeedb726036c68d951f76bf9a0af8aae1f97d9c0c084b02509cbf8c2c000000", + recipeConfig: [ + { + op: "Gzip", + args: ["Dynamic Huffman Coding", "comment", "", false] + }, + { + op: "Drop bytes", + args: [0, 10, false] + }, + { + op: "To Hex", + args: ["None"] + } + ] + }, + { + name: "Gzip: Has a comment, no checksum and no filename", + input: "The quick brown fox jumped over the slow dog", + expectedOutput: "636f6d6d656e74000dc9dd0180200804e0556ea8262848fb3dc588c6a7e76faa8aeedb726036c68d951f76bf9a0af8aae1f97d9c0c084b02509cbf8c2c000000", + recipeConfig: [ + { + op: "Gzip", + args: ["Dynamic Huffman Coding", "", "comment", false] + }, + { + op: "Drop bytes", + args: [0, 10, false] + }, + { + op: "To Hex", + args: ["None"] + } + ] + }, + { + name: "Gzip: Has a comment, no checksum and has a filename", + input: "The quick brown fox jumped over the slow dog", + expectedOutput: "66696c656e616d6500636f6d6d656e74000dc9dd0180200804e0556ea8262848fb3dc588c6a7e76faa8aeedb726036c68d951f76bf9a0af8aae1f97d9c0c084b02509cbf8c2c000000", + recipeConfig: [ + { + op: "Gzip", + args: ["Dynamic Huffman Coding", "filename", "comment", false] + }, + { + op: "Drop bytes", + args: [0, 10, false] + }, + { + op: "To Hex", + args: ["None"] + } + ] + }, +]); From 72ba579e1e2200d8ec7930d3525ab74aad71cff1 Mon Sep 17 00:00:00 2001 From: n1073645 Date: Tue, 17 Dec 2019 12:17:13 +0000 Subject: [PATCH 3/5] Remove unnecessary comments. --- src/core/operations/Gunzip.mjs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/core/operations/Gunzip.mjs b/src/core/operations/Gunzip.mjs index ace795be..ef487b06 100644 --- a/src/core/operations/Gunzip.mjs +++ b/src/core/operations/Gunzip.mjs @@ -5,10 +5,8 @@ */ import Operation from "../Operation.mjs"; -// import zlibAndGzip from "zlibjs/bin/zlib_and_gzip.min.js"; import gunzip from "zlibjs/bin/gunzip.min.js"; -// const Zlib = zlibAndGzip.Zlib; const Zlib = gunzip.Zlib; /** From 71078d9332d68633de4c3d736be365f6956c9ef4 Mon Sep 17 00:00:00 2001 From: n1073645 Date: Tue, 17 Dec 2019 12:28:09 +0000 Subject: [PATCH 4/5] Added tests for gunzip. --- tests/operations/index.mjs | 1 + tests/operations/tests/Gunzip.mjs | 58 +++++++++++++++++++++++++++++++ tests/operations/tests/Gzip.mjs | 2 +- 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 tests/operations/tests/Gunzip.mjs diff --git a/tests/operations/index.mjs b/tests/operations/index.mjs index 09ebf0fc..51676386 100644 --- a/tests/operations/index.mjs +++ b/tests/operations/index.mjs @@ -42,6 +42,7 @@ import "./tests/ExtractEmailAddresses.mjs"; import "./tests/Fork.mjs"; import "./tests/FromDecimal.mjs"; import "./tests/Gzip.mjs"; +import "./tests/Gunzip.mjs"; import "./tests/Hash.mjs"; import "./tests/HaversineDistance.mjs"; import "./tests/Hexdump.mjs"; diff --git a/tests/operations/tests/Gunzip.mjs b/tests/operations/tests/Gunzip.mjs new file mode 100644 index 00000000..f6e05c2f --- /dev/null +++ b/tests/operations/tests/Gunzip.mjs @@ -0,0 +1,58 @@ +/** + * Gunzip Tests. + * + * @author n1073645 [n1073645@gmail.com] + * + * @copyright Crown Copyright 2019 + * @license Apache-2.0 + */ + +import TestRegister from "../../lib/TestRegister.mjs"; + +TestRegister.addTests([ + { + name: "Gunzip: No comment, no checksum and no filename", + input: "1f8b0800f7c8f85d00ff0dc9dd0180200804e0556ea8262848fb3dc588c6a7e76faa8aeedb726036c68d951f76bf9a0af8aae1f97d9c0c084b02509cbf8c2c000000", + expectedOutput: "The quick brown fox jumped over the slow dog", + recipeConfig: [ + { + op: "From Hex", + args: ["None"] + }, + { + op: "Gunzip", + args: [] + } + ] + }, + { + name: "Gunzip: No comment, no checksum and filename", + input: "1f8b080843c9f85d00ff66696c656e616d65000dc9dd0180200804e0556ea8262848fb3dc588c6a7e76faa8aeedb726036c68d951f76bf9a0af8aae1f97d9c0c084b02509cbf8c2c000000", + expectedOutput: "The quick brown fox jumped over the slow dog", + recipeConfig: [ + { + op: "From Hex", + args: ["None"] + }, + { + op: "Gunzip", + args: [] + } + ] + }, + { + name: "Gunzip: Has a comment, no checksum and has a filename", + input: "1f8b08186fc9f85d00ff66696c656e616d6500636f6d6d656e74000dc9dd0180200804e0556ea8262848fb3dc588c6a7e76faa8aeedb726036c68d951f76bf9a0af8aae1f97d9c0c084b02509cbf8c2c000000", + expectedOutput: "The quick brown fox jumped over the slow dog", + recipeConfig: [ + { + op: "From Hex", + args: ["None"] + }, + { + op: "Gunzip", + args: [] + } + ] + } +]); \ No newline at end of file diff --git a/tests/operations/tests/Gzip.mjs b/tests/operations/tests/Gzip.mjs index 2b055ae8..c9b2b8ca 100644 --- a/tests/operations/tests/Gzip.mjs +++ b/tests/operations/tests/Gzip.mjs @@ -1,5 +1,5 @@ /** - * Gzip tests. + * Gzip Tests. * * @author n1073645 [n1073645@gmail.com] * From 4100a22c7f7edbcea7cfb8f9ac346f4ff0246585 Mon Sep 17 00:00:00 2001 From: n1073645 Date: Tue, 17 Dec 2019 12:30:32 +0000 Subject: [PATCH 5/5] Linting on tests --- tests/operations/tests/Gunzip.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/operations/tests/Gunzip.mjs b/tests/operations/tests/Gunzip.mjs index f6e05c2f..70b67a31 100644 --- a/tests/operations/tests/Gunzip.mjs +++ b/tests/operations/tests/Gunzip.mjs @@ -55,4 +55,4 @@ TestRegister.addTests([ } ] } -]); \ No newline at end of file +]);