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 |
---|
17 | extern "C" { |
---|
18 | #endif // ifdef __cplusplus |
---|
19 | |
---|
20 | using 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 | |
---|
32 | int |
---|
33 | isbinary(const char* buf, int size) |
---|
34 | { |
---|
35 | if (size < 0) { |
---|
36 | size = strlen(buf); |
---|
37 | } |
---|
38 | |
---|
39 | for (int index = 0; index < size; index++) { |
---|
40 | int c = *buf++; |
---|
41 | if (((c >= '\000') && (c <= '\010')) || |
---|
42 | ((c >= '\013') && (c <= '\014')) || |
---|
43 | ((c >= '\016') && (c <= '\037')) || |
---|
44 | ((c >= '\177') && (c <= '\377')) ) { |
---|
45 | // data is binary |
---|
46 | return index+1; |
---|
47 | } |
---|
48 | } |
---|
49 | return 0; |
---|
50 | } |
---|
51 | |
---|
52 | |
---|
53 | |
---|
54 | |
---|
55 | |
---|
56 | /**********************************************************************/ |
---|
57 | // FUNCTION: Rappture::encoding::encode() |
---|
58 | /// Rappture::encoding::encode function encodes provided string |
---|
59 | /** |
---|
60 | * Encode a string by compressing it with zlib and then base64 encoding it. |
---|
61 | * |
---|
62 | * Full function call: |
---|
63 | * Rappture::encoding::encode(buf,flags) |
---|
64 | */ |
---|
65 | |
---|
66 | Rappture::Outcome |
---|
67 | encode (Rappture::Buffer& buf, int flags) |
---|
68 | { |
---|
69 | |
---|
70 | int compress = 0; |
---|
71 | int base64 = 0; |
---|
72 | int addHeader = 0; |
---|
73 | Rappture::Outcome err; |
---|
74 | Rappture::Buffer outData; |
---|
75 | |
---|
76 | if ((flags & RPENC_Z) == RPENC_Z ) { |
---|
77 | compress = 1; |
---|
78 | } |
---|
79 | if ((flags & RPENC_B64) == RPENC_B64 ) { |
---|
80 | base64 = 1; |
---|
81 | } |
---|
82 | if ((flags & RPENC_HDR) == RPENC_HDR ) { |
---|
83 | addHeader = 1; |
---|
84 | } |
---|
85 | |
---|
86 | outData.append(buf.bytes(),buf.size()); |
---|
87 | err = outData.encode(compress,base64); |
---|
88 | if (!err) { |
---|
89 | buf.clear(); |
---|
90 | if (addHeader == 1) { |
---|
91 | if ((compress == 1) && (base64 == 0)) { |
---|
92 | buf.append("@@RP-ENC:z\n",11); |
---|
93 | } |
---|
94 | else if ((compress == 0) && (base64 == 1)) { |
---|
95 | buf.append("@@RP-ENC:b64\n",13); |
---|
96 | } |
---|
97 | else if ((compress == 1) && (base64 == 1)) { |
---|
98 | buf.append("@@RP-ENC:zb64\n",14); |
---|
99 | } |
---|
100 | else { |
---|
101 | // do nothing |
---|
102 | } |
---|
103 | } |
---|
104 | else { |
---|
105 | // do nothing |
---|
106 | } |
---|
107 | buf.append(outData.bytes(),outData.size()); |
---|
108 | } |
---|
109 | |
---|
110 | return err; |
---|
111 | } |
---|
112 | |
---|
113 | /**********************************************************************/ |
---|
114 | // FUNCTION: Rappture::encoding::decode() |
---|
115 | /// Rappture::encoding::decode function decodes provided string |
---|
116 | /** |
---|
117 | * Decode a string by uncompressing it with zlib and base64 decoding it. |
---|
118 | * If binary data is provided, the data is base64 decoded and uncompressed. |
---|
119 | * Rappture::encoding::isbinary is used to qualify binary data. |
---|
120 | * |
---|
121 | * Full function call: |
---|
122 | * Rappture::encoding::decode (buf,flags) |
---|
123 | */ |
---|
124 | |
---|
125 | Rappture::Outcome |
---|
126 | decode (Rappture::Buffer& buf, int flags) |
---|
127 | { |
---|
128 | |
---|
129 | int decompress = 0; |
---|
130 | int base64 = 0; |
---|
131 | int checkHDR = 0; |
---|
132 | Rappture::Outcome err; |
---|
133 | Rappture::Buffer outData; |
---|
134 | |
---|
135 | if ((flags & RPENC_Z) == RPENC_Z ) { |
---|
136 | decompress = 1; |
---|
137 | } |
---|
138 | if ((flags & RPENC_B64) == RPENC_B64 ) { |
---|
139 | base64 = 1; |
---|
140 | } |
---|
141 | if ((flags & RPENC_HDR) == RPENC_HDR ) { |
---|
142 | checkHDR = 1; |
---|
143 | } |
---|
144 | |
---|
145 | if ((buf.size() > 11) && (strncmp(buf.bytes(),"@@RP-ENC:z\n",11) == 0)) { |
---|
146 | outData.append(buf.bytes()+11,buf.size()-11); |
---|
147 | if ( (checkHDR == 1) || ( (decompress == 0) && (base64 == 0) ) ) { |
---|
148 | decompress = 1; |
---|
149 | base64 = 0; |
---|
150 | } |
---|
151 | } |
---|
152 | else if ((buf.size() > 13) && (strncmp(buf.bytes(),"@@RP-ENC:b64\n",13) == 0)) { |
---|
153 | outData.append(buf.bytes()+13,buf.size()-13); |
---|
154 | if ( (checkHDR == 1) || ( (decompress == 0) && (base64 == 0) ) ) { |
---|
155 | decompress = 0; |
---|
156 | base64 = 1; |
---|
157 | } |
---|
158 | } |
---|
159 | else if ((buf.size() > 14) && (strncmp(buf.bytes(),"@@RP-ENC:zb64\n",14) == 0)) { |
---|
160 | outData.append(buf.bytes()+14,buf.size()-14); |
---|
161 | if ( (checkHDR == 1) || ( (decompress == 0) && (base64 == 0) ) ) { |
---|
162 | decompress = 1; |
---|
163 | base64 = 1; |
---|
164 | } |
---|
165 | } |
---|
166 | else { |
---|
167 | // no special recognized tags |
---|
168 | outData.append(buf.bytes(),buf.size()); |
---|
169 | } |
---|
170 | |
---|
171 | err = outData.decode(decompress,base64); |
---|
172 | if (!err) { |
---|
173 | buf.move(outData); |
---|
174 | } |
---|
175 | |
---|
176 | return err; |
---|
177 | } |
---|
178 | |
---|
179 | #ifdef __cplusplus |
---|
180 | } |
---|
181 | #endif // ifdef __cplusplus |
---|