-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDDSGroup3.cc
471 lines (419 loc) · 11.5 KB
/
DDSGroup3.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
//
// 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 <string.h>
#include "DDSGroup3.h"
#include "DDSSubcode.h"
static bool GetSubcodePack(unsigned int id, const Track& A, const Track& B,
const uint8_t **data);
//
// This is an inline helper function for locating the row and column number
// for a given Group 3 data byte "i", called "Di".
//
// Being foremost an interpretation of a DATFrame, a DDS Group 3 frame
// has 1456 rows, each of which is 4-bytes long.
//
// 9.3.3 G3 Sub-Group
//
// +-----------------+-----------------+
// Channel -> | A | B |
// +--------+--------+--------+--------+
// Byte name -> | lower | upper | lower | upper |
// +------+========+========+========+========+
// Header | 0 |0000DFID| LF-ID |0000DFID| LF-ID |
// +------+--------+--------+--------+--------+
// | 1 | D0 | D1 | D2 | D3 |
// +------+--------+--------+--------+--------+
// | 2 | D4 | D5 | D6 | D7 |
// +------+--------+--------+--------+--------+
// | ... | ... | ... | ... | ... |
// +------+--------+--------+--------+--------+
// | 1439 | D5752 | D5853 | D5754 | D5755 |
// +------+--------+--------+--------+--------+
// | 1440 | All bytes set to zero |
// | ... | |
// | 1455 | |
// +------+-----------------------------------+
//
static inline uint8_t
Di(const DATFrame::DataArray& data, size_t i)
{
size_t row = (i/4) + 1;
size_t col = (i%4);
return data[row][col];
}
//
// From ECMA DDS specification, section 9.3.3 G3 Sub-Group
//
// A G3 group is one of the 22 pieces that, together, make up a Basic
// Group, which is about 126k of continuous data from the host.
//
DDSGroup3::DDSGroup3()
: mIsLastLogicalFrame(false), mAbsoluteFrameID(0),
mLogicalFrameID(0)
{
}
DDSGroup3::~DDSGroup3()
{
}
//
// The underlying frame object upon which this group 3 is built.
//
const DATFrame&
DDSGroup3::Frame() const
{
return mFrame;
}
//
// The tape area to which this frame purports to belong.
//
DDSGroup3::AreaID
DDSGroup3::Area() const
{
return mAreaID;
}
//
// The partition to which this frame purports to belong.
//
DDSGroup3::PartitionID
DDSGroup3::Partition() const
{
return mPartitionID;
}
//
// Absolute frame number. This frame's id relative to the entire
// tape.
//
uint32_t
DDSGroup3::AbsoluteFrameID() const
{
return mAbsoluteFrameID;
}
//////////////////////////////////////////////
// Data area frames also support the following
//
//
// The running basic group count.
//
uint32_t
DDSGroup3::BasicGroupID() const
{
return mBasicGroupID;
}
uint8_t
DDSGroup3::LogicalFrameID() const
{
return mLogicalFrameID;
}
//
// Whether this is the last frame in the G1 group.
//
// Again, probably only valid for frames in the DATA area.
//
bool
DDSGroup3::IsLastLogicalFrame() const
{
return mIsLastLogicalFrame;
}
//
// Whether or not this is an ECC3 frame. Generally if this is true
// then IsLastLogicalFrame() should also be true.
//
bool
DDSGroup3::IsECC3Frame() const
{
return mIsECC3Frame;
}
//
// The running Separator 1 count (file count).
//
uint32_t
DDSGroup3::Separator1Count() const
{
return mSeparator1Count;
}
//
// The running record count.
//
uint32_t
DDSGroup3::RecordCount() const
{
return mRecordCount;
}
//
// Construct a group from a received frame (also called a G4 group pair, and
// also called a track pair).
//
DDSGroup3::DecodeError
DDSGroup3::DecodeFrame(const Track& A, const Track& B)
{
//
// Retrieve the items from subcode id #3 from both tracks.
//
const uint8_t *a_subcode, *b_subcode;
if (!A.GetSubcode(3, &a_subcode))
return A_MISSING_SUBCODE_3;
if (!B.GetSubcode(3, &b_subcode))
return B_MISSING_SUBCODE_3;
//
// Every track should have a valid subcode pack 3. Let's fetch it from
// both tracks and make certain they match. (This may be redundant as
// the frame pairing protocol is supposed to have checked this earlier).
//
DDSSubcodePack3 a3, b3;
a3.Decode(a_subcode);
b3.Decode(b_subcode);
//
// Assert that both tracks contain the same absolute frame number.
//
if (a3.mAbsoluteFrameID != b3.mAbsoluteFrameID)
return ABSOLUTE_FRAME_MISMATCH;
//
// Record frame numbers and such.
//
mPartitionID = PartitionID(a3.mPartitionID);
mAreaID = AreaID(a3.mAreaID);
mAbsoluteFrameID = a3.mAbsoluteFrameID;
//
// Fetch additional items, depending on the Area of the tape
// we're supposedly in.
//
switch (mAreaID) {
case DEVICE_AREA:
case REFERENCE_AREA:
case SYSTEM_AREA:
case EOD_AREA:
//
// Not handling these areas yet.
//
return DECODE_OK;
case DATA_AREA:
//
// Data area frames also have data, which we should check and store.
//
// Assert that both tracks of this famre also agree on their logical
// frame number (this frame's position within the larger Basic Group).
//
if (a3.mLogicalFrameID != b3.mLogicalFrameID)
return LOGICAL_FRAME_MISMATCH;
return HandleDataAreaFrame(a3, A, B);
break;
default:
return UNKNOWN_AREA_ID;
}
}
const char *
DDSGroup3::ErrorDescription(DDSGroup3::DecodeError r)
{
switch (r) {
case DECODE_OK:
return "DECODE_OK";
case A_MISSING_SUBCODE_3:
return "A_MISSING_SUBCODE_3";
case B_MISSING_SUBCODE_3:
return "B_MISSING_SUBCODE_3";
case MISSING_SUBCODE_4:
return "MISSING_SUBCODE_4";
case ABSOLUTE_FRAME_MISMATCH:
return "ABSOLUTE_FRAME_MISMATCH";
case LOGICAL_FRAME_MISMATCH:
return "LOGICAL_FRAME_MISMATCH";
case UNKNOWN_AREA_ID:
return "UNKNOWN_AREA_ID";
case C2_ERRORS_PRESENT:
return "C2_ERRORS_PRESENT";
case INVALID_HEADER:
return "INVALID_HEADER";
case ECC4_ERROR:
return "ECC4_ERROR";
case MISSING_SUBCODE_1:
return "MISSING_SUBCODE_1";
case MISSING_SUBCODE_2:
return "MISSING_SUBCODE_2";
default:
return "?";
}
}
/////////////////////////////////////////////////////////////////////////////
// PROTECTED methods
/////////////////////////////////////////////////////////////////////////////
DDSGroup3::DecodeError
DDSGroup3::HandleDataAreaFrame(const DDSSubcodePack3& sub3, const Track& A,
const Track& B)
{
//
// Data area frames also have the following, which come from sub-code
// pack 3.
//
mLogicalFrameID = sub3.mLogicalFrameID;
mIsLastLogicalFrame = sub3.mIsLastLogicalFrame;
mIsECC3Frame = sub3.mIsECC3Frame;
//
// Retrieve sub-code pack 1 to get the group number and separator 1
// counts.
//
const uint8_t *subcode;
if (!GetSubcodePack(1, A, B, &subcode))
return MISSING_SUBCODE_1;
DDSSubcodePack1 sub1;
sub1.Decode(subcode);
mBasicGroupID = sub1.mGroup;
mSeparator1Count = sub1.mSeparator1Count;
//
// Retrieve sub-code pack 2 to get the separator 2 count and record
// counts.
//
DDSSubcodePack2 sub2;
if (!GetSubcodePack(2, A, B, &subcode))
return MISSING_SUBCODE_2;
sub2.Decode(subcode);
mSeparator2Count = sub2.mSeparator2Count;
mRecordCount = sub2.mRecordCount;
//
// Recreate the "LFID" byte from the LogicalFrameID, the
// IsECC3 and the IsLastFrame flags.
//
uint8_t original_lfid =
mLogicalFrameID |
(mIsECC3Frame ? 0x40 : 0) |
(mIsLastLogicalFrame ? 0x80 : 0);
//
// Also fetch sub-code 4 for extra error-checking.
//
if (!GetSubcodePack(4, A, B, &subcode))
return MISSING_SUBCODE_4;
//
// Decode the pack 4 subcode.
//
DDSSubcodePack4 sub4;
sub4.Decode(subcode);
//
// Fill in the frame data by demultiplexing the data from both tracks.
//
mFrame.FillFromTrackPair(A, B);
//
// Make certain everything was received ok.
//
if (!mFrame.OK())
return C2_ERRORS_PRESENT;
//
// Perform some other basic checks on the frame. First, let's check
// that the head bytes on the group look good.
//
// The logical frame id should be repeated bytes 1 and 3 of the first
// row of data.
//
const DATFrame::DataArray& data = mFrame.Data();
if (data[0][1] != original_lfid ||
data[0][3] != original_lfid ||
//
// Check that the format id is also zero.
//
data[0][0] != 0 ||
data[0][2] != 0) {
return INVALID_HEADER;
}
//
// Pick up the half column checksums from packs 3 and 4.
//
uint8_t c1 = sub3.mChecksum1;
uint8_t c2 = sub3.mChecksum2;
uint8_t c3 = sub4.mChecksum3;
uint8_t c4 = sub4.mChecksum4;
//
// Prepare to perform half-column checksuming of the received data.
//
uint8_t r_c1 = 0;
uint8_t r_c2 = 0;
uint8_t r_c3 = 0;
uint8_t r_c4 = 0;
//
// C1 and C3 start with the recreated LFID byte.
//
r_c1 = r_c3 = original_lfid;
//
// The rest of the error checking is convoluted.
//
for (size_t i = 0; i < 719; i++) {
//
// 9.4.3.3.1.1 Byte No. 5; i = [0,718]
//
r_c1 ^= Di(data, (8*i)+3) ^
Di(data, (8*i)+5) ^
Di(data, 5755); // This is dumb to repeat
//
// 9.4.3.3.1.2 Byte No. 6; i = [0,718]
//
r_c2 ^= Di(data, (8*i)+2) ^
Di(data, (8*i)+4) ^
Di(data, 5754); // This is dumb to repeat
//
// 9.4.4.3.1.1 Byte No. 5; i = [1,719]
//
r_c3 ^= Di(data, 1) ^ // This is dumb to repeat
Di(data, 8*(i+1)-1) ^
Di(data, 8*(i+1)+1);
//
// 9.4.4.3.1.2 Byte No. 6 [1-719]
//
r_c4 ^= Di(data, 0) ^ // This is dumb to repeat
Di(data, 8*(i+1)-2) ^
Di(data, 8*(i+1));
}
//
// Check that the half-column weird checksums match the data.
//
#if 0
if ((r_c1 != c1) ||
(r_c2 != c2) ||
(r_c3 != c3) ||
(r_c4 != c4))
return ECC4_ERROR;
#endif
return DECODE_OK;
}
//
// Get the first valid sub-code pack item we can from either track.
//
static bool
GetSubcodePack(unsigned int id, const Track& A, const Track& B,
const uint8_t ** subcode)
{
if (!A.GetSubcode(id, subcode)) {
//
// A's sub-code is missing. Perhaps we can get it from B.
//
if (!B.GetSubcode(id, subcode)) {
//
// Nope. Missing from both. We should have it.
//
return false;
}
}
return true;
}