value.c 8.5 KB

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