apr_strings.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. errno = 0;
  209. return APR_INT64_STRFN(nptr, endptr, base);
  210. #else
  211. const char *s;
  212. apr_int64_t acc;
  213. apr_int64_t val;
  214. int neg, any;
  215. char c;
  216. errno = 0;
  217. /*
  218. * Skip white space and pick up leading +/- sign if any.
  219. * If base is 0, allow 0x for hex and 0 for octal, else
  220. * assume decimal; if base is already 16, allow 0x.
  221. */
  222. s = nptr;
  223. do {
  224. c = *s++;
  225. } while (apr_isspace(c));
  226. if (c == '-') {
  227. neg = 1;
  228. c = *s++;
  229. } else {
  230. neg = 0;
  231. if (c == '+')
  232. c = *s++;
  233. }
  234. if ((base == 0 || base == 16) &&
  235. c == '0' && (*s == 'x' || *s == 'X')) {
  236. c = s[1];
  237. s += 2;
  238. base = 16;
  239. }
  240. if (base == 0)
  241. base = c == '0' ? 8 : 10;
  242. acc = any = 0;
  243. if (base < 2 || base > 36) {
  244. errno = EINVAL;
  245. if (endptr != NULL)
  246. *endptr = (char *)(any ? s - 1 : nptr);
  247. return acc;
  248. }
  249. /* The classic bsd implementation requires div/mod operators
  250. * to compute a cutoff. Benchmarking proves that is very, very
  251. * evil to some 32 bit processors. Instead, look for underflow
  252. * in both the mult and add/sub operation. Unlike the bsd impl,
  253. * we also work strictly in a signed int64 word as we haven't
  254. * implemented the unsigned type in win32.
  255. *
  256. * Set 'any' if any `digits' consumed; make it negative to indicate
  257. * overflow.
  258. */
  259. val = 0;
  260. for ( ; ; c = *s++) {
  261. if (c >= '0' && c <= '9')
  262. c -= '0';
  263. #if (('Z' - 'A') == 25)
  264. else if (c >= 'A' && c <= 'Z')
  265. c -= 'A' - 10;
  266. else if (c >= 'a' && c <= 'z')
  267. c -= 'a' - 10;
  268. #elif APR_CHARSET_EBCDIC
  269. else if (c >= 'A' && c <= 'I')
  270. c -= 'A' - 10;
  271. else if (c >= 'J' && c <= 'R')
  272. c -= 'J' - 19;
  273. else if (c >= 'S' && c <= 'Z')
  274. c -= 'S' - 28;
  275. else if (c >= 'a' && c <= 'i')
  276. c -= 'a' - 10;
  277. else if (c >= 'j' && c <= 'r')
  278. c -= 'j' - 19;
  279. else if (c >= 's' && c <= 'z')
  280. c -= 'z' - 28;
  281. #else
  282. #error "CANNOT COMPILE apr_strtoi64(), only ASCII and EBCDIC supported"
  283. #endif
  284. else
  285. break;
  286. if (c >= base)
  287. break;
  288. val *= base;
  289. if ( (any < 0) /* already noted an over/under flow - short circuit */
  290. || (neg && (val > acc || (val -= c) > acc)) /* underflow */
  291. || (!neg && (val < acc || (val += c) < acc))) { /* overflow */
  292. any = -1; /* once noted, over/underflows never go away */
  293. #ifdef APR_STRTOI64_OVERFLOW_IS_BAD_CHAR
  294. break;
  295. #endif
  296. } else {
  297. acc = val;
  298. any = 1;
  299. }
  300. }
  301. if (any < 0) {
  302. acc = neg ? INT64_MIN : INT64_MAX;
  303. errno = ERANGE;
  304. } else if (!any) {
  305. errno = EINVAL;
  306. }
  307. if (endptr != NULL)
  308. *endptr = (char *)(any ? s - 1 : nptr);
  309. return (acc);
  310. #endif
  311. }
  312. APR_DECLARE(apr_int64_t) apr_atoi64(const char *buf)
  313. {
  314. return apr_strtoi64(buf, NULL, 10);
  315. }
  316. APR_DECLARE(char *) apr_itoa(apr_pool_t *p, int n)
  317. {
  318. const int BUFFER_SIZE = sizeof(int) * 3 + 2;
  319. char *buf = apr_palloc(p, BUFFER_SIZE);
  320. char *start = buf + BUFFER_SIZE - 1;
  321. int negative;
  322. if (n < 0) {
  323. negative = 1;
  324. n = -n;
  325. }
  326. else {
  327. negative = 0;
  328. }
  329. *start = 0;
  330. do {
  331. *--start = '0' + (n % 10);
  332. n /= 10;
  333. } while (n);
  334. if (negative) {
  335. *--start = '-';
  336. }
  337. return start;
  338. }
  339. APR_DECLARE(char *) apr_ltoa(apr_pool_t *p, long n)
  340. {
  341. const int BUFFER_SIZE = sizeof(long) * 3 + 2;
  342. char *buf = apr_palloc(p, BUFFER_SIZE);
  343. char *start = buf + BUFFER_SIZE - 1;
  344. int negative;
  345. if (n < 0) {
  346. negative = 1;
  347. n = -n;
  348. }
  349. else {
  350. negative = 0;
  351. }
  352. *start = 0;
  353. do {
  354. *--start = (char)('0' + (n % 10));
  355. n /= 10;
  356. } while (n);
  357. if (negative) {
  358. *--start = '-';
  359. }
  360. return start;
  361. }
  362. APR_DECLARE(char *) apr_off_t_toa(apr_pool_t *p, apr_off_t n)
  363. {
  364. const int BUFFER_SIZE = sizeof(apr_off_t) * 3 + 2;
  365. char *buf = apr_palloc(p, BUFFER_SIZE);
  366. char *start = buf + BUFFER_SIZE - 1;
  367. int negative;
  368. if (n < 0) {
  369. negative = 1;
  370. n = -n;
  371. }
  372. else {
  373. negative = 0;
  374. }
  375. *start = 0;
  376. do {
  377. *--start = '0' + (char)(n % 10);
  378. n /= 10;
  379. } while (n);
  380. if (negative) {
  381. *--start = '-';
  382. }
  383. return start;
  384. }
  385. APR_DECLARE(char *) apr_strfsize(apr_off_t size, char *buf)
  386. {
  387. const char ord[] = "KMGTPE";
  388. const char *o = ord;
  389. int remain;
  390. if (size < 0) {
  391. return strcpy(buf, " - ");
  392. }
  393. if (size < 973) {
  394. if (apr_snprintf(buf, 5, "%3d ", (int) size) < 0)
  395. return strcpy(buf, "****");
  396. return buf;
  397. }
  398. do {
  399. remain = (int)(size & 1023);
  400. size >>= 10;
  401. if (size >= 973) {
  402. ++o;
  403. continue;
  404. }
  405. if (size < 9 || (size == 9 && remain < 973)) {
  406. if ((remain = ((remain * 5) + 256) / 512) >= 10)
  407. ++size, remain = 0;
  408. if (apr_snprintf(buf, 5, "%d.%d%c", (int) size, remain, *o) < 0)
  409. return strcpy(buf, "****");
  410. return buf;
  411. }
  412. if (remain >= 512)
  413. ++size;
  414. if (apr_snprintf(buf, 5, "%3d%c", (int) size, *o) < 0)
  415. return strcpy(buf, "****");
  416. return buf;
  417. } while (1);
  418. }