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