/src/gdal/frmts/wms/wmsutils.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /****************************************************************************** |
2 | | * |
3 | | * Project: WMS Client Driver |
4 | | * Purpose: Supporting utility functions for GDAL WMS driver. |
5 | | * Author: Adam Nowacki, nowak@xpam.de |
6 | | * |
7 | | ****************************************************************************** |
8 | | * Copyright (c) 2007, Adam Nowacki |
9 | | * |
10 | | * SPDX-License-Identifier: MIT |
11 | | ****************************************************************************/ |
12 | | |
13 | | #include "wmsdriver.h" |
14 | | |
15 | | OGRSpatialReference ProjToSRS(const CPLString &proj) |
16 | 32.9k | { |
17 | 32.9k | OGRSpatialReference sr; |
18 | 32.9k | sr.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER); |
19 | 32.9k | CPLString srs; |
20 | | |
21 | | /* We could of course recognize OSGEO:41001 to SetFromUserInput(), but this |
22 | | * hackish SRS */ |
23 | | /* is almost only used in the context of WMS */ |
24 | 32.9k | if (strcmp(proj.c_str(), "OSGEO:41001") == 0) |
25 | 0 | { |
26 | 0 | sr.importFromEPSG(3857); |
27 | 0 | } |
28 | 32.9k | else if (EQUAL(proj.c_str(), "EPSG:NONE")) |
29 | 0 | { |
30 | | // do nothing |
31 | 0 | } |
32 | 32.9k | else |
33 | 32.9k | { |
34 | 32.9k | sr.SetFromUserInput( |
35 | 32.9k | proj.c_str(), |
36 | 32.9k | OGRSpatialReference::SET_FROM_USER_INPUT_LIMITATIONS_get()); |
37 | 32.9k | } |
38 | 32.9k | return sr; |
39 | 32.9k | } |
40 | | |
41 | | // Terminates an URL base with either ? or &, so extra args can be appended |
42 | | void URLPrepare(CPLString &url) |
43 | 2.73k | { |
44 | 2.73k | if (url.find("?") == std::string::npos) |
45 | 0 | { |
46 | 0 | url.append("?"); |
47 | 0 | } |
48 | 2.73k | else |
49 | 2.73k | { |
50 | 2.73k | if (*url.rbegin() != '?' && *url.rbegin() != '&') |
51 | 1.72k | url.append("&"); |
52 | 2.73k | } |
53 | 2.73k | } |
54 | | |
55 | | CPLString BufferToVSIFile(GByte *buffer, size_t size) |
56 | 36 | { |
57 | 36 | CPLString file_name(VSIMemGenerateHiddenFilename("wmsresult.dat")); |
58 | 36 | VSILFILE *f = VSIFileFromMemBuffer(file_name.c_str(), buffer, size, false); |
59 | 36 | if (f == nullptr) |
60 | 0 | return CPLString(); |
61 | 36 | VSIFCloseL(f); |
62 | 36 | return file_name; |
63 | 36 | } |
64 | | |
65 | | int VersionStringToInt(const char *version) |
66 | 132k | { |
67 | 132k | if (version == nullptr) |
68 | 0 | return -1; |
69 | 132k | const char *p = version; |
70 | 132k | int v = 0; |
71 | 660k | for (int i = 3; i >= 0; --i) |
72 | 528k | { |
73 | 528k | int n = atoi(p); |
74 | 528k | if (n < 0 || n >= 100) |
75 | 0 | return -1; |
76 | 528k | v += (1 << (i * 8)) * n; |
77 | 891k | for (; (*p != '\0') && (*p != '.'); ++p) |
78 | 363k | ; |
79 | 528k | if (*p != '\0') |
80 | 231k | ++p; |
81 | 528k | } |
82 | 132k | return v; |
83 | 132k | } |
84 | | |
85 | | int StrToBool(const char *p) |
86 | 66.3k | { |
87 | 66.3k | if (p == nullptr) |
88 | 0 | return -1; |
89 | 66.3k | if (EQUAL(p, "1") || EQUAL(p, "true") || EQUAL(p, "yes") || |
90 | 66.3k | EQUAL(p, "enable") || EQUAL(p, "enabled") || EQUAL(p, "on")) |
91 | 33.4k | return 1; |
92 | 32.9k | if (EQUAL(p, "0") || EQUAL(p, "false") || EQUAL(p, "no") || |
93 | 32.9k | EQUAL(p, "disable") || EQUAL(p, "disabled") || EQUAL(p, "off")) |
94 | 32.9k | return 0; |
95 | 0 | return -1; |
96 | 32.9k | } |
97 | | |
98 | | int URLSearchAndReplace(CPLString *base, const char *search, const char *fmt, |
99 | | ...) |
100 | 1.45k | { |
101 | 1.45k | CPLString tmp; |
102 | 1.45k | va_list args; |
103 | | |
104 | 1.45k | size_t start = base->find(search); |
105 | 1.45k | if (start == std::string::npos) |
106 | 1.08k | { |
107 | 1.08k | return -1; |
108 | 1.08k | } |
109 | | |
110 | 368 | va_start(args, fmt); |
111 | 368 | tmp.vPrintf(fmt, args); |
112 | 368 | va_end(args); |
113 | | |
114 | 368 | base->replace(start, strlen(search), tmp); |
115 | 368 | return static_cast<int>(start); |
116 | 1.45k | } |
117 | | |
118 | | // decode s from base64, XMLencoded or read from the file name s |
119 | | const char *WMSUtilDecode(CPLString &s, const char *encoding) |
120 | 0 | { |
121 | 0 | if (EQUAL(encoding, "base64")) |
122 | 0 | { |
123 | 0 | std::vector<char> buffer(s.begin(), s.end()); |
124 | 0 | buffer.push_back('\0'); |
125 | 0 | int nSize = |
126 | 0 | CPLBase64DecodeInPlace(reinterpret_cast<GByte *>(&buffer[0])); |
127 | 0 | s.assign(&buffer[0], nSize); |
128 | 0 | } |
129 | 0 | else if (EQUAL(encoding, "XMLencoded")) |
130 | 0 | { |
131 | 0 | int len = static_cast<int>(s.size()); |
132 | 0 | char *result = CPLUnescapeString(s.c_str(), &len, CPLES_XML); |
133 | 0 | s.assign(result, static_cast<size_t>(len)); |
134 | 0 | CPLFree(result); |
135 | 0 | } |
136 | 0 | else if (EQUAL(encoding, "file")) |
137 | 0 | { // Not an encoding but an external file |
138 | 0 | VSILFILE *f = VSIFOpenL(s.c_str(), "rb"); |
139 | 0 | s.clear(); // Return an empty string if file can't be opened or read |
140 | 0 | if (f) |
141 | 0 | { |
142 | 0 | VSIFSeekL(f, 0, SEEK_END); |
143 | 0 | size_t size = static_cast<size_t>(VSIFTellL(f)); |
144 | 0 | VSIFSeekL(f, 0, SEEK_SET); |
145 | 0 | std::vector<char> buffer(size); |
146 | 0 | if (VSIFReadL(reinterpret_cast<void *>(&buffer[0]), size, 1, f)) |
147 | 0 | s.assign(&buffer[0], buffer.size()); |
148 | 0 | VSIFCloseL(f); |
149 | 0 | } |
150 | 0 | } |
151 | 0 | return s.c_str(); |
152 | 0 | } |