bitstring.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. {
  42. {
  43. "2.5.13.16",
  44. NULL,
  45. "bitStringMatch",
  46. "The bitStringMatch rule compares an assertion value of the Bit String "
  47. "syntax to an attribute value of a syntax (e.g., the Bit String "
  48. "syntax) whose corresponding ASN.1 type is BIT STRING. "
  49. "If the corresponding ASN.1 type of the attribute syntax does not have "
  50. "a named bit list [ASN.1] (which is the case for the Bit String "
  51. "syntax), then the rule evaluates to TRUE if and only if the attribute "
  52. "value has the same number of bits as the assertion value and the bits "
  53. "match on a bitwise basis. "
  54. "If the corresponding ASN.1 type does have a named bit list, then "
  55. "bitStringMatch operates as above, except that trailing zero bits in "
  56. "the attribute and assertion values are treated as absent.",
  57. BITSTRING_SYNTAX_OID,
  58. 0,
  59. NULL /* only the specified syntax is supported */
  60. }, /* matching rule desc */
  61. {
  62. "bitStringMatch-mr",
  63. VENDOR,
  64. DS_PACKAGE_VERSION,
  65. "bitStringMatch matching rule plugin"
  66. }, /* plugin desc */
  67. bitStringMatch_names, /* matching rule name/oid/aliases */
  68. NULL,
  69. NULL,
  70. bitstring_filter_ava,
  71. NULL,
  72. bitstring_values2keys,
  73. bitstring_assertion2keys_ava,
  74. NULL,
  75. bitstring_compare,
  76. NULL /* mr_normalize; */
  77. }
  78. };
  79. static size_t mr_plugin_table_size = sizeof(mr_plugin_table)/sizeof(mr_plugin_table[0]);
  80. static int
  81. matching_rule_plugin_init(Slapi_PBlock *pb)
  82. {
  83. return syntax_matching_rule_plugin_init(pb, mr_plugin_table, mr_plugin_table_size);
  84. }
  85. static int
  86. register_matching_rule_plugins(void)
  87. {
  88. return syntax_register_matching_rule_plugins(mr_plugin_table, mr_plugin_table_size, matching_rule_plugin_init);
  89. }
  90. int
  91. bitstring_init( Slapi_PBlock *pb )
  92. {
  93. int rc, flags;
  94. LDAPDebug( LDAP_DEBUG_PLUGIN, "=> bitstring_init\n", 0, 0, 0 );
  95. rc = slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
  96. (void *) SLAPI_PLUGIN_VERSION_01 );
  97. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION,
  98. (void *)&pdesc );
  99. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FILTER_AVA,
  100. (void *) bitstring_filter_ava );
  101. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FILTER_SUB,
  102. (void *) bitstring_filter_sub );
  103. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_VALUES2KEYS,
  104. (void *) bitstring_values2keys );
  105. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_ASSERTION2KEYS_AVA,
  106. (void *) bitstring_assertion2keys_ava );
  107. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_ASSERTION2KEYS_SUB,
  108. (void *) bitstring_assertion2keys_sub );
  109. flags = SLAPI_PLUGIN_SYNTAX_FLAG_ORDERING;
  110. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FLAGS,
  111. (void *) &flags );
  112. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_NAMES,
  113. (void *) names );
  114. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_OID,
  115. (void *) BITSTRING_SYNTAX_OID );
  116. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_COMPARE,
  117. (void *) bitstring_compare );
  118. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_VALIDATE,
  119. (void *) bitstring_validate );
  120. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_NORMALIZE,
  121. (void *) bitstring_normalize );
  122. rc |= register_matching_rule_plugins();
  123. LDAPDebug( LDAP_DEBUG_PLUGIN, "<= bitstring_init %d\n", rc, 0, 0 );
  124. return( rc );
  125. }
  126. static int
  127. bitstring_filter_ava(
  128. Slapi_PBlock *pb,
  129. struct berval *bvfilter,
  130. Slapi_Value **bvals,
  131. int ftype,
  132. Slapi_Value **retVal
  133. )
  134. {
  135. int filter_normalized = 0;
  136. int syntax = SYNTAX_CES;
  137. if (pb) {
  138. slapi_pblock_get( pb, SLAPI_PLUGIN_SYNTAX_FILTER_NORMALIZED,
  139. &filter_normalized );
  140. if (filter_normalized) {
  141. syntax |= SYNTAX_NORM_FILT;
  142. }
  143. }
  144. return( string_filter_ava( bvfilter, bvals, syntax, ftype, retVal ) );
  145. }
  146. static int
  147. bitstring_filter_sub(
  148. Slapi_PBlock *pb,
  149. char *initial,
  150. char **any,
  151. char *final,
  152. Slapi_Value **bvals
  153. )
  154. {
  155. return( string_filter_sub( pb, initial, any, final, bvals, SYNTAX_CES ) );
  156. }
  157. static int
  158. bitstring_values2keys(
  159. Slapi_PBlock *pb,
  160. Slapi_Value **vals,
  161. Slapi_Value ***ivals,
  162. int ftype
  163. )
  164. {
  165. return( string_values2keys( pb, vals, ivals, SYNTAX_CES,
  166. ftype ) );
  167. }
  168. static int
  169. bitstring_assertion2keys_ava(
  170. Slapi_PBlock *pb,
  171. Slapi_Value *val,
  172. Slapi_Value ***ivals,
  173. int ftype
  174. )
  175. {
  176. return(string_assertion2keys_ava( pb, val, ivals,
  177. SYNTAX_CES, ftype ));
  178. }
  179. static int
  180. bitstring_assertion2keys_sub(
  181. Slapi_PBlock *pb,
  182. char *initial,
  183. char **any,
  184. char *final,
  185. Slapi_Value ***ivals
  186. )
  187. {
  188. return( string_assertion2keys_sub( pb, initial, any, final, ivals,
  189. SYNTAX_CES ) );
  190. }
  191. static int bitstring_compare(
  192. struct berval *v1,
  193. struct berval *v2
  194. )
  195. {
  196. return value_cmp(v1, v2, SYNTAX_CES, 3 /* Normalise both values */);
  197. }
  198. static int
  199. bitstring_validate(
  200. struct berval *val
  201. )
  202. {
  203. int rc = 0; /* assume the value is valid */
  204. /* Don't allow a 0 length string */
  205. if ((val == NULL) || (val->bv_len == 0)) {
  206. rc = 1;
  207. goto exit;
  208. }
  209. rc = bitstring_validate_internal(val->bv_val, &(val->bv_val[val->bv_len - 1]));
  210. exit:
  211. return rc;
  212. }
  213. static void bitstring_normalize(
  214. Slapi_PBlock *pb,
  215. char *s,
  216. int trim_spaces,
  217. char **alt
  218. )
  219. {
  220. value_normalize_ext(s, SYNTAX_CES, trim_spaces, alt);
  221. return;
  222. }