OpenVAS Scanner  22.7.9
iconv.c File Reference

Unix SMB/CIFS implementation. minimal iconv implementation. More...

#include "iconv.h"
#include "charset.h"
#include "proto.h"
#include "smb.h"
#include <glib.h>
Include dependency graph for iconv.c:

Go to the source code of this file.

Typedefs

typedef unsigned int bool
 

Functions

static size_t iconv_copy_ntlmssp (void *, const char **, size_t *, char **, size_t *)
 
static struct charset_functions_ntlmsspfind_charset_functions_ntlmssp (const char *name)
 
size_t smb_iconv_ntlmssp (smb_iconv_t cd, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
 
static bool is_utf16_ntlmssp (const char *name)
 
smb_iconv_t smb_iconv_open_ntlmssp (const char *tocode, const char *fromcode)
 
int smb_iconv_close_ntlmssp (smb_iconv_t cd)
 

Variables

static struct charset_functions_ntlmsspcharsets = NULL
 

Detailed Description

Unix SMB/CIFS implementation. minimal iconv implementation.

Definition in file iconv.c.

Typedef Documentation

◆ bool

typedef unsigned int bool

Definition at line 21 of file iconv.c.

Function Documentation

◆ find_charset_functions_ntlmssp()

static struct charset_functions_ntlmssp* find_charset_functions_ntlmssp ( const char *  name)
static

Definition at line 29 of file iconv.c.

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 }

References charsets, charset_functions_ntlmssp::name, name, and charset_functions_ntlmssp::next.

Referenced by smb_iconv_open_ntlmssp().

Here is the caller graph for this function:

◆ iconv_copy_ntlmssp()

static size_t iconv_copy_ntlmssp ( void *  cd,
const char **  inbuf,
size_t *  inbytesleft,
char **  outbuf,
size_t *  outbytesleft 
)
static

Definition at line 223 of file iconv.c.

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 }

Referenced by smb_iconv_open_ntlmssp().

Here is the caller graph for this function:

◆ is_utf16_ntlmssp()

static bool is_utf16_ntlmssp ( const char *  name)
static

Definition at line 91 of file iconv.c.

92 {
93  return strcasecmp (name, "UCS-2LE") == 0
94  || strcasecmp (name, "UTF-16LE") == 0;
95 }

References name.

Referenced by smb_iconv_open_ntlmssp().

Here is the caller graph for this function:

◆ smb_iconv_close_ntlmssp()

int smb_iconv_close_ntlmssp ( smb_iconv_t  cd)

Definition at line 203 of file iconv.c.

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 }

References _smb_iconv_t::cd_direct, _smb_iconv_t::cd_pull, _smb_iconv_t::cd_push, _smb_iconv_t::from_name, and _smb_iconv_t::to_name.

Referenced by charset_name_ntlmssp(), and init_iconv_ntlmssp().

Here is the caller graph for this function:

◆ smb_iconv_ntlmssp()

size_t smb_iconv_ntlmssp ( smb_iconv_t  cd,
const char **  inbuf,
size_t *  inbytesleft,
char **  outbuf,
size_t *  outbytesleft 
)

This is a simple portable iconv() implementation.

It only knows about a very small number of character sets - just enough that Samba works on systems that don't have iconv.

Definition at line 53 of file iconv.c.

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 }

References _smb_iconv_t::cd_direct, _smb_iconv_t::cd_pull, _smb_iconv_t::cd_push, _smb_iconv_t::direct, _smb_iconv_t::pull, and _smb_iconv_t::push.

Referenced by convert_string_internal_ntlmssp().

Here is the caller graph for this function:

◆ smb_iconv_open_ntlmssp()

smb_iconv_t smb_iconv_open_ntlmssp ( const char *  tocode,
const char *  fromcode 
)

Definition at line 101 of file iconv.c.

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  {
120  ret->direct = iconv_copy_ntlmssp;
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 }

References _smb_iconv_t::cd_direct, _smb_iconv_t::cd_pull, _smb_iconv_t::cd_push, _smb_iconv_t::direct, find_charset_functions_ntlmssp(), _smb_iconv_t::from_name, iconv_copy_ntlmssp(), is_utf16_ntlmssp(), charset_functions_ntlmssp::pull, _smb_iconv_t::pull, charset_functions_ntlmssp::push, _smb_iconv_t::push, SMB_MALLOC_P, SMB_STRDUP, and _smb_iconv_t::to_name.

Referenced by charset_name_ntlmssp(), and init_iconv_ntlmssp().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ charsets

struct charset_functions_ntlmssp* charsets = NULL
static

Definition at line 26 of file iconv.c.

Referenced by find_charset_functions_ntlmssp().

_smb_iconv_t::pull
size_t(* pull)(void *cd, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
Definition: smb.h:78
charset_functions_ntlmssp::name
const char * name
Definition: charset.h:42
charset_functions_ntlmssp
Definition: charset.h:41
_smb_iconv_t::from_name
char * from_name
Definition: smb.h:83
charset_functions_ntlmssp::push
size_t(* push)(void *, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
Definition: charset.h:45
charset_functions_ntlmssp::next
struct charset_functions_ntlmssp * next
Definition: charset.h:47
SMB_MALLOC_P
#define SMB_MALLOC_P(type)
Definition: smb.h:172
name
const char * name
Definition: nasl_init.c:411
_smb_iconv_t::cd_push
void * cd_push
Definition: smb.h:82
_smb_iconv_t::direct
size_t(* direct)(void *cd, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
Definition: smb.h:76
_smb_iconv_t::push
size_t(* push)(void *cd, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
Definition: smb.h:80
_smb_iconv_t::cd_direct
void * cd_direct
Definition: smb.h:82
charset_functions_ntlmssp::pull
size_t(* pull)(void *, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
Definition: charset.h:43
charsets
static struct charset_functions_ntlmssp * charsets
Definition: iconv.c:26
is_utf16_ntlmssp
static bool is_utf16_ntlmssp(const char *name)
Definition: iconv.c:91
_smb_iconv_t
Definition: smb.h:75
iconv_copy_ntlmssp
static size_t iconv_copy_ntlmssp(void *, const char **, size_t *, char **, size_t *)
Definition: iconv.c:223
find_charset_functions_ntlmssp
static struct charset_functions_ntlmssp * find_charset_functions_ntlmssp(const char *name)
Definition: iconv.c:29
SMB_STRDUP
#define SMB_STRDUP(s)
Definition: smb.h:180
_smb_iconv_t::cd_pull
void * cd_pull
Definition: smb.h:82
_smb_iconv_t::to_name
char * to_name
Definition: smb.h:83