1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/other-licenses/android/ns_parse.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,222 @@ 1.4 +/* $NetBSD: ns_parse.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) 1996,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_parse.c,v 1.3.2.1.4.1 2004/03/09 08:33:44 marka Exp"; 1.40 +#else 1.41 +__RCSID("$NetBSD: ns_parse.c,v 1.2 2004/05/20 20:35:05 christos Exp $"); 1.42 +#endif 1.43 +#endif 1.44 + 1.45 +/* Import. */ 1.46 + 1.47 +#include <sys/types.h> 1.48 + 1.49 +#include <netinet/in.h> 1.50 +#include "arpa_nameser.h" 1.51 + 1.52 +#include <errno.h> 1.53 +#ifdef ANDROID_CHANGES 1.54 +#include "resolv_private.h" 1.55 +#else 1.56 +#include <resolv.h> 1.57 +#endif 1.58 +#include <string.h> 1.59 + 1.60 +/* Forward. */ 1.61 + 1.62 +static void setsection(ns_msg *msg, ns_sect sect); 1.63 + 1.64 +/* Macros. */ 1.65 + 1.66 +#define RETERR(err) do { errno = (err); return (-1); } while (/*NOTREACHED*//*CONSTCOND*/0) 1.67 + 1.68 +/* Public. */ 1.69 + 1.70 +/* These need to be in the same order as the nres.h:ns_flag enum. */ 1.71 +const struct _ns_flagdata _ns_flagdata[16] = { 1.72 + { 0x8000, 15 }, /* qr. */ 1.73 + { 0x7800, 11 }, /* opcode. */ 1.74 + { 0x0400, 10 }, /* aa. */ 1.75 + { 0x0200, 9 }, /* tc. */ 1.76 + { 0x0100, 8 }, /* rd. */ 1.77 + { 0x0080, 7 }, /* ra. */ 1.78 + { 0x0040, 6 }, /* z. */ 1.79 + { 0x0020, 5 }, /* ad. */ 1.80 + { 0x0010, 4 }, /* cd. */ 1.81 + { 0x000f, 0 }, /* rcode. */ 1.82 + { 0x0000, 0 }, /* expansion (1/6). */ 1.83 + { 0x0000, 0 }, /* expansion (2/6). */ 1.84 + { 0x0000, 0 }, /* expansion (3/6). */ 1.85 + { 0x0000, 0 }, /* expansion (4/6). */ 1.86 + { 0x0000, 0 }, /* expansion (5/6). */ 1.87 + { 0x0000, 0 }, /* expansion (6/6). */ 1.88 +}; 1.89 + 1.90 +int ns_msg_getflag(ns_msg handle, int flag) { 1.91 + return((u_int32_t)((handle)._flags & _ns_flagdata[flag].mask) >> _ns_flagdata[flag].shift); 1.92 +} 1.93 + 1.94 +int 1.95 +ns_skiprr(const u_char *ptr, const u_char *eom, ns_sect section, int count) { 1.96 + const u_char *optr = ptr; 1.97 + 1.98 + for (; count > 0; count--) { 1.99 + int b, rdlength; 1.100 + 1.101 + b = dn_skipname(ptr, eom); 1.102 + if (b < 0) 1.103 + RETERR(EMSGSIZE); 1.104 + ptr += b/*Name*/ + NS_INT16SZ/*Type*/ + NS_INT16SZ/*Class*/; 1.105 + if (section != ns_s_qd) { 1.106 + if (ptr + NS_INT32SZ + NS_INT16SZ > eom) 1.107 + RETERR(EMSGSIZE); 1.108 + ptr += NS_INT32SZ/*TTL*/; 1.109 + NS_GET16(rdlength, ptr); 1.110 + ptr += rdlength/*RData*/; 1.111 + } 1.112 + } 1.113 + if (ptr > eom) 1.114 + RETERR(EMSGSIZE); 1.115 + return (ptr - optr); 1.116 +} 1.117 + 1.118 +int 1.119 +ns_initparse(const u_char *msg, int msglen, ns_msg *handle) { 1.120 + const u_char *eom = msg + msglen; 1.121 + int i; 1.122 + 1.123 + memset(handle, 0x5e, sizeof *handle); 1.124 + handle->_msg = msg; 1.125 + handle->_eom = eom; 1.126 + if (msg + NS_INT16SZ > eom) 1.127 + RETERR(EMSGSIZE); 1.128 + NS_GET16(handle->_id, msg); 1.129 + if (msg + NS_INT16SZ > eom) 1.130 + RETERR(EMSGSIZE); 1.131 + NS_GET16(handle->_flags, msg); 1.132 + for (i = 0; i < ns_s_max; i++) { 1.133 + if (msg + NS_INT16SZ > eom) 1.134 + RETERR(EMSGSIZE); 1.135 + NS_GET16(handle->_counts[i], msg); 1.136 + } 1.137 + for (i = 0; i < ns_s_max; i++) 1.138 + if (handle->_counts[i] == 0) 1.139 + handle->_sections[i] = NULL; 1.140 + else { 1.141 + int b = ns_skiprr(msg, eom, (ns_sect)i, 1.142 + handle->_counts[i]); 1.143 + 1.144 + if (b < 0) 1.145 + return (-1); 1.146 + handle->_sections[i] = msg; 1.147 + msg += b; 1.148 + } 1.149 + if (msg != eom) 1.150 + RETERR(EMSGSIZE); 1.151 + setsection(handle, ns_s_max); 1.152 + return (0); 1.153 +} 1.154 + 1.155 +int 1.156 +ns_parserr(ns_msg *handle, ns_sect section, int rrnum, ns_rr *rr) { 1.157 + int b; 1.158 + 1.159 + /* Make section right. */ 1.160 + if ((unsigned)section >= (unsigned)ns_s_max) 1.161 + RETERR(ENODEV); 1.162 + if (section != handle->_sect) 1.163 + setsection(handle, section); 1.164 + 1.165 + /* Make rrnum right. */ 1.166 + if (rrnum == -1) 1.167 + rrnum = handle->_rrnum; 1.168 + if (rrnum < 0 || rrnum >= handle->_counts[(int)section]) 1.169 + RETERR(ENODEV); 1.170 + if (rrnum < handle->_rrnum) 1.171 + setsection(handle, section); 1.172 + if (rrnum > handle->_rrnum) { 1.173 + b = ns_skiprr(handle->_msg_ptr, handle->_eom, section, 1.174 + rrnum - handle->_rrnum); 1.175 + 1.176 + if (b < 0) 1.177 + return (-1); 1.178 + handle->_msg_ptr += b; 1.179 + handle->_rrnum = rrnum; 1.180 + } 1.181 + 1.182 + /* Do the parse. */ 1.183 + b = dn_expand(handle->_msg, handle->_eom, 1.184 + handle->_msg_ptr, rr->name, NS_MAXDNAME); 1.185 + if (b < 0) 1.186 + return (-1); 1.187 + handle->_msg_ptr += b; 1.188 + if (handle->_msg_ptr + NS_INT16SZ + NS_INT16SZ > handle->_eom) 1.189 + RETERR(EMSGSIZE); 1.190 + NS_GET16(rr->type, handle->_msg_ptr); 1.191 + NS_GET16(rr->rr_class, handle->_msg_ptr); 1.192 + if (section == ns_s_qd) { 1.193 + rr->ttl = 0; 1.194 + rr->rdlength = 0; 1.195 + rr->rdata = NULL; 1.196 + } else { 1.197 + if (handle->_msg_ptr + NS_INT32SZ + NS_INT16SZ > handle->_eom) 1.198 + RETERR(EMSGSIZE); 1.199 + NS_GET32(rr->ttl, handle->_msg_ptr); 1.200 + NS_GET16(rr->rdlength, handle->_msg_ptr); 1.201 + if (handle->_msg_ptr + rr->rdlength > handle->_eom) 1.202 + RETERR(EMSGSIZE); 1.203 + rr->rdata = handle->_msg_ptr; 1.204 + handle->_msg_ptr += rr->rdlength; 1.205 + } 1.206 + if (++handle->_rrnum > handle->_counts[(int)section]) 1.207 + setsection(handle, (ns_sect)((int)section + 1)); 1.208 + 1.209 + /* All done. */ 1.210 + return (0); 1.211 +} 1.212 + 1.213 +/* Private. */ 1.214 + 1.215 +static void 1.216 +setsection(ns_msg *msg, ns_sect sect) { 1.217 + msg->_sect = sect; 1.218 + if (sect == ns_s_max) { 1.219 + msg->_rrnum = -1; 1.220 + msg->_msg_ptr = NULL; 1.221 + } else { 1.222 + msg->_rrnum = 0; 1.223 + msg->_msg_ptr = msg->_sections[(int)sect]; 1.224 + } 1.225 +}