OpenVAS Scanner 23.40.3
iconv.c
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Greenbone AG
2 * SPDX-FileCopyrightText: 2002-2003 Jelmer Vernooij
3 * SPDX-FileCopyrightText: 2001 Andrew Tridgell
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 */
7
12
13#include "iconv.h"
14
15#include "charset.h"
16#include "proto.h"
17#include "smb.h"
18
19#include <glib.h>
20
21typedef unsigned int bool;
22
23static size_t
24iconv_copy_ntlmssp (void *, const char **, size_t *, char **, size_t *);
25
26static struct charset_functions_ntlmssp *charsets = NULL;
27
28static struct charset_functions_ntlmssp *
30{
32
33 while (c)
34 {
35 if (strcasecmp (name, c->name) == 0)
36 {
37 return c;
38 }
39 c = c->next;
40 }
41
42 return NULL;
43}
44
51
52size_t
53smb_iconv_ntlmssp (smb_iconv_t cd, const char **inbuf, size_t *inbytesleft,
54 char **outbuf, size_t *outbytesleft)
55{
56 char cvtbuf[2048];
57 char *bufp = cvtbuf;
58 size_t bufsize;
59
60 /* in many cases we can go direct */
61 if (cd->direct)
62 {
63 return cd->direct (cd->cd_direct, inbuf, inbytesleft, outbuf,
64 outbytesleft);
65 }
66
67 /* otherwise we have to do it chunks at a time */
68 while (*inbytesleft > 0)
69 {
70 bufp = cvtbuf;
71 bufsize = sizeof (cvtbuf);
72
73 if (cd->pull (cd->cd_pull, inbuf, inbytesleft, &bufp, &bufsize)
74 == (size_t) -1
75 && errno != E2BIG)
76 return -1;
77
78 bufp = cvtbuf;
79 bufsize = sizeof (cvtbuf) - bufsize;
80
81 if (cd->push (cd->cd_push, (const char **) &bufp, &bufsize, outbuf,
82 outbytesleft)
83 == (size_t) -1)
84 return -1;
85 }
86
87 return 0;
88}
89
90static bool
92{
93 return strcasecmp (name, "UCS-2LE") == 0
94 || strcasecmp (name, "UTF-16LE") == 0;
95}
96
97/*
98 simple iconv_open() wrapper
99 */
101smb_iconv_open_ntlmssp (const char *tocode, const char *fromcode)
102{
103 smb_iconv_t ret;
104 struct charset_functions_ntlmssp *from, *to;
105
106 ret = SMB_MALLOC_P (struct _smb_iconv_t);
107 if (!ret)
108 {
109 errno = ENOMEM;
110 return (smb_iconv_t) -1;
111 }
112 memset (ret, 0, sizeof (struct _smb_iconv_t));
113
114 ret->from_name = SMB_STRDUP (fromcode);
115 ret->to_name = SMB_STRDUP (tocode);
116
117 /* check for the simplest null conversion */
118 if (strcasecmp (fromcode, tocode) == 0)
119 {
121 return ret;
122 }
123
124 /* check if we have a builtin function for this conversion */
125 from = find_charset_functions_ntlmssp (fromcode);
126 if (from)
127 ret->pull = from->pull;
128
129 to = find_charset_functions_ntlmssp (tocode);
130 if (to)
131 ret->push = to->push;
132
133 /* check if we can use iconv for this conversion */
134#ifdef HAVE_NATIVE_ICONV
135 if (!ret->pull)
136 {
137 ret->cd_pull = iconv_open ("UTF-16LE", fromcode);
138 if (ret->cd_pull == (iconv_t) -1)
139 ret->cd_pull = iconv_open ("UCS-2LE", fromcode);
140 if (ret->cd_pull != (iconv_t) -1)
141 ret->pull = sys_iconv;
142 }
143
144 if (!ret->push)
145 {
146 ret->cd_push = iconv_open (tocode, "UTF-16LE");
147 if (ret->cd_push == (iconv_t) -1)
148 ret->cd_push = iconv_open (tocode, "UCS-2LE");
149 if (ret->cd_push != (iconv_t) -1)
150 ret->push = sys_iconv;
151 }
152#endif
153
154 if (!ret->push || !ret->pull)
155 {
156 g_free (ret->from_name);
157 g_free (ret->to_name);
158 g_free (ret);
159 errno = EINVAL;
160 return (smb_iconv_t) -1;
161 }
162
163 /* check for conversion to/from ucs2 */
164 if (is_utf16_ntlmssp (fromcode) && to)
165 {
166 ret->direct = to->push;
167 ret->push = ret->pull = NULL;
168 return ret;
169 }
170
171 if (is_utf16_ntlmssp (tocode) && from)
172 {
173 ret->direct = from->pull;
174 ret->push = ret->pull = NULL;
175 return ret;
176 }
177
178 /* Check if we can do the conversion direct */
179#ifdef HAVE_NATIVE_ICONV
180 if (is_utf16 (fromcode))
181 {
182 ret->direct = sys_iconv;
183 ret->cd_direct = ret->cd_push;
184 ret->cd_push = NULL;
185 return ret;
186 }
187 if (is_utf16 (tocode))
188 {
189 ret->direct = sys_iconv;
190 ret->cd_direct = ret->cd_pull;
191 ret->cd_pull = NULL;
192 return ret;
193 }
194#endif
195
196 return ret;
197}
198
199/*
200 simple iconv_close() wrapper
201*/
202int
204{
205#ifdef HAVE_NATIVE_ICONV
206 if (cd->cd_direct)
207 iconv_close ((iconv_t) cd->cd_direct);
208 if (cd->cd_pull)
209 iconv_close ((iconv_t) cd->cd_pull);
210 if (cd->cd_push)
211 iconv_close ((iconv_t) cd->cd_push);
212#endif
213
214 g_free (cd->from_name);
215 g_free (cd->to_name);
216
217 memset (cd, 0, sizeof (*cd));
218 g_free (cd);
219 return 0;
220}
221
222static size_t
223iconv_copy_ntlmssp (void *cd, const char **inbuf, size_t *inbytesleft,
224 char **outbuf, size_t *outbytesleft)
225{
226 int n;
227
228 n = MIN (*inbytesleft, *outbytesleft);
229
230 (void) cd;
231 memmove (*outbuf, *inbuf, n);
232
233 (*inbytesleft) -= n;
234 (*outbytesleft) -= n;
235 (*inbuf) += n;
236 (*outbuf) += n;
237
238 if (*inbytesleft > 0)
239 {
240 errno = E2BIG;
241 return -1;
242 }
243
244 return 0;
245}
unsigned int bool
Definition charcnv.c:62
Unix SMB/CIFS implementation. charset defines.
int smb_iconv_close_ntlmssp(smb_iconv_t cd)
Definition iconv.c:203
smb_iconv_t smb_iconv_open_ntlmssp(const char *tocode, const char *fromcode)
Definition iconv.c:101
static size_t iconv_copy_ntlmssp(void *, const char **, size_t *, char **, size_t *)
Definition iconv.c:223
static bool is_utf16_ntlmssp(const char *name)
Definition iconv.c:91
size_t smb_iconv_ntlmssp(smb_iconv_t cd, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
Definition iconv.c:53
static struct charset_functions_ntlmssp * charsets
Definition iconv.c:26
static struct charset_functions_ntlmssp * find_charset_functions_ntlmssp(const char *name)
Definition iconv.c:29
Unix SMB/CIFS implementation. iconv memory system include wrappers.
const char * name
Definition nasl_init.c:440
Unix SMB/CIFS implementation.
struct _smb_iconv_t * smb_iconv_t
#define SMB_MALLOC_P(type)
Definition smb.h:172
#define SMB_STRDUP(s)
Definition smb.h:180
char * from_name
Definition smb.h:83
void * cd_direct
Definition smb.h:82
size_t(* push)(void *cd, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
Definition smb.h:80
size_t(* pull)(void *cd, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
Definition smb.h:78
size_t(* direct)(void *cd, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
Definition smb.h:76
char * to_name
Definition smb.h:83
void * cd_pull
Definition smb.h:82
void * cd_push
Definition smb.h:82
struct charset_functions_ntlmssp * next
Definition charset.h:47
size_t(* pull)(void *, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
Definition charset.h:43
size_t(* push)(void *, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
Definition charset.h:45