File: sioBlocked.c

package info (click to toggle)
ted 2.16-5
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 13,944 kB
  • ctags: 20,273
  • sloc: ansic: 167,980; makefile: 12,518; sh: 263
file content (243 lines) | stat: -rw-r--r-- 5,960 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/************************************************************************/
/*									*/
/*  Simple io streams, recursive use for packing in 255 byte blocks	*/
/*  with a length indicator.						*/
/*  [Like in the GIF spec.]						*/
/*									*/
/************************************************************************/

#   include	"appUtilConfig.h"

#   include	<stdlib.h>
#   include	<string.h>

#   include	"sioBlocked.h"
#   include	<appDebugon.h>


typedef struct BlockedInputStream
    {
    SimpleInputStream *		bisBlocked;
    unsigned char		bisBytes[256];
    int				bisOffset;
    int				bisExhausted;
    } BlockedInputStream;

static int sioInBlockedReadBytes(	void *		voidbis,
					unsigned char *	buffer,
					int		count )
    {
    BlockedInputStream *	bis= (BlockedInputStream *)voidbis;
    int				done= 0;

    int				todo;

    if  ( bis->bisExhausted )
	{ return -1;	}

    if  ( bis->bisOffset < bis->bisBytes[0]+ 1 )
	{
	todo= bis->bisBytes[0]+ 1- bis->bisOffset;

	if  ( todo > count )
	    { todo= count;	}

	memcpy( buffer, bis->bisBytes+ bis->bisOffset, todo );

	bis->bisOffset += todo;
	buffer += todo;
	done += todo;
	}

    while( done < count )
	{
	if  ( bis->bisOffset != bis->bisBytes[0]+ 1 )
	    { LLDEB(bis->bisOffset,bis->bisBytes[0]);	}

	bis->bisOffset= 1;

	todo= sioInGetCharacter( bis->bisBlocked );
	if  ( todo == EOF )
	    { LDEB(todo); bis->bisExhausted= 1; return -1;	}

	bis->bisBytes[0]= todo;

	if  ( bis->bisBytes[0] == 0 )
	    { bis->bisExhausted= 1; break;	}
	
	if  ( todo > count- done )
	    {
	    if  ( sioInReadBytes( bis->bisBlocked, bis->bisBytes+ 1,
							    todo ) != todo )
		{ LDEB(todo); bis->bisExhausted= 1; return -1; }

	    todo= count- done;

	    memcpy( buffer, bis->bisBytes+ 1, todo );
	    }
	else{
	    if  ( sioInReadBytes( bis->bisBlocked, buffer, todo ) != todo )
		{ LDEB(todo); bis->bisExhausted= 1; return -1; }
	    }

	bis->bisOffset= 1+ todo;
	buffer += todo;
	done += todo;
	}

    return done;
    }

/************************************************************************/
/*									*/
/*  Close a Blocked Input stream.					*/
/*									*/
/*  1)  First drain it.							*/
/*									*/
/************************************************************************/

static int sioInBlockedClose(	void *	voidbis )
    {
    int				rval= 0;
    BlockedInputStream *	bis= (BlockedInputStream *)voidbis;

    /*  1 */
    while ( ! bis->bisExhausted )
	{
	unsigned char	bytes[SIOsizBUF];

	if  ( sioInBlockedReadBytes( voidbis, bytes, SIOsizBUF ) < 0 )
	    { LDEB(1); rval= -1; break;	}
	}

    free( voidbis );

    return rval;
    }

SimpleInputStream * sioInBlockedOpen(	SimpleInputStream *	sisBlocked )
    {
    SimpleInputStream *		sis;
    BlockedInputStream *	bis;

    bis= (BlockedInputStream *)malloc( sizeof(BlockedInputStream) );
    if  ( ! bis )
	{ XDEB(bis); return (SimpleInputStream *)0;	}

    bis->bisBlocked= sisBlocked;
    bis->bisBytes[0]= 0;
    bis->bisOffset= 1;
    bis->bisExhausted= 0;

    sis= sioInOpen( (void *)bis, sioInBlockedReadBytes, sioInBlockedClose );

    if  ( ! sis )
	{ XDEB(sis); free( bis ); return (SimpleInputStream *)0; }

    return sis;
    }

/************************************************************************/
/*									*/
/*  Output.								*/
/*									*/
/************************************************************************/

typedef struct BlockedOutputStream
    {
    SimpleOutputStream *	bosSosBlocked;
    unsigned char		bosBytes[256];
    } BlockedOutputStream;

/************************************************************************/
/*									*/
/*  Close a blocked stream.						*/
/*									*/
/*  1)  If necessary flush the cache.					*/
/*  2)  Should be impossible.						*/
/*  3)  Superfluous, but it feels nice to have predictable values in	*/
/*	the buffer.							*/
/*  4)  Emit the remaining digits and the padding '=' characters.	*/
/*									*/
/************************************************************************/

static int sioOutBlockedClose(	void *	voidbos )
    {
    int				rval= 0;
    BlockedOutputStream *	bos= (BlockedOutputStream *)voidbos;

    /*  1  */
    if  ( bos->bosBytes[0] > 0 )
	{
	if  ( sioOutWriteBytes( bos->bosSosBlocked, bos->bosBytes,
				bos->bosBytes[0]+ 1 ) != bos->bosBytes[0]+ 1 )
	    { LDEB(bos->bosBytes[0]); rval= -1;	}
	}

    sioOutPutCharacter( '\0', bos->bosSosBlocked );

    free( bos );

    return rval;
    }

/************************************************************************/
/*									*/
/*  Write bytes in 255 byte blocks with a length indicator.		*/
/*									*/
/************************************************************************/

static int sioOutBlockedWriteBytes(	void *			voidbos,
					const unsigned char *	buffer,
					int			count )
    {
    BlockedOutputStream *	bos= (BlockedOutputStream *)voidbos;
    int				done= 0;

    while( done < count )
	{
	int		todo;

	todo= count- done;
	if  ( todo > 255- bos->bosBytes[0] )
	    { todo= 255- bos->bosBytes[0]; }

	memcpy( bos->bosBytes+ bos->bosBytes[0]+ 1, buffer, todo );
	bos->bosBytes[0] += todo;

	if  ( bos->bosBytes[0] == 255 )
	    {
	    if  ( sioOutWriteBytes( bos->bosSosBlocked, bos->bosBytes,
				bos->bosBytes[0]+ 1 ) != bos->bosBytes[0]+ 1 )
		{ LDEB(bos->bosBytes[0]); return -1;	}

	    bos->bosBytes[0]= 0;
	    }

	buffer += todo;
	done += todo;
	}

    return count;
    }

SimpleOutputStream * sioOutBlockedOpen(	SimpleOutputStream *	sosBlocked )
    {
    SimpleOutputStream *	sos;
    BlockedOutputStream *	bos;

    bos= (BlockedOutputStream *)malloc( sizeof(BlockedOutputStream) );
    if  ( ! bos )
	{ XDEB(bos); return (SimpleOutputStream *)0;	}

    bos->bosSosBlocked= sosBlocked;
    bos->bosBytes[0]= 0;

    sos= sioOutOpen( (void *)bos, sioOutBlockedWriteBytes,
					(SIOoutSEEK)0, sioOutBlockedClose );

    if  ( ! sos )
	{ XDEB(sos); free( bos ); return (SimpleOutputStream *)0; }

    return sos;
    }