]> arthur.barton.de Git - netatalk.git/blob - bin/uniconv/iso8859_1_adapted.c
Move byteorder.h to include/atalk
[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 #include <arpa/inet.h>
27
28 #include <atalk/unicode.h>
29 #include <atalk/logger.h>
30 #include <atalk/byteorder.h>
31
32 static size_t   iso8859_adapted_pull(void *,char **, size_t *, char **, size_t *);
33 static size_t   iso8859_adapted_push(void *,char **, size_t *, char **, size_t *);
34
35 struct charset_functions charset_iso8859_adapted =
36 {
37         "ISO-8859-ADAPTED",
38         0,
39         iso8859_adapted_pull,
40         iso8859_adapted_push,
41         CHARSET_MULTIBYTE | CHARSET_PRECOMPOSED,
42         NULL,
43         NULL, NULL
44 };
45
46
47 /* ------------------------ 
48  * from unicode to iso8859_adapted code page
49 */
50
51 static size_t iso8859_adapted_push( void *cd _U_, char **inbuf, size_t *inbytesleft,
52                          char **outbuf, size_t *outbytesleft)
53 {
54     int len = 0;
55
56     while (*inbytesleft >= 2 && *outbytesleft >= 1) {
57         ucs2_t inptr = SVAL((*inbuf),0);
58         if ( inptr == 0x2122) {
59                 SSVAL((*outbuf),0,0xAA);
60         }
61         else if ( inptr == 0x0192) {
62                 SSVAL((*outbuf),0,0xC5);
63         }
64         else if ( inptr == 0x2014) {
65                 SSVAL((*outbuf),0,0xD1);
66         }
67         else if ( inptr == 0x201C) {
68                 SSVAL((*outbuf),0,0xD2);
69         }
70         else if ( inptr == 0x201D) {
71                 SSVAL((*outbuf),0,0xD3);
72         }
73         else if ( inptr == 0x2018) {
74                 SSVAL((*outbuf),0,0xD4);
75         }
76         else if ( inptr == 0x2019) {
77                 SSVAL((*outbuf),0,0xD5);
78         }
79         else if ( inptr == 0x25CA) {
80                 SSVAL((*outbuf),0,0xD7);
81         }
82         else if ( inptr > 0x0100) {
83                 errno = EILSEQ;
84                 return -1;
85         }
86         else {
87                 SSVAL((*outbuf), 0, inptr);
88         }
89         (*inbuf)        +=2;
90         (*outbuf)       +=1;
91         (*inbytesleft) -=2;
92         (*outbytesleft)-=1;
93         len++;
94     }
95
96     if (*inbytesleft > 0) {
97         errno = E2BIG;
98         return -1;
99     }
100
101     return len;
102 }
103
104 /* ------------------------ */
105 static size_t iso8859_adapted_pull ( void *cd _U_, char **inbuf, size_t *inbytesleft,
106                          char **outbuf, size_t *outbytesleft)
107 {
108     unsigned char  *inptr;
109     size_t         len = 0;
110
111     while (*inbytesleft >= 1 && *outbytesleft >= 2) {
112         inptr = (unsigned char *) *inbuf;
113
114         if ( *inptr == 0xAA) {
115                 SSVAL((*outbuf),0,0x2122);
116         }
117         else if ( *inptr == 0xC5) {
118                 SSVAL((*outbuf),0,0x0192);
119         }
120         else if ( *inptr == 0xD1) {
121                 SSVAL((*outbuf),0,0x2014);
122         }
123         else if ( *inptr == 0xD2) {
124                 SSVAL((*outbuf),0,0x201C);
125         }
126         else if ( *inptr == 0xD3) {
127                 SSVAL((*outbuf),0,0x201D);
128         }
129         else if ( *inptr == 0xD4) {
130                 SSVAL((*outbuf),0,0x2018);
131         }
132         else if ( *inptr == 0xD5) {
133                 SSVAL((*outbuf),0,0x2019);
134         }
135         else if ( *inptr == 0xD7) {
136                 SSVAL((*outbuf),0,0x25CA);
137         }
138         else {
139                 SSVAL((*outbuf),0,(*inptr));
140         }
141         (*outbuf)      +=2;
142         (*outbytesleft)-=2;
143         (*inbuf)        +=1;
144         (*inbytesleft) -=1;
145         len++;
146     }
147
148     if (*inbytesleft > 0) {
149         errno = E2BIG;
150         return (size_t) -1;
151     }
152     return len;
153 }
154