bin.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. /* bin.c - bin syntax routines */
  39. /*
  40. * This file actually implements two syntax plugins: OctetString and Binary.
  41. * We treat them identically for now. XXXmcs: check if that is correct.
  42. */
  43. #include <stdio.h>
  44. #include <string.h>
  45. #include <sys/types.h>
  46. #include "syntax.h"
  47. static int bin_filter_ava( Slapi_PBlock *pb, struct berval *bvfilter,
  48. Slapi_Value **bvals, int ftype, Slapi_Value **retVal );
  49. static int bin_values2keys( Slapi_PBlock *pb, Slapi_Value **bvals,
  50. Slapi_Value ***ivals, int ftype );
  51. static int bin_assertion2keys_ava( Slapi_PBlock *pb, Slapi_Value *bval,
  52. Slapi_Value ***ivals, int ftype );
  53. /*
  54. * Attribute syntaxes. We treat all of these the same for now, even though
  55. * the specifications (e.g., RFC 2252) impose various constraints on the
  56. * the format for each of these.
  57. *
  58. * Note: the first name is the official one from RFC 2252.
  59. */
  60. static char *bin_names[] = { "Binary", "bin", BINARY_SYNTAX_OID, 0 };
  61. static char *octetstring_names[] = { "OctetString", OCTETSTRING_SYNTAX_OID, 0 };
  62. static char *jpeg_names[] = { "JPEG", JPEG_SYNTAX_OID, 0 };
  63. static Slapi_PluginDesc bin_pdesc = {
  64. "bin-syntax", PLUGIN_MAGIC_VENDOR_STR, PRODUCTTEXT,
  65. "binary attribute syntax plugin"
  66. };
  67. static Slapi_PluginDesc octetstring_pdesc = {
  68. "octetstring-syntax", PLUGIN_MAGIC_VENDOR_STR, PRODUCTTEXT,
  69. "octet string attribute syntax plugin"
  70. };
  71. static Slapi_PluginDesc jpeg_pdesc = {
  72. "jpeg-syntax", PLUGIN_MAGIC_VENDOR_STR, PRODUCTTEXT,
  73. "JPEG attribute syntax plugin"
  74. };
  75. /*
  76. * register_bin_like_plugin(): register all items for a bin-like plugin.
  77. */
  78. static int
  79. register_bin_like_plugin( Slapi_PBlock *pb, Slapi_PluginDesc *pdescp,
  80. char **names, char *oid )
  81. {
  82. int rc;
  83. rc = slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
  84. (void *) SLAPI_PLUGIN_VERSION_01 );
  85. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION,
  86. (void *)pdescp );
  87. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FILTER_AVA,
  88. (void *) bin_filter_ava );
  89. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_VALUES2KEYS,
  90. (void *) bin_values2keys );
  91. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_ASSERTION2KEYS_AVA,
  92. (void *) bin_assertion2keys_ava );
  93. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_NAMES,
  94. (void *) names );
  95. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_OID,
  96. (void *) oid );
  97. return( rc );
  98. }
  99. int
  100. bin_init( Slapi_PBlock *pb )
  101. {
  102. int rc;
  103. LDAPDebug( LDAP_DEBUG_PLUGIN, "=> bin_init\n", 0, 0, 0 );
  104. rc = register_bin_like_plugin( pb, &bin_pdesc, bin_names,
  105. BINARY_SYNTAX_OID );
  106. LDAPDebug( LDAP_DEBUG_PLUGIN, "<= bin_init %d\n", rc, 0, 0 );
  107. return( rc );
  108. }
  109. int
  110. octetstring_init( Slapi_PBlock *pb )
  111. {
  112. int rc;
  113. LDAPDebug( LDAP_DEBUG_PLUGIN, "=> octetstring_init\n", 0, 0, 0 );
  114. rc = register_bin_like_plugin( pb, &octetstring_pdesc, octetstring_names,
  115. OCTETSTRING_SYNTAX_OID );
  116. LDAPDebug( LDAP_DEBUG_PLUGIN, "<= octetstring_init %d\n", rc, 0, 0 );
  117. return( rc );
  118. }
  119. int
  120. jpeg_init( Slapi_PBlock *pb )
  121. {
  122. int rc;
  123. LDAPDebug( LDAP_DEBUG_PLUGIN, "=> jpeg_init\n", 0, 0, 0 );
  124. rc = register_bin_like_plugin( pb, &jpeg_pdesc, jpeg_names,
  125. JPEG_SYNTAX_OID );
  126. LDAPDebug( LDAP_DEBUG_PLUGIN, "<= jpeg_init %d\n", rc, 0, 0 );
  127. return( rc );
  128. }
  129. static int
  130. bin_filter_ava( Slapi_PBlock *pb, struct berval *bvfilter,
  131. Slapi_Value **bvals, int ftype, Slapi_Value **retVal )
  132. {
  133. int i;
  134. for ( i = 0; bvals[i] != NULL; i++ ) {
  135. if ( slapi_value_get_length(bvals[i]) == bvfilter->bv_len &&
  136. 0 == memcmp( slapi_value_get_string(bvals[i]), bvfilter->bv_val, bvfilter->bv_len ))
  137. {
  138. if(retVal!=NULL)
  139. {
  140. *retVal= bvals[i];
  141. }
  142. return( 0 );
  143. }
  144. }
  145. if(retVal!=NULL)
  146. {
  147. *retVal= NULL;
  148. }
  149. return( -1 );
  150. }
  151. static int
  152. bin_values2keys( Slapi_PBlock *pb, Slapi_Value **bvals,
  153. Slapi_Value ***ivals, int ftype )
  154. {
  155. int i;
  156. if ( ftype != LDAP_FILTER_EQUALITY ) {
  157. return( LDAP_PROTOCOL_ERROR );
  158. }
  159. for ( i = 0; bvals[i] != NULL; i++ ) {
  160. /* NULL */
  161. }
  162. (*ivals) = (Slapi_Value **) slapi_ch_malloc(( i + 1 ) *
  163. sizeof(Slapi_Value *) );
  164. for ( i = 0; bvals[i] != NULL; i++ )
  165. {
  166. (*ivals)[i] = slapi_value_dup(bvals[i]);
  167. }
  168. (*ivals)[i] = NULL;
  169. return( 0 );
  170. }
  171. static int
  172. bin_assertion2keys_ava( Slapi_PBlock *pb, Slapi_Value *bval,
  173. Slapi_Value ***ivals, int ftype )
  174. {
  175. Slapi_Value *tmpval=NULL;
  176. size_t len;
  177. if (( ftype != LDAP_FILTER_EQUALITY ) &&
  178. ( ftype != LDAP_FILTER_EQUALITY_FAST))
  179. {
  180. return( LDAP_PROTOCOL_ERROR );
  181. }
  182. if(ftype == LDAP_FILTER_EQUALITY_FAST) {
  183. /* With the fast option, we are trying to avoid creating and freeing
  184. * a bunch of structures - we just do one malloc here - see
  185. * ava_candidates in filterentry.c
  186. */
  187. len=slapi_value_get_length(bval);
  188. tmpval=(*ivals)[0];
  189. if (len > tmpval->bv.bv_len) {
  190. tmpval->bv.bv_val=(char *)slapi_ch_malloc(len);
  191. }
  192. tmpval->bv.bv_len=len;
  193. memcpy(tmpval->bv.bv_val,slapi_value_get_string(bval),len);
  194. } else {
  195. (*ivals) = (Slapi_Value **) slapi_ch_malloc( 2 * sizeof(Slapi_Value *) );
  196. (*ivals)[0] = slapi_value_dup( bval );
  197. (*ivals)[1] = NULL;
  198. }
  199. return( 0 );
  200. }