ldc2: Cosmetic fixes and support for compiler orverride
[h316.git] / pc-tools / ldc2 / src / tape_block.cpp
CommitLineData
4741aa72 1/******************************************************************************
2 *
3 * LDC2 source code
4 *
8dc281af 5 * $Date: 2007/12/23 15:25:11 $
4741aa72 6 * $Author: hachti $
7 *
8 * $Log: tape_block.cpp,v $
8dc281af 9 * Revision 2.2 2007/12/23 15:25:11 hachti
10 * *** empty log message ***
11 *
12 * Revision 2.1 2007-03-26 03:20:31 hachti
d65ad4d7 13 * *** empty log message ***
14 *
15 * Revision 2.0 2007-03-26 01:00:40 hachti
4741aa72 16 * *** empty log message ***
17 *
18 *
19 ******************************************************************************/
7880ae2d 20
21#include <string>
22#include <vector>
23
97b26985 24#include <stdlib.h>
25#include <unistd.h>
26#include <string.h>
09cb0f4f 27#include <stdio.h>
6c06db96 28
97b26985 29#include "tape_block.hh"
7880ae2d 30
31#include "silent_code.hh"
32#include "hw_constants.hh"
33
34#include "eot_block.hh"
97b26985 35#include "data_block.hh"
7880ae2d 36#include "broken_block.hh"
0ec6e042 37#include "data_block_0.hh"
09cb0f4f 38#include "data_block_0_0.hh"
39#include "data_block_0_1.hh"
40#include "data_block_0_2.hh"
41#include "data_block_0_3.hh"
42#include "data_block_0_4.hh"
43#include "data_block_0_10.hh"
44#include "data_block_0_14.hh"
45#include "data_block_0_24.hh"
46#include "data_block_0_44.hh"
47#include "data_block_0_30.hh"
48#include "data_block_0_54.hh"
49#include "data_block_0_60.hh"
50#include "data_block_0_50.hh"
fd9632c0 51#include "data_block_0_64.hh"
52#include "data_block_1.hh"
53#include "data_block_2.hh"
54#include "data_block_3.hh"
55#include "data_block_4.hh"
56#include "data_block_5.hh"
57#include "data_block_6.hh"
58#include "data_block_7.hh"
97b26985 59
6c06db96 60using namespace std;
61
62/***************************************************************/
63/*!
64 *\brief Destructor.
65 */
66tape_block::~tape_block(){
67 free(raw_data);
68 free(word_data);
69}
70
71/***************************************************************/
72/*!
73 *\brief Copy constructor.
74 */
75tape_block::tape_block(tape_block & org){
76 operator=(org);
77}
78
79/***************************************************************/
80/*!
81 *\brief Assignment operator.
82 */
83void tape_block::operator=(tape_block &org){
84 block_type=org.block_type;
fed2c751 85 discarded_bytes=org.discarded_bytes;
6c06db96 86 raw_data=0;
87 raw_size=org.raw_size;
88 word_data=0;
89 word_size=org.word_size;
90 if (raw_size){
91 raw_data=(unsigned char *)malloc(raw_size);
92 memcpy(raw_data,org.raw_data,raw_size);
93 }
94 if (word_size){
95 word_data=(unsigned short *)malloc(word_size*sizeof(unsigned short));
96 memcpy(word_data,org.word_data,word_size*sizeof(unsigned short));
97 }
98}
99
6c06db96 100/***************************************************************/
101/*!
102 *\brief Determine the block's type
103 *
104 * This routine returns the block's type.\\
105 *\retval TBT_DATA if it's a data block
106 *\retval TBT_EOT if it's an end of tape mark
6c06db96 107 *
108 *\note This function is virtual. That means that if the tape block in
109 * question is of the subtype data_block which overwrites this method,
110 * it will return the block type contained in the upper 4 bits of the
111 * first data word! Try to avoid testing for TBT_DATA. \n
112 * Blocks generated by gen_from_fd should never have TBT_DATA.
113 */
114int tape_block::get_type(){
115 return block_type;
116}
117
118/***************************************************************/
119/*!
120 *\brief Determine the block's subtype
121 *
122 * Some data block types have a subtype.
123 *\note This is a virtual method and to be used in derived classes.
124 *\return Always 0
125 */
126int tape_block::get_subtype(){
127 return 0;
128}
129
130/***************************************************************/
131/*!
132 *\brief Get a cleartext description of the block.
133 *\return ldc style descriptive line for the block
134 */
135vector<string> tape_block:: get_description(){
6c06db96 136 vector<string> result;
09cb0f4f 137 result.insert(result.end(),
138 "***** "+get_typestring()+"Untyped tape block");
6c06db96 139 return result;
140}
141
142/***************************************************************/
143/*!
144 *\brief Get size of the internal raw data buffer
145 *\return The size of the internal raw data buffer
146 */
147int tape_block::get_raw_size(){
148 return raw_size;
149}
150
fed2c751 151/***************************************************************/
152/*!
153 *\brief Get amount of bytes discarded before beginning
154 * of block sensed.
155 *\return Length of unusable tape before block start delimiter.
156 */
157int tape_block::get_discarded_bytes(){
158 return discarded_bytes;
159}
160
6c06db96 161/***************************************************************/
162/*!
163 *\brief Access internal raw data buffer
164 *
d65ad4d7 165 * The raw data buffer contains all block data except the
166 * block start and end delimiters.
6c06db96 167 *\return Pointer to internal raw data buffer of the block
168 */
169unsigned char * tape_block::get_raw_data(){
170 return raw_data;
171}
172
874a2bd8 173/***************************************************************/
174/*!
175 *\brief Dump contents of the block to a file descriptor.
176 *
177 *\param fd A writable file descriptor.
178 *\return Always zero.
179 *\throw io_error_exception A write error occured.
180 */
181int tape_block::dump_to_fd(int fd){
182 int to_write=raw_size;
183 unsigned char * dump_ptr=raw_data;
184 int written;
185 while (to_write) {
186 written=write(fd,dump_ptr,to_write);
187 if (written < 0) throw io_error_exception();
188 to_write-=written;
189 dump_ptr+=written;
190 }
191 return 0;
192}
193
194/***************************************************************/
195/*!
196 *\brief Determine if the block marks the end of an object
197 *\retval true The block marks the end of an object.
198 *\retval false The block does not mark the end of an object.
199 *\note This is only a virtual method returning always false.
200 * Classes which can be the legal last block of an object
201 * will overwrite this method so that it returns true.
202 */
203bool tape_block::is_endblock(){
204 return false;
205}
206
207/***************************************************************/
208/*!
209 *\brief Get exported symbols.
210 *\return A vector containing any symbols exported in this block.
211 *\note This is only a virtual method returning always an empty
212 * vector. Classes describing appropriate data blocks
213 * will override this method.
214 */
215vector<string> tape_block::get_exported_symbols(){
216 vector<string> result;
217 return result;
218}
219
220/***************************************************************/
221/*!
222 *\brief Get called symbols.
223 *\return A vector containing any symbols called in this block.
224 *\note This is only a virtual method returning always an empty
225 * vector. Classes describing appropriate data blocks
226 * will override this method.
227 */
228vector<string> tape_block::get_called_symbols(){
229 vector<string> result;
230 return result;
231}
232
8dc281af 233/***************************************************************/
234/*!
235 *\brief Dump block contents
236 *\return A vector containing Information about the block's
237 * content.
238 */
239vector<string> tape_block::dump_contents(){
240 vector<string> result;
241 result.insert(result.end(),"No known contents.");
242 return result;
243}
244
874a2bd8 245/***************************************************************/
246/*!
247 *\brief Determine if the block has a known type.
248 *\return true if type and subtype are known.
249 *\return false if the block type is not one of the valid end
250 * nodes of the type tree.
251 */
252bool tape_block::has_known_type(){
253 return m_has_known_type;
254}
255
6c06db96 256/***************************************************************/
257/*!
7880ae2d 258 *\brief Static factory method for tape blocks.
6c06db96 259 *
260 * This method creates a tape_block by using the private
09cb0f4f 261 * constructor with file descriptor support.\n
7880ae2d 262 *
6c06db96 263 * The factory method tries to determine the block's type and then
264 * looks for appropriate subclasses. If there's an appropriate
265 * subclass fitting the newly-created object, an
266 * object of the subclass will be created and a pointer to this
267 * object will be returned to the caller.\n
268 * This allows for easy use of the object using it's virtual
269 * interface methods or a cast of the pointer to a sub type pointer
270 * according to the type information, and then using the subtype's
09cb0f4f 271 * methods not already present in tape_block.\n
272 * Objects of the following subclasses are automagically generated:\n
6c06db96 273 * - eot_block
274 * - data_block
09cb0f4f 275 * - data_block_0
276 * - data_block_0_0
277 * - data_block_0_1
278 * - data_block_0_2
279 * - data_block_0_3
280 * - data_block_0_4
281 * - data_block_0_10
282 * - data_block_0_14
283 * - data_block_0_24
284 * - data_block_0_30
285 * - data_block_0_50
286 * - data_block_0_54
287 * - data_block_0_60
fd9632c0 288 * - data_block_0_64
09cb0f4f 289 * - data_block_1
290 * - data_block_2
291 * - data_block_3
292 * - data_block_4
293 * - data_block_5
294 * - data_block_6
295 * - data_block_7
6c06db96 296 *
7880ae2d 297 *\return Pointer to an instance of tape_block or a subclass.
298 *\param fd A file descriptor where data can be read from.
6c06db96 299 *\param input_start A pointer to a function
300 * called before reading data from fd.
301 *\param input_stop A pointer to a function
302 * called after reading data from fd.
303 *
304 *\param start_stop_arg A pointer passed to input_start
305 * and input_stop(). Can be anything.
306 */
0ec6e042 307tape_block * tape_block::gen_from_fd(int fd,
308 void(*input_start)(void *),
309 void (*input_stop)(void *),
310 void * start_stop_arg)
97b26985 311{
fd9632c0 312 tape_block * n_tape_block=0;
313 data_block * n_data_block=0;
314 data_block * n_data_block_x=0;
315 data_block_0 * n_data_block_0=0;
316 data_block_0 * n_data_block_0_x=0;
317 eot_block * n_eot_block=0;
7880ae2d 318 broken_block * n_broken_block=0;
319
50c86ded 320 //Use the private constructor which reads in the block from a file descriptor
7880ae2d 321 try{
fd9632c0 322 n_tape_block=new tape_block(fd,input_start,input_stop,start_stop_arg);
7880ae2d 323 }
fd9632c0 324
7880ae2d 325 catch(checksum_error_exception e){
326 n_broken_block=new broken_block(*(e.get_block()));
327 delete e.get_block();
328 throw checksum_error_exception(n_broken_block);
329 }
330
331 // Retype to data_block, eot_block - if possible
fd9632c0 332 switch(n_tape_block->get_type()){
333 case tape_block::TBT_DATA:
334 n_data_block = new data_block(*n_tape_block);
0ec6e042 335 break;
09cb0f4f 336 case tape_block::TBT_EOT:
fd9632c0 337 n_eot_block=new eot_block(*n_tape_block);
338 delete n_tape_block;
874a2bd8 339 n_eot_block->m_has_known_type=true;
fd9632c0 340 return n_eot_block;
50c86ded 341 default: // Unknown block, a bad thing!
342 return n_tape_block;
0ec6e042 343 }
fd9632c0 344 delete n_tape_block;
345
346 // Now only data blocks are left
347
348 switch(n_data_block->get_type()){
349 case 00: n_data_block_0=new data_block_0(*n_data_block); break;
350 case 01: n_data_block_x=new data_block_1(*n_data_block); break;
351 case 02: n_data_block_x=new data_block_2(*n_data_block); break;
352 case 03: n_data_block_x=new data_block_3(*n_data_block); break;
353 case 04: n_data_block_x=new data_block_4(*n_data_block); break;
354 case 05: n_data_block_x=new data_block_5(*n_data_block); break;
355 case 06: n_data_block_x=new data_block_6(*n_data_block); break;
356 case 07: n_data_block_x=new data_block_7(*n_data_block); break;
0ec6e042 357 default:
fd9632c0 358 return n_data_block;
359 }
360 if (n_data_block_0==0){ // So we must have found another one
361 delete n_data_block;
874a2bd8 362 n_data_block_x->m_has_known_type=true;
fd9632c0 363 return n_data_block_x;
0ec6e042 364 }
0ec6e042 365
fd9632c0 366 // Here only type 0 left
fd9632c0 367 switch(n_data_block_0->get_subtype()){
368 case 000: n_data_block_0_x=new data_block_0_0(*n_data_block_0); break;
369 case 001: n_data_block_0_x=new data_block_0_1(*n_data_block_0); break;
370 case 002: n_data_block_0_x=new data_block_0_2(*n_data_block_0); break;
371 case 003: n_data_block_0_x=new data_block_0_3(*n_data_block_0); break;
de6b6757 372 case 004: n_data_block_0_x=new data_block_0_4(*n_data_block_0); break;
fd9632c0 373 case 010: n_data_block_0_x=new data_block_0_10(*n_data_block_0); break;
374 case 014: n_data_block_0_x=new data_block_0_14(*n_data_block_0); break;
375 case 024: n_data_block_0_x=new data_block_0_24(*n_data_block_0); break;
376 case 044: n_data_block_0_x=new data_block_0_44(*n_data_block_0); break;
377 case 030: n_data_block_0_x=new data_block_0_30(*n_data_block_0); break;
378 case 054: n_data_block_0_x=new data_block_0_54(*n_data_block_0); break;
379 case 060: n_data_block_0_x=new data_block_0_60(*n_data_block_0); break;
380 case 064: n_data_block_0_x=new data_block_0_64(*n_data_block_0); break;
381 case 050: n_data_block_0_x=new data_block_0_50(*n_data_block_0); break;
0ec6e042 382 default:
fd9632c0 383 return n_data_block_0;
0ec6e042 384 }
fd9632c0 385 delete n_data_block_0;
874a2bd8 386 n_data_block_0_x->m_has_known_type=true;
fd9632c0 387 return n_data_block_0_x;
97b26985 388}
389
6c06db96 390/***************************************************************/
0ec6e042 391/*!
392 *\brief Default constructor.
393 */
394tape_block::tape_block(){}
395
09cb0f4f 396/***************************************************************/
397/*!
398 *\brief get a short type description string.
399 *\return A small string to be used in description methods.
400 *
401 * The string is always 19 characters long.\n
402 * An example is "(99-99) ".
403 */
404string tape_block::get_typestring(){
405 char buffer[13];
406 sprintf(buffer,"(%o-%o) ",get_type(),get_subtype());
407 buffer[10]=0;
408 return string(buffer);
409}
0ec6e042 410
6c06db96 411/***************************************************************/
0ec6e042 412/*!
413 *\brief Automatic constructor with file descriptor support
414 *
7880ae2d 415 * This constructor is used to initialise the block read in the
416 * block's data via a file descriptor.\n
0ec6e042 417 * This is done in the following way:
418 * - input_start() is called.
419 * - Data is read from fd. Stops on end of file or end of block
420 * or IO error. On EOF or IO error input_stop() is NOT called.
421 * - If there was no IO error the block's checksum is calculated
422 * and checked.
423 * - The block's type field is initialised.
424 * - input_stop() is called.
425 *
426 *\param fd A file descriptor where the read is taken from
427 *\param input_stop A pointer to a function called at the end of input
428 *\param input_start A pointer to a function called at the beginning
429 * of input
430 *\param start_stop_arg A pointer passed to input_start and input_stop().
431 */
97b26985 432tape_block::tape_block (int fd,
433 void(*input_start)(void *),
434 void (*input_stop)(void *),
435 void * start_stop_arg
436 )
437{
438 unsigned char buffer;
439 int retval;
440 int eot_needed=EOT_LENGTH;
441 int eob_needed=EOB_LENGTH;
442 int block_complete=0;
443
7880ae2d 444 block_type= TBT_BROKEN;
fed2c751 445 discarded_bytes=0;
97b26985 446 raw_data = 0;
447 raw_size = 0;
448 word_data = 0;
449 word_size = 0;
fed2c751 450 poolsize = 1024;
451
452 // Get initial portion of memory
453 raw_data=(unsigned char*)malloc(poolsize);
454
97b26985 455 if (input_start) input_start (start_stop_arg);
456
457 /* Look for block start in input stream */
458 do {
459 retval=read(fd, &buffer, 1);
460 if (retval == 0){ // End of file
7880ae2d 461 free(raw_data);
462 throw eof_legal_exception(discarded_bytes);
97b26985 463 return;
464 }
465 if (retval == -1){ // Error reading from fd
7880ae2d 466 free(raw_data);
467 throw io_error_exception();
97b26985 468 return;
469 }
fed2c751 470 discarded_bytes++;
97b26985 471
472 /* Look for end ot tape sequence */
473 switch(eot_needed){
474 case 3:
475 if (buffer==EOT_SEQ_1) eot_needed--;
476 else eot_needed=EOT_LENGTH;
477 break;
478 case 2:
479 if (buffer==EOT_SEQ_2) eot_needed--;
480 else eot_needed=EOT_LENGTH;
481 break;
482 case 1:
483 if (buffer==EOT_SEQ_3){
7880ae2d 484 raw_data=(unsigned char*)realloc(raw_data,3);
97b26985 485 raw_size=3;
fed2c751 486 discarded_bytes-=3;
97b26985 487 raw_data[0]=EOT_SEQ_1;
488 raw_data[1]=EOT_SEQ_2;
489 raw_data[2]=EOT_SEQ_3;
490 block_type =TBT_EOT;
97b26985 491 return;
492 }
493 else eot_needed=EOT_LENGTH;
494 break;
495 }
496 } while (buffer != (unsigned char) BLOCK_START_DELIMITER);
874a2bd8 497 discarded_bytes--; // Don't want to declare the start delimiter discarded!
fed2c751 498 raw_size=1; // But we want to account it here!
874a2bd8 499
97b26985 500 /* Now input the block data */
501 block_complete = 0;
874a2bd8 502
503 raw_data[0]=buffer; // Put the start delimiter in!
504
97b26985 505 do {
506 retval = read (fd, &buffer, 1);
507
508 if (retval == 0){ // End of file
7880ae2d 509 broken_block * b_block=new broken_block(*this);
510 free(raw_data);
511 throw eof_illegal_exception(b_block);
97b26985 512 }
513 if (retval == -1){ // Error reading from fd
7880ae2d 514 free(raw_data);
515 throw io_error_exception();
97b26985 516 }
97b26985 517
518 /* sort in the new byte */
519 raw_size++;
fed2c751 520 // Need more memory??
874a2bd8 521 if (raw_size>poolsize){
fed2c751 522 poolsize+=1024;
523 raw_data=(unsigned char*)realloc(raw_data,poolsize);
524 }
525
874a2bd8 526 raw_data[raw_size-1]=buffer;
97b26985 527
528 /* Look for end ot block sequence */
529 switch(eob_needed){
530 case 2:
531 if (buffer==BLOCK_END_DELIMITER_1) eob_needed--;
532 else eob_needed=EOB_LENGTH;
533 break;
534 case 1:
535 if (buffer==BLOCK_END_DELIMITER_2){
97b26985 536 block_complete=1;
537 }
538 else eob_needed=EOB_LENGTH;
539 break;
540 }
541 }while (!block_complete);
7880ae2d 542
97b26985 543 if (input_stop) input_stop (start_stop_arg);
544
7880ae2d 545
97b26985 546 /* Now we have the data in. */
547
548 if (init_words()!=0){
7880ae2d 549 broken_block * b_block=new broken_block(*this);
550 free(raw_data);
551 free(word_data);
552 throw checksum_error_exception(b_block);
97b26985 553 }
7880ae2d 554
97b26985 555 if (test_checksum()!=0){
7880ae2d 556 broken_block * b_block=new broken_block(*this);
557 free(raw_data);
558 free(word_data);
559 throw checksum_error_exception(b_block);
97b26985 560 }
97b26985 561 block_type=TBT_DATA;
97b26985 562}
563
97b26985 564
6c06db96 565/***************************************************************/
0ec6e042 566/*!
6c06db96 567 *\brief Initialize word representation of data.
568 *\retval 0 on success.
569 *\retval 1 on error.
0ec6e042 570 */
97b26985 571int tape_block::init_words(){
874a2bd8 572 word_size=(raw_size-EOB_LENGTH-1)/3;
97b26985 573 if (word_size<MINIMUM_DATA_BLOCK_WORDSIZE) return 1; // Block too short!
7880ae2d 574 word_data=(unsigned short *)malloc(word_size*sizeof(unsigned short));
97b26985 575 for (int pos=0; pos<word_size; pos++)
874a2bd8 576 word_data[pos]=combine466(raw_data+1+3*pos);
97b26985 577 return 0;
578}
579
6c06db96 580/***************************************************************/
0ec6e042 581/*!
6c06db96 582 *\brief Calculate and check checksum.
583 *\retval 0 on success.
584 *\retval 1 on error.
0ec6e042 585 */
97b26985 586int tape_block::test_checksum(){
587 unsigned short sum=0;
588 for (int pos=0; pos<word_size-1;pos++)
0ec6e042 589 sum^=word_data[pos];
97b26985 590 if (sum==word_data[word_size-1]) return 0;
591 else return 1;
592}
6c06db96 593
7880ae2d 594/***************************************************************/
595/*!
596 *\brief Standard constructor of exception base class.
597 */
598tape_block::exception::exception(){
599}
600
601/***************************************************************/
602/*!
603 *\brief Standard desctructor.
604 */
605tape_block::exception::~exception(){
606}
607
608/***************************************************************/
609/*!
610 *\brief Get pointer to erroneous tape_block
611 *\return A pointer to a tape_block instance.
612 *\note The user has to take care to free the instance contained
613 * in objects of classes supporting this method.
614 */
615tape_block * tape_block::exception::get_block(){
616 return m_broken_block;
617}
618
619/***************************************************************/
620/*!
621 *\brief Constructor
622 */
623tape_block::checksum_error_exception::
624checksum_error_exception (tape_block * broken_block){
625 this->m_broken_block=broken_block;
626}
627
628/***************************************************************/
629/*!
630 *\brief Constructor
631 */
632tape_block::eof_illegal_exception::
633eof_illegal_exception (tape_block * broken_block){
634 this->m_broken_block=broken_block;
635}
636
637/***************************************************************/
638/*!
639 *\brief Constructor
640 *\param bytes_consumed Amount of bytes read while looking
641 * for a block.
642 */
643tape_block::eof_legal_exception::
644eof_legal_exception(int bytes_consumed){
645 this->m_broken_block=NULL;
646 this->bytes_consumed=bytes_consumed;
647}
648
649/***************************************************************/
650/*!
651 *\brief Get amount of consumed data before EOF came along.
d65ad4d7 652 *\return Amount of bytes read from fd while looking for
7880ae2d 653 * new block or EOF.
654 */
655int tape_block::eof_legal_exception::get_consumed(){
656 return bytes_consumed;
657}
658
659/***************************************************************/
660/*!
661 *\brief Constructor
662 */
663tape_block::io_error_exception::
664io_error_exception(){
665 this->m_broken_block=NULL;
666}
667
668
669
670
671
672