Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • boot/generic/src/gzip.c

    rb169619 r63a045c  
    3232#include <stddef.h>
    3333#include <errno.h>
    34 #include <mem.h>
     34#include <memstr.h>
    3535#include <byteorder.h>
    3636#include <gzip.h>
     
    6363} __attribute__((packed)) gzip_footer_t;
    6464
    65 /** Check GZIP signature
    66  *
    67  * Checks whether the source buffer start with a GZIP signature.
    68  *
    69  * @param[in] src    Source data buffer.
    70  * @param[in] srclen Source buffer size (bytes).
    71  *
    72  * @return True if GZIP signature found.
    73  * @return False if no GZIP signature found.
    74  *
    75  */
    76 bool gzip_check(const void *src, size_t srclen)
     65size_t gzip_size(const void *src, size_t srclen)
    7766{
    78         if ((srclen < (sizeof(gzip_header_t) + sizeof(gzip_footer_t))))
    79                 return false;
     67        gzip_header_t header;
     68        gzip_footer_t footer;
    8069
    81         gzip_header_t header;
     70        if ((srclen < sizeof(header)) || (srclen < sizeof(footer)))
     71                return 0;
     72
    8273        memcpy(&header, src, sizeof(header));
     74        memcpy(&footer, src + srclen - sizeof(footer), sizeof(footer));
    8375
    8476        if ((header.id1 != GZIP_ID1) ||
     
    8678            (header.method != GZIP_METHOD_DEFLATE) ||
    8779            ((header.flags & (~GZIP_FLAGS_MASK)) != 0))
    88                 return false;
    89 
    90         return true;
    91 }
    92 
    93 /** Get uncompressed size
    94  *
    95  * Note that the uncompressed size is read from the GZIP footer
    96  * (and not calculated by acutally decompressing the GZip archive).
    97  * Thus the source of the GZip archive needs to be trusted.
    98  *
    99  * @param[in]  src    Source data buffer.
    100  * @param[out] srclen Source buffer size (bytes).
    101  *
    102  * @return Uncompressed size.
    103  *
    104  */
    105 size_t gzip_size(const void *src, size_t srclen)
    106 {
    107         if (!gzip_check(src, srclen))
    10880                return 0;
    109 
    110         gzip_footer_t footer;
    111         memcpy(&footer, src + srclen - sizeof(footer), sizeof(footer));
    11281
    11382        return uint32_t_le2host(footer.size);
     
    11685/** Expand GZIP compressed data
    11786 *
    118  * The routine compares the output buffer size with
    119  * the size encoded in the input stream. This
     87 * The routine allocates the output buffer based
     88 * on the size encoded in the input stream. This
    12089 * effectively limits the size of the uncompressed
    12190 * data to 4 GiB (expanding input streams that actually
     
    139108int gzip_expand(const void *src, size_t srclen, void *dest, size_t destlen)
    140109{
    141         if (!gzip_check(src, srclen))
     110        gzip_header_t header;
     111        gzip_footer_t footer;
     112
     113        if ((srclen < sizeof(header)) || (srclen < sizeof(footer)))
    142114                return EINVAL;
    143115
    144116        /* Decode header and footer */
    145117
    146         gzip_header_t header;
    147118        memcpy(&header, src, sizeof(header));
     119        memcpy(&footer, src + srclen - sizeof(footer), sizeof(footer));
    148120
    149         gzip_footer_t footer;
    150         memcpy(&footer, src + srclen - sizeof(footer), sizeof(footer));
     121        if ((header.id1 != GZIP_ID1) ||
     122            (header.id2 != GZIP_ID2) ||
     123            (header.method != GZIP_METHOD_DEFLATE) ||
     124            ((header.flags & (~GZIP_FLAGS_MASK)) != 0))
     125                return EINVAL;
    151126
    152127        if (destlen != uint32_t_le2host(footer.size))
Note: See TracChangeset for help on using the changeset viewer.