integers.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. #ifdef SRTP_KERNEL
  47. #include "kernel_compat.h"
  48. #else /* SRTP_KERNEL */
  49. /* use standard integer definitions, if they're available */
  50. #ifdef HAVE_STDLIB_H
  51. # include <stdlib.h>
  52. #endif
  53. #ifdef HAVE_STDINT_H
  54. # include <stdint.h>
  55. #endif
  56. #ifdef HAVE_INTTYPES_H
  57. # include <inttypes.h>
  58. #endif
  59. #ifdef HAVE_SYS_TYPES_H
  60. # include <sys/types.h>
  61. #endif
  62. #ifdef HAVE_SYS_INT_TYPES_H
  63. # include <sys/int_types.h> /* this exists on Sun OS */
  64. #endif
  65. #ifdef HAVE_MACHINE_TYPES_H
  66. # include <machine/types.h>
  67. #endif
  68. /* Can we do 64 bit integers? */
  69. #if !defined(HAVE_UINT64_T)
  70. # if SIZEOF_UNSIGNED_LONG == 8
  71. typedef unsigned long uint64_t;
  72. # elif SIZEOF_UNSIGNED_LONG_LONG == 8
  73. typedef unsigned long long uint64_t;
  74. # else
  75. # define NO_64BIT_MATH 1
  76. # endif
  77. #endif
  78. /* Reasonable defaults for 32 bit machines - you may need to
  79. * edit these definitions for your own machine. */
  80. #ifndef HAVE_UINT8_T
  81. typedef unsigned char uint8_t;
  82. #endif
  83. #ifndef HAVE_UINT16_T
  84. typedef unsigned short int uint16_t;
  85. #endif
  86. #ifndef HAVE_UINT32_T
  87. typedef unsigned int uint32_t;
  88. #endif
  89. #if defined(NO_64BIT_MATH) && defined(HAVE_CONFIG_H)
  90. typedef double uint64_t;
  91. /* assert that sizeof(double) == 8 */
  92. extern uint64_t make64(uint32_t high, uint32_t low);
  93. extern uint32_t high32(uint64_t value);
  94. extern uint32_t low32(uint64_t value);
  95. #endif
  96. #endif /* SRTP_KERNEL */
  97. /* These macros are to load and store 32-bit values from un-aligned
  98. addresses. This is required for processors that do not allow unaligned
  99. loads. */
  100. #ifdef ALIGNMENT_32BIT_REQUIRED
  101. /* Note that if it's in a variable, you can memcpy it */
  102. #ifdef WORDS_BIGENDIAN
  103. #define PUT_32(addr,value) \
  104. { \
  105. ((unsigned char *) (addr))[0] = (value >> 24); \
  106. ((unsigned char *) (addr))[1] = (value >> 16) & 0xff; \
  107. ((unsigned char *) (addr))[2] = (value >> 8) & 0xff; \
  108. ((unsigned char *) (addr))[3] = (value) & 0xff; \
  109. }
  110. #define GET_32(addr) ((((unsigned char *) (addr))[0] << 24) | \
  111. (((unsigned char *) (addr))[1] << 16) | \
  112. (((unsigned char *) (addr))[2] << 8) | \
  113. (((unsigned char *) (addr))[3]))
  114. #else
  115. #define PUT_32(addr,value) \
  116. { \
  117. ((unsigned char *) (addr))[3] = (value >> 24); \
  118. ((unsigned char *) (addr))[2] = (value >> 16) & 0xff; \
  119. ((unsigned char *) (addr))[1] = (value >> 8) & 0xff; \
  120. ((unsigned char *) (addr))[0] = (value) & 0xff; \
  121. }
  122. #define GET_32(addr) ((((unsigned char *) (addr))[3] << 24) | \
  123. (((unsigned char *) (addr))[2] << 16) | \
  124. (((unsigned char *) (addr))[1] << 8) | \
  125. (((unsigned char *) (addr))[0]))
  126. #endif // WORDS_BIGENDIAN
  127. #else
  128. #define PUT_32(addr,value) *(((uint32_t *) (addr)) = (value)
  129. #define GET_32(addr) (*(((uint32_t *) (addr)))
  130. #endif
  131. #endif /* INTEGERS_H */