Coverage Report

Created: 2025-11-05 06:27

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
47.7k
{
18
47.7k
   h1 = toupper(static_cast< unsigned char >(h1));
19
47.7k
   h2 = toupper(static_cast< unsigned char >(h2));
20
21
47.7k
   int i1;
22
47.7k
   int i2;
23
24
47.7k
   if (isdigit(static_cast< unsigned char >(h1)))
25
601
   {
26
601
      i1 = h1 - '0';
27
601
   }
28
47.1k
   else
29
47.1k
   {
30
47.1k
      i1 = h1 - 'A' + 10;
31
47.1k
   }
32
33
47.7k
   if (isdigit(static_cast< unsigned char >(h2)))
34
692
   {
35
692
      i2 = h2 - '0';
36
692
   }
37
47.1k
   else
38
47.1k
   {
39
47.1k
      i2 = h2 - 'A' + 10;
40
47.1k
   }
41
   
42
47.7k
   return i1*16+i2;
43
47.7k
}
44
45
char*
46
Embedded::decode(const Data& in, unsigned int& count)
47
1.06M
{
48
1.06M
   const char *get = in.data();
49
1.06M
   const char *end = get + in.size();
50
1.06M
   char *ret = new char[in.size()];
51
1.06M
   char *put = ret;
52
53
1.06M
   count = 0;
54
8.89M
   while (get < end)
55
7.82M
   {
56
7.82M
      if (*get == Symbols::PERCENT[0] && get+2 < end)
57
47.7k
      {
58
47.7k
         *put = fromHex(*(get+1), *(get+2));
59
47.7k
         get += 3;
60
47.7k
      }
61
7.78M
      else
62
7.78M
      {
63
7.78M
         *put = *get;
64
7.78M
         get++;
65
7.78M
      }
66
7.82M
      count++;
67
7.82M
      put++;
68
7.82M
   }
69
70
1.06M
   return ret;
71
1.06M
}
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
 */