File: iconv-module.c

package info (click to toggle)
slang2 2.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 10,588 kB
  • ctags: 10,558
  • sloc: ansic: 95,506; sh: 3,277; makefile: 945; pascal: 143
file content (265 lines) | stat: -rw-r--r-- 5,572 bytes parent folder | download
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
262
263
264
265
/* This code was written by Dino Leonardo Sangoi <SANGOID@lloydadriatico.it> */

#include "config.h"

#include <stdio.h>
#include <slang.h>
#include <string.h>
#include <iconv.h>
#include <errno.h>

SLANG_MODULE(iconv);

#ifndef ICONV_CONST
# define ICONV_CONST
#endif

static int ICONV_Type_Id = 0;

typedef struct
{
   iconv_t cd;
}
ICONV_Type;

static void free_iconv_type (ICONV_Type *it)
{
   SLfree ((char *) it);
}

static SLang_MMT_Type *allocate_iconv_type (iconv_t cd)
{
   ICONV_Type *it;
   SLang_MMT_Type *mmt;

   it = (ICONV_Type *) SLmalloc (sizeof (ICONV_Type));
   if (it == NULL)
     return NULL;

   it->cd = cd;

   if (NULL == (mmt = SLang_create_mmt (ICONV_Type_Id, (VOID_STAR) it)))
     {
	free_iconv_type (it);
	return NULL;
     }
   return mmt;
}

static void _iconv_open(char *tocode, char *fromcode)
{
   iconv_t cd;
   SLang_MMT_Type *mmt;

   cd = iconv_open(tocode, fromcode);
   if (cd == (iconv_t)(-1))
     {
	SLang_verror (SL_INTRINSIC_ERROR, "Error preparing iconv to convert from '%s' to '%s'.", fromcode, tocode);
	return;
     }

   if (NULL == (mmt = allocate_iconv_type (cd)))
     {
	iconv_close(cd);
	return;
     }

   if (-1 == SLang_push_mmt (mmt))
     {
	SLang_free_mmt (mmt);
	return;
     }
   return;
}

static void _iconv_close(ICONV_Type *it)
{
   if (it->cd != (iconv_t)-1)
     {
	iconv_close(it->cd);
	it->cd = (iconv_t)-1;
     }
}

static void destroy_iconv (SLtype type, VOID_STAR f)
{
   ICONV_Type *it;
   (void) type;

   it = (ICONV_Type *) f;
   _iconv_close(it);
   free_iconv_type (it);
}

static void _iconv_reset(ICONV_Type *it)
{
   iconv(it->cd, NULL, NULL, NULL, NULL);
}

#define SHIFT_BUF_LEN 64U
static void _iconv_reset_shift(ICONV_Type *it)
{
   size_t n = SHIFT_BUF_LEN;
   char buf[SHIFT_BUF_LEN], *p = buf;
   SLang_BString_Type *bstr;
   size_t rc;

   rc = iconv(it->cd, NULL, NULL, &p, &n);
   if ((rc == (size_t)(-1)) || (n > rc))
     {
	SLang_verror (SL_Internal_Error, "Internal error: shift buffer too small in iconv_reset_shift!");
	return;
     }
   buf[SHIFT_BUF_LEN-n] = '\0';
   bstr = SLbstring_create((unsigned char *)buf, SHIFT_BUF_LEN-n);
   if (bstr == NULL)
     return;

   (void) SLang_push_bstring(bstr);
   SLbstring_free (bstr);
}

