summaryrefslogtreecommitdiff
path: root/src/libknot/util/utils.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/libknot/util/utils.h')
-rw-r--r--src/libknot/util/utils.h94
1 files changed, 40 insertions, 54 deletions
diff --git a/src/libknot/util/utils.h b/src/libknot/util/utils.h
index fd275b3..0aa7a55 100644
--- a/src/libknot/util/utils.h
+++ b/src/libknot/util/utils.h
@@ -27,6 +27,7 @@
#ifndef _KNOT_UTILS_H_
#define _KNOT_UTILS_H_
+#include "util/endian.h"
#include <string.h>
#include <stdint.h>
#include <stdio.h>
@@ -67,19 +68,6 @@ knot_lookup_table_t *knot_lookup_by_name(knot_lookup_table_t *table,
knot_lookup_table_t *knot_lookup_by_id(knot_lookup_table_t *table,
int id);
-/*!
- * \brief Strlcpy - safe string copy function, based on FreeBSD implementation.
- *
- * http://www.openbsd.org/cgi-bin/cvsweb/src/lib/libc/string/
- *
- * \param dst Destination string.
- * \param src Source string.
- * \param size How many characters to copy - 1.
- *
- * \return strlen(src), if retval >= siz, truncation occurred.
- */
-size_t knot_strlcpy(char *dst, const char *src, size_t size);
-
/*
* Writing / reading arbitrary data to / from wireformat.
*/
@@ -89,13 +77,11 @@ size_t knot_strlcpy(char *dst, const char *src, size_t size);
*
* \param pos Data to read the 2 bytes from.
*
- * \todo Wrong assumption of endianness (issue #1558).
- *
- * \return The 2 bytes read, in inverse endian.
+ * \return The 2 bytes read, in host byte order.
*/
static inline uint16_t knot_wire_read_u16(const uint8_t *pos)
{
- return (pos[0] << 8) | pos[1];
+ return be16toh(*(uint16_t *)pos);
}
/*!
@@ -103,13 +89,11 @@ static inline uint16_t knot_wire_read_u16(const uint8_t *pos)
*
* \param pos Data to read the 4 bytes from.
*
- * \todo Wrong assumption of endianness (issue #1558).
- *
- * \return The 4 bytes read, in inverse endian.
+ * \return The 4 bytes read, in host byte order.
*/
static inline uint32_t knot_wire_read_u32(const uint8_t *pos)
{
- return (pos[0] << 24) | (pos[1] << 16) | (pos[2] << 8) | pos[3];
+ return be32toh(*(uint32_t *)pos);
}
/*!
@@ -117,80 +101,83 @@ static inline uint32_t knot_wire_read_u32(const uint8_t *pos)
*
* \param pos Data to read the 6 bytes from.
*
- * \todo Wrong assumption of endianness (issue #1558).
- *
- * \return The 6 bytes read, in inverse endian.
+ * \return The 6 bytes read, in host byte order.
*/
static inline uint64_t knot_wire_read_u48(const uint8_t *pos)
{
- return ((uint64_t)(pos[0]) << 40) | ((uint64_t)(pos[1]) << 32)
- | ((uint64_t)(pos[2]) << 24) | ((uint64_t)(pos[3]) << 16)
- | ((uint64_t)(pos[4]) << 8) | (uint64_t)pos[5];
+ uint64_t input = 0;
+ memcpy((void *)&input + 1, (void *)pos, 6);
+ return be64toh(input) >> 8;
}
/*!
- * \brief Writes 2 bytes in wireformat.
+ * \brief Read 8 bytes from the wireformat data.
*
- * The endian of the data is inverted.
+ * \param pos Data to read the 8 bytes from.
+ *
+ * \return The 8 bytes read, in host byte order.
+ */
+static inline uint64_t knot_wire_read_u64(const uint8_t *pos)
+{
+ return be64toh(*(uint64_t *)pos);
+}
+
+/*!
+ * \brief Writes 2 bytes in wireformat.
*
- * \todo Wrong assumption of endianness (issue #1558).
+ * The data are stored in network byte order (big endian).
*
* \param pos Position where to put the 2 bytes.
* \param data Data to put.
*/
static inline void knot_wire_write_u16(uint8_t *pos, uint16_t data)
{
- *(pos++) = (uint8_t)((data >> 8) & 0xff);
- *pos = (uint8_t)(data & 0xff);
+ *(uint16_t *)pos = htobe16(data);
}
/*!
* \brief Writes 4 bytes in wireformat.
*
- * The endian of the data is inverted.
- *
- * \todo Wrong assumption of endianness (issue #1558).
+ * The data are stored in network byte order (big endian).
*
* \param pos Position where to put the 4 bytes.
* \param data Data to put.
*/
static inline void knot_wire_write_u32(uint8_t *pos, uint32_t data)
{
- *(pos++) = (uint8_t)((data >> 24) & 0xff);
- *(pos++) = (uint8_t)((data >> 16) & 0xff);
- *(pos++) = (uint8_t)((data >> 8) & 0xff);
- *pos = (uint8_t)(data & 0xff);
+ *(uint32_t *)pos = htobe32(data);
}
/*!
* \brief Writes 6 bytes in wireformat.
*
- * The endian of the data is inverted.
- *
- * \todo Wrong assumption of endianness (issue #1558).
+ * The data are stored in network byte order (big endian).
*
* \param pos Position where to put the 4 bytes.
* \param data Data to put.
*/
static inline void knot_wire_write_u48(uint8_t *pos, uint64_t data)
{
- *(pos++) = (uint8_t)((data >> 40) & 0xff);
- *(pos++) = (uint8_t)((data >> 32) & 0xff);
- *(pos++) = (uint8_t)((data >> 24) & 0xff);
- *(pos++) = (uint8_t)((data >> 16) & 0xff);
- *(pos++) = (uint8_t)((data >> 8) & 0xff);
- *pos = (uint8_t)(data & 0xff);
+ uint64_t swapped = htobe64(data << 8);
+ memcpy((void *)pos, (uint8_t *)&swapped + 1, 6);
}
/*!
- * \brief Linear congruential generator.
+ * \brief Writes 8 bytes in wireformat.
+ *
+ * The data are stored in network byte order (big endian).
*
- * Simple pseudorandom generator for general purpose.
- * \warning Do not use for cryptography.
- * \return Random number <0, (size_t)~0>
+ * \param pos Position where to put the 8 bytes.
+ * \param data Data to put.
*/
-size_t knot_quick_rand();
+static inline void knot_wire_write_u64(uint8_t *pos, uint64_t data)
+{
+ *(uint64_t *)pos = htobe64(data);
+}
+/*!
+ * \brief Get random packet id.
+ */
uint16_t knot_random_id();
/*!
@@ -206,4 +193,3 @@ struct flock* knot_file_lock(short type, short whence);
#endif /* _KNOT_UTILS_H_ */
/*! @} */
-