checkpoint work on arrays
diff --git a/src/qcbor_decode.c b/src/qcbor_decode.c
index 223cc7f..d7b3958 100644
--- a/src/qcbor_decode.c
+++ b/src/qcbor_decode.c
@@ -5922,34 +5922,145 @@
 
 
 
-/* The one that matches the endianness should return without copying
- The other either has to copy or has to swap by violating const-ness.
- If copying, the caller has to know. If violating const-ness, then
-the caller only knows that const-ness is violated. */
 
-void QCBORDecode_GetUint32ArrayBE(QCBORDecodeContext *pMe,
+
+
+typedef enum {
+   QCBOR_IS_BIG_ENDIAN = 0,
+   QCBOR_IS_LITTLE_ENDIAN = 1,
+   QCBOR_UNKNOWN_ENDIAN = 2
+} QCBOREndianness;
+
+void QCBORDecode_GetUint32Array(QCBORDecodeContext *pMe,
                                 uint8_t             uTagRequirement,
-                                uint32_t           *pInts,
-                                size_t             *pSize)
+                                uint32_t          **puUIntsDecoded,
+                                size_t             *puSizeDecoded,
+                                QCBOREndianness    *puEndianness)
 {
-   QCBORItem item;
-   QCBORDecode_VGetNext(pMe, &item);
+   QCBORItem Item;
+   QCBORDecode_VGetNext(pMe, &Item);
 
-   if(item.uDataType != QCBOR_TYPE_BYTE_STRING ||
-      !QCBORDecode_IsTagged(pMe, &item, 99)) { // TODO: correct tag number
+   if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
       pMe->uLastError = QCBOR_ERR_UNEXPECTED_TYPE;
       return;
    }
 
-   if(item.val.string.len % 4 != 0) {
-      pMe->uLastError = 99; // TODO: figure out error
+   const TagSpecification TagSpec =
+      {
+         uTagRequirement,
+         {CBOR_TAG_UINT32_BIG_ENDIAN_ARRAY, CBOR_TAG_UINT32_LITTLE_ENDIAN_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE},
+         {QCBOR_TYPE_BYTE_STRING, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}
+      };
+
+   pMe->uLastError = (uint8_t)CheckTagRequirement(TagSpec, &Item);
+   if(pMe->uLastError) {
       return;
    }
 
-   *pSize = item.val.string.len / 4;
+   if(QCBORDecode_IsTagged(pMe, &Item, CBOR_TAG_UINT32_BIG_ENDIAN_ARRAY)) {
+      *puEndianness = QCBOR_IS_BIG_ENDIAN;
+   } else if(QCBORDecode_IsTagged(pMe, &Item, CBOR_TAG_UINT32_LITTLE_ENDIAN_ARRAY)) {
+      *puEndianness = QCBOR_IS_LITTLE_ENDIAN;
+   } else {
+      *puEndianness = QCBOR_UNKNOWN_ENDIAN;
+   }
 
-   /* may have to swap if endianness doesn't match */
-   pInts = (uint32_t *) &(item.val.string.ptr);
+
+   if(Item.val.string.len % sizeof(uint32_t) != 0) {
+      pMe->uLastError = QCBOR_ERR_INPUT_SIZE_MULTIPLE;
+      return;
+   }
+
+   *puSizeDecoded = Item.val.string.len / sizeof(uint32_t);
+
+   *puUIntsDecoded = (uint32_t *)(Item.val.string.ptr);
+}
+
+
+
+
+
+// positive values are QCBOR error codes
+#define PERFORM_SWAP -1
+#define DO_NOT_SWAP 0
+static int
+ShallWeSwap(QCBOREndianRequirement uSwapRequirement, QCBOREndianness uEndianness)
+{
+   /* Asked to always swap */
+   if(uSwapRequirement == QCBOR_ENDIAN_SWAP) {
+      return PERFORM_SWAP;
+   }
+
+   /* Asked to never swap */
+   if(uSwapRequirement == QCBOR_ENDIAN_NO_SWAP) {
+      return DO_NOT_SWAP;
+   }
+
+   /* The following use cases require the input endianness to be known */
+   if(uEndianness == QCBOR_UNKNOWN_ENDIAN) {
+      return QCBOR_ERR_INPUT_ENDIANNESS_UNKNOWN;
+   }
+
+   QCBOREndianRequirement uOutput = uSwapRequirement;
+   if(uOutput == QCBOR_ENDIAN_MATCH_ENDIANNESS) {
+#if defined(USEFULBUF_CONFIG_BIG_ENDIAN)
+      uOutput = QCBOR_ENDIAN_BIG_ENDIAN;
+#else
+      uOutput = QCBOR_ENDIAN_LITTLE_ENDIAN;
+#endif
+   }
+
+   if((QCBOREndianness)uOutput != uEndianness) {
+      return PERFORM_SWAP;
+   } else {
+      return DO_NOT_SWAP;
+   }
+}
+
+
+void QCBORDecode_GetUint32ArrayCopy(QCBORDecodeContext    *pMe,
+                                    uint8_t                uTagRequirement,
+                                    QCBOREndianRequirement uSwapRequirement,
+                                    size_t                 uBufferSize,
+                                    uint32_t              *puUIntsBuffer,
+                                    size_t                *puReturnedSize)
+{
+   size_t           uReceivedArraySize;
+   uint32_t        *puReceivedArray;
+   QCBOREndianness  uEndianness;
+
+   QCBORDecode_GetUint32Array(pMe,
+                              uTagRequirement,
+                             &puReceivedArray,
+                             &uReceivedArraySize,
+                             &uEndianness);
+   if(pMe->uLastError) {
+      return;
+   }
+
+
+   if(uBufferSize < uReceivedArraySize) {
+      /* Given buffer is too small */
+      pMe->uLastError = QCBOR_ERR_BUFFER_TOO_SMALL;
+      return;
+   }
+
+   memcpy(puUIntsBuffer, puReceivedArray, uReceivedArraySize * sizeof(uint32_t));
+
+   int nSwapRequest = ShallWeSwap(uSwapRequirement, uEndianness);
+
+   if(nSwapRequest > 0) {
+      /* Error figuring out whether to swap or not. */
+      pMe->uLastError = (uint8_t)nSwapRequest;
+      return;
+   }
+
+   if(nSwapRequest == PERFORM_SWAP) {
+      for(size_t i = 0; i < uReceivedArraySize; i++) {
+         const uint32_t uArrayElement = USEFUL_SWAP32(puUIntsBuffer[i]);
+         puUIntsBuffer[i] = uArrayElement;
+      }
+   }
 }
 
 
@@ -5966,7 +6077,15 @@
 
    *pSize = item.val.string.len / 4;
 
-   // TODO: must swap all the ints
+#if defined(USEFULBUF_CONFIG_BIG_ENDIAN)
+   /* Must swap all the unsigned integers */
+   uint32_t *pUIntStart = item.val.string.ptr;
+   uint32_t *pUIntEnd   = pUIntStart + *pSize;
+   for(uint32_t *pUInt = pUIntStart; pUInt < pUIntEnd; pUInt++) {
+      const uint32_t u = USEFUL_SWAP32(*pUInt);
+      *pUInt = u;
+   }
+#endif
 
    pInts = (uint32_t *) &(item.val.string.ptr);
 
@@ -6043,18 +6162,14 @@
 }
 
 
