Skip to content
This repository has been archived by the owner on Apr 9, 2020. It is now read-only.

Commit

Permalink
Temporary fix for #29
Browse files Browse the repository at this point in the history
  • Loading branch information
cabo committed May 27, 2015
1 parent 241bc28 commit c2ce1ad
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/cn-cbor.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,23 @@ static double decode_half(int half) {

/* Fix these if you can't do non-aligned reads */
#define ntoh8p(p) (*(unsigned char*)(p))
#ifdef CBOR_CAN_DO_UNALIGNED_READS
#define ntoh16p(p) (ntohs(*(unsigned short*)(p)))
#define ntoh32p(p) (ntohl(*(unsigned long*)(p)))
#else
static uint16_t ntoh16p(unsigned char *p) {
uint16_t ret = ntoh8p(p);
ret <<= 8;
ret += ntoh8p(p+1);
return ret;
}
static uint32_t ntoh32p(unsigned char *p) {
uint64_t ret = ntoh16p(p);
ret <<= 16;
ret += ntoh16p(p+2);
return ret;
}
#endif
static uint64_t ntoh64p(unsigned char *p) {
uint64_t ret = ntoh32p(p);
ret <<= 32;
Expand Down

1 comment on commit c2ce1ad

@ks156
Copy link

@ks156 ks156 commented on c2ce1ad Jun 5, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something similar should be done in cn-encoder.c

diff --git a/src/cn-encoder.c b/src/cn-encoder.c
index f53e2c6..64db498 100644
--- a/src/cn-encoder.c
+++ b/src/cn-encoder.c
@@ -8,7 +8,9 @@ extern "C" {
 } /* Duh. */
 #endif

-#include <arpa/inet.h>
+#ifdef CBOR_CAN_DO_UNALIGNED_READS
+#include <arpa/inet.h> // needed for ntohl (e.g.) on Linux
+#endif
 #include <string.h>
 #include <strings.h>
 #include <stdbool.h>
@@ -18,8 +20,23 @@ extern "C" {
 #include "cbor.h"

 #define hton8p(p) (*(uint8_t*)(p))
+#ifdef CBOR_CAN_DO_UNALIGNED_READS
 #define hton16p(p) (htons(*(uint16_t*)(p)))
 #define hton32p(p) (htonl(*(uint32_t*)(p)))
+#else
+static uint16_t hton16p(const uint8_t *p) {
+  uint16_t ret = hton8p(p);
+  ret <<= 8;
+  ret += hton8p(p+1);
+  return ret;
+}
+static uint32_t hton32p(const uint8_t *p) {
+  uint64_t ret = hton16p(p);
+  ret <<= 16;
+  ret += hton16p(p+2);
+  return ret;
+}
+#endif
 static uint64_t hton64p(const uint8_t *p) {
   /* TODO: does this work on both BE and LE systems? */
   uint64_t ret = hton32p(p);

But this causes incompatible pointer type warnings here:
https://github.com/cabo/cn-cbor/blob/master/src/cn-encoder.c#L100
and here:
https://github.com/cabo/cn-cbor/blob/master/src/cn-encoder.c#L105

A cast to (uint8_t*) is probably needed then.

(I've not tested this patch yet. Just a workaround to compile the code)

Please sign in to comment.