summaryrefslogtreecommitdiffstats
path: root/lib/libcbor/src/cbor/internal/loaders.c
diff options
context:
space:
mode:
authordjm <djm@openbsd.org>2019-11-28 02:58:39 +0000
committerdjm <djm@openbsd.org>2019-11-28 02:58:39 +0000
commit9e5c2ddccc4a7ac0840ab8aa8ee64df773f69be5 (patch)
tree87472542e42cefecfb278df616a2df50e52c34b1 /lib/libcbor/src/cbor/internal/loaders.c
parentFix the buffer cache code to not use a giant uvm obj of all pages (diff)
downloadwireguard-openbsd-9e5c2ddccc4a7ac0840ab8aa8ee64df773f69be5.tar.xz
wireguard-openbsd-9e5c2ddccc4a7ac0840ab8aa8ee64df773f69be5.zip
update to libcbor rev 56a43b1e799; this includes a number of fixes
for unaligned accesses, requested by miod@ ok deraadt@
Diffstat (limited to 'lib/libcbor/src/cbor/internal/loaders.c')
-rw-r--r--lib/libcbor/src/cbor/internal/loaders.c119
1 files changed, 50 insertions, 69 deletions
diff --git a/lib/libcbor/src/cbor/internal/loaders.c b/lib/libcbor/src/cbor/internal/loaders.c
index 184691ca65a..fe000788923 100644
--- a/lib/libcbor/src/cbor/internal/loaders.c
+++ b/lib/libcbor/src/cbor/internal/loaders.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2014-2017 Pavel Kalvoda <me@pavelkalvoda.com>
+ * Copyright (c) 2014-2019 Pavel Kalvoda <me@pavelkalvoda.com>
*
* libcbor is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
@@ -7,93 +7,74 @@
#include "loaders.h"
#include <math.h>
+#include <string.h>
-#ifdef HAVE_ENDIAN_H
-#include <endian.h>
-#endif
-
-uint8_t _cbor_load_uint8(cbor_data source)
-{
- return (uint8_t) *source;
-}
+uint8_t _cbor_load_uint8(cbor_data source) { return (uint8_t)*source; }
-uint16_t _cbor_load_uint16(const unsigned char *source)
-{
-#ifdef HAVE_ENDIAN_H
- return be16toh(*(uint16_t *) source);
+uint16_t _cbor_load_uint16(const unsigned char *source) {
+#ifdef IS_BIG_ENDIAN
+ uint16_t result;
+ memcpy(&result, source, 2);
+ return result;
#else
- #ifdef IS_BIG_ENDIAN
- return *(uint16_t *) source;
- #else
- return ((uint16_t) *(source + 0) << 8) +
- (uint8_t) *(source + 1);
- #endif
+ return ((uint16_t) * (source + 0) << 8) + (uint8_t) * (source + 1);
#endif
}
-uint32_t _cbor_load_uint32(const unsigned char *source)
-{
-#ifdef HAVE_ENDIAN_H
- return be32toh(*(uint32_t *) source);
+uint32_t _cbor_load_uint32(const unsigned char *source) {
+#ifdef IS_BIG_ENDIAN
+ uint32_t result;
+ memcpy(&result, source, 4);
+ return result;
#else
- #ifdef IS_BIG_ENDIAN
- return *(uint32_t *) source;
- #else
- return ((uint32_t) *(source + 0) << 0x18) +
- ((uint32_t) *(source + 1) << 0x10) +
- ((uint16_t) *(source + 2) << 0x08) +
- (uint8_t) *(source + 3);
- #endif
+ return ((uint32_t) * (source + 0) << 0x18) +
+ ((uint32_t) * (source + 1) << 0x10) +
+ ((uint16_t) * (source + 2) << 0x08) + (uint8_t) * (source + 3);
#endif
}
-uint64_t _cbor_load_uint64(const unsigned char *source)
-{
-#ifdef HAVE_ENDIAN_H
- return be64toh(*(uint64_t *) source);
+uint64_t _cbor_load_uint64(const unsigned char *source) {
+#ifdef IS_BIG_ENDIAN
+ uint64_t result;
+ memcpy(&result, source, 8);
+ return result;
#else
- #ifdef IS_BIG_ENDIAN
- return *(uint64_t *) source;
- #else
- return ((uint64_t) *(source + 0) << 0x38) +
- ((uint64_t) *(source + 1) << 0x30) +
- ((uint64_t) *(source + 2) << 0x28) +
- ((uint64_t) *(source + 3) << 0x20) +
- ((uint32_t) *(source + 4) << 0x18) +
- ((uint32_t) *(source + 5) << 0x10) +
- ((uint16_t) *(source + 6) << 0x08) +
- (uint8_t) *(source + 7);
- #endif
+ return ((uint64_t) * (source + 0) << 0x38) +
+ ((uint64_t) * (source + 1) << 0x30) +
+ ((uint64_t) * (source + 2) << 0x28) +
+ ((uint64_t) * (source + 3) << 0x20) +
+ ((uint32_t) * (source + 4) << 0x18) +
+ ((uint32_t) * (source + 5) << 0x10) +
+ ((uint16_t) * (source + 6) << 0x08) + (uint8_t) * (source + 7);
#endif
}
/* As per http://tools.ietf.org/html/rfc7049#appendix-D */
-float _cbor_decode_half(unsigned char *halfp)
-{
- int half = (halfp[0] << 8) + halfp[1];
- int exp = (half >> 10) & 0x1f;
- int mant = half & 0x3ff;
- double val;
- if (exp == 0) val = ldexp(mant, -24);
- else if (exp != 31) val = ldexp(mant + 1024, exp - 25);
- else val = mant == 0 ? INFINITY : NAN;
- return (float) (half & 0x8000 ? -val : val);
+float _cbor_decode_half(unsigned char *halfp) {
+ int half = (halfp[0] << 8) + halfp[1];
+ int exp = (half >> 10) & 0x1f;
+ int mant = half & 0x3ff;
+ double val;
+ if (exp == 0)
+ val = ldexp(mant, -24);
+ else if (exp != 31)
+ val = ldexp(mant + 1024, exp - 25);
+ else
+ val = mant == 0 ? INFINITY : NAN;
+ return (float)(half & 0x8000 ? -val : val);
}
-double _cbor_load_half(cbor_data source)
-{
- /* Discard const */
- return _cbor_decode_half((unsigned char *) source);
+double _cbor_load_half(cbor_data source) {
+ /* Discard const */
+ return _cbor_decode_half((unsigned char *)source);
}
-float _cbor_load_float(cbor_data source)
-{
- union _cbor_float_helper helper = {.as_uint = _cbor_load_uint32(source)};
- return helper.as_float;
+float _cbor_load_float(cbor_data source) {
+ union _cbor_float_helper helper = {.as_uint = _cbor_load_uint32(source)};
+ return helper.as_float;
}
-double _cbor_load_double(cbor_data source)
-{
- union _cbor_double_helper helper = {.as_uint = _cbor_load_uint64(source)};
- return helper.as_double;
+double _cbor_load_double(cbor_data source) {
+ union _cbor_double_helper helper = {.as_uint = _cbor_load_uint64(source)};
+ return helper.as_double;
}