Blame SOURCES/genwqe-user-4.0.18-upstream.patch

b38c1b
diff --git a/.travis.yml b/.travis.yml
b38c1b
index 4157956..5d324e0 100644
b38c1b
--- a/.travis.yml
b38c1b
+++ b/.travis.yml
b38c1b
@@ -3,8 +3,14 @@ dist: trusty
b38c1b
 language: c
b38c1b
 compiler: gcc
b38c1b
 before_install:
b38c1b
+  - sudo dpkg --add-architecture i386
b38c1b
   - sudo apt-get -qq update
b38c1b
-  - sudo apt-get --assume-yes install gcc help2man git make zlib1g-dev
b38c1b
+  - sudo apt-cache search zlib
b38c1b
+  - sudo apt-get --assume-yes install gcc help2man git make zlib1g-dev libc6-dev-i386 zlib1g-dev:i386
b38c1b
+  - ls -l /usr/include/z*.h
b38c1b
 script:
b38c1b
-  - make
b38c1b
+  - make FORCE_32BIT=1 V=2 -j4
b38c1b
+  - make clean
b38c1b
+  - make -j4
b38c1b
   - make test_software
b38c1b
+  - make clean
b38c1b
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
b38c1b
new file mode 100644
b38c1b
index 0000000..500eaad
b38c1b
--- /dev/null
b38c1b
+++ b/CONTRIBUTING.md
b38c1b
@@ -0,0 +1,28 @@
b38c1b
+Developer's Certificate of Origin 1.1
b38c1b
+
b38c1b
+       By making a contribution to this project, I certify that:
b38c1b
+
b38c1b
+       (a) The contribution was created in whole or in part by me and I
b38c1b
+           have the right to submit it under the open source license
b38c1b
+           indicated in the file; or
b38c1b
+
b38c1b
+       (b) The contribution is based upon previous work that, to the best
b38c1b
+           of my knowledge, is covered under an appropriate open source
b38c1b
+           license and I have the right under that license to submit that
b38c1b
+           work with modifications, whether created in whole or in part
b38c1b
+           by me, under the same open source license (unless I am
b38c1b
+           permitted to submit under a different license), as indicated
b38c1b
+           in the file; or
b38c1b
+
b38c1b
+       (c) The contribution was provided directly to me by some other
b38c1b
+           person who certified (a), (b) or (c) and I have not modified
b38c1b
+           it.
b38c1b
+
b38c1b
+       (d) I understand and agree that this project and the contribution
b38c1b
+           are public and that a record of the contribution (including all
b38c1b
+           personal information I submit with it, including my sign-off) is
b38c1b
+           maintained indefinitely and may be redistributed consistent with
b38c1b
+           this project or the open source license(s) involved.
b38c1b
+
b38c1b
+The developer sign-off should include the reference to the DCO (example below):
b38c1b
+DCO 1.1 Signed-off-by: Random J Developer <random@developer.org>
b38c1b
diff --git a/include/deflate_fifo.h b/include/deflate_fifo.h
b38c1b
index 5415c29..0bbe397 100644
b38c1b
--- a/include/deflate_fifo.h
b38c1b
+++ b/include/deflate_fifo.h
b38c1b
@@ -38,6 +38,10 @@
b38c1b
 #define ZEDC_FIFO_SIZE		256
b38c1b
 #define ZEDC_FIFO_MASK		(ZEDC_FIFO_SIZE - 1)
b38c1b
 
b38c1b
+#ifndef ARRAY_SIZE
b38c1b
+#  define ARRAY_SIZE(a)		(sizeof((a)) / sizeof((a)[0]))
b38c1b
+#endif
b38c1b
+
b38c1b
 struct zedc_fifo {
b38c1b
 	unsigned int	push;	/* push into FIFO here */
b38c1b
 	unsigned int	pop;	/* pop from FIFO here */
b38c1b
@@ -83,11 +87,11 @@ static inline int fifo_push32(struct zedc_fifo *fifo, uint32_t data)
b38c1b
 		uint8_t u8[4];
b38c1b
 	} d;
b38c1b
 
b38c1b
-	if (fifo_free(fifo) < 4)
b38c1b
+	if (fifo_free(fifo) < ARRAY_SIZE(d.u8))
b38c1b
 		return 0;
b38c1b
 
b38c1b
 	d.u32 = data;
b38c1b
-	for (i = 0; i < 4; i++) {
b38c1b
+	for (i = 0; i < ARRAY_SIZE(d.u8); i++) {
b38c1b
 		fifo->fifo[fifo->push] = d.u8[i];
b38c1b
 		fifo->push = (fifo->push + 1) & ZEDC_FIFO_MASK;
b38c1b
 	}
b38c1b
@@ -112,10 +116,10 @@ static inline int fifo_pop16(struct zedc_fifo *fifo, uint16_t *data)
b38c1b
 		uint8_t u8[2];
b38c1b
 	} d;
b38c1b
 
b38c1b
-	if (fifo_used(fifo) < 2)
b38c1b
+	if (fifo_used(fifo) < ARRAY_SIZE(d.u8))
b38c1b
 		return 0;
b38c1b
 
b38c1b
-	for (i = 0; i < 4; i++)
b38c1b
+	for (i = 0; i < ARRAY_SIZE(d.u8); i++)
b38c1b
 		fifo_pop(fifo, &d.u8[i]);
b38c1b
 
b38c1b
 	*data = d.u16;
b38c1b
diff --git a/lib/ddcb_capi.c b/lib/ddcb_capi.c
b38c1b
index 323b2ca..9289996 100644
b38c1b
--- a/lib/ddcb_capi.c
b38c1b
+++ b/lib/ddcb_capi.c
b38c1b
@@ -432,8 +432,9 @@ static int __afu_open(struct dev_ctx *ctx)
b38c1b
 	rc = cxl_get_cr_device(ctx->afu_h, 0, &ctx->cr_device);
