File: encoding_wide.c

package info (click to toggle)
virtuoso-opensource 6.1.4%2Bdfsg1-7
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 245,116 kB
  • sloc: ansic: 639,631; sql: 439,225; xml: 287,085; java: 61,048; sh: 38,723; cpp: 36,889; cs: 25,240; php: 12,562; yacc: 9,036; lex: 7,149; makefile: 6,093; jsp: 4,447; awk: 1,643; perl: 1,017; ruby: 1,003; python: 329
file content (261 lines) | stat: -rw-r--r-- 8,442 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
 *  
 *  This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
 *  project.
 *  
 *  Copyright (C) 1998-2006 OpenLink Software
 *  
 *  This project is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; only version 2 of the License, dated June 1991.
 *  
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 *  
 *  
*/
#include "langfunc.h"
#include <stdarg.h>

/* WIDE identity */

#define next_wchar_begin(ptr) ((char *)( ((wchar_t *)(ptr)) + 1 ))

unichar eh_decode_char__widewrapper(__constcharptr *src_begin_ptr, const char *src_buf_end, ...)
{
  if (next_wchar_begin(src_begin_ptr[0]) > src_buf_end)
    {
      if (src_begin_ptr[0] > src_buf_end)
	return UNICHAR_EOD;
      return UNICHAR_NO_DATA;
    }
  else
    {
      const wchar_t *src_begin_wptr = (const wchar_t *)(src_begin_ptr[0]);
      unsigned char prefetch[MAX_ENCODED_CHAR];
      unsigned char *prefetch_tail = prefetch;
      unsigned char *prefetch_end = prefetch;
      size_t prefetch_ctr;
      unichar res_uchar;
      va_list tail;
      encoding_handler_t *my_eh;
      encoding_handler_t *inner_eh;
      int *state_ptr;
      va_start (tail, src_buf_end);
      my_eh = va_arg (tail, encoding_handler_t *);
      state_ptr = va_arg (tail, int *);
      va_end (tail);
      inner_eh = (encoding_handler_t *)(my_eh->eh_appdata);
      for (prefetch_ctr = 0;
	((prefetch_ctr < inner_eh->eh_maxsize) &&
	  next_wchar_begin (src_begin_wptr+prefetch_ctr) <= src_buf_end);
	prefetch_ctr++ )
        {
	  wchar_t wc = src_begin_wptr[prefetch_ctr];
	  if (wc & ~0xFF)
	    return UNICHAR_BAD_ENCODING;
	  (prefetch_end++)[0] = (unsigned char)wc;
	}
      res_uchar = inner_eh->eh_decode_char ((const char **)(&prefetch_tail), (char *)prefetch_end, inner_eh, state_ptr);
      src_begin_ptr[0] = (char *)(((wchar_t *)(src_begin_ptr[0])) + (prefetch_tail-prefetch));
      return res_uchar;
    }
}


char *eh_encode_char__widewrapper (unichar char_to_put, char *tgt_buf, char *tgt_buf_end, ...)
{
  if (char_to_put < 0)
    return tgt_buf;
  if (tgt_buf_end < next_wchar_begin(tgt_buf))
    return (char *)UNICHAR_NO_ROOM;
  ((wchar_t *)(tgt_buf))[0] = (wchar_t)('?');
  return next_wchar_begin(tgt_buf);
}


int eh_decode_buffer__widewrapper (unichar *tgt_buf, int tgt_buf_len, __constcharptr *src_begin_ptr, const char *src_buf_end, ...)
{
  int res = 0;
  const wchar_t *src_begin_wptr;
  unsigned char prefetch[MAX_ENCODED_CHAR];
  unsigned char *prefetch_tail;
  unsigned char *prefetch_end;
  size_t prefetch_ctr;
  unichar res_uchar;
  va_list tail;
  encoding_handler_t *my_eh;
  encoding_handler_t *inner_eh;
  int *state_ptr;
  va_start (tail, src_buf_end);
  my_eh = va_arg (tail, encoding_handler_t *);
  state_ptr = va_arg (tail, int *);
  va_end (tail);
  inner_eh = (encoding_handler_t *)(my_eh->eh_appdata);
  while((tgt_buf_len>0) && (next_wchar_begin(src_begin_ptr[0]) <= src_buf_end))
    {
      src_begin_wptr = (const wchar_t *)(src_begin_ptr[0]);
      prefetch_tail = prefetch;
      prefetch_end = prefetch;
      for (prefetch_ctr = 0;
	((prefetch_ctr < inner_eh->eh_maxsize) &&
	  next_wchar_begin (src_begin_wptr+prefetch_ctr) <= src_buf_end);
	prefetch_ctr++ )
        {
	  wchar_t wc = src_begin_wptr[prefetch_ctr];
	  if (wc & ~0xFF)
	    return UNICHAR_BAD_ENCODING;
	  (prefetch_end++)[0] = (unsigned char)wc;
	}
      res_uchar = inner_eh->eh_decode_char ((const char **)(&prefetch_tail), (char *)prefetch_end, inner_eh, state_ptr);
      switch (res_uchar)
	{
	case UNICHAR_BAD_ENCODING:
	case UNICHAR_EOD:
	  return res_uchar;
	case UNICHAR_NO_DATA:
	  src_begin_ptr[0] = (char *)(((wchar_t *)(src_begin_ptr[0])) + (prefetch_tail-prefetch));
	  return res;
	default: /* nop */ ;
	}
      src_begin_ptr[0] = (char *)(((wchar_t *)(src_begin_ptr[0])) + (prefetch_tail-prefetch));
      (tgt_buf++)[0] = res_uchar;
      tgt_buf_len--;
      res++;
    }
  if (src_begin_ptr[0] > src_buf_end)
    return UNICHAR_EOD;
  return res;
}


