1 /******************************************************************************
5 * $Date: 2007/03/26 01:00:40 $
8 * $Log: tape_block.cpp,v $
9 * Revision 2.0 2007/03/26 01:00:40 hachti
10 * *** empty log message ***
13 ******************************************************************************/
25 #include "tape_block.hh"
27 #include "silent_code.hh"
28 #include "hw_constants.hh"
30 #include "eot_block.hh"
31 #include "data_block.hh"
32 #include "broken_block.hh"
33 #include "data_block_0.hh"
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"
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"
58 /***************************************************************/
62 tape_block::~tape_block(){
67 /***************************************************************/
69 *\brief Copy constructor.
71 tape_block::tape_block(tape_block
& org
){
75 /***************************************************************/
77 *\brief Assignment operator.
79 void tape_block::operator=(tape_block
&org
){
80 block_type
=org
.block_type
;
81 discarded_bytes
=org
.discarded_bytes
;
83 raw_size
=org
.raw_size
;
85 word_size
=org
.word_size
;
87 raw_data
=(unsigned char *)malloc(raw_size
);
88 memcpy(raw_data
,org
.raw_data
,raw_size
);
91 word_data
=(unsigned short *)malloc(word_size
*sizeof(unsigned short));
92 memcpy(word_data
,org
.word_data
,word_size
*sizeof(unsigned short));
96 /***************************************************************/
98 *\brief Determine the block's type
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
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.
110 int tape_block::get_type(){
114 /***************************************************************/
116 *\brief Determine the block's subtype
118 * Some data block types have a subtype.
119 *\note This is a virtual method and to be used in derived classes.
122 int tape_block::get_subtype(){
126 /***************************************************************/
128 *\brief Get a cleartext description of the block.
129 *\return ldc style descriptive line for the block
131 vector
<string
> tape_block:: get_description(){
132 vector
<string
> result
;
133 result
.insert(result
.end(),
134 "***** "+get_typestring()+"Untyped tape block");
138 /***************************************************************/
140 *\brief Get size of the internal raw data buffer
141 *\return The size of the internal raw data buffer
143 int tape_block::get_raw_size(){
147 /***************************************************************/
149 *\brief Get amount of bytes discarded before beginning
151 *\return Length of unusable tape before block start delimiter.
153 int tape_block::get_discarded_bytes(){
154 return discarded_bytes
;
157 /***************************************************************/
159 *\brief Access internal raw data buffer
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
165 unsigned char * tape_block::get_raw_data(){
169 /***************************************************************/
171 *\brief Dump contents of the block to a file descriptor.
173 *\param fd A writable file descriptor.
174 *\return Always zero.
175 *\throw io_error_exception A write error occured.
177 int tape_block::dump_to_fd(int fd
){
178 int to_write
=raw_size
;
179 unsigned char * dump_ptr
=raw_data
;
182 written
=write(fd
,dump_ptr
,to_write
);
183 if (written
< 0) throw io_error_exception();
190 /***************************************************************/
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.
199 bool tape_block::is_endblock(){
203 /***************************************************************/
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.
211 vector
<string
> tape_block::get_exported_symbols(){
212 vector
<string
> result
;
216 /***************************************************************/
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.
224 vector
<string
> tape_block::get_called_symbols(){
225 vector
<string
> result
;
229 /***************************************************************/
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.
236 bool tape_block::has_known_type(){
237 return m_has_known_type
;
240 /***************************************************************/
242 *\brief Static factory method for tape blocks.
244 * This method creates a tape_block by using the private
245 * constructor with file descriptor support.\n
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
255 * methods not already present in tape_block.\n
256 * Objects of the following subclasses are automagically generated:\n
281 *\return Pointer to an instance of tape_block or a subclass.
282 *\param fd A file descriptor where data can be read from.
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.
288 *\param start_stop_arg A pointer passed to input_start
289 * and input_stop(). Can be anything.
291 tape_block
* tape_block::gen_from_fd(int fd
,
292 void(*input_start
)(void *),
293 void (*input_stop
)(void *),
294 void * start_stop_arg
)
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;
302 broken_block
* n_broken_block
=0;
304 //Use the private constructor which reads in the block from a file descriptor
306 n_tape_block
=new tape_block(fd
,input_start
,input_stop
,start_stop_arg
);
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
);
315 // Retype to data_block, eot_block - if possible
316 switch(n_tape_block
->get_type()){
317 case tape_block::TBT_DATA
:
318 n_data_block
= new data_block(*n_tape_block
);
320 case tape_block::TBT_EOT
:
321 n_eot_block
=new eot_block(*n_tape_block
);
323 n_eot_block
->m_has_known_type
=true;
325 default: // Unknown block, a bad thing!
330 // Now only data blocks are left
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;
344 if (n_data_block_0
==0){ // So we must have found another one
346 n_data_block_x
->m_has_known_type
=true;
347 return n_data_block_x
;
350 // Here only type 0 left
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;
356 case 004: n_data_block_0_x
=new data_block_0_4(*n_data_block_0
); break;
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;
367 return n_data_block_0
;
369 delete n_data_block_0
;
370 n_data_block_0_x
->m_has_known_type
=true;
371 return n_data_block_0_x
;
374 /***************************************************************/
376 *\brief Default constructor.
378 tape_block::tape_block(){}
380 /***************************************************************/
382 *\brief get a short type description string.
383 *\return A small string to be used in description methods.
385 * The string is always 19 characters long.\n
386 * An example is "(99-99) ".
388 string
tape_block::get_typestring(){
390 sprintf(buffer
,"(%o-%o) ",get_type(),get_subtype());
392 return string(buffer
);
395 /***************************************************************/
397 *\brief Automatic constructor with file descriptor support
399 * This constructor is used to initialise the block read in the
400 * block's data via a file descriptor.\n
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
407 * - The block's type field is initialised.
408 * - input_stop() is called.
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
414 *\param start_stop_arg A pointer passed to input_start and input_stop().
416 tape_block::tape_block (int fd
,
417 void(*input_start
)(void *),
418 void (*input_stop
)(void *),
419 void * start_stop_arg
422 unsigned char buffer
;
424 int eot_needed
=EOT_LENGTH
;
425 int eob_needed
=EOB_LENGTH
;
426 int block_complete
=0;
428 block_type
= TBT_BROKEN
;
436 // Get initial portion of memory
437 raw_data
=(unsigned char*)malloc(poolsize
);
439 if (input_start
) input_start (start_stop_arg
);
441 /* Look for block start in input stream */
443 retval
=read(fd
, &buffer
, 1);
444 if (retval
== 0){ // End of file
446 throw eof_legal_exception(discarded_bytes
);
449 if (retval
== -1){ // Error reading from fd
451 throw io_error_exception();
456 /* Look for end ot tape sequence */
459 if (buffer
==EOT_SEQ_1
) eot_needed
--;
460 else eot_needed
=EOT_LENGTH
;
463 if (buffer
==EOT_SEQ_2
) eot_needed
--;
464 else eot_needed
=EOT_LENGTH
;
467 if (buffer
==EOT_SEQ_3
){
468 raw_data
=(unsigned char*)realloc(raw_data
,3);
471 raw_data
[0]=EOT_SEQ_1
;
472 raw_data
[1]=EOT_SEQ_2
;
473 raw_data
[2]=EOT_SEQ_3
;
477 else eot_needed
=EOT_LENGTH
;
480 } while (buffer
!= (unsigned char) BLOCK_START_DELIMITER
);
481 discarded_bytes
--; // Don't want to declare the start delimiter discarded!
482 raw_size
=1; // But we want to account it here!
484 /* Now input the block data */
487 raw_data
[0]=buffer
; // Put the start delimiter in!
490 retval
= read (fd
, &buffer
, 1);
492 if (retval
== 0){ // End of file
493 broken_block
* b_block
=new broken_block(*this);
495 throw eof_illegal_exception(b_block
);
497 if (retval
== -1){ // Error reading from fd
499 throw io_error_exception();
502 /* sort in the new byte */
504 // Need more memory??
505 if (raw_size
>poolsize
){
507 raw_data
=(unsigned char*)realloc(raw_data
,poolsize
);
510 raw_data
[raw_size
-1]=buffer
;
512 /* Look for end ot block sequence */
515 if (buffer
==BLOCK_END_DELIMITER_1
) eob_needed
--;
516 else eob_needed
=EOB_LENGTH
;
519 if (buffer
==BLOCK_END_DELIMITER_2
){
522 else eob_needed
=EOB_LENGTH
;
525 }while (!block_complete
);
527 if (input_stop
) input_stop (start_stop_arg
);
530 /* Now we have the data in. */
532 if (init_words()!=0){
533 broken_block
* b_block
=new broken_block(*this);
536 throw checksum_error_exception(b_block
);
539 if (test_checksum()!=0){
540 broken_block
* b_block
=new broken_block(*this);
543 throw checksum_error_exception(b_block
);
549 /***************************************************************/
551 *\brief Initialize word representation of data.
552 *\retval 0 on success.
555 int tape_block::init_words(){
556 word_size
=(raw_size
-EOB_LENGTH
-1)/3;
557 if (word_size
<MINIMUM_DATA_BLOCK_WORDSIZE
) return 1; // Block too short!
558 word_data
=(unsigned short *)malloc(word_size
*sizeof(unsigned short));
559 for (int pos
=0; pos
<word_size
; pos
++)
560 word_data
[pos
]=combine466(raw_data
+1+3*pos
);
564 /***************************************************************/
566 *\brief Calculate and check checksum.
567 *\retval 0 on success.
570 int tape_block::test_checksum(){
571 unsigned short sum
=0;
572 for (int pos
=0; pos
<word_size
-1;pos
++)
574 if (sum
==word_data
[word_size
-1]) return 0;
578 /***************************************************************/
580 *\brief Standard constructor of exception base class.
582 tape_block::exception::exception(){
585 /***************************************************************/
587 *\brief Standard desctructor.
589 tape_block::exception::~exception(){
592 /***************************************************************/
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.
599 tape_block
* tape_block::exception::get_block(){
600 return m_broken_block
;
603 /***************************************************************/
607 tape_block::checksum_error_exception::
608 checksum_error_exception (tape_block
* broken_block
){
609 this->m_broken_block
=broken_block
;
612 /***************************************************************/
616 tape_block::eof_illegal_exception::
617 eof_illegal_exception (tape_block
* broken_block
){
618 this->m_broken_block
=broken_block
;
621 /***************************************************************/
624 *\param bytes_consumed Amount of bytes read while looking
627 tape_block::eof_legal_exception::
628 eof_legal_exception(int bytes_consumed
){
629 this->m_broken_block
=NULL
;
630 this->bytes_consumed
=bytes_consumed
;
633 /***************************************************************/
635 *\brief Get amount of consumed data before EOF came along.
636 *\return Amound of bytes read from fd while looking for
639 int tape_block::eof_legal_exception::get_consumed(){
640 return bytes_consumed
;
643 /***************************************************************/
647 tape_block::io_error_exception::
648 io_error_exception(){
649 this->m_broken_block
=NULL
;