1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
|
/*
* Copyright (c) 1985, 1989 Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
* provided that: (1) source distributions retain this entire copyright
* notice and comment, and (2) distributions including binaries display
* the following acknowledgement: ``This product includes software
* developed by the University of California, Berkeley and its contributors''
* in the documentation or other materials provided with the distribution
* and in all advertising materials mentioning features or use of this
* software. Neither the name of the University nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
#ifndef lint
static char Version[] = "@(#)misc.c e07@nikhef.nl (Eric Wassenaar) 991529";
#endif
#include "host.h"
#include "glob.h"
/*
** XALLOC -- Allocate or reallocate additional memory
** --------------------------------------------------
**
** Returns:
** Pointer to (re)allocated buffer space.
** Aborts if the requested memory could not be obtained.
*/
ptr_t *
xalloc(buf, size)
register ptr_t *buf; /* current start of buffer space */
input siz_t size; /* number of bytes to allocate */
{
if (buf == NULL)
buf = malloc(size);
else
buf = realloc(buf, size);
if (buf == NULL)
{
errmsg("Out of memory");
exit(EX_OSERR);
}
return(buf);
}
/*
** DTOA -- Convert value to decimal integer ascii string
** -----------------------------------------------------
**
** Returns:
** Pointer to string.
*/
char *
dtoa(n)
input int n; /* value to convert */
{
static char buf[30]; /* sufficient for 64-bit values */
(void) sprintf(buf, "%d", n);
return(buf);
}
/*
** UTOA -- Convert value to unsigned decimal ascii string
** ------------------------------------------------------
**
** Returns:
** Pointer to string.
*/
char *
utoa(n)
input int n; /* value to convert */
{
static char buf[30]; /* sufficient for 64-bit values */
(void) sprintf(buf, "%u", (unsigned)n);
return(buf);
}
/*
** XTOA -- Convert value to hexadecimal ascii string
** -------------------------------------------------
**
** Returns:
** Pointer to string.
*/
char *
xtoa(n)
input int n; /* value to convert */
{
static char buf[17]; /* sufficient for 64-bit values */
(void) sprintf(buf, "%X", (unsigned)n);
return(buf);
}
/*
** STOA -- Extract partial ascii string, escape if necessary
** ---------------------------------------------------------
**
** Returns:
** Pointer to string.
*/
char *
stoa(cp, size, escape)
input u_char *cp; /* current position in answer buf */
input int size; /* number of bytes to extract */
input bool escape; /* escape special characters if set */
{
static char buf[2*MAXDLEN+1];
register char *p;
register char c;
register int i;
if (size > MAXDLEN)
size = MAXDLEN;
#ifdef obsolete
if (size > 0)
(void) sprintf(buf, "%.*s", size, (char *)cp);
else
(void) sprintf(buf, "%s", "");
#endif
for (p = buf, i = 0; i < size; i++)
{
c = *cp++;
if (escape && (c == '\n' || c == '\\' || c == '"'))
*p++ = '\\';
*p++ = c;
}
*p = '\0';
return(buf);
}
/*
** BASE_NTOA -- Convert binary data to base64 ascii
** ------------------------------------------------
**
** Returns:
** Pointer to string.
**
** This routine is used to convert encoded keys, signatures,
** and certificates in T_KEY, T_SIG, and T_CERT resource records.
*/
char b64tab[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
char *
base_ntoa(cp, size)
input u_char *cp; /* current position in answer buf */
input int size; /* number of bytes to extract */
{
static char buf[MAXB64SIZE+1];
register char *p;
int c1, c2, c3, c4;
if (size > MAXMD5SIZE)
size = MAXMD5SIZE;
for (p = buf; size > 2; cp += 3, size -= 3)
{
c1 = (((int)cp[0] >> 2) & 0x3f);
c2 = (((int)cp[0] & 0x03) << 4) + (((int)cp[1] >> 4) & 0x0f);
c3 = (((int)cp[1] & 0x0f) << 2) + (((int)cp[2] >> 6) & 0x03);
c4 = ((int)cp[2] & 0x3f);
*p++ = b64tab[c1];
*p++ = b64tab[c2];
*p++ = b64tab[c3];
*p++ = b64tab[c4];
}
if (size == 2)
{
c1 = (((int)cp[0] >> 2) & 0x3f);
c2 = (((int)cp[0] & 0x03) << 4) + (((int)cp[1] >> 4) & 0x0f);
c3 = (((int)cp[1] & 0x0f) << 2);
*p++ = b64tab[c1];
*p++ = b64tab[c2];
*p++ = b64tab[c3];
*p++ = '=';
}
else if (size == 1)
{
c1 = (((int)cp[0] >> 2) & 0x3f);
c2 = (((int)cp[0] & 0x03) << 4);
*p++ = b64tab[c1];
*p++ = b64tab[c2];
*p++ = '=';
*p++ = '=';
}
*p = '\0';
return(buf);
}
/*
** NSAP_NTOA -- Convert binary nsap address to ascii
** -------------------------------------------------
**
** Returns:
** Pointer to string.
**
** As per RFC 1637 an nsap address is encoded in binary form
** in the resource record. It was unclear from RFC 1348 how
** the encoding should be. RFC 1629 defines an upper bound
** of 20 bytes to the size of a binary nsap address.
*/
char *
nsap_ntoa(cp, size)
input u_char *cp; /* current position in answer buf */
input int size; /* number of bytes to extract */
{
static char buf[3*MAXNSAP+1];
register char *p;
register int c;
register int i;
if (size > MAXNSAP)
size = MAXNSAP;
for (p = buf, i = 0; i < size; i++, cp++)
{
c = ((int)(*cp) >> 4) & 0x0f;
*p++ = hexdigit(c);
c = ((int)(*cp) >> 0) & 0x0f;
*p++ = hexdigit(c);
/* add dots for readability */
if ((i % 2) == 0 && (i + 1) < size)
*p++ = '.';
}
*p = '\0';
return(buf);
}
/*
** IPNG_NTOA -- Convert binary ip v6 address to ascii
** --------------------------------------------------
**
** Returns:
** Pointer to string.
**
** As per RFC 1886 an ip v6 address is encoded in binary form
** in the resource record. The size is fixed.
*/
char *
ipng_ntoa(cp)
input u_char *cp; /* current position in answer buf */
{
static char buf[5*(IPNGSIZE/2)+1];
register char *p;
register int n;
register int i;
for (p = buf, i = 0; i < IPNGSIZE/2; i++)
{
n = _getshort(cp);
cp += INT16SZ;
(void) sprintf(p, ":%X", n);
p += strlength(p);
}
*p = '\0';
return(buf + 1);
}
/*
** PR_DATE -- Produce printable version of a clock value
** -----------------------------------------------------
**
** Returns:
** Pointer to string.
**
** The value is a standard absolute clock value.
*/
char *
pr_date(value)
input int value; /* the clock value to be converted */
{
static char buf[sizeof("YYYYMMDDHHMMSS")+1];
time_t clocktime = value;
struct tm *t;
t = gmtime(&clocktime);
t->tm_year += 1900;
t->tm_mon += 1;
(void) sprintf(buf, "%04d%02d%02d%02d%02d%02d",
t->tm_year, t->tm_mon, t->tm_mday,
t->tm_hour, t->tm_min, t->tm_sec);
return(buf);
}
/*
** PR_TIME -- Produce printable version of a time interval
** -------------------------------------------------------
**
** Returns:
** Pointer to a string version of interval.
**
** The value is a time interval expressed in seconds.
*/
char *
pr_time(value, brief)
input int value; /* the interval to be converted */
input bool brief; /* use brief format if set */
{
static char buf[256];
register char *p = buf;
int week, days, hour, mins, secs;
/* special cases */
if (value < 0)
return("negative");
if ((value == 0) && !brief)
return("zero seconds");
/*
* Decode the components.
*/
secs = value % 60; value /= 60;
mins = value % 60; value /= 60;
hour = value % 24; value /= 24;
days = value;
if (!brief)
{
days = value % 7; value /= 7;
week = value;
}
/*
* Now turn it into a sexy form.
*/
if (brief)
{
if (days > 0)
{
(void) sprintf(p, "%d+", days);
p += strlength(p);
}
(void) sprintf(p, "%02d:%02d:%02d", hour, mins, secs);
return(buf);
}
if (week > 0)
{
(void) sprintf(p, ", %d week%s", week, plural(week));
p += strlength(p);
}
if (days > 0)
{
(void) sprintf(p, ", %d day%s", days, plural(days));
p += strlength(p);
}
if (hour > 0)
{
(void) sprintf(p, ", %d hour%s", hour, plural(hour));
p += strlength(p);
}
if (mins > 0)
{
(void) sprintf(p, ", %d minute%s", mins, plural(mins));
p += strlength(p);
}
if (secs > 0)
{
(void) sprintf(p, ", %d second%s", secs, plural(secs));
/* p += strlength(p); */
}
return(buf + 2);
}
/*
** PR_SPHERICAL -- Produce printable version of a spherical location
** -----------------------------------------------------------------
**
** Returns:
** Pointer to a string version of location.
**
** The value is a spherical location (latitude or longitude)
** expressed in thousandths of a second of arc.
** The value 2^31 represents zero (equator or prime meridian).
*/
char *
pr_spherical(value, pos, neg)
input int value; /* the location to be converted */
input char *pos; /* suffix if value positive */
input char *neg; /* suffix if value negative */
{
static char buf[256];
register char *p = buf;
char *direction;
int degrees, minutes, seconds, fracsec;
/*
* Normalize.
*/
value -= (int)((unsigned)1 << 31);
direction = pos;
if (value < 0)
{
direction = neg;
value = -value;
}
/*
* Decode the components.
*/
fracsec = value % 1000; value /= 1000;
seconds = value % 60; value /= 60;
minutes = value % 60; value /= 60;
degrees = value;
/*
* Construct output string.
*/
(void) sprintf(p, "%d", degrees);
p += strlength(p);
if (minutes > 0 || seconds > 0 || fracsec > 0)
{
(void) sprintf(p, " %02d", minutes);
p += strlength(p);
}
if (seconds > 0 || fracsec > 0)
{
(void) sprintf(p, " %02d", seconds);
p += strlength(p);
}
if (fracsec > 0)
{
(void) sprintf(p, ".%03d", fracsec);
p += strlength(p);
}
(void) sprintf(p, " %s", direction);
#ifdef obsolete
(void) sprintf(buf, "%d %02d %02d.%03d %s",
degrees, minutes, seconds, fracsec, direction);
#endif
return(buf);
}
/*
** PR_VERTICAL -- Produce printable version of a vertical location
** ---------------------------------------------------------------
**
** Returns:
** Pointer to a string version of location.
**
** The value is an altitude expressed in centimeters, starting
** from a base 100000 meters below the GPS reference spheroid.
** This allows for the actual range [-10000000 .. 4293967296].
*/
char *
pr_vertical(value, pos, neg)
input int value; /* the location to be converted */
input char *pos; /* prefix if value positive */
input char *neg; /* prefix if value negative */
{
static char buf[256];
register char *p = buf;
char *direction;
int meters, centimeters;
unsigned int altitude;
unsigned int reference;
/*
* Normalize.
*/
altitude = value;
reference = 100000*100;
if (altitude < reference)
{
direction = neg;
altitude = reference - altitude;
}
else
{
direction = pos;
altitude = altitude - reference;
}
/*
* Decode the components.
*/
centimeters = altitude % 100; altitude /= 100;
meters = altitude;
/*
* Construct output string.
*/
(void) sprintf(p, "%s%d", direction, meters);
p += strlength(p);
if (centimeters > 0)
(void) sprintf(p, ".%02d", centimeters);
#ifdef obsolete
(void) sprintf(buf, "%s%d.%02d", direction, meters, centimeters);
#endif
return(buf);
}
/*
** PR_PRECISION -- Produce printable version of a location precision
** -----------------------------------------------------------------
**
** Returns:
** Pointer to a string version of precision.
**
** The value is a precision expressed in centimeters, encoded
** as 4-bit mantissa and 4-bit power of 10 (each ranging 0-9).
*/
unsigned int poweroften[10] =
{1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000};
char *
pr_precision(value)
input int value; /* the precision to be converted */
{
static char buf[256];
register char *p = buf;
int meters, centimeters;
unsigned int precision;
register int mantissa;
register int exponent;
/*
* Normalize.
*/
mantissa = ((value >> 4) & 0x0f) % 10;
exponent = ((value >> 0) & 0x0f) % 10;
precision = mantissa * poweroften[exponent];
/*
* Decode the components.
*/
centimeters = precision % 100; precision /= 100;
meters = precision;
/*
* Construct output string.
*/
(void) sprintf(p, "%d", meters);
p += strlength(p);
if (centimeters > 0)
(void) sprintf(p, ".%02d", centimeters);
#ifdef obsolete
(void) sprintf(buf, "%d.%02d", meters, centimeters);
#endif
return(buf);
}
/*
** CONVTIME -- Decode a time period from input string
** --------------------------------------------------
**
** Returns:
** Non-negative numeric value of time period (in seconds).
** -1 in case of invalid time specification.
**
** Only rudimentary syntax errors are checked.
** It is easy to fool this routine to yield bizarre results.
** If the result is negative, it should not be trusted.
*/
int
convtime(string, defunits)
input char *string; /* time specification in ascii */
input char defunits; /* default units if none specified */
{
int period = 0; /* overall result value */
register int value; /* intermediate value of component */
register char units;
register char *p = string;
while (*p != '\0')
{
/* must start with numeric value */
if (!is_digit(*p))
return(-1);
/* assemble numeric value */
for (value = 0; is_digit(*p); p++)
value = (value * 10) + (*p - '0');
/* fetch units -- use default when omitted */
if (*p != '\0')
units = *p++;
else
units = defunits;
switch (lowercase(units))
{
case 'w': /* weeks */
value *= 7;
/*FALLTHROUGH*/
case 'd': /* days */
value *= 24;
/*FALLTHROUGH*/
case 'h': /* hours */
value *= 60;
/*FALLTHROUGH*/
case 'm': /* minutes */
value *= 60;
/*FALLTHROUGH*/
case 's': /* seconds */
break;
default: /* unknown */
value = -1;
break;
}
/* must be reasonable */
if (value < 0)
return(-1);
/* accumulate total */
period += value;
}
return(period);
}
|