~ubuntu-branches/ubuntu/precise/tidy/precise-security

« back to all changes in this revision

Viewing changes to src/buffio.c

  • Committer: Bazaar Package Importer
  • Author(s): Jason Thomas
  • Date: 2005-04-20 11:22:49 UTC
  • mto: (3.1.1 lenny)
  • mto: This revision was merged to the branch mainline in revision 2.
  • Revision ID: james.westby@ubuntu.com-20050420112249-epdnkgi03ubep83z
Tags: upstream-20050415
ImportĀ upstreamĀ versionĀ 20050415

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* buffio.c -- Treat buffer as an I/O stream.
 
2
 
 
3
  (c) 1998-2005 (W3C) MIT, ERCIM, Keio University
 
4
  See tidy.h for the copyright notice.
 
5
 
 
6
  CVS Info :
 
7
 
 
8
    $Author: arnaud02 $ 
 
9
    $Date: 2005/04/08 09:11:13 $ 
 
10
    $Revision: 1.8 $ 
 
11
 
 
12
  Requires buffer to automatically grow as bytes are added.
 
13
  Must keep track of current read and write points.
 
14
 
 
15
*/
 
16
 
 
17
#include "tidy.h"
 
18
#include "buffio.h"
 
19
 
 
20
 
 
21
/**************
 
22
   TIDY
 
23
**************/
 
24
 
 
25
static int TIDY_CALL insrc_getByte( ulong appData )
 
26
{
 
27
  TidyBuffer* buf = (TidyBuffer*) appData;
 
28
  return tidyBufGetByte( buf );
 
29
}
 
30
static Bool TIDY_CALL insrc_eof( ulong appData )
 
31
{
 
32
  TidyBuffer* buf = (TidyBuffer*) appData;
 
33
  return tidyBufEndOfInput( buf );
 
34
}
 
35
static void TIDY_CALL insrc_ungetByte( ulong appData, byte bv )
 
36
{
 
37
  TidyBuffer* buf = (TidyBuffer*) appData;
 
38
  tidyBufUngetByte( buf, bv );
 
39
}
 
40
 
 
41
void TIDY_CALL initInputBuffer( TidyInputSource* inp, TidyBuffer* buf )
 
42
{
 
43
  inp->getByte    = insrc_getByte;
 
44
  inp->eof        = insrc_eof;
 
45
  inp->ungetByte  = insrc_ungetByte;
 
46
  inp->sourceData = (ulong) buf;
 
47
}
 
48
 
 
49
static void TIDY_CALL outsink_putByte( ulong appData, byte bv )
 
50
{
 
51
  TidyBuffer* buf = (TidyBuffer*) appData;
 
52
  tidyBufPutByte( buf, bv );
 
53
}
 
54
 
 
55
void TIDY_CALL initOutputBuffer( TidyOutputSink* outp, TidyBuffer* buf )
 
56
{
 
57
  outp->putByte  = outsink_putByte;
 
58
  outp->sinkData = (ulong) buf;
 
59
}
 
60
 
 
61
 
 
62
void TIDY_CALL tidyBufInit( TidyBuffer* buf )
 
63
{
 
64
    assert( buf != NULL );
 
65
    ClearMemory( buf, sizeof(TidyBuffer) );
 
66
}
 
67
 
 
68
void TIDY_CALL tidyBufAlloc( TidyBuffer* buf, uint allocSize )
 
69
{
 
70
    tidyBufInit( buf );
 
71
    tidyBufCheckAlloc( buf, allocSize, 0 );
 
72
    buf->next = 0;
 
73
}
 
74
void TIDY_CALL tidyBufFree( TidyBuffer* buf )
 
75
{
 
76
    assert( buf != NULL );
 
77
    MemFree( buf->bp );
 
78
    tidyBufInit( buf );
 
79
}
 
80
 
 
81
void TIDY_CALL tidyBufClear( TidyBuffer* buf )
 
82
{
 
83
    assert( buf != NULL );
 
84
    if ( buf->bp )
 
85
    {
 
86
        ClearMemory( buf->bp, buf->allocated );
 
87
        buf->size = 0;
 
88
    }
 
89
    buf->next = 0;
 
90
}
 
