diff --git a/CHANGELOG.md b/CHANGELOG.md index 3cdcf4e..83c3e11 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,35 @@ tool under `tools/` keeps its own. ## [Unreleased] +## [v0.14.0] - 2026-12-02 + +This release narrows what `route.At` reads as text: a head that holds a control byte, or whose +bytes are neither UTF-8 nor windows-1252, is no longer text. `route.MediaType` now types text of no +signature by its name's extension, and a HEIC by its major brand. + +### Changed + +- **Breaking:** content of no routed signature, or content that fails a claim's own check, is text + only when its first `route.HeadLen` bytes pass two tests. They hold no control byte, meaning no + C0 control other than tab, line feed, form feed, or carriage return, and no DEL. They are also + either valid UTF-8 or windows-1252 without its five undefined bytes, 0x81, 0x8D, 0x8F, 0x90, and + 0x9D; a UTF-8 rune cut off at the head's end is allowed when the content runs past the head. + Before, any head without a NUL byte was text. Content with a control byte but no NUL byte, such + as a log holding terminal escapes, and content that is neither UTF-8 nor windows-1252 now routes + `route.Unrouted`, and `route.MediaType` types it application/octet-stream, not text/plain. + Content stored as an audio or video type routes by that type when its head fails the same tests. + A byte order mark still makes content text before the tests run. +- `route.MediaType` types content of no signature that routes `route.Text` by its name's extension, + as `route.Ext` reads it, from a table in the `route` package: text/markdown for .md or .markdown, + text/csv for .csv, text/tab-separated-values for .tsv, text/html for .html or .htm, + application/xml for .xml, application/json for .json, application/yaml for .yaml or .yml, text/css + for .css, text/javascript for .js or .mjs, text/calendar for .ics, and text/vcard for .vcf; any + other extension stays text/plain, as does text marked by a byte order mark or a script's "#!" + line. Content that isn't text stays application/octet-stream whatever its name. +- `route.MediaType` types a `route.HEIC` by its ftyp box's major brand: image/heic for heic, heix, + heim, or heis; image/heic-sequence for hevc or hevx; image/heif for heif, mif1, or miaf; and + image/heif-sequence for msf1. Before, every HEIC was image/heic. Its route is unchanged. + ## [v0.13.0] - 2026-12-02 This release adds `route.MediaType`, the media type of content as `route.At` routes it, on `At`'s @@ -745,7 +774,8 @@ fixtures scripts stay in the spike. The [limits page](docs/limits.md) states them: each stage's tool's limits, docproc-ooxml's behavior under a memory bound, the Threader's memory, and the formats no stage reads. -[Unreleased]: https://github.com/standards-lab/docproc/compare/v0.13.0...HEAD +[Unreleased]: https://github.com/standards-lab/docproc/compare/v0.14.0...HEAD +[v0.14.0]: https://github.com/standards-lab/docproc/compare/v0.13.0...v0.14.0 [v0.13.0]: https://github.com/standards-lab/docproc/compare/v0.12.0...v0.13.0 [v0.12.0]: https://github.com/standards-lab/docproc/compare/v0.11.0...v0.12.0 [v0.11.0]: https://github.com/standards-lab/docproc/compare/v0.10.0...v0.11.0 diff --git a/README.md b/README.md index cf07483..3c58e0f 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ tags. ## Install ```sh -go get github.com/standards-lab/docproc@v0.13.0 +go get github.com/standards-lab/docproc@v0.14.0 ``` The module's releases are tagged `vX.Y.Z`, and [`CHANGELOG.md`](CHANGELOG.md) states what each diff --git a/cmd/docproc/CHANGELOG.md b/cmd/docproc/CHANGELOG.md index 946053d..a6dd659 100644 --- a/cmd/docproc/CHANGELOG.md +++ b/cmd/docproc/CHANGELOG.md @@ -18,7 +18,7 @@ postgres modules, and each system tool under `tools/` keep their own. stays beside its `export_test.go`. The command line, run directory, and exit codes don't change. - `mise run build` at the repository root writes the docproc command to the git-ignored `bin/docproc`. -- The module requires the docproc base module at v0.12.0, from v0.11.0, whose +- The module requires the docproc base module at v0.13.0, from v0.11.0, whose [CHANGELOG](../../CHANGELOG.md) states its changes in full. - The module requires go-core v0.15.0, from v0.14.0; nothing `docproc run` does changes with it. diff --git a/cmd/docproc/go.mod b/cmd/docproc/go.mod index 765476a..42fa8a8 100644 --- a/cmd/docproc/go.mod +++ b/cmd/docproc/go.mod @@ -3,7 +3,7 @@ module github.com/standards-lab/docproc/cmd/docproc go 1.27.4 require ( - github.com/standards-lab/docproc v0.12.0 + github.com/standards-lab/docproc v0.13.0 github.com/standards-lab/go-cli-sdk v0.4.0 github.com/standards-lab/go-core v0.15.0 golang.org/x/sys v0.50.0 diff --git a/cmd/docproc/go.sum b/cmd/docproc/go.sum index 65bcb04..726a995 100644 --- a/cmd/docproc/go.sum +++ b/cmd/docproc/go.sum @@ -1,5 +1,5 @@ -github.com/standards-lab/docproc v0.12.0 h1:fKof6WCaze3tu+sCI5cx3OgsH5s86Aus65DBsQNA0Mg= -github.com/standards-lab/docproc v0.12.0/go.mod h1:ZA8JYQPooA6YA84b00Lpu5AiSL2VSj/u7QnenltqVBA= +github.com/standards-lab/docproc v0.13.0 h1:WRvUFHvMM1eFxBr9sFkgRH7Co2GkW2jgrEvdflPY6Io= +github.com/standards-lab/docproc v0.13.0/go.mod h1:ZA8JYQPooA6YA84b00Lpu5AiSL2VSj/u7QnenltqVBA= github.com/standards-lab/go-cli-sdk v0.4.0 h1:232dyKPD5kqshp0BMEVEZaedwJfvnDVqWLv7m1NQWmI= github.com/standards-lab/go-cli-sdk v0.4.0/go.mod h1:7Y1BCcHtuu8Unxc+Hryjlln+0fbiMYa5xY75aueJ/rY= github.com/standards-lab/go-core v0.15.0 h1:yYjuUDlI7CupvtHJMT9Db7dAzNug3QJPGAEVruhIJI8= diff --git a/docs/api.md b/docs/api.md index 010bb71..f108949 100644 --- a/docs/api.md +++ b/docs/api.md @@ -96,6 +96,8 @@ func Identify(head []byte) Kind // at most HeadLe func File(path, name, mimeType string) (Route, string, error) // the route, and why func At(r io.ReaderAt, size int64, head []byte, name, mimeType string) (Route, string, error) func MediaType(r io.ReaderAt, size int64, head []byte, name, mimeType string) (string, error) + // a Kind's type, a HEIC's by its major brand; text of no signature by Ext from route's + // table, else text/plain; content that isn't text application/octet-stream func Ext(name string) string // trimmed and lowercased func EncryptedPackage(r io.ReaderAt, size int64) (bool, error) // an OLE2 EncryptedPackage func RARVolume(head []byte) bool // a RAR's main header marks a volume @@ -931,9 +933,10 @@ What each stage yields, and why, beside the `Meta` keys `dispatch` documents on images, embedded files, and ink, is found at `page , media `, named by the name it was inserted or attached under, else by its media name; one with no content written, its data missing or invalid, is a failed child. A recording, an embedded file of type `audio` or `video`, carries - that type as its stored type, so content of no signature route knows, a NUL in its head, is - routed by it. A page's ink is an SVG, routed vision-converted, to the picture stage's PNG. A skipped section is an - unread child at `skipped section `: encrypted for a password-protected one, else failed. + that type as its stored type, so content of no signature route knows is routed by that type when + its head isn't text. A page's ink is an SVG, routed vision-converted, to the picture stage's PNG. + A skipped section is an unread child at `skipped section `: encrypted for a + password-protected one, else failed. - **audio.** No model runs: the stage prepares the windows a layer above transcribes and joins, and the plan it joins them by. Each window is yielded as the part's one decode cuts it, in plan order, `window N`, a 16 kHz mono 16-bit PCM WAV named by its SHA-256, its `Meta` carrying what a caller diff --git a/docs/limits.md b/docs/limits.md index 3c475e6..cc748af 100644 --- a/docs/limits.md +++ b/docs/limits.md @@ -5,12 +5,15 @@ an unencrypted one that is not a text, spreadsheet, presentation, or drawing doc such as a formula, chart, database, or image package (`.odf`, `.odc`, `.odb`, `.odi`), with no processor yet. ISO routes to no stage. docproc runs no virus scan and flags no macro in an OLE2 file. Content no processor claims is read as text, whatever its name, when its first 4096 bytes hold -no NUL byte, and is unrouted when they do. So HTML and an EPUB's entries are read as raw text, with -no structure, and UTF-16 without a byte order mark, which holds NULs, is refused. docproc detects no -text's encoding: it reads text by its byte order mark, as UTF-8, or else as windows-1252, so a -legacy multibyte encoding such as Shift-JIS, EUC-JP, or ISO-2022-JP reads as mojibake. Beyond that, -each stage inherits its tool's limits, as the tools' READMEs -([docproc-ooxml](../tools/ooxml/README.md), [docproc-mail](../tools/mail/README.md), +no control byte and are either valid UTF-8 or windows-1252 without its five undefined bytes (0x81, +0x8D, 0x8F, 0x90, 0x9D), and is unrouted otherwise. A control byte is a C0 control other than tab, +line feed, form feed, or carriage return, or DEL. A UTF-8 rune cut off at the 4096th byte is allowed +when the content runs past it. So HTML and an EPUB's entries are read as raw text, with no +structure, while UTF-16 without a byte order mark, which holds NULs, and a log holding terminal +escapes are refused. docproc detects no text's encoding: it reads text by its byte order mark, as +UTF-8, or else as windows-1252, so a legacy multibyte encoding such as Shift-JIS, EUC-JP, or +ISO-2022-JP reads as mojibake. Beyond that, each stage inherits its tool's limits, as the tools' +READMEs ([docproc-ooxml](../tools/ooxml/README.md), [docproc-mail](../tools/mail/README.md), [docproc-onenote](../tools/onenote/README.md)) state them in full; these are the ones a host sizes or routes by. diff --git a/postgres/CHANGELOG.md b/postgres/CHANGELOG.md index d39a903..2e990b7 100644 --- a/postgres/CHANGELOG.md +++ b/postgres/CHANGELOG.md @@ -14,7 +14,7 @@ each system tool under `tools/` keep their own. - The module's unit tests and integration suite sit in its `test/` directory as an external test package. No exported API changes. -- The module requires the docproc base module at v0.12.0, from v0.11.0, and passes its +- The module requires the docproc base module at v0.13.0, from v0.11.0, and passes its `dispatchtest.Index` suite; its [CHANGELOG](../CHANGELOG.md) states the base module's changes. ## [v0.1.9] - 2026-11-28 diff --git a/postgres/go.mod b/postgres/go.mod index 21c0a76..0567e7d 100644 --- a/postgres/go.mod +++ b/postgres/go.mod @@ -4,7 +4,7 @@ go 1.27.4 require ( github.com/jackc/pgx/v5 v5.13.0 - github.com/standards-lab/docproc v0.12.0 + github.com/standards-lab/docproc v0.13.0 github.com/standards-lab/sqlate v0.8.0 github.com/standards-lab/sqlate/postgres v0.9.0 ) diff --git a/postgres/go.sum b/postgres/go.sum index 8444874..37dd6a6 100644 --- a/postgres/go.sum +++ b/postgres/go.sum @@ -13,8 +13,8 @@ github.com/jackc/puddle/v2 v2.2.3 h1:f8HoREISb3l5eO9coPgK7U3hKLhWGokOFVcThl5jrao github.com/jackc/puddle/v2 v2.2.3/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/standards-lab/docproc v0.12.0 h1:fKof6WCaze3tu+sCI5cx3OgsH5s86Aus65DBsQNA0Mg= -github.com/standards-lab/docproc v0.12.0/go.mod h1:ZA8JYQPooA6YA84b00Lpu5AiSL2VSj/u7QnenltqVBA= +github.com/standards-lab/docproc v0.13.0 h1:WRvUFHvMM1eFxBr9sFkgRH7Co2GkW2jgrEvdflPY6Io= +github.com/standards-lab/docproc v0.13.0/go.mod h1:ZA8JYQPooA6YA84b00Lpu5AiSL2VSj/u7QnenltqVBA= github.com/standards-lab/sqlate v0.8.0 h1:G1uUIWmc2DxJz5YhtBvIlftfURnb3b2RKsYB59Wa03Y= github.com/standards-lab/sqlate v0.8.0/go.mod h1:pikNmGf/z3x46SBWFXgt/xKIDZuquSgCoXy5OWBFFR4= github.com/standards-lab/sqlate/postgres v0.9.0 h1:l6PXEZ3GQM4+0zekxlSONhyHjpQU8g8StAS16bBqzZE= diff --git a/route/doc.go b/route/doc.go index 87ac734..dae95d5 100644 --- a/route/doc.go +++ b/route/doc.go @@ -23,7 +23,7 @@ // for a cabinet, and for content of no signature, which is a message by the .eml rule. A name's // extension never makes content text, nor does a stored audio or video type make text media. // Content no route claims, or that fails a claim's own check, falls back on its head: text when -// its first [HeadLen] bytes hold no NUL byte, and unrouted when they do, its reason the claim it +// its first [HeadLen] bytes are text, and unrouted when they aren't, its reason the claim it // failed, or "no routed signature", then ", text by its head" or ", not text"; content named .eml // or stored as a message whose header isn't a message's shape is the exception, its reason "a // message by its , but not header-shaped", then "; text by its head" when it is @@ -34,6 +34,11 @@ // whole for an EncryptedPackage stream's directory entry, by [EncryptedPackage], which inspect // calls too; an mbox is read past its "From " line for a message's header; and content of no // signature is read for a message's header, a header after a UTF-8 byte order mark among them. +// A head is text when it holds no C0 control other than tab, line feed, form feed, or carriage +// return, and no DEL, and is either valid UTF-8 or windows-1252 without its five undefined bytes, +// 0x81, 0x8D, 0x8F, 0x90, and 0x9D; a rune the head cuts off at its end is valid when the content +// runs past the head. A byte order mark makes content text ahead of the test, so UTF-16 and UTF-32 +// marked by one are text. // The routes: // // - [Document]: a PDF, for the document package's stage. @@ -57,11 +62,11 @@ // - [Archive]: a ZIP that is neither office's nor ODF, a tar, gzip, zstd, 7z, RAR4, RAR5, xz, // or bzip2 archive, or a cabinet not named .onepkg. // - [Audio]: WAV, FLAC, MP3, an Ogg that begins no video stream, or M4A; or content of no -// signature stored as an audio type whose head holds a NUL byte. +// signature stored as an audio type whose head isn't text. // - [Video]: MP4, Matroska, WebM, AVI, an MPEG program stream, or an Ogg video; or content of -// no signature stored as a video type whose head holds a NUL byte. +// no signature stored as a video type whose head isn't text. // - [Text]: content marked by a byte order mark; a script, by its "#!" line; and the -// fallback's content whose head holds no NUL byte, whatever its name or stored audio or video +// fallback's content whose head is text, whatever its name or stored audio or video // type: HTML, CSS, JavaScript, an EPUB's entries, a .txt, .csv, or .json, a LICENSE, prose // that begins "From " without a message header, or content named .eml or stored as a message // whose header isn't a message's shape, but for an SVG, which routes by its root element. @@ -69,7 +74,7 @@ // executable; an .ost, whose reason names it, since mail refuses one; an XPS package, a ZIP // holding FixedDocumentSequence.fdseq, whole or as its first piece, whatever its name, ahead // of the OOXML rules, since one is an OPC package too and may be named .docx; and the -// fallback's content whose head holds a NUL byte, an OLE2 compound file without a legacy or +// fallback's content whose head isn't text, an OLE2 compound file without a legacy or // .msg extension, an mbox's "From " line without a message header, and content named .eml or // stored as a message whose header isn't a message's shape among it. // @@ -91,9 +96,12 @@ // - [File] routes the file at a path, and says why; [At] routes content already open. // - [MediaType] gives the media type of content as At routes it, on At's inputs: a [Kind]'s // type by its signature; within a ZIP, an ODF package, or an OLE2 compound file, the name's -// extension first, for an OOXML, OpenDocument, or legacy office type, .msg's, or .zip's; and, -// for content of no signature, text/plain, message/rfc822, or application/octet-stream by -// its route. +// extension first, for an OOXML, OpenDocument, or legacy office type, .msg's, or .zip's; a +// HEIC's by its ftyp box's major brand, image/heic, image/heic-sequence, image/heif, or +// image/heif-sequence; and, for content of no signature, message/rfc822 or +// application/octet-stream by its route, or, for [Text], a type by the name's extension from +// route's own table, such as text/markdown for .md, text/csv for .csv, or application/json +// for .json, and text/plain for an extension outside it. // - [Ext] reads a name's extension; [OOXMLExtension] reports whether it is an OOXML // package's, the one table of them; [LegacyTarget] gives the OOXML format a legacy // extension converts to, the one table of those; [EncryptedPackage] reports whether an OLE2 diff --git a/route/identify.go b/route/identify.go index 0149912..4690685 100644 --- a/route/identify.go +++ b/route/identify.go @@ -425,10 +425,10 @@ func isRIFF(head []byte) Kind { } func isISOBMFF(head []byte) Kind { - if len(head) < 12 || string(head[4:8]) != "ftyp" { + major, ok := majorBrand(head) + if !ok { return Unknown } - major := brand(head[8:12]) boxLen := min(int(binary.BigEndian.Uint32(head)), len(head)) var compatible []string for i := 16; i+4 <= boxLen; i += 4 { @@ -452,6 +452,13 @@ func isISOBMFF(head []byte) Kind { return MP4 } +func majorBrand(head []byte) (string, bool) { + if len(head) < 12 || string(head[4:8]) != "ftyp" { + return "", false + } + return brand(head[8:12]), true +} + func brand(b []byte) string { return string(bytes.TrimRight(b, " \x00")) } diff --git a/route/media.go b/route/media.go index 1c6335b..787aead 100644 --- a/route/media.go +++ b/route/media.go @@ -18,7 +18,6 @@ var kindTypes = map[Kind]string{ EMF: "image/emf", WMF: "image/wmf", SVG: "image/svg+xml", - HEIC: "image/heic", WebP: "image/webp", AVIF: "image/avif", ZIP: zipType, @@ -59,6 +58,31 @@ var kindTypes = map[Kind]string{ UTF32BOM: textPlain, } +var heifTypes = map[string]string{ + "heic": "image/heic", "heix": "image/heic", "heim": "image/heic", "heis": "image/heic", + "hevc": "image/heic-sequence", "hevx": "image/heic-sequence", + "heif": "image/heif", "mif1": "image/heif", "miaf": "image/heif", + "msf1": "image/heif-sequence", +} + +var textTypes = map[string]string{ + ".md": "text/markdown", + ".markdown": "text/markdown", + ".csv": "text/csv", + ".tsv": "text/tab-separated-values", + ".html": "text/html", + ".htm": "text/html", + ".xml": "application/xml", + ".json": "application/json", + ".yaml": "application/yaml", + ".yml": "application/yaml", + ".css": "text/css", + ".js": "text/javascript", + ".mjs": "text/javascript", + ".ics": "text/calendar", + ".vcf": "text/vcard", +} + var zipTypes = map[string]string{ ".zip": zipType, ".odt": "application/vnd.oasis.opendocument.text", @@ -85,15 +109,26 @@ const outlookType = "application/vnd.ms-outlook" // application/vnd.ms-powerpoint, or application/vnd.ms-outlook for .msg. Otherwise the [Kind] // head's signature names decides, such as application/pdf for a [PDF], application/zip for a ZIP // or an ODF package, application/x-ole-storage for an OLE2 compound file, application/mbox for an -// mbox, and text/plain for a script or a byte order mark. Content of no signature takes the route -// At gives it, with name and mimeType: text/plain for [Text], message/rfc822 for [Nested], and -// application/octet-stream otherwise. It returns an error only for r's own. +// mbox, and text/plain for a script or a byte order mark. A [HEIC] takes its type from its ftyp +// box's major brand: image/heic for heic, heix, heim, or heis; image/heic-sequence for hevc or +// hevx; image/heif for heif, mif1, or miaf; and image/heif-sequence for msf1. Content of no +// signature takes the route At gives it, with name and mimeType: message/rfc822 for [Nested]; +// application/octet-stream for any route but [Text], whatever its name; and, for Text, a type by +// the name's extension, text/markdown for .md or .markdown, text/csv for .csv, +// text/tab-separated-values for .tsv, text/html for .html or .htm, application/xml for .xml, +// application/json for .json, application/yaml for .yaml or .yml, text/css for .css, +// text/javascript for .js or .mjs, text/calendar for .ics, and text/vcard for .vcf, or text/plain +// for any other. It returns an error only for r's own. func MediaType(r io.ReaderAt, size int64, head []byte, name, mimeType string) (string, error) { head = head[:min(len(head), HeadLen)] kind := Identify(head) if t := containerType(kind, Ext(name)); t != "" { return t, nil } + if kind == HEIC { + major, _ := majorBrand(head) + return heifTypes[major], nil + } if t, ok := kindTypes[kind]; ok { return t, nil } @@ -103,6 +138,9 @@ func MediaType(r io.ReaderAt, size int64, head []byte, name, mimeType string) (s } switch routed { case Text: + if t, ok := textTypes[Ext(name)]; ok { + return t, nil + } return textPlain, nil case Nested: return "message/rfc822", nil diff --git a/route/route.go b/route/route.go index 8e1ae56..26a8365 100644 --- a/route/route.go +++ b/route/route.go @@ -9,6 +9,7 @@ import ( "os" "path" "strings" + "unicode/utf8" ) // Route names the stage content goes to next, for the caller to run, as the package doc lists @@ -54,24 +55,27 @@ const ( Archive Route = "archive" // Audio is WAV, FLAC, MP3, an Ogg that begins no video stream, or M4A; and content of no // signature whose stored MIME type's top-level type is audio and whose first [HeadLen] bytes - // hold a NUL byte, so the fallback refuses it as text. + // aren't text, as [Text] reads them, so the fallback refuses it as text. Audio Route = "audio" // Video is MP4, Matroska, WebM, AVI, an MPEG program stream, or an Ogg that begins a Theora, // VP8, Dirac, or OGM video stream; and content of no signature whose stored MIME type's - // top-level type is video and whose first [HeadLen] bytes hold a NUL byte, so the fallback - // refuses it as text. + // top-level type is video and whose first [HeadLen] bytes aren't text, as [Text] reads them, + // so the fallback refuses it as text. Video Route = "video" // Text is content that begins with a UTF-8, UTF-16, or UTF-32 byte order mark; a script, by // its "#!" line; and, as the fallback, content no other route claims, or that fails a - // claim's own check, whose first [HeadLen] bytes hold no NUL byte, whatever its name or - // stored audio or video type: HTML, CSS, JavaScript, an EPUB's entries, a .txt, .csv, or + // claim's own check, whose first [HeadLen] bytes are text, whatever its name or stored audio + // or video type: HTML, CSS, JavaScript, an EPUB's entries, a .txt, .csv, or // .json, a LICENSE, prose that begins "From " without a message header, or a mislabelled // attachment stored as audio among it. Its reason ends ", text by its head" and begins with // the claim it failed, such as "OLE2 signature without a legacy or .msg extension", `mbox // "From " line without a message header`, or "no routed signature"; for content named .eml or // stored as a message whose header isn't a message's shape, it is "a message by its , but not header-shaped; text by its head". A name's extension never makes content - // text. + // text. A head is text when it holds no C0 control other than tab, line feed, form feed, or + // carriage return, and no DEL, and is either valid UTF-8 or windows-1252 without its five + // undefined bytes, 0x81, 0x8D, 0x8F, 0x90, and 0x9D. A rune the head cuts off at its end is + // valid when the content runs past the head, and invalid when the content ends there. Text Route = "text" // Unrouted is a deliberate refusal: an executable, by a PE, ELF, or Mach-O signature; an XPS // package, a ZIP holding a FixedDocumentSequence.fdseq entry, whole or as its first piece, @@ -79,7 +83,8 @@ const ( // named .docx; and an .ost, an offline store mail refuses. It is the fallback's too: content // no other route claims, or that fails a claim's own check, such as an OLE2 compound file // without a legacy or .msg extension, nor an encrypted OOXML package's stream and name, or an - // mbox's "From " line without a message header, whose first [HeadLen] bytes hold a NUL byte. + // mbox's "From " line without a message header, whose first [HeadLen] bytes aren't text, as + // [Text] reads them: a control byte, or bytes that neither UTF-8 nor windows-1252 reads. // Its reason then ends ", not text" and begins with the claim it failed, or "no routed // signature"; for content named .eml or stored as a message whose header isn't a message's // shape, it is "a message by its , but not header-shaped". @@ -232,13 +237,14 @@ func File(path, name, mimeType string) (Route, string, error) { // whose first line is a field: an mbox is [Nested] only with one, and content of no signature, or // of a UTF-8 byte order mark, with one is a message whatever its name. Content of no signature // Identify knows whose mimeType's top-level type is audio or video, such as a OneNote recording's -// stored "audio", routes Audio or Video only when head holds a NUL byte. Content no route claims, +// stored "audio", routes Audio or Video only when head isn't text. Content no route claims, // an OLE2 compound file without a legacy or .msg extension, nor an encrypted OOXML package's stream // and name, an mbox without a message's header, and content named .eml or stored as a message whose -// header isn't a message's shape fall back on head: [Text] when it holds no NUL byte, [Unrouted] -// when it does. It returns an error only for r's own. +// header isn't a message's shape fall back on head: [Text] when it is text, as Text's doc reads +// a head, and [Unrouted] when it isn't. It returns an error only for r's own. func At(r io.ReaderAt, size int64, head []byte, name, mimeType string) (Route, string, error) { head = head[:min(len(head), HeadLen)] + cut := int64(len(head)) < size ext := Ext(name) kind := Identify(head) switch kind { @@ -275,10 +281,10 @@ func At(r io.ReaderAt, size int64, head []byte, name, mimeType string) (Route, s return Nested, `mbox "From " line`, nil } claim := `mbox "From " line without a message header` - route, why := fallBack(head, claim+", text by its head", claim+", not text") + route, why := fallBack(head, cut, claim+", text by its head", claim+", not text") return route, why, nil } - if route, why := byKind(kind, ext, head); route != "" { + if route, why := byKind(kind, ext, head, cut); route != "" { return route, why, nil } readHeader := func() (headerFields, error) { return header(io.NewSectionReader(r, 0, size)) } @@ -289,12 +295,12 @@ func At(r io.ReaderAt, size int64, head []byte, name, mimeType string) (Route, s return "", "", err case !fields.shaped(): claim := "a message by its " + as + ", but not header-shaped" - route, why := fallBack(head, claim+"; text by its head", claim) + route, why := fallBack(head, cut, claim+"; text by its head", claim) return route, why, nil } return Nested, "a message by its " + as + ", header-shaped", nil } - if r := storedMedia(mimeType); r != "" && kind == Unknown && holdsNUL(head) { + if r := storedMedia(mimeType); r != "" && kind == Unknown && !textual(head, cut) { return r, "no known signature, stored as " + mimeType, nil } if kind == Unknown || kind == UTF8BOM { @@ -310,18 +316,49 @@ func At(r io.ReaderAt, size int64, head []byte, name, mimeType string) (Route, s return Text, "text by its " + bom + " byte order mark", nil } const unclaimed = "no routed signature" - route, why := fallBack(head, unclaimed+", text by its head", unclaimed+", not text") + route, why := fallBack(head, cut, unclaimed+", text by its head", unclaimed+", not text") return route, why, nil } -func fallBack(head []byte, textWhy, notTextWhy string) (Route, string) { - if holdsNUL(head) { - return Unrouted, notTextWhy +func fallBack(head []byte, cut bool, textWhy, notTextWhy string) (Route, string) { + if textual(head, cut) { + return Text, textWhy } - return Text, textWhy + return Unrouted, notTextWhy } -func holdsNUL(head []byte) bool { return bytes.IndexByte(head, 0) >= 0 } +func textual(head []byte, cut bool) bool { + for _, b := range head { + if b < 0x20 && b != '\t' && b != '\n' && b != '\f' && b != '\r' || b == 0x7f { + return false + } + } + if utf8Head(head, cut) { + return true + } + for _, b := range head { + switch b { + case 0x81, 0x8d, 0x8f, 0x90, 0x9d: + return false + } + } + return true +} + +func utf8Head(head []byte, cut bool) bool { + if utf8.Valid(head) { + return true + } + if !cut { + return false + } + for i := len(head) - 1; i >= max(len(head)-utf8.UTFMax+1, 0); i-- { + if utf8.RuneStart(head[i]) { + return !utf8.FullRune(head[i:]) && utf8.Valid(head[:i]) + } + } + return false +} func mboxHeader(r io.Reader) (headerFields, error) { br := bufio.NewReaderSize(r, headerLineMax) @@ -350,7 +387,7 @@ func storedMedia(mimeType string) Route { return "" } -func byKind(kind Kind, ext string, head []byte) (Route, string) { +func byKind(kind Kind, ext string, head []byte, cut bool) (Route, string) { if r, ok := kindRoutes[kind]; ok { return r.route, r.why } @@ -371,7 +408,7 @@ func byKind(kind Kind, ext string, head []byte) (Route, string) { return Nested, "OLE2 signature, .msg extension" } claim := "OLE2 signature without a legacy or .msg extension" - return fallBack(head, claim+", text by its head", claim+", not text") + return fallBack(head, cut, claim+", text by its head", claim+", not text") case MSCF: if ext == ".onepkg" { return OneNote, "cabinet signature, .onepkg extension" diff --git a/route/test/at_test.go b/route/test/at_test.go index 5835e3f..6da3e7d 100644 --- a/route/test/at_test.go +++ b/route/test/at_test.go @@ -89,6 +89,61 @@ func TestAtContent(t *testing.T) { } } +func TestAtText(t *testing.T) { + const ( + byHead = "no routed signature, text by its head" + notText = "no routed signature, not text" + noHeader = `mbox "From " line without a message header, not text` + eml = "a message by its .eml extension, but not header-shaped" + ) + padded := strings.Repeat("a", route.HeadLen-2) + cases := []struct { + name, content, filename, mime string + want route.Route + why, media string + }{ + {"utf-8", "café, naïve, 日本語, 😀\n", "a.txt", "", route.Text, byHead, "text/plain"}, + {"utf-8 rune cut at the head's end", padded + "\u2041 more", "a.txt", "", route.Text, byHead, "text/plain"}, + {"utf-8 four-byte rune cut at the head's end", padded + "😀 more", "a.txt", "", route.Text, byHead, "text/plain"}, + {"utf-8 rune cut where the content ends", "abc\xe2\x81", "a.txt", "", route.Unrouted, notText, "application/octet-stream"}, + {"utf-8 rune cut where a head of HeadLen ends", padded + "\xe2\x81", "a.txt", "", route.Unrouted, notText, "application/octet-stream"}, + {"utf-8 c1 control", "a\u0085b\n", "a.txt", "", route.Text, byHead, "text/plain"}, + {"windows-1252", "caf\xe9 \x93quoted\x94 \x80 5\n", "menu.txt", "", route.Text, byHead, "text/plain"}, + {"windows-1252 undefined 0x81", "caf\xe9 \x81\n", "a.txt", "", route.Unrouted, notText, "application/octet-stream"}, + {"windows-1252 undefined 0x8d", "caf\xe9 \x8d\n", "a.txt", "", route.Unrouted, notText, "application/octet-stream"}, + {"windows-1252 undefined 0x8f", "caf\xe9 \x8f\n", "a.txt", "", route.Unrouted, notText, "application/octet-stream"}, + {"windows-1252 undefined 0x90", "caf\xe9 \x90\n", "a.txt", "", route.Unrouted, notText, "application/octet-stream"}, + {"windows-1252 undefined 0x9d", "caf\xe9 \x9d\n", "a.txt", "", route.Unrouted, notText, "application/octet-stream"}, + {"start of heading", "a\x01b\n", "a.txt", "", route.Unrouted, notText, "application/octet-stream"}, + {"escape", "\x1b[31mred\x1b[0m\n", "build.log", "", route.Unrouted, notText, "application/octet-stream"}, + {"vertical tab", "a\vb\n", "a.txt", "", route.Unrouted, notText, "application/octet-stream"}, + {"delete", "a\x7fb\n", "a.txt", "", route.Unrouted, notText, "application/octet-stream"}, + {"tab, line feed, carriage return, form feed", "a\tb\r\nc\fd\n", "a.txt", "", route.Text, byHead, "text/plain"}, + {"a control past the head", strings.Repeat("a", route.HeadLen) + "\x1b", "a.txt", "", route.Text, byHead, "text/plain"}, + {"utf-16 little-endian bom", "\xff\xfeh\x00i\x00\n\x00", "a.txt", "", route.Text, "text by its UTF-16 byte order mark", "text/plain"}, + {"mbox line with an escape", "From the desk of Ann\n\x1b[0m\n", "", "", route.Unrouted, noHeader, "application/mbox"}, + {"eml not header-shaped, an escape", "Dear Bob,\x1bhello\n", "a.eml", "", route.Unrouted, eml, "application/octet-stream"}, + {"an escape stored as audio", "a\x1bb", "a.mp3", "audio/mpeg", route.Audio, "no known signature, stored as audio/mpeg", "application/octet-stream"}, + {"windows-1252 stored as audio", "caf\xe9\n", "a.mp3", "audio/mpeg", route.Text, byHead, "text/plain"}, + } + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + r := strings.NewReader(c.content) + head := []byte(c.content[:min(len(c.content), route.HeadLen)]) + got, why, err := route.At(r, int64(len(c.content)), head, c.filename, c.mime) + if err != nil { + t.Fatal(err) + } + if got != c.want || why != c.why { + t.Errorf("route %q (%s), want %q (%s)", got, why, c.want, c.why) + } + if got := mediaType(t, c.content, c.filename, c.mime); got != c.media { + t.Errorf("MediaType = %q, want %q", got, c.media) + } + }) + } +} + func TestAtNearClaims(t *testing.T) { const ( separator = "From ann@example.org Sat Jan 3 01:05:34 1996\n" diff --git a/route/test/file_test.go b/route/test/file_test.go index 8d17686..3a4d38b 100644 --- a/route/test/file_test.go +++ b/route/test/file_test.go @@ -99,7 +99,7 @@ func TestFileRoutes(t *testing.T) { {"notice", "This product includes software\n", "NOTICE", "", route.Text, byHead}, {"transcript of an unlisted extension", "[00:01] Ann: hello\n", "call.transcript", "", route.Text, byHead}, {"windows-1252 text", "caf\xe9 \x93quoted\x94 \x80 5\n", "menu.txt", "", route.Text, byHead}, - {"escape and bell", "\x1b[31mred\x1b[0m\a\n", "build.log", "", route.Text, byHead}, + {"escape and bell", "\x1b[31mred\x1b[0m\a\n", "build.log", "", route.Unrouted, notText}, {"nul in the head", "plain\x00text", "a.txt", "text/plain", route.Unrouted, notText}, {"nul as the head's last byte", strings.Repeat("a", route.HeadLen-1) + "\x00", "a.txt", "", route.Unrouted, notText}, {"nul past the head", strings.Repeat("a", route.HeadLen) + "\x00", "a.txt", "", route.Text, byHead}, diff --git a/route/test/media_test.go b/route/test/media_test.go index e403f16..ac92cfa 100644 --- a/route/test/media_test.go +++ b/route/test/media_test.go @@ -165,6 +165,68 @@ func TestMediaTypeFallBack(t *testing.T) { } } +func TestMediaTypeTextByExtension(t *testing.T) { + const words = "the minutes of the meeting\n" + cases := []struct { + name, content, filename, want string + }{ + {"md", words, "notes.md", "text/markdown"}, + {"markdown", words, "notes.markdown", "text/markdown"}, + {"csv", words, "table.csv", "text/csv"}, + {"tsv", words, "table.tsv", "text/tab-separated-values"}, + {"html", words, "page.html", "text/html"}, + {"htm", words, "page.htm", "text/html"}, + {"xml", words, "feed.xml", "application/xml"}, + {"json", words, "data.json", "application/json"}, + {"yaml", words, "config.yaml", "application/yaml"}, + {"yml", words, "config.yml", "application/yaml"}, + {"css", words, "site.css", "text/css"}, + {"js", words, "app.js", "text/javascript"}, + {"mjs", words, "app.mjs", "text/javascript"}, + {"ics", words, "invite.ics", "text/calendar"}, + {"vcf", words, "card.vcf", "text/vcard"}, + {"uppercase, trailing dot", words, "NOTES.MD.", "text/markdown"}, + {"unknown extension", words, "notes.txt", "text/plain"}, + {"no extension", words, "README", "text/plain"}, + {"not text named .md", "a\x01b\n", "notes.md", "application/octet-stream"}, + {"not text named .json", "a\x01b\n", "data.json", "application/octet-stream"}, + {"utf-8 bom named .md", "\xef\xbb\xbf# notes\n", "notes.md", "text/plain"}, + {"shebang named .js", "#!/usr/bin/env node\nconsole.log(1)\n", "app.js", "text/plain"}, + } + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + if got := mediaType(t, c.content, c.filename, ""); got != c.want { + t.Errorf("MediaType = %q, want %q", got, c.want) + } + }) + } +} + +func TestMediaTypeHEIFBrands(t *testing.T) { + cases := []struct { + name, content, want string + }{ + {"heic", ftyp("heic", "mif1", "heic"), "image/heic"}, + {"heix", ftyp("heix", "mif1", "heix"), "image/heic"}, + {"heim", ftyp("heim", "mif1", "heim"), "image/heic"}, + {"heis", ftyp("heis", "mif1", "heis"), "image/heic"}, + {"hevc", ftyp("hevc", "msf1", "hevc"), "image/heic-sequence"}, + {"hevx", ftyp("hevx", "msf1", "hevx"), "image/heic-sequence"}, + {"heif", ftyp("heif", "mif1", "heif"), "image/heif"}, + {"mif1", ftyp("mif1", "mif1", "heic"), "image/heif"}, + {"miaf", ftyp("miaf", "miaf", "mif1"), "image/heif"}, + {"msf1", ftyp("msf1", "msf1", "hevc"), "image/heif-sequence"}, + {"iphone", ftyp("heic", "mif1", "MiHE", "MiPr", "miaf", "MiHB", "heic"), "image/heic"}, + } + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + if got := mediaType(t, c.content, "IMG_0001.HEIC", ""); got != c.want { + t.Errorf("MediaType = %q, want %q", got, c.want) + } + }) + } +} + type failingReader struct{ err error } func (f failingReader) ReadAt([]byte, int64) (int, error) { return 0, f.err } diff --git a/storage/CHANGELOG.md b/storage/CHANGELOG.md index 24cf061..5235d5d 100644 --- a/storage/CHANGELOG.md +++ b/storage/CHANGELOG.md @@ -14,7 +14,7 @@ each system tool under `tools/` keep their own. - The module's unit tests and integration suite sit in its `test/` directory as an external test package. No exported API changes. -- The module requires the docproc base module at v0.12.0, from v0.11.0, and passes its +- The module requires the docproc base module at v0.13.0, from v0.11.0, and passes its `dispatchtest.Content` suite; its [CHANGELOG](../CHANGELOG.md) states the base module's changes. ## [v0.1.9] - 2026-11-28 diff --git a/storage/go.mod b/storage/go.mod index 0527bb4..ee72954 100644 --- a/storage/go.mod +++ b/storage/go.mod @@ -3,7 +3,7 @@ module github.com/standards-lab/docproc/storage go 1.27.4 require ( - github.com/standards-lab/docproc v0.12.0 + github.com/standards-lab/docproc v0.13.0 github.com/standards-lab/go-storage v0.8.0 github.com/standards-lab/go-storage/s3 v0.3.0 ) diff --git a/storage/go.sum b/storage/go.sum index 883c785..5947d8e 100644 --- a/storage/go.sum +++ b/storage/go.sum @@ -36,8 +36,8 @@ github.com/aws/aws-sdk-go-v2/service/sts v1.52.0 h1:IaBLmJWiIEtKkjXMgMbYGZk7WKXb github.com/aws/aws-sdk-go-v2/service/sts v1.52.0/go.mod h1:MG6N8U9+lY4LqchrlHPqCOSr7hyxw70XU0Et2YR3eNA= github.com/aws/smithy-go v1.28.9 h1:zkZ058OpNX0iGTK24b+ZUEQSMc11CBA6oO7xtKZfzkY= github.com/aws/smithy-go v1.28.9/go.mod h1:YE2RhdIuDbA5E5bTdciG9KrW3+TiEONeUWCqxX9i1Fc= -github.com/standards-lab/docproc v0.12.0 h1:fKof6WCaze3tu+sCI5cx3OgsH5s86Aus65DBsQNA0Mg= -github.com/standards-lab/docproc v0.12.0/go.mod h1:ZA8JYQPooA6YA84b00Lpu5AiSL2VSj/u7QnenltqVBA= +github.com/standards-lab/docproc v0.13.0 h1:WRvUFHvMM1eFxBr9sFkgRH7Co2GkW2jgrEvdflPY6Io= +github.com/standards-lab/docproc v0.13.0/go.mod h1:ZA8JYQPooA6YA84b00Lpu5AiSL2VSj/u7QnenltqVBA= github.com/standards-lab/go-core v0.12.0 h1:sqrzK6QSppEYpwvVAyiWgzNuOsjfK0Kwe86GPuZpHsU= github.com/standards-lab/go-core v0.12.0/go.mod h1:Yl/R+gM9YWczsePBCKXdPGh3FSdZ/5i8SapLIh3BDHU= github.com/standards-lab/go-storage v0.8.0 h1:r2irs88NfmHRzBBOd9eTYdgS4rqrR6pm/aLaLiL/Gd4= diff --git a/tools/mail/CHANGELOG.md b/tools/mail/CHANGELOG.md index 45286fd..8b46995 100644 --- a/tools/mail/CHANGELOG.md +++ b/tools/mail/CHANGELOG.md @@ -19,6 +19,8 @@ output contract: a change to them is a change here. This changelog covers docpro - The crate graph locks byteorder 1.5.1 and sha2 0.11.3, patch releases, in place of 1.5.0 and 0.11.2, the latter yanked; the envelope crate's lock takes sha2 0.11.3 too. The output is unchanged. +- The crate graph locks pin-project-lite 0.2.19, a patch release, in place of 0.2.18. The output is + unchanged. ## [v0.2.0] - 2026-11-25 diff --git a/tools/mail/Cargo.lock b/tools/mail/Cargo.lock index f8b54d4..29df344 100644 --- a/tools/mail/Cargo.lock +++ b/tools/mail/Cargo.lock @@ -552,9 +552,9 @@ dependencies = [ [[package]] name = "pin-project-lite" -version = "0.2.18" +version = "0.2.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c938015719552d665fd0553b90d96cd7ff58e83e4c061ca99e6bb78865a72752" +checksum = "f120c8d5021b34abd2f2ffd7ee96687cbcd6c54a3e68d86b6169f4a5cd480ffe" [[package]] name = "potential_utf" diff --git a/tools/onenote/CHANGELOG.md b/tools/onenote/CHANGELOG.md index ecf693d..5c238ec 100644 --- a/tools/onenote/CHANGELOG.md +++ b/tools/onenote/CHANGELOG.md @@ -17,6 +17,8 @@ covers docproc-onenote only. - The crate graph locks byteorder 1.5.1 and sha2 0.11.3, patch releases, in place of 1.5.0 and 0.11.2, the latter yanked; the envelope crate's lock takes sha2 0.11.3 too. The output is unchanged. +- The crate graph locks pin-project-lite 0.2.19, a patch release, in place of 0.2.18. The output is + unchanged. ## [v0.1.2] - 2026-11-23 diff --git a/tools/onenote/Cargo.lock b/tools/onenote/Cargo.lock index 65a4630..4c5b7d2 100644 --- a/tools/onenote/Cargo.lock +++ b/tools/onenote/Cargo.lock @@ -515,9 +515,9 @@ dependencies = [ [[package]] name = "pin-project-lite" -version = "0.2.18" +version = "0.2.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c938015719552d665fd0553b90d96cd7ff58e83e4c061ca99e6bb78865a72752" +checksum = "f120c8d5021b34abd2f2ffd7ee96687cbcd6c54a3e68d86b6169f4a5cd480ffe" [[package]] name = "potential_utf"