Hash :
8628c62d
Author :
Date :
2018-03-15T01:21:57
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
/*
* Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/queue.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <zlib.h>
#include <sha1.h>
#include "got_error.h"
#include "got_repository.h"
#include "got_object.h"
#include "got_delta_lib.h"
#include "got_path_lib.h"
#include "got_zbuf_lib.h"
#ifndef MIN
#define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
#endif
struct got_delta *
got_delta_open(const char *path_packfile, off_t offset, size_t tslen,
int type, size_t size, off_t data_offset)
{
struct got_delta *delta;
delta = calloc(1, sizeof(*delta));
if (delta == NULL)
return NULL;
delta->path_packfile = strdup(path_packfile);
if (delta->path_packfile == NULL) {
free(delta);
return NULL;
}
delta->type = type;
delta->offset = offset;
delta->tslen = tslen;
delta->size = size;
delta->data_offset = data_offset;
return delta;
}
void
got_delta_close(struct got_delta *delta)
{
free(delta->path_packfile);
free(delta);
}
const struct got_error *
got_delta_chain_get_base_type(int *type, struct got_delta_chain *deltas)
{
struct got_delta *delta;
/* The first delta in the chain should represent the base object. */
delta = SIMPLEQ_FIRST(&deltas->entries);
if (delta->type == GOT_OBJ_TYPE_COMMIT ||
delta->type == GOT_OBJ_TYPE_TREE ||
delta->type == GOT_OBJ_TYPE_BLOB ||
delta->type == GOT_OBJ_TYPE_TAG) {
*type = delta->type;
return NULL;
}
return got_error(GOT_ERR_BAD_DELTA_CHAIN);
}
/* Fetch another (required) byte from the delta stream. */
static const struct got_error *
next_delta_byte(const uint8_t **p, size_t *remain)
{
if (--(*remain) == 0)
return got_error(GOT_ERR_BAD_DELTA);
(*p)++;
return NULL;
}
static const struct got_error *
parse_size(uint64_t *size, const uint8_t **p, size_t *remain)
{
const struct got_error *err = NULL;
int i = 0;
*size = 0;
do {
/* We do not support size values which don't fit in 64 bit. */
if (i > 9)
return got_error(GOT_ERR_NO_SPACE);
if (i == 0)
*size = ((**p) & GOT_DELTA_SIZE_VAL_MASK);
else {
size_t shift = GOT_DELTA_SIZE_SHIFT * i;
*size |= (((**p) & GOT_DELTA_SIZE_VAL_MASK) << shift);
}
if (((**p) & GOT_DELTA_SIZE_MORE) == 0)
break;
i++;
err = next_delta_byte(p, remain);
} while (err == NULL);
return err;
}
static const struct got_error *
parse_opcode(off_t *offset, size_t *len, const uint8_t **p, size_t *remain)
{
const struct got_error *err = NULL;
off_t o = 0;
size_t l = 0;
uint8_t opcode = **p;
if (opcode & GOT_DELTA_COPY_OFF1) {
err = next_delta_byte(p, remain);
if (err)
return err;
o = (off_t)(**p);
}
if (opcode & GOT_DELTA_COPY_OFF2) {
err = next_delta_byte(p, remain);
if (err)
return err;
o |= ((off_t)(**p)) << 8;
}
if (opcode & GOT_DELTA_COPY_OFF3) {
err = next_delta_byte(p, remain);
if (err)
return err;
o |= ((off_t)(**p)) << 16;
}
if (opcode & GOT_DELTA_COPY_OFF4) {
err = next_delta_byte(p, remain);
if (err)
return err;
o |= ((off_t)(**p)) << 24;
}
if (opcode & GOT_DELTA_COPY_LEN1) {
err = next_delta_byte(p, remain);
if (err)
return err;
l = (off_t)(**p);
}
if (opcode & GOT_DELTA_COPY_LEN2) {
err = next_delta_byte(p, remain);
if (err)
return err;
l |= ((off_t)(**p)) << 8;
}
if (opcode & GOT_DELTA_COPY_LEN3) {
err = next_delta_byte(p, remain);
if (err)
return err;
l |= ((off_t)(**p)) << 16;
}
if (o == 0)
o = GOT_DELTA_COPY_DEFAULT_OFF;
if (l == 0)
l = GOT_DELTA_COPY_DEFAULT_LEN;
*offset = o;
*len = l;
return NULL;
}
static const struct got_error *
copy_from_base(FILE *base_file, off_t offset, size_t size, FILE *outfile)
{
if (fseeko(base_file, offset, SEEK_SET) != 0)
return got_error_from_errno();
while (size > 0) {
uint8_t data[2048];
size_t len = MIN(size, sizeof(data));
size_t n;
n = fread(data, len, 1, base_file);
if (n != 1)
return got_ferror(base_file, GOT_ERR_IO);
n = fwrite(data, len, 1, outfile);
if (n != 1)
return got_ferror(outfile, GOT_ERR_IO);
size -= len;
}
return NULL;
}
static const struct got_error *
copy_from_delta(const uint8_t **p, size_t *remain, size_t len, FILE *outfile)
{
size_t n;
if (*remain < len)
return got_error(GOT_ERR_BAD_DELTA);
n = fwrite(*p, len, 1, outfile);
if (n != 1)
return got_ferror(outfile, GOT_ERR_IO);
*p += len;
*remain -= len;
return NULL;
}
static const struct got_error *
parse_delta_sizes(uint64_t *base_size, uint64_t *result_size,
const uint8_t **p, size_t *remain)
{
const struct got_error *err;
/* Read the two size fields at the beginning of the stream. */
err = parse_size(base_size, p, remain);
if (err)
return err;
err = next_delta_byte(p, remain);
if (err)
return err;
err = parse_size(result_size, p, remain);
if (err)
return err;
return NULL;
}
const struct got_error *
got_delta_get_sizes(uint64_t *base_size, uint64_t *result_size,
const uint8_t *delta_buf, size_t delta_len)
{
size_t remain;
const uint8_t *p;
if (delta_len < GOT_DELTA_STREAM_LENGTH_MIN)
return got_error(GOT_ERR_BAD_DELTA);
p = delta_buf;
remain = delta_len;
return parse_delta_sizes(base_size, result_size, &p, &remain);
}
const struct got_error *
got_delta_apply_in_mem(uint8_t *base_buf, const uint8_t *delta_buf,
size_t delta_len, uint8_t *outbuf, size_t *outsize)
{
const struct got_error *err = NULL;
uint64_t base_size, result_size;
size_t remain;
const uint8_t *p;
*outsize= 0;
if (delta_len < GOT_DELTA_STREAM_LENGTH_MIN)
return got_error(GOT_ERR_BAD_DELTA);
p = delta_buf;
remain = delta_len;
err = parse_delta_sizes(&base_size, &result_size, &p, &remain);
if (err)
return err;
/* Decode and execute copy instructions from the delta stream. */
err = next_delta_byte(&p, &remain);
while (err == NULL && remain > 0) {
if (*p & GOT_DELTA_BASE_COPY) {
off_t offset = 0;
size_t len = 0;
err = parse_opcode(&offset, &len, &p, &remain);
if (err)
break;
memcpy(outbuf + *outsize, base_buf + offset, len);
if (err == NULL) {
*outsize += len;
if (remain > 0) {
p++;
remain--;
}
}
} else {
size_t len = (size_t)*p;
if (len == 0) {
err = got_error(GOT_ERR_BAD_DELTA);
break;
}
err = next_delta_byte(&p, &remain);
if (err)
break;
if (remain < len)
return got_error(GOT_ERR_BAD_DELTA);
memcpy(outbuf + *outsize, p, len);
p += len;
remain -= len;
*outsize += len;
}
}
if (*outsize != result_size)
err = got_error(GOT_ERR_BAD_DELTA);
return err;
}
const struct got_error *
got_delta_apply(FILE *base_file, const uint8_t *delta_buf,
size_t delta_len, FILE *outfile)
{
const struct got_error *err = NULL;
uint64_t base_size, result_size;
size_t remain, outsize = 0;
const uint8_t *p;
FILE *memstream = NULL;
char *memstream_buf = NULL;
size_t memstream_size = 0;
if (delta_len < GOT_DELTA_STREAM_LENGTH_MIN)
return got_error(GOT_ERR_BAD_DELTA);
p = delta_buf;
remain = delta_len;
err = parse_delta_sizes(&base_size, &result_size, &p, &remain);
if (err)
return err;
if (result_size < GOT_DELTA_RESULT_SIZE_CACHED_MAX)
memstream = open_memstream(&memstream_buf, &memstream_size);
/* Decode and execute copy instructions from the delta stream. */
err = next_delta_byte(&p, &remain);
while (err == NULL && remain > 0) {
if (*p & GOT_DELTA_BASE_COPY) {
off_t offset = 0;
size_t len = 0;
err = parse_opcode(&offset, &len, &p, &remain);
if (err)
break;
err = copy_from_base(base_file, offset, len,
memstream ? memstream : outfile);
if (err == NULL) {
outsize += len;
if (remain > 0) {
p++;
remain--;
}
}
} else {
size_t len = (size_t)*p;
if (len == 0) {
err = got_error(GOT_ERR_BAD_DELTA);
break;
}
err = next_delta_byte(&p, &remain);
if (err)
break;
err = copy_from_delta(&p, &remain, len,
memstream ? memstream : outfile);
if (err == NULL)
outsize += len;
}
}
if (outsize != result_size)
err = got_error(GOT_ERR_BAD_DELTA);
if (memstream != NULL) {
fclose(memstream);
if (err == NULL) {
size_t n;
n = fwrite(memstream_buf, 1, memstream_size, outfile);
if (n != memstream_size)
err = got_ferror(outfile, GOT_ERR_IO);
}
free(memstream_buf);
}
if (err == NULL)
rewind(outfile);
return err;
}