From 409594639f15d825202971db7a275023e05772ff Mon Sep 17 00:00:00 2001
From: Adenilson Cavalcanti <adenilson.cavalcanti@arm.com>
Date: Tue, 28 Apr 2020 10:48:01 -0700
Subject: [PATCH] Local Changes:   - make C tests build as C++ code so we can
 use gtest.   - use gtest EXPECT_TRUE instead of C assert.   - replace C
 streams for C++ (portability issues).

---
 test/infcover.c | 167 ++++++++++++++++++++++++++----------------------
 1 file changed, 90 insertions(+), 77 deletions(-)

diff --git a/test/infcover.c b/test/infcover.c
index 2be0164..a8c51c7 100644
--- a/test/infcover.c
+++ b/test/infcover.c
@@ -4,11 +4,12 @@
  */
 
 /* to use, do: ./configure --cover && make cover */
-
+// clang-format off
+#include "infcover.h"
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <assert.h>
+
 #include "zlib.h"
 
 /* get definition of internal structure so we can mess with it (see pull()),
@@ -17,8 +18,22 @@
 #include "inftrees.h"
 #include "inflate.h"
 
+/* XXX: use C++ streams instead of printf/fputs/etc due to portability
+ * as type sizes can vary between platforms.
+ */
+#include <iostream>
 #define local static
 
+/* XXX: hacking C assert and plugging into GTest. */
+#include "gtest.h"
+#if defined(assert)
+#undef assert
+#define assert EXPECT_TRUE
+#endif
+
+/* XXX: handle what is a reserved word in C++. */
+#define try try_f
+
 /* -- memory tracking routines -- */
 
 /*
@@ -72,7 +87,7 @@ local void *mem_alloc(void *mem, unsigned count, unsigned size)
 {
     void *ptr;
     struct mem_item *item;
-    struct mem_zone *zone = mem;
+    struct mem_zone *zone = static_cast<struct mem_zone *>(mem);
     size_t len = count * (size_t)size;
 
     /* induced allocation failure */
@@ -87,7 +102,7 @@ local void *mem_alloc(void *mem, unsigned count, unsigned size)
     memset(ptr, 0xa5, len);
 
     /* create a new item for the list */