-
-
-void QCBORDecode_GetHomogenousArray(QCBORDecodeContext *pMe,
+void ProcessHomogenousArray(QCBORDecodeContext *pMe,
+                            const QCBORItem *pItem,
                                     uint8_t             uTagRequirement,
                                     uint8_t             uType,
                                     size_t              nInArrayCount,
                                     union QCBORHomogenousArray array,
                                     size_t             *pnOutArrayCount)
 {
-   QCBORItem item;
-   QCBORDecode_GetNext(pMe, &item);
-
    const uint64_t puAllowedTags[] = {CBOR_TAG_HOMOGENEOUS_ARRAY, CBOR_TAG_INVALID64};
 
    const uint8_t puAllowedContents[] = {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE};
@@ -6064,13 +6179,15 @@
                                             uTagRequirement,
                                             puAllowedTags,
                                             puAllowedContents,
-                                            &item);
+                                            pItem);
    if(uError) {
       pMe->uLastError = (uint8_t)uError;
       goto Done;
    }
 
-   const uint8_t uIntNestLevel = item.uNextNestLevel;
+   const uint8_t uIntNestLevel = pItem->uNextNestLevel;
+   QCBORItem item;
+
 
    size_t uIntCount = 0;
    do {
@@ -6125,3 +6242,80 @@
    return;
 }
 
