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
|
/* Copyright (C) 2018 June McEnroe <june@causal.agency>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
#include "edi.h"
static const char Magic[3] = "EDI";
static const char Version = 1;
static enum Error
write(FILE *stream, const void *ptr, size_t size, size_t nitems) {
size_t n = fwrite(ptr, size, nitems, stream);
return (n < nitems ? Errno + errno : Ok);
}
static enum Error writeBuffer(FILE *stream, const struct Buffer *buf) {
size_t len = 0;
const struct Block *block;
for (block = buf->block; block; block = block->prev) {
len += block->len;
}
enum Error error = write(stream, &len, sizeof(len), 1);
if (error) return error;
for (block = buf->block; block; block = block->prev) {
error = write(stream, block->chars, sizeof(wchar_t), block->len);
if (error) return error;
}
return Ok;
}
static enum Error
writeSlice(FILE *stream, const struct Buffer *buf, struct Slice slice) {
size_t offset = 0;
const struct Block *block = buf->block;
while (block) {
if (
slice.ptr >= block->chars && slice.ptr < &block->chars[block->len]
) break;
offset += block->len;
block = block->prev;
}
assert(block);
size_t ptr = offset + (size_t)(slice.ptr - block->chars);
enum Error error = write(stream, &ptr, sizeof(ptr), 1);
if (error) return error;
return write(stream, &slice.len, sizeof(slice.len), 1);
}
static enum Error
writeTable(FILE *stream, const struct Buffer *buf, const struct Table *table) {
enum Error error = write(stream, &table->len, sizeof(table->len), 1);
for (size_t i = 0; i < table->len; ++i) {
error = writeSlice(stream, buf, table->slices[i]);
if (error) return error;
}
return Ok;
}
static enum Error
writeState(FILE *stream, const struct Buffer *buf, const struct State *state) {
enum Error error;
error = write(stream, &state->prev, sizeof(state->prev), 1);
if (error) return error;
error = write(stream, &state->next, sizeof(state->next), 1);
if (error) return error;
return writeTable(stream, buf, &state->table);
}
static enum Error
writeLog(FILE *stream, const struct Buffer *buf, const struct Log *log) {
enum Error error;
error = write(stream, &log->state, sizeof(log->state), 1);
if (error) return error;
error = write(stream, &log->len, sizeof(log->len), 1);
if (error) return error;
for (size_t i = 0; i < log->len; ++i) {
error = writeState(stream, buf, &log->states[i]);
if (error) return error;
}
return Ok;
}
enum Error storeWrite(FILE *stream, const struct Edit *edit) {
enum Error error;
error = write(stream, Magic, sizeof(Magic), 1);
if (error) return error;
error = write(stream, &Version, sizeof(Version), 1);
if (error) return error;
error = writeBuffer(stream, &edit->buf);
if (error) return error;
return writeLog(stream, &edit->buf, &edit->log);
}
static enum Error read(FILE *stream, void *ptr, size_t size, size_t nitems) {
size_t n = fread(ptr, size, nitems, stream);
if (ferror(stream)) return Errno + errno;
return (n < nitems ? StoreEOF : Ok);
}
static enum Error readMagic(FILE *stream) {
char magic[3];
enum Error error = read(stream, magic, sizeof(magic), 1);
if (error) return error;
return (
magic[0] != Magic[0] || magic[1] != Magic[1] || magic[2] != Magic[2]
? StoreMagic
: Ok
);
}
static enum Error readVersion(FILE *stream) {
char version;
enum Error error = read(stream, &version, sizeof(version), 1);
if (error) return error;
return (version != Version ? StoreVersion : Ok);
}
static enum Error readBuffer(FILE *stream, struct Buffer *buf) {
size_t len;
enum Error error = read(stream, &len, sizeof(len), 1);
if (error) return error;
wchar_t *dest = bufferDest(buf, len);
return read(stream, dest, sizeof(wchar_t), len);
}
static enum Error
readSlice(FILE *stream, struct Buffer *buf, struct Table *table) {
enum Error error;
size_t ptr, len;
error = read(stream, &ptr, sizeof(ptr), 1);
if (error) return error;
error = read(stream, &len, sizeof(len), 1);
if (error) return error;
tablePush(table, (struct Slice) { &buf->slice.ptr[ptr], len });
return Ok;
}
static enum Error
readTable(FILE *stream, struct Buffer *buf, struct Table *table) {
size_t len;
enum Error error = read(stream, &len, sizeof(len), 1);
if (error) return error;
*table = tableAlloc(len);
for (size_t i = 0; i < len; ++i) {
error = readSlice(stream, buf, table);
if (error) return error;
}
return Ok;
}
static enum Error
readState(FILE *stream, struct Buffer *buf, size_t offset, struct Log *log) {
enum Error error;
size_t prev, next;
struct Table table;
error = read(stream, &prev, sizeof(prev), 1);
if (error) return error;
error = read(stream, &next, sizeof(next), 1);
if (error) return error;
error = readTable(stream, buf, &table);
if (error) return error;
logPush(log, table);
log->states[log->state].prev = offset + prev;
log->states[log->state].next = offset + next;
return Ok;
}
static enum Error readLog(FILE *stream, struct Buffer *buf, struct Log *log) {
enum Error error;
size_t state, len;
error = read(stream, &state, sizeof(state), 1);
if (error) return error;
error = read(stream, &len, sizeof(len), 1);
if (error) return error;
size_t offset = log->len;
for (size_t i = 0; i < len; ++i) {
error = readState(stream, buf, offset, log);
if (error) return error;
}
log->state = offset + state;
return Ok;
}
enum Error storeRead(FILE *stream, struct Edit *edit) {
enum Error error;
error = readMagic(stream);
if (error) return error;
error = readVersion(stream);
if (error) return error;
error = readBuffer(stream, &edit->buf);
if (error) return error;
return readLog(stream, &edit->buf, &edit->log);
}
|