value.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. void
  85. value_normalize(
  86. char *s,
  87. int syntax,
  88. int trim_spaces
  89. )
  90. {
  91. char *d;
  92. int prevspace, curspace;
  93. if ( ! (syntax & SYNTAX_CIS) && ! (syntax & SYNTAX_CES) ) {
  94. return;
  95. }
  96. if ( syntax & SYNTAX_DN ) {
  97. (void) slapi_dn_normalize_case( s );
  98. return;
  99. }
  100. d = s;
  101. if (trim_spaces) {
  102. /* strip leading blanks */
  103. while (utf8isspace_fast(s)) {
  104. LDAP_UTF8INC(s);
  105. }
  106. }
  107. /* for int syntax, look for leading sign, then trim 0s */
  108. /* have to do this after trimming spaces */
  109. if (syntax & SYNTAX_INT) {
  110. int foundsign = 0;
  111. if (*s == '-') {
  112. foundsign = 1;
  113. LDAP_UTF8INC(s);
  114. }
  115. while (*s && (*s == '0')) {
  116. LDAP_UTF8INC(s);
  117. }
  118. /* if there is a hyphen, make sure it is just to the left
  119. of the first significant (i.e. non-zero) digit e.g.
  120. convert -00000001 to -1 */
  121. if (foundsign && (s > d)) {
  122. *d = '-';
  123. d++;
  124. }
  125. /* s should now point at the first significant digit/char */
  126. }
  127. /* handle value of all spaces - turn into single space */
  128. /* unless space insensitive syntax or int - turn into zero length string */
  129. if ( *s == '\0' && s != d ) {
  130. if ( ! (syntax & SYNTAX_SI) && ! (syntax & SYNTAX_INT) ) {
  131. *d++ = ' ';
  132. }
  133. *d = '\0';
  134. return;
  135. }
  136. prevspace = 0;
  137. while ( *s ) {
  138. curspace = utf8isspace_fast(s);
  139. /* ignore spaces and '-' in telephone numbers */
  140. if ( (syntax & SYNTAX_TEL) && (curspace || *s == '-') ) {
  141. LDAP_UTF8INC(s);
  142. continue;
  143. }
  144. /* ignore all spaces if this is a space insensitive value */
  145. if ( (syntax & SYNTAX_SI) && curspace ) {
  146. LDAP_UTF8INC(s);
  147. continue;
  148. }
  149. /* compress multiple blanks */
  150. if ( prevspace && curspace ) {
  151. LDAP_UTF8INC(s);
  152. continue;
  153. }
  154. prevspace = curspace;
  155. if ( syntax & SYNTAX_CIS ) {
  156. int ssz, dsz;
  157. slapi_utf8ToLower((unsigned char*)s, (unsigned char *)d, &ssz, &dsz);
  158. s += ssz;
  159. d += dsz;
  160. } else {
  161. char *np;
  162. int sz;
  163. np = ldap_utf8next(s);
  164. if (np == NULL || np == s) break;
  165. sz = np - s;
  166. memmove(d,s,sz);
  167. d += sz;
  168. s += sz;
  169. }
  170. }
  171. *d = '\0';
  172. /* strip trailing blanks */
  173. if (prevspace && trim_spaces) {
  174. char *nd;
  175. nd = ldap_utf8prev(d);
  176. while (nd && utf8isspace_fast(nd)) {
  177. d = nd;
  178. nd = ldap_utf8prev(d);
  179. *d = '\0';
  180. }
  181. }
  182. }
  183. int
  184. value_cmp(
  185. struct berval *v1,
  186. struct berval *v2,
  187. int syntax,
  188. int normalize
  189. )
  190. {
  191. int rc = 0;
  192. struct berval bvcopy1;
  193. struct berval bvcopy2;
  194. char little_buffer[64];
  195. size_t buffer_space = sizeof(little_buffer);
  196. int buffer_offset = 0;
  197. int free_v1 = 0;
  198. int free_v2 = 0;
  199. int v1sign = 1, v2sign = 1; /* default to positive */
  200. /* This code used to call malloc up to four times in the copying
  201. * of attributes to be normalized. Now we attempt to keep everything
  202. * on the stack and only malloc if the data is big
  203. */
  204. if ( normalize & 1 ) {
  205. /* Do we have space in the little buffer ? */
  206. if (v1->bv_len < buffer_space) {
  207. bvcopy1.bv_len = v1->bv_len;
  208. SAFEMEMCPY(&little_buffer[buffer_offset],v1->bv_val,v1->bv_len);
  209. bvcopy1.bv_val = &little_buffer[buffer_offset];
  210. bvcopy1.bv_val[v1->bv_len] = '\0';
  211. v1 = &bvcopy1;
  212. buffer_space-= v1->bv_len+1;
  213. buffer_offset+= v1->bv_len+1;
  214. } else {
  215. v1 = ber_bvdup( v1 );
  216. free_v1 = 1;
  217. }
  218. value_normalize( v1->bv_val, syntax, 1 /* trim leading blanks */ );
  219. }
  220. if ( normalize & 2 ) {
  221. /* Do we have space in the little buffer ? */
  222. if (v2->bv_len < buffer_space) {
  223. bvcopy2.bv_len = v2->bv_len;
  224. SAFEMEMCPY(&little_buffer[buffer_offset],v2->bv_val,v2->bv_len);
  225. bvcopy2.bv_val = &little_buffer[buffer_offset];
  226. bvcopy2.bv_val[v2->bv_len] = '\0';
  227. v2 = &bvcopy2;
  228. buffer_space-= v2->bv_len+1;
  229. buffer_offset+= v2->bv_len+1;
  230. } else {
  231. v2 = ber_bvdup( v2 );
  232. free_v2 = 1;
  233. }
  234. value_normalize( v2->bv_val, syntax, 1 /* trim leading blanks */ );
  235. }
  236. if (syntax & SYNTAX_INT) {
  237. v1sign = v1->bv_val && (*v1->bv_val != '-');
  238. v2sign = v2->bv_val && (*v2->bv_val != '-');
  239. rc = v1sign - v2sign;
  240. if (rc) { /* one is positive, one is negative */
  241. goto done;
  242. }
  243. /* check magnitude */
  244. /* unfortunately, bv_len cannot be trusted - bv_len is not
  245. updated during or after value_normalize */
  246. rc = (strlen(v1->bv_val) - strlen(v2->bv_val));
  247. if (rc) {
  248. rc = (rc > 0) ? 1 : -1;
  249. if (!v1sign && !v2sign) { /* both negative */
  250. rc = 0 - rc; /* flip it */
  251. }
  252. goto done;
  253. }
  254. }
  255. if (syntax & SYNTAX_CIS) {
  256. rc = slapi_utf8casecmp( (unsigned char *)v1->bv_val,
  257. (unsigned char *)v2->bv_val );
  258. } else if (syntax & SYNTAX_CES) {
  259. rc = strcmp( v1->bv_val, v2->bv_val );
  260. } else { /* error - unknown syntax */
  261. LDAPDebug(LDAP_DEBUG_PLUGIN,
  262. "invalid syntax [%d]\n", syntax, 0, 0);
  263. }
  264. if ((syntax & SYNTAX_INT) && !v1sign && !v2sign) { /* both negative */
  265. rc = 0 - rc; /* flip it */
  266. }
  267. done:
  268. if ( (normalize & 1) && free_v1) {
  269. ber_bvfree( v1 );
  270. }
  271. if ( (normalize & 2) && free_v2) {
  272. ber_bvfree( v2 );
  273. }
  274. return( rc );
  275. }