Coverage Report

Created: 2025-12-31 06:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/MapServer/src/mapcpl.c
Line
Count
Source
1
/******************************************************************************
2
 * $Id$
3
 *
4
 * Project:  MapServer
5
 * Purpose:  Functions copied from GDAL's CPL. This file contain utility
6
 *           functions that come from the GDAL/OGR cpl library. The idea
7
 *           behind it is to have access in mapserver to all these
8
 *           utilities, without being constrained to link with GDAL/OGR.
9
 * Author:   Y. Assefa, DM Solutions Group (assefa@dmsolutions.ca)
10
 *
11
 *
12
 * Note:
13
 * Names of functions used here are the same as those in the cpl
14
 * library with the exception the the CPL prefix is changed to ms
15
 * (eg : CPLGetBasename() would become msGetBasename())
16
 *
17
 ******************************************************************************
18
 * Copyright (c) 1996-2005 Regents of the University of Minnesota.
19
 *
20
 * Permission is hereby granted, free of charge, to any person obtaining a
21
 * copy of this software and associated documentation files (the "Software"),
22
 * to deal in the Software without restriction, including without limitation
23
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
24
 * and/or sell copies of the Software, and to permit persons to whom the
25
 * Software is furnished to do so, subject to the following conditions:
26
 *
27
 * The above copyright notice and this permission notice shall be included in
28
 * all copies of this Software or works derived from this Software.
29
 *
30
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
31
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
33
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
35
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
36
 * DEALINGS IN THE SOFTWARE.
37
 ****************************************************************************/
38
39
/* $Id$ */
40
41
#include <assert.h>
42
#include "mapserver.h"
43
44
/* should be size of largest possible filename */
45
#define MS_PATH_BUF_SIZE 2048
46
static char szStaticResult[MS_PATH_BUF_SIZE];
47
48
/************************************************************************/
49
/*                        msFindFilenameStart()                         */
50
/************************************************************************/
51
52
static int msFindFilenameStart(const char *pszFilename)
53
54
0
{
55
0
  int iFileStart;
56
57
0
  for (iFileStart = strlen(pszFilename);
58
0
       iFileStart > 0 && pszFilename[iFileStart - 1] != '/' &&
59
0
       pszFilename[iFileStart - 1] != '\\';
60
0
       iFileStart--) {
61
0
  }
62
63
0
  return iFileStart;
64
0
}
65
66
/************************************************************************/
67
/*                           msGetBasename()                            */
68
/************************************************************************/
69
70
/**
71
 * Extract basename (non-directory, non-extension) portion of filename.
72
 *
73
 * Returns a string containing the file basename portion of the passed
74
 * name.  If there is no basename (passed value ends in trailing directory
75
 * separator, or filename starts with a dot) an empty string is returned.
76
 *
77
 * <pre>
78
 * msGetBasename( "abc/def.xyz" ) == "def"
79
 * msGetBasename( "abc/def" ) == "def"
80
 * msGetBasename( "abc/def/" ) == ""
81
 * </pre>
82
 *
83
 * @param pszFullFilename the full filename potentially including a path.
84
 *
85
 * @return just the non-directory, non-extension portion of the path in
86
 * an internal string which must not be freed.  The string
87
 * may be destroyed by the next ms filename handling call.
88
 */
89
90
const char *msGetBasename(const char *pszFullFilename)
91
92
0
{
93
0
  int iFileStart = msFindFilenameStart(pszFullFilename);
94
0
  int iExtStart, nLength;
95
96
0
  for (iExtStart = strlen(pszFullFilename);
97
0
       iExtStart > iFileStart && pszFullFilename[iExtStart] != '.';
98
0
       iExtStart--) {
99
0
  }
100
101
0
  if (iExtStart == iFileStart)
102
0
    iExtStart = strlen(pszFullFilename);
103
104
0
  nLength = iExtStart - iFileStart;
105
106
0
  assert(nLength < MS_PATH_BUF_SIZE);
107
108
0
  strlcpy(szStaticResult, pszFullFilename + iFileStart, nLength + 1);
109
110
0
  return szStaticResult;
111
0
}
112
113
/* Id: GDAL/port/cplgetsymbol.cpp,v 1.14 2004/11/11 20:40:38 fwarmerdam Exp */
114
/* ==================================================================== */
115
/*                  Unix Implementation                                 */
116
/* ==================================================================== */
117
#if defined(HAVE_DLFCN_H)
118
119
#define GOT_GETSYMBOL
120
121
#include <dlfcn.h>
122
123
/************************************************************************/
124
/*                            msGetSymbol()                            */
125
/************************************************************************/
126
127
/**
128
 * Fetch a function pointer from a shared library / DLL.
129
 *
130
 * This function is meant to abstract access to shared libraries and
131
 * DLLs and performs functions similar to dlopen()/dlsym() on Unix and
132
 * LoadLibrary() / GetProcAddress() on Windows.
133
 *
134
 * If no support for loading entry points from a shared library is available
135
 * this function will always return NULL.   Rules on when this function
136
 * issues a msError() or not are not currently well defined, and will have
137
 * to be resolved in the future.
138
 *
139
 * Currently msGetSymbol() doesn't try to:
140
 * <ul>
141
 *  <li> prevent the reference count on the library from going up
142
 *    for every request, or given any opportunity to unload
143
 *    the library.
144
 *  <li> Attempt to look for the library in non-standard
145
 *    locations.
146
 *  <li> Attempt to try variations on the symbol name, like
147
 *    pre-prending or post-pending an underscore.
148
 * </ul>
149
 *
150
 * Some of these issues may be worked on in the future.
151
 *
152
 * @param pszLibrary the name of the shared library or DLL containing
153
 * the function.  May contain path to file.  If not system supplies search
154
 * paths will be used.
155
 * @param pszSymbolName the name of the function to fetch a pointer to.
156
 * @return A pointer to the function if found, or NULL if the function isn't
157
 * found, or the shared library can't be loaded.
158
 */
