3 * Copyright (C) 1998, 1999 Henry Spencer.
5 * This library is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU Library General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 * License for more details.
15 * RCSID $Id: addrtoa.c,v 1.6 1999/04/10 23:19:36 henry Exp $
20 #define NBYTES 4 /* bytes in an address */
21 #define PERBYTE 4 /* three digits plus a dot or NUL */
22 #define BUFLEN (NBYTES*PERBYTE)
24 #if BUFLEN != ADDRTOA_BUF
25 #error "ADDRTOA_BUF in freeswan.h inconsistent with addrtoa() code"
29 * Fast implementation of conversion from byte to ASCII base 10.
30 * The normal ultoa() uses division and modulus which is _very_
33 inline size_t uctoa(unsigned long n, char *dst)
40 } else if (n >= 100) {
45 /* This is loop un-rolled... */
75 } else if (dp != dst) {
85 - addrtoa - convert binary address to ASCII dotted decimal
87 size_t /* space needed for full conversion */
88 addrtoa(addr, format, dst, dstlen)
90 int format; /* character */
91 char *dst; /* need not be valid if dstlen is 0 */
94 unsigned long a = ntohl(addr.s_addr);
110 for (i = NBYTES-1; i >= 0; i--) {
111 byte = (a >> (i*8)) & 0xff;
115 p += ultoa(byte, 10, p, PERBYTE);
124 buf[dstlen - 1] = '\0';