1 USING: arrays kernel io io.binary sbufs splitting grouping
2 strings sequences namespaces math math.parser parser
3 hints math.bitfields.lib assocs ;
6 : w+ ( int int -- int ) + 32 bits ; inline
8 : (nth-int) ( string n -- int )
9 2 shift dup 4 + rot <slice> ; inline
11 : nth-int ( string n -- int ) (nth-int) le> ; inline
13 : nth-int-be ( string n -- int ) (nth-int) be> ; inline
15 : update ( num var -- ) [ w+ ] change ; inline
17 : calculate-pad-length ( length -- pad-length )
18 dup 56 < 55 119 ? swap - ;
20 : preprocess-plaintext ( string big-endian? -- padded-string )
21 #! pad 0x80 then 00 til 8 bytes left, then 64bit length in bits
24 dup length HEX: 3f bitand
25 calculate-pad-length 0 <string> %
26 length 3 shift 8 rot [ >be ] [ >le ] if %
27 ] "" make over push-all ;
32 : pad-last-block ( str big-endian? length -- str )
36 dup HEX: 3f bitand calculate-pad-length 0 <string> %
37 3 shift 8 rot [ >be ] [ >le ] if %
40 : update-old-new ( old new -- )
41 [ get >r get r> ] 2keep >r >r w+ dup r> set r> set ; inline
43 : slice3 ( n seq -- a b c ) >r dup 3 + r> <slice> first3 ;
45 : seq>2seq ( seq -- seq1 seq2 )
46 #! { abcdefgh } -> { aceg } { bdfh }
47 2 group flip dup empty? [ drop { } { } ] [ first2 ] if ;
49 : 2seq>seq ( seq1 seq2 -- seq )
50 #! { aceg } { bdfh } -> { abcdefgh }
51 [ zip concat ] keep like ;
53 : mod-nth ( n seq -- elt )
55 [ length mod ] [ nth ] bi ;