! Copyright (C) 2022 John Benediktsson ! See https://factorcode.org/license.txt for BSD license USING: accessors assocs checksums checksums.crc32 combinators endian formatting io.encodings.binary io.files io.files.info kernel make math math.functions math.order namespaces sequences splitting strings ; IN: yenc ! TODO: Support yparts SYMBOL: yenc-line-length yenc-line-length [ 128 ] initialize > ] [ crc32 checksum-bytes be> ] bi "\n=yend size=%d crc32=%08X" sprintf % ; PRIVATE> : yenc-file ( path -- yenc ) [ [ ybegin% ] [ binary file-contents yenc% ] [ yend% ] tri ] B{ } make ; : ydec ( yenc -- bytes ) [ f swap [ ydec, ] each drop ] B{ } make ; string " " split [ "=" split1 ] H{ } map>assoc ; : find-metadata ( lines type -- metadata i ) [ '[ _ head? ] find ] keep ?head drop parse-metadata swap ; PRIVATE> : ydec-file ( yenc -- ybegin yend bytes ) "\n" split { [ "=ybegin " find-metadata 1 + ] [ "=yend " find-metadata swapd ] [ ] } cleave [ f swap [ [ ydec, ] each ] each drop ] B{ } make ;