omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / drafts / rosettacode / bacon-cipher.wiki
| 1 | =={{header|Omegese}}== |
| 2 | {{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} |
| 3 | Omega has no imports, so the small output helpers (<code>emit</code> and friends) are part of the program. |
| 4 | <syntaxhighlight lang="text"> |
| 5 | om2 |
| 6 | text nl "\n" |
| 7 | text secret "rosetta code" |
| 8 | text 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." |
| 9 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 10 | fn emit a view uchar * to void |
| 11 | o = ulong 0 |
| 12 | while lt o len a bound 4096 |
| 13 | k = write 1 a o sub len a o |
| 14 | if le k 0 |
| 15 | exit 1 |
| 16 | end |
| 17 | o = add o ulong k |
| 18 | end |
| 19 | end |
| 20 | note emitc writes one byte |
| 21 | fn emitc c uchar to void |
| 22 | b = new uchar 1 0 |
| 23 | put b 0 c |
| 24 | emit b |
| 25 | end |
| 26 | note 26-letter variant: letter n (a = 0) is 5 bits, most significant first; a space is 26 (11010). |
| 27 | note The bits ride on the cover text's letters: 0 lower case, 1 upper case; other bytes are copied, |
| 28 | note letters after the last bit stay lower case. Decoding reads the case of the letters 5 at a time. |
| 29 | fn code c uchar to ulong |
| 30 | if eq c 32 |
| 31 | ret 26 |
| 32 | end |
| 33 | ret ulong sub c 97 |
| 34 | end |
| 35 | fn isl c uchar to bool |
| 36 | ret and ge c 97 le c 122 |
| 37 | end |
| 38 | fn enc o edit uchar * to void |
| 39 | nb = mul len secret 5 |
| 40 | b = ulong 0 |
| 41 | for i ulong len cover |
| 42 | c = get cover i |
| 43 | if and isl c lt b nb |
| 44 | v = code get secret div b 5 |
| 45 | if eq and shr v sub 4 rem b 5 1 1 |
| 46 | c = sub c 32 |
| 47 | end |
| 48 | b = add b 1 |
| 49 | end |
| 50 | put o i c |
| 51 | end |
| 52 | end |
| 53 | fn dec e view uchar * n ulong to void |
| 54 | v = ulong 0 |
| 55 | k = ulong 0 |
| 56 | m = ulong 0 |
| 57 | for i ulong len e |
| 58 | c = get e i |
| 59 | if and lt m n or isl c and ge c 65 le c 90 |
| 60 | v = shl v 1 |
| 61 | if le c 90 |
| 62 | v = or v 1 |
| 63 | end |
| 64 | k = add k 1 |
| 65 | if eq k 5 |
| 66 | if eq v 26 |
| 67 | emitc 32 |
| 68 | else |
| 69 | emitc add uchar v 97 |
| 70 | end |
| 71 | v = 0 |
| 72 | k = 0 |
| 73 | m = add m 1 |
| 74 | end |
| 75 | end |
| 76 | end |
| 77 | emit nl |
| 78 | end |
| 79 | fn main to int |
| 80 | o = new uchar 256 0 |
| 81 | n = len cover |
| 82 | enc part o 0 n |
| 83 | emit part o 0 n |
| 84 | emit nl |
| 85 | dec part o 0 n len secret |
| 86 | ret 0 |
| 87 | end |
| 88 | </syntaxhighlight> |
| 89 | {{out}} |
| 90 | <pre> |
| 91 | 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. |
| 92 | rosetta code |
| 93 | </pre> |