summaryrefslogtreecommitdiff
path: root/misc.c
diff options
context:
space:
mode:
authorDamien Miller <djm@mindrot.org>2006-03-31 23:13:02 +1100
committerDamien Miller <djm@mindrot.org>2006-03-31 23:13:02 +1100
commit3f9418893e1254bda3b87e7d3af7029d11b0a6c7 (patch)
tree97662277f300ea1ab8255905b9d5d72a26f7578f /misc.c
parentd79b424e8ad424a44119f327e5ab1f79cd35649c (diff)
- djm@cvs.openbsd.org 2006/03/30 09:58:16
[authfd.c bufaux.c deattack.c gss-serv.c mac.c misc.c misc.h] [monitor_wrap.c msg.c packet.c sftp-client.c sftp-server.c ssh-agent.c] replace {GET,PUT}_XXBIT macros with functionally similar functions, silencing a heap of lint warnings. also allows them to use __bounded__ checking which can't be applied to macros; requested by and feedback from deraadt@
Diffstat (limited to 'misc.c')
-rw-r--r--misc.c93
1 files changed, 88 insertions, 5 deletions
diff --git a/misc.c b/misc.c
index daeb86c82..158d4878f 100644
--- a/misc.c
+++ b/misc.c
@@ -1,7 +1,7 @@
1/* $OpenBSD: misc.c,v 1.51 2006/03/25 13:17:02 djm Exp $ */ 1/* $OpenBSD: misc.c,v 1.52 2006/03/30 09:58:15 djm Exp $ */
2/* 2/*
3 * Copyright (c) 2000 Markus Friedl. All rights reserved. 3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 * Copyright (c) 2005 Damien Miller. All rights reserved. 4 * Copyright (c) 2005,2006 Damien Miller. All rights reserved.
5 * 5 *
6 * Redistribution and use in source and binary forms, with or without 6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions 7 * modification, are permitted provided that the following conditions
@@ -691,17 +691,100 @@ sanitise_stdfd(void)
691} 691}
692 692
693char * 693char *
694tohex(const u_char *d, u_int l) 694tohex(const void *vp, size_t l)
695{ 695{
696 const u_char *p = (const u_char *)vp;
696 char b[3], *r; 697 char b[3], *r;
697 u_int i, hl; 698 size_t i, hl;
699
700 if (l > 65536)
701 return xstrdup("tohex: length > 65536");
698 702
699 hl = l * 2 + 1; 703 hl = l * 2 + 1;
700 r = xcalloc(1, hl); 704 r = xcalloc(1, hl);
701 for (i = 0; i < l; i++) { 705 for (i = 0; i < l; i++) {
702 snprintf(b, sizeof(b), "%02x", d[i]); 706 snprintf(b, sizeof(b), "%02x", p[i]);
703 strlcat(r, b, hl); 707 strlcat(r, b, hl);
704 } 708 }
705 return (r); 709 return (r);
706} 710}
707 711
712u_int64_t
713get_u64(const void *vp)
714{
715 const u_char *p = (const u_char *)vp;
716 u_int64_t v;
717
718 v = (u_int64_t)p[0] << 56;
719 v |= (u_int64_t)p[1] << 48;
720 v |= (u_int64_t)p[2] << 40;
721 v |= (u_int64_t)p[3] << 32;
722 v |= (u_int64_t)p[4] << 24;
723 v |= (u_int64_t)p[5] << 16;
724 v |= (u_int64_t)p[6] << 8;
725 v |= (u_int64_t)p[7];
726
727 return (v);
728}
729
730u_int32_t
731get_u32(const void *vp)
732{
733 const u_char *p = (const u_char *)vp;
734 u_int32_t v;
735
736 v = (u_int32_t)p[0] << 24;
737 v |= (u_int32_t)p[1] << 16;
738 v |= (u_int32_t)p[2] << 8;
739 v |= (u_int32_t)p[3];
740
741 return (v);
742}
743
744u_int16_t
745get_u16(const void *vp)
746{
747 const u_char *p = (const u_char *)vp;
748 u_int16_t v;
749
750 v = (u_int16_t)p[0] << 8;
751 v |= (u_int16_t)p[1];
752
753 return (v);
754}
755
756void
757put_u64(void *vp, u_int64_t v)
758{
759 u_char *p = (u_char *)vp;
760
761 p[0] = (u_char)(v >> 56) & 0xff;
762 p[1] = (u_char)(v >> 48) & 0xff;
763 p[2] = (u_char)(v >> 40) & 0xff;
764 p[3] = (u_char)(v >> 32) & 0xff;
765 p[4] = (u_char)(v >> 24) & 0xff;
766 p[5] = (u_char)(v >> 16) & 0xff;
767 p[6] = (u_char)(v >> 8) & 0xff;
768 p[7] = (u_char)v & 0xff;
769}
770
771void
772put_u32(void *vp, u_int32_t v)
773{
774 u_char *p = (u_char *)vp;
775
776 p[0] = (u_char)(v >> 24) & 0xff;
777 p[1] = (u_char)(v >> 16) & 0xff;
778 p[2] = (u_char)(v >> 8) & 0xff;
779 p[3] = (u_char)v & 0xff;
780}
781
782
783void
784put_u16(void *vp, u_int16_t v)
785{
786 u_char *p = (u_char *)vp;
787
788 p[0] = (u_char)(v >> 8) & 0xff;
789 p[1] = (u_char)v & 0xff;
790}