interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / tasks / bacon-cipher / bacon-cipher.om

85 lines · 1756 bytes Raw Blame History
1note Rosetta Code task "Bacon cipher": https://rosettacode.org/wiki/Bacon_cipher
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text secret "rosetta code"
6text cover "bacon's cipher is a method of steganography devised by francis bacon. a message is concealed in the presentation of text rather than its content."
7note emit writes every byte of a to stdout; a short write is retried, an error exits 1
8fn emit a view uchar * to void
9o = ulong 0
10while lt o len a bound 4096
11k = write 1 a o sub len a o
12if le k 0
13exit 1
14end
15o = add o ulong k
16end
17end
18note emitc writes one byte
19fn emitc c uchar to void
20b = new uchar 1 0
21put b 0 c
22emit b
23end
24note 26-letter variant: letter n (a = 0) is 5 bits, most significant first; a space is 26 (11010).
25note The bits ride on the cover text's letters: 0 lower case, 1 upper case; other bytes are copied,
26note letters after the last bit stay lower case. Decoding reads the case of the letters 5 at a time.
27fn code c uchar to ulong
28if eq c 32
29ret 26
30end
31ret ulong sub c 97
32end
33fn isl c uchar to bool
34ret and ge c 97 le c 122
35end
36fn enc o edit uchar * to void
37nb = mul len secret 5
38b = ulong 0
39for i ulong len cover
40c = get cover i
41if and isl c lt b nb
42v = code get secret div b 5
43if eq and shr v sub 4 rem b 5 1 1
44c = sub c 32
45end
46b = add b 1
47end
48put o i c
49end
50end
51fn dec e view uchar * n ulong to void
52v = ulong 0
53k = ulong 0
54m = ulong 0
55for i ulong len e
56c = get e i
57if and lt m n or isl c and ge c 65 le c 90
58v = shl v 1
59if le c 90
60v = or v 1
61end
62k = add k 1
63if eq k 5
64if eq v 26
65emitc 32
66else
67emitc add uchar v 97
68end
69v = 0
70k = 0
71m = add m 1
72end
73end
74end
75emit nl
76end
77fn main to int
78o = new uchar 256 0
79n = len cover
80enc part o 0 n
81emit part o 0 n
82emit nl
83dec part o 0 n len secret
84ret 0
85end