-    item = malloc(sizeof(struct mem_item));
+    item = static_cast<struct mem_item *>(malloc(sizeof(struct mem_item)));
     if (item == NULL) {
         free(ptr);
         return NULL;
@@ -112,7 +127,7 @@ local void *mem_alloc(void *mem, unsigned count, unsigned size)
 local void mem_free(void *mem, void *ptr)
 {
     struct mem_item *item, *next;
-    struct mem_zone *zone = mem;
+    struct mem_zone *zone = static_cast<struct mem_zone *>(mem);
 
     /* if no zone, just do a free */
     if (zone == NULL) {
@@ -159,7 +174,7 @@ local void mem_setup(z_stream *strm)
 {
     struct mem_zone *zone;
 
-    zone = malloc(sizeof(struct mem_zone));
+    zone = static_cast<struct mem_zone *>(malloc(sizeof(struct mem_zone)));
     assert(zone != NULL);
     zone->first = NULL;
     zone->total = 0;
@@ -175,33 +190,33 @@ local void mem_setup(z_stream *strm)
 /* set a limit on the total memory allocation, or 0 to remove the limit */
 local void mem_limit(z_stream *strm, size_t limit)
 {
-    struct mem_zone *zone = strm->opaque;
+    struct mem_zone *zone = static_cast<struct mem_zone *>(strm->opaque);
 
     zone->limit = limit;
 }
 
 /* show the current total requested allocations in bytes */
-local void mem_used(z_stream *strm, char *prefix)
+local void mem_used(z_stream *strm, const char *prefix)
 {
-    struct mem_zone *zone = strm->opaque;
+    struct mem_zone *zone = static_cast<struct mem_zone *>(strm->opaque);
 
-    fprintf(stderr, "%s: %lu allocated\n", prefix, zone->total);
+    std::cout << prefix << ": " << zone->total << " allocated" << std::endl;
 }
 
 /* show the high water allocation in bytes */
-local void mem_high(z_stream *strm, char *prefix)
+local void mem_high(z_stream *strm, const char *prefix)
 {
-    struct mem_zone *zone = strm->opaque;
+    struct mem_zone *zone = static_cast<struct mem_zone *>(strm->opaque);
 
-    fprintf(stderr, "%s: %lu high water mark\n", prefix, zone->highwater);
+    std::cout << prefix << ": " << zone->highwater << " high water mark" << std::endl;
 }
 
 /* release the memory allocation zone -- if there are any surprises, notify */
-local void mem_done(z_stream *strm, char *prefix)
+local void mem_done(z_stream *strm, const char *prefix)
 {
     int count = 0;
     struct mem_item *item, *next;
-    struct mem_zone *zone = strm->opaque;
+    struct mem_zone *zone = static_cast<struct mem_zone *>(strm->opaque);
 
     /* show high water mark */
     mem_high(strm, prefix);
@@ -218,13 +233,20 @@ local void mem_done(z_stream *strm, char *prefix)
 
     /* issue alerts about anything unexpected */
     if (count || zone->total)
-        fprintf(stderr, "** %s: %lu bytes in %d blocks not freed\n",
-                prefix, zone->total, count);
+        std::cout << "** " << prefix << ": "
+                  << zone->total << " bytes in "
+                  << count << " blocks not freed"
+                  << std::endl;
+
     if (zone->notlifo)
-        fprintf(stderr, "** %s: %d frees not LIFO\n", prefix, zone->notlifo);
+        std::cout << "** " << prefix << ": "
+                  << zone->notlifo << " frees not LIFO"
+                  << std::endl;
+
     if (zone->rogue)
-        fprintf(stderr, "** %s: %d frees not recognized\n",
-                prefix, zone->rogue);
+        std::cout << "** " << prefix << ": "
+                  << zone->rogue << " frees not recognized"
+                  << std::endl;
 
     /* free the zone and delete from the stream */
     free(zone);
@@ -247,7 +269,7 @@ local unsigned char *h2b(const char *hex, unsigned *len)
     unsigned char *in, *re;
     unsigned next, val;
 
-    in = malloc((strlen(hex) + 1) >> 1);
+    in = static_cast<unsigned char *>(malloc((strlen(hex) + 1) >> 1));
     if (in == NULL)
         return NULL;
     next = 0;
@@ -268,7 +290,7 @@ local unsigned char *h2b(const char *hex, unsigned *len)
     } while (*hex++);       /* go through the loop with the terminating null */
     if (len != NULL)
         *len = next;
-    re = realloc(in, next);
+    re = static_cast<unsigned char *>(realloc(in, next));
     return re == NULL ? in : re;
 }
 
@@ -281,7 +303,7 @@ local unsigned char *h2b(const char *hex, unsigned *len)
    header information is collected with inflateGetHeader().  If a zlib stream
    is looking for a dictionary, then an empty dictionary is provided.
    inflate() is run until all of the input data is consumed. */
-local void inf(char *hex, char *what, unsigned step, int win, unsigned len,
+local void inf(const char *hex, const char *what, unsigned step, int win, unsigned len,
                int err)
 {
     int ret;
@@ -298,7 +320,7 @@ local void inf(char *hex, char *what, unsigned step, int win, unsigned len,
         mem_done(&strm, what);
         return;
     }
-    out = malloc(len);                          assert(out != NULL);
+    out = static_cast<unsigned char *>(malloc(len));                          assert(out != NULL);
     if (win == 47) {
         head.extra = out;
         head.extra_max = len;
@@ -347,7 +369,7 @@ local void inf(char *hex, char *what, unsigned step, int win, unsigned len,
 }
 
 /* cover all of the lines in inflate.c up to inflate() */
-local void cover_support(void)
+void cover_support(void)
 {
     int ret;
     z_stream strm;
@@ -381,11 +403,11 @@ local void cover_support(void)
     strm.next_in = Z_NULL;
     ret = inflateInit(&strm);                   assert(ret == Z_OK);
     ret = inflateEnd(&strm);                    assert(ret == Z_OK);
-    fputs("inflate built-in memory routines\n", stderr);
+    std::cout << "inflate built-in memory routines" << std::endl;;
 }
 
 /* cover all inflate() header and trailer cases and code after inflate() */
-local void cover_wrap(void)
+void cover_wrap(void)
 {
     int ret;
     z_stream strm, copy;
@@ -394,7 +416,7 @@ local void cover_wrap(void)
     ret = inflate(Z_NULL, 0);                   assert(ret == Z_STREAM_ERROR);
     ret = inflateEnd(Z_NULL);                   assert(ret == Z_STREAM_ERROR);
     ret = inflateCopy(Z_NULL, Z_NULL);          assert(ret == Z_STREAM_ERROR);
-    fputs("inflate bad parameters\n", stderr);
+    std::cout << "inflate bad parameters" << std::endl;
 
     inf("1f 8b 0 0", "bad gzip method", 0, 31, 0, Z_DATA_ERROR);
     inf("1f 8b 8 80", "bad gzip flags", 0, 31, 0, Z_DATA_ERROR);
@@ -415,9 +437,9 @@ local void cover_wrap(void)
     strm.next_in = Z_NULL;
     ret = inflateInit2(&strm, -8);
     strm.avail_in = 2;
-    strm.next_in = (void *)"\x63";
+    strm.next_in = (Bytef *)"\x63";
     strm.avail_out = 1;
-    strm.next_out = (void *)&ret;
+    strm.next_out = (Bytef *)&ret;
     mem_limit(&strm, 1);
     ret = inflate(&strm, Z_NO_FLUSH);           assert(ret == Z_MEM_ERROR);
     ret = inflate(&strm, Z_NO_FLUSH);           assert(ret == Z_MEM_ERROR);
@@ -428,11 +450,11 @@ local void cover_wrap(void)
     mem_limit(&strm, (sizeof(struct inflate_state) << 1) + 256);
     ret = inflatePrime(&strm, 16, 0);           assert(ret == Z_OK);
     strm.avail_in = 2;
-    strm.next_in = (void *)"\x80";
+    strm.next_in = (Bytef *)"\x80";
     ret = inflateSync(&strm);                   assert(ret == Z_DATA_ERROR);
     ret = inflate(&strm, Z_NO_FLUSH);           assert(ret == Z_STREAM_ERROR);
     strm.avail_in = 4;
-    strm.next_in = (void *)"\0\0\xff\xff";
+    strm.next_in = (Bytef *)"\0\0\xff\xff";
     ret = inflateSync(&strm);                   assert(ret == Z_OK);
     (void)inflateSyncPoint(&strm);
     ret = inflateCopy(&copy, &strm);            assert(ret == Z_MEM_ERROR);
@@ -454,7 +476,7 @@ local unsigned pull(void *desc, unsigned char **buf)
         next = 0;
         return 0;   /* no input (already provided at next_in) */
     }
-    state = (void *)((z_stream *)desc)->state;
+    state = reinterpret_cast<struct inflate_state *>(((z_stream *)desc)->state);
     if (state != Z_NULL)
         state->mode = SYNC;     /* force an otherwise impossible situation */
     return next < sizeof(dat) ? (*buf = dat + next++, 1) : 0;
@@ -467,7 +489,7 @@ local int push(void *desc, unsigned char *buf, unsigned len)
 }
 
 /* cover inflateBack() up to common deflate data cases and after those */
-local void cover_back(void)
+void cover_back(void)
 {
     int ret;
     z_stream strm;
@@ -479,17 +501,17 @@ local void cover_back(void)
     ret = inflateBack(Z_NULL, Z_NULL, Z_NULL, Z_NULL, Z_NULL);
                                                 assert(ret == Z_STREAM_ERROR);
     ret = inflateBackEnd(Z_NULL);               assert(ret == Z_STREAM_ERROR);
-    fputs("inflateBack bad parameters\n", stderr);
+    std::cout << "inflateBack bad parameters" << std::endl;;
 
     mem_setup(&strm);
     ret = inflateBackInit(&strm, 15, win);      assert(ret == Z_OK);
     strm.avail_in = 2;
-    strm.next_in = (void *)"\x03";
+    strm.next_in = (Bytef *)"\x03";
     ret = inflateBack(&strm, pull, Z_NULL, push, Z_NULL);
                                                 assert(ret == Z_STREAM_END);
         /* force output error */
     strm.avail_in = 3;
-    strm.next_in = (void *)"\x63\x00";
+    strm.next_in = (Bytef *)"\x63\x00";
     ret = inflateBack(&strm, pull, Z_NULL, push, &strm);
                                                 assert(ret == Z_BUF_ERROR);
         /* force mode error by mucking with state */
@@ -500,11 +522,11 @@ local void cover_back(void)
 
     ret = inflateBackInit(&strm, 15, win);      assert(ret == Z_OK);
     ret = inflateBackEnd(&strm);                assert(ret == Z_OK);
-    fputs("inflateBack built-in memory routines\n", stderr);
+    std::cout << "inflateBack built-in memory routines" << std::endl;;
 }
 
 /* do a raw inflate of data in hexadecimal with both inflate and inflateBack */
-local int try(char *hex, char *id, int err)
+local int try(const char *hex, const char *id, int err)
 {
     int ret;
     unsigned len, size;
@@ -518,11 +540,11 @@ local int try(char *hex, char *id, int err)
 
     /* allocate work areas */
     size = len << 3;
-    out = malloc(size);
+    out = static_cast<unsigned char *>(malloc(size));
     assert(out != NULL);
-    win = malloc(32768);
+    win = static_cast<unsigned char *>(malloc(32768));
     assert(win != NULL);
-    prefix = malloc(strlen(id) + 6);
+    prefix = static_cast<char *>(malloc(strlen(id) + 6));
     assert(prefix != NULL);
 
     /* first with inflate */
@@ -578,7 +600,7 @@ local int try(char *hex, char *id, int err)
 }
 
 /* cover deflate data cases in both inflate() and inflateBack() */
-local void cover_inflate(void)
+void cover_inflate(void)
 {
     try("0 0 0 0 0", "invalid stored block lengths", 1);
     try("3 0", "fixed", 0);
@@ -613,32 +635,33 @@ local void cover_inflate(void)
     inf("63 18 5 40 c 0", "window wrap", 3, -8, 300, Z_OK);
 }
 
+/* XXX(cavalcantii): fix linking error due inflate_table. */
 /* cover remaining lines in inftrees.c */
-local void cover_trees(void)
-{
-    int ret;
-    unsigned bits;
-    unsigned short lens[16], work[16];
-    code *next, table[ENOUGH_DISTS];
-
-    /* we need to call inflate_table() directly in order to manifest not-
-       enough errors, since zlib insures that enough is always enough */
-    for (bits = 0; bits < 15; bits++)
-        lens[bits] = (unsigned short)(bits + 1);
-    lens[15] = 15;
-    next = table;
-    bits = 15;
-    ret = inflate_table(DISTS, lens, 16, &next, &bits, work);
-                                                assert(ret == 1);
-    next = table;
-    bits = 1;
-    ret = inflate_table(DISTS, lens, 16, &next, &bits, work);
-                                                assert(ret == 1);
-    fputs("inflate_table not enough errors\n", stderr);
-}
+/* void cover_trees(void) */
+/* { */
+/*     int ret; */
+/*     unsigned bits; */
+/*     unsigned short lens[16], work[16]; */
+/*     code *next, table[ENOUGH_DISTS]; */
+
+/*     /\* we need to call inflate_table() directly in order to manifest not- */
+/*        enough errors, since zlib insures that enough is always enough *\/ */
+/*     for (bits = 0; bits < 15; bits++) */
+/*         lens[bits] = (unsigned short)(bits + 1); */
+/*     lens[15] = 15; */
+/*     next = table; */
+/*     bits = 15; */
+/*     ret = inflate_table(DISTS, lens, 16, &next, &bits, work); */
+/*                                                 assert(ret == 1); */
+/*     next = table; */
+/*     bits = 1; */
+/*     ret = inflate_table(DISTS, lens, 16, &next, &bits, work); */
+/*                                                 assert(ret == 1); */
+/*     fputs("inflate_table not enough errors\n", stderr); */
+/* } */
 
 /* cover remaining inffast.c decoding and window copying */
-local void cover_fast(void)
+void cover_fast(void)
 {
     inf("e5 e0 81 ad 6d cb b2 2c c9 01 1e 59 63 ae 7d ee fb 4d fd b5 35 41 68"
         " ff 7f 0f 0 0 0", "fast length extra bits", 0, -8, 258, Z_DATA_ERROR);
@@ -658,14 +681,4 @@ local void cover_fast(void)
         Z_STREAM_END);
 }
 
-int main(void)
-{
-    fprintf(stderr, "%s\n", zlibVersion());
-    cover_support();
-    cover_wrap();
-    cover_back();
-    cover_inflate();
-    cover_trees();
-    cover_fast();
-    return 0;
-}
+// clang-format on
-- 
2.21.1 (Apple Git-122.3)