bin.c 5.2 KB

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