159
160
233
void *msGetSymbol(const char *pszLibrary, const char *pszSymbolName) {
161
233
  void *pLibrary;
162
233
  void *pSymbol;
163
164
233
  pLibrary = dlopen(pszLibrary, RTLD_LAZY);
165
233
  if (pLibrary == NULL) {
166
229
    msSetError(MS_MISCERR, "Dynamic loading failed: %s", "msGetSymbol()",
167
229
               dlerror());
168
229
    return NULL;
169
229
  }
170
171
4
  pSymbol = dlsym(pLibrary, pszSymbolName);
172
173
#if (defined(__APPLE__) && defined(__MACH__))
174
  /* On mach-o systems, C symbols have a leading underscore and depending
175
   * on how dlcompat is configured it may or may not add the leading
176
   * underscore.  So if dlsym() fails add an underscore and try again.
177
   */
178
  if (pSymbol == NULL) {
179
    char withUnder[strlen(pszSymbolName) + 2];
180
    withUnder[0] = '_';
181
    withUnder[1] = 0;
182
    strcat(withUnder, pszSymbolName);
183
    pSymbol = dlsym(pLibrary, withUnder);
184
  }
185
#endif
186
187
4
  if (pSymbol == NULL) {
188
4
    msSetError(MS_MISCERR, "Dynamic loading failed: %s", "msGetSymbol()",
189
4
               dlerror());
190
4
    return NULL;
191
4
  }
192
193
  /* We accept leakage of pLibrary */
194
  /* coverity[leaked_storage] */
195
0
  return (pSymbol);
196
4
}
197
198
#endif /* def __unix__ && defined(HAVE_DLFCN_H) */
199
200
/* ==================================================================== */
201
/*                 Windows Implementation                               */
202
/* ==================================================================== */
203
#ifdef WIN32
204
205
#define GOT_GETSYMBOL
206
207
#include <windows.h>
208
209
/************************************************************************/
210
/*                            msGetSymbol()                            */
211
/************************************************************************/
212
213
void *msGetSymbol(const char *pszLibrary, const char *pszSymbolName) {
214
  void *pLibrary;
215
  void *pSymbol;
216
217
  pLibrary = LoadLibrary(pszLibrary);
218
  if (pLibrary == NULL) {
219
    msSetError(MS_MISCERR, "Can't load requested dynamic library: %s",
220
               "msGetSymbol()", pszLibrary);
221
    return NULL;
222
  }
223
224
  pSymbol = (void *)GetProcAddress((HINSTANCE)pLibrary, pszSymbolName);
225
226
  if (pSymbol == NULL) {
227
    msSetError(MS_MISCERR, "Can't find requested entry point: %s in lib %s",
228
               "msGetSymbol()", pszSymbolName, pLibrary);
229
    return NULL;
230
  }
231
232
  return (pSymbol);
233
}
234
235
#endif /* def _WIN32 */
236
237
/* ==================================================================== */
238
/*      Dummy implementation.                                           */
239
/* ==================================================================== */
240
241
#ifndef GOT_GETSYMBOL
242
243
/************************************************************************/
244
/*                            msGetSymbol()                            */
245
/*                                                                      */
246
/*      Dummy implementation.                                           */
247
/************************************************************************/
248
249
void *msGetSymbol(const char *pszLibrary, const char *pszEntryPoint) {
250
  msSetError(
251
      MS_MISCERR,
252
      "msGetSymbol(%s,%s) called.  Failed as this is stub implementation.",
253
      "msGetSymbol()", pszLibrary, pszEntryPoint);
254
  return NULL;
255
}
256
#endif