iPXE
zlib.c File Reference

zlib compressed images More...

#include <stdlib.h>
#include <errno.h>
#include <assert.h>
#include <ipxe/deflate.h>
#include <ipxe/image.h>
#include <ipxe/zlib.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 FILE_SECBOOT (PERMITTED)
int zlib_deflate (enum deflate_format format, const void *data, size_t len, struct image *extracted)
 Extract compressed data to image.
static int zlib_extract (struct image *image, struct image *extracted)
 Extract zlib image.
static int zlib_probe (struct image *image)
 Probe zlib image.
struct image_type zlib_image_type __image_type (PROBE_NORMAL)
 zlib image type

Detailed Description

zlib compressed images

Definition in file zlib.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED )

◆ zlib_deflate()

int zlib_deflate ( enum deflate_format format,
const void * data,
size_t len,
struct image * extracted )

Extract compressed data to image.

Parameters
formatCompression format code
dataCompressed input data
lenLength of compressed input data
extractedExtracted image
Return values
rcReturn status code

Definition at line 49 of file zlib.c.

50 {
51 struct deflate *deflate;
52 struct deflate_chunk out;
53 int rc;
54
55 /* Allocate and initialise decompressor */
56 deflate = zalloc ( sizeof ( *deflate ) );
57 if ( ! deflate ) {
58 rc = -ENOMEM;
59 goto err_alloc;
60 }
61
62 /* Decompress data, (re)allocating if necessary */
63 while ( 1 ) {
64
65 /* (Re)initialise decompressor */
67
68 /* Initialise output chunk */
69 deflate_chunk_init ( &out, extracted->rwdata, 0,
70 extracted->len );
71
72 /* Decompress data */
73 if ( ( rc = deflate_inflate ( deflate, data, len,
74 &out ) ) != 0 ) {
75 DBGC ( extracted, "ZLIB %s could not decompress: %s\n",
76 extracted->name, strerror ( rc ) );
77 goto err_inflate;
78 }
79
80 /* Check that decompression is valid */
81 if ( ! deflate_finished ( deflate ) ) {
82 DBGC ( extracted, "ZLIB %s decompression incomplete\n",
83 extracted->name );
84 rc = -EINVAL;
85 goto err_unfinished;
86 }
87
88 /* Finish if output image size was correct */
89 if ( out.offset == extracted->len )
90 break;
91
92 /* Otherwise, resize output image and retry */
93 if ( ( rc = image_set_len ( extracted, out.offset ) ) != 0 ) {
94 DBGC ( extracted, "ZLIB %s could not resize: %s\n",
95 extracted->name, strerror ( rc ) );
96 goto err_set_size;
97 }
98 }
99
100 /* Success */
101 rc = 0;
102
103 err_set_size:
104 err_unfinished:
105 err_inflate:
106 free ( deflate );
107 err_alloc:
108 return rc;
109}
__be32 out[4]
Definition CIB_PRM.h:8
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
void deflate_init(struct deflate *deflate, enum deflate_format format)
Initialise decompressor.
Definition deflate.c:992
int deflate_inflate(struct deflate *deflate, const void *data, size_t len, struct deflate_chunk *out)
Inflate compressed data.
Definition deflate.c:484
static int deflate_finished(struct deflate *deflate)
Check if decompression has finished.
Definition deflate.h:278
ring len
Length.
Definition dwmac.h:226
uint8_t data[48]
Additional event data.
Definition ena.h:11
#define DBGC(...)
Definition compiler.h:505
#define EINVAL
Invalid argument.
Definition errno.h:429
#define ENOMEM
Not enough space.
Definition errno.h:535
int image_set_len(struct image *image, size_t len)
Set image length.
Definition image.c:245
void * zalloc(size_t size)
Allocate cleared memory.
Definition malloc.c:662
static void(* free)(struct refcnt *refcnt))
Definition refcnt.h:55
char * strerror(int errno)
Retrieve string representation of error number.
Definition strerror.c:79
A chunk of data.
Definition deflate.h:246
Decompressor.
Definition deflate.h:156
char * name
Name.
Definition image.h:38
size_t len
Length of raw file image.
Definition image.h:56
void * rwdata
Writable data.
Definition image.h:53
int const char * format
Definition xfer.h:105

References data, DBGC, deflate_finished(), deflate_inflate(), deflate_init(), EINVAL, ENOMEM, format, free, image_set_len(), image::len, len, image::name, out, rc, image::rwdata, strerror(), and zalloc().

Referenced by gzip_extract(), and zlib_extract().

◆ zlib_extract()

int zlib_extract ( struct image * image,
struct image * extracted )
static

Extract zlib image.

Parameters
imageImage
extractedExtracted image
Return values
rcReturn status code

Definition at line 118 of file zlib.c.

118 {
119 int rc;
120
121 /* Decompress image */
123 extracted ) ) != 0 )
124 return rc;
125
126 return 0;
127}
@ DEFLATE_ZLIB
ZLIB header and footer.
Definition deflate.h:21
An executable image.
Definition image.h:24
const void * data
Read-only data.
Definition image.h:51
int zlib_deflate(enum deflate_format format, const void *data, size_t len, struct image *extracted)
Extract compressed data to image.
Definition zlib.c:49

References image::data, DEFLATE_ZLIB, image::len, rc, and zlib_deflate().

Referenced by __image_type().

◆ zlib_probe()

int zlib_probe ( struct image * image)
static

Probe zlib image.

Parameters
imagezlib image
Return values
rcReturn status code

Definition at line 135 of file zlib.c.

135 {
136 const union zlib_magic *magic;
137
138 /* Sanity check */
139 if ( image->len < sizeof ( *magic ) ) {
140 DBGC ( image, "ZLIB %s image too short\n", image->name );
141 return -ENOEXEC;
142 }
143 magic = image->data;
144
145 /* Check magic header */
146 if ( ! zlib_magic_is_valid ( magic ) ) {
147 DBGC ( image, "ZLIB %s invalid magic data\n", image->name );
148 return -ENOEXEC;
149 }
150
151 return 0;
152}
uint16_t magic
Magic signature.
Definition bzimage.h:1
#define ENOEXEC
Exec format error.
Definition errno.h:520
zlib magic header
Definition zlib.h:19
static int zlib_magic_is_valid(const union zlib_magic *magic)
Check that zlib magic header is valid.
Definition zlib.h:32

References image::data, DBGC, ENOEXEC, image::len, magic, image::name, and zlib_magic_is_valid().

Referenced by __image_type().

◆ __image_type()

struct image_type zlib_image_type __image_type ( PROBE_NORMAL )