iOS 0.6.0: milestone 5 (recording, MIDI, input bindings, themes, bundles, backups)
- Master recording (MasterSink ring + writer thread) and the sample recorder - CoreMIDI in/out: sound module, clock send/follow, virtual "Sizzle" ports; platform-free parser/module/clock in core (midi_test) - Hardware keyboard and gamepads via GameController; rebindable keyboard, gamepad and MIDI CC actions; per-parameter MIDI learn - Colour themes (.szt), project bundles (.szgz), backup & restore; ZIP in core with zlib (zip_test against jar and unzip) - Persistent app settings; audio pauses in the background unless sound module Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
435305e42a
commit
64e21e0bcc
@@ -0,0 +1,148 @@
|
||||
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#include "io/Zip.hpp"
|
||||
|
||||
#include <zlib.h>
|
||||
|
||||
#include <cstring>
|
||||
|
||||
namespace sizzle::io::zip {
|
||||
|
||||
namespace {
|
||||
|
||||
void u16(std::vector<uint8_t> &o, uint32_t v) { o.push_back((uint8_t)v); o.push_back((uint8_t)(v >> 8)); }
|
||||
void u32(std::vector<uint8_t> &o, uint32_t v) { u16(o, v & 0xFFFF); u16(o, v >> 16); }
|
||||
uint32_t r16(const uint8_t *p) { return (uint32_t)p[0] | ((uint32_t)p[1] << 8); }
|
||||
uint32_t r32(const uint8_t *p) { return r16(p) | (r16(p + 2) << 16); }
|
||||
|
||||
bool deflateRaw(const std::vector<uint8_t> &in, std::vector<uint8_t> &out) {
|
||||
z_stream z{};
|
||||
if (deflateInit2(&z, Z_DEFAULT_COMPRESSION, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY) != Z_OK) return false;
|
||||
out.resize(deflateBound(&z, (uLong)in.size()));
|
||||
z.next_in = const_cast<Bytef *>(in.data());
|
||||
z.avail_in = (uInt)in.size();
|
||||
z.next_out = out.data();
|
||||
z.avail_out = (uInt)out.size();
|
||||
const int rc = deflate(&z, Z_FINISH);
|
||||
out.resize(z.total_out);
|
||||
deflateEnd(&z);
|
||||
return rc == Z_STREAM_END;
|
||||
}
|
||||
|
||||
bool inflateRaw(const uint8_t *in, size_t n, std::vector<uint8_t> &out, size_t expected) {
|
||||
z_stream z{};
|
||||
if (inflateInit2(&z, -15) != Z_OK) return false;
|
||||
out.resize(expected + 1); // one spare byte: an empty entry still has room, and overlong data shows
|
||||
z.next_in = const_cast<Bytef *>(in);
|
||||
z.avail_in = (uInt)n;
|
||||
z.next_out = out.data();
|
||||
z.avail_out = (uInt)out.size();
|
||||
const int rc = inflate(&z, Z_FINISH);
|
||||
const bool ok = rc == Z_STREAM_END && z.total_out == expected;
|
||||
out.resize(expected);
|
||||
inflateEnd(&z);
|
||||
return ok;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
std::vector<uint8_t> write(const std::vector<Entry> &entries) {
|
||||
std::vector<uint8_t> o, central;
|
||||
uint32_t count = 0;
|
||||
for (const Entry &e : entries) {
|
||||
const uint32_t crc = (uint32_t)crc32(0, e.data.data(), (uInt)e.data.size());
|
||||
std::vector<uint8_t> packed;
|
||||
uint16_t method = 8;
|
||||
if (!deflateRaw(e.data, packed) || packed.size() >= e.data.size()) { packed = e.data; method = 0; }
|
||||
const uint32_t offset = (uint32_t)o.size();
|
||||
auto header = [&](std::vector<uint8_t> &h, bool isCentral) {
|
||||
u32(h, isCentral ? 0x02014b50 : 0x04034b50);
|
||||
if (isCentral) u16(h, 20); // made by
|
||||
u16(h, 20); // needed
|
||||
u16(h, 0x0800); // flags: UTF-8 names
|
||||
u16(h, method);
|
||||
u16(h, 0); u16(h, 0x21); // time, date (1980-01-01)
|
||||
u32(h, crc);
|
||||
u32(h, (uint32_t)packed.size());
|
||||
u32(h, (uint32_t)e.data.size());
|
||||
u16(h, (uint32_t)e.name.size());
|
||||
u16(h, 0); // extra
|
||||
if (isCentral) { u16(h, 0); u16(h, 0); u16(h, 0); u32(h, 0); u32(h, offset); }
|
||||
h.insert(h.end(), e.name.begin(), e.name.end());
|
||||
};
|
||||
header(o, false);
|
||||
o.insert(o.end(), packed.begin(), packed.end());
|
||||
header(central, true);
|
||||
++count;
|
||||
}
|
||||
const uint32_t cdOffset = (uint32_t)o.size();
|
||||
o.insert(o.end(), central.begin(), central.end());
|
||||
u32(o, 0x06054b50);
|
||||
u16(o, 0); u16(o, 0);
|
||||
u16(o, count); u16(o, count);
|
||||
u32(o, (uint32_t)central.size());
|
||||
u32(o, cdOffset);
|
||||
u16(o, 0);
|
||||
return o;
|
||||
}
|
||||
|
||||
bool read(const uint8_t *b, size_t size, std::vector<Entry> &out, uint64_t maxTotal, size_t maxEntries) {
|
||||
out.clear();
|
||||
if (size < 22) return false;
|
||||
// End of central directory: the last signature within the 64 KiB comment window.
|
||||
size_t eocd = SIZE_MAX;
|
||||
const size_t lowest = size > 22 + 65535 ? size - 22 - 65535 : 0;
|
||||
for (size_t i = size - 22 + 1; i-- > lowest;) {
|
||||
if (r32(b + i) == 0x06054b50) { eocd = i; break; }
|
||||
}
|
||||
if (eocd == SIZE_MAX) return false;
|
||||
const size_t count = r16(b + eocd + 10);
|
||||
const size_t cdSize = r32(b + eocd + 12), cdOffset = r32(b + eocd + 16);
|
||||
if (count > maxEntries || cdOffset > size || cdSize > size - cdOffset) return false;
|
||||
uint64_t total = 0;
|
||||
size_t p = cdOffset;
|
||||
for (size_t k = 0; k < count; ++k) {
|
||||
if (p + 46 > size || r32(b + p) != 0x02014b50) { out.clear(); return false; }
|
||||
const uint32_t flags = r16(b + p + 8), method = r16(b + p + 10), crc = r32(b + p + 16);
|
||||
const size_t csize = r32(b + p + 20), usize = r32(b + p + 24);
|
||||
const size_t nameLen = r16(b + p + 28), extraLen = r16(b + p + 30), commentLen = r16(b + p + 32);
|
||||
const size_t local = r32(b + p + 42);
|
||||
if (p + 46 + nameLen > size) { out.clear(); return false; }
|
||||
std::string name((const char *)b + p + 46, nameLen);
|
||||
p += 46 + nameLen + extraLen + commentLen;
|
||||
if (flags & 1) { out.clear(); return false; } // encrypted
|
||||
if (!name.empty() && name.back() == '/') continue; // a directory
|
||||
total += usize;
|
||||
if (total > maxTotal) { out.clear(); return false; }
|
||||
if (local + 30 > size || r32(b + local) != 0x04034b50) { out.clear(); return false; }
|
||||
const size_t data = local + 30 + r16(b + local + 26) + r16(b + local + 28);
|
||||
if (data > size || csize > size - data) { out.clear(); return false; }
|
||||
Entry e;
|
||||
e.name = std::move(name);
|
||||
if (method == 0) {
|
||||
if (csize != usize) { out.clear(); return false; }
|
||||
e.data.assign(b + data, b + data + csize);
|
||||
} else if (method == 8) {
|
||||
if (!inflateRaw(b + data, csize, e.data, usize)) { out.clear(); return false; }
|
||||
} else { out.clear(); return false; }
|
||||
if ((uint32_t)crc32(0, e.data.data(), (uInt)e.data.size()) != crc) { out.clear(); return false; }
|
||||
out.push_back(std::move(e));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool safeName(const std::string &name) {
|
||||
if (name.empty() || name[0] == '/' || name.find('\\') != std::string::npos || name.find('\0') != std::string::npos) return false;
|
||||
size_t start = 0;
|
||||
while (start <= name.size()) {
|
||||
size_t end = name.find('/', start);
|
||||
if (end == std::string::npos) end = name.size();
|
||||
const std::string part = name.substr(start, end - start);
|
||||
if (part == ".." || part == ".") return false;
|
||||
start = end + 1;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace sizzle::io::zip
|
||||
@@ -0,0 +1,40 @@
|
||||
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
// A small ZIP reader/writer (zlib) for project bundles (.szgz) and backups, the
|
||||
// archives java.util.zip writes and reads on Android. Whole archives in memory.
|
||||
// Writes deflated entries with sizes and CRC in the local headers (no data
|
||||
// descriptors), so ZipInputStream reads them. Reads through the central directory,
|
||||
// so entries with data descriptors (what ZipOutputStream writes) are fine. No
|
||||
// Zip64, no encryption.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace sizzle::io::zip {
|
||||
|
||||
struct Entry {
|
||||
std::string name;
|
||||
std::vector<uint8_t> data;
|
||||
};
|
||||
|
||||
/** An archive of [entries], deflated. */
|
||||
std::vector<uint8_t> write(const std::vector<Entry> &entries);
|
||||
|
||||
/**
|
||||
* Every file entry of the archive in [bytes] (directories skipped). False, with
|
||||
* [out] cleared, if it isn't a ZIP, is damaged (a CRC mismatch too), uses a
|
||||
* method other than stored/deflate, has more than [maxEntries] entries or more
|
||||
* than [maxTotal] bytes uncompressed (Android's ZipLimits budgets).
|
||||
*/
|
||||
bool read(const uint8_t *bytes, size_t size, std::vector<Entry> &out,
|
||||
uint64_t maxTotal = 512ull << 20, size_t maxEntries = 20000);
|
||||
|
||||
/** A name that is safe to extract: relative, no "..", no backslashes or NULs. */
|
||||
bool safeName(const std::string &name);
|
||||
|
||||
} // namespace sizzle::io::zip
|
||||
Reference in New Issue
Block a user