]> arthur.barton.de Git - netatalk.git/blob - bin/uniconv/iso8859_1_adapted.c
add uniconv tool to convert between volume encodings
[netatalk.git] / bin / uniconv / iso8859_1_adapted.c
1 /* 
2    iso-8859-1.adapted conversion routines
3    Copyright (C) Bjoern Fernhomberg 2002,2003
4    
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9    
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14    
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif /* HAVE_CONFIG_H */
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26
27 #include <netatalk/endian.h>
28 #include <atalk/unicode.h>
29 #include <atalk/logger.h>
30
31 #include "../../libatalk/unicode/byteorder.h"
32
33 static size_t   iso8859_adapted_pull(void *,char **, size_t *, char **, size_t *);
34 static size_t   iso8859_adapted_push(void *,char **, size_t *, char **, size_t *);
35
36 struct charset_functions charset_iso8859_adapted =
37 {
38         "ISO-8859-ADAPTED",
39         0,
40         iso8859_adapted_pull,
41         iso8859_adapted_push,
42         CHARSET_MULTIBYTE | CHARSET_PRECOMPOSED
43 };
44
45
46 /* ------------------------ 
47  * from unicode to iso8859_adapted code page
48 */
49
50 static size_t iso8859_adapted_push( void *cd, char **inbuf, size_t *inbytesleft,
51                          char **outbuf, size_t *outbytesleft)
52 {
53     int len = 0;
54
55     while (*inbytesleft >= 2 && *outbytesleft >= 1) {
56         ucs2_t inptr = SVAL((*inbuf),0);
57         if ( inptr == 0x2122) {
58                 SSVAL((*outbuf),0,0xAA);
59         }
60         else if ( inptr == 0x0192) {
61                 SSVAL((*outbuf),0,0xC5);
62         }
63         else if ( inptr == 0x2014) {
64                 SSVAL((*outbuf),0,0xD1);
65         }
66         else if ( inptr == 0x201C) {
67                 SSVAL((*outbuf),0,0xD2);
68         }
69         else if ( inptr == 0x201D) {
70                 SSVAL((*outbuf),0,0xD3);
71         }
72         else if ( inptr == 0x2018) {
73                 SSVAL((*outbuf),0,0xD4);
74         }
75         else if ( inptr == 0x2019) {
76                 SSVAL((*outbuf),0,0xD5);
77         }
78         else if ( inptr == 0x25CA) {
79                 SSVAL((*outbuf),0,0xD7);
80         }
81         else if ( inptr > 0x0100) {
82                 errno = EILSEQ;
83                 return -1;
84         }
85         else {
86                 SSVAL((*outbuf), 0, inptr);
87         }
88         (*inbuf)        +=2;
89         (*outbuf)       +=1;
90         (*inbytesleft) -=2;
91         (*outbytesleft)-=1;
92         len++;
93     }
94
95     if (*inbytesleft > 0) {
96         errno = E2BIG;
97         return -1;
98     }
99
100     return len;
101 }
102
103 /* ------------------------ */
104 static size_t iso8859_adapted_pull ( void *cd, char **inbuf, size_t *inbytesleft,
105                          char **outbuf, size_t *outbytesleft)
106 {
107     unsigned char  *inptr;
108     size_t         len = 0;
109
110     while (*inbytesleft >= 1 && *outbytesleft >= 2) {
111         inptr = (unsigned char *) *inbuf;
112
113         if ( *inptr == 0xAA) {
114                 SSVAL((*outbuf),0,0x2122);
115         }
116         else if ( *inptr == 0xC5) {
117                 SSVAL((*outbuf),0,0x0192);
118         }
119         else if ( *inptr == 0xD1) {
120                 SSVAL((*outbuf),0,0x2014);
121         }
122         else if ( *inptr == 0xD2) {
123                 SSVAL((*outbuf),0,0x201C);
124         }
125         else if ( *inptr == 0xD3) {
126                 SSVAL((*outbuf),0,0x201D);
127         }
128         else if ( *inptr == 0xD4) {
129                 SSVAL((*outbuf),0,0x2018);
130         }
131         else if ( *inptr == 0xD5) {
132                 SSVAL((*outbuf),0,0x2019);
133         }
134         else if ( *inptr == 0xD7) {
135                 SSVAL((*outbuf),0,0x25CA);
136         }
137         else {
138                 SSVAL((*outbuf),0,(*inptr));
139         }
140         (*outbuf)      +=2;
141         (*outbytesleft)-=2;
142         (*inbuf)        +=1;
143         (*inbytesleft) -=1;
144         len++;
145     }
146
147     if (*inbytesleft > 0) {
148         errno = E2BIG;
149         return (size_t) -1;
150     }
151     return len;
152 }
153