Browse files

Make ucs2 string conversion helpers support both endians

  • Loading branch information...
1 parent c6ba089 commit aa41e27a80c137741d28d037665ec2078d614740 @andoma committed Aug 15, 2012
Showing with 16 additions and 10 deletions.
  1. +3 −3 src/fileaccess/fa_nativesmb.c
  2. +11 −5 src/misc/string.c
  3. +2 −2 src/misc/string.h
View
6 src/fileaccess/fa_nativesmb.c
@@ -910,7 +910,7 @@ utf8_to_smb(const cifs_connection_t *cc, uint8_t *dst, const char *src)
{
size_t r;
if(cc->cc_unicode)
- r = utf8_to_ucs2(dst, src);
+ r = utf8_to_ucs2(dst, src, 1);
else
r = utf8_to_ascii(dst, src);
return r;
@@ -1038,7 +1038,7 @@ smb_neg_proto(cifs_connection_t *cc, char *errbuf, size_t errlen)
memcpy(cc->cc_challenge_key, reply->data, 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);
return 0;
}
@@ -2100,7 +2100,7 @@ cifs_scandir(cifs_tree_t *ct, const char *path, fa_dir_t *fd,
data = rbuf + off;
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);
View
16 src/misc/string.c
@@ -1150,11 +1150,12 @@ charset_get_name(const void *p)
*
*/
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;
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)
break;
src += 2;
@@ -1171,7 +1172,7 @@ ucs2_to_utf8(uint8_t *dst, size_t dstlen, const uint8_t *src, size_t srclen)
*
*/
size_t
-utf8_to_ucs2(uint8_t *dst, const char *src)
+utf8_to_ucs2(uint8_t *dst, const char *src, int le)
{
int c;
size_t o = 0;
@@ -1180,8 +1181,13 @@ utf8_to_ucs2(uint8_t *dst, const char *src)
return -1;
if(dst != NULL) {
- dst[o] = c;
- dst[o+1] = c >> 8;
+ if(le) {
+ dst[o] = c;
+ dst[o+1] = c >> 8;
+ } else {
+ dst[o] = c >> 8;
+ dst[o+1] = c;
+ }
}
o+=2;
}
View
4 src/misc/string.h
@@ -63,9 +63,9 @@ char *utf8_from_bytes(const char *str, int len, const uint16_t *table);
int hexnibble(char c);
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);

0 comments on commit aa41e27

Please sign in to comment.