| 2213 | */ |
| 2214 | |
| 2215 | char *CPLEscapeString(const char *pszInput, int nLength, int nScheme) |
| 2216 | { |
| 2217 | const size_t szLength = |
| 2218 | (nLength < 0) ? strlen(pszInput) : static_cast<size_t>(nLength); |
| 2219 | #define nLength no_longer_use_me |
| 2220 | |
| 2221 | size_t nSizeAlloc = 1; |
| 2222 | #if SIZEOF_VOIDP < 8 |
| 2223 | bool bWrapAround = false; |
| 2224 | const auto IncSizeAlloc = [&nSizeAlloc, &bWrapAround](size_t inc) |
| 2225 | { |
| 2226 | constexpr size_t SZ_MAX = std::numeric_limits<size_t>::max(); |
| 2227 | if (nSizeAlloc > SZ_MAX - inc) |
| 2228 | { |
| 2229 | bWrapAround = true; |
| 2230 | nSizeAlloc = 0; |
| 2231 | } |
| 2232 | nSizeAlloc += inc; |
| 2233 | }; |
| 2234 | #else |
| 2235 | const auto IncSizeAlloc = [&nSizeAlloc](size_t inc) { nSizeAlloc += inc; }; |
| 2236 | #endif |
| 2237 | |
| 2238 | if (nScheme == CPLES_BackslashQuotable) |
| 2239 | { |
| 2240 | for (size_t iIn = 0; iIn < szLength; iIn++) |
| 2241 | { |
| 2242 | if (pszInput[iIn] == '\0' || pszInput[iIn] == '\n' || |
| 2243 | pszInput[iIn] == '"' || pszInput[iIn] == '\\') |
| 2244 | IncSizeAlloc(2); |
| 2245 | else |
| 2246 | IncSizeAlloc(1); |
| 2247 | } |
| 2248 | } |
| 2249 | else if (nScheme == CPLES_XML || nScheme == CPLES_XML_BUT_QUOTES) |
| 2250 | { |
| 2251 | for (size_t iIn = 0; iIn < szLength; ++iIn) |
| 2252 | { |
| 2253 | if (pszInput[iIn] == '<') |
| 2254 | { |
| 2255 | IncSizeAlloc(4); |
| 2256 | } |
| 2257 | else if (pszInput[iIn] == '>') |
| 2258 | { |
| 2259 | IncSizeAlloc(4); |
| 2260 | } |
| 2261 | else if (pszInput[iIn] == '&') |
| 2262 | { |
| 2263 | IncSizeAlloc(5); |
| 2264 | } |
| 2265 | else if (pszInput[iIn] == '"' && nScheme != CPLES_XML_BUT_QUOTES) |
| 2266 | { |
| 2267 | IncSizeAlloc(6); |
| 2268 | } |
| 2269 | // Python 2 does not display the UTF-8 character corresponding |
| 2270 | // to the byte-order mark (BOM), so escape it. |
| 2271 | else if ((reinterpret_cast<const unsigned char *>(pszInput))[iIn] == |
| 2272 | 0xEF && |
no test coverage detected