Coverage Report

Created: 2026-01-24 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/bind9/lib/dns/rdata/generic/loc_29.c
Line
Count
Source
1
/*
2
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3
 *
4
 * SPDX-License-Identifier: MPL-2.0
5
 *
6
 * This Source Code Form is subject to the terms of the Mozilla Public
7
 * License, v. 2.0. If a copy of the MPL was not distributed with this
8
 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
9
 *
10
 * See the COPYRIGHT file distributed with this work for additional
11
 * information regarding copyright ownership.
12
 */
13
14
/* RFC1876 */
15
16
#ifndef RDATA_GENERIC_LOC_29_C
17
#define RDATA_GENERIC_LOC_29_C
18
19
29.7k
#define RRTYPE_LOC_ATTRIBUTES (0)
20
21
static isc_result_t
22
loc_getdecimal(const char *str, unsigned long max, size_t precision, char units,
23
7.77k
         unsigned long *valuep) {
24
7.77k
  bool ok;
25
7.77k
  char *e;
26
7.77k
  size_t i;
27
7.77k
  long tmp;
28
7.77k
  unsigned long value;
29
30
7.77k
  value = strtoul(str, &e, 10);
31
7.77k
  if (*e != 0 && *e != '.' && *e != units) {
32
62
    return DNS_R_SYNTAX;
33
62
  }
34
7.71k
  if (value > max) {
35
181
    return ISC_R_RANGE;
36
181
  }
37
7.52k
  ok = e != str;
38
7.52k
  if (*e == '.') {
39
2.36k
    e++;
40
5.87k
    for (i = 0; i < precision; i++) {
41
4.86k
      if (*e == 0 || *e == units) {
42
1.34k
        break;
43
1.34k
      }
44
3.52k
      if ((tmp = decvalue(*e++)) < 0) {
45
6
        return DNS_R_SYNTAX;
46
6
      }
47
3.51k
      ok = true;
48
3.51k
      value *= 10;
49
3.51k
      value += tmp;
50
3.51k
    }
51
4.16k
    for (; i < precision; i++) {
52
1.80k
      value *= 10;
53
1.80k
    }
54
5.16k
  } else {
55
16.8k
    for (i = 0; i < precision; i++) {
56
11.7k
      value *= 10;
57
11.7k
    }
58
5.16k
  }
59
7.52k
  if (*e != 0 && *e == units) {
60
774
    e++;
61
774
  }
62
7.52k
  if (!ok || *e != 0) {
63
29
    return DNS_R_SYNTAX;
64
29
  }
65
7.49k
  *valuep = value;
66
7.49k
  return ISC_R_SUCCESS;
67
7.52k
}
68
69
static isc_result_t
70
7.54k
loc_getprecision(const char *str, unsigned char *valuep) {
71
7.54k
  unsigned long poweroften[8] = { 1,     10,     100, 1000,
72
7.54k
          10000, 100000, 1000000, 10000000 };
73
7.54k
  unsigned long m, cm;
74
7.54k
  bool ok;
75
7.54k
  char *e;
76
7.54k
  size_t i;
77
7.54k
  long tmp;
78
7.54k
  int man;
79
7.54k
  int exp;
80
81
7.54k
  m = strtoul(str, &e, 10);
82
7.54k
  if (*e != 0 && *e != '.' && *e != 'm') {
83
51
    return DNS_R_SYNTAX;
84
51
  }
85
7.49k
  if (m > 90000000) {
86
136
    return ISC_R_RANGE;
87
136
  }
88
7.35k
  cm = 0;
89
7.35k
  ok = e != str;
90
7.35k
  if (*e == '.') {
91
2.12k
    e++;
92
4.24k
    for (i = 0; i < 2; i++) {
93
3.54k
      if (*e == 0 || *e == 'm') {
94
1.40k
        break;
95
1.40k
      }
96
2.13k
      if ((tmp = decvalue(*e++)) < 0) {
97
15
        return DNS_R_SYNTAX;
98
15
      }
99
2.11k
      ok = true;
100
2.11k
      cm *= 10;
101
2.11k
      cm += tmp;
102
2.11k
    }
103
4.21k
    for (; i < 2; i++) {
104
2.10k
      cm *= 10;
105
2.10k
    }
106
2.11k
  }
107
7.34k
  if (*e == 'm') {
108
1.41k
    e++;
109
1.41k
  }
110
7.34k
  if (!ok || *e != 0) {
111
25
    return DNS_R_SYNTAX;
112
25
  }
113
114
  /*
115
   * We don't just multiply out as we will overflow.
116
   */
117
7.31k
  if (m > 0) {
118
11.9k
    for (exp = 0; exp < 7; exp++) {
119
11.5k
      if (m < poweroften[exp + 1]) {
120
3.81k
        break;
121
3.81k
      }
122
11.5k
    }
123
4.21k
    man = m / poweroften[exp];
124
4.21k
    exp += 2;
125
4.21k
  } else if (cm >= 10) {
126
569
    man = cm / 10;
127
569
    exp = 1;
128
2.53k
  } else {
129
2.53k
    man = cm;
130
2.53k
    exp = 0;
131
2.53k
  }
132
7.31k
  *valuep = (man << 4) + exp;
133
7.31k
  return ISC_R_SUCCESS;
134
7.34k
}
135
136
static isc_result_t
137
11.6k
get_degrees(isc_lex_t *lexer, isc_token_t *token, unsigned long *d) {
138
11.6k
  RETERR(isc_lex_getmastertoken(lexer, token, isc_tokentype_number,
139
11.6k
              false));
140
11.6k
  *d = token->value.as_ulong;
141
142
11.6k
  return ISC_R_SUCCESS;
143
11.6k
}
144
145
static isc_result_t
146
check_coordinate(unsigned long d, unsigned long m, unsigned long s,
147
19.3k
     unsigned long maxd) {
148
19.3k
  if (d > maxd || m > 59U) {
149
104
    return ISC_R_RANGE;
150
104
  }
151
19.2k
  if (d == maxd && (m != 0 || s != 0)) {
152
14
    return ISC_R_RANGE;
153
14
  }
154
155
19.2k
  return ISC_R_SUCCESS;
156
19.2k
}
157
158
static isc_result_t
159
5.79k
get_minutes(isc_lex_t *lexer, isc_token_t *token, unsigned long *m) {
160
5.79k
  RETERR(isc_lex_getmastertoken(lexer, token, isc_tokentype_number,
161
5.79k
              false));
162
163
5.74k
  *m = token->value.as_ulong;
164
165
5.74k
  return ISC_R_SUCCESS;
166
5.79k
}
167
168
static isc_result_t
169
2.23k
get_seconds(isc_lex_t *lexer, isc_token_t *token, unsigned long *s) {
170
2.23k
  RETERR(isc_lex_getmastertoken(lexer, token, isc_tokentype_string,
171
2.23k
              false));
172
2.23k
  RETERR(loc_getdecimal(DNS_AS_STR(*token), 59, 3, '\0', s));
173
174
1.97k
  return ISC_R_SUCCESS;
175
2.23k
}
176
177
static isc_result_t
178
get_direction(isc_lex_t *lexer, isc_token_t *token, const char *directions,
179
19.2k
        int *direction) {
180
19.2k
  RETERR(isc_lex_getmastertoken(lexer, token, isc_tokentype_string,
181
19.2k
              false));
182
19.1k
  if (DNS_AS_STR(*token)[0] == directions[1] &&
183
5.19k
      DNS_AS_STR(*token)[1] == 0)
184
5.17k
  {
185
5.17k
    *direction = DNS_AS_STR(*token)[0];
186
5.17k
    return ISC_R_SUCCESS;
187
5.17k
  }
188
189
14.0k
  if (DNS_AS_STR(*token)[0] == directions[0] &&
190
5.99k
      DNS_AS_STR(*token)[1] == 0)
191
5.97k
  {
192
5.97k
    *direction = DNS_AS_STR(*token)[0];
193
5.97k
    return ISC_R_SUCCESS;
194
5.97k
  }
195
196
8.04k
  *direction = 0;
197
8.04k
  isc_lex_ungettoken(lexer, token);
198
8.04k
  return ISC_R_SUCCESS;
199
14.0k
}
200
201
static isc_result_t
202
loc_getcoordinate(isc_lex_t *lexer, unsigned long *dp, unsigned long *mp,
203
      unsigned long *sp, const char *directions, int *directionp,
204
11.6k
      unsigned long maxd) {
205
11.6k
  isc_result_t result = ISC_R_SUCCESS;
206
11.6k
  isc_token_t token;
207
11.6k
  unsigned long d, m, s;
208
11.6k
  int direction = 0;
209
210
11.6k
  m = 0;
211
11.6k
  s = 0;
212
213
  /*
214
   * Degrees.
215
   */
216
11.6k
  RETERR(get_degrees(lexer, &token, &d));
217
11.6k
  RETTOK(check_coordinate(d, m, s, maxd));
218
219
  /*
220
   * Minutes.
221
   */
222
11.5k
  RETERR(get_direction(lexer, &token, directions, &direction));
223
11.5k
  if (direction > 0) {
224
5.78k
    goto done;
225
5.78k
  }
226
227
5.79k
  RETERR(get_minutes(lexer, &token, &m));
228
5.74k
  RETTOK(check_coordinate(d, m, s, maxd));
229
230
  /*
231
   * Seconds.
232
   */
233
5.71k
  RETERR(get_direction(lexer, &token, directions, &direction));
234
5.68k
  if (direction > 0) {
235
3.44k
    goto done;
236
3.44k
  }
237
238
2.23k
  result = get_seconds(lexer, &token, &s);
239
2.23k
  if (result == ISC_R_RANGE || result == DNS_R_SYNTAX) {
240
264
    RETTOK(result);
241
264
  }
242
1.97k
  RETERR(result);
243
1.97k
  RETTOK(check_coordinate(d, m, s, maxd));
244
245
  /*
246
   * Direction.
247
   */
248
1.97k
  RETERR(get_direction(lexer, &token, directions, &direction));
249
1.93k
  if (direction == 0) {
250
16
    RETERR(DNS_R_SYNTAX);
251
0
  }
252
11.1k
done:
253
254
11.1k
  *directionp = direction;
255
11.1k
  *dp = d;
256
11.1k
  *mp = m;
257
11.1k
  *sp = s;
258
259
11.1k
  return ISC_R_SUCCESS;
260
1.93k
}
261
262
static isc_result_t
263
6.09k
loc_getlatitude(isc_lex_t *lexer, unsigned long *latitude) {
264
6.09k
  unsigned long d1 = 0, m1 = 0, s1 = 0;
265
6.09k
  int direction = 0;
266
267
6.09k
  RETERR(loc_getcoordinate(lexer, &d1, &m1, &s1, "SN", &direction, 90U));
268
269
5.60k
  switch (direction) {
270
4.67k
  case 'N':
271
4.67k
    *latitude = 0x80000000 + (d1 * 3600 + m1 * 60) * 1000 + s1;
272
4.67k
    break;
273
927
  case 'S':
274
927
    *latitude = 0x80000000 - (d1 * 3600 + m1 * 60) * 1000 - s1;
275
927
    break;
276
0
  default:
277
0
    UNREACHABLE();
278
5.60k
  }
279
280
5.60k
  return ISC_R_SUCCESS;
281
5.60k
}
282
283
static isc_result_t
284
5.60k
loc_getlongitude(isc_lex_t *lexer, unsigned long *longitude) {
285
5.60k
  unsigned long d2 = 0, m2 = 0, s2 = 0;
286
5.60k
  int direction = 0;
287
288
5.60k
  RETERR(loc_getcoordinate(lexer, &d2, &m2, &s2, "WE", &direction, 180U));
289
290
5.54k
  switch (direction) {
291
497
  case 'E':
292
497
    *longitude = 0x80000000 + (d2 * 3600 + m2 * 60) * 1000 + s2;
293
497
    break;
294
5.04k
  case 'W':
295
5.04k
    *longitude = 0x80000000 - (d2 * 3600 + m2 * 60) * 1000 - s2;
296
5.04k
    break;
297
0
  default:
298
0
    UNREACHABLE();
299
5.54k
  }
300
301
5.54k
  return ISC_R_SUCCESS;
302
5.54k
}
303
304
static isc_result_t
305
5.54k
loc_getaltitude(isc_lex_t *lexer, unsigned long *altitude) {
306
5.54k
  isc_token_t token;
307
5.54k
  unsigned long cm;
308
5.54k
  const char *str;
309
310
5.54k
  RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
311
5.54k
              false));
