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 <stdbool.h>
00017 #include <time.h>
00018 
00019 #define dprintf(X,Y) fprintf(stderr, (X), (Y))
00020 /* #define      dprintf(X, Y)  */
00021 
00022 #define LDNS_VERSION "1.2.1 "
00023 
00027 #ifdef S_SPLINT_S
00028 #define INLINE 
00029 #else
00030 #define INLINE static inline
00031 #endif
00032 
00036 #define LDNS_MALLOC(type)               LDNS_XMALLOC(type, 1)
00037 
00038 #define LDNS_XMALLOC(type, count)       ((type *) malloc((count) * sizeof(type)))
00039 
00040 #define LDNS_REALLOC(ptr, type)         LDNS_XREALLOC((ptr), type, 1)
00041 
00042 #define LDNS_XREALLOC(ptr, type, count)                         \
00043         ((type *) realloc((ptr), (count) * sizeof(type)))
00044 
00045 #define LDNS_FREE(ptr) \
00046         do { free((ptr)); (ptr) = NULL; } while (0)
00047 
00048 #define LDNS_DEP     printf("DEPRECATED FUNCTION!\n");
00049 
00050 /*
00051  * Copy data allowing for unaligned accesses in network byte order
00052  * (big endian).
00053  */
00054 INLINE uint16_t
00055 ldns_read_uint16(const void *src)
00056 {
00057 #ifdef ALLOW_UNALIGNED_ACCESSES
00058         return ntohs(*(uint16_t *) src);
00059 #else
00060         uint8_t *p = (uint8_t *) src;
00061         return ((uint16_t) p[0] << 8) | (uint16_t) p[1];
00062 #endif
00063 }
00064 
00065 INLINE uint32_t
00066 ldns_read_uint32(const void *src)
00067 {
00068 #ifdef ALLOW_UNALIGNED_ACCESSES
00069         return ntohl(*(uint32_t *) src);
00070 #else
00071         uint8_t *p = (uint8_t *) src;
00072         return (  ((uint32_t) p[0] << 24)
00073                 | ((uint32_t) p[1] << 16)
00074                 | ((uint32_t) p[2] << 8)
00075                 |  (uint32_t) p[3]);
00076 #endif
00077 }
00078 
00079 /*
00080  * Copy data allowing for unaligned accesses in network byte order
00081  * (big endian).
00082  */
00083 INLINE void
00084 ldns_write_uint16(void *dst, uint16_t data)
00085 {
00086 #ifdef ALLOW_UNALIGNED_ACCESSES
00087         * (uint16_t *) dst = htons(data);
00088 #else
00089         uint8_t *p = (uint8_t *) dst;
00090         p[0] = (uint8_t) ((data >> 8) & 0xff);
00091         p[1] = (uint8_t) (data & 0xff);
00092 #endif
00093 }
00094 
00095 INLINE void
00096 ldns_write_uint32(void *dst, uint32_t data)
00097 {
00098 #ifdef ALLOW_UNALIGNED_ACCESSES
00099         * (uint32_t *) dst = htonl(data);
00100 #else
00101         uint8_t *p = (uint8_t *) dst;
00102         p[0] = (uint8_t) ((data >> 24) & 0xff);
00103         p[1] = (uint8_t) ((data >> 16) & 0xff);
00104         p[2] = (uint8_t) ((data >> 8) & 0xff);
00105         p[3] = (uint8_t) (data & 0xff);
00106 #endif
00107 }
00108 
00109 /* warning. */
00110 INLINE void
00111 ldns_write_uint64_as_uint48(void *dst, uint64_t data)
00112 {
00113         uint8_t *p = (uint8_t *) dst;
00114         p[0] = (uint8_t) ((data >> 40) & 0xff);
00115         p[1] = (uint8_t) ((data >> 32) & 0xff);
00116         p[2] = (uint8_t) ((data >> 24) & 0xff);
00117         p[3] = (uint8_t) ((data >> 16) & 0xff);
00118         p[4] = (uint8_t) ((data >> 8) & 0xff);
00119         p[5] = (uint8_t) (data & 0xff);
00120 }
00121 
00122 
00129 struct ldns_schwartzian_compare_struct {
00130         void *original_object;
00131         void *transformed_object;
00132 };
00133 
00141 struct ldns_struct_lookup_table {
00142         int id;
00143         const char *name;
00144 };
00145 typedef struct ldns_struct_lookup_table ldns_lookup_table;
00146   
00153 ldns_lookup_table *ldns_lookup_by_name(ldns_lookup_table table[],
00154                                        const char *name);
00155 
00162 ldns_lookup_table *ldns_lookup_by_id(ldns_lookup_table table[], int id);
00163 
00172 int ldns_get_bit(uint8_t bits[], size_t index);
00173 
00174 
00183 int ldns_get_bit_r(uint8_t bits[], size_t index);
00184 
00195 void ldns_set_bit(uint8_t *byte, int bit_nr, bool value);
00196 
00201 /*@unused@*/
00202 INLINE long
00203 ldns_power(long a, long b) {
00204         long result = 1;
00205         while (b > 0) {
00206                 if (b & 1) {
00207                         result *= a;
00208                         if (b == 1) {
00209                                 return result;
00210                         }
00211                 }
00212                 a *= a;
00213                 b /= 2;
00214         }
00215         return result;
00216 }
00217 
00223 int ldns_hexdigit_to_int(char ch);
00224 
00230 char ldns_int_to_hexdigit(int ch);
00231 
00236 const char * ldns_version(void);
00237 
00244 time_t mktime_from_utc(const struct tm *tm);
00245 
00246 #endif /* !_UTIL_H */

Generated on Sat Dec 8 05:43:31 2007 for ldns by  doxygen 1.5.4