-
Notifications
You must be signed in to change notification settings - Fork 26
/
framePackets.C
268 lines (227 loc) · 6.47 KB
/
framePackets.C
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
** framePackets.C
**
** Author: $Author: phnxbld $
** Date: $Date: 2009/08/19 12:30:51 $
**
** $Log: framePackets.C,v $
** Revision 1.3 2009/08/19 12:30:51 phnxbld
** fix check on failure
**
** Revision 1.2 2002/05/31 22:15:42 purschke
** mlp -- went through the insure report and tried to fix
** every little problem there is, unused variables, dead statements,
** all. It'll probably take another round to complete, but it should get
** rid of most warnings.
**
** Revision 1.1.1.1 2000/07/21 01:51:13 purschke
** mlp -- adding the new automakified "basic" module to CVS.
**
**
** Revision 1.6 1998/12/23 20:34:57 markacs
** fixed findFramePacketId so it returns ptrFailure if called for an index of 0 in the case that there are no packets in the frame
**
** Revision 1.5 1998/12/11 22:02:04 markacs
** (stephen markacs) adding log into cvs tags
**
*/
/*
** Routines that perform functions on packets with knowledge of the
** frame that the packets reside in.
**
**/
#include "framePackets.h"
#include "Cframe.h"
#include "Cpacket.h"
#include "packetPublic.h"
#include "framePublic.h"
#include "frameRoutines.h"
#include "packetRoutines.h"
#include "formatError.h"
PACKET_ptr appendEmptyFramePacket (FRAME_ptr frame_ptr, PHDWORD maxFrameLength, UINT packetId)
{
UINT extendDwords;
UINT maxPacketLength;
PACKET_ptr newPacket_ptr;
/*
** Check for valid frame header
*/
if (!validFrameHdr (frame_ptr))
return ptrFailure;
/*
** Now extend the frame to hold the new packet header
*/
extendDwords = extendFrameData(frame_ptr, maxFrameLength, packetV1HdrLength);
if (extendDwords == valueFailure) {
setUserError(FORMAT_ERR_BUFFER_OVERFLOW, maxFrameLength);
return ptrFailure;
}
/*
** Get pointer to data section
*/
newPacket_ptr = findFrameDataStart(frame_ptr);
/*
** Now make the empty packet
*/
maxPacketLength = maxFrameLength - (UINT) (newPacket_ptr - frame_ptr);
makeEmptyPacket(newPacket_ptr, maxPacketLength, packetId);
setPacketSuccess();
return newPacket_ptr;
}
LOGIC_ret isLastFramePacket (FRAME_ptr frame_ptr, PACKET_ptr packet_ptr)
{
PACKET_ptr lastPacket_ptr;
/*
** Check for valid frame, packet
*/
if (!validFrameHdr (frame_ptr)) return FALSE;
if (!validPacketHdr (frame_ptr)) return FALSE;
/*
** Now find the last packet
*/
lastPacket_ptr = findLastFramePacket (frame_ptr);
return (lastPacket_ptr == packet_ptr);
}
/*
** Return the index'th packet in the frame where the indexing starts from 0.
** (i.e. findFramePacketIndex(frame_ptr, 0) returns the first packet)
*/
PACKET_ptr findFramePacketIndex(FRAME_ptr frame_ptr, UINT index)
{
PACKET_ptr testPacket_ptr;
/*
** Check first for valid frame header
*/
if (!validFrameHdr (frame_ptr)) {
return ptrFailure;
}
/*
** Point to first packet
*/
testPacket_ptr = (PACKET_ptr) findFrameDataStart (frame_ptr);
if (index > 0) {
UINT iPacket;
/*
** Loop the necessary number of times but check to make sure we have valid pointer
*/
for (iPacket=0; iPacket<index ; iPacket++) {
testPacket_ptr = findNextFramePacket (frame_ptr, testPacket_ptr);
if (testPacket_ptr == ptrFailure) {
return ptrFailure;
}
}
}
else /* if index=0 make sure there's a packet there */
{
if (!validPacketHdr(testPacket_ptr)) return ptrFailure;
}
/*
** If we get here we have the requested packet pointer
*/
return testPacket_ptr;
}
/*
** Return a pointer to the first packet in the frame
*/
PACKET_ptr findFirstFramePacket(FRAME_ptr frame_ptr)
{
return findFramePacketIndex(frame_ptr, 0);
}
/*
** Return a pointer to the first packet in the frame
*/
PACKET_ptr findLastFramePacket(FRAME_ptr frame_ptr)
{
PACKET_ptr thisPacket_ptr, nextPacket_ptr;
/*
** Find the first packet
*/
if (!(nextPacket_ptr = findFirstFramePacket (frame_ptr))) return ptrFailure;
/*
** Now iterate to the end
*/
while (nextPacket_ptr != ptrFailure) {
thisPacket_ptr = nextPacket_ptr;
nextPacket_ptr = findNextFramePacket (frame_ptr, nextPacket_ptr);
}
return thisPacket_ptr;
}
/*
** findFramePacketID
**
** Find a packet with a specified packet ID in a frame starting at the
** packet in the frame. If no packet with the ID is found ptrFailure is returned.
**
*/
PACKET_ptr findFramePacketId (FRAME_ptr frame_ptr, UINT packetId)
{
PACKET_ptr testPacket_ptr;
/*
** Check to make sure we have a good frame
*/
if (!validFrameHdr (frame_ptr)) {
return ptrFailure;
}
/*
** Point to first packet
*/
testPacket_ptr = (PACKET_ptr) findFrameDataStart (frame_ptr);
/*
** Now iterate until we fail to get enxt packet or find the one we want
*/
while ( (testPacket_ptr != ptrFailure) && (getPacketId(testPacket_ptr) != packetId) )
testPacket_ptr = findNextFramePacket (frame_ptr, testPacket_ptr);
return testPacket_ptr;
}
PACKET_ptr findNextFramePacketId (FRAME_ptr frame_ptr, PACKET_ptr previousPacket_ptr, UINT packetId)
{
/*
** BEWARE !!! No check on valid frame once we're past the first packet.
*/
/*
** Skip the "previous" packet
*/
PACKET_ptr testPacket_ptr = findNextFramePacket(frame_ptr, previousPacket_ptr);
/*
** Now iterate until we fail to get enxt packet or find the one we want
*/
while ( (testPacket_ptr != ptrFailure) && (getPacketId(testPacket_ptr) != packetId) )
testPacket_ptr = findNextFramePacket (frame_ptr, testPacket_ptr);
return testPacket_ptr;
}
/*
** Private routine to search for a packet in a unbroken data block of packets
*/
PACKET_ptr findNextFramePacket (FRAME_ptr frame_ptr, PACKET_ptr currentPacket_ptr)
{
PACKET_ptr nextPacket_ptr;
/*
** Figure out where the frame ends
*/
PHDWORD* frameDataStart_ptr = findFrameDataStart(frame_ptr);
PHDWORD* frameDataEnd_ptr = findFrameDataEnd(frame_ptr);
/*
** Check for valid current packet pointer
*/
if ((currentPacket_ptr < frameDataStart_ptr) || (currentPacket_ptr > frameDataEnd_ptr)) {
return ptrFailure;
}
/*
** Now skip past "current" packet
*/
nextPacket_ptr = findPacketEnd(currentPacket_ptr) + 1;
if (nextPacket_ptr > frameDataEnd_ptr) return ptrFailure;
else return nextPacket_ptr;
}
/*
** routine to find the number of packets in a frame
*/
VALUE_ret frameNumPackets (FRAME_ptr frame_ptr)
{
UINT numPackets = 1;
PACKET_ptr packet_ptr = findFirstFramePacket(frame_ptr);
if (packet_ptr == ptrFailure) return 0;
while ( (packet_ptr = findNextFramePacket(frame_ptr,packet_ptr)) != ptrFailure )
numPackets++;
return numPackets;
}