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