SQLite Forum

Asking for code review as well proposing to add new extension that implements rot47 function
Login

Asking for code review as well proposing to add new extension that implements rot47 function

(1) By AlexJ (CompuRoot) on 2021-12-06 21:19:42 [link] [source]

Good day,

As title says: Asking for code review as well proposing to add new extension that implements rot47 function to official SQLite repository.

To compare to rot13, new function can obfuscate the whole visible ASCII character set from ! and up to ~ in the same manner as rot13, - which is: "rot47(rot47(X)) should always equal X".

Source code for review is hosted on github, - here

Actually there no too much for review, since the only code added is:

static unsigned char rot47(unsigned char c){
  if(c>='!' && c<='O'){
    c += 47;
  }else if(c>='P' && c<='~'){
    c -= 47;
  }
  return c;
}

Everything else was taken from official rot13 extension without modification except function's names.

The code been used in production without glitches, but extra pair of someone's eyes won't be never excessive, so if one find a time to take a look on it, it would be great and appreciated.

(2) By Simon Slavin (slavin) on 2021-12-06 22:27:15 in reply to 1 [source]

It is customary for rot13() routines to operate on both upper case and lower case, even if the original spec was defined at a time lower case was not an issue. You may want to make sure that lower case letters are changed in the same way as their upper case equivalents.

(3) By AlexJ (CompuRoot) on 2021-12-06 23:31:25 in reply to 2 [link] [source]

It is customary for rot13() routines to operate on both upper case and lower case

AFAIK, it is not customary, otherwise math won't work.

The whole point of rot13, - is to be reversible cipher, which means, the same algorithm used for encoding and decoding. Also there no specification for rot13 as far as I know, but it is a simple math, if you take 26 characters (English alphabet) and divide it by 2, then you can rotate it 13 times to encode and rotate another 13 times to decode. That's why it called rot. (more about it with pictures that shows nicely the same concept is here).

The point of offered function is to expand Caesar cipher to the whole set of visible characters of ASCII, which is 94 characters and if you divide it by 2, you will get number 47, the same logic as with rot13, but in this case one can encode/decode punctuation, numbers and characters by using proposed extension.

Bellow is lookup table used to conversion back and forth:

!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
PQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNO

each character from top line associated with bottom one, that's exactly why it satisfying primary rule for such monoalphabetic substitution ciphers, where f(f(x)) = x

You may want to make sure that lower case letters are changed in the same way as their upper case equivalents.

I tested it and absolutely sure, that lookup table shown above correctly works and satisfying f(f(x)) = x and the proposed extension correctly encoding/decoding all ASCII, while bypassing all other characters unchanged.