value.c 8.7 KB

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