[zlib] Add support for zlib archive images

Signed-off-by: Michael Brown <mcb30@ipxe.org>
diff --git a/src/config/config_archive.c b/src/config/config_archive.c
index 3644b5d..eceebae 100644
--- a/src/config/config_archive.c
+++ b/src/config/config_archive.c
@@ -30,3 +30,7 @@
  */
 
 PROVIDE_REQUIRING_SYMBOL();
+
+#ifdef IMAGE_ZLIB
+REQUIRE_OBJECT ( zlib );
+#endif
diff --git a/src/config/general.h b/src/config/general.h
index fd317be..d665379 100644
--- a/src/config/general.h
+++ b/src/config/general.h
@@ -117,6 +117,7 @@
 #define	IMAGE_PNG		/* PNG image support */
 #define	IMAGE_DER		/* DER image support */
 #define	IMAGE_PEM		/* PEM image support */
+#define	IMAGE_ZLIB		/* ZLIB image support */
 
 /*
  * Command-line commands to include
diff --git a/src/image/zlib.c b/src/image/zlib.c
new file mode 100644
index 0000000..bc27142
--- /dev/null
+++ b/src/image/zlib.c
@@ -0,0 +1,162 @@
+/*
+ * Copyright (C) 2021 Michael Brown <mbrown@fensystems.co.uk>.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ *
+ * You can also choose to distribute this program under the terms of
+ * the Unmodified Binary Distribution Licence (as given in the file
+ * COPYING.UBDL), provided that you have satisfied its requirements.
+ */
+
+FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
+
+#include <stdlib.h>
+#include <errno.h>
+#include <assert.h>
+#include <ipxe/deflate.h>
+#include <ipxe/uaccess.h>
+#include <ipxe/image.h>
+#include <ipxe/zlib.h>
+
+/** @file
+ *
+ * zlib compressed images
+ *
+ */
+
+/**
+ * Extract compressed data to image
+ *
+ * @v format		Compression format code
+ * @v in		Compressed input chunk
+ * @v extracted		Extracted image
+ * @ret rc		Return status code
+ */
+int zlib_deflate ( enum deflate_format format, struct deflate_chunk *in,
+		   struct image *extracted ) {
+	struct deflate *deflate;
+	struct deflate_chunk out;
+	int rc;
+
+	/* Allocate and initialise decompressor */
+	deflate = zalloc ( sizeof ( *deflate ) );
+	if ( ! deflate ) {
+		rc = -ENOMEM;
+		goto err_alloc;
+	}
+
+	/* Decompress data, (re)allocating if necessary */
+	while ( 1 ) {
+
+		/* (Re)initialise decompressor */
+		deflate_init ( deflate, format );
+
+		/* (Re)initialise input chunk */
+		in->offset = 0;
+
+		/* Initialise output chunk */
+		deflate_chunk_init ( &out, extracted->data, 0, extracted->len );
+
+		/* Decompress data */
+		if ( ( rc = deflate_inflate ( deflate, in, &out ) ) != 0 ) {
+			DBGC ( extracted, "ZLIB %p could not decompress: %s\n",
+			       extracted, strerror ( rc ) );
+			goto err_inflate;
+		}
+
+		/* Check that decompression is valid */
+		if ( ! deflate_finished ( deflate ) ) {
+			DBGC ( extracted, "ZLIB %p decompression incomplete\n",
+			       extracted );
+			rc = -EINVAL;
+			goto err_unfinished;
+		}
+
+		/* Finish if output image size was correct */
+		if ( out.offset == extracted->len )
+			break;
+
+		/* Otherwise, resize output image and retry */
+		if ( ( rc = image_set_len ( extracted, out.offset ) ) != 0 ) {
+			DBGC ( extracted, "ZLIB %p could not resize: %s\n",
+			       extracted, strerror ( rc ) );
+			goto err_set_size;
+		}
+	}
+
+	/* Success */
+	rc = 0;
+
+ err_set_size:
+ err_unfinished:
+ err_inflate:
+	free ( deflate );
+ err_alloc:
+	return rc;
+}
+
+/**
+ * Extract zlib image
+ *
+ * @v image		Image
+ * @v extracted		Extracted image
+ * @ret rc		Return status code
+ */
+static int zlib_extract ( struct image *image, struct image *extracted ) {
+	struct deflate_chunk in;
+	int rc;
+
+	/* Initialise input chunk */
+	deflate_chunk_init ( &in, image->data, 0, image->len );
+
+	/* Decompress image */
+	if ( ( rc = zlib_deflate ( DEFLATE_ZLIB, &in, extracted ) ) != 0 )
+		return rc;
+
+	return 0;
+}
+
+/**
+ * Probe zlib image
+ *
+ * @v image		zlib image
+ * @ret rc		Return status code
+ */
+static int zlib_probe ( struct image *image ) {
+	union zlib_magic magic;
+
+	/* Sanity check */
+	if ( image->len < sizeof ( magic ) ) {
+		DBGC ( image, "ZLIB %p image too short\n", image );
+		return -ENOEXEC;
+	}
+
+	/* Check magic header */
+	copy_from_user ( &magic, image->data, 0, sizeof ( magic ) );
+	if ( ! zlib_magic_is_valid ( &magic ) ) {
+		DBGC ( image, "ZLIB %p invalid magic data\n", image );
+		return -ENOEXEC;
+	}
+
+	return 0;
+}
+
+/** zlib image type */
+struct image_type zlib_image_type __image_type ( PROBE_NORMAL ) = {
+	.name = "zlib",
+	.probe = zlib_probe,
+	.extract = zlib_extract,
+};
diff --git a/src/include/ipxe/errfile.h b/src/include/ipxe/errfile.h
index 162e6b7..35b03fe 100644
--- a/src/include/ipxe/errfile.h
+++ b/src/include/ipxe/errfile.h
@@ -302,6 +302,7 @@
 #define ERRFILE_der		      ( ERRFILE_IMAGE | 0x00080000 )
 #define ERRFILE_pem		      ( ERRFILE_IMAGE | 0x00090000 )
 #define ERRFILE_archive		      ( ERRFILE_IMAGE | 0x000a0000 )
