2
0

apr_strings.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. 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 express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * Copyright (c) 1990, 1993
  18. * The Regents of the University of California. All rights reserved.
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions
  22. * are met:
  23. * 1. Redistributions of source code must retain the above copyright
  24. * notice, this list of conditions and the following disclaimer.
  25. * 2. Redistributions in binary form must reproduce the above copyright
  26. * notice, this list of conditions and the following disclaimer in the
  27. * documentation and/or other materials provided with the distribution.
  28. * 3. All advertising materials mentioning features or use of this software
  29. * must display the following acknowledgement:
  30. * This product includes software developed by the University of
  31. * California, Berkeley and its contributors.
  32. * 4. Neither the name of the University nor the names of its contributors
  33. * may be used to endorse or promote products derived from this software
  34. * without specific prior written permission.
  35. *
  36. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  37. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  38. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  39. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  40. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  41. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  42. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  44. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  45. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. */
  48. #include "apr.h"
  49. #include "apr_strings.h"
  50. #include "apr_general.h"
  51. #include "apr_private.h"
  52. #include "apr_lib.h"
  53. #define APR_WANT_STDIO
  54. #define APR_WANT_STRFUNC
  55. #include "apr_want.h"
  56. #ifdef HAVE_STDDEF_H
  57. #include <stddef.h> /* NULL */
  58. #endif
  59. #ifdef HAVE_STDLIB_H
  60. #include <stdlib.h> /* strtol and strtoll */
  61. #endif
  62. /** this is used to cache lengths in apr_pstrcat */
  63. #define MAX_SAVED_LENGTHS 6
  64. APR_DECLARE(char *) apr_pstrdup(apr_pool_t *a, const char *s)
  65. {
  66. char *res;
  67. apr_size_t len;
  68. if (s == NULL) {
  69. return NULL;
  70. }
  71. len = strlen(s) + 1;
  72. res = apr_palloc(a, len);
  73. memcpy(res, s, len);
  74. return res;
  75. }
  76. APR_DECLARE(char *) apr_pstrndup(apr_pool_t *a, const char *s, apr_size_t n)
  77. {
  78. char *res;
  79. const char *end;
  80. if (s == NULL) {
  81. return NULL;
  82. }
  83. end = memchr(s, '\0', n);
  84. if (end != NULL)
  85. n = end - s;
  86. res = apr_palloc(a, n + 1);
  87. memcpy(res, s, n);
  88. res[n] = '\0';
  89. return res;
  90. }
  91. APR_DECLARE(char *) apr_pstrmemdup(apr_pool_t *a, const char *s, apr_size_t n)
  92. {
  93. char *res;
  94. if (s == NULL) {
  95. return NULL;
  96. }
  97. res = apr_palloc(a, n + 1);
  98. memcpy(res, s, n);
  99. res[n] = '\0';
  100. return res;
  101. }
  102. APR_DECLARE(void *) apr_pmemdup(apr_pool_t *a, const void *m, apr_size_t n)
  103. {
  104. void *res;
  105. if (m == NULL)
  106. return NULL;
  107. res = apr_palloc(a, n);
  108. memcpy(res, m, n);
  109. return res;
  110. }
  111. APR_DECLARE_NONSTD(char *) apr_pstrcat(apr_pool_t *a, ...)
  112. {
  113. char *cp, *argp, *res;
  114. apr_size_t saved_lengths[MAX_SAVED_LENGTHS];
  115. int nargs = 0;
  116. /* Pass one --- find length of required string */
  117. apr_size_t len = 0;
  118. va_list adummy;
  119. va_start(adummy, a);
  120. while ((cp = va_arg(adummy, char *)) != NULL) {
  121. apr_size_t cplen = strlen(cp);
  122. if (nargs < MAX_SAVED_LENGTHS) {
  123. saved_lengths[nargs++] = cplen;
  124. }
  125. len += cplen;
  126. }
  127. va_end(adummy);
  128. /* Allocate the required string */
  129. res = (char *) apr_palloc(a, len + 1);
  130. cp = res;
  131. /* Pass two --- copy the argument strings into the result space */
  132. va_start(adummy, a);
  133. nargs = 0;
  134. while ((argp = va_arg(adummy, char *)) != NULL) {
  135. if (nargs < MAX_SAVED_LENGTHS) {
  136. len = saved_lengths[nargs++];
  137. }
  138. else {
  139. len = strlen(argp);
  140. }
  141. memcpy(cp, argp, len);
  142. cp += len;
  143. }
  144. va_end(adummy);
  145. /* Return the result string */
  146. *cp = '\0';
  147. return res;
  148. }
  149. APR_DECLARE(char *) apr_pstrcatv(apr_pool_t *a, const struct iovec *vec,
  150. apr_size_t nvec, apr_size_t *nbytes)
  151. {
  152. apr_size_t i;
  153. apr_size_t len;
  154. const struct iovec *src;
  155. char *res;
  156. char *dst;
  157. /* Pass one --- find length of required string */
  158. len = 0;
  159. src = vec;
  160. for (i = nvec; i; i--) {
  161. len += src->iov_len;
  162. src++;
  163. }
  164. if (nbytes) {
  165. *nbytes = len;
  166. }
  167. /* Allocate the required string */
  168. res = (char *) apr_palloc(a, len + 1);
  169. /* Pass two --- copy the argument strings into the result space */
  170. src = vec;
  171. dst = res;
  172. for (i = nvec; i; i--) {
  173. memcpy(dst, src->iov_base, src->iov_len);
  174. dst += src->iov_len;
  175. src++;
  176. }
  177. /* Return the result string */
  178. *dst = '\0';
  179. return res;
  180. }
  181. #if (!APR_HAVE_MEMCHR)
  182. void *memchr(const void *s, int c, size_t n)
  183. {
  184. const char *cp;
  185. for (cp = s; n > 0; n--, cp++) {
  186. if (*cp == c)
  187. return (char *) cp; /* Casting away the const here */
  188. }
  189. return NULL;
  190. }
  191. #endif
  192. #ifndef INT64_MAX
  193. #define INT64_MAX APR_INT64_C(0x7fffffffffffffff)
  194. #endif
  195. #ifndef INT64_MIN
  196. #define INT64_MIN (-APR_INT64_C(0x7fffffffffffffff) - APR_INT64_C(1))
  197. #endif
  198. APR_DECLARE(apr_status_t) apr_strtoff(apr_off_t *offset, const char *nptr,
  199. char **endptr, int base)
  200. {
  201. errno = 0;
  202. *offset = APR_OFF_T_STRFN(nptr, endptr, base);
  203. return APR_FROM_OS_ERROR(errno);
  204. }
  205. APR_DECLARE(apr_int64_t) apr_strtoi64(const char *nptr, char **endptr, int base)
  206. {
  207. #ifdef APR_INT64_STRFN
  208. return APR_INT64_STRFN(nptr, endptr, base);
  209. #else
  210. const char *s;
  211. apr_int64_t acc;
  212. apr_int64_t val;
  213. int neg, any;
  214. char c;
  215. /*
  216. * Skip white space and pick up leading +/- sign if any.
  217. * If base is 0, allow 0x for hex and 0 for octal, else
  218. * assume decimal; if base is already 16, allow 0x.
  219. */
  220. s = nptr;
  221. do {
  222. c = *s++;
  223. } while (apr_isspace(c));
  224. if (c == '-') {
  225. neg = 1;
  226. c = *s++;
  227. } else {
  228. neg = 0;
  229. if (c == '+')
  230. c = *s++;
  231. }
  232. if ((base == 0 || base == 16) &&
  233. c == '0' && (*s == 'x' || *s == 'X')) {
  234. c = s[1];
  235. s += 2;
  236. base = 16;
  237. }
  238. if (base == 0)
  239. base = c == '0' ? 8 : 10;
  240. acc = any = 0;
  241. if (base < 2 || base > 36) {
  242. errno = EINVAL;
  243. if (endptr != NULL)
  244. *endptr = (char *)(any ? s - 1 : nptr);
  245. return acc;
  246. }
  247. /* The classic bsd implementation requires div/mod operators
  248. * to compute a cutoff. Benchmarking proves that is very, very
  249. * evil to some 32 bit processors. Instead, look for underflow
  250. * in both the mult and add/sub operation. Unlike the bsd impl,
  251. * we also work strictly in a signed int64 word as we haven't
  252. * implemented the unsigned type in win32.
  253. *
  254. * Set 'any' if any `digits' consumed; make it negative to indicate
  255. * overflow.
  256. */
  257. val = 0;
  258. for ( ; ; c = *s++) {
  259. if (c >= '0' && c <= '9')
  260. c -= '0';
  261. #if (('Z' - 'A') == 25)
  262. else if (c >= 'A' && c <= 'Z')
  263. c -= 'A' - 10;
  264. else if (c >= 'a' && c <= 'z')
  265. c -= 'a' - 10;
  266. #elif APR_CHARSET_EBCDIC
  267. else if (c >= 'A' && c <= 'I')
  268. c -= 'A' - 10;
  269. else if (c >= 'J' && c <= 'R')
  270. c -= 'J' - 19;
  271. else if (c >= 'S' && c <= 'Z')
  272. c -= 'S' - 28;
  273. else if (c >= 'a' && c <= 'i')
  274. c -= 'a' - 10;
  275. else if (c >= 'j' && c <= 'r')
  276. c -= 'j' - 19;
  277. else if (c >= 's' && c <= 'z')
  278. c -= 'z' - 28;
  279. #else
  280. #error "CANNOT COMPILE apr_strtoi64(), only ASCII and EBCDIC supported"
  281. #endif
  282. else
  283. break;
  284. if (c >= base)
  285. break;
  286. val *= base;
  287. if ( (any < 0) /* already noted an over/under flow - short circuit */
  288. || (neg && (val > acc || (val -= c) > acc)) /* underflow */
  289. || (!neg && (val < acc || (val += c) < acc))) { /* overflow */
  290. any = -1; /* once noted, over/underflows never go away */
  291. #ifdef APR_STRTOI64_OVERFLOW_IS_BAD_CHAR
  292. break;
  293. #endif
  294. } else {
  295. acc = val;
  296. any = 1;
  297. }
  298. }
  299. if (any < 0) {
  300. acc = neg ? INT64_MIN : INT64_MAX;
  301. errno = ERANGE;
  302. } else if (!any) {
  303. errno = EINVAL;
  304. }
  305. if (endptr != NULL)
  306. *endptr = (char *)(any ? s - 1 : nptr);
  307. return (acc);
  308. #endif
  309. }
  310. APR_DECLARE(apr_int64_t) apr_atoi64(const char *buf)
  311. {
  312. return apr_strtoi64(buf, NULL, 10);
  313. }
  314. APR_DECLARE(char *) apr_itoa(apr_pool_t *p, int n)
  315. {
  316. const int BUFFER_SIZE = sizeof(int) * 3 + 2;
  317. char *buf = apr_palloc(p, BUFFER_SIZE);
  318. char *start = buf + BUFFER_SIZE - 1;
  319. int negative;
  320. if (n < 0) {
  321. negative = 1;
  322. n = -n;
  323. }
  324. else {
  325. negative = 0;
  326. }
  327. *start = 0;
  328. do {
  329. *--start = '0' + (n % 10);
  330. n /= 10;
  331. } while (n);
  332. if (negative) {
  333. *--start = '-';
  334. }
  335. return start;
  336. }
  337. APR_DECLARE(char *) apr_ltoa(apr_pool_t *p, long n)
  338. {
  339. const int BUFFER_SIZE = sizeof(long) * 3 + 2;
  340. char *buf = apr_palloc(p, BUFFER_SIZE);
  341. char *start = buf + BUFFER_SIZE - 1;
  342. int negative;
  343. if (n < 0) {
  344. negative = 1;
  345. n = -n;
  346. }
  347. else {
  348. negative = 0;
  349. }
  350. *start = 0;
  351. do {
  352. *--start = (char)('0' + (n % 10));
  353. n /= 10;
  354. } while (n);
  355. if (negative) {
  356. *--start = '-';
  357. }
  358. return start;
  359. }
  360. APR_DECLARE(char *) apr_off_t_toa(apr_pool_t *p, apr_off_t n)
  361. {
  362. const int BUFFER_SIZE = sizeof(apr_off_t) * 3 + 2;
  363. char *buf = apr_palloc(p, BUFFER_SIZE);
  364. char *start = buf + BUFFER_SIZE - 1;
  365. int negative;
  366. if (n < 0) {
  367. negative = 1;
  368. n = -n;
  369. }
  370. else {
  371. negative = 0;
  372. }
  373. *start = 0;
  374. do {
  375. *--start = '0' + (char)(n % 10);
  376. n /= 10;
  377. } while (n);
  378. if (negative) {
  379. *--start = '-';
  380. }
  381. return start;
  382. }
  383. APR_DECLARE(char *) apr_strfsize(apr_off_t size, char *buf)
  384. {
  385. const char ord[] = "KMGTPE";
  386. const char *o = ord;
  387. int remain;
  388. if (size < 0) {
  389. return strcpy(buf, " - ");
  390. }
  391. if (size < 973) {
  392. if (apr_snprintf(buf, 5, "%3d ", (int) size) < 0)
  393. return strcpy(buf, "****");
  394. return buf;
  395. }
  396. do {
  397. remain = (int)(size & 1023);
  398. size >>= 10;
  399. if (size >= 973) {
  400. ++o;
  401. continue;
  402. }
  403. if (size < 9 || (size == 9 && remain < 973)) {
  404. if ((remain = ((remain * 5) + 256) / 512) >= 10)
  405. ++size, remain = 0;
  406. if (apr_snprintf(buf, 5, "%d.%d%c", (int) size, remain, *o) < 0)
  407. return strcpy(buf, "****");
  408. return buf;
  409. }
  410. if (remain >= 512)
  411. ++size;
  412. if (apr_snprintf(buf, 5, "%3d%c", (int) size, *o) < 0)
  413. return strcpy(buf, "****");
  414. return buf;
  415. } while (1);
  416. }