value.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright 2001 Sun Microsystems, Inc.
  3. * Portions copyright 1999, 2001-2003 Netscape Communications Corporation.
  4. * All rights reserved.
  5. * END COPYRIGHT BLOCK **/
  6. /* value.c - routines for dealing with values */
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <sys/types.h>
  10. #include "syntax.h"
  11. /*
  12. * Do not use the SDK ldap_utf8isspace directly until it is faster
  13. * than this one.
  14. */
  15. static int
  16. utf8isspace_fast( char* s )
  17. {
  18. register unsigned char c = *(unsigned char*)s;
  19. if (0x80 & c) return(ldap_utf8isspace(s));
  20. switch (c) {
  21. case 0x09:
  22. case 0x0A:
  23. case 0x0B:
  24. case 0x0C:
  25. case 0x0D:
  26. case 0x20:
  27. return 1;
  28. default: break;
  29. }
  30. return 0;
  31. }
  32. /*
  33. ** This function is used to normalizes search filter components,
  34. ** and attribute values.
  35. **
  36. ** jcm: I added the trim_spaces flag since this function
  37. ** was incorrectly modifying search filter components. A search
  38. ** of the form "cn=a* b*" (note the space) would be wrongly
  39. ** normalized into "cn=a*b*", because this function is called
  40. ** once for "a" and once for " b".
  41. */
  42. void
  43. value_normalize(
  44. char *s,
  45. int syntax,
  46. int trim_spaces
  47. )
  48. {
  49. char *d;
  50. int prevspace, curspace;
  51. if ( ! (syntax & SYNTAX_CIS) && ! (syntax & SYNTAX_CES) ) {
  52. return;
  53. }
  54. if ( syntax & SYNTAX_DN ) {
  55. (void) slapi_dn_normalize_case( s );
  56. return;
  57. }
  58. d = s;
  59. if (trim_spaces) {
  60. /* strip leading blanks */
  61. while (utf8isspace_fast(s)) {
  62. LDAP_UTF8INC(s);
  63. }
  64. }
  65. /* handle value of all spaces - turn into single space */
  66. /* unless space insensitive syntax - turn into zero length string */
  67. if ( *s == '\0' && s != d ) {
  68. if ( ! (syntax & SYNTAX_SI)) {
  69. *d++ = ' ';
  70. }
  71. *d = '\0';
  72. return;
  73. }
  74. prevspace = 0;
  75. while ( *s ) {
  76. curspace = utf8isspace_fast(s);
  77. /* ignore spaces and '-' in telephone numbers */
  78. if ( (syntax & SYNTAX_TEL) && (curspace || *s == '-') ) {
  79. LDAP_UTF8INC(s);
  80. continue;
  81. }
  82. /* ignore all spaces if this is a space insensitive value */
  83. if ( (syntax & SYNTAX_SI) && curspace ) {
  84. LDAP_UTF8INC(s);
  85. continue;
  86. }
  87. /* compress multiple blanks */
  88. if ( prevspace && curspace ) {
  89. LDAP_UTF8INC(s);
  90. continue;
  91. }
  92. prevspace = curspace;
  93. if ( syntax & SYNTAX_CIS ) {
  94. int ssz, dsz;
  95. slapi_utf8ToLower((unsigned char*)s, (unsigned char *)d, &ssz, &dsz);
  96. s += ssz;
  97. d += dsz;
  98. } else {
  99. char *np;
  100. int sz;
  101. np = ldap_utf8next(s);
  102. if (np == NULL || np == s) break;
  103. sz = np - s;
  104. memcpy(d,s,sz);
  105. d += sz;
  106. s += sz;
  107. }
  108. }
  109. *d = '\0';
  110. /* strip trailing blanks */
  111. if (prevspace && trim_spaces) {
  112. char *nd;
  113. nd = ldap_utf8prev(d);
  114. while (nd && utf8isspace_fast(nd)) {
  115. d = nd;
  116. nd = ldap_utf8prev(d);
  117. *d = '\0';
  118. }
  119. }
  120. }
  121. int
  122. value_cmp(
  123. struct berval *v1,
  124. struct berval *v2,
  125. int syntax,
  126. int normalize
  127. )
  128. {
  129. int rc;
  130. struct berval bvcopy1;
  131. struct berval bvcopy2;
  132. char little_buffer[64];
  133. size_t buffer_space = sizeof(little_buffer);
  134. int buffer_offset = 0;
  135. int free_v1 = 0;
  136. int free_v2 = 0;
  137. /* This code used to call malloc up to four times in the copying
  138. * of attributes to be normalized. Now we attempt to keep everything
  139. * on the stack and only malloc if the data is big
  140. */
  141. if ( normalize & 1 ) {
  142. /* Do we have space in the little buffer ? */
  143. if (v1->bv_len < buffer_space) {
  144. bvcopy1.bv_len = v1->bv_len;
  145. SAFEMEMCPY(&little_buffer[buffer_offset],v1->bv_val,v1->bv_len);
  146. bvcopy1.bv_val = &little_buffer[buffer_offset];
  147. bvcopy1.bv_val[v1->bv_len] = '\0';
  148. v1 = &bvcopy1;
  149. buffer_space-= v1->bv_len+1;
  150. buffer_offset+= v1->bv_len+1;
  151. } else {
  152. v1 = ber_bvdup( v1 );
  153. free_v1 = 1;
  154. }
  155. value_normalize( v1->bv_val, syntax, 1 /* trim leading blanks */ );
  156. }
  157. if ( normalize & 2 ) {
  158. /* Do we have space in the little buffer ? */
  159. if (v2->bv_len < buffer_space) {
  160. bvcopy2.bv_len = v2->bv_len;
  161. SAFEMEMCPY(&little_buffer[buffer_offset],v2->bv_val,v2->bv_len);
  162. bvcopy2.bv_val = &little_buffer[buffer_offset];
  163. bvcopy2.bv_val[v2->bv_len] = '\0';
  164. v2 = &bvcopy2;
  165. buffer_space-= v2->bv_len+1;
  166. buffer_offset+= v2->bv_len+1;
  167. } else {
  168. v2 = ber_bvdup( v2 );
  169. free_v2 = 1;
  170. }
  171. value_normalize( v2->bv_val, syntax, 1 /* trim leading blanks */ );
  172. }
  173. switch ( syntax ) {
  174. case SYNTAX_CIS:
  175. case (SYNTAX_CIS | SYNTAX_TEL):
  176. case (SYNTAX_CIS | SYNTAX_DN):
  177. case (SYNTAX_CIS | SYNTAX_SI):
  178. rc = slapi_utf8casecmp( (unsigned char *)v1->bv_val,
  179. (unsigned char *)v2->bv_val );
  180. break;
  181. case SYNTAX_CES:
  182. rc = strcmp( v1->bv_val, v2->bv_val );
  183. break;
  184. }
  185. if ( (normalize & 1) && free_v1) {
  186. ber_bvfree( v1 );
  187. }
  188. if ( (normalize & 2) && free_v2) {
  189. ber_bvfree( v2 );
  190. }
  191. return( rc );
  192. }