other-licenses/android/res_comp.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/other-licenses/android/res_comp.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,282 @@
     1.4 +/*	$NetBSD: res_comp.c,v 1.6 2004/05/22 23:47:09 christos Exp $	*/
     1.5 +
     1.6 +/*
     1.7 + * Copyright (c) 1985, 1993
     1.8 + *    The Regents of the University of California.  All rights reserved.
     1.9 + *
    1.10 + * Redistribution and use in source and binary forms, with or without
    1.11 + * modification, are permitted provided that the following conditions
    1.12 + * are met:
    1.13 + * 1. Redistributions of source code must retain the above copyright
    1.14 + *    notice, this list of conditions and the following disclaimer.
    1.15 + * 2. Redistributions in binary form must reproduce the above copyright
    1.16 + *    notice, this list of conditions and the following disclaimer in the
    1.17 + *    documentation and/or other materials provided with the distribution.
    1.18 + * 3. All advertising materials mentioning features or use of this software
    1.19 + *    must display the following acknowledgement:
    1.20 + * 	This product includes software developed by the University of
    1.21 + * 	California, Berkeley and its contributors.
    1.22 + * 4. Neither the name of the University nor the names of its contributors
    1.23 + *    may be used to endorse or promote products derived from this software
    1.24 + *    without specific prior written permission.
    1.25 + *
    1.26 + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
    1.27 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.28 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.29 + * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
    1.30 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    1.31 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    1.32 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    1.33 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    1.34 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    1.35 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    1.36 + * SUCH DAMAGE.
    1.37 + */
    1.38 +
    1.39 +/*
    1.40 + * Portions Copyright (c) 1993 by Digital Equipment Corporation.
    1.41 + *
    1.42 + * Permission to use, copy, modify, and distribute this software for any
    1.43 + * purpose with or without fee is hereby granted, provided that the above
    1.44 + * copyright notice and this permission notice appear in all copies, and that
    1.45 + * the name of Digital Equipment Corporation not be used in advertising or
    1.46 + * publicity pertaining to distribution of the document or software without
    1.47 + * specific, written prior permission.
    1.48 + *
    1.49 + * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
    1.50 + * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
    1.51 + * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
    1.52 + * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
    1.53 + * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
    1.54 + * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
    1.55 + * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
    1.56 + * SOFTWARE.
    1.57 + */
    1.58 +
    1.59 +/*
    1.60 + * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
    1.61 + * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
    1.62 + *
    1.63 + * Permission to use, copy, modify, and distribute this software for any
    1.64 + * purpose with or without fee is hereby granted, provided that the above
    1.65 + * copyright notice and this permission notice appear in all copies.
    1.66 + *
    1.67 + * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
    1.68 + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
    1.69 + * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
    1.70 + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
    1.71 + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
    1.72 + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
    1.73 + * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
    1.74 + */
    1.75 +
    1.76 +/*
    1.77 + * This version of this file is derived from Android 2.3 "Gingerbread",
    1.78 + * which contains uncredited changes by Android/Google developers.  It has
    1.79 + * been modified in 2011 for use in the Android build of Mozilla Firefox by
    1.80 + * Mozilla contributors (including Michael Edwards <m.k.edwards@gmail.com>,
    1.81 + * and Steve Workman <sjhworkman@gmail.com>).
    1.82 + * These changes are offered under the same license as the original NetBSD
    1.83 + * file, whose copyright and license are unchanged above.
    1.84 + */
    1.85 +
    1.86 +#define ANDROID_CHANGES 1
    1.87 +#define MOZILLA_NECKO_EXCLUDE_CODE 1
    1.88 +
    1.89 +#include <sys/cdefs.h>
    1.90 +#if defined(LIBC_SCCS) && !defined(lint)
    1.91 +#ifdef notdef
    1.92 +static const char sccsid[] = "@(#)res_comp.c	8.1 (Berkeley) 6/4/93";
    1.93 +static const char rcsid[] = "Id: res_comp.c,v 1.1.2.1.4.1 2004/03/09 08:33:54 marka Exp";
    1.94 +#else
    1.95 +__RCSID("$NetBSD: res_comp.c,v 1.6 2004/05/22 23:47:09 christos Exp $");
    1.96 +#endif
    1.97 +#endif /* LIBC_SCCS and not lint */
    1.98 +
    1.99 +#include <sys/types.h>
   1.100 +#include <sys/param.h>
   1.101 +#include <netinet/in.h>
   1.102 +#include "arpa_nameser.h"
   1.103 +#include <ctype.h>
   1.104 +#ifdef ANDROID_CHANGES
   1.105 +#include "resolv_private.h"
   1.106 +#else
   1.107 +#include <resolv.h>
   1.108 +#endif
   1.109 +#include <stdio.h>
   1.110 +#include <string.h>
   1.111 +#include <unistd.h>
   1.112 +
   1.113 +/*
   1.114 + * Expand compressed domain name 'src' to full domain name.
   1.115 + * 'msg' is a pointer to the begining of the message,
   1.116 + * 'eom' points to the first location after the message,
   1.117 + * 'dst' is a pointer to a buffer of size 'dstsiz' for the result.
   1.118 + * Return size of compressed name or -1 if there was an error.
   1.119 + */
   1.120 +int
   1.121 +dn_expand(const u_char *msg, const u_char *eom, const u_char *src,
   1.122 +	  char *dst, int dstsiz)
   1.123 +{
   1.124 +	int n = ns_name_uncompress(msg, eom, src, dst, (size_t)dstsiz);
   1.125 +
   1.126 +	if (n > 0 && dst[0] == '.')
   1.127 +		dst[0] = '\0';
   1.128 +	return (n);
   1.129 +}
   1.130 +
   1.131 +/*
   1.132 + * Pack domain name 'exp_dn' in presentation form into 'comp_dn'.
   1.133 + * Return the size of the compressed name or -1.
   1.134 + * 'length' is the size of the array pointed to by 'comp_dn'.
   1.135 + */
   1.136 +int
   1.137 +dn_comp(const char *src, u_char *dst, int dstsiz,
   1.138 +	u_char **dnptrs, u_char **lastdnptr)
   1.139 +{
   1.140 +	return (ns_name_compress(src, dst, (size_t)dstsiz,
   1.141 +				 (const u_char **)dnptrs,
   1.142 +				 (const u_char **)lastdnptr));
   1.143 +}
   1.144 +
   1.145 +/*
   1.146 + * Skip over a compressed domain name. Return the size or -1.
   1.147 + */
   1.148 +int
   1.149 +dn_skipname(const u_char *ptr, const u_char *eom) {
   1.150 +	const u_char *saveptr = ptr;
   1.151 +
   1.152 +	if (ns_name_skip(&ptr, eom) == -1)
   1.153 +		return (-1);
   1.154 +	return (ptr - saveptr);
   1.155 +}
   1.156 +
   1.157 +/*
   1.158 + * Verify that a domain name uses an acceptable character set.
   1.159 + */
   1.160 +
   1.161 +/*
   1.162 + * Note the conspicuous absence of ctype macros in these definitions.  On
   1.163 + * non-ASCII hosts, we can't depend on string literals or ctype macros to
   1.164 + * tell us anything about network-format data.  The rest of the BIND system
   1.165 + * is not careful about this, but for some reason, we're doing it right here.
   1.166 + */
   1.167 +
   1.168 +/* BIONIC: We also accept underscores in the middle of labels.
   1.169 + *         This extension is needed to make resolution on some VPN networks
   1.170 + *         work properly.
   1.171 + */
   1.172 +
   1.173 +#define PERIOD 0x2e
   1.174 +#define	hyphenchar(c) ((c) == 0x2d)
   1.175 +#define bslashchar(c) ((c) == 0x5c)
   1.176 +#define periodchar(c) ((c) == PERIOD)
   1.177 +#define asterchar(c) ((c) == 0x2a)
   1.178 +#define alphachar(c) (((c) >= 0x41 && (c) <= 0x5a) \
   1.179 +		   || ((c) >= 0x61 && (c) <= 0x7a))
   1.180 +#define digitchar(c) ((c) >= 0x30 && (c) <= 0x39)
   1.181 +#define underscorechar(c)  ((c) == 0x5f)
   1.182 +
   1.183 +#define borderchar(c) (alphachar(c) || digitchar(c))
   1.184 +#define middlechar(c) (borderchar(c) || hyphenchar(c) || underscorechar(c))
   1.185 +#define	domainchar(c) ((c) > 0x20 && (c) < 0x7f)
   1.186 +
   1.187 +int
   1.188 +res_hnok(const char *dn) {
   1.189 +	int pch = PERIOD, ch = *dn++;
   1.190 +
   1.191 +	while (ch != '\0') {
   1.192 +		int nch = *dn++;
   1.193 +
   1.194 +		if (periodchar(ch)) {
   1.195 +			;
   1.196 +		} else if (periodchar(pch)) {
   1.197 +			if (!borderchar(ch))
   1.198 +				return (0);
   1.199 +		} else if (periodchar(nch) || nch == '\0') {
   1.200 +			if (!borderchar(ch))
   1.201 +				return (0);
   1.202 +		} else {
   1.203 +			if (!middlechar(ch))
   1.204 +				return (0);
   1.205 +		}
   1.206 +		pch = ch, ch = nch;
   1.207 +	}
   1.208 +	return (1);
   1.209 +}
   1.210 +
   1.211 +#ifndef MOZILLA_NECKO_EXCLUDE_CODE
   1.212 +/*
   1.213 + * hostname-like (A, MX, WKS) owners can have "*" as their first label
   1.214 + * but must otherwise be as a host name.
   1.215 + */
   1.216 +int
   1.217 +res_ownok(const char *dn) {
   1.218 +	if (asterchar(dn[0])) {
   1.219 +		if (periodchar(dn[1]))
   1.220 +			return (res_hnok(dn+2));
   1.221 +		if (dn[1] == '\0')
   1.222 +			return (1);
   1.223 +	}
   1.224 +	return (res_hnok(dn));
   1.225 +}
   1.226 +
   1.227 +/*
   1.228 + * SOA RNAMEs and RP RNAMEs can have any printable character in their first
   1.229 + * label, but the rest of the name has to look like a host name.
   1.230 + */
   1.231 +int
   1.232 +res_mailok(const char *dn) {
   1.233 +	int ch, escaped = 0;
   1.234 +
   1.235 +	/* "." is a valid missing representation */
   1.236 +	if (*dn == '\0')
   1.237 +		return (1);
   1.238 +
   1.239 +	/* otherwise <label>.<hostname> */
   1.240 +	while ((ch = *dn++) != '\0') {
   1.241 +		if (!domainchar(ch))
   1.242 +			return (0);
   1.243 +		if (!escaped && periodchar(ch))
   1.244 +			break;
   1.245 +		if (escaped)
   1.246 +			escaped = 0;
   1.247 +		else if (bslashchar(ch))
   1.248 +			escaped = 1;
   1.249 +	}
   1.250 +	if (periodchar(ch))
   1.251 +		return (res_hnok(dn));
   1.252 +	return (0);
   1.253 +}
   1.254 +
   1.255 +/*
   1.256 + * This function is quite liberal, since RFC 1034's character sets are only
   1.257 + * recommendations.
   1.258 + */
   1.259 +int
   1.260 +res_dnok(const char *dn) {
   1.261 +	int ch;
   1.262 +
   1.263 +	while ((ch = *dn++) != '\0')
   1.264 +		if (!domainchar(ch))
   1.265 +			return (0);
   1.266 +	return (1);
   1.267 +}
   1.268 +#endif
   1.269 +
   1.270 +#ifdef BIND_4_COMPAT
   1.271 +/*
   1.272 + * This module must export the following externally-visible symbols:
   1.273 + *	___putlong
   1.274 + *	___putshort
   1.275 + *	__getlong
   1.276 + *	__getshort
   1.277 + * Note that one _ comes from C and the others come from us.
   1.278 + */
   1.279 +void __putlong(u_int32_t src, u_char *dst) { ns_put32(src, dst); }
   1.280 +void __putshort(u_int16_t src, u_char *dst) { ns_put16(src, dst); }
   1.281 +#ifndef __ultrix__
   1.282 +u_int32_t _getlong(const u_char *src) { return (ns_get32(src)); }
   1.283 +u_int16_t _getshort(const u_char *src) { return (ns_get16(src)); }
   1.284 +#endif /*__ultrix__*/
   1.285 +#endif /*BIND_4_COMPAT*/

mercurial