value.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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 (NULL == s) {
  104. return;
  105. }
  106. if ( ! (syntax & SYNTAX_CIS) && ! (syntax & SYNTAX_CES) ) {
  107. return;
  108. }
  109. if ( syntax & SYNTAX_DN ) {
  110. char *dest = NULL;
  111. size_t dlen = 0;
  112. int rc = slapi_dn_normalize_case_ext(s, 0, &dest, &dlen);
  113. if (rc > 0) {
  114. *alt = dest;
  115. } else if (rc == 0) { /* normalized in line; not terminated */
  116. *(dest + dlen) = '\0';
  117. }
  118. return;
  119. }
  120. d = s;
  121. if (trim_spaces) {
  122. /* strip leading blanks */
  123. while (utf8isspace_fast(s)) {
  124. LDAP_UTF8INC(s);
  125. }
  126. }
  127. /* for int syntax, look for leading sign, then trim 0s */
  128. /* have to do this after trimming spaces */
  129. if (syntax & SYNTAX_INT) {
  130. int foundsign = 0;
  131. int foundzero = 0;
  132. if (*s == '-') {
  133. foundsign = 1;
  134. LDAP_UTF8INC(s);
  135. }
  136. while (*s && (*s == '0')) {
  137. foundzero = 1;
  138. LDAP_UTF8INC(s);
  139. }
  140. if (foundzero && !*s) { /* value is all zeros */
  141. *d++ = '0'; /* set value to a single zero */
  142. } else if (foundsign && (s > d)) {
  143. /* if there is a hyphen, make sure it is just to the left
  144. of the first significant (i.e. non-zero) digit e.g.
  145. convert -00000001 to -1 */
  146. *d++ = '-';
  147. }
  148. /* s should now point at the first significant digit/char */
  149. }
  150. /* handle value of all spaces - turn into single space */
  151. /* unless space insensitive syntax or int - turn into zero length string */
  152. if ( *s == '\0' && s != d ) {
  153. if ( ! (syntax & SYNTAX_SI) && ! (syntax & SYNTAX_INT) ) {
  154. *d++ = ' ';
  155. }
  156. *d = '\0';
  157. return;
  158. }
  159. prevspace = 0;
  160. while ( *s ) {
  161. curspace = utf8isspace_fast(s);
  162. /* ignore spaces and '-' in telephone numbers */
  163. if ( (syntax & SYNTAX_TEL) && (curspace || *s == '-') ) {
  164. LDAP_UTF8INC(s);
  165. continue;
  166. }
  167. /* ignore all spaces if this is a space insensitive value */
  168. if ( (syntax & SYNTAX_SI) && curspace ) {
  169. LDAP_UTF8INC(s);
  170. continue;
  171. }
  172. /* compress multiple blanks */
  173. if ( prevspace && curspace ) {
  174. LDAP_UTF8INC(s);
  175. continue;
  176. }
  177. prevspace = curspace;
  178. if ( syntax & SYNTAX_CIS ) {
  179. int ssz, dsz;
  180. slapi_utf8ToLower((unsigned char*)s, (unsigned char *)d, &ssz, &dsz);
  181. s += ssz;
  182. d += dsz;
  183. } else {
  184. char *np;
  185. int sz;
  186. np = ldap_utf8next(s);
  187. if (np == NULL || np == s) break;
  188. sz = np - s;
  189. memmove(d,s,sz);
  190. d += sz;
  191. s += sz;
  192. }
  193. }
  194. *d = '\0';
  195. /* strip trailing blanks */
  196. if (prevspace && trim_spaces) {
  197. char *nd;
  198. nd = ldap_utf8prev(d);
  199. while (nd && nd >= head && utf8isspace_fast(nd)) {
  200. d = nd;
  201. nd = ldap_utf8prev(d);
  202. *d = '\0';
  203. }
  204. }
  205. }
  206. void
  207. value_normalize(
  208. char *s,
  209. int syntax,
  210. int trim_spaces
  211. )
  212. {
  213. /* deprecated */
  214. }
  215. int
  216. value_cmp(
  217. struct berval *v1,
  218. struct berval *v2,
  219. int syntax,
  220. int normalize
  221. )
  222. {
  223. int rc = 0;
  224. struct berval bvcopy1;
  225. struct berval bvcopy2;
  226. char little_buffer[64];
  227. size_t buffer_space = sizeof(little_buffer);
  228. int buffer_offset = 0;
  229. int free_v1 = 0;
  230. int free_v2 = 0;
  231. int v1sign = 1, v2sign = 1; /* default to positive */
  232. char *alt = NULL;
  233. // check NULL values before normalization
  234. if (!v1->bv_val) {
  235. if (v2->bv_val) rc = -1;
  236. goto done;
  237. }
  238. if (!v2->bv_val) {
  239. rc = 1;
  240. goto done;
  241. }
  242. /* This code used to call malloc up to four times in the copying
  243. * of attributes to be normalized. Now we attempt to keep everything
  244. * on the stack and only malloc if the data is big
  245. */
  246. if ( normalize & 1 ) {
  247. /* Do we have space in the little buffer ? */
  248. if (v1->bv_len < buffer_space) {
  249. bvcopy1.bv_len = v1->bv_len;
  250. SAFEMEMCPY(&little_buffer[buffer_offset],v1->bv_val,v1->bv_len);
  251. bvcopy1.bv_val = &little_buffer[buffer_offset];
  252. bvcopy1.bv_val[v1->bv_len] = '\0';
  253. v1 = &bvcopy1;
  254. } else {
  255. v1 = ber_bvdup( v1 );
  256. free_v1 = 1;
  257. }
  258. value_normalize_ext( v1->bv_val, syntax,
  259. 1 /* trim leading blanks */, &alt );
  260. if (alt) {
  261. int inserted = 0;
  262. if (free_v1) {
  263. slapi_ch_free_string(&v1->bv_val);
  264. v1->bv_val = alt;
  265. v1->bv_len = strlen(alt);
  266. inserted = 1;
  267. } else {
  268. if (strlen(alt) < buffer_space) {
  269. v1->bv_len = strlen(alt);
  270. /* Copying to little_buffer */
  271. SAFEMEMCPY(v1->bv_val, alt, v1->bv_len);
  272. *(v1->bv_val + v1->bv_len) = '\0';
  273. } else {
  274. free_v1 = 1;
  275. v1 = (struct berval *)slapi_ch_malloc(sizeof(struct berval));
  276. v1->bv_val = alt;
  277. v1->bv_len = strlen(alt);
  278. inserted = 1;
  279. }
  280. }
  281. if(!inserted){
  282. slapi_ch_free_string(&alt);
  283. }
  284. }
  285. if (!free_v1) {
  286. buffer_space -= v1->bv_len + 1;
  287. buffer_offset += v1->bv_len + 1;
  288. }
  289. }
  290. if ( normalize & 2 ) {
  291. /* Do we have space in the little buffer ? */
  292. if (v2->bv_len < buffer_space) {
  293. bvcopy2.bv_len = v2->bv_len;
  294. SAFEMEMCPY(&little_buffer[buffer_offset],v2->bv_val,v2->bv_len);
  295. bvcopy2.bv_val = &little_buffer[buffer_offset];
  296. bvcopy2.bv_val[v2->bv_len] = '\0';
  297. v2 = &bvcopy2;
  298. } else {
  299. v2 = ber_bvdup( v2 );
  300. free_v2 = 1;
  301. }
  302. value_normalize_ext( v2->bv_val, syntax,
  303. 1 /* trim leading blanks */, &alt );
  304. if (alt) {
  305. int inserted = 0;
  306. if (free_v2) {
  307. slapi_ch_free_string(&v2->bv_val);
  308. v2->bv_val = alt;
  309. v2->bv_len = strlen(alt);
  310. inserted = 1;
  311. } else {
  312. if (strlen(alt) < buffer_space) {
  313. v2->bv_len = strlen(alt);
  314. /* Copying to little_buffer */
  315. SAFEMEMCPY(v2->bv_val, alt, v2->bv_len);
  316. *(v2->bv_val + v2->bv_len) = '\0';
  317. } else {
  318. free_v2 = 1;
  319. v2 = (struct berval *)slapi_ch_malloc(sizeof(struct berval));
  320. v2->bv_val = alt;
  321. v2->bv_len = strlen(alt);
  322. inserted = 1;
  323. }
  324. }
  325. if(!inserted){
  326. slapi_ch_free_string(&alt);
  327. }
  328. }
  329. if (!free_v2) {
  330. buffer_space -= v2->bv_len + 1;
  331. buffer_offset += v2->bv_len + 1;
  332. }
  333. }
  334. if (normalize) {
  335. // check NULL values after normalization
  336. if (!v1->bv_val) {
  337. if (v2->bv_val) rc = -1;
  338. goto done;
  339. }
  340. if (!v2->bv_val) {
  341. rc = 1;
  342. goto done;
  343. }
  344. }
  345. if (syntax & SYNTAX_INT) {
  346. v1sign = *v1->bv_val != '-';
  347. v2sign = *v2->bv_val != '-';
  348. rc = v1sign - v2sign;
  349. if (rc) { /* one is positive, one is negative */
  350. goto done;
  351. }
  352. /* check magnitude */
  353. /* unfortunately, bv_len cannot be trusted - bv_len is not
  354. updated during or after value_normalize */
  355. rc = (strlen(v1->bv_val) - strlen(v2->bv_val));
  356. if (rc) {
  357. rc = (rc > 0) ? 1 : -1;
  358. if (!v1sign && !v2sign) { /* both negative */
  359. rc = 0 - rc; /* flip it */
  360. }
  361. goto done;
  362. }
  363. }
  364. if (syntax & SYNTAX_CIS) {
  365. rc = slapi_utf8casecmp( (unsigned char *)v1->bv_val,
  366. (unsigned char *)v2->bv_val );
  367. } else if (syntax & SYNTAX_CES) {
  368. rc = strcmp( v1->bv_val, v2->bv_val );
  369. } else { /* error - unknown syntax */
  370. LDAPDebug(LDAP_DEBUG_PLUGIN,
  371. "invalid syntax [%d]\n", syntax, 0, 0);
  372. }
  373. if ((syntax & SYNTAX_INT) && !v1sign && !v2sign) { /* both negative */
  374. rc = 0 - rc; /* flip it */
  375. }
  376. done:
  377. if ( (normalize & 1) && free_v1) {
  378. ber_bvfree( v1 );
  379. }
  380. if ( (normalize & 2) && free_v2) {
  381. ber_bvfree( v2 );
  382. }
  383. return( rc );
  384. }