Skip to content
Snippets Groups Projects
Commit 8b312d86 authored by Libor Peltan's avatar Libor Peltan
Browse files

Merge branch 'lowercase_simplify' into 'master'

Lowercase simplify

See merge request !673
parents 55817279 8ad9b946
Branches
Tags
1 merge request!673Lowercase simplify
Pipeline #1888 passed with stages
in 5 minutes and 3 seconds
/* Copyright (C) 2011 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
/* Copyright (C) 2017 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
......@@ -463,10 +463,11 @@ int knot_dname_to_lower(knot_dname_t *name)
if (name == NULL)
return KNOT_EINVAL;
/*! \todo Faster with \xdfdf mask. */
while (*name != '\0') {
for (uint8_t i = 0; i < *name; ++i)
name[1 + i] = knot_tolower(name[1 + i]);
uint8_t len = *name;
for (uint8_t i = 1; i <= len; ++i) {
name[i] = knot_tolower(name[i]);
}
name = (uint8_t *)knot_wire_next_label(name, NULL);
if (name == NULL) { /* Must not be used on compressed names. */
return KNOT_EMALF;
......@@ -653,39 +654,35 @@ void knot_dname_free(knot_dname_t **name, knot_mm_t *mm)
/*----------------------------------------------------------------------------*/
_public_
int knot_dname_cmp(const knot_dname_t *d1, const knot_dname_t *d2)
{
return knot_dname_cmp_wire(d1, d2, NULL);
}
/*----------------------------------------------------------------------------*/
_public_
int knot_dname_cmp_wire(const knot_dname_t *d1, const knot_dname_t *d2,
const uint8_t *pkt)
{
/* This would be hard to catch since -1 is a good result, assert instead. */
assert(d1 != NULL || d2 != NULL);
/* Convert to lookup format. */
uint8_t d1_lf[KNOT_DNAME_MAXLEN], d2_lf[KNOT_DNAME_MAXLEN];
if (knot_dname_lf(d1_lf, d1, pkt) < 0 || knot_dname_lf(d2_lf, d2, pkt) < 0) {
if (knot_dname_lf(d1_lf, d1, NULL) < 0 || knot_dname_lf(d2_lf, d2, NULL) < 0) {
assert(0); /* This must not happened as the d1, d2 are checked. */
return KNOT_EINVAL;
}
/* Compare common part. */
uint8_t common = d1_lf[0];
if (common > d2_lf[0])
if (common > d2_lf[0]) {
common = d2_lf[0];
int ret = memcmp(d1_lf+1, d2_lf+1, common);
if (ret != 0)
}
int ret = memcmp(d1_lf + 1, d2_lf + 1, common);
if (ret != 0) {
return ret;
}
/* If they match, compare lengths. */
if (d1_lf[0] < d2_lf[0])
if (d1_lf[0] < d2_lf[0]) {
return -1;
if (d1_lf[0] > d2_lf[0])
} else if (d1_lf[0] > d2_lf[0]) {
return 1;
return 0;
} else {
return 0;
}
}
/*----------------------------------------------------------------------------*/
......@@ -791,28 +788,28 @@ int knot_dname_lf(uint8_t *dst, const knot_dname_t *src, const uint8_t *pkt)
uint8_t *len = dst++;
*len = '\0';
*dst = '\0';
const uint8_t* l = src;
/*! \todo This could be made as offsets to pkt? */
const uint8_t *l = src;
const uint8_t* lstack[KNOT_DNAME_MAXLABELS];
const uint8_t **sp = lstack;
while(*l != 0) { /* build label stack */
while (*l != 0) { /* build label stack */
*sp++ = l;
l = knot_wire_next_label(l, pkt);
}
while(sp != lstack) { /* consume stack */
while (sp != lstack) { /* consume stack */
l = *--sp; /* fetch rightmost label */
memcpy(dst, l+1, *l); /* write label */
for (int i = 0; i < *l; ++i) { /* convert to lowercase */
dst[i] = knot_tolower(dst[i]);
uint8_t label_len = *l++;
for (int i = 0; i < label_len; ++i) {
dst[i] = knot_tolower(l[i]); /* write label in lowercase */
}
dst += *l;
*dst++ = '\0'; /* label separator */
*len += *l + 1;
dst += label_len;
*dst++ = '\0'; /* label separator */
*len += label_len + 1;
}
/* root label special case */
if (*len == 0)
if (*len == 0) {
*len = 1; /* \x00 */
}
return KNOT_EOK;
}
/* Copyright (C) 2011 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
/* Copyright (C) 2017 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
......@@ -281,7 +281,11 @@ knot_dname_t *knot_dname_replace_suffix(const knot_dname_t *name, unsigned label
void knot_dname_free(knot_dname_t **name, knot_mm_t *mm);
/*!
* \brief Compares two domain names (case sensitive).
* \brief Compares two domain names by labels (case sensitive).
*
* \warning Since it would be hard to catch errors, because negative value
* is also a good result, there are assertions that expect neither
* d1 or d2 to be NULL.
*
* \param d1 First domain name.
* \param d2 Second domain name.
......@@ -293,27 +297,6 @@ void knot_dname_free(knot_dname_t **name, knot_mm_t *mm);
_pure_
int knot_dname_cmp(const knot_dname_t *d1, const knot_dname_t *d2);
/*!
* \brief Compare domain name by labels.
*
* \todo No case insensitivity, flags...
*
* \warning Since it would be hard to catch errors, because negative value
* is also a good result, there are assertions that expect neither
* d1 or d2 to be NULL.
*
* \param d1 Domain name.
* \param d2 Domain name.
* \param pkt Packet wire related to names (or NULL).
*
* \retval 0 if they are identical
* \retval 1 if d1 > d2
* \retval -1 if d1 < d2
*/
_pure_
int knot_dname_cmp_wire(const knot_dname_t *d1, const knot_dname_t *d2,
const uint8_t *pkt);
/*!
* \brief Compares two domain names (case sensitive).
*
......@@ -381,7 +364,7 @@ int knot_dname_align(const uint8_t **d1, uint8_t d1_labels,
*
* Example:
* Name: lake.example.com. Wire: \x04lake\x07example\x03com\x00
* Lookup format com\x00example\x00lake\x00
* Lookup format \x11com\x00example\x00lake\x00
*
* Maximum length of such a domain name is KNOT_DNAME_MAXLEN characters.
*
......
/* Copyright (C) 2013 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
/* Copyright (C) 2017 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
......@@ -33,8 +33,8 @@ static bool compr_label_match(const uint8_t *n, const uint8_t *p)
}
uint8_t len = *n;
for (uint8_t i = 0; i < len; ++i) {
if (knot_tolower(n[1 + i]) != knot_tolower(p[1 + i])) {
for (uint8_t i = 1; i <= len; ++i) {
if (knot_tolower(n[i]) != knot_tolower(p[i])) {
return false;
}
}
......@@ -96,7 +96,7 @@ int knot_compr_put_dname(const knot_dname_t *dname, uint8_t *dst, uint16_t max,
const knot_dname_t *next_suffix = knot_wire_next_label(suffix, compr->wire);
/* Two labels match, extend suffix length. */
if (dname[0] != suffix[0] || !compr_label_match(dname, suffix)) {
if (!compr_label_match(dname, suffix)) {
/* If they don't match, write unmatched labels. */
uint16_t mismatch_len = (dname - match_begin) + (*dname + 1);
WRITE_LABEL(dst, written, match_begin, max, mismatch_len);
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment