Skip to content

Commit

Permalink
fix unicode strings generation.
Browse files Browse the repository at this point in the history
  • Loading branch information
jfdelnero committed Nov 7, 2023
1 parent db8c148 commit f5b3c9c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
5 changes: 4 additions & 1 deletion src/mtp_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,11 @@ int poke_string(void * buffer, int index, int maxsize, const char *str)

index++;

if( str[0] == '\0' )
return index; // Empty string case.

// Char to unicode...
len = char2unicodestring((char*)ptr, index, maxsize, (char*)str, 256);
len = char2unicodestring((char*)ptr, index, maxsize, (char*)str, 255);

if(len < 0)
{
Expand Down
21 changes: 15 additions & 6 deletions src/usbstring.c
Original file line number Diff line number Diff line change
Expand Up @@ -321,28 +321,37 @@ int char2unicodestring(char * unicodestr, int index, int maxsize, char * str, in
{
uint16_t unicode;
int ofs, len, start;
int unicode_size;

start = index;
len = 0;
ofs = 0;
do{
unicode_size = 0;
do
{
unicode = utf2unicode((unsigned char*)str, &ofs);
str = str + ofs;

if(index + 2 >= maxsize)
return -1;

unicodestr[index++] = unicode & 0xFF;
unicodestr[index++] = (unicode >> 8) & 0xFF;

unicode_size += 2;

len++;
}while(unicode && ofs && index < unicodestrsize*2);
} while(unicode && ofs && unicode_size < unicodestrsize*2 && unicode_size < 255 * 2 );

if( len >= unicodestrsize)
if( unicode_size >= 255 * 2 )
{
if(start + ((unicodestrsize*2)-2) >= maxsize)
return -1;

unicodestr[start + ((unicodestrsize*2)-2)] = 0x00;
unicodestr[start + ((unicodestrsize*2)-1)] = 0x00;
len = unicodestrsize;
unicodestr[start + ((255*2)-2)] = 0x00;
unicodestr[start + ((255*2)-1)] = 0x00;

len = 255;
}

return len;
Expand Down

0 comments on commit f5b3c9c

Please sign in to comment.