OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / freeswan / lib / addrtoa.c
1 /*
2  * addresses to ASCII
3  * Copyright (C) 1998, 1999  Henry Spencer.
4  * 
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>.
9  * 
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.
14  *
15  * RCSID $Id: addrtoa.c,v 1.6 1999/04/10 23:19:36 henry Exp $
16  */
17 #include "internal.h"
18 #include "freeswan.h"
19
20 #define NBYTES  4               /* bytes in an address */
21 #define PERBYTE 4               /* three digits plus a dot or NUL */
22 #define BUFLEN  (NBYTES*PERBYTE)
23
24 #if BUFLEN != ADDRTOA_BUF
25 #error  "ADDRTOA_BUF in freeswan.h inconsistent with addrtoa() code"
26 #endif
27
28 /*
29  *      Fast implementation of conversion from byte to ASCII base 10.
30  *      The normal ultoa() uses division and modulus which is _very_
31  *      slow.
32  */
33 inline size_t uctoa(unsigned long n, char *dst)
34 {
35         char    *dp = dst;
36
37         if (n >= 200) {
38                 *dp++ = '2';
39                 n -= 200;
40         } else if (n >= 100) {
41                 *dp++ = '1';
42                 n -= 100;
43         }
44
45         /* This is loop un-rolled... */
46         if (n >= 10) {
47                 if (n >= 90) {
48                         *dp++ = '9';
49                         n -= 90;
50                 } else if (n >= 80) {
51                         *dp++ = '8';
52                         n -= 80;
53                 } else if (n >= 70) {
54                         *dp++ = '7';
55                         n -= 70;
56                 } else if (n >= 60) {
57                         *dp++ = '6';
58                         n -= 60;
59                 } else if (n >= 50) {
60                         *dp++ = '5';
61                         n -= 50;
62                 } else if (n >= 40) {
63                         *dp++ = '4';
64                         n -= 40;
65                 } else if (n >= 30) {
66                         *dp++ = '3';
67                         n -= 30;
68                 } else if (n >= 20) {
69                         *dp++ = '2';
70                         n -= 20;
71                 } else {
72                         *dp++ = '1';
73                         n -= 10;
74                 }
75         } else if (dp != dst) {
76                 *dp++ = '0';
77         }
78
79         *dp++ = '0' + n;
80         *dp++ = 0;
81         return(dp - dst);
82 }
83
84 /*
85  - addrtoa - convert binary address to ASCII dotted decimal
86  */
87 size_t                          /* space needed for full conversion */
88 addrtoa(addr, format, dst, dstlen)
89 struct in_addr addr;
90 int format;                     /* character */
91 char *dst;                      /* need not be valid if dstlen is 0 */
92 size_t dstlen;
93 {
94         unsigned long a = ntohl(addr.s_addr);
95         int i;
96         size_t n;
97         unsigned long byte;
98         char buf[BUFLEN];
99         char *p;
100
101         switch (format) {
102         case 0:
103                 break;
104         default:
105                 return 0;
106                 break;
107         }
108
109         p = buf;
110         for (i = NBYTES-1; i >= 0; i--) {
111                 byte = (a >> (i*8)) & 0xff;
112 #if 1
113                 p += uctoa(byte, p);
114 #else
115                 p += ultoa(byte, 10, p, PERBYTE);
116 #endif
117                 if (i != 0)
118                         *(p-1) = '.';
119         }
120         n = p - buf;
121
122         if (dstlen > 0) {
123                 if (n > dstlen)
124                         buf[dstlen - 1] = '\0';
125                 strcpy(dst, buf);
126         }
127         return n;
128 }