tricu

An interpreted language for exploring Tree Calculus
Log | Files | Refs | README | LICENSE

InspectorOracle.hs (1502B)


      1 {-# LANGUAGE OverloadedStrings #-}
      2 -- Test-only independent report oracle. Never load a tricu inspector export.
      3 module Main where
      4 
      5 import Wire
      6 import Crypto.Hash (hash, Digest, SHA256)
      7 import qualified Data.ByteString as BS
      8 import qualified Data.ByteString.Char8 as B8
      9 import qualified Data.Foldable as F
     10 import Data.Text.Encoding (encodeUtf8)
     11 import Numeric (showHex)
     12 import System.Environment (getArgs)
     13 import System.Exit (die)
     14 
     15 field :: BS.ByteString -> BS.ByteString
     16 field = B8.pack . concatMap escape . BS.unpack
     17   where
     18     escape b | b < 32 || b >= 127 || b == 37 = '%' : pad (showHex b "")
     19              | otherwise = [toEnum (fromIntegral b)]
     20     pad s = map upper (replicate (2 - length s) '0' ++ s)
     21     upper c | c >= 'a' && c <= 'f' = toEnum (fromEnum c - 32)
     22             | otherwise = c
     23 
     24 main :: IO ()
     25 main = do
     26   args <- getArgs
     27   path <- case args of [p] -> pure p; _ -> die "usage: inspector-oracle BUNDLE"
     28   bytes <- BS.readFile path
     29   bundle <- either die pure (decodeBundle bytes)
     30   either die pure (verifyBundle bundle)
     31   B8.putStrLn "arboricx.inspect.v1"
     32   B8.putStrLn ("bundle-sha256\t" <> B8.pack (show (hash bytes :: Digest SHA256)))
     33   B8.putStrLn ("stored-nodes\t" <> B8.pack (show (F.length (bundleNodes bundle))))
     34   mapM_ (B8.putStrLn . row) (manifestExports (bundleManifest bundle))
     35   where
     36     row e = B8.intercalate "\t" ["export", field (encodeUtf8 (exportName e)),
     37       B8.pack (show (exportRoot e)), field (encodeUtf8 (exportKind e)), field (encodeUtf8 (exportAbi e))]