+
+void QCBORDecode_GetHomogenousArray(QCBORDecodeContext *pMe,
+                                    uint8_t             uTagRequirement,
+                                    uint8_t             uType,
+                                    size_t              nInArrayCount,
+                                    union QCBORHomogenousArray array,
+                                    size_t             *pnOutArrayCount)
+{
+   QCBORItem item;
+   QCBORDecode_VGetNext(pMe, &item);
+   if(pMe->uLastError != QCBOR_SUCCESS) {
+      return;
+   }
+
+   ProcessHomogenousArray(pMe,
+                          &item,
+                          uTagRequirement,
+                          uType,
+                          nInArrayCount,
+                          array,
+                          pnOutArrayCount);
+}
+
+void QCBORDecode_GetHomogenousArrayInMapN(QCBORDecodeContext *pMe,
+                                          int64_t             nLabel,
+                                          uint8_t             uTagRequirement,
+                                          uint8_t             uType,
+                                          size_t              nInArrayCount,
+                                          union QCBORHomogenousArray array,
+                                          size_t             *pnOutArrayCount)
+{
+   if(pMe->uLastError != QCBOR_SUCCESS) {
+      return;
+   }
+
+   QCBORItem Item;
+   QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item);
+   if(pMe->uLastError != QCBOR_SUCCESS) {
+      return;
+   }
+
+   ProcessHomogenousArray(pMe,
+                          &Item,
+                          uTagRequirement,
+                          uType,
+                          nInArrayCount,
+                          array,
+                          pnOutArrayCount);
+}
+
+
+void QCBORDecode_GetHomogenousArrayInMapSZ(QCBORDecodeContext *pMe,
+                                           const char          *szLabel,
+                                           uint8_t             uTagRequirement,
+                                           uint8_t             uType,
+                                           size_t              nInArrayCount,
+                                           union QCBORHomogenousArray array,
+                                           size_t             *pnOutArrayCount)
+{
+   if(pMe->uLastError != QCBOR_SUCCESS) {
+      return;
+   }
+
+   QCBORItem Item;
+   QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item);
+   if(pMe->uLastError != QCBOR_SUCCESS) {
+      return;
+   }
+
+   ProcessHomogenousArray(pMe,
+                          &Item,
+                          uTagRequirement,
+                          uType,
+                          nInArrayCount,
+                          array,
+                          pnOutArrayCount);
+}
diff --git a/src/qcbor_encode.c b/src/qcbor_encode.c
index 55d51f6..2a3ae31 100644
--- a/src/qcbor_encode.c
+++ b/src/qcbor_encode.c
@@ -953,49 +953,43 @@
 
 
 
