/src/resiprocate/resip/stack/Embedded.cxx
Line | Count | Source (jump to first uncovered line) |
1 | | #if defined(HAVE_CONFIG_H) |
2 | | #include "config.h" |
3 | | #endif |
4 | | |
5 | | |
6 | | #include "rutil/ResipAssert.h" |
7 | | |
8 | | #include "resip/stack/Embedded.hxx" |
9 | | #include "rutil/Data.hxx" |
10 | | #include "resip/stack/Symbols.hxx" |
11 | | #include "rutil/WinLeakCheck.hxx" |
12 | | |
13 | | using namespace resip; |
14 | | |
15 | | char fromHex(char h1, char h2) |
16 | 2.49k | { |
17 | 2.49k | h1 = toupper(h1); |
18 | 2.49k | h2 = toupper(h2); |
19 | | |
20 | 2.49k | int i1; |
21 | 2.49k | int i2; |
22 | | |
23 | 2.49k | if (isdigit(h1)) |
24 | 206 | { |
25 | 206 | i1 = h1 - '0'; |
26 | 206 | } |
27 | 2.28k | else |
28 | 2.28k | { |
29 | 2.28k | i1 = h1 - 'A' + 10; |
30 | 2.28k | } |
31 | | |
32 | 2.49k | if (isdigit(h2)) |
33 | 207 | { |
34 | 207 | i2 = h2 - '0'; |
35 | 207 | } |
36 | 2.28k | else |
37 | 2.28k | { |
38 | 2.28k | i2 = h2 - 'A' + 10; |
39 | 2.28k | } |
40 | | |
41 | 2.49k | return i1*16+i2; |
42 | 2.49k | } |
43 | | |
44 | | char* |
45 | | Embedded::decode(const Data& in, unsigned int& count) |
46 | 1.76M | { |
47 | 1.76M | const char *get = in.data(); |
48 | 1.76M | const char *end = get + in.size(); |
49 | 1.76M | char *ret = new char[in.size()]; |
50 | 1.76M | char *put = ret; |
51 | | |
52 | 1.76M | count = 0; |
53 | 6.85M | while (get < end) |
54 | 5.09M | { |
55 | 5.09M | if (*get == Symbols::PERCENT[0] && get+2 < end) |
56 | 2.49k | { |
57 | 2.49k | *put = fromHex(*(get+1), *(get+2)); |
58 | 2.49k | get += 3; |
59 | 2.49k | } |
60 | 5.09M | else |
61 | 5.09M | { |
62 | 5.09M | *put = *get; |
63 | 5.09M | get++; |
64 | 5.09M | } |
65 | 5.09M | count++; |
66 | 5.09M | put++; |
67 | 5.09M | } |
68 | | |
69 | 1.76M | return ret; |
70 | 1.76M | } |
71 | | |
72 | | static char hexMap[] = "0123456789ABCDEF"; |
73 | | |
74 | | /* |
75 | | This method encodes the hname and hvalue production of SIP-URI. |
76 | | |
77 | | SIP-URI = "sip:" [ userinfo ] hostport |
78 | | uri-parameters [ headers ] |
79 | | |
80 | | headers = "?" header *( "&" header ) |
81 | | header = hname "=" hvalue |
82 | | hname = 1*( hnv-unreserved / unreserved / escaped ) |
83 | | hvalue = *( hnv-unreserved / unreserved / escaped ) |
84 | | |
85 | | hnv-unreserved = "[" / "]" / "/" / "?" / ":" / "+" / "$" |
86 | | |
87 | | unreserved = alphanum / mark |
88 | | mark = "-" / "_" / "." / "!" / "~" / "*" / "'" |
89 | | / "(" / ")" |
90 | | escaped = "%" HEXDIG HEXDIG |
91 | | |
92 | | alphanum = ALPHA / DIGIT |
93 | | |
94 | | It is both unsafe and unwise to express what needs to be escaped |
95 | | and simply not escape everything else, because the omission of an item |
96 | | in need of escaping will cause mis-parsing on the remote end. |
97 | | Because escaping unnecessarily causes absolutely no harm, the omission |
98 | | of a symbol from the list of items positively allowed causes no |
99 | | damage whatsoever. |
100 | | */ |
101 | | |
102 | | Data |
103 | | Embedded::encode(const Data& dat) |
104 | 0 | { |
105 | 0 | Data out((int)((dat.size()*11)/10), Data::Preallocate); |
106 | | |
107 | 0 | { |
108 | 0 | DataStream str(out); |
109 | 0 | for (Data::size_type i = 0; i < dat.size(); i++) |
110 | 0 | { |
111 | 0 | switch (dat[i]) |
112 | 0 | { |
113 | 0 | case '0': case '1': case '2': case '3': case '4': case '5': |
114 | 0 | case '6': case '7': case '8': case '9': case 'a': case 'b': |
115 | 0 | case 'c': case 'd': case 'e': case 'f': case 'g': case 'h': |
116 | 0 | case 'i': case 'j': case 'k': case 'l': case 'm': case 'n': |
117 | 0 | case 'o': case 'p': case 'q': case 'r': case 's': case 't': |
118 | 0 | case 'u': case 'v': case 'w': case 'x': case 'y': case 'z': |
119 | 0 | case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': |
120 | 0 | case 'G': case 'H': case 'I': case 'J': case 'K': case 'L': |
121 | 0 | case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R': |
122 | 0 | case 'S': case 'T': case 'U': case 'V': case 'W': case 'X': |
123 | 0 | case 'Y': case 'Z': case '[': case ']': case ',': case '?': |
124 | 0 | case ':': case '+': case '$': case '-': case '_': case '.': |
125 | 0 | case '!': case '~': case '*': case '\'': case '(': case ')': |
126 | 0 | str << dat[i]; |
127 | 0 | break; |
128 | | |
129 | 0 | default: |
130 | 0 | { |
131 | 0 | str << Symbols::PERCENT; |
132 | |
|
133 | 0 | unsigned char temp = dat[i]; |
134 | 0 | int hi = (temp & 0xf0)>>4; |
135 | 0 | int low = (temp & 0xf); |
136 | |
|
137 | 0 | str << hexMap[hi]; |
138 | 0 | str << hexMap[low]; |
139 | 0 | } |
140 | 0 | } |
141 | 0 | } |
142 | 0 | } |
143 | | |
144 | 0 | return out; |
145 | 0 | } |
146 | | /* ==================================================================== |
147 | | * The Vovida Software License, Version 1.0 |
148 | | * |
149 | | * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved. |
150 | | * |
151 | | * Redistribution and use in source and binary forms, with or without |
152 | | * modification, are permitted provided that the following conditions |
153 | | * are met: |
154 | | * |
155 | | * 1. Redistributions of source code must retain the above copyright |
156 | | * notice, this list of conditions and the following disclaimer. |
157 | | * |
158 | | * 2. Redistributions in binary form must reproduce the above copyright |
159 | | * notice, this list of conditions and the following disclaimer in |
160 | | * the documentation and/or other materials provided with the |
161 | | * distribution. |
162 | | * |
163 | | * 3. The names "VOCAL", "Vovida Open Communication Application Library", |
164 | | * and "Vovida Open Communication Application Library (VOCAL)" must |
165 | | * not be used to endorse or promote products derived from this |
166 | | * software without prior written permission. For written |
167 | | * permission, please contact vocal@vovida.org. |
168 | | * |
169 | | * 4. Products derived from this software may not be called "VOCAL", nor |
170 | | * may "VOCAL" appear in their name, without prior written |
171 | | * permission of Vovida Networks, Inc. |
172 | | * |
173 | | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED |
174 | | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
175 | | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND |
176 | | * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA |
177 | | * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES |
178 | | * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL, |
179 | | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
180 | | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
181 | | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
182 | | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
183 | | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE |
184 | | * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
185 | | * DAMAGE. |
186 | | * |
187 | | * ==================================================================== |
188 | | * |
189 | | * This software consists of voluntary contributions made by Vovida |
190 | | * Networks, Inc. and many individuals on behalf of Vovida Networks, |
191 | | * Inc. For more information on Vovida Networks, Inc., please see |
192 | | * <http://www.vovida.org/>. |
193 | | * |
194 | | */ |