00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #ifndef _UTIL_H
00014 #define _UTIL_H
00015
00016 #include <stdbool.h>
00017
00018 #define dprintf(X,Y) fprintf(stderr, (X), (Y))
00019
00020
00021 #define LDNS_VERSION "1.0.1"
00022
00026 #ifdef S_SPLINT_S
00027 #define INLINE
00028 #else
00029 #define INLINE static inline
00030 #endif
00031
00035 #define LDNS_MALLOC(type) LDNS_XMALLOC(type, 1)
00036
00037 #define LDNS_XMALLOC(type, count) ((type *) malloc((count) * sizeof(type)))
00038
00039 #define LDNS_REALLOC(ptr, type) LDNS_XREALLOC((ptr), type, 1)
00040
00041 #define LDNS_XREALLOC(ptr, type, count) \
00042 ((type *) realloc((ptr), (count) * sizeof(type)))
00043
00044 #define LDNS_FREE(ptr) \
00045 do { free((ptr)); (ptr) = NULL; } while (0)
00046
00047 #define LDNS_DEP printf("DEPRECATED FUNCTION!\n");
00048
00049
00050
00051
00052
00053 INLINE uint16_t
00054 ldns_read_uint16(const void *src)
00055 {
00056 #ifdef ALLOW_UNALIGNED_ACCESSES
00057 return ntohs(*(uint16_t *) src);
00058 #else
00059 uint8_t *p = (uint8_t *) src;
00060 return ((uint16_t) p[0] << 8) | (uint16_t) p[1];
00061 #endif
00062 }
00063
00064 INLINE uint32_t
00065 ldns_read_uint32(const void *src)
00066 {
00067 #ifdef ALLOW_UNALIGNED_ACCESSES
00068 return ntohl(*(uint32_t *) src);
00069 #else
00070 uint8_t *p = (uint8_t *) src;
00071 return ( ((uint32_t) p[0] << 24)
00072 | ((uint32_t) p[1] << 16)
00073 | ((uint32_t) p[2] << 8)
00074 | (uint32_t) p[3]);
00075 #endif
00076 }
00077
00078
00079
00080
00081
00082 INLINE void
00083 ldns_write_uint16(void *dst, uint16_t data)
00084 {
00085 #ifdef ALLOW_UNALIGNED_ACCESSES
00086 * (uint16_t *) dst = htons(data);
00087 #else
00088 uint8_t *p = (uint8_t *) dst;
00089 p[0] = (uint8_t) ((data >> 8) & 0xff);
00090 p[1] = (uint8_t) (data & 0xff);
00091 #endif
00092 }
00093
00094 INLINE void
00095 ldns_write_uint32(void *dst, uint32_t data)
00096 {
00097 #ifdef ALLOW_UNALIGNED_ACCESSES
00098 * (uint32_t *) dst = htonl(data);
00099 #else
00100 uint8_t *p = (uint8_t *) dst;
00101 p[0] = (uint8_t) ((data >> 24) & 0xff);
00102 p[1] = (uint8_t) ((data >> 16) & 0xff);
00103 p[2] = (uint8_t) ((data >> 8) & 0xff);
00104 p[3] = (uint8_t) (data & 0xff);
00105 #endif
00106 }
00107
00108
00109 INLINE void
00110 ldns_write_uint64_as_uint48(void *dst, uint64_t data)
00111 {
00112 uint8_t *p = (uint8_t *) dst;
00113 p[0] = (uint8_t) ((data >> 40) & 0xff);
00114 p[1] = (uint8_t) ((data >> 32) & 0xff);
00115 p[2] = (uint8_t) ((data >> 24) & 0xff);
00116 p[3] = (uint8_t) ((data >> 16) & 0xff);
00117 p[4] = (uint8_t) ((data >> 8) & 0xff);
00118 p[5] = (uint8_t) (data & 0xff);
00119 }
00120
00121
00122 typedef struct ldns_struct_lookup_table ldns_lookup_table;
00123 struct ldns_struct_lookup_table {
00124 int id;
00125 const char *name;
00126 };
00127
00131 ldns_lookup_table *ldns_lookup_by_name(ldns_lookup_table table[],
00132 const char *name);
00133
00137 ldns_lookup_table *ldns_lookup_by_id(ldns_lookup_table table[], int id);
00138
00144 int ldns_get_bit(uint8_t bits[], size_t index);
00145
00146
00152 int ldns_get_bit_r(uint8_t bits[], size_t index);
00153
00164 void ldns_set_bit(uint8_t *byte, int bit_nr, bool value);
00165
00170
00171 static inline long
00172 ldns_power(long a, long b) {
00173 long result = 1;
00174 while (b > 0) {
00175 if (b & 1) {
00176 result *= a;
00177 if (b == 1) {
00178 return result;
00179 }
00180 }
00181 a *= a;
00182 b /= 2;
00183 }
00184 return result;
00185 }
00186
00190 int ldns_hexdigit_to_int(char ch);
00191
00195 char ldns_int_to_hexdigit(int ch);
00196
00201 const char * ldns_version(void);
00202
00212 int ldns_serial(uint32_t s1, uint32_t s2);
00213
00218 time_t mktime_from_utc(const struct tm *tm);
00219
00220 #endif