312
5.53k
  str = DNS_AS_STR(token);
313
5.53k
  if (DNS_AS_STR(token)[0] == '-') {
314
428
    RETTOK(loc_getdecimal(str + 1, 100000, 2, 'm', &cm));
315
425
    if (cm > 10000000UL) {
316
1
      RETTOK(ISC_R_RANGE);
317
1
    }
318
424
    *altitude = 10000000 - cm;
319
5.10k
  } else {
320
5.10k
    RETTOK(loc_getdecimal(str, 42849672, 2, 'm', &cm));
321
5.09k
    if (cm > 4284967295UL) {
322
0
      RETTOK(ISC_R_RANGE);
323
0
    }
324
5.09k
    *altitude = 10000000 + cm;
325
5.09k
  }
326
327
5.51k
  return ISC_R_SUCCESS;
328
5.53k
}
329
330
static isc_result_t
331
11.1k
loc_getoptionalprecision(isc_lex_t *lexer, unsigned char *valuep) {
332
11.1k
  isc_token_t token;
333
334
11.1k
  RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
335
11.1k
              true));
336
11.0k
  if (token.type == isc_tokentype_eol || token.type == isc_tokentype_eof)
337
3.52k
  {
338
3.52k
    isc_lex_ungettoken(lexer, &token);
339
3.52k
    return ISC_R_NOMORE;
340
3.52k
  }
