1
/* Copyright (c) 1996-2004, Adaptec Corporation
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions are met:
7
* - Redistributions of source code must retain the above copyright notice, this
8
* list of conditions and the following disclaimer.
9
* - Redistributions in binary form must reproduce the above copyright notice,
10
* this list of conditions and the following disclaimer in the documentation
11
* and/or other materials provided with the distribution.
12
* - Neither the name of the Adaptec Corporation nor the names of its
13
* contributors may be used to endorse or promote products derived from this
14
* software without specific prior written permission.
16
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26
* POSSIBILITY OF SUCH DAMAGE.
30
//***************************************************************************
34
// This file contains function definitions for the dptBuffer_S
37
//Author: Doug Anderson
45
//***************************************************************************
48
//Include Files -------------------------------------------------------------
53
//Function - dptBuffer_S::extract() - start
54
//===========================================================================
58
// This function reads a block of data from the next available location
59
//in the DPT buffer. This function can be called repetitively to read
60
//multiple data blocks from a single DPT buffer.
66
// 0 = Extraction truncated
67
// 1 = Success - all requested bytes were extracted
69
//Global Variables Affected:
71
//Remarks: (Side effects, Assumptions, Warnings...)
74
//---------------------------------------------------------------------------
76
uSHORT dptBuffer_S::extract(void *inData_P,uLONG dataSize)
80
// If there is any space left...
81
if (writeIndex>readIndex) {
82
// Determine how many bytes have not been read...
83
uLONG numLeft = writeIndex - readIndex;
84
// Determine how many bytes to read from the buffer
85
numToCopy = (numLeft > dataSize) ? dataSize : numLeft;
86
// Copy the data from the I/O buffer to the specified buffer
87
memcpy(inData_P,data+readIndex,(uINT)numToCopy);
88
// Increment the read index
89
readIndex += numToCopy;
92
return (numToCopy==dataSize);
95
//dptBuffer_S::extract() - end
98
//Function - dptBuffer_S::skip() - start
99
//===========================================================================
103
// This function increments the read index by the specified amount
104
//without extracting any information.
110
// 0 = Extraction truncated
111
// 1 = Success - all requested bytes were extracted
113
//Global Variables Affected:
115
//Remarks: (Side effects, Assumptions, Warnings...)
118
//---------------------------------------------------------------------------
120
uSHORT dptBuffer_S::skip(uLONG dataSize)
124
// If there is any space left...
125
if (writeIndex>readIndex) {
126
// Determine how many bytes have not been read...
127
uLONG numLeft = writeIndex - readIndex;
128
// Determine how many bytes may be skipped
129
numToCopy = (numLeft>dataSize) ? dataSize : numLeft;
130
// Increment the read index
131
readIndex += numToCopy;
134
return (numToCopy==dataSize);
137
//dptBuffer_S::skip() - end
140
//Function - dptBuffer_S::insert() - start
141
//===========================================================================
145
// This function writes a block of data to the next available location
146
//in the DPT buffer. This function can be called repetitively to read
147
//multiple data blocks from a single DPT buffer.
153
// 0 = Insertion incomplete
154
// 1 = Success - all requested bytes were inserted
156
//Global Variables Affected:
158
//Remarks: (Side effects, Assumptions, Warnings...)
161
//---------------------------------------------------------------------------
163
uSHORT dptBuffer_S::insert(void *inData_P,uLONG dataSize)
167
// If there is any room left...
168
if (allocSize>writeIndex) {
169
// Determine how many bytes are left in the data buffer
170
uLONG numLeft = allocSize - writeIndex;
171
// Determine how many bytes may be copied into the buffer
172
numToCopy = (numLeft < dataSize) ? numLeft : dataSize;
173
// Copy the data into the buffer
174
memcpy(data+writeIndex,inData_P,(uINT)numToCopy);
175
// Increment the write index
176
writeIndex += numToCopy;
179
return (numToCopy==dataSize);
182
//dptBuffer_S::insert() - end
185
//Function - dptBuffer_S::setExtractSize() - start
186
//===========================================================================
190
// This function sets the number of bytes that can be read from
197
//Global Variables Affected:
199
//Remarks: (Side effects, Assumptions, Warnings...)
201
// 1. This function should be used sparingly. This function is
202
// provided for use in situations where an the reset(),insert()
203
// procedure was not used to initialize the buffer.
205
//---------------------------------------------------------------------------
207
void dptBuffer_S::setExtractSize(uLONG inSize)
210
// Take the minimum of the buffer size and the request size
211
writeIndex = (allocSize > inSize) ? inSize : allocSize;
214
//dptBuffer_S::setExtractSize() - end
217
//Function - dptBuffer_S::newBuffer() - start
218
//===========================================================================
222
// This function allocates a new DPT I/O buffer with the specified
223
//data buffer size and initializes the buffer header.
229
//Global Variables Affected:
231
//Remarks: (Side effects, Assumptions, Warnings...)
234
//---------------------------------------------------------------------------
236
dptBuffer_S * dptBuffer_S::newBuffer(uLONG size)
239
dptBuffer_S *buff_P = NULL;
242
// If a non-zero data buffer has been requested...
246
// Set up the alloc size to include the buffer header
248
AllocSize = sizeof(dptBuffer_S)-1 + size;
251
// Now lets make sure the size is an even multiple of 16, and
252
// just for good measure we will buffer it by 16 for all of those
253
// environments that have a problem with alignment
255
AllocSize = (AllocSize & 0xfffffff0) + 0x10;
257
// Allocate a new I/O buffer of the specified size
258
buff_P = (dptBuffer_S *) new uCHAR[AllocSize];
259
// If the buffer was allocated...
260
if (buff_P != NULL) {
261
// Initialize the buffer header
263
buff_P->allocSize = size;
264
buff_P->readIndex = 0;
265
buff_P->writeIndex = 0;
267
// Clear the data buffer
268
memset(buff_P->data,0,(uINT)size);
275
//dptBuffer_S::newBuffer() - end
278
//Function - dptBuffer_S::delBuffer() - start
279
//===========================================================================
283
// This function frees an I/O buffer that was allocated with'
290
//Global Variables Affected:
292
//Remarks: (Side effects, Assumptions, Warnings...)
295
//---------------------------------------------------------------------------
297
void dptBuffer_S::delBuffer(dptBuffer_S *buff_P)
300
// If a valid I/O buffer was specified...
302
// Delete the buffer as a uCHAR array
303
delete[] ((uCHAR *)buff_P);
306
//dptBuffer_S::delBuffer() - end
308
//Function - dptBuffer_S::netInsert() - start
309
//===========================================================================
313
// This function is used to place inData into DPT network order
314
//(byte swapping if necessary) and then call insert() to place the
315
//value in the DPT buffer. Apart from the possible byte swapping, this
316
//function should behave identically to insert().
322
// 0 = Extraction truncated
323
// 1 = Success - all requested bytes were extracted
325
//Global Variables Affected:
327
//Remarks: (Side effects, Assumptions, Warnings...)
330
//---------------------------------------------------------------------------
332
uSHORT dptBuffer_S::netInsert(uLONG inData)
334
uLONG temp = NET_SWAP_4(inData);
336
return (insert(&temp, sizeof(uLONG)));
339
uSHORT dptBuffer_S::netInsert(uSHORT inData)
341
uSHORT temp = NET_SWAP_2(inData);
343
return (insert(&temp, sizeof(uSHORT)));
345
//dptBuffer_S::netInsert() - end
348
//Function - dptBuffer_S::netExtract() - start
349
//===========================================================================
353
// This function is used to call extract() to remove data from the DPT
354
//buffer (which is in DPT network order) and then return the data in host
355
//byte order (byte swapping if necessary). Apart from the possible byte
356
//swapping, this function should behave identically to extract().
362
// 0 = Extraction truncated
363
// 1 = Success - all requested bytes were extracted
365
//Global Variables Affected:
367
//Remarks: (Side effects, Assumptions, Warnings...)
370
//---------------------------------------------------------------------------
372
uSHORT dptBuffer_S::netExtract(uLONG &inData)
374
uSHORT retVal = extract(&inData, sizeof(uLONG));
376
inData = NET_SWAP_4(inData);
378
// return the result of the call to extract()
382
uSHORT dptBuffer_S::netExtract(long &inData)
384
uSHORT retVal = extract(&inData, sizeof(long));
386
inData = NET_SWAP_4(inData);
388
// return the result of the call to extract()
392
uSHORT dptBuffer_S::netExtract(uSHORT &inData)
394
uSHORT retVal = extract(&inData, sizeof(uSHORT));
396
inData = NET_SWAP_2(inData);
398
// return the result of the call to extract()
402
uSHORT dptBuffer_S::netExtract(short &inData)
404
uSHORT retVal = extract(&inData, sizeof(short));
406
inData = NET_SWAP_2(inData);
408
// return the result of the call to extract()
411
//dptBuffer_S::netExtract() - end