SDL  2.0
sys2utf8.c
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
4 
5  This software is provided 'as-is', without any express or implied
6  warranty. In no event will the authors be held liable for any damages
7  arising from the use of this software.
8 
9  Permission is granted to anyone to use this software for any purpose,
10  including commercial applications, and to alter it and redistribute it
11  freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you must not
14  claim that you wrote the original software. If you use this software
15  in a product, an acknowledgment in the product documentation would be
16  appreciated but is not required.
17  2. Altered source versions must be plainly marked as such, and must not be
18  misrepresented as being the original software.
19  3. This notice may not be removed or altered from any source distribution.
20 */
21 
22 #include "geniconv.h"
23 #include <stdlib.h>
24 
25 int StrUTF8(int fToUTF8, char *pcDst, int cbDst, char *pcSrc, int cbSrc)
26 {
27  size_t rc;
28  char *pcDstStart = pcDst;
29  iconv_t cd;
30  char *pszToCP, *pszFromCP;
31  int fError = 0;
32 
33  if (cbDst < 4)
34  return -1;
35 
36  if (fToUTF8) {
37  pszToCP = "UTF-8";
38  pszFromCP = "";
39  } else {
40  pszToCP = "";
41  pszFromCP = "UTF-8";
42  }
43 
44  cd = iconv_open(pszToCP, pszFromCP);
45  if (cd == (iconv_t)-1)
46  return -1;
47 
48  while (cbSrc > 0) {
49  rc = iconv(cd, &pcSrc, (size_t *)&cbSrc, &pcDst, (size_t *)&cbDst);
50  if (rc == (size_t)-1) {
51  if (errno == EILSEQ) {
52  /* Try to skip invalid character */
53  pcSrc++;
54  cbSrc--;
55  continue;
56  }
57 
58  fError = 1;
59  break;
60  }
61  }
62 
63  iconv_close(cd);
64 
65  /* Write trailing ZERO (1 byte for UTF-8, 2 bytes for the system cp) */
66  if (fToUTF8) {
67  if (cbDst < 1) {
68  pcDst--;
69  fError = 1; /* The destination buffer overflow */
70  }
71  *pcDst = '\0';
72  } else {
73  if (cbDst < 2) {
74  pcDst -= (cbDst == 0)? 2 : 1;
75  fError = 1; /* The destination buffer overflow */
76  }
77  *((short *)pcDst) = '\0';
78  }
79 
80  return (fError) ? -1 : (pcDst - pcDstStart);
81 }
82 
83 char *StrUTF8New(int fToUTF8, char *pcStr, int cbStr)
84 {
85  int cbNewStr = (((cbStr > 4)? cbStr : 4) + 1) * 2;
86  char *pszNewStr = (char *) malloc(cbNewStr);
87 
88  if (pszNewStr == NULL)
89  return NULL;
90 
91  cbNewStr = StrUTF8(fToUTF8, pszNewStr, cbNewStr, pcStr, cbStr);
92  if (cbNewStr != -1) {
93  pcStr = (char *) realloc(pszNewStr, cbNewStr + ((fToUTF8)? 1 : sizeof(short)));
94  if (pcStr)
95  return pcStr;
96  }
97 
98  free(pszNewStr);
99  return NULL;
100 }
101 
102 void StrUTF8Free(char *pszStr)
103 {
104  free(pszStr);
105 }
106 
107 /* vi: set ts=4 sw=4 expandtab: */
SDL_EventEntry * free
Definition: SDL_events.c:89
#define malloc
Definition: SDL_qsort.c:46
#define NULL
Definition: begin_code.h:163
void * iconv_t
Definition: iconv.h:7
int iconv_close(iconv_t)
iconv_t iconv_open(const char *, const char *)
size_t iconv(iconv_t, char **, size_t *, char **, size_t *)
void StrUTF8Free(char *pszStr)
Definition: sys2utf8.c:102
char * StrUTF8New(int fToUTF8, char *pcStr, int cbStr)
Definition: sys2utf8.c:83
int StrUTF8(int fToUTF8, char *pcDst, int cbDst, char *pcSrc, int cbSrc)
Definition: sys2utf8.c:25