341
7.54k
  RETTOK(loc_getprecision(DNS_AS_STR(token), valuep));
342
343
7.31k
  return ISC_R_SUCCESS;
344
7.54k
}
345
346
static isc_result_t
347
5.51k
loc_getsize(isc_lex_t *lexer, unsigned char *sizep) {
348
5.51k
  return loc_getoptionalprecision(lexer, sizep);
349
5.51k
}
350
351
static isc_result_t
352
3.17k
loc_gethorizontalprecision(isc_lex_t *lexer, unsigned char *hpp) {
353
3.17k
  return loc_getoptionalprecision(lexer, hpp);
354
3.17k
}
355
356
static isc_result_t
357
2.45k
loc_getverticalprecision(isc_lex_t *lexer, unsigned char *vpp) {
358
2.45k
  return loc_getoptionalprecision(lexer, vpp);
359
2.45k
}
360
361
/* The LOC record is expressed in a master file in the following format:
362
 *
363
 * <owner> <TTL> <class> LOC ( d1 [m1 [s1]] {"N"|"S"} d2 [m2 [s2]]
364
 *                             {"E"|"W"} alt["m"] [siz["m"] [hp["m"]
365
 *                             [vp["m"]]]] )
366
 *
367
 * (The parentheses are used for multi-line data as specified in [RFC
368
 * 1035] section 5.1.)
369
 *
370
 * where:
371
 *
372
 *     d1:     [0 .. 90]            (degrees latitude)
373
 *     d2:     [0 .. 180]           (degrees longitude)
374
 *     m1, m2: [0 .. 59]            (minutes latitude/longitude)
375
 *     s1, s2: [0 .. 59.999]        (seconds latitude/longitude)
376
 *     alt:    [-100000.00 .. 42849672.95] BY .01 (altitude in meters)
377
 *     siz, hp, vp: [0 .. 90000000.00] (size/precision in meters)
378
 *
379
 * If omitted, minutes and seconds default to zero, size defaults to 1m,
380
 * horizontal precision defaults to 10000m, and vertical precision
381
 * defaults to 10m.  These defaults are chosen to represent typical
382
 * ZIP/postal code area sizes, since it is often easy to find
383
 * approximate geographical location by ZIP/postal code.
384
 */
