Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* $NetBSD: ns_ttl.c,v 1.2 2004/05/20 20:35:05 christos Exp $ */ |
michael@0 | 2 | |
michael@0 | 3 | /* |
michael@0 | 4 | * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") |
michael@0 | 5 | * Copyright (c) 1996,1999 by Internet Software Consortium. |
michael@0 | 6 | * |
michael@0 | 7 | * Permission to use, copy, modify, and distribute this software for any |
michael@0 | 8 | * purpose with or without fee is hereby granted, provided that the above |
michael@0 | 9 | * copyright notice and this permission notice appear in all copies. |
michael@0 | 10 | * |
michael@0 | 11 | * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES |
michael@0 | 12 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
michael@0 | 13 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR |
michael@0 | 14 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
michael@0 | 15 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
michael@0 | 16 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
michael@0 | 17 | * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
michael@0 | 18 | */ |
michael@0 | 19 | |
michael@0 | 20 | /* |
michael@0 | 21 | * This version of this file is derived from Android 2.3 "Gingerbread", |
michael@0 | 22 | * which contains uncredited changes by Android/Google developers. It has |
michael@0 | 23 | * been modified in 2011 for use in the Android build of Mozilla Firefox by |
michael@0 | 24 | * Mozilla contributors (including Michael Edwards <m.k.edwards@gmail.com>, |
michael@0 | 25 | * and Steve Workman <sjhworkman@gmail.com>). |
michael@0 | 26 | * These changes are offered under the same license as the original NetBSD |
michael@0 | 27 | * file, whose copyright and license are unchanged above. |
michael@0 | 28 | */ |
michael@0 | 29 | |
michael@0 | 30 | #define ANDROID_CHANGES 1 |
michael@0 | 31 | #define MOZILLA_NECKO_EXCLUDE_CODE 1 |
michael@0 | 32 | |
michael@0 | 33 | #include <sys/cdefs.h> |
michael@0 | 34 | #ifndef lint |
michael@0 | 35 | #ifdef notdef |
michael@0 | 36 | static const char rcsid[] = "Id: ns_ttl.c,v 1.1.206.1 2004/03/09 08:33:45 marka Exp"; |
michael@0 | 37 | #else |
michael@0 | 38 | __RCSID("$NetBSD: ns_ttl.c,v 1.2 2004/05/20 20:35:05 christos Exp $"); |
michael@0 | 39 | #endif |
michael@0 | 40 | #endif |
michael@0 | 41 | |
michael@0 | 42 | /* Import. */ |
michael@0 | 43 | |
michael@0 | 44 | #include "arpa_nameser.h" |
michael@0 | 45 | |
michael@0 | 46 | #include <ctype.h> |
michael@0 | 47 | #include <errno.h> |
michael@0 | 48 | #include <stdio.h> |
michael@0 | 49 | #include <string.h> |
michael@0 | 50 | |
michael@0 | 51 | #ifdef SPRINTF_CHAR |
michael@0 | 52 | # define SPRINTF(x) strlen(sprintf/**/x) |
michael@0 | 53 | #else |
michael@0 | 54 | # define SPRINTF(x) ((size_t)sprintf x) |
michael@0 | 55 | #endif |
michael@0 | 56 | |
michael@0 | 57 | /* Forward. */ |
michael@0 | 58 | |
michael@0 | 59 | static int fmt1(int t, char s, char **buf, size_t *buflen); |
michael@0 | 60 | |
michael@0 | 61 | /* Macros. */ |
michael@0 | 62 | |
michael@0 | 63 | #define T(x) do { if ((x) < 0) return (-1); } while(0) |
michael@0 | 64 | |
michael@0 | 65 | /* Public. */ |
michael@0 | 66 | |
michael@0 | 67 | int |
michael@0 | 68 | ns_format_ttl(u_long src, char *dst, size_t dstlen) { |
michael@0 | 69 | char *odst = dst; |
michael@0 | 70 | int secs, mins, hours, days, weeks, x; |
michael@0 | 71 | char *p; |
michael@0 | 72 | |
michael@0 | 73 | secs = src % 60; src /= 60; |
michael@0 | 74 | mins = src % 60; src /= 60; |
michael@0 | 75 | hours = src % 24; src /= 24; |
michael@0 | 76 | days = src % 7; src /= 7; |
michael@0 | 77 | weeks = src; src = 0; |
michael@0 | 78 | |
michael@0 | 79 | x = 0; |
michael@0 | 80 | if (weeks) { |
michael@0 | 81 | T(fmt1(weeks, 'W', &dst, &dstlen)); |
michael@0 | 82 | x++; |
michael@0 | 83 | } |
michael@0 | 84 | if (days) { |
michael@0 | 85 | T(fmt1(days, 'D', &dst, &dstlen)); |
michael@0 | 86 | x++; |
michael@0 | 87 | } |
michael@0 | 88 | if (hours) { |
michael@0 | 89 | T(fmt1(hours, 'H', &dst, &dstlen)); |
michael@0 | 90 | x++; |
michael@0 | 91 | } |
michael@0 | 92 | if (mins) { |
michael@0 | 93 | T(fmt1(mins, 'M', &dst, &dstlen)); |
michael@0 | 94 | x++; |
michael@0 | 95 | } |
michael@0 | 96 | if (secs || !(weeks || days || hours || mins)) { |
michael@0 | 97 | T(fmt1(secs, 'S', &dst, &dstlen)); |
michael@0 | 98 | x++; |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | if (x > 1) { |
michael@0 | 102 | int ch; |
michael@0 | 103 | |
michael@0 | 104 | for (p = odst; (ch = *p) != '\0'; p++) |
michael@0 | 105 | if (isascii(ch) && isupper(ch)) |
michael@0 | 106 | *p = tolower(ch); |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | return (dst - odst); |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | #ifndef MOZILLA_NECKO_EXCLUDE_CODE |
michael@0 | 113 | #ifndef _LIBC |
michael@0 | 114 | int |
michael@0 | 115 | ns_parse_ttl(const char *src, u_long *dst) { |
michael@0 | 116 | u_long ttl, tmp; |
michael@0 | 117 | int ch, digits, dirty; |
michael@0 | 118 | |
michael@0 | 119 | ttl = 0; |
michael@0 | 120 | tmp = 0; |
michael@0 | 121 | digits = 0; |
michael@0 | 122 | dirty = 0; |
michael@0 | 123 | while ((ch = *src++) != '\0') { |
michael@0 | 124 | if (!isascii(ch) || !isprint(ch)) |
michael@0 | 125 | goto einval; |
michael@0 | 126 | if (isdigit(ch)) { |
michael@0 | 127 | tmp *= 10; |
michael@0 | 128 | tmp += (ch - '0'); |
michael@0 | 129 | digits++; |
michael@0 | 130 | continue; |
michael@0 | 131 | } |
michael@0 | 132 | if (digits == 0) |
michael@0 | 133 | goto einval; |
michael@0 | 134 | if (islower(ch)) |
michael@0 | 135 | ch = toupper(ch); |
michael@0 | 136 | switch (ch) { |
michael@0 | 137 | case 'W': tmp *= 7; /*FALLTHROUGH*/ |
michael@0 | 138 | case 'D': tmp *= 24; /*FALLTHROUGH*/ |
michael@0 | 139 | case 'H': tmp *= 60; /*FALLTHROUGH*/ |
michael@0 | 140 | case 'M': tmp *= 60; /*FALLTHROUGH*/ |
michael@0 | 141 | case 'S': break; |
michael@0 | 142 | default: goto einval; |
michael@0 | 143 | } |
michael@0 | 144 | ttl += tmp; |
michael@0 | 145 | tmp = 0; |
michael@0 | 146 | digits = 0; |
michael@0 | 147 | dirty = 1; |
michael@0 | 148 | } |
michael@0 | 149 | if (digits > 0) { |
michael@0 | 150 | if (dirty) |
michael@0 | 151 | goto einval; |
michael@0 | 152 | else |
michael@0 | 153 | ttl += tmp; |
michael@0 | 154 | } |
michael@0 | 155 | *dst = ttl; |
michael@0 | 156 | return (0); |
michael@0 | 157 | |
michael@0 | 158 | einval: |
michael@0 | 159 | errno = EINVAL; |
michael@0 | 160 | return (-1); |
michael@0 | 161 | } |
michael@0 | 162 | #endif |
michael@0 | 163 | #endif |
michael@0 | 164 | |
michael@0 | 165 | /* Private. */ |
michael@0 | 166 | |
michael@0 | 167 | static int |
michael@0 | 168 | fmt1(int t, char s, char **buf, size_t *buflen) { |
michael@0 | 169 | char tmp[50]; |
michael@0 | 170 | size_t len; |
michael@0 | 171 | |
michael@0 | 172 | len = SPRINTF((tmp, "%d%c", t, s)); |
michael@0 | 173 | if (len + 1 > *buflen) |
michael@0 | 174 | return (-1); |
michael@0 | 175 | strcpy(*buf, tmp); |
michael@0 | 176 | *buf += len; |
michael@0 | 177 | *buflen -= len; |
michael@0 | 178 | return (0); |
michael@0 | 179 | } |