-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasicGroup.cc
385 lines (339 loc) · 9.2 KB
/
BasicGroup.cc
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
//
// Copyright 2018, Jeremy Cooper
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
#include <stdio.h>
#include "BasicGroup.h"
#include "ECC_C3.h"
#include "ECCFill_C3.h"
//
// A class for encapsulating a DDS "Basic Group" -- which is
// the smallest logical unit that can be written to the tape.
//
// A basic group is 126632 bytes in size and contains both user
// data and meta-data specifying where the set marks and record
// marks reside within that data.
//
BasicGroup::BasicGroup(uint32_t id)
: mBasicGroupID(id)
{
for (size_t i = 0; i < kSize; i++) {
mData[i] = 0;
mDataIsValid[i] = false;
}
for (size_t i = 0; i < DDSGroup1::kSize; i++) {
mECCData[i] = 0;
mECCDataIsValid[i] = false;
}
}
BasicGroup::~BasicGroup()
{
}
bool
BasicGroup::LoadFromFile(const char *datapath, const char *validpath,
const char *eccpath, const char *eccvalidpath)
{
FILE *data_fd, *valid_fd, *ecc_fd, *eccvalid_fd;
DataArray data, valid;
DDSGroup1::DataArray ecc, eccvalid;
bool res = false;
data_fd = fopen(datapath, "rb");
if (data_fd == NULL)
goto CantOpenData;
valid_fd = fopen(validpath, "rb");
if (valid_fd == NULL)
goto CantOpenValid;
ecc_fd = fopen(eccpath, "rb");
if (ecc_fd == NULL)
goto CantOpenECC;
eccvalid_fd = fopen(eccvalidpath, "rb");
if (eccvalid_fd == NULL)
goto CantOpenECCValid;
//
// Read in all the data items to temporary places.
//
if (fread(data, sizeof(data), 1, data_fd) != 1)
goto ShortDataRead;
if (fread(valid, sizeof(valid), 1, valid_fd) != 1)
goto ShortValidRead;
if (fread(ecc, sizeof(ecc), 1, ecc_fd) != 1)
goto ShortECCRead;
if (fread(eccvalid, sizeof(eccvalid), 1, eccvalid_fd) != 1)
goto ShortECCValidRead;
//
// Copy the temporary data into our member variables, translating
// booleans where needed.
//
for (size_t i = 0; i < kSize; i++) {
mData[i] = data[i];
mDataIsValid[i] = valid[i] != 0;
}
for (size_t i = 0; i < DDSGroup1::kSize; i++) {
mECCData[i] = ecc[i];
mECCDataIsValid[i] = eccvalid[i] != 0;
}
res = true;
ShortECCValidRead:
ShortECCRead:
ShortValidRead:
ShortDataRead:
fclose(eccvalid_fd);
CantOpenECCValid:
fclose(ecc_fd);
CantOpenECC:
fclose(valid_fd);
CantOpenValid:
fclose(data_fd);
CantOpenData:
return res;
}
bool
BasicGroup::DumpToFile(const char *datapath, const char *validpath,
const char *eccpath, const char *eccvalidpath)
{
FILE *data_fd, *valid_fd, *ecc_fd, *eccvalid_fd;
DataArray valid;
DDSGroup1::DataArray eccvalid;
bool res = false;
data_fd = fopen(datapath, "wb");
if (data_fd == NULL)
goto CantOpenData;
valid_fd = fopen(validpath, "wb");
if (valid_fd == NULL)
goto CantOpenValid;
ecc_fd = fopen(eccpath, "wb");
if (ecc_fd == NULL)
goto CantOpenECC;
eccvalid_fd = fopen(eccvalidpath, "wb");
if (eccvalid_fd == NULL)
goto CantOpenECCValid;
//
// Create character arrays from all boolean items.
//
for (size_t i = 0; i < kSize; i++)
valid[i] = mDataIsValid[i] ? 0xff : 0;
for (size_t i = 0; i < DDSGroup1::kSize; i++)
eccvalid[i] = mECCDataIsValid[i] ? 0xff : 0;
//
// Write all data items, .
//
if (fwrite(mData, sizeof(mData), 1, data_fd) != 1)
goto CantWriteData;
if (fwrite(valid, sizeof(valid), 1, valid_fd) != 1)
goto CantWriteValid;
if (fwrite(mECCData, sizeof(mECCData), 1, ecc_fd) != 1)
goto CantWriteECC;
if (fwrite(eccvalid, sizeof(eccvalid), 1, eccvalid_fd) != 1)
goto CantWriteECCValid;
res = true;
CantWriteECCValid:
CantWriteECC:
CantWriteValid:
CantWriteData:
fclose(eccvalid_fd);
CantOpenECCValid:
fclose(ecc_fd);
CantOpenECC:
fclose(valid_fd);
CantOpenValid:
fclose(data_fd);
CantOpenData:
return res;
}
bool
BasicGroup::AddSubFrame(const DDSGroup1& frame)
{
//
// Make certain that this sub-frame belongs to this group.
//
if (frame.BasicGroupID() != mBasicGroupID) {
printf("Attempt to add sub-frame to wrong basic group!\n");
return false;
}
//
// Don't dump sub-frame zero, if you ever encounter one.
//
if (frame.SubFrameID() == 0)
return true;
//
// ECC frames get special treatment.
//
bool is_ecc = frame.IsECCFrame();
//
// Derive pointers into the appropriate place in out data to capture
// this new frame.
//
uint8_t *data_existing;
bool *valid_existing;
if (!is_ecc) {
//
// Normal data frame. Frames are numbered starting at 1 and are
// about 5k in size.
//
size_t pos = DDSGroup1::kSize * (frame.SubFrameID() - 1);
data_existing = &mData[pos];
valid_existing = &mDataIsValid[pos];
} else {
//
// ECC frames go into their own buffer.
//
data_existing = &mECCData[0];
valid_existing = &mECCDataIsValid[0];
}
//
// Get pointers to the frame data and validity buffer.
//
const DDSGroup1::DataArray& data = frame.Data();
const DDSGroup1::ValidArray& valid = frame.Valid();
//
// Incorporate the frame's data as long as it is better than the existing
// data.
//
for (size_t i = 0; i < DDSGroup1::kSize; i++) {
if (valid[i] && !valid_existing[i]) {
//
// New valid data that replaces invalid data. Use it!
//
data_existing[i] = data[i];
valid_existing[i] = true;
} else if (valid[i] && valid_existing[i]) {
//
// We already have data at this spot. Check to make sure that the
// update is the same.
//
if (data[i] != data_existing[i]) {
//
// Oh oh, mistmatch.
//
printf("Reread mismatch at Basic Group %d, SubGroup %d, offset %zd"
". Old/New = %02x/%02x. Keeping existing data.\n",
frame.BasicGroupID(), frame.SubFrameID(), i, data_existing[i],
data[i]);
}
} else if (!valid[i] && !valid_existing[i]) {
//
// Existing data is invalid and incoming data is also marked invalid,
// but the incoming data likely has more information than the existing.
// Sometimes entire blocks get marked invalid due to error detection
// but a good deal of the block is actually correct. We have enough
// storage space to represent the data invalidity yet retain the likely
// correct data.
//
// Copy in the supposedly invalid data.
//
data_existing[i] = data[i];
valid_existing[i] = valid[i];
}
}
return true;
}
//
// With the help of the ECC3 data, correct any erasures that are still
// present in this group.
//
bool
BasicGroup::Correct()
{
ECC_C3 C3;
size_t uncorrectableErrors = 0;
for (ECCFill_C3 c3_fill(*this); !c3_fill.End(); c3_fill.Next()) {
//
// Fill the error check vector.
//
C3.Fill(c3_fill);
//
// Detect errors in this vector and correct them.
//
ECC_C3::Status ok = C3.Correct();
switch (ok) {
case ECC_C3::NO_ERRORS:
//
// No errors. Nothing to be done.
//
break;
case ECC_C3::CORRECTED:
//
// There were errors but they were corrected.
// Put the corrected data back into the track.
//
C3.Dump(c3_fill);
break;
case ECC_C3::UNCORRECTABLE:
//
// Slice was uncorrectable. Leave it as is.
//
uncorrectableErrors++;
break;
}
}
return uncorrectableErrors == 0;
}
uint32_t
BasicGroup::BasicGroupID() const
{
return mBasicGroupID;
}
const BasicGroup::DataArray&
BasicGroup::Data() const
{
return mData;
}
const BasicGroup::ValidArray&
BasicGroup::Valid() const
{
return mDataIsValid;
}
const BasicGroup::ECCDataArray&
BasicGroup::ECCData() const
{
return mECCData;
}
const BasicGroup::ECCValidArray&
BasicGroup::ECCValid() const
{
return mECCDataIsValid;
}
BasicGroup::DataArray&
BasicGroup::ModifiableData()
{
return mData;
}
BasicGroup::ValidArray&
BasicGroup::ModifiableValid()
{
return mDataIsValid;
}
BasicGroup::ECCDataArray&
BasicGroup::ModifiableECCData()
{
return mECCData;
}
BasicGroup::ECCValidArray&
BasicGroup::ModifiableECCValid()
{
return mECCDataIsValid;
}