refactor the utf code so that it does not leak memory.
This commit is contained in:
parent
d1f209293e
commit
cdf86847f5
@ -33,7 +33,7 @@
|
||||
__FBSDID("$FreeBSD: src/sbin/gpt/add.c,v 1.14 2006/06/22 22:05:28 marcel Exp $");
|
||||
#endif
|
||||
#ifdef __RCSID
|
||||
__RCSID("$NetBSD: add.c,v 1.35 2015/12/02 01:01:55 christos Exp $");
|
||||
__RCSID("$NetBSD: add.c,v 1.36 2015/12/02 04:07:11 christos Exp $");
|
||||
#endif
|
||||
|
||||
#include <sys/types.h>
|
||||
@ -78,8 +78,9 @@ ent_set(struct gpt_ent *ent, const map_t map, const gpt_uuid_t xtype,
|
||||
gpt_uuid_copy(ent->ent_type, xtype);
|
||||
ent->ent_lba_start = htole64(map->map_start);
|
||||
ent->ent_lba_end = htole64(map->map_start + map->map_size - 1LL);
|
||||
if (xname != NULL)
|
||||
utf8_to_utf16(xname, ent->ent_name, sizeof(ent->ent_name));
|
||||
if (xname == NULL)
|
||||
return;
|
||||
utf8_to_utf16(xname, ent->ent_name, __arraycount(ent->ent_name));
|
||||
}
|
||||
|
||||
static int
|
||||
|
@ -1,4 +1,4 @@
|
||||
/* $NetBSD: biosboot.c,v 1.18 2015/12/01 23:29:07 christos Exp $ */
|
||||
/* $NetBSD: biosboot.c,v 1.19 2015/12/02 04:07:11 christos Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 2009 The NetBSD Foundation, Inc.
|
||||
@ -37,7 +37,7 @@
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#ifdef __RCSID
|
||||
__RCSID("$NetBSD: biosboot.c,v 1.18 2015/12/01 23:29:07 christos Exp $");
|
||||
__RCSID("$NetBSD: biosboot.c,v 1.19 2015/12/02 04:07:11 christos Exp $");
|
||||
#endif
|
||||
|
||||
#include <sys/stat.h>
|
||||
@ -173,6 +173,7 @@ biosboot(gpt_t gpt)
|
||||
struct mbr *mbr, *bootcode;
|
||||
unsigned int i;
|
||||
struct gpt_ent *ent;
|
||||
uint8_t utfbuf[__arraycount(ent->ent_name) * 3 + 1];
|
||||
|
||||
/*
|
||||
* Parse and validate partition maps
|
||||
@ -212,10 +213,11 @@ biosboot(gpt_t gpt)
|
||||
if (entry > 0 && m->map_index == entry)
|
||||
break;
|
||||
|
||||
if (label != NULL)
|
||||
if (strcmp((char *)label,
|
||||
(char *)utf16_to_utf8(ent->ent_name)) == 0)
|
||||
if (label != NULL) {
|
||||
utf16_to_utf8(ent->ent_name, utfbuf, sizeof(utfbuf));
|
||||
if (strcmp((char *)label, (char *)utfbuf) == 0)
|
||||
break;
|
||||
}
|
||||
|
||||
/* next, partition as could be specified by wedge */
|
||||
if (entry < 1 && label == NULL && size > 0 &&
|
||||
|
@ -35,7 +35,7 @@
|
||||
__FBSDID("$FreeBSD: src/sbin/gpt/gpt.c,v 1.16 2006/07/07 02:44:23 marcel Exp $");
|
||||
#endif
|
||||
#ifdef __RCSID
|
||||
__RCSID("$NetBSD: gpt.c,v 1.52 2015/12/01 23:29:07 christos Exp $");
|
||||
__RCSID("$NetBSD: gpt.c,v 1.53 2015/12/02 04:07:11 christos Exp $");
|
||||
#endif
|
||||
|
||||
#include <sys/param.h>
|
||||
@ -121,24 +121,16 @@ crc32(const void *buf, size_t size)
|
||||
return crc ^ ~0U;
|
||||
}
|
||||
|
||||
uint8_t *
|
||||
utf16_to_utf8(uint16_t *s16)
|
||||
void
|
||||
utf16_to_utf8(const uint16_t *s16, uint8_t *s8, size_t s8len)
|
||||
{
|
||||
static uint8_t *s8 = NULL;
|
||||
static size_t s8len = 0;
|
||||
size_t s8idx, s16idx, s16len;
|
||||
uint32_t utfchar;
|
||||
unsigned int c;
|
||||
|
||||
s16len = 0;
|
||||
while (s16[s16len++] != 0)
|
||||
;
|
||||
if (s8len < s16len * 3) {
|
||||
if (s8 != NULL)
|
||||
free(s8);
|
||||
s8len = s16len * 3;
|
||||
s8 = calloc(s16len, 3);
|
||||
}
|
||||
continue;
|
||||
s8idx = s16idx = 0;
|
||||
while (s16idx < s16len) {
|
||||
utfchar = le16toh(s16[s16idx++]);
|
||||
@ -150,22 +142,30 @@ utf16_to_utf8(uint16_t *s16)
|
||||
s16idx++;
|
||||
}
|
||||
if (utfchar < 0x80) {
|
||||
if (s8idx + 1 >= s8len)
|
||||
break;
|
||||
s8[s8idx++] = utfchar;
|
||||
} else if (utfchar < 0x800) {
|
||||
if (s8idx + 2 >= s8len)
|
||||
break;
|
||||
s8[s8idx++] = 0xc0 | (utfchar >> 6);
|
||||
s8[s8idx++] = 0x80 | (utfchar & 0x3f);
|
||||
} else if (utfchar < 0x10000) {
|
||||
if (s8idx + 3 >= s8len)
|
||||
break;
|
||||
s8[s8idx++] = 0xe0 | (utfchar >> 12);
|
||||
s8[s8idx++] = 0x80 | ((utfchar >> 6) & 0x3f);
|
||||
s8[s8idx++] = 0x80 | (utfchar & 0x3f);
|
||||
} else if (utfchar < 0x200000) {
|
||||
if (s8idx + 4 >= s8len)
|
||||
break;
|
||||
s8[s8idx++] = 0xf0 | (utfchar >> 18);
|
||||
s8[s8idx++] = 0x80 | ((utfchar >> 12) & 0x3f);
|
||||
s8[s8idx++] = 0x80 | ((utfchar >> 6) & 0x3f);
|
||||
s8[s8idx++] = 0x80 | (utfchar & 0x3f);
|
||||
}
|
||||
}
|
||||
return (s8);
|
||||
s8[s8idx] = 0;
|
||||
}
|
||||
|
||||
void
|
||||
@ -983,6 +983,7 @@ gpt_change_ent(gpt_t gpt, const struct gpt_find *find,
|
||||
struct gpt_hdr *hdr;
|
||||
struct gpt_ent *ent;
|
||||
unsigned int i;
|
||||
uint8_t utfbuf[__arraycount(ent->ent_name) * 3 + 1];
|
||||
|
||||
if (!find->all ^
|
||||
(find->block > 0 || find->entry > 0 || find->label != NULL
|
||||
@ -1006,10 +1007,11 @@ gpt_change_ent(gpt_t gpt, const struct gpt_find *find,
|
||||
i = m->map_index - 1;
|
||||
|
||||
ent = gpt_ent_primary(gpt, i);
|
||||
if (find->label != NULL)
|
||||
if (strcmp((char *)find->label,
|
||||
(char *)utf16_to_utf8(ent->ent_name)) != 0)
|
||||
if (find->label != NULL) {
|
||||
utf16_to_utf8(ent->ent_name, utfbuf, sizeof(utfbuf));
|
||||
if (strcmp((char *)find->label, (char *)utfbuf) == 0)
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!gpt_uuid_is_nil(find->type) &&
|
||||
!gpt_uuid_equal(find->type, ent->ent_type))
|
||||
|
@ -97,7 +97,7 @@ struct gpt_ent *gpt_ent_primary(gpt_t, unsigned int);
|
||||
struct gpt_ent *gpt_ent_backup(gpt_t, unsigned int);
|
||||
int gpt_usage(const char *, const struct gpt_cmd *);
|
||||
|
||||
uint8_t *utf16_to_utf8(uint16_t *);
|
||||
void utf16_to_utf8(const uint16_t *, uint8_t *, size_t);
|
||||
void utf8_to_utf16(const uint8_t *, uint16_t *, size_t);
|
||||
|
||||
#define GPT_FIND "ab:i:L:s:t:"
|
||||
|
@ -33,7 +33,7 @@
|
||||
__FBSDID("$FreeBSD: src/sbin/gpt/label.c,v 1.3 2006/10/04 18:20:25 marcel Exp $");
|
||||
#endif
|
||||
#ifdef __RCSID
|
||||
__RCSID("$NetBSD: label.c,v 1.23 2015/12/01 23:29:07 christos Exp $");
|
||||
__RCSID("$NetBSD: label.c,v 1.24 2015/12/02 04:07:11 christos Exp $");
|
||||
#endif
|
||||
|
||||
#include <sys/types.h>
|
||||
@ -70,7 +70,7 @@ static void
|
||||
change(struct gpt_ent *ent, void *v)
|
||||
{
|
||||
uint8_t *name = v;
|
||||
utf8_to_utf16(name, ent->ent_name, 36);
|
||||
utf8_to_utf16(name, ent->ent_name, __arraycount(ent->ent_name));
|
||||
}
|
||||
|
||||
static int
|
||||
|
@ -33,7 +33,7 @@
|
||||
__FBSDID("$FreeBSD: src/sbin/gpt/create.c,v 1.11 2005/08/31 01:47:19 marcel Exp $");
|
||||
#endif
|
||||
#ifdef __RCSID
|
||||
__RCSID("$NetBSD: restore.c,v 1.11 2015/12/01 16:32:19 christos Exp $");
|
||||
__RCSID("$NetBSD: restore.c,v 1.12 2015/12/02 04:07:11 christos Exp $");
|
||||
#endif
|
||||
|
||||
#include <sys/types.h>
|
||||
@ -331,7 +331,8 @@ restore(gpt_t gpt)
|
||||
propstr = prop_dictionary_get(gpt_dict, "name");
|
||||
if (propstr != NULL) {
|
||||
s = prop_string_cstring_nocopy(propstr);
|
||||
utf8_to_utf16((const uint8_t *)s, ent.ent_name, 36);
|
||||
utf8_to_utf16((const uint8_t *)s, ent.ent_name,
|
||||
__arraycount(ent.ent_name));
|
||||
}
|
||||
propnum = prop_dictionary_get(gpt_dict, "index");
|
||||
PROP_ERR(propnum);
|
||||
|
@ -33,7 +33,7 @@
|
||||
__FBSDID("$FreeBSD: src/sbin/gpt/show.c,v 1.14 2006/06/22 22:22:32 marcel Exp $");
|
||||
#endif
|
||||
#ifdef __RCSID
|
||||
__RCSID("$NetBSD: show.c,v 1.26 2015/12/01 23:29:07 christos Exp $");
|
||||
__RCSID("$NetBSD: show.c,v 1.27 2015/12/02 04:07:11 christos Exp $");
|
||||
#endif
|
||||
|
||||
#include <sys/types.h>
|
||||
@ -77,6 +77,7 @@ show(gpt_t gpt)
|
||||
struct mbr *mbr;
|
||||
struct gpt_ent *ent;
|
||||
unsigned int i;
|
||||
uint8_t utfbuf[__arraycount(ent->ent_name) * 3 + 1];
|
||||
|
||||
printf(" %*s", gpt->lbawidth, "start");
|
||||
printf(" %*s", gpt->lbawidth, "size");
|
||||
@ -134,8 +135,9 @@ show(gpt_t gpt)
|
||||
printf("GPT part ");
|
||||
ent = m->map_data;
|
||||
if (show_label) {
|
||||
printf("- \"%s\"",
|
||||
utf16_to_utf8(ent->ent_name));
|
||||
utf16_to_utf8(ent->ent_name, utfbuf,
|
||||
sizeof(utfbuf));
|
||||
printf("- \"%s\"", (char *)utfbuf);
|
||||
} else if (show_guid) {
|
||||
char buf[128];
|
||||
gpt_uuid_snprintf(
|
||||
@ -169,6 +171,7 @@ show_one(gpt_t gpt)
|
||||
map_t m;
|
||||
struct gpt_ent *ent;
|
||||
char s1[128], s2[128];
|
||||
uint8_t utfbuf[__arraycount(ent->ent_name) * 3 + 1];
|
||||
#ifdef HN_AUTOSCALE
|
||||
char human_num[5];
|
||||
#endif
|
||||
@ -212,7 +215,8 @@ show_one(gpt_t gpt)
|
||||
gpt_uuid_snprintf(s2, sizeof(s1), "%d", ent->ent_guid);
|
||||
printf("GUID: %s\n", s2);
|
||||
|
||||
printf("Label: %s\n", utf16_to_utf8(ent->ent_name));
|
||||
utf16_to_utf8(ent->ent_name, utfbuf, sizeof(utfbuf));
|
||||
printf("Label: %s\n", (char *)utfbuf);
|
||||
|
||||
printf("Attributes:\n");
|
||||
if (ent->ent_attr == 0)
|
||||
|
Loading…
Reference in New Issue
Block a user