49 #include "gnsstk_export.h"
53 #include <arpa/nameser_compat.h>
55 #include <sys/machine.h>
63 #define LITTLE_ENDIAN 1
65 #define BYTE_ORDER LITTLE_ENDIAN
67 #error "Platform doesn't support BYTE_ORDER?"
100 inline void buitohs (
const void* p, uint16_t& v,
unsigned pos = 0);
102 inline void buitohl (
const void* p, uint32_t& v,
unsigned pos = 0);
104 inline void buitohll (
const void* p, uint64_t& v,
unsigned pos = 0);
106 inline void buitohss (
const void* p, int16_t& v,
unsigned pos = 0);
108 inline void buitohsl (
const void* p, int32_t& v,
unsigned pos = 0);
110 inline void buitohsll(
const void* p, int64_t& v,
unsigned pos = 0);
112 inline void buitohf (
const void* p,
float& v,
unsigned pos = 0);
114 inline void buitohd (
const void* p,
double& v,
unsigned pos = 0);
122 inline void buntohs (
const void* p, uint16_t& v,
unsigned pos = 0);
124 inline void buntohl (
const void* p, uint32_t& v,
unsigned pos = 0);
126 inline void buntohll (
const void* p, uint64_t& v,
unsigned pos = 0);
128 inline void buntohss (
const void* p, int16_t& v,
unsigned pos = 0);
130 inline void buntohsl (
const void* p, int32_t& v,
unsigned pos = 0);
132 inline void buntohsll(
const void* p, int64_t& v,
unsigned pos = 0);
134 inline void buntohf (
const void* p,
float& v,
unsigned pos = 0);
136 inline void buntohd (
const void* p,
double& v,
unsigned pos = 0);
146 inline void buhtois (
void* p, uint16_t v,
unsigned pos = 0);
148 inline void buhtoil (
void* p, uint32_t v,
unsigned pos = 0);
150 inline void buhtoill (
void* p, uint64_t v,
unsigned pos = 0);
152 inline void buhtoiss (
void* p, int16_t v,
unsigned pos = 0);
154 inline void buhtoisl (
void* p, int32_t v,
unsigned pos = 0);
156 inline void buhtoisll(
void* p, int64_t v,
unsigned pos = 0);
158 inline void buhtoif (
void* p,
float v,
unsigned pos = 0);
160 inline void buhtoid (
void* p,
double v,
unsigned pos = 0);
169 inline void buhtons (
void* p, uint16_t v,
unsigned pos = 0);
171 inline void buhtonl (
void* p, uint32_t v,
unsigned pos = 0);
173 inline void buhtonll (
void* p, uint64_t v,
unsigned pos = 0);
175 inline void buhtonss (
void* p, int16_t v,
unsigned pos = 0);
177 inline void buhtonsl (
void* p, int32_t v,
unsigned pos = 0);
179 inline void buhtonsll(
void* p, int64_t v,
unsigned pos = 0);
181 inline void buhtonf (
void* p,
float v,
unsigned pos = 0);
183 inline void buhtond (
void* p,
double v,
unsigned pos = 0);
193 inline T
decodeVar(
const std::string& str,
194 std::string::size_type
pos );
205 std::string::size_type
pos );
231 inline std::string
encodeVar(
const T& v );
243 inline void encodeVar(
const T& v, std::string& str,
size_t pos=0 );
263 inline void encodeVarLE(
const T& v, std::string& str,
size_t pos=0 );
273 inline unsigned short countBits(uint32_t v);
276 inline unsigned long reflect (
unsigned long crc,
292 CRCParam(
int o,
unsigned long p,
unsigned long i,
unsigned long f,
293 bool d,
bool ri,
bool ro);
338 inline std::string
xorChecksum(
const std::string& str,
unsigned wordSize);
346 static const int S[] = {1, 2, 4, 8, 16};
347 static const uint32_t B[] = {0x55555555, 0x33333333, 0x0F0F0F0F,
348 0x00FF00FF, 0x0000FFFF};
354 c = ((c >> S[0]) & B[0]) + (c & B[0]);
355 c = ((c >> S[1]) & B[1]) + (c & B[1]);
356 c = ((c >> S[2]) & B[2]) + (c & B[2]);
357 c = ((c >> S[3]) & B[3]) + (c & B[3]);
358 c = ((c >> S[4]) & B[4]) + (c & B[4]);
367 std::string::size_type
pos )
370 #if BYTE_ORDER == LITTLE_ENDIAN
371 std::string scopy(str,
pos,
sizeof(T));
372 std::reverse(scopy.begin(), scopy.end());
373 std::memcpy(&rv, scopy.c_str(),
sizeof(T));
375 std::memcpy(&rv, &str[
pos],
sizeof(T));
384 std::string::size_type
pos )
387 #if BYTE_ORDER == BIG_ENDIAN
388 std::string scopy(str,
pos,
sizeof(T));
389 std::reverse(scopy.begin(), scopy.end());
390 std::memcpy(&rv, scopy.c_str(),
sizeof(T));
392 std::memcpy(&rv, &str[
pos],
sizeof(T));
400 T t = gnsstk::BinUtils::decodeVar<T>(str, 0);
401 str.erase( 0,
sizeof(T) );
408 T t = gnsstk::BinUtils::decodeVarLE<T>(str, 0);
409 str.erase( 0,
sizeof(T) );
416 std::string rv((
const char*)&v,
sizeof(v));
417 #if BYTE_ORDER == LITTLE_ENDIAN
418 std::reverse(rv.begin(), rv.end());
427 str.replace(
pos,
sizeof(T),
reinterpret_cast<const char*
>(&v),
429 #if BYTE_ORDER == LITTLE_ENDIAN
430 std::reverse(str.begin()+
pos, str.begin()+
pos+
sizeof(T));
438 std::string rv((
const char*)&v,
sizeof(v));
439 #if BYTE_ORDER == BIG_ENDIAN
440 std::reverse(rv.begin(), rv.end());
449 str.replace(
pos,
sizeof(T),
reinterpret_cast<const char*
>(&v),
451 #if BYTE_ORDER == BIG_ENDIAN
452 std::reverse(str.begin()+
pos, str.begin()+
pos+
sizeof(T));
457 inline unsigned long reflect (
unsigned long crc,
460 unsigned long i, j = 1, crcout = 0;
462 for (i = (
unsigned long)1 << (bitnum - 1); i; i >>= 1)
481 uint32_t i, j, c, bit;
487 ((((uint32_t)1 << (params.
order - 1)) - 1) << 1) | 1;
488 uint32_t crchighbit = (uint32_t)1 << (params.
order - 1);
492 for (i = 0; i < (uint32_t)params.
order; i++)
508 for (i = 0; i < len; i++)
510 c = (uint32_t) *
data++;
516 for (j = 0x80; j; j >>= 1)
518 bit = crc & crchighbit;
531 for (i = 0; i < (uint32_t)params.
order; i++)
533 bit = crc & crchighbit;
551 std::string
xorChecksum(
const std::string& str,
unsigned wordSize)
553 size_t strSize = str.size();
554 std::string rv(wordSize, 0);
556 if (strSize % wordSize != 0)
558 gnsstk::InvalidParameter ip(
"Incomplete word in string.");
562 for (
size_t i = 0; (i + wordSize - 1) < strSize; i += wordSize)
564 for (
size_t j = 0; j < wordSize; j++)
574 inline void buitohs(
const void* p, uint16_t& v,
unsigned pos)
576 const uint8_t *
cp =
static_cast<const uint8_t*
>(p) +
pos;
577 const uint16_t *tp =
reinterpret_cast<const uint16_t*
>(
cp);
578 #if BYTE_ORDER == LITTLE_ENDIAN
581 v = (((*tp & 0x00ff) << 8) |
582 ((*tp & 0xff00) >> 8));
587 inline void buitohl(
const void* p, uint32_t& v,
unsigned pos)
589 const uint8_t *
cp =
static_cast<const uint8_t*
>(p) +
pos;
590 const uint32_t *tp =
reinterpret_cast<const uint32_t*
>(
cp);
591 #if BYTE_ORDER == LITTLE_ENDIAN
594 v = (((*tp & 0x000000ff) << 24) |
595 ((*tp & 0x0000ff00) << 8) |
596 ((*tp & 0x00ff0000) >> 8) |
597 ((*tp & 0xff000000) >> 24));
604 const uint8_t *
cp =
static_cast<const uint8_t*
>(p) +
pos;
605 const uint64_t *tp =
reinterpret_cast<const uint64_t*
>(
cp);
606 #if BYTE_ORDER == LITTLE_ENDIAN
609 v = (((*tp & 0x00000000000000ffULL) << 56) |
610 ((*tp & 0x000000000000ff00ULL) << 40) |
611 ((*tp & 0x0000000000ff0000ULL) << 24) |
612 ((*tp & 0x00000000ff000000ULL) << 8) |
613 ((*tp & 0x000000ff00000000ULL) >> 8) |
614 ((*tp & 0x0000ff0000000000ULL) >> 24) |
615 ((*tp & 0x00ff000000000000ULL) >> 40) |
616 ((*tp & 0xff00000000000000ULL) >> 56));
623 const uint8_t *
cp =
static_cast<const uint8_t*
>(p) +
pos;
624 const int16_t *tp =
reinterpret_cast<const int16_t*
>(
cp);
625 #if BYTE_ORDER == LITTLE_ENDIAN
629 v = (((*tp & 0x00ff) << 8) |
630 ((*tp >> 8) & 0x00ff));
637 const uint8_t *
cp =
static_cast<const uint8_t*
>(p) +
pos;
638 const int32_t *tp =
reinterpret_cast<const int32_t*
>(
cp);
639 #if BYTE_ORDER == LITTLE_ENDIAN
643 v = (((*tp & 0x000000ff) << 24) |
644 ((*tp & 0x0000ff00) << 8) |
645 ((*tp & 0x00ff0000) >> 8) |
646 ((*tp >> 24) & 0x000000ff));
653 const uint8_t *
cp =
static_cast<const uint8_t*
>(p) +
pos;
654 const int64_t *tp =
reinterpret_cast<const int64_t*
>(
cp);
655 #if BYTE_ORDER == LITTLE_ENDIAN
659 v = (((*tp & 0x00000000000000ffULL) << 56) |
660 ((*tp & 0x000000000000ff00ULL) << 40) |
661 ((*tp & 0x0000000000ff0000ULL) << 24) |
662 ((*tp & 0x00000000ff000000ULL) << 8) |
663 ((*tp & 0x000000ff00000000ULL) >> 8) |
664 ((*tp & 0x0000ff0000000000ULL) >> 24) |
665 ((*tp & 0x00ff000000000000ULL) >> 40) |
666 ((*tp >> 56) & 0x00000000000000ffULL));
673 const uint8_t *
cp =
static_cast<const uint8_t*
>(p) +
pos;
674 uint8_t *vp =
reinterpret_cast<uint8_t*
>(&v);
675 #if BYTE_ORDER == LITTLE_ENDIAN
676 std::memcpy(vp,
cp,
sizeof(
float));
688 const uint8_t *
cp =
static_cast<const uint8_t*
>(p) +
pos;
689 uint8_t *vp =
reinterpret_cast<uint8_t*
>(&v);
690 #if BYTE_ORDER == LITTLE_ENDIAN
691 std::memcpy(vp,
cp,
sizeof(
double));
706 inline void buntohs(
const void* p, uint16_t& v,
unsigned pos)
708 const uint8_t *
cp =
static_cast<const uint8_t*
>(p) +
pos;
709 const uint16_t *tp =
reinterpret_cast<const uint16_t*
>(
cp);
710 #if BYTE_ORDER == BIG_ENDIAN
713 v = (((*tp & 0x00ff) << 8) |
714 ((*tp & 0xff00) >> 8));
719 inline void buntohl(
const void* p, uint32_t& v,
unsigned pos)
721 const uint8_t *
cp =
static_cast<const uint8_t*
>(p) +
pos;
722 const uint32_t *tp =
reinterpret_cast<const uint32_t*
>(
cp);
723 #if BYTE_ORDER == BIG_ENDIAN
726 v = (((*tp & 0x000000ff) << 24) |
727 ((*tp & 0x0000ff00) << 8) |
728 ((*tp & 0x00ff0000) >> 8) |
729 ((*tp & 0xff000000) >> 24));
736 const uint8_t *
cp =
static_cast<const uint8_t*
>(p) +
pos;
737 const uint64_t *tp =
reinterpret_cast<const uint64_t*
>(
cp);
738 #if BYTE_ORDER == BIG_ENDIAN
741 v = (((*tp & 0x00000000000000ffULL) << 56) |
742 ((*tp & 0x000000000000ff00ULL) << 40) |
743 ((*tp & 0x0000000000ff0000ULL) << 24) |
744 ((*tp & 0x00000000ff000000ULL) << 8) |
745 ((*tp & 0x000000ff00000000ULL) >> 8) |
746 ((*tp & 0x0000ff0000000000ULL) >> 24) |
747 ((*tp & 0x00ff000000000000ULL) >> 40) |
748 ((*tp & 0xff00000000000000ULL) >> 56));
755 const uint8_t *
cp =
static_cast<const uint8_t*
>(p) +
pos;
756 const int16_t *tp =
reinterpret_cast<const int16_t*
>(
cp);
757 #if BYTE_ORDER == BIG_ENDIAN
761 v = (((*tp & 0x00ff) << 8) |
762 ((*tp >> 8) & 0x00ff));
769 const uint8_t *
cp =
static_cast<const uint8_t*
>(p) +
pos;
770 const int32_t *tp =
reinterpret_cast<const int32_t*
>(
cp);
771 #if BYTE_ORDER == BIG_ENDIAN
775 v = (((*tp & 0x000000ff) << 24) |
776 ((*tp & 0x0000ff00) << 8) |
777 ((*tp & 0x00ff0000) >> 8) |
778 ((*tp >> 24) & 0x000000ff));
785 const uint8_t *
cp =
static_cast<const uint8_t*
>(p) +
pos;
786 const int64_t *tp =
reinterpret_cast<const int64_t*
>(
cp);
787 #if BYTE_ORDER == BIG_ENDIAN
791 v = (((*tp & 0x00000000000000ffULL) << 56) |
792 ((*tp & 0x000000000000ff00ULL) << 40) |
793 ((*tp & 0x0000000000ff0000ULL) << 24) |
794 ((*tp & 0x00000000ff000000ULL) << 8) |
795 ((*tp & 0x000000ff00000000ULL) >> 8) |
796 ((*tp & 0x0000ff0000000000ULL) >> 24) |
797 ((*tp & 0x00ff000000000000ULL) >> 40) |
798 ((*tp >> 56) & 0x00000000000000ffULL));
805 const uint8_t *
cp =
static_cast<const uint8_t*
>(p) +
pos;
806 uint8_t *vp =
reinterpret_cast<uint8_t*
>(&v);
807 #if BYTE_ORDER == BIG_ENDIAN
808 std::memcpy(vp,
cp,
sizeof(
float));
820 const uint8_t *
cp =
static_cast<const uint8_t*
>(p) +
pos;
821 uint8_t *vp =
reinterpret_cast<uint8_t*
>(&v);
822 #if BYTE_ORDER == BIG_ENDIAN
823 std::memcpy(vp,
cp,
sizeof(
double));
840 uint8_t *
cp =
static_cast<uint8_t*
>(p) +
pos;
841 uint16_t *tp =
reinterpret_cast<uint16_t*
>(
cp);
842 #if BYTE_ORDER == LITTLE_ENDIAN
845 *tp = (((v & 0x00ff) << 8) |
846 ((v & 0xff00) >> 8));
853 uint8_t *
cp =
static_cast<uint8_t*
>(p) +
pos;
854 uint32_t *tp =
reinterpret_cast<uint32_t*
>(
cp);
855 #if BYTE_ORDER == LITTLE_ENDIAN
858 *tp = (((v & 0x000000ff) << 24) |
859 ((v & 0x0000ff00) << 8) |
860 ((v & 0x00ff0000) >> 8) |
861 ((v & 0xff000000) >> 24));
868 uint8_t *
cp =
static_cast<uint8_t*
>(p) +
pos;
869 uint64_t *tp =
reinterpret_cast<uint64_t*
>(
cp);
870 #if BYTE_ORDER == LITTLE_ENDIAN
873 *tp = (((v & 0x00000000000000ffULL) << 56) |
874 ((v & 0x000000000000ff00ULL) << 40) |
875 ((v & 0x0000000000ff0000ULL) << 24) |
876 ((v & 0x00000000ff000000ULL) << 8) |
877 ((v & 0x000000ff00000000ULL) >> 8) |
878 ((v & 0x0000ff0000000000ULL) >> 24) |
879 ((v & 0x00ff000000000000ULL) >> 40) |
880 ((v & 0xff00000000000000ULL) >> 56));
887 uint8_t *
cp =
static_cast<uint8_t*
>(p) +
pos;
888 int16_t *tp =
reinterpret_cast<int16_t*
>(
cp);
889 #if BYTE_ORDER == LITTLE_ENDIAN
893 *tp = (((v & 0x00ff) << 8) |
894 ((v >> 8) & 0x00ff));
901 uint8_t *
cp =
static_cast<uint8_t*
>(p) +
pos;
902 int32_t *tp =
reinterpret_cast<int32_t*
>(
cp);
903 #if BYTE_ORDER == LITTLE_ENDIAN
907 *tp = (((v & 0x000000ff) << 24) |
908 ((v & 0x0000ff00) << 8) |
909 ((v & 0x00ff0000) >> 8) |
910 ((v >> 24) & 0x000000ff));
917 uint8_t *
cp =
static_cast<uint8_t*
>(p) +
pos;
918 int64_t *tp =
reinterpret_cast<int64_t*
>(
cp);
919 #if BYTE_ORDER == LITTLE_ENDIAN
923 *tp = (((v & 0x00000000000000ffULL) << 56) |
924 ((v & 0x000000000000ff00ULL) << 40) |
925 ((v & 0x0000000000ff0000ULL) << 24) |
926 ((v & 0x00000000ff000000ULL) << 8) |
927 ((v & 0x000000ff00000000ULL) >> 8) |
928 ((v & 0x0000ff0000000000ULL) >> 24) |
929 ((v & 0x00ff000000000000ULL) >> 40) |
930 ((v >> 56) & 0x00000000000000ffULL));
937 uint8_t *
cp =
static_cast<uint8_t*
>(p) +
pos;
938 uint8_t *vp =
reinterpret_cast<uint8_t*
>(&v);
939 #if BYTE_ORDER == LITTLE_ENDIAN
940 std::memcpy(
cp, vp,
sizeof(
float));
952 uint8_t *
cp =
static_cast<uint8_t*
>(p) +
pos;
953 uint8_t *vp =
reinterpret_cast<uint8_t*
>(&v);
954 #if BYTE_ORDER == LITTLE_ENDIAN
955 std::memcpy(
cp, vp,
sizeof(
double));
972 uint8_t *
cp =
static_cast<uint8_t*
>(p) +
pos;
973 uint16_t *tp =
reinterpret_cast<uint16_t*
>(
cp);
974 #if BYTE_ORDER == BIG_ENDIAN
977 *tp = (((v & 0x00ff) << 8) |
978 ((v & 0xff00) >> 8));
985 uint8_t *
cp =
static_cast<uint8_t*
>(p) +
pos;
986 uint32_t *tp =
reinterpret_cast<uint32_t*
>(
cp);
987 #if BYTE_ORDER == BIG_ENDIAN
990 *tp = (((v & 0x000000ff) << 24) |
991 ((v & 0x0000ff00) << 8) |
992 ((v & 0x00ff0000) >> 8) |
993 ((v & 0xff000000) >> 24));
1000 uint8_t *
cp =
static_cast<uint8_t*
>(p) +
pos;
1001 uint64_t *tp =
reinterpret_cast<uint64_t*
>(
cp);
1002 #if BYTE_ORDER == BIG_ENDIAN
1005 *tp = (((v & 0x00000000000000ffULL) << 56) |
1006 ((v & 0x000000000000ff00ULL) << 40) |
1007 ((v & 0x0000000000ff0000ULL) << 24) |
1008 ((v & 0x00000000ff000000ULL) << 8) |
1009 ((v & 0x000000ff00000000ULL) >> 8) |
1010 ((v & 0x0000ff0000000000ULL) >> 24) |
1011 ((v & 0x00ff000000000000ULL) >> 40) |
1012 ((v & 0xff00000000000000ULL) >> 56));
1019 uint8_t *
cp =
static_cast<uint8_t*
>(p) +
pos;
1020 int16_t *tp =
reinterpret_cast<int16_t*
>(
cp);
1021 #if BYTE_ORDER == BIG_ENDIAN
1025 *tp = (((v & 0x00ff) << 8) |
1026 ((v >> 8) & 0x00ff));
1033 uint8_t *
cp =
static_cast<uint8_t*
>(p) +
pos;
1034 int32_t *tp =
reinterpret_cast<int32_t*
>(
cp);
1035 #if BYTE_ORDER == BIG_ENDIAN
1039 *tp = (((v & 0x000000ff) << 24) |
1040 ((v & 0x0000ff00) << 8) |
1041 ((v & 0x00ff0000) >> 8) |
1042 ((v >> 24) & 0x000000ff));
1049 uint8_t *
cp =
static_cast<uint8_t*
>(p) +
pos;
1050 int64_t *tp =
reinterpret_cast<int64_t*
>(
cp);
1051 #if BYTE_ORDER == BIG_ENDIAN
1055 *tp = (((v & 0x00000000000000ffULL) << 56) |
1056 ((v & 0x000000000000ff00ULL) << 40) |
1057 ((v & 0x0000000000ff0000ULL) << 24) |
1058 ((v & 0x00000000ff000000ULL) << 8) |
1059 ((v & 0x000000ff00000000ULL) >> 8) |
1060 ((v & 0x0000ff0000000000ULL) >> 24) |
1061 ((v & 0x00ff000000000000ULL) >> 40) |
1062 ((v >> 56) & 0x00000000000000ffULL));
1069 uint8_t *
cp =
static_cast<uint8_t*
>(p) +
pos;
1070 uint8_t *vp =
reinterpret_cast<uint8_t*
>(&v);
1071 #if BYTE_ORDER == BIG_ENDIAN
1072 std::memcpy(
cp, vp,
sizeof(
float));
1084 uint8_t *
cp =
static_cast<uint8_t*
>(p) +
pos;
1085 uint8_t *vp =
reinterpret_cast<uint8_t*
>(&v);
1086 #if BYTE_ORDER == BIG_ENDIAN
1087 std::memcpy(
cp, vp,
sizeof(
double));