From: hachti Date: Thu, 8 Mar 2007 23:20:20 +0000 (+0000) Subject: *** empty log message *** X-Git-Url: http://gitweb.hachti.de/?a=commitdiff_plain;h=7880ae2dd0ed55ec671be0fc449487420a126626;p=h316.git *** empty log message *** --- diff --git a/pc-tools/ldc2/Makefile b/pc-tools/ldc2/Makefile index d59b5b7..e5b9c8a 100644 --- a/pc-tools/ldc2/Makefile +++ b/pc-tools/ldc2/Makefile @@ -1,26 +1,37 @@ APP_NAME=ldc2 APP_OBJECTS= main.o silent_code.o configuration_manager.o tool.o config.o CCFLAGS+=-Wall -BLOCK_OBJECTS=data_block_0_10.o data_block_0_14.o data_block_0_1.o data_block_0_24.o\ + + + +BUILD_STAMP="\"`uname -a`\"" +VERSION="\"0.7\"" +BUILD_DATE="\"`date` \"" + +CCFLAGS+=-D BUILD_STAMP=$(BUILD_STAMP) -D VERSION=$(VERSION) -D BUILD_DATE=$(BUILD_DATE) + +BLOCK_OBJECTS=tape_block.o eot_block.o data_block.o broken_block.o\ + data_block_0_10.o data_block_0_14.o data_block_0_1.o data_block_0_24.o\ data_block_0_2.o data_block_0_30.o data_block_0_3.o data_block_0_4.o\ data_block_0_50.o data_block_0_54.o data_block_0_60.o data_block_0_44.o\ - data_block_0_64.o tape_block.o eot_block.o discard_block.o\ - data_block.o data_block_0.o data_block_0_0.o\ + data_block_0_64.o \ + data_block_0.o data_block_0_0.o\ data_block_0_label_extractor.o data_block_1.o data_block_2.o\ data_block_3.o data_block_4.o data_block_5.o \ - data_block_6.o data_block_7.o \ - + data_block_6.o data_block_7.o + -APP_OBJECTS+=$(BLOCK_OBJECTS) +OBJECTS=$(BLOCK_OBJECTS) $(APP_OBJECTS) SRC_DIR=src default: $(APP_NAME) -APP_SOURCES=$(foreach obj, $(APP_OBJECTS:.o=.cpp),$(SRC_DIR)/$(obj)) +clean: + @rm -rf $(APP_NAME) *.o doxy/ $(SRC_DIR)/*~ ul_timestamp *.dep + @$(MAKE) -C doc clean -##All objects depend on all headers. simple but works -#*.o: $(SRC_DIR)/*.hh +APP_SOURCES=$(foreach obj, $(OBJECTS:.o=.cpp),$(SRC_DIR)/$(obj)) %.dep : $(SRC_DIR)/%.cpp @ echo Building dependencies for $<. @@ -30,21 +41,18 @@ APP_SOURCES=$(foreach obj, $(APP_OBJECTS:.o=.cpp),$(SRC_DIR)/$(obj)) %.o : $(SRC_DIR)/%.cpp %.dep g++ -c $(CCFLAGS) -o $@ $< -$(APP_NAME) : $(APP_OBJECTS) - g++ -o$@ $^ +$(APP_NAME) : $(OBJECTS) + g++ -o$@ $^ -saustall: $(APP_OBJECTS:.o=.dep) +saustall: $(OBJECTS:.o=.dep) -clean: - @rm -rf $(APP_NAME) *.o doxy/ $(SRC_DIR)/*~ ul_timestamp *.dep - @$(MAKE) -C doc clean - -doxy/timestamp: $(APP_OBJECTS) +doxy/timestamp: $(OBJECTS) @ if [ ! -d doxy ]; then mkdir doxy; fi @doxygen Doxyfile touch $@ doxy: doxy/timestamp + upload: ul_timestamp ul_timestamp: doxy/timestamp @@ -58,10 +66,15 @@ doc: all: $(APP_NAME) doxy doc -.PHONY: all clean doxy upload docdata_block_0_0.o +.PHONY: all clean doxy upload docdata_block_0_0.o default .PRECIOUS: *.dep -#$(APP_OBJECTS:.o=.d) +#$(OBJECTS:.o=.d) + +%.hh %.cpp: + @ echo "Missing file \"$@\"... No problem :-)" + include $(wildcard *.dep) +#include $(OBJECTS:.o=.dep) \ No newline at end of file diff --git a/pc-tools/ldc2/data/io.lib b/pc-tools/ldc2/data/io.lib new file mode 100644 index 0000000..1b64302 Binary files /dev/null and b/pc-tools/ldc2/data/io.lib differ diff --git a/pc-tools/ldc2/data/test1.obj b/pc-tools/ldc2/data/test1.obj new file mode 100644 index 0000000..b0275b5 Binary files /dev/null and b/pc-tools/ldc2/data/test1.obj differ diff --git a/pc-tools/ldc2/src/broken_block.cpp b/pc-tools/ldc2/src/broken_block.cpp new file mode 100644 index 0000000..17c8628 --- /dev/null +++ b/pc-tools/ldc2/src/broken_block.cpp @@ -0,0 +1,34 @@ + +#include "broken_block.hh" + +using namespace std; + +/*! + *\brief Specialisation constructor. + */ +broken_block::broken_block(tape_block& org) + :tape_block(org) +{ + block_type=TBT_BROKEN; +} + +/*! + *\brief Describe the block. + *\return A vector of text lines describing this block. + */ +vector broken_block::get_description(){ + vector result; + string r_string="********* (B-B) Broken block"; + result.insert(result.end(),r_string); + return result; +} + +/*! + *\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. + */ +bool broken_block::get_obj_end(){ + return false; +} + diff --git a/pc-tools/ldc2/src/broken_block.hh b/pc-tools/ldc2/src/broken_block.hh new file mode 100644 index 0000000..fe84d57 --- /dev/null +++ b/pc-tools/ldc2/src/broken_block.hh @@ -0,0 +1,29 @@ +#ifndef BROKEN_BLOCK_HH +#define BROKEN_BLOCK_HH + +#include +#include + +#include "tape_block.hh" + +using namespace std; + +/*! + * Class representating a broken tape block + */ +class broken_block + : public tape_block +{ + friend class tape_block; + +private: + broken_block(tape_block&); + +public: + int get_word_size(); + virtual vector get_description(); + virtual bool get_obj_end(); +}; + + +#endif diff --git a/pc-tools/ldc2/src/config.cpp b/pc-tools/ldc2/src/config.cpp index a3f14ff..974fc91 100644 --- a/pc-tools/ldc2/src/config.cpp +++ b/pc-tools/ldc2/src/config.cpp @@ -28,13 +28,14 @@ int cfg_output_exported=0; int cfg_output_unsatisfied=0; int cfg_splitobjects=0; int cfg_splitobjects_numbered=0; -int cfg_ignoreblock_errors=0; -int cfg_ignorechecksum_errors=0; +int cfg_ignore_block_errors=0; +int cfg_ignore_checksum_errors=0; int cfg_pause_on_checksum_error=0; int cfg_ignore_unknown_block_errors=0; int cfg_ignore_object_integrity_errors=0; int cfg_list_contents=1; int cfg_verbose=0; +int cfg_version=0; int in_fd=0; //! File descriptor for data input int out_fd=1; //! File descriptor for text output @@ -76,11 +77,11 @@ void do_config(int argc, char ** args){ ar.add_option_switch("b","ignore_block_errors", "Ignore block integrity errors. This will output broken blocks,too", - &cfg_ignoreblock_errors,true,true); + &cfg_ignore_block_errors,true,true); ar.add_option_switch("k","ignore_checksum_errors", "Ignore block checksum errors. Errors will be converted to warnings.", - &cfg_ignorechecksum_errors,true,true); + &cfg_ignore_checksum_errors,true,true); ar.add_option_switch("p","pause_on_checksum_error", "Wait for user input on checksum error.", @@ -98,6 +99,10 @@ Errors will be converted to warnings.", ar.add_option_switch("v","verbose", "Be a bit more verbose.", &cfg_verbose,true,true); + + ar.add_option_switch("V","version", + "Output version information and exit.", + &cfg_version,false,true); ar.add_option_value("i","in_file", "specify input file", @@ -146,7 +151,8 @@ Errors will be converted to warnings.", // If user has a config file, use it. if (cfg_config_file_set){ - fprintf(stderr,"Using config file: \"%s\"\n",cfg_config_file.c_str()); + if (cfg_verbose) + fprintf(stderr,"Using config file: \"%s\"\n",cfg_config_file.c_str()); if(dump_vector_err(ar.read_file(cfg_config_file))){ dump_vector_err(ar.get_help()); exit(7); @@ -160,26 +166,30 @@ Errors will be converted to warnings.", } if (cfg_infile_set==1){ - fprintf(stderr,"Opening file for input: \"%s\"\n",cfg_infile.c_str()); + if (cfg_verbose) + fprintf(stderr,"Opening file for input: \"%s\"\n",cfg_infile.c_str()); in_fd=open(cfg_infile.c_str(),O_RDONLY); if (in_fd<0){ - fprintf(stderr,"Error: could not open file \"%s\" for reading!\n",cfg_infile.c_str()); + fprintf(stderr,"Error: could not open file \"%s\" for reading!\n", + cfg_infile.c_str()); exit (1); } } if (cfg_outfile_set==1){ - fprintf(stderr,"Opening file for output: \"%s\"\n",cfg_outfile.c_str()); - out_fd=open(cfg_outfile.c_str(),O_WRONLY|O_TRUNC); + if (cfg_verbose) + fprintf(stderr,"Opening file for output: \"%s\"\n",cfg_outfile.c_str()); + out_fd=open(cfg_outfile.c_str(),O_CREAT|O_WRONLY|O_TRUNC,0666); if (out_fd<0){ - fprintf(stderr,"Error: could not open file \"%s\" for writing!\n",cfg_outfile.c_str()); + fprintf(stderr,"Error: could not open file \"%s\" for writing!\n", + cfg_outfile.c_str()); exit (1); } } // The configuration implications according to /cfg007/ if (in_fd==0) cfg_pause_on_checksum_error=0; - if (cfg_pause_on_checksum_error==1) cfg_ignorechecksum_errors=1; + if (cfg_pause_on_checksum_error==1) cfg_ignore_checksum_errors=1; if (cfg_output_info||cfg_output_called||cfg_output_exported||cfg_output_unsatisfied) cfg_list_contents=0; diff --git a/pc-tools/ldc2/src/config.hh b/pc-tools/ldc2/src/config.hh index 1f80c79..fec7763 100644 --- a/pc-tools/ldc2/src/config.hh +++ b/pc-tools/ldc2/src/config.hh @@ -19,13 +19,14 @@ extern int cfg_output_exported; extern int cfg_output_unsatisfied; extern int cfg_splitobjects; extern int cfg_splitobjects_numbered; -extern int cfg_ignoreblock_errors; -extern int cfg_ignorechecksum_errors; +extern int cfg_ignore_block_errors; +extern int cfg_ignore_checksum_errors; extern int cfg_pause_on_checksum_error; extern int cfg_ignore_unknown_block_errors; extern int cfg_ignore_object_integrity_errors; extern int cfg_list_contents; extern int cfg_verbose; +extern int cfg_version; extern int in_fd; //! File descriptor for data input extern int out_fd; //! File descriptor for text output diff --git a/pc-tools/ldc2/src/data_block.cpp b/pc-tools/ldc2/src/data_block.cpp index e5d7c00..25abaa3 100644 --- a/pc-tools/ldc2/src/data_block.cpp +++ b/pc-tools/ldc2/src/data_block.cpp @@ -20,10 +20,7 @@ data_block::data_block(tape_block& org) *\return the block type extracted from the block's data. */ int data_block::get_type(){ - if ((init_state==TBS_OK)&&word_data) - return (word_data[0]&0xf000)>>12; - else - return block_type; + return (word_data[0]&0xf000)>>12; } /*! diff --git a/pc-tools/ldc2/src/data_block.hh b/pc-tools/ldc2/src/data_block.hh index 2b18f95..415337b 100644 --- a/pc-tools/ldc2/src/data_block.hh +++ b/pc-tools/ldc2/src/data_block.hh @@ -1,5 +1,5 @@ -#ifndef DATA_BLOCK_H -#define DATA_BLOCK_H +#ifndef DATA_BLOCK_HH +#define DATA_BLOCK_HH #include #include diff --git a/pc-tools/ldc2/src/discard_block.cpp b/pc-tools/ldc2/src/discard_block.cpp deleted file mode 100644 index a854a49..0000000 --- a/pc-tools/ldc2/src/discard_block.cpp +++ /dev/null @@ -1,69 +0,0 @@ -#include -#include - -#include "tape_block.hh" -#include "discard_block.hh" - - -using namespace std; - -/***************************************************************/ -/*! - *\brief Copy constructor. - */ -discard_block::discard_block(discard_block & org){ - operator=(org); -} - -/***************************************************************/ -/*! - *\brief Get a cleartext description of the block. - *\return ldc style descriptive line for the block - */ -vector discard_block::get_description(){ - string r_string; - switch(init_state){ - case TBS_OK: - r_string="***** (X-XX) Discard block, init_status OK,\ - this is an illegal condition!"; - break; - case TBS_EOF_LEGAL: - r_string=" (EOF) Legal EOF"; - break; - case TBS_EOF_ILLEGAL: - r_string=" (EOF) Illegal EOF while in block!"; - break; - case TBS_CHECKSUM: - r_string=" (CHK) Checksum error in Block!"; - break; - case TBS_IOERR: - r_string=" (IOE) IO-Error during initialisation!"; - break; - case TBS_DEFAULT: - r_string="***** (X-XX) Discard block, init_status not set,\ - this is an illegal condition!"; - break; - default: - r_string="***** (X-XX) Discard block, init_status unknown,\ - this is an illegal condition!"; - break; - } - vector result; - result.insert(result.end(),r_string); - return result; -} - -/***************************************************************/ -/*! - *\brief Specialisation constructor - */ -discard_block::discard_block(tape_block & org) - : tape_block(org) -{ -} - -/***************************************************************/ -/*! - *\brief Default constructor. - */ -discard_block::discard_block(){} diff --git a/pc-tools/ldc2/src/discard_block.hh b/pc-tools/ldc2/src/discard_block.hh deleted file mode 100644 index af9ad66..0000000 --- a/pc-tools/ldc2/src/discard_block.hh +++ /dev/null @@ -1,38 +0,0 @@ -#ifndef DISCARD_BLOCK_H -#define DISCARD_BLOCK_H - -#include -#include - -#include "tape_block.hh" - -using namespace std; - -/*! - *\brief Class representating an invalid block. - * - *Reasons for a block to be invalid are:\n - * - EOF on fd used to initialise, - * - Checksum error in a block - * - IO error while reading from fd - * The reason for the block to be a discard_block can be - * resolved by using get_state(). - */ -class discard_block - :public tape_block -{ - friend tape_block* tape_block::gen_from_fd(int,void(*)(void*), - void(*)(void*),void*); -public: // methods - discard_block(discard_block &); - virtual vector get_description(); - -discard_block(tape_block &); -private: // methods - - discard_block(); - -}; // class discard_block - - -#endif diff --git a/pc-tools/ldc2/src/hw_constants.hh b/pc-tools/ldc2/src/hw_constants.hh index 4be1bc0..1924686 100644 --- a/pc-tools/ldc2/src/hw_constants.hh +++ b/pc-tools/ldc2/src/hw_constants.hh @@ -2,14 +2,13 @@ #define HW_CONSTANTS_H /* Block start delimiter */ -#define BLOCK_START_DELIMITER 0x81 +#define BLOCK_START_DELIMITER 0x81 //0201 (SOM:Start of Message) #define MINIMUM_DATA_BLOCK_WORDSIZE 1 /* Block end delimiters */ #define EOB_LENGTH 2 -#define BLOCK_END_DELIMITER_1 0223 -//#define BLOCK_END_DELIMITER_2 0223 // ==0x93 +#define BLOCK_END_DELIMITER_1 0223 // XOFF 0x93 #define BLOCK_END_DELIMITER_2 0xFF diff --git a/pc-tools/ldc2/src/main.cpp b/pc-tools/ldc2/src/main.cpp index 53c764e..ba8a36c 100644 --- a/pc-tools/ldc2/src/main.cpp +++ b/pc-tools/ldc2/src/main.cpp @@ -1,5 +1,6 @@ /* ldc2 preliminary main program */ + #include #include @@ -11,16 +12,33 @@ using namespace std; +#ifndef BUILD_STAMP +#define BUILD_STAMP "" +#endif + +#ifndef VERSION +#define VERSION "0.0" +#endif + +#define ID_STRING "\n ldc2 - The X16 object analyser\n\ + (C) 2007 Philipp Hachtmann\n\n\ + Version %s, built %s\n %s\n\n" + + + /*! *\brief The main routine. */ int main(int argc, char ** args){ + // Do all the configuration stuff do_config(argc,args); - // Now we go to read the data! - + if (cfg_version){ + fprintf(stderr, ID_STRING, VERSION, BUILD_DATE, BUILD_STAMP); + exit(0); + } vector tape; vector > objects; @@ -28,157 +46,231 @@ int main(int argc, char ** args){ tape_block * block=0; bool read_ahead=true; + int read_pointer=0; int block_start=0; int block_end=0; int blocks_read=0; + bool in_object=false; + int errors=0; int warnings=0; int errcode=0; + bool spaced=false; + while(read_ahead){ - block=tape_block::gen_from_fd(in_fd); - - block_start=read_pointer+block->get_discarded_bytes(); - block_end=block_start+block->get_raw_size()-1; - read_pointer=block_end+1; - //printf("Discarded:%2x, Raw Size:%2x\n",block->get_discarded_bytes(),block->get_raw_size()); - switch(block->get_state()){ // switchy - case tape_block::TBS_EOF_LEGAL: - delete block; - if (!in_object){ - fprintf(stderr,"File successfully read.\n"); - } else { - if (cfg_ignore_object_integrity_errors){ - fprintf(stderr,"Warning: Object integrity check failed!\n"); - warnings++; - } else { - fprintf(stderr,"Error: Object integrity check failed!\n"); - errors++; - errcode=6; - } - } - read_ahead=false; - break; - case tape_block::TBS_EOF_ILLEGAL: - delete block; - if (cfg_ignoreblock_errors){ - fprintf(stderr,"Warning: Block integrity check failed!\n"); - warnings++; - } else { - fprintf(stderr,"Error: Block integrity check failed!\n"); - errors++; - errcode=3; - } - read_ahead=false; - break; - case tape_block::TBS_CHECKSUM: - delete block; - char buffer[200]; - snprintf(buffer,200,"Block No. %3i: Start(hex):%5x End(hex):%5x Size(hex):%3x\n", - blocks_read-1, - block_start, - block_end, - block->get_raw_size()); - if (cfg_ignorechecksum_errors){ + try{ + block=tape_block::gen_from_fd(in_fd); + blocks_read++; + tape.insert(tape.end(),block); + block_start=read_pointer+block->get_discarded_bytes(); + block_end=block_start+block->get_raw_size()-1; + read_pointer=block_end+1; + + if (cfg_verbose) { char buffer[200]; - snprintf(buffer,200,"Block No. %3i: Start(hex):%5x End(hex):%5x Size(hex):%3x\n", + if (!spaced) fprintf(stderr,"\n"); + spaced=false; + + snprintf(buffer,200,"Block No.%03i: Start:0x%04x End:0x%04x Size:0x%02x\n", blocks_read-1, block_start, block_end, block->get_raw_size()); - write (out_fd,buffer,strlen(buffer)); - fprintf(stderr,"Warning: Block checksum wrong!\n"); - fprintf(stderr,buffer); - warnings++; - } else { - fprintf(stderr,"Error: Block checksum wrong!\n"); fprintf(stderr,buffer); - errors++; - read_ahead=false; - errcode=4; } - break; - case tape_block::TBS_DEFAULT: - delete block; - fprintf(stderr,"TBS_DEFAULT encountered ->> SEVERE INTERNAL ERROR!\n"); - errors++; - errcode=100; - read_ahead=false; - break; - case tape_block::TBS_IOERR: - delete block; + if(cfg_list_contents) + dump_vector(block->get_description()); + } + + catch(tape_block::eof_legal_exception &e){ + read_pointer+=e.get_consumed(); + break; // Immediately leave read loop. + } + + catch(tape_block::io_error_exception){ if (in_fd){ - fprintf(stderr,"Error: Could not read from \"%s\"!\n",cfg_infile.c_str()); + fprintf(stderr,"Error: Could not read from \"%s\"!\n",cfg_infile.c_str()); } else { - fprintf(stderr,"Error: Could not read from stdin!\n"); + fprintf(stderr,"Error: Could not read from stdin!\n"); } errors++; - read_ahead=false; errcode=2; - case tape_block::TBS_OK: - tape.insert(tape.end(),block); + break; // Immediately leave read loop. + } + + catch(tape_block::eof_illegal_exception &e){ + block=e.get_block(); blocks_read++; + tape.insert(tape.end(),block); + block_start=read_pointer+block->get_discarded_bytes(); + block_end=block_start+block->get_raw_size()-1; + read_pointer=block_end+1; - if (cfg_verbose){ - char buffer[200]; - snprintf(buffer,200,"Block No. %3i: Start(hex):%5x End(hex):%5x Size(hex):%3x\n", - blocks_read-1, - block_start, - block_end, - block->get_raw_size()); - write (out_fd,buffer,strlen(buffer)); + if (!spaced) fprintf(stderr,"\n"); + spaced=false; + char buffer[200]; + snprintf(buffer,200,"Block No.%03i: Start:0x%04x End:0x%04x Size:0x%02x\n", + blocks_read-1, + block_start, + block_end, + block->get_raw_size()); + fprintf(stderr,buffer); + + if (cfg_ignore_block_errors){ + fprintf(stderr,"Warning: Block integrity check failed!\n"); + warnings++; + } else { + fprintf(stderr,"Error: Block integrity check failed!\n"); + errors++; + errcode=3; + read_ahead=false; } - if(cfg_list_contents) dump_vector(block->get_description()); + fprintf(stderr,"\n"); + spaced=true; + } + + catch(tape_block::checksum_error_exception &e){ + block=e.get_block(); + blocks_read++; + tape.insert(tape.end(),block); + block_start=read_pointer+block->get_discarded_bytes(); + block_end=block_start+block->get_raw_size()-1; + read_pointer=block_end+1; - if (block->get_type()==0x10){ // Unknown data block! - if (cfg_ignore_unknown_block_errors){ - fprintf(stderr,"Warning: Unknown block type!\n"); - warnings++; - }else { - fprintf(stderr,"Error: Unknown block type!\n"); - errors++; - errcode=5; - read_ahead=false; - } - } - current_object.insert(current_object.end(),block); + if (!spaced) fprintf(stderr,"\n"); + spaced=false; + char buffer[200]; + snprintf(buffer,200,"Block No.%03i: Start:0x%04x End:0x%04x Size:0x%02x\n", + blocks_read-1, + block_start, + block_end, + block->get_raw_size()); + fprintf(stderr,buffer); - if ((block->get_type()==data_block::TBT_EOT) - ||((block->get_type()==0)&&(block->get_subtype()==3)) - ||((block->get_type()==4)||(block->get_type()==3)) - ||((block->get_type()==0)&&(block->get_subtype()==014))){ - in_object=false; - objects.insert(objects.end(),current_object); - current_object.clear(); + if (cfg_ignore_checksum_errors){ + fprintf(stderr,"Warning: Block checksum wrong!\n"); + warnings++; } else { - in_object=true; + fprintf(stderr,"Error: Block checksum wrong!\n"); + errors++; + read_ahead=false; + errcode=4; } - - break; - } // switch - } // generate loop - close(in_fd); + if(cfg_list_contents) + dump_vector(block->get_description()); + fprintf(stderr,"\n"); + spaced=true; + } + + + } // while (read_ahead) // Now we have our blocks, tell the user about that! + if (!spaced) fprintf(stderr,"\n"); + spaced=false; char buffer[200]; sprintf(buffer,"Bytes read:%i Blocks read:%i\nErrors:%i Warnings:%i\n", - read_pointer,blocks_read,errors,warnings); + read_pointer,blocks_read,errors,warnings); if(cfg_output_info){ write (out_fd,buffer,strlen(buffer)); -// } else { -// fprintf(stderr,buffer); + } else { + fprintf(stderr,buffer); } - + if (errors>0){ - fprintf(stderr,"Errors make me sick. Goodbye!\n"); + fprintf(stderr,"Failed. (%i)\n",errcode); exit(errcode); } + + if (cfg_verbose){ + fprintf(stderr,"Data read successfully.\n"); + } + close(in_fd); + +// switch(block->get_state()){ // switchy +// case tape_block::TBS_EOF_LEGAL: +// delete block; +// if (!in_object){ +// fprintf(stderr,"File successfully read.\n"); +// } else { +// if (cfg_ignore_object_integrity_errors){ +// fprintf(stderr,"Warning: Object integrity check failed!\n"); +// warnings++; +// } else { +// fprintf(stderr,"Error: Object integrity check failed!\n"); +// errors++; +// errcode=6; +// } +// } +// read_ahead=false; +// break; +// case tape_block::TBS_EOF_ILLEGAL: +// break; +// case tape_block::TBS_CHECKSUM: +// break; +// case tape_block::TBS_DEFAULT: +// delete block; +// fprintf(stderr,"TBS_DEFAULT encountered ->> SEVERE INTERNAL ERROR!\n"); +// errors++; +// errcode=100; +// read_ahead=false; +// break; +// case tape_block::TBS_IOERR: +// case tape_block::TBS_OK: +// tape.insert(tape.end(),block); +// blocks_read++; + +// if (cfg_verbose){ +// char buffer[200]; +// snprintf(buffer,200,"Block No. %3i: Start(hex):%5x End(hex):%5x Size(hex):%3x\n", +// blocks_read-1, +// block_start, +// block_end, +// block->get_raw_size()); +// write (out_fd,buffer,strlen(buffer)); +// } +// if(cfg_list_contents) +// dump_vector(block->get_description()); + +// if (block->get_type()==0x10){ // Unknown data block! +// if (cfg_ignore_unknown_block_errors){ +// fprintf(stderr,"Warning: Unknown block type!\n"); +// warnings++; +// }else { +// fprintf(stderr,"Error: Unknown block type!\n"); +// errors++; +// errcode=5; +// read_ahead=false; +// } +// } + +// current_object.insert(current_object.end(),block); + +// if ((block->get_type()==data_block::TBT_EOT) +// ||((block->get_type()==0)&&(block->get_subtype()==3)) +// ||((block->get_type()==4)||(block->get_type()==3)) +// ||((block->get_type()==0)&&(block->get_subtype()==014))){ +// in_object=false; +// objects.insert(objects.end(),current_object); +// current_object.clear(); +// } else { +// in_object=true; +// } + +// break; +// } // switch +// } // generate loop + + +// } + return 0; } // main() diff --git a/pc-tools/ldc2/src/tape_block.cpp b/pc-tools/ldc2/src/tape_block.cpp index ecc0237..3de7bfb 100644 --- a/pc-tools/ldc2/src/tape_block.cpp +++ b/pc-tools/ldc2/src/tape_block.cpp @@ -1,10 +1,21 @@ + + +#include +#include + #include #include #include #include #include "tape_block.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" @@ -28,11 +39,6 @@ #include "data_block_6.hh" #include "data_block_7.hh" -#include "eot_block.hh" -#include "discard_block.hh" -#include "silent_code.hh" -#include "hw_constants.hh" - using namespace std; /***************************************************************/ @@ -58,7 +64,6 @@ tape_block::tape_block(tape_block & org){ */ void tape_block::operator=(tape_block &org){ block_type=org.block_type; - init_state=org.init_state; discarded_bytes=org.discarded_bytes; raw_data=0; raw_size=org.raw_size; @@ -74,17 +79,6 @@ void tape_block::operator=(tape_block &org){ } } -/***************************************************************/ -/*! - *\brief Query the block's state. - * - * If the state is not TBS_OK, the block must be discarded. - *\return The block's initialisation state - */ -tape_block::tb_state_t tape_block::get_state(){ - return init_state; -} - /***************************************************************/ /*! *\brief Determine the block's type @@ -92,7 +86,6 @@ tape_block::tb_state_t tape_block::get_state(){ * 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, @@ -161,13 +154,11 @@ unsigned char * tape_block::get_raw_data(){ /***************************************************************/ /*! - *\brief Static factory method with file descriptor support. + *\brief Static factory method for tape blocks. * * This method creates a tape_block by using the private * constructor with file descriptor support.\n - * It always returns a valid pointer, errors - * or exceptional conditions during creation are marked in the - * object's state.\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 @@ -179,7 +170,6 @@ unsigned char * tape_block::get_raw_data(){ * methods not already present in tape_block.\n * Objects of the following subclasses are automagically generated:\n * - eot_block - * - discard_block * - data_block * - data_block_0 * - data_block_0_0 @@ -203,8 +193,8 @@ unsigned char * tape_block::get_raw_data(){ * - data_block_6 * - data_block_7 * - *\return Pointer to an object of type tape_block or a subclass. - *\param fd A file descriptor where the read is taken from. + *\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 @@ -224,12 +214,20 @@ tape_block * tape_block::gen_from_fd(int fd, data_block_0 * n_data_block_0=0; data_block_0 * n_data_block_0_x=0; eot_block * n_eot_block=0; - discard_block * n_discard_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 to data_block, eot_block, discard_block - if possible + 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); @@ -238,10 +236,6 @@ tape_block * tape_block::gen_from_fd(int fd, n_eot_block=new eot_block(*n_tape_block); delete n_tape_block; return n_eot_block; - case tape_block::TBT_DISCARD: - n_discard_block = new discard_block(*n_tape_block); - delete n_tape_block; - return n_discard_block; default: // Unknown block, a bad thing! return n_tape_block; } @@ -314,8 +308,8 @@ string tape_block::get_typestring(){ /*! *\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 @@ -343,8 +337,7 @@ tape_block::tape_block (int fd, int eob_needed=EOB_LENGTH; int block_complete=0; - block_type= TBT_DISCARD; - init_state= TBS_DEFAULT; + block_type= TBT_BROKEN; discarded_bytes=0; raw_data = 0; raw_size = 0; @@ -361,13 +354,13 @@ tape_block::tape_block (int fd, 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; } discarded_bytes++; @@ -384,14 +377,13 @@ tape_block::tape_block (int fd, 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; @@ -407,14 +399,13 @@ tape_block::tape_block (int fd, 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(); } /* sort in the new byte */ @@ -435,30 +426,32 @@ tape_block::tape_block (int fd, break; case 1: if (buffer==BLOCK_END_DELIMITER_2){ - raw_size-=EOB_LENGTH; // Don't want the delimiter(s) in the data! 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); } - raw_size+=EOB_LENGTH; // Now we want it... - block_type=TBT_DATA; - init_state=TBS_OK; } @@ -469,9 +462,9 @@ tape_block::tape_block (int fd, *\retval 1 on error. */ int tape_block::init_words(){ - word_size=raw_size/3; + word_size=(raw_size-EOB_LENGTH)/3; if (word_sizem_broken_block=broken_block; +} + +/***************************************************************/ +/*! + *\brief Constructor + */ +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 Get amount of consumed data before EOF came along. + *\return Amound of bytes read from fd while looking for + * new block or EOF. + */ +int tape_block::eof_legal_exception::get_consumed(){ + return bytes_consumed; +} + +/***************************************************************/ +/*! + *\brief Constructor + */ +tape_block::io_error_exception:: +io_error_exception(){ + this->m_broken_block=NULL; +} + + + + + + diff --git a/pc-tools/ldc2/src/tape_block.hh b/pc-tools/ldc2/src/tape_block.hh index c1084e9..3022a6a 100644 --- a/pc-tools/ldc2/src/tape_block.hh +++ b/pc-tools/ldc2/src/tape_block.hh @@ -1,8 +1,5 @@ -#ifndef TAPE_BLOCK_H -#define TAPE_BLOCK_H - -#include -#include +#ifndef TAPE_BLOCK_HH +#define TAPE_BLOCK_HH using namespace std; @@ -13,37 +10,79 @@ using namespace std; * That may be some kind of data block or a end of tape mark. */ class tape_block{ - + public: // types - - /*! - *\brief Initialisation state. - */ - typedef enum { - TBS_OK, //!< Block successfully initialised - TBS_EOF_LEGAL, //!< Legal EOF while initialising - TBS_EOF_ILLEGAL, //!< Illegal EOF while initialising - TBS_CHECKSUM, //!< Checksum error - TBS_IOERR, //!< I/O-Error while reading - TBS_DEFAULT //!< Block not initialised. - } tb_state_t; - + /*! * Tape block types. */ typedef enum { TBT_DATA=0x10, //!< Data block TBT_EOT, //!< End of tape block - TBT_DISCARD //!< Invalid block, check block_type + TBT_BROKEN, //!< A broken block } tb_type_t; - + + /*! + *\brief Local base class for exceptions. + */ + class exception { + protected: + exception(); + public: + virtual ~exception(); + tape_block * get_block(); + protected: + tape_block * m_broken_block; + }; + + /*! + *\brief Checksum error exception. + */ + class checksum_error_exception + : public exception { + public: + checksum_error_exception(tape_block * block); + }; + + /*! + *\brief EOF while reading block exception. + */ + class eof_illegal_exception + : public exception { + public: + eof_illegal_exception(tape_block * block); + }; + + /*! + *\brief EOF after reading block exception. + */ + class eof_legal_exception + : public exception { + public: + eof_legal_exception(int bytes_consumed); + int get_consumed(); + private: + int bytes_consumed; + tape_block * get_block(); + }; + + /*! + *\brief IO error exception. + */ + class io_error_exception + : exception { + public: + io_error_exception(); + private: + tape_block * get_block(); + }; + public: // methods virtual ~tape_block(); tape_block(tape_block &); void operator=(tape_block &); - - tb_state_t get_state(); + virtual int get_type(); virtual int get_subtype(); virtual vector get_description(); @@ -56,11 +95,11 @@ public: // methods void (*input_stop)(void *)=0, void * start_stop_arg=0 ); - + protected: // methods tape_block(); string get_typestring(); - + private: // methods tape_block (int fd_p, void(*input_start)(void *)=0, @@ -70,10 +109,9 @@ private: // methods int init_words(void); int test_checksum(); - + protected: // members int block_type; //!< Type of this block. - tb_state_t init_state; //!< Initialisation state. int discarded_bytes; //!< Amount of bytes discarded before beginning. unsigned char * raw_data; //!< Raw block data in bytes. int raw_size; //!< Size of the raw data. @@ -82,5 +120,4 @@ protected: // members int poolsize; //!< Amount of data malloc'ed }; // class tape_block - #endif