Commit 317068b8b6b15ad38f7aa1019310e41764c80e5b

Authored by Heinrich Schuchardt
1 parent ade317a281

efi_loader: support unaligned u16 strings

Allow unaligned u16 strings as arguments to u16_strdup() and u16_strlen().

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

Showing 2 changed files with 16 additions and 11 deletions Side-by-side Diff

... ... @@ -178,7 +178,7 @@
178 178 * ReturnValue: number of non-zero words.
179 179 * This is not the number of utf-16 letters!
180 180 */
181   -size_t u16_strlen(const u16 *in);
  181 +size_t u16_strlen(const void *in);
182 182  
183 183 /**
184 184 * u16_strlen - count non-zero words
... ... @@ -214,7 +214,7 @@
214 214 * @src: source buffer (null terminated)
215 215 * Return: allocated new buffer on success, NULL on failure
216 216 */
217   -u16 *u16_strdup(const u16 *src);
  217 +u16 *u16_strdup(const void *src);
218 218  
219 219 /**
220 220 * utf16_to_utf8() - Convert an utf16 string to utf8
... ... @@ -335,11 +335,16 @@
335 335 return ret;
336 336 }
337 337  
338   -size_t u16_strlen(const u16 *in)
  338 +size_t u16_strlen(const void *in)
339 339 {
340   - size_t i;
341   - for (i = 0; in[i]; i++);
342   - return i;
  340 + const char *pos = in;
  341 + size_t ret;
  342 +
  343 + for (; pos[0] || pos[1]; pos += 2)
  344 + ;
  345 + ret = pos - (char *)in;
  346 + ret >>= 1;
  347 + return ret;
343 348 }
344 349  
345 350 size_t u16_strnlen(const u16 *in, size_t count)
346 351  
347 352  
348 353  
... ... @@ -362,18 +367,18 @@
362 367 return tmp;
363 368 }
364 369  
365   -u16 *u16_strdup(const u16 *src)
  370 +u16 *u16_strdup(const void *src)
366 371 {
367 372 u16 *new;
  373 + size_t len;
368 374  
369 375 if (!src)
370 376 return NULL;
371   -
372   - new = malloc((u16_strlen(src) + 1) * sizeof(u16));
  377 + len = (u16_strlen(src) + 1) * sizeof(u16);
  378 + new = malloc(len);
373 379 if (!new)
374 380 return NULL;
375   -
376   - u16_strcpy(new, src);
  381 + memcpy(new, src, len);
377 382  
378 383 return new;
379 384 }