]> git.sur5r.net Git - openldap/blob - libraries/libldap/charset.c
VC++ Port: round 1
[openldap] / libraries / libldap / charset.c
1 /*
2  *  Copyright (c) 1995 Regents of the University of Michigan.
3  *  All rights reserved.
4  *
5  *  charset.c
6  */
7
8 #include "portable.h"
9
10 #ifdef STR_TRANSLATION
11
12 #ifndef lint 
13 static char copyright[] = "@(#) Copyright (c) 1995 Regents of the University of Michigan.\nAll rights reserved.\n";
14 #endif
15
16 #include <stdio.h>
17 #include <stdlib.h>
18
19 #include <ac/socket.h>
20 #include <ac/string.h>
21
22 #ifdef HAVE_SYS_PARAM_H
23 #include <sys/param.h>
24 #endif
25
26 #include "lber.h"
27 #include "ldap.h"
28 #include "ldap-int.h"
29
30
31 void
32 ldap_set_string_translators( LDAP *ld, BERTranslateProc encode_proc,
33         BERTranslateProc decode_proc )
34 {
35         ld->ld_lber_encode_translate_proc = encode_proc;
36         ld->ld_lber_decode_translate_proc = decode_proc;
37 }
38
39
40 void
41 ldap_enable_translation( LDAP *ld, LDAPMessage *entry, int enable )
42 {
43         char    *optionsp;
44
45         optionsp = ( entry == NULLMSG ) ? &ld->ld_lberoptions :
46             &entry->lm_ber->ber_options;
47                 
48         if ( enable ) {
49                 *optionsp |= LBER_TRANSLATE_STRINGS;
50         } else {
51                 *optionsp &= ~LBER_TRANSLATE_STRINGS;
52         }
53 }
54
55
56 int
57 ldap_translate_from_t61( LDAP *ld, char **bufp, unsigned long *lenp,
58     int free_input )
59 {
60         if ( ld->ld_lber_decode_translate_proc == NULL ) {
61                 return( LDAP_SUCCESS );
62         }
63             
64         return( (*ld->ld_lber_decode_translate_proc)( bufp, lenp, free_input ));
65 }
66
67
68 int
69 ldap_translate_to_t61( LDAP *ld, char **bufp, unsigned long *lenp,
70     int free_input )
71 {
72         if ( ld->ld_lber_encode_translate_proc == NULL ) {
73                 return( LDAP_SUCCESS );
74         }
75             
76         return( (*ld->ld_lber_encode_translate_proc)( bufp, lenp, free_input ));
77 }
78
79
80 /*
81  ** Character translation routine notes:
82  *
83  * On entry:  bufp points to a "string" to be converted (not necessarily
84  *  zero-terminated) and buflenp points to the length of the buffer.
85  *
86  * On exit:  bufp should point to a malloc'd result.  If free_input is
87  *  non-zero then the original bufp will be freed.  *buflenp should be
88  *  set to the new length.  Zero bytes in the input buffer must be left
89  *  as zero bytes.
90  *
91  * Return values: any ldap error code (LDAP_SUCCESS if all goes well).
92  */
93
94
95 #ifdef LDAP_CHARSET_8859
96
97 #if LDAP_CHARSET_8859 == 88591
98 #define ISO_8859 1
99 #elif LDAP_CHARSET_8859 == 88592
100 #define ISO_8859 2
101 #elif LDAP_CHARSET_8859 == 88593
102 #define ISO_8859 3
103 #elif LDAP_CHARSET_8859 == 88594
104 #define ISO_8859 4
105 #elif LDAP_CHARSET_8859 == 88595
106 #define ISO_8859 5
107 #elif LDAP_CHARSET_8859 == 88596
108 #define ISO_8859 6
109 #elif LDAP_CHARSET_8859 == 88597
110 #define ISO_8859 7
111 #elif LDAP_CHARSET_8859 == 88598
112 #define ISO_8859 8
113 #elif LDAP_CHARSET_8859 == 88599
114 #define ISO_8859 9
115 #elif LDAP_CHARSET_8859 == 885910
116 #define ISO_8859 10
117 #else
118 #define ISO_8859 0
119 #endif
120
121 /*
122  * the following ISO_8859 to/afrom T.61 character set translation code is
123  * based on the code found in Enrique Silvestre Mora's iso-t61.c, found
124  * as part of this package:
125  *   ftp://pereiii.uji.es/pub/uji-ftp/unix/ldap/iso-t61.translation.tar.Z
126  * Enrique is now (10/95) at this address: enrique.silvestre@uv.es
127  *
128  * changes made by mcs@umich.edu 12 October 1995:
129  *   Change calling conventions of iso8859_t61() and t61_iso8859() to
130  *      match libldap conventions; rename to ldap_8859_to_t61() and
131  *      ldap_t61_to_8859().
132  *   Change conversion routines to deal with non-zero terminated strings.
133  *   ANSI-ize functions and include prototypes.
134  */
135
136 /* iso-t61.c - ISO-T61 translation routines (version: 0.2.1, July-1994) */
137 /*
138  * Copyright (c) 1994 Enrique Silvestre Mora, Universitat Jaume I, Spain.
139  * All rights reserved.
140  *
141  * Redistribution and use in source and binary forms are permitted
142  * provided that this notice is preserved and that due credit is given
143  * to the Universitat Jaume I. The name of the University
144  * may not be used to endorse or promote products derived from this
145  * software without specific prior written permission. This software
146  * is provided ``as is'' without express or implied warranty.
147 */
148
149
150 #include <stdio.h>
151 #include <stdlib.h>
152 #include <ac/string.h>
153
154 /* Character set used: ISO 8859-1, ISO 8859-2, ISO 8859-3, ... */
155 /* #define  ISO_8859      1 */
156
157 #ifndef ISO_8859
158 #  define ISO_8859     0
159 #endif
160
161 typedef unsigned char  Byte;
162 typedef struct { Byte  a, b; } Couple;
163
164 static Byte *c_to_hh LDAP_P(( Byte *o, Byte c ));
165 static Byte *c_to_cc LDAP_P(( Byte *o, Couple *cc, Byte c ));
166 static int hh_to_c LDAP_P(( Byte *h ));
167 static Byte *cc_to_t61 LDAP_P(( Byte *o, Byte *s ));
168
169 /*
170    Character choosed as base in diacritics alone: NO-BREAK SPACE.
171    (The standard say it must be a blank space, 0x20.)
172 */
173 #define  ALONE  0xA0
174
175 static Couple diacritic[16] = {
176 #if (ISO_8859 == 1) || (ISO_8859 == 9)
177         {0,0},       {'`',0},     {0xb4,0},    {'^',0},
178         {'~',0},     {0xaf,0},    {'(',ALONE}, {'.',ALONE},
179         {0xa8,0},    {0,0},       {'0',ALONE}, {0xb8,0},
180         {0,0},       {'"',ALONE}, {';',ALONE}, {'<',ALONE},
181 #elif (ISO_8859 == 2)
182         {0,0},       {'`',0},     {0xb4,0},    {'^',0},
183         {'~',0},     {'-',ALONE}, {0xa2,0},    {0xff,0},
184         {0xa8,0},    {0,0},       {'0',ALONE}, {0xb8,0},
185         {0,0},       {0xbd,0},    {0xb2,0},    {0xb7,0}
186 #elif (ISO_8859 == 3)
187         {0,0},       {'`',0},     {0xb4,0},    {'^',0},
188         {'~',0},     {'-',ALONE}, {0xa2,0},    {0xff,0},
189         {0xa8,0},    {0,0},       {'0',ALONE}, {0xb8,0},
190         {0,0},       {'"',ALONE}, {';',ALONE}, {'<',ALONE}
191 #elif (ISO_8859 == 4)
192         {0,0},       {'`',0},     {0xb4,0},    {'^',0},
193         {'~',0},     {0xaf,0},    {'(',ALONE}, {0xff,0},
194         {0xa8,0},    {0,0},       {'0',ALONE}, {0xb8,0},
195         {0,0},       {'"',ALONE}, {0xb2,0},    {0xb7,0}
196 #else
197         {0,0},       {'`',0},     {'\'',ALONE}, {'^',0},
198         {'~',0},     {'-',ALONE}, {'(',ALONE},  {'.',ALONE},
199         {':',ALONE}, {0,0},       {'0',ALONE},  {',',ALONE},
200         {0,0},       {'"',ALONE}, {';',ALONE},  {'<',ALONE}
201 #endif
202 };
203
204 /*
205    --- T.61 (T.51) letters with diacritics: conversion to ISO 8859-n -----
206        A,   C,   D,   E,   G,   H,   I,   J,   K,
207        L,   N,   O,   R,   S,   T,   U,   W,   Y,   Z.
208    -----------------------------------------------------------------------
209 */
210 static int letter_w_diacritic[16][38] = {
211 #if (ISO_8859 == 1)
212         0,   0,   0,   0,   0,   0,   0,   0,   0,
213         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
214         0,   0,   0,   0,   0,   0,   0,   0,   0,
215         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
216         0xc0,0,   0,   0xc8,0,   0,   0xcc,0,   0,
217         0,   0,   0xd2,0,   0,   0,   0xd9,0,   0,   0,
218         0xe0,0,   0,   0xe8,0,   0,   0xec,0,   0,
219         0,   0,   0xf2,0,   0,   0,   0xf9,0,   0,   0,
220         0xc1,-1,  0,   0xc9,0,   0,   0xcd,0,   0,
221         -1,  -1,  0xd3,-1,  -1,  0,   0xda,0,   0xdd,-1,
222         0xe1,-1,  0,   0xe9,0,   0,   0xed,0,   0,
223         -1,  -1,  0xf3,-1,  -1,  0,   0xfa,0,   0xfd,-1,
224         0xc2,-1,  0,   0xca,-1,  -1,  0xce,-1,  0,
225         0,   0,   0xd4,0,   -1,  0,   0xdb,-1,  -1,  0,
226         0xe2,-1,  0,   0xea,-1,  -1,  0xee,-1,  0,
227         0,   0,   0xf4,0,   -1,  0,   0xfb,-1,  -1,  0,
228         0xc3,0,   0,   0,   0,   0,   -1,  0,   0,
229         0,   0xd1,0xd5,0,   0,   0,   -1,  0,   0,   0,
230         0xe3,0,   0,   0,   0,   0,   -1,  0,   0,
231         0,   0xf1,0xf5,0,   0,   0,   -1,  0,   0,   0,
232         -1,  0,   0,   -1,  0,   0,   -1,  0,   0,
233         0,   0,   -1,  0,   0,   0,   -1,  0,   0,   0,
234         -1,  0,   0,   -1,  0,   0,   -1,  0,   0,
235         0,   0,   -1,  0,   0,   0,   -1,  0,   0,   0,
236         -1,  0,   0,   0,   -1,  0,   0,   0,   0,
237         0,   0,   0,   0,   0,   0,   -1,  0,   0,   0,
238         -1,  0,   0,   0,   -1,  0,   0,   0,   0,
239         0,   0,   0,   0,   0,   0,   -1,  0,   0,   0,
240         0,   -1,  0,   -1,  -1,  0,   -1,  0,   0,
241         0,   0,   0,   0,   0,   0,   0,   0,   0,   -1,
242         0,   -1,  0,   -1,  -1,  0,   0,   0,   0,
243         0,   0,   0,   0,   0,   0,   0,   0,   0,   -1,
244         0xc4,0,   0,   0xcb,0,   0,   0xcf,0,   0,
245         0,   0,   0xd6,0,   0,   0,   0xdc,0,   -1,  0,
246         0xe4,0,   0,   0xeb,0,   0,   0xef,0,   0,
247         0,   0,   0xf6,0,   0,   0,   0xfc,0,   0xff,0,
248         0,   0,   0,   0,   0,   0,   0,   0,   0,
249         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
250         0,   0,   0,   0,   0,   0,   0,   0,   0,
251         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
252         0xc5,0,   0,   0,   0,   0,   0,   0,   0,
253         0,   0,   0,   0,   0,   0,   -1,  0,   0,   0,
254         0xe5,0,   0,   0,   0,   0,   0,   0,   0,
255         0,   0,   0,   0,   0,   0,   -1,  0,   0,   0,
256         0,   0xc7,0,   0,   -1,  0,   0,   0,   -1,
257         -1,  -1,  0,   -1,  -1,  -1,  0,   0,   0,   0,
258         0,   0xe7,0,   0,   -1,  0,   0,   0,   -1,
259         -1,  -1,  0,   -1,  -1,  -1,  0,   0,   0,   0,
260         0,   0,   0,   0,   0,   0,   0,   0,   0,
261         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
262         0,   0,   0,   0,   0,   0,   0,   0,   0,
263         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
264         0,   0,   0,   0,   0,   0,   0,   0,   0,
265         0,   0,   -1,  0,   0,   0,   -1,  0,   0,   0,
266         0,   0,   0,   0,   0,   0,   0,   0,   0,
267         0,   0,   -1,  0,   0,   0,   -1,  0,   0,   0,
268         -1,  0,   0,   -1,  0,   0,   -1,  0,   0,
269         0,   0,   0,   0,   0,   0,   -1,  0,   0,   0,
270         -1,  0,   0,   -1,  0,   0,   -1,  0,   0,
271         0,   0,   0,   0,   0,   0,   -1,  0,   0,   0,
272         0,   -1,  -1,  -1,  0,   0,   0,   0,   0,
273         -1,  -1,  0,   -1,  -1,  -1,  0,   0,   0,   -1,
274         0,   -1,  -1,  -1,  0,   0,   0,   0,   0,
275         -1,  -1,  0,   -1,  -1,  -1,  0,   0,   0,   -1
276 #elif (ISO_8859 == 2)
277         0,   0,   0,   0,   0,   0,   0,   0,   0,
278         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
279         0,   0,   0,   0,   0,   0,   0,   0,   0,
280         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
281         -1,  0,   0,   -1,  0,   0,   -1,  0,   0,
282         0,   0,   -1,  0,   0,   0,   -1,  0,   0,   0,
283         -1,  0,   0,   -1,  0,   0,   -1,  0,   0,
284         0,   0,   -1,  0,   0,   0,   -1,  0,   0,   0,
285         0xc1,0xc6,0,   0xc9,0,   0,   0xcd,0,   0,
286         0xc5,0xd1,0xd3,0xc0,0xa6,0,   0xda,0,   0xdd,0xac,
287         0xe1,0xe6,0,   0xe9,0,   0,   0xed,0,   0,
288         0xe5,0xf1,0xf3,0xe0,0xb6,0,   0xfa,0,   0xfd,0xbc,
289         0xc2,-1,  0,   -1,  -1,  -1,  0xce,-1,  0,
290         0,   0,   0xd4,0,   -1,  0,   -1,  -1,  -1,  0,
291         0xe2,-1,  0,   -1,  -1,  -1,  0xee,-1,  0,
292         0,   0,   0xf4,0,   -1,  0,   -1,  -1,  -1,  0,
293         -1,  0,   0,   0,   0,   0,   -1,  0,   0,
294         0,   -1,  -1,  0,   0,   0,   -1,  0,   0,   0,
295         -1,  0,   0,   0,   0,   0,   -1,  0,   0,
296         0,   -1,  -1,  0,   0,   0,   -1,  0,   0,   0,
297         -1,  0,   0,   -1,  0,   0,   -1,  0,   0,
298         0,   0,   -1,  0,   0,   0,   -1,  0,   0,   0,
299         -1,  0,   0,   -1,  0,   0,   -1,  0,   0,
300         0,   0,   -1,  0,   0,   0,   -1,  0,   0,   0,
301         0xc3,0,   0,   0,   -1,  0,   0,   0,   0,
302         0,   0,   0,   0,   0,   0,   -1,  0,   0,   0,
303         0xe3,0,   0,   0,   -1,  0,   0,   0,   0,
304         0,   0,   0,   0,   0,   0,   -1,  0,   0,   0,
305         0,   -1,  0,   -1,  -1,  0,   -1,  0,   0,
306         0,   0,   0,   0,   0,   0,   0,   0,   0,   0xaf,
307         0,   -1,  0,   -1,  -1,  0,   0,   0,   0,
308         0,   0,   0,   0,   0,   0,   0,   0,   0,   0xbf,
309         0xc4,0,   0,   0xcb,0,   0,   -1,  0,   0,
310         0,   0,   0xd6,0,   0,   0,   0xdc,0,   -1,  0,
311         0xe4,0,   0,   0xeb,0,   0,   -1,  0,   0,
312         0,   0,   0xf6,0,   0,   0,   0xfc,0,   -1,  0,
313         0,   0,   0,   0,   0,   0,   0,   0,   0,
314         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
315         0,   0,   0,   0,   0,   0,   0,   0,   0,
316         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
317         -1,  0,   0,   0,   0,   0,   0,   0,   0,
318         0,   0,   0,   0,   0,   0,   0xd9,0,   0,   0,
319         -1,  0,   0,   0,   0,   0,   0,   0,   0,
320         0,   0,   0,   0,   0,   0,   0xf9,0,   0,   0,
321         0,   0xc7,0,   0,   -1,  0,   0,   0,   -1,
322         -1,  -1,  0,   -1,  0xaa,0xde,0,   0,   0,   0,
323         0,   0xe7,0,   0,   -1,  0,   0,   0,   -1,
324         -1,  -1,  0,   -1,  0xba,0xfe,0,   0,   0,   0,
325         0,   0,   0,   0,   0,   0,   0,   0,   0,
326         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
327         0,   0,   0,   0,   0,   0,   0,   0,   0,
328         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
329         0,   0,   0,   0,   0,   0,   0,   0,   0,
330         0,   0,   0xd5,0,   0,   0,   0xdb,0,   0,   0,
331         0,   0,   0,   0,   0,   0,   0,   0,   0,
332         0,   0,   0xf5,0,   0,   0,   0xfb,0,   0,   0,
333         0xa1,0,   0,   0xca,0,   0,   -1,  0,   0,
334         0,   0,   0,   0,   0,   0,   -1,  0,   0,   0,
335         0xb1,0,   0,   0xea,0,   0,   -1,  0,   0,
336         0,   0,   0,   0,   0,   0,   -1,  0,   0,   0,
337         0,   0xc8,0xcf,0xcc,0,   0,   0,   0,   0,
338         0xa5,0xd2,0,   0xd8,0xa9,0xab,0,   0,   0,   0xae,
339         0,   0xe8,0xef,0xec,0,   0,   0,   0,   0,
340         0xb5,0xf2,0,   0xf8,0xb9,0xbb,0,   0,   0,   0xbe
341 #elif (ISO_8859 == 3)
342         0,   0,   0,   0,   0,   0,   0,   0,   0,
343         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
344         0,   0,   0,   0,   0,   0,   0,   0,   0,
345         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
346         0xc0,0,   0,   0xc8,0,   0,   0xcc,0,   0,
347         0,   0,   0xd2,0,   0,   0,   0xd9,0,   0,   0,
348         0xe0,0,   0,   0xe8,0,   0,   0xec,0,   0,
349         0,   0,   0xf2,0,   0,   0,   0xf9,0,   0,   0,
350         0xc1,-1,  0,   0xc9,0,   0,   0xcd,0,   0,
351         -1,  -1,  0xd3,-1,  -1,  0,   0xda,0,   -1,  -1,
352         0xe1,-1,  0,   0xe9,0,   0,   0xed,0,   0,
353         -1,  -1,  0xf3,-1,  -1,  0,   0xfa,0,   -1,  -1,
354         0xc2,0xc6,0,   0xca,0xd8,0xa6,0xce,0xac,0,
355         0,   0,   0xd4,0,   0xde,0,   0xdb,-1,  -1,  0,
356         0xe2,0xe6,0,   0xea,0xf8,0xb6,0xee,0xbc,0,
357         0,   0,   0xf4,0,   0xfe,0,   0xfb,-1,  -1,  0,
358         -1,  0,   0,   0,   0,   0,   -1,  0,   0,
359         0,   0xd1,-1,  0,   0,   0,   -1,  0,   0,   0,
360         -1,  0,   0,   0,   0,   0,   -1,  0,   0,
361         0,   0xf1,-1,  0,   0,   0,   -1,  0,   0,   0,
362         -1,  0,   0,   -1,  0,   0,   -1,  0,   0,
363         0,   0,   -1,  0,   0,   0,   -1,  0,   0,   0,
364         -1,  0,   0,   -1,  0,   0,   -1,  0,   0,
365         0,   0,   -1,  0,   0,   0,   -1,  0,   0,   0,
366         -1,  0,   0,   0,   0xab,0,   0,   0,   0,
367         0,   0,   0,   0,   0,   0,   0xdd,0,   0,   0,
368         -1,  0,   0,   0,   0xbb,0,   0,   0,   0,
369         0,   0,   0,   0,   0,   0,   0xfd,0,   0,   0,
370         0,   0xc5,0,   -1,  0xd5,0,   0xa9,0,   0,
371         0,   0,   0,   0,   0,   0,   0,   0,   0,   0xaf,
372         0,   0xe5,0,   -1,  0xf5,0,   0,   0,   0,
373         0,   0,   0,   0,   0,   0,   0,   0,   0,   0xbf,
374         0xc4,0,   0,   0xcb,0,   0,   0xcf,0,   0,
375         0,   0,   0xd6,0,   0,   0,   0xdc,0,   -1,  0,
376         0xe4,0,   0,   0xeb,0,   0,   0xef,0,   0,
377         0,   0,   0xf6,0,   0,   0,   0xfc,0,   -1,  0,
378         0,   0,   0,   0,   0,   0,   0,   0,   0,
379         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
380         0,   0,   0,   0,   0,   0,   0,   0,   0,
381         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
382         -1,  0,   0,   0,   0,   0,   0,   0,   0,
383         0,   0,   0,   0,   0,   0,   -1,  0,   0,   0,
384         -1,  0,   0,   0,   0,   0,   0,   0,   0,
385         0,   0,   0,   0,   0,   0,   -1,  0,   0,   0,
386         0,   0xc7,0,   0,   -1,  0,   0,   0,   -1,
387         -1,  -1,  0,   -1,  0xaa,-1,  0,   0,   0,   0,
388         0,   0xe7,0,   0,   -1,  0,   0,   0,   -1,
389         -1,  -1,  0,   -1,  0xba,-1,  0,   0,   0,   0,
390         0,   0,   0,   0,   0,   0,   0,   0,   0,
391         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
392         0,   0,   0,   0,   0,   0,   0,   0,   0,
393         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
394         0,   0,   0,   0,   0,   0,   0,   0,   0,
395         0,   0,   -1,  0,   0,   0,   -1,  0,   0,   0,
396         0,   0,   0,   0,   0,   0,   0,   0,   0,
397         0,   0,   -1,  0,   0,   0,   -1,  0,   0,   0,
398         -1,  0,   0,   -1,  0,   0,   -1,  0,   0,
399         0,   0,   0,   0,   0,   0,   -1,  0,   0,   0,
400         -1,  0,   0,   -1,  0,   0,   -1,  0,   0,
401         0,   0,   0,   0,   0,   0,   -1,  0,   0,   0,
402         0,   -1,  -1,  -1,  0,   0,   0,   0,   0,
403         -1,  -1,  0,   -1,  -1,  -1,  0,   0,   0,   -1,
404         0,   -1,  -1,  -1,  0,   0,   0,   0,   0,
405         -1,  -1,  0,   -1,  -1,  -1,  0,   0,   0,   -1
406 #elif (ISO_8859 == 4)
407         0,   0,   0,   0,   0,   0,   0,   0,   0,
408         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
409         0,   0,   0,   0,   0,   0,   0,   0,   0,
410         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
411         -1,  0,   0,   -1,  0,   0,   -1,  0,   0,
412         0,   0,   -1,  0,   0,   0,   -1,  0,   0,   0,
413         -1,  0,   0,   -1,  0,   0,   -1,  0,   0,
414         0,   0,   -1,  0,   0,   0,   -1,  0,   0,   0,
415         0xc1,-1,  0,   0xc9,0,   0,   0xcd,0,   0,
416         -1,  -1,  -1,  -1,  -1,  0,   0xda,0,   -1,  -1,
417         0xe1,-1,  0,   0xe9,0,   0,   0xed,0,   0,
418         -1,  -1,  -1,  -1,  -1,  0,   0xfa,0,   -1,  -1,
419         0xc2,-1,  0,   -1,  -1,  -1,  0xce,-1,  0,
420         0,   0,   0xd4,0,   -1,  0,   0xdb,-1,  -1,  0,
421         0xe2,-1,  0,   -1,  -1,  -1,  0xee,-1,  0,
422         0,   0,   0xf4,0,   -1,  0,   0xfb,-1,  -1,  0,
423         0xc3,0,   0,   0,   0,   0,   0xa5,0,   0,
424         0,   -1,  0xd5,0,   0,   0,   0xdd,0,   0,   0,
425         0xe3,0,   0,   0,   0,   0,   0xb5,0,   0,
426         0,   -1,  0xf5,0,   0,   0,   0xfd,0,   0,   0,
427         0xc0,0,   0,   0xaa,0,   0,   0xcf,0,   0,
428         0,   0,   0xd2,0,   0,   0,   0xde,0,   0,   0,
429         0xe0,0,   0,   0xba,0,   0,   0xef,0,   0,
430         0,   0,   0xf2,0,   0,   0,   0xfe,0,   0,   0,
431         -1,  0,   0,   0,   -1,  0,   0,   0,   0,
432         0,   0,   0,   0,   0,   0,   -1,  0,   0,   0,
433         -1,  0,   0,   0,   -1,  0,   0,   0,   0,
434         0,   0,   0,   0,   0,   0,   -1,  0,   0,   0,
435         0,   -1,  0,   0xcc,-1,  0,   -1,  0,   0,
436         0,   0,   0,   0,   0,   0,   0,   0,   0,   -1,
437         0,   -1,  0,   0xec,-1,  0,   0,   0,   0,
438         0,   0,   0,   0,   0,   0,   0,   0,   0,   -1,
439         0xc4,0,   0,   0xcb,0,   0,   -1,  0,   0,
440         0,   0,   0xd6,0,   0,   0,   0xdc,0,   -1,  0,
441         0xe4,0,   0,   0xeb,0,   0,   -1,  0,   0,
442         0,   0,   0xf6,0,   0,   0,   0xfc,0,   -1,  0,
443         0,   0,   0,   0,   0,   0,   0,   0,   0,
444         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
445         0,   0,   0,   0,   0,   0,   0,   0,   0,
446         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
447         0xc5,0,   0,   0,   0,   0,   0,   0,   0,
448         0,   0,   0,   0,   0,   0,   -1,  0,   0,   0,
449         0xe5,0,   0,   0,   0,   0,   0,   0,   0,
450         0,   0,   0,   0,   0,   0,   -1,  0,   0,   0,
451         0,   -1,  0,   0,   0xab,0,   0,   0,   0xd3,
452         0xa6,0xd1,0,   0xa3,-1,  -1,  0,   0,   0,   0,
453         0,   -1,  0,   0,   0xbb,0,   0,   0,   0xf3,
454         0xb6,0xf1,0,   0xb3,-1,  -1,  0,   0,   0,   0,
455         0,   0,   0,   0,   0,   0,   0,   0,   0,
456         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
457         0,   0,   0,   0,   0,   0,   0,   0,   0,
458         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
459         0,   0,   0,   0,   0,   0,   0,   0,   0,
460         0,   0,   -1,  0,   0,   0,   -1,  0,   0,   0,
461         0,   0,   0,   0,   0,   0,   0,   0,   0,
462         0,   0,   -1,  0,   0,   0,   -1,  0,   0,   0,
463         0xa1,0,   0,   0xca,0,   0,   0xc7,0,   0,
464         0,   0,   0,   0,   0,   0,   0xd9,0,   0,   0,
465         0xb1,0,   0,   0xea,0,   0,   0xe7,0,   0,
466         0,   0,   0,   0,   0,   0,   0xf9,0,   0,   0,
467         0,   0xc8,-1,  -1,  0,   0,   0,   0,   0,
468         -1,  -1,  0,   -1,  0xa9,-1,  0,   0,   0,   0xae,
469         0,   0xe8,-1,  -1,  0,   0,   0,   0,   0,
470         -1,  -1,  0,   -1,  0xb9,-1,  0,   0,   0,   0xbe
471 #elif (ISO_8859 == 9)
472         0,   0,   0,   0,   0,   0,   0,   0,   0,
473         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
474         0,   0,   0,   0,   0,   0,   0,   0,   0,
475         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
476         0xc0,0,   0,   0xc8,0,   0,   0xcc,0,   0,
477         0,   0,   0xd2,0,   0,   0,   0xd9,0,   0,   0,
478         0xe0,0,   0,   0xe8,0,   0,   -1,  0,   0,
479         0,   0,   0xf2,0,   0,   0,   0xf9,0,   0,   0,
480         0xc1,-1,  0,   0xc9,0,   0,   0xcd,0,   0,
481         -1,  -1,  0xd3,-1,  -1,  0,   0xda,0,   -1,  -1,
482         0xe1,-1,  0,   0xe9,0,   0,   0xed,0,   0,
483         -1,  -1,  0xf3,-1,  -1,  0,   0xfa,0,   -1,  -1,
484         0xc2,-1,  0,   0xca,-1,  -1,  0xce,-1,  0,
485         0,   0,   0xd4,0,   -1,  0,   0xdb,-1,  -1,  0,
486         0xe2,-1,  0,   -1,  -1,  -1,  0xee,-1,  0,
487         0,   0,   0xf4,0,   -1,  0,   0xfb,-1,  -1,  0,
488         0xc3,0,   0,   0,   0,   0,   -1,  0,   0,
489         0,   0xd1,0xd5,0,   0,   0,   -1,  0,   0,   0,
490         0xe3,0,   0,   0,   0,   0,   -1,  0,   0,
491         0,   0xf1,0xf5,0,   0,   0,   -1,  0,   0,   0,
492         -1,  0,   0,   -1,  0,   0,   -1,  0,   0,
493         0,   0,   -1,  0,   0,   0,   -1,  0,   0,   0,
494         -1,  0,   0,   -1,  0,   0,   0xef,0,   0,
495         0,   0,   -1,  0,   0,   0,   -1,  0,   0,   0,
496         -1,  0,   0,   0,   0xd0,0,   0,   0,   0,
497         0,   0,   0,   0,   0,   0,   -1,  0,   0,   0,
498         -1,  0,   0,   0,   0xf0,0,   0,   0,   0,
499         0,   0,   0,   0,   0,   0,   -1,  0,   0,   0,
500         0,   -1,  0,   -1,  -1,  0,   0xdd,0,   0,
501         0,   0,   0,   0,   0,   0,   0,   0,   0,   -1,
502         0,   -1,  0,   0xec,-1,  0,   0,   0,   0,
503         0,   0,   0,   0,   0,   0,   0,   0,   0,   -1,
504         0xc4,0,   0,   0xcb,0,   0,   0xcf,0,   0,
505         0,   0,   0xd6,0,   0,   0,   0xdc,0,   -1,  0,
506         0xe4,0,   0,   0xeb,0,   0,   -1,  0,   0,
507         0,   0,   0xf6,0,   0,   0,   0xfc,0,   0xff,0,
508         0,   0,   0,   0,   0,   0,   0,   0,   0,
509         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
510         0,   0,   0,   0,   0,   0,   0,   0,   0,
511         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
512         0xc5,0,   0,   0,   0,   0,   0,   0,   0,
513         0,   0,   0,   0,   0,   0,   -1,  0,   0,   0,
514         0xe5,0,   0,   0,   0,   0,   0,   0,   0,
515         0,   0,   0,   0,   0,   0,   -1,  0,   0,   0,
516         0,   0xc7,0,   0,   -1,  0,   0,   0,   -1,
517         -1,  -1,  0,   -1,  0xde,-1,  0,   0,   0,   0,
518         0,   0xe7,0,   0,   -1,  0,   0,   0,   -1,
519         -1,  -1,  0,   -1,  0xfe,-1,  0,   0,   0,   0,
520         0,   0,   0,   0,   0,   0,   0,   0,   0,
521         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
522         0,   0,   0,   0,   0,   0,   0,   0,   0,
523         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
524         0,   0,   0,   0,   0,   0,   0,   0,   0,
525         0,   0,   -1,  0,   0,   0,   -1,  0,   0,   0,
526         0,   0,   0,   0,   0,   0,   0,   0,   0,
527         0,   0,   -1,  0,   0,   0,   -1,  0,   0,   0,
528         -1,  0,   0,   -1,  0,   0,   -1,  0,   0,
529         0,   0,   0,   0,   0,   0,   -1,  0,   0,   0,
530         -1,  0,   0,   0xea,0,   0,   -1,  0,   0,
531         0,   0,   0,   0,   0,   0,   -1,  0,   0,   0,
532         0,   -1,  -1,  -1,  0,   0,   0,   0,   0,
533         -1,  -1,  0,   -1,  -1,  -1,  0,   0,   0,   -1,
534         0,   -1,  -1,  -1,  0,   0,   0,   0,   0,
535         -1,  -1,  0,   -1,  -1,  -1,  0,   0,   0,   -1
536 #elif (ISO_8859 == 10)
537         0,   0,   0,   0,   0,   0,   0,   0,   0,
538         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
539         0,   0,   0,   0,   0,   0,   0,   0,   0,
540         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
541         -1,  0,   0,   -1,  0,   0,   -1,  0,   0,
542         0,   0,   -1,  0,   0,   0,   -1,  0,   0,   0,
543         -1,  0,   0,   -1,  0,   0,   -1,  0,   0,
544         0,   0,   -1,  0,   0,   0,   -1,  0,   0,   0,
545         0xc1,-1,  0,   0xc9,0,   0,   0xcd,0,   0,
546         -1,  -1,  0xd3,-1,  -1,  0,   0xda,0,   0xdd,-1,
547         0xe1,-1,  0,   0xe9,0,   0,   0xed,0,   0,
548         -1,  -1,  0xf3,-1,  -1,  0,   0xfa,0,   0xfd,-1,
549         0xc2,-1,  0,   -1,  -1,  -1,  0xce,-1,  0,
550         0,   0,   0xd4,0,   -1,  0,   0xdb,-1,  -1,  0,
551         0xe2,-1,  0,   -1,  -1,  -1,  0xee,-1,  0,
552         0,   0,   0xf4,0,   -1,  0,   0xfb,-1,  -1,  0,
553         0xc3,0,   0,   0,   0,   0,   0xa5,0,   0,
554         0,   -1,  0xd5,0,   0,   0,   0xd7,0,   0,   0,
555         0xe3,0,   0,   0,   0,   0,   0xb5,0,   0,
556         0,   -1,  0xf5,0,   0,   0,   0xf7,0,   0,   0,
557         0xc0,0,   0,   0xa2,0,   0,   0xa4,0,   0,
558         0,   0,   0xd2,0,   0,   0,   0xae,0,   0,   0,
559         0xe0,0,   0,   0xb2,0,   0,   0xb4,0,   0,
560         0,   0,   0xf2,0,   0,   0,   0xbe,0,   0,   0,
561         -1,  0,   0,   0,   -1,  0,   0,   0,   0,
562         0,   0,   0,   0,   0,   0,   -1,  0,   0,   0,
563         -1,  0,   0,   0,   -1,  0,   0,   0,   0,
564         0,   0,   0,   0,   0,   0,   -1,  0,   0,   0,
565         0,   -1,  0,   0xcc,-1,  0,   -1,  0,   0,
566         0,   0,   0,   0,   0,   0,   0,   0,   0,   -1,
567         0,   -1,  0,   0xec,-1,  0,   0,   0,   0,
568         0,   0,   0,   0,   0,   0,   0,   0,   0,   -1,
569         0xc4,0,   0,   0xcb,0,   0,   0xcf,0,   0,
570         0,   0,   0xd6,0,   0,   0,   0xdc,0,   -1,  0,
571         0xe4,0,   0,   0xeb,0,   0,   0xef,0,   0,
572         0,   0,   0xf6,0,   0,   0,   0xfc,0,   -1,  0,
573         0,   0,   0,   0,   0,   0,   0,   0,   0,
574         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
575         0,   0,   0,   0,   0,   0,   0,   0,   0,
576         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
577         0xc5,0,   0,   0,   0,   0,   0,   0,   0,
578         0,   0,   0,   0,   0,   0,   -1,  0,   0,   0,
579         0xe5,0,   0,   0,   0,   0,   0,   0,   0,
580         0,   0,   0,   0,   0,   0,   -1,  0,   0,   0,
581         0,   -1,  0,   0,   0xa3,0,   0,   0,   0xa6,
582         0xa8,0xd1,0,   -1,  -1,  -1,  0,   0,   0,   0,
583         0,   -1,  0,   0,   0xb3,0,   0,   0,   0xb6,
584         0xb8,0xf1,0,   -1,  -1,  -1,  0,   0,   0,   0,
585         0,   0,   0,   0,   0,   0,   0,   0,   0,
586         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
587         0,   0,   0,   0,   0,   0,   0,   0,   0,
588         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
589         0,   0,   0,   0,   0,   0,   0,   0,   0,
590         0,   0,   -1,  0,   0,   0,   -1,  0,   0,   0,
591         0,   0,   0,   0,   0,   0,   0,   0,   0,
592         0,   0,   -1,  0,   0,   0,   -1,  0,   0,   0,
593         0xa1,0,   0,   0xca,0,   0,   0xc7,0,   0,
594         0,   0,   0,   0,   0,   0,   0xd9,0,   0,   0,
595         0xb1,0,   0,   0xea,0,   0,   0xe7,0,   0,
596         0,   0,   0,   0,   0,   0,   0xf9,0,   0,   0,
597         0,   0xc8,-1,  -1,  0,   0,   0,   0,   0,
598         -1,  -1,  0,   -1,  0xaa,-1,  0,   0,   0,   0xac,
599         0,   0xe8,-1,  -1,  0,   0,   0,   0,   0,
600         -1,  -1,  0,   -1,  0xba,-1,  0,   0,   0,   0xbc
601 #else
602         0,   0,   0,   0,   0,   0,   0,   0,   0,
603         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
604         0,   0,   0,   0,   0,   0,   0,   0,   0,
605         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
606         -1,  0,   0,   -1,  0,   0,   -1,  0,   0,
607         0,   0,   -1,  0,   0,   0,   -1,  0,   0,   0,
608         -1,  0,   0,   -1,  0,   0,   -1,  0,   0,
609         0,   0,   -1,  0,   0,   0,   -1,  0,   0,   0,
610         -1,  -1,  0,   -1,  0,   0,   -1,  0,   0,
611         -1,  -1,  -1,  -1,  -1,  0,   -1,  0,   -1,  -1,
612         -1,  -1,  0,   -1,  0,   0,   -1,  0,   0,
613         -1,  -1,  -1,  -1,  -1,  0,   -1,  0,   -1,  -1,
614         -1,  -1,  0,   -1,  -1,  -1,  -1,  -1,  0,
615         0,   0,   -1,  0,   -1,  0,   -1,  -1,  -1,  0,
616         -1,  -1,  0,   -1,  -1,  -1,  -1,  -1,  0,
617         0,   0,   -1,  0,   -1,  0,   -1,  -1,  -1,  0,
618         -1,  0,   0,   0,   0,   0,   -1,  0,   0,
619         0,   -1,  -1,  0,   0,   0,   -1,  0,   0,   0,
620         -1,  0,   0,   0,   0,   0,   -1,  0,   0,
621         0,   -1,  -1,  0,   0,   0,   -1,  0,   0,   0,
622         -1,  0,   0,   -1,  0,   0,   -1,  0,   0,
623         0,   0,   -1,  0,   0,   0,   -1,  0,   0,   0,
624         -1,  0,   0,   -1,  0,   0,   -1,  0,   0,
625         0,   0,   -1,  0,   0,   0,   -1,  0,   0,   0,
626         -1,  0,   0,   0,   -1,  0,   0,   0,   0,
627         0,   0,   0,   0,   0,   0,   -1,  0,   0,   0,
628         -1,  0,   0,   0,   -1,  0,   0,   0,   0,
629         0,   0,   0,   0,   0,   0,   -1,  0,   0,   0,
630         0,   -1,  0,   -1,  -1,  0,   -1,  0,   0,
631         0,   0,   0,   0,   0,   0,   0,   0,   0,   -1,
632         0,   -1,  0,   -1,  -1,  0,   0,   0,   0,
633         0,   0,   0,   0,   0,   0,   0,   0,   0,   -1,
634         -1,  0,   0,   -1,  0,   0,   -1,  0,   0,
635         0,   0,   -1,  0,   0,   0,   -1,  0,   -1,  0,
636         -1,  0,   0,   -1,  0,   0,   -1,  0,   0,
637         0,   0,   -1,  0,   0,   0,   -1,  0,   -1,  0,
638         0,   0,   0,   0,   0,   0,   0,   0,   0,
639         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
640         0,   0,   0,   0,   0,   0,   0,   0,   0,
641         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
642         -1,  0,   0,   0,   0,   0,   0,   0,   0,
643         0,   0,   0,   0,   0,   0,   -1,  0,   0,   0,
644         -1,  0,   0,   0,   0,   0,   0,   0,   0,
645         0,   0,   0,   0,   0,   0,   -1,  0,   0,   0,
646         0,   -1,  0,   0,   -1,  0,   0,   0,   -1,
647         -1,  -1,  0,   -1,  -1,  -1,  0,   0,   0,   0,
648         0,   -1,  0,   0,   -1,  0,   0,   0,   -1,
649         -1,  -1,  0,   -1,  -1,  -1,  0,   0,   0,   0,
650         0,   0,   0,   0,   0,   0,   0,   0,   0,
651         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
652         0,   0,   0,   0,   0,   0,   0,   0,   0,
653         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
654         0,   0,   0,   0,   0,   0,   0,   0,   0,
655         0,   0,   -1,  0,   0,   0,   -1,  0,   0,   0,
656         0,   0,   0,   0,   0,   0,   0,   0,   0,
657         0,   0,   -1,  0,   0,   0,   -1,  0,   0,   0,
658         -1,  0,   0,   -1,  0,   0,   -1,  0,   0,
659         0,   0,   0,   0,   0,   0,   -1,  0,   0,   0,
660         -1,  0,   0,   -1,  0,   0,   -1,  0,   0,
661         0,   0,   0,   0,   0,   0,   -1,  0,   0,   0,
662         0,   -1,  -1,  -1,  0,   0,   0,   0,   0,
663         -1,  -1,  0,   -1,  -1,  -1,  0,   0,   0,   -1,
664         0,   -1,  -1,  -1,  0,   0,   0,   0,   0,
665         -1,  -1,  0,   -1,  -1,  -1,  0,   0,   0,   -1
666 #endif
667 };
668
669 /*
670 --- T.61 characters [0xA0 .. 0xBF] -----------------
671 */
672 static Couple trans_t61a_iso8859[32] = {
673 #if (ISO_8859 == 1) || (ISO_8859 == 9)
674         {'N','S'}, {0xa1,0},  {0xa2,0},  {0xa3,0},
675         {'D','O'}, {0xa5,0},  {'C','u'}, {0xa7,0},
676         {0xa4,0},  {'\'','6'},{'"','6'}, {0xab,0},
677         {'<','-'}, {'-','!'}, {'-','>'}, {'-','v'},
678         {0xb0,0},  {0xb1,0},  {0xb2,0},  {0xb3,0},
679         {0xd7,0},  {0xb5,0},  {0xb6,0},  {0xb7,0},
680         {0xf7,0},  {'\'','9'},{'"','9'}, {0xbb,0},
681         {0xbc,0},  {0xbd,0},  {0xbe,0},  {0xbf,0}
682 #elif (ISO_8859 == 2) || (ISO_8859 == 4)
683         {'N','S'}, {'!','I'}, {'C','t'}, {'P','d'},
684         {'D','O'}, {'Y','e'}, {'C','u'}, {0xa7,0},
685         {0xa4,0},  {'\'','6'},{'"','6'}, {'<','<'},
686         {'<','-'}, {'-','!'}, {'-','>'}, {'-','v'},
687         {0xb0,0},  {'+','-'}, {'2','S'}, {'3','S'},
688         {0xd7,0},  {'M','y'}, {'P','I'}, {'.','M'},
689         {0xf7,0},  {'\'','9'},{'"','9'}, {'>','>'},
690         {'1','4'}, {'1','2'}, {'3','4'}, {'?','I'},
691 #elif (ISO_8859 == 3)
692         {'N','S'}, {'!','I'}, {'C','t'}, {0xa3,0},
693         {'D','O'}, {'Y','e'}, {'C','u'}, {0xa7,0},
694         {0xa4,0},  {'\'','6'},{'"','6'}, {'<','<'},
695         {'<','-'}, {'-','!'}, {'-','>'}, {'-','v'},
696         {0xb0,0},  {'+','-'}, {0xb2,0},  {0xb3,0},
697         {0xd7,0},  {0xb5,0},  {'P','I'}, {0xb7,0},
698         {0xf7,0},  {'\'','9'},{'"','9'}, {'>','>'},
699         {'1','4'}, {0xbd,0},  {'3','4'}, {'?','I'}
700 #elif (ISO_8859 == 10)
701         {'N','S'}, {'!','I'}, {'C','t'}, {'P','d'},
702         {'D','O'}, {'Y','e'}, {'C','u'}, {0xa7,0},
703         {'C','u'}, {'\'','6'},{'"','6'}, {'<','<'},
704         {'<','-'}, {'-','!'}, {'-','>'}, {'-','v'},
705         {0xb0,0},  {'+','-'}, {'2','S'}, {'3','S'},
706         {'*','X'}, {'M','y'}, {'P','I'}, {0xb7,0},
707         {'-',':'}, {'\'','9'},{'"','9'}, {'>','>'},
708         {'1','4'}, {'1','2'}, {'3','4'}, {'?','I'}
709 #else
710         {'N','S'}, {'!','I'}, {'C','t'}, {'P','d'},
711         {'D','O'}, {'Y','e'}, {'C','u'}, {'S','E'},
712         {'X','O'}, {'\'','6'},{'"','6'}, {'<','<'},
713         {'<','-'}, {'-','!'}, {'-','>'}, {'-','v'},
714         {'D','G'}, {'+','-'}, {'2','S'}, {'3','S'},
715         {'*','X'}, {'M','y'}, {'P','I'}, {'.','M'},
716         {'-',':'}, {'\'','9'},{'"','9'}, {'>','>'},
717         {'1','4'}, {'1','2'}, {'3','4'}, {'?','I'}
718 #endif
719 };
720
721 /*
722 --- T.61 characters [0xE0 .. 0xFF] -----------------
723 */
724 static Couple trans_t61b_iso8859[48] = {
725 #if (ISO_8859 == 1)
726         {'-','M'}, {0xb9,0},  {0xae,0},  {0xa9,0},
727         {'T','M'}, {'M','8'}, {0xac,0},  {0xa6,0},
728         {0,0},     {0,0},     {0,0},     {0,0},
729         {'1','8'}, {'3','8'}, {'5','8'}, {'7','8'},
730         {'O','m'}, {0xc6,0},  {0xd0,0},  {0xaa,0},
731         {'H','/'}, {0,0},     {'I','J'}, {'L','.'},
732         {'L','/'}, {0xd8,0},  {'O','E'}, {0xba,0},
733         {0xde,0},  {'T','/'}, {'N','G'}, {'\'','n'},
734         {'k','k'}, {0xe6,0},  {'d','/'}, {0xf0,0},
735         {'h','/'}, {'i','.'}, {'i','j'}, {'l','.'},
736         {'l','/'}, {0xf8,0},  {'o','e'}, {0xdf,0},
737         {0xfe,0},  {'t','/'}, {'n','g'}, {'-','-'}
738 #elif (ISO_8859 == 2)
739         {'-','M'}, {'1','S'}, {'R','g'}, {'C','o'},
740         {'T','M'}, {'M','8'}, {'N','O'}, {'B','B'},
741         {0,0},     {0,0},     {0,0},     {0,0},
742         {'1','8'}, {'3','8'}, {'5','8'}, {'7','8'},
743         {'O','m'}, {'A','E'}, {0xd0,0},  {'-','a'},
744         {'H','/'}, {0,0},     {'I','J'}, {'L','.'},
745         {0xa3,0},  {'O','/'}, {'O','E'}, {'-','o'},
746         {'T','H'}, {'T','/'}, {'N','G'}, {'\'','n'},
747         {'k','k'}, {'a','e'}, {0xf0,0},  {'d','-'},
748         {'h','/'}, {'i','.'}, {'i','j'}, {'l','.'},
749         {0xb3,0},  {'o','/'}, {'o','e'}, {0xdf,0},
750         {'t','h'}, {'t','/'}, {'n','g'}, {'-','-'}
751 #elif (ISO_8859 == 3)
752         {'-','M'}, {'1','S'}, {'R','g'}, {'C','o'},
753         {'T','M'}, {'M','8'}, {'N','O'}, {'B','B'},
754         {0,0},     {0,0},     {0,0},     {0,0},
755         {'1','8'}, {'3','8'}, {'5','8'}, {'7','8'},
756         {'O','m'}, {'A','E'}, {'D','/'}, {'-','a'},
757         {0xa1,0},  {0,0},     {'I','J'}, {'L','.'},
758         {'L','/'}, {'O','/'}, {'O','E'}, {'-','o'},
759         {'T','H'}, {'T','/'}, {'N','G'}, {'\'','n'},
760         {'k','k'}, {'a','e'}, {'d','/'}, {'d','-'},
761         {0xb1,0},  {0xb9,0},  {'i','j'}, {'l','.'},
762         {'l','/'}, {'o','/'}, {'o','e'}, {0xdf,0},
763         {'t','h'}, {'t','/'}, {'n','g'}, {'-','-'}
764 #elif (ISO_8859 == 4)
765         {'-','M'}, {'1','S'}, {'R','g'}, {'C','o'},
766         {'T','M'}, {'M','8'}, {'N','O'}, {'B','B'},
767         {0,0},     {0,0},     {0,0},     {0,0},
768         {'1','8'}, {'3','8'}, {'5','8'}, {'7','8'},
769         {'O','m'}, {0xc6,0},  {0xd0,0},  {'-','a'},
770         {'H','/'}, {0,0},     {'I','J'}, {'L','.'},
771         {'L','/'}, {0xd8,0},  {'O','E'}, {'-','o'},
772         {'T','H'}, {0xac,0},  {0xbd,0},  {'\'','n'},
773         {0xa2,0},  {0xe6,0},  {0xf0,0},  {'d','-'},
774         {'h','/'}, {'i','.'}, {'i','j'}, {'l','.'},
775         {'l','/'}, {0xf8,0},  {'o','e'}, {0xdf,0},
776         {'t','h'}, {0xbc,0},  {0xbf,0},  {'-','-'}
777 #elif (ISO_8859 == 9)
778         {'-','M'}, {0xb9,0},  {0xae,0},  {0xa9,0},
779         {'T','M'}, {'M','8'}, {0xac,0},  {0xa6,0},
780         {0,0},     {0,0},     {0,0},     {0,0},
781         {'1','8'}, {'3','8'}, {'5','8'}, {'7','8'},
782         {'O','m'}, {0xc6,0},  {'D','/'}, {0xaa,0},
783         {'H','/'}, {0,0},     {'I','J'}, {'L','.'},
784         {'L','/'}, {0xd8,0},  {'O','E'}, {0xba,0},
785         {'T','H'}, {'T','/'}, {'N','G'}, {'\'','n'},
786         {'k','k'}, {0xe6,0},  {'d','/'}, {'d','-'},
787         {'h','/'}, {0xfd,0},  {'i','j'}, {'l','.'},
788         {'l','/'}, {0xf8,0},  {'o','e'}, {0xdf,0},
789         {'t','h'}, {'t','/'}, {'n','g'}, {'-','-'}
790 #elif (ISO_8859 == 10)
791         {0xbd,0},  {'1','S'}, {'R','g'}, {'C','o'},
792         {'T','M'}, {'M','8'}, {'N','O'}, {'B','B'},
793         {0,0},     {0,0},     {0,0},     {0,0},
794         {'1','8'}, {'3','8'}, {'5','8'}, {'7','8'},
795         {'O','m'}, {0xc6,0},  {0xa9,0},  {'-','a'},
796         {'H','/'}, {0,0},     {'I','J'}, {'L','.'},
797         {'L','/'}, {0xd8,0},  {'O','E'}, {'-','o'},
798         {0xde,0},  {0xab,0},  {0xaf,0},  {'\'','n'},
799         {0xff,0},  {0xe6,0},  {0xb9,0},  {0xf0,0},
800         {'h','/'}, {'i','.'}, {'i','j'}, {'l','.'},
801         {'l','/'}, {0xf8,0},  {'o','e'}, {0xdf,0},
802         {0xfe,0},  {0xbb,0},  {0xbf,0},  {'-','-'}
803 #else
804         {'-','M'}, {'1','S'}, {'R','g'}, {'C','o'},
805         {'T','M'}, {'M','8'}, {'N','O'}, {'B','B'},
806         {0,0},     {0,0},     {0,0},     {0,0},
807         {'1','8'}, {'3','8'}, {'5','8'}, {'7','8'},
808         {'O','m'}, {'A','E'}, {'D','/'}, {'-','a'},
809         {'H','/'}, {0,0},     {'I','J'}, {'L','.'},
810         {'L','/'}, {'O','/'}, {'O','E'}, {'-','o'},
811         {'T','H'}, {'T','/'}, {'N','G'}, {'\'','n'},
812         {'k','k'}, {'a','e'}, {'d','/'}, {'d','-'},
813         {'h','/'}, {'i','.'}, {'i','j'}, {'l','.'},
814         {'l','/'}, {'o','/'}, {'o','e'}, {'s','s'},
815         {'t','h'}, {'t','-'}, {'n','g'}, {'-','-'}
816 #endif
817 };
818
819 /*
820 --- ISO 8859-n characters <0xA0 .. 0xFF> -------------------
821 */
822 #if (ISO_8859 == 1)
823 static Couple trans_iso8859_t61[96] = {
824         {0xa0,0},     {0xa1,0},     {0xa2,0},     {0xa3,0},
825         {0xa8,0},     {0xa5,0},     {0xd7,0},     {0xa7,0},
826         {0xc8,ALONE}, {0xd3,0},     {0xe3,0},     {0xab,0},
827         {0xd6,0},     {0xff,0},     {0xd2,0},     {0xc5,ALONE},
828         {0xb0,0},     {0xb1,0},     {0xb2,0},     {0xb3,0},
829         {0xc2,ALONE}, {0xb5,0},     {0xb6,0},     {0xb7,0},
830         {0xcb,ALONE}, {0xd1,0},     {0xeb,0},     {0xbb,0},
831         {0xbc,0},     {0xbd,0},     {0xbe,0},     {0xbf,0},
832         {0xc1,'A'},   {0xc2,'A'},   {0xc3,'A'},   {0xc4,'A'},
833         {0xc8,'A'},   {0xca,'A'},   {0xe1,0},     {0xcb,'C'},
834         {0xc1,'E'},   {0xc2,'E'},   {0xc3,'E'},   {0xc8,'E'},
835         {0xc1,'I'},   {0xc2,'I'},   {0xc3,'I'},   {0xc8,'I'},
836         {0xe2,0},     {0xc4,'N'},   {0xc1,'O'},   {0xc2,'O'},
837         {0xc3,'O'},   {0xc4,'O'},   {0xc8,'O'},   {0xb4,0},
838         {0xe9,0},     {0xc1,'U'},   {0xc2,'U'},   {0xc3,'U'},
839         {0xc8,'U'},   {0xc2,'Y'},   {0xec,0},     {0xfb,0},
840         {0xc1,'a'},   {0xc2,'a'},   {0xc3,'a'},   {0xc4,'a'},
841         {0xc8,'a'},   {0xca,'a'},   {0xf1,0},     {0xcb,'c'},
842         {0xc1,'e'},   {0xc2,'e'},   {0xc3,'e'},   {0xc8,'e'},
843         {0xc1,'i'},   {0xc2,'i'},   {0xc3,'i'},   {0xc8,'i'},
844         {0xf3,0},     {0xc4,'n'},   {0xc1,'o'},   {0xc2,'o'},
845         {0xc3,'o'},   {0xc4,'o'},   {0xc8,'o'},   {0xb8,0},
846         {0xf9,0},     {0xc1,'u'},   {0xc2,'u'},   {0xc3,'u'},
847         {0xc8,'u'},   {0xc2,'y'},   {0xfc,0},     {0xc8,'y'}
848 };
849 #elif (ISO_8859 == 2)
850 static Couple trans_iso8859_t61[96] = {
851         {0xa0,0},     {0xce,'A'},   {0xc6,ALONE}, {0xe8,0},
852         {0xa8,0},     {0xcf,'L'},   {0xc2,'S'},   {0xa7,0},
853         {0xc8,ALONE}, {0xcf,'S'},   {0xcb,'S'},   {0xcf,'T'},
854         {0xc2,'Z'},   {0xff,0},     {0xcf,'Z'},   {0xc7,'Z'},
855         {0xb0,0},     {0xce,'a'},   {0xce,ALONE}, {0xf8,0},
856         {0xc2,ALONE}, {0xcf,'l'},   {0xc2,'s'},   {0xcf,ALONE},
857         {0xcb,ALONE}, {0xcf,'s'},   {0xcb,'s'},   {0xcf,'t'},
858         {0xc2,'z'},   {0xcd,ALONE}, {0xcf,'z'},   {0xc7,'z'},
859         {0xc2,'R'},   {0xc2,'A'},   {0xc3,'A'},   {0xc6,'A'},
860         {0xc8,'A'},   {0xc2,'L'},   {0xc2,'C'},   {0xcb,'C'},
861         {0xcf,'C'},   {0xc2,'E'},   {0xce,'E'},   {0xc8,'E'},
862         {0xcf,'E'},   {0xc2,'I'},   {0xc3,'I'},   {0xcf,'D'},
863         {0xe2,0},     {0xc2,'N'},   {0xcf,'N'},   {0xc2,'O'},
864         {0xc3,'O'},   {0xcd,'O'},   {0xc8,'O'},   {0xb4,0},
865         {0xcf,'R'},   {0xca,'U'},   {0xc2,'U'},   {0xcd,'U'},
866         {0xc8,'U'},   {0xc2,'Y'},   {0xcb,'T'},   {0xfb,0},
867         {0xc2,'r'},   {0xc2,'a'},   {0xc3,'a'},   {0xc6,'a'},
868         {0xc8,'a'},   {0xc2,'l'},   {0xc2,'c'},   {0xcb,'c'},
869         {0xcf,'c'},   {0xc2,'e'},   {0xce,'e'},   {0xc8,'e'},
870         {0xcf,'e'},   {0xc2,'i'},   {0xc3,'i'},   {0xcf,'d'},
871         {0xf2,0},     {0xc2,'n'},   {0xcf,'n'},   {0xc2,'o'},
872         {0xc3,'o'},   {0xcd,'o'},   {0xc8,'o'},   {0xb8,0},
873         {0xcf,'r'},   {0xca,'u'},   {0xc2,'u'},   {0xcd,'u'},
874         {0xc8,'u'},   {0xc2,'y'},   {0xcb,'t'},   {0xc7,ALONE}
875 };
876 #elif (ISO_8859 == 3)
877 static Couple trans_iso8859_t61[96] = {
878         {0xa0,0},     {0xe4,0},     {0xc6,ALONE}, {0xa3,0},
879         {0xa8,0},     {0,0},        {0xc3,'H'},   {0xa7,0},
880         {0xc8,ALONE}, {0xc7,'I'},   {0xcb,'S'},   {0xc6,'G'},
881         {0xc3,'J'},   {0xff,0},     {0,0},        {0xc7,'Z'},
882         {0xb0,0},     {0xf4,0},     {0xb2,0},     {0xb3,0},
883         {0xc2,ALONE}, {0xb5,0},     {0xc3,'h'},   {0xb7,0},
884         {0xcb,ALONE}, {0xf5,0},     {0xcb,'s'},   {0xc6,'g'},
885         {0xc3,'j'},   {0xbd,0},     {0,0},        {0xc7,'z'},
886         {0xc1,'A'},   {0xc2,'A'},   {0xc3,'A'},   {0,0},
887         {0xc8,'A'},   {0xc7,'C'},   {0xc3,'C'},   {0xcb,'C'},
888         {0xc1,'E'},   {0xc2,'E'},   {0xc3,'E'},   {0xc8,'E'},
889         {0xc1,'I'},   {0xc2,'I'},   {0xc3,'I'},   {0xc8,'I'},
890         {0,0},        {0xc4,'N'},   {0xc1,'O'},   {0xc2,'O'},
891         {0xc3,'O'},   {0xc7,'G'},   {0xc8,'O'},   {0xb4,0},
892         {0xc3,'G'},   {0xc1,'U'},   {0xc2,'U'},   {0xc3,'U'},
893         {0xc8,'U'},   {0xc6,'U'},   {0xc3,'S'},   {0xfb,0},
894         {0xc1,'a'},   {0xc2,'a'},   {0xc3,'a'},   {0,0},
895         {0xc8,'a'},   {0xc7,'c'},   {0xc3,'c'},   {0xcb,'c'},
896         {0xc1,'e'},   {0xc2,'e'},   {0xc3,'e'},   {0xc8,'e'},
897         {0xc1,'i'},   {0xc2,'i'},   {0xc3,'i'},   {0xc8,'i'},
898         {0,0},        {0xc4,'n'},   {0xc1,'o'},   {0xc2,'o'},
899         {0xc3,'o'},   {0xc7,'g'},   {0xc8,'o'},   {0xb8,0},
900         {0xc3,'g'},   {0xc1,'u'},   {0xc2,'u'},   {0xc3,'u'},
901         {0xc8,'u'},   {0xc6,'u'},   {0xc3,'s'},   {0xc7,ALONE}
902 };
903 #elif (ISO_8859 == 4)
904 static Couple trans_iso8859_t61[96] = {
905         {0xa0,0},     {0xce,'A'},   {0xf0,0},     {0xcb,'R'},
906         {0xa8,0},     {0xc4,'I'},   {0xcb,'L'},   {0xa7,0},
907         {0xc8,ALONE}, {0xcf,'S'},   {0xc5,'E'},   {0xcb,'G'},
908         {0xed,0},     {0xff,0},     {0xcf,'Z'},   {0xc5,ALONE},
909         {0xb0,0},     {0xce,'a'},   {0xce,ALONE}, {0xcb,'r'},
910         {0xc2,ALONE}, {0xc4,'i'},   {0xcb,'l'},   {0xcf,ALONE},
911         {0xcb,ALONE}, {0xcf,'s'},   {0xc5,'e'},   {0xcb,'g'},
912         {0xfd,0},     {0xee,0},     {0xcf,'z'},   {0xfe,0},
913         {0xc5,'A'},   {0xc2,'A'},   {0xc3,'A'},   {0xc4,'A'},
914         {0xc8,'A'},   {0xca,'A'},   {0xe1,0},     {0xce,'I'},
915         {0xcf,'C'},   {0xc2,'E'},   {0xce,'E'},   {0xc8,'E'},
916         {0xc7,'E'},   {0xc2,'I'},   {0xc3,'I'},   {0xc5,'I'},
917         {0xe2,0},     {0xcb,'N'},   {0xc5,'O'},   {0xcb,'K'},
918         {0xc3,'O'},   {0xc4,'O'},   {0xc8,'O'},   {0xb4,0},
919         {0xe9,0},     {0xce,'U'},   {0xc2,'U'},   {0xc3,'U'},
920         {0xc8,'U'},   {0xc4,'U'},   {0xc5,'U'},   {0xfb,0},
921         {0xc5,'a'},   {0xc2,'a'},   {0xc3,'a'},   {0xc4,'a'},
922         {0xc8,'a'},   {0xca,'a'},   {0xf1,0},     {0xce,'i'},
923         {0xcf,'c'},   {0xc2,'e'},   {0xce,'e'},   {0xc8,'e'},
924         {0xc7,'e'},   {0xc2,'i'},   {0xc3,'i'},   {0xc5,'i'},
925         {0xf2,0},     {0xcb,'n'},   {0xc5,'o'},   {0xcb,'k'},
926         {0xc3,'o'},   {0xc4,'o'},   {0xc8,'o'},   {0xb8,0},
927         {0xf9,0},     {0xce,'u'},   {0xc2,'u'},   {0xc3,'u'},
928         {0xc8,'u'},   {0xc4,'u'},   {0xc5,'u'},   {0xc7,ALONE}
929 };
930 #elif (ISO_8859 == 9)
931 static Couple trans_iso8859_t61[96] = {
932         {0xa0,0},     {0xa1,0},     {0xa2,0},     {0xa3,0},
933         {0xa8,0},     {0xa5,0},     {0xd7,0},     {0xa7,0},
934         {0xc8,ALONE}, {0xd3,0},     {0xe3,0},     {0xab,0},
935         {0xd6,0},     {0xff,0},     {0xd2,0},     {0xc5,ALONE},
936         {0xb0,0},     {0xb1,0},     {0xb2,0},     {0xb3,0},
937         {0xc2,ALONE}, {0xb5,0},     {0xb6,0},     {0xb7,0},
938         {0xcb,ALONE}, {0xd1,0},     {0xeb,0},     {0xbb,0},
939         {0xbc,0},     {0xbd,0},     {0xbe,0},     {0xbf,0},
940         {0xc1,'A'},   {0xc2,'A'},   {0xc3,'A'},   {0xc4,'A'},
941         {0xc8,'A'},   {0xca,'A'},   {0xe1,0},     {0xcb,'C'},
942         {0xc1,'E'},   {0xc2,'E'},   {0xc3,'E'},   {0xc8,'E'},
943         {0xc1,'I'},   {0xc2,'I'},   {0xc3,'I'},   {0xc8,'I'},
944         {0xc6,'G'},   {0xc4,'N'},   {0xc1,'O'},   {0xc2,'O'},
945         {0xc3,'O'},   {0xc4,'O'},   {0xc8,'O'},   {0xb4,0},
946         {0xe9,0},     {0xc1,'U'},   {0xc2,'U'},   {0xc3,'U'},
947         {0xc8,'U'},   {0xc7,'I'},   {0xcb,'S'},   {0xfb,0},
948         {0xc1,'a'},   {0xc2,'a'},   {0xc3,'a'},   {0xc4,'a'},
949         {0xc8,'a'},   {0xca,'a'},   {0xf1,0},     {0xcb,'c'},
950         {0xc1,'e'},   {0xc2,'e'},   {0xce,'e'},   {0xc8,'e'},
951         {0xc7,'e'},   {0xc2,'i'},   {0xc3,'i'},   {0xc5,'i'},
952         {0xc6,'g'},   {0xc4,'n'},   {0xc1,'o'},   {0xc2,'o'},
953         {0xc3,'o'},   {0xc4,'o'},   {0xc8,'o'},   {0xb8,0},
954         {0xf9,0},     {0xc1,'u'},   {0xc2,'u'},   {0xc3,'u'},
955         {0xc8,'u'},   {0xf5,0},     {0xcb,'s'},   {0xc8,'y'}
956 };
957 #elif (ISO_8859 == 10)
958 static Couple trans_iso8859_t61[96] = {
959         {0xa0,0},     {0xce,'A'},   {0xc5,'E'},   {0xcb,'G'},
960         {0xc5,'I'},   {0xc4,'I'},   {0xcb,'K'},   {0xa7,0},
961         {0xcb,'L'},   {0xe2,0},     {0xcf,'S'},   {0xed,0},
962         {0xcf,'Z'},   {0xff,0},     {0xc5,'U'},   {0xee,0},
963         {0xb0,0},     {0xce,'a'},   {0xc5,'e'},   {0xcb,'g'},
964         {0xc5,'i'},   {0xc4,'i'},   {0xcb,'k'},   {0xb7,0},
965         {0xcb,'l'},   {0xf2,0},     {0xcf,'s'},   {0xfd,0},
966         {0xcf,'z'},   {0xd0,0},     {0xc5,'u'},   {0xfe,0},
967         {0xc5,'A'},   {0xc2,'A'},   {0xc3,'A'},   {0xc4,'A'},
968         {0xc8,'A'},   {0xca,'A'},   {0xe1,0},     {0xce,'I'},
969         {0xcf,'C'},   {0xc2,'E'},   {0xce,'E'},   {0xc8,'E'},
970         {0xc7,'E'},   {0xc2,'I'},   {0xc3,'I'},   {0xc8,'I'},
971         {0,0},        {0xcb,'N'},   {0xc5,'O'},   {0xc2,'O'},
972         {0xc3,'O'},   {0xc4,'O'},   {0xc8,'O'},   {0xc4,'U'},
973         {0xe9,0},     {0xce,'U'},   {0xc2,'U'},   {0xc3,'U'},
974         {0xc8,'U'},   {0xc2,'Y'},   {0xec,0},     {0xfb,0},
975         {0xc5,'a'},   {0xc2,'a'},   {0xc3,'a'},   {0xc4,'a'},
976         {0xc8,'a'},   {0xca,'a'},   {0xf1,0},     {0xce,'i'},
977         {0xcf,'c'},   {0xc2,'e'},   {0xce,'e'},   {0xc8,'e'},
978         {0xc7,'e'},   {0xc2,'i'},   {0xc3,'i'},   {0xc8,'i'},
979         {0xf3,0},     {0xcb,'n'},   {0xc5,'o'},   {0xc2,'o'},
980         {0xc3,'o'},   {0xc4,'o'},   {0xc8,'o'},   {0xc4,'u'},
981         {0xf9,0},     {0xce,'u'},   {0xc2,'u'},   {0xc3,'u'},
982         {0xc8,'u'},   {0xc2,'y'},   {0xfc,0},     {0xf0,0}
983 };
984 #endif
985
986
987 static Byte *
988 c_to_hh( Byte *o, Byte c )
989 {
990   Byte n;
991
992   *o++ = '{'; *o++ = 'x';
993   n = c >> 4;
994   *o++ = ((n < 0xA) ? '0' : 'A' - 0xA) + n;
995   n = c & 0x0F;
996   *o++ = ((n < 0xA) ? '0' : 'A' - 0xA) + n;
997   *o++ = '}';
998   return o;
999 }
1000
1001
1002 static Byte *
1003 c_to_cc( Byte *o, Couple *cc, Byte c )
1004 {
1005   if ( (*cc).a != 0 ) {
1006     if ( (*cc).b == 0 )
1007       *o++ = (*cc).a;
1008     else {
1009       *o++ = '{';
1010       *o++ = (*cc).a;
1011       *o++ = (*cc).b;
1012       *o++ = '}';
1013     }
1014     return o;
1015   }
1016   else
1017     return c_to_hh( o, c );
1018 }
1019
1020 /* --- routine to convert from T.61 to ISO 8859-n --- */
1021
1022 int
1023 ldap_t61_to_8859( char **bufp, unsigned long *buflenp, int free_input )
1024 {
1025   Byte          *s, *oo, *o;
1026   unsigned int  n;
1027   int           c;
1028   unsigned long len;
1029   Couple        *cc;
1030
1031   Debug( LDAP_DEBUG_TRACE, "ldap_t61_to_8859 input length: %ld\n",
1032         *buflenp, 0, 0 );
1033
1034   len = *buflenp;
1035   s = (Byte *) *bufp;
1036
1037   if ( (o = oo = (Byte *)malloc( 2 * len + 64 )) == NULL ) {
1038         return( 1 );
1039   }
1040
1041   while ( (char *)s - *(char **)bufp < len ) {
1042     switch ( *s >> 4 ) {
1043
1044     case 0xA: case 0xB:
1045       o = c_to_cc( o, &trans_t61a_iso8859[ *s - 0xA0 ], *s );
1046       s++;
1047       break;
1048
1049     case 0xD: case 0xE: case 0xF:
1050       o = c_to_cc( o, &trans_t61b_iso8859[ *s - 0xD0 ], *s );
1051       s++;
1052       break;
1053
1054     case 0xC:
1055       if ( (*s == 0xC0) || (*s == 0xC9) || (*s == 0xCC) ) {
1056         o = c_to_hh( o, *s++ );
1057         break;
1058       }
1059
1060       n = (*s++) - 0xC0;
1061       switch ( *s ) {
1062
1063       case 'A':  c = letter_w_diacritic[n][0]; break;
1064       case 'C':  c = letter_w_diacritic[n][1]; break;
1065       case 'D':  c = letter_w_diacritic[n][2]; break;
1066       case 'E':  c = letter_w_diacritic[n][3]; break;
1067       case 'G':  c = letter_w_diacritic[n][4]; break;
1068       case 'H':  c = letter_w_diacritic[n][5]; break;
1069       case 'I':  c = letter_w_diacritic[n][6]; break;
1070       case 'J':  c = letter_w_diacritic[n][7]; break;
1071       case 'K':  c = letter_w_diacritic[n][8]; break;
1072       case 'L':  c = letter_w_diacritic[n][9]; break;
1073       case 'N':  c = letter_w_diacritic[n][10]; break;
1074       case 'O':  c = letter_w_diacritic[n][11]; break;
1075       case 'R':  c = letter_w_diacritic[n][12]; break;
1076       case 'S':  c = letter_w_diacritic[n][13]; break;
1077       case 'T':  c = letter_w_diacritic[n][14]; break;
1078       case 'U':  c = letter_w_diacritic[n][15]; break;
1079       case 'W':  c = letter_w_diacritic[n][16]; break;
1080       case 'Y':  c = letter_w_diacritic[n][17]; break;
1081       case 'Z':  c = letter_w_diacritic[n][18]; break;
1082
1083       case 'a':  c = letter_w_diacritic[n][19]; break;
1084       case 'c':  c = letter_w_diacritic[n][20]; break;
1085       case 'd':  c = letter_w_diacritic[n][21]; break;
1086       case 'e':  c = letter_w_diacritic[n][22]; break;
1087       case 'g':  c = letter_w_diacritic[n][23]; break;
1088       case 'h':  c = letter_w_diacritic[n][24]; break;
1089       case 'i':  c = letter_w_diacritic[n][25]; break;
1090       case 'j':  c = letter_w_diacritic[n][26]; break;
1091       case 'k':  c = letter_w_diacritic[n][27]; break;
1092       case 'l':  c = letter_w_diacritic[n][28]; break;
1093       case 'n':  c = letter_w_diacritic[n][29]; break;
1094       case 'o':  c = letter_w_diacritic[n][30]; break;
1095       case 'r':  c = letter_w_diacritic[n][31]; break;
1096       case 's':  c = letter_w_diacritic[n][32]; break;
1097       case 't':  c = letter_w_diacritic[n][33]; break;
1098       case 'u':  c = letter_w_diacritic[n][34]; break;
1099       case 'w':  c = letter_w_diacritic[n][35]; break;
1100       case 'y':  c = letter_w_diacritic[n][36]; break;
1101       case 'z':  c = letter_w_diacritic[n][37]; break;
1102
1103       case ALONE:  c = (( !diacritic[n].b ) ? diacritic[n].a : -1);
1104                    break;
1105
1106       default:   c = 0;
1107       }
1108
1109       if ( c > 0 ) {
1110         *o++ = c;  s++;
1111       } else {
1112         *o++ = '{';
1113         if ( c == -1 ) {
1114           *o++ = ( ( *s == ALONE ) ? ' ' : *s );
1115           s++;
1116         } else {
1117           *o++ = '"';
1118         }
1119         *o++ = diacritic[n].a;
1120         *o++ = '}';
1121       }
1122       break;
1123
1124 #if (ISO_8859 == 0)
1125     case 0x8: case 0x9:
1126       *o++ = 0x1B; /* <ESC> */
1127       *o++ = *s++ - 0x40;
1128       break;
1129 #endif
1130
1131       default:
1132         *o++ = *s++;
1133     }
1134   }
1135
1136   len = o - oo;
1137   o = oo;
1138
1139   if ( (oo = (Byte *)realloc( o, len )) == NULL ) {
1140     free( o );
1141     return( 1 );
1142   }
1143
1144   if ( free_input ) {
1145     free( *bufp );
1146   }
1147   *bufp = (char *) oo;
1148   *buflenp = len;
1149   return( 0 );
1150 }
1151
1152
1153 static int
1154 hh_to_c( Byte *h )
1155 {
1156   Byte c;
1157
1158   if ( (*h >= '0') && (*h <= '9') )      c = *h++ - '0';
1159   else if ( (*h >= 'A') && (*h <= 'F') ) c = *h++ - 'A' + 10;
1160   else if ( (*h >= 'a') && (*h <= 'f') ) c = *h++ - 'a' + 10;
1161   else return -1;
1162
1163   c <<= 4;
1164
1165   if ( (*h >= '0') && (*h <= '9') )      c |= *h - '0';
1166   else if ( (*h >= 'A') && (*h <= 'F') ) c |= *h - 'A' + 10;
1167   else if ( (*h >= 'a') && (*h <= 'f') ) c |= *h - 'a' + 10;
1168   else return -1;
1169
1170   return c;
1171 }
1172
1173
1174 static Byte *
1175 cc_to_t61( Byte *o, Byte *s )
1176 {
1177   int n, c = 0;
1178
1179   switch ( *(s + 1) ) {
1180
1181   case '`':  c = -1;   break;    /* <grave-accent> */
1182
1183   case '!':
1184     switch ( *s ) {
1185     case '!':  c = 0x7C;  break;  /* <vertical-line> */
1186     case '(':  c = 0x7B;  break;  /* <left-curly-bracket> */
1187     case '-':  c = 0xAD;  break;  /* <upwards-arrow> */
1188     default:   c = -1;            /* <grave-accent> */
1189     }
1190     break;
1191
1192 #if (ISO_8859 == 1) || (ISO_8859 == 2) || (ISO_8859 == 3) || \
1193     (ISO_8859 == 4) || (ISO_8859 == 9)
1194   case 0xB4:
1195 #endif
1196   case '\'': c = -2;  break;    /* <acute-accent> */
1197
1198   case '^':  c = -3;  break;    /* <circumflex-acent> */
1199
1200   case '>':
1201     switch ( *s ) {
1202     case ')':  c = 0x5D;  break;  /* <right-square-bracket> */
1203     case '>':  c = 0xBB;  break;  /* <right-angle-quotation> */
1204     case '-':  c = 0xAE;  break;  /* <rightwards-arrow> */
1205     default:   c = -3;            /* <circumflex-acent> */
1206     }
1207     break;
1208
1209   case '~':
1210   case '?':  c = -4;  break;        /* <tilde> */
1211
1212 #if (ISO_8859 == 1) || (ISO_8859 == 4) || (ISO_8859 == 9)
1213   case 0xAF:  c = -5;  break;       /* <macron> */
1214 #endif
1215
1216   case '-':
1217     switch ( *s ) {
1218     case '-':  c = 0xFF; break; /* <soft-hyphen> */
1219     case '<':  c = 0xAC; break; /* <leftwards arrow> */
1220     case '+':  c = 0xB1; break; /* <plus-minus> */
1221     case 'd':  c = 0xF3; break; /* <eth> */
1222     default:   c = -5;          /* <macron> */
1223     }
1224     break;
1225
1226 #if (ISO_8859 == 2) || (ISO_8859 == 3)
1227   case 0xA2:  c = -6;  break;            /* <breve> */
1228 #endif
1229
1230   case '(':
1231     if ( *s == '<' ) c = 0x5B;  /* <left-square-bracket> */
1232     else             c = -6;    /* <breve> */
1233     break;
1234
1235 #if (ISO_8859 == 2) || (ISO_8859 == 3) || (ISO_8859 == 4)
1236   case 0xFF:  c = -7;  break;    /* <dot-accent> */
1237 #endif
1238
1239   case '.':
1240     switch ( *s ) {
1241     case 'i':  c = 0xF5; break; /* <dotless-i> */
1242     case 'L':  c = 0xE7; break; /* <L-middle-dot> */
1243     case 'l':  c = 0xF7; break; /* <l-middle-dot> */
1244     default:   c = -7;          /* <dot-accent> */
1245     }
1246     break;
1247
1248 #if (ISO_8859 == 1) || (ISO_8859 == 2) || (ISO_8859 == 3) || \
1249     (ISO_8859 == 4) || (ISO_8859 == 9)
1250   case 0xA8:  c = -8; break; /* <diaeresis> */
1251 #endif
1252
1253   case ':':
1254     if ( *s == '-')  c = 0xB8; /* <division-sign> */
1255     else             c = -8;   /* <diaeresis> */
1256     break;
1257
1258 #if (ISO_8859 == 1) || (ISO_8859 == 2) || (ISO_8859 == 3) || \
1259     (ISO_8859 == 4) || (ISO_8859 == 9) || (ISO_8859 == 10)
1260   case 0xB0:
1261 #endif
1262   case '0':  c = -10;  break;  /* <ring-above> */
1263
1264 #if (ISO_8859 == 1) || (ISO_8859 == 2) || (ISO_8859 == 3) || \
1265     (ISO_8859 == 4) || (ISO_8859 == 9)
1266   case 0xB8:
1267 #endif
1268   case ',':  c = -11; break; /* <cedilla> */
1269
1270 #if (ISO_8859 == 2)
1271   case 0xBD:
1272 #endif
1273   case '"':  c = -13; break; /* <double-acute-accent> */
1274
1275 #if (ISO_8859 == 2) || (ISO_8859 == 4)
1276   case 0xB2:
1277 #endif
1278   case ';':  c = -14; break; /* <ogonek> */
1279
1280 #if (ISO_8859 == 2) || (ISO_8859 == 4)
1281   case 0xB7:  c = -15;  break;  /* <caron> */
1282 #endif
1283
1284   case ')':
1285     if ( *s == '!' )  c = 0x7D;  /* <left-curly-bracket> */
1286     break;
1287
1288   case '<':
1289     if ( *s == '<' )  c = 0xAB;  /* <left-angle-quotation> */
1290     else              c = -15;   /* <caron> */
1291     break;
1292
1293   case '/':
1294     switch ( *s ) {
1295     case '/':  c = 0x5C; break; /* <reverse-solidus> */
1296     case 'D':  c = 0xE2; break; /* <D-stroke> */
1297     case 'd':  c = 0xF2; break; /* <d-stroke> */
1298     case 'H':  c = 0xE4; break; /* <H-stroke> */
1299     case 'h':  c = 0xF4; break; /* <h-stroke> */
1300     case 'L':  c = 0xE8; break; /* <L-stroke> */
1301     case 'l':  c = 0xF8; break; /* <l-stroke> */
1302     case 'O':  c = 0xE9; break; /* <O-stroke> */
1303     case 'o':  c = 0xF9; break; /* <o-stroke> */
1304     case 'T':  c = 0xED; break; /* <T-stroke> */
1305     case 't':  c = 0xFD; break; /* <t-stroke> */
1306     }
1307     break;
1308
1309   case '2':
1310     if ( *s == '1' )  c = 0xBD;    /* <one-half> */
1311     break;
1312
1313   case '4':
1314     switch ( *s ) {
1315     case '1':  c = 0xBC; break; /* <one-quarter> */
1316     case '3':  c = 0xBE; break; /* <three-quarters> */
1317     }
1318     break;
1319
1320   case '6':
1321     switch ( *s ) {
1322     case '\'': c = 0xA9; break; /* <left-single-quotation> */
1323     case '"':  c = 0xAA; break; /* <left-double-quotation> */
1324     }
1325     break;
1326
1327   case '8':
1328     switch ( *s ) {
1329     case '1':  c = 0xDC; break; /* <one-eighth> */
1330     case '3':  c = 0xDD; break; /* <three-eighths> */
1331     case '5':  c = 0xDE; break; /* <five-eighths> */
1332     case '7':  c = 0xDF; break; /* <seven-eighths> */
1333     case 'M':  c = 0xD5; break; /* <eighth-note> */
1334     }
1335     break;
1336
1337   case '9':
1338     switch ( *s ) {
1339     case '\'': c = 0xB9; break; /* <right-single-quotation> */
1340     case '"':  c = 0xBA; break; /* <right-double-quotation> */
1341     }
1342     break;
1343
1344   case 'A':
1345     if ( *s == 'A' )  c = -10;  /* <ring-above> + <A> */
1346     break;
1347
1348   case 'a':
1349     switch ( *s ) {
1350     case '-':  c = 0xE3; break; /* <femenine-ordinal-a> */
1351     case 'a':  c = -10;  break; /* <ring-above> + <a> */
1352     }
1353     break;
1354
1355   case 'B':
1356     if ( *s == 'B' )  c = 0xD7; /* <broken-bar> */
1357     break;
1358
1359   case 'b':
1360     if ( *s == 'N' )  c = 0xA6;  /* <number-sign> */
1361     break;
1362
1363   case 'd':
1364     if ( *s == 'P' )  c = 0xA3;  /* <pound-sign> */
1365     break;
1366
1367   case 'E':
1368     switch ( *s ) {
1369     case 'S':  c = 0xA7; break; /* <section-sign> */
1370     case 'A':  c = 0xE1; break; /* <AE> */
1371     case 'O':  c = 0xEA; break; /* <OE> */
1372     }
1373     break;
1374
1375   case 'e':
1376     switch ( *s ) {
1377     case 'a':  c = 0xF1; break; /* <ae> */
1378     case 'o':  c = 0xFA; break; /* <oe> */
1379     case 'Y':  c = 0xA5;  break;  /* <yen-sign> */
1380     }
1381     break;
1382
1383   case 'G':
1384     switch ( *s ) {
1385     case 'D':  c = 0xB0; break; /* <degree-sign> */
1386     case 'N':  c = 0xEE; break; /* <Eng> */
1387     }
1388     break;
1389
1390   case 'g':
1391     switch ( *s ) {
1392     case 'R':  c = 0xD2; break;  /* <registered-sign> */
1393     case 'n':  c = 0xFE; break; /* <eng> */
1394     }
1395     break;
1396
1397   case 'H':
1398     if ( *s == 'T' )  c = 0xEC;  /* <Thorn> */
1399     break;
1400
1401   case 'h':
1402     if ( *s == 't' )  c = 0xFC; /* <thorn> */
1403     break;
1404
1405   case 'I':
1406     switch ( *s ) {
1407     case 'P':  c = 0xB6; break;  /* <pilcrow-sign> */
1408     case '!':  c = 0xA1; break; /* <inverted-exclamation> */
1409     case '?':  c = 0xBF; break; /* <inverted-question> */
1410     }
1411     break;
1412
1413   case 'J':
1414     if ( *s == 'I' )  c = 0xE6; /* <IJ> */
1415     break;
1416
1417   case 'j':
1418     if ( *s == 'i' )  c = 0xF6;  /* <ij> */
1419     break;
1420
1421   case 'k':
1422     if ( *s == 'k' )  c = 0xF0; /* <kra> */
1423     break;
1424
1425   case 'M':
1426     switch ( *s ) {
1427     case '.':  c = 0xB7; break; /* <middle-dot> */
1428     case '-':  c = 0xD0; break; /* <em-dash> */
1429     case 'T':  c = 0xD4; break; /* <trade-mark-sign> */
1430     }
1431     break;
1432
1433   case 'm':
1434     switch ( *s ) {
1435     case '\'':                  /* <macron> RFC 1345 */
1436     case ' ':  c = -5;   break; /* <macron> */
1437     case 'O':  c = 0xE0; break; /* <Ohm sign> */
1438     }
1439     break;
1440
1441   case 'n':
1442     if ( *s == '\'' )  c = 0xEF; /* <n-preceded-by-apostrophe> */
1443     break;
1444
1445   case 'O':
1446     switch ( *s ) {
1447     case 'D':  c = 0xA4; break; /* <dollar-sign> */
1448     case 'N':  c = 0xD6; break; /* <not-sign> */
1449     }
1450     break;
1451
1452   case 'o':
1453     switch ( *s ) {
1454     case 'C':  c = 0xD3; break; /* <copyright-sign> */
1455     case '-':  c = 0xEB; break; /* <masculine-ordinal-o> */
1456     }
1457     break;
1458
1459   case 'S':
1460     switch ( *s ) {
1461     case '1':  c = 0xD1; break; /* <superscript-1> */
1462     case '2':  c = 0xB2; break; /* <superscript-2> */
1463     case '3':  c = 0xB3; break; /* <superscript-3> */
1464     case 'N':  c = 0xA0; break; /* <no-break-space> */
1465     }
1466     break;
1467
1468   case 's':
1469     if ( *s == 's' )  c = 0xFB; /* <sharp-s> */
1470     break;
1471
1472   case 't':
1473     if ( *s == 'C' )  c = 0xA2; /* <cent-sign> */
1474     break;
1475
1476   case 'u':
1477     if ( *s == 'C' )  c = 0xA8; /* <currency-sign> */
1478     break;
1479
1480   case 'v':
1481     if ( *s == '-' )  c = 0xAF; /* <downwards-arrow> */
1482     break;
1483
1484   case 'X':
1485     if ( *s == '*' )  c = 0xB4; /* <multiplication-sign> */
1486     break;
1487
1488   case 'y':
1489     if ( *s == 'M' )  c = 0xB5; /* <micro-sign> */
1490     break;
1491   }
1492
1493   if ( c > 0 ) {
1494     *o++ = c;
1495     return o;
1496   } else if ( !c )
1497     return NULL;
1498
1499   /* else: c < 0 */
1500   n = -c;
1501   switch ( *s ) {
1502
1503   case 'A':  c = letter_w_diacritic[n][0]; break;
1504   case 'C':  c = letter_w_diacritic[n][1]; break;
1505   case 'D':  c = letter_w_diacritic[n][2]; break;
1506   case 'E':  c = letter_w_diacritic[n][3]; break;
1507   case 'G':  c = letter_w_diacritic[n][4]; break;
1508   case 'H':  c = letter_w_diacritic[n][5]; break;
1509   case 'I':  c = letter_w_diacritic[n][6]; break;
1510   case 'J':  c = letter_w_diacritic[n][7]; break;
1511   case 'K':  c = letter_w_diacritic[n][8]; break;
1512   case 'L':  c = letter_w_diacritic[n][9]; break;
1513   case 'N':  c = letter_w_diacritic[n][10]; break;
1514   case 'O':  c = letter_w_diacritic[n][11]; break;
1515   case 'R':  c = letter_w_diacritic[n][12]; break;
1516   case 'S':  c = letter_w_diacritic[n][13]; break;
1517   case 'T':  c = letter_w_diacritic[n][14]; break;
1518   case 'U':  c = letter_w_diacritic[n][15]; break;
1519   case 'W':  c = letter_w_diacritic[n][16]; break;
1520   case 'Y':  c = letter_w_diacritic[n][17]; break;
1521   case 'Z':  c = letter_w_diacritic[n][18]; break;
1522
1523   case 'a':  c = letter_w_diacritic[n][19]; break;
1524   case 'c':  c = letter_w_diacritic[n][20]; break;
1525   case 'd':  c = letter_w_diacritic[n][21]; break;
1526   case 'e':  c = letter_w_diacritic[n][22]; break;
1527   case 'g':  c = letter_w_diacritic[n][23]; break;
1528   case 'h':  c = letter_w_diacritic[n][24]; break;
1529   case 'i':  c = letter_w_diacritic[n][25]; break;
1530   case 'j':  c = letter_w_diacritic[n][26]; break;
1531   case 'k':  c = letter_w_diacritic[n][27]; break;
1532   case 'l':  c = letter_w_diacritic[n][28]; break;
1533   case 'n':  c = letter_w_diacritic[n][29]; break;
1534   case 'o':  c = letter_w_diacritic[n][30]; break;
1535   case 'r':  c = letter_w_diacritic[n][31]; break;
1536   case 's':  c = letter_w_diacritic[n][32]; break;
1537   case 't':  c = letter_w_diacritic[n][33]; break;
1538   case 'u':  c = letter_w_diacritic[n][34]; break;
1539   case 'w':  c = letter_w_diacritic[n][35]; break;
1540   case 'y':  c = letter_w_diacritic[n][36]; break;
1541   case 'z':  c = letter_w_diacritic[n][37]; break;
1542
1543   case '\'':
1544   case ' ':  c = -1; break;
1545
1546   default:   c = 0;
1547   }
1548
1549   if ( !c )
1550     return NULL;
1551
1552   *o++ = n + 0xC0;
1553   *o++ = ( ( (*s == ' ') || (*s == '\'') ) ? ALONE : *s );
1554   return o;
1555 }
1556
1557
1558 /* --- routine to convert from ISO 8859-n to T.61 --- */
1559
1560 int
1561 ldap_8859_to_t61( char **bufp, unsigned long *buflenp, int free_input )
1562 {
1563   Byte          *s, *oo, *o, *aux;
1564   int           c;
1565   unsigned long len; 
1566   Couple        *cc;
1567
1568   Debug( LDAP_DEBUG_TRACE, "ldap_8859_to_t61 input length: %ld\n",
1569         *buflenp, 0, 0 );
1570
1571   len = *buflenp;
1572   s = (Byte *) *bufp;
1573
1574   if ( (o = oo = (Byte *)malloc( 2 * len + 64 )) == NULL ) {
1575         return( 1 );
1576   }
1577
1578   while ( (char *)s - *(char **)bufp < len ) {
1579     switch( *s >> 5 ) {
1580
1581     case 2:
1582       switch ( *s ) {
1583
1584       case '^':  *o++ = 0xC3; *o++ = ALONE; s++; break;
1585
1586       case '\\':
1587         s++;
1588         if ( (c = hh_to_c( s )) != -1 ) {
1589           *o++ = c;
1590           s += 2;
1591         } else
1592           *o++ = '\\';
1593         break;
1594
1595       default:  *o++ = *s++;
1596       }
1597       break;
1598
1599     case 3:
1600       switch ( *s ) {
1601
1602       case '`':  *o++ = 0xC1; *o++ = ALONE; s++; break;
1603       case '~':  *o++ = 0xC4; *o++ = ALONE; s++; break;
1604
1605       case '{':
1606         s++;
1607         if ( *(s + 2) == '}' ) {
1608           if ( (aux = cc_to_t61( o, s )) != NULL ) {
1609             o = aux;
1610             s += 3;
1611           } else {
1612             *o++ = '{';
1613           }
1614         } else if ( (*(s + 3) == '}') && ( (*s == 'x') || (*s == 'X') ) &&
1615                     ( (c = hh_to_c( s + 1 )) != -1 ) ) {
1616           *o++ = c;
1617           s += 4;
1618         } else {
1619           *o++ = '{';
1620         }
1621         break;
1622
1623       default:
1624         *o++ = *s++;
1625       }
1626       break;
1627
1628 #if (ISO_8859 == 0)
1629     case 4: case 5: case 6: case 7:
1630       s++;
1631       break;
1632 #else
1633     case 5: case 6: case 7:
1634 # if (ISO_8859 == 1) || (ISO_8859 == 2) || (ISO_8859 == 3) || \
1635      (ISO_8859 == 4) || (ISO_8859 == 9) || (ISO_8859 == 10)
1636       if ( (*(cc = &trans_iso8859_t61[ *s - 0xA0 ])).a ) {
1637         *o++ = (*cc).a;
1638         if ( (*cc).b )  *o++ = (*cc).b;
1639       }
1640 # endif
1641       s++;
1642       break;
1643 #endif
1644
1645     default:
1646       *o++ = *s++;
1647     }
1648   }
1649
1650   len = o - oo;
1651   o = oo;
1652
1653   if ( (oo = (Byte *)realloc( o, len )) == NULL ) {
1654     free( o );
1655     return( 1 );
1656   }
1657
1658   if ( free_input ) {
1659     free( *bufp );
1660   }
1661   *bufp = (char *) oo;
1662   *buflenp = len;
1663   return( 0 );
1664 }
1665
1666
1667 #ifdef NOT_NEEDED_IN_LIBLDAP    /* mcs@umich.edu 12 Oct 1995 */
1668 /* --- routine to convert "escaped" (\hh) characters to 8bits --- */
1669
1670 void convert_escaped_to_8bit( s )
1671 char    *s;
1672 {
1673   char  *o = s;
1674   int   c;
1675
1676   while ( *s ) {
1677     if ( *s == '\\' ) {
1678       if ( (c = hh_to_c( ++s )) != -1 ) {
1679         *o++ = c;
1680         s += 2;
1681       } else
1682         *o++ = '\\';
1683     } else
1684       *o++ = *s++;
1685   }
1686   *o = '\0';
1687 }
1688
1689 /* --- routine to convert 8bits characters to the "escaped" (\hh) form --- */
1690
1691 char *convert_8bit_to_escaped( s )
1692 Byte  *s;
1693 {
1694   Byte  *o, *oo;
1695   Byte  n;
1696
1697   if ( (o = oo = (Byte *)malloc( 2 * strlen( s ) + 64 )) == NULL ) {
1698         return( NULL );
1699   }
1700
1701   while ( *s ) {
1702     if ( *s < 0x80 )
1703       *o++ = *s++;
1704     else {
1705       *o++ = '\\';
1706       n = *s >> 4;
1707       *o++ = ((n < 0xA) ? '0' : 'A' - 0xA) + n;
1708       n = *s++ & 0x0F;
1709       *o++ = ((n < 0xA) ? '0' : 'A' - 0xA) + n;
1710     }
1711   }
1712   *o = '\0';
1713
1714   o = oo;
1715
1716   if ( (oo = (Byte *)realloc( o, strlen( o ) + 1 )) == NULL ) {
1717     free( o );
1718     return( NULL );
1719   }
1720
1721   return( (char *)oo );
1722 }
1723
1724 /* --- routine to convert from T.61 to printable characters --- */
1725
1726 /*
1727    printable characters [RFC 1488]: 'A'..'Z', 'a'..'z', '0'..'9',
1728        '\'', '(', ')', '+', ',', '-', '.', '/', ':', '?, ' '.
1729
1730    that conversion is language dependent.
1731 */
1732
1733 static Couple last_t61_printabled[32] = {
1734         {0,0},     {'A','E'}, {'D',0},   {0,0},
1735         {'H',0},   {0,0},     {'I','J'}, {'L',0},
1736         {'L',0},   {'O',0},   {'O','E'}, {0,0},
1737         {'T','H'}, {'T',0},   {'N','G'}, {'n',0},
1738         {'k',0},   {'a','e'}, {'d',0},   {'d',0},
1739         {'h',0},   {'i',0},   {'i','j'}, {'l',0},
1740         {'l',0},   {'o',0},   {'o','e'}, {'s','s'},
1741         {'t','h'}, {'t',0},   {'n','g'}, {0,0}
1742 };
1743
1744 char *t61_printable( s )
1745 Byte  *s;
1746 {
1747   Byte   *o, *oo;
1748   Byte   n;
1749   Couple *cc;
1750
1751   if ( (o = oo = (Byte *)malloc( 2 * strlen( s ) + 64 )) == NULL ) {
1752         return( NULL );
1753   }
1754
1755   while ( *s ) {
1756     if ( ( (*s >= 'A') && (*s <= 'Z') ) ||
1757          ( (*s >= 'a') && (*s <= 'z') ) ||
1758          ( (*s >= '0') && (*s <= '9') ) ||
1759          ( (*s >= '\'') && (*s <= ')') ) ||
1760          ( (*s >= '+') && (*s <= '/') ) ||
1761          ( *s == '?' ) || ( *s == ' ' ) )
1762       *o++ = *s++;
1763     else {
1764       if ( *s >= 0xE0 ) {
1765         if ( (*(cc = &last_t61_printabled[ *s - 0xE0 ])).a ) {
1766           *o++ = (*cc).a;
1767           if ( (*cc).b )  *o++ = (*cc).b;
1768         }
1769       }
1770       else if ( (*s >> 4) == 0xC ) {
1771         switch ( *s ) {
1772         case 0xCA:                      /* ring */
1773           switch ( *(s + 1) ) {
1774           case 'A':  *o++ = 'A'; *o++ = 'A'; s++; break; /* Swedish */
1775           case 'a':  *o++ = 'a'; *o++ = 'a'; s++; break; /* Swedish */
1776           }
1777           break;
1778
1779         case 0xC8:                      /* diaeresis */
1780           switch ( *(s + 1) ) {
1781           case 'Y':  *o++ = 'I'; *o++ = 'J'; s++; break; /* Dutch */
1782           case 'y':  *o++ = 'i'; *o++ = 'j'; s++; break; /* Dutch */
1783           }
1784           break;
1785         }
1786       }
1787       s++;
1788     }
1789   }
1790   *o = '\0';
1791
1792   o = oo;
1793
1794   if ( (oo = (Byte *)realloc( o, strlen( o ) + 1 )) == NULL ) {
1795     free( o );
1796     return( NULL );
1797   }
1798
1799   return( (char *)oo );
1800 }
1801 #endif /* NOT_NEEDED_IN_LIBLDAP */      /* mcs@umich.edu 12 Oct 1995 */
1802
1803 #endif /* LDAP_CHARSET_8859 */
1804 #endif /* STR_TRANSLATION */