91
 
 
92
/* Avoid thrashing memory by doubling buffer size
 
93
** until larger than requested size.
 
94
*/
 
95
void TIDY_CALL tidyBufCheckAlloc( TidyBuffer* buf, uint allocSize, uint chunkSize )
 
96
{
 
97
    assert( buf != NULL );
 
98
    if ( 0 == chunkSize )
 
99
        chunkSize = 256;
 
100
    if ( allocSize > buf->allocated )
 
101
    {
 
102
        byte* bp;
 
103
        uint allocAmt = chunkSize;
 
104
        if ( buf->allocated > 0 )
 
105
            allocAmt = buf->allocated;
 
106
        while ( allocAmt < allocSize )
 
107
            allocAmt *= 2;
 
108
 
 
109
        bp = (byte*)MemRealloc( buf->bp, allocAmt );
 
110
        if ( bp != NULL )
 
111
        {
 
112
            ClearMemory( bp + buf->allocated, allocAmt - buf->allocated );
 
113
            buf->bp = bp;
 
114
            buf->allocated = allocAmt;
 
115
        }
 
116
    }
 
117
}
 
118
 
 
119
/* Attach buffer to a chunk O' memory w/out allocation */
 
120
void  TIDY_CALL tidyBufAttach( TidyBuffer* buf, byte* bp, uint size )
 
121
{
 
122
    assert( buf != NULL );
 
123
    buf->bp = bp;
 
124
    buf->size = buf->allocated = size;
 
125
    buf->next = 0;
 
126
}
 
127
 
 
128
/* Clear pointer to memory w/out deallocation */
 
129
void TIDY_CALL tidyBufDetach( TidyBuffer* buf )
 
130
{
 
131
    tidyBufInit( buf );
 
132
}
 
133
 
 
134
 
 
135
/**************
 
136
   OUTPUT
 
137
**************/
 
138
 
 
139
void TIDY_CALL tidyBufAppend( TidyBuffer* buf, void* vp, uint size )
 
140
{
 
141
    assert( buf != NULL );
 
142
    if ( vp != NULL && size > 0 )
 
143
    {
 
144
        tidyBufCheckAlloc( buf, buf->size + size, 0 );
 
145
        memcpy( buf->bp + buf->size, vp, size );
 
146
        buf->size += size;
 
147
    }
 
148
}
 
149
 
 
150
void TIDY_CALL tidyBufPutByte( TidyBuffer* buf, byte bv )
 
151
{
 
152
    assert( buf != NULL );
 
153
    tidyBufCheckAlloc( buf, buf->size + 1, 0 );
 
154
    buf->bp[ buf->size++ ] = bv;
 
155
}
 
156
 
 
157
 
 
158
int TIDY_CALL tidyBufPopByte( TidyBuffer* buf )
 
159
{
 
160
    int bv = EOF;
 
161
    assert( buf != NULL );
 
162
    if ( buf->size > 0 )
 
163
      bv = buf->bp[ --buf->size ];
 
164
    return bv;
 
165
}
 
166
 
 
167
/**************
 
168
   INPUT
 
169
**************/
 
170
 
 
171
int TIDY_CALL tidyBufGetByte( TidyBuffer* buf )
 
172
{
 
173
    int bv = EOF;
 
174
    if ( ! tidyBufEndOfInput(buf) )
 
175
      bv = buf->bp[ buf->next++ ];
 
176
    return bv;
 
177
}
 
178
 
 
179
Bool TIDY_CALL tidyBufEndOfInput( TidyBuffer* buf )
 
180
{
 
181
    return ( buf->next >= buf->size );
 
182
}
 
183
 
 
184
void TIDY_CALL tidyBufUngetByte( TidyBuffer* buf, byte bv )
 
185
{
 
186
    if ( buf->next > 0 )
 
187
    {
 
188
        --buf->next;
 
189
        assert( bv == buf->bp[ buf->next ] );
 
190
    }
 
191
}
 
192