-static inline void UsefulOutBuf_InsertUint32LE(UsefulOutBuf *pMe,
-                                             uint32_t uInteger32,
-                                             size_t uPos)
+
+static inline void
+UsefulOutBuf_InsertUint32LittleEndian(UsefulOutBuf *pMe,
+                            uint32_t      uInteger32,
+                            size_t        uPos)
 {
    /* See UsefulOutBuf_InsertUint64() for comments on this code */
 
-    /* htonl is not used (but it is used for outputting BE)
+#if defined(USEFULBUF_CONFIG_BIG_ENDIAN)
+   const uint32_t uOut = USEFUL_SWAP32(uInteger32);
 
-   htonl will swap if CPU is little endian and do nothing if it is big endian
+#else /* Default is little endian; tests catch mis-configuration. */
+   const uint32_t uOut = uInteger32;
 
-     The opposite is needed.
+#endif /* big or little endian */
 
-     Could use htonl and then always swap the result, but
-     that doesn't seem any better than the default
-     shift and swap that always works.
-
-
-     */
-
-   const void *pBytes;
-
-#if defined(USEFULBUF_CONFIG_LITTLE_ENDIAN)
-   pBytes = &uInteger32;
-
-#elif defined(USEFULBUF_CONFIG_BIG_ENDIAN) && defined(USEFULBUF_CONFIG_BSWAP)
-   uint32_t uTmp = __builtin_bswap32(uInteger32);
-
-   pBytes = &uTmp;
-
-#else
-   uint8_t aTmp[4];
-
-   aTmp[0] = (uint8_t)((uInteger32 & 0xff) >> 24);
-   aTmp[1] = (uint8_t)((uInteger32 & 0xff00) >> 16);
-   aTmp[2] = (uint8_t)((uInteger32 & 0xff0000) >> 8);
-   aTmp[3] = (uint8_t)(uInteger32 & 0xff000000);
-
-   pBytes = aTmp;
-#endif
-
-   UsefulOutBuf_InsertData(pMe, pBytes, 4, uPos);
+   UsefulOutBuf_InsertData(pMe, &uOut, 4, uPos);
 }
 
+static inline void
+UsefulOutBuf_InsertUint32BE(UsefulOutBuf *pMe,
+                            uint32_t      uInteger32,
+                            size_t        uPos)
+{
+   /* See UsefulOutBuf_InsertUint64() for comments on this code */
+
+#if !defined(USEFULBUF_CONFIG_BIG_ENDIAN)
+   const uint32_t uOut = USEFUL_SWAP32(uInteger32);
+
+#else /* Default is little endian; tests catch mis-configuration. */
+   const uint32_t uOut = uInteger32;
+#endif /* big or little endian */
+
+   UsefulOutBuf_InsertData(pMe, &uOut, 4, uPos);
+}
+
+
 void
 QCBOREncode_AddUint32ArrayLittleEndian(QCBOREncodeContext *pMe,
                                          const uint32_t      array[],
@@ -1008,12 +1002,13 @@
    AppendCBORHead(pMe, CBOR_MAJOR_TYPE_BYTE_STRING, uNumBytes, 0);
 
    for(size_t n = 0; n < uArrayLen; n++) {
-      UsefulOutBuf_InsertUint32LE(&(pMe->OutBuf), array[n], 99); // TODO: correct position
+      UsefulOutBuf_InsertUint32LittleEndian(&(pMe->OutBuf), array[n], 99); // TODO: correct position
    }
 }
 
+
 void
