1
0

int.c 7.1 KB

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