summaryrefslogtreecommitdiff
path: root/libguile/mbconv.c
blob: 09b3f34f12eb8ec6a22fc5e1749ebd4c2f725aa1 (plain)
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*	Copyright (C) 1999 Free Software Foundation, Inc.
 * 
 * This program 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; either version 2, or (at your option)
 * any later version.
 * 
 * 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 software; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307 USA
 *
 * As a special exception, the Free Software Foundation gives permission
 * for additional uses of the text contained in its release of GUILE.
 *
 * The exception is that, if you link the GUILE library with other files
 * to produce an executable, this does not by itself cause the
 * resulting executable to be covered by the GNU General Public License.
 * Your use of that executable is in no way restricted on account of
 * linking the GUILE library code into it.
 *
 * This exception does not however invalidate any other reasons why
 * the executable file might be covered by the GNU General Public License.
 *
 * This exception applies only to the code released by the
 * Free Software Foundation under the name GUILE.  If you copy
 * code from other Free Software Foundation releases into a copy of
 * GUILE, as the General Public License permits, the exception does
 * not apply to the code that you add in this way.  To avoid misleading
 * anyone as to the status of such modified files, you must delete
 * this exception notice from them.
 *
 * If you write modifications of your own for GUILE, it is your choice
 * whether to permit this exception to apply to your modifications.
 * If you do not wish that, delete this exception notice.
 */

/* Much code here is borrowed from Tom Tromey's libunicode --- thanks,
   Tom!  Unfortunately, I can't simply use libunicode itself at the
   moment, since our canonical internal encoding is different.  When
   Emacs and Guile switch to UTF-8, hopefully that will change.  */


/* Headers.  */

#include "_scm.h"

#include <stddef.h>
#include <stdlib.h>
#include <string.h>

#include "mbconv.h"

#ifdef HAVE_ICONV
#  include <iconv.h>
#else
/* This is harmless as it is completely ignored by the code when the
   above condition is false.  */
typedef void *iconv_t;
#endif


/* Exceptions.  */

SCM_GLOBAL_SYMBOL (scm_text_unknown_encoding, "text:unknown-encoding");
static const char text_unknown_encoding_msg[] =
  "unable to convert from encoding `%s' to encoding `%s'";


/* The implementation of the conversion context object.  */

struct scm_mb_iconv_ours
{
  /* Converter from source character set to Guile character set.  */
  struct scm_mb_encoding *from;

  /* Converter private data.  */
  void *from_data;

  /* Converter from Guile character set to destination character set.  */
  struct scm_mb_encoding *to;

  /* Converter private data.  */
  void *to_data;

  /* Buffer used to store intermediate results.  */
  scm_char_t *buffer;

  /* Number of valid characters in buffer.  */
  size_t valid;

  /* Total number of character slots in buffer.  */
  size_t size;
};

struct scm_mb_iconv
{
  /* Type of converter.  */
  enum {
    scm_mb_iconv_type_native,
    scm_mb_iconv_type_ours
  } type;

  union
  {
    iconv_t native;		   /* Used in `native' case.  */
    struct scm_mb_iconv_ours ours; /* Used in `our' case.  */
  } u;
};


/* The registry of all encodings.  */

/* Linked list of all character sets.  */
static struct scm_mb_encoding *encodings;

void
scm_mb_register_encoding (struct scm_mb_encoding *vec)
{
  vec->next = encodings;
  encodings = vec;
}

static struct scm_mb_encoding *
find_encoding (const char *name)
{
  struct scm_mb_encoding *cs;

  for (cs = encodings; cs; cs = cs->next)
    {
      int i;
      for (i = 0; cs->names[i]; ++i)
	{
	  if (! strcasecmp (cs->names[i], name))
	    return cs;
	}
    }

  return cs;
}


/* Creating and destroying conversion contexts.  */

static void
no_memory_for_open (struct scm_mb_iconv *r)
{
  if (r)
    {
      if (r->u.ours.buffer) free (r->u.ours.buffer);
      free (r);
    }

  scm_memory_error ("scm_mb_iconv_open");
}

struct scm_mb_iconv *
scm_mb_iconv_open (const char *tocode, const char *fromcode)
{
  struct scm_mb_iconv *r = (struct scm_mb_iconv *) malloc (sizeof (*r));

  if (! r)
    no_memory_for_open (r);

  r->u.ours.buffer = 0;

  /* Try our encodings first.  */
  r->u.ours.from = find_encoding (fromcode);
  if (r->u.ours.from)
    r->u.ours.to = find_encoding (tocode);
  if (r->u.ours.from && r->u.ours.to)
    {
      r->type = scm_mb_iconv_type_ours;

      /* FIXME: how to pick the size?  */
      r->u.ours.size = 1024;
      r->u.ours.buffer = (scm_char_t *) malloc (r->u.ours.size
						* sizeof (scm_char_t));
      if (! r->u.ours.buffer)
	no_memory_for_open (r);

      r->u.ours.valid = 0;

      if (r->u.ours.from->init
	  && ! r->u.ours.from->init (&r->u.ours.from_data))
	no_memory_for_open (r);

      if (r->u.ours.to->init && ! r->u.ours.to->init (&r->u.ours.to_data))
	{
	  if (r->u.ours.from->destroy)
	    r->u.ours.from->destroy (&r->u.ours.from_data);
	  no_memory_for_open (r);
	}

      return r;
    }

#ifdef HAVE_ICONV
  /* We don't have the conversions the user requested.  Try the
     system's iconv library.  */
  r->u.native = iconv_open (tocode, fromcode);
  if (r->u.native != (iconv_t) -1)
    {
      r->type = scm_mb_iconv_type_native;
      return r;
    }
#endif

  free (r);

  {
    SCM args = SCM_LIST2 (scm_makfrom0str (fromcode),
			  scm_makfrom0str (tocode));

    scm_error (scm_text_unknown_encoding, "scm_mb_iconv_open",
	       text_unknown_encoding_msg, args, args);
  }

  return 0;
}

