int.c 7.2 KB

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