lib:util: Use C99 initializer for weird_table in charset
[metze/samba/wip.git] / lib / util / charset / weird.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Samba module with developer tools
4    Copyright (C) Andrew Tridgell 2001
5    Copyright (C) Jelmer Vernooij 2002
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 3 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, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "charset_proto.h"
23
24 #ifdef DEVELOPER
25
26 static struct {
27         char from;
28         const char *to;
29         int len;
30 } weird_table[] = {
31         {
32                 .from = 'q',
33                 .to   = "^q^",
34                 .len  = 3,
35         },
36         {
37                 .from = 'Q',
38                 .to   = "^Q^",
39                 .len  = 3,
40         },
41         {
42                 .len = 0,
43         }
44 };
45
46 size_t weird_pull(void *cd, const char **inbuf, size_t *inbytesleft,
47                   char **outbuf, size_t *outbytesleft)
48 {
49         while (*inbytesleft >= 1 && *outbytesleft >= 2) {
50                 int i;
51                 int done = 0;
52                 for (i=0;weird_table[i].from;i++) {
53                         if (strncmp((*inbuf), 
54                                     weird_table[i].to, 
55                                     weird_table[i].len) == 0) {
56                                 if (*inbytesleft < weird_table[i].len) {
57                                         DEBUG(0,("ERROR: truncated weird string\n"));
58                                         /* smb_panic("weird_pull"); */
59
60                                 } else {
61                                         (*outbuf)[0] = weird_table[i].from;
62                                         (*outbuf)[1] = 0;
63                                         (*inbytesleft)  -= weird_table[i].len;
64                                         (*outbytesleft) -= 2;
65                                         (*inbuf)  += weird_table[i].len;
66                                         (*outbuf) += 2;
67                                         done = 1;
68                                         break;
69                                 }
70                         }
71                 }
72                 if (done) continue;
73                 (*outbuf)[0] = (*inbuf)[0];
74                 (*outbuf)[1] = 0;
75                 (*inbytesleft)  -= 1;
76                 (*outbytesleft) -= 2;
77                 (*inbuf)  += 1;
78                 (*outbuf) += 2;
79         }
80
81         if (*inbytesleft > 0) {
82                 errno = E2BIG;
83                 return -1;
84         }
85         
86         return 0;
87 }
88
89 size_t weird_push(void *cd, const char **inbuf, size_t *inbytesleft,
90                   char **outbuf, size_t *outbytesleft)
91 {
92         int ir_count=0;
93
94         while (*inbytesleft >= 2 && *outbytesleft >= 1) {
95                 int i;
96                 int done=0;
97                 for (i=0;weird_table[i].from;i++) {
98                         if ((*inbuf)[0] == weird_table[i].from &&
99                             (*inbuf)[1] == 0) {
100                                 if (*outbytesleft < weird_table[i].len) {
101                                         DEBUG(0,("No room for weird character\n"));
102                                         /* smb_panic("weird_push"); */
103                                 } else {
104                                         memcpy(*outbuf, weird_table[i].to, 
105                                                weird_table[i].len);
106                                         (*inbytesleft)  -= 2;
107                                         (*outbytesleft) -= weird_table[i].len;
108                                         (*inbuf)  += 2;
109                                         (*outbuf) += weird_table[i].len;
110                                         done = 1;
111                                         break;
112                                 }
113                         }
114                 }
115                 if (done) continue;
116
117                 (*outbuf)[0] = (*inbuf)[0];
118                 if ((*inbuf)[1]) ir_count++;
119                 (*inbytesleft)  -= 2;
120                 (*outbytesleft) -= 1;
121                 (*inbuf)  += 2;
122                 (*outbuf) += 1;
123         }
124
125         if (*inbytesleft == 1) {
126                 errno = EINVAL;
127                 return -1;
128         }
129
130         if (*inbytesleft > 1) {
131                 errno = E2BIG;
132                 return -1;
133         }
134         
135         return ir_count;
136 }
137
138 #else
139 void charset_weird_dummy(void);
140 void charset_weird_dummy(void)
141 {
142         return;
143 }
144
145 #endif