util.h
Go to the documentation of this file.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 #include <time.h>
00018 #include <stdio.h>
00019
00020 #define dprintf(X,Y) fprintf(stderr, (X), (Y))
00021
00022
00023 #define LDNS_VERSION "1.3.0"
00024
00028 #ifdef S_SPLINT_S
00029 #define INLINE
00030 #else
00031 #define INLINE static inline
00032 #endif
00033
00037 #define LDNS_MALLOC(type) LDNS_XMALLOC(type, 1)
00038
00039 #define LDNS_XMALLOC(type, count) ((type *) malloc((count) * sizeof(type)))
00040
00041 #define LDNS_REALLOC(ptr, type) LDNS_XREALLOC((ptr), type, 1)
00042
00043 #define LDNS_XREALLOC(ptr, type, count) \
00044 ((type *) realloc((ptr), (count) * sizeof(type)))
00045
00046 #define LDNS_FREE(ptr) \
00047 do { free((ptr)); (ptr) = NULL; } while (0)
00048
00049 #define LDNS_DEP printf("DEPRECATED FUNCTION!\n");
00050
00051
00052
00053
00054
00055 INLINE uint16_t
00056 ldns_read_uint16(const void *src)
00057 {
00058 #ifdef ALLOW_UNALIGNED_ACCESSES
00059 return ntohs(*(uint16_t *) src);
00060 #else
00061 uint8_t *p = (uint8_t *) src;
00062 return ((uint16_t) p[0] << 8) | (uint16_t) p[1];
00063 #endif
00064 }
00065
00066 INLINE uint32_t
00067 ldns_read_uint32(const void *src)
00068 {
00069 #ifdef ALLOW_UNALIGNED_ACCESSES
00070 return ntohl(*(uint32_t *) src);
00071 #else
00072 uint8_t *p = (uint8_t *) src;
00073 return ( ((uint32_t) p[0] << 24)
00074 | ((uint32_t) p[1] << 16)
00075 | ((uint32_t) p[2] << 8)
00076 | (uint32_t) p[3]);
00077 #endif
00078 }
00079
00080
00081
00082
00083
00084 INLINE void
00085 ldns_write_uint16(void *dst, uint16_t data)
00086 {
00087 #ifdef ALLOW_UNALIGNED_ACCESSES
00088 * (uint16_t *) dst = htons(data);
00089 #else
00090 uint8_t *p = (uint8_t *) dst;
00091 p[0] = (uint8_t) ((data >> 8) & 0xff);
00092 p[1] = (uint8_t) (data & 0xff);
00093 #endif
00094 }
00095
00096 INLINE void
00097 ldns_write_uint32(void *dst, uint32_t data)
00098 {
00099 #ifdef ALLOW_UNALIGNED_ACCESSES
00100 * (uint32_t *) dst = htonl(data);
00101 #else
00102 uint8_t *p = (uint8_t *) dst;
00103 p[0] = (uint8_t) ((data >> 24) & 0xff);
00104 p[1] = (uint8_t) ((data >> 16) & 0xff);
00105 p[2] = (uint8_t) ((data >> 8) & 0xff);
00106 p[3] = (uint8_t) (data & 0xff);
00107 #endif
00108 }
00109
00110
00111 INLINE void
00112 ldns_write_uint64_as_uint48(void *dst, uint64_t data)
00113 {
00114 uint8_t *p = (uint8_t *) dst;
00115 p[0] = (uint8_t) ((data >> 40) & 0xff);
00116 p[1] = (uint8_t) ((data >> 32) & 0xff);
00117 p[2] = (uint8_t) ((data >> 24) & 0xff);
00118 p[3] = (uint8_t) ((data >> 16) & 0xff);
00119 p[4] = (uint8_t) ((data >> 8) & 0xff);
00120 p[5] = (uint8_t) (data & 0xff);
00121 }
00122
00123
00130 struct ldns_schwartzian_compare_struct {
00131 void *original_object;
00132 void *transformed_object;
00133 };
00134
00142 struct ldns_struct_lookup_table {
00143 int id;
00144 const char *name;
00145 };
00146 typedef struct ldns_struct_lookup_table ldns_lookup_table;
00147
00154 ldns_lookup_table *ldns_lookup_by_name(ldns_lookup_table table[],
00155 const char *name);
00156
00163 ldns_lookup_table *ldns_lookup_by_id(ldns_lookup_table table[], int id);
00164
00173 int ldns_get_bit(uint8_t bits[], size_t index);
00174
00175
00184 int ldns_get_bit_r(uint8_t bits[], size_t index);
00185
00196 void ldns_set_bit(uint8_t *byte, int bit_nr, bool value);
00197
00202
00203 INLINE long
00204 ldns_power(long a, long b) {
00205 long result = 1;
00206 while (b > 0) {
00207 if (b & 1) {
00208 result *= a;
00209 if (b == 1) {
00210 return result;
00211 }
00212 }
00213 a *= a;
00214 b /= 2;
00215 }
00216 return result;
00217 }
00218
00224 int ldns_hexdigit_to_int(char ch);
00225
00231 char ldns_int_to_hexdigit(int ch);
00232
00242 int
00243 ldns_hexstring_to_data(uint8_t *data, const char *str);
00244
00249 const char * ldns_version(void);
00250
00257 time_t mktime_from_utc(const struct tm *tm);
00258
00278 int ldns_init_random(FILE *fd, unsigned int size);
00279
00280 #endif