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