b6fc25b49183b5b80ebe296ce2fabbd9e477e03c
[h316.git] / pc-tools / ldc2 / src / tape_block.cpp
1
2
3 #include <string>
4 #include <vector>
5
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <string.h>
9 #include <stdio.h>
10
11 #include "tape_block.hh"
12
13 #include "silent_code.hh"
14 #include "hw_constants.hh"
15
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"
41
42 using namespace std;
43
44 /***************************************************************/
45 /*!
46 *\brief Destructor.
47 */
48 tape_block::~tape_block(){
49 free(raw_data);
50 free(word_data);
51 }
52
53 /***************************************************************/
54 /*!
55 *\brief Copy constructor.
56 */
57 tape_block::tape_block(tape_block & org){
58 operator=(org);
59 }
60
61 /***************************************************************/
62 /*!
63 *\brief Assignment operator.
64 */
65 void tape_block::operator=(tape_block &org){
66 block_type=org.block_type;
67 discarded_bytes=org.discarded_bytes;
68 raw_data=0;
69 raw_size=org.raw_size;
70 word_data=0;
71 word_size=org.word_size;
72 if (raw_size){
73 raw_data=(unsigned char *)malloc(raw_size);
74 memcpy(raw_data,org.raw_data,raw_size);
75 }
76 if (word_size){
77 word_data=(unsigned short *)malloc(word_size*sizeof(unsigned short));
78 memcpy(word_data,org.word_data,word_size*sizeof(unsigned short));
79 }
80 }
81
82 /***************************************************************/
83 /*!
84 *\brief Determine the block's type
85 *
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
89 *
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.
95 */
96 int tape_block::get_type(){
97 return block_type;
98 }
99
100 /***************************************************************/
101 /*!
102 *\brief Determine the block's subtype
103 *
104 * Some data block types have a subtype.
105 *\note This is a virtual method and to be used in derived classes.
106 *\return Always 0
107 */
108 int tape_block::get_subtype(){
109 return 0;
110 }
111
112 /***************************************************************/
113 /*!
114 *\brief Get a cleartext description of the block.
115 *\return ldc style descriptive line for the block
116 */
117 vector<string> tape_block:: get_description(){
118 vector<string> result;
119 result.insert(result.end(),
120 "***** "+get_typestring()+"Untyped tape block");
121 return result;
122 }
123
124 /***************************************************************/
125 /*!
126 *\brief Get size of the internal raw data buffer
127 *\return The size of the internal raw data buffer
128 */
129 int tape_block::get_raw_size(){
130 return raw_size;
131 }
132
133 /***************************************************************/
134 /*!
135 *\brief Get amount of bytes discarded before beginning
136 * of block sensed.
137 *\return Length of unusable tape before block start delimiter.
138 */
139 int tape_block::get_discarded_bytes(){
140 return discarded_bytes;
141 }
142
143 /***************************************************************/
144 /*!
145 *\brief Access internal raw data buffer
146 *
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
150 */
151 unsigned char * tape_block::get_raw_data(){
152 return raw_data;
153 }
154
155 /***************************************************************/
156 /*!
157 *\brief Dump contents of the block to a file descriptor.
158 *
159 *\param fd A writable file descriptor.
160 *\return Always zero.
161 *\throw io_error_exception A write error occured.
162 */
163 int tape_block::dump_to_fd(int fd){
164 int to_write=raw_size;
165 unsigned char * dump_ptr=raw_data;
166 int written;
167 while (to_write) {
168 written=write(fd,dump_ptr,to_write);
169 if (written < 0) throw io_error_exception();
170 to_write-=written;
171 dump_ptr+=written;
172 }
173 return 0;
174 }
175
176 /***************************************************************/
177 /*!
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.
184 */
185 bool tape_block::is_endblock(){
186 return false;
187 }
188
189 /***************************************************************/
190 /*!
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.
196 */
197 vector<string> tape_block::get_exported_symbols(){
198 vector<string> result;
199 return result;
200 }
201
202 /***************************************************************/
203 /*!
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.
209 */
210 vector<string> tape_block::get_called_symbols(){
211 vector<string> result;
212 return result;
213 }
214
215 /***************************************************************/
216 /*!
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.
221 */
222 bool tape_block::has_known_type(){
223 return m_has_known_type;
224 }
225
226 /***************************************************************/
227 /*!
228 *\brief Static factory method for tape blocks.
229 *
230 * This method creates a tape_block by using the private
231 * constructor with file descriptor support.\n
232 *
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
243 * - eot_block
244 * - data_block
245 * - data_block_0
246 * - data_block_0_0
247 * - data_block_0_1
248 * - data_block_0_2
249 * - data_block_0_3
250 * - data_block_0_4
251 * - data_block_0_10
252 * - data_block_0_14
253 * - data_block_0_24
254 * - data_block_0_30
255 * - data_block_0_50
256 * - data_block_0_54
257 * - data_block_0_60
258 * - data_block_0_64
259 * - data_block_1
260 * - data_block_2
261 * - data_block_3
262 * - data_block_4
263 * - data_block_5
264 * - data_block_6
265 * - data_block_7
266 *
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.
273 *
274 *\param start_stop_arg A pointer passed to input_start
275 * and input_stop(). Can be anything.
276 */
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)
281 {
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;
289
290 //Use the private constructor which reads in the block from a file descriptor
291 try{
292 n_tape_block=new tape_block(fd,input_start,input_stop,start_stop_arg);
293 }
294
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);
299 }
300
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);
305 break;
306 case tape_block::TBT_EOT:
307 n_eot_block=new eot_block(*n_tape_block);
308 delete n_tape_block;
309 n_eot_block->m_has_known_type=true;
310 return n_eot_block;
311 default: // Unknown block, a bad thing!
312 return n_tape_block;
313 }
314 delete n_tape_block;
315
316 // Now only data blocks are left
317
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;
327 default:
328 return n_data_block;
329 }
330 if (n_data_block_0==0){ // So we must have found another one
331 delete n_data_block;
332 n_data_block_x->m_has_known_type=true;
333 return n_data_block_x;
334 }
335
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;
352 default:
353 return n_data_block_0;
354 }
355 delete n_data_block_0;
356 n_data_block_0_x->m_has_known_type=true;
357 return n_data_block_0_x;
358 }
359
360 /***************************************************************/
361 /*!
362 *\brief Default constructor.
363 */
364 tape_block::tape_block(){}
365
366 /***************************************************************/
367 /*!
368 *\brief get a short type description string.
369 *\return A small string to be used in description methods.
370 *
371 * The string is always 19 characters long.\n
372 * An example is "(99-99) ".
373 */
374 string tape_block::get_typestring(){
375 char buffer[13];
376 sprintf(buffer,"(%o-%o) ",get_type(),get_subtype());
377 buffer[10]=0;
378 return string(buffer);
379 }
380
381 /***************************************************************/
382 /*!
383 *\brief Automatic constructor with file descriptor support
384 *
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
392 * and checked.
393 * - The block's type field is initialised.
394 * - input_stop() is called.
395 *
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
399 * of input
400 *\param start_stop_arg A pointer passed to input_start and input_stop().
401 */
402 tape_block::tape_block (int fd,
403 void(*input_start)(void *),
404 void (*input_stop)(void *),
405 void * start_stop_arg
406 )
407 {
408 unsigned char buffer;
409 int retval;
410 int eot_needed=EOT_LENGTH;
411 int eob_needed=EOB_LENGTH;
412 int block_complete=0;
413
414 block_type= TBT_BROKEN;
415 discarded_bytes=0;
416 raw_data = 0;
417 raw_size = 0;
418 word_data = 0;
419 word_size = 0;
420 poolsize = 1024;
421
422 // Get initial portion of memory
423 raw_data=(unsigned char*)malloc(poolsize);
424
425 if (input_start) input_start (start_stop_arg);
426
427 /* Look for block start in input stream */
428 do {
429 retval=read(fd, &buffer, 1);
430 if (retval == 0){ // End of file
431 free(raw_data);
432 throw eof_legal_exception(discarded_bytes);
433 return;
434 }
435 if (retval == -1){ // Error reading from fd
436 free(raw_data);
437 throw io_error_exception();
438 return;
439 }
440 discarded_bytes++;
441
442 /* Look for end ot tape sequence */
443 switch(eot_needed){
444 case 3:
445 if (buffer==EOT_SEQ_1) eot_needed--;
446 else eot_needed=EOT_LENGTH;
447 break;
448 case 2:
449 if (buffer==EOT_SEQ_2) eot_needed--;
450 else eot_needed=EOT_LENGTH;
451 break;
452 case 1:
453 if (buffer==EOT_SEQ_3){
454 raw_data=(unsigned char*)realloc(raw_data,3);
455 raw_size=3;
456 discarded_bytes-=3;
457 raw_data[0]=EOT_SEQ_1;
458 raw_data[1]=EOT_SEQ_2;
459 raw_data[2]=EOT_SEQ_3;
460 block_type =TBT_EOT;
461 return;
462 }
463 else eot_needed=EOT_LENGTH;
464 break;
465 }
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!
469
470 /* Now input the block data */
471 block_complete = 0;
472
473 raw_data[0]=buffer; // Put the start delimiter in!
474
475 do {
476 retval = read (fd, &buffer, 1);
477
478 if (retval == 0){ // End of file
479 broken_block * b_block=new broken_block(*this);
480 free(raw_data);
481 throw eof_illegal_exception(b_block);
482 }
483 if (retval == -1){ // Error reading from fd
484 free(raw_data);
485 throw io_error_exception();
486 }
487
488 /* sort in the new byte */
489 raw_size++;
490 // Need more memory??
491 if (raw_size>poolsize){
492 poolsize+=1024;
493 raw_data=(unsigned char*)realloc(raw_data,poolsize);
494 }
495
496 raw_data[raw_size-1]=buffer;
497
498 /* Look for end ot block sequence */
499 switch(eob_needed){
500 case 2:
501 if (buffer==BLOCK_END_DELIMITER_1) eob_needed--;
502 else eob_needed=EOB_LENGTH;
503 break;
504 case 1:
505 if (buffer==BLOCK_END_DELIMITER_2){
506 block_complete=1;
507 }
508 else eob_needed=EOB_LENGTH;
509 break;
510 }
511 }while (!block_complete);
512
513 if (input_stop) input_stop (start_stop_arg);
514
515
516 /* Now we have the data in. */
517
518 if (init_words()!=0){
519 broken_block * b_block=new broken_block(*this);
520 free(raw_data);
521 free(word_data);
522 throw checksum_error_exception(b_block);
523 }
524
525 if (test_checksum()!=0){
526 broken_block * b_block=new broken_block(*this);
527 free(raw_data);
528 free(word_data);
529 throw checksum_error_exception(b_block);
530 }
531 block_type=TBT_DATA;
532 }
533
534
535 /***************************************************************/
536 /*!
537 *\brief Initialize word representation of data.
538 *\retval 0 on success.
539 *\retval 1 on error.
540 */
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);
547 return 0;
548 }
549
550 /***************************************************************/
551 /*!
552 *\brief Calculate and check checksum.
553 *\retval 0 on success.
554 *\retval 1 on error.
555 */
556 int tape_block::test_checksum(){
557 unsigned short sum=0;
558 for (int pos=0; pos<word_size-1;pos++)
559 sum^=word_data[pos];
560 if (sum==word_data[word_size-1]) return 0;
561 else return 1;
562 }
563
564 /***************************************************************/
565 /*!
566 *\brief Standard constructor of exception base class.
567 */
568 tape_block::exception::exception(){
569 }
570
571 /***************************************************************/
572 /*!
573 *\brief Standard desctructor.
574 */
575 tape_block::exception::~exception(){
576 }
577
578 /***************************************************************/
579 /*!
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.
584 */
585 tape_block * tape_block::exception::get_block(){
586 return m_broken_block;
587 }
588
589 /***************************************************************/
590 /*!
591 *\brief Constructor
592 */
593 tape_block::checksum_error_exception::
594 checksum_error_exception (tape_block * broken_block){
595 this->m_broken_block=broken_block;
596 }
597
598 /***************************************************************/
599 /*!
600 *\brief Constructor
601 */
602 tape_block::eof_illegal_exception::
603 eof_illegal_exception (tape_block * broken_block){
604 this->m_broken_block=broken_block;
605 }
606
607 /***************************************************************/
608 /*!
609 *\brief Constructor
610 *\param bytes_consumed Amount of bytes read while looking
611 * for a block.
612 */
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;
617 }
618
619 /***************************************************************/
620 /*!
621 *\brief Get amount of consumed data before EOF came along.
622 *\return Amound of bytes read from fd while looking for
623 * new block or EOF.
624 */
625 int tape_block::eof_legal_exception::get_consumed(){
626 return bytes_consumed;
627 }
628
629 /***************************************************************/
630 /*!
631 *\brief Constructor
632 */
633 tape_block::io_error_exception::
634 io_error_exception(){
635 this->m_broken_block=NULL;
636 }
637
638
639
640
641
642