-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutil.c
110 lines (83 loc) · 2.91 KB
/
util.c
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
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "context.h"
#include "lcs.h"
#include "draw.h"
#define MIN(x,y) ((x) < (y) ? (x) : (y))
bool load_previous(Context *ctx, uint8_t *lbuf, size_t *scratch)
{
off_t old_pos = ctx->offset_pos - 1;
ctx->offset = ctx->prev_offset[old_pos];
ctx->nf_offset -= ctx->prev_nshift[old_pos];
ctx->of_offset -= ctx->prev_oshift[old_pos];
ctx->offset_pos--;
if (!ctx->is_cleared) {
memset(scratch, 0, (ctx->blocksize + 1) * (ctx->blocksize + 1) * sizeof(*scratch));
memset(lbuf, 0, ctx->blocksize);
memset(&ctx->odiff[0], 0, ctx->odsize);
memset(&ctx->ndiff[0], 0, ctx->ndsize);
}
ctx->ndsize = 0;
ctx->odsize = 0;
return calc_lcs_mask(ctx, lbuf, scratch);
}
bool calc_next_mask(Context *ctx, uint8_t *lbuf, size_t *scratch, bool *err)
{
off_t old_pos = ctx->offset_pos;
size_t cmp_size;
*err = false;
/* Dummy call. */
draw_ui_dummy(ctx);
if ((size_t) old_pos >= ctx->prev_offset_size) {
uint16_t *new_prev_offset;
uint16_t *new_prev_oshift;
uint16_t *new_prev_nshift;
ctx->prev_offset_size *= 2;
new_prev_offset = realloc(ctx->prev_offset, ctx->prev_offset_size * sizeof(*new_prev_offset));
if (new_prev_offset == NULL) {
*err = true;
return false;
}
ctx->prev_offset = new_prev_offset;
new_prev_oshift = realloc(ctx->prev_oshift, ctx->prev_offset_size * sizeof(*new_prev_oshift));
if (new_prev_oshift == NULL) {
*err = true;
return false;
}
ctx->prev_oshift = new_prev_oshift;
new_prev_nshift = realloc(ctx->prev_nshift, ctx->prev_offset_size * sizeof(*new_prev_nshift));
if (new_prev_nshift == NULL) {
*err = true;
return false;
}
ctx->prev_nshift = new_prev_nshift;
}
ctx->prev_offset[old_pos] = ctx->offset - 2;
ctx->prev_oshift[old_pos] = ctx->oshift;
ctx->prev_nshift[old_pos] = ctx->nshift;
ctx->offset_pos++;
ctx->nf_offset += ctx->nshift;
ctx->of_offset += ctx->oshift;
if (!ctx->is_cleared) {
memset(lbuf, 0, ctx->blocksize);
memset(scratch, 0, (ctx->blocksize + 1) * (ctx->blocksize + 1) * sizeof(*scratch));
memset(&ctx->odiff[0], 0, ctx->odsize);
memset(&ctx->ndiff[0], 0, ctx->ndsize);
ctx->is_cleared = true;
}
ctx->ndsize = 0;
ctx->odsize = 0;
ctx->offset = 0;
cmp_size = MIN(MIN(ctx->osize - ctx->of_offset, ctx->blocksize), ctx->nsize - ctx->nf_offset);
if (!memcmp(&ctx->obuf[ctx->of_offset], &ctx->nbuf[ctx->nf_offset], cmp_size)) {
ctx->ndsize = cmp_size;
ctx->odsize = cmp_size;
if (cmp_size < ctx->blocksize)
ctx->done = true;
return true;
}
return calc_lcs_mask(ctx, lbuf, scratch);
}