00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #ifndef _UTIL_H
00014 #define _UTIL_H
00015
00016 #include <inttypes.h>
00017 #include <sys/types.h>
00018 #include <unistd.h>
00019 #include <ldns/common.h>
00020 #include <time.h>
00021 #include <stdio.h>
00022
00023 #ifdef __cplusplus
00024 extern "C" {
00025 #endif
00026
00027 #define dprintf(X,Y) fprintf(stderr, (X), (Y))
00028
00029
00030 #define LDNS_VERSION "1.6.17"
00031 #define LDNS_REVISION ((1<<16)|(6<<8)|(17))
00032
00036 #ifdef S_SPLINT_S
00037 # define INLINE
00038 #else
00039 # ifdef SWIG
00040 # define INLINE static
00041 # else
00042 # define INLINE static inline
00043 # endif
00044 #endif
00045
00049 #define LDNS_MALLOC(type) LDNS_XMALLOC(type, 1)
00050
00051 #define LDNS_XMALLOC(type, count) ((type *) malloc((count) * sizeof(type)))
00052
00053 #define LDNS_CALLOC(type, count) ((type *) calloc((count), sizeof(type)))
00054
00055 #define LDNS_REALLOC(ptr, type) LDNS_XREALLOC((ptr), type, 1)
00056
00057 #define LDNS_XREALLOC(ptr, type, count) \
00058 ((type *) realloc((ptr), (count) * sizeof(type)))
00059
00060 #define LDNS_FREE(ptr) \
00061 do { free((ptr)); (ptr) = NULL; } while (0)
00062
00063 #define LDNS_DEP printf("DEPRECATED FUNCTION!\n");
00064
00065
00066
00067
00068
00069 INLINE uint16_t
00070 ldns_read_uint16(const void *src)
00071 {
00072 #ifdef ALLOW_UNALIGNED_ACCESSES
00073 return ntohs(*(uint16_t *) src);
00074 #else
00075 uint8_t *p = (uint8_t *) src;
00076 return ((uint16_t) p[0] << 8) | (uint16_t) p[1];
00077 #endif
00078 }
00079
00080 INLINE uint32_t
00081 ldns_read_uint32(const void *src)
00082 {
00083 #ifdef ALLOW_UNALIGNED_ACCESSES
00084 return ntohl(*(uint32_t *) src);
00085 #else
00086 uint8_t *p = (uint8_t *) src;
00087 return ( ((uint32_t) p[0] << 24)
00088 | ((uint32_t) p[1] << 16)
00089 | ((uint32_t) p[2] << 8)
00090 | (uint32_t) p[3]);
00091 #endif
00092 }
00093
00094
00095
00096
00097
00098 INLINE void
00099 ldns_write_uint16(void *dst, uint16_t data)
00100 {
00101 #ifdef ALLOW_UNALIGNED_ACCESSES
00102 * (uint16_t *) dst = htons(data);
00103 #else
00104 uint8_t *p = (uint8_t *) dst;
00105 p[0] = (uint8_t) ((data >> 8) & 0xff);
00106 p[1] = (uint8_t) (data & 0xff);
00107 #endif
00108 }
00109
00110 INLINE void
00111 ldns_write_uint32(void *dst, uint32_t data)
00112 {
00113 #ifdef ALLOW_UNALIGNED_ACCESSES
00114 * (uint32_t *) dst = htonl(data);
00115 #else
00116 uint8_t *p = (uint8_t *) dst;
00117 p[0] = (uint8_t) ((data >> 24) & 0xff);
00118 p[1] = (uint8_t) ((data >> 16) & 0xff);
00119 p[2] = (uint8_t) ((data >> 8) & 0xff);
00120 p[3] = (uint8_t) (data & 0xff);
00121 #endif
00122 }
00123
00124
00125 INLINE void
00126 ldns_write_uint64_as_uint48(void *dst, uint64_t data)
00127 {
00128 uint8_t *p = (uint8_t *) dst;
00129 p[0] = (uint8_t) ((data >> 40) & 0xff);
00130 p[1] = (uint8_t) ((data >> 32) & 0xff);
00131 p[2] = (uint8_t) ((data >> 24) & 0xff);
00132 p[3] = (uint8_t) ((data >> 16) & 0xff);
00133 p[4] = (uint8_t) ((data >> 8) & 0xff);
00134 p[5] = (uint8_t) (data & 0xff);
00135 }
00136
00137
00144 struct ldns_schwartzian_compare_struct {
00145 void *original_object;
00146 void *transformed_object;
00147 };
00148
00156 struct ldns_struct_lookup_table {
00157 int id;
00158 const char *name;
00159 };
00160 typedef struct ldns_struct_lookup_table ldns_lookup_table;
00161
00168 ldns_lookup_table *ldns_lookup_by_name(ldns_lookup_table table[],
00169 const char *name);
00170
00177 ldns_lookup_table *ldns_lookup_by_id(ldns_lookup_table table[], int id);
00178
00187 int ldns_get_bit(uint8_t bits[], size_t index);
00188
00189
00198 int ldns_get_bit_r(uint8_t bits[], size_t index);
00199
00210 void ldns_set_bit(uint8_t *byte, int bit_nr, bool value);
00211
00216
00217 INLINE long
00218 ldns_power(long a, long b) {
00219 long result = 1;
00220 while (b > 0) {
00221 if (b & 1) {
00222 result *= a;
00223 if (b == 1) {
00224 return result;
00225 }
00226 }
00227 a *= a;
00228 b /= 2;
00229 }
00230 return result;
00231 }
00232
00238 int ldns_hexdigit_to_int(char ch);
00239
00245 char ldns_int_to_hexdigit(int ch);
00246
00256 int
00257 ldns_hexstring_to_data(uint8_t *data, const char *str);
00258
00263 const char * ldns_version(void);
00264
00271 time_t ldns_mktime_from_utc(const struct tm *tm);
00272
00273 time_t mktime_from_utc(const struct tm *tm);
00274
00289 struct tm * ldns_serial_arithmitics_gmtime_r(int32_t time, time_t now, struct tm *result);
00290
00310 int ldns_init_random(FILE *fd, unsigned int size);
00311
00317 uint16_t ldns_get_random(void);
00318
00326 char *ldns_bubblebabble(uint8_t *data, size_t len);
00327
00328
00329 INLINE time_t ldns_time(time_t *t) { return time(t); }
00330
00331
00335
00336 INLINE size_t ldns_b32_ntop_calculate_size(size_t src_data_length)
00337 {
00338 return src_data_length == 0 ? 0 : ((src_data_length - 1) / 5 + 1) * 8;
00339 }
00340
00341 INLINE size_t ldns_b32_ntop_calculate_size_no_padding(size_t src_data_length)
00342 {
00343 return ((src_data_length + 3) * 8 / 5) - 4;
00344 }
00345
00346 int ldns_b32_ntop(const uint8_t* src_data, size_t src_data_length,
00347 char* target_text_buffer, size_t target_text_buffer_size);
00348
00349 int ldns_b32_ntop_extended_hex(const uint8_t* src_data, size_t src_data_length,
00350 char* target_text_buffer, size_t target_text_buffer_size);
00351
00352 #if ! LDNS_BUILD_CONFIG_HAVE_B32_NTOP
00353
00354 int b32_ntop(const uint8_t* src_data, size_t src_data_length,
00355 char* target_text_buffer, size_t target_text_buffer_size);
00356
00357 int b32_ntop_extended_hex(const uint8_t* src_data, size_t src_data_length,
00358 char* target_text_buffer, size_t target_text_buffer_size);
00359
00360 #endif
00361
00362
00366
00367 INLINE size_t ldns_b32_pton_calculate_size(size_t src_text_length)
00368 {
00369 return src_text_length * 5 / 8;
00370 }
00371
00372 int ldns_b32_pton(const char* src_text, size_t src_text_length,
00373 uint8_t* target_data_buffer, size_t target_data_buffer_size);
00374
00375 int ldns_b32_pton_extended_hex(const char* src_text, size_t src_text_length,
00376 uint8_t* target_data_buffer, size_t target_data_buffer_size);
00377
00378 #if ! LDNS_BUILD_CONFIG_HAVE_B32_PTON
00379
00380 int b32_pton(const char* src_text, size_t src_text_length,
00381 uint8_t* target_data_buffer, size_t target_data_buffer_size);
00382
00383 int b32_pton_extended_hex(const char* src_text, size_t src_text_length,
00384 uint8_t* target_data_buffer, size_t target_data_buffer_size);
00385
00386 #endif
00387
00388
00389 #ifdef __cplusplus
00390 }
00391 #endif
00392
00393 #endif