static void _iconv(ICONV_Type *it, SLang_BString_Type *bstr)
{
   char *buf = NULL;
   size_t rc;
   char ICONV_CONST *instr;
   char *outstr;
   size_t inn, outn, bufn;
   size_t fail = (size_t)-1;
   SLstrlen_Type bstrlen;

   if (NULL == (instr = (char ICONV_CONST *)SLbstring_get_pointer(bstr, &bstrlen)))
     return;
   inn = (size_t) bstrlen;

   bufn = outn = 2*inn + 2;
   if (NULL == (buf = (char *)SLmalloc(bufn)))
     return;

   outstr = buf;

   while (1)
     {
	errno = 0;
	rc = iconv(it->cd, &instr, &inn, &outstr, &outn);
	if (rc != (size_t)-1)
	  break; /* ok! */

	if (fail == inn)
	  {
	     SLang_verror (SL_Unknown_Error, "Unknown error in iconv");
	     goto error;
	  }

	fail = inn;
	switch (errno)
	  {
	   case EILSEQ:
	     SLang_verror (SL_InvalidUTF8_Error, "Invalid multibyte sequence or unable to convert to the target encoding");
	     goto error;
	   case EINVAL:
	     SLang_verror (SL_InvalidUTF8_Error, "Incomplete multibyte sequence");
	     goto error;
	   case 0:
	     /* drop */
	     /* grrrr
	      * At least on windows, libiconv returns with errno = 0
	      * (or unmodified?) when there's no more roon on outstr
	      * if so, fallback
	      */
	   case E2BIG:
	       {
		  char *p;
		  long outdelta;

		  outdelta = outstr - buf;
		  outn += bufn;
		  bufn += bufn;
		  p = (char *)SLrealloc(buf, bufn);
		  if (p == NULL)
		    goto error;
		  buf = p;
		  outstr = buf + outdelta;
	       }
	     break;
	   default:
	     SLang_verror (SL_Unknown_Error, "Unknown iconv error");
	     goto error;
	  }
     }

   bstr = SLbstring_create((unsigned char *) buf, outstr - buf);
   if (bstr != NULL)
     (void) SLang_push_bstring(bstr);
   SLbstring_free (bstr);
   /* drop */
error:
   SLfree(buf);
}

#define DUMMY_ICONV_TYPE 255
#define P DUMMY_ICONV_TYPE
#define V SLANG_VOID_TYPE
#define S SLANG_STRING_TYPE
#define B SLANG_BSTRING_TYPE
static SLang_Intrin_Fun_Type ICONV_Intrinsics [] =
{
   MAKE_INTRINSIC_2("iconv_open", _iconv_open, V, S, S),
   MAKE_INTRINSIC_1("iconv_close", _iconv_close, V, P),
   MAKE_INTRINSIC_1("iconv_reset", _iconv_reset, V, P),
   MAKE_INTRINSIC_1("iconv_reset_shift", _iconv_reset_shift, V, P),
   MAKE_INTRINSIC_2("iconv", _iconv, V, P, B),
   SLANG_END_INTRIN_FUN_TABLE
};

#undef B
#undef S
#undef V
#undef P

static int register_iconv_type (void)
{
   SLang_Class_Type *cl;

   if (ICONV_Type_Id != 0)
     return 0;

   if (NULL == (cl = SLclass_allocate_class ("ICONV_Type")))
     return -1;

   if (-1 == SLclass_set_destroy_function (cl, destroy_iconv))
     return -1;

   /* By registering as SLANG_VOID_TYPE, slang will dynamically allocate a
    * type.
    */
   if (-1 == SLclass_register_class (cl, SLANG_VOID_TYPE, sizeof (ICONV_Type), SLANG_CLASS_TYPE_MMT))
     return -1;

   ICONV_Type_Id = SLclass_get_class_id (cl);
   if (-1 == SLclass_patch_intrin_fun_table1 (ICONV_Intrinsics, DUMMY_ICONV_TYPE, ICONV_Type_Id))
     return -1;

   return 0;
}

int init_iconv_module_ns (char *ns_name)
{
   SLang_NameSpace_Type *ns = SLns_create_namespace (ns_name);
   if (ns == NULL)
     return -1;

   if (-1 == register_iconv_type ())
     return -1;

   if (-1 == SLns_add_intrin_fun_table (ns, ICONV_Intrinsics, "__ICONV__"))
     return -1;

   return 0;
}

/* This function is optional */
void deinit_iconv_module (void)
{
}