value.c 10 KB

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