SDL  2.0
os2iconv.c File Reference
#include "geniconv.h"
#include <uconv.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <os2.h>
#include "os2cp.h"
+ Include dependency graph for os2iconv.c:

Go to the source code of this file.

Data Structures

struct  iuconv_obj
 

Macros

#define ICONV_THREAD_SAFE   1
 
#define _ULS_CALLCONV_
 
#define CALLCONV   _System
 
#define INCL_DOSSEMAPHORES
 
#define INCL_DOSERRORS
 
#define min(a, b)   (((a) < (b)) ? (a) : (b))
 
#define MAX_CP_NAME_LEN   64
 

Functions

static int _createUconvObj (const char *code, UconvObject *uobj)
 
static int uconv_open (const char *code, UconvObject *uobj)
 
iconv_t _System os2_iconv_open (const char *tocode, const char *fromcode)
 
size_t _System os2_iconv (iconv_t cd, char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
 
int _System os2_iconv_close (iconv_t cd)
 

Macro Definition Documentation

◆ _ULS_CALLCONV_

#define _ULS_CALLCONV_

Definition at line 31 of file os2iconv.c.

◆ CALLCONV

#define CALLCONV   _System

Definition at line 32 of file os2iconv.c.

◆ ICONV_THREAD_SAFE

#define ICONV_THREAD_SAFE   1

Definition at line 28 of file os2iconv.c.

◆ INCL_DOSERRORS

#define INCL_DOSERRORS

Definition at line 39 of file os2iconv.c.

◆ INCL_DOSSEMAPHORES

#define INCL_DOSSEMAPHORES

Definition at line 38 of file os2iconv.c.

◆ MAX_CP_NAME_LEN

#define MAX_CP_NAME_LEN   64

Definition at line 48 of file os2iconv.c.

◆ min

#define min (   a,
  b 
)    (((a) < (b)) ? (a) : (b))

Definition at line 45 of file os2iconv.c.

Function Documentation

◆ _createUconvObj()

static int _createUconvObj ( const char *  code,
UconvObject *  uobj 
)
static

Definition at line 61 of file os2iconv.c.

62 {
63  UniChar uc_code[MAX_CP_NAME_LEN];
64  int i;
65  const char *ch = code;
66 
67  if (code == NULL)
68  uc_code[0] = 0;
69  else {
70  for (i = 0; i < MAX_CP_NAME_LEN; i++) {
71  uc_code[i] = (unsigned short)*ch;
72  if (! (*ch))
73  break;
74  ch++;
75  }
76  }
77 
78  return UniCreateUconvObject(uc_code, uobj);
79 }
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
#define NULL
Definition: begin_code.h:163
#define MAX_CP_NAME_LEN
Definition: os2iconv.c:48

References i, MAX_CP_NAME_LEN, and NULL.

Referenced by uconv_open().

◆ os2_iconv()

size_t _System os2_iconv ( iconv_t  cd,
char **  inbuf,
size_t inbytesleft,
char **  outbuf,
size_t outbytesleft 
)

Definition at line 146 of file os2iconv.c.

