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