punycode: refactoring

use the correct term "label" instead of "component" when speaking
about the parts of a hostname.  Also, fix the sizes for max hostname
and label.
This commit is contained in:
Omar Polo 2021-01-28 16:25:59 +00:00
parent 473e4531d5
commit 35cf19e3f3
2 changed files with 14 additions and 13 deletions

5
gmid.h
View File

@ -51,8 +51,9 @@
#define HOSTSLEN 64 #define HOSTSLEN 64
#define LOCLEN 32 #define LOCLEN 32
/* RFC1034 imposes this limit. 63+1 for the NUL-terminator */ /* maximum hostname and label length, +1 for the NUL-terminator */
#define DOMAIN_NAME_LEN (63+1) #define DOMAIN_NAME_LEN (253+1)
#define LABEL_LEN (63+1)
#define LOGE(c, fmt, ...) logs(LOG_ERR, c, fmt, __VA_ARGS__) #define LOGE(c, fmt, ...) logs(LOG_ERR, c, fmt, __VA_ARGS__)
#define LOGW(c, fmt, ...) logs(LOG_WARNING, c, fmt, __VA_ARGS__) #define LOGW(c, fmt, ...) logs(LOG_WARNING, c, fmt, __VA_ARGS__)

22
puny.c
View File

@ -49,7 +49,7 @@ adapt(int delta, int numpoints, int firsttime)
} }
static const char * static const char *
copy_until_delimiter(const char *s, char *out, size_t len) copy_label(const char *s, char *out, size_t len)
{ {
char *end, *t; char *end, *t;
size_t l; size_t l;
@ -117,16 +117,16 @@ insert(char *out, size_t len, int codepoint, size_t i)
switch (l) { switch (l) {
case 2: case 2:
t[1] = ( codepoint & 0x3F) + 0x80; t[1] = ( codepoint & 0x3F) + 0x80;
t[0] = ((codepoint >> 6) & 0x1F) + 0xC0; t[0] = ((codepoint >> 6) & 0x1F) + 0xC0;
break; break;
case 3: case 3:
t[2] = ( codepoint & 0x3F) + 0x80; t[2] = ( codepoint & 0x3F) + 0x80;
t[1] = ((codepoint >> 6) & 0x3F) + 0x80; t[1] = ((codepoint >> 6) & 0x3F) + 0x80;
t[0] = ((codepoint >> 12) & 0x0F) + 0xE0; t[0] = ((codepoint >> 12) & 0x0F) + 0xE0;
break; break;
case 4: case 4:
t[3] = ( codepoint & 0x3F) + 0x80; t[3] = ( codepoint & 0x3F) + 0x80;
t[2] = ((codepoint >> 6) & 0x3F) + 0x80; t[2] = ((codepoint >> 6) & 0x3F) + 0x80;
t[1] = ((codepoint >> 12) & 0x3F) + 0x80; t[1] = ((codepoint >> 12) & 0x3F) + 0x80;
t[0] = ((codepoint >> 18) & 0x07) + 0xF0; t[0] = ((codepoint >> 18) & 0x07) + 0xF0;
break; break;
@ -152,7 +152,7 @@ decode(const char *str, char *out, size_t len)
str += 4; str += 4;
if (strchr(str, '-') != NULL) { if (strchr(str, '-') != NULL) {
if ((s = copy_until_delimiter(str, out, len)) == NULL) if ((s = copy_label(str, out, len)) == NULL)
return 0; return 0;
if (*s == '-') if (*s == '-')
s++; s++;
@ -205,7 +205,7 @@ decode(const char *str, char *out, size_t len)
} }
static const char * static const char *
end_of_component(const char *hostname) end_of_label(const char *hostname)
{ {
for (; *hostname != '\0' && *hostname != '.'; ++hostname) for (; *hostname != '\0' && *hostname != '.'; ++hostname)
; /* nop */ ; /* nop */
@ -215,7 +215,7 @@ end_of_component(const char *hostname)
int int
puny_decode(const char *hostname, char *out, size_t len) puny_decode(const char *hostname, char *out, size_t len)
{ {
char comp[DOMAIN_NAME_LEN]; char label[LABEL_LEN];
const char *s, *end; const char *s, *end;
size_t l; size_t l;
@ -227,13 +227,13 @@ puny_decode(const char *hostname, char *out, size_t len)
for (;;) { for (;;) {
end = end_of_component(s); end = end_of_component(s);
l = end - s; l = end - s;
if (l >= sizeof(comp)) if (l >= sizeof(label))
return 0; return 0;
memcpy(comp, s, end - s); memcpy(label, s, l);
comp[end - s] = '\0'; label[l] = '\0';
if (!decode(comp, out, len)) if (!decode(label, out, len))
return 0; return 0;
if (*end == '\0') if (*end == '\0')