netatalk  4.4.0
Free and Open Source Apple Filing Protocol (AFP) Server
Loading...
Searching...
No Matches
charcnv.c File Reference
#include <arpa/inet.h>
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <unistd.h>
#include <iconv.h>
#include <atalk/logger.h>
#include <atalk/unicode.h>
#include <atalk/util.h>
#include <atalk/compat.h>
#include <atalk/byteorder.h>

Macros

#define MAX_CHARSETS   20
#define CHECK_FLAGS(a, b)
#define hextoint(c)

Functions

static const char * charset_name (charset_t ch)
int set_charset_name (charset_t ch, const char *name)
void free_charset_names (void)
static struct charset_functionsget_charset_functions (charset_t ch)
static void lazy_initialize_conv (void)
charset_t add_charset (const char *name)
void init_iconv (void)
 Initialize iconv conversion descriptors.
static size_t add_null (charset_t to, char *buf, size_t bytesleft, size_t len)
static size_t convert_string_internal (charset_t from, charset_t to, void const *src, size_t srclen, void *dest, size_t destlen)
 Convert string from one encoding to another, making error checking etc.
size_t convert_string (charset_t from, charset_t to, void const *src, size_t srclen, void *dest, size_t destlen)
static size_t convert_string_allocate_internal (charset_t from, charset_t to, void const *src, size_t srclen, char **dest)
 Convert between character sets, allocating a new buffer for the result.
size_t convert_string_allocate (charset_t from, charset_t to, void const *src, size_t srclen, char **dest)
size_t charset_strupper (charset_t ch, const char *src, size_t srclen, char *dest, size_t destlen)
size_t charset_strlower (charset_t ch, const char *src, size_t srclen, char *dest, size_t destlen)
size_t ucs2_to_charset (charset_t ch, const ucs2_t *src, char *dest, size_t destlen)
 Copy a string from a UCS2 src to a unix char * destination, allocating a buffer.
size_t ucs2_to_charset_allocate (charset_t ch, char **dest, const ucs2_t *src)
size_t charset_precompose (charset_t ch, char *src, size_t inlen, char *dst, size_t outlen)
size_t charset_decompose (charset_t ch, char *src, size_t inlen, char *dst, size_t outlen)
static size_t pull_charset_flags (charset_t from_set, charset_t to_set, charset_t cap_set, const char *src, size_t srclen, char *dest, size_t destlen, uint16_t *flags)
 Convert from MB to UCS2 charset.
static size_t push_charset_flags (charset_t to_set, charset_t cap_set, char *src, size_t srclen, char *dest, size_t destlen, uint16_t *flags)
 Convert from UCS2 to MB charset.
size_t convert_charset (charset_t from_set, charset_t to_set, charset_t cap_charset, const char *src, size_t src_len, char *dest, size_t dest_len, uint16_t *flags)

Variables

static atalk_iconv_t conv_handles [MAX_CHARSETS][MAX_CHARSETS]
static char * charset_names [MAX_CHARSETS]
static struct charset_functionscharsets [MAX_CHARSETS]
static char hexdig [] = "0123456789abcdef"

Detailed Description

Character-set conversion routines based on samba iconv wrappers.

Macro Definition Documentation

◆ CHECK_FLAGS

#define CHECK_FLAGS ( a,
b )
Value:
(((a)!=NULL) ? (*(a) & (b)) : 0 )

◆ hextoint

#define hextoint ( c)
Value:
( isdigit( c ) ? c - '0' : c + 10 - 'a' )

◆ MAX_CHARSETS

#define MAX_CHARSETS   20

Function Documentation

◆ add_charset()

charset_t add_charset ( const char * name)

◆ add_null()

size_t add_null ( charset_t to,
char * buf,
size_t bytesleft,
size_t len )
static

◆ charset_decompose()

size_t charset_decompose ( charset_t ch,
char * src,
size_t inlen,
char * dst,
size_t outlen )

◆ charset_name()

const char * charset_name ( charset_t ch)
static

Return the name of a charset to give to iconv().

◆ charset_precompose()

size_t charset_precompose ( charset_t ch,
char * src,
size_t inlen,
char * dst,
size_t outlen )

◆ charset_strlower()

size_t charset_strlower ( charset_t ch,
const char * src,
size_t srclen,
char * dest,
size_t destlen )

◆ charset_strupper()

size_t charset_strupper ( charset_t ch,
const char * src,
size_t srclen,
char * dest,
size_t destlen )

