Details
Details
- Reviewers
ryanmce - Group Reviewers
hg-reviewers - Commits
- rHG761355833867: mpatch: reformat function prototypes with clang-format
Diff Detail
Diff Detail
- Repository
- rHG Mercurial
- Lint
Lint Skipped - Unit
Unit Tests Skipped
( )
| ryanmce |
| hg-reviewers |
| Lint Skipped |
| Unit Tests Skipped |
| Path | Packages | |||
|---|---|---|---|---|
| M | mercurial/mpatch.h (10 lines) | |||
| M | mercurial/mpatch.c (12 lines) |
| #ifndef _HG_MPATCH_H_ | #ifndef _HG_MPATCH_H_ | ||||
| #define _HG_MPATCH_H_ | #define _HG_MPATCH_H_ | ||||
| #define MPATCH_ERR_NO_MEM -3 | #define MPATCH_ERR_NO_MEM -3 | ||||
| #define MPATCH_ERR_CANNOT_BE_DECODED -2 | #define MPATCH_ERR_CANNOT_BE_DECODED -2 | ||||
| #define MPATCH_ERR_INVALID_PATCH -1 | #define MPATCH_ERR_INVALID_PATCH -1 | ||||
| struct mpatch_frag { | struct mpatch_frag { | ||||
| int start, end, len; | int start, end, len; | ||||
| const char *data; | const char *data; | ||||
| }; | }; | ||||
| struct mpatch_flist { | struct mpatch_flist { | ||||
| struct mpatch_frag *base, *head, *tail; | struct mpatch_frag *base, *head, *tail; | ||||
| }; | }; | ||||
| int mpatch_decode(const char *bin, ssize_t len, struct mpatch_flist** res); | int mpatch_decode(const char *bin, ssize_t len, struct mpatch_flist **res); | ||||
| ssize_t mpatch_calcsize(ssize_t len, struct mpatch_flist *l); | ssize_t mpatch_calcsize(ssize_t len, struct mpatch_flist *l); | ||||
| void mpatch_lfree(struct mpatch_flist *a); | void mpatch_lfree(struct mpatch_flist *a); | ||||
| int mpatch_apply(char *buf, const char *orig, ssize_t len, | int mpatch_apply(char *buf, const char *orig, ssize_t len, | ||||
| struct mpatch_flist *l); | struct mpatch_flist *l); | ||||
| struct mpatch_flist *mpatch_fold(void *bins, | struct mpatch_flist * | ||||
| struct mpatch_flist* (*get_next_item)(void*, ssize_t), | mpatch_fold(void *bins, struct mpatch_flist *(*get_next_item)(void *, ssize_t), | ||||
| ssize_t start, ssize_t end); | ssize_t start, ssize_t end); | ||||
| #endif | #endif | ||||
| { | { | ||||
| return a->tail - a->head; | return a->tail - a->head; | ||||
| } | } | ||||
| /* move hunks in source that are less cut to dest, compensating | /* move hunks in source that are less cut to dest, compensating | ||||
| for changes in offset. the last hunk may be split if necessary. | for changes in offset. the last hunk may be split if necessary. | ||||
| */ | */ | ||||
| static int gather(struct mpatch_flist *dest, struct mpatch_flist *src, int cut, | static int gather(struct mpatch_flist *dest, struct mpatch_flist *src, int cut, | ||||
| int offset) | int offset) | ||||
| { | { | ||||
| struct mpatch_frag *d = dest->tail, *s = src->head; | struct mpatch_frag *d = dest->tail, *s = src->head; | ||||
| int postend, c, l; | int postend, c, l; | ||||
| while (s != src->tail) { | while (s != src->tail) { | ||||
| if (s->start + offset >= cut) | if (s->start + offset >= cut) | ||||
| break; /* we've gone far enough */ | break; /* we've gone far enough */ | ||||
| src->head = s; | src->head = s; | ||||
| return offset; | return offset; | ||||
| } | } | ||||
| /* combine hunk lists a and b, while adjusting b for offset changes in a/ | /* combine hunk lists a and b, while adjusting b for offset changes in a/ | ||||
| this deletes a and b and returns the resultant list. */ | this deletes a and b and returns the resultant list. */ | ||||
| static struct mpatch_flist *combine(struct mpatch_flist *a, | static struct mpatch_flist *combine(struct mpatch_flist *a, | ||||
| struct mpatch_flist *b) | struct mpatch_flist *b) | ||||
| { | { | ||||
| struct mpatch_flist *c = NULL; | struct mpatch_flist *c = NULL; | ||||
| struct mpatch_frag *bh, *ct; | struct mpatch_frag *bh, *ct; | ||||
| int offset = 0, post; | int offset = 0, post; | ||||
| if (a && b) | if (a && b) | ||||
| c = lalloc((lsize(a) + lsize(b)) * 2); | c = lalloc((lsize(a) + lsize(b)) * 2); | ||||
| f++; | f++; | ||||
| } | } | ||||
| outlen += len - last; | outlen += len - last; | ||||
| return outlen; | return outlen; | ||||
| } | } | ||||
| int mpatch_apply(char *buf, const char *orig, ssize_t len, | int mpatch_apply(char *buf, const char *orig, ssize_t len, | ||||
| struct mpatch_flist *l) | struct mpatch_flist *l) | ||||
| { | { | ||||
| struct mpatch_frag *f = l->head; | struct mpatch_frag *f = l->head; | ||||
| int last = 0; | int last = 0; | ||||
| char *p = buf; | char *p = buf; | ||||
| while (f != l->tail) { | while (f != l->tail) { | ||||
| if (f->start < last || f->end > len) { | if (f->start < last || f->end > len) { | ||||
| return MPATCH_ERR_INVALID_PATCH; | return MPATCH_ERR_INVALID_PATCH; | ||||
| } | } | ||||
| memcpy(p, orig + last, f->start - last); | memcpy(p, orig + last, f->start - last); | ||||
| p += f->start - last; | p += f->start - last; | ||||
| memcpy(p, f->data, f->len); | memcpy(p, f->data, f->len); | ||||
| last = f->end; | last = f->end; | ||||
| p += f->len; | p += f->len; | ||||
| f++; | f++; | ||||
| } | } | ||||
| memcpy(p, orig + last, len - last); | memcpy(p, orig + last, len - last); | ||||
| return 0; | return 0; | ||||
| } | } | ||||
| /* recursively generate a patch of all bins between start and end */ | /* recursively generate a patch of all bins between start and end */ | ||||
| struct mpatch_flist *mpatch_fold(void *bins, | struct mpatch_flist * | ||||
| struct mpatch_flist* (*get_next_item)(void*, ssize_t), | mpatch_fold(void *bins, struct mpatch_flist *(*get_next_item)(void *, ssize_t), | ||||
| ssize_t start, ssize_t end) | ssize_t start, ssize_t end) | ||||
| { | { | ||||
| ssize_t len; | ssize_t len; | ||||
| if (start + 1 == end) { | if (start + 1 == end) { | ||||
| /* trivial case, output a decoded list */ | /* trivial case, output a decoded list */ | ||||
| return get_next_item(bins, start); | return get_next_item(bins, start); | ||||
| } | } | ||||
| /* divide and conquer, memory management is elsewhere */ | /* divide and conquer, memory management is elsewhere */ | ||||
| len = (end - start) / 2; | len = (end - start) / 2; | ||||
| return combine(mpatch_fold(bins, get_next_item, start, start + len), | return combine(mpatch_fold(bins, get_next_item, start, start + len), | ||||
| mpatch_fold(bins, get_next_item, start + len, end)); | mpatch_fold(bins, get_next_item, start + len, end)); | ||||
| } | } | ||||