1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/mozapps/update/updater/bspatch.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,187 @@ 1.4 +/*- 1.5 + * Copyright 2003,2004 Colin Percival 1.6 + * All rights reserved 1.7 + * 1.8 + * Redistribution and use in source and binary forms, with or without 1.9 + * modification, are permitted providing that the following conditions 1.10 + * are met: 1.11 + * 1. Redistributions of source code must retain the above copyright 1.12 + * notice, this list of conditions and the following disclaimer. 1.13 + * 2. Redistributions in binary form must reproduce the above copyright 1.14 + * notice, this list of conditions and the following disclaimer in the 1.15 + * documentation and/or other materials provided with the distribution. 1.16 + * 1.17 + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1.18 + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 1.19 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1.20 + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 1.21 + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1.22 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 1.23 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 1.24 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 1.25 + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 1.26 + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 1.27 + * POSSIBILITY OF SUCH DAMAGE. 1.28 + * 1.29 + * Changelog: 1.30 + * 2005-04-26 - Define the header as a C structure, add a CRC32 checksum to 1.31 + * the header, and make all the types 32-bit. 1.32 + * --Benjamin Smedberg <benjamin@smedbergs.us> 1.33 + */ 1.34 + 1.35 +#include "bspatch.h" 1.36 +#include "errors.h" 1.37 + 1.38 +#include <sys/stat.h> 1.39 +#include <stdlib.h> 1.40 +#include <stdio.h> 1.41 +#include <fcntl.h> 1.42 +#include <string.h> 1.43 +#include <limits.h> 1.44 + 1.45 +#if defined(XP_WIN) 1.46 +# include <io.h> 1.47 +#else 1.48 +# include <unistd.h> 1.49 +#endif 1.50 + 1.51 +#ifdef XP_WIN 1.52 +# include <winsock2.h> 1.53 +#else 1.54 +# include <arpa/inet.h> 1.55 +#endif 1.56 + 1.57 +#ifndef SSIZE_MAX 1.58 +# define SSIZE_MAX LONG_MAX 1.59 +#endif 1.60 + 1.61 +int 1.62 +MBS_ReadHeader(FILE* file, MBSPatchHeader *header) 1.63 +{ 1.64 + size_t s = fread(header, 1, sizeof(MBSPatchHeader), file); 1.65 + if (s != sizeof(MBSPatchHeader)) 1.66 + return READ_ERROR; 1.67 + 1.68 + header->slen = ntohl(header->slen); 1.69 + header->scrc32 = ntohl(header->scrc32); 1.70 + header->dlen = ntohl(header->dlen); 1.71 + header->cblen = ntohl(header->cblen); 1.72 + header->difflen = ntohl(header->difflen); 1.73 + header->extralen = ntohl(header->extralen); 1.74 + 1.75 + struct stat hs; 1.76 + s = fstat(fileno(file), &hs); 1.77 + if (s) 1.78 + return READ_ERROR; 1.79 + 1.80 + if (memcmp(header->tag, "MBDIFF10", 8) != 0) 1.81 + return UNEXPECTED_BSPATCH_ERROR; 1.82 + 1.83 + if (sizeof(MBSPatchHeader) + 1.84 + header->cblen + 1.85 + header->difflen + 1.86 + header->extralen != uint32_t(hs.st_size)) 1.87 + return UNEXPECTED_BSPATCH_ERROR; 1.88 + 1.89 + return OK; 1.90 +} 1.91 + 1.92 +int 1.93 +MBS_ApplyPatch(const MBSPatchHeader *header, FILE* patchFile, 1.94 + unsigned char *fbuffer, FILE* file) 1.95 +{ 1.96 + unsigned char *fbufend = fbuffer + header->slen; 1.97 + 1.98 + unsigned char *buf = (unsigned char*) malloc(header->cblen + 1.99 + header->difflen + 1.100 + header->extralen); 1.101 + if (!buf) 1.102 + return BSPATCH_MEM_ERROR; 1.103 + 1.104 + int rv = OK; 1.105 + 1.106 + size_t r = header->cblen + header->difflen + header->extralen; 1.107 + unsigned char *wb = buf; 1.108 + while (r) { 1.109 + const size_t count = (r > SSIZE_MAX) ? SSIZE_MAX : r; 1.110 + size_t c = fread(wb, 1, count, patchFile); 1.111 + if (c != count) { 1.112 + rv = READ_ERROR; 1.113 + goto end; 1.114 + } 1.115 + 1.116 + r -= c; 1.117 + wb += c; 1.118 + } 1.119 + 1.120 + { 1.121 + MBSPatchTriple *ctrlsrc = (MBSPatchTriple*) buf; 1.122 + unsigned char *diffsrc = buf + header->cblen; 1.123 + unsigned char *extrasrc = diffsrc + header->difflen; 1.124 + 1.125 + MBSPatchTriple *ctrlend = (MBSPatchTriple*) diffsrc; 1.126 + unsigned char *diffend = extrasrc; 1.127 + unsigned char *extraend = extrasrc + header->extralen; 1.128 + 1.129 + do { 1.130 + ctrlsrc->x = ntohl(ctrlsrc->x); 1.131 + ctrlsrc->y = ntohl(ctrlsrc->y); 1.132 + ctrlsrc->z = ntohl(ctrlsrc->z); 1.133 + 1.134 +#ifdef DEBUG_bsmedberg 1.135 + printf("Applying block:\n" 1.136 + " x: %u\n" 1.137 + " y: %u\n" 1.138 + " z: %i\n", 1.139 + ctrlsrc->x, 1.140 + ctrlsrc->y, 1.141 + ctrlsrc->z); 1.142 +#endif 1.143 + 1.144 + /* Add x bytes from oldfile to x bytes from the diff block */ 1.145 + 1.146 + if (fbuffer + ctrlsrc->x > fbufend || 1.147 + diffsrc + ctrlsrc->x > diffend) { 1.148 + rv = UNEXPECTED_BSPATCH_ERROR; 1.149 + goto end; 1.150 + } 1.151 + for (uint32_t i = 0; i < ctrlsrc->x; ++i) { 1.152 + diffsrc[i] += fbuffer[i]; 1.153 + } 1.154 + if ((uint32_t) fwrite(diffsrc, 1, ctrlsrc->x, file) != ctrlsrc->x) { 1.155 + rv = WRITE_ERROR; 1.156 + goto end; 1.157 + } 1.158 + fbuffer += ctrlsrc->x; 1.159 + diffsrc += ctrlsrc->x; 1.160 + 1.161 + /* Copy y bytes from the extra block */ 1.162 + 1.163 + if (extrasrc + ctrlsrc->y > extraend) { 1.164 + rv = UNEXPECTED_BSPATCH_ERROR; 1.165 + goto end; 1.166 + } 1.167 + if ((uint32_t) fwrite(extrasrc, 1, ctrlsrc->y, file) != ctrlsrc->y) { 1.168 + rv = WRITE_ERROR; 1.169 + goto end; 1.170 + } 1.171 + extrasrc += ctrlsrc->y; 1.172 + 1.173 + /* "seek" forwards in oldfile by z bytes */ 1.174 + 1.175 + if (fbuffer + ctrlsrc->z > fbufend) { 1.176 + rv = UNEXPECTED_BSPATCH_ERROR; 1.177 + goto end; 1.178 + } 1.179 + fbuffer += ctrlsrc->z; 1.180 + 1.181 + /* and on to the next control block */ 1.182 + 1.183 + ++ctrlsrc; 1.184 + } while (ctrlsrc < ctrlend); 1.185 + } 1.186 + 1.187 +end: 1.188 + free(buf); 1.189 + return rv; 1.190 +}