]> The Tcpdump Group git mirrors - tcpdump/blob - util-print.c
758aa77331bcae80af9d2a12f3d607c2a8f074e7
[tcpdump] / util-print.c
1 /*
2 * Copyright (c) 1990, 1991, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
16 * written permission.
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 */
21
22 /*
23 * txtproto_print() derived from original code by Hannes Gredler
24 * (hannes@juniper.net):
25 *
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that: (1) source code
28 * distributions retain the above copyright notice and this paragraph
29 * in its entirety, and (2) distributions including binary code include
30 * the above copyright notice and this paragraph in its entirety in
31 * the documentation or other materials provided with the distribution.
32 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND
33 * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT
34 * LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
35 * FOR A PARTICULAR PURPOSE.
36 */
37
38 #ifdef HAVE_CONFIG_H
39 #include "config.h"
40 #endif
41
42 #include <tcpdump-stdinc.h>
43
44 #include <sys/stat.h>
45
46 #ifdef HAVE_FCNTL_H
47 #include <fcntl.h>
48 #endif
49 #include <stdio.h>
50 #include <stdarg.h>
51 #include <stdlib.h>
52 #include <string.h>
53
54 #include "interface.h"
55 #include "ascii_strcasecmp.h"
56
57 int32_t thiszone; /* seconds offset from gmt to local time */
58
59 /*
60 * timestamp display buffer size, the biggest size of both formats is needed
61 * sizeof("0000000000.000000000") > sizeof("00:00:00.000000000")
62 */
63 #define TS_BUF_SIZE sizeof("0000000000.000000000")
64
65 #define TOKBUFSIZE 128
66
67 /*
68 * Print out a null-terminated filename (or other ascii string).
69 * If ep is NULL, assume no truncation check is needed.
70 * Return true if truncated.
71 */
72 int
73 fn_print(netdissect_options *ndo,
74 register const u_char *s, register const u_char *ep)
75 {
76 register int ret;
77 register u_char c;
78
79 ret = 1; /* assume truncated */
80 while (ep == NULL || s < ep) {
81 c = *s++;
82 if (c == '\0') {
83 ret = 0;
84 break;
85 }
86 if (!ND_ISASCII(c)) {
87 c = ND_TOASCII(c);
88 ND_PRINT((ndo, "M-"));
89 }
90 if (!ND_ISPRINT(c)) {
91 c ^= 0x40; /* DEL to ?, others to alpha */
92 ND_PRINT((ndo, "^"));
93 }
94 ND_PRINT((ndo, "%c", c));
95 }
96 return(ret);
97 }
98
99 /*
100 * Print out a counted filename (or other ascii string).
101 * If ep is NULL, assume no truncation check is needed.
102 * Return true if truncated.
103 */
104 int
105 fn_printn(netdissect_options *ndo,
106 register const u_char *s, register u_int n, register const u_char *ep)
107 {
108 register u_char c;
109
110 while (n > 0 && (ep == NULL || s < ep)) {
111 n--;
112 c = *s++;
113 if (!ND_ISASCII(c)) {
114 c = ND_TOASCII(c);
115 ND_PRINT((ndo, "M-"));
116 }
117 if (!ND_ISPRINT(c)) {
118 c ^= 0x40; /* DEL to ?, others to alpha */
119 ND_PRINT((ndo, "^"));
120 }
121 ND_PRINT((ndo, "%c", c));
122 }
123 return (n == 0) ? 0 : 1;
124 }
125
126 /*
127 * Print out a null-padded filename (or other ascii string).
128 * If ep is NULL, assume no truncation check is needed.
129 * Return true if truncated.
130 */
131 int
132 fn_printzp(netdissect_options *ndo,
133 register const u_char *s, register u_int n,
134 register const u_char *ep)
135 {
136 register int ret;
137 register u_char c;
138
139 ret = 1; /* assume truncated */
140 while (n > 0 && (ep == NULL || s < ep)) {
141 n--;
142 c = *s++;
143 if (c == '\0') {
144 ret = 0;
145 break;
146 }
147 if (!ND_ISASCII(c)) {
148 c = ND_TOASCII(c);
149 ND_PRINT((ndo, "M-"));
150 }
151 if (!ND_ISPRINT(c)) {
152 c ^= 0x40; /* DEL to ?, others to alpha */
153 ND_PRINT((ndo, "^"));
154 }
155 ND_PRINT((ndo, "%c", c));
156 }
157 return (n == 0) ? 0 : ret;
158 }
159
160 /*
161 * Format the timestamp
162 */
163 static char *
164 ts_format(netdissect_options *ndo
165 #ifndef HAVE_PCAP_SET_TSTAMP_PRECISION
166 _U_
167 #endif
168 , int sec, int usec, char *buf)
169 {
170 const char *format;
171
172 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
173 switch (ndo->ndo_tstamp_precision) {
174
175 case PCAP_TSTAMP_PRECISION_MICRO:
176 format = "%02d:%02d:%02d.%06u";
177 break;
178
179 case PCAP_TSTAMP_PRECISION_NANO:
180 format = "%02d:%02d:%02d.%09u";
181 break;
182
183 default:
184 format = "%02d:%02d:%02d.{unknown}";
185 break;
186 }
187 #else
188 format = "%02d:%02d:%02d.%06u";
189 #endif
190
191 snprintf(buf, TS_BUF_SIZE, format,
192 sec / 3600, (sec % 3600) / 60, sec % 60, usec);
193
194 return buf;
195 }
196
197 /*
198 * Format the timestamp - Unix timeval style
199 */
200 static char *
201 ts_unix_format(netdissect_options *ndo
202 #ifndef HAVE_PCAP_SET_TSTAMP_PRECISION
203 _U_
204 #endif
205 , int sec, int usec, char *buf)
206 {
207 const char *format;
208
209 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
210 switch (ndo->ndo_tstamp_precision) {
211
212 case PCAP_TSTAMP_PRECISION_MICRO:
213 format = "%u.%06u";
214 break;
215
216 case PCAP_TSTAMP_PRECISION_NANO:
217 format = "%u.%09u";
218 break;
219
220 default:
221 format = "%u.{unknown}";
222 break;
223 }
224 #else
225 format = "%u.%06u";
226 #endif
227
228 snprintf(buf, TS_BUF_SIZE, format,
229 (unsigned)sec, (unsigned)usec);
230
231 return buf;
232 }
233
234 /*
235 * Print the timestamp
236 */
237 void
238 ts_print(netdissect_options *ndo,
239 register const struct timeval *tvp)
240 {
241 register int s;
242 struct tm *tm;
243 time_t Time;
244 static unsigned b_sec;
245 static unsigned b_usec;
246 int d_usec;
247 int d_sec;
248 char buf[TS_BUF_SIZE];
249
250 switch (ndo->ndo_tflag) {
251
252 case 0: /* Default */
253 s = (tvp->tv_sec + thiszone) % 86400;
254 ND_PRINT((ndo, "%s ", ts_format(ndo, s, tvp->tv_usec, buf)));
255 break;
256
257 case 1: /* No time stamp */
258 break;
259
260 case 2: /* Unix timeval style */
261 ND_PRINT((ndo, "%s ", ts_unix_format(ndo,
262 tvp->tv_sec, tvp->tv_usec, buf)));
263 break;
264
265 case 3: /* Microseconds since previous packet */
266 case 5: /* Microseconds since first packet */
267 if (b_sec == 0) {
268 /* init timestamp for first packet */
269 b_usec = tvp->tv_usec;
270 b_sec = tvp->tv_sec;
271 }
272
273 d_usec = tvp->tv_usec - b_usec;
274 d_sec = tvp->tv_sec - b_sec;
275
276 while (d_usec < 0) {
277 d_usec += 1000000;
278 d_sec--;
279 }
280
281 ND_PRINT((ndo, "%s ", ts_format(ndo, d_sec, d_usec, buf)));
282
283 if (ndo->ndo_tflag == 3) { /* set timestamp for last packet */
284 b_sec = tvp->tv_sec;
285 b_usec = tvp->tv_usec;
286 }
287 break;
288
289 case 4: /* Default + Date */
290 s = (tvp->tv_sec + thiszone) % 86400;
291 Time = (tvp->tv_sec + thiszone) - s;
292 tm = gmtime (&Time);
293 if (!tm)
294 ND_PRINT((ndo, "Date fail "));
295 else
296 ND_PRINT((ndo, "%04d-%02d-%02d %s ",
297 tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
298 ts_format(ndo, s, tvp->tv_usec, buf)));
299 break;
300 }
301 }
302
303 /*
304 * Print a relative number of seconds (e.g. hold time, prune timer)
305 * in the form 5m1s. This does no truncation, so 32230861 seconds
306 * is represented as 1y1w1d1h1m1s.
307 */
308 void
309 relts_print(netdissect_options *ndo,
310 int secs)
311 {
312 static const char *lengths[] = {"y", "w", "d", "h", "m", "s"};
313 static const int seconds[] = {31536000, 604800, 86400, 3600, 60, 1};
314 const char **l = lengths;
315 const int *s = seconds;
316
317 if (secs == 0) {
318 ND_PRINT((ndo, "0s"));
319 return;
320 }
321 if (secs < 0) {
322 ND_PRINT((ndo, "-"));
323 secs = -secs;
324 }
325 while (secs > 0) {
326 if (secs >= *s) {
327 ND_PRINT((ndo, "%d%s", secs / *s, *l));
328 secs -= (secs / *s) * *s;
329 }
330 s++;
331 l++;
332 }
333 }
334
335 /*
336 * this is a generic routine for printing unknown data;
337 * we pass on the linefeed plus indentation string to
338 * get a proper output - returns 0 on error
339 */
340
341 int
342 print_unknown_data(netdissect_options *ndo, const u_char *cp,const char *ident,int len)
343 {
344 if (len < 0) {
345 ND_PRINT((ndo,"%sDissector error: print_unknown_data called with negative length",
346 ident));
347 return(0);
348 }
349 if (ndo->ndo_snapend - cp < len)
350 len = ndo->ndo_snapend - cp;
351 if (len < 0) {
352 ND_PRINT((ndo,"%sDissector error: print_unknown_data called with pointer past end of packet",
353 ident));
354 return(0);
355 }
356 hex_print(ndo, ident,cp,len);
357 return(1); /* everything is ok */
358 }
359
360 /*
361 * Convert a token value to a string; use "fmt" if not found.
362 */
363 const char *
364 tok2strbuf(register const struct tok *lp, register const char *fmt,
365 register u_int v, char *buf, size_t bufsize)
366 {
367 if (lp != NULL) {
368 while (lp->s != NULL) {
369 if (lp->v == v)
370 return (lp->s);
371 ++lp;
372 }
373 }
374 if (fmt == NULL)
375 fmt = "#%d";
376
377 (void)snprintf(buf, bufsize, fmt, v);
378 return (const char *)buf;
379 }
380
381 /*
382 * Convert a token value to a string; use "fmt" if not found.
383 */
384 const char *
385 tok2str(register const struct tok *lp, register const char *fmt,
386 register u_int v)
387 {
388 static char buf[4][TOKBUFSIZE];
389 static int idx = 0;
390 char *ret;
391
392 ret = buf[idx];
393 idx = (idx+1) & 3;
394 return tok2strbuf(lp, fmt, v, ret, sizeof(buf[0]));
395 }
396
397 /*
398 * Convert a bit token value to a string; use "fmt" if not found.
399 * this is useful for parsing bitfields, the output strings are seperated
400 * if the s field is positive.
401 */
402 static char *
403 bittok2str_internal(register const struct tok *lp, register const char *fmt,
404 register u_int v, const char *sep)
405 {
406 static char buf[256]; /* our stringbuffer */
407 int buflen=0;
408 register u_int rotbit; /* this is the bit we rotate through all bitpositions */
409 register u_int tokval;
410 const char * sepstr = "";
411
412 while (lp != NULL && lp->s != NULL) {
413 tokval=lp->v; /* load our first value */
414 rotbit=1;
415 while (rotbit != 0) {
416 /*
417 * lets AND the rotating bit with our token value
418 * and see if we have got a match
419 */
420 if (tokval == (v&rotbit)) {
421 /* ok we have found something */
422 buflen+=snprintf(buf+buflen, sizeof(buf)-buflen, "%s%s",
423 sepstr, lp->s);
424 sepstr = sep;
425 break;
426 }
427 rotbit=rotbit<<1; /* no match - lets shift and try again */
428 }
429 lp++;
430 }
431
432 if (buflen == 0)
433 /* bummer - lets print the "unknown" message as advised in the fmt string if we got one */
434 (void)snprintf(buf, sizeof(buf), fmt == NULL ? "#%08x" : fmt, v);
435 return (buf);
436 }
437
438 /*
439 * Convert a bit token value to a string; use "fmt" if not found.
440 * this is useful for parsing bitfields, the output strings are not seperated.
441 */
442 char *
443 bittok2str_nosep(register const struct tok *lp, register const char *fmt,
444 register u_int v)
445 {
446 return (bittok2str_internal(lp, fmt, v, ""));
447 }
448
449 /*
450 * Convert a bit token value to a string; use "fmt" if not found.
451 * this is useful for parsing bitfields, the output strings are comma seperated.
452 */
453 char *
454 bittok2str(register const struct tok *lp, register const char *fmt,
455 register u_int v)
456 {
457 return (bittok2str_internal(lp, fmt, v, ", "));
458 }
459
460 /*
461 * Convert a value to a string using an array; the macro
462 * tok2strary() in <interface.h> is the public interface to
463 * this function and ensures that the second argument is
464 * correct for bounds-checking.
465 */
466 const char *
467 tok2strary_internal(register const char **lp, int n, register const char *fmt,
468 register int v)
469 {
470 static char buf[TOKBUFSIZE];
471
472 if (v >= 0 && v < n && lp[v] != NULL)
473 return lp[v];
474 if (fmt == NULL)
475 fmt = "#%d";
476 (void)snprintf(buf, sizeof(buf), fmt, v);
477 return (buf);
478 }
479
480 /*
481 * Convert a 32-bit netmask to prefixlen if possible
482 * the function returns the prefix-len; if plen == -1
483 * then conversion was not possible;
484 */
485
486 int
487 mask2plen(uint32_t mask)
488 {
489 uint32_t bitmasks[33] = {
490 0x00000000,
491 0x80000000, 0xc0000000, 0xe0000000, 0xf0000000,
492 0xf8000000, 0xfc000000, 0xfe000000, 0xff000000,
493 0xff800000, 0xffc00000, 0xffe00000, 0xfff00000,
494 0xfff80000, 0xfffc0000, 0xfffe0000, 0xffff0000,
495 0xffff8000, 0xffffc000, 0xffffe000, 0xfffff000,
496 0xfffff800, 0xfffffc00, 0xfffffe00, 0xffffff00,
497 0xffffff80, 0xffffffc0, 0xffffffe0, 0xfffffff0,
498 0xfffffff8, 0xfffffffc, 0xfffffffe, 0xffffffff
499 };
500 int prefix_len = 32;
501
502 /* let's see if we can transform the mask into a prefixlen */
503 while (prefix_len >= 0) {
504 if (bitmasks[prefix_len] == mask)
505 break;
506 prefix_len--;
507 }
508 return (prefix_len);
509 }
510
511 #ifdef INET6
512 int
513 mask62plen(const u_char *mask)
514 {
515 u_char bitmasks[9] = {
516 0x00,
517 0x80, 0xc0, 0xe0, 0xf0,
518 0xf8, 0xfc, 0xfe, 0xff
519 };
520 int byte;
521 int cidr_len = 0;
522
523 for (byte = 0; byte < 16; byte++) {
524 u_int bits;
525
526 for (bits = 0; bits < (sizeof (bitmasks) / sizeof (bitmasks[0])); bits++) {
527 if (mask[byte] == bitmasks[bits]) {
528 cidr_len += bits;
529 break;
530 }
531 }
532
533 if (mask[byte] != 0xff)
534 break;
535 }
536 return (cidr_len);
537 }
538 #endif /* INET6 */
539
540 /*
541 * Routine to print out information for text-based protocols such as FTP,
542 * HTTP, SMTP, RTSP, SIP, ....
543 */
544 #define MAX_TOKEN 128
545
546 /*
547 * Fetch a token from a packet, starting at the specified index,
548 * and return the length of the token.
549 *
550 * Returns 0 on error; yes, this is indistinguishable from an empty
551 * token, but an "empty token" isn't a valid token - it just means
552 * either a space character at the beginning of the line (this
553 * includes a blank line) or no more tokens remaining on the line.
554 */
555 static int
556 fetch_token(netdissect_options *ndo, const u_char *pptr, u_int idx, u_int len,
557 u_char *tbuf, size_t tbuflen)
558 {
559 size_t toklen = 0;
560
561 for (; idx < len; idx++) {
562 if (!ND_TTEST(*(pptr + idx))) {
563 /* ran past end of captured data */
564 return (0);
565 }
566 if (!isascii(*(pptr + idx))) {
567 /* not an ASCII character */
568 return (0);
569 }
570 if (isspace(*(pptr + idx))) {
571 /* end of token */
572 break;
573 }
574 if (!isprint(*(pptr + idx))) {
575 /* not part of a command token or response code */
576 return (0);
577 }
578 if (toklen + 2 > tbuflen) {
579 /* no room for this character and terminating '\0' */
580 return (0);
581 }
582 tbuf[toklen] = *(pptr + idx);
583 toklen++;
584 }
585 if (toklen == 0) {
586 /* no token */
587 return (0);
588 }
589 tbuf[toklen] = '\0';
590
591 /*
592 * Skip past any white space after the token, until we see
593 * an end-of-line (CR or LF).
594 */
595 for (; idx < len; idx++) {
596 if (!ND_TTEST(*(pptr + idx))) {
597 /* ran past end of captured data */
598 break;
599 }
600 if (*(pptr + idx) == '\r' || *(pptr + idx) == '\n') {
601 /* end of line */
602 break;
603 }
604 if (!isascii(*(pptr + idx)) || !isprint(*(pptr + idx))) {
605 /* not a printable ASCII character */
606 break;
607 }
608 if (!isspace(*(pptr + idx))) {
609 /* beginning of next token */
610 break;
611 }
612 }
613 return (idx);
614 }
615
616 /*
617 * Scan a buffer looking for a line ending - LF or CR-LF.
618 * Return the index of the character after the line ending or 0 if
619 * we encounter a non-ASCII or non-printable character or don't find
620 * the line ending.
621 */
622 static u_int
623 print_txt_line(netdissect_options *ndo, const char *protoname,
624 const char *prefix, const u_char *pptr, u_int idx, u_int len)
625 {
626 u_int startidx;
627 u_int linelen;
628
629 startidx = idx;
630 while (idx < len) {
631 ND_TCHECK(*(pptr+idx));
632 if (*(pptr+idx) == '\n') {
633 /*
634 * LF without CR; end of line.
635 * Skip the LF and print the line, with the
636 * exception of the LF.
637 */
638 linelen = idx - startidx;
639 idx++;
640 goto print;
641 } else if (*(pptr+idx) == '\r') {
642 /* CR - any LF? */
643 if ((idx+1) >= len) {
644 /* not in this packet */
645 return (0);
646 }
647 ND_TCHECK(*(pptr+idx+1));
648 if (*(pptr+idx+1) == '\n') {
649 /*
650 * CR-LF; end of line.
651 * Skip the CR-LF and print the line, with
652 * the exception of the CR-LF.
653 */
654 linelen = idx - startidx;
655 idx += 2;
656 goto print;
657 }
658
659 /*
660 * CR followed by something else; treat this
661 * as if it were binary data, and don't print
662 * it.
663 */
664 return (0);
665 } else if (!isascii(*(pptr+idx)) ||
666 (!isprint(*(pptr+idx)) && *(pptr+idx) != '\t')) {
667 /*
668 * Not a printable ASCII character and not a tab;
669 * treat this as if it were binary data, and
670 * don't print it.
671 */
672 return (0);
673 }
674 idx++;
675 }
676
677 /*
678 * All printable ASCII, but no line ending after that point
679 * in the buffer; treat this as if it were truncated.
680 */
681 trunc:
682 linelen = idx - startidx;
683 ND_PRINT((ndo, "%s%.*s[!%s]", prefix, (int)linelen, pptr + startidx,
684 protoname));
685 return (0);
686
687 print:
688 ND_PRINT((ndo, "%s%.*s", prefix, (int)linelen, pptr + startidx));
689 return (idx);
690 }
691
692 void
693 txtproto_print(netdissect_options *ndo, const u_char *pptr, u_int len,
694 const char *protoname, const char **cmds, u_int flags)
695 {
696 u_int idx, eol;
697 u_char token[MAX_TOKEN+1];
698 const char *cmd;
699 int is_reqresp = 0;
700 const char *pnp;
701
702 if (cmds != NULL) {
703 /*
704 * This protocol has more than just request and
705 * response lines; see whether this looks like a
706 * request or response.
707 */
708 idx = fetch_token(ndo, pptr, 0, len, token, sizeof(token));
709 if (idx != 0) {
710 /* Is this a valid request name? */
711 while ((cmd = *cmds++) != NULL) {
712 if (ascii_strcasecmp((const char *)token, cmd) == 0) {
713 /* Yes. */
714 is_reqresp = 1;
715 break;
716 }
717 }
718
719 /*
720 * No - is this a valid response code (3 digits)?
721 *
722 * Is this token the response code, or is the next
723 * token the response code?
724 */
725 if (flags & RESP_CODE_SECOND_TOKEN) {
726 /*
727 * Next token - get it.
728 */
729 idx = fetch_token(ndo, pptr, idx, len, token,
730 sizeof(token));
731 }
732 if (idx != 0) {
733 if (isdigit(token[0]) && isdigit(token[1]) &&
734 isdigit(token[2]) && token[3] == '\0') {
735 /* Yes. */
736 is_reqresp = 1;
737 }
738 }
739 }
740 } else {
741 /*
742 * This protocol has only request and response lines
743 * (e.g., FTP, where all the data goes over a
744 * different connection); assume the payload is
745 * a request or response.
746 */
747 is_reqresp = 1;
748 }
749
750 /* Capitalize the protocol name */
751 for (pnp = protoname; *pnp != '\0'; pnp++)
752 ND_PRINT((ndo, "%c", toupper(*pnp)));
753
754 if (is_reqresp) {
755 /*
756 * In non-verbose mode, just print the protocol, followed
757 * by the first line as the request or response info.
758 *
759 * In verbose mode, print lines as text until we run out
760 * of characters or see something that's not a
761 * printable-ASCII line.
762 */
763 if (ndo->ndo_vflag) {
764 /*
765 * We're going to print all the text lines in the
766 * request or response; just print the length
767 * on the first line of the output.
768 */
769 ND_PRINT((ndo, ", length: %u", len));
770 for (idx = 0;
771 idx < len && (eol = print_txt_line(ndo, protoname, "\n\t", pptr, idx, len)) != 0;
772 idx = eol)
773 ;
774 } else {
775 /*
776 * Just print the first text line.
777 */
778 print_txt_line(ndo, protoname, ": ", pptr, 0, len);
779 }
780 }
781 }
782
783 void
784 safeputs(netdissect_options *ndo,
785 const u_char *s, const u_int maxlen)
786 {
787 u_int idx = 0;
788
789 while (*s && idx < maxlen) {
790 safeputchar(ndo, *s);
791 idx++;
792 s++;
793 }
794 }
795
796 void
797 safeputchar(netdissect_options *ndo,
798 const u_char c)
799 {
800 ND_PRINT((ndo, (c < 0x80 && ND_ISPRINT(c)) ? "%c" : "\\0x%02x", c));
801 }
802
803 #ifdef LBL_ALIGN
804 /*
805 * Some compilers try to optimize memcpy(), using the alignment constraint
806 * on the argument pointer type. by using this function, we try to avoid the
807 * optimization.
808 */
809 void
810 unaligned_memcpy(void *p, const void *q, size_t l)
811 {
812 memcpy(p, q, l);
813 }
814
815 /* As with memcpy(), so with memcmp(). */
816 int
817 unaligned_memcmp(const void *p, const void *q, size_t l)
818 {
819 return (memcmp(p, q, l));
820 }
821 #endif
822