--[[-- moonsource_weread.package:App 章节包口令派生与 ZipCrypto 解包。 --]] local Assert = require("support.assert") local Package = require("moonsource_weread.package") -- 抓包样本:vid 与响应头 encryptKey → 口令。 Assert.eq(Package.password("393727764", "LEyM8AVZMBvMt93WBCFCPw=="), "I1RVbke@p") Assert.is_nil(Package.password("abc", "LEyM8AVZMBvMt93WBCFCPw==")) -- zip -P 生成:a.txt 为 deflate、b.txt 为 stored,均带数据描述符。 local hex = "504b0304140009000800e5a2565df9129239220000000902000005001c00612e74787455540900032e00da6a2e00da6a75780b000104f50100000400000000905dd2c021a840192b344394132d37f6762d7d7cf1a02473b117f93084642c384d7f504b0708f91292392200000009020000504b03040a0009000000e5a2565d69111fb6120000000600000005001c00622e74787455540900032e00da6a2e00da6a75780b000104f50100000400000000dfd0647c7666697985230063aeed35d06c94504b070869111fb61200000006000000504b01021e03140009000800e5a2565df91292392200000009020000050018000000000001000000a48100000000612e74787455540500032e00da6a75780b000104f50100000400000000504b01021e030a0009000000e5a2565d69111fb61200000006000000050018000000000001000000a48171000000622e74787455540500032e00da6a75780b000104f50100000400000000504b0506000000000200020096000000d20000000000" local zip = hex:gsub("%x%x", function(h) return string.char(tonumber(h, 16)) end) local files = Package.unzip(zip, "I1RVbke@p") Assert.eq(files["a.txt"], ("hello 月读 "):rep(40) .. "\n") Assert.eq(files["b.txt"], "second") local need = Package.unzipNeed(zip) local unpacked = #files["a.txt"] + #files["b.txt"] Assert.eq(need, 2 * unpacked + 3 * 34 + 65536, "预检覆盖正文拼接、最大加密条目的三份临时缓冲与 inflate 输出块") Assert.eq(Package.unzipNeed("not a zip"), 3 * #"not a zip") local bad, err = Package.unzip(zip, "wrong") Assert.is_nil(bad) Assert.matches(err, "^zip:") local none, err2 = Package.unzip(zip, nil) Assert.is_nil(none) Assert.matches(err2, "password required") local info = "info" package.loaded["json"] = { decode = function(raw) Assert.eq(raw, info) return { chapters = { { chapterUid = 27, files = { "Text/S.xhtml", "Text/S_1.xhtml", "Text/S_2.xhtml" } } } } end, } package.loaded["moonsource_weread.package"] = nil local Composed = require("moonsource_weread.package") local book = { ["info.txt"] = info, ["Text/S.xhtml"] = "

T

", ["Text/S_1.xhtml"] = "

a

", ["Text/S_2.xhtml"] = "

b

", } Assert.eq(Composed.chapterHtml(book, 27), "

T

a

b

") local miss, merr = Composed.chapterHtml(book, 99) Assert.is_nil(miss) Assert.matches(merr, "not in package") -- TXT 走 ZIP 时 info.txt 只有 chapterUid、没有 files:不能再索引 files[1] 崩掉。 package.loaded["json"] = { decode = function() return { chapters = { { chapterUid = 1595 } } } end, } package.loaded["moonsource_weread.package"] = nil local NoFiles = require("moonsource_weread.package") local bare, bare_err = NoFiles.chapterHtml({ ["info.txt"] = "bare" }, 1595) Assert.is_nil(bare) Assert.matches(bare_err, "no HTML files") -- ZIP 解开后的 map:正文条目 bookId_uid / bookId_uid_o(实机样本)及续篇。 local zip_map = { ["info.txt"] = '{"chapters":[{"chapterUid":1595}]}', ["820954_1595_o"] = "\239\187\191甲", ["820954_1595_0001"] = "乙", ["820954_1596_o"] = "别章", } Assert.eq(Package.chapterZipText(zip_map, "820954", 1595), "甲乙", "_o 主文件须排在 _0001 续篇前") Assert.is_nil(Package.chapterZipText(zip_map, "820954", 9)) Assert.is_nil(Package.chapterZipText({ ["info.txt"] = "{}" }, "820954", 1)) local function tarHeader(name, size) local h = string.rep("\0", 512) local function poke(off, s) h = h:sub(1, off - 1) .. s .. h:sub(off + #s) end poke(1, name) poke(125, string.format("%011o", size)) poke(157, "0") poke(258, "ustar\0") return h end local function tarEntry(name, data) local padded = data .. string.rep("\0", (512 - (#data % 512)) % 512) return tarHeader(name, #data) .. padded end local tar = tarEntry("info.txt", "{}") .. tarEntry("820954_1", "\239\187\191甲") .. tarEntry("820954_1_0001", "乙") .. tarEntry("820954_12", "别章") .. string.rep("\0", 1024) local text = Package.chapterText(tar, "820954", 1) Assert.eq(text, "甲乙") local absent = Package.chapterText(tar, "820954", 9) Assert.is_nil(absent) Assert.eq(Package.pickStyle({ ["Styles/stylesheets.css"] = "a" }), "a") Assert.eq(Package.pickStyle({ ["Styles/stylesheet.css"] = "b" }), "b") Assert.eq(Package.pickStyle({ ["Styles/extra.css"] = "x", ["Styles/stylesheet.css"] = "b", }), "b") Assert.eq(Package.pickStyle({ ["Styles/a.css"] = "1", ["Styles/z.css"] = "2" }), "1") Assert.is_nil(Package.pickStyle({ ["Text/a.xhtml"] = "

" })) -- 官方 chapters 批次串:连续合并,首批 0-n,其后 0,a-b。 Assert.eq(Package.formatChaptersParam({ 1, 2, 3, 4, 5 }), "0-5") Assert.eq(Package.formatChaptersParam({ 6, 7, 8, 9, 10 }), "0,6-10") Assert.eq(Package.formatChaptersParam({ 96, 97, 98, 99, 100 }), "0,96-100") Assert.eq(Package.formatChaptersParam({ 12 }), "0,12") Assert.eq(Package.formatChaptersParam({}), "0") Assert.eq(Package.formatChaptersParam({ "a", "b" }), "0,a,b") -- 批量解包:一次 zip 多章 + 样式。 package.loaded["json"] = { decode = function() return { chapters = { { chapterUid = 1, files = { "Text/c1.xhtml" } }, { chapterUid = 2, files = { "Text/c2.xhtml" } }, } } end, } package.loaded["moonsource_weread.package"] = nil local Batch = require("moonsource_weread.package") local batch_files = { ["info.txt"] = "info", ["Text/c1.xhtml"] = "

一

", ["Text/c2.xhtml"] = "

二

", ["Styles/stylesheets.css"] = ".x{}", } local extracted, style = Batch.extractChapters(batch_files, "bid", { 1, 2 }, "epub") Assert.eq(style, ".x{}") Assert.eq(extracted["1"].text, "

一

") Assert.eq(extracted["2"].text, "

二

") Assert.is_nil(extracted["1"].err) local miss = Batch.extractChapters(batch_files, "bid", { 9 }, "epub") Assert.not_nil(miss["9"].err)