385
static isc_result_t
386
6.09k
fromtext_loc(ARGS_FROMTEXT) {
387
6.09k
  isc_result_t result = ISC_R_SUCCESS;
388
6.09k
  unsigned long latitude = 0;
389
6.09k
  unsigned long longitude = 0;
390
6.09k
  unsigned long altitude = 0;
391
6.09k
  unsigned char size = 0x12; /* Default: 1.00m */
392
6.09k
  unsigned char hp = 0x16;   /* Default: 10000.00 m */
393
6.09k
  unsigned char vp = 0x13;   /* Default: 10.00 m */
394
6.09k
  unsigned char version = 0;
395
396
6.09k
  REQUIRE(type == dns_rdatatype_loc);
397
398
6.09k
  UNUSED(type);
399
6.09k
  UNUSED(rdclass);
400
6.09k
  UNUSED(origin);
401
6.09k
  UNUSED(options);
402
6.09k
  UNUSED(callbacks);
403
404
6.09k
  RETERR(loc_getlatitude(lexer, &latitude));
405
5.60k
  RETERR(loc_getlongitude(lexer, &longitude));
406
5.54k
  RETERR(loc_getaltitude(lexer, &altitude));
407
5.51k
  result = loc_getsize(lexer, &size);
408
5.51k
  if (result == ISC_R_NOMORE) {
409
2.13k
    result = ISC_R_SUCCESS;
410
2.13k
    goto encode;
411
2.13k
  }
412
3.38k
  RETERR(result);
413
3.17k
  result = loc_gethorizontalprecision(lexer, &hp);
414
3.17k
  if (result == ISC_R_NOMORE) {
415
650
    result = ISC_R_SUCCESS;
416
650
    goto encode;
417
650
  }
418
2.52k
  RETERR(result);
419
2.45k
  result = loc_getverticalprecision(lexer, &vp);
420
2.45k
  if (result == ISC_R_NOMORE) {
421
738
    result = ISC_R_SUCCESS;
422
738
    goto encode;
423
738
  }
424
1.71k
  RETERR(result);
425
5.21k
encode:
426
5.21k
  RETERR(mem_tobuffer(target, &version, 1));
427
5.21k
  RETERR(mem_tobuffer(target, &size, 1));
428
5.21k
  RETERR(mem_tobuffer(target, &hp, 1));
429
5.21k
  RETERR(mem_tobuffer(target, &vp, 1));
430
431
5.21k
  RETERR(uint32_tobuffer(latitude, target));
432
5.21k
  RETERR(uint32_tobuffer(longitude, target));
433
5.21k
  RETERR(uint32_tobuffer(altitude, target));
434
435
5.21k
  return result;
436
5.21k
}
437
438
static isc_result_t
439
5.42k
totext_loc(ARGS_TOTEXT) {
440
5.42k
  int d1, m1, s1, fs1;
441
5.42k
  int d2, m2, s2, fs2;
442
5.42k
  unsigned long latitude;
443
5.42k
  unsigned long longitude;
444
5.42k
  unsigned long altitude;
445
5.42k
  bool north;
446
5.42k
  bool east;
447
5.42k
  bool below;
448
5.42k
  isc_region_t sr;
449
5.42k
  char sbuf[sizeof("90000000m")];
450
5.42k
  char hbuf[sizeof("90000000m")];
451
5.42k
  char vbuf[sizeof("90000000m")];
452
  /* "89 59 59.999 N 179 59 59.999 E " */
453
  /* "-42849672.95m 90000000m 90000000m 90000000m"; */
454
5.42k
  char buf[8 * 6 + 12 * 1 + 2 * 10 + sizeof(sbuf) + sizeof(hbuf) +
455
5.42k
     sizeof(vbuf)];
456
5.42k
  unsigned char size, hp, vp;
457
5.42k
  unsigned long poweroften[8] = { 1,     10,     100, 1000,
458
5.42k
          10000, 100000, 1000000, 10000000 };
459
460
5.42k
  UNUSED(tctx);
461
462
5.42k
  REQUIRE(rdata->type == dns_rdatatype_loc);
463
5.42k
  REQUIRE(rdata->length != 0);
464
465
5.42k
  dns_rdata_toregion(rdata, &sr);
466
467
5.42k
  if (sr.base[0] != 0) {
468
1.20k
    return ISC_R_NOTIMPLEMENTED;
469
1.20k
  }
470
471
4.22k
  REQUIRE(rdata->length == 16);
472
473
4.22k
  size = sr.base[1];
474
4.22k
  INSIST((size & 0x0f) < 10 && (size >> 4) < 10);
475
4.22k
  if ((size & 0x0f) > 1) {
476
1.02k
    snprintf(sbuf, sizeof(sbuf), "%lum",
477
1.02k
       (size >> 4) * poweroften[(size & 0x0f) - 2]);
478
3.20k
  } else {
479
3.20k
    snprintf(sbuf, sizeof(sbuf), "0.%02lum",
480
3.20k
       (size >> 4) * poweroften[(size & 0x0f)]);
481
3.20k
  }
482
4.22k
  hp = sr.base[2];
483
4.22k
  INSIST((hp & 0x0f) < 10 && (hp >> 4) < 10);
484
4.22k
  if ((hp & 0x0f) > 1) {
485
3.10k
    snprintf(hbuf, sizeof(hbuf), "%lum",
486
3.10k
       (hp >> 4) * poweroften[(hp & 0x0f) - 2]);
487
3.10k
  } else {
488
1.12k
    snprintf(hbuf, sizeof(hbuf), "0.%02lum",
489
1.12k
       (hp >> 4) * poweroften[(hp & 0x0f)]);
490
1.12k
  }
491
4.22k
  vp = sr.base[3];
492
4.22k
  INSIST((vp & 0x0f) < 10 && (vp >> 4) < 10);
493
4.22k
  if ((vp & 0x0f) > 1) {
494
866
    snprintf(vbuf, sizeof(vbuf), "%lum",
495
866
       (vp >> 4) * poweroften[(vp & 0x0f) - 2]);
496
3.35k
  } else {
497
3.35k
    snprintf(vbuf, sizeof(vbuf), "0.%02lum",
498
3.35k
       (vp >> 4) * poweroften[(vp & 0x0f)]);
499
3.35k
  }
500
4.22k
  isc_region_consume(&sr, 4);
501
502
4.22k
  latitude = uint32_fromregion(&sr);
503
4.22k
  isc_region_consume(&sr, 4);
504
4.22k
  if (latitude >= 0x80000000) {
505
2.82k
    north = true;
506
2.82k
    latitude -= 0x80000000;
507
2.82k
  } else {
508
1.40k
    north = false;
509
1.40k
    latitude = 0x80000000 - latitude;
510
1.40k
  }
511
4.22k
  fs1 = (int)(latitude % 1000);
512
4.22k
  latitude /= 1000;
513
4.22k
  s1 = (int)(latitude % 60);
514
4.22k
  latitude /= 60;
515
4.22k
  m1 = (int)(latitude % 60);
516
4.22k
  latitude /= 60;
517
4.22k
  d1 = (int)latitude;
518
4.22k
  INSIST(latitude <= 90U);
519
520
4.22k
  longitude = uint32_fromregion(&sr);
521
4.22k
  isc_region_consume(&sr, 4);
522
4.22k
  if (longitude >= 0x80000000) {
523
1.18k
    east = true;
524
1.18k
    longitude -= 0x80000000;
525
3.03k
  } else {
526
3.03k
    east = false;
527
3.03k
    longitude = 0x80000000 - longitude;
528
3.03k
  }
529
4.22k
  fs2 = (int)(longitude % 1000);
530
4.22k
  longitude /= 1000;
531
4.22k
  s2 = (int)(longitude % 60);
532
4.22k
  longitude /= 60;
533
4.22k
  m2 = (int)(longitude % 60);
534
4.22k
  longitude /= 60;
535
4.22k
  d2 = (int)longitude;
536
4.22k
  INSIST(longitude <= 180U);
537
538
4.22k
  altitude = uint32_fromregion(&sr);
539
4.22k
  isc_region_consume(&sr, 4);
540
4.22k
  if (altitude < 10000000U) {
541
676
    below = true;
542
676
    altitude = 10000000 - altitude;
543
3.54k
  } else {
544
3.54k
    below = false;
545
3.54k
    altitude -= 10000000;
546
3.54k
  }
547
548
4.22k
  snprintf(buf, sizeof(buf),
549
4.22k
     "%d %d %d.%03d %s %d %d %d.%03d %s %s%lu.%02lum %s %s %s", d1,
550
4.22k
     m1, s1, fs1, north ? "N" : "S", d2, m2, s2, fs2,
551
4.22k
     east ? "E" : "W", below ? "-" : "", altitude / 100,
552
4.22k
     altitude % 100, sbuf, hbuf, vbuf);
553
554
4.22k
  return str_totext(buf, target);
555
5.42k
}
556
557
static isc_result_t
558
7.07k
fromwire_loc(ARGS_FROMWIRE) {
559
7.07k
  isc_region_t sr;
560
7.07k
  unsigned char c;
561
7.07k
  unsigned long latitude;
562
7.07k
  unsigned long longitude;
563
564
7.07k
  REQUIRE(type == dns_rdatatype_loc);
565
566
7.07k
  UNUSED(type);
567
7.07k
  UNUSED(rdclass);
568
7.07k
  UNUSED(dctx);
569
570
7.07k
  isc_buffer_activeregion(source, &sr);
571
7.07k
  if (sr.length < 1) {
572
5
    return ISC_R_UNEXPECTEDEND;
573
5
  }
574
7.07k
  if (sr.base[0] != 0) {
575
    /* Treat as unknown. */
576
2.63k
    isc_buffer_forward(source, sr.length);
577
2.63k
    return mem_tobuffer(target, sr.base, sr.length);
578
2.63k
  }
579
4.44k
  if (sr.length < 16) {
580
6
    return ISC_R_UNEXPECTEDEND;
581
6
  }
582
583
  /*
584
   * Size.
585
   */
586
4.43k
  c = sr.base[1];
587
4.43k
  if (c != 0) {
588
1.44k
    if ((c & 0xf) > 9 || ((c >> 4) & 0xf) > 9 ||
589
1.43k
        ((c >> 4) & 0xf) == 0)
590
18
    {
591
18
      return ISC_R_RANGE;
592
593
      /*
594
       * Horizontal precision.
595
       */
596
18
    }
597
1.44k
  }
598
599
  /*
600
   * Horizontal precision.
601
   */
602
4.41k
  c = sr.base[2];
603
4.41k
  if (c != 0) {
604
3.50k
    if ((c & 0xf) > 9 || ((c >> 4) & 0xf) > 9 ||
605
3.49k
        ((c >> 4) & 0xf) == 0)
606
15
    {
607
15
      return ISC_R_RANGE;
608
609
      /*
610
       * Vertical precision.
611
       */
612
15
    }
613
3.50k
  }
614
615
  /*
616
   * Vertical precision.
617
   */
618
4.40k
  c = sr.base[3];
619
4.40k
  if (c != 0) {
620
2.90k
    if ((c & 0xf) > 9 || ((c >> 4) & 0xf) > 9 ||
621
2.89k
        ((c >> 4) & 0xf) == 0)
622
12
    {
623
12
      return ISC_R_RANGE;
624
12
    }
625
2.90k
  }
626
4.39k
  isc_region_consume(&sr, 4);
627
628
  /*
629
   * Latitude.
630
   */
631
4.39k
  latitude = uint32_fromregion(&sr);
632
4.39k
  if (latitude < (0x80000000UL - 90 * 3600000) ||
633
4.33k
      latitude > (0x80000000UL + 90 * 3600000))
634
62
  {
635
62
    return ISC_R_RANGE;
636
62
  }
637
4.32k
  isc_region_consume(&sr, 4);
638
639
  /*
640
   * Longitude.
641
   */
642
4.32k
  longitude = uint32_fromregion(&sr);
643
4.32k
  if (longitude < (0x80000000UL - 180 * 3600000) ||
644
4.23k
      longitude > (0x80000000UL + 180 * 3600000))
645
118
  {
646
118
    return ISC_R_RANGE;
647
118
  }
648
649
  /*
650
   * Altitude.
651
   * All values possible.
652
   */
653
654
4.21k
  isc_buffer_activeregion(source, &sr);
655
4.21k
  isc_buffer_forward(source, 16);
656
4.21k
  return mem_tobuffer(target, sr.base, 16);
657
4.32k
}
658
659
static isc_result_t
660
2.78k
towire_loc(ARGS_TOWIRE) {
661
2.78k
  UNUSED(cctx);
662
663
2.78k
  REQUIRE(rdata->type == dns_rdatatype_loc);
664
2.78k
  REQUIRE(rdata->length != 0);
665
666
2.78k
  return mem_tobuffer(target, rdata->data, rdata->length);
667
2.78k
}
668
669
static int
670
11.0k
compare_loc(ARGS_COMPARE) {
671
11.0k
  isc_region_t r1;
672
11.0k
  isc_region_t r2;
673
674
11.0k
  REQUIRE(rdata1->type == rdata2->type);
675
11.0k
  REQUIRE(rdata1->rdclass == rdata2->rdclass);
676
11.0k
  REQUIRE(rdata1->type == dns_rdatatype_loc);
677
11.0k
  REQUIRE(rdata1->length != 0);
678
11.0k
  REQUIRE(rdata2->length != 0);
679
680
11.0k
  dns_rdata_toregion(rdata1, &r1);
681
11.0k
  dns_rdata_toregion(rdata2, &r2);
682
11.0k
  return isc_region_compare(&r1, &r2);
683
11.0k
}
684
685
static isc_result_t
686
0
fromstruct_loc(ARGS_FROMSTRUCT) {
687
0
  dns_rdata_loc_t *loc = source;
688
0
  uint8_t c;
689
690
0
  REQUIRE(type == dns_rdatatype_loc);
691
0
  REQUIRE(loc != NULL);
692
0
  REQUIRE(loc->common.rdtype == type);
693
0
  REQUIRE(loc->common.rdclass == rdclass);
694
695
0
  UNUSED(type);
696
0
  UNUSED(rdclass);
697
698
0
  if (loc->v.v0.version != 0) {
699
0
    return ISC_R_NOTIMPLEMENTED;
700
0
  }
701
0
  RETERR(uint8_tobuffer(loc->v.v0.version, target));
702
703
0
  c = loc->v.v0.size;
704
0
  if ((c & 0xf) > 9 || ((c >> 4) & 0xf) > 9 || ((c >> 4) & 0xf) == 0) {
705
0
    return ISC_R_RANGE;
706
0
  }
707
0
  RETERR(uint8_tobuffer(loc->v.v0.size, target));
708
709
0
  c = loc->v.v0.horizontal;
710
0
  if ((c & 0xf) > 9 || ((c >> 4) & 0xf) > 9 || ((c >> 4) & 0xf) == 0) {
711
0
    return ISC_R_RANGE;
712
0
  }
713
0
  RETERR(uint8_tobuffer(loc->v.v0.horizontal, target));
714
715
0
  c = loc->v.v0.vertical;
716
0
  if ((c & 0xf) > 9 || ((c >> 4) & 0xf) > 9 || ((c >> 4) & 0xf) == 0) {
717
0
    return ISC_R_RANGE;
718
0
  }
719
0
  RETERR(uint8_tobuffer(loc->v.v0.vertical, target));
720
721
0
  if (loc->v.v0.latitude < (0x80000000UL - 90 * 3600000) ||
722
0
      loc->v.v0.latitude > (0x80000000UL + 90 * 3600000))
723
0
  {
724
0
    return ISC_R_RANGE;
725
0
  }
726
0
  RETERR(uint32_tobuffer(loc->v.v0.latitude, target));
727
728
0
  if (loc->v.v0.longitude < (0x80000000UL - 180 * 3600000) ||
729
0
      loc->v.v0.longitude > (0x80000000UL + 180 * 3600000))
730
0
  {
731
0
    return ISC_R_RANGE;
732
0
  }
733
0
  RETERR(uint32_tobuffer(loc->v.v0.longitude, target));
734
0
  return uint32_tobuffer(loc->v.v0.altitude, target);
735
0
}
736
737
static isc_result_t
738
0
tostruct_loc(ARGS_TOSTRUCT) {
739
0
  dns_rdata_loc_t *loc = target;
740
0
  isc_region_t r;
741
0
  uint8_t version;
742
743
0
  REQUIRE(rdata->type == dns_rdatatype_loc);
744
0
  REQUIRE(loc != NULL);
745
0
  REQUIRE(rdata->length != 0);
746
747
0
  UNUSED(mctx);
748
749
0
  dns_rdata_toregion(rdata, &r);
750
0
  version = uint8_fromregion(&r);
751
0
  if (version != 0) {
752
0
    return ISC_R_NOTIMPLEMENTED;
753
0
  }
754
755
0
  DNS_RDATACOMMON_INIT(loc, rdata->type, rdata->rdclass);
756
757
0
  loc->v.v0.version = version;
758
0
  isc_region_consume(&r, 1);
759
0
  loc->v.v0.size = uint8_fromregion(&r);
760
0
  isc_region_consume(&r, 1);
761
0
  loc->v.v0.horizontal = uint8_fromregion(&r);
762
0
  isc_region_consume(&r, 1);
763
0
  loc->v.v0.vertical = uint8_fromregion(&r);
764
0
  isc_region_consume(&r, 1);
765
0
  loc->v.v0.latitude = uint32_fromregion(&r);
766
0
  isc_region_consume(&r, 4);
767
0
  loc->v.v0.longitude = uint32_fromregion(&r);
768
0
  isc_region_consume(&r, 4);
769
0
  loc->v.v0.altitude = uint32_fromregion(&r);
770
0
  isc_region_consume(&r, 4);
771
0
  return ISC_R_SUCCESS;
772
0
}
773
774
static void
775
0
freestruct_loc(ARGS_FREESTRUCT) {
776
0
  dns_rdata_loc_t *loc = source;
777
778
0
  REQUIRE(loc != NULL);
779
0
  REQUIRE(loc->common.rdtype == dns_rdatatype_loc);
780
781
0
  UNUSED(source);
782
0
  UNUSED(loc);
783
0
}
784
785
static isc_result_t
786
0
additionaldata_loc(ARGS_ADDLDATA) {
787
0
  REQUIRE(rdata->type == dns_rdatatype_loc);
788
789
0
  UNUSED(rdata);
790
0
  UNUSED(owner);
791
0
  UNUSED(add);
792
0
  UNUSED(arg);
793
794
0
  return ISC_R_SUCCESS;
795
0
}
796
797
static isc_result_t
798
0
digest_loc(ARGS_DIGEST) {
799
0
  isc_region_t r;
800
801
0
  REQUIRE(rdata->type == dns_rdatatype_loc);
802
803
0
  dns_rdata_toregion(rdata, &r);
804
805
0
  return (digest)(arg, &r);
806
0
}
807
808
static bool
809
0
checkowner_loc(ARGS_CHECKOWNER) {
810
0
  REQUIRE(type == dns_rdatatype_loc);
811
812
0
  UNUSED(name);
813
0
  UNUSED(type);
814
0
  UNUSED(rdclass);
815
0
  UNUSED(wildcard);
816
817
0
  return true;
818
0
}
819
820
static bool
821
0
checknames_loc(ARGS_CHECKNAMES) {
822
0
  REQUIRE(rdata->type == dns_rdatatype_loc);
823
824
0
  UNUSED(rdata);
825
0
  UNUSED(owner);
826
0
  UNUSED(bad);
827
828
0
  return true;
829
0
}
830
831
static int
832
0
casecompare_loc(ARGS_COMPARE) {
833
0
  return compare_loc(rdata1, rdata2);
834
0
}
835
836
#endif /* RDATA_GENERIC_LOC_29_C */