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
/*
Simple DirectMedia Layer
Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
/*
Implementation iconv via OS/2 conversion objects API.
Andrey Vasilkin.
*/
#define ICONV_THREAD_SAFE 1
#include "geniconv.h"
#define _ULS_CALLCONV_
#define CALLCONV _System
#include <uconv.h>
#ifdef ICONV_THREAD_SAFE
#define INCL_DOSSEMAPHORES
#define INCL_DOSERRORS
#include <os2.h>
#endif
#include "os2cp.h"
#ifndef GENICONV_STANDALONE
#include "../../../SDL_internal.h"
#else
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if !defined(min)
#define min(a, b) (((a) < (b)) ? (a) : (b))
#endif
#define SDL_min min
#define SDL_strcasecmp stricmp
#define SDL_snprintf _snprintf
#define SDL_malloc malloc
#define SDL_free free
#define SDL_memcpy memcpy
#endif
#define MAX_CP_NAME_LEN 64
typedef struct iuconv_obj {
UconvObject uo_tocode;
UconvObject uo_fromcode;
int buf_len;
UniChar *buf;
#ifdef ICONV_THREAD_SAFE
HMTX hMtx;
#endif
} iuconv_obj;
static int _createUconvObj(const char *code, UconvObject *uobj)
{
UniChar uc_code[MAX_CP_NAME_LEN];
int i;
const unsigned char *ch =
(const unsigned char *)code;
if (code == NULL)
uc_code[0] = 0;
else {
for (i = 0; i < MAX_CP_NAME_LEN; i++) {
uc_code[i] = (unsigned short)*ch;
if (! (*ch))
break;
ch++;
}
}
return UniCreateUconvObject(uc_code, uobj);
}
static int uconv_open(const char *code, UconvObject *uobj)
{
int rc;
if (!SDL_strcasecmp(code, "UTF-16")) {
*uobj = NULL;
return ULS_SUCCESS;
}
rc = _createUconvObj(code, uobj);
if (rc != ULS_SUCCESS) {
unsigned long cp = os2cpFromName((char *)code);
char cp_name[16];
if (cp != 0 && SDL_snprintf(cp_name, sizeof(cp_name), "IBM-%u", cp) > 0) {
rc = _createUconvObj(cp_name, uobj);
}
}
return rc;
}
iconv_t _System os2_iconv_open(const char* tocode, const char* fromcode)
{
UconvObject uo_tocode;
UconvObject uo_fromcode;
int rc;
iuconv_obj *iuobj;
if (tocode == NULL) {
tocode = "";
}
if (fromcode == NULL) {
fromcode = "";
}
if (SDL_strcasecmp(tocode, fromcode) != 0) {
rc = uconv_open(fromcode, &uo_fromcode);
if (rc != ULS_SUCCESS) {
errno = EINVAL;
return (iconv_t)(-1);
}
rc = uconv_open(tocode, &uo_tocode);
if (rc != ULS_SUCCESS) {
UniFreeUconvObject(uo_fromcode);
errno = EINVAL;
return (iconv_t)(-1);
}
} else {
uo_tocode = NULL;
uo_fromcode = NULL;
}
iuobj = (iuconv_obj *) SDL_malloc(sizeof(iuconv_obj));
iuobj->uo_tocode = uo_tocode;
iuobj->uo_fromcode = uo_fromcode;
iuobj->buf_len = 0;
iuobj->buf = NULL;
#ifdef ICONV_THREAD_SAFE
DosCreateMutexSem(NULL, &iuobj->hMtx, 0, FALSE);
#endif
return iuobj;
}
size_t _System os2_iconv(iconv_t cd,
char **inbuf, size_t *inbytesleft ,
char **outbuf, size_t *outbytesleft)
{
UconvObject uo_tocode = ((iuconv_obj *)(cd))->uo_tocode;
UconvObject uo_fromcode = ((iuconv_obj *)(cd))->uo_fromcode;
size_t nonIdenticalConv = 0;
UniChar *uc_buf;
size_t uc_buf_len;
UniChar **uc_str;
size_t *uc_str_len;
int rc;
size_t ret = (size_t)(-1);
if (uo_tocode == NULL && uo_fromcode == NULL) {
uc_buf_len = SDL_min(*inbytesleft, *outbytesleft);
SDL_memcpy(*outbuf, *inbuf, uc_buf_len);
*inbytesleft -= uc_buf_len;
*outbytesleft -= uc_buf_len;
outbuf += uc_buf_len;
inbuf += uc_buf_len;
return uc_buf_len;
}
#ifdef ICONV_THREAD_SAFE
DosRequestMutexSem(((iuconv_obj *)(cd))->hMtx, SEM_INDEFINITE_WAIT);
#endif
if (uo_tocode && uo_fromcode && (((iuconv_obj *)cd)->buf_len >> 1) < *inbytesleft) {
if (((iuconv_obj *)cd)->buf != NULL) {
SDL_free(((iuconv_obj *)cd)->buf);
}
((iuconv_obj *)cd)->buf_len = *inbytesleft << 1;
((iuconv_obj *)cd)->buf = (UniChar *) SDL_malloc(((iuconv_obj *)cd)->buf_len);
}
if (uo_fromcode) {
if (uo_tocode) {
uc_buf = ((iuconv_obj *)cd)->buf;
uc_buf_len = ((iuconv_obj *)cd)->buf_len;
uc_str = &uc_buf;
} else {
uc_str = (UniChar **)outbuf;
uc_buf_len = *outbytesleft;
}
uc_buf_len = uc_buf_len >> 1;
uc_str_len = &uc_buf_len;
rc = UniUconvToUcs(uo_fromcode, (void **)inbuf, inbytesleft,
uc_str, uc_str_len, &nonIdenticalConv);
uc_buf_len = uc_buf_len << 1;
if (!uo_tocode) {
*outbytesleft = uc_buf_len;
}
if (rc != ULS_SUCCESS) {
errno = EILSEQ;
goto done;
} else if (*inbytesleft && !*uc_str_len) {
errno = E2BIG;
goto done;
}
if (!uo_tocode) {
return nonIdenticalConv;
}
uc_buf = ((iuconv_obj *)cd)->buf;
uc_buf_len = ((iuconv_obj *)cd)->buf_len - uc_buf_len;
uc_str = &uc_buf;
uc_str_len = &uc_buf_len;
} else {
uc_str = (UniChar **)inbuf;
uc_str_len = inbytesleft;
}
*uc_str_len = *uc_str_len>>1;
rc = UniUconvFromUcs(uo_tocode, uc_str, uc_str_len, (void **)outbuf,
outbytesleft, &nonIdenticalConv);
if (rc != ULS_SUCCESS) {
switch (rc) {
case ULS_BUFFERFULL:
errno = E2BIG;
break;
case ULS_ILLEGALSEQUENCE:
errno = EILSEQ;
break;
case ULS_INVALID:
errno = EINVAL;
break;
}
goto done;
} else if (*uc_str_len && !*outbytesleft) {
errno = E2BIG;
goto done;
}
ret = nonIdenticalConv;
done:
#ifdef ICONV_THREAD_SAFE
DosReleaseMutexSem(((iuconv_obj *)cd)->hMtx);
#endif
return ret;
}
int _System os2_iconv_close(iconv_t cd)
{
if (!cd) return 0;
#ifdef ICONV_THREAD_SAFE
DosCloseMutexSem(((iuconv_obj *)cd)->hMtx);
#endif
if (((iuconv_obj *)cd)->uo_tocode != NULL) {
UniFreeUconvObject(((iuconv_obj *)cd)->uo_tocode);
}
if (((iuconv_obj *)cd)->uo_fromcode != NULL) {
UniFreeUconvObject(((iuconv_obj *)cd)->uo_fromcode);
}
if (((iuconv_obj *)cd)->buf != NULL) {
SDL_free(((iuconv_obj *)cd)->buf);
}
SDL_free(cd);
return 0;
}
/* vi: set ts=4 sw=4 expandtab: */