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