b38c1b
 	if (rc == 0) {
b38c1b
 		if (ctx->cr_device != CGZIP_CR_DEVICE) {
b38c1b
-			VERBOSE0(" [%s] ERR: device_id: %ld/%d\n",
b38c1b
-				 __func__, (unsigned long)ctx->cr_device,
b38c1b
+			VERBOSE1(" [%s] WARNING: device_id: %ld/%d "
b38c1b
+				 "skipping, no CGZIP card\n", __func__,
b38c1b
+				 (unsigned long)ctx->cr_device,
b38c1b
 				 CGZIP_CR_VENDOR);
b38c1b
 			rc = DDCB_ERR_CARD;
b38c1b
 			goto err_afu_free;
b38c1b
diff --git a/lib/ddcb_card.c b/lib/ddcb_card.c
b38c1b
index d1ee60a..bffe230 100644
b38c1b
--- a/lib/ddcb_card.c
b38c1b
+++ b/lib/ddcb_card.c
b38c1b
@@ -109,7 +109,7 @@ static uint64_t _card_get_frequency(void *card_data)
b38c1b
 	if (speed >= ARRAY_SIZE(speed_grade))
b38c1b
 		return 0;       /* illegal value */
b38c1b
 
b38c1b
-	return speed_grade[speed] * 1000000;  /* in Hz */
b38c1b
+	return speed_grade[speed] * (uint64_t)1000000;  /* in Hz */
b38c1b
 }
b38c1b
 
b38c1b
 static void card_dump_hardware_version(void *card_data, FILE *fp)
b38c1b
diff --git a/lib/deflate.c b/lib/deflate.c
b38c1b
index f1e4ddf..4299878 100644
b38c1b
--- a/lib/deflate.c
b38c1b
+++ b/lib/deflate.c
b38c1b
@@ -292,6 +292,9 @@ int zedc_deflateSetDictionary(zedc_streamp strm,
b38c1b
 	if (dictLength > ZEDC_DICT_LEN)
b38c1b
 		return ZEDC_STREAM_ERROR;
b38c1b
 
b38c1b
+	if (dictionary == NULL)
b38c1b
+		return ZEDC_STREAM_ERROR;
b38c1b
+
b38c1b
 	memcpy(&strm->wsp->dict[0], dictionary, dictLength);
b38c1b
 	strm->dict_len = dictLength;
b38c1b
 	strm->dict_adler32 = __adler32(1, dictionary, dictLength);
b38c1b
diff --git a/lib/hardware.c b/lib/hardware.c
b38c1b
index f7631b7..e695118 100644
b38c1b
--- a/lib/hardware.c
b38c1b
+++ b/lib/hardware.c
b38c1b
@@ -99,8 +99,10 @@ static int zlib_xcheck = 1;
b38c1b
 static unsigned int zlib_ibuf_total = CONFIG_DEFLATE_BUF_SIZE;
b38c1b
 static unsigned int zlib_obuf_total = CONFIG_INFLATE_BUF_SIZE;
b38c1b
 
b38c1b
+#define ZEDC_CARDS_LENGTH 128
b38c1b
+
b38c1b
 /* Try to cache filehandles for faster access. Do not close them. */
b38c1b
-static zedc_handle_t zedc_cards[128 + 1];
b38c1b
+static zedc_handle_t zedc_cards[ZEDC_CARDS_LENGTH + 1];
b38c1b
 
b38c1b
 static zedc_handle_t __zedc_open(int card_no, int card_type, int mode,
b38c1b
 				 int *err_code)
b38c1b
@@ -112,15 +114,15 @@ static zedc_handle_t __zedc_open(int card_no, int card_type, int mode,
b38c1b
 				 err_code);
b38c1b
 
b38c1b
 	if (card_no == -1) {
b38c1b
-		if (zedc_cards[128])
b38c1b
-			return zedc_cards[128];
b38c1b
+		if (zedc_cards[ZEDC_CARDS_LENGTH])
b38c1b
+			return zedc_cards[ZEDC_CARDS_LENGTH];
b38c1b
 
b38c1b
-		zedc_cards[128] = zedc_open(card_no, card_type, mode,
b38c1b
+		zedc_cards[ZEDC_CARDS_LENGTH] = zedc_open(card_no, card_type, mode,
b38c1b
 					    err_code);
b38c1b
-		return zedc_cards[128];
b38c1b
+		return zedc_cards[ZEDC_CARDS_LENGTH];
b38c1b
 	}
b38c1b
 
b38c1b
-	if (card_no < 0 || card_no >= 128)
b38c1b
+	if (card_no < 0 || card_no >= ZEDC_CARDS_LENGTH)
b38c1b
 		return NULL;
b38c1b
 
b38c1b
 	if (zedc_cards[card_no] != NULL) {
b38c1b
@@ -169,11 +171,29 @@ static void stream_zlib_to_zedc(zedc_streamp h, z_streamp s)
b38c1b
 /**
b38c1b
  * Take care CRC/ADLER is correctly reported to the upper levels.
b38c1b
  */
b38c1b
-static void __fixup_crc_or_adler( z_streamp s, zedc_streamp h)
b38c1b
+static void __fixup_crc_or_adler(z_streamp s, zedc_streamp h)
b38c1b
 {
b38c1b
 	s->adler = (h->format == ZEDC_FORMAT_GZIP) ? h->crc32 : h->adler32;
b38c1b
 }
b38c1b
 
b38c1b
+/**
b38c1b
+ * See #152 The adler32 start value is 1 according to the specification.
b38c1b
+ * If there was a call to deflateSetDictionary() the adler field in s
b38c1b
+ * will be set to the adler32 value of the passed in dictionary.
b38c1b
+ * Nevertheless the data processing needs to start with a 1. This
b38c1b
+ * function takes are that on the 1st call of deflate when total_in
b38c1b
+ * is still 0, we set the start value always to 1.
b38c1b
+ */
b38c1b
+static void __prep_crc_or_adler(z_streamp s, zedc_streamp h)
b38c1b
+{
b38c1b
+	if (s->total_in == 0) {
b38c1b
+		if (h->format == ZEDC_FORMAT_ZLIB)
b38c1b
+			s->adler = 1;
b38c1b
+		else
b38c1b
+			s->adler = 0;
b38c1b
+	}
b38c1b
+}
b38c1b
+
b38c1b
 static void __free(void *ptr)
b38c1b
 {
b38c1b
 	if (ptr == NULL)
b38c1b
@@ -456,7 +476,10 @@ int h_deflateSetDictionary(z_streamp strm, const uint8_t *dictionary,
b38c1b
 	h = &s->h;
b38c1b
 
b38c1b
 	rc = zedc_deflateSetDictionary(h, dictionary, dictLength);
b38c1b
+	hw_trace("[%p]    adler32=%08x  dict_adler32=%08x\n", strm,
b38c1b
+		 h->adler32, h->dict_adler32);
b38c1b
 
b38c1b
+	strm->adler = h->dict_adler32; /* See #152 */
b38c1b
 	return rc_zedc_to_libz(rc);
b38c1b
 }
b38c1b
 
b38c1b
@@ -608,10 +631,11 @@ int h_deflate(z_streamp strm, int flush)
b38c1b
 		return s->rc;
b38c1b
 	}
b38c1b
 
b38c1b
+	__prep_crc_or_adler(strm, h);
b38c1b
 	hw_trace("[%p] h_deflate: flush=%s avail_in=%d avail_out=%d "
b38c1b
-		 "ibuf_avail=%d obuf_avail=%d\n",
b38c1b
+		 "ibuf_avail=%d obuf_avail=%d adler32/cr32=%08x/%08x\n",
b38c1b
 		 strm, flush_to_str(flush), strm->avail_in, strm->avail_out,
b38c1b
-		 (int)s->ibuf_avail, (int)s->obuf_avail);
b38c1b
+		 (int)s->ibuf_avail, (int)s->obuf_avail, h->adler32, h->crc32);
b38c1b
 
b38c1b
 	do {
b38c1b
 		hw_trace("[%p]   *** loop=%d flush=%s\n", strm, loops,
b38c1b
@@ -1291,7 +1315,7 @@ static inline int __check_stream_end(z_streamp strm)
b38c1b
  sync_avail_in:
b38c1b
 	/*
b38c1b
 	 * Only if we saw Z_STREAM_END and no problems understanding
b38c1b
-	 * the empty HUFFMAN or COPY_BLOCKs arised, we sync up the
b38c1b
+	 * the empty HUFFMAN or COPY_BLOCKs arose, we sync up the
b38c1b
 	 * stream.
b38c1b
 	 *
b38c1b
 	 * For DEFLATE and ZLIB we need to read the adler32 or
b38c1b
@@ -1671,7 +1695,7 @@ void zedc_hw_done(void)
b38c1b
 	if ((flags & ZLIB_FLAG_CACHE_HANDLES) == 0x0)
b38c1b
 		return;
b38c1b
 
b38c1b
-	for (card_no = 0; card_no <= 128; card_no++) {
b38c1b
+	for (card_no = 0; card_no <= ZEDC_CARDS_LENGTH; card_no++) {
b38c1b
 		if (zedc_cards[card_no] == NULL)
b38c1b
 			continue;
b38c1b
 		zedc_close(zedc_cards[card_no]);
b38c1b
diff --git a/lib/inflate.c b/lib/inflate.c
b38c1b
index e25fa7b..9e2762c 100644
b38c1b
--- a/lib/inflate.c
b38c1b
+++ b/lib/inflate.c
b38c1b
@@ -965,30 +965,30 @@ int zedc_inflateSaveBuffers(zedc_streamp strm, const char *prefix)
b38c1b
 		return rc;
b38c1b
 
b38c1b
 	snprintf(fname, sizeof(fname) - 1, "%s_out_buf.bin", prefix);
b38c1b
-	__save_buf_to_file(fname, (void *)(unsigned long)
b38c1b
-			   __be64_to_cpu(asiv->out_buff),
b38c1b
-			   __be32_to_cpu(asiv->out_buff_len));
b38c1b
+	rc = __save_buf_to_file(fname, (void *)(unsigned long)
b38c1b
+				__be64_to_cpu(asiv->out_buff),
b38c1b
+				__be32_to_cpu(asiv->out_buff_len));
b38c1b
 	if (rc != ZEDC_OK)
b38c1b
 		return rc;
b38c1b
 
b38c1b
 	snprintf(fname, sizeof(fname) - 1, "%s_in_dict.bin", prefix);
b38c1b
-	__save_buf_to_file(fname, (void *)(unsigned long)
b38c1b
-			   __be64_to_cpu(asiv->in_dict),
b38c1b
-			   __be32_to_cpu(asiv->in_dict_len));
b38c1b
+	rc = __save_buf_to_file(fname, (void *)(unsigned long)
b38c1b
+				__be64_to_cpu(asiv->in_dict),
b38c1b
+				__be32_to_cpu(asiv->in_dict_len));
b38c1b
 	if (rc != ZEDC_OK)
b38c1b
 		return rc;
b38c1b
 
b38c1b
 	snprintf(fname, sizeof(fname) - 1, "%s_out_dict.bin", prefix);
b38c1b
-	__save_buf_to_file(fname, (void *)(unsigned long)
b38c1b
-			   __be64_to_cpu(asiv->out_dict),
b38c1b
-			   __be32_to_cpu(asiv->out_dict_len));
b38c1b
+	rc = __save_buf_to_file(fname, (void *)(unsigned long)
b38c1b
+				__be64_to_cpu(asiv->out_dict),
b38c1b
+				__be32_to_cpu(asiv->out_dict_len));
b38c1b
 	if (rc != ZEDC_OK)
b38c1b
 		return rc;
b38c1b
 
b38c1b
 	snprintf(fname, sizeof(fname) - 1, "%s_inp_scratch.bin", prefix);
b38c1b
-	__save_buf_to_file(fname, (void *)(unsigned long)
b38c1b
-			   __be64_to_cpu(asiv->inp_scratch),
b38c1b
-			   __be32_to_cpu(asiv->in_scratch_len));
b38c1b
+	rc = __save_buf_to_file(fname, (void *)(unsigned long)
b38c1b
+				__be64_to_cpu(asiv->inp_scratch),
b38c1b
+				__be32_to_cpu(asiv->in_scratch_len));
b38c1b
 	if (rc != ZEDC_OK)
b38c1b
 		return rc;
b38c1b
 
b38c1b
diff --git a/lib/libcard.c b/lib/libcard.c
b38c1b
index b511730..d573b34 100644
b38c1b
--- a/lib/libcard.c
b38c1b
+++ b/lib/libcard.c
b38c1b
@@ -1062,10 +1062,10 @@ void genwqe_card_lib_debug(int onoff)
b38c1b
  */
b38c1b
 static void ddcb_setup_crc32(struct lib_data_t *d)
b38c1b
 {
b38c1b
-	int i, j;
b38c1b
+	unsigned int i, j;
b38c1b
 	uint32_t crc;
b38c1b
 
b38c1b
-	for (i = 0;  i < 256;  i++) {
b38c1b
+	for (i = 0;  i < ARRAY_SIZE(d->crc32_tab);  i++) {
b38c1b
 		crc = i << 24;
b38c1b
 		for (j = 0;  j < 8;  j++) {
b38c1b
 			if (crc & 0x80000000)
b38c1b
@@ -1364,9 +1364,9 @@ int genwqe_pin_memory(card_handle_t dev, const void *addr, size_t size,
b38c1b
 			if (0 == rc)
b38c1b
 				return GENWQE_OK;
b38c1b
 		}
b38c1b
+		pr_err("Dev: %p Fault: %d addr=%p size=%lld dir=%d\n", dev,
b38c1b
+				dev->drv_errno, addr, (long long)size, direction);
b38c1b
 	}
b38c1b
-	pr_err("Dev: %p Fault: %d addr=%p size=%lld dir=%d\n", dev,
b38c1b
-	       dev->drv_errno, addr, (long long)size, direction);
b38c1b
 	return GENWQE_ERR_PINNING;
b38c1b
 }
b38c1b
 
b38c1b
@@ -1390,9 +1390,9 @@ int genwqe_unpin_memory(card_handle_t dev, const void *addr, size_t size)
b38c1b
 			if (0 == rc)
b38c1b
 				return GENWQE_OK;
b38c1b
 		}
b38c1b
+		pr_err("Dev: %p Fault: %d addr=%p size=%lld\n", dev,
b38c1b
+				dev->drv_errno, addr, (long long)size);
b38c1b
 	}
b38c1b
-	pr_err("Dev: %p Fault: %d addr=%p size=%lld\n", dev,
b38c1b
-	       dev->drv_errno, addr, (long long)size);
b38c1b
 	return GENWQE_ERR_PINNING;
b38c1b
 }
b38c1b
 
b38c1b
@@ -1642,20 +1642,18 @@ static void __hexdump(FILE *fp, const void *buff, unsigned int size)
b38c1b
  */
b38c1b
 void genwqe_hexdump(FILE *fp, const void *buff, unsigned int size)
b38c1b
 {
b38c1b
-	unsigned int i;
b38c1b
+	unsigned int i, j = 0;
b38c1b
 	const uint8_t *b = (uint8_t *)buff;
b38c1b
 	char ascii[17];
b38c1b
-	char str[2] = { 0x0, };
b38c1b
 
b38c1b
 	for (i = 0; i < size; i++) {
b38c1b
 		if ((i & 0x0f) == 0x00) {
b38c1b
 			fprintf(fp, " %08x:", i);
b38c1b
-			memset(ascii, 0, sizeof(ascii));
b38c1b
+			memset(ascii, '\0', sizeof(ascii));
b38c1b
+			j = 0;
b38c1b
 		}
b38c1b
 		fprintf(fp, " %02x", b[i]);
b38c1b
-		str[0] = isalnum(b[i]) ? b[i] : '.';
b38c1b
-		str[1] = '\0';
b38c1b
-		strncat(ascii, str, sizeof(ascii) - 1);
b38c1b
+		ascii[j++] = isalnum(b[i]) ? b[i] : '.';
b38c1b
 
b38c1b
 		if ((i & 0x0f) == 0x0f)
b38c1b
 			fprintf(fp, " | %s\n", ascii);
b38c1b
@@ -1664,9 +1662,7 @@ void genwqe_hexdump(FILE *fp, const void *buff, unsigned int size)
b38c1b
 	/* print trailing up to a 16 byte boundary. */
b38c1b
 	for (; i < ((size + 0xf) & ~0xf); i++) {
b38c1b
 		fprintf(fp, "   ");
b38c1b
-		str[0] = ' ';
b38c1b
-		str[1] = '\0';
b38c1b
-		strncat(ascii, str, sizeof(ascii) - 1);
b38c1b
+		ascii[j++] = ' ';
b38c1b
 
b38c1b
 		if ((i & 0x0f) == 0x0f)
b38c1b
 			fprintf(fp, " | %s\n", ascii);
b38c1b
@@ -1737,8 +1733,10 @@ int genwqe_flash_read(card_handle_t dev, struct card_upd_params *upd)
b38c1b
 	rc = __genwqe_flash_read(dev, upd->partition, buf, buflen,
b38c1b
 				 &upd->retc, &upd->attn,
b38c1b
 				 &upd->progress);
b38c1b
-	if (rc < 0)
b38c1b
+	if (rc < 0) {
b38c1b
+		close(fd);
b38c1b
 		goto err_exit;
b38c1b
+	}
b38c1b
 
b38c1b
 	rc = (int)write(fd, buf, (size_t)upd->flength);
b38c1b
 	close(fd);
b38c1b
diff --git a/lib/libddcb.c b/lib/libddcb.c
b38c1b
index 4e94b5e..251e27e 100644
b38c1b
--- a/lib/libddcb.c
b38c1b
+++ b/lib/libddcb.c
b38c1b
@@ -150,10 +150,9 @@ const char *ddcb_strerror(int errnum)
b38c1b
 
b38c1b
 void ddcb_hexdump(FILE *fp, const void *buff, unsigned int size)
b38c1b
 {
b38c1b
-	unsigned int i;
b38c1b
+	unsigned int i, j = 0;
b38c1b
 	const uint8_t *b = (uint8_t *)buff;
b38c1b
 	char ascii[17];
b38c1b
-	char str[2] = { 0x0, };
b38c1b
 
b38c1b
 	if (fp == NULL)
b38c1b
 		return;
b38c1b
@@ -161,12 +160,11 @@ void ddcb_hexdump(FILE *fp, const void *buff, unsigned int size)
b38c1b
 	for (i = 0; i < size; i++) {
b38c1b
 		if ((i & 0x0f) == 0x00) {
b38c1b
 			fprintf(fp, " %08x:", i);
b38c1b
-			memset(ascii, 0, sizeof(ascii));
b38c1b
+			memset(ascii, '\0', sizeof(ascii));
b38c1b
+			j = 0;
b38c1b
 		}
b38c1b
 		fprintf(fp, " %02x", b[i]);
b38c1b
-		str[0] = isalnum(b[i]) ? b[i] : '.';
b38c1b
-		str[1] = '\0';
b38c1b
-		strncat(ascii, str, sizeof(ascii) - 1);
b38c1b
+		ascii[j++] = isalnum(b[i]) ? b[i] : '.';
b38c1b
 
b38c1b
 		if ((i & 0x0f) == 0x0f)
b38c1b
 			fprintf(fp, " | %s\n", ascii);
b38c1b
@@ -175,9 +173,7 @@ void ddcb_hexdump(FILE *fp, const void *buff, unsigned int size)
b38c1b
 	/* print trailing up to a 16 byte boundary. */
b38c1b
 	for (; i < ((size + 0xf) & ~0xf); i++) {
b38c1b
 		fprintf(fp, "   ");
b38c1b
-		str[0] = ' ';
b38c1b
-		str[1] = '\0';
b38c1b
-		strncat(ascii, str, sizeof(ascii) - 1);
b38c1b
+		ascii[j++] = ' ';
b38c1b
 
b38c1b
 		if ((i & 0x0f) == 0x0f)
b38c1b
 			fprintf(fp, " | %s\n", ascii);
b38c1b
@@ -534,7 +530,7 @@ int ddcb_register_accelerator(struct ddcb_accel_funcs *accel)
b38c1b
 	if (accel == NULL)
b38c1b
 		return DDCB_ERR_INVAL;
b38c1b
 
b38c1b
-	if ddcb_gather_statistics() {
b38c1b
+	if (ddcb_gather_statistics()) {
b38c1b
 		rc = pthread_mutex_init(&accel->slock, NULL);
b38c1b
 		if (rc != 0)
b38c1b
 			return DDCB_ERRNO;
b38c1b
diff --git a/lib/wrapper.c b/lib/wrapper.c
b38c1b
index 2e93f71..c16fbf7 100644
b38c1b
--- a/lib/wrapper.c
b38c1b
+++ b/lib/wrapper.c
b38c1b
@@ -626,6 +626,9 @@ int deflateSetDictionary(z_streamp strm,
b38c1b
 	strm->state = w->priv_data;
b38c1b
 	rc = w->impl ? h_deflateSetDictionary(strm, dictionary, dictLength) :
b38c1b
 		       z_deflateSetDictionary(strm, dictionary, dictLength);
b38c1b
+	
b38c1b
+	pr_trace("[%p]    calculated adler32=%08x\n", strm,
b38c1b
+		 (unsigned int)strm->adler);
b38c1b
 	strm->state = (void *)w;
b38c1b
 
b38c1b
 	return rc;
b38c1b
@@ -845,9 +848,10 @@ int deflateEnd(z_streamp strm)
b38c1b
 	}
b38c1b
 
b38c1b
 	rc = __deflateEnd(strm, w);
b38c1b
-	free(w);
b38c1b
 
b38c1b
 	pr_trace("[%p] deflateEnd w=%p rc=%d\n", strm, w, rc);
b38c1b
+	free(w);
b38c1b
+
b38c1b
 	return rc;
b38c1b
 }
b38c1b
 
b38c1b
@@ -1302,9 +1306,10 @@ int inflateEnd(z_streamp strm)
b38c1b
 		free(w->dictionary);
b38c1b
 		w->dictionary = NULL;
b38c1b
 	}
b38c1b
-	free(w);
b38c1b
 
b38c1b
 	pr_trace("[%p] inflateEnd w=%p rc=%d\n", strm, w, rc);
b38c1b
+	free(w);
b38c1b
+
b38c1b
 	return rc;
b38c1b
 }
b38c1b
 
b38c1b
diff --git a/licenses/cla-corporate.txt b/licenses/cla-corporate.txt
b38c1b
deleted file mode 100644
b38c1b
index d137017..0000000
b38c1b
--- a/licenses/cla-corporate.txt
b38c1b
+++ /dev/null
b38c1b
@@ -1,157 +0,0 @@
b38c1b
-        International Business machines, Inc.
b38c1b
-  Software Grant and Corporate Contributor License Agreement ("Agreement")
b38c1b
-        http://www.github.org/ibm-genwqe/licenses/
b38c1b
-
b38c1b
-
b38c1b
-Thank you for your interest in IBM’s ibm-genwqe project (“Hardware
b38c1b
-acceleration of deflate/zlib compression with IBM FPGA
b38c1b
-accelerators"). In order to clarify the intellectual property license
b38c1b
-granted with Contributions from any person or entity, IBM must have a
b38c1b
-Contributor License Agreement (CLA) on file that has been signed by
b38c1b
-each Contributor, indicating agreement to the license terms
b38c1b
-below. This license is for your protection as a Contributor as well as
b38c1b
-the protection of IBM and its users; it does not change your rights to
b38c1b
-use your own Contributions for any other purpose.
b38c1b
-
b38c1b
-This version of the Agreement allows an entity (the "Corporation") to
b38c1b
-submit Contributions to the Project, to authorize Contributions
b38c1b
-submitted by its designated employees to the Project, and to grant
b38c1b
-copyright and patent licenses thereto.
b38c1b
-   
b38c1b
-If you have not already done so, please complete and sign, then scan
b38c1b
-and email a pdf file of this Agreement to <haverkam@de.ibm.com>. If
b38c1b
-necessary, send an original signed Agreement to:
b38c1b
-
b38c1b
-IBM Deutschland RD GmbH 
b38c1b
-SCHOENAICHER STR. 220, BOEBLINGEN 71032
b38c1b
-Germany
b38c1b
-Attn: Frank Haverkamp
b38c1b
-
b38c1b
-
b38c1b
-   Please read this document carefully before signing and keep a copy
b38c1b
-   for your records.
b38c1b
-
b38c1b
-Corporation name:    ________________________________________________
b38c1b
-      
b38c1b
-Corporation address: ________________________________________________
b38c1b
-
b38c1b
-                     ________________________________________________
b38c1b
-
b38c1b
-Point of Contact:    ________________________________________________
b38c1b
-
b38c1b
-E-Mail:              ________________________________________________
b38c1b
-
b38c1b
-Telephone:           _____________________ 
b38c1b
-      
b38c1b
-
b38c1b
-You accept and agree to the following terms and conditions for Your
b38c1b
-present and future Contributions submitted to the Project. Except for
b38c1b
-the license granted herein to IBM and recipients of software
b38c1b
-distributed by IBM, You reserve all right, title, and interest in and
b38c1b
-to Your Contributions.
b38c1b
-
b38c1b
-1. Definitions.
b38c1b
-
b38c1b
-   "You" (or "Your") shall mean the copyright owner or legal entity
b38c1b
-   authorized by the copyright owner that is making this Agreement
b38c1b
-   with IBM. For legal entities, the entity making a Contribution and    
b38c1b
-   all other entities that control, are controlled by, or are under 
b38c1b
-   common control with that entity are considered to be a single    
b38c1b
-   Contributor. For the purposes of this definition, "control" means    
b38c1b
-   (i) the power, direct or indirect, to cause the direction or
b38c1b
-   management of such entity, whether by contract or otherwise, or
b38c1b
-   (ii) ownership of fifty percent (50%) or more of the outstanding
b38c1b
-   shares, or (iii) beneficial ownership of such entity.
b38c1b
-
b38c1b
-   "Contribution" shall mean the code, documentation or other original
b38c1b
-   works of authorship expressly identified in Schedule B, as well as
b38c1b
-   any original work of authorship, including any modifications or
b38c1b
-   additions to an existing work, that is intentionally submitted by
b38c1b
-   You to IBM for inclusion in, or documentation of, the Project
b38c1b
-   managed by IBM (the "Work"). For the purposes of this definition,
b38c1b
-   "submitted" means any form of electronic, verbal, or written
b38c1b
-   communication sent to IBM or its representatives, including but not
b38c1b
-   limited to communication on electronic mailing lists, source code
b38c1b
-   control systems, and issue tracking systems that are managed by, or
b38c1b
-   on behalf of, IBM for the purpose of discussing and improving the
b38c1b
-   Work, but excluding communication that is conspicuously marked or
b38c1b
-   otherwise designated in writing by You as "Not a Contribution."
b38c1b
-
b38c1b
-2. Grant of Copyright License. Subject to the terms and conditions
b38c1b
-   of this Agreement, You hereby grant to IBM and to
b38c1b
-   recipients of software distributed by IBM a perpetual,
b38c1b
-   worldwide, non-exclusive, no-charge, royalty-free, irrevocable
b38c1b
-   copyright license to reproduce, prepare derivative works of,
b38c1b
-   publicly display, publicly perform, sublicense, and distribute
b38c1b
-   Your Contributions and such derivative works.
b38c1b
-
b38c1b
-3. Grant of Patent License. Subject to the terms and conditions of
b38c1b
-   this Agreement, You hereby grant to IBM and to recipients
b38c1b
-   of software distributed by IBM a perpetual, worldwide,
b38c1b
-   non-exclusive, no-charge, royalty-free, irrevocable (except as
b38c1b
-   stated in this section) patent license to make, have made, use,
b38c1b
-   offer to sell, sell, import, and otherwise transfer the Work,
b38c1b
-   where such license applies only to those patent claims licensable
b38c1b
-   by You that are necessarily infringed by Your Contribution(s)
b38c1b
-   alone or by combination of Your Contribution(s) with the Work to
b38c1b
-   which such Contribution(s) were submitted. If any entity institutes
b38c1b
-   patent litigation against You or any other entity (including a
b38c1b
-   cross-claim or counterclaim in a lawsuit) alleging that your
b38c1b
-   Contribution, or the Work to which you have contributed, constitutes
b38c1b
-   direct or contributory patent infringement, then any patent licenses
b38c1b
-   granted to that entity under this Agreement for that Contribution or
b38c1b
-   Work shall terminate as of the date such litigation is filed.
b38c1b
-
b38c1b
-4. You represent that You are legally entitled to grant the above
b38c1b
-   license. You represent further that each employee of the
b38c1b
-   Corporation designated on Schedule A below (or in a subsequent
b38c1b
-   written modification to that Schedule) is authorized to submit
b38c1b
-   Contributions on behalf of the Corporation.
b38c1b
-
b38c1b
-5. You represent that each of Your Contributions is Your original
b38c1b
-   creation (see section 7 for submissions on behalf of others).
b38c1b
-
b38c1b
-6. You are not expected to provide support for Your Contributions,
b38c1b
-   except to the extent You desire to provide support. You may provide
b38c1b
-   support for free, for a fee, or not at all. Unless required by
b38c1b
-   applicable law or agreed to in writing, You provide Your
b38c1b
-   Contributions on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
b38c1b
-   OF ANY KIND, either express or implied, including, without
b38c1b
-   limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT,
b38c1b
-   MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE.
b38c1b
-
b38c1b
-7. Should You wish to submit work that is not Your original creation,
b38c1b
-   You may submit it to IBM separately from any
b38c1b
-   Contribution, identifying the complete details of its source and
b38c1b
-   of any license or other restriction (including, but not limited
b38c1b
-   to, related patents, trademarks, and license agreements) of which
b38c1b
-   you are personally aware, and conspicuously marking the work as
b38c1b
-   "Submitted on behalf of a third-party: [named here]".
b38c1b
-
b38c1b
-8. It is your responsibility to notify IBM when any change
b38c1b
-   is required to the list of designated employees authorized to submit
b38c1b
-   Contributions on behalf of the Corporation, or to the Corporation's
b38c1b
-   Point of Contact with IBM.
b38c1b
-
b38c1b
-
b38c1b
-
b38c1b
-Please sign: __________________________________ Date: _______________
b38c1b
-
b38c1b
-Title:       __________________________________
b38c1b
-
b38c1b
-Corporation: __________________________________
b38c1b
-
b38c1b
-
b38c1b
-Schedule A
b38c1b
-
b38c1b
-   [Initial list of designated employees.  NB: authorization is not
b38c1b
-    tied to particular Contributions.]
b38c1b
-
b38c1b
-
b38c1b
-
b38c1b
-
b38c1b
-Schedule B
b38c1b
-
b38c1b
-   [Identification of optional concurrent software grant.  Would be
b38c1b
-    left blank or omitted if there is no concurrent software grant.]
b38c1b
-
b38c1b
diff --git a/licenses/cla-individual.txt b/licenses/cla-individual.txt
b38c1b
deleted file mode 100644
b38c1b
index 56aad29..0000000
b38c1b
--- a/licenses/cla-individual.txt
b38c1b
+++ /dev/null
b38c1b
@@ -1,140 +0,0 @@
b38c1b
-        International Business Machines, Inc. (IBM)
b38c1b
-     Individual Contributor License Agreement ("Agreement")
b38c1b
-        http://www.github.org/ibm-genwqe/licenses/
b38c1b
-
b38c1b
-Thank you for your interest in the ibm-genwqe project ("Hardware
b38c1b
-acceleration of deflate/zlib compression with IBM FPGA accelerators").
b38c1b
-
b38c1b
-In order to clarify the intellectual property license granted with
b38c1b
-Contributions from any person or entity, IBM must have a Contributor
b38c1b
-License Agreement ("CLA") on file that has been signed by each
b38c1b
-Contributor, indicating agreement to the license terms below. This
b38c1b
-license is for your protection as a Contributor as well as the
b38c1b
-protection of IBM and its customers; it does not change your rights to
b38c1b
-use your own Contributions for any other purpose.  If you have not
b38c1b
-already done so, please complete and sign, then scan and email a pdf
b38c1b
-file of this Agreement to <haverkam@de.ibm.com>.
b38c1b
-
b38c1b
-The original signed agreement should be sent to:
b38c1b
-
b38c1b
-IBM Deutschland RD GmbH 
b38c1b
-SCHOENAICHER STR. 220, BOEBLINGEN 71032
b38c1b
-Germany
b38c1b
-Attn: Frank Haverkamp
b38c1b
-
b38c1b
-Please read this document carefully before signing and keep a copy for
b38c1b
-your records.
b38c1b
-
b38c1b
-  Full name: ______________________________________________________
b38c1b
-
b38c1b
-  (optional) Public name: _________________________________________
b38c1b
-
b38c1b
-  Mailing Address: ________________________________________________
b38c1b
-
b38c1b
-                   ________________________________________________
b38c1b
-
b38c1b
-  Country:   ______________________________________________________
b38c1b
-
b38c1b
-  Telephone: ______________________________________________________
b38c1b
-
b38c1b
-  E-Mail:    ______________________________________________________
b38c1b
-
b38c1b
-
b38c1b
-You accept and agree to the following terms and conditions for Your
b38c1b
-present and future Contributions submitted to the Project. Except for
b38c1b
-the license granted herein to IBM and recipients of software
b38c1b
-distributed by IBM, You reserve all right, title, and interest in and
b38c1b
-to Your Contributions.
b38c1b
-
b38c1b
-1. Definitions.
b38c1b
-
b38c1b
-   "You" (or "Your") shall mean the copyright owner or legal entity
b38c1b
-   authorized by the copyright owner that is making this Agreement
b38c1b
-   with IBM. For legal entities, the entity making a Contribution and
b38c1b
-   all other entities that control, are controlled by, or are under
b38c1b
-   common control with that entity are considered to be a single
b38c1b
-   Contributor. For the purposes of this definition, "control" means
b38c1b
-   (i) the power, direct or indirect, to cause the direction or
b38c1b
-   management of such entity, whether by contract or otherwise, or
b38c1b
-   (ii) ownership of fifty percent (50%) or more of the outstanding
b38c1b
-   shares, or (iii) beneficial ownership of such entity.
b38c1b
-
b38c1b
-   "Contribution" shall mean any original work of authorship,
b38c1b
-   including any modifications or additions to an existing work, that
b38c1b
-   is intentionally submitted by You to the Project for inclusion in,
b38c1b
-   or documentation of, the Project (”the Work”). For the purposes of
b38c1b
-   this definition, "submitted" means any form of electronic, verbal,
b38c1b
-   or written communication sent to the Project or its
b38c1b
-   representatives,including but not limited to communication on
b38c1b
-   electronic mailing lists, source code control systems, and issue
b38c1b
-   tracking systems that are managed by, or on behalf of, the Project
b38c1b
-   for the purpose of discussing and improving the Work, but excluding
b38c1b
-   communication that is conspicuously marked or otherwise designated
b38c1b
-   in writing by You as "Not a Contribution."
b38c1b
-
b38c1b
-2. Grant of Copyright License. Subject to the terms and conditions of
b38c1b
-   this Agreement, You hereby grant to IBM and to recipients of software
b38c1b
-   distributed by IBM a perpetual, worldwide, non-exclusive, no-charge,
b38c1b
-   royalty-free, irrevocable copyright license to reproduce, prepare
b38c1b
-   derivative works of, publicly display, publicly perform, sublicense,
b38c1b
-   and distribute Your Contributions and such derivative works.
b38c1b
-
b38c1b
-3. Grant of Patent License. Subject to the terms and conditions of
b38c1b
-   this Agreement, You hereby grant to IBM and to recipients of software
b38c1b
-   distributed by IBM a perpetual, worldwide, non-exclusive, no-charge,
b38c1b
-   royalty-free, irrevocable (except as stated in this section) patent
b38c1b
-   license to make, have made, use, offer to sell, sell, import, and
b38c1b
-   otherwise transfer the Work to which Your Contribution(s) were
b38c1b
-   submitted, where such license applies only to those patent claims
b38c1b
-   licensable by You that are necessarily infringed by Your
b38c1b
-   Contribution(s) alone or by combination of Your Contribution(s) with
b38c1b
-   the Work to which such Contribution(s) was submitted. If any entity
b38c1b
-   institutes patent litigation against You or any other entity
b38c1b
-   (including a cross-claim or counterclaim in a lawsuit) alleging that
b38c1b
-   your Contribution, or the Work to which you have contributed,
b38c1b
-   constitutes direct or contributory patent infringement, then any
b38c1b
-   patent licenses granted to that entity under this Agreement for that
b38c1b
-   Contribution or Work shall terminate as of the date such litigation is
b38c1b
-   filed.
b38c1b
-
b38c1b
-4. You represent that you are legally entitled to grant the above
b38c1b
-   license. If your employer(s) has rights to intellectual property
b38c1b
-   that you create that includes your Contributions, you represent
b38c1b
-   that you have received permission to make Contributions on behalf
b38c1b
-   of that employer, that your employer has waived such rights for
b38c1b
-   your Contributions to the Project, or that your employer has
b38c1b
-   executed a separate Corporate CLA with IBM.
b38c1b
-
b38c1b
-5. You represent that each of Your Contributions is Your original
b38c1b
-   creation (see section 7 for submissions on behalf of others).  You
b38c1b
-   represent that Your Contribution submissions include complete
b38c1b
-   details of any third-party license or other restriction (including,
b38c1b
-   but not limited to, related patents and trademarks) of which you
b38c1b
-   are personally aware and which are associated with any part of Your
b38c1b
-   Contributions.
b38c1b
-
b38c1b
-6. You are not expected to provide support for Your Contributions,
b38c1b
-   except to the extent You desire to provide support. You may provide
b38c1b
-   support for free, for a fee, or not at all. Unless required by
b38c1b
-   applicable law or agreed to in writing, You provide Your
b38c1b
-   Contributions on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
b38c1b
-   OF ANY KIND, either express or implied, including, without
b38c1b
-   limitation, any warranties or conditions of TITLE, NON-
b38c1b
-   INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE.
b38c1b
-
b38c1b
-7. Should You wish to submit work that is not Your original creation,
b38c1b
-   You may submit it to the Project separately from any
b38c1b
-   Contribution, identifying the complete details of its source and of
b38c1b
-   any license or other restriction (including, but not limited to,
b38c1b
-   related patents, trademarks, and license agreements) of which you
b38c1b
-   are personally aware, and conspicuously marking the work as
b38c1b
-   "Submitted on behalf of a third-party: [named here]".
b38c1b
-
b38c1b
-8. You agree to notify IBM of any facts or circumstances of
b38c1b
-   which you become aware that would make these representations
b38c1b
-   inaccurate in any respect.
b38c1b
-
b38c1b
-
b38c1b
-
b38c1b
-Please sign: __________________________________ Date: ________________
b38c1b
-
b38c1b
diff --git a/misc/zlib_test.sh b/misc/zlib_test.sh
b38c1b
index ec031f7..ddc6271 100755
b38c1b
--- a/misc/zlib_test.sh
b38c1b
+++ b/misc/zlib_test.sh
b38c1b
@@ -397,7 +397,7 @@ function zlib_append ()
b38c1b
     local params=$2
b38c1b
 
b38c1b
     # Use default settings ...
b38c1b
-    # Set size large enough that hardware inflate is realy used
b38c1b
+    # Set size large enough that hardware inflate is really used
b38c1b
     #
b38c1b
     # hhh [0x3ffff1c655d8] loops=0 flush=1 Z_PARTIAL_FLUSH
b38c1b
     # hhh [0x3ffff1c655d8] *** giving out 100 bytes ...
b38c1b
diff --git a/misc/zpipe_append.c b/misc/zpipe_append.c
b38c1b
index 7cb2e2d..7f75a7b 100644
b38c1b
--- a/misc/zpipe_append.c
b38c1b
+++ b/misc/zpipe_append.c
b38c1b
@@ -100,7 +100,7 @@ static int def(FILE *source, FILE *dest, int window_bits, int _flush,
b38c1b
 		return Z_ERRNO;
b38c1b
 
b38c1b
 	out = malloc(CHUNK_o);
b38c1b
-	if (in == NULL) {
b38c1b
+	if (out == NULL) {
b38c1b
 		free(in);
b38c1b
 		return Z_ERRNO;
b38c1b
 	}
b38c1b
@@ -113,8 +113,11 @@ static int def(FILE *source, FILE *dest, int window_bits, int _flush,
b38c1b
 
b38c1b
 	ret = deflateInit2(&strm, level, Z_DEFLATED, window_bits, 8,
b38c1b
 			   Z_DEFAULT_STRATEGY);
b38c1b
-	if (ret != Z_OK)
b38c1b
+	if (ret != Z_OK) {
b38c1b
+		free(in);
b38c1b
+		free(out);
b38c1b
 		return ret;
b38c1b
+	}
b38c1b
 
b38c1b
 	/* compress until end of file */
b38c1b
 	do {
b38c1b
@@ -210,7 +213,7 @@ static int inf(FILE *source, FILE *dest, int window_bits, int _flush,
b38c1b
 		return Z_ERRNO;
b38c1b
 
b38c1b
 	out = malloc(CHUNK_o);
b38c1b
-	if (in == NULL) {
b38c1b
+	if (out == NULL) {
b38c1b
 		free(in);
b38c1b
 		return Z_ERRNO;
b38c1b
 	}
b38c1b
@@ -503,6 +506,8 @@ int main(int argc, char **argv)
b38c1b
 	rc = def(i_fp, o_fp, window_bits, flush, Z_DEFAULT_COMPRESSION,
b38c1b
 		 &expected_bytes, &decompressed_bytes);
b38c1b
 	if (rc != Z_OK) {
b38c1b
+		fclose(o_fp);
b38c1b
+		fclose(i_fp);
b38c1b
 		fprintf(stderr, "err: compression failed.\n");
b38c1b
 		zerr(rc);
b38c1b
 		return rc;
b38c1b
@@ -566,6 +571,8 @@ int main(int argc, char **argv)
b38c1b
 	}
b38c1b
 
b38c1b
 	if (rc != Z_OK) {
b38c1b
+		fclose(o_fp);
b38c1b
+		fclose(n_fp);
b38c1b
 		fprintf(stderr, "err: decompression failed.\n");
b38c1b
 		zerr(rc);
b38c1b
 		return rc;
b38c1b
diff --git a/misc/zpipe_mt.c b/misc/zpipe_mt.c
b38c1b
index 03c57a1..27344f6 100644
b38c1b
--- a/misc/zpipe_mt.c
b38c1b
+++ b/misc/zpipe_mt.c
b38c1b
@@ -42,7 +42,7 @@
b38c1b
 #  define SET_BINARY_MODE(file)
b38c1b
 #endif
b38c1b
 
b38c1b
-/* FIXME Fake this for old RHEL verions e.g. RHEL5.6 */
b38c1b
+/* FIXME Fake this for old RHEL versions e.g. RHEL5.6 */
b38c1b
 #ifndef CPU_ALLOC
b38c1b
 #define	  CPU_ALLOC(cpus)		      ({ void *ptr = NULL; ptr; })
b38c1b
 #define	  CPU_ALLOC_SIZE(cpus)		      ({ int val = 0; val; })
b38c1b
@@ -54,7 +54,7 @@
b38c1b
 #define	  sched_setaffinity(x, size, cpusetp) ({ int val = 0; val; })
b38c1b
 #endif
b38c1b
 
b38c1b
-/* FIXME Fake this for old RHEL verions e.g. RHEL5.6 */
b38c1b
+/* FIXME Fake this for old RHEL versions e.g. RHEL5.6 */
b38c1b
 #ifndef CLOCK_MONOTONIC_RAW
b38c1b
 #define   clock_gettime(clk_id, tp) ({ int val = 0; val; })
b38c1b
 #endif
b38c1b
diff --git a/misc/zpipe_rnd.c b/misc/zpipe_rnd.c
b38c1b
index 042cbc1..ee8e3eb 100644
b38c1b
--- a/misc/zpipe_rnd.c
b38c1b
+++ b/misc/zpipe_rnd.c
b38c1b
@@ -48,8 +48,8 @@ static unsigned int CHUNK_o = 4 * 1024 * 1024; /* 16384; */
b38c1b
    level is supplied, Z_VERSION_ERROR if the version of zlib.h and the
b38c1b
    version of the library linked do not match, or Z_ERRNO if there is
b38c1b
    an error reading or writing the files. */
b38c1b
-static int def(FILE *source, FILE *dest, int level, int windowBits,
b38c1b
-	       uint8_t *dictionary, int dictLength)
b38c1b
+static int def(FILE *source, FILE *dest, int level, int strategy,
b38c1b
+	       int windowBits, uint8_t *dictionary, int dictLength)
b38c1b
 {
b38c1b
 	int ret, flush;
b38c1b
 	unsigned have;
b38c1b
@@ -64,22 +64,30 @@ static int def(FILE *source, FILE *dest, int level, int windowBits,
b38c1b
 		return Z_ERRNO;
b38c1b
 
b38c1b
 	out = malloc(CHUNK_o);
b38c1b
-	if (out == NULL)
b38c1b
+	if (out == NULL) {
b38c1b
+		free(in);
b38c1b
 		return Z_ERRNO;
b38c1b
+	}
b38c1b
 
b38c1b
 	/* allocate deflate state */
b38c1b
 	strm.zalloc = Z_NULL;
b38c1b
 	strm.zfree = Z_NULL;
b38c1b
 	strm.opaque = Z_NULL;
b38c1b
 	ret = deflateInit2(&strm, level, Z_DEFLATED, windowBits, 8,
b38c1b
-			   Z_DEFAULT_STRATEGY);
b38c1b
-	if (ret != Z_OK)
b38c1b
+			   strategy);
b38c1b
+	if (ret != Z_OK) {
b38c1b
+		free(in);
b38c1b
+		free(out);
b38c1b
 		return ret;
b38c1b
+	}
b38c1b
 
b38c1b
 	if (dictLength > 0) {
b38c1b
 		ret = deflateSetDictionary(&strm, dictionary, dictLength);
b38c1b
-		if (ret != Z_OK)
b38c1b
+		if (ret != Z_OK) {
b38c1b
+			free(in);
b38c1b
+			free(out);
b38c1b
 			return ret;
b38c1b
+		}
b38c1b
 	}
b38c1b
 
b38c1b
 	/* compress until end of file */
b38c1b
@@ -152,8 +160,10 @@ static int inf(FILE *source, FILE *dest, int windowBits,
b38c1b
 		return Z_ERRNO;
b38c1b
 
b38c1b
 	out = malloc(CHUNK_o);
b38c1b
-	if (out == NULL)
b38c1b
+	if (out == NULL) {
b38c1b
+		free(in);
b38c1b
 		return Z_ERRNO;
b38c1b
+	}
b38c1b
 
b38c1b
 	/* allocate inflate state */
b38c1b
 	strm.zalloc = Z_NULL;
b38c1b
@@ -163,14 +173,20 @@ static int inf(FILE *source, FILE *dest, int windowBits,
b38c1b
 	strm.next_in = Z_NULL;
b38c1b
 
b38c1b
 	ret = inflateInit2(&strm, windowBits);
b38c1b
-	if (ret != Z_OK)
b38c1b
+	if (ret != Z_OK) {
b38c1b
+		free(in);
b38c1b
+		free(out);
b38c1b
 		return ret;
b38c1b
+	}
b38c1b
 
b38c1b
 	if (!((windowBits >= 8) && (windowBits <= 15)) &&  /* !ZLIB */
b38c1b
 	    (dictLength > 0)) {
b38c1b
 		ret = inflateSetDictionary(&strm, dictionary, dictLength);
b38c1b
-		if (ret != Z_OK)
b38c1b
+		if (ret != Z_OK) {
b38c1b
+			free(in);
b38c1b
+			free(out);
b38c1b
 			return ret;
b38c1b
+		}
b38c1b
 	}
b38c1b
 
b38c1b
 	/* decompress until deflate stream ends or end of file */
b38c1b
@@ -306,6 +322,8 @@ static void usage(char *prog)
b38c1b
 
b38c1b
 	fprintf(stderr, "%s usage: %s [-d, --decompress]\n"
b38c1b
 		"    [-F, --format <ZLIB|DEFLATE|GZIP>]\n"
b38c1b
+		"    [-S, --strategy <0..4>] 0: DEFAULT,\n"
b38c1b
+		"      1: FILTERED, 2: HUFFMAN_ONLY, 3: RLE, 4: FIXED\n"
b38c1b
 		"    [-r, --rnd\n"		
b38c1b
 		"    [-s, --seed <seed>\n"		
b38c1b
 		"    [-1, --fast]\n"
b38c1b
@@ -369,6 +387,7 @@ int main(int argc, char **argv)
b38c1b
 	int dictLength = 0;
b38c1b
 	int windowBits;
b38c1b
 	int level = Z_DEFAULT_COMPRESSION;
b38c1b
+	int strategy = Z_DEFAULT_STRATEGY;
b38c1b
 
b38c1b
 	/* avoid end-of-line conversions */
b38c1b
 	SET_BINARY_MODE(stdin);
b38c1b
@@ -379,6 +398,7 @@ int main(int argc, char **argv)
b38c1b
 		int option_index = 0;
b38c1b
 		static struct option long_options[] = {
b38c1b
 			{ "decompress",  no_argument,       NULL, 'd' },
b38c1b
+			{ "strategy",	 required_argument, NULL, 'S' },
b38c1b
 			{ "format",	 required_argument, NULL, 'F' },
b38c1b
 			{ "fast",        no_argument,       NULL, '1' },
b38c1b
 			{ "default",     no_argument,       NULL, '6' },
b38c1b
@@ -393,7 +413,7 @@ int main(int argc, char **argv)
b38c1b
 			{ 0,		 no_argument,       NULL, 0   },
b38c1b
 		};
b38c1b
 
b38c1b
-		ch = getopt_long(argc, argv, "169D:F:rs:i:o:dvh?",
b38c1b
+		ch = getopt_long(argc, argv, "169D:F:rs:i:o:S:dvh?",
b38c1b
 				 long_options, &option_index);
b38c1b
 		if (ch == -1)    /* all params processed ? */
b38c1b
 			break;
b38c1b
@@ -427,6 +447,9 @@ int main(int argc, char **argv)
b38c1b
 		case 's':
b38c1b
 			seed = str_to_num(optarg);
b38c1b
 			break;
b38c1b
+		case 'S':
b38c1b
+			strategy = str_to_num(optarg);
b38c1b
+			break;
b38c1b
 		case 'i':
b38c1b
 			CHUNK_i = str_to_num(optarg);
b38c1b
 			break;
b38c1b
@@ -447,7 +470,8 @@ int main(int argc, char **argv)
b38c1b
 
b38c1b
 	/* do compression if no arguments */
b38c1b
 	if (compress == 1) {
b38c1b
-		ret = def(stdin, stdout, level, windowBits, dictionary, dictLength);
b38c1b
+		ret = def(stdin, stdout, level, strategy,
b38c1b
+			  windowBits, dictionary, dictLength);
b38c1b
 		if (ret != Z_OK)
b38c1b
 			zerr(ret);
b38c1b
 		return ret;
b38c1b
diff --git a/tools/Makefile b/tools/Makefile
b38c1b
index 4d14b10..cefdcdd 100644
b38c1b
--- a/tools/Makefile
b38c1b
+++ b/tools/Makefile
b38c1b
@@ -62,7 +62,9 @@ $(projs): $(libs)
b38c1b
 
b38c1b
 objs = force_cpu.o genwqe_vpd_common.o $(projs:=.o)
b38c1b
 
b38c1b
-manpages = $(projs:=.1.gz)
b38c1b
+test_scripts = genwqe_mt_perf genwqe_test_gz
b38c1b
+
b38c1b
+manpages = $(projs:=.1.gz) $(test_scripts:=.1.gz)
b38c1b
 
b38c1b
 manpages: all $(manpages)
b38c1b
 
b38c1b
@@ -83,6 +85,7 @@ genwqe_gunzip.o: genwqe_gzip.c
b38c1b
 ### Setting LD_LIBRARY_PATH helps to try tools with dynamic linkage
b38c1b
 %.1: %
b38c1b
 	LD_LIBRARY_PATH=../lib $(HELP2MAN) -N --output=$@ \
b38c1b
+		--help-option='-h' --version-option='-V' \
b38c1b
 		--name "IBM Hardware Accelerator Tool." ./$<
b38c1b
 
b38c1b
 %.1.gz: %.1
b38c1b
diff --git a/tools/force_cpu.c b/tools/force_cpu.c
b38c1b
index eb1634b..0f8cb4c 100644
b38c1b
--- a/tools/force_cpu.c
b38c1b
+++ b/tools/force_cpu.c
b38c1b
@@ -1,3 +1,18 @@
b38c1b
+/*
b38c1b
+ * Copyright 2017 International Business Machines
b38c1b
+ *
b38c1b
+ * Licensed under the Apache License, Version 2.0 (the "License");
b38c1b
+ * you may not use this file except in compliance with the License.
b38c1b
+ * You may obtain a copy of the License at
b38c1b
+ *
b38c1b
+ *     http://www.apache.org/licenses/LICENSE-2.0
b38c1b
+ *
b38c1b
+ * Unless required by applicable law or agreed to in writing, software
b38c1b
+ * distributed under the License is distributed on an "AS IS" BASIS,
b38c1b
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
b38c1b
+ * See the License for the specific language governing permissions and
b38c1b
+ * limitations under the License.
b38c1b
+ */
b38c1b
 
b38c1b
 #include <stdio.h>
b38c1b
 #include <stdlib.h>
b38c1b
diff --git a/tools/genwqe_echo.c b/tools/genwqe_echo.c
b38c1b
index 449564d..8386a9e 100644
b38c1b
--- a/tools/genwqe_echo.c
b38c1b
+++ b/tools/genwqe_echo.c
b38c1b
@@ -75,7 +75,6 @@ static void usage(const char *prog)
b38c1b
 	       "  -i, --interval=INTERVAL_USEC\n"
b38c1b
 	       "  -s, --string=TESTSTRING\n"
b38c1b
 	       "  -p, --polling          use DDCB polling mode.\n"
b38c1b
-	       "  -q, --quiet            only summary output\n"
b38c1b
 	       "\n"
b38c1b
 	       "This utility sends echo DDCBs either to the service layer\n"
b38c1b
 	       "or other chip units. It can be used to check the cards\n"
b38c1b
diff --git a/tools/genwqe_find_card b/tools/genwqe_find_card
b38c1b
index ca60040..aedb07a 100755
b38c1b
--- a/tools/genwqe_find_card
b38c1b
+++ b/tools/genwqe_find_card
b38c1b
@@ -25,6 +25,14 @@
b38c1b
 
b38c1b
 export accel=UNKNOWN
b38c1b
 
b38c1b
+# Print usage message helper function
b38c1b
+function usage() {
b38c1b
+	echo "Usage of $PROGRAM:"
b38c1b
+	echo "    [-A] <accelerator> use either GENWQE for the PCIe "
b38c1b
+	echo "         and CAPI for CAPI based solution available "
b38c1b
+	echo "         only on System p"
b38c1b
+}
b38c1b
+
b38c1b
 # Parse any options given on the command line
b38c1b
 while getopts "A:C:t:PvVhl" opt; do
b38c1b
 	case ${opt} in
b38c1b
@@ -46,14 +54,6 @@ while getopts "A:C:t:PvVhl" opt; do
b38c1b
 	esac
b38c1b
 done
b38c1b
 
b38c1b
-# Print usage message helper function
b38c1b
-function usage() {
b38c1b
-	echo "Usage of $PROGRAM:"
b38c1b
-	echo "    [-A] <accelerator> use either GENWQE for the PCIe "
b38c1b
-	echo "         and CAPI for CAPI based solution available "
b38c1b
-	echo "         only on System p"
b38c1b
-}
b38c1b
-
b38c1b
 #
b38c1b
 # We need to take into account that there might be other CAPI cards
b38c1b
 # in our system. Therefore we check the psl_revision, which identifies
b38c1b
diff --git a/tools/genwqe_gzip.c b/tools/genwqe_gzip.c
b38c1b
index d2990d3..9df40ef 100644
b38c1b
--- a/tools/genwqe_gzip.c
b38c1b
+++ b/tools/genwqe_gzip.c
b38c1b
@@ -368,10 +368,9 @@ static void usage(FILE *fp, char *prog, int argc, char *argv[])
b38c1b
 
b38c1b
 static inline void hexdump(FILE *fp, const void *buff, unsigned int size)
b38c1b
 {
b38c1b
-	unsigned int i;
b38c1b
+	unsigned int i, j = 0;
b38c1b
 	const uint8_t *b = (uint8_t *)buff;
b38c1b
 	char ascii[17];
b38c1b
-	char str[2] = { 0x0, };
b38c1b
 
b38c1b
 	if (size == 0)
b38c1b
 		return;
b38c1b
@@ -379,12 +378,11 @@ static inline void hexdump(FILE *fp, const void *buff, unsigned int size)
b38c1b
 	for (i = 0; i < size; i++) {
b38c1b
 		if ((i & 0x0f) == 0x00) {
b38c1b
 			fprintf(fp, " %08x:", i);
b38c1b
-			memset(ascii, 0, sizeof(ascii));
b38c1b
+			memset(ascii, '\0', sizeof(ascii));
b38c1b
+			j = 0;
b38c1b
 		}
b38c1b
 		fprintf(fp, " %02x", b[i]);
b38c1b
-		str[0] = isalnum(b[i]) ? b[i] : '.';
b38c1b
-		str[1] = '\0';
b38c1b
-		strncat(ascii, str, sizeof(ascii) - 1);
b38c1b
+		ascii[j++] = isalnum(b[i]) ? b[i] : '.';
b38c1b
 
b38c1b
 		if ((i & 0x0f) == 0x0f)
b38c1b
 			fprintf(fp, " | %s\n", ascii);
b38c1b
@@ -393,9 +391,7 @@ static inline void hexdump(FILE *fp, const void *buff, unsigned int size)
b38c1b
 	/* print trailing up to a 16 byte boundary. */
b38c1b
 	for (; i < ((size + 0xf) & ~0xf); i++) {
b38c1b
 		fprintf(fp, "   ");
b38c1b
-		str[0] = ' ';
b38c1b
-		str[1] = '\0';
b38c1b
-		strncat(ascii, str, sizeof(ascii) - 1);
b38c1b
+		ascii[j++] = ' ';
b38c1b
 
b38c1b
 		if ((i & 0x0f) == 0x0f)
b38c1b
 			fprintf(fp, " | %s\n", ascii);
b38c1b
diff --git a/tools/genwqe_memcopy.c b/tools/genwqe_memcopy.c
b38c1b
index 1222cdd..7d25391 100644
b38c1b
--- a/tools/genwqe_memcopy.c
b38c1b
+++ b/tools/genwqe_memcopy.c
b38c1b
@@ -746,7 +746,7 @@ int main(int argc, char *argv[])
b38c1b
 				break;
b38c1b
 			}
b38c1b
 			ip.card_type = strtol(optarg, (char **)NULL, 0);
b38c1b
-			if ((DDCB_TYPE_GENWQE != ip.card_type) ||
b38c1b
+			if ((DDCB_TYPE_GENWQE != ip.card_type) &&
b38c1b
 				(DDCB_TYPE_CAPI != ip.card_type)) {
b38c1b
 				usage(argv[0]);
b38c1b
 				exit(EXIT_FAILURE);
b38c1b
diff --git a/tools/genwqe_vpd_common.c b/tools/genwqe_vpd_common.c
b38c1b
index d6d27d0..4c98906 100644
b38c1b
--- a/tools/genwqe_vpd_common.c
b38c1b
+++ b/tools/genwqe_vpd_common.c
b38c1b
@@ -49,10 +49,10 @@ static char crc_token[]={"CS"};
b38c1b
 
b38c1b
 void genwqe_crc32_setup_lut(void)
b38c1b
 {
b38c1b
-	int i, j;
b38c1b
+	unsigned int i, j;
b38c1b
 	uint32_t crc;
b38c1b
 
b38c1b
-	for (i = 0;  i < 256;  i++) {
b38c1b
+	for (i = 0;  i < ARRAY_SIZE(genwqe_crc32_lut);  i++) {
b38c1b
 		crc = i << 24;
b38c1b
 		for ( j = 0;  j < 8;  j++ ) {
b38c1b
 			if (crc & 0x80000000)
b38c1b
diff --git a/tools/gzFile_test.c b/tools/gzFile_test.c
b38c1b
index 348d3fc..19b56cb 100644
b38c1b
--- a/tools/gzFile_test.c
b38c1b
+++ b/tools/gzFile_test.c
b38c1b
@@ -557,6 +557,11 @@ int main(int argc, char **argv)
b38c1b
 		exit(EXIT_FAILURE);
b38c1b
 	}
b38c1b
 
b38c1b
+	if ((i_fname == NULL) || (o_fname == NULL)) {
b38c1b
+		pr_err("No input or output file name provided.");
b38c1b
+		return -1;
b38c1b
+        }
b38c1b
+
b38c1b
 	fprintf(stderr, "%sCompress %s to %s in %ld bytes, "
b38c1b
 		"out %ld bytes chunks with level %d (size=%lld, offs=%lld)\n",
b38c1b
 		use_compress ? "" : "De",
b38c1b
diff --git a/tools/zlib_mt_perf.c b/tools/zlib_mt_perf.c
b38c1b
index e90e824..dff5a0f 100644
b38c1b
--- a/tools/zlib_mt_perf.c
b38c1b
+++ b/tools/zlib_mt_perf.c
b38c1b
@@ -218,8 +218,10 @@ static int defl(struct thread_data *d, FILE *source, int level)
b38c1b
 		return Z_ERRNO;
b38c1b
 
b38c1b
 	out = __malloc(CHUNK_o);
b38c1b
-	if (out == NULL)
b38c1b
+	if (out == NULL) {
b38c1b
+		__free(in);
b38c1b
 		return Z_ERRNO;
b38c1b
+	}
b38c1b
 
b38c1b
 	/* allocate deflate state */
b38c1b
 	strm.zalloc = Z_NULL;
b38c1b
@@ -319,8 +321,10 @@ static int infl(struct thread_data *d, FILE *source)
b38c1b
 		return Z_ERRNO;
b38c1b
 
b38c1b
 	out = __malloc(CHUNK_o);
b38c1b
-	if (out == NULL)
b38c1b
+	if (out == NULL) {
b38c1b
+		__free(in);
b38c1b
 		return Z_ERRNO;
b38c1b
+	}
b38c1b
 
b38c1b
 	/* allocate inflate state */
b38c1b
 	strm.zalloc = Z_NULL;