OpenVAS Libraries  9.0.3
charcnv.c File Reference

Character-set conversion routines built on our iconv. More...

#include "byteorder.h"
#include "iconv.h"
#include "smb.h"
#include "proto.h"
#include "../misc/openvas_logging.h"
Include dependency graph for charcnv.c:

Go to the source code of this file.

Macros

#define uint8   uint8_t
 
#define uint16   uint16_t
 
#define _PUBLIC_
 
#define False   0
 
#define True   1
 

Typedefs

typedef unsigned int bool
 

Functions

size_t convert_string_ntlmssp (charset_t from, charset_t to, void const *src, size_t srclen, void *dest, size_t destlen, bool allow_badcharcnv)
 
char lp_failed_convert_char_ntlmssp (void)
 
void init_valid_table_ntlmssp (void)
 
size_t strlen_w_ntlmssp (const uint16 *src)
 
void lazy_initialize_conv_ntlmssp (void)
 
void init_iconv_ntlmssp (void)
 

Detailed Description

Character-set conversion routines built on our iconv.

Note
Samba's internal character set (at least in the 3.0 series) is always the same as the one for the Unix filesystem. It is not necessarily UTF-8 and may be different on machines that need i18n filenames to be compatible with Unix software. It does have to be a superset of ASCII. All multibyte sequences must start with a byte with the high bit set.
See also
lib/iconv.c

Definition in file charcnv.c.

Macro Definition Documentation

◆ _PUBLIC_

#define _PUBLIC_

Definition at line 54 of file charcnv.c.

◆ False

#define False   0

Definition at line 58 of file charcnv.c.

◆ True

#define True   1

Definition at line 59 of file charcnv.c.

◆ uint16

#define uint16   uint16_t

Definition at line 50 of file charcnv.c.

◆ uint8

#define uint8   uint8_t

Definition at line 46 of file charcnv.c.

Typedef Documentation

◆ bool

typedef unsigned int bool

Definition at line 57 of file charcnv.c.

Function Documentation

◆ convert_string_ntlmssp()

size_t convert_string_ntlmssp ( charset_t  from,
charset_t  to,
void const *  src,
size_t  srclen,
void *  dest,
size_t  destlen,
bool  allow_bad_conv 
)

Convert string from one encoding to another, making error checking etc Fast path version - handles ASCII first.

Parameters
srcpointer to source string (multibyte or singlebyte)
srclenlength of the source string in bytes, or -1 for nul terminated.
destpointer to destination string (multibyte or singlebyte)
destlenmaximal length allowed for string - NEVER -1.
allow_bad_convdetermines if a "best effort" conversion is acceptable (never returns errors)
Returns
the number of bytes occupied in the destination

Ensure the srclen contains the terminating zero.

This function has been hand-tuned to provide a fast path. Don't change unless you really know what you are doing. JRA.

Definition at line 441 of file charcnv.c.

