1 /* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13 * express or implied.
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
17 */
18 #ifndef PVRTSP_CLIENT_ENGINE_UTILS_H
19 #include "pvrtsp_client_engine_utils.h"
20 #endif
21 #include "oscl_mem.h"
22 #include "oscl_mem_basic_functions.h"
23
composeURL(const char * baseURL,const char * relativeURL,char * completeURL,unsigned int & completeURLLen)24 bool composeURL(const char *baseURL, const char *relativeURL, char *completeURL, unsigned int &completeURLLen)
25 {
26 char* copyOfBaseURL = OSCL_ARRAY_NEW(char, 8 * MAX_LONG_TEXT_LEN);
27 if (NULL == copyOfBaseURL)
28 {
29 return false;
30 }
31
32 URLType urlType = findRelativeURLType(relativeURL);
33 switch (urlType)
34 {
35 case CONCATENATE:
36 {
37 int len = oscl_strlen(baseURL);
38 oscl_strncpy(copyOfBaseURL, baseURL, (len + 1));
39 if ((copyOfBaseURL[len-1] != '/') && (len > 0))
40 {
41 copyOfBaseURL[len] = '/';
42 copyOfBaseURL[len+1] = 0;
43 len++;
44 }
45 if (completeURLLen <= (len + oscl_strlen(relativeURL)))
46 {
47 OSCL_ARRAY_DELETE(copyOfBaseURL);
48 return false;
49 }
50 oscl_strncpy(completeURL, copyOfBaseURL, (oscl_strlen(copyOfBaseURL) + 1));
51 oscl_strcat(completeURL, relativeURL);
52 completeURLLen = oscl_strlen(completeURL);
53 break;
54 }
55 case REPLACE_PATH:
56 {
57 oscl_strncpy(copyOfBaseURL, baseURL, (oscl_strlen(baseURL) + 1));
58 if (completeURLLen <= (oscl_strlen(copyOfBaseURL) + oscl_strlen(relativeURL)))
59 {
60 OSCL_ARRAY_DELETE(copyOfBaseURL);
61 return false;
62 }
63 oscl_strncpy(completeURL, copyOfBaseURL, (oscl_strlen(copyOfBaseURL) + 1));
64 oscl_strcat(completeURL, relativeURL);
65 completeURLLen = oscl_strlen(completeURL);
66 break;
67 }
68 case REPLACE_HOST:
69 {
70 const char RTSP[] = "rtsp:";
71 if (completeURLLen > (oscl_strlen(RTSP) + oscl_strlen(relativeURL)))
72 {
73 oscl_strncpy(completeURL, RTSP, (oscl_strlen(RTSP) + 1));
74 oscl_strcat(completeURL, relativeURL);
75 completeURLLen = oscl_strlen(completeURL);
76 }
77 else
78 {
79 OSCL_ARRAY_DELETE(copyOfBaseURL);
80 return false;
81 }
82 break;
83 }
84 case UNKNOWN:
85 {
86 OSCL_ARRAY_DELETE(copyOfBaseURL);
87 return false;
88 // break; This statement was removed to avoid compiler warning for Unreachable Code
89
90 }
91 }
92
93 if (copyOfBaseURL)
94 OSCL_ARRAY_DELETE(copyOfBaseURL);
95
96 return true;
97 }
98
findRelativeURL(const char * aURL)99 const char* findRelativeURL(const char *aURL)
100 {
101 int i = 0;
102 while (aURL[i] != '\0')
103 {
104 if (aURL[i] == FWD_SLASH)
105 {
106 if (aURL[i+1] != FWD_SLASH)
107 {
108 return &(aURL[i]);
109 }
110 else
111 {// "//"
112 i++;
113 }
114 }
115 i++;
116 }
117 return NULL;
118 }
119
findRelativeURLType(const char * relativeURL)120 URLType findRelativeURLType(const char *relativeURL)
121 {
122 if ((FWD_SLASH == relativeURL[0]) && (FWD_SLASH == relativeURL[1]))
123 {
124 return REPLACE_HOST;
125 }
126 else if ((FWD_SLASH == relativeURL[0]) && (FWD_SLASH != relativeURL[1]))
127 {
128 return REPLACE_PATH;
129 }
130 else if ((oscl_strstr(relativeURL, &COLON) != NULL) || (DOT == relativeURL[0]))
131 {
132 return UNKNOWN;
133 }
134 else
135 {
136 return CONCATENATE;
137 }
138 }
139
dropTextAfterLastSlash(char * copyOfBaseURL)140 void dropTextAfterLastSlash(char *copyOfBaseURL)
141 {
142 int textLen = oscl_strlen(copyOfBaseURL) - 1;
143
144 for (int ii = textLen; ii > 0; ii--)
145 {
146 if ((FWD_SLASH == copyOfBaseURL[ii]) && (ii == textLen))
147 {
148 return;
149 }
150 else if ((FWD_SLASH != copyOfBaseURL[ii-1]) &&
151 (FWD_SLASH == copyOfBaseURL[ii]) &&
152 (FWD_SLASH != copyOfBaseURL[ii+1]))
153 {
154 copyOfBaseURL[ii+1] = '\0';//NULL;
155 return;
156 }
157 }
158 //We reach this point only if we a base URL that is not terminated with a '/'.
159 //So, we need to append '/' to the string.
160 if (8*MAX_LONG_TEXT_LEN >= (textLen + 2))
161 {
162 copyOfBaseURL[textLen+1] = FWD_SLASH;
163 copyOfBaseURL[textLen+2] = '\0';//NULL;
164 }
165 return;
166 }
167
dropTextAfterFirstSlash(char * copyOfBaseURL)168 void dropTextAfterFirstSlash(char *copyOfBaseURL)
169 {
170 int textLen = oscl_strlen(copyOfBaseURL);
171 int ii;
172 for (ii = 1; ii < textLen - 1; ii++)
173 {
174 if ((FWD_SLASH != copyOfBaseURL[ii-1]) &&
175 (FWD_SLASH == copyOfBaseURL[ii]) &&
176 (FWD_SLASH != copyOfBaseURL[ii+1]))
177 {
178 copyOfBaseURL[ii] = '\0';//NULL;
179 return;
180 }
181 }
182 //We reach this point if we were not able to find a '/' in the end of the clip.
183 if (FWD_SLASH == copyOfBaseURL[ii])
184 {
185 copyOfBaseURL[ii] = '\0';//NULL;
186 }
187
188 return;
189 }
190
191 #ifndef OSCL_TIME_H_INCLUDED
192 #include "oscl_time.h"
193 #endif
194
195 #ifndef OSCL_SNPRINTF_H_INCLUDED
196 #include "oscl_snprintf.h"
197 #endif
198 #ifndef OSCL_STRING_CONTAINERS_H_INCLUDED
199 #include "oscl_string_containers.h"
200 #endif
201
generatePseudoUUID(OSCL_String & aUUID)202 bool generatePseudoUUID(OSCL_String& aUUID)
203 {
204 aUUID = NULL;
205 return true;
206 }
207
208