bdiff.c
488 lines
| 10.0 KiB
| text/x-c
|
CLexer
/ mercurial / bdiff.c
|
r400 | /* | ||
bdiff.c - efficient binary diff extension for Mercurial | ||||
|
r2859 | Copyright 2005, 2006 Matt Mackall <mpm@selenic.com> | ||
|
r400 | |||
This software may be used and distributed according to the terms of | ||||
the GNU General Public License, incorporated herein by reference. | ||||
Based roughly on Python difflib | ||||
*/ | ||||
|
r16749 | #define PY_SSIZE_T_CLEAN | ||
|
r400 | #include <Python.h> | ||
#include <stdlib.h> | ||||
#include <string.h> | ||||
|
r5341 | #include <limits.h> | ||
|
r867 | |||
|
r11364 | #include "util.h" | ||
|
r29444 | #include "bitmanipulation.h" | ||
|
r11364 | |||
|
r400 | struct line { | ||
|
r16749 | int hash, n, e; | ||
Py_ssize_t len; | ||||
|
r400 | const char *l; | ||
}; | ||||
|
r474 | struct pos { | ||
int pos, len; | ||||
}; | ||||
|
r13089 | struct hunk; | ||
|
r400 | struct hunk { | ||
int a1, a2, b1, b2; | ||||
|
r13089 | struct hunk *next; | ||
|
r400 | }; | ||
|
r16749 | static int splitlines(const char *a, Py_ssize_t len, struct line **lr) | ||
|
r400 | { | ||
|
r13732 | unsigned hash; | ||
|
r13731 | int i; | ||
|
r400 | const char *p, *b = a; | ||
|
r5340 | const char * const plast = a + len - 1; | ||
|
r400 | struct line *l; | ||
/* count the lines */ | ||||
i = 1; /* extra line for sentinel */ | ||||
for (p = a; p < a + len; p++) | ||||
|
r5340 | if (*p == '\n' || p == plast) | ||
|
r400 | i++; | ||
|
r1978 | *lr = l = (struct line *)malloc(sizeof(struct line) * i); | ||
|
r400 | if (!l) | ||
return -1; | ||||
/* build the line array and calculate hashes */ | ||||
|
r13732 | hash = 0; | ||
|
r400 | for (p = a; p < a + len; p++) { | ||
|
r5342 | /* Leonid Yuriev's hash */ | ||
|
r13732 | hash = (hash * 1664525) + (unsigned char)*p + 1013904223; | ||
|
r5342 | |||
|
r5340 | if (*p == '\n' || p == plast) { | ||
|
r13732 | l->hash = hash; | ||
hash = 0; | ||||
|
r400 | l->len = p - b + 1; | ||
l->l = b; | ||||
|
r5341 | l->n = INT_MAX; | ||
|
r400 | l++; | ||
b = p + 1; | ||||
} | ||||
} | ||||
/* set up a sentinel */ | ||||
|
r13732 | l->hash = 0; | ||
|
r13731 | l->len = 0; | ||
|
r400 | l->l = a + len; | ||
return i - 1; | ||||
} | ||||
|
r13729 | static inline int cmp(struct line *a, struct line *b) | ||
|
r400 | { | ||
|
r13732 | return a->hash != b->hash || a->len != b->len || memcmp(a->l, b->l, a->len); | ||
|
r400 | } | ||
static int equatelines(struct line *a, int an, struct line *b, int bn) | ||||
{ | ||||
|
r5452 | int i, j, buckets = 1, t, scale; | ||
struct pos *h = NULL; | ||||
|
r400 | |||
/* build a hash table of the next highest power of 2 */ | ||||
while (buckets < bn + 1) | ||||
buckets *= 2; | ||||
|
r5339 | /* try to allocate a large hash table to avoid collisions */ | ||
|
r5452 | for (scale = 4; scale; scale /= 2) { | ||
|
r5339 | h = (struct pos *)malloc(scale * buckets * sizeof(struct pos)); | ||
|
r5452 | if (h) | ||
break; | ||||
} | ||||
|
r5339 | |||
|
r474 | if (!h) | ||
|
r400 | return 0; | ||
|
r5339 | buckets = buckets * scale - 1; | ||
|
r400 | /* clear the hash table */ | ||
|
r474 | for (i = 0; i <= buckets; i++) { | ||
|
r29013 | h[i].pos = -1; | ||
|
r474 | h[i].len = 0; | ||
} | ||||
|
r400 | |||
/* add lines to the hash table chains */ | ||||
|
r29013 | for (i = 0; i < bn; i++) { | ||
|
r400 | /* find the equivalence class */ | ||
|
r29013 | for (j = b[i].hash & buckets; h[j].pos != -1; | ||
|
r474 | j = (j + 1) & buckets) | ||
if (!cmp(b + i, b + h[j].pos)) | ||||
|
r400 | break; | ||
/* add to the head of the equivalence class */ | ||||
|
r474 | b[i].n = h[j].pos; | ||
|
r433 | b[i].e = j; | ||
|
r474 | h[j].pos = i; | ||
h[j].len++; /* keep track of popularity */ | ||||
|
r400 | } | ||
/* compute popularity threshold */ | ||||
|
r9534 | t = (bn >= 31000) ? bn / 1000 : 1000000 / (bn + 1); | ||
|
r400 | |||
/* match items in a to their equivalence class in b */ | ||||
for (i = 0; i < an; i++) { | ||||
/* find the equivalence class */ | ||||
|
r29013 | for (j = a[i].hash & buckets; h[j].pos != -1; | ||
|
r474 | j = (j + 1) & buckets) | ||
if (!cmp(a + i, b + h[j].pos)) | ||||
|
r400 | break; | ||
|
r433 | a[i].e = j; /* use equivalence class for quick compare */ | ||
|
r1542 | if (h[j].len <= t) | ||
|
r474 | a[i].n = h[j].pos; /* point to head of match list */ | ||
|
r400 | else | ||
|
r29013 | a[i].n = -1; /* too popular */ | ||
|
r400 | } | ||
/* discard hash tables */ | ||||
free(h); | ||||
return 1; | ||||
} | ||||
|
r474 | static int longest_match(struct line *a, struct line *b, struct pos *pos, | ||
|
r400 | int a1, int a2, int b1, int b2, int *omi, int *omj) | ||
{ | ||||
|
r29323 | int mi = a1, mj = b1, mk = 0, i, j, k, half; | ||
|
r29015 | |||
/* window our search on large regions to better bound | ||||
worst-case performance. by choosing a window at the end, we | ||||
reduce skipping overhead on the b chains. */ | ||||
if (a2 - a1 > 30000) | ||||
a1 = a2 - 30000; | ||||
half = (a1 + a2) / 2; | ||||
|
r400 | |||
for (i = a1; i < a2; i++) { | ||||
|
r29013 | /* skip all lines in b after the current block */ | ||
for (j = a[i].n; j >= b2; j = b[j].n) | ||||
|
r400 | ; | ||
/* loop through all lines match a[i] in b */ | ||||
|
r29013 | for (; j >= b1; j = b[j].n) { | ||
|
r400 | /* does this extend an earlier match? */ | ||
|
r29322 | for (k = 1; j - k >= b1 && i - k >= a1; k++) { | ||
/* reached an earlier match? */ | ||||
if (pos[j - k].pos == i - k) { | ||||
k += pos[j - k].len; | ||||
break; | ||||
} | ||||
/* previous line mismatch? */ | ||||
if (a[i - k].e != b[j - k].e) | ||||
break; | ||||
} | ||||
|
r474 | pos[j].pos = i; | ||
pos[j].len = k; | ||||
|
r400 | |||
|
r29014 | /* best match so far? we prefer matches closer | ||
to the middle to balance recursion */ | ||||
if (k > mk || (k == mk && (i <= mi || i < half))) { | ||||
|
r400 | mi = i; | ||
mj = j; | ||||
mk = k; | ||||
} | ||||
} | ||||
} | ||||
if (mk) { | ||||
mi = mi - mk + 1; | ||||
mj = mj - mk + 1; | ||||
} | ||||
|
r29323 | /* expand match to include subsequent popular lines */ | ||
|
r400 | while (mi + mk < a2 && mj + mk < b2 && | ||
|
r433 | a[mi + mk].e == b[mj + mk].e) | ||
|
r400 | mk++; | ||
|
r29323 | *omi = mi; | ||
*omj = mj; | ||||
|
r5341 | |||
|
r29323 | return mk; | ||
|
r400 | } | ||
|
r13089 | static struct hunk *recurse(struct line *a, struct line *b, struct pos *pos, | ||
int a1, int a2, int b1, int b2, struct hunk *l) | ||||
|
r400 | { | ||
int i, j, k; | ||||
|
r10500 | while (1) { | ||
/* find the longest match in this chunk */ | ||||
k = longest_match(a, b, pos, a1, a2, b1, b2, &i, &j); | ||||
if (!k) | ||||
|
r13089 | return l; | ||
|
r400 | |||
|
r10500 | /* and recurse on the remaining chunks on either side */ | ||
|
r13089 | l = recurse(a, b, pos, a1, i, b1, j, l); | ||
if (!l) | ||||
return NULL; | ||||
l->next = (struct hunk *)malloc(sizeof(struct hunk)); | ||||
if (!l->next) | ||||
return NULL; | ||||
l = l->next; | ||||
l->a1 = i; | ||||
l->a2 = i + k; | ||||
l->b1 = j; | ||||
l->b2 = j + k; | ||||
l->next = NULL; | ||||
/* tail-recursion didn't happen, so do equivalent iteration */ | ||||
|
r10500 | a1 = i + k; | ||
b1 = j + k; | ||||
} | ||||
|
r400 | } | ||
|
r13089 | static int diff(struct line *a, int an, struct line *b, int bn, | ||
struct hunk *base) | ||||
|
r400 | { | ||
|
r7104 | struct hunk *curr; | ||
|
r474 | struct pos *pos; | ||
|
r13089 | int t, count = 0; | ||
|
r433 | |||
/* allocate and fill arrays */ | ||||
t = equatelines(a, an, b, bn); | ||||
|
r5571 | pos = (struct pos *)calloc(bn ? bn : 1, sizeof(struct pos)); | ||
|
r13089 | |||
if (pos && t) { | ||||
/* generate the matching block list */ | ||||
curr = recurse(a, b, pos, 0, an, 0, bn, base); | ||||
if (!curr) | ||||
return -1; | ||||
|
r433 | |||
|
r13089 | /* sentinel end hunk */ | ||
curr->next = (struct hunk *)malloc(sizeof(struct hunk)); | ||||
if (!curr->next) | ||||
|
r13090 | return -1; | ||
|
r13089 | curr = curr->next; | ||
curr->a1 = curr->a2 = an; | ||||
curr->b1 = curr->b2 = bn; | ||||
curr->next = NULL; | ||||
|
r433 | } | ||
|
r474 | free(pos); | ||
|
r7104 | |||
|
r7625 | /* normalize the hunk list, try to push each hunk towards the end */ | ||
|
r13089 | for (curr = base->next; curr; curr = curr->next) { | ||
struct hunk *next = curr->next; | ||||
|
r7104 | |||
|
r13089 | if (!next) | ||
|
r7104 | break; | ||
|
r29010 | if (curr->a2 == next->a1 || curr->b2 == next->b1) | ||
|
r29011 | while (curr->a2 < an && curr->b2 < bn | ||
|
r29012 | && next->a1 < next->a2 | ||
&& next->b1 < next->b2 | ||||
|
r29011 | && !cmp(a + curr->a2, b + curr->b2)) { | ||
curr->a2++; | ||||
next->a1++; | ||||
curr->b2++; | ||||
next->b1++; | ||||
} | ||||
|
r7104 | } | ||
|
r13089 | for (curr = base->next; curr; curr = curr->next) | ||
count++; | ||||
return count; | ||||
} | ||||
static void freehunks(struct hunk *l) | ||||
{ | ||||
struct hunk *n; | ||||
for (; l; l = n) { | ||||
n = l->next; | ||||
free(l); | ||||
} | ||||
|
r433 | } | ||
static PyObject *blocks(PyObject *self, PyObject *args) | ||||
{ | ||||
|
r435 | PyObject *sa, *sb, *rl = NULL, *m; | ||
|
r433 | struct line *a, *b; | ||
|
r13089 | struct hunk l, *h; | ||
int an, bn, count, pos = 0; | ||||
|
r400 | |||
|
r19962 | l.next = NULL; | ||
|
r400 | if (!PyArg_ParseTuple(args, "SS:bdiff", &sa, &sb)) | ||
return NULL; | ||||
|
r11364 | an = splitlines(PyBytes_AsString(sa), PyBytes_Size(sa), &a); | ||
bn = splitlines(PyBytes_AsString(sb), PyBytes_Size(sb), &b); | ||||
|
r433 | if (!a || !b) | ||
goto nomem; | ||||
|
r13089 | count = diff(a, an, b, bn, &l); | ||
if (count < 0) | ||||
|
r433 | goto nomem; | ||
|
r13089 | rl = PyList_New(count); | ||
if (!rl) | ||||
goto nomem; | ||||
for (h = l.next; h; h = h->next) { | ||||
|
r433 | m = Py_BuildValue("iiii", h->a1, h->a2, h->b1, h->b2); | ||
PyList_SetItem(rl, pos, m); | ||||
pos++; | ||||
} | ||||
nomem: | ||||
free(a); | ||||
free(b); | ||||
|
r13089 | freehunks(l.next); | ||
|
r433 | return rl ? rl : PyErr_NoMemory(); | ||
} | ||||
static PyObject *bdiff(PyObject *self, PyObject *args) | ||||
{ | ||||
|
r15222 | char *sa, *sb, *rb; | ||
|
r3335 | PyObject *result = NULL; | ||
|
r433 | struct line *al, *bl; | ||
|
r13089 | struct hunk l, *h; | ||
|
r16749 | int an, bn, count; | ||
Py_ssize_t len = 0, la, lb; | ||||
|
r16477 | PyThreadState *_save; | ||
|
r433 | |||
|
r19962 | l.next = NULL; | ||
|
r3369 | if (!PyArg_ParseTuple(args, "s#s#:bdiff", &sa, &la, &sb, &lb)) | ||
|
r433 | return NULL; | ||
|
r18551 | if (la > UINT_MAX || lb > UINT_MAX) { | ||
PyErr_SetString(PyExc_ValueError, "bdiff inputs too large"); | ||||
return NULL; | ||||
} | ||||
|
r16477 | _save = PyEval_SaveThread(); | ||
|
r3335 | an = splitlines(sa, la, &al); | ||
bn = splitlines(sb, lb, &bl); | ||||
|
r433 | if (!al || !bl) | ||
|
r400 | goto nomem; | ||
|
r13089 | count = diff(al, an, bl, bn, &l); | ||
if (count < 0) | ||||
|
r433 | goto nomem; | ||
|
r400 | |||
/* calculate length of output */ | ||||
|
r3335 | la = lb = 0; | ||
|
r13089 | for (h = l.next; h; h = h->next) { | ||
|
r400 | if (h->a1 != la || h->b1 != lb) | ||
len += 12 + bl[h->b1].l - bl[lb].l; | ||||
la = h->a2; | ||||
lb = h->b2; | ||||
} | ||||
|
r16477 | PyEval_RestoreThread(_save); | ||
_save = NULL; | ||||
|
r400 | |||
|
r11364 | result = PyBytes_FromStringAndSize(NULL, len); | ||
|
r400 | if (!result) | ||
goto nomem; | ||||
/* build binary patch */ | ||||
|
r11364 | rb = PyBytes_AsString(result); | ||
|
r400 | la = lb = 0; | ||
|
r13089 | for (h = l.next; h; h = h->next) { | ||
|
r400 | if (h->a1 != la || h->b1 != lb) { | ||
len = bl[h->b1].l - bl[lb].l; | ||||
|
r18551 | putbe32((uint32_t)(al[la].l - al->l), rb); | ||
putbe32((uint32_t)(al[h->a1].l - al->l), rb + 4); | ||||
putbe32((uint32_t)len, rb + 8); | ||||
|
r400 | memcpy(rb + 12, bl[lb].l, len); | ||
rb += 12 + len; | ||||
} | ||||
la = h->a2; | ||||
lb = h->b2; | ||||
} | ||||
nomem: | ||||
|
r16477 | if (_save) | ||
PyEval_RestoreThread(_save); | ||||
|
r400 | free(al); | ||
free(bl); | ||||
|
r13089 | freehunks(l.next); | ||
|
r400 | return result ? result : PyErr_NoMemory(); | ||
} | ||||
|
r15530 | /* | ||
* If allws != 0, remove all whitespace (' ', \t and \r). Otherwise, | ||||
* reduce whitespace sequences to a single space and trim remaining whitespace | ||||
* from end of lines. | ||||
*/ | ||||
static PyObject *fixws(PyObject *self, PyObject *args) | ||||
{ | ||||
PyObject *s, *result = NULL; | ||||
char allws, c; | ||||
const char *r; | ||||
|
r16749 | Py_ssize_t i, rlen, wlen = 0; | ||
|
r15530 | char *w; | ||
if (!PyArg_ParseTuple(args, "Sb:fixws", &s, &allws)) | ||||
return NULL; | ||||
r = PyBytes_AsString(s); | ||||
rlen = PyBytes_Size(s); | ||||
|
r16071 | w = (char *)malloc(rlen ? rlen : 1); | ||
|
r15530 | if (!w) | ||
goto nomem; | ||||
for (i = 0; i != rlen; i++) { | ||||
c = r[i]; | ||||
if (c == ' ' || c == '\t' || c == '\r') { | ||||
if (!allws && (wlen == 0 || w[wlen - 1] != ' ')) | ||||
w[wlen++] = ' '; | ||||
} else if (c == '\n' && !allws | ||||
&& wlen > 0 && w[wlen - 1] == ' ') { | ||||
w[wlen - 1] = '\n'; | ||||
} else { | ||||
w[wlen++] = c; | ||||
} | ||||
} | ||||
result = PyBytes_FromStringAndSize(w, wlen); | ||||
nomem: | ||||
free(w); | ||||
return result ? result : PyErr_NoMemory(); | ||||
} | ||||
|
r400 | static char mdiff_doc[] = "Efficient binary diff."; | ||
static PyMethodDef methods[] = { | ||||
{"bdiff", bdiff, METH_VARARGS, "calculate a binary diff\n"}, | ||||
|
r433 | {"blocks", blocks, METH_VARARGS, "find a list of matching lines\n"}, | ||
|
r15530 | {"fixws", fixws, METH_VARARGS, "normalize diff whitespaces\n"}, | ||
|
r400 | {NULL, NULL} | ||
}; | ||||
|
r11364 | #ifdef IS_PY3K | ||
static struct PyModuleDef bdiff_module = { | ||||
PyModuleDef_HEAD_INIT, | ||||
"bdiff", | ||||
mdiff_doc, | ||||
-1, | ||||
methods | ||||
}; | ||||
PyMODINIT_FUNC PyInit_bdiff(void) | ||||
{ | ||||
return PyModule_Create(&bdiff_module); | ||||
} | ||||
#else | ||||
|
r400 | PyMODINIT_FUNC initbdiff(void) | ||
{ | ||||
Py_InitModule3("bdiff", methods, mdiff_doc); | ||||
} | ||||
|
r11364 | #endif | ||
|
r1542 | |||