| 1 | #include <stdlib.h> |
| 2 | #include <unistd.h> |
| 3 | #include <string.h> |
| 4 | #include <stdio.h> |
| 5 | |
| 6 | #include "tape_block.hh" |
| 7 | #include "data_block.hh" |
| 8 | #include "data_block_0.hh" |
| 9 | #include "data_block_0_0.hh" |
| 10 | #include "data_block_0_1.hh" |
| 11 | #include "data_block_0_2.hh" |
| 12 | #include "data_block_0_3.hh" |
| 13 | #include "data_block_0_4.hh" |
| 14 | #include "data_block_0_10.hh" |
| 15 | #include "data_block_0_14.hh" |
| 16 | #include "data_block_0_24.hh" |
| 17 | #include "data_block_0_44.hh" |
| 18 | #include "data_block_0_30.hh" |
| 19 | #include "data_block_0_54.hh" |
| 20 | #include "data_block_0_60.hh" |
| 21 | #include "data_block_0_50.hh" |
| 22 | #include "data_block_0_64.hh" |
| 23 | #include "data_block_1.hh" |
| 24 | #include "data_block_2.hh" |
| 25 | #include "data_block_3.hh" |
| 26 | #include "data_block_4.hh" |
| 27 | #include "data_block_5.hh" |
| 28 | #include "data_block_6.hh" |
| 29 | #include "data_block_7.hh" |
| 30 | |
| 31 | #include "eot_block.hh" |
| 32 | #include "discard_block.hh" |
| 33 | #include "silent_code.hh" |
| 34 | #include "hw_constants.hh" |
| 35 | |
| 36 | using namespace std; |
| 37 | |
| 38 | /***************************************************************/ |
| 39 | /*! |
| 40 | *\brief Destructor. |
| 41 | */ |
| 42 | tape_block::~tape_block(){ |
| 43 | free(raw_data); |
| 44 | free(word_data); |
| 45 | } |
| 46 | |
| 47 | /***************************************************************/ |
| 48 | /*! |
| 49 | *\brief Copy constructor. |
| 50 | */ |
| 51 | tape_block::tape_block(tape_block & org){ |
| 52 | operator=(org); |
| 53 | } |
| 54 | |
| 55 | /***************************************************************/ |
| 56 | /*! |
| 57 | *\brief Assignment operator. |
| 58 | */ |
| 59 | void tape_block::operator=(tape_block &org){ |
| 60 | block_type=org.block_type; |
| 61 | init_state=org.init_state; |
| 62 | discarded_bytes=org.discarded_bytes; |
| 63 | raw_data=0; |
| 64 | raw_size=org.raw_size; |
| 65 | word_data=0; |
| 66 | word_size=org.word_size; |
| 67 | if (raw_size){ |
| 68 | raw_data=(unsigned char *)malloc(raw_size); |
| 69 | memcpy(raw_data,org.raw_data,raw_size); |
| 70 | } |
| 71 | if (word_size){ |
| 72 | word_data=(unsigned short *)malloc(word_size*sizeof(unsigned short)); |
| 73 | memcpy(word_data,org.word_data,word_size*sizeof(unsigned short)); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /***************************************************************/ |
| 78 | /*! |
| 79 | *\brief Query the block's state. |
| 80 | * |
| 81 | * If the state is not TBS_OK, the block must be discarded. |
| 82 | *\return The block's initialisation state |
| 83 | */ |
| 84 | tape_block::tb_state_t tape_block::get_state(){ |
| 85 | return init_state; |
| 86 | } |
| 87 | |
| 88 | /***************************************************************/ |
| 89 | /*! |
| 90 | *\brief Determine the block's type |
| 91 | * |
| 92 | * This routine returns the block's type.\\ |
| 93 | *\retval TBT_DATA if it's a data block |
| 94 | *\retval TBT_EOT if it's an end of tape mark |
| 95 | *\retval TBR_DISCARD if the block is unusable |
| 96 | * |
| 97 | *\note This function is virtual. That means that if the tape block in |
| 98 | * question is of the subtype data_block which overwrites this method, |
| 99 | * it will return the block type contained in the upper 4 bits of the |
| 100 | * first data word! Try to avoid testing for TBT_DATA. \n |
| 101 | * Blocks generated by gen_from_fd should never have TBT_DATA. |
| 102 | */ |
| 103 | int tape_block::get_type(){ |
| 104 | return block_type; |
| 105 | } |
| 106 | |
| 107 | /***************************************************************/ |
| 108 | /*! |
| 109 | *\brief Determine the block's subtype |
| 110 | * |
| 111 | * Some data block types have a subtype. |
| 112 | *\note This is a virtual method and to be used in derived classes. |
| 113 | *\return Always 0 |
| 114 | */ |
| 115 | int tape_block::get_subtype(){ |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | /***************************************************************/ |
| 120 | /*! |
| 121 | *\brief Get a cleartext description of the block. |
| 122 | *\return ldc style descriptive line for the block |
| 123 | */ |
| 124 | vector<string> tape_block:: get_description(){ |
| 125 | vector<string> result; |
| 126 | result.insert(result.end(), |
| 127 | "***** "+get_typestring()+"Untyped tape block"); |
| 128 | return result; |
| 129 | } |
| 130 | |
| 131 | /***************************************************************/ |
| 132 | /*! |
| 133 | *\brief Get size of the internal raw data buffer |
| 134 | *\return The size of the internal raw data buffer |
| 135 | */ |
| 136 | int tape_block::get_raw_size(){ |
| 137 | return raw_size; |
| 138 | } |
| 139 | |
| 140 | /***************************************************************/ |
| 141 | /*! |
| 142 | *\brief Get amount of bytes discarded before beginning |
| 143 | * of block sensed. |
| 144 | *\return Length of unusable tape before block start delimiter. |
| 145 | */ |
| 146 | int tape_block::get_discarded_bytes(){ |
| 147 | return discarded_bytes; |
| 148 | } |
| 149 | |
| 150 | /***************************************************************/ |
| 151 | /*! |
| 152 | *\brief Access internal raw data buffer |
| 153 | * |
| 154 | * The raw data buffer contains all block data except the \ \ |
| 155 | * block start delimiter and block end delimiter. |
| 156 | *\return Pointer to internal raw data buffer of the block |
| 157 | */ |
| 158 | unsigned char * tape_block::get_raw_data(){ |
| 159 | return raw_data; |
| 160 | } |
| 161 | |
| 162 | /***************************************************************/ |
| 163 | /*! |
| 164 | *\brief Static factory method with file descriptor support. |
| 165 | * |
| 166 | * This method creates a tape_block by using the private |
| 167 | * constructor with file descriptor support.\n |
| 168 | * It always returns a valid pointer, errors |
| 169 | * or exceptional conditions during creation are marked in the |
| 170 | * object's state.\n |
| 171 | * The factory method tries to determine the block's type and then |
| 172 | * looks for appropriate subclasses. If there's an appropriate |
| 173 | * subclass fitting the newly-created object, an |
| 174 | * object of the subclass will be created and a pointer to this |
| 175 | * object will be returned to the caller.\n |
| 176 | * This allows for easy use of the object using it's virtual |
| 177 | * interface methods or a cast of the pointer to a sub type pointer |
| 178 | * according to the type information, and then using the subtype's |
| 179 | * methods not already present in tape_block.\n |
| 180 | * Objects of the following subclasses are automagically generated:\n |
| 181 | * - eot_block |
| 182 | * - discard_block |
| 183 | * - data_block |
| 184 | * - data_block_0 |
| 185 | * - data_block_0_0 |
| 186 | * - data_block_0_1 |
| 187 | * - data_block_0_2 |
| 188 | * - data_block_0_3 |
| 189 | * - data_block_0_4 |
| 190 | * - data_block_0_10 |
| 191 | * - data_block_0_14 |
| 192 | * - data_block_0_24 |
| 193 | * - data_block_0_30 |
| 194 | * - data_block_0_50 |
| 195 | * - data_block_0_54 |
| 196 | * - data_block_0_60 |
| 197 | * - data_block_0_64 |
| 198 | * - data_block_1 |
| 199 | * - data_block_2 |
| 200 | * - data_block_3 |
| 201 | * - data_block_4 |
| 202 | * - data_block_5 |
| 203 | * - data_block_6 |
| 204 | * - data_block_7 |
| 205 | * |
| 206 | *\return Pointer to an object of type tape_block or a subclass. |
| 207 | *\param fd A file descriptor where the read is taken from. |
| 208 | *\param input_start A pointer to a function |
| 209 | * called before reading data from fd. |
| 210 | *\param input_stop A pointer to a function |
| 211 | * called after reading data from fd. |
| 212 | * |
| 213 | *\param start_stop_arg A pointer passed to input_start |
| 214 | * and input_stop(). Can be anything. |
| 215 | */ |
| 216 | tape_block * tape_block::gen_from_fd(int fd, |
| 217 | void(*input_start)(void *), |
| 218 | void (*input_stop)(void *), |
| 219 | void * start_stop_arg) |
| 220 | { |
| 221 | tape_block * n_tape_block=0; |
| 222 | data_block * n_data_block=0; |
| 223 | data_block * n_data_block_x=0; |
| 224 | data_block_0 * n_data_block_0=0; |
| 225 | data_block_0 * n_data_block_0_x=0; |
| 226 | eot_block * n_eot_block=0; |
| 227 | discard_block * n_discard_block=0; |
| 228 | |
| 229 | //Use the private constructor which reads in the block from a file descriptor |
| 230 | n_tape_block=new tape_block(fd,input_start,input_stop,start_stop_arg); |
| 231 | |
| 232 | // Retype to data_block, eot_block, discard_block - if possible |
| 233 | switch(n_tape_block->get_type()){ |
| 234 | case tape_block::TBT_DATA: |
| 235 | n_data_block = new data_block(*n_tape_block); |
| 236 | break; |
| 237 | case tape_block::TBT_EOT: |
| 238 | n_eot_block=new eot_block(*n_tape_block); |
| 239 | delete n_tape_block; |
| 240 | return n_eot_block; |
| 241 | case tape_block::TBT_DISCARD: |
| 242 | n_discard_block = new discard_block(*n_tape_block); |
| 243 | delete n_tape_block; |
| 244 | return n_discard_block; |
| 245 | default: // Unknown block, a bad thing! |
| 246 | return n_tape_block; |
| 247 | } |
| 248 | delete n_tape_block; |
| 249 | |
| 250 | // Now only data blocks are left |
| 251 | |
| 252 | switch(n_data_block->get_type()){ |
| 253 | case 00: n_data_block_0=new data_block_0(*n_data_block); break; |
| 254 | case 01: n_data_block_x=new data_block_1(*n_data_block); break; |
| 255 | case 02: n_data_block_x=new data_block_2(*n_data_block); break; |
| 256 | case 03: n_data_block_x=new data_block_3(*n_data_block); break; |
| 257 | case 04: n_data_block_x=new data_block_4(*n_data_block); break; |
| 258 | case 05: n_data_block_x=new data_block_5(*n_data_block); break; |
| 259 | case 06: n_data_block_x=new data_block_6(*n_data_block); break; |
| 260 | case 07: n_data_block_x=new data_block_7(*n_data_block); break; |
| 261 | default: |
| 262 | return n_data_block; |
| 263 | } |
| 264 | if (n_data_block_0==0){ // So we must have found another one |
| 265 | delete n_data_block; |
| 266 | return n_data_block_x; |
| 267 | } |
| 268 | |
| 269 | // Here only type 0 left |
| 270 | switch(n_data_block_0->get_subtype()){ |
| 271 | case 000: n_data_block_0_x=new data_block_0_0(*n_data_block_0); break; |
| 272 | case 001: n_data_block_0_x=new data_block_0_1(*n_data_block_0); break; |
| 273 | case 002: n_data_block_0_x=new data_block_0_2(*n_data_block_0); break; |
| 274 | case 003: n_data_block_0_x=new data_block_0_3(*n_data_block_0); break; |
| 275 | case 004: n_data_block_0_x=new data_block_0_4(*n_data_block_0); break; |
| 276 | case 010: n_data_block_0_x=new data_block_0_10(*n_data_block_0); break; |
| 277 | case 014: n_data_block_0_x=new data_block_0_14(*n_data_block_0); break; |
| 278 | case 024: n_data_block_0_x=new data_block_0_24(*n_data_block_0); break; |
| 279 | case 044: n_data_block_0_x=new data_block_0_44(*n_data_block_0); break; |
| 280 | case 030: n_data_block_0_x=new data_block_0_30(*n_data_block_0); break; |
| 281 | case 054: n_data_block_0_x=new data_block_0_54(*n_data_block_0); break; |
| 282 | case 060: n_data_block_0_x=new data_block_0_60(*n_data_block_0); break; |
| 283 | case 064: n_data_block_0_x=new data_block_0_64(*n_data_block_0); break; |
| 284 | case 050: n_data_block_0_x=new data_block_0_50(*n_data_block_0); break; |
| 285 | default: |
| 286 | return n_data_block_0; |
| 287 | } |
| 288 | delete n_data_block_0; |
| 289 | return n_data_block_0_x; |
| 290 | } |
| 291 | |
| 292 | /***************************************************************/ |
| 293 | /*! |
| 294 | *\brief Default constructor. |
| 295 | */ |
| 296 | tape_block::tape_block(){} |
| 297 | |
| 298 | /***************************************************************/ |
| 299 | /*! |
| 300 | *\brief get a short type description string. |
| 301 | *\return A small string to be used in description methods. |
| 302 | * |
| 303 | * The string is always 19 characters long.\n |
| 304 | * An example is "(99-99) ". |
| 305 | */ |
| 306 | string tape_block::get_typestring(){ |
| 307 | char buffer[13]; |
| 308 | sprintf(buffer,"(%o-%o) ",get_type(),get_subtype()); |
| 309 | buffer[10]=0; |
| 310 | return string(buffer); |
| 311 | } |
| 312 | |
| 313 | /***************************************************************/ |
| 314 | /*! |
| 315 | *\brief Automatic constructor with file descriptor support |
| 316 | * |
| 317 | * This constructor is used to initialise the block read in the block's |
| 318 | * data via a file descriptor.\n |
| 319 | * This is done in the following way: |
| 320 | * - input_start() is called. |
| 321 | * - Data is read from fd. Stops on end of file or end of block |
| 322 | * or IO error. On EOF or IO error input_stop() is NOT called. |
| 323 | * - If there was no IO error the block's checksum is calculated |
| 324 | * and checked. |
| 325 | * - The block's type field is initialised. |
| 326 | * - input_stop() is called. |
| 327 | * |
| 328 | *\param fd A file descriptor where the read is taken from |
| 329 | *\param input_stop A pointer to a function called at the end of input |
| 330 | *\param input_start A pointer to a function called at the beginning |
| 331 | * of input |
| 332 | *\param start_stop_arg A pointer passed to input_start and input_stop(). |
| 333 | */ |
| 334 | tape_block::tape_block (int fd, |
| 335 | void(*input_start)(void *), |
| 336 | void (*input_stop)(void *), |
| 337 | void * start_stop_arg |
| 338 | ) |
| 339 | { |
| 340 | unsigned char buffer; |
| 341 | int retval; |
| 342 | int eot_needed=EOT_LENGTH; |
| 343 | int eob_needed=EOB_LENGTH; |
| 344 | int block_complete=0; |
| 345 | |
| 346 | block_type= TBT_DISCARD; |
| 347 | init_state= TBS_DEFAULT; |
| 348 | discarded_bytes=0; |
| 349 | raw_data = 0; |
| 350 | raw_size = 0; |
| 351 | word_data = 0; |
| 352 | word_size = 0; |
| 353 | poolsize = 1024; |
| 354 | |
| 355 | // Get initial portion of memory |
| 356 | raw_data=(unsigned char*)malloc(poolsize); |
| 357 | |
| 358 | if (input_start) input_start (start_stop_arg); |
| 359 | |
| 360 | /* Look for block start in input stream */ |
| 361 | do { |
| 362 | retval=read(fd, &buffer, 1); |
| 363 | if (retval == 0){ // End of file |
| 364 | block_type=TBT_DISCARD; |
| 365 | init_state=TBS_EOF_LEGAL; |
| 366 | return; |
| 367 | } |
| 368 | if (retval == -1){ // Error reading from fd |
| 369 | block_type=TBT_DISCARD; |
| 370 | init_state=TBS_IOERR; |
| 371 | return; |
| 372 | } |
| 373 | discarded_bytes++; |
| 374 | |
| 375 | /* Look for end ot tape sequence */ |
| 376 | switch(eot_needed){ |
| 377 | case 3: |
| 378 | if (buffer==EOT_SEQ_1) eot_needed--; |
| 379 | else eot_needed=EOT_LENGTH; |
| 380 | break; |
| 381 | case 2: |
| 382 | if (buffer==EOT_SEQ_2) eot_needed--; |
| 383 | else eot_needed=EOT_LENGTH; |
| 384 | break; |
| 385 | case 1: |
| 386 | if (buffer==EOT_SEQ_3){ |
| 387 | raw_data=(unsigned char*)malloc(3); |
| 388 | raw_size=3; |
| 389 | discarded_bytes-=3; |
| 390 | raw_data[0]=EOT_SEQ_1; |
| 391 | raw_data[1]=EOT_SEQ_2; |
| 392 | raw_data[2]=EOT_SEQ_3; |
| 393 | block_type =TBT_EOT; |
| 394 | init_state =TBS_OK; |
| 395 | return; |
| 396 | } |
| 397 | else eot_needed=EOT_LENGTH; |
| 398 | break; |
| 399 | } |
| 400 | } while (buffer != (unsigned char) BLOCK_START_DELIMITER); |
| 401 | discarded_bytes--; // Dont want to declare the start delimiter discarded! |
| 402 | raw_size=1; // But we want to account it here! |
| 403 | |
| 404 | /* Now input the block data */ |
| 405 | block_complete = 0; |
| 406 | do { |
| 407 | retval = read (fd, &buffer, 1); |
| 408 | |
| 409 | if (retval == 0){ // End of file |
| 410 | block_type=TBT_DISCARD; |
| 411 | init_state=TBS_EOF_ILLEGAL; |
| 412 | return; |
| 413 | } |
| 414 | if (retval == -1){ // Error reading from fd |
| 415 | block_type=TBT_DISCARD; |
| 416 | init_state=TBS_IOERR; |
| 417 | return; |
| 418 | } |
| 419 | |
| 420 | /* sort in the new byte */ |
| 421 | raw_size++; |
| 422 | // Need more memory?? |
| 423 | if (raw_size==poolsize){ |
| 424 | poolsize+=1024; |
| 425 | raw_data=(unsigned char*)realloc(raw_data,poolsize); |
| 426 | } |
| 427 | |
| 428 | raw_data[raw_size-2]=buffer; |
| 429 | |
| 430 | /* Look for end ot block sequence */ |
| 431 | switch(eob_needed){ |
| 432 | case 2: |
| 433 | if (buffer==BLOCK_END_DELIMITER_1) eob_needed--; |
| 434 | else eob_needed=EOB_LENGTH; |
| 435 | break; |
| 436 | case 1: |
| 437 | if (buffer==BLOCK_END_DELIMITER_2){ |
| 438 | raw_size-=EOB_LENGTH; // Don't want the delimiter(s) in the data! |
| 439 | block_complete=1; |
| 440 | } |
| 441 | else eob_needed=EOB_LENGTH; |
| 442 | break; |
| 443 | } |
| 444 | }while (!block_complete); |
| 445 | if (input_stop) input_stop (start_stop_arg); |
| 446 | |
| 447 | /* Now we have the data in. */ |
| 448 | |
| 449 | if (init_words()!=0){ |
| 450 | init_state=TBS_CHECKSUM; |
| 451 | return; |
| 452 | } |
| 453 | |
| 454 | if (test_checksum()!=0){ |
| 455 | init_state=TBS_CHECKSUM; |
| 456 | return; |
| 457 | } |
| 458 | raw_size+=EOB_LENGTH; // Now we want it... |
| 459 | |
| 460 | block_type=TBT_DATA; |
| 461 | init_state=TBS_OK; |
| 462 | } |
| 463 | |
| 464 | |
| 465 | /***************************************************************/ |
| 466 | /*! |
| 467 | *\brief Initialize word representation of data. |
| 468 | *\retval 0 on success. |
| 469 | *\retval 1 on error. |
| 470 | */ |
| 471 | int tape_block::init_words(){ |
| 472 | word_size=raw_size/3; |
| 473 | if (word_size<MINIMUM_DATA_BLOCK_WORDSIZE) return 1; // Block too short! |
| 474 | word_data=(unsigned short *)malloc(raw_size*sizeof(unsigned short)); |
| 475 | for (int pos=0; pos<word_size; pos++) |
| 476 | word_data[pos]=combine466(raw_data+3*pos); |
| 477 | return 0; |
| 478 | } |
| 479 | |
| 480 | /***************************************************************/ |
| 481 | /*! |
| 482 | *\brief Calculate and check checksum. |
| 483 | *\retval 0 on success. |
| 484 | *\retval 1 on error. |
| 485 | */ |
| 486 | int tape_block::test_checksum(){ |
| 487 | unsigned short sum=0; |
| 488 | for (int pos=0; pos<word_size-1;pos++) |
| 489 | sum^=word_data[pos]; |
| 490 | if (sum==word_data[word_size-1]) return 0; |
| 491 | else return 1; |
| 492 | } |
| 493 | |