summaryrefslogtreecommitdiff
path: root/src/adb_comp.c
diff options
context:
space:
mode:
authorTimo Teräs <timo.teras@iki.fi>2021-07-16 16:31:59 +0300
committerTimo Teräs <timo.teras@iki.fi>2021-07-22 15:30:08 +0300
commit7af34db6cfed5792b8acd4a4fd4da56de8381673 (patch)
treecd4823b746a03f81e976a929065d5655e0f8db80 /src/adb_comp.c
parent7b58f909fac0dd309355b244af60dc7374d7ef76 (diff)
downloadapk-tools-7af34db6cfed5792b8acd4a4fd4da56de8381673.tar.gz
apk-tools-7af34db6cfed5792b8acd4a4fd4da56de8381673.tar.bz2
apk-tools-7af34db6cfed5792b8acd4a4fd4da56de8381673.tar.xz
apk-tools-7af34db6cfed5792b8acd4a4fd4da56de8381673.zip
adb: support seamless de/compression of adb files
Add compression header of adb files. Support uncompressed and deflate compression at this time.
Diffstat (limited to 'src/adb_comp.c')
-rw-r--r--src/adb_comp.c48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/adb_comp.c b/src/adb_comp.c
new file mode 100644
index 0000000..8aed95d
--- /dev/null
+++ b/src/adb_comp.c
@@ -0,0 +1,48 @@
+/* adb_comp.c - Alpine Package Keeper (APK)
+ *
+ * Copyright (C) 2021 Timo Teräs <timo.teras@iki.fi>
+ * All rights reserved.
+ *
+ * SPDX-License-Identifier: GPL-2.0-only
+ */
+
+#include "apk_defines.h"
+#include "adb.h"
+
+struct apk_istream *adb_decompress(struct apk_istream *is, adb_comp_t *compression)
+{
+ adb_comp_t c = ADB_COMP_NONE;
+
+ if (IS_ERR_OR_NULL(is)) return is;
+
+ uint8_t *buf = apk_istream_peek(is, 4);
+ if (memcmp(buf, "ADB", 3) == 0) switch (buf[3]) {
+ case '.':
+ c = ADB_COMP_NONE;
+ break;
+ case 'd':
+ c = ADB_COMP_DEFLATE;
+ is = apk_istream_deflate(is);
+ break;
+ default:
+ apk_istream_close(is);
+ return ERR_PTR(-APKE_ADB_COMPRESSION);
+ }
+ if (compression) *compression = c;
+ return is;
+}
+
+struct apk_ostream *adb_compress(struct apk_ostream *os, adb_comp_t compression)
+{
+ if (IS_ERR_OR_NULL(os)) return os;
+ switch (compression) {
+ case ADB_COMP_NONE:
+ return os;
+ case ADB_COMP_DEFLATE:
+ if (apk_ostream_write(os, "ADBd", 4) < 0) goto err;
+ return apk_ostream_deflate(os);
+ }
+err:
+ apk_ostream_cancel(os, -APKE_ADB_COMPRESSION);
+ return ERR_PTR(apk_ostream_close(os));
+}