+/******************************************************************************
+ *
+ * LDC2 source code
+ *
+ * $Date: 2007/03/26 03:20:31 $
+ * $Author: hachti $
+ *
+ * $Log: tape_block.cpp,v $
+ * Revision 2.1 2007/03/26 03:20:31 hachti
+ * *** empty log message ***
+ *
+ * Revision 2.0 2007-03-26 01:00:40 hachti
+ * *** empty log message ***
+ *
+ *
+ ******************************************************************************/
+
+#include <string>
+#include <vector>
+
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
+#include <stdio.h>
#include "tape_block.hh"
-#include "data_block.hh"
-#include "data_block_0.hh"
#include "silent_code.hh"
#include "hw_constants.hh"
+#include "eot_block.hh"
+#include "data_block.hh"
+#include "broken_block.hh"
+#include "data_block_0.hh"
+#include "data_block_0_0.hh"
+#include "data_block_0_1.hh"
+#include "data_block_0_2.hh"
+#include "data_block_0_3.hh"
+#include "data_block_0_4.hh"
+#include "data_block_0_10.hh"
+#include "data_block_0_14.hh"
+#include "data_block_0_24.hh"
+#include "data_block_0_44.hh"
+#include "data_block_0_30.hh"
+#include "data_block_0_54.hh"
+#include "data_block_0_60.hh"
+#include "data_block_0_50.hh"
+#include "data_block_0_64.hh"
+#include "data_block_1.hh"
+#include "data_block_2.hh"
+#include "data_block_3.hh"
+#include "data_block_4.hh"
+#include "data_block_5.hh"
+#include "data_block_6.hh"
+#include "data_block_7.hh"
+
+using namespace std;
+
+/***************************************************************/
+/*!
+ *\brief Destructor.
+ */
+tape_block::~tape_block(){
+ free(raw_data);
+ free(word_data);
+}
+
+/***************************************************************/
+/*!
+ *\brief Copy constructor.
+ */
+tape_block::tape_block(tape_block & org){
+ operator=(org);
+}
+
+/***************************************************************/
+/*!
+ *\brief Assignment operator.
+ */
+void tape_block::operator=(tape_block &org){
+ block_type=org.block_type;
+ discarded_bytes=org.discarded_bytes;
+ raw_data=0;
+ raw_size=org.raw_size;
+ word_data=0;
+ word_size=org.word_size;
+ if (raw_size){
+ raw_data=(unsigned char *)malloc(raw_size);
+ memcpy(raw_data,org.raw_data,raw_size);
+ }
+ if (word_size){
+ word_data=(unsigned short *)malloc(word_size*sizeof(unsigned short));
+ memcpy(word_data,org.word_data,word_size*sizeof(unsigned short));
+ }
+}
+
+/***************************************************************/
+/*!
+ *\brief Determine the block's type
+ *
+ * This routine returns the block's type.\\
+ *\retval TBT_DATA if it's a data block
+ *\retval TBT_EOT if it's an end of tape mark
+ *
+ *\note This function is virtual. That means that if the tape block in
+ * question is of the subtype data_block which overwrites this method,
+ * it will return the block type contained in the upper 4 bits of the
+ * first data word! Try to avoid testing for TBT_DATA. \n
+ * Blocks generated by gen_from_fd should never have TBT_DATA.
+ */
+int tape_block::get_type(){
+ return block_type;
+}
+
+/***************************************************************/
+/*!
+ *\brief Determine the block's subtype
+ *
+ * Some data block types have a subtype.
+ *\note This is a virtual method and to be used in derived classes.
+ *\return Always 0
+ */
+int tape_block::get_subtype(){
+ return 0;
+}
+
+/***************************************************************/
+/*!
+ *\brief Get a cleartext description of the block.
+ *\return ldc style descriptive line for the block
+ */
+vector<string> tape_block:: get_description(){
+ vector<string> result;
+ result.insert(result.end(),
+ "***** "+get_typestring()+"Untyped tape block");
+ return result;
+}
+
+/***************************************************************/
+/*!
+ *\brief Get size of the internal raw data buffer
+ *\return The size of the internal raw data buffer
+ */
+int tape_block::get_raw_size(){
+ return raw_size;
+}
+
+/***************************************************************/
+/*!
+ *\brief Get amount of bytes discarded before beginning
+ * of block sensed.
+ *\return Length of unusable tape before block start delimiter.
+ */
+int tape_block::get_discarded_bytes(){
+ return discarded_bytes;
+}
+
+/***************************************************************/
+/*!
+ *\brief Access internal raw data buffer
+ *
+ * The raw data buffer contains all block data except the
+ * block start and end delimiters.
+ *\return Pointer to internal raw data buffer of the block
+ */
+unsigned char * tape_block::get_raw_data(){
+ return raw_data;
+}
+
+/***************************************************************/
+/*!
+ *\brief Dump contents of the block to a file descriptor.
+ *
+ *\param fd A writable file descriptor.
+ *\return Always zero.
+ *\throw io_error_exception A write error occured.
+ */
+int tape_block::dump_to_fd(int fd){
+ int to_write=raw_size;
+ unsigned char * dump_ptr=raw_data;
+ int written;
+ while (to_write) {
+ written=write(fd,dump_ptr,to_write);
+ if (written < 0) throw io_error_exception();
+ to_write-=written;
+ dump_ptr+=written;
+ }
+ return 0;
+}
+
+/***************************************************************/
+/*!
+ *\brief Determine if the block marks the end of an object
+ *\retval true The block marks the end of an object.
+ *\retval false The block does not mark the end of an object.
+ *\note This is only a virtual method returning always false.
+ * Classes which can be the legal last block of an object
+ * will overwrite this method so that it returns true.
+ */
+bool tape_block::is_endblock(){
+ return false;
+}
+
+/***************************************************************/
+/*!
+ *\brief Get exported symbols.
+ *\return A vector containing any symbols exported in this block.
+ *\note This is only a virtual method returning always an empty
+ * vector. Classes describing appropriate data blocks
+ * will override this method.
+ */
+vector<string> tape_block::get_exported_symbols(){
+ vector<string> result;
+ return result;
+}
+
+/***************************************************************/
+/*!
+ *\brief Get called symbols.
+ *\return A vector containing any symbols called in this block.
+ *\note This is only a virtual method returning always an empty
+ * vector. Classes describing appropriate data blocks
+ * will override this method.
+ */
+vector<string> tape_block::get_called_symbols(){
+ vector<string> result;
+ return result;
+}
+
+/***************************************************************/
+/*!
+ *\brief Determine if the block has a known type.
+ *\return true if type and subtype are known.
+ *\return false if the block type is not one of the valid end
+ * nodes of the type tree.
+ */
+bool tape_block::has_known_type(){
+ return m_has_known_type;
+}
+/***************************************************************/
+/*!
+ *\brief Static factory method for tape blocks.
+ *
+ * This method creates a tape_block by using the private
+ * constructor with file descriptor support.\n
+ *
+ * The factory method tries to determine the block's type and then
+ * looks for appropriate subclasses. If there's an appropriate
+ * subclass fitting the newly-created object, an
+ * object of the subclass will be created and a pointer to this
+ * object will be returned to the caller.\n
+ * This allows for easy use of the object using it's virtual
+ * interface methods or a cast of the pointer to a sub type pointer
+ * according to the type information, and then using the subtype's
+ * methods not already present in tape_block.\n
+ * Objects of the following subclasses are automagically generated:\n
+ * - eot_block
+ * - data_block
+ * - data_block_0
+ * - data_block_0_0
+ * - data_block_0_1
+ * - data_block_0_2
+ * - data_block_0_3
+ * - data_block_0_4
+ * - data_block_0_10
+ * - data_block_0_14
+ * - data_block_0_24
+ * - data_block_0_30
+ * - data_block_0_50
+ * - data_block_0_54
+ * - data_block_0_60
+ * - data_block_0_64
+ * - data_block_1
+ * - data_block_2
+ * - data_block_3
+ * - data_block_4
+ * - data_block_5
+ * - data_block_6
+ * - data_block_7
+ *
+ *\return Pointer to an instance of tape_block or a subclass.
+ *\param fd A file descriptor where data can be read from.
+ *\param input_start A pointer to a function
+ * called before reading data from fd.
+ *\param input_stop A pointer to a function
+ * called after reading data from fd.
+ *
+ *\param start_stop_arg A pointer passed to input_start
+ * and input_stop(). Can be anything.
+ */
tape_block * tape_block::gen_from_fd(int fd,
void(*input_start)(void *),
void (*input_stop)(void *),
void * start_stop_arg)
{
- tape_block * res_block;
- res_block=new tape_block(fd,input_start,input_stop,start_stop_arg);
- data_block * d_block;
- data_block_0 * d0_block;
-
- // Retype to data_block if possible
- switch(res_block->get_type()){
- case tape_block::TBT_DATA: // Make pointer a data block
- d_block=new data_block(*res_block);
- delete res_block;
- res_block=d_block;
- break;
- default: // All other cases
- return res_block;
+ tape_block * n_tape_block=0;
+ data_block * n_data_block=0;
+ data_block * n_data_block_x=0;
+ data_block_0 * n_data_block_0=0;
+ data_block_0 * n_data_block_0_x=0;
+ eot_block * n_eot_block=0;
+ broken_block * n_broken_block=0;
+
+ //Use the private constructor which reads in the block from a file descriptor
+ try{
+ n_tape_block=new tape_block(fd,input_start,input_stop,start_stop_arg);
}
- // Retype again, if it's a data_block now
-
- switch(res_block->get_type()){
- case 00:
- d0_block=new data_block_0(*d_block);
- d_block=d0_block;
+ catch(checksum_error_exception e){
+ n_broken_block=new broken_block(*(e.get_block()));
+ delete e.get_block();
+ throw checksum_error_exception(n_broken_block);
+ }
+
+ // Retype to data_block, eot_block - if possible
+ switch(n_tape_block->get_type()){
+ case tape_block::TBT_DATA:
+ n_data_block = new data_block(*n_tape_block);
break;
-// case 01: d_block=new data_block_1(*d_block); break;
-// case 02: d_block=new data_block_2(*d_block); break;
-// case 03: d_block=new data_block_3(*d_block); break;
-// case 04: d_block=new data_block_4(*d_block); break;
-// case 05: d_block=new data_block_5(*d_block); break;
-// case 06: d_block=new data_block_6(*d_block); break;
-// case 07: d_block=new data_block_7(*d_block); break;
+ case tape_block::TBT_EOT:
+ n_eot_block=new eot_block(*n_tape_block);
+ delete n_tape_block;
+ n_eot_block->m_has_known_type=true;
+ return n_eot_block;
+ default: // Unknown block, a bad thing!
+ return n_tape_block;
+ }
+ delete n_tape_block;
+
+ // Now only data blocks are left
+
+ switch(n_data_block->get_type()){
+ case 00: n_data_block_0=new data_block_0(*n_data_block); break;
+ case 01: n_data_block_x=new data_block_1(*n_data_block); break;
+ case 02: n_data_block_x=new data_block_2(*n_data_block); break;
+ case 03: n_data_block_x=new data_block_3(*n_data_block); break;
+ case 04: n_data_block_x=new data_block_4(*n_data_block); break;
+ case 05: n_data_block_x=new data_block_5(*n_data_block); break;
+ case 06: n_data_block_x=new data_block_6(*n_data_block); break;
+ case 07: n_data_block_x=new data_block_7(*n_data_block); break;
default:
- return res_block;
+ return n_data_block;
+ }
+ if (n_data_block_0==0){ // So we must have found another one
+ delete n_data_block;
+ n_data_block_x->m_has_known_type=true;
+ return n_data_block_x;
}
- delete res_block;
- res_block=d_block;
- if (res_block->get_type()==0) switch(d0_block->get_subtype()){
-// case 000: d0_block=new data_block_0_00(d0_block); break;
-// case 001: d0_block=new data_block_0_01(d0_block); break;
-// case 002: d0_block=new data_block_0_02(d0_block); break;
-// case 003: d0_block=new data_block_0_03(d0_block); break;
-// case 004: d0_block=new data_block_0_04(d0_block); break;
-// case 010: d0_block=new data_block_0_10(d0_block); break;
-// case 014: d0_block=new data_block_0_14(d0_block); break;
-// case 024: d0_block=new data_block_0_24(d0_block); break;
-// case 030: d0_block=new data_block_0_30(d0_block); break;
-// case 054: d0_block=new data_block_0_54(d0_block); break;
-// case 060: d0_block=new data_block_0_60(d0_block); break;
+ // Here only type 0 left
+ switch(n_data_block_0->get_subtype()){
+ case 000: n_data_block_0_x=new data_block_0_0(*n_data_block_0); break;
+ case 001: n_data_block_0_x=new data_block_0_1(*n_data_block_0); break;
+ case 002: n_data_block_0_x=new data_block_0_2(*n_data_block_0); break;
+ case 003: n_data_block_0_x=new data_block_0_3(*n_data_block_0); break;
+ case 004: n_data_block_0_x=new data_block_0_4(*n_data_block_0); break;
+ case 010: n_data_block_0_x=new data_block_0_10(*n_data_block_0); break;
+ case 014: n_data_block_0_x=new data_block_0_14(*n_data_block_0); break;
+ case 024: n_data_block_0_x=new data_block_0_24(*n_data_block_0); break;
+ case 044: n_data_block_0_x=new data_block_0_44(*n_data_block_0); break;
+ case 030: n_data_block_0_x=new data_block_0_30(*n_data_block_0); break;
+ case 054: n_data_block_0_x=new data_block_0_54(*n_data_block_0); break;
+ case 060: n_data_block_0_x=new data_block_0_60(*n_data_block_0); break;
+ case 064: n_data_block_0_x=new data_block_0_64(*n_data_block_0); break;
+ case 050: n_data_block_0_x=new data_block_0_50(*n_data_block_0); break;
default:
- return res_block;
+ return n_data_block_0;
}
- delete res_block;
- res_block=d0_block;
-
- return res_block;
+ delete n_data_block_0;
+ n_data_block_0_x->m_has_known_type=true;
+ return n_data_block_0_x;
}
+/***************************************************************/
/*!
*\brief Default constructor.
*/
tape_block::tape_block(){}
-
-tape_block::tape_block(tape_block & org){
- block_type=org.block_type;
- init_state=org.init_state;
- data_read=org.data_read;
- raw_data=0;
- raw_size=org.raw_size;
- word_data=0;
- word_size=org.word_size;
- if (raw_size){
- raw_data=(unsigned char *)malloc(raw_size);
- memcpy(raw_data,org.raw_data,raw_size);
- }
- if (word_size){
- word_data=(unsigned short *)malloc(word_size*sizeof(unsigned short));
- memcpy(word_data,org.word_data,word_size*sizeof(unsigned short));
- }
+/***************************************************************/
+/*!
+ *\brief get a short type description string.
+ *\return A small string to be used in description methods.
+ *
+ * The string is always 19 characters long.\n
+ * An example is "(99-99) ".
+ */
+string tape_block::get_typestring(){
+ char buffer[13];
+ sprintf(buffer,"(%o-%o) ",get_type(),get_subtype());
+ buffer[10]=0;
+ return string(buffer);
}
+/***************************************************************/
/*!
*\brief Automatic constructor with file descriptor support
*
- * This constructor is used to initialise the block read in the block's
- * data via a file descriptor.\n
+ * This constructor is used to initialise the block read in the
+ * block's data via a file descriptor.\n
* This is done in the following way:
* - input_start() is called.
* - Data is read from fd. Stops on end of file or end of block
int eob_needed=EOB_LENGTH;
int block_complete=0;
- block_type= TBT_DISCARD;
- init_state= TBS_DEFAULT;
- data_read = 0;
+ block_type= TBT_BROKEN;
+ discarded_bytes=0;
raw_data = 0;
raw_size = 0;
word_data = 0;
word_size = 0;
-
+ poolsize = 1024;
+
+ // Get initial portion of memory
+ raw_data=(unsigned char*)malloc(poolsize);
+
if (input_start) input_start (start_stop_arg);
/* Look for block start in input stream */
do {
retval=read(fd, &buffer, 1);
if (retval == 0){ // End of file
- block_type=TBT_DISCARD;
- init_state=TBS_EOF_LEGAL;
+ free(raw_data);
+ throw eof_legal_exception(discarded_bytes);
return;
}
if (retval == -1){ // Error reading from fd
- block_type=TBT_DISCARD;
- init_state=TBS_IOERR;
+ free(raw_data);
+ throw io_error_exception();
return;
}
- data_read++;
+ discarded_bytes++;
/* Look for end ot tape sequence */
switch(eot_needed){
break;
case 1:
if (buffer==EOT_SEQ_3){
- raw_data=(unsigned char*)malloc(3);
+ raw_data=(unsigned char*)realloc(raw_data,3);
raw_size=3;
+ discarded_bytes-=3;
raw_data[0]=EOT_SEQ_1;
raw_data[1]=EOT_SEQ_2;
raw_data[2]=EOT_SEQ_3;
block_type =TBT_EOT;
- init_state =TBS_OK;
return;
}
else eot_needed=EOT_LENGTH;
break;
}
} while (buffer != (unsigned char) BLOCK_START_DELIMITER);
+ discarded_bytes--; // Don't want to declare the start delimiter discarded!
+ raw_size=1; // But we want to account it here!
-
/* Now input the block data */
block_complete = 0;
+
+ raw_data[0]=buffer; // Put the start delimiter in!
+
do {
retval = read (fd, &buffer, 1);
if (retval == 0){ // End of file
- block_type=TBT_DISCARD;
- init_state=TBS_EOF_ILLEGAL;
- return;
+ broken_block * b_block=new broken_block(*this);
+ free(raw_data);
+ throw eof_illegal_exception(b_block);
}
if (retval == -1){ // Error reading from fd
- block_type=TBT_DISCARD;
- init_state=TBS_IOERR;
- return;
+ free(raw_data);
+ throw io_error_exception();
}
- data_read++; // We have consumed a byte
/* sort in the new byte */
raw_size++;
- raw_data=(unsigned char*)realloc(raw_data,raw_size);
+ // Need more memory??
+ if (raw_size>poolsize){
+ poolsize+=1024;
+ raw_data=(unsigned char*)realloc(raw_data,poolsize);
+ }
+
raw_data[raw_size-1]=buffer;
/* Look for end ot block sequence */
break;
case 1:
if (buffer==BLOCK_END_DELIMITER_2){
- raw_size-=EOB_LENGTH; // Don't want the delimiter(s) in the data!
- raw_data=(unsigned char*)realloc(raw_data,raw_size);
block_complete=1;
}
else eob_needed=EOB_LENGTH;
break;
}
}while (!block_complete);
+
if (input_stop) input_stop (start_stop_arg);
+
/* Now we have the data in. */
if (init_words()!=0){
- init_state=TBS_CHECKSUM;
- return;
+ broken_block * b_block=new broken_block(*this);
+ free(raw_data);
+ free(word_data);
+ throw checksum_error_exception(b_block);
}
-
+
if (test_checksum()!=0){
- init_state=TBS_CHECKSUM;
- return;
+ broken_block * b_block=new broken_block(*this);
+ free(raw_data);
+ free(word_data);
+ throw checksum_error_exception(b_block);
}
-
block_type=TBT_DATA;
- init_state=TBS_OK;
}
+
+/***************************************************************/
/*!
- *\brief Default destructor.
+ *\brief Initialize word representation of data.
+ *\retval 0 on success.
+ *\retval 1 on error.
*/
-tape_block::~tape_block(){
- free(raw_data);
- free(word_data);
+int tape_block::init_words(){
+ word_size=(raw_size-EOB_LENGTH-1)/3;
+ if (word_size<MINIMUM_DATA_BLOCK_WORDSIZE) return 1; // Block too short!
+ word_data=(unsigned short *)malloc(word_size*sizeof(unsigned short));
+ for (int pos=0; pos<word_size; pos++)
+ word_data[pos]=combine466(raw_data+1+3*pos);
+ return 0;
}
+/***************************************************************/
/*!
- *\brief Query the block's state.
- *
- * If the state is not TBS_OK, the block must be discarded.
- *\return The block's initialisation state
+ *\brief Calculate and check checksum.
+ *\retval 0 on success.
+ *\retval 1 on error.
*/
-tape_block::tb_state_t tape_block::get_state(){
- return init_state;
+int tape_block::test_checksum(){
+ unsigned short sum=0;
+ for (int pos=0; pos<word_size-1;pos++)
+ sum^=word_data[pos];
+ if (sum==word_data[word_size-1]) return 0;
+ else return 1;
}
+/***************************************************************/
+/*!
+ *\brief Standard constructor of exception base class.
+ */
+tape_block::exception::exception(){
+}
+/***************************************************************/
/*!
- *\brief Get size of the internal raw data buffer
- *\return The size of the internal raw data buffer
+ *\brief Standard desctructor.
*/
-int tape_block::get_raw_size(){
- return raw_size;
+tape_block::exception::~exception(){
}
+/***************************************************************/
/*!
- *\brief Access internal raw data buffer
- *
- * The raw data buffer contains all block data except the \ \
- * block start delimiter and block end delimiter.
- *\return Pointer to internal raw data buffer of the block
+ *\brief Get pointer to erroneous tape_block
+ *\return A pointer to a tape_block instance.
+ *\note The user has to take care to free the instance contained
+ * in objects of classes supporting this method.
*/
-unsigned char * tape_block::get_raw_data(){
- return raw_data;
+tape_block * tape_block::exception::get_block(){
+ return m_broken_block;
}
+/***************************************************************/
/*!
- *\brief Determine the block's type
- *
- * This routine returns the block's type.\\
- *\retval TBT_DATA if it's a data block
- *\retval TBT_EOT if it's an end of tape mark
- *\retval TBR_DISCARD if the block is unusable
- *
- *\note This function is virtual. That means that if the tape block in
- * question is of the subtype data_block which overwrites this method,
- * it will return the block type contained in the upper 4 bits of the
- * first data word! Try to avoid testing for TBT_DATA. \n
- * Blocks generated by gen_from_fd should never have TBT_DATA.
+ *\brief Constructor
*/
-int tape_block::get_type(){
- return block_type;
+tape_block::checksum_error_exception::
+checksum_error_exception (tape_block * broken_block){
+ this->m_broken_block=broken_block;
}
+/***************************************************************/
/*!
- *\brief Determine the block's subtype
- *
- * Some data block types have a subtype.
- *\note This is to be used in derived classes.
- *\return Always 0
+ *\brief Constructor
*/
-int tape_block::get_subtype(){
- return 0;
+tape_block::eof_illegal_exception::
+eof_illegal_exception (tape_block * broken_block){
+ this->m_broken_block=broken_block;
}
+/***************************************************************/
+/*!
+ *\brief Constructor
+ *\param bytes_consumed Amount of bytes read while looking
+ * for a block.
+ */
+tape_block::eof_legal_exception::
+eof_legal_exception(int bytes_consumed){
+ this->m_broken_block=NULL;
+ this->bytes_consumed=bytes_consumed;
+}
+/***************************************************************/
/*!
- *\brief Initialize word representation of data
- *\retval 0 on success
- *\retval 1 on error
+ *\brief Get amount of consumed data before EOF came along.
+ *\return Amount of bytes read from fd while looking for
+ * new block or EOF.
*/
-int tape_block::init_words(){
- word_size=raw_size/3;
- if (word_size<MINIMUM_DATA_BLOCK_WORDSIZE) return 1; // Block too short!
- word_data=(unsigned short *)malloc(raw_size*sizeof(unsigned short));
- for (int pos=0; pos<word_size; pos++)
- word_data[pos]=combine466(raw_data+3*pos);
- return 0;
+int tape_block::eof_legal_exception::get_consumed(){
+ return bytes_consumed;
}
+/***************************************************************/
/*!
- *\brief Calculate and check checksum
- *\retval 0 on success
- *\retval 1 on error
+ *\brief Constructor
*/
-int tape_block::test_checksum(){
- unsigned short sum=0;
- for (int pos=0; pos<word_size-1;pos++)
- sum^=word_data[pos];
- if (sum==word_data[word_size-1]) return 0;
- else return 1;
+tape_block::io_error_exception::
+io_error_exception(){
+ this->m_broken_block=NULL;
}
+
+
+
+
+
+