You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

endianness.h 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* This file is part of Netsukuku
  2. * (c) Copyright 2005 Andrea Lo Pumo aka AlpT <alpt@freaknet.org>
  3. *
  4. * This source code is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as published
  6. * by the Free Software Foundation; either version 2 of the License,
  7. * or (at your option) any later version.
  8. *
  9. * This source code is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. * Please refer to the GNU Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Public License along with
  15. * this source code; if not, write to:
  16. * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #ifndef ENDIANNESS_H
  19. #define ENDIANNESS_H
  20. #define MAX_INTS_PER_STRUCT 8 /* The maximum number of short/int variables
  21. present in a struct */
  22. #define IINFO_DYNAMIC_VALUE -1 /* This define is used to fill part in a
  23. int_info struct that must be set each time.
  24. If that part is not set, -1 will remain, and
  25. the int_info functions will call fatal().
  26. Therefore this is useful to track bugs. */
  27. /* flags for int_info.int_type */
  28. #define INT_TYPE_VOID 0 /* Emptiness is loneliness, and loneliness is
  29. cleanliness */
  30. #define INT_TYPE_32BIT 1 /* The int var is of 32 bits */
  31. #define INT_TYPE_16BIT (1<<1) /* The int var is of 16 bits */
  32. #define INT_TYPE_WORDS (1<<2) /* The int var is composed by an array of ints,
  33. like the ipv6 ip (struct in6_addr) */
  34. #define INT_TYPE_NETWORK (1<<3) /* The int var is stored in network order */
  35. /*
  36. * int_info: this struct is used to keep the information about the int/short
  37. * variables present in a struct. It is useful to convert all the int/short
  38. * vars in another endian format with a simple function.
  39. * WARNING: There is a drawback: the struct must have the __packed__
  40. * attribute (but since we are using this for packet structs we don't care).
  41. *
  42. * Here there is an example which show how to use this int_info:
  43. *
  44. * given the struct s:
  45. * struct
  46. * {
  47. * u_char a;
  48. * int b;
  49. * short c;
  50. * char d[23];
  51. * int e[4];
  52. * }__attribute__ ((__packed__)) s;
  53. *
  54. * its int_info struct should be filled in this way:
  55. *
  56. * int_info s_int_info = { 3,
  57. * {INT_TYPE_32BIT, INT_TYPE_16BIT, INT_TYPE_32BIT},
  58. * { sizeof(char), sizeof(char)+sizeof(int),
  59. * sizeof(char)+sizeof(int)+sizeof(short)+sizeof(char)*23},
  60. * { 1, 1, 4 }
  61. * };
  62. */
  63. typedef struct {
  64. /* The total int/short vars present in the struct */
  65. int total_ints;
  66. /* Each member in the int_type array corresponds to a int/short var
  67. * and it is set using the above INT_TYPE_ flags */
  68. char int_type[MAX_INTS_PER_STRUCT];
  69. /* Each member in the int_offset array specifies the amount of bytes
  70. * to be added at the end of the struct to get the relative int/short
  71. * var. */
  72. size_t int_offset[MAX_INTS_PER_STRUCT];
  73. /* int_nmemb[x] is equal to the number of consecutive ints/shorts var,
  74. * which start at the int_offset[x] offset. */
  75. size_t int_nmemb[MAX_INTS_PER_STRUCT];
  76. } int_info;
  77. /* Useful to declare constant static int_info structs in .h files */
  78. #define INT_INFO const static int_info
  79. #if BYTE_ORDER == LITTLE_ENDIAN
  80. #include <linux/byteorder/little_endian.h>
  81. #else
  82. #include <linux/byteorder/big_endian.h>
  83. #endif
  84. /* * * Functions declaration * * */
  85. void *int_info_copy(int_info * dst, const int_info * src);
  86. void ints_array_htons(short *netshort, int nmemb);
  87. void ints_array_ntohs(short *hostshort, int nmemb);
  88. void ints_array_htonl(int *netlong, int nmemb);
  89. void ints_array_ntohl(int *hostlong, int nmemb);
  90. void ints_network_to_host(void *s, int_info iinfo);
  91. void ints_host_to_network(void *s, int_info iinfo);
  92. void ints_printf(void *s, int_info iinfo,
  93. void (*print_func(const char *, ...)));
  94. #endif /*ENDIANNESS_H */