]> arthur.barton.de Git - netatalk.git/blob - libatalk/unicode/charsets/generic_mb.c
Move byteorder.h to include/atalk
[netatalk.git] / libatalk / unicode / charsets / generic_mb.c
1 /* 
2    Unix SMB/CIFS implementation.
3    minimal iconv implementation
4    Copyright (C) Andrew Tridgell 2001
5    Copyright (C) Jelmer Vernooij 2002,2003
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20    
21    From samba 3.0 beta and GNU libiconv-1.8
22    It's bad but most of the time we can't use libc iconv service:
23    - it doesn't round trip for most encoding
24    - it doesn't know about Apple extension
25 */
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif /* HAVE_CONFIG_H */
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <arpa/inet.h>
35
36 #include <atalk/unicode.h>
37 #include <atalk/logger.h>
38 #include <atalk/byteorder.h>
39
40 #include "generic_mb.h"
41
42
43
44 /* ------------------------ */
45
46 size_t mb_generic_push( int (*char_func)(unsigned char *, ucs2_t), void *cd _U_, char **inbuf, 
47                         size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
48 {
49         int len = 0;
50         unsigned char *tmpptr = (unsigned char *) *outbuf;
51         ucs2_t inval;
52
53         while (*inbytesleft >= 2 && *outbytesleft >= 1) {
54
55                 inval = SVAL((*inbuf),0);
56                 if ( (char_func)( tmpptr, inval)) {
57                         (*inbuf) += 2;
58                         tmpptr++;
59                         len++;
60                         (*inbytesleft)  -= 2;
61                         (*outbytesleft) -= 1;
62                 }
63                 else    
64                 {
65                         errno = EILSEQ;
66                         return (size_t) -1;     
67                 }
68         }
69
70         if (*inbytesleft > 0) {
71                 errno = E2BIG;
72                 return -1;
73         }
74
75         return len;
76 }
77
78 /* ------------------------ */
79
80 size_t mb_generic_pull ( int (*char_func)(ucs2_t *, const unsigned char *), void *cd _U_, 
81                         char **inbuf, size_t *inbytesleft,char **outbuf, size_t *outbytesleft)
82 {
83         ucs2_t          temp;
84         unsigned char   *inptr;
85         size_t  len = 0;
86
87         while (*inbytesleft >= 1 && *outbytesleft >= 2) {
88
89                 inptr = (unsigned char *) *inbuf;
90                 if (char_func ( &temp, inptr)) {
91                         SSVAL((*outbuf), 0, temp);
92                         (*inbuf)        +=1;
93                         (*outbuf)       +=2;
94                         (*inbytesleft) -=1;
95                         (*outbytesleft)-=2;
96                         len++;
97                         
98                 }
99                 else    
100                 {
101                         errno = EILSEQ;
102                         return (size_t) -1;     
103                 }
104         }
105
106         if (*inbytesleft > 0) {
107                 errno = E2BIG;
108                 return (size_t) -1;
109         }
110
111         return len;
112
113 }