void
scm_mb_iconv_close (struct scm_mb_iconv *cd)
{
  switch (cd->type)
    {
    case scm_mb_iconv_type_native:
#ifdef HAVE_ICONV
      if (iconv_close (cd->u.native) < 0)
	scm_syserror ("scm_mb_iconv_close");
      break;
#else
      abort ();
#endif

    case scm_mb_iconv_type_ours:
      if (cd->u.ours.to->destroy)
	cd->u.ours.to->destroy (&cd->u.ours.to_data);
      if (cd->u.ours.from->destroy)
	cd->u.ours.from->destroy (&cd->u.ours.from_data);
      free (cd->u.ours.buffer);
      break;
    }

  free (cd);
}


/* Actually converting text.  */

size_t
scm_mb_iconv (struct scm_mb_iconv *context,
	      const char **inbuf,  size_t *inbytesleft,
	      char **outbuf,       size_t *outbytesleft)
{
  struct scm_mb_iconv_ours *ours;

#ifdef HAVE_ICONV
  if (context->type == scm_mb_iconv_type_native)
    {
      size_t result = iconv (context->u.native,
			     inbuf, inbytesleft,
			     outbuf, outbytesleft);
      if (result == (size_t) -1)
	switch (errno)
	  {
	  case EILSEQ: return scm_mb_iconv_bad_encoding;
	  case E2BIG:  return scm_mb_iconv_more_room;
	  case EINVAL: return scm_mb_iconv_incomplete_encoding;
	  default:
	    scm_syserror ("scm_mb_iconv");
	  }
      else
	return result;
    }
#endif /* HAVE_ICONV */

  ours = &context->u.ours;
  
  /* If inbuf or *inbuf is zero, then we need to reset the conversion
     states.  */
  if (! inbuf || ! *inbuf)
    {
      int result;

      /* Try to reset the output conversion first; if it fails, we need to
	 leave the input conversion state untouched.  */
      result = ours->to->reset (ours->to_data, outbuf, outbytesleft);
      if (result < 0)
	return result;

      /* Reset the input context.  */
      return ours->from->reset (ours->from_data, 0, 0);
    }

  if (! outbuf || *outbytesleft <= 0)
    return scm_mb_iconv_more_room;

  /* Oh!  We actually have some *TEXT* to convert!  Not bureaucracy!!!  */
  while (*inbytesleft > 0)
    {
      /* Convert as many characters as possible from the input buffer
	 into the intermediate scm_char_t buffer.  */
      {
	scm_char_t *buf = ours->buffer + ours->valid;
	size_t buf_left = ours->size - ours->valid;
	enum scm_mb_read_result read_result
	  = ours->from->read (ours->from_data, inbuf, inbytesleft,
			      &buf, &buf_left);
	ours->valid = ours->size - buf_left;

	if (*inbytesleft < 0)
	  abort ();

	switch (read_result)
	  {
	  case scm_mb_read_ok:
	    break;
	  case scm_mb_read_incomplete:
	    return scm_mb_iconv_incomplete_encoding;
	  case scm_mb_read_error:
	    return scm_mb_iconv_bad_encoding;
	  default:
	    abort ();
	  }
      }

      /* Convert as many characters as possible from the intermediate
         scm_char_t buffer to the output buffer.  */
      {
	scm_char_t *buf = ours->buffer;
	size_t buf_left = ours->valid;
	enum scm_mb_write_result write_result
	  = ours->to->write (ours->to_data, &buf, &buf_left,
			     outbuf, outbytesleft);
	if (buf_left > 0)
	  memmove (ours->buffer, buf, buf_left * sizeof (scm_char_t));
	ours->valid = buf_left;
	switch (write_result)
	  {
	  case scm_mb_write_ok:
	    break;
	  case scm_mb_write_more_room:
	    return scm_mb_iconv_more_room;
	  default:
	    abort ();
	  }
      }
    }
    
  return 0;
}

#if 0

/* Input conversion ports.  */

/* Given that PORT is an input port containing data in the encoding
   named ENCODING, return a new input port carrying the same data as
   PORT, converted into Guile's internal encoding.  The resulting input
   port is not seekable.  */
SCM_PROC (s_convert_input_port, "convert-input-port", 2, 0, 0, scm_convert_input_port);
SCM
scm_convert_input_port (SCM port, SCM encoding)
{
  
}


/* Output conversion ports.  */
/* Return a new port which accepts data in Guile's internal encoding,
   and sends it to PORT in ENCODING.  PORT must be an output port, and
   ENCODING must be the name of an encoding.  */
SCM
scm_convert_output_port (SCM port, SCM encoding)
{
  
}
#endif


/* Initialization.  */

void
scm_init_mbconv ()
{
#include "mbconv.x"
}