No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / bind / dist / contrib / query-loc-0.4.0 / loc_ntoa.c
blobaaaebab5630bdf2bc6eda2cdc6e4c483763893f5
1 /* $NetBSD$ */
3 /* Stolen from BIND */
5 /*
6 * Copyright (c) 1985
7 * The Regents of the University of California. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
39 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
41 * Permission to use, copy, modify, and distribute this software for any
42 * purpose with or without fee is hereby granted, provided that the above
43 * copyright notice and this permission notice appear in all copies, and that
44 * the name of Digital Equipment Corporation not be used in advertising or
45 * publicity pertaining to distribution of the document or software without
46 * specific, written prior permission.
48 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
49 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
50 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
51 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
52 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
53 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
54 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
55 * SOFTWARE.
59 * Portions Copyright (c) 1995 by International Business Machines, Inc.
61 * International Business Machines, Inc. (hereinafter called IBM) grants
62 * permission under its copyrights to use, copy, modify, and distribute this
63 * Software with or without fee, provided that the above copyright notice and
64 * all paragraphs of this notice appear in all copies, and that the name of IBM
65 * not be used in connection with the marketing of any product incorporating
66 * the Software or modifications thereof, without specific, written prior
67 * permission.
69 * To the extent it has a right to do so, IBM grants an immunity from suit
70 * under its patents, if any, for the use, sale or manufacture of products to
71 * the extent that such products are used for performing Domain Name System
72 * dynamic updates in TCP/IP networks by means of the Software. No immunity is
73 * granted for any product per se or for any other function of any product.
75 * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
76 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
77 * PARTICULAR PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
78 * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
79 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
80 * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
84 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
86 * Permission to use, copy, modify, and distribute this software for any
87 * purpose with or without fee is hereby granted, provided that the above
88 * copyright notice and this permission notice appear in all copies.
90 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
91 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
92 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
93 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
94 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
95 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
96 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
97 * SOFTWARE.
100 #include <sys/types.h>
101 #include <sys/param.h>
102 #include <sys/socket.h>
104 #include <netinet/in.h>
105 #include <arpa/inet.h>
106 #include <arpa/nameser.h>
108 #include <ctype.h>
109 #include <errno.h>
110 #include <math.h>
111 #include <netdb.h>
112 #include <resolv.h>
113 #include <stdio.h>
114 #include <stdlib.h>
115 #include <string.h>
116 #include <time.h>
118 #include "loc.h"
120 const char *precsize_ntoa();
122 /* takes an on-the-wire LOC RR and formats it in a human readable format. */
123 const char *
124 loc_ntoa(binary, ascii)
125 const u_char *binary;
126 char *ascii;
128 static char *error = "?";
129 static char tmpbuf[sizeof
130 "1000 60 60.000 N 1000 60 60.000 W -12345678.00m 90000000.00m 90000000.00m 90000000.00m"];
131 const u_char *cp = binary;
133 int latdeg, latmin, latsec, latsecfrac;
134 int longdeg, longmin, longsec, longsecfrac;
135 char northsouth, eastwest;
136 int altmeters, altfrac, altsign;
138 const u_int32_t referencealt = 100000 * 100;
140 int32_t latval, longval, altval;
141 u_int32_t templ;
142 u_int8_t sizeval, hpval, vpval, versionval;
144 char *sizestr, *hpstr, *vpstr;
146 versionval = *cp++;
148 if (ascii == NULL)
149 ascii = tmpbuf;
151 if (versionval) {
152 (void) sprintf(ascii, "; error: unknown LOC RR version");
153 return (ascii);
156 sizeval = *cp++;
158 hpval = *cp++;
159 vpval = *cp++;
161 GETLONG(templ, cp);
162 latval = (templ - ((unsigned)1<<31));
164 GETLONG(templ, cp);
165 longval = (templ - ((unsigned)1<<31));
167 GETLONG(templ, cp);
168 if (templ < referencealt) { /* below WGS 84 spheroid */
169 altval = referencealt - templ;
170 altsign = -1;
171 } else {
172 altval = templ - referencealt;
173 altsign = 1;
176 if (latval < 0) {
177 northsouth = 'S';
178 latval = -latval;
179 } else
180 northsouth = 'N';
182 latsecfrac = latval % 1000;
183 latval = latval / 1000;
184 latsec = latval % 60;
185 latval = latval / 60;
186 latmin = latval % 60;
187 latval = latval / 60;
188 latdeg = latval;
190 if (longval < 0) {
191 eastwest = 'W';
192 longval = -longval;
193 } else
194 eastwest = 'E';
196 longsecfrac = longval % 1000;
197 longval = longval / 1000;
198 longsec = longval % 60;
199 longval = longval / 60;
200 longmin = longval % 60;
201 longval = longval / 60;
202 longdeg = longval;
204 altfrac = altval % 100;
205 altmeters = (altval / 100) * altsign;
207 if ((sizestr = strdup(precsize_ntoa(sizeval))) == NULL)
208 sizestr = error;
209 if ((hpstr = strdup(precsize_ntoa(hpval))) == NULL)
210 hpstr = error;
211 if ((vpstr = strdup(precsize_ntoa(vpval))) == NULL)
212 vpstr = error;
214 sprintf(ascii,
215 "%d %.2d %.2d.%.3d %c %d %.2d %.2d.%.3d %c %d.%.2dm %sm %sm %sm",
216 latdeg, latmin, latsec, latsecfrac, northsouth,
217 longdeg, longmin, longsec, longsecfrac, eastwest,
218 altmeters, altfrac, sizestr, hpstr, vpstr);
220 if (sizestr != error)
221 free(sizestr);
222 if (hpstr != error)
223 free(hpstr);
224 if (vpstr != error)
225 free(vpstr);
227 return (ascii);
230 static unsigned int poweroften[10] = {1, 10, 100, 1000, 10000, 100000,
231 1000000,10000000,100000000,1000000000};
233 /* takes an XeY precision/size value, returns a string representation. */
234 const char *
235 precsize_ntoa(prec)
236 u_int8_t prec;
238 static char retbuf[sizeof "90000000.00"]; /* XXX nonreentrant */
239 unsigned long val;
240 int mantissa, exponent;
242 mantissa = (int)((prec >> 4) & 0x0f) % 10;
243 exponent = (int)((prec >> 0) & 0x0f) % 10;
245 val = mantissa * poweroften[exponent];
247 (void) sprintf(retbuf, "%ld.%.2ld", val/100, val%100);
248 return (retbuf);