other-licenses/android/ns_ttl.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/other-licenses/android/ns_ttl.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,179 @@
     1.4 +/*	$NetBSD: ns_ttl.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_ttl.c,v 1.1.206.1 2004/03/09 08:33:45 marka Exp";
    1.40 +#else
    1.41 +__RCSID("$NetBSD: ns_ttl.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 "arpa_nameser.h"
    1.48 +
    1.49 +#include <ctype.h>
    1.50 +#include <errno.h>
    1.51 +#include <stdio.h>
    1.52 +#include <string.h>
    1.53 +
    1.54 +#ifdef SPRINTF_CHAR
    1.55 +# define SPRINTF(x) strlen(sprintf/**/x)
    1.56 +#else
    1.57 +# define SPRINTF(x) ((size_t)sprintf x)
    1.58 +#endif
    1.59 +
    1.60 +/* Forward. */
    1.61 +
    1.62 +static int	fmt1(int t, char s, char **buf, size_t *buflen);
    1.63 +
    1.64 +/* Macros. */
    1.65 +
    1.66 +#define T(x) do { if ((x) < 0) return (-1); } while(0)
    1.67 +
    1.68 +/* Public. */
    1.69 +
    1.70 +int
    1.71 +ns_format_ttl(u_long src, char *dst, size_t dstlen) {
    1.72 +	char *odst = dst;
    1.73 +	int secs, mins, hours, days, weeks, x;
    1.74 +	char *p;
    1.75 +
    1.76 +	secs = src % 60;   src /= 60;
    1.77 +	mins = src % 60;   src /= 60;
    1.78 +	hours = src % 24;  src /= 24;
    1.79 +	days = src % 7;    src /= 7;
    1.80 +	weeks = src;       src = 0;
    1.81 +
    1.82 +	x = 0;
    1.83 +	if (weeks) {
    1.84 +		T(fmt1(weeks, 'W', &dst, &dstlen));
    1.85 +		x++;
    1.86 +	}
    1.87 +	if (days) {
    1.88 +		T(fmt1(days, 'D', &dst, &dstlen));
    1.89 +		x++;
    1.90 +	}
    1.91 +	if (hours) {
    1.92 +		T(fmt1(hours, 'H', &dst, &dstlen));
    1.93 +		x++;
    1.94 +	}
    1.95 +	if (mins) {
    1.96 +		T(fmt1(mins, 'M', &dst, &dstlen));
    1.97 +		x++;
    1.98 +	}
    1.99 +	if (secs || !(weeks || days || hours || mins)) {
   1.100 +		T(fmt1(secs, 'S', &dst, &dstlen));
   1.101 +		x++;
   1.102 +	}
   1.103 +
   1.104 +	if (x > 1) {
   1.105 +		int ch;
   1.106 +
   1.107 +		for (p = odst; (ch = *p) != '\0'; p++)
   1.108 +			if (isascii(ch) && isupper(ch))
   1.109 +				*p = tolower(ch);
   1.110 +	}
   1.111 +
   1.112 +	return (dst - odst);
   1.113 +}
   1.114 +
   1.115 +#ifndef MOZILLA_NECKO_EXCLUDE_CODE
   1.116 +#ifndef _LIBC
   1.117 +int
   1.118 +ns_parse_ttl(const char *src, u_long *dst) {
   1.119 +	u_long ttl, tmp;
   1.120 +	int ch, digits, dirty;
   1.121 +
   1.122 +	ttl = 0;
   1.123 +	tmp = 0;
   1.124 +	digits = 0;
   1.125 +	dirty = 0;
   1.126 +	while ((ch = *src++) != '\0') {
   1.127 +		if (!isascii(ch) || !isprint(ch))
   1.128 +			goto einval;
   1.129 +		if (isdigit(ch)) {
   1.130 +			tmp *= 10;
   1.131 +			tmp += (ch - '0');
   1.132 +			digits++;
   1.133 +			continue;
   1.134 +		}
   1.135 +		if (digits == 0)
   1.136 +			goto einval;
   1.137 +		if (islower(ch))
   1.138 +			ch = toupper(ch);
   1.139 +		switch (ch) {
   1.140 +		case 'W':  tmp *= 7;	/*FALLTHROUGH*/
   1.141 +		case 'D':  tmp *= 24;	/*FALLTHROUGH*/
   1.142 +		case 'H':  tmp *= 60;	/*FALLTHROUGH*/
   1.143 +		case 'M':  tmp *= 60;	/*FALLTHROUGH*/
   1.144 +		case 'S':  break;
   1.145 +		default:   goto einval;
   1.146 +		}
   1.147 +		ttl += tmp;
   1.148 +		tmp = 0;
   1.149 +		digits = 0;
   1.150 +		dirty = 1;
   1.151 +	}
   1.152 +	if (digits > 0) {
   1.153 +		if (dirty)
   1.154 +			goto einval;
   1.155 +		else
   1.156 +			ttl += tmp;
   1.157 +	}
   1.158 +	*dst = ttl;
   1.159 +	return (0);
   1.160 +
   1.161 + einval:
   1.162 +	errno = EINVAL;
   1.163 +	return (-1);
   1.164 +}
   1.165 +#endif
   1.166 +#endif
   1.167 +
   1.168 +/* Private. */
   1.169 +
   1.170 +static int
   1.171 +fmt1(int t, char s, char **buf, size_t *buflen) {
   1.172 +	char tmp[50];
   1.173 +	size_t len;
   1.174 +
   1.175 +	len = SPRINTF((tmp, "%d%c", t, s));
   1.176 +	if (len + 1 > *buflen)
   1.177 +		return (-1);
   1.178 +	strcpy(*buf, tmp);
   1.179 +	*buf += len;
   1.180 +	*buflen -= len;
   1.181 +	return (0);
   1.182 +}

mercurial