value.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. #include <config.h>
  11. #endif
  12. /* value.c - routines for dealing with values */
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <sys/types.h>
  16. #include "syntax.h"
  17. /*
  18. * Do not use the SDK ldap_utf8isspace directly until it is faster
  19. * than this one.
  20. */
  21. static int
  22. utf8isspace_fast(char *s)
  23. {
  24. register unsigned char c = *(unsigned char *)s;
  25. if (0x80 & c)
  26. return (ldap_utf8isspace(s));
  27. switch (c) {
  28. case 0x09:
  29. case 0x0A:
  30. case 0x0B:
  31. case 0x0C:
  32. case 0x0D:
  33. case 0x20:
  34. return 1;
  35. default:
  36. break;
  37. }
  38. return 0;
  39. }
  40. /*
  41. ** This function is used to normalizes search filter components,
  42. ** and attribute values.
  43. **
  44. ** jcm: I added the trim_spaces flag since this function
  45. ** was incorrectly modifying search filter components. A search
  46. ** of the form "cn=a* b*" (note the space) would be wrongly
  47. ** normalized into "cn=a*b*", because this function is called
  48. ** once for "a" and once for " b".
  49. ** richm 20070917 - added integer syntax - note that this implementation
  50. ** of integer syntax tries to mimic the old implementation (atol) as much
  51. ** as possible - leading spaces are ignored, then the optional hyphen for
  52. ** negative numbers, then leading 0s. That is
  53. ** " -0000000000001" should normalize to "-1" which is what atol() does
  54. ** Also note that this deviates from rfc 4517 INTEGER syntax, but we must
  55. ** support legacy clients for the time being
  56. */
  57. /*
  58. * alt stores the normalized value in case the normalized value is longer
  59. * than the original value. It may happen the value is DN.
  60. */
  61. void
  62. value_normalize_ext(
  63. char *s,
  64. int syntax,
  65. int trim_spaces,
  66. char **alt)
  67. {
  68. char *head = s;
  69. char *d;
  70. int prevspace, curspace;
  71. if (NULL == alt) {
  72. return;
  73. }
  74. *alt = NULL;
  75. if (NULL == s) {
  76. return;
  77. }
  78. if (!(syntax & SYNTAX_CIS) && !(syntax & SYNTAX_CES)) {
  79. return;
  80. }
  81. if (syntax & SYNTAX_DN) {
  82. char *dest = NULL;
  83. size_t dlen = 0;
  84. int rc = slapi_dn_normalize_case_ext(s, 0, &dest, &dlen);
  85. if (rc > 0) {
  86. *alt = dest;
  87. } else if (rc == 0) { /* normalized in line; not terminated */
  88. *(dest + dlen) = '\0';
  89. }
  90. return;
  91. }
  92. d = s;
  93. if (trim_spaces) {
  94. /* strip leading blanks */
  95. while (utf8isspace_fast(s)) {
  96. LDAP_UTF8INC(s);
  97. }
  98. }
  99. /* for int syntax, look for leading sign, then trim 0s */
  100. /* have to do this after trimming spaces */
  101. if (syntax & SYNTAX_INT) {
  102. int foundsign = 0;
  103. int foundzero = 0;
  104. if (*s == '-') {
  105. foundsign = 1;
  106. LDAP_UTF8INC(s);
  107. }
  108. while (*s && (*s == '0')) {
  109. foundzero = 1;
  110. LDAP_UTF8INC(s);
  111. }
  112. if (foundzero && !*s) { /* value is all zeros */
  113. *d++ = '0'; /* set value to a single zero */
  114. } else if (foundsign && (s > d)) {
  115. /* if there is a hyphen, make sure it is just to the left
  116. of the first significant (i.e. non-zero) digit e.g.
  117. convert -00000001 to -1 */
  118. *d++ = '-';
  119. }
  120. /* s should now point at the first significant digit/char */
  121. }
  122. /* handle value of all spaces - turn into single space */
  123. /* unless space insensitive syntax or int - turn into zero length string */
  124. if (*s == '\0' && s != d) {
  125. if (!(syntax & SYNTAX_SI) && !(syntax & SYNTAX_INT)) {
  126. *d++ = ' ';
  127. }
  128. *d = '\0';
  129. return;
  130. }
  131. prevspace = 0;
  132. while (*s) {
  133. curspace = utf8isspace_fast(s);
  134. /* ignore spaces and '-' in telephone numbers */
  135. if ((syntax & SYNTAX_TEL) && (curspace || *s == '-')) {
  136. LDAP_UTF8INC(s);
  137. continue;
  138. }
  139. /* ignore all spaces if this is a space insensitive value */
  140. if ((syntax & SYNTAX_SI) && curspace) {
  141. LDAP_UTF8INC(s);
  142. continue;
  143. }
  144. /* compress multiple blanks */
  145. if (prevspace && curspace) {
  146. LDAP_UTF8INC(s);
  147. continue;
  148. }
  149. prevspace = curspace;
  150. if (syntax & SYNTAX_CIS) {
  151. int ssz, dsz;
  152. slapi_utf8ToLower((unsigned char *)s, (unsigned char *)d, &ssz, &dsz);
  153. s += ssz;
  154. d += dsz;
  155. } else {
  156. char *np;
  157. int sz;
  158. np = ldap_utf8next(s);
  159. if (np == NULL || np == s)
  160. break;
  161. sz = np - s;
  162. memmove(d, s, sz);
  163. d += sz;
  164. s += sz;
  165. }
  166. }
  167. *d = '\0';
  168. /* strip trailing blanks */
  169. if (prevspace && trim_spaces) {
  170. char *nd;
  171. nd = ldap_utf8prev(d);
  172. while (nd && nd >= head && utf8isspace_fast(nd)) {
  173. d = nd;
  174. nd = ldap_utf8prev(d);
  175. *d = '\0';
  176. }
  177. }
  178. }
  179. void
  180. value_normalize(
  181. char *s __attribute__((unused)),
  182. int syntax __attribute__((unused)),
  183. int trim_spaces __attribute__((unused)))
  184. {
  185. /* deprecated */
  186. }
  187. int
  188. value_cmp(
  189. struct berval *v1,
  190. struct berval *v2,
  191. int syntax,
  192. int normalize)
  193. {
  194. int rc = 0;
  195. struct berval bvcopy1;
  196. struct berval bvcopy2;
  197. char little_buffer[64];
  198. size_t buffer_space = sizeof(little_buffer);
  199. int buffer_offset = 0;
  200. int free_v1 = 0;
  201. int free_v2 = 0;
  202. int v1sign = 1, v2sign = 1; /* default to positive */
  203. char *alt = NULL;
  204. // check NULL values before normalization
  205. if (!v1->bv_val) {
  206. if (v2->bv_val)
  207. rc = -1;
  208. goto done;
  209. }
  210. if (!v2->bv_val) {
  211. rc = 1;
  212. goto done;
  213. }
  214. /* This code used to call malloc up to four times in the copying
  215. * of attributes to be normalized. Now we attempt to keep everything
  216. * on the stack and only malloc if the data is big
  217. */
  218. if (normalize & 1) {
  219. /* Do we have space in the little buffer ? */
  220. if (v1->bv_len < buffer_space) {
  221. bvcopy1.bv_len = v1->bv_len;
  222. SAFEMEMCPY(&little_buffer[buffer_offset], v1->bv_val, v1->bv_len);
  223. bvcopy1.bv_val = &little_buffer[buffer_offset];
  224. bvcopy1.bv_val[v1->bv_len] = '\0';
  225. v1 = &bvcopy1;
  226. } else {
  227. v1 = ber_bvdup(v1);
  228. free_v1 = 1;
  229. }
  230. value_normalize_ext(v1->bv_val, syntax,
  231. 1 /* trim leading blanks */, &alt);
  232. if (alt) {
  233. int inserted = 0;
  234. if (free_v1) {
  235. slapi_ch_free_string(&v1->bv_val);
  236. v1->bv_val = alt;
  237. v1->bv_len = strlen(alt);
  238. inserted = 1;
  239. } else {
  240. if (strlen(alt) < buffer_space) {
  241. v1->bv_len = strlen(alt);
  242. /* Copying to little_buffer */
  243. SAFEMEMCPY(v1->bv_val, alt, v1->bv_len);
  244. *(v1->bv_val + v1->bv_len) = '\0';
  245. } else {
  246. free_v1 = 1;
  247. v1 = (struct berval *)slapi_ch_malloc(sizeof(struct berval));
  248. v1->bv_val = alt;
  249. v1->bv_len = strlen(alt);
  250. inserted = 1;
  251. }
  252. }
  253. if (!inserted) {
  254. slapi_ch_free_string(&alt);
  255. }
  256. }
  257. if (!free_v1) {
  258. buffer_space -= v1->bv_len + 1;
  259. buffer_offset += v1->bv_len + 1;
  260. }
  261. }
  262. if (normalize & 2) {
  263. /* Do we have space in the little buffer ? */
  264. if (v2->bv_len < buffer_space) {
  265. bvcopy2.bv_len = v2->bv_len;
  266. SAFEMEMCPY(&little_buffer[buffer_offset], v2->bv_val, v2->bv_len);
  267. bvcopy2.bv_val = &little_buffer[buffer_offset];
  268. bvcopy2.bv_val[v2->bv_len] = '\0';
  269. v2 = &bvcopy2;
  270. } else {
  271. v2 = ber_bvdup(v2);
  272. free_v2 = 1;
  273. }
  274. value_normalize_ext(v2->bv_val, syntax,
  275. 1 /* trim leading blanks */, &alt);
  276. if (alt) {
  277. int inserted = 0;
  278. if (free_v2) {
  279. slapi_ch_free_string(&v2->bv_val);
  280. v2->bv_val = alt;
  281. v2->bv_len = strlen(alt);
  282. inserted = 1;
  283. } else {
  284. if (strlen(alt) < buffer_space) {
  285. v2->bv_len = strlen(alt);
  286. /* Copying to little_buffer */
  287. SAFEMEMCPY(v2->bv_val, alt, v2->bv_len);
  288. *(v2->bv_val + v2->bv_len) = '\0';
  289. } else {
  290. free_v2 = 1;
  291. v2 = (struct berval *)slapi_ch_malloc(sizeof(struct berval));
  292. v2->bv_val = alt;
  293. v2->bv_len = strlen(alt);
  294. inserted = 1;
  295. }
  296. }
  297. if (!inserted) {
  298. slapi_ch_free_string(&alt);
  299. }
  300. }
  301. if (!free_v2) {
  302. buffer_space -= v2->bv_len + 1;
  303. buffer_offset += v2->bv_len + 1;
  304. }
  305. }
  306. if (normalize) {
  307. // check NULL values after normalization
  308. if (!v1->bv_val) {
  309. if (v2->bv_val)
  310. rc = -1;
  311. goto done;
  312. }
  313. if (!v2->bv_val) {
  314. rc = 1;
  315. goto done;
  316. }
  317. }
  318. if (syntax & SYNTAX_INT) {
  319. v1sign = *v1->bv_val != '-';
  320. v2sign = *v2->bv_val != '-';
  321. rc = v1sign - v2sign;
  322. if (rc) { /* one is positive, one is negative */
  323. goto done;
  324. }
  325. /* check magnitude */
  326. /* unfortunately, bv_len cannot be trusted - bv_len is not
  327. updated during or after value_normalize */
  328. rc = (strlen(v1->bv_val) - strlen(v2->bv_val));
  329. if (rc) {
  330. rc = (rc > 0) ? 1 : -1;
  331. if (!v1sign && !v2sign) { /* both negative */
  332. rc = 0 - rc; /* flip it */
  333. }
  334. goto done;
  335. }
  336. }
  337. if (syntax & SYNTAX_CIS) {
  338. rc = slapi_utf8casecmp((unsigned char *)v1->bv_val,
  339. (unsigned char *)v2->bv_val);
  340. } else if (syntax & SYNTAX_CES) {
  341. rc = strcmp(v1->bv_val, v2->bv_val);
  342. } else { /* error - unknown syntax */
  343. slapi_log_err(SLAPI_LOG_PLUGIN, SYNTAX_PLUGIN_SUBSYSTEM,
  344. "value_cmp - invalid syntax [%d]\n", syntax);
  345. }
  346. if ((syntax & SYNTAX_INT) && !v1sign && !v2sign) { /* both negative */
  347. rc = 0 - rc; /* flip it */
  348. }
  349. done:
  350. if ((normalize & 1) && free_v1) {
  351. ber_bvfree(v1);
  352. }
  353. if ((normalize & 2) && free_v2) {
  354. ber_bvfree(v2);
  355. }
  356. return (rc);
  357. }