util.h
Go to the documentation of this file.
/*
* util.h
*
* helper function header file
*
* a Net::DNS like library for C
*
* (c) NLnet Labs, 2004
*
* See the file LICENSE for the license
*/
#ifndef _UTIL_H
#define _UTIL_H
#include <inttypes.h>
#include <sys/types.h>
#include <unistd.h>
#include <ldns/common.h>
#include <time.h>
#include <stdio.h>
#ifdef __cplusplus
extern "C" {
#endif
#define dprintf(X,Y) fprintf(stderr, (X), (Y))
/* #define dprintf(X, Y) */
#define LDNS_VERSION "1.8.4"
#define LDNS_REVISION ((1<<16)|(8<<8)|(4))
#ifdef S_SPLINT_S
# define INLINE
#else
# ifdef SWIG
# define INLINE static
# else
# define INLINE static inline
# endif
#endif
#define LDNS_MALLOC(type) LDNS_XMALLOC(type, 1)
#define LDNS_XMALLOC(type, count) ((type *) malloc((count) * sizeof(type)))
#define LDNS_CALLOC(type, count) ((type *) calloc((count), sizeof(type)))
#define LDNS_REALLOC(ptr, type) LDNS_XREALLOC((ptr), type, 1)
#define LDNS_XREALLOC(ptr, type, count) \
((type *) realloc((ptr), (count) * sizeof(type)))
#define LDNS_FREE(ptr) \
do { free((ptr)); (ptr) = NULL; } while (0)
#define LDNS_DEP printf("DEPRECATED FUNCTION!\n");
/*
* Copy data allowing for unaligned accesses in network byte order
* (big endian).
*/
INLINE uint16_t
ldns_read_uint16(const void *src)
{
#ifdef ALLOW_UNALIGNED_ACCESSES
return ntohs(*(const uint16_t *) src);
#else
const uint8_t *p = (const uint8_t *) src;
return ((uint16_t) p[0] << 8) | (uint16_t) p[1];
#endif
}
INLINE uint32_t
ldns_read_uint32(const void *src)
{
#ifdef ALLOW_UNALIGNED_ACCESSES
return ntohl(*(const uint32_t *) src);
#else
const uint8_t *p = (const uint8_t *) src;
return ( ((uint32_t) p[0] << 24)
| ((uint32_t) p[1] << 16)
| ((uint32_t) p[2] << 8)
| (uint32_t) p[3]);
#endif
}
/*
* Copy data allowing for unaligned accesses in network byte order
* (big endian).
*/
INLINE void
ldns_write_uint16(void *dst, uint16_t data)
{
#ifdef ALLOW_UNALIGNED_ACCESSES
* (uint16_t *) dst = htons(data);
#else
uint8_t *p = (uint8_t *) dst;
p[0] = (uint8_t) ((data >> 8) & 0xff);
p[1] = (uint8_t) (data & 0xff);
#endif
}
INLINE void
ldns_write_uint32(void *dst, uint32_t data)
{
#ifdef ALLOW_UNALIGNED_ACCESSES
* (uint32_t *) dst = htonl(data);
#else
uint8_t *p = (uint8_t *) dst;
p[0] = (uint8_t) ((data >> 24) & 0xff);
p[1] = (uint8_t) ((data >> 16) & 0xff);
p[2] = (uint8_t) ((data >> 8) & 0xff);
p[3] = (uint8_t) (data & 0xff);
#endif
}
/* warning. */
INLINE void
ldns_write_uint64_as_uint48(void *dst, uint64_t data)
{
uint8_t *p = (uint8_t *) dst;
p[0] = (uint8_t) ((data >> 40) & 0xff);
p[1] = (uint8_t) ((data >> 32) & 0xff);
p[2] = (uint8_t) ((data >> 24) & 0xff);
p[3] = (uint8_t) ((data >> 16) & 0xff);
p[4] = (uint8_t) ((data >> 8) & 0xff);
p[5] = (uint8_t) (data & 0xff);
}
struct ldns_schwartzian_compare_struct {
void *original_object;
void *transformed_object;
};
struct ldns_struct_lookup_table {
int id;
const char *name;
};
typedef struct ldns_struct_lookup_table ldns_lookup_table;
ldns_lookup_table *ldns_lookup_by_name(ldns_lookup_table table[],
const char *name);
ldns_lookup_table *ldns_lookup_by_id(ldns_lookup_table table[], int id);
int ldns_get_bit(uint8_t bits[], size_t index);
int ldns_get_bit_r(uint8_t bits[], size_t index);
void ldns_set_bit(uint8_t *byte, int bit_nr, bool value);
/*@unused@*/
INLINE long
ldns_power(long a, long b) {
long result = 1;
while (b > 0) {
if (b & 1) {
result *= a;
if (b == 1) {
return result;
}
}
a *= a;
b /= 2;
}
return result;
}
int ldns_hexdigit_to_int(char ch);
char ldns_int_to_hexdigit(int ch);
int
ldns_hexstring_to_data(uint8_t *data, const char *str);
const char * ldns_version(void);
time_t ldns_mktime_from_utc(const struct tm *tm);
time_t mktime_from_utc(const struct tm *tm);
struct tm * ldns_serial_arithmetics_gmtime_r(int32_t time, time_t now, struct tm *result);
/* previously used wrong spelling */
#define ldns_serial_arithmitics_gmtime_r ldns_serial_arithmetics_gmtime_r
int ldns_init_random(FILE *fd, unsigned int size);
uint16_t ldns_get_random(void);
char *ldns_bubblebabble(uint8_t *data, size_t len);
INLINE time_t ldns_time(time_t *t) { return time(t); }
/*@unused@*/
INLINE size_t ldns_b32_ntop_calculate_size(size_t src_data_length)
{
return src_data_length == 0 ? 0 : ((src_data_length - 1) / 5 + 1) * 8;
}
INLINE size_t ldns_b32_ntop_calculate_size_no_padding(size_t src_data_length)
{
return ((src_data_length + 3) * 8 / 5) - 4;
}
int ldns_b32_ntop(const uint8_t* src_data, size_t src_data_length,
char* target_text_buffer, size_t target_text_buffer_size);
int ldns_b32_ntop_extended_hex(const uint8_t* src_data, size_t src_data_length,
char* target_text_buffer, size_t target_text_buffer_size);
#if ! LDNS_BUILD_CONFIG_HAVE_B32_NTOP
int b32_ntop(const uint8_t* src_data, size_t src_data_length,
char* target_text_buffer, size_t target_text_buffer_size);
int b32_ntop_extended_hex(const uint8_t* src_data, size_t src_data_length,
char* target_text_buffer, size_t target_text_buffer_size);
#endif /* ! LDNS_BUILD_CONFIG_HAVE_B32_NTOP */
/*@unused@*/
INLINE size_t ldns_b32_pton_calculate_size(size_t src_text_length)
{
return src_text_length * 5 / 8;
}
int ldns_b32_pton(const char* src_text, size_t src_text_length,
uint8_t* target_data_buffer, size_t target_data_buffer_size);
int ldns_b32_pton_extended_hex(const char* src_text, size_t src_text_length,
uint8_t* target_data_buffer, size_t target_data_buffer_size);
#if ! LDNS_BUILD_CONFIG_HAVE_B32_PTON
int b32_pton(const char* src_text, size_t src_text_length,
uint8_t* target_data_buffer, size_t target_data_buffer_size);
int b32_pton_extended_hex(const char* src_text, size_t src_text_length,
uint8_t* target_data_buffer, size_t target_data_buffer_size);
#endif /* ! LDNS_BUILD_CONFIG_HAVE_B32_PTON */
#ifdef __cplusplus
}
#endif
#endif /* !_UTIL_H */
common.h
Common definitions for LDNS.
ldns_schwartzian_compare_struct
Structure to do a Schwartzian-like transformation, for instance when sorting.
Definition util.h:144
ldns_schwartzian_compare_struct::original_object
void * original_object
Definition util.h:145
ldns_schwartzian_compare_struct::transformed_object
void * transformed_object
Definition util.h:146
ldns_struct_lookup_table
A general purpose lookup table.
Definition util.h:156
ldns_struct_lookup_table::name
const char * name
Definition util.h:158
ldns_struct_lookup_table::id
int id
Definition util.h:157
ldns_hexdigit_to_int
int ldns_hexdigit_to_int(char ch)
Returns the int value of the given (hex) digit.
Definition util.c:88
ldns_b32_pton_extended_hex
int ldns_b32_pton_extended_hex(const char *src_text, size_t src_text_length, uint8_t *target_data_buffer, size_t target_data_buffer_size)
Definition util.c:772
ldns_hexstring_to_data
int ldns_hexstring_to_data(uint8_t *data, const char *str)
Converts a hex string to binary data.
Definition util.c:138
ldns_mktime_from_utc
time_t ldns_mktime_from_utc(const struct tm *tm)
Convert TM to seconds since epoch (midnight, January 1st, 1970).
Definition util.c:194
ldns_b32_ntop
int ldns_b32_ntop(const uint8_t *src_data, size_t src_data_length, char *target_text_buffer, size_t target_text_buffer_size)
Definition util.c:600
ldns_lookup_by_name
ldns_lookup_table * ldns_lookup_by_name(ldns_lookup_table table[], const char *name)
Looks up the table entry by name, returns NULL if not found.
INLINE
#define INLINE
splint static inline workaround
Definition util.h:42
ldns_serial_arithmetics_gmtime_r
struct tm * ldns_serial_arithmetics_gmtime_r(int32_t time, time_t now, struct tm *result)
The function interprets time as the number of seconds since epoch with respect to now using serial ar...
Definition util.c:304
ldns_b32_ntop_extended_hex
int ldns_b32_ntop_extended_hex(const uint8_t *src_data, size_t src_data_length, char *target_text_buffer, size_t target_text_buffer_size)
Definition util.c:606
b32_ntop_extended_hex
int b32_ntop_extended_hex(const uint8_t *src_data, size_t src_data_length, char *target_text_buffer, size_t target_text_buffer_size)
Definition util.c:621
b32_pton
int b32_pton(const char *src_text, size_t src_text_length, uint8_t *target_data_buffer, size_t target_data_buffer_size)
Definition util.c:781
ldns_version
const char * ldns_version(void)
Show the internal library version.
Definition util.c:160
ldns_get_bit_r
int ldns_get_bit_r(uint8_t bits[], size_t index)
Returns the value of the specified bit The bits are counted from right to left, so bit #0 is the righ...
Definition util.c:62
ldns_lookup_by_id
ldns_lookup_table * ldns_lookup_by_id(ldns_lookup_table table[], int id)
Looks up the table entry by id, returns NULL if not found.
ldns_bubblebabble
char * ldns_bubblebabble(uint8_t *data, size_t len)
Encode data as BubbleBabble.
Definition util.c:430
ldns_set_bit
void ldns_set_bit(uint8_t *byte, int bit_nr, signed char value)
sets the specified bit in the specified byte to 1 if value is true, 0 if false The bits are counted f...
Definition util.c:72
ldns_int_to_hexdigit
char ldns_int_to_hexdigit(int ch)
Returns the char (hex) representation of the given int.
Definition util.c:113
ldns_get_bit
int ldns_get_bit(uint8_t bits[], size_t index)
Returns the value of the specified bit The bits are counted from left to right, so bit #0 is the left...
Definition util.c:52
b32_ntop
int b32_ntop(const uint8_t *src_data, size_t src_data_length, char *target_text_buffer, size_t target_text_buffer_size)
Definition util.c:615
mktime_from_utc
time_t mktime_from_utc(const struct tm *tm)
Definition util.c:219
ldns_get_random
uint16_t ldns_get_random(void)
Get random number.
Definition util.c:412
ldns_b32_pton
int ldns_b32_pton(const char *src_text, size_t src_text_length, uint8_t *target_data_buffer, size_t target_data_buffer_size)
Definition util.c:766
ldns_init_random
int ldns_init_random(FILE *fd, unsigned int size)
Seed the random function.
Definition util.c:339
b32_pton_extended_hex
int b32_pton_extended_hex(const char *src_text, size_t src_text_length, uint8_t *target_data_buffer, size_t target_data_buffer_size)
Definition util.c:787