int.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. /* int.c - integer syntax routines */
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <sys/types.h>
  10. #include "syntax.h"
  11. static int int_filter_ava( Slapi_PBlock *pb, struct berval *bvfilter,
  12. Slapi_Value **bvals, int ftype, Slapi_Value **retVal );
  13. static int int_values2keys( Slapi_PBlock *pb, Slapi_Value **val,
  14. Slapi_Value ***ivals );
  15. static int int_assertion2keys( Slapi_PBlock *pb, Slapi_Value *val,
  16. Slapi_Value ***ivals, int ftype );
  17. static int int_compare(struct berval *v1, struct berval *v2);
  18. static long int_to_canonical( long num );
  19. /* the first name is the official one from RFC 2252 */
  20. static char *names[] = { "INTEGER", "int", INTEGER_SYNTAX_OID, 0 };
  21. static Slapi_PluginDesc pdesc = { "int-syntax", PLUGIN_MAGIC_VENDOR_STR,
  22. PRODUCTTEXT, "integer attribute syntax plugin" };
  23. int
  24. int_init( Slapi_PBlock *pb )
  25. {
  26. int rc, flags;
  27. LDAPDebug( LDAP_DEBUG_PLUGIN, "=> int_init\n", 0, 0, 0 );
  28. rc = slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
  29. (void *) SLAPI_PLUGIN_VERSION_01 );
  30. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION,
  31. (void *)&pdesc );
  32. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FILTER_AVA,
  33. (void *) int_filter_ava );
  34. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_VALUES2KEYS,
  35. (void *) int_values2keys );
  36. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_ASSERTION2KEYS_AVA,
  37. (void *) int_assertion2keys );
  38. flags = SLAPI_PLUGIN_SYNTAX_FLAG_ORDERING;
  39. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FLAGS,
  40. (void *) &flags );
  41. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_NAMES,
  42. (void *) names );
  43. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_OID,
  44. (void *) INTEGER_SYNTAX_OID );
  45. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_COMPARE,
  46. (void *) int_compare );
  47. LDAPDebug( LDAP_DEBUG_PLUGIN, "<= int_init %d\n", rc, 0, 0 );
  48. return( rc );
  49. }
  50. static int
  51. int_filter_ava( Slapi_PBlock *pb, struct berval *bvfilter,
  52. Slapi_Value **bvals, int ftype, Slapi_Value **retVal )
  53. {
  54. int i, rc;
  55. long flong, elong;
  56. if ( ftype == LDAP_FILTER_APPROX ) {
  57. return( LDAP_PROTOCOL_ERROR );
  58. }
  59. if(retVal) {
  60. *retVal=NULL;
  61. }
  62. flong = atol( bvfilter->bv_val );
  63. for ( i = 0; bvals[i] != NULL; i++ ) {
  64. elong = atol ( slapi_value_get_string(bvals[i]) );
  65. rc = elong - flong;
  66. switch ( ftype ) {
  67. case LDAP_FILTER_GE:
  68. if ( rc >= 0 ) {
  69. if(retVal) {
  70. *retVal = bvals[i];
  71. }
  72. return( 0 );
  73. }
  74. break;
  75. case LDAP_FILTER_LE:
  76. if ( rc <= 0 ) {
  77. if(retVal) {
  78. *retVal = bvals[i];
  79. }
  80. return( 0 );
  81. }
  82. break;
  83. case LDAP_FILTER_EQUALITY:
  84. if ( rc == 0 ) {
  85. if(retVal) {
  86. *retVal = bvals[i];
  87. }
  88. return( 0 );
  89. }
  90. break;
  91. }
  92. }
  93. return( -1 );
  94. }
  95. static int
  96. int_values2keys( Slapi_PBlock *pb, Slapi_Value **vals, Slapi_Value ***ivals )
  97. {
  98. long num;
  99. int i;
  100. for ( i = 0; vals[i] != NULL; i++ ) {
  101. /* NULL */
  102. }
  103. *ivals = (Slapi_Value **) slapi_ch_malloc(( i + 1 ) * sizeof(Slapi_Value *) );
  104. for ( i = 0; vals[i] != NULL; i++ )
  105. {
  106. num = atol( slapi_value_get_string(vals[i]) );
  107. num = int_to_canonical( num );
  108. (*ivals)[i] = slapi_value_new();
  109. slapi_value_set((*ivals)[i],&num,sizeof(long));
  110. }
  111. (*ivals)[i] = NULL;
  112. return( 0 );
  113. }
  114. static int
  115. int_assertion2keys( Slapi_PBlock *pb, Slapi_Value *val, Slapi_Value ***ivals, int ftype )
  116. {
  117. long num;
  118. size_t len;
  119. unsigned char *b;
  120. Slapi_Value *tmpval=NULL;
  121. num = atol( slapi_value_get_string(val) );
  122. num = int_to_canonical( num );
  123. /* similar to string.c to optimize equality path: avoid malloc/free */
  124. if(ftype == LDAP_FILTER_EQUALITY_FAST) {
  125. len=sizeof(long);
  126. tmpval=(*ivals)[0];
  127. if ( len > tmpval->bv.bv_len) {
  128. tmpval->bv.bv_val=(char *)slapi_ch_malloc(len);
  129. }
  130. tmpval->bv.bv_len=len;
  131. b = (unsigned char *)&num;
  132. memcpy(tmpval->bv.bv_val,b,len);
  133. } else {
  134. *ivals = (Slapi_Value **) slapi_ch_malloc( 2 * sizeof(Slapi_Value *) );
  135. (*ivals)[0] = (Slapi_Value *) slapi_ch_malloc( sizeof(Slapi_Value) );
  136. /* XXXSD initialize memory */
  137. memset((*ivals)[0],0,sizeof(Slapi_Value));
  138. slapi_value_set((*ivals)[0],&num,sizeof(long));
  139. (*ivals)[1] = NULL;
  140. }
  141. return( 0 );
  142. }
  143. static int int_compare(
  144. struct berval *v1,
  145. struct berval *v2
  146. )
  147. {
  148. long value1 = atol(v1->bv_val);
  149. long value2 = atol(v2->bv_val);
  150. if (value1 == value2) {
  151. return 0;
  152. }
  153. return ( ((value1 - value2) > 0) ? 1 : -1);
  154. }
  155. static long
  156. int_to_canonical( long num )
  157. {
  158. long ret = 0L;
  159. unsigned char *b = (unsigned char *)&ret;
  160. b[0] = (unsigned char)(num >> 24);
  161. b[1] = (unsigned char)(num >> 16);
  162. b[2] = (unsigned char)(num >> 8);
  163. b[3] = (unsigned char)num;
  164. return ret;
  165. }