aboutsummaryrefslogtreecommitdiffstats
path: root/lib/asn1kit/types/boolean.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/asn1kit/types/boolean.rb')
-rw-r--r--lib/asn1kit/types/boolean.rb41
1 files changed, 41 insertions, 0 deletions
diff --git a/lib/asn1kit/types/boolean.rb b/lib/asn1kit/types/boolean.rb
new file mode 100644
index 0000000..b8a1c73
--- /dev/null
+++ b/lib/asn1kit/types/boolean.rb
@@ -0,0 +1,41 @@
+# coding: ASCII-8BIT
+
+class ASN1Kit::Boolean < ASN1Kit::Type
+ asn1_tag :IMPLICIT, :UNIVERSAL, 1
+ asn1_alias "BOOLEAN"
+
+ def initialize(value)
+ self.value = value
+ end
+
+ def value
+ @value
+ end
+
+ def value=(value)
+ if value != true && value != false
+ raise TypeError, "illegal value for BOOLEAN: %p" % value
+ end
+ @value = value
+ end
+
+ def to_der
+ der_header(1) << (value ? "\xff" : "\x00")
+ end
+
+ def self.from_ber(ber)
+ content = check_ber_header(ber)
+ if content.bytesize != 1
+ raise EncodingError, "invalid content length for BOOLEAN"
+ end
+ new(content != "\x00")
+ end
+
+ def ==(other)
+ other.class <= ASN1Kit::Boolean && value == other.value
+ end
+
+ private def inspect_inner
+ @value ? "TRUE" : "FALSE"
+ end
+end