File: sioHex.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 (210 lines) | stat: -rw-r--r-- 4,923 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
/************************************************************************/
/*									*/
/*  Simple io streams, recursive use for hexadecimal data.		*/
/*									*/
/*  Do not forget to manually add the '>' at the end if you use these	*/
/*  streams in conjuntion with '/ASCIIHexDecode filter' in PostScript.	*/
/*									*/
/************************************************************************/

#   include	"appUtilConfig.h"

#   include	<stdlib.h>

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


/************************************************************************/
/*									*/
/*  For Hexadecimal decoding.						*/
/*									*/
/************************************************************************/

static const unsigned char	SioHexDigitsU[]= "0123456789ABCDEF";
static const unsigned char	SioHexDigitsL[]= "0123456789abcdef";
static unsigned char		SioHexIndices[256];

/************************************************************************/
/*									*/
/*  Exchange of hexed binary data.					*/
/*									*/
/************************************************************************/

static int sioInHexClose( void *	voids )
    { return 0;	}

static int sioInHexReadBytes(	void *		voidsis,
				unsigned char *	buffer,
				int		count )
    {
    SimpleInputStream *	sis= (SimpleInputStream *)voidsis;
    int			done= 0;

    while( done < count )
	{
	int		c;

	c= sioInGetCharacter( sis );

	while( c == ' ' || c == '\r' || c == '\n' )
	    { c= sioInGetCharacter( sis );	}

	if  ( c == EOF )
	    { return done;	}
	else{
	    if  ( SioHexIndices[c] == 0xff )
		{ sioInUngetLastRead( sis ); return done;	}
	    else{ buffer[0]= SioHexIndices[c];			}
	    }

	c= sioInGetCharacter( sis );

	buffer[0] *= 16;

	while( c == ' ' || c == '\r' || c == '\n' )
	    { c= sioInGetCharacter( sis );	}

	if  ( c == EOF )
	    { LDEB(c); return -1;			}

	if  ( SioHexIndices[c] == 0xff )
	    { CDEB(c); return -1;			}
	else{ buffer[0] += SioHexIndices[c];		}

	buffer++; done++;
	}

    return done;
    }


SimpleInputStream * sioInHexOpen(	SimpleInputStream *	sisHex )
    {
    SimpleInputStream *	sis;

    if  ( SioHexIndices[0] == 0 )
	{
	int	i;

	for ( i= 0; i < sizeof(SioHexIndices); i++ )
	    { SioHexIndices[i]= 0xff;	}

	i= 0;
	while( SioHexDigitsU[i] )
	    { SioHexIndices[SioHexDigitsU[i]]= i; i++;	}
	i= 0;
	while( SioHexDigitsL[i] )
	    { SioHexIndices[SioHexDigitsL[i]]= i; i++;	}
	}

    sis= sioInOpen( (void *)sisHex, sioInHexReadBytes, sioInHexClose );

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

    return sis;
    }

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

typedef struct HexOutputStream
    {
    SimpleOutputStream *	hosSosHex;
    int				hosWide;
    int				hosLastNl;
    int				hosColumn;
    } HexOutputStream;

static int sioOutHexClose( void *	voidhos )
    {
    HexOutputStream *		hos= (HexOutputStream *)voidhos;
    SimpleOutputStream *	sos= hos->hosSosHex;

    if  ( hos->hosWide > 0 && hos->hosLastNl && hos->hosColumn > 0 )
	{
	sioOutPutCharacter( '\n', sos );
	hos->hosColumn= 0;
	}

    free( hos );

    return 0;
    }

static int sioOutHexWriteBytes(	void *			voidhos,
				const unsigned char *	buffer,
				int			count )
    {
    HexOutputStream *		hos= (HexOutputStream *)voidhos;
    SimpleOutputStream *	sos= hos->hosSosHex;
    int				done= 0;

    while( done < count )
	{
	if  ( hos->hosWide > 0 && hos->hosColumn >= hos->hosWide )
	    {
	    sioOutPutCharacter( '\n', sos );
	    hos->hosColumn= 0;
	    }

	sioOutPutCharacter( SioHexDigitsL[ ( (*buffer) >> 4 ) & 0x0f ], sos );
	sioOutPutCharacter( SioHexDigitsL[ ( (*buffer) >> 0 ) & 0x0f ], sos );

	if  ( hos->hosWide > 0 )
	    { hos->hosColumn += 2;	}

	buffer++; done++;
	}

    return count;
    }

static int sioHexSeek(		void *			voidhos,
				long			pos )
    {
    HexOutputStream *		hos= (HexOutputStream *)voidhos;

    if  ( hos->hosWide > 0 )
	{ LDEB(hos->hosWide); return -1;	}

    if  ( sioOutSeek( hos->hosSosHex, 2* pos ) )
	{ LDEB(0); return -1;	}

    return 0;
    }

SimpleOutputStream * sioOutHexOpenFolded(
				    SimpleOutputStream *	sosHex,
				    int				wide,
				    int				lastNl )
    {
    HexOutputStream *		hos= malloc( sizeof(HexOutputStream) );
    SimpleOutputStream *	sos;

    if  ( ! hos )
	{ XDEB(hos); return (SimpleOutputStream *)0;	}

    hos->hosSosHex= sosHex;
    hos->hosWide= wide;
    hos->hosLastNl= lastNl;
    hos->hosColumn= 0;

    sos= sioOutOpen( (void *)hos, sioOutHexWriteBytes,
						sioHexSeek, sioOutHexClose );

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

    return sos;
    }

SimpleOutputStream * sioOutHexOpen(	SimpleOutputStream *	sosHex )
    {
    return sioOutHexOpenFolded( sosHex, 0, 0 );
    }