From ede7f1431ae06c9086f2a83a57bd7832d99280e3 Mon Sep 17 00:00:00 2001
From: Thiago Macieira <thiago.macieira@intel.com>
Date: Sat, 30 Jul 2016 23:08:39 -0700
Subject: [PATCH] Revert "Revert "Give a name to unnamed union in CborEncoder""

This reverts commit 4628a110b736c3c31bd8c577ea1d29400e0c0a53 and thus
brings 0e1440ab929aeee9b4ccc8040365039767801ebf back.

Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
---
 src/cbor.h                                |  6 +++---
 src/cborencoder.c                         | 22 +++++++++++-----------
 src/cborencoder_close_container_checked.c |  4 ++--
 tools/json2cbor/json2cbor.c               |  4 ++--
 4 files changed, 18 insertions(+), 18 deletions(-)

diff --git a/src/cbor.h b/src/cbor.h
index 68b3fd3..a485cdf 100644
--- a/src/cbor.h
+++ b/src/cbor.h
@@ -162,7 +162,7 @@ struct CborEncoder
     union {
         uint8_t *ptr;
         ptrdiff_t bytes_needed;
-    };
+    } data;
     const uint8_t *end;
     size_t added;
     int flags;
@@ -204,12 +204,12 @@ CBOR_API CborError cbor_encoder_close_container_checked(CborEncoder *encoder, co
 
 CBOR_INLINE_API size_t cbor_encoder_get_buffer_size(const CborEncoder *encoder, const uint8_t *buffer)
 {
-    return (size_t)(encoder->ptr - buffer);
+    return (size_t)(encoder->data.ptr - buffer);
 }
 
 CBOR_INLINE_API size_t cbor_encoder_get_extra_bytes_needed(const CborEncoder *encoder)
 {
-    return encoder->end ? 0 : (size_t)encoder->bytes_needed;
+    return encoder->end ? 0 : (size_t)encoder->data.bytes_needed;
 }
 
 /* Parser API */
diff --git a/src/cborencoder.c b/src/cborencoder.c
index ef1bc0b..4af705b 100644
--- a/src/cborencoder.c
+++ b/src/cborencoder.c
@@ -197,7 +197,7 @@
  */
 void cbor_encoder_init(CborEncoder *encoder, uint8_t *buffer, size_t size, int flags)
 {
-    encoder->ptr = buffer;
+    encoder->data.ptr = buffer;
     encoder->end = buffer + size;
     encoder->added = 0;
     encoder->flags = flags;
@@ -235,7 +235,7 @@ static inline void put64(void *where, uint64_t v)
 static inline bool would_overflow(CborEncoder *encoder, size_t len)
 {
     ptrdiff_t remaining = (ptrdiff_t)encoder->end;
-    remaining -= remaining ? (ptrdiff_t)encoder->ptr : encoder->bytes_needed;
+    remaining -= remaining ? (ptrdiff_t)encoder->data.ptr : encoder->data.bytes_needed;
     remaining -= (ptrdiff_t)len;
     return unlikely(remaining < 0);
 }
@@ -243,26 +243,26 @@ static inline bool would_overflow(CborEncoder *encoder, size_t len)
 static inline void advance_ptr(CborEncoder *encoder, size_t n)
 {
     if (encoder->end)
-        encoder->ptr += n;
+        encoder->data.ptr += n;
     else
-        encoder->bytes_needed += n;
+        encoder->data.bytes_needed += n;
 }
 
 static inline CborError append_to_buffer(CborEncoder *encoder, const void *data, size_t len)
 {
     if (would_overflow(encoder, len)) {
         if (encoder->end != NULL) {
-            len -= encoder->end - encoder->ptr;
+            len -= encoder->end - encoder->data.ptr;
             encoder->end = NULL;
-            encoder->bytes_needed = 0;
+            encoder->data.bytes_needed = 0;
         }
 
         advance_ptr(encoder, len);
         return CborErrorOutOfMemory;
     }
 
-    memcpy(encoder->ptr, data, len);
-    encoder->ptr += len;
+    memcpy(encoder->data.ptr, data, len);
+    encoder->data.ptr += len;
     return CborNoError;
 }
 
@@ -447,7 +447,7 @@ __attribute__((noinline))
 static CborError create_container(CborEncoder *encoder, CborEncoder *container, size_t length, uint8_t shiftedMajorType)
 {
     CborError err;
-    container->ptr = encoder->ptr;
+    container->data.ptr = encoder->data.ptr;
     container->end = encoder->end;
     ++encoder->added;
     container->added = 0;
@@ -527,9 +527,9 @@ CborError cbor_encoder_create_map(CborEncoder *encoder, CborEncoder *mapEncoder,
 CborError cbor_encoder_close_container(CborEncoder *encoder, const CborEncoder *containerEncoder)
 {
     if (encoder->end)
-        encoder->ptr = containerEncoder->ptr;
+        encoder->data.ptr = containerEncoder->data.ptr;
     else
-        encoder->bytes_needed = containerEncoder->bytes_needed;
+        encoder->data.bytes_needed = containerEncoder->data.bytes_needed;
     encoder->end = containerEncoder->end;
     if (containerEncoder->flags & CborIteratorFlag_UnknownLength)
         return append_byte_to_buffer(encoder, BreakByte);
diff --git a/src/cborencoder_close_container_checked.c b/src/cborencoder_close_container_checked.c
index cdabbc6..0f341f5 100644
--- a/src/cborencoder_close_container_checked.c
+++ b/src/cborencoder_close_container_checked.c
@@ -55,14 +55,14 @@
  */
 CborError cbor_encoder_close_container_checked(CborEncoder *encoder, const CborEncoder *containerEncoder)
 {
-    const uint8_t *ptr = encoder->ptr;
+    const uint8_t *ptr = encoder->data.ptr;
     CborError err = cbor_encoder_close_container(encoder, containerEncoder);
     if (containerEncoder->flags & CborIteratorFlag_UnknownLength || encoder->end == NULL)
         return err;
 
     /* check what the original length was */
     uint64_t actually_added;
-    err = extract_number(&ptr, encoder->ptr, &actually_added);
+    err = extract_number(&ptr, encoder->data.ptr, &actually_added);
     if (err)
         return err;
 
diff --git a/tools/json2cbor/json2cbor.c b/tools/json2cbor/json2cbor.c
index fe7b389..96d3ed1 100644
--- a/tools/json2cbor/json2cbor.c
+++ b/tools/json2cbor/json2cbor.c
@@ -332,7 +332,7 @@ CborError decode_json(cJSON *json, CborEncoder *encoder)
                 return err;
 
             *encoder = container;   // restore state
-            encoder->ptr = newbuffer + (container.ptr - buffer);
+            encoder->data.ptr = newbuffer + (container.data.ptr - buffer);
             encoder->end = newbuffer + buffersize;
             buffer = newbuffer;
             goto encode_double;
@@ -483,7 +483,7 @@ int main(int argc, char **argv)
         return EXIT_FAILURE;
     }
 
-    fwrite(buffer, 1, encoder.ptr - buffer, stdout);
+    fwrite(buffer, 1, encoder.data.ptr - buffer, stdout);
     free(buffer);
     return EXIT_SUCCESS;
 }
