top page > computer > web > rfc > x_690 > 8_1_common > coding > existential > decode_rules.html
更新日:
文責: 重城良国

X.690: ASN.1のBER方式の共通部分の存在型を利用したコーディング: デコードルール例の追加

SEQUENCEを読み込みリストにする

instance Asnable [a] where
	getAsn1Tag _ = Asn1Tag Universal Constructed 16
sequenceRule :: RuleType
sequenceRule rl t@(Asn1Tag Universal Constructed 16) ln =
	Just $ do
		Just (RawConstructed _ s) <- getAsnable <$>
			fromJust (recRule rl t ln)
		return $ AsnableBox as
sequenceRule _ _ _ = Nothing

試してみる

% ghci testAsnable.hs
Main> :set -XOverloadedStrings
Main> let Right (ab, "") = runAnalyzer (decodeWith [Rule
sequenceRule, Rule recRule]) cert
Main> let Just as = getAsnable ab :: Maybe [AsnableBox]
Main> length as
3
Main> getAsn1Tag $ as !! 1
Asn1Tag Universal Constructed 16
Main> let Just as1 = getAsnable $ as !! 1 :: Maybe
[AsnableBox]
Main> length as1
2
Main> getAsn1Tag $ as1 !! 0
Asn1Tag Universal Primitive 6
Main> getAsn1Tag $ as1 !! 1
Asn1Tag Universal Primitive 5
Main> getAsnable $ as1 !! 0 :: Maybe Raw
Just (Raw (Asn1Tag Universal Primitive 6)
"*\134H\134\247r\SOH\SOH\ENQ")
Main> getAsnable $ as1 !! 1 :: Maybe Raw
Just (Raw (Asn1Tag Universal Primitive 5) "")

Bool値を読み込む

instance Asnable Bool where
	getAsn1Tag _ = Asn1Tag Universal Primitive 1
boolRule :: RuleType
boolRule rl t@(Asn1Tag Universal Primitive 1) ln@(Just 1) =
	Just $ do
		Just (Raw _ bs) <- getAsnable <$>
			fromJust (rawRule rl t ln)
		return . AsnableBox $ bs /= "\x00"
boolRule _ _ _ = Nothing

試してみる

ghci testAsnable.hs
Main> :set -XOverloadedStrings
Main> let Right (ab, "") = runAnalyzer (decodeWith [Rule
boolRule, Rule sequenceRule, Rule recRule]) ind_test
Main> let Just as = getAsnable ab :: Maybe [AsnableBox]
Main> length as
1
Main> getAsnable $ as !! 0 :: Maybe Bool
Just True

整数値を読み込む

integerRule :: RuleType
integerRule r t@(Asn1Tag Universal Primitive 2)
	ln@(Just _) = Just $ do
		Just (Raw _ bs) <- getAsnable <$>
			fromJust (rawRule r t ln)
		return . AsnableBox $ readInteger bs
integerRule _ _ _ = Nothing
readInteger :: ByteString -> Integer
readInteger bs = case BS.uncons bs of
	Just (h, t) -> if testBit h 7
		then readIntegerR
			(fromIntegral h - 0x100) t
		else readIntegerR (fromIntegral h) t
	_ -> 0
readIntegerR :: Integer -> ByteString -> Integer
readIntegerR n bs = case uncons bs of
	Just (h, t) -> readIntegerR
		(n `shiftL` 8 .|. fromIntegral h) t
	_ -> n

試してみる

% ghci testAsnable.hs
Main> :set -XOverloadedStrings
Main> let Right (ab, "") = runAnalyzer (decodeWith [Rule
integerRule, Rule sequenceRule, Rule recRule]) cert
Main> let Just as = getAsnable ab :: Maybe [AsnableBox]
Main> length as
3
Main> getAsn1Tag $ as !! 0
Asn1Tag Universal Constructed 16
Main> let Just as0 = getAsnable $ as !! 0 :: Maybe [AsnableBox]
Main> length as0
8
Main> getAsn1Tag $ as0 !! 1
Asn1Tag Universal Primitive 2
Main> getAsnable $ as0 !! 1 :: Maybe Integer
Just 9541091809747837828

「デコード関数」へもどる 「タグやデータ長のエンコード」へ

正当なCSSです! HTML5 Powered with CSS3 / styling, and Semantics