*** empty log message ***
[h316.git] / pc-tools / ldc2 / src / tape_block.cpp
CommitLineData
97b26985 1#include <stdlib.h>
2#include <unistd.h>
3#include <string.h>
09cb0f4f 4#include <stdio.h>
6c06db96 5
97b26985 6#include "tape_block.hh"
7#include "data_block.hh"
0ec6e042 8#include "data_block_0.hh"
09cb0f4f 9#include "data_block_0_0.hh"
10#include "data_block_0_1.hh"
11#include "data_block_0_2.hh"
12#include "data_block_0_3.hh"
13#include "data_block_0_4.hh"
14#include "data_block_0_10.hh"
15#include "data_block_0_14.hh"
16#include "data_block_0_24.hh"
17#include "data_block_0_44.hh"
18#include "data_block_0_30.hh"
19#include "data_block_0_54.hh"
20#include "data_block_0_60.hh"
21#include "data_block_0_50.hh"
97b26985 22
09cb0f4f 23#include "eot_block.hh"
24#include "discard_block.hh"
97b26985 25#include "silent_code.hh"
26#include "hw_constants.hh"
27
6c06db96 28using namespace std;
29
30/***************************************************************/
31/*!
32 *\brief Destructor.
33 */
34tape_block::~tape_block(){
35 free(raw_data);
36 free(word_data);
37}
38
39/***************************************************************/
40/*!
41 *\brief Copy constructor.
42 */
43tape_block::tape_block(tape_block & org){
44 operator=(org);
45}
46
47/***************************************************************/
48/*!
49 *\brief Assignment operator.
50 */
51void tape_block::operator=(tape_block &org){
52 block_type=org.block_type;
53 init_state=org.init_state;
54 data_read=org.data_read;
55 raw_data=0;
56 raw_size=org.raw_size;
57 word_data=0;
58 word_size=org.word_size;
59 if (raw_size){
60 raw_data=(unsigned char *)malloc(raw_size);
61 memcpy(raw_data,org.raw_data,raw_size);
62 }
63 if (word_size){
64 word_data=(unsigned short *)malloc(word_size*sizeof(unsigned short));
65 memcpy(word_data,org.word_data,word_size*sizeof(unsigned short));
66 }
67}
68
69/***************************************************************/
70/*!
71 *\brief Query the block's state.
72 *
73 * If the state is not TBS_OK, the block must be discarded.
74 *\return The block's initialisation state
75 */
76tape_block::tb_state_t tape_block::get_state(){
77 return init_state;
78}
0ec6e042 79
6c06db96 80/***************************************************************/
81/*!
82 *\brief Determine the block's type
83 *
84 * This routine returns the block's type.\\
85 *\retval TBT_DATA if it's a data block
86 *\retval TBT_EOT if it's an end of tape mark
87 *\retval TBR_DISCARD if the block is unusable
88 *
89 *\note This function is virtual. That means that if the tape block in
90 * question is of the subtype data_block which overwrites this method,
91 * it will return the block type contained in the upper 4 bits of the
92 * first data word! Try to avoid testing for TBT_DATA. \n
93 * Blocks generated by gen_from_fd should never have TBT_DATA.
94 */
95int tape_block::get_type(){
96 return block_type;
97}
98
99/***************************************************************/
100/*!
101 *\brief Determine the block's subtype
102 *
103 * Some data block types have a subtype.
104 *\note This is a virtual method and to be used in derived classes.
105 *\return Always 0
106 */
107int tape_block::get_subtype(){
108 return 0;
109}
110
111/***************************************************************/
112/*!
113 *\brief Get a cleartext description of the block.
114 *\return ldc style descriptive line for the block
115 */
116vector<string> tape_block:: get_description(){
6c06db96 117 vector<string> result;
09cb0f4f 118 result.insert(result.end(),
119 "***** "+get_typestring()+"Untyped tape block");
6c06db96 120 return result;
121}
122
123/***************************************************************/
124/*!
125 *\brief Get size of the internal raw data buffer
126 *\return The size of the internal raw data buffer
127 */
128int tape_block::get_raw_size(){
129 return raw_size;
130}
131
132/***************************************************************/
133/*!
134 *\brief Access internal raw data buffer
135 *
136 * The raw data buffer contains all block data except the \ \
137 * block start delimiter and block end delimiter.
138 *\return Pointer to internal raw data buffer of the block
139 */
140unsigned char * tape_block::get_raw_data(){
141 return raw_data;
142}
143
144/***************************************************************/
145/*!
146 *\brief Static factory method with file descriptor support.
147 *
148 * This method creates a tape_block by using the private
09cb0f4f 149 * constructor with file descriptor support.\n
6c06db96 150 * It always returns a valid pointer, errors
151 * or exceptional conditions during creation are marked in the
152 * object's state.\n
153 * The factory method tries to determine the block's type and then
154 * looks for appropriate subclasses. If there's an appropriate
155 * subclass fitting the newly-created object, an
156 * object of the subclass will be created and a pointer to this
157 * object will be returned to the caller.\n
158 * This allows for easy use of the object using it's virtual
159 * interface methods or a cast of the pointer to a sub type pointer
160 * according to the type information, and then using the subtype's
09cb0f4f 161 * methods not already present in tape_block.\n
162 * Objects of the following subclasses are automagically generated:\n
6c06db96 163 * - eot_block
09cb0f4f 164 * - discard_block
6c06db96 165 * - data_block
09cb0f4f 166 * - data_block_0
167 * - data_block_0_0
168 * - data_block_0_1
169 * - data_block_0_2
170 * - data_block_0_3
171 * - data_block_0_4
172 * - data_block_0_10
173 * - data_block_0_14
174 * - data_block_0_24
175 * - data_block_0_30
176 * - data_block_0_50
177 * - data_block_0_54
178 * - data_block_0_60
179 * - data_block_1
180 * - data_block_2
181 * - data_block_3
182 * - data_block_4
183 * - data_block_5
184 * - data_block_6
185 * - data_block_7
6c06db96 186 *
187 *\return Pointer to an object of type tape_block or a subclass.
188 *\param fd A file descriptor where the read is taken from.
189 *\param input_start A pointer to a function
190 * called before reading data from fd.
191 *\param input_stop A pointer to a function
192 * called after reading data from fd.
193 *
194 *\param start_stop_arg A pointer passed to input_start
195 * and input_stop(). Can be anything.
196 */
0ec6e042 197tape_block * tape_block::gen_from_fd(int fd,
198 void(*input_start)(void *),
199 void (*input_stop)(void *),
200 void * start_stop_arg)
97b26985 201{
0ec6e042 202 tape_block * res_block;
203 res_block=new tape_block(fd,input_start,input_stop,start_stop_arg);
09cb0f4f 204 data_block * d_block;
205 data_block_0 * d0_block;
206 eot_block * e_block;
207 discard_block * di_block;
208
0ec6e042 209 // Retype to data_block if possible
210 switch(res_block->get_type()){
211 case tape_block::TBT_DATA: // Make pointer a data block
212 d_block=new data_block(*res_block);
213 delete res_block;
214 res_block=d_block;
215 break;
09cb0f4f 216 case tape_block::TBT_EOT:
217 e_block=new eot_block(*res_block);
218 delete res_block;
219 res_block=e_block;
220 break;
221 case tape_block::TBT_DISCARD:
222 di_block=new discard_block(*res_block);
223 delete res_block;
224 res_block=di_block;
225 break;
0ec6e042 226 default: // All other cases
227 return res_block;
228 }
229
230 // Retype again, if it's a data_block now
231
232 switch(res_block->get_type()){
233 case 00:
234 d0_block=new data_block_0(*d_block);
235 d_block=d0_block;
236 break;
237// case 01: d_block=new data_block_1(*d_block); break;
238// case 02: d_block=new data_block_2(*d_block); break;
239// case 03: d_block=new data_block_3(*d_block); break;
240// case 04: d_block=new data_block_4(*d_block); break;
241// case 05: d_block=new data_block_5(*d_block); break;
242// case 06: d_block=new data_block_6(*d_block); break;
243// case 07: d_block=new data_block_7(*d_block); break;
244 default:
245 return res_block;
246 }
247 delete res_block;
248 res_block=d_block;
249
250 if (res_block->get_type()==0) switch(d0_block->get_subtype()){
09cb0f4f 251 case 000: d0_block=new data_block_0_0(*d0_block); break;
252 case 001: d0_block=new data_block_0_1(*d0_block); break;
253 case 002: d0_block=new data_block_0_2(*d0_block); break;
254 case 003: d0_block=new data_block_0_3(*d0_block); break;
255 case 004: d0_block=new data_block_0_4(*d0_block); break;
256 case 010: d0_block=new data_block_0_10(*d0_block); break;
257 case 014: d0_block=new data_block_0_14(*d0_block); break;
258 case 024: d0_block=new data_block_0_24(*d0_block); break;
259 case 044: d0_block=new data_block_0_44(*d0_block); break;
260 case 030: d0_block=new data_block_0_30(*d0_block); break;
261 case 054: d0_block=new data_block_0_54(*d0_block); break;
262 case 060: d0_block=new data_block_0_60(*d0_block); break;
263 case 050: d0_block=new data_block_0_50(*d0_block); break;
0ec6e042 264 default:
265 return res_block;
266 }
267 delete res_block;
268 res_block=d0_block;
269
270 return res_block;
97b26985 271}
272
6c06db96 273/***************************************************************/
0ec6e042 274/*!
275 *\brief Default constructor.
276 */
277tape_block::tape_block(){}
278
09cb0f4f 279/***************************************************************/
280/*!
281 *\brief get a short type description string.
282 *\return A small string to be used in description methods.
283 *
284 * The string is always 19 characters long.\n
285 * An example is "(99-99) ".
286 */
287string tape_block::get_typestring(){
288 char buffer[13];
289 sprintf(buffer,"(%o-%o) ",get_type(),get_subtype());
290 buffer[10]=0;
291 return string(buffer);
292}
0ec6e042 293
6c06db96 294/***************************************************************/
0ec6e042 295/*!
296 *\brief Automatic constructor with file descriptor support
297 *
298 * This constructor is used to initialise the block read in the block's
299 * data via a file descriptor.\n
300 * This is done in the following way:
301 * - input_start() is called.
302 * - Data is read from fd. Stops on end of file or end of block
303 * or IO error. On EOF or IO error input_stop() is NOT called.
304 * - If there was no IO error the block's checksum is calculated
305 * and checked.
306 * - The block's type field is initialised.
307 * - input_stop() is called.
308 *
309 *\param fd A file descriptor where the read is taken from
310 *\param input_stop A pointer to a function called at the end of input
311 *\param input_start A pointer to a function called at the beginning
312 * of input
313 *\param start_stop_arg A pointer passed to input_start and input_stop().
314 */
97b26985 315tape_block::tape_block (int fd,
316 void(*input_start)(void *),
317 void (*input_stop)(void *),
318 void * start_stop_arg
319 )
320{
321 unsigned char buffer;
322 int retval;
323 int eot_needed=EOT_LENGTH;
324 int eob_needed=EOB_LENGTH;
325 int block_complete=0;
326
327 block_type= TBT_DISCARD;
328 init_state= TBS_DEFAULT;
329 data_read = 0;
330 raw_data = 0;
331 raw_size = 0;
332 word_data = 0;
333 word_size = 0;
334
335 if (input_start) input_start (start_stop_arg);
336
337 /* Look for block start in input stream */
338 do {
339 retval=read(fd, &buffer, 1);
340 if (retval == 0){ // End of file
341 block_type=TBT_DISCARD;
342 init_state=TBS_EOF_LEGAL;
343 return;
344 }
345 if (retval == -1){ // Error reading from fd
346 block_type=TBT_DISCARD;
347 init_state=TBS_IOERR;
348 return;
349 }
350 data_read++;
351
352 /* Look for end ot tape sequence */
353 switch(eot_needed){
354 case 3:
355 if (buffer==EOT_SEQ_1) eot_needed--;
356 else eot_needed=EOT_LENGTH;
357 break;
358 case 2:
359 if (buffer==EOT_SEQ_2) eot_needed--;
360 else eot_needed=EOT_LENGTH;
361 break;
362 case 1:
363 if (buffer==EOT_SEQ_3){
364 raw_data=(unsigned char*)malloc(3);
365 raw_size=3;
366 raw_data[0]=EOT_SEQ_1;
367 raw_data[1]=EOT_SEQ_2;
368 raw_data[2]=EOT_SEQ_3;
369 block_type =TBT_EOT;
370 init_state =TBS_OK;
371 return;
372 }
373 else eot_needed=EOT_LENGTH;
374 break;
375 }
376 } while (buffer != (unsigned char) BLOCK_START_DELIMITER);
377
378
379 /* Now input the block data */
380 block_complete = 0;
381 do {
382 retval = read (fd, &buffer, 1);
383
384 if (retval == 0){ // End of file
385 block_type=TBT_DISCARD;
386 init_state=TBS_EOF_ILLEGAL;
387 return;
388 }
389 if (retval == -1){ // Error reading from fd
390 block_type=TBT_DISCARD;
391 init_state=TBS_IOERR;
392 return;
393 }
394 data_read++; // We have consumed a byte
395
396 /* sort in the new byte */
397 raw_size++;
398 raw_data=(unsigned char*)realloc(raw_data,raw_size);
399 raw_data[raw_size-1]=buffer;
400
401 /* Look for end ot block sequence */
402 switch(eob_needed){
403 case 2:
404 if (buffer==BLOCK_END_DELIMITER_1) eob_needed--;
405 else eob_needed=EOB_LENGTH;
406 break;
407 case 1:
408 if (buffer==BLOCK_END_DELIMITER_2){
409 raw_size-=EOB_LENGTH; // Don't want the delimiter(s) in the data!
410 raw_data=(unsigned char*)realloc(raw_data,raw_size);
411 block_complete=1;
412 }
413 else eob_needed=EOB_LENGTH;
414 break;
415 }
416 }while (!block_complete);
417 if (input_stop) input_stop (start_stop_arg);
418
419 /* Now we have the data in. */
420
421 if (init_words()!=0){
422 init_state=TBS_CHECKSUM;
423 return;
424 }
425
426 if (test_checksum()!=0){
427 init_state=TBS_CHECKSUM;
428 return;
429 }
430
431 block_type=TBT_DATA;
432 init_state=TBS_OK;
433}
434
97b26985 435
6c06db96 436/***************************************************************/
0ec6e042 437/*!
6c06db96 438 *\brief Initialize word representation of data.
439 *\retval 0 on success.
440 *\retval 1 on error.
0ec6e042 441 */
97b26985 442int tape_block::init_words(){
443 word_size=raw_size/3;
444 if (word_size<MINIMUM_DATA_BLOCK_WORDSIZE) return 1; // Block too short!
445 word_data=(unsigned short *)malloc(raw_size*sizeof(unsigned short));
446 for (int pos=0; pos<word_size; pos++)
447 word_data[pos]=combine466(raw_data+3*pos);
448 return 0;
449}
450
6c06db96 451/***************************************************************/
0ec6e042 452/*!
6c06db96 453 *\brief Calculate and check checksum.
454 *\retval 0 on success.
455 *\retval 1 on error.
0ec6e042 456 */
97b26985 457int tape_block::test_checksum(){
458 unsigned short sum=0;
459 for (int pos=0; pos<word_size-1;pos++)
0ec6e042 460 sum^=word_data[pos];
97b26985 461 if (sum==word_data[word_size-1]) return 0;
462 else return 1;
463}
6c06db96 464