value.c 8.5 KB

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