source: trunk/src/core/RpEncode.cc @ 675

Last change on this file since 675 was 675, checked in by dkearney, 17 years ago

added buffer length check so we dont upset strncmp

File size: 4.7 KB
Line 
1/*
2 * ----------------------------------------------------------------------
3 *  Rappture::encoding
4 *
5 *  The encoding module for rappture used to zip and b64 encode data.
6 * ======================================================================
7 *  AUTHOR:  Derrick Kearney, Purdue University
8 *  Copyright (c) 2004-2007  Purdue Research Foundation
9 *
10 *  See the file "license.terms" for information on usage and
11 *  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
12 * ======================================================================
13 */
14#include "RpEncode.h"
15
16#ifdef __cplusplus
17extern "C" {
18#endif // ifdef __cplusplus
19
20using namespace Rappture::encoding;
21
22/**********************************************************************/
23// FUNCTION: Rappture::encoding::isbinary()
24/// isbinary checks to see if given string is binary.
25/**
26 * Checks to see if any of size characters in *buf are binary
27 * Full function call:
28 * Rappture::encoding::isbinary(buf,size);
29 *
30 */
31
32int
33isbinary(const char* buf, int size)
34{
35    int index = 0;
36
37    if (size == -1) {
38        size = strlen(buf);
39    }
40
41    for (index = 0; index < size; index++) {
42        if (((buf[index] >= '\000') && (buf[index] <= '\010')) ||
43            ((buf[index] >= '\013') && (buf[index] <= '\014')) ||
44            ((buf[index] >= '\016') && (buf[index] <= '\037')) ||
45            ((buf[index] >= '\177') && (buf[index] <= '\377')) ) {
46            // data is binary
47            return index+1;
48        }
49
50    }
51    return 0;
52}
53
54
55
56
57
58/**********************************************************************/
59// FUNCTION: Rappture::encoding::encode()
60/// Rappture::encoding::encode function encodes provided string
61/**
62 * Encode a string by compressing it with zlib and then base64 encoding it.
63 *
64 * Full function call:
65 * Rappture::encoding::encode(buf,flags)
66 */
67
68Rappture::Outcome
69encode (Rappture::Buffer& buf, int flags)
70{
71
72    int compress              = 0;
73    int base64                = 0;
74    int addHeader             = 0;
75    Rappture::Outcome err;
76    Rappture::Buffer outData;
77
78    if ((flags & RPENC_Z) == RPENC_Z ) {
79        compress = 1;
80    }
81    if ((flags & RPENC_B64) == RPENC_B64 ) {
82        base64 = 1;
83    }
84    if ((flags & RPENC_HDR) == RPENC_HDR ) {
85        addHeader = 1;
86    }
87
88    outData.append(buf.bytes(),buf.size());
89    err = outData.encode(compress,base64);
90    if (!err) {
91        buf.clear();
92        if (addHeader == 1) {
93            if ((compress == 1) && (base64 == 0)) {
94                buf.append("@@RP-ENC:z\n",11);
95            }
96            else if ((compress == 0) && (base64 == 1)) {
97                buf.append("@@RP-ENC:b64\n",13);
98            }
99            else if ((compress == 1) && (base64 == 1)) {
100                buf.append("@@RP-ENC:zb64\n",14);
101            }
102            else {
103                // do nothing
104            }
105        }
106        else {
107            // do nothing
108        }
109        buf.append(outData.bytes(),outData.size());
110    }
111
112    return err;
113}
114
115/**********************************************************************/
116// FUNCTION: Rappture::encoding::decode()
117/// Rappture::encoding::decode function decodes provided string
118/**
119 * Decode a string by uncompressing it with zlib and base64 decoding it.
120 * If binary data is provided, the data is base64 decoded and uncompressed.
121 * Rappture::encoding::isbinary is used to qualify binary data.
122 *
123 * Full function call:
124 * Rappture::encoding::decode (buf,flags)
125 */
126
127Rappture::Outcome
128decode (Rappture::Buffer& buf, int flags)
129{
130
131    int decompress            = 0;
132    int base64                = 0;
133    Rappture::Outcome err;
134    Rappture::Buffer outData;
135
136    if ((flags & RPENC_Z) == RPENC_Z ) {
137        decompress = 1;
138    }
139    if ((flags & RPENC_B64) == RPENC_B64 ) {
140        base64 = 1;
141    }
142
143    if ((buf.size() > 11) && (strncmp(buf.bytes(),"@@RP-ENC:z\n",11) == 0)) {
144        outData.append(buf.bytes()+11,buf.size()-11);
145        if ( (decompress == 0) && (base64 == 0) ) {
146            decompress = 1;
147            base64 = 0;
148        }
149    }
150    else if ((buf.size() > 13) && (strncmp(buf.bytes(),"@@RP-ENC:b64\n",13) == 0)) {
151        outData.append(buf.bytes()+13,buf.size()-13);
152        if ( (decompress == 0) && (base64 == 0) ) {
153            decompress = 0;
154            base64 = 1;
155        }
156    }
157    else if ((buf.size() > 14) && (strncmp(buf.bytes(),"@@RP-ENC:zb64\n",14) == 0)) {
158        outData.append(buf.bytes()+14,buf.size()-14);
159        if ( (decompress == 0) && (base64 == 0) ) {
160            decompress = 1;
161            base64 = 1;
162        }
163    }
164    else {
165        // no special recognized tags
166        outData.append(buf.bytes(),buf.size());
167    }
168
169    err = outData.decode(decompress,base64);
170    if (!err) {
171        buf.move(outData);
172    }
173
174    return err;
175}
176
177#ifdef __cplusplus
178}
179#endif // ifdef __cplusplus
Note: See TracBrowser for help on using the repository browser.