+#define ERRFILE_zlib		      ( ERRFILE_IMAGE | 0x000b0000 )
 
 #define ERRFILE_asn1		      ( ERRFILE_OTHER | 0x00000000 )
 #define ERRFILE_chap		      ( ERRFILE_OTHER | 0x00010000 )
diff --git a/src/include/ipxe/zlib.h b/src/include/ipxe/zlib.h
new file mode 100644
index 0000000..29016c3
--- /dev/null
+++ b/src/include/ipxe/zlib.h
@@ -0,0 +1,43 @@
+#ifndef _IPXE_ZLIB_H
+#define _IPXE_ZLIB_H
+
+/** @file
+ *
+ * zlib compressed images
+ *
+ */
+
+FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
+
+#include <stdint.h>
+#include <byteswap.h>
+#include <ipxe/image.h>
+#include <ipxe/deflate.h>
+
+/** zlib magic header */
+union zlib_magic {
+	/** Compression method and flags */
+	uint8_t cmf;
+	/** Check value */
+	uint16_t check;
+} __attribute__ (( packed ));
+
+/**
+ * Check that zlib magic header is valid
+ *
+ * @v magic		Magic header
+ * @ret is_valid	Magic header is valid
+ */
+static inline int zlib_magic_is_valid ( union zlib_magic *magic ) {
+
+	/* Check magic value as per RFC 6713 */
+	return ( ( ( magic->cmf & 0x8f ) == 0x08 ) &&
+		 ( ( be16_to_cpu ( magic->check ) % 31 ) == 0 ) );
+}
+
+extern int zlib_deflate ( enum deflate_format format, struct deflate_chunk *in,
+			  struct image *extracted );
+
+extern struct image_type zlib_image_type __image_type ( PROBE_NORMAL );
+
+#endif /* _IPXE_ZLIB_H */
diff --git a/src/tests/tests.c b/src/tests/tests.c
index 2e812d6..f4cf041 100644
--- a/src/tests/tests.c
+++ b/src/tests/tests.c
@@ -73,3 +73,4 @@
 REQUIRE_OBJECT ( der_test );
 REQUIRE_OBJECT ( pem_test );
 REQUIRE_OBJECT ( ntlm_test );
