#!/usr/bin/perl -w # Find an appropriate encoding offset. # # Most of our byte codes are in the printable range. For those that # are not, we would like to use a multi-byte encoding scheme of: # # # Where represents some magic offset value multiplied N times, # such that # # (offset * N + delta) % (5 * 37) == original byte # # If original byte is already printable then N would be just zero. use strict; # Return true if input byte is acceptable to be used in a string. Byte is # acceptable if it is printable, and it is not going to be a character # that will cause trouble when used as a preprocessor macro argument. sub is_acceptable($) { my ($byte) = @_; return $byte > 32 && $byte < 127 && index("\"\\,()", chr($byte)) < 0; } # Search through all possible offsets and print the ones that have a # proper encoding for each byte. for(my $offset = 1; $offset < 5 * 37; $offset++) { my $table = ""; my $errors = 0; for(my $i = 9; $i < 5 * 37; $i++) { # Skip over bytes that will not be used by our bytecode. if( $i == 37 || $i == 2 * 37 || $i == 3 * 37 || ($i > 4 * 37 && $i < 4 * 37 + 19) || $i > 4 * 37 + 19 ) { next; } # ($offset * $prefix + x) % (5 * 37) == $i # ($offset * $prefix + x) == $i + 5 * 37 * N # $byte = $i - $offset * $prefix + 5 * 37 * N my $prefix = 0; my $byte; for(; $prefix < 5; $prefix++) { $byte = $i - $offset * $prefix; while( $byte < 0 ) { $byte += 5 * 37; } last if is_acceptable($byte); } if( is_acceptable($byte) ) { $table .= ('o' x $prefix) . chr($byte); } else { $errors++; } } next if $errors > 0; printf 'offset = %3d, table = %3d: ', $offset, length($table); print "$table\n"; }