Debian Codegraph @

stream.c

raw ◀ ▶
Source reconstructed at patch state 0 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 /*************************************************************
2 Copyright (C) 1990, 1991, 1993 Andy C. Hung, all rights reserved.
3 PUBLIC DOMAIN LICENSE: Stanford University Portable Video Research
4 Group. If you use this software, you agree to the following: This
5 program package is purely experimental, and is licensed "as is".
6 Permission is granted to use, modify, and distribute this program
7 without charge for any purpose, provided this license/ disclaimer
8 notice appears in the copies. No warranty or maintenance is given,
9 either expressed or implied. In no event shall the author(s) be
10 liable to you or a third party for any special, incidental,
11 consequential, or other damages, arising out of the use or inability
12 to use the program for any purpose (or the loss of data), even if we
13 have been advised of such possibilities. Any public reference or
14 advertisement of this source code should refer to it as the Portable
15 Video Research Group (PVRG) code, and not by any author(s) (or
16 Stanford University) name.
17 *************************************************************/
18
19 /*
20 ************************************************************
21 stream.c
22
23 This file is used for management of bit-aligned files.
24
25 ************************************************************
26 */
27
28 /*LABEL stream.c */
29
30 /* Include files */
31
32 #include "globals.h"
33 #include "marker.h"
34 #include "stream.h"
35
36 /*PUBLIC*/
37
38 extern void initstream();
39 extern void pushstream();
40 extern void popstream();
41 extern void bpushc();
42 extern int bgetc();
43 extern void bputc();
44 extern void mropen();
45 extern void mrclose();
46 extern void mwopen();
47 extern void swbytealign();
48 extern void mwclose();
49 extern long mwtell();
50 extern long mrtell();
51 extern void mwseek();
52 extern void mrseek();
53 extern int megetb();
54 extern void meputv();
55 extern int megetv();
56 extern int DoMarker();
57 extern int ScreenMarker();
58 extern void Resync();
59 extern void WriteResync();
60 extern int ReadResync();
61 extern int ScreenAllMarker();
62 extern int DoAllMarker();
63
64 static int pgetc();
65
66 /*PRIVATE*/
67
68 /* External values */
69
70 extern int ErrorValue;
71 extern IMAGE *CImage;
72 extern int Robust; /* Whether to ignore scan markers and such */
73
74 /* Masks */
75 int bit_set_mask[] = { /* This is 2^i at ith position */
76 0x00000001,0x00000002,0x00000004,0x00000008,
77 0x00000010,0x00000020,0x00000040,0x00000080,
78 0x00000100,0x00000200,0x00000400,0x00000800,
79 0x00001000,0x00002000,0x00004000,0x00008000,
80 0x00010000,0x00020000,0x00040000,0x00080000,
81 0x00100000,0x00200000,0x00400000,0x00800000,
82 0x01000000,0x02000000,0x04000000,0x08000000,
83 0x10000000,0x20000000,0x40000000,0x80000000};
84 int lmask[] = { /* This is 2^{i+1}-1 */
85 0x00000001,0x00000003,0x00000007,0x0000000f,
86 0x0000001f,0x0000003f,0x0000007f,0x000000ff,
87 0x000001ff,0x000003ff,0x000007ff,0x00000fff,
88 0x00001fff,0x00003fff,0x00007fff,0x0000ffff,
89 0x0001ffff,0x0003ffff,0x0007ffff,0x000fffff,
90 0x001fffff,0x003fffff,0x007fffff,0x00ffffff,
91 0x01ffffff,0x03ffffff,0x07ffffff,0x0fffffff,
92 0x1fffffff,0x3fffffff,0x7fffffff,0xffffffff};
93
94 #ifdef __OLD
95 int umask[] = { /* This is -1 XOR 2^{i+1}-1 */
96 0xfffffffe,0xfffffffc,0xfffffff8,0xfffffff0,
97 0xffffffe0,0xffffffc0,0xffffff80,0xffffff00,
98 0xfffffe00,0xfffffc00,0xfffff800,0xfffff000,
99 0xffffe000,0xffffc000,0xffff8000,0xffff0000,
100 0xfffe0000,0xfffc0000,0xfff80000,0xfff00000,
101 0xffe00000,0xffc00000,0xff800000,0xff000000,
102 0xfe000000,0xfc000000,0xf8000000,0xf0000000,
103 0xe0000000,0xc0000000,0x80000000,0x00000000};
104 #endif
105
106 /* Internally kept variables for global flag communication */
107
108 int CleartoResync=0; /* Return black blocks until last Resync reached*/
109 int ResyncEnable=0; /* This enables the resync feature */
110 int ResyncCount=0; /* This is the resync marker count */
111 int LastKnownResync=0; /* This is the index of the next Resync */
112 int EndofFile=0; /* End of file means read stream exhausted */
113 int EndofImage=0; /* End of image means EOI image marker found */
114
115 /* Static variables that keep internal state. */
116
117 static FILE *swout;
118 static FILE *srin;
119 static unsigned int current_write_byte;
120 static unsigned int current_read_byte;
121 static unsigned int marker_read_byte;
122 static int read_position;
123 static int write_position;
124 static int InResync=0;
125
126 /* Stack of variables to handle multiple streams. */
127
128 static int Stack_Stream_Current= -1;
129 static int Stack_Stream_Active[NUMBER_OF_STREAMS];
130 static int Stack_Stream_CleartoResync[NUMBER_OF_STREAMS];
131 static int Stack_Stream_ResyncEnable[NUMBER_OF_STREAMS];
132 static int Stack_Stream_ResyncCount[NUMBER_OF_STREAMS];
133 static int Stack_Stream_LastKnownResync[NUMBER_OF_STREAMS];
134 static int Stack_Stream_EndofFile[NUMBER_OF_STREAMS];
135 static int Stack_Stream_EndofImage[NUMBER_OF_STREAMS];
136 static FILE * Stack_Stream_swout[NUMBER_OF_STREAMS];
137 static FILE * Stack_Stream_srin[NUMBER_OF_STREAMS];
138 static unsigned int Stack_Stream_current_write_byte[NUMBER_OF_STREAMS];
139 static unsigned int Stack_Stream_current_read_byte[NUMBER_OF_STREAMS];
140 static unsigned int Stack_Stream_marker_read_byte[NUMBER_OF_STREAMS];
141 static int Stack_Stream_read_position[NUMBER_OF_STREAMS];
142 static int Stack_Stream_write_position[NUMBER_OF_STREAMS];
143
144 /*START*/
145
146 /* STACK STREAM LIBRARY */
147
148 /*BFUNC
149
150 initstream() initializes all of the stream variables-- especially the
151 stack. Not necessary to call unless you wish to use more than one
152 stream variable.
153
154 EFUNC*/
155
156 void initstream()
157 {
158 BEGIN("initstream");
159 int i;
160
161 Stack_Stream_Current= -1;
162 for(i=0;i<NUMBER_OF_STREAMS;i++)
163 {
164 Stack_Stream_Active[i]=0;
165 Stack_Stream_swout[i]=NULL;
166 Stack_Stream_srin[i]=NULL;
167 }
168 }
169
170 /*BFUNC
171
172 pushstream() pushes the currently active stream into its predefined
173 location.
174
175 EFUNC*/
176
177 void pushstream()
178 {
179 BEGIN("pushstream");
180
181 if (Stack_Stream_Current < 0) return;
182 Stack_Stream_CleartoResync[Stack_Stream_Current]=CleartoResync;
183 Stack_Stream_ResyncEnable[Stack_Stream_Current]=ResyncEnable;
184 Stack_Stream_ResyncCount[Stack_Stream_Current]=ResyncCount;
185 Stack_Stream_LastKnownResync[Stack_Stream_Current]=LastKnownResync;
186 Stack_Stream_EndofFile[Stack_Stream_Current]=EndofFile;
187 Stack_Stream_EndofImage[Stack_Stream_Current]=EndofImage;
188 Stack_Stream_swout[Stack_Stream_Current]=swout;
189 Stack_Stream_srin[Stack_Stream_Current]=srin;
190 Stack_Stream_current_write_byte[Stack_Stream_Current]=current_write_byte;
191 Stack_Stream_current_read_byte[Stack_Stream_Current]=current_read_byte;
192 Stack_Stream_marker_read_byte[Stack_Stream_Current]=marker_read_byte;
193 Stack_Stream_read_position[Stack_Stream_Current]=read_position;
194 Stack_Stream_write_position[Stack_Stream_Current]=write_position;
195 }
196
197 /*BFUNC
198
199 popstream() gets the specified stream from the location. If there
200 is already a current active stream, it removes it.
201
202 EFUNC*/
203
204 void popstream(index)
205 int index;
206 {
207 BEGIN("popstream");
208
209 if ((index < 0)||(!Stack_Stream_Active[index]))
210 {
211 WHEREAMI();
212 printf("Cannot pop non-existent stream.\n");
213 exit(ERROR_BOUNDS);
214 }
215 if (Stack_Stream_Current >=0) pushstream();
216 CleartoResync=Stack_Stream_CleartoResync[index];
217 ResyncEnable=Stack_Stream_ResyncEnable[index];
218 ResyncCount=Stack_Stream_ResyncCount[index];
219 LastKnownResync=Stack_Stream_LastKnownResync[index];
220 EndofFile=Stack_Stream_EndofFile[index];
221 EndofImage=Stack_Stream_EndofImage[index];
222 swout=Stack_Stream_swout[index];
223 srin=Stack_Stream_srin[index];
224 current_write_byte=Stack_Stream_current_write_byte[index];
225 current_read_byte=Stack_Stream_current_read_byte[index];
226 marker_read_byte=Stack_Stream_marker_read_byte[index];
227 read_position=Stack_Stream_read_position[index];
228 write_position=Stack_Stream_write_position[index];
229 }
230
231 /* THAT'S ALL FOR THE STACK STREAM LIBRARY! */
232
233 /* BUFFER LIBRARY */
234
235 /*BFUNC
236
237 brtell() is used to find the location in the read stream.
238
239 EFUNC*/
240
241 int brtell()
242 {BEGIN("brtell");return(ftell(srin));}
243
244 /*BFUNC
245
246 brseek() is used to find the location in the read stream.
247
248 EFUNC*/
249
250 int brseek(offset,ptr)
251 int offset;
252 int ptr;
253 {BEGIN("brseek");return(fseek(srin,offset,ptr));}
254
255 /*BFUNC
256
257 bpushc() is used to unget a character value from the current stream.
258
259 EFUNC*/
260
261 void bpushc(value)
262 int value;
263 {BEGIN("bpushc");ungetc(value,srin);}
264
265 /*BFUNC
266
267 bgetc() gets a character from the stream. It is byte aligned and
268 bypasses bit buffering.
269
270 EFUNC*/
271
272 int bgetc()
273 {BEGIN("bgetc");return(getc(srin));}
274
275 /*BFUNC
276
277 bgetw() gets a msb word from the stream.
278
279 EFUNC*/
280
281 int bgetw()
282 {BEGIN("bgetw"); int fu; fu=getc(srin); return ((fu << 8)| getc(srin));}
283
284 /*BFUNC
285
286 bputc() puts a character into the stream. It is byte aligned and
287 bypasses the bit buffering.
288
289 EFUNC*/
290
291 void bputc(c)
292 int c;
293 {BEGIN("bputc");putc(c,swout);}
294
295 /* PROTECTED MARKER GETS AND FETCHES */
296
297 /*BFUNC
298
299 pgetc() gets a character onto the stream but it checks to see
300 if there are any marker conflicts.
301
302 EFUNC*/
303
304 static int pgetc()
305 {
306 BEGIN("pgetc");
307 int temp;
308
309 if (CleartoResync) /* If cleartoresync do not read from stream */
310 {
311 return(0);
312 }
313 if ((temp = bgetc())==MARKER_MARKER) /* If MARKER then */
314 {
315 if ((temp = bgetc())) /* if next is not 0xff, then marker */
316 {
317 WHEREAMI();
318 printf("Unanticipated marker detected.\n");
319 if (!ResyncEnable) DoAllMarker(); /* If no resync enabled */
320 } /* could be marker */
321 else
322 {
323 return(MARKER_MARKER); /* else truly 0xff */
324 }
325 }
326 return(temp);
327 }
328
329 /*BMACRO
330
331 pputc(stream,)
332 ) puts a value onto the stream; puts a value onto the stream, appending an extra '0' if it
333 matches the marker code.
334
335 EMACRO*/
336
337 #define pputc(val) {bputc(val); if (val==MARKER_MARKER) bputc(0);}
338
339 /* MAIN ROUTINES */
340
341 /*BFUNC
342
343 mropen() opens a given filename as the input read stream.
344
345 EFUNC*/
346
347 void mropen(filename,index)
348 char *filename;
349 int index;
350 {
351 BEGIN("mropen");
352
353 if (Stack_Stream_Active[index])
354 {
355 WHEREAMI();
356 printf("%s cannot be opened because %d stream slot filled.\n",
357 filename,index);
358 exit(ERROR_BOUNDS);
359 }
360 if (Stack_Stream_Current!=index) pushstream();
361 current_read_byte=0;
362 read_position = -1;
363 if ((srin = fopen(filename,"r"))==NULL)
364 {
365 WHEREAMI();
366 printf("Cannot read input file %s.\n",
367 filename);
368 exit(ERROR_INIT_FILE);
369 }
370 CleartoResync=0;
371 ResyncEnable=0;
372 ResyncCount=0;
373 LastKnownResync=0;
374 EndofFile=0;
375 EndofImage=1; /* We start after "virtual" end of previous image */
376 Stack_Stream_Current= index;
377 Stack_Stream_Active[index]=1;
378 }
379
380 /*BFUNC
381
382 mrclose() closes the input read stream.
383
384 EFUNC*/
385
386 void mrclose()
387 {
388 BEGIN("mrclose");
389 fclose(srin);
390 srin=NULL;
391 if (swout==NULL)
392 {
393 Stack_Stream_Active[Stack_Stream_Current]=0;
394 Stack_Stream_Current= -1;
395 }
396 }
397
398 /*BFUNC
399
400 mwopen() opens the stream for writing. Note that reading and
401 writing can occur simultaneously because the read and write
402 routines are independently buffered.
403
404 EFUNC*/
405
406 void mwopen(filename,index)
407 char *filename;
408 int index;
409 {
410 BEGIN("mwopen");
411
412 if (Stack_Stream_Active[index])
413 {
414 WHEREAMI();
415 printf("%s cannot be opened because %d stream slot filled.\n",
416 filename,index);
417 exit(ERROR_BOUNDS);
418 }
419 if ((Stack_Stream_Current!=index)) pushstream();
420 current_write_byte=0;
421 write_position=7;
422 if ((swout = fopen(filename,"w+"))==NULL)
423 {
424 WHEREAMI();
425 printf("Cannot open output file %s.\n",filename);
426 exit(ERROR_INIT_FILE);
427 }
428 Stack_Stream_Current= index;
429 Stack_Stream_Active[index]=1;
430 }
431
432 /*BFUNC
433
434 swbytealign() flushes the current bit-buffered byte out to the stream.
435 This is used before marker codes.
436
437 EFUNC*/
438
439 void swbytealign()
440 {
441 BEGIN("swbytealign");
442
443 if (write_position !=7)
444 {
445 current_write_byte |= lmask[write_position];
446 pputc(current_write_byte);
447 write_position=7;
448 current_write_byte=0;
449 }
450 }
451
452 /*BFUNC
453
454 mwclose() closes the stream that has been opened for writing.
455
456 EFUNC*/
457
458 void mwclose()
459 {
460 BEGIN("mwclose");
461
462 swbytealign();
463 fclose(swout);
464 swout=NULL;
465 if (srin==NULL)
466 {
467 Stack_Stream_Active[Stack_Stream_Current]=0;
468 Stack_Stream_Current= -1;
469 }
470 }
471
472 /*BFUNC
473
474 mwtell() returns the bit position on the write stream.
475
476 EFUNC*/
477
478 long mwtell()
479 {
480 BEGIN("mwtell");
481
482 return((ftell(swout)<<3) + (7 - write_position));
483 }
484
485 /*BFUNC
486
487 mrtell() returns the bit position on the read stream.
488
489 EFUNC*/
490
491 long mrtell()
492 {
493 BEGIN("mrtell");
494
495 return((ftell(srin)<<3) - (read_position+1));
496 }
497
498 /*BFUNC
499
500 mwseek returns the bit position on the write stream.
501
502 EFUNC*/
503
504 void mwseek(distance)
505 long distance;
506 {
507 BEGIN("mwseek");
508 int length;
509
510 if (write_position!=7) /* Must flush out current byte */
511 {
512 putc(current_write_byte,swout);
513 }
514 fseek(swout,0,2L); /* Find end */
515 length = ftell(swout);
516 fseek(swout,((distance+7)>>3),0L);
517 if ((length<<3) <= distance) /* Make sure we read clean stuff */
518 {
519 current_write_byte = 0;
520 write_position = 7 - distance & 0x7;
521 }
522 else
523 {
524 current_write_byte = getc(swout); /* if within bounds, then read byte */
525 write_position = 7 - (distance & 0x7);
526 fseek(swout,((distance+7)>>3),0L); /* Reset seek pointer for write */
527 }
528 }
529
530
531 /*BFUNC
532
533 mrseek() jumps to a bit position on the read stream.
534
535 EFUNC*/
536
537 void mrseek(distance)
538 long distance;
539 {
540 BEGIN("mrseek");
541
542 fseek(srin,(distance>>3),0L); /* Go to location */
543 current_read_byte = bgetc(); /* read byte in */
544 read_position = 7 - (distance % 8);
545 }
546
547
548 /*BFUNC
549
550 megetb() gets a bit from the read stream.
551
552 EFUNC*/
553
554 int megetb()
555 {
556 BEGIN("megetb");
557
558 if (read_position < 0)
559 {
560 current_read_byte = pgetc();
561 read_position=7;
562 }
563 if (current_read_byte&bit_set_mask[read_position--])
564 {
565 return(1);
566 }
567 return(0);
568 }
569
570 /*BFUNC
571
572 meputv() puts n bits from b onto the writer stream.
573
574 EFUNC*/
575
576 void meputv(n,b)
577 int n;
578 int b;
579 {
580 BEGIN("meputv");
581 int p;
582
583 n--;
584 b &= lmask[n];
585 p = n - write_position;
586 if (!p) /* Can do parallel save immediately */
587 {
588 current_write_byte |= b;
589 pputc(current_write_byte);
590 current_write_byte = 0;
591 write_position = 7;
592 return;
593 }
594 else if (p < 0) /* if can fit, we have to shift byte */
595 {
596 p = -p;
597 current_write_byte |= (b << p);
598 write_position = p-1;
599 return;
600 }
601 current_write_byte |= (b >> p); /* cannot fit. we must do putc's */
602 pputc(current_write_byte); /* Save off remainder */
603 while(p > 7) /* Save off bytes while remaining > 7 */
604 {
605 p -= 8;
606 current_write_byte = (b >> p) & lmask[7];
607 pputc(current_write_byte);
608 }
609 if (!p) /* If zero then reset position */
610 {
611 write_position = 7;
612 current_write_byte = 0;
613 }
614 else /* Otherwise reset write byte buffer */
615 {
616 write_position = 8-p;
617 current_write_byte = (b << write_position) & lmask[7];
618 write_position--;
619 }
620 }
621
622 /*BFUNC
623
624 megetv() gets n bits from the read stream and returns it.
625
626 EFUNC*/
627
628 int megetv(n)
629 int n;
630 {
631 BEGIN("megetv");
632 int p,rv;
633
634 n--;
635 p = n-read_position;
636 while(p > 0)
637 {
638 if (read_position>23) /* If byte buffer contains almost entire word */
639 {
640 rv = (current_read_byte << p); /* Manipulate buffer */
641 current_read_byte = pgetc(); /* Change read bytes */
642 rv |= (current_read_byte >> (8-p));
643 read_position = 7-p;
644 return(rv & lmask[n]); /* Can return pending residual val */
645 }
646 current_read_byte = (current_read_byte << 8) | pgetc();
647 read_position += 8; /* else shift in new information */
648 p -= 8;
649 }
650 if (!p) /* If position is zero */
651 {
652 read_position = -1; /* Can return current byte */
653 return(current_read_byte & lmask[n]);
654 }
655 p = -p; /* Else reverse position and shift */
656 read_position = p-1;
657 return((current_read_byte >> p) & lmask[n]);
658 }
659
660
661 /*BFUNC
662
663 DoMarker() performs marker analysis. We assume that the Current Marker
664 head has been read (0xFF) plus top information is at
665 marker\_read\_byte.
666
667 EFUNC*/
668
669 int DoMarker()
670 {
671 BEGIN("DoMarker");
672 int i,hin,lon,marker,length;
673
674 current_read_byte = 0;
675 read_position= -1; /* Make sure we are byte-flush. */
676 while(marker_read_byte==MARKER_FIL) /* Get rid of FIL markers */
677 {
678 #ifdef VERSION_1.0
679 if ((marker_read_byte = bgetc())!=MARKER_MARKER)
680 {
681 WHEREAMI();
682 printf("Unknown FIL marker. Bypassing.\n");
683 ErrorValue = ERROR_MARKER;
684 return(0);
685 }
686 #endif
687 marker_read_byte = bgetc();
688 }
689 lon = marker_read_byte & 0x0f; /* Segregate between hi and lo */
690 hin = (marker_read_byte>>4) & 0x0f; /* nybbles for the marker read byte */
691 marker = marker_read_byte;
692
693 if (InResync)
694 {
695 if ((marker <0xd0)||(marker>0xd7))
696 {
697 WHEREAMI();
698 printf("Illegal resync marker found.\n");
699 return(0);
700 }
701 }
702 switch(hin) /* Pretty much self explanatory */
703 {
704 case 0x0c: /* Frame Style Marker */
705 switch(lon)
706 {
707 case 0x04:
708 ReadDht();
709 break;
710 case 0x00:
711 case 0x01:
712 case 0x03:
713 ReadSof(lon);
714 break;
715 case 0x08:
716 case 0x09:
717 case 0x0a:
718 case 0x0b:
719 case 0x0c:
720 case 0x0d:
721 case 0x0e:
722 case 0x0f:
723 WHEREAMI();
724 printf("Arithmetic coding not supported.\n");
725 length = bgetw();
726 for(i=2;i<length;i++) /* Length adds 2 bytes itself */
727 bgetc();
728 break;
729 case 0x02:
730 case 0x05:
731 case 0x06:
732 case 0x07:
733 default:
734 WHEREAMI();
735 printf("Frame type %x not supported.\n",lon);
736 length = bgetw();
737 for(i=2;i<length;i++) /* Length adds 2 bytes itself */
738 bgetc();
739 break;
740 }
741 break;
742 case 0x0d: /* Resync Marker */
743 if (lon > 7)
744 {
745 switch(lon)
746 {
747 case 0x08: /* Start of Image */
748 EndofImage=0; /* If End of Image occurs */
749 CImage->ImageSequence++; /* reset, and increment sequence */
750 break;
751 case 0x09: /* End of Image */
752 EndofImage=1;
753 break;
754 case 0x0a:
755 ResyncCount=0; /* SOS clears the resync count */
756 ReadSos();
757 break;
758 case 0x0b:
759 ReadDqt();
760 break;
761 case 0x0c:
762 ReadDnl();
763 break;
764 case 0x0d:
765 ReadDri();
766 break;
767 default:
768 WHEREAMI();
769 printf("Hierarchical markers found.\n");
770 length = bgetw();
771 for(i=2;i<length;i++) /* Length adds 2 bytes itself */
772 {
773 bgetc();
774 }
775 break;
776 }
777 }
778 break;
779 case 0x0e: /* Application Specific */
780 length = bgetw();
781 for(i=2;i<length;i++) /* Length adds 2 bytes itself */
782 bgetc();
783 break;
784 case 0x0f: /* JPEG Specific */
785 length = bgetw();
786 for(i=2;i<length;i++) /* Length adds 2 bytes itself */
787 bgetc();
788 break;
789 default:
790 WHEREAMI();
791 printf("Bad marker byte %d.\n",marker);
792 Resync();
793 ErrorValue = ERROR_MARKER;
794 return(-1);
795 break;
796 }
797 return(marker);
798 }
799
800 /*BFUNC
801
802 ScreenMarker() looks to see what marker is present on the stream. It
803 returns with the marker value read.
804
805 EFUNC*/
806
807 int ScreenMarker()
808 {
809 BEGIN("ScreenMarker");
810
811 if (read_position!=7) /* Already read byte */
812 {
813 current_read_byte = 0;
814 read_position= -1; /* Consume byte to be flush */
815 if ((marker_read_byte=bgetc())==EOF)
816 {
817 EndofFile=2;
818 return(EOF);
819 }
820 }
821 else /* If flush, then marker byte is current read byte */
822 {
823 marker_read_byte = current_read_byte;
824 }
825 if (marker_read_byte!=MARKER_MARKER) /* Not a marker, return -1. */
826 {
827 current_read_byte = marker_read_byte;
828 read_position=7;
829 return(-1);
830 }
831 while((marker_read_byte = bgetc())==MARKER_FIL)
832 { /* Get rid of FIL markers */
833 if ((marker_read_byte = bgetc())!=MARKER_MARKER)
834 {
835 WHEREAMI();
836 printf("Unattached FIL marker.\n");
837 ErrorValue = ERROR_MARKER;
838 return(-1);
839 }
840 if (marker_read_byte == EOF) /* Found end of file */
841 {
842 EndofFile=2;
843 return(EOF);
844 }
845 marker_read_byte = bgetc(); /* Otherwise read another byte */
846 } /* Call processor for markers */
847 if (marker_read_byte) return(DoMarker());
848 else /* Is a FF00 so don't process */
849 {
850 current_read_byte=MARKER_MARKER; /* 255 actually read */
851 read_position=7;
852 return(-1);
853 }
854 }
855
856 /*BFUNC
857
858 Resync() does a resync action on the stream. This involves searching
859 for the next resync byte.
860
861 EFUNC*/
862
863 void Resync()
864 {
865 BEGIN("Resync");
866
867 if (!ResyncEnable)
868 {
869 WHEREAMI();
870 printf("Resync without resync enabled\n");
871 printf("Fatal error.\n");
872 TerminateFile();
873 exit(ERROR_UNRECOVERABLE);
874 }
875 WHEREAMI();
876 printf("Attempting resynchronization.\n");
877 do
878 {
879 while((marker_read_byte = bgetc())!=MARKER_MARKER)
880 {
881 if (marker_read_byte==EOF)
882 {
883 WHEREAMI();
884 printf("Attempt to resync at end of file.\n");
885 printf("Sorry.\n");
886 TerminateFile();
887 exit(ERROR_PREMATURE_EOF);
888 }
889 }
890 }
891 while(((marker_read_byte = bgetc()) & MARKER_RSC_MASK)!=MARKER_RSC);
892 LastKnownResync = marker_read_byte & 0x07; /* Set up currently read */
893 WHEREAMI(); /* resync byte as future ref */
894 printf("Resync successful!\n");
895 /*
896 In general, we assume that we must add black space
897 until resynchronization. This is consistent under both
898 byte loss, byte gain, and byte corruption.
899 We assume corruption does not create new markers with
900 an RSC value--if so, we are probably dead, anyways.
901 */
902 CleartoResync=1;
903 ResyncCount = (LastKnownResync+1)&0x07;
904 current_read_byte = 0;
905 read_position = -1;
906 ResetCodec(); /* Reset the codec incase in a non-local jump. */
907
908 printf("ResyncCount: %d LastKnownResync: %d\n",
909 ResyncCount,LastKnownResync);
910 }
911
912 /*BFUNC
913
914 WriteResync() writes a resync marker out to the write stream.
915
916 EFUNC*/
917
918 void WriteResync()
919 {
920 BEGIN("WriteResync");
921
922 swbytealign(); /* This procedure writes a byte-aligned */
923 bputc(MARKER_MARKER); /* resync marker. */
924 bputc((MARKER_RSC|(ResyncCount & 0x07)));
925 ResyncCount = (ResyncCount + 1) & 0x07;
926 }
927
928 /*BFUNC
929
930 ReadResync() looks for a resync marker on the stream. It returns a 0
931 if successful and a -1 if a search pass was required.
932
933 EFUNC*/
934
935 int ReadResync()
936 {
937 BEGIN("ReadResync");
938 int ValueRead;
939
940 if (Robust) InResync=1;
941 while((ValueRead = ScreenMarker()) >= 0)
942 {
943 if ((ValueRead & MARKER_RSC_MASK)!=MARKER_RSC) /* Strange marker found */
944 {
945 if (ValueRead != MARKER_DNL) /* DNL only other possibility */
946 { /* actually excluded, never reached */
947 WHEREAMI(); /* 11/19/91 ACH */
948 printf("Non-Resync marker found for resync.\n");
949 printf("Trying again.\n");
950 }
951 }
952 else
953 {
954 ValueRead = ValueRead & 0x07; /* If so, then check resync count */
955 if (ValueRead != ResyncCount)
956 {
957 WHEREAMI();
958 printf("Bad resync counter. No search done.\n");
959 }
960 ResyncCount = (ResyncCount+1)&0x07;
961 /* Flush spurious markers. */
962 while((ValueRead = ScreenMarker()) >= 0);
963 InResync=0;
964 return(0);
965 }
966 }
967 WHEREAMI();
968 printf("Anticipated resync not found.\n");
969 Resync();
970 InResync=0;
971 return(-1);
972 }
973
974 /*BFUNC
975
976 ScreenAllMarker() looks for all the markers on the stream. It returns
977 a 0 if a marker has been found, -1 if no markers exist.
978
979 EFUNC*/
980
981 int ScreenAllMarker()
982 {
983 BEGIN("ScreenAllMarker");
984
985 if (ScreenMarker()<0)
986 {
987 return(-1);
988 }
989 while(ScreenMarker()>=0); /* Flush out all markers */
990 return(0);
991 }
992
993 /*BFUNC
994
995 DoAllMarker() is the same as ScreenAllMarker except we assume that the
996 prefix markerbyte (0xff) has been read and the second byte of the
997 prefix is in the marker\_byte variable. It returns a -1 if there is an
998 error in reading the marker.
999
1000 EFUNC*/
1001
1002 int DoAllMarker()
1003 {
1004 BEGIN("DoAllMarker");
1005
1006 if (DoMarker()<0)
1007 {
1008 return(-1);
1009 }
1010 while(ScreenMarker()>=0); /* Flush out all markers */
1011 return(0);
1012 }
1013
1014 /*END*/