1
0

bitstring.c 7.5 KB

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