17c478bd9Sstevel@tonic-gate /*
29525b14bSRao Shoaib * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
37c478bd9Sstevel@tonic-gate * Copyright (c) 1996,1999 by Internet Software Consortium.
47c478bd9Sstevel@tonic-gate *
57c478bd9Sstevel@tonic-gate * Permission to use, copy, modify, and distribute this software for any
67c478bd9Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above
77c478bd9Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies.
87c478bd9Sstevel@tonic-gate *
99525b14bSRao Shoaib * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
109525b14bSRao Shoaib * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
119525b14bSRao Shoaib * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
129525b14bSRao Shoaib * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
139525b14bSRao Shoaib * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
149525b14bSRao Shoaib * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
159525b14bSRao Shoaib * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
167c478bd9Sstevel@tonic-gate */
177c478bd9Sstevel@tonic-gate
187c478bd9Sstevel@tonic-gate #include "port_before.h"
197c478bd9Sstevel@tonic-gate
207c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
217c478bd9Sstevel@tonic-gate
227c478bd9Sstevel@tonic-gate #include <ctype.h>
237c478bd9Sstevel@tonic-gate #include <errno.h>
247c478bd9Sstevel@tonic-gate #include <stdio.h>
257c478bd9Sstevel@tonic-gate #include <string.h>
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate #include "port_after.h"
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate #ifdef SPRINTF_CHAR
307c478bd9Sstevel@tonic-gate # define SPRINTF(x) strlen(sprintf/**/x)
317c478bd9Sstevel@tonic-gate #else
327c478bd9Sstevel@tonic-gate # define SPRINTF(x) ((size_t)sprintf x)
337c478bd9Sstevel@tonic-gate #endif
347c478bd9Sstevel@tonic-gate
357c478bd9Sstevel@tonic-gate /* Forward. */
367c478bd9Sstevel@tonic-gate
377c478bd9Sstevel@tonic-gate static int fmt1(int t, char s, char **buf, size_t *buflen);
387c478bd9Sstevel@tonic-gate
397c478bd9Sstevel@tonic-gate /* Macros. */
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gate #define T(x) if ((x) < 0) return (-1); else (void)NULL
427c478bd9Sstevel@tonic-gate
437c478bd9Sstevel@tonic-gate /* Public. */
447c478bd9Sstevel@tonic-gate
457c478bd9Sstevel@tonic-gate int
ns_format_ttl(u_long src,char * dst,size_t dstlen)467c478bd9Sstevel@tonic-gate ns_format_ttl(u_long src, char *dst, size_t dstlen) {
477c478bd9Sstevel@tonic-gate char *odst = dst;
487c478bd9Sstevel@tonic-gate int secs, mins, hours, days, weeks, x;
497c478bd9Sstevel@tonic-gate char *p;
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate secs = src % 60; src /= 60;
527c478bd9Sstevel@tonic-gate mins = src % 60; src /= 60;
537c478bd9Sstevel@tonic-gate hours = src % 24; src /= 24;
547c478bd9Sstevel@tonic-gate days = src % 7; src /= 7;
557c478bd9Sstevel@tonic-gate weeks = src; src = 0;
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate x = 0;
587c478bd9Sstevel@tonic-gate if (weeks) {
597c478bd9Sstevel@tonic-gate T(fmt1(weeks, 'W', &dst, &dstlen));
607c478bd9Sstevel@tonic-gate x++;
617c478bd9Sstevel@tonic-gate }
627c478bd9Sstevel@tonic-gate if (days) {
637c478bd9Sstevel@tonic-gate T(fmt1(days, 'D', &dst, &dstlen));
647c478bd9Sstevel@tonic-gate x++;
657c478bd9Sstevel@tonic-gate }
667c478bd9Sstevel@tonic-gate if (hours) {
677c478bd9Sstevel@tonic-gate T(fmt1(hours, 'H', &dst, &dstlen));
687c478bd9Sstevel@tonic-gate x++;
697c478bd9Sstevel@tonic-gate }
707c478bd9Sstevel@tonic-gate if (mins) {
717c478bd9Sstevel@tonic-gate T(fmt1(mins, 'M', &dst, &dstlen));
727c478bd9Sstevel@tonic-gate x++;
737c478bd9Sstevel@tonic-gate }
747c478bd9Sstevel@tonic-gate if (secs || !(weeks || days || hours || mins)) {
757c478bd9Sstevel@tonic-gate T(fmt1(secs, 'S', &dst, &dstlen));
767c478bd9Sstevel@tonic-gate x++;
777c478bd9Sstevel@tonic-gate }
787c478bd9Sstevel@tonic-gate
797c478bd9Sstevel@tonic-gate if (x > 1) {
807c478bd9Sstevel@tonic-gate int ch;
817c478bd9Sstevel@tonic-gate
827c478bd9Sstevel@tonic-gate for (p = odst; (ch = *p) != '\0'; p++)
837c478bd9Sstevel@tonic-gate if (isascii(ch) && isupper(ch))
847c478bd9Sstevel@tonic-gate *p = tolower(ch);
857c478bd9Sstevel@tonic-gate }
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate return (dst - odst);
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gate int
ns_parse_ttl(const char * src,u_long * dst)917c478bd9Sstevel@tonic-gate ns_parse_ttl(const char *src, u_long *dst) {
927c478bd9Sstevel@tonic-gate u_long ttl, tmp;
937c478bd9Sstevel@tonic-gate int ch, digits, dirty;
947c478bd9Sstevel@tonic-gate
957c478bd9Sstevel@tonic-gate ttl = 0;
967c478bd9Sstevel@tonic-gate tmp = 0;
977c478bd9Sstevel@tonic-gate digits = 0;
987c478bd9Sstevel@tonic-gate dirty = 0;
997c478bd9Sstevel@tonic-gate while ((ch = *src++) != '\0') {
1007c478bd9Sstevel@tonic-gate if (!isascii(ch) || !isprint(ch))
1017c478bd9Sstevel@tonic-gate goto einval;
1027c478bd9Sstevel@tonic-gate if (isdigit(ch)) {
1037c478bd9Sstevel@tonic-gate tmp *= 10;
1047c478bd9Sstevel@tonic-gate tmp += (ch - '0');
1057c478bd9Sstevel@tonic-gate digits++;
1067c478bd9Sstevel@tonic-gate continue;
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate if (digits == 0)
1097c478bd9Sstevel@tonic-gate goto einval;
1107c478bd9Sstevel@tonic-gate if (islower(ch))
1117c478bd9Sstevel@tonic-gate ch = toupper(ch);
1127c478bd9Sstevel@tonic-gate switch (ch) {
1137c478bd9Sstevel@tonic-gate case 'W': tmp *= 7;
114*d32e0fcaSToomas Soome /* FALLTHROUGH */
1157c478bd9Sstevel@tonic-gate case 'D': tmp *= 24;
116*d32e0fcaSToomas Soome /* FALLTHROUGH */
1177c478bd9Sstevel@tonic-gate case 'H': tmp *= 60;
118*d32e0fcaSToomas Soome /* FALLTHROUGH */
1197c478bd9Sstevel@tonic-gate case 'M': tmp *= 60;
120*d32e0fcaSToomas Soome /* FALLTHROUGH */
1217c478bd9Sstevel@tonic-gate case 'S': break;
1227c478bd9Sstevel@tonic-gate default: goto einval;
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate ttl += tmp;
1257c478bd9Sstevel@tonic-gate tmp = 0;
1267c478bd9Sstevel@tonic-gate digits = 0;
1277c478bd9Sstevel@tonic-gate dirty = 1;
1287c478bd9Sstevel@tonic-gate }
1297c478bd9Sstevel@tonic-gate if (digits > 0) {
1307c478bd9Sstevel@tonic-gate if (dirty)
1317c478bd9Sstevel@tonic-gate goto einval;
1327c478bd9Sstevel@tonic-gate else
1337c478bd9Sstevel@tonic-gate ttl += tmp;
1349525b14bSRao Shoaib } else if (!dirty)
1359525b14bSRao Shoaib goto einval;
1367c478bd9Sstevel@tonic-gate *dst = ttl;
1377c478bd9Sstevel@tonic-gate return (0);
1387c478bd9Sstevel@tonic-gate
1397c478bd9Sstevel@tonic-gate einval:
1407c478bd9Sstevel@tonic-gate errno = EINVAL;
1417c478bd9Sstevel@tonic-gate return (-1);
1427c478bd9Sstevel@tonic-gate }
1437c478bd9Sstevel@tonic-gate
1447c478bd9Sstevel@tonic-gate /* Private. */
1457c478bd9Sstevel@tonic-gate
1467c478bd9Sstevel@tonic-gate static int
fmt1(int t,char s,char ** buf,size_t * buflen)1477c478bd9Sstevel@tonic-gate fmt1(int t, char s, char **buf, size_t *buflen) {
1487c478bd9Sstevel@tonic-gate char tmp[50];
1497c478bd9Sstevel@tonic-gate size_t len;
1507c478bd9Sstevel@tonic-gate
1517c478bd9Sstevel@tonic-gate len = SPRINTF((tmp, "%d%c", t, s));
1527c478bd9Sstevel@tonic-gate if (len + 1 > *buflen)
1537c478bd9Sstevel@tonic-gate return (-1);
1547c478bd9Sstevel@tonic-gate strcpy(*buf, tmp);
1557c478bd9Sstevel@tonic-gate *buf += len;
1567c478bd9Sstevel@tonic-gate *buflen -= len;
1577c478bd9Sstevel@tonic-gate return (0);
1587c478bd9Sstevel@tonic-gate }
1599525b14bSRao Shoaib
1609525b14bSRao Shoaib /*! \file */
161