1
|
1
|
|
2 #include "vcd_read.c"
|
|
3
|
|
4 //=================== STREAMER =========================
|
|
5
|
|
6 #define STREAM_BUFFER_SIZE 4096
|
|
7
|
|
8 #define STREAMTYPE_FILE 0
|
|
9 #define STREAMTYPE_VCD 1
|
|
10
|
|
11 typedef struct {
|
|
12 int fd;
|
|
13 long pos;
|
|
14 int eof;
|
|
15 int type; // 0=file 1=VCD
|
|
16 unsigned int buf_pos,buf_len;
|
|
17 unsigned char buffer[STREAM_BUFFER_SIZE];
|
|
18 } stream_t;
|
|
19
|
|
20 int stream_fill_buffer(stream_t *s){
|
|
21 int len;
|
|
22 if(s->eof){ s->buf_pos=s->buf_len=0; return 0; }
|
|
23 switch(s->type){
|
|
24 case STREAMTYPE_FILE:
|
|
25 len=read(s->fd,s->buffer,STREAM_BUFFER_SIZE);break;
|
|
26 case STREAMTYPE_VCD:
|
|
27 #ifdef VCD_CACHE
|
|
28 len=vcd_cache_read(s->fd,s->buffer);break;
|
|
29 #else
|
|
30 len=vcd_read(s->fd,s->buffer);break;
|
|
31 #endif
|
|
32 default: len=0;
|
|
33 }
|
|
34 if(len<=0){ s->eof=1; s->buf_pos=s->buf_len=0; return 0; }
|
|
35 s->buf_pos=0;
|
|
36 s->buf_len=len;
|
|
37 s->pos+=len;
|
|
38 // printf("[%d]",len);fflush(stdout);
|
|
39 return len;
|
|
40 }
|
|
41
|
|
42 inline unsigned int stream_read_char(stream_t *s){
|
|
43 return (s->buf_pos<s->buf_len)?s->buffer[s->buf_pos++]:
|
|
44 (stream_fill_buffer(s)?s->buffer[s->buf_pos++]:0);
|
|
45 // if(s->buf_pos<s->buf_len) return s->buffer[s->buf_pos++];
|
|
46 // stream_fill_buffer(s);
|
|
47 // if(s->buf_pos<s->buf_len) return s->buffer[s->buf_pos++];
|
|
48 // return 0; // EOF
|
|
49 }
|
|
50
|
|
51 inline unsigned int stream_read_word(stream_t *s){
|
|
52 int x,y;
|
|
53 x=stream_read_char(s);
|
|
54 y=stream_read_char(s);
|
|
55 return (x<<8)|y;
|
|
56 }
|
|
57
|
|
58 inline unsigned int stream_read_dword(stream_t *s){
|
|
59 unsigned int y;
|
|
60 y=stream_read_char(s);
|
|
61 y=(y<<8)|stream_read_char(s);
|
|
62 y=(y<<8)|stream_read_char(s);
|
|
63 y=(y<<8)|stream_read_char(s);
|
|
64 return y;
|
|
65 }
|
|
66
|
|
67 inline unsigned int stream_read_word_le(stream_t *s){
|
|
68 int x,y;
|
|
69 x=stream_read_char(s);
|
|
70 y=stream_read_char(s);
|
|
71 return (y<<8)|x;
|
|
72 }
|
|
73
|
|
74 inline unsigned int stream_read_dword_le(stream_t *s){
|
|
75 unsigned int y;
|
|
76 y=stream_read_char(s);
|
|
77 y|=stream_read_char(s)<<8;
|
|
78 y|=stream_read_char(s)<<16;
|
|
79 y|=stream_read_char(s)<<24;
|
|
80 return y;
|
|
81 }
|
|
82
|
|
83 inline void stream_read(stream_t *s,char* mem,int len){
|
|
84 while(len>0){
|
|
85 int x;
|
|
86 x=s->buf_len-s->buf_pos;
|
|
87 if(x==0){
|
|
88 if(!stream_fill_buffer(s)) return; // EOF
|
|
89 x=s->buf_len-s->buf_pos;
|
|
90 }
|
|
91 if(s->buf_pos>s->buf_len) printf("stream_read: WARNING! s->buf_pos>s->buf_len\n");
|
|
92 if(x>len) x=len;
|
|
93 memcpy(mem,&s->buffer[s->buf_pos],x);
|
|
94 s->buf_pos+=x; mem+=x; len-=x;
|
|
95 }
|
|
96 }
|
|
97
|
|
98 inline int stream_eof(stream_t *s){
|
|
99 return s->eof;
|
|
100 }
|
|
101
|
|
102 inline int stream_tell(stream_t *s){
|
|
103 return s->pos+s->buf_pos-s->buf_len;
|
|
104 }
|
|
105
|
|
106 inline int stream_seek(stream_t *s,unsigned int pos){
|
|
107 unsigned int newpos;
|
|
108
|
|
109 if(verbose>=3) printf("seek to 0x%X\n",pos);
|
|
110
|
|
111 if(pos<s->pos){
|
|
112 int x=pos-(s->pos-s->buf_len);
|
|
113 if(x>=0){
|
|
114 s->buf_pos=x;
|
|
115 // putchar('*');fflush(stdout);
|
|
116 return 1;
|
|
117 }
|
|
118 }
|
|
119
|
|
120 if(verbose>=3){
|
|
121 printf("s->pos=%X newpos=%X new_bufpos=%X buflen=%X \n",
|
|
122 s->pos,newpos,pos,s->buf_len);
|
|
123 }
|
|
124
|
|
125 s->buf_pos=s->buf_len=0;
|
|
126
|
|
127 switch(s->type){
|
|
128 case STREAMTYPE_FILE:
|
|
129 newpos=pos&(~4095);break;
|
|
130 case STREAMTYPE_VCD:
|
|
131 newpos=(pos/VCD_SECTOR_DATA)*VCD_SECTOR_DATA;break;
|
|
132 }
|
|
133
|
|
134 pos-=newpos;
|
|
135
|
|
136 if(newpos==0 || newpos!=s->pos){
|
|
137 s->pos=newpos; // real seek
|
|
138 switch(s->type){
|
|
139 case STREAMTYPE_FILE:
|
|
140 if(lseek(s->fd,s->pos,SEEK_SET)<0) s->eof=1;
|
|
141 break;
|
|
142 case STREAMTYPE_VCD:
|
|
143 #ifdef VCD_CACHE
|
|
144 vcd_cache_seek(s->pos/VCD_SECTOR_DATA);
|
|
145 #else
|
|
146 vcd_set_msf(s->pos/VCD_SECTOR_DATA);
|
|
147 #endif
|
|
148 break;
|
|
149 }
|
|
150 // putchar('.');fflush(stdout);
|
|
151 //} else {
|
|
152 // putchar('%');fflush(stdout);
|
|
153 }
|
|
154
|
|
155 stream_fill_buffer(s);
|
|
156 if(pos>=0 && pos<s->buf_len){
|
|
157 s->buf_pos=pos; // byte position in sector
|
|
158 return 1;
|
|
159 }
|
|
160 printf("stream_seek: WARNING! Can't seek to 0x%X !\n",pos+newpos);
|
|
161 return 0;
|
|
162 }
|
|
163
|
|
164 inline void stream_skip(stream_t *s,int len){
|
|
165 if(len<0 || len>2*STREAM_BUFFER_SIZE){
|
|
166 // negative or big skip!
|
|
167 stream_seek(s,stream_tell(s)+len);
|
|
168 return;
|
|
169 }
|
|
170 while(len>0){
|
|
171 int x=s->buf_len-s->buf_pos;
|
|
172 if(x==0){
|
|
173 if(!stream_fill_buffer(s)) return; // EOF
|
|
174 x=s->buf_len-s->buf_pos;
|
|
175 }
|
|
176 if(x>len) x=len;
|
|
177 //memcpy(mem,&s->buf[s->buf_pos],x);
|
|
178 s->buf_pos+=x; len-=x;
|
|
179 }
|
|
180 }
|
|
181
|
|
182 void stream_reset(stream_t *s){
|
|
183 if(s->eof){
|
|
184 s->pos=0; //ftell(f);
|
|
185 // s->buf_pos=s->buf_len=0;
|
|
186 s->eof=0;
|
|
187 }
|
|
188 //stream_seek(s,0);
|
|
189 }
|
|
190
|
|
191 stream_t* new_stream(int fd,int type){
|
|
192 stream_t *s=malloc(sizeof(stream_t));
|
|
193 s->fd=fd;
|
|
194 s->type=type;
|
|
195 stream_reset(s);
|
|
196 return s;
|
|
197 }
|
|
198
|
|
199 void free_stream(stream_t *s){
|
|
200 free(s);
|
|
201 }
|
|
202
|
|
203
|