aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/llmessage/lldatapacker.h
diff options
context:
space:
mode:
authorJacek Antonelli2008-08-15 23:44:46 -0500
committerJacek Antonelli2008-08-15 23:44:46 -0500
commit38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4 (patch)
treeadca584755d22ca041a2dbfc35d4eca01f70b32c /linden/indra/llmessage/lldatapacker.h
parentREADME.txt (diff)
downloadmeta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.zip
meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.gz
meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.bz2
meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.xz
Second Life viewer sources 1.13.2.12
Diffstat (limited to 'linden/indra/llmessage/lldatapacker.h')
-rw-r--r--linden/indra/llmessage/lldatapacker.h417
1 files changed, 417 insertions, 0 deletions
diff --git a/linden/indra/llmessage/lldatapacker.h b/linden/indra/llmessage/lldatapacker.h
new file mode 100644
index 0000000..4e0b4cd
--- /dev/null
+++ b/linden/indra/llmessage/lldatapacker.h
@@ -0,0 +1,417 @@
1/**
2 * @file lldatapacker.h
3 * @brief Data packer declaration for tightly storing binary data.
4 *
5 * Copyright (c) 2002-2007, Linden Research, Inc.
6 *
7 * The source code in this file ("Source Code") is provided by Linden Lab
8 * to you under the terms of the GNU General Public License, version 2.0
9 * ("GPL"), unless you have obtained a separate licensing agreement
10 * ("Other License"), formally executed by you and Linden Lab. Terms of
11 * the GPL can be found in doc/GPL-license.txt in this distribution, or
12 * online at http://secondlife.com/developers/opensource/gplv2
13 *
14 * There are special exceptions to the terms and conditions of the GPL as
15 * it is applied to this Source Code. View the full text of the exception
16 * in the file doc/FLOSS-exception.txt in this software distribution, or
17 * online at http://secondlife.com/developers/opensource/flossexception
18 *
19 * By copying, modifying or distributing this software, you acknowledge
20 * that you have read and understood your obligations described above,
21 * and agree to abide by those obligations.
22 *
23 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
24 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
25 * COMPLETENESS OR PERFORMANCE.
26 */
27
28#ifndef LL_LLDATAPACKER_H
29#define LL_LLDATAPACKER_H
30
31#include <stdio.h>
32#include <iostream>
33
34#include "llerror.h"
35
36class LLColor4;
37class LLColor4U;
38class LLVector2;
39class LLVector3;
40class LLVector4;
41class LLUUID;
42
43class LLDataPacker
44{
45public:
46 virtual ~LLDataPacker() {}
47
48 virtual void reset() { llerrs << "Using unimplemented datapacker reset!" << llendl; };
49 virtual void dumpBufferToLog() { llerrs << "dumpBufferToLog not implemented for this type!" << llendl; }
50
51 virtual BOOL hasNext() const = 0;
52
53 virtual BOOL packString(const char *value, const char *name) = 0;
54 virtual BOOL unpackString(std::string& value, const char *name) = 0;
55
56 virtual BOOL packBinaryData(const U8 *value, S32 size, const char *name) = 0;
57 virtual BOOL unpackBinaryData(U8 *value, S32 &size, const char *name) = 0;
58
59 // Constant size binary data packing
60 virtual BOOL packBinaryDataFixed(const U8 *value, S32 size, const char *name) = 0;
61 virtual BOOL unpackBinaryDataFixed(U8 *value, S32 size, const char *name) = 0;
62
63 virtual BOOL packU8(const U8 value, const char *name) = 0;
64 virtual BOOL unpackU8(U8 &value, const char *name) = 0;
65
66 virtual BOOL packU16(const U16 value, const char *name) = 0;
67 virtual BOOL unpackU16(U16 &value, const char *name) = 0;
68
69 virtual BOOL packU32(const U32 value, const char *name) = 0;
70 virtual BOOL unpackU32(U32 &value, const char *name) = 0;
71
72 virtual BOOL packS32(const S32 value, const char *name) = 0;
73 virtual BOOL unpackS32(S32 &value, const char *name) = 0;
74
75 virtual BOOL packF32(const F32 value, const char *name) = 0;
76 virtual BOOL unpackF32(F32 &value, const char *name) = 0;
77
78 // Packs a float into an integer, using the given size
79 // and picks the right U* data type to pack into.
80 BOOL packFixed(const F32 value, const char *name,
81 const BOOL is_signed, const U32 int_bits, const U32 frac_bits);
82 BOOL unpackFixed(F32 &value, const char *name,
83 const BOOL is_signed, const U32 int_bits, const U32 frac_bits);
84
85 virtual BOOL packColor4(const LLColor4 &value, const char *name) = 0;
86 virtual BOOL unpackColor4(LLColor4 &value, const char *name) = 0;
87
88 virtual BOOL packColor4U(const LLColor4U &value, const char *name) = 0;
89 virtual BOOL unpackColor4U(LLColor4U &value, const char *name) = 0;
90
91 virtual BOOL packVector2(const LLVector2 &value, const char *name) = 0;
92 virtual BOOL unpackVector2(LLVector2 &value, const char *name) = 0;
93
94 virtual BOOL packVector3(const LLVector3 &value, const char *name) = 0;
95 virtual BOOL unpackVector3(LLVector3 &value, const char *name) = 0;
96
97 virtual BOOL packVector4(const LLVector4 &value, const char *name) = 0;
98 virtual BOOL unpackVector4(LLVector4 &value, const char *name) = 0;
99
100 virtual BOOL packUUID(const LLUUID &value, const char *name) = 0;
101 virtual BOOL unpackUUID(LLUUID &value, const char *name) = 0;
102 U32 getPassFlags() const { return mPassFlags; }
103 void setPassFlags(U32 flags) { mPassFlags = flags; }
104protected:
105 LLDataPacker();
106protected:
107 U32 mPassFlags;
108 BOOL mWriteEnabled; // disable this to do things like determine filesize without actually copying data
109};
110
111class LLDataPackerBinaryBuffer : public LLDataPacker
112{
113public:
114 LLDataPackerBinaryBuffer(U8 *bufferp, S32 size)
115 : LLDataPacker(),
116 mBufferp(bufferp),
117 mCurBufferp(bufferp),
118 mBufferSize(size)
119 {
120 mWriteEnabled = TRUE;
121 }
122
123 LLDataPackerBinaryBuffer()
124 : LLDataPacker(),
125 mBufferp(NULL),
126 mCurBufferp(NULL),
127 mBufferSize(0)
128 {
129 }
130
131 /*virtual*/ BOOL packString(const char *value, const char *name);
132 /*virtual*/ BOOL unpackString(std::string& value, const char *name);
133
134 /*virtual*/ BOOL packBinaryData(const U8 *value, S32 size, const char *name);
135 /*virtual*/ BOOL unpackBinaryData(U8 *value, S32 &size, const char *name);
136
137 // Constant size binary data packing
138 /*virtual*/ BOOL packBinaryDataFixed(const U8 *value, S32 size, const char *name);
139 /*virtual*/ BOOL unpackBinaryDataFixed(U8 *value, S32 size, const char *name);
140
141 /*virtual*/ BOOL packU8(const U8 value, const char *name);
142 /*virtual*/ BOOL unpackU8(U8 &value, const char *name);
143
144 /*virtual*/ BOOL packU16(const U16 value, const char *name);
145 /*virtual*/ BOOL unpackU16(U16 &value, const char *name);
146
147 /*virtual*/ BOOL packU32(const U32 value, const char *name);
148 /*virtual*/ BOOL unpackU32(U32 &value, const char *name);
149
150 /*virtual*/ BOOL packS32(const S32 value, const char *name);
151 /*virtual*/ BOOL unpackS32(S32 &value, const char *name);
152
153 /*virtual*/ BOOL packF32(const F32 value, const char *name);
154 /*virtual*/ BOOL unpackF32(F32 &value, const char *name);
155
156 /*virtual*/ BOOL packColor4(const LLColor4 &value, const char *name);
157 /*virtual*/ BOOL unpackColor4(LLColor4 &value, const char *name);
158
159 /*virtual*/ BOOL packColor4U(const LLColor4U &value, const char *name);
160 /*virtual*/ BOOL unpackColor4U(LLColor4U &value, const char *name);
161
162 /*virtual*/ BOOL packVector2(const LLVector2 &value, const char *name);
163 /*virtual*/ BOOL unpackVector2(LLVector2 &value, const char *name);
164
165 /*virtual*/ BOOL packVector3(const LLVector3 &value, const char *name);
166 /*virtual*/ BOOL unpackVector3(LLVector3 &value, const char *name);
167
168 /*virtual*/ BOOL packVector4(const LLVector4 &value, const char *name);
169 /*virtual*/ BOOL unpackVector4(LLVector4 &value, const char *name);
170
171 /*virtual*/ BOOL packUUID(const LLUUID &value, const char *name);
172 /*virtual*/ BOOL unpackUUID(LLUUID &value, const char *name);
173
174 S32 getCurrentSize() const { return (S32)(mCurBufferp - mBufferp); }
175 S32 getBufferSize() const { return mBufferSize; }
176 void reset() { mCurBufferp = mBufferp; mWriteEnabled = (mCurBufferp != NULL); }
177 void freeBuffer() { delete [] mBufferp; mBufferp = mCurBufferp = NULL; mBufferSize = 0; mWriteEnabled = FALSE; }
178 void assignBuffer(U8 *bufferp, S32 size)
179 {
180 mBufferp = bufferp;
181 mCurBufferp = bufferp;
182 mBufferSize = size;
183 mWriteEnabled = TRUE;
184 }
185 const LLDataPackerBinaryBuffer& operator=(const LLDataPackerBinaryBuffer &a);
186
187 /*virtual*/ BOOL hasNext() const { return getCurrentSize() < getBufferSize(); }
188
189 /*virtual*/ void dumpBufferToLog();
190protected:
191 inline BOOL verifyLength(const S32 data_size, const char *name);
192
193 U8 *mBufferp;
194 U8 *mCurBufferp;
195 S32 mBufferSize;
196};
197
198inline BOOL LLDataPackerBinaryBuffer::verifyLength(const S32 data_size, const char *name)
199{
200 if (mWriteEnabled && (mCurBufferp - mBufferp) > mBufferSize - data_size)
201 {
202 llwarns << "Buffer overflow in BinaryBuffer length verify, field name " << name << "!" << llendl;
203 llwarns << "Current pos: " << (int)(mCurBufferp - mBufferp) << " Buffer size: " << mBufferSize << " Data size: " << data_size << llendl;
204 return FALSE;
205 }
206
207 return TRUE;
208}
209
210class LLDataPackerAsciiBuffer : public LLDataPacker
211{
212public:
213 LLDataPackerAsciiBuffer(char* bufferp, S32 size)
214 {
215 mBufferp = bufferp;
216 mCurBufferp = bufferp;
217 mBufferSize = size;
218 mPassFlags = 0;
219 mIncludeNames = FALSE;
220 mWriteEnabled = TRUE;
221 }
222
223 LLDataPackerAsciiBuffer()
224 {
225 mBufferp = NULL;
226 mCurBufferp = NULL;
227 mBufferSize = 0;
228 mPassFlags = 0;
229 mIncludeNames = FALSE;
230 mWriteEnabled = FALSE;
231 }
232
233 /*virtual*/ BOOL packString(const char *value, const char *name);
234 /*virtual*/ BOOL unpackString(std::string& value, const char *name);
235
236 /*virtual*/ BOOL packBinaryData(const U8 *value, S32 size, const char *name);
237 /*virtual*/ BOOL unpackBinaryData(U8 *value, S32 &size, const char *name);
238
239 // Constant size binary data packing
240 /*virtual*/ BOOL packBinaryDataFixed(const U8 *value, S32 size, const char *name);
241 /*virtual*/ BOOL unpackBinaryDataFixed(U8 *value, S32 size, const char *name);
242
243 /*virtual*/ BOOL packU8(const U8 value, const char *name);
244 /*virtual*/ BOOL unpackU8(U8 &value, const char *name);
245
246 /*virtual*/ BOOL packU16(const U16 value, const char *name);
247 /*virtual*/ BOOL unpackU16(U16 &value, const char *name);
248
249 /*virtual*/ BOOL packU32(const U32 value, const char *name);
250 /*virtual*/ BOOL unpackU32(U32 &value, const char *name);
251
252 /*virtual*/ BOOL packS32(const S32 value, const char *name);
253 /*virtual*/ BOOL unpackS32(S32 &value, const char *name);
254
255 /*virtual*/ BOOL packF32(const F32 value, const char *name);
256 /*virtual*/ BOOL unpackF32(F32 &value, const char *name);
257
258 /*virtual*/ BOOL packColor4(const LLColor4 &value, const char *name);
259 /*virtual*/ BOOL unpackColor4(LLColor4 &value, const char *name);
260
261 /*virtual*/ BOOL packColor4U(const LLColor4U &value, const char *name);
262 /*virtual*/ BOOL unpackColor4U(LLColor4U &value, const char *name);
263
264 /*virtual*/ BOOL packVector2(const LLVector2 &value, const char *name);
265 /*virtual*/ BOOL unpackVector2(LLVector2 &value, const char *name);
266
267 /*virtual*/ BOOL packVector3(const LLVector3 &value, const char *name);
268 /*virtual*/ BOOL unpackVector3(LLVector3 &value, const char *name);
269
270 /*virtual*/ BOOL packVector4(const LLVector4 &value, const char *name);
271 /*virtual*/ BOOL unpackVector4(LLVector4 &value, const char *name);
272
273 /*virtual*/ BOOL packUUID(const LLUUID &value, const char *name);
274 /*virtual*/ BOOL unpackUUID(LLUUID &value, const char *name);
275
276 void setIncludeNames(BOOL b) { mIncludeNames = b; }
277
278 // Include the trailing NULL so it's always a valid string
279 S32 getCurrentSize() const { return (S32)(mCurBufferp - mBufferp) + 1; }
280
281 S32 getBufferSize() const { return mBufferSize; }
282 /*virtual*/ void reset() { mCurBufferp = mBufferp; mWriteEnabled = (mCurBufferp != NULL); }
283
284 /*virtual*/ BOOL hasNext() const { return getCurrentSize() < getBufferSize(); }
285
286 inline void freeBuffer();
287 inline void assignBuffer(char* bufferp, S32 size);
288 void dump();
289
290protected:
291 void writeIndentedName(const char *name);
292 BOOL getValueStr(const char *name, char *out_value, const S32 value_len);
293
294protected:
295 inline BOOL verifyLength(const S32 data_size, const char *name);
296
297 char *mBufferp;
298 char *mCurBufferp;
299 S32 mBufferSize;
300 BOOL mIncludeNames; // useful for debugging, print the name of each field
301};
302
303inline void LLDataPackerAsciiBuffer::freeBuffer()
304{
305 delete [] mBufferp;
306 mBufferp = mCurBufferp = NULL;
307 mBufferSize = 0;
308 mWriteEnabled = FALSE;
309}
310
311inline void LLDataPackerAsciiBuffer::assignBuffer(char* bufferp, S32 size)
312{
313 mBufferp = bufferp;
314 mCurBufferp = bufferp;
315 mBufferSize = size;
316 mWriteEnabled = TRUE;
317}
318
319inline BOOL LLDataPackerAsciiBuffer::verifyLength(const S32 data_size, const char *name)
320{
321 if (mWriteEnabled && (mCurBufferp - mBufferp) > mBufferSize - data_size)
322 {
323 llwarns << "Buffer overflow in AsciiBuffer length verify, field name " << name << "!" << llendl;
324 llwarns << "Current pos: " << (int)(mCurBufferp - mBufferp) << " Buffer size: " << mBufferSize << " Data size: " << data_size << llendl;
325 return FALSE;
326 }
327
328 return TRUE;
329}
330
331class LLDataPackerAsciiFile : public LLDataPacker
332{
333public:
334 LLDataPackerAsciiFile(FILE *fp, const S32 indent = 2)
335 : LLDataPacker(),
336 mIndent(indent),
337 mFP(fp),
338 mOutputStream(NULL),
339 mInputStream(NULL)
340 {
341 }
342
343 LLDataPackerAsciiFile(std::ostream& output_stream, const S32 indent = 2)
344 : LLDataPacker(),
345 mIndent(indent),
346 mFP(NULL),
347 mOutputStream(&output_stream),
348 mInputStream(NULL)
349 {
350 mWriteEnabled = TRUE;
351 }
352
353 LLDataPackerAsciiFile(std::istream& input_stream, const S32 indent = 2)
354 : LLDataPacker(),
355 mIndent(indent),
356 mFP(NULL),
357 mOutputStream(NULL),
358 mInputStream(&input_stream)
359 {
360 }
361
362 /*virtual*/ BOOL packString(const char *value, const char *name);
363 /*virtual*/ BOOL unpackString(std::string& value, const char *name);
364
365 /*virtual*/ BOOL packBinaryData(const U8 *value, S32 size, const char *name);
366 /*virtual*/ BOOL unpackBinaryData(U8 *value, S32 &size, const char *name);
367
368 /*virtual*/ BOOL packBinaryDataFixed(const U8 *value, S32 size, const char *name);
369 /*virtual*/ BOOL unpackBinaryDataFixed(U8 *value, S32 size, const char *name);
370
371 /*virtual*/ BOOL packU8(const U8 value, const char *name);
372 /*virtual*/ BOOL unpackU8(U8 &value, const char *name);
373
374 /*virtual*/ BOOL packU16(const U16 value, const char *name);
375 /*virtual*/ BOOL unpackU16(U16 &value, const char *name);
376
377 /*virtual*/ BOOL packU32(const U32 value, const char *name);
378 /*virtual*/ BOOL unpackU32(U32 &value, const char *name);
379
380 /*virtual*/ BOOL packS32(const S32 value, const char *name);
381 /*virtual*/ BOOL unpackS32(S32 &value, const char *name);
382
383 /*virtual*/ BOOL packF32(const F32 value, const char *name);
384 /*virtual*/ BOOL unpackF32(F32 &value, const char *name);
385
386 /*virtual*/ BOOL packColor4(const LLColor4 &value, const char *name);
387 /*virtual*/ BOOL unpackColor4(LLColor4 &value, const char *name);
388
389 /*virtual*/ BOOL packColor4U(const LLColor4U &value, const char *name);
390 /*virtual*/ BOOL unpackColor4U(LLColor4U &value, const char *name);
391
392 /*virtual*/ BOOL packVector2(const LLVector2 &value, const char *name);
393 /*virtual*/ BOOL unpackVector2(LLVector2 &value, const char *name);
394
395 /*virtual*/ BOOL packVector3(const LLVector3 &value, const char *name);
396 /*virtual*/ BOOL unpackVector3(LLVector3 &value, const char *name);
397
398 /*virtual*/ BOOL packVector4(const LLVector4 &value, const char *name);
399 /*virtual*/ BOOL unpackVector4(LLVector4 &value, const char *name);
400
401 /*virtual*/ BOOL packUUID(const LLUUID &value, const char *name);
402 /*virtual*/ BOOL unpackUUID(LLUUID &value, const char *name);
403protected:
404 void writeIndentedName(const char *name);
405 BOOL getValueStr(const char *name, char *out_value, const S32 value_len);
406
407 /*virtual*/ BOOL hasNext() const { return true; }
408
409protected:
410 S32 mIndent;
411 FILE *mFP;
412 std::ostream* mOutputStream;
413 std::istream* mInputStream;
414};
415
416#endif // LL_LLDATAPACKER
417