BinUtils.hpp
Go to the documentation of this file.
1 //==============================================================================
2 //
3 // This file is part of GNSSTk, the ARL:UT GNSS Toolkit.
4 //
5 // The GNSSTk is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published
7 // by the Free Software Foundation; either version 3.0 of the License, or
8 // any later version.
9 //
10 // The GNSSTk is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with GNSSTk; if not, write to the Free Software Foundation,
17 // Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
18 //
19 // This software was developed by Applied Research Laboratories at the
20 // University of Texas at Austin.
21 // Copyright 2004-2022, The Board of Regents of The University of Texas System
22 //
23 //==============================================================================
24 
25 //==============================================================================
26 //
27 // This software was developed by Applied Research Laboratories at the
28 // University of Texas at Austin, under contract to an agency or agencies
29 // within the U.S. Department of Defense. The U.S. Government retains all
30 // rights to use, duplicate, distribute, disclose, or release this software.
31 //
32 // Pursuant to DoD Directive 523024
33 //
34 // DISTRIBUTION STATEMENT A: This software has been approved for public
35 // release, distribution is unlimited.
36 //
37 //==============================================================================
38 
44 #ifndef BINUTILS_HPP
45 #define BINUTILS_HPP
46 
47 #include <cstring>
48 #include <algorithm>
49 #include "gnsstk_export.h"
50 #include "gnsstkplatform.h"
51 
52 #ifdef __sun
53 #include <arpa/nameser_compat.h>
54 #elif defined (_AIX)
55 #include <sys/machine.h>
56 #endif
57 
58 #include "Exception.hpp"
59 #include <memory.h>
60 
61 #ifndef BYTE_ORDER
62 #ifdef _MSC_VER
63 #define LITTLE_ENDIAN 1
64 #define BIG_ENDIAN 0
65 #define BYTE_ORDER LITTLE_ENDIAN
66 #else
67 #error "Platform doesn't support BYTE_ORDER?"
68 #endif
69 #endif
70 
73 namespace gnsstk
74 {
86  namespace BinUtils
87  {
89 
90 
91  // Why so damn many functions when we used to use templates?
92  // Because of optimization, but mostly due to issues of correctness.
93 
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);
115 
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);
137 
138 
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);
161 
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);
184 
192  template <class T>
193  inline T decodeVar( const std::string& str,
194  std::string::size_type pos );
195 
203  template <class T>
204  inline T decodeVarLE( const std::string& str,
205  std::string::size_type pos );
206 
213  template <class T>
214  inline T decodeVar( std::string& str );
215 
222  template <class T>
223  inline T decodeVarLE( std::string& str );
224 
230  template<class T>
231  inline std::string encodeVar( const T& v );
232 
242  template<class T>
243  inline void encodeVar( const T& v, std::string& str, size_t pos=0 );
244 
250  template<class T>
251  inline std::string encodeVarLE( const T& v );
252 
262  template<class T>
263  inline void encodeVarLE( const T& v, std::string& str, size_t pos=0 );
264 
267  NEW_EXCEPTION_CLASS(CRCException, Exception);
268 
273  inline unsigned short countBits(uint32_t v);
274 
276  inline unsigned long reflect (unsigned long crc,
277  int bitnum);
278 
280  class CRCParam
281  {
282  public:
292  CRCParam(int o, unsigned long p, unsigned long i, unsigned long f,
293  bool d, bool ri, bool ro);
294 
295  int order;
296  unsigned long polynom;
297  unsigned long initial;
298  unsigned long final;
299  bool direct;
300  bool refin;
301  bool refout;
302  };
303 
305  GNSSTK_EXPORT extern const CRCParam CRCCCITT;
307  GNSSTK_EXPORT extern const CRCParam CRC16;
309  GNSSTK_EXPORT extern const CRCParam CRC32;
311  GNSSTK_EXPORT extern const CRCParam CRC24Q;
313  GNSSTK_EXPORT extern const CRCParam CRCGLOL3;
314 
324  inline uint32_t computeCRC(const unsigned char *data,
325  unsigned long len,
326  const CRCParam& params);
327 
338  inline std::string xorChecksum(const std::string& str, unsigned wordSize);
339 
341 
342  inline unsigned short countBits(uint32_t v)
343  {
344  // Stolen from http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
345  uint32_t c; // store the total here
346  static const int S[] = {1, 2, 4, 8, 16}; // Magic Binary Numbers
347  static const uint32_t B[] = {0x55555555, 0x33333333, 0x0F0F0F0F,
348  0x00FF00FF, 0x0000FFFF};
349 
350  // ...and if we were to turn this into a loop, it would
351  // totally defeat the purpose. The point here is to be
352  // FAST.
353  c = v;
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]);
359 
360  return c;
361  }
362 
363 
364 
365  template <class T>
366  inline T decodeVar( const std::string& str,
367  std::string::size_type pos )
368  {
369  T rv;
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));
374 #else
375  std::memcpy(&rv, &str[pos], sizeof(T));
376 #endif
377  return rv;
378  }
379 
380 
381 
382  template <class T>
383  inline T decodeVarLE( const std::string& str,
384  std::string::size_type pos )
385  {
386  T rv;
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));
391 #else
392  std::memcpy(&rv, &str[pos], sizeof(T));
393 #endif
394  return rv;
395  }
396 
397  template <class T>
398  inline T decodeVar( std::string& str )
399  {
400  T t = gnsstk::BinUtils::decodeVar<T>(str, 0);
401  str.erase( 0, sizeof(T) );
402  return t;
403  }
404 
405  template <class T>
406  inline T decodeVarLE( std::string& str )
407  {
408  T t = gnsstk::BinUtils::decodeVarLE<T>(str, 0);
409  str.erase( 0, sizeof(T) );
410  return t;
411  }
412 
413  template<class T>
414  inline std::string encodeVar( const T& v )
415  {
416  std::string rv((const char*)&v, sizeof(v));
417 #if BYTE_ORDER == LITTLE_ENDIAN
418  std::reverse(rv.begin(), rv.end());
419 #endif
420  return rv;
421  }
422 
423 
424  template<class T>
425  inline void encodeVar( const T& v, std::string& str, size_t pos )
426  {
427  str.replace(pos, sizeof(T), reinterpret_cast<const char*>(&v),
428  sizeof(T));
429 #if BYTE_ORDER == LITTLE_ENDIAN
430  std::reverse(str.begin()+pos, str.begin()+pos+sizeof(T));
431 #endif
432  }
433 
434 
435  template<class T>
436  inline std::string encodeVarLE( const T& v )
437  {
438  std::string rv((const char*)&v, sizeof(v));
439 #if BYTE_ORDER == BIG_ENDIAN
440  std::reverse(rv.begin(), rv.end());
441 #endif
442  return rv;
443  }
444 
445 
446  template<class T>
447  inline void encodeVarLE( const T& v, std::string& str, size_t pos )
448  {
449  str.replace(pos, sizeof(T), reinterpret_cast<const char*>(&v),
450  sizeof(T));
451 #if BYTE_ORDER == BIG_ENDIAN
452  std::reverse(str.begin()+pos, str.begin()+pos+sizeof(T));
453 #endif
454  }
455 
456 
457  inline unsigned long reflect (unsigned long crc,
458  int bitnum)
459  {
460  unsigned long i, j = 1, crcout = 0;
461 
462  for (i = (unsigned long)1 << (bitnum - 1); i; i >>= 1)
463  {
464  if (crc & i)
465  {
466  crcout |= j;
467  }
468  j <<= 1;
469  }
470  return (crcout);
471  }
472 
473 
474  // This code "stolen" from Sven Reifegerste (zorci@gmx.de).
475  // Found at http://rcswww.urz.tu-dresden.de/~sr21/crctester.c
476  // from link at http://rcswww.urz.tu-dresden.de/~sr21/crc.html
477  inline uint32_t computeCRC(const unsigned char *data,
478  unsigned long len,
479  const CRCParam& params)
480  {
481  uint32_t i, j, c, bit;
482  uint32_t crc = params.initial;
483 
484  // at first, compute constant bit masks for whole CRC and
485  // CRC high bit
486  uint32_t crcmask =
487  ((((uint32_t)1 << (params.order - 1)) - 1) << 1) | 1;
488  uint32_t crchighbit = (uint32_t)1 << (params.order - 1);
489 
490  if (crc && params.direct)
491  {
492  for (i = 0; i < (uint32_t)params.order; i++)
493  {
494  bit = crc & 1;
495  if (bit)
496  {
497  crc ^= params.polynom;
498  }
499  crc >>= 1;
500  if (bit)
501  {
502  crc |= crchighbit;
503  }
504  }
505  }
506 
507 
508  for (i = 0; i < len; i++)
509  {
510  c = (uint32_t) * data++;
511  if (params.refin)
512  {
513  c = reflect(c, 8);
514  }
515 
516  for (j = 0x80; j; j >>= 1)
517  {
518  bit = crc & crchighbit;
519  crc <<= 1;
520  if (c & j)
521  {
522  crc |= 1;
523  }
524  if (bit)
525  {
526  crc ^= params.polynom;
527  }
528  }
529  }
530 
531  for (i = 0; i < (uint32_t)params.order; i++)
532  {
533  bit = crc & crchighbit;
534  crc <<= 1;
535  if (bit)
536  {
537  crc ^= params.polynom;
538  }
539  }
540 
541  if (params.refout)
542  {
543  crc = reflect(crc, params.order);
544  }
545  crc ^= params.final;
546  crc &= crcmask;
547 
548  return crc;
549  }
550 
551  std::string xorChecksum(const std::string& str, unsigned wordSize)
552  {
553  size_t strSize = str.size();
554  std::string rv(wordSize, 0);
555 
556  if (strSize % wordSize != 0)
557  {
558  gnsstk::InvalidParameter ip("Incomplete word in string.");
559  GNSSTK_THROW(ip);
560  }
561 
562  for (size_t i = 0; (i + wordSize - 1) < strSize; i += wordSize)
563  {
564  for (size_t j = 0; j < wordSize; j++)
565  {
566  rv[j] ^= str[i+j];
567  }
568  }
569 
570  return rv;
571  }
572 
573 
574  inline void buitohs(const void* p, uint16_t& v, unsigned pos)
575  {
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
579  v = *tp;
580 #else
581  v = (((*tp & 0x00ff) << 8) |
582  ((*tp & 0xff00) >> 8));
583 #endif
584  }
585 
586 
587  inline void buitohl(const void* p, uint32_t& v, unsigned pos)
588  {
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
592  v = *tp;
593 #else
594  v = (((*tp & 0x000000ff) << 24) |
595  ((*tp & 0x0000ff00) << 8) |
596  ((*tp & 0x00ff0000) >> 8) |
597  ((*tp & 0xff000000) >> 24));
598 #endif
599  }
600 
601 
602  inline void buitohll(const void* p, uint64_t& v, unsigned pos)
603  {
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
607  v = *tp;
608 #else
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));
617 #endif
618  }
619 
620 
621  inline void buitohss(const void* p, int16_t& v, unsigned pos)
622  {
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
626  v = *tp;
627 #else
628  // mask after shift so sign extension doesn't break stuff
629  v = (((*tp & 0x00ff) << 8) |
630  ((*tp >> 8) & 0x00ff));
631 #endif
632  }
633 
634 
635  inline void buitohsl(const void* p, int32_t& v, unsigned pos)
636  {
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
640  v = *tp;
641 #else
642  // mask after shift so sign extension doesn't break stuff
643  v = (((*tp & 0x000000ff) << 24) |
644  ((*tp & 0x0000ff00) << 8) |
645  ((*tp & 0x00ff0000) >> 8) |
646  ((*tp >> 24) & 0x000000ff));
647 #endif
648  }
649 
650 
651  inline void buitohsll(const void* p, int64_t& v, unsigned pos)
652  {
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
656  v = *tp;
657 #else
658  // mask after shift so sign extension doesn't break stuff
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));
667 #endif
668  }
669 
670 
671  inline void buitohf(const void* p, float& v, unsigned pos)
672  {
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));
677 #else
678  vp[0] = cp[3];
679  vp[1] = cp[2];
680  vp[2] = cp[1];
681  vp[3] = cp[0];
682 #endif
683  }
684 
685 
686  inline void buitohd(const void* p, double& v, unsigned pos)
687  {
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));
692 #else
693  vp[0] = cp[7];
694  vp[1] = cp[6];
695  vp[2] = cp[5];
696  vp[3] = cp[4];
697  vp[4] = cp[3];
698  vp[5] = cp[2];
699  vp[6] = cp[1];
700  vp[7] = cp[0];
701 #endif
702  }
703 
704  // network to host conversions
705 
706  inline void buntohs(const void* p, uint16_t& v, unsigned pos)
707  {
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
711  v = *tp;
712 #else
713  v = (((*tp & 0x00ff) << 8) |
714  ((*tp & 0xff00) >> 8));
715 #endif
716  }
717 
718 
719  inline void buntohl(const void* p, uint32_t& v, unsigned pos)
720  {
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
724  v = *tp;
725 #else
726  v = (((*tp & 0x000000ff) << 24) |
727  ((*tp & 0x0000ff00) << 8) |
728  ((*tp & 0x00ff0000) >> 8) |
729  ((*tp & 0xff000000) >> 24));
730 #endif
731  }
732 
733 
734  inline void buntohll(const void* p, uint64_t& v, unsigned pos)
735  {
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
739  v = *tp;
740 #else
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));
749 #endif
750  }
751 
752 
753  inline void buntohss(const void* p, int16_t& v, unsigned pos)
754  {
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
758  v = *tp;
759 #else
760  // mask after shift so sign extension doesn't break stuff
761  v = (((*tp & 0x00ff) << 8) |
762  ((*tp >> 8) & 0x00ff));
763 #endif
764  }
765 
766 
767  inline void buntohsl(const void* p, int32_t& v, unsigned pos)
768  {
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
772  v = *tp;
773 #else
774  // mask after shift so sign extension doesn't break stuff
775  v = (((*tp & 0x000000ff) << 24) |
776  ((*tp & 0x0000ff00) << 8) |
777  ((*tp & 0x00ff0000) >> 8) |
778  ((*tp >> 24) & 0x000000ff));
779 #endif
780  }
781 
782 
783  inline void buntohsll(const void* p, int64_t& v, unsigned pos)
784  {
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
788  v = *tp;
789 #else
790  // mask after shift so sign extension doesn't break stuff
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));
799 #endif
800  }
801 
802 
803  inline void buntohf(const void* p, float& v, unsigned pos)
804  {
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));
809 #else
810  vp[0] = cp[3];
811  vp[1] = cp[2];
812  vp[2] = cp[1];
813  vp[3] = cp[0];
814 #endif
815  }
816 
817 
818  inline void buntohd(const void* p, double& v, unsigned pos)
819  {
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));
824 #else
825  vp[0] = cp[7];
826  vp[1] = cp[6];
827  vp[2] = cp[5];
828  vp[3] = cp[4];
829  vp[4] = cp[3];
830  vp[5] = cp[2];
831  vp[6] = cp[1];
832  vp[7] = cp[0];
833 #endif
834  }
835 
836  // host to intel conversions
837 
838  inline void buhtois(void* p, uint16_t v, unsigned pos)
839  {
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
843  *tp = v;
844 #else
845  *tp = (((v & 0x00ff) << 8) |
846  ((v & 0xff00) >> 8));
847 #endif
848  }
849 
850 
851  inline void buhtoil(void* p, uint32_t v, unsigned pos)
852  {
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
856  *tp = v;
857 #else
858  *tp = (((v & 0x000000ff) << 24) |
859  ((v & 0x0000ff00) << 8) |
860  ((v & 0x00ff0000) >> 8) |
861  ((v & 0xff000000) >> 24));
862 #endif
863  }
864 
865 
866  inline void buhtoill(void* p, uint64_t v, unsigned pos)
867  {
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
871  *tp = v;
872 #else
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));
881 #endif
882  }
883 
884 
885  inline void buhtoiss(void* p, int16_t v, unsigned pos)
886  {
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
890  *tp = v;
891 #else
892  // mask after shift so sign extension doesn't break stuff
893  *tp = (((v & 0x00ff) << 8) |
894  ((v >> 8) & 0x00ff));
895 #endif
896  }
897 
898 
899  inline void buhtoisl(void* p, int32_t v, unsigned pos)
900  {
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
904  *tp = v;
905 #else
906  // mask after shift so sign extension doesn't break stuff
907  *tp = (((v & 0x000000ff) << 24) |
908  ((v & 0x0000ff00) << 8) |
909  ((v & 0x00ff0000) >> 8) |
910  ((v >> 24) & 0x000000ff));
911 #endif
912  }
913 
914 
915  inline void buhtoisll(void* p, int64_t v, unsigned pos)
916  {
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
920  *tp = v;
921 #else
922  // mask after shift so sign extension doesn't break stuff
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));
931 #endif
932  }
933 
934 
935  inline void buhtoif(void* p, float v, unsigned pos)
936  {
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));
941 #else
942  cp[0] = vp[3];
943  cp[1] = vp[2];
944  cp[2] = vp[1];
945  cp[3] = vp[0];
946 #endif
947  }
948 
949 
950  inline void buhtoid(void* p, double v, unsigned pos)
951  {
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));
956 #else
957  cp[0] = vp[7];
958  cp[1] = vp[6];
959  cp[2] = vp[5];
960  cp[3] = vp[4];
961  cp[4] = vp[3];
962  cp[5] = vp[2];
963  cp[6] = vp[1];
964  cp[7] = vp[0];
965 #endif
966  }
967 
968  // host to network conversions
969 
970  inline void buhtons(void* p, uint16_t v, unsigned pos)
971  {
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
975  *tp = v;
976 #else
977  *tp = (((v & 0x00ff) << 8) |
978  ((v & 0xff00) >> 8));
979 #endif
980  }
981 
982 
983  inline void buhtonl(void* p, uint32_t v, unsigned pos)
984  {
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
988  *tp = v;
989 #else
990  *tp = (((v & 0x000000ff) << 24) |
991  ((v & 0x0000ff00) << 8) |
992  ((v & 0x00ff0000) >> 8) |
993  ((v & 0xff000000) >> 24));
994 #endif
995  }
996 
997 
998  inline void buhtonll(void* p, uint64_t v, unsigned pos)
999  {
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
1003  *tp = v;
1004 #else
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));
1013 #endif
1014  }
1015 
1016 
1017  inline void buhtonss(void* p, int16_t v, unsigned pos)
1018  {
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
1022  *tp = v;
1023 #else
1024  // mask after shift so sign extension doesn't break stuff
1025  *tp = (((v & 0x00ff) << 8) |
1026  ((v >> 8) & 0x00ff));
1027 #endif
1028  }
1029 
1030 
1031  inline void buhtonsl(void* p, int32_t v, unsigned pos)
1032  {
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
1036  *tp = v;
1037 #else
1038  // mask after shift so sign extension doesn't break stuff
1039  *tp = (((v & 0x000000ff) << 24) |
1040  ((v & 0x0000ff00) << 8) |
1041  ((v & 0x00ff0000) >> 8) |
1042  ((v >> 24) & 0x000000ff));
1043 #endif
1044  }
1045 
1046 
1047  inline void buhtonsll(void* p, int64_t v, unsigned pos)
1048  {
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
1052  *tp = v;
1053 #else
1054  // mask after shift so sign extension doesn't break stuff
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));
1063 #endif
1064  }
1065 
1066 
1067  inline void buhtonf(void* p, float v, unsigned pos)
1068  {
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));
1073 #else
1074  cp[0] = vp[3];
1075  cp[1] = vp[2];
1076  cp[2] = vp[1];
1077  cp[3] = vp[0];
1078 #endif
1079  }
1080 
1081 
1082  inline void buhtond(void* p, double v, unsigned pos)
1083  {
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));
1088 #else
1089  cp[0] = vp[7];
1090  cp[1] = vp[6];
1091  cp[2] = vp[5];
1092  cp[3] = vp[4];
1093  cp[4] = vp[3];
1094  cp[5] = vp[2];
1095  cp[6] = vp[1];
1096  cp[7] = vp[0];
1097 #endif
1098  }
1099 
1100  } // end namespace BinUtils
1101 } // end namespace gnsstk
1102 
1103 #endif
gnsstk::BinUtils::CRC24Q
const CRCParam CRC24Q(24, 0x823ba9, 0, 0xffffff, true, false, false)
CRC-24Q parameters.
Definition: BinUtils.hpp:311
gnsstk::BinUtils::buitohsl
void buitohsl(const void *p, int32_t &v, unsigned pos=0)
Definition: BinUtils.hpp:635
gnsstk::BinUtils::CRCParam::refout
bool refout
reflect the CRC result before final XOR.
Definition: BinUtils.hpp:301
gnsstk::BinUtils::buitohs
void buitohs(const void *p, uint16_t &v, unsigned pos=0)
Definition: BinUtils.hpp:574
gnsstk::BinUtils::buntohsll
void buntohsll(const void *p, int64_t &v, unsigned pos=0)
Definition: BinUtils.hpp:783
gnsstk::BinUtils::decodeVarLE
T decodeVarLE(const std::string &str, std::string::size_type pos)
Definition: BinUtils.hpp:383
gnsstk::BinUtils::buntohd
void buntohd(const void *p, double &v, unsigned pos=0)
Definition: BinUtils.hpp:818
gnsstk::BinUtils::buhtonl
void buhtonl(void *p, uint32_t v, unsigned pos=0)
Definition: BinUtils.hpp:983
gnsstk::BinUtils::buhtoid
void buhtoid(void *p, double v, unsigned pos=0)
Definition: BinUtils.hpp:950
gnsstkplatform.h
gnsstk::BinUtils::buhtoiss
void buhtoiss(void *p, int16_t v, unsigned pos=0)
Definition: BinUtils.hpp:885
gnsstk::BinUtils::buhtond
void buhtond(void *p, double v, unsigned pos=0)
Definition: BinUtils.hpp:1082
gnsstk::BinUtils::CRCParam::CRCParam
CRCParam(int o, unsigned long p, unsigned long i, unsigned long f, bool d, bool ri, bool ro)
Definition: BinUtils.cpp:50
gnsstk::BinUtils::buhtonll
void buhtonll(void *p, uint64_t v, unsigned pos=0)
Definition: BinUtils.hpp:998
gnsstk::BinUtils::CRCParam::refin
bool refin
reflect the data bytes before processing.
Definition: BinUtils.hpp:300
gnsstk
For Sinex::InputHistory.
Definition: BasicFramework.cpp:50
gnsstk::BinUtils::buitohss
void buitohss(const void *p, int16_t &v, unsigned pos=0)
Definition: BinUtils.hpp:621
gnsstk::BinUtils::CRCParam::polynom
unsigned long polynom
CRC polynomial w/o the leading '1' bit.
Definition: BinUtils.hpp:296
gnsstk::BinUtils::buhtonss
void buhtonss(void *p, int16_t v, unsigned pos=0)
Definition: BinUtils.hpp:1017
gnsstk::BinUtils::computeCRC
uint32_t computeCRC(const unsigned char *data, unsigned long len, const CRCParam &params)
Definition: BinUtils.hpp:477
gnsstk::BinUtils::reflect
unsigned long reflect(unsigned long crc, int bitnum)
Reflects the lower bitnum bits of crc.
Definition: BinUtils.hpp:457
gnsstk::BinUtils::buntohl
void buntohl(const void *p, uint32_t &v, unsigned pos=0)
Definition: BinUtils.hpp:719
gnsstk::BinUtils::buhtonsl
void buhtonsl(void *p, int32_t v, unsigned pos=0)
Definition: BinUtils.hpp:1031
gnsstk::BinUtils::buntohss
void buntohss(const void *p, int16_t &v, unsigned pos=0)
Definition: BinUtils.hpp:753
gnsstk::BinUtils::CRCParam
Encapsulate parameters for CRC computation.
Definition: BinUtils.hpp:280
gnsstk::BinUtils::buitohsll
void buitohsll(const void *p, int64_t &v, unsigned pos=0)
Definition: BinUtils.hpp:651
gnsstk::BinUtils::buhtois
void buhtois(void *p, uint16_t v, unsigned pos=0)
Definition: BinUtils.hpp:838
gnsstk::BinUtils::xorChecksum
std::string xorChecksum(const std::string &str, unsigned wordSize)
Definition: BinUtils.hpp:551
gnsstk::BinUtils::buntohll
void buntohll(const void *p, uint64_t &v, unsigned pos=0)
Definition: BinUtils.hpp:734
gnsstk::BinUtils::CRCParam::initial
unsigned long initial
initial CRC initial value.
Definition: BinUtils.hpp:297
gnsstk::BinUtils::CRCParam::order
int order
CRC polynomial order w/o leading '1' bit.
Definition: BinUtils.hpp:295
gnsstk::BinUtils::buhtoil
void buhtoil(void *p, uint32_t v, unsigned pos=0)
Definition: BinUtils.hpp:851
gnsstk::BinUtils::buntohf
void buntohf(const void *p, float &v, unsigned pos=0)
Definition: BinUtils.hpp:803
gnsstk::BinUtils::buntohs
void buntohs(const void *p, uint16_t &v, unsigned pos=0)
Definition: BinUtils.hpp:706
gnsstk::BinUtils::CRCParam::direct
bool direct
kind of algorithm, true = no augmented zero bits.
Definition: BinUtils.hpp:299
gnsstk::BinUtils::buitohl
void buitohl(const void *p, uint32_t &v, unsigned pos=0)
Definition: BinUtils.hpp:587
gnsstk::BinUtils::CRCParam::final
unsigned long final
final final XOR value.
Definition: BinUtils.hpp:298
example4.pos
pos
Definition: example4.py:125
gnsstk::BinUtils::encodeVarLE
std::string encodeVarLE(const T &v)
Definition: BinUtils.hpp:436
gnsstk::BinUtils::buitohf
void buitohf(const void *p, float &v, unsigned pos=0)
Definition: BinUtils.hpp:671
gnsstk::BinUtils::buitohd
void buitohd(const void *p, double &v, unsigned pos=0)
Definition: BinUtils.hpp:686
example3.data
data
Definition: example3.py:22
cp
double cp
longitude sin, T*sin, cos coefficients
Definition: IERS2003NutationData.hpp:47
gnsstk::BinUtils::buhtoill
void buhtoill(void *p, uint64_t v, unsigned pos=0)
Definition: BinUtils.hpp:866
gnsstk::BinUtils::buhtonsll
void buhtonsll(void *p, int64_t v, unsigned pos=0)
Definition: BinUtils.hpp:1047
gnsstk::BinUtils::buhtoisll
void buhtoisll(void *p, int64_t v, unsigned pos=0)
Definition: BinUtils.hpp:915
Exception.hpp
gnsstk::BinUtils::buntohsl
void buntohsl(const void *p, int32_t &v, unsigned pos=0)
Definition: BinUtils.hpp:767
gnsstk::BinUtils::buhtonf
void buhtonf(void *p, float v, unsigned pos=0)
Definition: BinUtils.hpp:1067
gnsstk::BinUtils::countBits
unsigned short countBits(uint32_t v)
Definition: BinUtils.hpp:342
GNSSTK_THROW
#define GNSSTK_THROW(exc)
Definition: Exception.hpp:366
gnsstk::BinUtils::NEW_EXCEPTION_CLASS
NEW_EXCEPTION_CLASS(CRCException, Exception)
gnsstk::BinUtils::CRCGLOL3
const CRCParam CRCGLOL3(24, 0xc3267d, 0, 0xffffff, true, false, false)
GLONASS CDMA L3 CRC parameters.
Definition: BinUtils.hpp:313
gnsstk::BinUtils::buitohll
void buitohll(const void *p, uint64_t &v, unsigned pos=0)
Definition: BinUtils.hpp:602
gnsstk::BinUtils::buhtoif
void buhtoif(void *p, float v, unsigned pos=0)
Definition: BinUtils.hpp:935
gnsstk::BinUtils::CRC32
const CRCParam CRC32(32, 0x4c11db7, 0xffffffff, 0xffffffff, true, true, true)
CRC-32 parameters.
Definition: BinUtils.hpp:309
gnsstk::BinUtils::buhtoisl
void buhtoisl(void *p, int32_t v, unsigned pos=0)
Definition: BinUtils.hpp:899
gnsstk::BinUtils::encodeVar
std::string encodeVar(const T &v)
Definition: BinUtils.hpp:414
gnsstk::BinUtils::CRC16
const CRCParam CRC16(16, 0x8005, 0, 0, true, true, true)
CRC-16 parameters.
Definition: BinUtils.hpp:307
gnsstk::BinUtils::CRCCCITT
const CRCParam CRCCCITT(16, 0x1021, 0xffff, 0, true, false, false)
CCITT CRC parameters.
Definition: BinUtils.hpp:305
gnsstk::BinUtils::buhtons
void buhtons(void *p, uint16_t v, unsigned pos=0)
Definition: BinUtils.hpp:970
gnsstk::BinUtils::decodeVar
T decodeVar(const std::string &str, std::string::size_type pos)
Definition: BinUtils.hpp:366


gnsstk
Author(s):
autogenerated on Wed Oct 25 2023 02:40:38