149 {
150  UconvObject uo_tocode = ((iuconv_obj *)(cd))->uo_tocode;
151  UconvObject uo_fromcode = ((iuconv_obj *)(cd))->uo_fromcode;
152  size_t nonIdenticalConv = 0;
153  UniChar *uc_buf;
154  size_t uc_buf_len;
155  UniChar **uc_str;
156  size_t *uc_str_len;
157  int rc;
158  size_t ret = (size_t)(-1);
159 
160  if (uo_tocode == NULL && uo_fromcode == NULL) {
161  uc_buf_len = min(*inbytesleft, *outbytesleft);
162  memcpy(*outbuf, *inbuf, uc_buf_len);
163  *inbytesleft -= uc_buf_len;
164  *outbytesleft -= uc_buf_len;
165  outbuf += uc_buf_len;
166  inbuf += uc_buf_len;
167  return uc_buf_len;
168  }
169 
170 #ifdef ICONV_THREAD_SAFE
171  DosRequestMutexSem(((iuconv_obj *)(cd))->hMtx, SEM_INDEFINITE_WAIT);
172 #endif
173 
174  if (uo_tocode && uo_fromcode &&
175  (((iuconv_obj *)cd)->buf_len >> 1) < *inbytesleft) {
176  if (((iuconv_obj *)cd)->buf != NULL)
177  free(((iuconv_obj *)cd)->buf);
178  ((iuconv_obj *)cd)->buf_len = *inbytesleft << 1;
179  ((iuconv_obj *)cd)->buf = (UniChar *)malloc(((iuconv_obj *)cd)->buf_len);
180  }
181 
182  if (uo_fromcode) {
183  if (uo_tocode) {
184  uc_buf = ((iuconv_obj *)cd)->buf;
185  uc_buf_len = ((iuconv_obj *)cd)->buf_len;
186  uc_str = &uc_buf;
187  } else {
188  uc_str = (UniChar **)outbuf;
189  uc_buf_len = *outbytesleft;
190  }
191  uc_buf_len = uc_buf_len >> 1;
192  uc_str_len = &uc_buf_len;
193  rc = UniUconvToUcs(uo_fromcode, (void **)inbuf, inbytesleft,
194  uc_str, uc_str_len, &nonIdenticalConv);
195  uc_buf_len = uc_buf_len << 1;
196  if (!uo_tocode)
197  *outbytesleft = uc_buf_len;
198 
199  if (rc != ULS_SUCCESS) {
200  errno = EILSEQ;
201  goto done;
202  } else if (*inbytesleft && !*uc_str_len) {
203  errno = E2BIG;
204  goto done;
205  }
206 
207  if (!uo_tocode)
208  return nonIdenticalConv;
209 
210  uc_buf = ((iuconv_obj *)cd)->buf;
211  uc_buf_len = ((iuconv_obj *)cd)->buf_len - uc_buf_len;
212  uc_str = &uc_buf;
213  uc_str_len = &uc_buf_len;
214  } else {
215  uc_str = (UniChar **)inbuf;
216  uc_str_len = inbytesleft;
217  }
218 
219  *uc_str_len = *uc_str_len>>1;
220  rc = UniUconvFromUcs(uo_tocode, uc_str, uc_str_len, (void **)outbuf,
221  outbytesleft, &nonIdenticalConv);
222  if (rc != ULS_SUCCESS) {
223  switch (rc) {
224  case ULS_BUFFERFULL:
225  errno = E2BIG;
226  break;
227  case ULS_ILLEGALSEQUENCE:
228  errno = EILSEQ;
229  break;
230  case ULS_INVALID:
231  errno = EINVAL;
232  break;
233  }
234  goto done;
235  } else if (*uc_str_len && !*outbytesleft) {
236  errno = E2BIG;
237  goto done;
238  }
239 
240  ret = nonIdenticalConv;
241 
242 done:
243 
244 #ifdef ICONV_THREAD_SAFE
245  DosReleaseMutexSem(((iuconv_obj *)cd)->hMtx);
246 #endif
247  return ret;
248 }
unsigned int size_t
SDL_EventEntry * free
Definition: SDL_events.c:89
#define memcpy
Definition: SDL_malloc.c:630
GLenum GLuint GLenum GLsizei const GLchar * buf
#define malloc
Definition: SDL_qsort.c:46
int done
Definition: checkkeys.c:28
#define min(a, b)
Definition: os2iconv.c:45

References done, free, malloc, memcpy, min, and NULL.

Referenced by _init().

◆ os2_iconv_close()

int _System os2_iconv_close ( iconv_t  cd)

Definition at line 250 of file os2iconv.c.

