Coverage Report

Created: 2025-12-31 06:34

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/resiprocate/resip/stack/Embedded.cxx
Line
Count
Source
1
#if defined(HAVE_CONFIG_H)
2
#include "config.h"
3
#endif
4
5
#include <ctype.h>
6
7
#include "rutil/ResipAssert.h"
8
9
#include "resip/stack/Embedded.hxx"
10
#include "rutil/Data.hxx"
11
#include "resip/stack/Symbols.hxx"
12
#include "rutil/WinLeakCheck.hxx"
13
14
using namespace resip;
15
16
char fromHex(char h1, char h2)
17
3.67k
{
18
3.67k
   h1 = toupper(static_cast< unsigned char >(h1));
19
3.67k
   h2 = toupper(static_cast< unsigned char >(h2));
20
21
3.67k
   int i1;
22
3.67k
   int i2;
23
24
3.67k
   if (isdigit(static_cast< unsigned char >(h1)))
25
392
   {
26
392
      i1 = h1 - '0';
27
392
   }
28
3.28k
   else
29
3.28k
   {
30
3.28k
      i1 = h1 - 'A' + 10;
31
3.28k
   }
32
33
3.67k
   if (isdigit(static_cast< unsigned char >(h2)))
34
512
   {
35
512
      i2 = h2 - '0';
36
512
   }
37
3.16k
   else
38
3.16k
   {
39
3.16k
      i2 = h2 - 'A' + 10;
40
3.16k
   }
41
   
42
3.67k
   return i1*16+i2;
43
3.67k
}
44
45
char*
46
Embedded::decode(const Data& in, unsigned int& count)
47
994k
{
48
994k
   const char *get = in.data();
49
994k
   const char *end = get + in.size();
50
994k
   char *ret = new char[in.size()];
51
994k
   char *put = ret;
52
53
994k
   count = 0;
54
7.30M
   while (get < end)
55
6.31M
   {
56
6.31M
      if (*get == Symbols::PERCENT[0] && get+2 < end)
57
3.67k
      {
58
3.67k
         *put = fromHex(*(get+1), *(get+2));
59
3.67k
         get += 3;
60
3.67k
      }
61
6.30M
      else
62
6.30M
      {
63
6.30M
         *put = *get;
64
6.30M
         get++;
65
6.30M
      }
66
6.31M
      count++;
67
6.31M
      put++;
68
6.31M
   }
69
70
994k
   return ret;
71
994k
}
72
73
static 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
103
Data
104
Embedded::encode(const Data& dat)
105
0
{
106
0
   Data out((int)((dat.size()*11)/10), Data::Preallocate);
107
   
108
0
   {
109
0
      DataStream str(out);
110
0
      for (Data::size_type i = 0; i < dat.size(); i++)
111
0
      {
112
0
         switch (dat[i])
113
0
         {
114
0
            case '0': case '1': case '2': case '3': case '4': case '5':
115
0
            case '6': case '7': case '8': case '9': case 'a': case 'b':
116
0
            case 'c': case 'd': case 'e': case 'f': case 'g': case 'h':
117
0
            case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
118
0
            case 'o': case 'p': case 'q': case 'r': case 's': case 't':
119
0
            case 'u': case 'v': case 'w': case 'x': case 'y': case 'z':
120
0
            case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
121
0
            case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
122
0
            case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
123
0
            case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
124
0
            case 'Y': case 'Z': case '[': case ']': case ',': case '?':
125
0
            case ':': case '+': case '$': case '-': case '_': case '.':
126
0
            case '!': case '~': case '*': case '\'': case '(': case ')':
127
0
               str << dat[i];
128
0
               break;
129
130
0
            default:
131
0
            {
132
0
               str << Symbols::PERCENT;
133
134
0
               unsigned char temp = dat[i];
135
0
               int hi = (temp & 0xf0)>>4;
136
0
               int low = (temp & 0xf);
137
138
0
               str << hexMap[hi];
139
0
               str << hexMap[low];
140
0
            }
141
0
         }
142
0
      }
143
0
   }
144
145
0
   return out;
146
0
}
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
 */