iPXE
Functions
gzip.c File Reference

gzip compressed images More...

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

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
static int gzip_extract (struct image *image, struct image *extracted)
 Extract gzip image. More...
 
static int gzip_probe (struct image *image)
 Probe gzip image. More...
 
struct image_type gzip_image_type __image_type (PROBE_NORMAL)
 gzip image type More...
 

Detailed Description

gzip compressed images

Definition in file gzip.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ gzip_extract()

static int gzip_extract ( struct image image,
struct image extracted 
)
static

Extract gzip image.

Parameters
imageImage
extractedExtracted image
Return values
rcReturn status code

Definition at line 48 of file gzip.c.

48  {
49  const struct gzip_header *header;
50  const struct gzip_extra_header *extra;
51  const struct gzip_crc_header *crc;
52  const struct gzip_footer *footer;
53  const void *data;
54  size_t extra_len;
55  size_t string_len;
56  size_t len;
57  unsigned int strings;
58  int rc;
59 
60  /* Sanity check */
61  assert ( image->len >= ( sizeof ( *header ) + sizeof ( *footer ) ) );
62  data = image->data;
63  len = image->len;
64 
65  /* Extract footer */
66  assert ( len >= sizeof ( *footer ) );
67  len -= sizeof ( *footer );
68  footer = ( data + len );
69 
70  /* Extract fixed header */
71  assert ( len >= sizeof ( *header ) );
72  header = data;
73  data += sizeof ( *header );
74  len -= sizeof ( *header );
75 
76  /* Skip extra header, if present */
77  if ( header->flags & GZIP_FL_EXTRA ) {
78  if ( len < sizeof ( *extra ) ) {
79  DBGC ( image, "GZIP %s overlength extra header\n",
80  image->name );
81  return -EINVAL;
82  }
83  extra = data;
84  data += sizeof ( *extra );
85  len -= sizeof ( *extra );
86  extra_len = le16_to_cpu ( extra->len );
87  if ( len < extra_len ) {
88  DBGC ( image, "GZIP %s overlength extra header\n",
89  image->name );
90  return -EINVAL;
91  }
92  data += extra_len;
93  len -= extra_len;
94  }
95 
96  /* Skip name and/or comment, if present */
97  strings = 0;
98  if ( header->flags & GZIP_FL_NAME )
99  strings++;
100  if ( header->flags & GZIP_FL_COMMENT )
101  strings++;
102  while ( strings-- ) {
103  string_len = strnlen ( data, len );
104  if ( string_len == len ) {
105  DBGC ( image, "GZIP %s overlength name/comment\n",
106  image->name );
107  return -EINVAL;
108  }
109  data += ( string_len + 1 /* NUL */ );
110  len -= ( string_len + 1 /* NUL */ );
111  }
112 
113  /* Skip CRC, if present */
114  if ( header->flags & GZIP_FL_HCRC ) {
115  if ( len < sizeof ( *crc ) ) {
116  DBGC ( image, "GZIP %s overlength CRC header\n",
117  image->name );
118  return -EINVAL;
119  }
120  data += sizeof ( *crc );
121  len -= sizeof ( *crc );
122  }
123 
124  /* Presize extracted image */
125  if ( ( rc = image_set_len ( extracted,
126  le32_to_cpu ( footer->len ) ) ) != 0 ) {
127  DBGC ( image, "GZIP %s could not presize: %s\n",
128  image->name, strerror ( rc ) );
129  return rc;
130  }
131 
132  /* Decompress image (expanding if necessary) */
133  if ( ( rc = zlib_deflate ( DEFLATE_RAW, data, len,
134  extracted ) ) != 0 ) {
135  DBGC ( image, "GZIP %s could not decompress: %s\n",
136  image->name, strerror ( rc ) );
137  return rc;
138  }
139 
140  return 0;
141 }
#define EINVAL
Invalid argument.
Definition: errno.h:428
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
uint16_t crc
CRC-16.
Definition: gzip.h:58
gzip extra header
Definition: gzip.h:50
#define le32_to_cpu(value)
Definition: byteswap.h:113
#define GZIP_FL_NAME
File name is present.
Definition: gzip.h:44
const void * data
Read-only data.
Definition: image.h:50
uint8_t extra
Signature extra byte.
Definition: smbios.h:17
#define GZIP_FL_HCRC
CRC header is present.
Definition: gzip.h:38
#define DBGC(...)
Definition: compiler.h:505
An executable image.
Definition: image.h:23
gzip header
Definition: gzip.h:16
gzip CRC header
Definition: gzip.h:56
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
ring len
Length.
Definition: dwmac.h:231
#define GZIP_FL_EXTRA
Extra header is present.
Definition: gzip.h:41
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
size_t len
Length of raw file image.
Definition: image.h:55
size_t strnlen(const char *src, size_t max)
Get length of string.
Definition: string.c:255
#define le16_to_cpu(value)
Definition: byteswap.h:112
int image_set_len(struct image *image, size_t len)
Set image length.
Definition: image.c:244
Raw DEFLATE data (no header or footer)
Definition: deflate.h:18
struct ena_llq_option header
Header locations.
Definition: ena.h:16
uint8_t data[48]
Additional event data.
Definition: ena.h:22
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
#define GZIP_FL_COMMENT
File comment is present.
Definition: gzip.h:47
char * name
Name.
Definition: image.h:37

References assert(), gzip_crc_header::crc, gzip_footer::crc, data, image::data, DBGC, DEFLATE_RAW, EINVAL, extra, GZIP_FL_COMMENT, GZIP_FL_EXTRA, GZIP_FL_HCRC, GZIP_FL_NAME, header, image_set_len(), le16_to_cpu, le32_to_cpu, image::len, gzip_footer::len, len, image::name, rc, strerror(), strnlen(), and zlib_deflate().

◆ gzip_probe()

static int gzip_probe ( struct image image)
static

Probe gzip image.

Parameters
imagegzip image
Return values
rcReturn status code

Definition at line 149 of file gzip.c.

149  {
150  const struct gzip_header *header;
151  const struct gzip_footer *footer;
152 
153  /* Sanity check */
154  if ( image->len < ( sizeof ( *header ) + sizeof ( *footer ) ) ) {
155  DBGC ( image, "GZIP %s image too short\n", image->name );
156  return -ENOEXEC;
157  }
158  header = image->data;
159 
160  /* Check magic header */
161  if ( header->magic != cpu_to_be16 ( GZIP_MAGIC ) ) {
162  DBGC ( image, "GZIP %s invalid magic\n", image->name );
163  return -ENOEXEC;
164  }
165 
166  return 0;
167 }
#define cpu_to_be16(value)
Definition: byteswap.h:109
const void * data
Read-only data.
Definition: image.h:50
#define ENOEXEC
Exec format error.
Definition: errno.h:519
#define DBGC(...)
Definition: compiler.h:505
An executable image.
Definition: image.h:23
gzip header
Definition: gzip.h:16
size_t len
Length of raw file image.
Definition: image.h:55
#define GZIP_MAGIC
Magic ID.
Definition: gzip.h:32
struct ena_llq_option header
Header locations.
Definition: ena.h:16
char * name
Name.
Definition: image.h:37

References cpu_to_be16, image::data, DBGC, ENOEXEC, GZIP_MAGIC, header, image::len, and image::name.

◆ __image_type()

struct image_type gzip_image_type __image_type ( PROBE_NORMAL  )

gzip image type