251 {
252  if (!cd) return 0;
253 
254 #ifdef ICONV_THREAD_SAFE
255  DosCloseMutexSem(((iuconv_obj *)cd)->hMtx);
256 #endif
257  if (((iuconv_obj *)cd)->uo_tocode != NULL)
258  UniFreeUconvObject(((iuconv_obj *)cd)->uo_tocode);
259  if (((iuconv_obj *)cd)->uo_fromcode != NULL)
260  UniFreeUconvObject(((iuconv_obj *)cd)->uo_fromcode);
261 
262  if (((iuconv_obj *)cd)->buf != NULL)
263  free(((iuconv_obj *)cd)->buf);
264 
265  free(cd);
266 
267  return 0;
268 }

References free, and NULL.

Referenced by _init().

◆ os2_iconv_open()

iconv_t _System os2_iconv_open ( const char *  tocode,
const char *  fromcode 
)

Definition at line 103 of file os2iconv.c.

104 {
105  UconvObject uo_tocode;
106  UconvObject uo_fromcode;
107  int rc;
108  iuconv_obj *iuobj;
109 
110  if (tocode == NULL)
111  tocode = "";
112 
113  if (fromcode == NULL)
114  fromcode = "";
115 
116  if (stricmp(tocode, fromcode) != 0) {
117  rc = uconv_open(fromcode, &uo_fromcode);
118  if (rc != ULS_SUCCESS) {
119  errno = EINVAL;
120  return (iconv_t)(-1);
121  }
122 
123  rc = uconv_open(tocode, &uo_tocode);
124  if (rc != ULS_SUCCESS) {
125  UniFreeUconvObject(uo_fromcode);
126  errno = EINVAL;
127  return (iconv_t)(-1);
128  }
129  } else {
130  uo_tocode = NULL;
131  uo_fromcode = NULL;
132  }
133 
134  iuobj = (iuconv_obj *) malloc(sizeof(iuconv_obj));
135  iuobj->uo_tocode = uo_tocode;
136  iuobj->uo_fromcode = uo_fromcode;
137  iuobj->buf_len = 0;
138  iuobj->buf = NULL;
139 #ifdef ICONV_THREAD_SAFE
140  DosCreateMutexSem(NULL, &iuobj->hMtx, 0, FALSE);
141 #endif
142 
143  return iuobj;
144 }
#define FALSE
Definition: edid-parse.c:34
void * iconv_t
Definition: iconv.h:7
static int uconv_open(const char *code, UconvObject *uobj)
Definition: os2iconv.c:81
int buf_len
Definition: os2iconv.c:53
HMTX hMtx
Definition: os2iconv.c:56
UniChar * buf
Definition: os2iconv.c:54
UconvObject uo_tocode
Definition: os2iconv.c:51
UconvObject uo_fromcode
Definition: os2iconv.c:52

References iuconv_obj::buf, iuconv_obj::buf_len, FALSE, iuconv_obj::hMtx, malloc, NULL, uconv_open(), iuconv_obj::uo_fromcode, and iuconv_obj::uo_tocode.

Referenced by _init().

◆ uconv_open()

static int uconv_open ( const char *  code,
UconvObject *  uobj 
)
static

Definition at line 81 of file os2iconv.c.

82 {
83  int rc;
84 
85  if (!stricmp(code, "UTF-16")) {
86  *uobj = NULL;
87  return ULS_SUCCESS;
88  }
89 
90  rc = _createUconvObj(code, uobj);
91  if (rc != ULS_SUCCESS) {
92  unsigned long cp = os2cpFromName((char *)code);
93  char cp_name[16];
94 
95  if (cp != 0 && _snprintf(cp_name, sizeof(cp_name), "IBM-%u", cp) > 0)
96  rc = _createUconvObj(cp_name, uobj);
97  }
98 
99  return rc;
100 }
static const double cp
Definition: e_pow.c:96
unsigned long os2cpFromName(char *cp)
Definition: os2cp.c:340
static int _createUconvObj(const char *code, UconvObject *uobj)
Definition: os2iconv.c:61

References _createUconvObj(), cp, NULL, and os2cpFromName().

Referenced by os2_iconv_open().