444 {
445  /*
446  * NB. We deliberately don't do a strlen here if srclen == -1.
447  * This is very expensive over millions of calls and is taken
448  * care of in the slow path in convert_string_internal. JRA.
449  */
450 
451  if (srclen == 0)
452  return 0;
453 
454  if (from != CH_UTF16LE && from != CH_UTF16BE && to != CH_UTF16LE && to != CH_UTF16BE) {
455  const unsigned char *p = (const unsigned char *)src;
456  unsigned char *q = (unsigned char *)dest;
457  size_t slen = srclen;
458  size_t dlen = destlen;
459  unsigned char lastp = '\0';
460  size_t retval = 0;
461 
462  /* If all characters are ascii, fast path here. */
463  while (slen && dlen) {
464  if ((lastp = *p) <= 0x7f) {
465  *q++ = *p++;
466  if (slen != (size_t)-1) {
467  slen--;
468  }
469  dlen--;
470  retval++;
471  if (!lastp)
472  break;
473  } else {
474  #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
475  goto general_case;
476  #else
477  size_t ret = convert_string_internal_ntlmssp(from, to, p, slen, q, dlen, allow_bad_conv);
478  if (ret == (size_t)-1) {
479  return ret;
480  }
481  return retval + ret;
482  #endif
483  }
484  }
485  if (!dlen) {
486  /* Even if we fast path we should note if we ran out of room. */
487  if (((slen != (size_t)-1) && slen) ||
488  ((slen == (size_t)-1) && lastp)) {
489  errno = E2BIG;
490  }
491  }
492  return retval;
493  } else if (from == CH_UTF16LE && to != CH_UTF16LE) {
494  const unsigned char *p = (const unsigned char *)src;
495  unsigned char *q = (unsigned char *)dest;
496  size_t retval = 0;
497  size_t slen = srclen;
498  size_t dlen = destlen;
499  unsigned char lastp = '\0';
500 
501  /* If all characters are ascii, fast path here. */
502  while (((slen == (size_t)-1) || (slen >= 2)) && dlen) {
503  if (((lastp = *p) <= 0x7f) && (p[1] == 0)) {
504  *q++ = *p;
505  if (slen != (size_t)-1) {
506  slen -= 2;
507  }
508  p += 2;
509  dlen--;
510  retval++;
511  if (!lastp)
512  break;
513  } else {
514  #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
515  goto general_case;
516  #else
517  return retval + convert_string_internal_ntlmssp(from, to, p, slen, q, dlen, allow_bad_conv);
518  #endif
519  }
520  }
521  if (!dlen) {
522  /* Even if we fast path we should note if we ran out of room. */
523  if (((slen != (size_t)-1) && slen) ||
524  ((slen == (size_t)-1) && lastp)) {
525  errno = E2BIG;
526  }
527  }
528  return retval;
529  } else if (from != CH_UTF16LE && from != CH_UTF16BE && to == CH_UTF16LE) {
530  const unsigned char *p = (const unsigned char *)src;
531  unsigned char *q = (unsigned char *)dest;
532  size_t retval = 0;
533  size_t slen = srclen;
534  size_t dlen = destlen;
535  unsigned char lastp = '\0';
536 
537  /* If all characters are ascii, fast path here. */
538  while (slen && (dlen >= 2)) {
539  if ((lastp = *p) <= 0x7F) {
540  *q++ = *p++;
541  *q++ = '\0';
542  if (slen != (size_t)-1) {
543  slen--;
544  }
545  dlen -= 2;
546  retval += 2;
547  if (!lastp)
548  break;
549  } else {
550  #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
551  goto general_case;
552  #else
553  return retval + convert_string_internal_ntlmssp(from, to, p, slen, q, dlen, allow_bad_conv);
554  #endif
555  }
556  }
557  if (!dlen) {
558  /* Even if we fast path we should note if we ran out of room. */
559  if (((slen != (size_t)-1) && slen) ||
560  ((slen == (size_t)-1) && lastp)) {
561  errno = E2BIG;
562  }
563  }
564  return retval;
565  }
566 
567  #ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
568  general_case:
569  #endif
570  return convert_string_internal_ntlmssp(from, to, src, srclen, dest, destlen, allow_bad_conv);
571 }

References CH_UTF16BE, and CH_UTF16LE.

◆ init_iconv_ntlmssp()

void init_iconv_ntlmssp ( void  )

Initialize iconv conversion descriptors.

This is called the first time it is needed, and also called again every time the configuration is reloaded, because the charset or codepage might have changed.

Definition at line 214 of file charcnv.c.

