Commit 16766507 authored by Lubos Slovak's avatar Lubos Slovak

Optimized dname_compare_labels().

parent 3757e792
...@@ -144,21 +144,23 @@ static int dnslib_dname_compare_labels(const uint8_t *label1, ...@@ -144,21 +144,23 @@ static int dnslib_dname_compare_labels(const uint8_t *label1,
} }
if (i < label_length) { // difference in some octet if (i < label_length) { // difference in some octet
if (tolower(*pos1) < tolower(*pos2)) { return (tolower(*pos1) - tolower(*pos2));
return -1; // if (tolower(*pos1) < tolower(*pos2)) {
} else { // return -1;
assert(tolower(*pos1) > tolower(*pos2)); // } else {
return 1; // assert(tolower(*pos1) > tolower(*pos2));
} // return 1;
// }
} }
if (label1[0] < label2[0]) { // one label shorter return (label1[0] - label2[0]);
return -1; // if (label1[0] < label2[0]) { // one label shorter
} else if (label1[0] > label2[0]) { // return -1;
return 1; // } else if (label1[0] > label2[0]) {
} // return 1;
// }
return 0; // return 0;
} }
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
...@@ -431,6 +433,7 @@ dnslib_dname_t *dnslib_dname_left_chop(const dnslib_dname_t *dname) ...@@ -431,6 +433,7 @@ dnslib_dname_t *dnslib_dname_left_chop(const dnslib_dname_t *dname)
int dnslib_dname_is_subdomain(const dnslib_dname_t *sub, int dnslib_dname_is_subdomain(const dnslib_dname_t *sub,
const dnslib_dname_t *domain) const dnslib_dname_t *domain)
{ {
DEBUG_DNSLIB_DNAME(
char *name1 = dnslib_dname_to_str(sub); char *name1 = dnslib_dname_to_str(sub);
char *name2 = dnslib_dname_to_str(domain); char *name2 = dnslib_dname_to_str(domain);
...@@ -438,6 +441,7 @@ int dnslib_dname_is_subdomain(const dnslib_dname_t *sub, ...@@ -438,6 +441,7 @@ int dnslib_dname_is_subdomain(const dnslib_dname_t *sub,
name1, name2); name1, name2);
free(name1); free(name1);
free(name2); free(name2);
);
if (sub == domain) { if (sub == domain) {
return 0; return 0;
...@@ -545,6 +549,7 @@ dnslib_dname_t *dnslib_dname_replace_suffix(const dnslib_dname_t *dname, ...@@ -545,6 +549,7 @@ dnslib_dname_t *dnslib_dname_replace_suffix(const dnslib_dname_t *dname,
int size, int size,
const dnslib_dname_t *suffix) const dnslib_dname_t *suffix)
{ {
DEBUG_DNSLIB_DNAME(
char *name = dnslib_dname_to_str(dname); char *name = dnslib_dname_to_str(dname);
debug_dnslib_dname("Replacing suffix of name %s, size %d with ", name, debug_dnslib_dname("Replacing suffix of name %s, size %d with ", name,
size); size);
...@@ -552,7 +557,7 @@ dnslib_dname_t *dnslib_dname_replace_suffix(const dnslib_dname_t *dname, ...@@ -552,7 +557,7 @@ dnslib_dname_t *dnslib_dname_replace_suffix(const dnslib_dname_t *dname,
name = dnslib_dname_to_str(suffix); name = dnslib_dname_to_str(suffix);
debug_dnslib_dname("%s (size %d)\n", name, suffix->size); debug_dnslib_dname("%s (size %d)\n", name, suffix->size);
free(name); free(name);
);
dnslib_dname_t *res = dnslib_dname_new(); dnslib_dname_t *res = dnslib_dname_new();
CHECK_ALLOC(res, NULL); CHECK_ALLOC(res, NULL);
...@@ -604,6 +609,7 @@ void dnslib_dname_free(dnslib_dname_t **dname) ...@@ -604,6 +609,7 @@ void dnslib_dname_free(dnslib_dname_t **dname)
int dnslib_dname_compare(const dnslib_dname_t *d1, const dnslib_dname_t *d2) int dnslib_dname_compare(const dnslib_dname_t *d1, const dnslib_dname_t *d2)
{ {
DEBUG_DNSLIB_DNAME(
char *name1 = dnslib_dname_to_str(d1); char *name1 = dnslib_dname_to_str(d1);
char *name2 = dnslib_dname_to_str(d2); char *name2 = dnslib_dname_to_str(d2);
...@@ -611,6 +617,7 @@ int dnslib_dname_compare(const dnslib_dname_t *d1, const dnslib_dname_t *d2) ...@@ -611,6 +617,7 @@ int dnslib_dname_compare(const dnslib_dname_t *d1, const dnslib_dname_t *d2)
name1, name2); name1, name2);
free(name1); free(name1);
free(name2); free(name2);
);
if (d1 == d2) { if (d1 == d2) {
return 0; return 0;
......
...@@ -238,8 +238,8 @@ void dnslib_dname_free(dnslib_dname_t **dname); ...@@ -238,8 +238,8 @@ void dnslib_dname_free(dnslib_dname_t **dname);
* \param d1 First domain name. * \param d1 First domain name.
* \param d2 Second domain name. * \param d2 Second domain name.
* *
* \retval -1 if \a d1 goes before \a d2 in canonical order. * \retval < 0 if \a d1 goes before \a d2 in canonical order.
* \retval 1 if \a d1 goes after \a d2 in canonical order. * \retval > 0 if \a d1 goes after \a d2 in canonical order.
* \retval 0 if the domain names are identical. * \retval 0 if the domain names are identical.
*/ */
int dnslib_dname_compare(const dnslib_dname_t *d1, const dnslib_dname_t *d2); int dnslib_dname_compare(const dnslib_dname_t *d1, const dnslib_dname_t *d2);
......
Markdown is supported
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