iPXE
Functions
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)
 
int zlib_deflate (enum deflate_format format, const void *data, size_t len, struct image *extracted)
 Extract compressed data to image. More...
 
static int zlib_extract (struct image *image, struct image *extracted)
 Extract zlib image. More...
 
static int zlib_probe (struct image *image)
 Probe zlib image. More...
 
struct image_type zlib_image_type __image_type (PROBE_NORMAL)
 zlib image type More...
 

Detailed Description

zlib compressed images

Definition in file zlib.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ 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 48 of file zlib.c.

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

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()

static 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 117 of file zlib.c.

117  {
118  int rc;
119 
120  /* Decompress image */
121  if ( ( rc = zlib_deflate ( DEFLATE_ZLIB, image->data, image->len,
122  extracted ) ) != 0 )
123  return rc;
124 
125  return 0;
126 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
const void * data
Read-only data.
Definition: image.h:50
An executable image.
Definition: image.h:23
ZLIB header and footer.
Definition: deflate.h:20
size_t len
Length of raw file image.
Definition: image.h:55
int zlib_deflate(enum deflate_format format, const void *data, size_t len, struct image *extracted)
Extract compressed data to image.
Definition: zlib.c:48

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

◆ zlib_probe()

static int zlib_probe ( struct image image)
static

Probe zlib image.

Parameters
imagezlib image
Return values
rcReturn status code

Definition at line 134 of file zlib.c.

134  {
135  const union zlib_magic *magic;
136 
137  /* Sanity check */
138  if ( image->len < sizeof ( *magic ) ) {
139  DBGC ( image, "ZLIB %s image too short\n", image->name );
140  return -ENOEXEC;
141  }
142  magic = image->data;
143 
144  /* Check magic header */
145  if ( ! zlib_magic_is_valid ( magic ) ) {
146  DBGC ( image, "ZLIB %s invalid magic data\n", image->name );
147  return -ENOEXEC;
148  }
149 
150  return 0;
151 }
const void * data
Read-only data.
Definition: image.h:50
uint16_t magic
Magic signature.
Definition: bzimage.h:6
#define ENOEXEC
Exec format error.
Definition: errno.h:519
#define DBGC(...)
Definition: compiler.h:505
An executable image.
Definition: image.h:23
static int zlib_magic_is_valid(const union zlib_magic *magic)
Check that zlib magic header is valid.
Definition: zlib.h:31
zlib magic header
Definition: zlib.h:18
size_t len
Length of raw file image.
Definition: image.h:55
char * name
Name.
Definition: image.h:37

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

◆ __image_type()

struct image_type zlib_image_type __image_type ( PROBE_NORMAL  )

zlib image type