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