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