Character set conversion Extensions
Copyright (C) Igor Vergeichik <iverg@mail.ru> 2001
Copyright (C) Andrew Tridgell 2001
+ Copyright (C) Simo Sorce 2001
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
{
size_t i_len, o_len, destlen;
size_t retval;
- char* inbuf = (char *)src;
+ char *inbuf = (char *)src;
char *outbuf, *ob;
smb_iconv_t descriptor;
ob = (char *)realloc(outbuf, destlen);
if (!ob) {
DEBUG(0, ("convert_string_allocate: realloc failed!\n"));
- free(outbuf);
+ SAFE_FREE(outbuf);
return -1;
}
else outbuf = ob;
}
destlen = destlen - o_len;
+ *dest = (char *)realloc(ob,destlen);
*dest = (char *)realloc(outbuf,destlen);
if (!*dest) {
DEBUG(0, ("convert_string_allocate: out of memory!\n"));
- free(outbuf);
+ SAFE_FREE(ob);
return -1;
}
size_t dlen;
void *dest;
- slen = strlen_w(src) + 1;
+ slen = (strlen_w(src) + 1) * sizeof(smb_ucs2_t);
dlen = convert_string_allocate(CH_UCS2, CH_UNIX, src, slen, &dest);
if (dlen == -1) return NULL;
else return dest;
}
/****************************************************************************
-convert from ucs2 to unix charset and return the
+convert from unix to ucs2 charset and return the
allocated and converted string or NULL if an error occurred.
you must provide a zero terminated string.
the returning string will be zero terminated.
if (dlen == -1) return NULL;
else return dest;
}
+
+/****************************************************************************
+convert from ucs2 to dos charset and return the
+allocated and converted string or NULL if an error occurred.
+you must provide a zero terminated string.
+the returning string will be zero terminated.
+****************************************************************************/
+char *acnv_u2dos(const smb_ucs2_t *src)
+{
+ size_t slen;
+ size_t dlen;
+ void *dest;
+
+ slen = (strlen_w(src) + 1) * sizeof(smb_ucs2_t);
+ dlen = convert_string_allocate(CH_UCS2, CH_DOS, src, slen, &dest);
+ if (dlen == -1) return NULL;
+ else return dest;
+}
+
+/****************************************************************************
+convert from dos to ucs2 charset and return the
+allocated and converted string or NULL if an error occurred.
+you must provide a zero terminated string.
+the returning string will be zero terminated.
+****************************************************************************/
+smb_ucs2_t *acnv_dosu2(const char *src)
+{
+ size_t slen;
+ size_t dlen;
+ void *dest;
+
+ slen = strlen(src) + 1;
+ dlen = convert_string_allocate(CH_DOS, CH_UCS2, src, slen, &dest);
+ if (dlen == -1) return NULL;
+ else return dest;
+}
Samba utility functions
Copyright (C) Andrew Tridgell 1992-1998
Copyright (C) Jeremy Allison 2001
+ Copyright (C) Simo Sorce 2001
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
smb_ucs2_t *ns;
smb_ucs2_t *p, *r, *t;
- DEBUG(3, ("unix_clean_name_w\n")); /* [%unicode]\n")); */
+ DEBUG(3, ("unix_clean_path\n")); /* [%unicode]\n")); */
/* convert '\' to '/' */
ns = strdup_w(s);
/* reduce any /../ */
t = ns;
- while ((r = strstr_wa(t, "/..")) != NULL) {
+ while ((r = strstr_wa(t, "/.."))) {
t = &(r[3]);
if (*t == UCS2_CHAR('/') || *t == 0) {
*r = 0;
return len;
}
+/*******************************************************************
+ Count up to max number of characters in a smb_ucs2_t string.
+********************************************************************/
+size_t strnlen_w(const smb_ucs2_t *src, size_t max)
+{
+ size_t len;
+
+ for(len = 0; *src++ && (len < max); len++) ;
+
+ return len;
+}
+
/*******************************************************************
wide strchr()
********************************************************************/
if (!dest || !src) return NULL;
start = strlen_w(dest);
- len = strlen_w(src);
- if (len > max) len = max;
+ len = strnlen_w(src, max);
memcpy(&dest[start], src, len*sizeof(smb_ucs2_t));
dest[start+len] = 0;
return dest;
}
-
/*******************************************************************
replace any occurence of oldc with newc in unicode string
********************************************************************/
return dest;
}
+/*******************************************************************
+convert and duplicate an ascii string
+********************************************************************/
+smb_ucs2_t *strdup_wa(const char *src)
+{
+ return strndup_wa(src, 0);
+}
+
+/* if len == 0 then duplicate the whole string */
+smb_ucs2_t *strndup_wa(const char *src, size_t len)
+{
+ smb_ucs2_t *dest, *s;
+
+ s = acnv_dosu2(src);
+ if (!len) len = strlen_w(s);
+ dest = (smb_ucs2_t *)malloc((len + 1) * sizeof(smb_ucs2_t));
+ if (!dest) {
+ DEBUG(0,("strdup_w: out of memory!\n"));
+ SAFE_FREE(s);
+ return NULL;
+ }
+
+ memcpy(dest, src, len * sizeof(smb_ucs2_t));
+ dest[len] = 0;
+
+ SAFE_FREE(s);
+ return dest;
+}
+
/*******************************************************************
append a string of len bytes and add a terminator
********************************************************************/
}
else /* FOUND */
{
- p = (smb_ucs2_t *)malloc(data.dsize*sizeof(smb_ucs2_t));
+ p = acnv_dosu2(data.dptr);
if (!p)
{
DEBUG(0,("mangle: out of memory!\n"));
goto done;
}
- dos_to_ucs2(p, data.dptr, data.dsize*sizeof(smb_ucs2_t));
}
if (ext)
{
smb_ucs2_t *in, *out;
char *dos_mangled;
- size_t len;
if (!dos_unmangled || !*dos_unmangled) return NULL;
- len = (strlen(dos_unmangled) + 1) * sizeof(smb_ucs2_t);
- in = (smb_ucs2_t *)malloc(len);
+ in = acnv_dosu2(dos_unmangled);
if (!in)
{
DEBUG(0,("dos_mangle: out of memory!\n"));
return NULL;
}
- dos_to_ucs2(in, dos_unmangled, len);
out = mangle(in);
if (!out)
return NULL;
}
- dos_mangled = (char *)malloc(PSTRING_LEN);
+ dos_mangled = acnv_u2dos(out);
if (!dos_mangled)
{
DEBUG(0,("dos_mangle: out of memory!\n"));
goto done;
}
- ucs2_to_dos (dos_mangled, out, PSTRING_LEN);
done:
SAFE_FREE(in);
{
smb_ucs2_t *in, *out;
char *dos_unmangled;
- size_t len;
if (!dos_mangled || !*dos_mangled) return NULL;
- len = (strlen(dos_mangled) + 1) * sizeof(smb_ucs2_t);
- in = (smb_ucs2_t *)malloc(len);
+ in = acnv_dosu2(dos_mangled);
if (!in)
{
DEBUG(0,("dos_unmangle: out of memory!\n"));
return NULL;
}
- dos_to_ucs2(in, dos_mangled, len);
out = mangle(in);
if (!out)
return NULL;
}
- dos_unmangled = (char *)malloc(PSTRING_LEN);
+ dos_unmangled = acnv_u2dos(out);
if (!dos_unmangled)
{
DEBUG(0,("dos_unmangle: out of memory!\n"));
goto done;
}
- ucs2_to_dos (dos_unmangled, out, PSTRING_LEN);
done:
SAFE_FREE(in);
if (strlen(fname) > 12) return False;
- ucs2name = (smb_ucs2_t *)malloc(13 * sizeof(smb_ucs2_t));
+ ucs2name = acnv_uxu2(fname);
if (!ucs2name)
{
DEBUG(0,("is_8_3: out of memory!\n"));
goto done;
}
- push_ucs2(NULL, ucs2name, fname, 13, STR_TERMINATE);
ret = is_8_3_w(ucs2name);
done:
if (!fname || !*fname) return NT_STATUS_INVALID_PARAMETER;
- DEBUG(10,("has_valid_chars: testing\n")); /* [%s]\n", s)); */
+ DEBUG(10,("is_valid_name: testing\n")); /* [%s]\n", s)); */
ret = has_valid_chars(fname);
if (NT_STATUS_IS_ERR(ret)) return ret;
BOOL is_mangled(const char *s)
{
smb_ucs2_t *u2, *res;
- size_t u2len;
BOOL ret = False;
DEBUG(10,("is_mangled: testing [%s]\n", s));
if (!s || !*s) return False;
if ((strlen(s) > 12) || (!strchr(s, '~'))) return False;
- u2len = (strlen(s) + 1) * sizeof(smb_ucs2_t);
- u2 = (smb_ucs2_t *)malloc(u2len);
+ u2 = acnv_dosu2(s);
if (!u2)
{
DEBUG(0,("is_mangled: out of memory!\n"));
return ret;
}
- dos_to_ucs2(u2, s, u2len);
res = unmangle(u2);
if (res) ret = True;
BOOL check_mangled_cache(char *s)
{
smb_ucs2_t *u2, *res;
- size_t slen, u2len;
BOOL ret = False;
DEBUG(10,("check_mangled_cache: I'm so ugly, please remove me!\n"));
if (!s || !*s) return False;
- slen = strlen(s);
- u2len = (slen + 1) * sizeof(smb_ucs2_t);
- u2 = (smb_ucs2_t *)malloc(u2len);
+ u2 = acnv_dosu2(s);
if (!u2)
{
DEBUG(0,("check_mangled_cache: out of memory!\n"));
return ret;
}
- dos_to_ucs2(u2, s, u2len);
res = unmangle(u2);
if (res)
void mangle_name_83(char *s)
{
smb_ucs2_t *u2, *res;
- size_t slen, u2len;
DEBUG(10,("mangle_name_83: I'm so ugly, please remove me!\n"));
DEBUG(10,("mangle_name_83: testing -> [%s]\n", s));
if (!s || !*s) return;
- slen = strlen(s);
- u2len = (slen + 1) * sizeof(smb_ucs2_t);
- u2 = (smb_ucs2_t *)malloc(u2len);
+ u2 = acnv_dosu2(s);
if (!u2)
{
DEBUG(0,("mangle_name_83: out of memory!\n"));
return;
}
- dos_to_ucs2(u2, s, u2len);
res = mangle(u2);
if (res) ucs2_to_dos (s, res, 13); /* ugly, but must be done this way */