215 {
216  int c1, c2;
217  bool did_reload = False;
218 
219  /* so that charset_name() works we need to get the UNIX<->UCS2 going
220  first */
221  if (!conv_handles_ntlmssp[CH_UNIX][CH_UTF16LE])
222  conv_handles_ntlmssp[CH_UNIX][CH_UTF16LE] = smb_iconv_open_ntlmssp(charset_name_ntlmssp(CH_UTF16LE), "ASCII");
223 
224  if (!conv_handles_ntlmssp[CH_UTF16LE][CH_UNIX])
225  conv_handles_ntlmssp[CH_UTF16LE][CH_UNIX] = smb_iconv_open_ntlmssp("ASCII", charset_name_ntlmssp(CH_UTF16LE));
226 
227  for (c1=0;c1<NUM_CHARSETS;c1++) {
228  for (c2=0;c2<NUM_CHARSETS;c2++) {
229  const char *n1 = charset_name_ntlmssp((charset_t)c1);
230  const char *n2 = charset_name_ntlmssp((charset_t)c2);
231  if (conv_handles_ntlmssp[c1][c2] &&
232  strcmp(n1, conv_handles_ntlmssp[c1][c2]->from_name) == 0 &&
233  strcmp(n2, conv_handles_ntlmssp[c1][c2]->to_name) == 0)
234  continue;
235 
236  did_reload = True;
237 
238  if (conv_handles_ntlmssp[c1][c2])
239  smb_iconv_close_ntlmssp(conv_handles_ntlmssp[c1][c2]);
240 
241  conv_handles_ntlmssp[c1][c2] = smb_iconv_open_ntlmssp(n2,n1);
242  if (conv_handles_ntlmssp[c1][c2] == (smb_iconv_t)-1) {
243  if (c1 != CH_UTF16LE && c1 != CH_UTF16BE) {
244  n1 = "ASCII";
245  }
246  if (c2 != CH_UTF16LE && c2 != CH_UTF16BE) {
247  n2 = "ASCII";
248  }
249  conv_handles_ntlmssp[c1][c2] = smb_iconv_open_ntlmssp(n2,n1);
250  if (!conv_handles_ntlmssp[c1][c2]) {
251  log_legacy_write ("init_iconv_ntlmssp: conv_handle"
252  " initialization failed");
253  }
254  }
255  }
256  }
257 
258  if (did_reload) {
259  /* XXX: Does this really get called every time the dos
260  * codepage changes? */
261  /* XXX: Is the did_reload test too strict? */
262  conv_silent_ntlmssp = True;
264  conv_silent_ntlmssp = False;
265  }
266 }
charset_t
Definition: charset.h:30
void init_valid_table_ntlmssp(void)
Definition: charcnv.c:108
void log_legacy_write(const char *format,...)
Legacy function to write a log message.
smb_iconv_t smb_iconv_open_ntlmssp(const char *tocode, const char *fromcode)
Definition: iconv.c:105
#define True
Definition: charcnv.c:59
#define False
Definition: charcnv.c:58
int smb_iconv_close_ntlmssp(smb_iconv_t cd)
Definition: iconv.c:195
#define NUM_CHARSETS
Definition: charset.h:32

References False.

◆ init_valid_table_ntlmssp()

void init_valid_table_ntlmssp ( void  )

Definition at line 108 of file charcnv.c.

109 {
110  static int mapped_file;
111  int i;
112  const char *allowed = ".!#$%&'()_-@^`~";
113 
114 if (mapped_file) {
115 /* Can't unmap files, so stick with what we have */
116  return;
117 }
118 
119 
120 /* we're using a dynamically created valid_table.
121  * It might need to be regenerated if the code page changed.
122  * We know that we're not using a mapped file, so we can
123  * free() the old one. */
124 
125 /* use free rather than unmap */
126 valid_table_use_unmap_ntlmssp = False;
127 
128  valid_table_ntlmssp = (uint8 *)SMB_MALLOC(0x10000);
129  for (i=0;i<128;i++) {
130  valid_table_ntlmssp[i] = isalnum(i) || strchr(allowed,i);
131  }
132 
134 
135  for (;i<0x10000;i++) {
136  uint16_t c;
137  SSVAL(&c, 0, i);
138  valid_table_ntlmssp[i] = check_dos_char_slowly_ntlmssp(c);
139  }
140 }
#define SSVAL(buf, pos, val)
Definition: byteorder.h:122
#define False
Definition: charcnv.c:58
void lazy_initialize_conv_ntlmssp(void)
Definition: charcnv.c:196
#define uint8
Definition: charcnv.c:46

◆ lazy_initialize_conv_ntlmssp()

void lazy_initialize_conv_ntlmssp ( void  )

Definition at line 196 of file charcnv.c.

197 {
198  static int initialized = False;
199 
200  if (!initialized) {
201  initialized = True;
203  }
204 }
void init_iconv_ntlmssp(void)
Definition: charcnv.c:214
#define True
Definition: charcnv.c:59
#define False
Definition: charcnv.c:58

◆ lp_failed_convert_char_ntlmssp()

char lp_failed_convert_char_ntlmssp ( void  )

Definition at line 85 of file charcnv.c.

86 {
87  return '_';
88 }

◆ strlen_w_ntlmssp()

size_t strlen_w_ntlmssp ( const uint16 src)

Definition at line 146 of file charcnv.c.

147 {
148  size_t len;
149  uint16 c;
150 
151  for(len = 0; *(COPY_UCS2_CHAR(&c,src)); src++, len++) {
152  ;
153  }
154 
155  return len;
156 }
#define uint16
Definition: charcnv.c:50
#define COPY_UCS2_CHAR(dest, src)
Definition: smb.h:162

References COPY_UCS2_CHAR, and uint16.