Skip to content

Commit

Permalink
Make ucs2 string conversion helpers support both endians
Browse files Browse the repository at this point in the history
  • Loading branch information
andoma committed Aug 15, 2012
1 parent c6ba089 commit aa41e27
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/fileaccess/fa_nativesmb.c
Expand Up @@ -910,7 +910,7 @@ utf8_to_smb(const cifs_connection_t *cc, uint8_t *dst, const char *src)
{ {
size_t r; size_t r;
if(cc->cc_unicode) if(cc->cc_unicode)
r = utf8_to_ucs2(dst, src); r = utf8_to_ucs2(dst, src, 1);
else else
r = utf8_to_ascii(dst, src); r = utf8_to_ascii(dst, src);
return r; return r;
Expand Down Expand Up @@ -1038,7 +1038,7 @@ smb_neg_proto(cifs_connection_t *cc, char *errbuf, size_t errlen)
memcpy(cc->cc_challenge_key, reply->data, 8); memcpy(cc->cc_challenge_key, reply->data, 8);
len -= 8; len -= 8;


ucs2_to_utf8(cc->cc_domain, sizeof(cc->cc_domain), reply->data + 8, len); ucs2_to_utf8(cc->cc_domain, sizeof(cc->cc_domain), reply->data + 8, len, 1);
free(rbuf); free(rbuf);
return 0; return 0;
} }
Expand Down Expand Up @@ -2100,7 +2100,7 @@ cifs_scandir(cifs_tree_t *ct, const char *path, fa_dir_t *fd,
data = rbuf + off; data = rbuf + off;


ucs2_to_utf8(fname, sizeof(fname), ucs2_to_utf8(fname, sizeof(fname),
data->filename, htole_32(data->file_name_len)); data->filename, htole_32(data->file_name_len), 1);


snprintf(urlbase, urlspace, "%s", fname); snprintf(urlbase, urlspace, "%s", fname);


Expand Down
16 changes: 11 additions & 5 deletions src/misc/string.c
Expand Up @@ -1150,11 +1150,12 @@ charset_get_name(const void *p)
* *
*/ */
void void
ucs2_to_utf8(uint8_t *dst, size_t dstlen, const uint8_t *src, size_t srclen) ucs2_to_utf8(uint8_t *dst, size_t dstlen, const uint8_t *src, size_t srclen,
int le)
{ {
int c, r; int c, r;
while(dstlen > 3 && srclen >= 2) { while(dstlen > 3 && srclen >= 2) {
c = src[0] | src[1] << 8; c = le ? (src[0] | src[1] << 8) : (src[1] | src[0] << 8);
if(c == 0) if(c == 0)
break; break;
src += 2; src += 2;
Expand All @@ -1171,7 +1172,7 @@ ucs2_to_utf8(uint8_t *dst, size_t dstlen, const uint8_t *src, size_t srclen)
* *
*/ */
size_t size_t
utf8_to_ucs2(uint8_t *dst, const char *src) utf8_to_ucs2(uint8_t *dst, const char *src, int le)
{ {
int c; int c;
size_t o = 0; size_t o = 0;
Expand All @@ -1180,8 +1181,13 @@ utf8_to_ucs2(uint8_t *dst, const char *src)
return -1; return -1;


if(dst != NULL) { if(dst != NULL) {
dst[o] = c; if(le) {
dst[o+1] = c >> 8; dst[o] = c;
dst[o+1] = c >> 8;
} else {
dst[o] = c >> 8;
dst[o+1] = c;
}
} }
o+=2; o+=2;
} }
Expand Down
4 changes: 2 additions & 2 deletions src/misc/string.h
Expand Up @@ -63,9 +63,9 @@ char *utf8_from_bytes(const char *str, int len, const uint16_t *table);
int hexnibble(char c); int hexnibble(char c);


void ucs2_to_utf8(uint8_t *dst, size_t dstlen, void ucs2_to_utf8(uint8_t *dst, size_t dstlen,
const uint8_t *src, size_t srclen); const uint8_t *src, size_t srclen, int little_endian);


size_t utf8_to_ucs2(uint8_t *dst, const char *src); size_t utf8_to_ucs2(uint8_t *dst, const char *src, int little_endian);


size_t utf8_to_ascii(uint8_t *dst, const char *src); size_t utf8_to_ascii(uint8_t *dst, const char *src);


Expand Down

0 comments on commit aa41e27

Please sign in to comment.