578
|
1
|
|
2 #define STREAM_BUFFER_SIZE 2048
|
|
3
|
|
4 #define STREAMTYPE_FILE 0
|
|
5 #define STREAMTYPE_VCD 1
|
692
|
6 #define STREAMTYPE_STREAM 2 // same as FILE but no seeking (for stdin)
|
578
|
7
|
|
8 #define VCD_SECTOR_SIZE 2352
|
|
9 #define VCD_SECTOR_OFFS 24
|
|
10 #define VCD_SECTOR_DATA 2324
|
|
11
|
|
12 int vcd_seek_to_track(int fd,int track);
|
|
13 void vcd_read_toc(int fd);
|
|
14
|
|
15 #ifdef VCD_CACHE
|
|
16 void vcd_cache_init(int s);
|
|
17 #endif
|
|
18
|
|
19 typedef struct {
|
|
20 int fd;
|
|
21 long pos;
|
|
22 int eof;
|
|
23 int type; // 0=file 1=VCD
|
|
24 unsigned int buf_pos,buf_len;
|
598
|
25 long start_pos,end_pos;
|
583
|
26 unsigned char buffer[STREAM_BUFFER_SIZE>VCD_SECTOR_SIZE?STREAM_BUFFER_SIZE:VCD_SECTOR_SIZE];
|
578
|
27 } stream_t;
|
|
28
|
|
29 int stream_fill_buffer(stream_t *s);
|
|
30 int stream_seek_long(stream_t *s,unsigned int pos);
|
|
31
|
|
32 inline static int stream_read_char(stream_t *s){
|
|
33 return (s->buf_pos<s->buf_len)?s->buffer[s->buf_pos++]:
|
|
34 (stream_fill_buffer(s)?s->buffer[s->buf_pos++]:-256);
|
|
35 // if(s->buf_pos<s->buf_len) return s->buffer[s->buf_pos++];
|
|
36 // stream_fill_buffer(s);
|
|
37 // if(s->buf_pos<s->buf_len) return s->buffer[s->buf_pos++];
|
|
38 // return 0; // EOF
|
|
39 }
|
|
40
|
|
41 inline static unsigned int stream_read_word(stream_t *s){
|
|
42 int x,y;
|
|
43 x=stream_read_char(s);
|
|
44 y=stream_read_char(s);
|
|
45 return (x<<8)|y;
|
|
46 }
|
|
47
|
|
48 inline static unsigned int stream_read_dword(stream_t *s){
|
|
49 unsigned int y;
|
|
50 y=stream_read_char(s);
|
|
51 y=(y<<8)|stream_read_char(s);
|
|
52 y=(y<<8)|stream_read_char(s);
|
|
53 y=(y<<8)|stream_read_char(s);
|
|
54 return y;
|
|
55 }
|
|
56
|
|
57 inline static unsigned int stream_read_word_le(stream_t *s){
|
|
58 int x,y;
|
|
59 x=stream_read_char(s);
|
|
60 y=stream_read_char(s);
|
|
61 return (y<<8)|x;
|
|
62 }
|
|
63
|
|
64 inline static unsigned int stream_read_dword_le(stream_t *s){
|
|
65 unsigned int y;
|
|
66 y=stream_read_char(s);
|
|
67 y|=stream_read_char(s)<<8;
|
|
68 y|=stream_read_char(s)<<16;
|
|
69 y|=stream_read_char(s)<<24;
|
|
70 return y;
|
|
71 }
|
|
72
|
|
73 inline static void stream_read(stream_t *s,char* mem,int len){
|
|
74 while(len>0){
|
|
75 int x;
|
|
76 x=s->buf_len-s->buf_pos;
|
|
77 if(x==0){
|
|
78 if(!stream_fill_buffer(s)) return; // EOF
|
|
79 x=s->buf_len-s->buf_pos;
|
|
80 }
|
|
81 if(s->buf_pos>s->buf_len) printf("stream_read: WARNING! s->buf_pos>s->buf_len\n");
|
|
82 if(x>len) x=len;
|
|
83 memcpy(mem,&s->buffer[s->buf_pos],x);
|
|
84 s->buf_pos+=x; mem+=x; len-=x;
|
|
85 }
|
|
86 }
|
|
87
|
|
88 inline static int stream_eof(stream_t *s){
|
|
89 return s->eof;
|
|
90 }
|
|
91
|
|
92 inline static int stream_tell(stream_t *s){
|
|
93 return s->pos+s->buf_pos-s->buf_len;
|
|
94 }
|
|
95
|
|
96 inline static int stream_seek(stream_t *s,unsigned int pos){
|
|
97
|
|
98 // if(verbose>=3) printf("seek to 0x%X\n",pos);
|
|
99
|
|
100 if(pos<s->pos){
|
|
101 int x=pos-(s->pos-s->buf_len);
|
|
102 if(x>=0){
|
|
103 s->buf_pos=x;
|
|
104 // putchar('*');fflush(stdout);
|
|
105 return 1;
|
|
106 }
|
|
107 }
|
|
108
|
|
109 return stream_seek_long(s,pos);
|
|
110 }
|
|
111
|
|
112 inline static void stream_skip(stream_t *s,int len){
|
692
|
113 if(len<0 || (len>2*STREAM_BUFFER_SIZE && s->type!=STREAMTYPE_STREAM)){
|
578
|
114 // negative or big skip!
|
|
115 stream_seek(s,stream_tell(s)+len);
|
|
116 return;
|
|
117 }
|
|
118 while(len>0){
|
|
119 int x=s->buf_len-s->buf_pos;
|
|
120 if(x==0){
|
|
121 if(!stream_fill_buffer(s)) return; // EOF
|
|
122 x=s->buf_len-s->buf_pos;
|
|
123 }
|
|
124 if(x>len) x=len;
|
|
125 //memcpy(mem,&s->buf[s->buf_pos],x);
|
|
126 s->buf_pos+=x; len-=x;
|
|
127 }
|
|
128 }
|
|
129
|
|
130 void stream_reset(stream_t *s);
|
|
131 stream_t* new_stream(int fd,int type);
|
|
132 void free_stream(stream_t *s);
|
|
133
|