11 #include "tape_block.hh"
13 #include "silent_code.hh"
14 #include "hw_constants.hh"
16 #include "eot_block.hh"
17 #include "data_block.hh"
18 #include "broken_block.hh"
19 #include "data_block_0.hh"
20 #include "data_block_0_0.hh"
21 #include "data_block_0_1.hh"
22 #include "data_block_0_2.hh"
23 #include "data_block_0_3.hh"
24 #include "data_block_0_4.hh"
25 #include "data_block_0_10.hh"
26 #include "data_block_0_14.hh"
27 #include "data_block_0_24.hh"
28 #include "data_block_0_44.hh"
29 #include "data_block_0_30.hh"
30 #include "data_block_0_54.hh"
31 #include "data_block_0_60.hh"
32 #include "data_block_0_50.hh"
33 #include "data_block_0_64.hh"
34 #include "data_block_1.hh"
35 #include "data_block_2.hh"
36 #include "data_block_3.hh"
37 #include "data_block_4.hh"
38 #include "data_block_5.hh"
39 #include "data_block_6.hh"
40 #include "data_block_7.hh"
44 /***************************************************************/
48 tape_block::~tape_block(){
53 /***************************************************************/
55 *\brief Copy constructor.
57 tape_block::tape_block(tape_block
& org
){
61 /***************************************************************/
63 *\brief Assignment operator.
65 void tape_block::operator=(tape_block
&org
){
66 block_type
=org
.block_type
;
67 discarded_bytes
=org
.discarded_bytes
;
69 raw_size
=org
.raw_size
;
71 word_size
=org
.word_size
;
73 raw_data
=(unsigned char *)malloc(raw_size
);
74 memcpy(raw_data
,org
.raw_data
,raw_size
);
77 word_data
=(unsigned short *)malloc(word_size
*sizeof(unsigned short));
78 memcpy(word_data
,org
.word_data
,word_size
*sizeof(unsigned short));
82 /***************************************************************/
84 *\brief Determine the block's type
86 * This routine returns the block's type.\\
87 *\retval TBT_DATA if it's a data block
88 *\retval TBT_EOT if it's an end of tape mark
90 *\note This function is virtual. That means that if the tape block in
91 * question is of the subtype data_block which overwrites this method,
92 * it will return the block type contained in the upper 4 bits of the
93 * first data word! Try to avoid testing for TBT_DATA. \n
94 * Blocks generated by gen_from_fd should never have TBT_DATA.
96 int tape_block::get_type(){
100 /***************************************************************/
102 *\brief Determine the block's subtype
104 * Some data block types have a subtype.
105 *\note This is a virtual method and to be used in derived classes.
108 int tape_block::get_subtype(){
112 /***************************************************************/
114 *\brief Get a cleartext description of the block.
115 *\return ldc style descriptive line for the block
117 vector
<string
> tape_block:: get_description(){
118 vector
<string
> result
;
119 result
.insert(result
.end(),
120 "***** "+get_typestring()+"Untyped tape block");
124 /***************************************************************/
126 *\brief Get size of the internal raw data buffer
127 *\return The size of the internal raw data buffer
129 int tape_block::get_raw_size(){
133 /***************************************************************/
135 *\brief Get amount of bytes discarded before beginning
137 *\return Length of unusable tape before block start delimiter.
139 int tape_block::get_discarded_bytes(){
140 return discarded_bytes
;
143 /***************************************************************/
145 *\brief Access internal raw data buffer
147 * The raw data buffer contains all block data except the \ \
148 * block start delimiter and block end delimiter.
149 *\return Pointer to internal raw data buffer of the block
151 unsigned char * tape_block::get_raw_data(){
155 /***************************************************************/
157 *\brief Dump contents of the block to a file descriptor.
159 *\param fd A writable file descriptor.
160 *\return Always zero.
161 *\throw io_error_exception A write error occured.
163 int tape_block::dump_to_fd(int fd
){
164 int to_write
=raw_size
;
165 unsigned char * dump_ptr
=raw_data
;
168 written
=write(fd
,dump_ptr
,to_write
);
169 if (written
< 0) throw io_error_exception();
176 /***************************************************************/
178 *\brief Determine if the block marks the end of an object
179 *\retval true The block marks the end of an object.
180 *\retval false The block does not mark the end of an object.
181 *\note This is only a virtual method returning always false.
182 * Classes which can be the legal last block of an object
183 * will overwrite this method so that it returns true.
185 bool tape_block::is_endblock(){
189 /***************************************************************/
191 *\brief Get exported symbols.
192 *\return A vector containing any symbols exported in this block.
193 *\note This is only a virtual method returning always an empty
194 * vector. Classes describing appropriate data blocks
195 * will override this method.
197 vector
<string
> tape_block::get_exported_symbols(){
198 vector
<string
> result
;
202 /***************************************************************/
204 *\brief Get called symbols.
205 *\return A vector containing any symbols called in this block.
206 *\note This is only a virtual method returning always an empty
207 * vector. Classes describing appropriate data blocks
208 * will override this method.
210 vector
<string
> tape_block::get_called_symbols(){
211 vector
<string
> result
;
215 /***************************************************************/
217 *\brief Determine if the block has a known type.
218 *\return true if type and subtype are known.
219 *\return false if the block type is not one of the valid end
220 * nodes of the type tree.
222 bool tape_block::has_known_type(){
223 return m_has_known_type
;
226 /***************************************************************/
228 *\brief Static factory method for tape blocks.
230 * This method creates a tape_block by using the private
231 * constructor with file descriptor support.\n
233 * The factory method tries to determine the block's type and then
234 * looks for appropriate subclasses. If there's an appropriate
235 * subclass fitting the newly-created object, an
236 * object of the subclass will be created and a pointer to this
237 * object will be returned to the caller.\n
238 * This allows for easy use of the object using it's virtual
239 * interface methods or a cast of the pointer to a sub type pointer
240 * according to the type information, and then using the subtype's
241 * methods not already present in tape_block.\n
242 * Objects of the following subclasses are automagically generated:\n
267 *\return Pointer to an instance of tape_block or a subclass.
268 *\param fd A file descriptor where data can be read from.
269 *\param input_start A pointer to a function
270 * called before reading data from fd.
271 *\param input_stop A pointer to a function
272 * called after reading data from fd.
274 *\param start_stop_arg A pointer passed to input_start
275 * and input_stop(). Can be anything.
277 tape_block
* tape_block::gen_from_fd(int fd
,
278 void(*input_start
)(void *),
279 void (*input_stop
)(void *),
280 void * start_stop_arg
)
282 tape_block
* n_tape_block
=0;
283 data_block
* n_data_block
=0;
284 data_block
* n_data_block_x
=0;
285 data_block_0
* n_data_block_0
=0;
286 data_block_0
* n_data_block_0_x
=0;
287 eot_block
* n_eot_block
=0;
288 broken_block
* n_broken_block
=0;
290 //Use the private constructor which reads in the block from a file descriptor
292 n_tape_block
=new tape_block(fd
,input_start
,input_stop
,start_stop_arg
);
295 catch(checksum_error_exception e
){
296 n_broken_block
=new broken_block(*(e
.get_block()));
297 delete e
.get_block();
298 throw checksum_error_exception(n_broken_block
);
301 // Retype to data_block, eot_block - if possible
302 switch(n_tape_block
->get_type()){
303 case tape_block::TBT_DATA
:
304 n_data_block
= new data_block(*n_tape_block
);
306 case tape_block::TBT_EOT
:
307 n_eot_block
=new eot_block(*n_tape_block
);
309 n_eot_block
->m_has_known_type
=true;
311 default: // Unknown block, a bad thing!
316 // Now only data blocks are left
318 switch(n_data_block
->get_type()){
319 case 00: n_data_block_0
=new data_block_0(*n_data_block
); break;
320 case 01: n_data_block_x
=new data_block_1(*n_data_block
); break;
321 case 02: n_data_block_x
=new data_block_2(*n_data_block
); break;
322 case 03: n_data_block_x
=new data_block_3(*n_data_block
); break;
323 case 04: n_data_block_x
=new data_block_4(*n_data_block
); break;
324 case 05: n_data_block_x
=new data_block_5(*n_data_block
); break;
325 case 06: n_data_block_x
=new data_block_6(*n_data_block
); break;
326 case 07: n_data_block_x
=new data_block_7(*n_data_block
); break;
330 if (n_data_block_0
==0){ // So we must have found another one
332 n_data_block_x
->m_has_known_type
=true;
333 return n_data_block_x
;
336 // Here only type 0 left
337 switch(n_data_block_0
->get_subtype()){
338 case 000: n_data_block_0_x
=new data_block_0_0(*n_data_block_0
); break;
339 case 001: n_data_block_0_x
=new data_block_0_1(*n_data_block_0
); break;
340 case 002: n_data_block_0_x
=new data_block_0_2(*n_data_block_0
); break;
341 case 003: n_data_block_0_x
=new data_block_0_3(*n_data_block_0
); break;
342 // case 004: n_data_block_0_x=new data_block_0_4(*n_data_block_0); break;
343 case 010: n_data_block_0_x
=new data_block_0_10(*n_data_block_0
); break;
344 case 014: n_data_block_0_x
=new data_block_0_14(*n_data_block_0
); break;
345 case 024: n_data_block_0_x
=new data_block_0_24(*n_data_block_0
); break;
346 case 044: n_data_block_0_x
=new data_block_0_44(*n_data_block_0
); break;
347 case 030: n_data_block_0_x
=new data_block_0_30(*n_data_block_0
); break;
348 case 054: n_data_block_0_x
=new data_block_0_54(*n_data_block_0
); break;
349 case 060: n_data_block_0_x
=new data_block_0_60(*n_data_block_0
); break;
350 case 064: n_data_block_0_x
=new data_block_0_64(*n_data_block_0
); break;
351 case 050: n_data_block_0_x
=new data_block_0_50(*n_data_block_0
); break;
353 return n_data_block_0
;
355 delete n_data_block_0
;
356 n_data_block_0_x
->m_has_known_type
=true;
357 return n_data_block_0_x
;
360 /***************************************************************/
362 *\brief Default constructor.
364 tape_block::tape_block(){}
366 /***************************************************************/
368 *\brief get a short type description string.
369 *\return A small string to be used in description methods.
371 * The string is always 19 characters long.\n
372 * An example is "(99-99) ".
374 string
tape_block::get_typestring(){
376 sprintf(buffer
,"(%o-%o) ",get_type(),get_subtype());
378 return string(buffer
);
381 /***************************************************************/
383 *\brief Automatic constructor with file descriptor support
385 * This constructor is used to initialise the block read in the
386 * block's data via a file descriptor.\n
387 * This is done in the following way:
388 * - input_start() is called.
389 * - Data is read from fd. Stops on end of file or end of block
390 * or IO error. On EOF or IO error input_stop() is NOT called.
391 * - If there was no IO error the block's checksum is calculated
393 * - The block's type field is initialised.
394 * - input_stop() is called.
396 *\param fd A file descriptor where the read is taken from
397 *\param input_stop A pointer to a function called at the end of input
398 *\param input_start A pointer to a function called at the beginning
400 *\param start_stop_arg A pointer passed to input_start and input_stop().
402 tape_block::tape_block (int fd
,
403 void(*input_start
)(void *),
404 void (*input_stop
)(void *),
405 void * start_stop_arg
408 unsigned char buffer
;
410 int eot_needed
=EOT_LENGTH
;
411 int eob_needed
=EOB_LENGTH
;
412 int block_complete
=0;
414 block_type
= TBT_BROKEN
;
422 // Get initial portion of memory
423 raw_data
=(unsigned char*)malloc(poolsize
);
425 if (input_start
) input_start (start_stop_arg
);
427 /* Look for block start in input stream */
429 retval
=read(fd
, &buffer
, 1);
430 if (retval
== 0){ // End of file
432 throw eof_legal_exception(discarded_bytes
);
435 if (retval
== -1){ // Error reading from fd
437 throw io_error_exception();
442 /* Look for end ot tape sequence */
445 if (buffer
==EOT_SEQ_1
) eot_needed
--;
446 else eot_needed
=EOT_LENGTH
;
449 if (buffer
==EOT_SEQ_2
) eot_needed
--;
450 else eot_needed
=EOT_LENGTH
;
453 if (buffer
==EOT_SEQ_3
){
454 raw_data
=(unsigned char*)realloc(raw_data
,3);
457 raw_data
[0]=EOT_SEQ_1
;
458 raw_data
[1]=EOT_SEQ_2
;
459 raw_data
[2]=EOT_SEQ_3
;
463 else eot_needed
=EOT_LENGTH
;
466 } while (buffer
!= (unsigned char) BLOCK_START_DELIMITER
);
467 discarded_bytes
--; // Don't want to declare the start delimiter discarded!
468 raw_size
=1; // But we want to account it here!
470 /* Now input the block data */
473 raw_data
[0]=buffer
; // Put the start delimiter in!
476 retval
= read (fd
, &buffer
, 1);
478 if (retval
== 0){ // End of file
479 broken_block
* b_block
=new broken_block(*this);
481 throw eof_illegal_exception(b_block
);
483 if (retval
== -1){ // Error reading from fd
485 throw io_error_exception();
488 /* sort in the new byte */
490 // Need more memory??
491 if (raw_size
>poolsize
){
493 raw_data
=(unsigned char*)realloc(raw_data
,poolsize
);
496 raw_data
[raw_size
-1]=buffer
;
498 /* Look for end ot block sequence */
501 if (buffer
==BLOCK_END_DELIMITER_1
) eob_needed
--;
502 else eob_needed
=EOB_LENGTH
;
505 if (buffer
==BLOCK_END_DELIMITER_2
){
508 else eob_needed
=EOB_LENGTH
;
511 }while (!block_complete
);
513 if (input_stop
) input_stop (start_stop_arg
);
516 /* Now we have the data in. */
518 if (init_words()!=0){
519 broken_block
* b_block
=new broken_block(*this);
522 throw checksum_error_exception(b_block
);
525 if (test_checksum()!=0){
526 broken_block
* b_block
=new broken_block(*this);
529 throw checksum_error_exception(b_block
);
535 /***************************************************************/
537 *\brief Initialize word representation of data.
538 *\retval 0 on success.
541 int tape_block::init_words(){
542 word_size
=(raw_size
-EOB_LENGTH
-1)/3;
543 if (word_size
<MINIMUM_DATA_BLOCK_WORDSIZE
) return 1; // Block too short!
544 word_data
=(unsigned short *)malloc(word_size
*sizeof(unsigned short));
545 for (int pos
=0; pos
<word_size
; pos
++)
546 word_data
[pos
]=combine466(raw_data
+1+3*pos
);
550 /***************************************************************/
552 *\brief Calculate and check checksum.
553 *\retval 0 on success.
556 int tape_block::test_checksum(){
557 unsigned short sum
=0;
558 for (int pos
=0; pos
<word_size
-1;pos
++)
560 if (sum
==word_data
[word_size
-1]) return 0;
564 /***************************************************************/
566 *\brief Standard constructor of exception base class.
568 tape_block::exception::exception(){
571 /***************************************************************/
573 *\brief Standard desctructor.
575 tape_block::exception::~exception(){
578 /***************************************************************/
580 *\brief Get pointer to erroneous tape_block
581 *\return A pointer to a tape_block instance.
582 *\note The user has to take care to free the instance contained
583 * in objects of classes supporting this method.
585 tape_block
* tape_block::exception::get_block(){
586 return m_broken_block
;
589 /***************************************************************/
593 tape_block::checksum_error_exception::
594 checksum_error_exception (tape_block
* broken_block
){
595 this->m_broken_block
=broken_block
;
598 /***************************************************************/
602 tape_block::eof_illegal_exception::
603 eof_illegal_exception (tape_block
* broken_block
){
604 this->m_broken_block
=broken_block
;
607 /***************************************************************/
610 *\param bytes_consumed Amount of bytes read while looking
613 tape_block::eof_legal_exception::
614 eof_legal_exception(int bytes_consumed
){
615 this->m_broken_block
=NULL
;
616 this->bytes_consumed
=bytes_consumed
;
619 /***************************************************************/
621 *\brief Get amount of consumed data before EOF came along.
622 *\return Amound of bytes read from fd while looking for
625 int tape_block::eof_legal_exception::get_consumed(){
626 return bytes_consumed
;
629 /***************************************************************/
633 tape_block::io_error_exception::
634 io_error_exception(){
635 this->m_broken_block
=NULL
;