+REQUIRE_OBJECT ( zlib_test );
diff --git a/src/tests/zlib_test.c b/src/tests/zlib_test.c
new file mode 100644
index 0000000..df52d09
--- /dev/null
+++ b/src/tests/zlib_test.c
@@ -0,0 +1,133 @@
+/*
+ * Copyright (C) 2021 Michael Brown <mbrown@fensystems.co.uk>.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ *
+ * You can also choose to distribute this program under the terms of
+ * the Unmodified Binary Distribution Licence (as given in the file
+ * COPYING.UBDL), provided that you have satisfied its requirements.
+ */
+
+FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
+
+/** @file
+ *
+ * zlib image tests
+ *
+ */
+
+/* Forcibly enable assertions */
+#undef NDEBUG
+
+#include <stdint.h>
+#include <ipxe/image.h>
+#include <ipxe/zlib.h>
+#include <ipxe/test.h>
+
+/** A zlib test */
+struct zlib_test {
+	/** Compressed filename */
+	const char *compressed_name;
+	/** Compressed data */
+	const void *compressed;
+	/** Length of compressed data */
+	size_t compressed_len;
+	/** Expected uncompressed name */
+	const char *expected_name;
+	/** Expected uncompressed data */
+	const void *expected;
+	/** Length of expected uncompressed data */
+	size_t expected_len;
+};
+
+/** Define inline data */
+#define DATA(...) { __VA_ARGS__ }
+
+/** Define a zlib test */
+#define ZLIB( name, COMPRESSED, EXPECTED )				\
+	static const uint8_t name ## _compressed[] = COMPRESSED;	\
+	static const uint8_t name ## _expected[] = EXPECTED;		\
+	static struct zlib_test name = {				\
+		.compressed_name = #name ".z",				\
+		.compressed = name ## _compressed,			\
+		.compressed_len = sizeof ( name ## _compressed ),	\
+		.expected_name = #name,					\
+		.expected = name ## _expected,				\
+		.expected_len = sizeof ( name ## _expected ),		\
+	};
+
+/** "Hello world" */
+ZLIB ( hello_world,
+       DATA ( 0x78, 0x9c, 0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0x57, 0x28, 0xcf,
+	      0x2f, 0xca, 0x49, 0x01, 0x00, 0x18, 0xab, 0x04, 0x3d ),
+       DATA ( 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c,
+	      0x64 ) );
+
+/**
+ * Report zlib test result
+ *
+ * @v test		zlib test
+ * @v file		Test code file
+ * @v line		Test code line
+ */
+static void zlib_okx ( struct zlib_test *test, const char *file,
+		       unsigned int line ) {
+	struct image *image;
+	struct image *extracted;
+
+	/* Construct compressed image */
+	image = image_memory ( test->compressed_name,
+			       virt_to_user ( test->compressed ),
+			       test->compressed_len );
+	okx ( image != NULL, file, line );
+	okx ( image->len == test->compressed_len, file, line );
+
+	/* Check type detection */
+	okx ( image->type == &zlib_image_type, file, line );
+
+	/* Extract archive image */
+	okx ( image_extract ( image, NULL, &extracted ) == 0, file, line );
+
+	/* Verify extracted image content */
+	okx ( extracted->len == test->expected_len, file, line );
+	okx ( memcmp_user ( extracted->data, 0,
+			    virt_to_user ( test->expected ), 0,
+			    test->expected_len ) == 0, file, line );
+
+	/* Verify extracted image name */
+	okx ( strcmp ( extracted->name, test->expected_name ) == 0,
+	      file, line );
+
+	/* Unregister images */
+	unregister_image ( extracted );
+	unregister_image ( image );
+}
+#define zlib_ok( test ) zlib_okx ( test, __FILE__, __LINE__ )
+
+/**
+ * Perform zlib self-test
+ *
+ */
+static void zlib_test_exec ( void ) {
+
+	zlib_ok ( &hello_world );
+}
+
+/** zlib self-test */
+struct self_test zlib_test __self_test = {
+	.name = "zlib",
+	.exec = zlib_test_exec,
+};