Home | History | Annotate | Download | only in applypatch
      1 /*
      2  * Copyright (C) 2009 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 // See imgdiff.c in this directory for a description of the patch file
     18 // format.
     19 
     20 #include <stdio.h>
     21 #include <sys/stat.h>
     22 #include <errno.h>
     23 #include <unistd.h>
     24 #include <string.h>
     25 
     26 #include "zlib.h"
     27 #include "mincrypt/sha.h"
     28 #include "applypatch.h"
     29 #include "imgdiff.h"
     30 #include "utils.h"
     31 
     32 /*
     33  * Apply the patch given in 'patch_filename' to the source data given
     34  * by (old_data, old_size).  Write the patched output to the 'output'
     35  * file, and update the SHA context with the output data as well.
     36  * Return 0 on success.
     37  */
     38 int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size,
     39                     const Value* patch,
     40                     SinkFn sink, void* token, SHA_CTX* ctx) {
     41     ssize_t pos = 12;
     42     char* header = patch->data;
     43     if (patch->size < 12) {
     44         printf("patch too short to contain header\n");
     45         return -1;
     46     }
     47 
     48     // IMGDIFF2 uses CHUNK_NORMAL, CHUNK_DEFLATE, and CHUNK_RAW.
     49     // (IMGDIFF1, which is no longer supported, used CHUNK_NORMAL and
     50     // CHUNK_GZIP.)
     51     if (memcmp(header, "IMGDIFF2", 8) != 0) {
     52         printf("corrupt patch file header (magic number)\n");
     53         return -1;
     54     }
     55 
     56     int num_chunks = Read4(header+8);
     57 
     58     int i;
     59     for (i = 0; i < num_chunks; ++i) {
     60         // each chunk's header record starts with 4 bytes.
     61         if (pos + 4 > patch->size) {
     62             printf("failed to read chunk %d record\n", i);
     63             return -1;
     64         }
     65         int type = Read4(patch->data + pos);
     66         pos += 4;
     67 
     68         if (type == CHUNK_NORMAL) {
     69             char* normal_header = patch->data + pos;
     70             pos += 24;
     71             if (pos > patch->size) {
     72                 printf("failed to read chunk %d normal header data\n", i);
     73                 return -1;
     74             }
     75 
     76             size_t src_start = Read8(normal_header);
     77             size_t src_len = Read8(normal_header+8);
     78             size_t patch_offset = Read8(normal_header+16);
     79 
     80             ApplyBSDiffPatch(old_data + src_start, src_len,
     81                              patch, patch_offset, sink, token, ctx);
     82         } else if (type == CHUNK_RAW) {
     83             char* raw_header = patch->data + pos;
     84             pos += 4;
     85             if (pos > patch->size) {
     86                 printf("failed to read chunk %d raw header data\n", i);
     87                 return -1;
     88             }
     89 
     90             ssize_t data_len = Read4(raw_header);
     91 
     92             if (pos + data_len > patch->size) {
     93                 printf("failed to read chunk %d raw data\n", i);
     94                 return -1;
     95             }
     96             SHA_update(ctx, patch->data + pos, data_len);
     97             if (sink((unsigned char*)patch->data + pos,
     98                      data_len, token) != data_len) {
     99                 printf("failed to write chunk %d raw data\n", i);
    100                 return -1;
    101             }
    102             pos += data_len;
    103         } else if (type == CHUNK_DEFLATE) {
    104             // deflate chunks have an additional 60 bytes in their chunk header.
    105             char* deflate_header = patch->data + pos;
    106             pos += 60;
    107             if (pos > patch->size) {
    108                 printf("failed to read chunk %d deflate header data\n", i);
    109                 return -1;
    110             }
    111 
    112             size_t src_start = Read8(deflate_header);
    113             size_t src_len = Read8(deflate_header+8);
    114             size_t patch_offset = Read8(deflate_header+16);
    115             size_t expanded_len = Read8(deflate_header+24);
    116             size_t target_len = Read8(deflate_header+32);
    117             int level = Read4(deflate_header+40);
    118             int method = Read4(deflate_header+44);
    119             int windowBits = Read4(deflate_header+48);
    120             int memLevel = Read4(deflate_header+52);
    121             int strategy = Read4(deflate_header+56);
    122 
    123             // Decompress the source data; the chunk header tells us exactly
    124             // how big we expect it to be when decompressed.
    125 
    126             unsigned char* expanded_source = malloc(expanded_len);
    127             if (expanded_source == NULL) {
    128                 printf("failed to allocate %d bytes for expanded_source\n",
    129                        expanded_len);
    130                 return -1;
    131             }
    132 
    133             z_stream strm;
    134             strm.zalloc = Z_NULL;
    135             strm.zfree = Z_NULL;
    136             strm.opaque = Z_NULL;
    137             strm.avail_in = src_len;
    138             strm.next_in = (unsigned char*)(old_data + src_start);
    139             strm.avail_out = expanded_len;
    140             strm.next_out = expanded_source;
    141 
    142             int ret;
    143             ret = inflateInit2(&strm, -15);
    144             if (ret != Z_OK) {
    145                 printf("failed to init source inflation: %d\n", ret);
    146                 return -1;
    147             }
    148 
    149             // Because we've provided enough room to accommodate the output
    150             // data, we expect one call to inflate() to suffice.
    151             ret = inflate(&strm, Z_SYNC_FLUSH);
    152             if (ret != Z_STREAM_END) {
    153                 printf("source inflation returned %d\n", ret);
    154                 return -1;
    155             }
    156             // We should have filled the output buffer exactly.
    157             if (strm.avail_out != 0) {
    158                 printf("source inflation short by %d bytes\n", strm.avail_out);
    159                 return -1;
    160             }
    161             inflateEnd(&strm);
    162 
    163             // Next, apply the bsdiff patch (in memory) to the uncompressed
    164             // data.
    165             unsigned char* uncompressed_target_data;
    166             ssize_t uncompressed_target_size;
    167             if (ApplyBSDiffPatchMem(expanded_source, expanded_len,
    168                                     patch, patch_offset,
    169                                     &uncompressed_target_data,
    170                                     &uncompressed_target_size) != 0) {
    171                 return -1;
    172             }
    173 
    174             // Now compress the target data and append it to the output.
    175 
    176             // we're done with the expanded_source data buffer, so we'll
    177             // reuse that memory to receive the output of deflate.
    178             unsigned char* temp_data = expanded_source;
    179             ssize_t temp_size = expanded_len;
    180             if (temp_size < 32768) {
    181                 // ... unless the buffer is too small, in which case we'll
    182                 // allocate a fresh one.
    183                 free(temp_data);
    184                 temp_data = malloc(32768);
    185                 temp_size = 32768;
    186             }
    187 
    188             // now the deflate stream
    189             strm.zalloc = Z_NULL;
    190             strm.zfree = Z_NULL;
    191             strm.opaque = Z_NULL;
    192             strm.avail_in = uncompressed_target_size;
    193             strm.next_in = uncompressed_target_data;
    194             ret = deflateInit2(&strm, level, method, windowBits, memLevel, strategy);
    195             do {
    196                 strm.avail_out = temp_size;
    197                 strm.next_out = temp_data;
    198                 ret = deflate(&strm, Z_FINISH);
    199                 ssize_t have = temp_size - strm.avail_out;
    200 
    201                 if (sink(temp_data, have, token) != have) {
    202                     printf("failed to write %ld compressed bytes to output\n",
    203                            (long)have);
    204                     return -1;
    205                 }
    206                 SHA_update(ctx, temp_data, have);
    207             } while (ret != Z_STREAM_END);
    208             deflateEnd(&strm);
    209 
    210             free(temp_data);
    211             free(uncompressed_target_data);
    212         } else {
    213             printf("patch chunk %d is unknown type %d\n", i, type);
    214             return -1;
    215         }
    216     }
    217 
    218     return 0;
    219 }
    220