1 | #include "sysincludes.h"
|
---|
2 | #include "msdos.h"
|
---|
3 | #include "mtools.h"
|
---|
4 |
|
---|
5 | typedef struct Filter_t {
|
---|
6 | Class_t *Class;
|
---|
7 | int refs;
|
---|
8 | Stream_t *Next;
|
---|
9 | Stream_t *Buffer;
|
---|
10 |
|
---|
11 | int dospos;
|
---|
12 | int unixpos;
|
---|
13 | int mode;
|
---|
14 | int rw;
|
---|
15 | int lastchar;
|
---|
16 | } Filter_t;
|
---|
17 |
|
---|
18 | #define F_READ 1
|
---|
19 | #define F_WRITE 2
|
---|
20 |
|
---|
21 | /* read filter filters out messy dos' bizarre end of lines and final 0x1a's */
|
---|
22 |
|
---|
23 | static int read_filter(Stream_t *Stream, char *buf, mt_off_t iwhere, size_t len)
|
---|
24 | {
|
---|
25 | DeclareThis(Filter_t);
|
---|
26 | int i,j,ret;
|
---|
27 |
|
---|
28 | off_t where = truncBytes32(iwhere);
|
---|
29 |
|
---|
30 | if ( where != This->unixpos ){
|
---|
31 | fprintf(stderr,"Bad offset\n");
|
---|
32 | exit(1);
|
---|
33 | }
|
---|
34 | if (This->rw == F_WRITE){
|
---|
35 | fprintf(stderr,"Change of transfer direction!\n");
|
---|
36 | exit(1);
|
---|
37 | }
|
---|
38 | This->rw = F_READ;
|
---|
39 |
|
---|
40 | ret = READS(This->Next, buf, (mt_off_t) This->dospos, len);
|
---|
41 | if ( ret < 0 )
|
---|
42 | return ret;
|
---|
43 |
|
---|
44 | j = 0;
|
---|
45 | for (i=0; i< ret; i++){
|
---|
46 | if ( buf[i] == '\r' )
|
---|
47 | continue;
|
---|
48 | if (buf[i] == 0x1a)
|
---|
49 | break;
|
---|
50 | This->lastchar = buf[j++] = buf[i];
|
---|
51 | }
|
---|
52 |
|
---|
53 | This->dospos += i;
|
---|
54 | This->unixpos += j;
|
---|
55 | return j;
|
---|
56 | }
|
---|
57 |
|
---|
58 | static int write_filter(Stream_t *Stream, char *buf, mt_off_t iwhere,
|
---|
59 | size_t len)
|
---|
60 | {
|
---|
61 | DeclareThis(Filter_t);
|
---|
62 | int i,j,ret;
|
---|
63 | char buffer[1025];
|
---|
64 |
|
---|
65 | off_t where = truncBytes32(iwhere);
|
---|
66 |
|
---|
67 | if(This->unixpos == -1)
|
---|
68 | return -1;
|
---|
69 |
|
---|
70 | if (where != This->unixpos ){
|
---|
71 | fprintf(stderr,"Bad offset\n");
|
---|
72 | exit(1);
|
---|
73 | }
|
---|
74 |
|
---|
75 | if (This->rw == F_READ){
|
---|
76 | fprintf(stderr,"Change of transfer direction!\n");
|
---|
77 | exit(1);
|
---|
78 | }
|
---|
79 | This->rw = F_WRITE;
|
---|
80 |
|
---|
81 | j=i=0;
|
---|
82 | while(i < 1024 && j < len){
|
---|
83 | if (buf[j] == '\n' ){
|
---|
84 | buffer[i++] = '\r';
|
---|
85 | buffer[i++] = '\n';
|
---|
86 | j++;
|
---|
87 | continue;
|
---|
88 | }
|
---|
89 | buffer[i++] = buf[j++];
|
---|
90 | }
|
---|
91 | This->unixpos += j;
|
---|
92 |
|
---|
93 | ret = force_write(This->Next, buffer, (mt_off_t) This->dospos, i);
|
---|
94 | if(ret >0 )
|
---|
95 | This->dospos += ret;
|
---|
96 | if ( ret != i ){
|
---|
97 | /* no space on target file ? */
|
---|
98 | This->unixpos = -1;
|
---|
99 | return -1;
|
---|
100 | }
|
---|
101 | return j;
|
---|
102 | }
|
---|
103 |
|
---|
104 | static int free_filter(Stream_t *Stream)
|
---|
105 | {
|
---|
106 | DeclareThis(Filter_t);
|
---|
107 | char buffer=0x1a;
|
---|
108 |
|
---|
109 | /* write end of file */
|
---|
110 | if (This->rw == F_WRITE)
|
---|
111 | return force_write(This->Next, &buffer, (mt_off_t) This->dospos, 1);
|
---|
112 | else
|
---|
113 | return 0;
|
---|
114 | }
|
---|
115 |
|
---|
116 | static Class_t FilterClass = {
|
---|
117 | read_filter,
|
---|
118 | write_filter,
|
---|
119 | 0, /* flush */
|
---|
120 | free_filter,
|
---|
121 | 0, /* set geometry */
|
---|
122 | get_data_pass_through,
|
---|
123 | 0
|
---|
124 | };
|
---|
125 |
|
---|
126 | Stream_t *open_filter(Stream_t *Next)
|
---|
127 | {
|
---|
128 | Filter_t *This;
|
---|
129 |
|
---|
130 | This = New(Filter_t);
|
---|
131 | if (!This)
|
---|
132 | return NULL;
|
---|
133 | This->Class = &FilterClass;
|
---|
134 | This->dospos = This->unixpos = This->rw = 0;
|
---|
135 | This->Next = Next;
|
---|
136 | This->refs = 1;
|
---|
137 | This->Buffer = 0;
|
---|
138 |
|
---|
139 | return (Stream_t *) This;
|
---|
140 | }
|
---|