Show More
@@ -15,64 +15,62 b' from . import _bdiff' | |||||
15 | ffi = _bdiff.ffi |
|
15 | ffi = _bdiff.ffi | |
16 | lib = _bdiff.lib |
|
16 | lib = _bdiff.lib | |
17 |
|
17 | |||
18 | if True: |
|
18 | def blocks(sa, sb): | |
19 | if True: |
|
19 | a = ffi.new("struct bdiff_line**") | |
20 | def blocks(sa, sb): |
|
20 | b = ffi.new("struct bdiff_line**") | |
21 | a = ffi.new("struct bdiff_line**") |
|
21 | ac = ffi.new("char[]", str(sa)) | |
22 | b = ffi.new("struct bdiff_line**") |
|
22 | bc = ffi.new("char[]", str(sb)) | |
23 | ac = ffi.new("char[]", str(sa)) |
|
23 | l = ffi.new("struct bdiff_hunk*") | |
24 | bc = ffi.new("char[]", str(sb)) |
|
24 | try: | |
25 | l = ffi.new("struct bdiff_hunk*") |
|
25 | an = lib.bdiff_splitlines(ac, len(sa), a) | |
26 | try: |
|
26 | bn = lib.bdiff_splitlines(bc, len(sb), b) | |
27 | an = lib.bdiff_splitlines(ac, len(sa), a) |
|
27 | if not a[0] or not b[0]: | |
28 | bn = lib.bdiff_splitlines(bc, len(sb), b) |
|
28 | raise MemoryError | |
29 | if not a[0] or not b[0]: |
|
29 | count = lib.bdiff_diff(a[0], an, b[0], bn, l) | |
30 | raise MemoryError |
|
30 | if count < 0: | |
31 | count = lib.bdiff_diff(a[0], an, b[0], bn, l) |
|
31 | raise MemoryError | |
32 |
|
|
32 | rl = [None] * count | |
33 | raise MemoryError |
|
33 | h = l.next | |
34 | rl = [None] * count |
|
34 | i = 0 | |
35 | h = l.next |
|
35 | while h: | |
36 | i = 0 |
|
36 | rl[i] = (h.a1, h.a2, h.b1, h.b2) | |
37 |
|
|
37 | h = h.next | |
38 | rl[i] = (h.a1, h.a2, h.b1, h.b2) |
|
38 | i += 1 | |
39 | h = h.next |
|
39 | finally: | |
40 | i += 1 |
|
40 | lib.free(a[0]) | |
41 | finally: |
|
41 | lib.free(b[0]) | |
42 | lib.free(a[0]) |
|
42 | lib.bdiff_freehunks(l.next) | |
43 | lib.free(b[0]) |
|
43 | return rl | |
44 | lib.bdiff_freehunks(l.next) |
|
|||
45 | return rl |
|
|||
46 |
|
44 | |||
47 |
|
|
45 | def bdiff(sa, sb): | |
48 |
|
|
46 | a = ffi.new("struct bdiff_line**") | |
49 |
|
|
47 | b = ffi.new("struct bdiff_line**") | |
50 |
|
|
48 | ac = ffi.new("char[]", str(sa)) | |
51 |
|
|
49 | bc = ffi.new("char[]", str(sb)) | |
52 |
|
|
50 | l = ffi.new("struct bdiff_hunk*") | |
53 |
|
|
51 | try: | |
54 |
|
|
52 | an = lib.bdiff_splitlines(ac, len(sa), a) | |
55 |
|
|
53 | bn = lib.bdiff_splitlines(bc, len(sb), b) | |
56 |
|
|
54 | if not a[0] or not b[0]: | |
57 |
|
|
55 | raise MemoryError | |
58 |
|
|
56 | count = lib.bdiff_diff(a[0], an, b[0], bn, l) | |
59 |
|
|
57 | if count < 0: | |
60 |
|
|
58 | raise MemoryError | |
61 |
|
|
59 | rl = [] | |
62 |
|
|
60 | h = l.next | |
63 |
|
|
61 | la = lb = 0 | |
64 |
|
|
62 | while h: | |
65 |
|
|
63 | if h.a1 != la or h.b1 != lb: | |
66 |
|
|
64 | lgt = (b[0] + h.b1).l - (b[0] + lb).l | |
67 |
|
|
65 | rl.append(struct.pack(">lll", (a[0] + la).l - a[0].l, | |
68 | (a[0] + h.a1).l - a[0].l, lgt)) |
|
66 | (a[0] + h.a1).l - a[0].l, lgt)) | |
69 |
|
|
67 | rl.append(str(ffi.buffer((b[0] + lb).l, lgt))) | |
70 |
|
|
68 | la = h.a2 | |
71 |
|
|
69 | lb = h.b2 | |
72 |
|
|
70 | h = h.next | |
73 |
|
71 | |||
74 |
|
|
72 | finally: | |
75 |
|
|
73 | lib.free(a[0]) | |
76 |
|
|
74 | lib.free(b[0]) | |
77 |
|
|
75 | lib.bdiff_freehunks(l.next) | |
78 |
|
|
76 | return "".join(rl) |
@@ -14,37 +14,35 b' from . import _mpatch' | |||||
14 | ffi = _mpatch.ffi |
|
14 | ffi = _mpatch.ffi | |
15 | lib = _mpatch.lib |
|
15 | lib = _mpatch.lib | |
16 |
|
16 | |||
17 | if True: |
|
17 | @ffi.def_extern() | |
18 | if True: |
|
18 | def cffi_get_next_item(arg, pos): | |
19 | @ffi.def_extern() |
|
19 | all, bins = ffi.from_handle(arg) | |
20 | def cffi_get_next_item(arg, pos): |
|
20 | container = ffi.new("struct mpatch_flist*[1]") | |
21 | all, bins = ffi.from_handle(arg) |
|
21 | to_pass = ffi.new("char[]", str(bins[pos])) | |
22 | container = ffi.new("struct mpatch_flist*[1]") |
|
22 | all.append(to_pass) | |
23 | to_pass = ffi.new("char[]", str(bins[pos])) |
|
23 | r = lib.mpatch_decode(to_pass, len(to_pass) - 1, container) | |
24 | all.append(to_pass) |
|
24 | if r < 0: | |
25 | r = lib.mpatch_decode(to_pass, len(to_pass) - 1, container) |
|
25 | return ffi.NULL | |
26 | if r < 0: |
|
26 | return container[0] | |
27 | return ffi.NULL |
|
|||
28 | return container[0] |
|
|||
29 |
|
27 | |||
30 |
|
|
28 | def patches(text, bins): | |
31 |
|
|
29 | lgt = len(bins) | |
32 |
|
|
30 | all = [] | |
33 |
|
|
31 | if not lgt: | |
34 |
|
|
32 | return text | |
35 |
|
|
33 | arg = (all, bins) | |
36 |
|
|
34 | patch = lib.mpatch_fold(ffi.new_handle(arg), | |
37 |
|
|
35 | lib.cffi_get_next_item, 0, lgt) | |
38 |
|
|
36 | if not patch: | |
39 |
|
|
37 | raise mpatchError("cannot decode chunk") | |
40 |
|
|
38 | outlen = lib.mpatch_calcsize(len(text), patch) | |
41 |
|
|
39 | if outlen < 0: | |
42 |
|
|
40 | lib.mpatch_lfree(patch) | |
43 |
|
|
41 | raise mpatchError("inconsistency detected") | |
44 |
|
|
42 | buf = ffi.new("char[]", outlen) | |
45 |
|
|
43 | if lib.mpatch_apply(buf, text, len(text), patch) < 0: | |
46 |
|
|
44 | lib.mpatch_lfree(patch) | |
47 |
|
|
45 | raise mpatchError("error applying patches") | |
48 |
|
|
46 | res = ffi.buffer(buf, outlen)[:] | |
49 |
|
|
47 | lib.mpatch_lfree(patch) | |
50 |
|
|
48 | return res |
General Comments 0
You need to be logged in to leave comments.
Login now