xref: /plan9/sys/src/cmd/vnc/latin1.c (revision 9a747e4fd48b9f4522c70c07e8f882a15030f964)
1 #include <u.h>
2 
3 /*
4  * The code makes two assumptions: strlen(ld) is 1 or 2; latintab[i].ld can be a
5  * prefix of latintab[j].ld only when j<i.
6  */
7 struct cvlist
8 {
9 	char	*ld;		/* must be seen before using this conversion */
10 	char	*si;		/* options for last input characters */
11 	Rune	*so;		/* the corresponding Rune for each si entry */
12 } latintab[] = {
13 #include "latin1.h"
14 	0,	0,		0
15 };
16 
17 /*
18  * Given 5 characters k[0]..k[4], find the rune or return -1 for failure.
19  */
20 long
unicode(Rune * k)21 unicode(Rune *k)
22 {
23 	long i, c;
24 
25 	k++;	/* skip 'X' */
26 	c = 0;
27 	for(i=0; i<4; i++,k++){
28 		c <<= 4;
29 		if('0'<=*k && *k<='9')
30 			c += *k-'0';
31 		else if('a'<=*k && *k<='f')
32 			c += 10 + *k-'a';
33 		else if('A'<=*k && *k<='F')
34 			c += 10 + *k-'A';
35 		else
36 			return -1;
37 	}
38 	return c;
39 }
40 
41 /*
42  * Given n characters k[0]..k[n-1], find the corresponding rune or return -1 for
43  * failure, or something < -1 if n is too small.  In the latter case, the result
44  * is minus the required n.
45  */
46 long
latin1(Rune * k,int n)47 latin1(Rune *k, int n)
48 {
49 	struct cvlist *l;
50 	int c;
51 	char* p;
52 
53 	if(k[0] == 'X')
54 		if(n>=5)
55 			return unicode(k);
56 		else
57 			return -5;
58 	for(l=latintab; l->ld!=0; l++)
59 		if(k[0] == l->ld[0]){
60 			if(n == 1)
61 				return -2;
62 			if(l->ld[1] == 0)
63 				c = k[1];
64 			else if(l->ld[1] != k[1])
65 				continue;
66 			else if(n == 2)
67 				return -3;
68 			else
69 				c = k[2];
70 			for(p=l->si; *p!=0; p++)
71 				if(*p == c)
72 					return l->so[p - l->si];
73 			return -1;
74 		}
75 	return -1;
76 }
77