omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / drafts / rosettacode / vigenere-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 key "VIGENERECIPHER" |
| 8 | text msg "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!" |
| 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 clean keeps the letters of a, upper-cased, in r and returns how many |
| 21 | fn clean a view uchar * r edit uchar * to ulong |
| 22 | n = ulong 0 |
| 23 | for i ulong len a |
| 24 | c = get a i |
| 25 | if and ge c 97 le c 122 |
| 26 | c = sub c 32 |
| 27 | end |
| 28 | if and ge c 65 le c 90 |
| 29 | put r n c |
| 30 | n = add n 1 |
| 31 | end |
| 32 | end |
| 33 | ret n |
| 34 | end |
| 35 | note vig adds (dir 0) or subtracts (dir 1) the key letters mod 26, the key repeating |
| 36 | fn vig a view uchar * r edit uchar * dir ulong to void |
| 37 | for i ulong len a |
| 38 | k = sub get key rem i len key 65 |
| 39 | c = sub get a i 65 |
| 40 | if eq dir 0 |
| 41 | c = rem add c k 26 |
| 42 | else |
| 43 | c = rem sub add c 26 k 26 |
| 44 | end |
| 45 | put r i add c 65 |
| 46 | end |
| 47 | end |
| 48 | fn main to int |
| 49 | t = new uchar 256 0 |
| 50 | e = new uchar 256 0 |
| 51 | d = new uchar 256 0 |
| 52 | n = clean msg t |
| 53 | emit part t 0 n |
| 54 | emit nl |
| 55 | vig part t 0 n part e 0 n 0 |
| 56 | emit part e 0 n |
| 57 | emit nl |
| 58 | vig part e 0 n part d 0 n 1 |
| 59 | emit part d 0 n |
| 60 | emit nl |
| 61 | ret 0 |
| 62 | end |
| 63 | </syntaxhighlight> |
| 64 | {{out}} |
| 65 | <pre> |
| 66 | BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHATCATCH |
| 67 | WMCEEIKLGRPIFVMEUGXQPWQVIOIAVEYXUEKFKBTALVXTGAFXYEVKPAGY |
| 68 | BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHATCATCH |
| 69 | </pre> |