-QCBOREncode_AddUint32ArrayBigEndian(QCBOREncodeContext *pMe,
+QCBOREncode_AddTypedArrayOfUInt32BigEndian(QCBOREncodeContext *pMe,
                                          const uint32_t      array[],
                                          size_t              uArrayLen)
 {
@@ -1024,21 +1019,21 @@
    AppendCBORHead(pMe, CBOR_MAJOR_TYPE_BYTE_STRING, uNumBytes, 0);
 
    for(size_t n = 0; n < uArrayLen; n++) {
-      UsefulOutBuf_AppendUint32(&(pMe->OutBuf), array[n]);
+      UsefulOutBuf_InsertUint32BE(&(pMe->OutBuf), array[n], 99);
    }
 }
 
 
-// Note that this will produce an array of major type 0 and 1,
-// but that is what is necessary to have an array of positive
-// and negative integers. The types allowed for a homogeneous
-// array are caller-defined, not strictly by CBOR major type
-// or such, so this is just fine.
+
+
+/*
+ * Public function to encode an array. See qcbor/qcbor_encode.h
+ */
 void
-QCBOREncode_AddArrayOfInts(QCBOREncodeContext *pMe,
-                           uint8_t             uTagRequirement,
-                           const int64_t      *puInts,
-                           size_t              uNumInts)
+QCBOREncode_AddArrayOfInt64s(QCBOREncodeContext *pMe,
+                             uint8_t             uTagRequirement,
+                             const int64_t       pInts[],
+                             size_t              uNumInts)
 {
    if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
       QCBOREncode_AddTag(pMe, CBOR_TAG_HOMOGENEOUS_ARRAY);
@@ -1046,34 +1041,40 @@
    QCBOREncode_OpenArray(pMe);
 
    for(size_t i = 0; i < uNumInts; i++) {
-      QCBOREncode_AddInt64(pMe, puInts[i]);
+      QCBOREncode_AddInt64(pMe, pInts[i]);
    }
    QCBOREncode_CloseArray(pMe);
 }
 
 
+/*
+ * Public function to encode an array. See qcbor/qcbor_encode.h
+ */
 void
-QCBOREncode_AddArrayOfUInts(QCBOREncodeContext *pMe,
-                            uint8_t             uTagRequirement,
-                            const uint64_t     *puInts,
-                            size_t              uNumInts)
+QCBOREncode_AddArrayOfUInt64s(QCBOREncodeContext *pMe,
+                              uint8_t             uTagRequirement,
+                              const uint64_t      puInts[],
+                              size_t              uNumUInts)
 {
    if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
       QCBOREncode_AddTag(pMe, CBOR_TAG_HOMOGENEOUS_ARRAY);
    }
    QCBOREncode_OpenArray(pMe);
 
-   for(size_t i = 0; i < uNumInts; i++) {
+   for(size_t i = 0; i < uNumUInts; i++) {
       QCBOREncode_AddUInt64(pMe, puInts[i]);
    }
    QCBOREncode_CloseArray(pMe);
 }
 
 
+/*
+ * Public function to encode an array. See qcbor/qcbor_encode.h
+ */
 void
 QCBOREncode_AddArrayOfDoubles(QCBOREncodeContext *pMe,
                               uint8_t             uTagRequirement,
-                              const double       *pdDoubles,
+                              const double        pdDoubles[],
                               size_t              uNumDoubles)
 {
    if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
@@ -1088,10 +1089,13 @@
 }
 
 
+/*
+ * Public function to encode an array. See qcbor/qcbor_encode.h
+ */
 void
 QCBOREncode_AddArrayOfByteStrings(QCBOREncodeContext *pMe,
                                   uint8_t             uTagRequirement,
-                                  const UsefulBufC   *pStrings,
+                                  const UsefulBufC    pStrings[],
                                   size_t              uNumStrings)
 {
    if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
@@ -1105,10 +1109,14 @@
    QCBOREncode_CloseArray(pMe);
 }
 
+
+/*
+ * Public function to encode an array. See qcbor/qcbor_encode.h
+ */
 void
 QCBOREncode_AddArrayOfTextStrings(QCBOREncodeContext *pMe,
                                   uint8_t             uTagRequirement,
-                                  const UsefulBufC   *pStrings,
+                                  const UsefulBufC    pStrings[],
                                   size_t              uNumStrings)
 {
    if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
@@ -1123,11 +1131,14 @@
 }
 
 
+/*
+ * Public function to encode an array. See qcbor/qcbor_encode.h
+ */
 void
-QCBOREncode_AddArrayOSZStrings(QCBOREncodeContext *pMe,
-                               uint8_t             uTagRequirement,
-                               const char        **pStrings,
-                               size_t              uNumStrings)
+QCBOREncode_AddArrayOfSZStrings(QCBOREncodeContext *pMe,
+                                uint8_t             uTagRequirement,
+                                const char         *pStrings[],
+                                size_t              uNumStrings)
 {
    if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
       QCBOREncode_AddTag(pMe, CBOR_TAG_HOMOGENEOUS_ARRAY);
@@ -1139,6 +1150,3 @@
    }
    QCBOREncode_CloseArray(pMe);
 }
-
-
-