util.h
Go to the documentation of this file.
00001 /*
00002  * util.h
00003  *  
00004  * helper function header file
00005  * 
00006  * a Net::DNS like library for C
00007  * 
00008  * (c) NLnet Labs, 2004
00009  * 
00010  * See the file LICENSE for the license
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 /* #define      dprintf(X, Y)  */
00029 
00030 #define LDNS_VERSION "1.6.12"
00031 #define LDNS_REVISION ((1<<16)|(6<<8)|(12))
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  * Copy data allowing for unaligned accesses in network byte order
00067  * (big endian).
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  * Copy data allowing for unaligned accesses in network byte order
00096  * (big endian).
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 /* warning. */
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 /*@unused@*/
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 mktime_from_utc(const struct tm *tm);
00272 
00287 struct tm * ldns_serial_arithmitics_gmtime_r(int32_t time, time_t now, struct tm *result);
00288  
00308 int ldns_init_random(FILE *fd, unsigned int size);
00309 
00315 uint16_t ldns_get_random(void);
00316 
00324 char *ldns_bubblebabble(uint8_t *data, size_t len);
00325 
00326 #ifndef B32_NTOP
00327 int ldns_b32_ntop(uint8_t const *src, size_t srclength,
00328              char *target, size_t targsize);
00329 int b32_ntop(uint8_t const *src, size_t srclength,
00330              char *target, size_t targsize);
00331 int ldns_b32_ntop_extended_hex(uint8_t const *src, size_t srclength,
00332              char *target, size_t targsize);
00333 int b32_ntop_extended_hex(uint8_t const *src, size_t srclength,
00334              char *target, size_t targsize);
00338 /*@unused@*/
00339 INLINE size_t ldns_b32_ntop_calculate_size(size_t srcsize)
00340 {
00341         size_t result = ((((srcsize / 5) * 8) - 2) + 2);
00342         return result;
00343 }
00344 #endif /* !B32_NTOP */
00345 #ifndef B32_PTON
00346 int ldns_b32_pton(char const *src, size_t hashed_owner_str_len, uint8_t *target, size_t targsize);
00347 int b32_pton(char const *src, size_t hashed_owner_str_len, uint8_t *target, size_t targsize);
00348 int ldns_b32_pton_extended_hex(char const *src, size_t hashed_owner_str_len, uint8_t *target, size_t targsize);
00349 int b32_pton_extended_hex(char const *src, size_t hashed_owner_str_len, uint8_t *target, size_t targsize);
00353 /*@unused@*/
00354 INLINE size_t ldns_b32_pton_calculate_size(size_t srcsize)
00355 {
00356         size_t result = ((((srcsize) / 8) * 5));
00357         return result;
00358 }
00359 #endif /* !B32_PTON */
00360 
00361 INLINE time_t ldns_time(time_t *t) { return time(t); }
00362 
00363 #ifdef __cplusplus
00364 }
00365 #endif
00366 
00367 #endif /* !_UTIL_H */