2 * Copyright (C) 2004, 2005 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: loc_29.c,v 1.41.18.2 2005/04/29 00:16:34 marka Exp $ */
20 /* Reviewed: Wed Mar 15 18:13:09 PST 2000 by explorer */
24 #ifndef RDATA_GENERIC_LOC_29_C
25 #define RDATA_GENERIC_LOC_29_C
27 #define RRTYPE_LOC_ATTRIBUTES (0)
29 static inline isc_result_t
30 fromtext_loc(ARGS_FROMTEXT) {
37 unsigned char version;
38 isc_boolean_t east = ISC_FALSE;
39 isc_boolean_t north = ISC_FALSE;
43 long poweroften[8] = { 1, 10, 100, 1000,
44 10000, 100000, 1000000, 10000000 };
49 unsigned long latitude;
50 unsigned long longitude;
51 unsigned long altitude;
65 size = 0x12; /* 1.00m */
66 hp = 0x16; /* 10000.00 m */
67 vp = 0x13; /* 10.00 m */
73 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
75 if (token.value.as_ulong > 90U)
77 d1 = (int)token.value.as_ulong;
81 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
83 if (strcasecmp(DNS_AS_STR(token), "N") == 0)
85 if (north || strcasecmp(DNS_AS_STR(token), "S") == 0)
87 m1 = strtol(DNS_AS_STR(token), &e, 10);
90 if (m1 < 0 || m1 > 59)
92 if (d1 == 90 && m1 != 0)
98 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
100 if (strcasecmp(DNS_AS_STR(token), "N") == 0)
102 if (north || strcasecmp(DNS_AS_STR(token), "S") == 0)
104 s1 = strtol(DNS_AS_STR(token), &e, 10);
105 if (*e != 0 && *e != '.')
106 RETTOK(DNS_R_SYNTAX);
107 if (s1 < 0 || s1 > 59)
112 for (i = 0; i < 3; i++) {
115 if ((tmp = decvalue(*e++)) < 0)
116 RETTOK(DNS_R_SYNTAX);
124 if (decvalue(*e++) < 0)
125 RETTOK(DNS_R_SYNTAX);
127 if (*l != '\0' && callbacks != NULL) {
128 const char *file = isc_lex_getsourcename(lexer);
129 unsigned long line = isc_lex_getsourceline(lexer);
133 (*callbacks->warn)(callbacks, "%s: %s:%u: '%s' extra "
134 "precision digits ignored",
135 "dns_rdata_fromtext", file, line,
140 if (d1 == 90 && s1 != 0)
146 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
148 if (strcasecmp(DNS_AS_STR(token), "N") == 0)
150 if (!north && strcasecmp(DNS_AS_STR(token), "S") != 0)
151 RETTOK(DNS_R_SYNTAX);
157 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
159 if (token.value.as_ulong > 180U)
161 d2 = (int)token.value.as_ulong;
166 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
168 if (strcasecmp(DNS_AS_STR(token), "E") == 0)
170 if (east || strcasecmp(DNS_AS_STR(token), "W") == 0)
172 m2 = strtol(DNS_AS_STR(token), &e, 10);
174 RETTOK(DNS_R_SYNTAX);
175 if (m2 < 0 || m2 > 59)
177 if (d2 == 180 && m2 != 0)
183 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
185 if (strcasecmp(DNS_AS_STR(token), "E") == 0)
187 if (east || strcasecmp(DNS_AS_STR(token), "W") == 0)
189 s2 = strtol(DNS_AS_STR(token), &e, 10);
190 if (*e != 0 && *e != '.')
191 RETTOK(DNS_R_SYNTAX);
192 if (s2 < 0 || s2 > 59)
197 for (i = 0; i < 3; i++) {
200 if ((tmp = decvalue(*e++)) < 0)
201 RETTOK(DNS_R_SYNTAX);
209 if (decvalue(*e++) < 0)
210 RETTOK(DNS_R_SYNTAX);
212 if (*l != '\0' && callbacks != NULL) {
213 const char *file = isc_lex_getsourcename(lexer);
214 unsigned long line = isc_lex_getsourceline(lexer);
218 (*callbacks->warn)(callbacks, "%s: %s:%u: '%s' extra "
219 "precision digits ignored",
220 "dns_rdata_fromtext",
221 file, line, DNS_AS_STR(token));
225 if (d2 == 180 && s2 != 0)
231 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
233 if (strcasecmp(DNS_AS_STR(token), "E") == 0)
235 if (!east && strcasecmp(DNS_AS_STR(token), "W") != 0)
236 RETTOK(DNS_R_SYNTAX);
242 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
244 m = strtol(DNS_AS_STR(token), &e, 10);
245 if (*e != 0 && *e != '.' && *e != 'm')
246 RETTOK(DNS_R_SYNTAX);
247 if (m < -100000 || m > 42849672)
252 for (i = 0; i < 2; i++) {
253 if (*e == 0 || *e == 'm')
255 if ((tmp = decvalue(*e++)) < 0)
256 return (DNS_R_SYNTAX);
269 RETTOK(DNS_R_SYNTAX);
270 if (m == -100000 && cm != 0)
272 if (m == 42849672 && cm > 95)
277 altitude = m + 100000;
284 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
286 if (token.type == isc_tokentype_eol ||
287 token.type == isc_tokentype_eof) {
288 isc_lex_ungettoken(lexer, &token);
291 m = strtol(DNS_AS_STR(token), &e, 10);
292 if (*e != 0 && *e != '.' && *e != 'm')
293 RETTOK(DNS_R_SYNTAX);
294 if (m < 0 || m > 90000000)
299 for (i = 0; i < 2; i++) {
300 if (*e == 0 || *e == 'm')
302 if ((tmp = decvalue(*e++)) < 0)
303 RETTOK(DNS_R_SYNTAX);
313 RETTOK(DNS_R_SYNTAX);
315 * We don't just multiply out as we will overflow.
318 for (exp = 0; exp < 7; exp++)
319 if (m < poweroften[exp+1])
321 man = m / poweroften[exp];
332 size = (man << 4) + exp;
335 * Horizontal precision: optional.
337 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
339 if (token.type == isc_tokentype_eol ||
340 token.type == isc_tokentype_eof) {
341 isc_lex_ungettoken(lexer, &token);
344 m = strtol(DNS_AS_STR(token), &e, 10);
345 if (*e != 0 && *e != '.' && *e != 'm')
346 RETTOK(DNS_R_SYNTAX);
347 if (m < 0 || m > 90000000)
352 for (i = 0; i < 2; i++) {
353 if (*e == 0 || *e == 'm')
355 if ((tmp = decvalue(*e++)) < 0)
356 RETTOK(DNS_R_SYNTAX);
366 RETTOK(DNS_R_SYNTAX);
368 * We don't just multiply out as we will overflow.
371 for (exp = 0; exp < 7; exp++)
372 if (m < poweroften[exp+1])
374 man = m / poweroften[exp];
376 } else if (cm >= 10) {
383 hp = (man << 4) + exp;
386 * Vertical precision: optional.
388 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
390 if (token.type == isc_tokentype_eol ||
391 token.type == isc_tokentype_eof) {
392 isc_lex_ungettoken(lexer, &token);
395 m = strtol(DNS_AS_STR(token), &e, 10);
396 if (*e != 0 && *e != '.' && *e != 'm')
397 RETTOK(DNS_R_SYNTAX);
398 if (m < 0 || m > 90000000)
403 for (i = 0; i < 2; i++) {
404 if (*e == 0 || *e == 'm')
406 if ((tmp = decvalue(*e++)) < 0)
407 RETTOK(DNS_R_SYNTAX);
417 RETTOK(DNS_R_SYNTAX);
419 * We don't just multiply out as we will overflow.
422 for (exp = 0; exp < 7; exp++)
423 if (m < poweroften[exp+1])
425 man = m / poweroften[exp];
427 } else if (cm >= 10) {
434 vp = (man << 4) + exp;
437 RETERR(mem_tobuffer(target, &version, 1));
438 RETERR(mem_tobuffer(target, &size, 1));
439 RETERR(mem_tobuffer(target, &hp, 1));
440 RETERR(mem_tobuffer(target, &vp, 1));
442 latitude = 0x80000000 + ( d1 * 3600 + m1 * 60 ) * 1000 + s1;
444 latitude = 0x80000000 - ( d1 * 3600 + m1 * 60 ) * 1000 - s1;
445 RETERR(uint32_tobuffer(latitude, target));
448 longitude = 0x80000000 + ( d2 * 3600 + m2 * 60 ) * 1000 + s2;
450 longitude = 0x80000000 - ( d2 * 3600 + m2 * 60 ) * 1000 - s2;
451 RETERR(uint32_tobuffer(longitude, target));
453 return (uint32_tobuffer(altitude, target));
456 static inline isc_result_t
457 totext_loc(ARGS_TOTEXT) {
460 unsigned long latitude;
461 unsigned long longitude;
462 unsigned long altitude;
467 char buf[sizeof("89 59 59.999 N 179 59 59.999 E "
468 "42849672.95m 90000000m 90000000m 90000000m")];
469 char sbuf[sizeof("90000000m")];
470 char hbuf[sizeof("90000000m")];
471 char vbuf[sizeof("90000000m")];
472 unsigned char size, hp, vp;
473 unsigned long poweroften[8] = { 1, 10, 100, 1000,
474 10000, 100000, 1000000, 10000000 };
478 REQUIRE(rdata->type == 29);
479 REQUIRE(rdata->length != 0);
481 dns_rdata_toregion(rdata, &sr);
483 /* version = sr.base[0]; */
486 sprintf(sbuf, "%lum", (size>>4) * poweroften[(size&0x0f)-2]);
488 sprintf(sbuf, "0.%02lum", (size>>4) * poweroften[(size&0x0f)]);
491 sprintf(hbuf, "%lum", (hp>>4) * poweroften[(hp&0x0f)-2]);
493 sprintf(hbuf, "0.%02lum", (hp>>4) * poweroften[(hp&0x0f)]);
496 sprintf(vbuf, "%lum", (vp>>4) * poweroften[(vp&0x0f)-2]);
498 sprintf(vbuf, "0.%02lum", (vp>>4) * poweroften[(vp&0x0f)]);
499 isc_region_consume(&sr, 4);
501 latitude = uint32_fromregion(&sr);
502 isc_region_consume(&sr, 4);
503 if (latitude >= 0x80000000) {
505 latitude -= 0x80000000;
508 latitude = 0x80000000 - latitude;
510 fs1 = (int)(latitude % 1000);
512 s1 = (int)(latitude % 60);
514 m1 = (int)(latitude % 60);
518 longitude = uint32_fromregion(&sr);
519 isc_region_consume(&sr, 4);
520 if (longitude >= 0x80000000) {
522 longitude -= 0x80000000;
525 longitude = 0x80000000 - longitude;
527 fs2 = (int)(longitude % 1000);
529 s2 = (int)(longitude % 60);
531 m2 = (int)(longitude % 60);
535 altitude = uint32_fromregion(&sr);
536 isc_region_consume(&sr, 4);
537 if (altitude < 10000000U) {
539 altitude = 10000000 - altitude;
542 altitude -= 10000000;
545 sprintf(buf, "%d %d %d.%03d %s %d %d %d.%03d %s %s%ld.%02ldm %s %s %s",
546 d1, m1, s1, fs1, north ? "N" : "S",
547 d2, m2, s2, fs2, east ? "E" : "W",
548 below ? "-" : "", altitude/100, altitude % 100,
551 return (str_totext(buf, target));
554 static inline isc_result_t
555 fromwire_loc(ARGS_FROMWIRE) {
558 unsigned long latitude;
559 unsigned long longitude;
568 isc_buffer_activeregion(source, &sr);
570 return (ISC_R_UNEXPECTEDEND);
572 return (ISC_R_NOTIMPLEMENTED);
574 return (ISC_R_UNEXPECTEDEND);
581 if ((c&0xf) > 9 || ((c>>4)&0xf) > 9 || ((c>>4)&0xf) == 0)
582 return (ISC_R_RANGE);
585 * Horizontal precision.
589 if ((c&0xf) > 9 || ((c>>4)&0xf) > 9 || ((c>>4)&0xf) == 0)
590 return (ISC_R_RANGE);
593 * Vertical precision.
597 if ((c&0xf) > 9 || ((c>>4)&0xf) > 9 || ((c>>4)&0xf) == 0)
598 return (ISC_R_RANGE);
599 isc_region_consume(&sr, 4);
604 latitude = uint32_fromregion(&sr);
605 if (latitude < (0x80000000UL - 90 * 3600000) ||
606 latitude > (0x80000000UL + 90 * 3600000))
607 return (ISC_R_RANGE);
608 isc_region_consume(&sr, 4);
613 longitude = uint32_fromregion(&sr);
614 if (longitude < (0x80000000UL - 180 * 3600000) ||
615 longitude > (0x80000000UL + 180 * 3600000))
616 return (ISC_R_RANGE);
620 * All values possible.
623 isc_buffer_activeregion(source, &sr);
624 isc_buffer_forward(source, 16);
625 return (mem_tobuffer(target, sr.base, 16));
628 static inline isc_result_t
629 towire_loc(ARGS_TOWIRE) {
632 REQUIRE(rdata->type == 29);
633 REQUIRE(rdata->length != 0);
635 return (mem_tobuffer(target, rdata->data, rdata->length));
639 compare_loc(ARGS_COMPARE) {
643 REQUIRE(rdata1->type == rdata2->type);
644 REQUIRE(rdata1->rdclass == rdata2->rdclass);
645 REQUIRE(rdata1->type == 29);
646 REQUIRE(rdata1->length != 0);
647 REQUIRE(rdata2->length != 0);
649 dns_rdata_toregion(rdata1, &r1);
650 dns_rdata_toregion(rdata2, &r2);
651 return (isc_region_compare(&r1, &r2));
654 static inline isc_result_t
655 fromstruct_loc(ARGS_FROMSTRUCT) {
656 dns_rdata_loc_t *loc = source;
660 REQUIRE(source != NULL);
661 REQUIRE(loc->common.rdtype == type);
662 REQUIRE(loc->common.rdclass == rdclass);
667 if (loc->v.v0.version != 0)
668 return (ISC_R_NOTIMPLEMENTED);
669 RETERR(uint8_tobuffer(loc->v.v0.version, target));
672 if ((c&0xf) > 9 || ((c>>4)&0xf) > 9 || ((c>>4)&0xf) == 0)
673 return (ISC_R_RANGE);
674 RETERR(uint8_tobuffer(loc->v.v0.size, target));
676 c = loc->v.v0.horizontal;
677 if ((c&0xf) > 9 || ((c>>4)&0xf) > 9 || ((c>>4)&0xf) == 0)
678 return (ISC_R_RANGE);
679 RETERR(uint8_tobuffer(loc->v.v0.horizontal, target));
681 c = loc->v.v0.vertical;
682 if ((c&0xf) > 9 || ((c>>4)&0xf) > 9 || ((c>>4)&0xf) == 0)
683 return (ISC_R_RANGE);
684 RETERR(uint8_tobuffer(loc->v.v0.vertical, target));
686 if (loc->v.v0.latitude < (0x80000000UL - 90 * 3600000) ||
687 loc->v.v0.latitude > (0x80000000UL + 90 * 3600000))
688 return (ISC_R_RANGE);
689 RETERR(uint32_tobuffer(loc->v.v0.latitude, target));
691 if (loc->v.v0.longitude < (0x80000000UL - 180 * 3600000) ||
692 loc->v.v0.longitude > (0x80000000UL + 180 * 3600000))
693 return (ISC_R_RANGE);
694 RETERR(uint32_tobuffer(loc->v.v0.longitude, target));
695 return (uint32_tobuffer(loc->v.v0.altitude, target));
698 static inline isc_result_t
699 tostruct_loc(ARGS_TOSTRUCT) {
700 dns_rdata_loc_t *loc = target;
704 REQUIRE(rdata->type == 29);
705 REQUIRE(target != NULL);
706 REQUIRE(rdata->length != 0);
710 dns_rdata_toregion(rdata, &r);
711 version = uint8_fromregion(&r);
713 return (ISC_R_NOTIMPLEMENTED);
715 loc->common.rdclass = rdata->rdclass;
716 loc->common.rdtype = rdata->type;
717 ISC_LINK_INIT(&loc->common, link);
719 loc->v.v0.version = version;
720 isc_region_consume(&r, 1);
721 loc->v.v0.size = uint8_fromregion(&r);
722 isc_region_consume(&r, 1);
723 loc->v.v0.horizontal = uint8_fromregion(&r);
724 isc_region_consume(&r, 1);
725 loc->v.v0.vertical = uint8_fromregion(&r);
726 isc_region_consume(&r, 1);
727 loc->v.v0.latitude = uint32_fromregion(&r);
728 isc_region_consume(&r, 4);
729 loc->v.v0.longitude = uint32_fromregion(&r);
730 isc_region_consume(&r, 4);
731 loc->v.v0.altitude = uint32_fromregion(&r);
732 isc_region_consume(&r, 4);
733 return (ISC_R_SUCCESS);
737 freestruct_loc(ARGS_FREESTRUCT) {
738 dns_rdata_loc_t *loc = source;
740 REQUIRE(source != NULL);
741 REQUIRE(loc->common.rdtype == 29);
747 static inline isc_result_t
748 additionaldata_loc(ARGS_ADDLDATA) {
749 REQUIRE(rdata->type == 29);
755 return (ISC_R_SUCCESS);
758 static inline isc_result_t
759 digest_loc(ARGS_DIGEST) {
762 REQUIRE(rdata->type == 29);
764 dns_rdata_toregion(rdata, &r);
766 return ((digest)(arg, &r));
769 static inline isc_boolean_t
770 checkowner_loc(ARGS_CHECKOWNER) {
782 static inline isc_boolean_t
783 checknames_loc(ARGS_CHECKNAMES) {
785 REQUIRE(rdata->type == 29);
794 #endif /* RDATA_GENERIC_LOC_29_C */