| 1 | /* ldc2 preliminary main program */ |
| 2 | |
| 3 | |
| 4 | #include <vector> |
| 5 | #include <string> |
| 6 | |
| 7 | #include "config.hh" |
| 8 | #include "tool.hh" |
| 9 | |
| 10 | #include "tape_block.hh" |
| 11 | #include "data_block_0.hh" |
| 12 | |
| 13 | using namespace std; |
| 14 | |
| 15 | #ifndef BUILD_STAMP |
| 16 | #define BUILD_STAMP "" |
| 17 | #endif |
| 18 | |
| 19 | #ifndef VERSION |
| 20 | #define VERSION "0.0" |
| 21 | #endif |
| 22 | |
| 23 | #define ID_STRING "\n ldc2 - The X16 object analyser\n\ |
| 24 | (C) 2007 Philipp Hachtmann\n\n\ |
| 25 | Version %s, built %s\n %s\n\n" |
| 26 | |
| 27 | |
| 28 | |
| 29 | /*! |
| 30 | *\brief The main routine. |
| 31 | */ |
| 32 | int main(int argc, char ** args){ |
| 33 | |
| 34 | |
| 35 | // Do all the configuration stuff |
| 36 | do_config(argc,args); |
| 37 | |
| 38 | if (cfg_version){ |
| 39 | fprintf(stderr, ID_STRING, VERSION, BUILD_DATE, BUILD_STAMP); |
| 40 | exit(0); |
| 41 | } |
| 42 | |
| 43 | vector<tape_block*> tape; |
| 44 | vector<vector<tape_block *> > objects; |
| 45 | vector<tape_block *> current_object; |
| 46 | |
| 47 | tape_block * block=0; |
| 48 | bool read_ahead=true; |
| 49 | |
| 50 | int read_pointer=0; |
| 51 | int block_start=0; |
| 52 | int block_end=0; |
| 53 | int blocks_read=0; |
| 54 | |
| 55 | bool in_object=false; |
| 56 | |
| 57 | int errors=0; |
| 58 | int warnings=0; |
| 59 | int errcode=0; |
| 60 | |
| 61 | bool spaced=false; |
| 62 | |
| 63 | while(read_ahead){ |
| 64 | try{ |
| 65 | block=tape_block::gen_from_fd(in_fd); |
| 66 | blocks_read++; |
| 67 | tape.insert(tape.end(),block); |
| 68 | block_start=read_pointer+block->get_discarded_bytes(); |
| 69 | block_end=block_start+block->get_raw_size()-1; |
| 70 | read_pointer=block_end+1; |
| 71 | |
| 72 | if (cfg_verbose) { |
| 73 | char buffer[200]; |
| 74 | if (!spaced) fprintf(stderr,"\n"); |
| 75 | spaced=false; |
| 76 | |
| 77 | snprintf(buffer,200,"Block No.%03i: Start:0x%04x End:0x%04x Size:0x%02x\n", |
| 78 | blocks_read-1, |
| 79 | block_start, |
| 80 | block_end, |
| 81 | block->get_raw_size()); |
| 82 | fprintf(stderr,buffer); |
| 83 | } |
| 84 | if(cfg_list_contents) |
| 85 | dump_vector(block->get_description()); |
| 86 | } |
| 87 | |
| 88 | catch(tape_block::eof_legal_exception &e){ |
| 89 | read_pointer+=e.get_consumed(); |
| 90 | break; // Immediately leave read loop. |
| 91 | } |
| 92 | |
| 93 | catch(tape_block::io_error_exception){ |
| 94 | if (in_fd){ |
| 95 | fprintf(stderr,"Error: Could not read from \"%s\"!\n",cfg_infile.c_str()); |
| 96 | } else { |
| 97 | fprintf(stderr,"Error: Could not read from stdin!\n"); |
| 98 | } |
| 99 | errors++; |
| 100 | errcode=2; |
| 101 | break; // Immediately leave read loop. |
| 102 | } |
| 103 | |
| 104 | catch(tape_block::eof_illegal_exception &e){ |
| 105 | block=e.get_block(); |
| 106 | blocks_read++; |
| 107 | tape.insert(tape.end(),block); |
| 108 | block_start=read_pointer+block->get_discarded_bytes(); |
| 109 | block_end=block_start+block->get_raw_size()-1; |
| 110 | read_pointer=block_end+1; |
| 111 | |
| 112 | if (!spaced) fprintf(stderr,"\n"); |
| 113 | spaced=false; |
| 114 | char buffer[200]; |
| 115 | snprintf(buffer,200,"Block No.%03i: Start:0x%04x End:0x%04x Size:0x%02x\n", |
| 116 | blocks_read-1, |
| 117 | block_start, |
| 118 | block_end, |
| 119 | block->get_raw_size()); |
| 120 | fprintf(stderr,buffer); |
| 121 | |
| 122 | if (cfg_ignore_block_errors){ |
| 123 | fprintf(stderr,"Warning: Block integrity check failed!\n"); |
| 124 | warnings++; |
| 125 | } else { |
| 126 | fprintf(stderr,"Error: Block integrity check failed!\n"); |
| 127 | errors++; |
| 128 | errcode=3; |
| 129 | read_ahead=false; |
| 130 | } |
| 131 | if(cfg_list_contents) |
| 132 | dump_vector(block->get_description()); |
| 133 | fprintf(stderr,"\n"); |
| 134 | spaced=true; |
| 135 | } |
| 136 | |
| 137 | catch(tape_block::checksum_error_exception &e){ |
| 138 | block=e.get_block(); |
| 139 | blocks_read++; |
| 140 | tape.insert(tape.end(),block); |
| 141 | block_start=read_pointer+block->get_discarded_bytes(); |
| 142 | block_end=block_start+block->get_raw_size()-1; |
| 143 | read_pointer=block_end+1; |
| 144 | |
| 145 | |
| 146 | if (!spaced) fprintf(stderr,"\n"); |
| 147 | spaced=false; |
| 148 | char buffer[200]; |
| 149 | snprintf(buffer,200,"Block No.%03i: Start:0x%04x End:0x%04x Size:0x%02x\n", |
| 150 | blocks_read-1, |
| 151 | block_start, |
| 152 | block_end, |
| 153 | block->get_raw_size()); |
| 154 | fprintf(stderr,buffer); |
| 155 | |
| 156 | if (cfg_ignore_checksum_errors){ |
| 157 | fprintf(stderr,"Warning: Block checksum wrong!\n"); |
| 158 | warnings++; |
| 159 | } else { |
| 160 | fprintf(stderr,"Error: Block checksum wrong!\n"); |
| 161 | errors++; |
| 162 | read_ahead=false; |
| 163 | errcode=4; |
| 164 | } |
| 165 | if(cfg_list_contents) |
| 166 | dump_vector(block->get_description()); |
| 167 | fprintf(stderr,"\n"); |
| 168 | spaced=true; |
| 169 | } |
| 170 | |
| 171 | |
| 172 | } // while (read_ahead) |
| 173 | |
| 174 | // Now we have our blocks, tell the user about that! |
| 175 | if (!spaced) fprintf(stderr,"\n"); |
| 176 | spaced=false; |
| 177 | char buffer[200]; |
| 178 | sprintf(buffer,"Bytes read:%i Blocks read:%i\nErrors:%i Warnings:%i\n", |
| 179 | read_pointer,blocks_read,errors,warnings); |
| 180 | if(cfg_output_info){ |
| 181 | write (out_fd,buffer,strlen(buffer)); |
| 182 | } else { |
| 183 | fprintf(stderr,buffer); |
| 184 | } |
| 185 | |
| 186 | if (errors>0){ |
| 187 | fprintf(stderr,"Failed. (%i)\n",errcode); |
| 188 | exit(errcode); |
| 189 | } |
| 190 | |
| 191 | if (cfg_verbose){ |
| 192 | fprintf(stderr,"Data read successfully.\n"); |
| 193 | } |
| 194 | close(in_fd); |
| 195 | |
| 196 | // switch(block->get_state()){ // switchy |
| 197 | // case tape_block::TBS_EOF_LEGAL: |
| 198 | // delete block; |
| 199 | // if (!in_object){ |
| 200 | // fprintf(stderr,"File successfully read.\n"); |
| 201 | // } else { |
| 202 | // if (cfg_ignore_object_integrity_errors){ |
| 203 | // fprintf(stderr,"Warning: Object integrity check failed!\n"); |
| 204 | // warnings++; |
| 205 | // } else { |
| 206 | // fprintf(stderr,"Error: Object integrity check failed!\n"); |
| 207 | // errors++; |
| 208 | // errcode=6; |
| 209 | // } |
| 210 | // } |
| 211 | // read_ahead=false; |
| 212 | // break; |
| 213 | // case tape_block::TBS_EOF_ILLEGAL: |
| 214 | // break; |
| 215 | // case tape_block::TBS_CHECKSUM: |
| 216 | // break; |
| 217 | // case tape_block::TBS_DEFAULT: |
| 218 | // delete block; |
| 219 | // fprintf(stderr,"TBS_DEFAULT encountered ->> SEVERE INTERNAL ERROR!\n"); |
| 220 | // errors++; |
| 221 | // errcode=100; |
| 222 | // read_ahead=false; |
| 223 | // break; |
| 224 | // case tape_block::TBS_IOERR: |
| 225 | // case tape_block::TBS_OK: |
| 226 | // tape.insert(tape.end(),block); |
| 227 | // blocks_read++; |
| 228 | |
| 229 | // if (cfg_verbose){ |
| 230 | // char buffer[200]; |
| 231 | // snprintf(buffer,200,"Block No. %3i: Start(hex):%5x End(hex):%5x Size(hex):%3x\n", |
| 232 | // blocks_read-1, |
| 233 | // block_start, |
| 234 | // block_end, |
| 235 | // block->get_raw_size()); |
| 236 | // write (out_fd,buffer,strlen(buffer)); |
| 237 | // } |
| 238 | |
| 239 | // if(cfg_list_contents) |
| 240 | // dump_vector(block->get_description()); |
| 241 | |
| 242 | // if (block->get_type()==0x10){ // Unknown data block! |
| 243 | // if (cfg_ignore_unknown_block_errors){ |
| 244 | // fprintf(stderr,"Warning: Unknown block type!\n"); |
| 245 | // warnings++; |
| 246 | // }else { |
| 247 | // fprintf(stderr,"Error: Unknown block type!\n"); |
| 248 | // errors++; |
| 249 | // errcode=5; |
| 250 | // read_ahead=false; |
| 251 | // } |
| 252 | // } |
| 253 | |
| 254 | // current_object.insert(current_object.end(),block); |
| 255 | |
| 256 | // if ((block->get_type()==data_block::TBT_EOT) |
| 257 | // ||((block->get_type()==0)&&(block->get_subtype()==3)) |
| 258 | // ||((block->get_type()==4)||(block->get_type()==3)) |
| 259 | // ||((block->get_type()==0)&&(block->get_subtype()==014))){ |
| 260 | // in_object=false; |
| 261 | // objects.insert(objects.end(),current_object); |
| 262 | // current_object.clear(); |
| 263 | // } else { |
| 264 | // in_object=true; |
| 265 | // } |
| 266 | |
| 267 | // break; |
| 268 | // } // switch |
| 269 | // } // generate loop |
| 270 | |
| 271 | |
| 272 | // } |
| 273 | |
| 274 | |
| 275 | return 0; |
| 276 | } // main() |
| 277 | |