delta: move delta application to delta.c Move the delta application functions into `delta.c`, next to the similar delta creation functions. Make the `git__delta_apply` functions adhere to other naming and parameter style within the library.
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 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
diff --git a/src/delta-apply.c b/src/delta-apply.c
deleted file mode 100644
index 02ec7b7..0000000
--- a/src/delta-apply.c
+++ /dev/null
@@ -1,166 +0,0 @@
-/*
- * Copyright (C) the libgit2 contributors. All rights reserved.
- *
- * This file is part of libgit2, distributed under the GNU GPL v2 with
- * a Linking Exception. For full terms see the included COPYING file.
- */
-#include "common.h"
-#include "git2/odb.h"
-#include "delta-apply.h"
-
-/*
- * This file was heavily cribbed from BinaryDelta.java in JGit, which
- * itself was heavily cribbed from <code>patch-delta.c</code> in the
- * GIT project. The original delta patching code was written by
- * Nicolas Pitre <nico@cam.org>.
- */
-
-static int hdr_sz(
- size_t *size,
- const unsigned char **delta,
- const unsigned char *end)
-{
- const unsigned char *d = *delta;
- size_t r = 0;
- unsigned int c, shift = 0;
-
- do {
- if (d == end)
- return -1;
- c = *d++;
- r |= (c & 0x7f) << shift;
- shift += 7;
- } while (c & 0x80);
- *delta = d;
- *size = r;
- return 0;
-}
-
-int git__delta_read_header(
- const unsigned char *delta,
- size_t delta_len,
- size_t *base_sz,
- size_t *res_sz)
-{
- const unsigned char *delta_end = delta + delta_len;
- if ((hdr_sz(base_sz, &delta, delta_end) < 0) ||
- (hdr_sz(res_sz, &delta, delta_end) < 0))
- return -1;
- return 0;
-}
-
-#define DELTA_HEADER_BUFFER_LEN 16
-int git__delta_read_header_fromstream(size_t *base_sz, size_t *res_sz, git_packfile_stream *stream)
-{
- static const size_t buffer_len = DELTA_HEADER_BUFFER_LEN;
- unsigned char buffer[DELTA_HEADER_BUFFER_LEN];
- const unsigned char *delta, *delta_end;
- size_t len;
- ssize_t read;
-
- len = read = 0;
- while (len < buffer_len) {
- read = git_packfile_stream_read(stream, &buffer[len], buffer_len - len);
-
- if (read == 0)
- break;
-
- if (read == GIT_EBUFS)
- continue;
-
- len += read;
- }
-
- delta = buffer;
- delta_end = delta + len;
- if ((hdr_sz(base_sz, &delta, delta_end) < 0) ||
- (hdr_sz(res_sz, &delta, delta_end) < 0))
- return -1;
-
- return 0;
-}
-
-int git__delta_apply(
- git_rawobj *out,
- const unsigned char *base,
- size_t base_len,
- const unsigned char *delta,
- size_t delta_len)
-{
- const unsigned char *delta_end = delta + delta_len;
- size_t base_sz, res_sz, alloc_sz;
- unsigned char *res_dp;
-
- /* Check that the base size matches the data we were given;
- * if not we would underflow while accessing data from the
- * base object, resulting in data corruption or segfault.
- */
- if ((hdr_sz(&base_sz, &delta, delta_end) < 0) || (base_sz != base_len)) {
- giterr_set(GITERR_INVALID, "Failed to apply delta. Base size does not match given data");
- return -1;
- }
-
- if (hdr_sz(&res_sz, &delta, delta_end) < 0) {
- giterr_set(GITERR_INVALID, "Failed to apply delta. Base size does not match given data");
- return -1;
- }
-
- GITERR_CHECK_ALLOC_ADD(&alloc_sz, res_sz, 1);
- res_dp = git__malloc(alloc_sz);
- GITERR_CHECK_ALLOC(res_dp);
-
- res_dp[res_sz] = '\0';
- out->data = res_dp;
- out->len = res_sz;
-
- while (delta < delta_end) {
- unsigned char cmd = *delta++;
- if (cmd & 0x80) {
- /* cmd is a copy instruction; copy from the base.
- */
- size_t off = 0, len = 0;
-
- if (cmd & 0x01) off = *delta++;
- if (cmd & 0x02) off |= *delta++ << 8UL;
- if (cmd & 0x04) off |= *delta++ << 16UL;
- if (cmd & 0x08) off |= *delta++ << 24UL;
-
- if (cmd & 0x10) len = *delta++;
- if (cmd & 0x20) len |= *delta++ << 8UL;
- if (cmd & 0x40) len |= *delta++ << 16UL;
- if (!len) len = 0x10000;
-
- if (base_len < off + len || res_sz < len)
- goto fail;
- memcpy(res_dp, base + off, len);
- res_dp += len;
- res_sz -= len;
-
- } else if (cmd) {
- /* cmd is a literal insert instruction; copy from
- * the delta stream itself.
- */
- if (delta_end - delta < cmd || res_sz < cmd)
- goto fail;
- memcpy(res_dp, delta, cmd);
- delta += cmd;
- res_dp += cmd;
- res_sz -= cmd;
-
- } else {
- /* cmd == 0 is reserved for future encodings.
- */
- goto fail;
- }
- }
-
- if (delta != delta_end || res_sz)
- goto fail;
- return 0;
-
-fail:
- git__free(out->data);
- out->data = NULL;
- giterr_set(GITERR_INVALID, "Failed to apply delta");
- return -1;
-}
diff --git a/src/delta-apply.h b/src/delta-apply.h
deleted file mode 100644
index eeeb786..0000000
--- a/src/delta-apply.h
+++ /dev/null
@@ -1,62 +0,0 @@
-/*
- * Copyright (C) the libgit2 contributors. All rights reserved.
- *
- * This file is part of libgit2, distributed under the GNU GPL v2 with
- * a Linking Exception. For full terms see the included COPYING file.
- */
-#ifndef INCLUDE_delta_apply_h__
-#define INCLUDE_delta_apply_h__
-
-#include "odb.h"
-#include "pack.h"
-
-/**
- * Apply a git binary delta to recover the original content.
- *
- * @param out the output buffer to receive the original data.
- * Only out->data and out->len are populated, as this is
- * the only information available in the delta.
- * @param base the base to copy from during copy instructions.
- * @param base_len number of bytes available at base.
- * @param delta the delta to execute copy/insert instructions from.
- * @param delta_len total number of bytes in the delta.
- * @return
- * - 0 on a successful delta unpack.
- * - GIT_ERROR if the delta is corrupt or doesn't match the base.
- */
-extern int git__delta_apply(
- git_rawobj *out,
- const unsigned char *base,
- size_t base_len,
- const unsigned char *delta,
- size_t delta_len);
-
-/**
- * Read the header of a git binary delta.
- *
- * @param delta the delta to execute copy/insert instructions from.
- * @param delta_len total number of bytes in the delta.
- * @param base_sz pointer to store the base size field.
- * @param res_sz pointer to store the result size field.
- * @return
- * - 0 on a successful decoding the header.
- * - GIT_ERROR if the delta is corrupt.
- */
-extern int git__delta_read_header(
- const unsigned char *delta,
- size_t delta_len,
- size_t *base_sz,
- size_t *res_sz);
-
-/**
- * Read the header of a git binary delta
- *
- * This variant reads just enough from the packfile stream to read the
- * delta header.
- */
-extern int git__delta_read_header_fromstream(
- size_t *base_sz,
- size_t *res_sz,
- git_packfile_stream *stream);
-
-#endif
diff --git a/src/delta.c b/src/delta.c
index d72d820..8a4c2a1 100644
--- a/src/delta.c
+++ b/src/delta.c
@@ -441,3 +441,170 @@ git_delta_create(
*delta_size = outpos;
return out;
}
+
+/*
+* Delta application was heavily cribbed from BinaryDelta.java in JGit, which
+* itself was heavily cribbed from <code>patch-delta.c</code> in the
+* GIT project. The original delta patching code was written by
+* Nicolas Pitre <nico@cam.org>.
+*/
+
+static int hdr_sz(
+ size_t *size,
+ const unsigned char **delta,
+ const unsigned char *end)
+{
+ const unsigned char *d = *delta;
+ size_t r = 0;
+ unsigned int c, shift = 0;
+
+ do {
+ if (d == end)
+ return -1;
+ c = *d++;
+ r |= (c & 0x7f) << shift;
+ shift += 7;
+ } while (c & 0x80);
+ *delta = d;
+ *size = r;
+ return 0;
+}
+
+int git_delta_read_header(
+ size_t *base_out,
+ size_t *result_out,
+ const unsigned char *delta,
+ size_t delta_len)
+{
+ const unsigned char *delta_end = delta + delta_len;
+ if ((hdr_sz(base_out, &delta, delta_end) < 0) ||
+ (hdr_sz(result_out, &delta, delta_end) < 0))
+ return -1;
+ return 0;
+}
+
+#define DELTA_HEADER_BUFFER_LEN 16
+int git_delta_read_header_fromstream(
+ size_t *base_sz, size_t *res_sz, git_packfile_stream *stream)
+{
+ static const size_t buffer_len = DELTA_HEADER_BUFFER_LEN;
+ unsigned char buffer[DELTA_HEADER_BUFFER_LEN];
+ const unsigned char *delta, *delta_end;
+ size_t len;
+ ssize_t read;
+
+ len = read = 0;
+ while (len < buffer_len) {
+ read = git_packfile_stream_read(stream, &buffer[len], buffer_len - len);
+
+ if (read == 0)
+ break;
+
+ if (read == GIT_EBUFS)
+ continue;
+
+ len += read;
+ }
+
+ delta = buffer;
+ delta_end = delta + len;
+ if ((hdr_sz(base_sz, &delta, delta_end) < 0) ||
+ (hdr_sz(res_sz, &delta, delta_end) < 0))
+ return -1;
+
+ return 0;
+}
+
+int git_delta_apply(
+ void **out,
+ size_t *out_len,
+ const unsigned char *base,
+ size_t base_len,
+ const unsigned char *delta,
+ size_t delta_len)
+{
+ const unsigned char *delta_end = delta + delta_len;
+ size_t base_sz, res_sz, alloc_sz;
+ unsigned char *res_dp;
+
+ *out = NULL;
+ *out_len = 0;
+
+ /* Check that the base size matches the data we were given;
+ * if not we would underflow while accessing data from the
+ * base object, resulting in data corruption or segfault.
+ */
+ if ((hdr_sz(&base_sz, &delta, delta_end) < 0) || (base_sz != base_len)) {
+ giterr_set(GITERR_INVALID, "Failed to apply delta. Base size does not match given data");
+ return -1;
+ }
+
+ if (hdr_sz(&res_sz, &delta, delta_end) < 0) {
+ giterr_set(GITERR_INVALID, "Failed to apply delta. Base size does not match given data");
+ return -1;
+ }
+
+ GITERR_CHECK_ALLOC_ADD(&alloc_sz, res_sz, 1);
+ res_dp = git__malloc(alloc_sz);
+ GITERR_CHECK_ALLOC(res_dp);
+
+ res_dp[res_sz] = '\0';
+ *out = res_dp;
+ *out_len = res_sz;
+
+ while (delta < delta_end) {
+ unsigned char cmd = *delta++;
+ if (cmd & 0x80) {
+ /* cmd is a copy instruction; copy from the base.
+ */
+ size_t off = 0, len = 0;
+
+ if (cmd & 0x01) off = *delta++;
+ if (cmd & 0x02) off |= *delta++ << 8UL;
+ if (cmd & 0x04) off |= *delta++ << 16UL;
+ if (cmd & 0x08) off |= *delta++ << 24UL;
+
+ if (cmd & 0x10) len = *delta++;
+ if (cmd & 0x20) len |= *delta++ << 8UL;
+ if (cmd & 0x40) len |= *delta++ << 16UL;
+ if (!len) len = 0x10000;
+
+ if (base_len < off + len || res_sz < len)
+ goto fail;
+ memcpy(res_dp, base + off, len);
+ res_dp += len;
+ res_sz -= len;
+
+ }
+ else if (cmd) {
+ /* cmd is a literal insert instruction; copy from
+ * the delta stream itself.
+ */
+ if (delta_end - delta < cmd || res_sz < cmd)
+ goto fail;
+ memcpy(res_dp, delta, cmd);
+ delta += cmd;
+ res_dp += cmd;
+ res_sz -= cmd;
+
+ }
+ else {
+ /* cmd == 0 is reserved for future encodings.
+ */
+ goto fail;
+ }
+ }
+
+ if (delta != delta_end || res_sz)
+ goto fail;
+ return 0;
+
+fail:
+ git__free(*out);
+
+ *out = NULL;
+ *out_len = 0;
+
+ giterr_set(GITERR_INVALID, "Failed to apply delta");
+ return -1;
+}
diff --git a/src/delta.h b/src/delta.h
index 4ca3279..d9d1d0f 100644
--- a/src/delta.h
+++ b/src/delta.h
@@ -6,6 +6,7 @@
#define INCLUDE_git_delta_h__
#include "common.h"
+#include "pack.h"
/* opaque object for delta index */
struct git_delta_index;
@@ -19,8 +20,8 @@ struct git_delta_index;
* before free_delta_index() is called. The returned pointer must be freed
* using free_delta_index().
*/
-extern struct git_delta_index *
-git_delta_create_index(const void *buf, unsigned long bufsize);
+extern struct git_delta_index *git_delta_create_index(
+ const void *buf, unsigned long bufsize);
/*
* free_delta_index: free the index created by create_delta_index()
@@ -111,4 +112,50 @@ GIT_INLINE(unsigned long) git_delta_get_hdr_size(
return size;
}
+/**
+* Apply a git binary delta to recover the original content.
+* The caller is responsible for freeing the returned buffer.
+*
+* @param out the output buffer
+* @param out_len the length of the output buffer
+* @param base the base to copy from during copy instructions.
+* @param base_len number of bytes available at base.
+* @param delta the delta to execute copy/insert instructions from.
+* @param delta_len total number of bytes in the delta.
+* @return 0 on success or an error code
+*/
+extern int git_delta_apply(
+ void **out,
+ size_t *out_len,
+ const unsigned char *base,
+ size_t base_len,
+ const unsigned char *delta,
+ size_t delta_len);
+
+/**
+* Read the header of a git binary delta.
+*
+* @param base_out pointer to store the base size field.
+* @param result_out pointer to store the result size field.
+* @param delta the delta to execute copy/insert instructions from.
+* @param delta_len total number of bytes in the delta.
+* @return 0 on success or an error code
+*/
+extern int git_delta_read_header(
+ size_t *base_out,
+ size_t *result_out,
+ const unsigned char *delta,
+ size_t delta_len);
+
+/**
+ * Read the header of a git binary delta
+ *
+ * This variant reads just enough from the packfile stream to read the
+ * delta header.
+ */
+extern int git_delta_read_header_fromstream(
+ size_t *base_out,
+ size_t *result_out,
+ git_packfile_stream *stream);
+
#endif
diff --git a/src/odb.c b/src/odb.c
index 890e6e2..777e3dc 100644
--- a/src/odb.c
+++ b/src/odb.c
@@ -12,7 +12,7 @@
#include "fileops.h"
#include "hash.h"
#include "odb.h"
-#include "delta-apply.h"
+#include "delta.h"
#include "filter.h"
#include "repository.h"
diff --git a/src/odb_loose.c b/src/odb_loose.c
index 3c33160..228d4c3 100644
--- a/src/odb_loose.c
+++ b/src/odb_loose.c
@@ -12,7 +12,7 @@
#include "fileops.h"
#include "hash.h"
#include "odb.h"
-#include "delta-apply.h"
+#include "delta.h"
#include "filebuf.h"
#include "git2/odb_backend.h"
diff --git a/src/odb_pack.c b/src/odb_pack.c
index 5a57864..244e12b 100644
--- a/src/odb_pack.c
+++ b/src/odb_pack.c
@@ -13,7 +13,7 @@
#include "fileops.h"
#include "hash.h"
#include "odb.h"
-#include "delta-apply.h"
+#include "delta.h"
#include "sha1_lookup.h"
#include "mwindow.h"
#include "pack.h"
diff --git a/src/pack.c b/src/pack.c
index 6a700e2..310f00f 100644
--- a/src/pack.c
+++ b/src/pack.c
@@ -8,7 +8,7 @@
#include "common.h"
#include "odb.h"
#include "pack.h"
-#include "delta-apply.h"
+#include "delta.h"
#include "sha1_lookup.h"
#include "mwindow.h"
#include "fileops.h"
@@ -505,7 +505,7 @@ int git_packfile_resolve_header(
git_mwindow_close(&w_curs);
if ((error = git_packfile_stream_open(&stream, p, curpos)) < 0)
return error;
- error = git__delta_read_header_fromstream(&base_size, size_p, &stream);
+ error = git_delta_read_header_fromstream(&base_size, size_p, &stream);
git_packfile_stream_free(&stream);
if (error < 0)
return error;
@@ -730,8 +730,9 @@ int git_packfile_unpack(
obj->len = 0;
obj->type = GIT_OBJ_BAD;
- error = git__delta_apply(obj, base.data, base.len, delta.data, delta.len);
+ error = git_delta_apply(&obj->data, &obj->len, base.data, base.len, delta.data, delta.len);
obj->type = base_type;
+
/*
* We usually don't want to free the base at this
* point, as we put it into the cache in the previous