ydec-file ( yenc -- ybegin yend bytes )


Vocabulary
yenc

Inputs
yenca byte sequence


Outputs
ybeginan assoc
yendan assoc
bytesa byte-array


Word description
Decodes the first yEnc envelope, ignoring text before and after it. Returns the header and trailer metadata with string values and the decoded bytes. Filenames may contain spaces and equals signs. Sizes and any single-part CRC32 are checked; corrupt envelopes throw invalid-yenc.

Notes
For a multipart envelope, decodes one part and checks its range, part number, size and required pcrc32. The ypart begin and end fields are included in the returned header as one-based, inclusive byte offsets. Separate parts are not assembled automatically. A whole-file crc32 is returned but can only be checked when the part covers the entire file. yenc-file produces single-part envelopes.

Definition


:: ydec-file ( yenc -- ybegin yend bytes )
yenc "\n" split
:> lines lines "=ybegin " find-metadata :> ( header start )
lines start 1 + tail
:> remaining remaining "=yend " find-metadata
:> ( trailer end ) remaining end head
:> body "part" header key? [
body empty? [ "Missing part header" invalid-yenc ] when
body first >string "=ypart " ?head
[ parse-metadata ]
[ "Missing part header" invalid-yenc ] if
:> part body rest B{ } join
ydec :> bytes header trailer part bytes check-part
part header assoc-union trailer bytes
] [
body B{ } join
ydec :> bytes header "size" metadata-number
bytes length check-size bytes trailer "crc32" check-crc
header trailer bytes
] if dup length trailer "size" metadata-number check-size ;