bitstring.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2009 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. /* bitstring.c - Bit String syntax routines */
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <sys/types.h>
  16. #include "syntax.h"
  17. static int bitstring_filter_ava( Slapi_PBlock *pb, struct berval *bvfilter,
  18. Slapi_Value **bvals, int ftype, Slapi_Value **retVal );
  19. static int bitstring_filter_sub( Slapi_PBlock *pb, char *initial, char **any,
  20. char *final, Slapi_Value **bvals );
  21. static int bitstring_values2keys( Slapi_PBlock *pb, Slapi_Value **val,
  22. Slapi_Value ***ivals, int ftype );
  23. static int bitstring_assertion2keys_ava( Slapi_PBlock *pb, Slapi_Value *val,
  24. Slapi_Value ***ivals, int ftype );
  25. static int bitstring_assertion2keys_sub( Slapi_PBlock *pb, char *initial, char **any,
  26. char *final, Slapi_Value ***ivals );
  27. static int bitstring_compare(struct berval *v1, struct berval *v2);
  28. static int bitstring_validate(struct berval *val);
  29. static void bitstring_normalize(
  30. Slapi_PBlock *pb,
  31. char *s,
  32. int trim_spaces,
  33. char **alt
  34. );
  35. /* the first name is the official one from RFC 4517 */
  36. static char *names[] = { "Bit String", "bitstring", BITSTRING_SYNTAX_OID, 0 };
  37. static Slapi_PluginDesc pdesc = { "bitstring-syntax", VENDOR, DS_PACKAGE_VERSION,
  38. "Bit String attribute syntax plugin" };
  39. static const char *bitStringMatch_names[] = {"bitStringMatch", "2.5.13.16", NULL};
  40. static struct mr_plugin_def mr_plugin_table[] = {
  41. {{"2.5.13.16", NULL, "bitStringMatch", "The bitStringMatch rule compares an assertion value of the Bit String "
  42. "syntax to an attribute value of a syntax (e.g., the Bit String "
  43. "syntax) whose corresponding ASN.1 type is BIT STRING. "
  44. "If the corresponding ASN.1 type of the attribute syntax does not have "
  45. "a named bit list [ASN.1] (which is the case for the Bit String "
  46. "syntax), then the rule evaluates to TRUE if and only if the attribute "
  47. "value has the same number of bits as the assertion value and the bits "
  48. "match on a bitwise basis. "
  49. "If the corresponding ASN.1 type does have a named bit list, then "
  50. "bitStringMatch operates as above, except that trailing zero bits in "
  51. "the attribute and assertion values are treated as absent.",
  52. BITSTRING_SYNTAX_OID, 0, NULL /* only the specified syntax is supported */}, /* matching rule desc */
  53. {"bitStringMatch-mr", VENDOR, DS_PACKAGE_VERSION, "bitStringMatch matching rule plugin"}, /* plugin desc */
  54. bitStringMatch_names, /* matching rule name/oid/aliases */
  55. NULL, NULL, bitstring_filter_ava, NULL, bitstring_values2keys,
  56. bitstring_assertion2keys_ava, NULL, bitstring_compare}
  57. };
  58. static size_t mr_plugin_table_size = sizeof(mr_plugin_table)/sizeof(mr_plugin_table[0]);
  59. static int
  60. matching_rule_plugin_init(Slapi_PBlock *pb)
  61. {
  62. return syntax_matching_rule_plugin_init(pb, mr_plugin_table, mr_plugin_table_size);
  63. }
  64. static int
  65. register_matching_rule_plugins()
  66. {
  67. return syntax_register_matching_rule_plugins(mr_plugin_table, mr_plugin_table_size, matching_rule_plugin_init);
  68. }
  69. int
  70. bitstring_init( Slapi_PBlock *pb )
  71. {
  72. int rc, flags;
  73. LDAPDebug( LDAP_DEBUG_PLUGIN, "=> bitstring_init\n", 0, 0, 0 );
  74. rc = slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
  75. (void *) SLAPI_PLUGIN_VERSION_01 );
  76. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION,
  77. (void *)&pdesc );
  78. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FILTER_AVA,
  79. (void *) bitstring_filter_ava );
  80. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FILTER_SUB,
  81. (void *) bitstring_filter_sub );
  82. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_VALUES2KEYS,
  83. (void *) bitstring_values2keys );
  84. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_ASSERTION2KEYS_AVA,
  85. (void *) bitstring_assertion2keys_ava );
  86. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_ASSERTION2KEYS_SUB,
  87. (void *) bitstring_assertion2keys_sub );
  88. flags = SLAPI_PLUGIN_SYNTAX_FLAG_ORDERING;
  89. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FLAGS,
  90. (void *) &flags );
  91. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_NAMES,
  92. (void *) names );
  93. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_OID,
  94. (void *) BITSTRING_SYNTAX_OID );
  95. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_COMPARE,
  96. (void *) bitstring_compare );
  97. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_VALIDATE,
  98. (void *) bitstring_validate );
  99. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_NORMALIZE,
  100. (void *) bitstring_normalize );
  101. rc |= register_matching_rule_plugins();
  102. LDAPDebug( LDAP_DEBUG_PLUGIN, "<= bitstring_init %d\n", rc, 0, 0 );
  103. return( rc );
  104. }
  105. static int
  106. bitstring_filter_ava(
  107. Slapi_PBlock *pb,
  108. struct berval *bvfilter,
  109. Slapi_Value **bvals,
  110. int ftype,
  111. Slapi_Value **retVal
  112. )
  113. {
  114. int filter_normalized = 0;
  115. int syntax = SYNTAX_CES;
  116. if (pb) {
  117. slapi_pblock_get( pb, SLAPI_PLUGIN_SYNTAX_FILTER_NORMALIZED,
  118. &filter_normalized );
  119. if (filter_normalized) {
  120. syntax |= SYNTAX_NORM_FILT;
  121. }
  122. }
  123. return( string_filter_ava( bvfilter, bvals, syntax, ftype, retVal ) );
  124. }
  125. static int
  126. bitstring_filter_sub(
  127. Slapi_PBlock *pb,
  128. char *initial,
  129. char **any,
  130. char *final,
  131. Slapi_Value **bvals
  132. )
  133. {
  134. return( string_filter_sub( pb, initial, any, final, bvals, SYNTAX_CES ) );
  135. }
  136. static int
  137. bitstring_values2keys(
  138. Slapi_PBlock *pb,
  139. Slapi_Value **vals,
  140. Slapi_Value ***ivals,
  141. int ftype
  142. )
  143. {
  144. return( string_values2keys( pb, vals, ivals, SYNTAX_CES,
  145. ftype ) );
  146. }
  147. static int
  148. bitstring_assertion2keys_ava(
  149. Slapi_PBlock *pb,
  150. Slapi_Value *val,
  151. Slapi_Value ***ivals,
  152. int ftype
  153. )
  154. {
  155. return(string_assertion2keys_ava( pb, val, ivals,
  156. SYNTAX_CES, ftype ));
  157. }
  158. static int
  159. bitstring_assertion2keys_sub(
  160. Slapi_PBlock *pb,
  161. char *initial,
  162. char **any,
  163. char *final,
  164. Slapi_Value ***ivals
  165. )
  166. {
  167. return( string_assertion2keys_sub( pb, initial, any, final, ivals,
  168. SYNTAX_CES ) );
  169. }
  170. static int bitstring_compare(
  171. struct berval *v1,
  172. struct berval *v2
  173. )
  174. {
  175. return value_cmp(v1, v2, SYNTAX_CES, 3 /* Normalise both values */);
  176. }
  177. static int
  178. bitstring_validate(
  179. struct berval *val
  180. )
  181. {
  182. int rc = 0; /* assume the value is valid */
  183. /* Don't allow a 0 length string */
  184. if ((val == NULL) || (val->bv_len == 0)) {
  185. rc = 1;
  186. goto exit;
  187. }
  188. rc = bitstring_validate_internal(val->bv_val, &(val->bv_val[val->bv_len - 1]));
  189. exit:
  190. return rc;
  191. }
  192. static void bitstring_normalize(
  193. Slapi_PBlock *pb,
  194. char *s,
  195. int trim_spaces,
  196. char **alt
  197. )
  198. {
  199. value_normalize_ext(s, SYNTAX_CES, trim_spaces, alt);
  200. return;
  201. }