File: sioHex.c

package info (click to toggle)
ted 2.11-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 11,064 kB
  • ctags: 13,935
  • sloc: ansic: 120,446; makefile: 7,469; sh: 253
file content (146 lines) | stat: -rw-r--r-- 3,332 bytes parent folder | download
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
/************************************************************************/
/*									*/
/*  Simple io streams, recursive use for hexadecimal data.		*/
/*									*/
/************************************************************************/

#   include	"appUtilConfig.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 sioHexClose( 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, (SIOinSEEK)0, sioHexClose );

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

    return sis;
    }

static int sioOutHexWriteBytes(	void *			voidsos,
				const unsigned char *	buffer,
				int			count )
    {
    SimpleOutputStream *	sos= (SimpleOutputStream *)voidsos;
    int				done= 0;

    while( done < count )
	{
	sioOutPutCharacter( SioHexDigitsL[ ( (*buffer) >> 4 ) & 0x0f ], sos );
	sioOutPutCharacter( SioHexDigitsL[ ( (*buffer) >> 0 ) & 0x0f ], sos );

	buffer++; done++;
	}

    return count;
    }

static int sioHexSeek(		void *			voidsos,
				long			pos )
    {
    SimpleOutputStream *	sos= (SimpleOutputStream *)voidsos;

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

    return 0;
    }

SimpleOutputStream * sioOutHexOpen(	SimpleOutputStream *	sosHex )
    {
    SimpleOutputStream *	sos;

    sos= sioOutOpen( (void *)sosHex, sioOutHexWriteBytes,
						sioHexSeek, sioHexClose );

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

    return sos;
    }