]> andersk Git - gssapi-openssh.git/blame - openssh/misc.c
fix for AIX build problems from
[gssapi-openssh.git] / openssh / misc.c
CommitLineData
30460aeb 1/* $OpenBSD: misc.c,v 1.64 2006/08/03 03:34:42 deraadt Exp $ */
3c0ef626 2/*
3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
30460aeb 4 * Copyright (c) 2005,2006 Damien Miller. All rights reserved.
3c0ef626 5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "includes.h"
08822d99 28
30460aeb 29#include <sys/types.h>
30#include <sys/ioctl.h>
31#include <sys/socket.h>
32#include <sys/param.h>
33
34#include <stdarg.h>
35#include <stdio.h>
36#include <stdlib.h>
37#include <string.h>
38#include <unistd.h>
39
40#include <netinet/in.h>
41#include <netinet/tcp.h>
42
43#include <errno.h>
44#include <fcntl.h>
45#ifdef HAVE_PATHS_H
46# include <paths.h>
47#include <pwd.h>
48#endif
08822d99 49#ifdef SSH_TUN_OPENBSD
50#include <net/if.h>
51#endif
3c0ef626 52
30460aeb 53#include "xmalloc.h"
3c0ef626 54#include "misc.h"
55#include "log.h"
30460aeb 56#include "ssh.h"
3c0ef626 57
58/* remove newline at end of string */
59char *
60chop(char *s)
61{
62 char *t = s;
63 while (*t) {
75be3237 64 if (*t == '\n' || *t == '\r') {
3c0ef626 65 *t = '\0';
66 return s;
67 }
68 t++;
69 }
70 return s;
71
72}
73
74/* set/unset filedescriptor to non-blocking */
7e82606e 75int
3c0ef626 76set_nonblock(int fd)
77{
78 int val;
79
80 val = fcntl(fd, F_GETFL, 0);
81 if (val < 0) {
82 error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno));
7e82606e 83 return (-1);
3c0ef626 84 }
85 if (val & O_NONBLOCK) {
7e82606e 86 debug3("fd %d is O_NONBLOCK", fd);
87 return (0);
3c0ef626 88 }
7cac2b65 89 debug2("fd %d setting O_NONBLOCK", fd);
3c0ef626 90 val |= O_NONBLOCK;
7e82606e 91 if (fcntl(fd, F_SETFL, val) == -1) {
92 debug("fcntl(%d, F_SETFL, O_NONBLOCK): %s", fd,
93 strerror(errno));
94 return (-1);
95 }
96 return (0);
3c0ef626 97}
98
7e82606e 99int
3c0ef626 100unset_nonblock(int fd)
101{
102 int val;
103
104 val = fcntl(fd, F_GETFL, 0);
105 if (val < 0) {
106 error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno));
7e82606e 107 return (-1);
3c0ef626 108 }
109 if (!(val & O_NONBLOCK)) {
7e82606e 110 debug3("fd %d is not O_NONBLOCK", fd);
111 return (0);
3c0ef626 112 }
113 debug("fd %d clearing O_NONBLOCK", fd);
114 val &= ~O_NONBLOCK;
7e82606e 115 if (fcntl(fd, F_SETFL, val) == -1) {
116 debug("fcntl(%d, F_SETFL, ~O_NONBLOCK): %s",
75be3237 117 fd, strerror(errno));
7e82606e 118 return (-1);
119 }
120 return (0);
75be3237 121}
122
123/* disable nagle on socket */
124void
125set_nodelay(int fd)
126{
127 int opt;
128 socklen_t optlen;
129
130 optlen = sizeof opt;
131 if (getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, &optlen) == -1) {
540d72c3 132 debug("getsockopt TCP_NODELAY: %.100s", strerror(errno));
75be3237 133 return;
134 }
135 if (opt == 1) {
136 debug2("fd %d is TCP_NODELAY", fd);
137 return;
138 }
139 opt = 1;
140 debug2("fd %d setting TCP_NODELAY", fd);
141 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1)
142 error("setsockopt TCP_NODELAY: %.100s", strerror(errno));
3c0ef626 143}
144
145/* Characters considered whitespace in strsep calls. */
146#define WHITESPACE " \t\r\n"
30460aeb 147#define QUOTE "\""
3c0ef626 148
75be3237 149/* Characters considered as quotations. */
150#define QUOTES "'\""
151
3c0ef626 152/* return next token in configuration line */
153char *
154strdelim(char **s)
155{
75be3237 156 char *old, *p, *q;
3c0ef626 157 int wspace = 0;
158
159 if (*s == NULL)
160 return NULL;
161
162 old = *s;
163
75be3237 164 if ((q=strchr(QUOTES, (int) *old)) && *q)
165 {
166 /* find next quote character, point old to start of quoted
167 * string */
168 for (p = ++old;*p && *p!=*q; p++)
169 ;
170
171 /* find start of next token */
172 *s = (*p) ? p + strspn(p + 1, WHITESPACE) + 1 : NULL;
173
174 /* terminate 'old' token */
175 *p = '\0';
176 return (old);
177 }
178
30460aeb 179 *s = strpbrk(*s, WHITESPACE QUOTE "=");
3c0ef626 180 if (*s == NULL)
181 return (old);
182
30460aeb 183 if (*s[0] == '\"') {
184 memmove(*s, *s + 1, strlen(*s)); /* move nul too */
185 /* Find matching quote */
186 if ((*s = strpbrk(*s, QUOTE)) == NULL) {
187 return (NULL); /* no matching quote */
188 } else {
189 *s[0] = '\0';
190 return (old);
191 }
192 }
193
3c0ef626 194 /* Allow only one '=' to be skipped */
195 if (*s[0] == '=')
196 wspace = 1;
197 *s[0] = '\0';
198
30460aeb 199 /* Skip any extra whitespace after first token */
3c0ef626 200 *s += strspn(*s + 1, WHITESPACE) + 1;
201 if (*s[0] == '=' && !wspace)
202 *s += strspn(*s + 1, WHITESPACE) + 1;
203
204 return (old);
205}
206
207struct passwd *
208pwcopy(struct passwd *pw)
209{
30460aeb 210 struct passwd *copy = xcalloc(1, sizeof(*copy));
3c0ef626 211
3c0ef626 212 copy->pw_name = xstrdup(pw->pw_name);
213 copy->pw_passwd = xstrdup(pw->pw_passwd);
214 copy->pw_gecos = xstrdup(pw->pw_gecos);
215 copy->pw_uid = pw->pw_uid;
216 copy->pw_gid = pw->pw_gid;
217#ifdef HAVE_PW_EXPIRE_IN_PASSWD
218 copy->pw_expire = pw->pw_expire;
219#endif
220#ifdef HAVE_PW_CHANGE_IN_PASSWD
221 copy->pw_change = pw->pw_change;
222#endif
223#ifdef HAVE_PW_CLASS_IN_PASSWD
224 copy->pw_class = xstrdup(pw->pw_class);
225#endif
226 copy->pw_dir = xstrdup(pw->pw_dir);
227 copy->pw_shell = xstrdup(pw->pw_shell);
228 return copy;
229}
230
231/*
232 * Convert ASCII string to TCP/IP port number.
233 * Port must be >0 and <=65535.
234 * Return 0 if invalid.
235 */
236int
237a2port(const char *s)
238{
239 long port;
240 char *endp;
241
242 errno = 0;
243 port = strtol(s, &endp, 0);
244 if (s == endp || *endp != '\0' ||
245 (errno == ERANGE && (port == LONG_MIN || port == LONG_MAX)) ||
246 port <= 0 || port > 65535)
247 return 0;
248
249 return port;
250}
251
08822d99 252int
253a2tun(const char *s, int *remote)
254{
255 const char *errstr = NULL;
256 char *sp, *ep;
257 int tun;
258
259 if (remote != NULL) {
260 *remote = SSH_TUNID_ANY;
261 sp = xstrdup(s);
262 if ((ep = strchr(sp, ':')) == NULL) {
263 xfree(sp);
264 return (a2tun(s, NULL));
265 }
266 ep[0] = '\0'; ep++;
267 *remote = a2tun(ep, NULL);
268 tun = a2tun(sp, NULL);
269 xfree(sp);
270 return (*remote == SSH_TUNID_ERR ? *remote : tun);
271 }
272
273 if (strcasecmp(s, "any") == 0)
274 return (SSH_TUNID_ANY);
275
276 tun = strtonum(s, 0, SSH_TUNID_MAX, &errstr);
277 if (errstr != NULL)
278 return (SSH_TUNID_ERR);
279
280 return (tun);
281}
282
3c0ef626 283#define SECONDS 1
284#define MINUTES (SECONDS * 60)
285#define HOURS (MINUTES * 60)
286#define DAYS (HOURS * 24)
287#define WEEKS (DAYS * 7)
288
289/*
290 * Convert a time string into seconds; format is
291 * a sequence of:
292 * time[qualifier]
293 *
294 * Valid time qualifiers are:
295 * <none> seconds
296 * s|S seconds
297 * m|M minutes
298 * h|H hours
299 * d|D days
300 * w|W weeks
301 *
302 * Examples:
303 * 90m 90 minutes
304 * 1h30m 90 minutes
305 * 2d 2 days
306 * 1w 1 week
307 *
308 * Return -1 if time string is invalid.
309 */
310long
311convtime(const char *s)
312{
313 long total, secs;
314 const char *p;
315 char *endp;
316
317 errno = 0;
318 total = 0;
319 p = s;
320
321 if (p == NULL || *p == '\0')
322 return -1;
323
324 while (*p) {
325 secs = strtol(p, &endp, 10);
326 if (p == endp ||
327 (errno == ERANGE && (secs == LONG_MIN || secs == LONG_MAX)) ||
328 secs < 0)
329 return -1;
330
331 switch (*endp++) {
332 case '\0':
333 endp--;
30460aeb 334 break;
3c0ef626 335 case 's':
336 case 'S':
337 break;
338 case 'm':
339 case 'M':
340 secs *= MINUTES;
341 break;
342 case 'h':
343 case 'H':
344 secs *= HOURS;
345 break;
346 case 'd':
347 case 'D':
348 secs *= DAYS;
349 break;
350 case 'w':
351 case 'W':
352 secs *= WEEKS;
353 break;
354 default:
355 return -1;
356 }
357 total += secs;
358 if (total < 0)
359 return -1;
360 p = endp;
361 }
362
363 return total;
364}
365
30460aeb 366/*
367 * Returns a standardized host+port identifier string.
368 * Caller must free returned string.
369 */
370char *
371put_host_port(const char *host, u_short port)
372{
373 char *hoststr;
374
375 if (port == 0 || port == SSH_DEFAULT_PORT)
376 return(xstrdup(host));
377 if (asprintf(&hoststr, "[%s]:%d", host, (int)port) < 0)
378 fatal("put_host_port: asprintf: %s", strerror(errno));
379 debug3("put_host_port: %s", hoststr);
380 return hoststr;
381}
382
dfddba3d 383/*
384 * Search for next delimiter between hostnames/addresses and ports.
385 * Argument may be modified (for termination).
386 * Returns *cp if parsing succeeds.
387 * *cp is set to the start of the next delimiter, if one was found.
388 * If this is the last field, *cp is set to NULL.
389 */
390char *
391hpdelim(char **cp)
392{
393 char *s, *old;
394
395 if (cp == NULL || *cp == NULL)
396 return NULL;
397
398 old = s = *cp;
399 if (*s == '[') {
400 if ((s = strchr(s, ']')) == NULL)
401 return NULL;
402 else
403 s++;
404 } else if ((s = strpbrk(s, ":/")) == NULL)
405 s = *cp + strlen(*cp); /* skip to end (see first case below) */
406
407 switch (*s) {
408 case '\0':
409 *cp = NULL; /* no more fields*/
410 break;
8b32eddc 411
dfddba3d 412 case ':':
413 case '/':
414 *s = '\0'; /* terminate */
415 *cp = s + 1;
416 break;
8b32eddc 417
dfddba3d 418 default:
419 return NULL;
420 }
421
422 return old;
423}
424
3c0ef626 425char *
426cleanhostname(char *host)
427{
428 if (*host == '[' && host[strlen(host) - 1] == ']') {
429 host[strlen(host) - 1] = '\0';
430 return (host + 1);
431 } else
432 return host;
433}
434
435char *
436colon(char *cp)
437{
438 int flag = 0;
439
440 if (*cp == ':') /* Leading colon is part of file name. */
441 return (0);
442 if (*cp == '[')
443 flag = 1;
444
445 for (; *cp; ++cp) {
446 if (*cp == '@' && *(cp+1) == '[')
447 flag = 1;
448 if (*cp == ']' && *(cp+1) == ':' && flag)
449 return (cp+1);
450 if (*cp == ':' && !flag)
451 return (cp);
452 if (*cp == '/')
453 return (0);
454 }
455 return (0);
456}
457
458/* function to assist building execv() arguments */
459void
460addargs(arglist *args, char *fmt, ...)
461{
462 va_list ap;
08822d99 463 char *cp;
7e82606e 464 u_int nalloc;
08822d99 465 int r;
3c0ef626 466
467 va_start(ap, fmt);
08822d99 468 r = vasprintf(&cp, fmt, ap);
3c0ef626 469 va_end(ap);
08822d99 470 if (r == -1)
471 fatal("addargs: argument too long");
3c0ef626 472
29d88157 473 nalloc = args->nalloc;
3c0ef626 474 if (args->list == NULL) {
29d88157 475 nalloc = 32;
3c0ef626 476 args->num = 0;
29d88157 477 } else if (args->num+2 >= nalloc)
478 nalloc *= 2;
3c0ef626 479
30460aeb 480 args->list = xrealloc(args->list, nalloc, sizeof(char *));
29d88157 481 args->nalloc = nalloc;
08822d99 482 args->list[args->num++] = cp;
3c0ef626 483 args->list[args->num] = NULL;
484}
dfddba3d 485
08822d99 486void
487replacearg(arglist *args, u_int which, char *fmt, ...)
488{
489 va_list ap;
490 char *cp;
491 int r;
492
493 va_start(ap, fmt);
494 r = vasprintf(&cp, fmt, ap);
495 va_end(ap);
496 if (r == -1)
497 fatal("replacearg: argument too long");
498
499 if (which >= args->num)
500 fatal("replacearg: tried to replace invalid arg %d >= %d",
501 which, args->num);
502 xfree(args->list[which]);
503 args->list[which] = cp;
504}
505
506void
507freeargs(arglist *args)
508{
509 u_int i;
510
511 if (args->list != NULL) {
512 for (i = 0; i < args->num; i++)
513 xfree(args->list[i]);
514 xfree(args->list);
515 args->nalloc = args->num = 0;
516 args->list = NULL;
517 }
518}
519
2ce0bfe4 520/*
521 * Expands tildes in the file name. Returns data allocated by xmalloc.
522 * Warning: this calls getpw*.
523 */
524char *
525tilde_expand_filename(const char *filename, uid_t uid)
526{
527 const char *path;
528 char user[128], ret[MAXPATHLEN];
529 struct passwd *pw;
530 u_int len, slash;
531
532 if (*filename != '~')
533 return (xstrdup(filename));
534 filename++;
535
536 path = strchr(filename, '/');
537 if (path != NULL && path > filename) { /* ~user/path */
538 slash = path - filename;
539 if (slash > sizeof(user) - 1)
540 fatal("tilde_expand_filename: ~username too long");
541 memcpy(user, filename, slash);
542 user[slash] = '\0';
543 if ((pw = getpwnam(user)) == NULL)
544 fatal("tilde_expand_filename: No such user %s", user);
545 } else if ((pw = getpwuid(uid)) == NULL) /* ~/path */
546 fatal("tilde_expand_filename: No such uid %d", uid);
547
548 if (strlcpy(ret, pw->pw_dir, sizeof(ret)) >= sizeof(ret))
549 fatal("tilde_expand_filename: Path too long");
550
551 /* Make sure directory has a trailing '/' */
552 len = strlen(pw->pw_dir);
553 if ((len == 0 || pw->pw_dir[len - 1] != '/') &&
554 strlcat(ret, "/", sizeof(ret)) >= sizeof(ret))
555 fatal("tilde_expand_filename: Path too long");
556
557 /* Skip leading '/' from specified path */
558 if (path != NULL)
559 filename = path + 1;
560 if (strlcat(ret, filename, sizeof(ret)) >= sizeof(ret))
561 fatal("tilde_expand_filename: Path too long");
562
563 return (xstrdup(ret));
564}
565
566/*
567 * Expand a string with a set of %[char] escapes. A number of escapes may be
568 * specified as (char *escape_chars, char *replacement) pairs. The list must
569 * be terminated by a NULL escape_char. Returns replaced string in memory
570 * allocated by xmalloc.
571 */
572char *
573percent_expand(const char *string, ...)
574{
575#define EXPAND_MAX_KEYS 16
576 struct {
577 const char *key;
578 const char *repl;
579 } keys[EXPAND_MAX_KEYS];
580 u_int num_keys, i, j;
581 char buf[4096];
582 va_list ap;
583
584 /* Gather keys */
585 va_start(ap, string);
586 for (num_keys = 0; num_keys < EXPAND_MAX_KEYS; num_keys++) {
587 keys[num_keys].key = va_arg(ap, char *);
588 if (keys[num_keys].key == NULL)
589 break;
590 keys[num_keys].repl = va_arg(ap, char *);
591 if (keys[num_keys].repl == NULL)
592 fatal("percent_expand: NULL replacement");
593 }
594 va_end(ap);
595
596 if (num_keys >= EXPAND_MAX_KEYS)
597 fatal("percent_expand: too many keys");
598
599 /* Expand string */
600 *buf = '\0';
601 for (i = 0; *string != '\0'; string++) {
602 if (*string != '%') {
603 append:
604 buf[i++] = *string;
605 if (i >= sizeof(buf))
606 fatal("percent_expand: string too long");
607 buf[i] = '\0';
608 continue;
609 }
610 string++;
611 if (*string == '%')
612 goto append;
613 for (j = 0; j < num_keys; j++) {
614 if (strchr(keys[j].key, *string) != NULL) {
615 i = strlcat(buf, keys[j].repl, sizeof(buf));
616 if (i >= sizeof(buf))
617 fatal("percent_expand: string too long");
618 break;
619 }
620 }
621 if (j >= num_keys)
622 fatal("percent_expand: unknown key %%%c", *string);
623 }
624 return (xstrdup(buf));
625#undef EXPAND_MAX_KEYS
626}
627
dfddba3d 628/*
629 * Read an entire line from a public key file into a static buffer, discarding
630 * lines that exceed the buffer size. Returns 0 on success, -1 on failure.
631 */
632int
633read_keyfile_line(FILE *f, const char *filename, char *buf, size_t bufsz,
634 u_long *lineno)
635{
636 while (fgets(buf, bufsz, f) != NULL) {
637 (*lineno)++;
638 if (buf[strlen(buf) - 1] == '\n' || feof(f)) {
639 return 0;
640 } else {
641 debug("%s: %s line %lu exceeds size limit", __func__,
642 filename, *lineno);
643 /* discard remainder of line */
8b32eddc 644 while (fgetc(f) != '\n' && !feof(f))
dfddba3d 645 ; /* nothing */
646 }
647 }
648 return -1;
649}
2ce0bfe4 650
08822d99 651int
652tun_open(int tun, int mode)
653{
654#if defined(CUSTOM_SYS_TUN_OPEN)
655 return (sys_tun_open(tun, mode));
656#elif defined(SSH_TUN_OPENBSD)
657 struct ifreq ifr;
658 char name[100];
659 int fd = -1, sock;
660
661 /* Open the tunnel device */
662 if (tun <= SSH_TUNID_MAX) {
663 snprintf(name, sizeof(name), "/dev/tun%d", tun);
664 fd = open(name, O_RDWR);
665 } else if (tun == SSH_TUNID_ANY) {
666 for (tun = 100; tun >= 0; tun--) {
667 snprintf(name, sizeof(name), "/dev/tun%d", tun);
668 if ((fd = open(name, O_RDWR)) >= 0)
669 break;
670 }
671 } else {
672 debug("%s: invalid tunnel %u", __func__, tun);
673 return (-1);
674 }
675
676 if (fd < 0) {
677 debug("%s: %s open failed: %s", __func__, name, strerror(errno));
678 return (-1);
679 }
680
681 debug("%s: %s mode %d fd %d", __func__, name, mode, fd);
682
683 /* Set the tunnel device operation mode */
684 snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "tun%d", tun);
685 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1)
686 goto failed;
687
688 if (ioctl(sock, SIOCGIFFLAGS, &ifr) == -1)
689 goto failed;
690
691 /* Set interface mode */
692 ifr.ifr_flags &= ~IFF_UP;
693 if (mode == SSH_TUNMODE_ETHERNET)
694 ifr.ifr_flags |= IFF_LINK0;
695 else
696 ifr.ifr_flags &= ~IFF_LINK0;
697 if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1)
698 goto failed;
699
700 /* Bring interface up */
701 ifr.ifr_flags |= IFF_UP;
702 if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1)
703 goto failed;
704
705 close(sock);
706 return (fd);
707
708 failed:
709 if (fd >= 0)
710 close(fd);
711 if (sock >= 0)
712 close(sock);
713 debug("%s: failed to set %s mode %d: %s", __func__, name,
714 mode, strerror(errno));
715 return (-1);
716#else
717 error("Tunnel interfaces are not supported on this platform");
718 return (-1);
719#endif
720}
721
722void
723sanitise_stdfd(void)
724{
725 int nullfd, dupfd;
726
727 if ((nullfd = dupfd = open(_PATH_DEVNULL, O_RDWR)) == -1) {
728 fprintf(stderr, "Couldn't open /dev/null: %s", strerror(errno));
729 exit(1);
730 }
731 while (++dupfd <= 2) {
732 /* Only clobber closed fds */
733 if (fcntl(dupfd, F_GETFL, 0) >= 0)
734 continue;
735 if (dup2(nullfd, dupfd) == -1) {
736 fprintf(stderr, "dup2: %s", strerror(errno));
737 exit(1);
738 }
739 }
740 if (nullfd > 2)
741 close(nullfd);
742}
743
2ce0bfe4 744char *
30460aeb 745tohex(const void *vp, size_t l)
2ce0bfe4 746{
30460aeb 747 const u_char *p = (const u_char *)vp;
2ce0bfe4 748 char b[3], *r;
30460aeb 749 size_t i, hl;
750
751 if (l > 65536)
752 return xstrdup("tohex: length > 65536");
2ce0bfe4 753
754 hl = l * 2 + 1;
30460aeb 755 r = xcalloc(1, hl);
2ce0bfe4 756 for (i = 0; i < l; i++) {
30460aeb 757 snprintf(b, sizeof(b), "%02x", p[i]);
2ce0bfe4 758 strlcat(r, b, hl);
759 }
760 return (r);
761}
762
30460aeb 763u_int64_t
764get_u64(const void *vp)
765{
766 const u_char *p = (const u_char *)vp;
767 u_int64_t v;
768
769 v = (u_int64_t)p[0] << 56;
770 v |= (u_int64_t)p[1] << 48;
771 v |= (u_int64_t)p[2] << 40;
772 v |= (u_int64_t)p[3] << 32;
773 v |= (u_int64_t)p[4] << 24;
774 v |= (u_int64_t)p[5] << 16;
775 v |= (u_int64_t)p[6] << 8;
776 v |= (u_int64_t)p[7];
777
778 return (v);
779}
780
781u_int32_t
782get_u32(const void *vp)
783{
784 const u_char *p = (const u_char *)vp;
785 u_int32_t v;
786
787 v = (u_int32_t)p[0] << 24;
788 v |= (u_int32_t)p[1] << 16;
789 v |= (u_int32_t)p[2] << 8;
790 v |= (u_int32_t)p[3];
791
792 return (v);
793}
794
795u_int16_t
796get_u16(const void *vp)
797{
798 const u_char *p = (const u_char *)vp;
799 u_int16_t v;
800
801 v = (u_int16_t)p[0] << 8;
802 v |= (u_int16_t)p[1];
803
804 return (v);
805}
806
807void
808put_u64(void *vp, u_int64_t v)
809{
810 u_char *p = (u_char *)vp;
811
812 p[0] = (u_char)(v >> 56) & 0xff;
813 p[1] = (u_char)(v >> 48) & 0xff;
814 p[2] = (u_char)(v >> 40) & 0xff;
815 p[3] = (u_char)(v >> 32) & 0xff;
816 p[4] = (u_char)(v >> 24) & 0xff;
817 p[5] = (u_char)(v >> 16) & 0xff;
818 p[6] = (u_char)(v >> 8) & 0xff;
819 p[7] = (u_char)v & 0xff;
820}
821
822void
823put_u32(void *vp, u_int32_t v)
824{
825 u_char *p = (u_char *)vp;
826
827 p[0] = (u_char)(v >> 24) & 0xff;
828 p[1] = (u_char)(v >> 16) & 0xff;
829 p[2] = (u_char)(v >> 8) & 0xff;
830 p[3] = (u_char)v & 0xff;
831}
832
833
834void
835put_u16(void *vp, u_int16_t v)
836{
837 u_char *p = (u_char *)vp;
838
839 p[0] = (u_char)(v >> 8) & 0xff;
840 p[1] = (u_char)v & 0xff;
841}
This page took 2.927406 seconds and 5 git commands to generate.