1 /***********************************************************************
2  *
3  *      C interface to i18n functions
4  *
5  *
6  ***********************************************************************/
7 
8 /***********************************************************************
9  *
10  * Copyright 2001, 2002, 2004, 2005, 2006 Free Software Foundation, Inc.
11  * Written by Paolo Bonzini.
12  *
13  * This file is part of GNU Smalltalk.
14  *
15  * GNU Smalltalk is free software; you can redistribute it and/or modify it
16  * under the terms of the GNU General Public License as published by the Free
17  * Software Foundation; either version 2, or (at your option) any later
18  * version.
19  *
20  * Linking GNU Smalltalk statically or dynamically with other modules is
21  * making a combined work based on GNU Smalltalk.  Thus, the terms and
22  * conditions of the GNU General Public License cover the whole
23  * combination.
24  *
25  * In addition, as a special exception, the Free Software Foundation
26  * give you permission to combine GNU Smalltalk with free software
27  * programs or libraries that are released under the GNU LGPL and with
28  * independent programs running under the GNU Smalltalk virtual machine.
29  *
30  * You may copy and distribute such a system following the terms of the
31  * GNU GPL for GNU Smalltalk and the licenses of the other code
32  * concerned, provided that you include the source code of that other
33  * code when and as the GNU GPL requires distribution of source code.
34  *
35  * Note that people who make modified versions of GNU Smalltalk are not
36  * obligated to grant this special exception for their modified
37  * versions; it is their choice whether to do so.  The GNU General
38  * Public License gives permission to release a modified version without
39  * this exception; this exception also makes it possible to release a
40  * modified version which carries forward this exception.
41  *
42  * GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
43  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
44  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
45  * more details.
46  *
47  * You should have received a copy of the GNU General Public License along with
48  * GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
49  * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
50  *
51  ***********************************************************************/
52 
53 #include "config.h"
54 #include "gstpub.h"
55 #include <stdio.h>
56 #include <limits.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <iconv.h>
60 #include <errno.h>
61 
62 static VMProxy *vmProxy;
63 
64 mst_Boolean
iconvWrapper(iconv_t handle,OOP readBufferOOP,int readPos,int readCount,OOP writeBufferOOP,int writeCount,OOP bytesLeftOOP)65 iconvWrapper (iconv_t handle, OOP readBufferOOP, int readPos,
66 	      int readCount, OOP writeBufferOOP, int writeCount,
67 	      OOP bytesLeftOOP)
68 {
69   const char *inbuf;
70   size_t inbytesleft;
71   char *outbuf;
72   size_t outbytesleft;
73   int save_errno;
74 
75   gst_object bytesLeft, readBuffer, writeBuffer;
76 
77   readBuffer = OOP_TO_OBJ (readBufferOOP);
78   inbuf = &STRING_OOP_AT (readBuffer, readPos);
79   inbytesleft = readCount;
80 
81   writeBuffer = OOP_TO_OBJ (writeBufferOOP);
82   outbuf = &STRING_OOP_AT (writeBuffer, 1);
83   outbytesleft = writeCount;
84 
85   iconv (handle, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
86   save_errno = errno;
87   errno = 0;
88 
89   bytesLeft = OOP_TO_OBJ (bytesLeftOOP);
90   ARRAY_OOP_AT (bytesLeft, 1) = vmProxy->intToOOP (inbytesleft);
91   ARRAY_OOP_AT (bytesLeft, 2) = vmProxy->intToOOP (outbytesleft);
92   return (save_errno != EILSEQ);
93 }
94 
95 void
gst_initModule(VMProxy * proxy)96 gst_initModule (VMProxy * proxy)
97 {
98   vmProxy = proxy;
99   vmProxy->defineCFunc ("iconv_open", iconv_open);
100   vmProxy->defineCFunc ("iconv_close", iconv_close);
101   vmProxy->defineCFunc ("iconvWrapper", iconvWrapper);
102 }
103