-
Notifications
You must be signed in to change notification settings - Fork 1
/
baudot.c
83 lines (75 loc) · 1.78 KB
/
baudot.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "baudot.h"
#include "charmap.c"
Shift state = LTRS;
void reset_shift()
{
state = LTRS;
}
unsigned char *encode_ita2(char *s)
{
unsigned char *str = malloc((strlen(s)*2)+1);
unsigned char *ptr = str;
reset_shift();
while (*s) {
char c = *s;
if (islower(c))
c = toupper(c);
ITA2Char ch = ita2_ascii[(int)c];
if (state != ch.shift) {
*ptr++ = ch.shift;
state = ch.shift;
}
*ptr++ = ch.code;
s++;
}
*ptr = '\0';
return str;
}
char *decode_ita2(unsigned char *s)
{
char *str = malloc(strlen((char *)s)+1);
char *ptr = str;
reset_shift();
while (*s) {
if (*s == LTRS || *s == FIGS) {
state = *s;
} else {
if (state == LTRS) {
*ptr++ = ita2_ltrs2ascii[*s];
} else {
*ptr++ = ita2_figs2ascii[*s];
}
}
s++;
}
*ptr = '\0';
return str;
}
void test_roundtrip_ita2()
{
char *orig_msg = "HELLO, WORLD 123.";
unsigned char *enc = encode_ita2(orig_msg);
char *dec_msg = decode_ita2(enc);
if (strcmp(orig_msg, dec_msg) != 0) {
fprintf(stderr, "error decoding: want '%s', got '%s'\n", orig_msg, dec_msg);
}
}
void test_roundtrip_ita2_all()
{
char *orig_msg = "E3\nA- S'I8U7\rD\5R4J\aN,F!C:K(T5Z+L)W2H$Y6P0Q1O9B?G&M.X/V;";
unsigned char *enc = encode_ita2(orig_msg);
char *dec_msg = decode_ita2(enc);
if (strcmp(orig_msg, dec_msg) != 0) {
fprintf(stderr, "error decoding: want '%s', got '%s'\n", orig_msg, dec_msg);
}
}
int main(int argc, char **argv)
{
test_roundtrip_ita2();
test_roundtrip_ita2_all();
return 0;
}