toolkit/mozapps/update/updater/bspatch.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /*-
michael@0 2 * Copyright 2003,2004 Colin Percival
michael@0 3 * All rights reserved
michael@0 4 *
michael@0 5 * Redistribution and use in source and binary forms, with or without
michael@0 6 * modification, are permitted providing that the following conditions
michael@0 7 * are met:
michael@0 8 * 1. Redistributions of source code must retain the above copyright
michael@0 9 * notice, this list of conditions and the following disclaimer.
michael@0 10 * 2. Redistributions in binary form must reproduce the above copyright
michael@0 11 * notice, this list of conditions and the following disclaimer in the
michael@0 12 * documentation and/or other materials provided with the distribution.
michael@0 13 *
michael@0 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
michael@0 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
michael@0 16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
michael@0 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
michael@0 18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
michael@0 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
michael@0 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
michael@0 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
michael@0 22 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
michael@0 23 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
michael@0 24 * POSSIBILITY OF SUCH DAMAGE.
michael@0 25 *
michael@0 26 * Changelog:
michael@0 27 * 2005-04-26 - Define the header as a C structure, add a CRC32 checksum to
michael@0 28 * the header, and make all the types 32-bit.
michael@0 29 * --Benjamin Smedberg <benjamin@smedbergs.us>
michael@0 30 */
michael@0 31
michael@0 32 #include "bspatch.h"
michael@0 33 #include "errors.h"
michael@0 34
michael@0 35 #include <sys/stat.h>
michael@0 36 #include <stdlib.h>
michael@0 37 #include <stdio.h>
michael@0 38 #include <fcntl.h>
michael@0 39 #include <string.h>
michael@0 40 #include <limits.h>
michael@0 41
michael@0 42 #if defined(XP_WIN)
michael@0 43 # include <io.h>
michael@0 44 #else
michael@0 45 # include <unistd.h>
michael@0 46 #endif
michael@0 47
michael@0 48 #ifdef XP_WIN
michael@0 49 # include <winsock2.h>
michael@0 50 #else
michael@0 51 # include <arpa/inet.h>
michael@0 52 #endif
michael@0 53
michael@0 54 #ifndef SSIZE_MAX
michael@0 55 # define SSIZE_MAX LONG_MAX
michael@0 56 #endif
michael@0 57
michael@0 58 int
michael@0 59 MBS_ReadHeader(FILE* file, MBSPatchHeader *header)
michael@0 60 {
michael@0 61 size_t s = fread(header, 1, sizeof(MBSPatchHeader), file);
michael@0 62 if (s != sizeof(MBSPatchHeader))
michael@0 63 return READ_ERROR;
michael@0 64
michael@0 65 header->slen = ntohl(header->slen);
michael@0 66 header->scrc32 = ntohl(header->scrc32);
michael@0 67 header->dlen = ntohl(header->dlen);
michael@0 68 header->cblen = ntohl(header->cblen);
michael@0 69 header->difflen = ntohl(header->difflen);
michael@0 70 header->extralen = ntohl(header->extralen);
michael@0 71
michael@0 72 struct stat hs;
michael@0 73 s = fstat(fileno(file), &hs);
michael@0 74 if (s)
michael@0 75 return READ_ERROR;
michael@0 76
michael@0 77 if (memcmp(header->tag, "MBDIFF10", 8) != 0)
michael@0 78 return UNEXPECTED_BSPATCH_ERROR;
michael@0 79
michael@0 80 if (sizeof(MBSPatchHeader) +
michael@0 81 header->cblen +
michael@0 82 header->difflen +
michael@0 83 header->extralen != uint32_t(hs.st_size))
michael@0 84 return UNEXPECTED_BSPATCH_ERROR;
michael@0 85
michael@0 86 return OK;
michael@0 87 }
michael@0 88
michael@0 89 int
michael@0 90 MBS_ApplyPatch(const MBSPatchHeader *header, FILE* patchFile,
michael@0 91 unsigned char *fbuffer, FILE* file)
michael@0 92 {
michael@0 93 unsigned char *fbufend = fbuffer + header->slen;
michael@0 94
michael@0 95 unsigned char *buf = (unsigned char*) malloc(header->cblen +
michael@0 96 header->difflen +
michael@0 97 header->extralen);
michael@0 98 if (!buf)
michael@0 99 return BSPATCH_MEM_ERROR;
michael@0 100
michael@0 101 int rv = OK;
michael@0 102
michael@0 103 size_t r = header->cblen + header->difflen + header->extralen;
michael@0 104 unsigned char *wb = buf;
michael@0 105 while (r) {
michael@0 106 const size_t count = (r > SSIZE_MAX) ? SSIZE_MAX : r;
michael@0 107 size_t c = fread(wb, 1, count, patchFile);
michael@0 108 if (c != count) {
michael@0 109 rv = READ_ERROR;
michael@0 110 goto end;
michael@0 111 }
michael@0 112
michael@0 113 r -= c;
michael@0 114 wb += c;
michael@0 115 }
michael@0 116
michael@0 117 {
michael@0 118 MBSPatchTriple *ctrlsrc = (MBSPatchTriple*) buf;
michael@0 119 unsigned char *diffsrc = buf + header->cblen;
michael@0 120 unsigned char *extrasrc = diffsrc + header->difflen;
michael@0 121
michael@0 122 MBSPatchTriple *ctrlend = (MBSPatchTriple*) diffsrc;
michael@0 123 unsigned char *diffend = extrasrc;
michael@0 124 unsigned char *extraend = extrasrc + header->extralen;
michael@0 125
michael@0 126 do {
michael@0 127 ctrlsrc->x = ntohl(ctrlsrc->x);
michael@0 128 ctrlsrc->y = ntohl(ctrlsrc->y);
michael@0 129 ctrlsrc->z = ntohl(ctrlsrc->z);
michael@0 130
michael@0 131 #ifdef DEBUG_bsmedberg
michael@0 132 printf("Applying block:\n"
michael@0 133 " x: %u\n"
michael@0 134 " y: %u\n"
michael@0 135 " z: %i\n",
michael@0 136 ctrlsrc->x,
michael@0 137 ctrlsrc->y,
michael@0 138 ctrlsrc->z);
michael@0 139 #endif
michael@0 140
michael@0 141 /* Add x bytes from oldfile to x bytes from the diff block */
michael@0 142
michael@0 143 if (fbuffer + ctrlsrc->x > fbufend ||
michael@0 144 diffsrc + ctrlsrc->x > diffend) {
michael@0 145 rv = UNEXPECTED_BSPATCH_ERROR;
michael@0 146 goto end;
michael@0 147 }
michael@0 148 for (uint32_t i = 0; i < ctrlsrc->x; ++i) {
michael@0 149 diffsrc[i] += fbuffer[i];
michael@0 150 }
michael@0 151 if ((uint32_t) fwrite(diffsrc, 1, ctrlsrc->x, file) != ctrlsrc->x) {
michael@0 152 rv = WRITE_ERROR;
michael@0 153 goto end;
michael@0 154 }
michael@0 155 fbuffer += ctrlsrc->x;
michael@0 156 diffsrc += ctrlsrc->x;
michael@0 157
michael@0 158 /* Copy y bytes from the extra block */
michael@0 159
michael@0 160 if (extrasrc + ctrlsrc->y > extraend) {
michael@0 161 rv = UNEXPECTED_BSPATCH_ERROR;
michael@0 162 goto end;
michael@0 163 }
michael@0 164 if ((uint32_t) fwrite(extrasrc, 1, ctrlsrc->y, file) != ctrlsrc->y) {
michael@0 165 rv = WRITE_ERROR;
michael@0 166 goto end;
michael@0 167 }
michael@0 168 extrasrc += ctrlsrc->y;
michael@0 169
michael@0 170 /* "seek" forwards in oldfile by z bytes */
michael@0 171
michael@0 172 if (fbuffer + ctrlsrc->z > fbufend) {
michael@0 173 rv = UNEXPECTED_BSPATCH_ERROR;
michael@0 174 goto end;
michael@0 175 }
michael@0 176 fbuffer += ctrlsrc->z;
michael@0 177
michael@0 178 /* and on to the next control block */
michael@0 179
michael@0 180 ++ctrlsrc;
michael@0 181 } while (ctrlsrc < ctrlend);
michael@0 182 }
michael@0 183
michael@0 184 end:
michael@0 185 free(buf);
michael@0 186 return rv;
michael@0 187 }

mercurial