| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include "encoding.h" |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
struct encoder { |
| 11 |
unsigned char (*fn)(unsigned char); |
| 12 |
|
| 13 |
}; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
static unsigned char outermost_table[256]; |
| 18 |
const struct encoder *encoder_current; |
| 19 |
unsigned char *encoding_loaded_table = outermost_table; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
static unsigned char encoderfn_raw(unsigned char byte) |
| 27 |
{ |
| 28 |
return byte; |
| 29 |
} |
| 30 |
|
| 31 |
static unsigned char encoderfn_pet(unsigned char byte) |
| 32 |
{ |
| 33 |
if ((byte >= (unsigned char) 'A') && (byte <= (unsigned char) 'Z')) |
| 34 |
return byte | 0x80; |
| 35 |
if ((byte >= (unsigned char) 'a') && (byte <= (unsigned char) 'z')) |
| 36 |
return byte - 32; |
| 37 |
return byte; |
| 38 |
} |
| 39 |
|
| 40 |
static unsigned char encoderfn_scr(unsigned char byte) |
| 41 |
{ |
| 42 |
if ((byte >= (unsigned char) 'a') && (byte <= (unsigned char) 'z')) |
| 43 |
return byte - 96; |
| 44 |
if ((byte >= (unsigned char) '[') && (byte <= (unsigned char) '_')) |
| 45 |
return byte - 64; |
| 46 |
if (byte == '`') |
| 47 |
return 64; |
| 48 |
if (byte == '@') |
| 49 |
return 0; |
| 50 |
return byte; |
| 51 |
} |
| 52 |
|
| 53 |
static unsigned char encoderfn_file(unsigned char byte) |
| 54 |
{ |
| 55 |
return encoding_loaded_table[byte]; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
const struct encoder encoder_raw = { |
| 63 |
encoderfn_raw |
| 64 |
}; |
| 65 |
const struct encoder encoder_pet = { |
| 66 |
encoderfn_pet |
| 67 |
}; |
| 68 |
const struct encoder encoder_scr = { |
| 69 |
encoderfn_scr |
| 70 |
}; |
| 71 |
const struct encoder encoder_file = { |
| 72 |
encoderfn_file |
| 73 |
}; |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
unsigned char encoding_encode_char(unsigned char byte) |
| 81 |
{ |
| 82 |
return encoder_current->fn(byte); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
void encoding_passinit(void) |
| 87 |
{ |
| 88 |
encoder_current = &encoder_raw; |
| 89 |
} |