◆ convert_charset()

size_t convert_charset ( charset_t from_set,
charset_t to_set,
charset_t cap_charset,
const char * src,
size_t src_len,
char * dest,
size_t dest_len,
uint16_t * flags )
Bug
the size is a mess we really need a malloc/free logic
Note
dest size must be dest_len +2

◆ convert_string()

size_t convert_string ( charset_t from,
charset_t to,
void const * src,
size_t srclen,
void * dest,
size_t destlen )

◆ convert_string_allocate()

size_t convert_string_allocate ( charset_t from,
charset_t to,
void const * src,
size_t srclen,
char ** dest )

◆ convert_string_allocate_internal()

size_t convert_string_allocate_internal ( charset_t from,
charset_t to,
void const * src,
size_t srclen,
char ** dest )
static

Convert between character sets, allocating a new buffer for the result.

Parameters
fromsource character set
todestination character set
srcpointer to source string (multibyte or singlebyte)
srclenlength of source buffer
destalways set at least to NULL
Note
-1 is not accepted for srclen
Returns
Size in bytes of the converted string; or -1 in case of error

◆ convert_string_internal()

size_t convert_string_internal ( charset_t from,
charset_t to,
void const * src,
size_t srclen,
void * dest,
size_t destlen )
static

Convert string from one encoding to another, making error checking etc.

Parameters
fromsource character set
todestination character set
srcpointer to source string (multibyte or singlebyte)
srclenlength of the source string in bytes
destpointer to destination string (multibyte or singlebyte)
destlenmaximal length allowed for string
Returns
the number of bytes occupied in the destination

◆ free_charset_names()

void free_charset_names ( void )

◆ get_charset_functions()

struct charset_functions * get_charset_functions ( charset_t ch)
static

◆ init_iconv()

void init_iconv ( 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.

◆ lazy_initialize_conv()

void lazy_initialize_conv ( void )
static

◆ pull_charset_flags()

size_t pull_charset_flags ( charset_t from_set,
charset_t to_set,
charset_t cap_set,
const char * src,
size_t srclen,
char * dest,
size_t destlen,
uint16_t * flags )
static

Convert from MB to UCS2 charset.

Flags:

  • CONV_UNESCAPEHEX: ':XX' will be converted to an UCS2 character
  • CONV_IGNORE: return the first convertable characters.
  • CONV_FORCE: force convertion
Bug
This will not work if the destination charset is not multibyte, i.e. UCS2->UCS2 will fail The (un)escape scheme is not compatible to the old cap style escape. This is bad, we need it for e.g. HFS cdroms.

◆ push_charset_flags()

size_t push_charset_flags ( charset_t to_set,
charset_t cap_set,
char * src,
size_t srclen,
char * dest,
size_t destlen,
uint16_t * flags )
static

Convert from UCS2 to MB charset.

Flags:

  • CONV_ESCAPEDOTS: escape leading dots
  • CONV_ESCAPEHEX: unconvertable characters and '/' will be escaped to :XX
  • CONV_IGNORE: return the first convertable characters.
  • CONV__EILSEQ: unconvertable characters will be replaced with '_'
  • CONV_FORCE: force convertion
    Bug
    CONV_IGNORE and CONV_ESCAPEHEX can't work together. Should we check this ? This will not work if the destination charset is not multibyte, i.e. UCS2->UCS2 will fail The escape scheme is not compatible to the old cap style escape. This is bad, we need it for e.g. HFS cdroms.

◆ set_charset_name()

int set_charset_name ( charset_t ch,
const char * name )

◆ ucs2_to_charset()

size_t ucs2_to_charset ( charset_t ch,
const ucs2_t * src,
char * dest,
size_t destlen )

Copy a string from a UCS2 src to a unix char * destination, allocating a buffer.

Parameters
chdestination character set
srcsource UCS2 string
destalways set at least to NULL
destlenmaximum length of destination buffer
Returns
The number of bytes occupied by the string in the destination

◆ ucs2_to_charset_allocate()

size_t ucs2_to_charset_allocate ( charset_t ch,
char ** dest,
const ucs2_t * src )

Variable Documentation

◆ charset_names

char* charset_names[MAX_CHARSETS]
static

◆ charsets

struct charset_functions* charsets[MAX_CHARSETS]
static

◆ conv_handles

atalk_iconv_t conv_handles[MAX_CHARSETS][MAX_CHARSETS]
static

◆ hexdig

char hexdig[] = "0123456789abcdef"
static