diff options
author | Michael Brown <mcb30@ipxe.org> | 2021-05-06 18:38:37 +0100 |
---|---|---|
committer | Michael Brown <mcb30@ipxe.org> | 2021-05-08 15:34:19 +0100 |
commit | 866fa1ce7639c93de8905cf16ab79b8086990728 (patch) | |
tree | 69c4c77fd2c7c90732fd4f75e433223126330ff7 /src | |
parent | d093683d93ccfac4c76e72264ec3b0d8f0017b92 (diff) | |
download | ipxe-imgextract.zip ipxe-imgextract.tar.gz ipxe-imgextract.tar.bz2 |
[gzip] Add support for gzip archive imagesimgextract
Signed-off-by: Michael Brown <mcb30@ipxe.org>
Diffstat (limited to 'src')
-rw-r--r-- | src/config/config_archive.c | 3 | ||||
-rw-r--r-- | src/config/general.h | 1 | ||||
-rw-r--r-- | src/image/gzip.c | 166 | ||||
-rw-r--r-- | src/include/ipxe/errfile.h | 1 | ||||
-rw-r--r-- | src/include/ipxe/gzip.h | 71 | ||||
-rw-r--r-- | src/tests/gzip_test.c | 160 | ||||
-rw-r--r-- | src/tests/tests.c | 1 |
7 files changed, 403 insertions, 0 deletions
diff --git a/src/config/config_archive.c b/src/config/config_archive.c index eceebae..84f21b9 100644 --- a/src/config/config_archive.c +++ b/src/config/config_archive.c @@ -34,3 +34,6 @@ PROVIDE_REQUIRING_SYMBOL(); #ifdef IMAGE_ZLIB REQUIRE_OBJECT ( zlib ); #endif +#ifdef IMAGE_GZIP +REQUIRE_OBJECT ( gzip ); +#endif diff --git a/src/config/general.h b/src/config/general.h index d665379..fcfbaf5 100644 --- a/src/config/general.h +++ b/src/config/general.h @@ -118,6 +118,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL ); #define IMAGE_DER /* DER image support */ #define IMAGE_PEM /* PEM image support */ #define IMAGE_ZLIB /* ZLIB image support */ +#define IMAGE_GZIP /* GZIP image support */ /* * Command-line commands to include diff --git a/src/image/gzip.c b/src/image/gzip.c new file mode 100644 index 0000000..4072813 --- /dev/null +++ b/src/image/gzip.c @@ -0,0 +1,166 @@ +/* + * 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> +#include <ipxe/gzip.h> + +/** @file + * + * gzip compressed images + * + */ + +/** + * Extract gzip image + * + * @v image Image + * @v extracted Extracted image + * @ret rc Return status code + */ +static int gzip_extract ( struct image *image, struct image *extracted ) { + struct gzip_header header; + struct gzip_extra_header extra; + struct gzip_crc_header crc; + struct gzip_footer footer; + struct deflate_chunk in; + unsigned int strings; + size_t offset; + size_t len; + off_t nul; + int rc; + + /* Sanity check */ + assert ( image->len >= ( sizeof ( header ) + sizeof ( footer ) ) ); + + /* Extract footer */ + len = ( image->len - sizeof ( footer ) ); + copy_from_user ( &footer, image->data, len, sizeof ( footer ) ); + + /* Extract fixed header */ + copy_from_user ( &header, image->data, 0, sizeof ( header ) ); + offset = sizeof ( header ); + assert ( offset <= ( image->len - sizeof ( footer ) ) ); + + /* Skip extra header, if present */ + if ( header.flags & GZIP_FL_EXTRA ) { + copy_from_user ( &extra, image->data, offset, + sizeof ( extra ) ); + offset += sizeof ( extra ); + offset += le16_to_cpu ( extra.len ); + if ( offset > len ) { + DBGC ( image, "GZIP %p overlength extra header\n", + image ); + return -EINVAL; + } + } + assert ( offset <= ( image->len - sizeof ( footer ) ) ); + + /* Skip name and/or comment, if present */ + strings = 0; + if ( header.flags & GZIP_FL_NAME ) + strings++; + if ( header.flags & GZIP_FL_COMMENT ) + strings++; + while ( strings-- ) { + nul = memchr_user ( image->data, offset, 0, ( len - offset ) ); + if ( nul < 0 ) { + DBGC ( image, "GZIP %p overlength name/comment\n", + image ); + return -EINVAL; + } + offset = ( nul + 1 /* NUL */ ); + } + assert ( offset <= ( image->len - sizeof ( footer ) ) ); + + /* Skip CRC, if present */ + if ( header.flags & GZIP_FL_HCRC ) { + offset += sizeof ( crc ); + if ( offset > len ) { + DBGC ( image, "GZIP %p overlength CRC header\n", + image ); + return -EINVAL; + } + } + + /* Initialise input chunk */ + deflate_chunk_init ( &in, userptr_add ( image->data, offset ), 0, len ); + + /* Presize extracted image */ + if ( ( rc = image_set_len ( extracted, + le32_to_cpu ( footer.len ) ) ) != 0 ) { + DBGC ( image, "GZIP %p could not presize: %s\n", + image, strerror ( rc ) ); + return rc; + } + + /* Decompress image (expanding if necessary) */ + if ( ( rc = zlib_deflate ( DEFLATE_RAW, &in, extracted ) ) != 0 ) { + DBGC ( image, "GZIP %p could not decompress: %s\n", + image, strerror ( rc ) ); + return rc; + } + + return 0; +} + +/** + * Probe gzip image + * + * @v image gzip image + * @ret rc Return status code + */ +static int gzip_probe ( struct image *image ) { + struct gzip_header header; + struct gzip_footer footer; + + /* Sanity check */ + if ( image->len < ( sizeof ( header ) + sizeof ( footer ) ) ) { + DBGC ( image, "GZIP %p image too short\n", image ); + return -ENOEXEC; + } + + /* Check magic header */ + copy_from_user ( &header.magic, image->data, 0, + sizeof ( header.magic ) ); + if ( header.magic != cpu_to_be16 ( GZIP_MAGIC ) ) { + DBGC ( image, "GZIP %p invalid magic\n", image ); + return -ENOEXEC; + } + + return 0; +} + +/** gzip image type */ +struct image_type gzip_image_type __image_type ( PROBE_NORMAL ) = { + .name = "gzip", + .probe = gzip_probe, + .extract = gzip_extract, +}; diff --git a/src/include/ipxe/errfile.h b/src/include/ipxe/errfile.h index 35b03fe..90c91cd 100644 --- a/src/include/ipxe/errfile.h +++ b/src/include/ipxe/errfile.h @@ -303,6 +303,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL ); #define ERRFILE_pem ( ERRFILE_IMAGE | 0x00090000 ) #define ERRFILE_archive ( ERRFILE_IMAGE | 0x000a0000 ) #define ERRFILE_zlib ( ERRFILE_IMAGE | 0x000b0000 ) +#define ERRFILE_gzip ( ERRFILE_IMAGE | 0x000c0000 ) #define ERRFILE_asn1 ( ERRFILE_OTHER | 0x00000000 ) #define ERRFILE_chap ( ERRFILE_OTHER | 0x00010000 ) diff --git a/src/include/ipxe/gzip.h b/src/include/ipxe/gzip.h new file mode 100644 index 0000000..c8cf641 --- /dev/null +++ b/src/include/ipxe/gzip.h @@ -0,0 +1,71 @@ +#ifndef _IPXE_GZIP_H +#define _IPXE_GZIP_H + +/** @file + * + * gzip compressed images + * + */ + +FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL ); + +#include <stdint.h> +#include <ipxe/image.h> + +/** gzip header */ +struct gzip_header { + /** Magic ID */ + uint16_t magic; + /** Compression method */ + uint8_t method; + /** Flags */ + uint8_t flags; + /** Modification time */ + uint32_t mtime; + /** Extra flags */ + uint8_t extra; + /** Operating system */ + uint8_t os; +} __attribute__ (( packed )); + +/** Magic ID */ +#define GZIP_MAGIC 0x1f8b + +/** Compression method */ +#define GZIP_METHOD_DEFLATE 0x08 + +/** CRC header is present */ +#define GZIP_FL_HCRC 0x02 + +/** Extra header is present */ +#define GZIP_FL_EXTRA 0x04 + +/** File name is present */ +#define GZIP_FL_NAME 0x08 + +/** File comment is present */ +#define GZIP_FL_COMMENT 0x10 + +/** gzip extra header */ +struct gzip_extra_header { + /** Extra header length (excluding this field) */ + uint16_t len; +} __attribute__ (( packed )); + +/** gzip CRC header */ +struct gzip_crc_header { + /** CRC-16 */ + uint16_t crc; +} __attribute__ (( packed )); + +/** gzip footer */ +struct gzip_footer { + /** CRC-32 */ + uint32_t crc; + /** Uncompressed size (modulo 2^32) */ + uint32_t len; +} __attribute__ (( packed )); + +extern struct image_type gzip_image_type __image_type ( PROBE_NORMAL ); + +#endif /* _IPXE_GZIP_H */ diff --git a/src/tests/gzip_test.c b/src/tests/gzip_test.c new file mode 100644 index 0000000..fa76edc --- /dev/null +++ b/src/tests/gzip_test.c @@ -0,0 +1,160 @@ +/* + * 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 + * + * gzip image tests + * + */ + +/* Forcibly enable assertions */ +#undef NDEBUG + +#include <stdint.h> +#include <ipxe/image.h> +#include <ipxe/gzip.h> +#include <ipxe/test.h> + +/** A gzip test */ +struct gzip_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 gzip test */ +#define GZIP( name, COMPRESSED, EXPECTED ) \ + static const uint8_t name ## _compressed[] = COMPRESSED; \ + static const uint8_t name ## _expected[] = EXPECTED; \ + static struct gzip_test name = { \ + .compressed_name = #name ".gz", \ + .compressed = name ## _compressed, \ + .compressed_len = sizeof ( name ## _compressed ), \ + .expected_name = #name, \ + .expected = name ## _expected, \ + .expected_len = sizeof ( name ## _expected ), \ + }; + +/** "Hello world" */ +GZIP ( hello_world, + DATA ( 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, + 0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0x57, 0x28, 0xcf, 0x2f, 0xca, + 0x49, 0x01, 0x00, 0x52, 0x9e, 0xd6, 0x8b, 0x0b, 0x00, 0x00, + 0x00 ), + DATA ( 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, + 0x64 ) ); + +/** "Hello filename" */ +GZIP ( hello_filename, + DATA ( 0x1f, 0x8b, 0x08, 0x08, 0xeb, 0x5b, 0x96, 0x60, 0x00, 0x03, + 0x68, 0x77, 0x2e, 0x74, 0x78, 0x74, 0x00, 0xf3, 0x48, 0xcd, + 0xc9, 0xc9, 0x57, 0x48, 0xcb, 0xcc, 0x49, 0xcd, 0x4b, 0xcc, + 0x4d, 0x05, 0x00, 0x69, 0x37, 0x25, 0x3c, 0x0e, 0x00, 0x00, + 0x00 ), + DATA ( 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x66, 0x69, 0x6c, 0x65, + 0x6e, 0x61, 0x6d, 0x65 ) ); + +/** "Hello assorted headers" */ +GZIP ( hello_headers, + DATA ( 0x1f, 0x8b, 0x08, 0x1c, 0x11, 0x5c, 0x96, 0x60, 0x00, 0x03, + 0x05, 0x00, 0x41, 0x70, 0x01, 0x00, 0x0d, 0x68, 0x77, 0x2e, + 0x74, 0x78, 0x74, 0x00, 0x2f, 0x2f, 0x77, 0x68, 0x79, 0x3f, + 0x00, 0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0x57, 0x48, 0x2c, 0x2e, + 0xce, 0x2f, 0x2a, 0x49, 0x4d, 0x51, 0xc8, 0x48, 0x4d, 0x4c, + 0x49, 0x2d, 0x2a, 0x06, 0x00, 0x59, 0xa4, 0x19, 0x61, 0x16, + 0x00, 0x00, 0x00 ), + DATA ( 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x61, 0x73, 0x73, 0x6f, + 0x72, 0x74, 0x65, 0x64, 0x20, 0x68, 0x65, 0x61, 0x64, 0x65, + 0x72, 0x73 ) ); + +/** + * Report gzip test result + * + * @v test gzip test + * @v file Test code file + * @v line Test code line + */ +static void gzip_okx ( struct gzip_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 == &gzip_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 gzip_ok( test ) gzip_okx ( test, __FILE__, __LINE__ ) + +/** + * Perform gzip self-test + * + */ +static void gzip_test_exec ( void ) { + + gzip_ok ( &hello_world ); + gzip_ok ( &hello_filename ); + gzip_ok ( &hello_headers ); +} + +/** gzip self-test */ +struct self_test gzip_test __self_test = { + .name = "gzip", + .exec = gzip_test_exec, +}; diff --git a/src/tests/tests.c b/src/tests/tests.c index f4cf041..1cc4c81 100644 --- a/src/tests/tests.c +++ b/src/tests/tests.c @@ -74,3 +74,4 @@ REQUIRE_OBJECT ( der_test ); REQUIRE_OBJECT ( pem_test ); REQUIRE_OBJECT ( ntlm_test ); REQUIRE_OBJECT ( zlib_test ); +REQUIRE_OBJECT ( gzip_test ); |