omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / tasks / vigenere-cipher / vigenere-cipher.om
| 1 | note Rosetta Code task "Vigenère cipher": https://rosettacode.org/wiki/Vigen%C3%A8re_cipher |
| 2 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | om2 |
| 4 | text nl "\n" |
| 5 | text key "VIGENERECIPHER" |
| 6 | text msg "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!" |
| 7 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 8 | fn emit a view uchar * to void |
| 9 | o = ulong 0 |
| 10 | while lt o len a bound 4096 |
| 11 | k = write 1 a o sub len a o |
| 12 | if le k 0 |
| 13 | exit 1 |
| 14 | end |
| 15 | o = add o ulong k |
| 16 | end |
| 17 | end |
| 18 | note clean keeps the letters of a, upper-cased, in r and returns how many |
| 19 | fn clean a view uchar * r edit uchar * to ulong |
| 20 | n = ulong 0 |
| 21 | for i ulong len a |
| 22 | c = get a i |
| 23 | if and ge c 97 le c 122 |
| 24 | c = sub c 32 |
| 25 | end |
| 26 | if and ge c 65 le c 90 |
| 27 | put r n c |
| 28 | n = add n 1 |
| 29 | end |
| 30 | end |
| 31 | ret n |
| 32 | end |
| 33 | note vig adds (dir 0) or subtracts (dir 1) the key letters mod 26, the key repeating |
| 34 | fn vig a view uchar * r edit uchar * dir ulong to void |
| 35 | for i ulong len a |
| 36 | k = sub get key rem i len key 65 |
| 37 | c = sub get a i 65 |
| 38 | if eq dir 0 |
| 39 | c = rem add c k 26 |
| 40 | else |
| 41 | c = rem sub add c 26 k 26 |
| 42 | end |
| 43 | put r i add c 65 |
| 44 | end |
| 45 | end |
| 46 | fn main to int |
| 47 | t = new uchar 256 0 |
| 48 | e = new uchar 256 0 |
| 49 | d = new uchar 256 0 |
| 50 | n = clean msg t |
| 51 | emit part t 0 n |
| 52 | emit nl |
| 53 | vig part t 0 n part e 0 n 0 |
| 54 | emit part e 0 n |
| 55 | emit nl |
| 56 | vig part e 0 n part d 0 n 1 |
| 57 | emit part d 0 n |
| 58 | emit nl |
| 59 | ret 0 |
| 60 | end |