int eh_decode_buffer_to_wchar__widewrapper (wchar_t *tgt_buf, int tgt_buf_len, __constcharptr *src_begin_ptr, const char *src_buf_end, ...)
{
  int res = 0;
  const wchar_t *src_begin_wptr;
  unsigned char prefetch[MAX_ENCODED_CHAR];
  unsigned char *prefetch_tail;
  unsigned char *prefetch_end;
  size_t prefetch_ctr;
  unichar res_uchar;
  va_list tail;
  encoding_handler_t *my_eh;
  encoding_handler_t *inner_eh;
  int *state_ptr;
  va_start (tail, src_buf_end);
  my_eh = va_arg (tail, encoding_handler_t *);
  state_ptr = va_arg (tail, int *);
  va_end (tail);
  inner_eh = (encoding_handler_t *)(my_eh->eh_appdata);
  while((tgt_buf_len>0) && (next_wchar_begin(src_begin_ptr[0]) <= src_buf_end))
    {
      src_begin_wptr = (const wchar_t *)(src_begin_ptr[0]);
      prefetch_tail = prefetch;
      prefetch_end = prefetch;
      for (prefetch_ctr = 0;
	((prefetch_ctr < inner_eh->eh_maxsize) &&
	  next_wchar_begin (src_begin_wptr+prefetch_ctr) <= src_buf_end);
	prefetch_ctr++ )
        {
	  wchar_t wc = src_begin_wptr[prefetch_ctr];
	  if (wc & ~0xFF)
	    return UNICHAR_BAD_ENCODING;
	  (prefetch_end++)[0] = (unsigned char)wc;
	}
      res_uchar = inner_eh->eh_decode_char ((const char **)(&prefetch_tail), (char *)prefetch_end, inner_eh, state_ptr);
      switch (res_uchar)
	{
	case UNICHAR_BAD_ENCODING:
	case UNICHAR_EOD:
	  return res_uchar;
	case UNICHAR_NO_DATA:
	  src_begin_ptr[0] = (char *)(((wchar_t *)(src_begin_ptr[0])) + (prefetch_tail-prefetch));
	  return res;
	default:
          if (res & ~0xffffl)
            return UNICHAR_OUT_OF_WCHAR;
	}
      src_begin_ptr[0] = (char *)(((wchar_t *)(src_begin_ptr[0])) + (prefetch_tail-prefetch));
      (tgt_buf++)[0] = res_uchar;
      tgt_buf_len--;
      res++;
    }
  if (src_begin_ptr[0] > src_buf_end)
    return UNICHAR_EOD;
  return res;
}


char *eh_encode_buffer__widewrapper (const unichar *src_buf, const unichar *src_buf_end, char *tgt_buf, char *tgt_buf_end, ...)
{
  unichar char_to_put;
  if ((((wchar_t *)tgt_buf_end) - ((wchar_t *)tgt_buf)) < (src_buf_end-src_buf))
    return (char *)UNICHAR_NO_ROOM;
  while (src_buf < src_buf_end)
    {
      char_to_put = (src_buf++)[0];
      ((wchar_t *)(tgt_buf))[0] = (wchar_t)('?');
      tgt_buf = next_wchar_begin(tgt_buf);
    }
  return tgt_buf;
}


char *eh_encode_wchar_buffer__widewrapper (const wchar_t *src_buf, const wchar_t *src_buf_end, char *tgt_buf, char *tgt_buf_end, ...)
{
  unichar char_to_put;
  if ((((wchar_t *)tgt_buf_end) - ((wchar_t *)tgt_buf)) < (src_buf_end-src_buf))
    return (char *)UNICHAR_NO_ROOM;
  while (src_buf < src_buf_end)
    {
      char_to_put = (src_buf++)[0];
      ((wchar_t *)(tgt_buf))[0] = (wchar_t)('?');
      tgt_buf = next_wchar_begin(tgt_buf);
    }
  return tgt_buf;
}


encoding_handler_t *eh_wide_from_narrow (encoding_handler_t *eh_narrow)
{
  encoding_handler_t *res = NULL;
#ifndef __NO_LIBDK
  static dk_mutex_t *mtx = NULL;
  static dk_hash_t *cache = NULL;
  if (NULL == mtx)
    {
      mtx = mutex_allocate();
      cache = hash_table_allocate(31);
    }
  mutex_enter (mtx);
  res = (encoding_handler_t *) gethash (eh_narrow, cache);
  if (NULL != res)
    goto res_is_prepared;
#endif
  res = (encoding_handler_t *)dk_alloc(sizeof(encoding_handler_t));
  memcpy (res, eh_narrow, sizeof(encoding_handler_t));
  res->eh_minsize *= sizeof (wchar_t);
  res->eh_maxsize *= sizeof (wchar_t);
  res->eh_byteorder = 0;
  res->eh_encodedlangs = NULL;
  res->eh_appdata = eh_narrow;
  res->eh_decode_char = eh_decode_char__widewrapper;
  res->eh_decode_buffer = eh_decode_buffer__widewrapper;
  res->eh_decode_buffer_to_wchar = eh_decode_buffer_to_wchar__widewrapper;
  res->eh_encode_char = eh_encode_char__widewrapper;
  res->eh_encode_buffer = eh_encode_buffer__widewrapper;
  res->eh_encode_wchar_buffer = eh_encode_wchar_buffer__widewrapper;
#ifndef __NO_LIBDK
  sethash (eh_narrow, cache, res);  
res_is_prepared:
  mutex_leave (mtx);
#endif
  return res;
}