diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/include/gnunet_common.h | 116 | ||||
-rw-r--r-- | src/util/common_endian.c | 53 |
2 files changed, 151 insertions, 18 deletions
diff --git a/src/include/gnunet_common.h b/src/include/gnunet_common.h index fc5cb80d9..880fc1fee 100644 --- a/src/include/gnunet_common.h +++ b/src/include/gnunet_common.h | |||
@@ -655,37 +655,125 @@ GNUNET_error_type_to_string (enum GNUNET_ErrorType kind); | |||
655 | /* ************************* endianess conversion ****************** */ | 655 | /* ************************* endianess conversion ****************** */ |
656 | 656 | ||
657 | /** | 657 | /** |
658 | * Convert unsigned 64-bit integer to host-byte-order. | 658 | * Convert unsigned 64-bit integer to network byte order. |
659 | * @param n the value in network byte order | 659 | * |
660 | * @return the same value in host byte order | 660 | * @param n |
661 | * The value in host byte order. | ||
662 | * | ||
663 | * @return The same value in network byte order. | ||
661 | */ | 664 | */ |
662 | uint64_t | 665 | uint64_t |
663 | GNUNET_ntohll (uint64_t n); | 666 | GNUNET_htonll (uint64_t n); |
667 | |||
664 | 668 | ||
665 | /** | 669 | /** |
666 | * Convert unsigned 64-bit integer to network-byte-order. | 670 | * Convert unsigned 64-bit integer to host byte order. |
667 | * @param n the value in host byte order | 671 | * |
668 | * @return the same value in network byte order | 672 | * @param n |
673 | * The value in network byte order. | ||
674 | * | ||
675 | * @return The same value in host byte order. | ||
669 | */ | 676 | */ |
670 | uint64_t | 677 | uint64_t |
671 | GNUNET_htonll (uint64_t n); | 678 | GNUNET_ntohll (uint64_t n); |
679 | |||
672 | 680 | ||
673 | /** | 681 | /** |
674 | * Convert double to network-byte-order. | 682 | * Convert double to network byte order. |
675 | * @param d the value in network byte order | 683 | * |
676 | * @return the same value in host byte order | 684 | * @param d |
685 | * The value in network byte order. | ||
686 | * | ||
687 | * @return The same value in host byte order. | ||
677 | */ | 688 | */ |
678 | double | 689 | double |
679 | GNUNET_hton_double (double d); | 690 | GNUNET_hton_double (double d); |
680 | 691 | ||
692 | |||
681 | /** | 693 | /** |
682 | * Convert double to host-byte-order | 694 | * Convert double to host byte order |
683 | * @param d the value in network byte order | 695 | * |
684 | * @return the same value in host byte order | 696 | * @param d |
697 | * The value in network byte order. | ||
698 | * | ||
699 | * @return The same value in host byte order. | ||
685 | */ | 700 | */ |
686 | double | 701 | double |
687 | GNUNET_ntoh_double (double d); | 702 | GNUNET_ntoh_double (double d); |
688 | 703 | ||
704 | |||
705 | /** | ||
706 | * Convert signed 64-bit integer to network byte order. | ||
707 | * | ||
708 | * @param n | ||
709 | * The value in host byte order. | ||
710 | * | ||
711 | * @return The same value in network byte order. | ||
712 | */ | ||
713 | uint64_t | ||
714 | GNUNET_htonll_signed (int64_t n); | ||
715 | |||
716 | |||
717 | /** | ||
718 | * Convert signed 64-bit integer to host byte order. | ||
719 | * | ||
720 | * @param n | ||
721 | * The value in network byte order. | ||
722 | * | ||
723 | * @return The same value in host byte order. | ||
724 | */ | ||
725 | int64_t | ||
726 | GNUNET_ntohll_signed (uint64_t n); | ||
727 | |||
728 | |||
729 | /** | ||
730 | * Convert signed 32-bit integer to network byte order. | ||
731 | * | ||
732 | * @param n | ||
733 | * The value in host byte order. | ||
734 | * | ||
735 | * @return The same value in network byte order. | ||
736 | */ | ||
737 | uint32_t | ||
738 | GNUNET_htonl_signed (int32_t n); | ||
739 | |||
740 | |||
741 | /** | ||
742 | * Convert signed 32-bit integer to host byte order. | ||
743 | * | ||
744 | * @param n | ||
745 | * The value in network byte order. | ||
746 | * | ||
747 | * @return The same value in host byte order. | ||
748 | */ | ||
749 | int32_t | ||
750 | GNUNET_ntohl_signed (uint32_t n); | ||
751 | |||
752 | |||
753 | /** | ||
754 | * Convert signed 16-bit integer to network byte order. | ||
755 | * | ||
756 | * @param n | ||
757 | * The value in host byte order. | ||
758 | * | ||
759 | * @return The same value in network byte order. | ||
760 | */ | ||
761 | uint16_t | ||
762 | GNUNET_htons_signed (int16_t n); | ||
763 | |||
764 | |||
765 | /** | ||
766 | * Convert signed 16-bit integer to host byte order. | ||
767 | * | ||
768 | * @param n | ||
769 | * The value in network byte order. | ||
770 | * | ||
771 | * @return The same value in host byte order. | ||
772 | */ | ||
773 | int16_t | ||
774 | GNUNET_ntohs_signed (uint16_t n); | ||
775 | |||
776 | |||
689 | /* ************************* allocation functions ****************** */ | 777 | /* ************************* allocation functions ****************** */ |
690 | 778 | ||
691 | /** | 779 | /** |
diff --git a/src/util/common_endian.c b/src/util/common_endian.c index 218875e0c..4a8a01664 100644 --- a/src/util/common_endian.c +++ b/src/util/common_endian.c | |||
@@ -22,6 +22,7 @@ | |||
22 | * @file util/common_endian.c | 22 | * @file util/common_endian.c |
23 | * @brief endian conversion helpers | 23 | * @brief endian conversion helpers |
24 | * @author Christian Grothoff | 24 | * @author Christian Grothoff |
25 | * @author Gabor X Toth | ||
25 | */ | 26 | */ |
26 | 27 | ||
27 | #include "platform.h" | 28 | #include "platform.h" |
@@ -29,25 +30,27 @@ | |||
29 | 30 | ||
30 | #define LOG(kind,...) GNUNET_log_from (kind, "util",__VA_ARGS__) | 31 | #define LOG(kind,...) GNUNET_log_from (kind, "util",__VA_ARGS__) |
31 | 32 | ||
33 | |||
32 | uint64_t | 34 | uint64_t |
33 | GNUNET_ntohll (uint64_t n) | 35 | GNUNET_htonll (uint64_t n) |
34 | { | 36 | { |
35 | #if __BYTE_ORDER == __BIG_ENDIAN | 37 | #if __BYTE_ORDER == __BIG_ENDIAN |
36 | return n; | 38 | return n; |
37 | #elif __BYTE_ORDER == __LITTLE_ENDIAN | 39 | #elif __BYTE_ORDER == __LITTLE_ENDIAN |
38 | return (((uint64_t) ntohl (n)) << 32) + ntohl (n >> 32); | 40 | return (((uint64_t) htonl (n)) << 32) + htonl (n >> 32); |
39 | #else | 41 | #else |
40 | #error byteorder undefined | 42 | #error byteorder undefined |
41 | #endif | 43 | #endif |
42 | } | 44 | } |
43 | 45 | ||
46 | |||
44 | uint64_t | 47 | uint64_t |
45 | GNUNET_htonll (uint64_t n) | 48 | GNUNET_ntohll (uint64_t n) |
46 | { | 49 | { |
47 | #if __BYTE_ORDER == __BIG_ENDIAN | 50 | #if __BYTE_ORDER == __BIG_ENDIAN |
48 | return n; | 51 | return n; |
49 | #elif __BYTE_ORDER == __LITTLE_ENDIAN | 52 | #elif __BYTE_ORDER == __LITTLE_ENDIAN |
50 | return (((uint64_t) htonl (n)) << 32) + htonl (n >> 32); | 53 | return (((uint64_t) ntohl (n)) << 32) + ntohl (n >> 32); |
51 | #else | 54 | #else |
52 | #error byteorder undefined | 55 | #error byteorder undefined |
53 | #endif | 56 | #endif |
@@ -90,5 +93,47 @@ GNUNET_ntoh_double (double d) | |||
90 | } | 93 | } |
91 | 94 | ||
92 | 95 | ||
96 | uint64_t | ||
97 | GNUNET_htonll_signed (int64_t n) | ||
98 | { | ||
99 | return GNUNET_htonll (n - INT64_MIN); | ||
100 | } | ||
101 | |||
102 | |||
103 | int64_t | ||
104 | GNUNET_ntohll_signed (uint64_t n) | ||
105 | { | ||
106 | return GNUNET_ntohll (n) + INT64_MIN; | ||
107 | } | ||
108 | |||
109 | |||
110 | uint32_t | ||
111 | GNUNET_htonl_signed (int32_t n) | ||
112 | { | ||
113 | return htonl (n - INT32_MIN); | ||
114 | } | ||
115 | |||
116 | |||
117 | int32_t | ||
118 | GNUNET_ntohl_signed (uint32_t n) | ||
119 | { | ||
120 | return ntohl (n) + INT32_MIN; | ||
121 | } | ||
122 | |||
123 | |||
124 | uint16_t | ||
125 | GNUNET_htons_signed (int16_t n) | ||
126 | { | ||
127 | return htons (n - INT16_MIN); | ||
128 | } | ||
129 | |||
130 | |||
131 | int16_t | ||
132 | GNUNET_ntohs_signed (uint16_t n) | ||
133 | { | ||
134 | return ntohs (n) + INT16_MIN; | ||
135 | } | ||
136 | |||
137 | |||
93 | 138 | ||
94 | /* end of common_endian.c */ | 139 | /* end of common_endian.c */ |