Initial import from FreeBSD RELENG_4:
[dragonfly.git] / lib / libcr / net / ns_print.c
1 /*
2  * Copyright (c) 1996, 1998 by Internet Software Consortium.
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
9  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
10  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
11  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
13  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
14  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
15  * SOFTWARE.
16  */
17
18 #ifndef lint
19 static char rcsid[] = "$FreeBSD: src/lib/libc/net/ns_print.c,v 1.2 1999/08/28 00:00:15 peter Exp $";
20 #endif
21
22 /* Import. */
23
24 #include <sys/types.h>
25 #include <sys/socket.h>
26
27 #include <netinet/in.h>
28 #include <arpa/nameser.h>
29 #include <arpa/inet.h>
30
31 #include <assert.h>
32 #include <errno.h>
33 #include <resolv.h>
34 #include <string.h>
35 #include <ctype.h>
36
37 #define SPRINTF(x) ((size_t)sprintf x)
38
39 /* Forward. */
40
41 static size_t   prune_origin(const char *name, const char *origin);
42 static int      charstr(const u_char *rdata, const u_char *edata,
43                         char **buf, size_t *buflen);
44 static int      addname(const u_char *msg, size_t msglen,
45                         const u_char **p, const char *origin,
46                         char **buf, size_t *buflen);
47 static void     addlen(size_t len, char **buf, size_t *buflen);
48 static int      addstr(const char *src, size_t len,
49                        char **buf, size_t *buflen);
50 static int      addtab(size_t len, size_t target, int spaced,
51                        char **buf, size_t *buflen);
52
53 /* Macros. */
54
55 #define T(x) \
56         do { \
57                 if ((x) < 0) \
58                         return (-1); \
59         } while (0)
60
61 /* Public. */
62
63 /*
64  * int
65  * ns_sprintrr(handle, rr, name_ctx, origin, buf, buflen)
66  *      Convert an RR to presentation format.
67  * return:
68  *      Number of characters written to buf, or -1 (check errno).
69  */
70 int
71 ns_sprintrr(const ns_msg *handle, const ns_rr *rr,
72             const char *name_ctx, const char *origin,
73             char *buf, size_t buflen)
74 {
75         int n;
76
77         n = ns_sprintrrf(ns_msg_base(*handle), ns_msg_size(*handle),
78                          ns_rr_name(*rr), ns_rr_class(*rr), ns_rr_type(*rr),
79                          ns_rr_ttl(*rr), ns_rr_rdata(*rr), ns_rr_rdlen(*rr),
80                          name_ctx, origin, buf, buflen);
81         return (n);
82 }
83
84 /*
85  * int
86  * ns_sprintrrf(msg, msglen, name, class, type, ttl, rdata, rdlen,
87  *             name_ctx, origin, buf, buflen)
88  *      Convert the fields of an RR into presentation format.
89  * return:
90  *      Number of characters written to buf, or -1 (check errno).
91  */
92 int
93 ns_sprintrrf(const u_char *msg, size_t msglen,
94             const char *name, ns_class class, ns_type type,
95             u_long ttl, const u_char *rdata, size_t rdlen,
96             const char *name_ctx, const char *origin,
97             char *buf, size_t buflen)
98 {
99         const char *obuf = buf;
100         const u_char *edata = rdata + rdlen;
101         int spaced = 0;
102
103         const char *comment;
104         char tmp[100];
105         int len, x;
106
107         /*
108          * Owner.
109          */
110         if (name_ctx != NULL && strcasecmp(name_ctx, name) == 0) {
111                 T(addstr("\t\t\t", 3, &buf, &buflen));
112         } else {
113                 len = prune_origin(name, origin);
114                 if (len == 0) {
115                         T(addstr("@\t\t\t", 4, &buf, &buflen));
116                 } else {
117                         T(addstr(name, len, &buf, &buflen));
118                         /* Origin not used and no trailing dot? */
119                         if ((!origin || !origin[0] || name[len] == '\0') &&
120                             name[len - 1] != '.') {
121                                 T(addstr(".", 1, &buf, &buflen));
122                                 len++;
123                         }
124                         T(spaced = addtab(len, 24, spaced, &buf, &buflen));
125                 }
126         }
127
128         /*
129          * TTL, Class, Type.
130          */
131         T(x = ns_format_ttl(ttl, buf, buflen));
132         addlen(x, &buf, &buflen);
133         len = SPRINTF((tmp, " %s %s", p_class(class), p_type(type)));
134         T(addstr(tmp, len, &buf, &buflen));
135         T(spaced = addtab(x + len, 16, spaced, &buf, &buflen));
136
137         /*
138          * RData.
139          */
140         switch (type) {
141         case ns_t_a:
142                 if (rdlen != NS_INADDRSZ)
143                         goto formerr;
144                 (void) inet_ntop(AF_INET, rdata, buf, buflen);
145                 addlen(strlen(buf), &buf, &buflen);
146                 break;
147
148         case ns_t_cname:
149         case ns_t_mb:
150         case ns_t_mg:
151         case ns_t_mr:
152         case ns_t_ns:
153         case ns_t_ptr:
154                 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
155                 break;
156
157         case ns_t_hinfo:
158         case ns_t_isdn:
159                 /* First word. */
160                 T(len = charstr(rdata, edata, &buf, &buflen));
161                 if (len == 0)
162                         goto formerr;
163                 rdata += len;
164                 T(addstr(" ", 1, &buf, &buflen));
165
166                 /* Second word. */
167                 T(len = charstr(rdata, edata, &buf, &buflen));
168                 if (len == 0)
169                         goto formerr;
170                 rdata += len;
171                 break;
172
173         case ns_t_soa: {
174                 u_long t;
175
176                 /* Server name. */
177                 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
178                 T(addstr(" ", 1, &buf, &buflen));
179
180                 /* Administrator name. */
181                 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
182                 T(addstr(" (\n", 3, &buf, &buflen));
183                 spaced = 0;
184
185                 if ((edata - rdata) != 5*NS_INT32SZ)
186                         goto formerr;
187
188                 /* Serial number. */
189                 t = ns_get32(rdata);  rdata += NS_INT32SZ;
190                 T(addstr("\t\t\t\t\t", 5, &buf, &buflen));
191                 len = SPRINTF((tmp, "%lu", t));
192                 T(addstr(tmp, len, &buf, &buflen));
193                 T(spaced = addtab(len, 16, spaced, &buf, &buflen));
194                 T(addstr("; serial\n", 9, &buf, &buflen));
195                 spaced = 0;
196
197                 /* Refresh interval. */
198                 t = ns_get32(rdata);  rdata += NS_INT32SZ;
199                 T(addstr("\t\t\t\t\t", 5, &buf, &buflen));
200                 T(len = ns_format_ttl(t, buf, buflen));
201                 addlen(len, &buf, &buflen);
202                 T(spaced = addtab(len, 16, spaced, &buf, &buflen));
203                 T(addstr("; refresh\n", 10, &buf, &buflen));
204                 spaced = 0;
205
206                 /* Retry interval. */
207                 t = ns_get32(rdata);  rdata += NS_INT32SZ;
208                 T(addstr("\t\t\t\t\t", 5, &buf, &buflen));
209                 T(len = ns_format_ttl(t, buf, buflen));
210                 addlen(len, &buf, &buflen);
211                 T(spaced = addtab(len, 16, spaced, &buf, &buflen));
212                 T(addstr("; retry\n", 8, &buf, &buflen));
213                 spaced = 0;
214
215                 /* Expiry. */
216                 t = ns_get32(rdata);  rdata += NS_INT32SZ;
217                 T(addstr("\t\t\t\t\t", 5, &buf, &buflen));
218                 T(len = ns_format_ttl(t, buf, buflen));
219                 addlen(len, &buf, &buflen);
220                 T(spaced = addtab(len, 16, spaced, &buf, &buflen));
221                 T(addstr("; expiry\n", 9, &buf, &buflen));
222                 spaced = 0;
223
224                 /* Minimum TTL. */
225                 t = ns_get32(rdata);  rdata += NS_INT32SZ;
226                 T(addstr("\t\t\t\t\t", 5, &buf, &buflen));
227                 T(len = ns_format_ttl(t, buf, buflen));
228                 addlen(len, &buf, &buflen);
229                 T(addstr(" )", 2, &buf, &buflen));
230                 T(spaced = addtab(len, 16, spaced, &buf, &buflen));
231                 T(addstr("; minimum\n", 10, &buf, &buflen));
232
233                 break;
234             }
235
236         case ns_t_mx:
237         case ns_t_afsdb:
238         case ns_t_rt: {
239                 u_int t;
240
241                 if (rdlen < NS_INT16SZ)
242                         goto formerr;
243
244                 /* Priority. */
245                 t = ns_get16(rdata);
246                 rdata += NS_INT16SZ;
247                 len = SPRINTF((tmp, "%u ", t));
248                 T(addstr(tmp, len, &buf, &buflen));
249
250                 /* Target. */
251                 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
252
253                 break;
254             }
255
256         case ns_t_px: {
257                 u_int t;
258
259                 if (rdlen < NS_INT16SZ)
260                         goto formerr;
261
262                 /* Priority. */
263                 t = ns_get16(rdata);
264                 rdata += NS_INT16SZ;
265                 len = SPRINTF((tmp, "%u ", t));
266                 T(addstr(tmp, len, &buf, &buflen));
267
268                 /* Name1. */
269                 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
270                 T(addstr(" ", 1, &buf, &buflen));
271
272                 /* Name2. */
273                 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
274
275                 break;
276             }
277
278         case ns_t_x25:
279                 T(len = charstr(rdata, edata, &buf, &buflen));
280                 if (len == 0)
281                         goto formerr;
282                 rdata += len;
283                 break;
284
285         case ns_t_txt:
286                 while (rdata < edata) {
287                         T(len = charstr(rdata, edata, &buf, &buflen));
288                         if (len == 0)
289                                 goto formerr;
290                         rdata += len;
291                         if (rdata < edata)
292                                 T(addstr(" ", 1, &buf, &buflen));
293                 }
294                 break;
295
296         case ns_t_nsap: {
297                 char t[255*3];
298
299                 (void) inet_nsap_ntoa(rdlen, rdata, t);
300                 T(addstr(t, strlen(t), &buf, &buflen));
301                 break;
302             }
303
304         case ns_t_aaaa:
305                 if (rdlen != NS_IN6ADDRSZ)
306                         goto formerr;
307                 (void) inet_ntop(AF_INET6, rdata, buf, buflen);
308                 addlen(strlen(buf), &buf, &buflen);
309                 break;
310
311         case ns_t_loc: {
312                 char t[255];
313
314                 /* XXX protocol format checking? */
315                 (void) loc_ntoa(rdata, t);
316                 T(addstr(t, strlen(t), &buf, &buflen));
317                 break;
318             }
319
320         case ns_t_naptr: {
321                 u_int order, preference;
322                 char t[50];
323
324                 if (rdlen < 2*NS_INT16SZ)
325                         goto formerr;
326
327                 /* Order, Precedence. */
328                 order = ns_get16(rdata);        rdata += NS_INT16SZ;
329                 preference = ns_get16(rdata);   rdata += NS_INT16SZ;
330                 len = SPRINTF((t, "%u %u ", order, preference));
331                 T(addstr(t, len, &buf, &buflen));
332
333                 /* Flags. */
334                 T(len = charstr(rdata, edata, &buf, &buflen));
335                 if (len == 0)
336                         goto formerr;
337                 rdata += len;
338                 T(addstr(" ", 1, &buf, &buflen));
339
340                 /* Service. */
341                 T(len = charstr(rdata, edata, &buf, &buflen));
342                 if (len == 0)
343                         goto formerr;
344                 rdata += len;
345                 T(addstr(" ", 1, &buf, &buflen));
346
347                 /* Regexp. */
348                 T(len = charstr(rdata, edata, &buf, &buflen));
349                 if (len < 0)
350                         return (-1);
351                 if (len == 0)
352                         goto formerr;
353                 rdata += len;
354                 T(addstr(" ", 1, &buf, &buflen));
355
356                 /* Server. */
357                 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
358                 break;
359             }
360
361         case ns_t_srv: {
362                 u_int priority, weight, port;
363                 char t[50];
364
365                 if (rdlen < NS_INT16SZ*3)
366                         goto formerr;
367
368                 /* Priority, Weight, Port. */
369                 priority = ns_get16(rdata);  rdata += NS_INT16SZ;
370                 weight   = ns_get16(rdata);  rdata += NS_INT16SZ;
371                 port     = ns_get16(rdata);  rdata += NS_INT16SZ;
372                 len = SPRINTF((t, "%u %u %u ", priority, weight, port));
373                 T(addstr(t, len, &buf, &buflen));
374
375                 /* Server. */
376                 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
377                 break;
378             }
379
380         case ns_t_minfo:
381         case ns_t_rp:
382                 /* Name1. */
383                 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
384                 T(addstr(" ", 1, &buf, &buflen));
385
386                 /* Name2. */
387                 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
388
389                 break;
390
391         case ns_t_wks: {
392                 int n, lcnt;
393
394                 if (rdlen < NS_INT32SZ + 1)
395                         goto formerr;
396
397                 /* Address. */
398                 (void) inet_ntop(AF_INET, rdata, buf, buflen);
399                 addlen(strlen(buf), &buf, &buflen);
400                 rdata += NS_INADDRSZ;
401
402                 /* Protocol. */
403                 len = SPRINTF((tmp, " %u ( ", *rdata));
404                 T(addstr(tmp, len, &buf, &buflen));
405                 rdata += NS_INT8SZ;
406
407                 /* Bit map. */
408                 n = 0;
409                 lcnt = 0;
410                 while (rdata < edata) {
411                         u_int c = *rdata++;
412                         do {
413                                 if (c & 0200) {
414                                         if (lcnt == 0) {
415                                                 T(addstr("\n\t\t\t\t", 5,
416                                                          &buf, &buflen));
417                                                 lcnt = 10;
418                                                 spaced = 0;
419                                         }
420                                         len = SPRINTF((tmp, "%d ", n));
421                                         T(addstr(tmp, len, &buf, &buflen));
422                                         lcnt--;
423                                 }
424                                 c <<= 1;
425                         } while (++n & 07);
426                 }
427                 T(addstr(")", 1, &buf, &buflen));
428
429                 break;
430             }
431
432         case ns_t_key: {
433                 char base64_key[NS_MD5RSA_MAX_BASE64];
434                 u_int keyflags, protocol, algorithm;
435                 const char *leader;
436                 int n;
437
438                 if (rdlen < NS_INT16SZ + NS_INT8SZ + NS_INT8SZ)
439                         goto formerr;
440
441                 /* Key flags, Protocol, Algorithm. */
442                 keyflags = ns_get16(rdata);  rdata += NS_INT16SZ;
443                 protocol = *rdata++;
444                 algorithm = *rdata++;
445                 len = SPRINTF((tmp, "0x%04x %u %u",
446                                keyflags, protocol, algorithm));
447                 T(addstr(tmp, len, &buf, &buflen));
448
449                 /* Public key data. */
450                 len = b64_ntop(rdata, edata - rdata,
451                                base64_key, sizeof base64_key);
452                 if (len < 0)
453                         goto formerr;
454                 if (len > 15) {
455                         T(addstr(" (", 2, &buf, &buflen));
456                         leader = "\n\t\t";
457                         spaced = 0;
458                 } else
459                         leader = " ";
460                 for (n = 0; n < len; n += 48) {
461                         T(addstr(leader, strlen(leader), &buf, &buflen));
462                         T(addstr(base64_key + n, MIN(len - n, 48),
463                                  &buf, &buflen));
464                 }
465                 if (len > 15)
466                         T(addstr(" )", 2, &buf, &buflen));
467
468                 break;
469             }
470
471         case ns_t_sig: {
472                 char base64_key[NS_MD5RSA_MAX_BASE64];
473                 u_int type, algorithm, labels, footprint;
474                 const char *leader;
475                 u_long t;
476                 int n;
477
478                 if (rdlen < 22)
479                         goto formerr;
480
481                 /* Type covered, Algorithm, Label count, Original TTL. */
482                 type = ns_get16(rdata);  rdata += NS_INT16SZ;
483                 algorithm = *rdata++;
484                 labels = *rdata++;
485                 t = ns_get32(rdata);  rdata += NS_INT32SZ;
486                 len = SPRINTF((tmp, " %s %d %lu ",
487                                p_type(type), algorithm, t));
488                 T(addstr(tmp, len, &buf, &buflen));
489                 if (labels != (u_int)dn_count_labels(name))
490                         goto formerr;
491
492                 /* Signature expiry. */
493                 t = ns_get32(rdata);  rdata += NS_INT32SZ;
494                 len = SPRINTF((tmp, "%s ", p_secstodate(t)));
495                 T(addstr(tmp, len, &buf, &buflen));
496
497                 /* Time signed. */
498                 t = ns_get32(rdata);  rdata += NS_INT32SZ;
499                 len = SPRINTF((tmp, "%s ", p_secstodate(t)));
500                 T(addstr(tmp, len, &buf, &buflen));
501
502                 /* Signature Footprint. */
503                 footprint = ns_get16(rdata);  rdata += NS_INT16SZ;
504                 len = SPRINTF((tmp, "%u ", footprint));
505                 T(addstr(tmp, len, &buf, &buflen));
506
507                 /* Signer's name. */
508                 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
509
510                 /* Signature. */
511                 len = b64_ntop(rdata, edata - rdata,
512                                base64_key, sizeof base64_key);
513                 if (len > 15) {
514                         T(addstr(" (", 2, &buf, &buflen));
515                         leader = "\n\t\t";
516                         spaced = 0;
517                 } else
518                         leader = " ";
519                 if (len < 0)
520                         goto formerr;
521                 for (n = 0; n < len; n += 48) {
522                         T(addstr(leader, strlen(leader), &buf, &buflen));
523                         T(addstr(base64_key + n, MIN(len - n, 48),
524                                  &buf, &buflen));
525                 }
526                 if (len > 15)
527                         T(addstr(" )", 2, &buf, &buflen));
528
529                 break;
530             }
531
532         case ns_t_nxt: {
533                 int n, c;
534
535                 /* Next domain name. */
536                 T(addname(msg, msglen, &rdata, origin, &buf, &buflen));
537
538                 /* Type bit map. */
539                 n = edata - rdata;
540                 for (c = 0; c < n*8; c++)
541                         if (NS_NXT_BIT_ISSET(c, rdata)) {
542                                 len = SPRINTF((tmp, " %s", p_type(c)));
543                                 T(addstr(tmp, len, &buf, &buflen));
544                         }
545                 break;
546             }
547
548         default:
549                 comment = "unknown RR type";
550                 goto hexify;
551         }
552         return (buf - obuf);
553  formerr:
554         comment = "RR format error";
555  hexify: {
556         int n, m;
557         char *p;
558
559         len = SPRINTF((tmp, "\\#(\t\t; %s", comment));
560         T(addstr(tmp, len, &buf, &buflen));
561         while (rdata < edata) {
562                 p = tmp;
563                 p += SPRINTF((p, "\n\t"));
564                 spaced = 0;
565                 n = MIN(16, edata - rdata);
566                 for (m = 0; m < n; m++)
567                         p += SPRINTF((p, "%02x ", rdata[m]));
568                 T(addstr(tmp, p - tmp, &buf, &buflen));
569                 if (n < 16) {
570                         T(addstr(")", 1, &buf, &buflen));
571                         T(addtab(p - tmp + 1, 48, spaced, &buf, &buflen));
572                 }
573                 p = tmp;
574                 p += SPRINTF((p, "; "));
575                 for (m = 0; m < n; m++)
576                         *p++ = (isascii(rdata[m]) && isprint(rdata[m]))
577                                 ? rdata[m]
578                                 : '.';
579                 T(addstr(tmp, p - tmp, &buf, &buflen));
580                 rdata += n;
581         }
582         return (buf - obuf);
583     }
584 }
585
586 /* Private. */
587
588 /*
589  * size_t
590  * prune_origin(name, origin)
591  *      Find out if the name is at or under the current origin.
592  * return:
593  *      Number of characters in name before start of origin,
594  *      or length of name if origin does not match.
595  * notes:
596  *      This function should share code with samedomain().
597  */
598 static size_t
599 prune_origin(const char *name, const char *origin) {
600         const char *oname = name;
601
602         while (*name != '\0') {
603                 if (origin != NULL && strcasecmp(name, origin) == 0)
604                         return (name - oname - (name > oname));
605                 while (*name != '\0') {
606                         if (*name == '\\') {
607                                 name++;
608                                 /* XXX need to handle \nnn form. */
609                                 if (*name == '\0')
610                                         break;
611                         } else if (*name == '.') {
612                                 name++;
613                                 break;
614                         }
615                         name++;
616                 }
617         }
618         return (name - oname);
619 }
620
621 /*
622  * int
623  * charstr(rdata, edata, buf, buflen)
624  *      Format a <character-string> into the presentation buffer.
625  * return:
626  *      Number of rdata octets consumed
627  *      0 for protocol format error
628  *      -1 for output buffer error
629  * side effects:
630  *      buffer is advanced on success.
631  */
632 static int
633 charstr(const u_char *rdata, const u_char *edata, char **buf, size_t *buflen) {
634         const u_char *odata = rdata;
635         size_t save_buflen = *buflen;
636         char *save_buf = *buf;
637
638         if (addstr("\"", 1, buf, buflen) < 0)
639                 goto enospc;
640         if (rdata < edata) {
641                 int n = *rdata;
642
643                 if (rdata + 1 + n <= edata) {
644                         rdata++;
645                         while (n-- > 0) {
646                                 if (strchr("\n\"\\", *rdata) != NULL)
647                                         if (addstr("\\", 1, buf, buflen) < 0)
648                                                 goto enospc;
649                                 if (addstr((const char *)rdata, 1,
650                                            buf, buflen) < 0)
651                                         goto enospc;
652                                 rdata++;
653                         }
654                 }
655         }
656         if (addstr("\"", 1, buf, buflen) < 0)
657                 goto enospc;
658         return (rdata - odata);
659  enospc:
660         errno = ENOSPC;
661         *buf = save_buf;
662         *buflen = save_buflen;
663         return (-1);
664 }
665
666 static int
667 addname(const u_char *msg, size_t msglen,
668         const u_char **pp, const char *origin,
669         char **buf, size_t *buflen)
670 {
671         size_t newlen, save_buflen = *buflen;
672         char *save_buf = *buf;
673         int n;
674
675         n = dn_expand(msg, msg + msglen, *pp, *buf, *buflen);
676         if (n < 0)
677                 goto enospc;    /* Guess. */
678         newlen = prune_origin(*buf, origin);
679         if ((origin == NULL || origin[0] == '\0' || (*buf)[newlen] == '\0') &&
680             (newlen == 0 || (*buf)[newlen - 1] != '.')) {
681                 /* No trailing dot. */
682                 if (newlen + 2 > *buflen)
683                         goto enospc;    /* No room for ".\0". */
684                 (*buf)[newlen++] = '.';
685                 (*buf)[newlen] = '\0';
686         }
687         if (newlen == 0) {
688                 /* Use "@" instead of name. */
689                 if (newlen + 2 > *buflen)
690                         goto enospc;        /* No room for "@\0". */
691                 (*buf)[newlen++] = '@';
692                 (*buf)[newlen] = '\0';
693         }
694         *pp += n;
695         addlen(newlen, buf, buflen);
696         **buf = '\0';
697         return (newlen);
698  enospc:
699         errno = ENOSPC;
700         *buf = save_buf;
701         *buflen = save_buflen;
702         return (-1);
703 }
704
705 static void
706 addlen(size_t len, char **buf, size_t *buflen) {
707         assert(len <= *buflen);
708         *buf += len;
709         *buflen -= len;
710 }
711
712 static int
713 addstr(const char *src, size_t len, char **buf, size_t *buflen) {
714         if (len > *buflen) {
715                 errno = ENOSPC;
716                 return (-1);
717         }
718         memcpy(*buf, src, len);
719         addlen(len, buf, buflen);
720         **buf = '\0';
721         return (0);
722 }
723
724 static int
725 addtab(size_t len, size_t target, int spaced, char **buf, size_t *buflen) {
726         size_t save_buflen = *buflen;
727         char *save_buf = *buf;
728         int t;
729
730         if (spaced || len >= target - 1) {
731                 T(addstr("  ", 2, buf, buflen));
732                 spaced = 1;
733         } else {
734                 for (t = (target - len - 1) / 8; t >= 0; t--)
735                         if (addstr("\t", 1, buf, buflen) < 0) {
736                                 *buflen = save_buflen;
737                                 *buf = save_buf;
738                                 return (-1);
739                         }
740                 spaced = 0;
741         }
742         return (spaced);
743 }