From e68227d2b34f09e63a0d7b234ac6af4513468728 Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Sun, 30 Jan 2022 10:24:49 -0800 Subject: [PATCH] yenc: adding yEnc format encoding/decoding --- extra/yenc/authors.txt | 1 + extra/yenc/summary.txt | 1 + extra/yenc/yenc-tests.factor | 9 ++++ extra/yenc/yenc.factor | 85 ++++++++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+) create mode 100644 extra/yenc/authors.txt create mode 100644 extra/yenc/summary.txt create mode 100644 extra/yenc/yenc-tests.factor create mode 100644 extra/yenc/yenc.factor diff --git a/extra/yenc/authors.txt b/extra/yenc/authors.txt new file mode 100644 index 0000000000..e091bb8164 --- /dev/null +++ b/extra/yenc/authors.txt @@ -0,0 +1 @@ +John Benediktsson diff --git a/extra/yenc/summary.txt b/extra/yenc/summary.txt new file mode 100644 index 0000000000..e9ce82a5e3 --- /dev/null +++ b/extra/yenc/summary.txt @@ -0,0 +1 @@ +Encoding and decoding of the yEnc format diff --git a/extra/yenc/yenc-tests.factor b/extra/yenc/yenc-tests.factor new file mode 100644 index 0000000000..982fe8c2e1 --- /dev/null +++ b/extra/yenc/yenc-tests.factor @@ -0,0 +1,9 @@ +USING: byte-arrays io.encodings.binary io.files kernel sequences +tools.test yenc ; + +{ t } [ 255 >byte-array dup yenc ydec = ] unit-test + +{ t } [ + "resource:LICENSE.txt" + [ yenc-file ydec-file 2nip ] [ binary file-contents ] bi = +] unit-test diff --git a/extra/yenc/yenc.factor b/extra/yenc/yenc.factor new file mode 100644 index 0000000000..ed85ea67a0 --- /dev/null +++ b/extra/yenc/yenc.factor @@ -0,0 +1,85 @@ +! Copyright (C) 2022 John Benediktsson +! See http://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 ; -- 2.34.1