other-licenses/android/ns_samedomain.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/other-licenses/android/ns_samedomain.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,226 @@
     1.4 +/*	$NetBSD: ns_samedomain.c,v 1.2 2004/05/20 20:35:05 christos Exp $	*/
     1.5 +
     1.6 +/*
     1.7 + * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
     1.8 + * Copyright (c) 1995,1999 by Internet Software Consortium.
     1.9 + *
    1.10 + * Permission to use, copy, modify, and distribute this software for any
    1.11 + * purpose with or without fee is hereby granted, provided that the above
    1.12 + * copyright notice and this permission notice appear in all copies.
    1.13 + *
    1.14 + * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
    1.15 + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
    1.16 + * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
    1.17 + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
    1.18 + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
    1.19 + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
    1.20 + * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
    1.21 + */
    1.22 +
    1.23 +/*
    1.24 + * This version of this file is derived from Android 2.3 "Gingerbread",
    1.25 + * which contains uncredited changes by Android/Google developers.  It has
    1.26 + * been modified in 2011 for use in the Android build of Mozilla Firefox by
    1.27 + * Mozilla contributors (including Michael Edwards <m.k.edwards@gmail.com>,
    1.28 + * and Steve Workman <sjhworkman@gmail.com>).
    1.29 + * These changes are offered under the same license as the original NetBSD
    1.30 + * file, whose copyright and license are unchanged above.
    1.31 + */
    1.32 +
    1.33 +#define ANDROID_CHANGES 1
    1.34 +#define MOZILLA_NECKO_EXCLUDE_CODE 1
    1.35 +
    1.36 +#include <sys/cdefs.h>
    1.37 +#ifndef lint
    1.38 +#ifdef notdef
    1.39 +static const char rcsid[] = "Id: ns_samedomain.c,v 1.1.2.2.4.2 2004/03/16 12:34:17 marka Exp";
    1.40 +#else
    1.41 +__RCSID("$NetBSD: ns_samedomain.c,v 1.2 2004/05/20 20:35:05 christos Exp $");
    1.42 +#endif
    1.43 +#endif
    1.44 +
    1.45 +#include <sys/types.h>
    1.46 +#include "arpa_nameser.h"
    1.47 +#include <errno.h>
    1.48 +#include <string.h>
    1.49 +
    1.50 +#ifndef MOZILLA_NECKO_EXCLUDE_CODE
    1.51 +#ifndef _LIBC
    1.52 +/*
    1.53 + * int
    1.54 + * ns_samedomain(a, b)
    1.55 + *	Check whether a name belongs to a domain.
    1.56 + * Inputs:
    1.57 + *	a - the domain whose ancestory is being verified
    1.58 + *	b - the potential ancestor we're checking against
    1.59 + * Return:
    1.60 + *	boolean - is a at or below b?
    1.61 + * Notes:
    1.62 + *	Trailing dots are first removed from name and domain.
    1.63 + *	Always compare complete subdomains, not only whether the
    1.64 + *	domain name is the trailing string of the given name.
    1.65 + *
    1.66 + *	"host.foobar.top" lies in "foobar.top" and in "top" and in ""
    1.67 + *	but NOT in "bar.top"
    1.68 + */
    1.69 +
    1.70 +int
    1.71 +ns_samedomain(const char *a, const char *b) {
    1.72 +	size_t la, lb;
    1.73 +	int diff, i, escaped;
    1.74 +	const char *cp;
    1.75 +
    1.76 +	la = strlen(a);
    1.77 +	lb = strlen(b);
    1.78 +
    1.79 +	/* Ignore a trailing label separator (i.e. an unescaped dot) in 'a'. */
    1.80 +	if (la != 0U && a[la - 1] == '.') {
    1.81 +		escaped = 0;
    1.82 +		/* Note this loop doesn't get executed if la==1. */
    1.83 +		for (i = la - 2; i >= 0; i--)
    1.84 +			if (a[i] == '\\') {
    1.85 +				if (escaped)
    1.86 +					escaped = 0;
    1.87 +				else
    1.88 +					escaped = 1;
    1.89 +			} else
    1.90 +				break;
    1.91 +		if (!escaped)
    1.92 +			la--;
    1.93 +	}
    1.94 +
    1.95 +	/* Ignore a trailing label separator (i.e. an unescaped dot) in 'b'. */
    1.96 +	if (lb != 0U && b[lb - 1] == '.') {
    1.97 +		escaped = 0;
    1.98 +		/* note this loop doesn't get executed if lb==1 */
    1.99 +		for (i = lb - 2; i >= 0; i--)
   1.100 +			if (b[i] == '\\') {
   1.101 +				if (escaped)
   1.102 +					escaped = 0;
   1.103 +				else
   1.104 +					escaped = 1;
   1.105 +			} else
   1.106 +				break;
   1.107 +		if (!escaped)
   1.108 +			lb--;
   1.109 +	}
   1.110 +
   1.111 +	/* lb == 0 means 'b' is the root domain, so 'a' must be in 'b'. */
   1.112 +	if (lb == 0U)
   1.113 +		return (1);
   1.114 +
   1.115 +	/* 'b' longer than 'a' means 'a' can't be in 'b'. */
   1.116 +	if (lb > la)
   1.117 +		return (0);
   1.118 +
   1.119 +	/* 'a' and 'b' being equal at this point indicates sameness. */
   1.120 +	if (lb == la)
   1.121 +		return (strncasecmp(a, b, lb) == 0);
   1.122 +
   1.123 +	/* Ok, we know la > lb. */
   1.124 +
   1.125 +	diff = la - lb;
   1.126 +
   1.127 +	/*
   1.128 +	 * If 'a' is only 1 character longer than 'b', then it can't be
   1.129 +	 * a subdomain of 'b' (because of the need for the '.' label
   1.130 +	 * separator).
   1.131 +	 */
   1.132 +	if (diff < 2)
   1.133 +		return (0);
   1.134 +
   1.135 +	/*
   1.136 +	 * If the character before the last 'lb' characters of 'b'
   1.137 +	 * isn't '.', then it can't be a match (this lets us avoid
   1.138 +	 * having "foobar.com" match "bar.com").
   1.139 +	 */
   1.140 +	if (a[diff - 1] != '.')
   1.141 +		return (0);
   1.142 +
   1.143 +	/*
   1.144 +	 * We're not sure about that '.', however.  It could be escaped
   1.145 +         * and thus not a really a label separator.
   1.146 +	 */
   1.147 +	escaped = 0;
   1.148 +	for (i = diff - 2; i >= 0; i--)
   1.149 +		if (a[i] == '\\') {
   1.150 +			if (escaped)
   1.151 +				escaped = 0;
   1.152 +			else
   1.153 +				escaped = 1;
   1.154 +		} else
   1.155 +			break;
   1.156 +	if (escaped)
   1.157 +		return (0);
   1.158 +
   1.159 +	/* Now compare aligned trailing substring. */
   1.160 +	cp = a + diff;
   1.161 +	return (strncasecmp(cp, b, lb) == 0);
   1.162 +}
   1.163 +
   1.164 +/*
   1.165 + * int
   1.166 + * ns_subdomain(a, b)
   1.167 + *	is "a" a subdomain of "b"?
   1.168 + */
   1.169 +int
   1.170 +ns_subdomain(const char *a, const char *b) {
   1.171 +	return (ns_samename(a, b) != 1 && ns_samedomain(a, b));
   1.172 +}
   1.173 +#endif
   1.174 +#endif
   1.175 +
   1.176 +/*
   1.177 + * int
   1.178 + * ns_makecanon(src, dst, dstsize)
   1.179 + *	make a canonical copy of domain name "src"
   1.180 + * notes:
   1.181 + *	foo -> foo.
   1.182 + *	foo. -> foo.
   1.183 + *	foo.. -> foo.
   1.184 + *	foo\. -> foo\..
   1.185 + *	foo\\. -> foo\\.
   1.186 + */
   1.187 +
   1.188 +int
   1.189 +ns_makecanon(const char *src, char *dst, size_t dstsize) {
   1.190 +	size_t n = strlen(src);
   1.191 +
   1.192 +	if (n + sizeof "." > dstsize) {			/* Note: sizeof == 2 */
   1.193 +		errno = EMSGSIZE;
   1.194 +		return (-1);
   1.195 +	}
   1.196 +	strcpy(dst, src);
   1.197 +	while (n >= 1U && dst[n - 1] == '.')		/* Ends in "." */
   1.198 +		if (n >= 2U && dst[n - 2] == '\\' &&	/* Ends in "\." */
   1.199 +		    (n < 3U || dst[n - 3] != '\\'))	/* But not "\\." */
   1.200 +			break;
   1.201 +		else
   1.202 +			dst[--n] = '\0';
   1.203 +	dst[n++] = '.';
   1.204 +	dst[n] = '\0';
   1.205 +	return (0);
   1.206 +}
   1.207 +
   1.208 +/*
   1.209 + * int
   1.210 + * ns_samename(a, b)
   1.211 + *	determine whether domain name "a" is the same as domain name "b"
   1.212 + * return:
   1.213 + *	-1 on error
   1.214 + *	0 if names differ
   1.215 + *	1 if names are the same
   1.216 + */
   1.217 +
   1.218 +int
   1.219 +ns_samename(const char *a, const char *b) {
   1.220 +	char ta[NS_MAXDNAME], tb[NS_MAXDNAME];
   1.221 +
   1.222 +	if (ns_makecanon(a, ta, sizeof ta) < 0 ||
   1.223 +	    ns_makecanon(b, tb, sizeof tb) < 0)
   1.224 +		return (-1);
   1.225 +	if (strcasecmp(ta, tb) == 0)
   1.226 +		return (1);
   1.227 +	else
   1.228 +		return (0);
   1.229 +}

mercurial