1 /* $NetBSD: loc_ntoa.c,v 1.4 2014/12/10 04:37:56 christos Exp $ */
7 * The Regents of the University of California. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
35 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
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.
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
55 * Portions Copyright (c) 1995 by International Business Machines, Inc.
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
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.
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.
80 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
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.
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
96 #include <sys/types.h>
97 #include <sys/param.h>
98 #include <sys/socket.h>
100 #include <netinet/in.h>
101 #include <arpa/inet.h>
102 #include <arpa/nameser.h>
116 const char *precsize_ntoa();
118 /* takes an on-the-wire LOC RR and formats it in a human readable format. */
120 loc_ntoa(binary
, ascii
)
121 const u_char
*binary
;
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
;
129 int latdeg
, latmin
, latsec
, latsecfrac
;
130 int longdeg
, longmin
, longsec
, longsecfrac
;
131 char northsouth
, eastwest
;
132 int altmeters
, altfrac
, altsign
;
134 const u_int32_t referencealt
= 100000 * 100;
136 int32_t latval
, longval
, altval
;
138 u_int8_t sizeval
, hpval
, vpval
, versionval
;
140 char *sizestr
, *hpstr
, *vpstr
;
148 (void) sprintf(ascii
, "; error: unknown LOC RR version");
158 latval
= (templ
- ((unsigned)1<<31));
161 longval
= (templ
- ((unsigned)1<<31));
164 if (templ
< referencealt
) { /* below WGS 84 spheroid */
165 altval
= referencealt
- templ
;
168 altval
= templ
- referencealt
;
178 latsecfrac
= latval
% 1000;
179 latval
= latval
/ 1000;
180 latsec
= latval
% 60;
181 latval
= latval
/ 60;
182 latmin
= latval
% 60;
183 latval
= latval
/ 60;
192 longsecfrac
= longval
% 1000;
193 longval
= longval
/ 1000;
194 longsec
= longval
% 60;
195 longval
= longval
/ 60;
196 longmin
= longval
% 60;
197 longval
= longval
/ 60;
200 altfrac
= altval
% 100;
201 altmeters
= (altval
/ 100) * altsign
;
203 if ((sizestr
= strdup(precsize_ntoa(sizeval
))) == NULL
)
205 if ((hpstr
= strdup(precsize_ntoa(hpval
))) == NULL
)
207 if ((vpstr
= strdup(precsize_ntoa(vpval
))) == NULL
)
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
);
216 if (sizestr
!= error
)
226 static unsigned int poweroften
[10] = {1, 10, 100, 1000, 10000, 100000,
227 1000000,10000000,100000000,1000000000};
229 /* takes an XeY precision/size value, returns a string representation. */
234 static char retbuf
[sizeof "90000000.00"]; /* XXX nonreentrant */
236 int mantissa
, exponent
;
238 mantissa
= (int)((prec
>> 4) & 0x0f) % 10;
239 exponent
= (int)((prec
>> 0) & 0x0f) % 10;
241 val
= mantissa
* poweroften
[exponent
];
243 (void) sprintf(retbuf
, "%ld.%.2ld", val
/100, val
%100);