integers.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * integers.h
  3. *
  4. * defines integer types (or refers to their definitions)
  5. *
  6. * David A. McGrew
  7. * Cisco Systems, Inc.
  8. */
  9. /*
  10. *
  11. * Copyright (c) 2001-2006, Cisco Systems, Inc.
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions
  16. * are met:
  17. *
  18. * Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials provided
  24. * with the distribution.
  25. *
  26. * Neither the name of the Cisco Systems, Inc. nor the names of its
  27. * contributors may be used to endorse or promote products derived
  28. * from this software without specific prior written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  33. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  34. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  35. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  36. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  37. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  38. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  41. * OF THE POSSIBILITY OF SUCH DAMAGE.
  42. *
  43. */
  44. #ifndef INTEGERS_H
  45. #define INTEGERS_H
  46. #include "config.h" /* configuration file, using autoconf */
  47. #ifdef SRTP_KERNEL
  48. #include "kernel_compat.h"
  49. #else /* SRTP_KERNEL */
  50. /* use standard integer definitions, if they're available */
  51. #ifdef HAVE_STDLIB_H
  52. # include <stdlib.h>
  53. #endif
  54. #ifdef HAVE_STDINT_H
  55. # include <stdint.h>
  56. #endif
  57. #ifdef HAVE_INTTYPES_H
  58. # include <inttypes.h>
  59. #endif
  60. #ifdef HAVE_SYS_TYPES_H
  61. # include <sys/types.h>
  62. #endif
  63. #ifdef HAVE_SYS_INT_TYPES_H
  64. # include <sys/int_types.h> /* this exists on Sun OS */
  65. #endif
  66. #ifdef HAVE_MACHINE_TYPES_H
  67. # include <machine/types.h>
  68. #endif
  69. /* Can we do 64 bit integers? */
  70. #ifndef HAVE_UINT64_T
  71. # if SIZEOF_UNSIGNED_LONG == 8
  72. typedef unsigned long uint64_t;
  73. # elif SIZEOF_UNSIGNED_LONG_LONG == 8
  74. typedef unsigned long long uint64_t;
  75. # else
  76. # define NO_64BIT_MATH 1
  77. # endif
  78. #endif
  79. /* Reasonable defaults for 32 bit machines - you may need to
  80. * edit these definitions for your own machine. */
  81. #ifndef HAVE_UINT8_T
  82. typedef unsigned char uint8_t;
  83. #endif
  84. #ifndef HAVE_UINT16_T
  85. typedef unsigned short int uint16_t;
  86. #endif
  87. #ifndef HAVE_UINT32_T
  88. typedef unsigned int uint32_t;
  89. #endif
  90. #ifdef NO_64BIT_MATH
  91. typedef double uint64_t;
  92. /* assert that sizeof(double) == 8 */
  93. extern uint64_t make64(uint32_t high, uint32_t low);
  94. extern uint32_t high32(uint64_t value);
  95. extern uint32_t low32(uint64_t value);
  96. #endif
  97. #endif /* SRTP_KERNEL */
  98. /* These macros are to load and store 32-bit values from un-aligned
  99. addresses. This is required for processors that do not allow unaligned
  100. loads. */
  101. #ifdef ALIGNMENT_32BIT_REQUIRED
  102. /* Note that if it's in a variable, you can memcpy it */
  103. #ifdef WORDS_BIGENDIAN
  104. #define PUT_32(addr,value) \
  105. { \
  106. ((unsigned char *) (addr))[0] = (value >> 24); \
  107. ((unsigned char *) (addr))[1] = (value >> 16) & 0xff; \
  108. ((unsigned char *) (addr))[2] = (value >> 8) & 0xff; \
  109. ((unsigned char *) (addr))[3] = (value) & 0xff; \
  110. }
  111. #define GET_32(addr) ((((unsigned char *) (addr))[0] << 24) | \
  112. (((unsigned char *) (addr))[1] << 16) | \
  113. (((unsigned char *) (addr))[2] << 8) | \
  114. (((unsigned char *) (addr))[3]))
  115. #else
  116. #define PUT_32(addr,value) \
  117. { \
  118. ((unsigned char *) (addr))[3] = (value >> 24); \
  119. ((unsigned char *) (addr))[2] = (value >> 16) & 0xff; \
  120. ((unsigned char *) (addr))[1] = (value >> 8) & 0xff; \
  121. ((unsigned char *) (addr))[0] = (value) & 0xff; \
  122. }
  123. #define GET_32(addr) ((((unsigned char *) (addr))[3] << 24) | \
  124. (((unsigned char *) (addr))[2] << 16) | \
  125. (((unsigned char *) (addr))[1] << 8) | \
  126. (((unsigned char *) (addr))[0]))
  127. #endif // WORDS_BIGENDIAN
  128. #else
  129. #define PUT_32(addr,value) *(((uint32_t *) (addr)) = (value)
  130. #define GET_32(addr) (*(((uint32_t *) (addr)))
  131. #endif
  132. #endif /* INTEGERS_H */