diff --git a/contrib/bin_codec_kaitai/kaitai.t b/contrib/bin_codec_kaitai/kaitai.t index e40dabe83a109295f5a4344dc4b8f9f69ea63f93..59b61eed6ff30029a2f578ad153b7d6a8c90ccd9 100644 --- a/contrib/bin_codec_kaitai/kaitai.t +++ b/contrib/bin_codec_kaitai/kaitai.t @@ -19,3 +19,89 @@ ground.bool test - id: bool type: u1 enum: bool +ground.int8 test + $ ./codec.exe dump kaitai for ground.int8 + meta: + id: ground__int8 + endian: be + seq: + - id: int8 + type: s1 +ground.uint16 test + $ ./codec.exe dump kaitai for ground.uint16 + meta: + id: ground__uint16 + endian: be + seq: + - id: uint16 + type: u2 +ground.int16 test + $ ./codec.exe dump kaitai for ground.int16 + meta: + id: ground__int16 + endian: be + seq: + - id: int16 + type: s2 +ground.int32 test + $ ./codec.exe dump kaitai for ground.int32 + meta: + id: ground__int32 + endian: be + seq: + - id: int32 + type: s4 +ground.int64 test + $ ./codec.exe dump kaitai for ground.int64 + meta: + id: ground__int64 + endian: be + seq: + - id: int64 + type: s8 +ground.int31 test + $ ./codec.exe dump kaitai for ground.int31 + meta: + id: ground__int31 + endian: be + seq: + - id: int31 + type: s4 +ground.float test + $ ./codec.exe dump kaitai for ground.float + meta: + id: ground__float + endian: be + seq: + - id: float + type: f8 +ground.bytes test + $ ./codec.exe dump kaitai for ground.bytes + meta: + id: ground__bytes + endian: be + types: + fixed_bytes: + seq: + - id: size + type: u4 + - id: value + size: size + seq: + - id: fixed size (uint30) bytes + type: fixed_bytes +ground.string test + $ ./codec.exe dump kaitai for ground.string + meta: + id: ground__string + endian: be + types: + fixed_bytes: + seq: + - id: size + type: u4 + - id: value + size: size + seq: + - id: fixed size (uint30) bytes + type: fixed_bytes diff --git a/contrib/kaitai-ocaml/src/parse.ml b/contrib/kaitai-ocaml/src/parse.ml index 5eb7f4786cf4e05004bee64ecb97545bc8851e34..e94a439a00e75f6dd0d8071e4250de521e9d9be3 100644 --- a/contrib/kaitai-ocaml/src/parse.ml +++ b/contrib/kaitai-ocaml/src/parse.ml @@ -57,7 +57,7 @@ let keys m f = m.m_members let parse ?file ?(path = []) s = - let rec classSpec yaml = + let rec classSpec ?(isTopLevel = false) yaml = let m = mapping yaml in let meta = let content = find_key m "meta" in @@ -174,6 +174,7 @@ let parse ?file ?(path = []) s = ClassSpec. { fileName = file; + isTopLevel; path; meta; doc; @@ -207,4 +208,4 @@ let parse ?file ?(path = []) s = | Ok x -> x | Error (`Msg msg) -> failwith msg in - classSpec yaml + classSpec ~isTopLevel:true yaml diff --git a/contrib/kaitai-ocaml/src/print.ml b/contrib/kaitai-ocaml/src/print.ml index ecaca247a950957c38edcf6730d12c4ee7552a1a..63cf7483536e02417a8bad664b8175b5a9dfc085 100644 --- a/contrib/kaitai-ocaml/src/print.ml +++ b/contrib/kaitai-ocaml/src/print.ml @@ -60,13 +60,8 @@ let metaSpec (t : MetaSpec.t) = t.endian @? [] -let classSpec _ = mapping [("test", scalar "test")] - let instanceSpec _ = mapping [("test", scalar "test")] -let types_spec types = - mapping (types |> List.map (fun (k, v) -> (k, classSpec v))) - let instances_spec instances = mapping (instances |> List.map (fun (k, v) -> (k, instanceSpec v))) @@ -85,19 +80,16 @@ let attr_type_if_not_any attr = if attr.AttrSpec.dataType = AnyType then None else Some ("type", scalar (DataType.to_string attr.AttrSpec.dataType)) -let size_header_mapping = mapping [("id", scalar "size"); ("type", scalar "u4")] - let attr_spec attr = match attr.AttrSpec.dataType with (* [BytesType] attr require size header. *) | BytesType (BytesLimitType {size; _}) -> - size_header_mapping - :: [ - mapping - (Some ("id", scalar attr.AttrSpec.id) - @? Some ("size", scalar (Ast.to_string size)) - @? []); - ] + [ + mapping + (Some ("id", scalar attr.AttrSpec.id) + @? Some ("size", scalar (Ast.to_string size)) + @? []); + ] | _ -> [ mapping @@ -114,15 +106,17 @@ let not_empty = function [] -> false | _ -> true let spec_if_non_empty name args f = if not_empty args then Some (name, f args) else None -let to_yaml (t : ClassSpec.t) = +let rec to_yaml (t : ClassSpec.t) = mapping - @@ Some ("meta", metaSpec t.meta) + @@ (if t.isTopLevel then Some ("meta", metaSpec t.meta) else None) @? spec_if_non_empty "types" t.types types_spec @? spec_if_non_empty "instances" t.instances instances_spec @? spec_if_non_empty "enums" t.enums enums_spec @? spec_if_non_empty "seq" t.seq seq_spec @? [] +and types_spec types = mapping (types |> List.map (fun (k, v) -> (k, to_yaml v))) + let print t = let y = to_yaml t in match Yaml.yaml_to_string y with Ok x -> x | Error (`Msg m) -> failwith m diff --git a/contrib/kaitai-ocaml/src/types.ml b/contrib/kaitai-ocaml/src/types.ml index a80a0a77a380e5b0ab8040f7123b5b4b07c5e2b8..4ee1ea08c66978c3df8b76203aaa0bfdb4ed65a6 100644 --- a/contrib/kaitai-ocaml/src/types.ml +++ b/contrib/kaitai-ocaml/src/types.ml @@ -322,6 +322,7 @@ end = struct | _ -> failwith "not supported") | NumericType (Float_type (FloatMultiType {width = _; endian = _})) -> "f8" | BytesType (BytesLimitType _) -> "fixed size (uint30) bytes" + | ComplexDataType (UserType class_spec) -> Option.get class_spec.meta.id | _ -> failwith "not supported" end @@ -401,6 +402,7 @@ and ClassSpec : sig type t = { fileName : string option; path : string list; + isTopLevel : bool; meta : MetaSpec.t; doc : DocSpec.t; toStringExpr : Ast.expr option; @@ -414,6 +416,7 @@ end = struct type t = { fileName : string option; path : string list; + isTopLevel : bool; meta : MetaSpec.t; doc : DocSpec.t; toStringExpr : Ast.expr option; diff --git a/contrib/kaitai-ocaml/src/types.mli b/contrib/kaitai-ocaml/src/types.mli index 78c7481995d6ce4ca71a4684ca5799c84994e1fb..53453dba9a2b59fcd78ce5290ecb48144c833548 100644 --- a/contrib/kaitai-ocaml/src/types.mli +++ b/contrib/kaitai-ocaml/src/types.mli @@ -15,7 +15,7 @@ module Identifier : sig end (** [Ast] module defines Kaitai Struct expression language. - + - For more about it see {{:https://doc.kaitai.io/user_guide.html#_expression_language} the Kaitai Struct User Guide}. - For a reference implementation {{:https://github.com/kaitai-io/kaitai_struct_compiler/blob/master/shared/src/main/scala/io/kaitai/struct/exprlang/Ast.scala} see}.*) module Ast : sig @@ -61,7 +61,7 @@ module Ast : sig | IntNum of int (** [IntNum] is originally represented by {@scala[scala.math.BigInt]}. *) | FloatNum of float - (** [FloatNum] is originally represented by {@scala[scala.math.BigDecimal]}. *) + (** [FloatNum] is originally represented by {@scala[scala.math.BigDecimal]}. *) | Str of string (** [Str] is a string expression. *) | Bool of bool (** [Bool] is a bool expression. *) | EnumByLabel of { @@ -76,9 +76,9 @@ module Ast : sig | CastToType of {value : t; typeName : typeId} (** [CastToType] is used for casting types. *) | ByteSizeOfType of {typeName : typeId} - (** [BytesSizeOfType] expression for specifying type size in bytes. *) + (** [BytesSizeOfType] expression for specifying type size in bytes. *) | BitSizeOfType of {typeName : typeId} - (** [BitSizeOfType] expression for specifying type size in bits. *) + (** [BitSizeOfType] expression for specifying type size in bits. *) | Subscript of {value : t; idx : t} (** Represents [[X[Y]]]. *) | Name of Identifier.t (** [Name] is used for defining variable, e.g. [x]. *) @@ -313,7 +313,8 @@ and ClassSpec : sig type t = { fileName : string option; path : string list; - (* isTopLevel : bool; *) + isTopLevel : bool; + (** if [isTopLevel class_spec] is true, we have a corresponding meta section. *) meta : MetaSpec.t; doc : DocSpec.t; toStringExpr : Ast.expr option; diff --git a/contrib/kaitai-struct-files/005-PsBabyM1__nonce.ksy b/contrib/kaitai-struct-files/005-PsBabyM1__nonce.ksy index 114711772904b79636a83a83f538e775d0750e2d..9f35a220b05e07b214039d639a9d748b6621bb56 100644 --- a/contrib/kaitai-struct-files/005-PsBabyM1__nonce.ksy +++ b/contrib/kaitai-struct-files/005-PsBabyM1__nonce.ksy @@ -1,8 +1,13 @@ meta: id: 005-PsBabyM1__nonce endian: be +types: + fixed_bytes: + seq: + - id: size + type: u4 + - id: value + size: size seq: -- id: size - type: u4 - id: fixed size (uint30) bytes - size: size + type: fixed_bytes diff --git a/contrib/kaitai-struct-files/005-PsBabyM1__seed.ksy b/contrib/kaitai-struct-files/005-PsBabyM1__seed.ksy index d72a4eb00e35fb2a27b87914c1d4e34c6c11ff5d..5505803490a5ced115b84a038c6330753ba29b6e 100644 --- a/contrib/kaitai-struct-files/005-PsBabyM1__seed.ksy +++ b/contrib/kaitai-struct-files/005-PsBabyM1__seed.ksy @@ -1,8 +1,13 @@ meta: id: 005-PsBabyM1__seed endian: be +types: + fixed_bytes: + seq: + - id: size + type: u4 + - id: value + size: size seq: -- id: size - type: u4 - id: fixed size (uint30) bytes - size: size + type: fixed_bytes diff --git a/contrib/kaitai-struct-files/006-PsCARTHA__nonce.ksy b/contrib/kaitai-struct-files/006-PsCARTHA__nonce.ksy index cedd9671d18c5989233ea1f43eb51e71c75c3872..cc10a9f785b4097af606059172b855dc7c132c7c 100644 --- a/contrib/kaitai-struct-files/006-PsCARTHA__nonce.ksy +++ b/contrib/kaitai-struct-files/006-PsCARTHA__nonce.ksy @@ -1,8 +1,13 @@ meta: id: 006-PsCARTHA__nonce endian: be +types: + fixed_bytes: + seq: + - id: size + type: u4 + - id: value + size: size seq: -- id: size - type: u4 - id: fixed size (uint30) bytes - size: size + type: fixed_bytes diff --git a/contrib/kaitai-struct-files/006-PsCARTHA__seed.ksy b/contrib/kaitai-struct-files/006-PsCARTHA__seed.ksy index 715cfb5646bbd1ab568aac65d6d7068803a466f6..4ef81db2348d099baee0c78916ad339e5921916e 100644 --- a/contrib/kaitai-struct-files/006-PsCARTHA__seed.ksy +++ b/contrib/kaitai-struct-files/006-PsCARTHA__seed.ksy @@ -1,8 +1,13 @@ meta: id: 006-PsCARTHA__seed endian: be +types: + fixed_bytes: + seq: + - id: size + type: u4 + - id: value + size: size seq: -- id: size - type: u4 - id: fixed size (uint30) bytes - size: size + type: fixed_bytes diff --git a/contrib/kaitai-struct-files/007-PsDELPH1__nonce.ksy b/contrib/kaitai-struct-files/007-PsDELPH1__nonce.ksy index 55f9022f5502ad69cb2ce6ddde68e9567a0fa14f..5d1f3305bf4af8bbeb59a436b8dae2427a904bb3 100644 --- a/contrib/kaitai-struct-files/007-PsDELPH1__nonce.ksy +++ b/contrib/kaitai-struct-files/007-PsDELPH1__nonce.ksy @@ -1,8 +1,13 @@ meta: id: 007-PsDELPH1__nonce endian: be +types: + fixed_bytes: + seq: + - id: size + type: u4 + - id: value + size: size seq: -- id: size - type: u4 - id: fixed size (uint30) bytes - size: size + type: fixed_bytes diff --git a/contrib/kaitai-struct-files/007-PsDELPH1__seed.ksy b/contrib/kaitai-struct-files/007-PsDELPH1__seed.ksy index 964c82c90c5a9b9b9e0466a7792a8253e116db03..a796c37ac7b8e948cc5edf7c766b3c40c0671b1f 100644 --- a/contrib/kaitai-struct-files/007-PsDELPH1__seed.ksy +++ b/contrib/kaitai-struct-files/007-PsDELPH1__seed.ksy @@ -1,8 +1,13 @@ meta: id: 007-PsDELPH1__seed endian: be +types: + fixed_bytes: + seq: + - id: size + type: u4 + - id: value + size: size seq: -- id: size - type: u4 - id: fixed size (uint30) bytes - size: size + type: fixed_bytes diff --git a/contrib/kaitai-struct-files/008-PtEdo2Zk__nonce.ksy b/contrib/kaitai-struct-files/008-PtEdo2Zk__nonce.ksy index 1109030fafa8f80e1011525eb74bd3743f2016c5..3d627188653f05ef16064333c547f8a3828b1105 100644 --- a/contrib/kaitai-struct-files/008-PtEdo2Zk__nonce.ksy +++ b/contrib/kaitai-struct-files/008-PtEdo2Zk__nonce.ksy @@ -1,8 +1,13 @@ meta: id: 008-PtEdo2Zk__nonce endian: be +types: + fixed_bytes: + seq: + - id: size + type: u4 + - id: value + size: size seq: -- id: size - type: u4 - id: fixed size (uint30) bytes - size: size + type: fixed_bytes diff --git a/contrib/kaitai-struct-files/008-PtEdo2Zk__seed.ksy b/contrib/kaitai-struct-files/008-PtEdo2Zk__seed.ksy index d880e32055cae963f323283abd9cf9a3399ad9c2..1105b0032a36294a3deb8e2dd9bdee1cb5c7dd7f 100644 --- a/contrib/kaitai-struct-files/008-PtEdo2Zk__seed.ksy +++ b/contrib/kaitai-struct-files/008-PtEdo2Zk__seed.ksy @@ -1,8 +1,13 @@ meta: id: 008-PtEdo2Zk__seed endian: be +types: + fixed_bytes: + seq: + - id: size + type: u4 + - id: value + size: size seq: -- id: size - type: u4 - id: fixed size (uint30) bytes - size: size + type: fixed_bytes diff --git a/contrib/kaitai-struct-files/009-PsFLoren__nonce.ksy b/contrib/kaitai-struct-files/009-PsFLoren__nonce.ksy index 277dce096d9ccf9530b389af05c40256a5d628ea..a1d29ccc831c98317ea457e148c3d25d789283c2 100644 --- a/contrib/kaitai-struct-files/009-PsFLoren__nonce.ksy +++ b/contrib/kaitai-struct-files/009-PsFLoren__nonce.ksy @@ -1,8 +1,13 @@ meta: id: 009-PsFLoren__nonce endian: be +types: + fixed_bytes: + seq: + - id: size + type: u4 + - id: value + size: size seq: -- id: size - type: u4 - id: fixed size (uint30) bytes - size: size + type: fixed_bytes diff --git a/contrib/kaitai-struct-files/009-PsFLoren__seed.ksy b/contrib/kaitai-struct-files/009-PsFLoren__seed.ksy index 8670eb37c42d90d979929ea1c11dc7b463e566c5..3295d970f045357d6d2e59317a59686b3606076d 100644 --- a/contrib/kaitai-struct-files/009-PsFLoren__seed.ksy +++ b/contrib/kaitai-struct-files/009-PsFLoren__seed.ksy @@ -1,8 +1,13 @@ meta: id: 009-PsFLoren__seed endian: be +types: + fixed_bytes: + seq: + - id: size + type: u4 + - id: value + size: size seq: -- id: size - type: u4 - id: fixed size (uint30) bytes - size: size + type: fixed_bytes diff --git a/contrib/kaitai-struct-files/010-PtGRANAD__nonce.ksy b/contrib/kaitai-struct-files/010-PtGRANAD__nonce.ksy index f0063f43d9a32b832b9eda541ab056c416e3a117..aa8351dcc3ae311107372c0476f7454c8a20d2e0 100644 --- a/contrib/kaitai-struct-files/010-PtGRANAD__nonce.ksy +++ b/contrib/kaitai-struct-files/010-PtGRANAD__nonce.ksy @@ -1,8 +1,13 @@ meta: id: 010-PtGRANAD__nonce endian: be +types: + fixed_bytes: + seq: + - id: size + type: u4 + - id: value + size: size seq: -- id: size - type: u4 - id: fixed size (uint30) bytes - size: size + type: fixed_bytes diff --git a/contrib/kaitai-struct-files/010-PtGRANAD__seed.ksy b/contrib/kaitai-struct-files/010-PtGRANAD__seed.ksy index ad323ed6227a12f1876b39e31120160fe7407258..41aa6cebd523dfdabb12cd585062202b6ffb74eb 100644 --- a/contrib/kaitai-struct-files/010-PtGRANAD__seed.ksy +++ b/contrib/kaitai-struct-files/010-PtGRANAD__seed.ksy @@ -1,8 +1,13 @@ meta: id: 010-PtGRANAD__seed endian: be +types: + fixed_bytes: + seq: + - id: size + type: u4 + - id: value + size: size seq: -- id: size - type: u4 - id: fixed size (uint30) bytes - size: size + type: fixed_bytes diff --git a/contrib/kaitai-struct-files/011-PtHangz2__nonce.ksy b/contrib/kaitai-struct-files/011-PtHangz2__nonce.ksy index 6127d6d870dfcf64c5ac3c513555bd20ab97a348..279feb50aa64b085ec74dba8739c371456611150 100644 --- a/contrib/kaitai-struct-files/011-PtHangz2__nonce.ksy +++ b/contrib/kaitai-struct-files/011-PtHangz2__nonce.ksy @@ -1,8 +1,13 @@ meta: id: 011-PtHangz2__nonce endian: be +types: + fixed_bytes: + seq: + - id: size + type: u4 + - id: value + size: size seq: -- id: size - type: u4 - id: fixed size (uint30) bytes - size: size + type: fixed_bytes diff --git a/contrib/kaitai-struct-files/011-PtHangz2__seed.ksy b/contrib/kaitai-struct-files/011-PtHangz2__seed.ksy index 89171ef31ecbcee9a8395c83d06b3d270d038ae8..922c648d41d5e12709ca5efe09000e4e97c1dba1 100644 --- a/contrib/kaitai-struct-files/011-PtHangz2__seed.ksy +++ b/contrib/kaitai-struct-files/011-PtHangz2__seed.ksy @@ -1,8 +1,13 @@ meta: id: 011-PtHangz2__seed endian: be +types: + fixed_bytes: + seq: + - id: size + type: u4 + - id: value + size: size seq: -- id: size - type: u4 - id: fixed size (uint30) bytes - size: size + type: fixed_bytes diff --git a/contrib/kaitai-struct-files/012-Psithaca__nonce.ksy b/contrib/kaitai-struct-files/012-Psithaca__nonce.ksy index 3c5a9b4ac4f527ba4bcb398907679bba3f20a762..cf5d8f5715202afca94f7a49474c094120dd8611 100644 --- a/contrib/kaitai-struct-files/012-Psithaca__nonce.ksy +++ b/contrib/kaitai-struct-files/012-Psithaca__nonce.ksy @@ -1,8 +1,13 @@ meta: id: 012-Psithaca__nonce endian: be +types: + fixed_bytes: + seq: + - id: size + type: u4 + - id: value + size: size seq: -- id: size - type: u4 - id: fixed size (uint30) bytes - size: size + type: fixed_bytes diff --git a/contrib/kaitai-struct-files/012-Psithaca__seed.ksy b/contrib/kaitai-struct-files/012-Psithaca__seed.ksy index ff373f03ebff7e945305e7747734e9f0e28423cf..746ee7a033621e15e274b1a875f458abcb1d8d50 100644 --- a/contrib/kaitai-struct-files/012-Psithaca__seed.ksy +++ b/contrib/kaitai-struct-files/012-Psithaca__seed.ksy @@ -1,8 +1,13 @@ meta: id: 012-Psithaca__seed endian: be +types: + fixed_bytes: + seq: + - id: size + type: u4 + - id: value + size: size seq: -- id: size - type: u4 - id: fixed size (uint30) bytes - size: size + type: fixed_bytes diff --git a/contrib/kaitai-struct-files/013-PtJakart__nonce.ksy b/contrib/kaitai-struct-files/013-PtJakart__nonce.ksy index 6963c428c6bbee62ec542152c375631982936aaa..91a24376374f60473eac5b7b24468027ed0cb175 100644 --- a/contrib/kaitai-struct-files/013-PtJakart__nonce.ksy +++ b/contrib/kaitai-struct-files/013-PtJakart__nonce.ksy @@ -1,8 +1,13 @@ meta: id: 013-PtJakart__nonce endian: be +types: + fixed_bytes: + seq: + - id: size + type: u4 + - id: value + size: size seq: -- id: size - type: u4 - id: fixed size (uint30) bytes - size: size + type: fixed_bytes diff --git a/contrib/kaitai-struct-files/013-PtJakart__seed.ksy b/contrib/kaitai-struct-files/013-PtJakart__seed.ksy index 3c37725cd6be46703f4a4ac8ecc1f3282810ecd6..3d0f74e9d4ba2987fe3008625ef53d65a4a3e7b9 100644 --- a/contrib/kaitai-struct-files/013-PtJakart__seed.ksy +++ b/contrib/kaitai-struct-files/013-PtJakart__seed.ksy @@ -1,8 +1,13 @@ meta: id: 013-PtJakart__seed endian: be +types: + fixed_bytes: + seq: + - id: size + type: u4 + - id: value + size: size seq: -- id: size - type: u4 - id: fixed size (uint30) bytes - size: size + type: fixed_bytes diff --git a/contrib/kaitai-struct-files/014-PtKathma__nonce.ksy b/contrib/kaitai-struct-files/014-PtKathma__nonce.ksy index f1c9bd78996e3044591814ce9547c00a3f5ce8d2..15cd0e0d1216eebd9402857130234aeeb293559a 100644 --- a/contrib/kaitai-struct-files/014-PtKathma__nonce.ksy +++ b/contrib/kaitai-struct-files/014-PtKathma__nonce.ksy @@ -1,8 +1,13 @@ meta: id: 014-PtKathma__nonce endian: be +types: + fixed_bytes: + seq: + - id: size + type: u4 + - id: value + size: size seq: -- id: size - type: u4 - id: fixed size (uint30) bytes - size: size + type: fixed_bytes diff --git a/contrib/kaitai-struct-files/014-PtKathma__seed.ksy b/contrib/kaitai-struct-files/014-PtKathma__seed.ksy index 678690d1ccdbf4206a5e66737a58821cc18238c9..62198ae676196b4a1311ebdf92c69bbcd8d5954b 100644 --- a/contrib/kaitai-struct-files/014-PtKathma__seed.ksy +++ b/contrib/kaitai-struct-files/014-PtKathma__seed.ksy @@ -1,8 +1,13 @@ meta: id: 014-PtKathma__seed endian: be +types: + fixed_bytes: + seq: + - id: size + type: u4 + - id: value + size: size seq: -- id: size - type: u4 - id: fixed size (uint30) bytes - size: size + type: fixed_bytes diff --git a/contrib/kaitai-struct-files/015-PtLimaPt__nonce.ksy b/contrib/kaitai-struct-files/015-PtLimaPt__nonce.ksy index dd6ecbfee9fe739936be43385d4c02eb9340ef2c..c374284144bdd05fd826590b15607368a1844a37 100644 --- a/contrib/kaitai-struct-files/015-PtLimaPt__nonce.ksy +++ b/contrib/kaitai-struct-files/015-PtLimaPt__nonce.ksy @@ -1,8 +1,13 @@ meta: id: 015-PtLimaPt__nonce endian: be +types: + fixed_bytes: + seq: + - id: size + type: u4 + - id: value + size: size seq: -- id: size - type: u4 - id: fixed size (uint30) bytes - size: size + type: fixed_bytes diff --git a/contrib/kaitai-struct-files/015-PtLimaPt__seed.ksy b/contrib/kaitai-struct-files/015-PtLimaPt__seed.ksy index 431cce6fcfad359313f98a084030cad626d66771..771a2708dc1d351025b7f7878c9dc77b2e7777c4 100644 --- a/contrib/kaitai-struct-files/015-PtLimaPt__seed.ksy +++ b/contrib/kaitai-struct-files/015-PtLimaPt__seed.ksy @@ -1,8 +1,13 @@ meta: id: 015-PtLimaPt__seed endian: be +types: + fixed_bytes: + seq: + - id: size + type: u4 + - id: value + size: size seq: -- id: size - type: u4 - id: fixed size (uint30) bytes - size: size + type: fixed_bytes diff --git a/contrib/kaitai-struct-files/016-PtMumbai__nonce.ksy b/contrib/kaitai-struct-files/016-PtMumbai__nonce.ksy index d5f0336fa4626240b1d356f47466f1ac106669bc..6a55d0c3452d3ee3505fb7a5dccd6dc8c704c7bc 100644 --- a/contrib/kaitai-struct-files/016-PtMumbai__nonce.ksy +++ b/contrib/kaitai-struct-files/016-PtMumbai__nonce.ksy @@ -1,8 +1,13 @@ meta: id: 016-PtMumbai__nonce endian: be +types: + fixed_bytes: + seq: + - id: size + type: u4 + - id: value + size: size seq: -- id: size - type: u4 - id: fixed size (uint30) bytes - size: size + type: fixed_bytes diff --git a/contrib/kaitai-struct-files/016-PtMumbai__seed.ksy b/contrib/kaitai-struct-files/016-PtMumbai__seed.ksy index f430b42b64e94eec6e91efffda10823883862894..62e1e04e5c61d024464e88c12f21d4a965a55f1c 100644 --- a/contrib/kaitai-struct-files/016-PtMumbai__seed.ksy +++ b/contrib/kaitai-struct-files/016-PtMumbai__seed.ksy @@ -1,8 +1,13 @@ meta: id: 016-PtMumbai__seed endian: be +types: + fixed_bytes: + seq: + - id: size + type: u4 + - id: value + size: size seq: -- id: size - type: u4 - id: fixed size (uint30) bytes - size: size + type: fixed_bytes diff --git a/contrib/kaitai-struct-files/017-PtNairob__nonce.ksy b/contrib/kaitai-struct-files/017-PtNairob__nonce.ksy index 01e0ced757d3cb154b89c3d68f2b19f0564f0415..5611e220bd7c6a1bae562100c0a8f4d1425bd292 100644 --- a/contrib/kaitai-struct-files/017-PtNairob__nonce.ksy +++ b/contrib/kaitai-struct-files/017-PtNairob__nonce.ksy @@ -1,8 +1,13 @@ meta: id: 017-PtNairob__nonce endian: be +types: + fixed_bytes: + seq: + - id: size + type: u4 + - id: value + size: size seq: -- id: size - type: u4 - id: fixed size (uint30) bytes - size: size + type: fixed_bytes diff --git a/contrib/kaitai-struct-files/017-PtNairob__seed.ksy b/contrib/kaitai-struct-files/017-PtNairob__seed.ksy index d60072b7d83761042590d3dd39d01147e0a7d652..c3639d596724c03c03c2fd4ff47afd450c542dd3 100644 --- a/contrib/kaitai-struct-files/017-PtNairob__seed.ksy +++ b/contrib/kaitai-struct-files/017-PtNairob__seed.ksy @@ -1,8 +1,13 @@ meta: id: 017-PtNairob__seed endian: be +types: + fixed_bytes: + seq: + - id: size + type: u4 + - id: value + size: size seq: -- id: size - type: u4 - id: fixed size (uint30) bytes - size: size + type: fixed_bytes diff --git a/contrib/kaitai-struct-files/018-Proxford__nonce.ksy b/contrib/kaitai-struct-files/018-Proxford__nonce.ksy index 40538146ad6e2e12c1db410a23bb7e68de305cbd..95348e828aba50e15922a4e1ccb293bb65ee9fa1 100644 --- a/contrib/kaitai-struct-files/018-Proxford__nonce.ksy +++ b/contrib/kaitai-struct-files/018-Proxford__nonce.ksy @@ -1,8 +1,13 @@ meta: id: 018-Proxford__nonce endian: be +types: + fixed_bytes: + seq: + - id: size + type: u4 + - id: value + size: size seq: -- id: size - type: u4 - id: fixed size (uint30) bytes - size: size + type: fixed_bytes diff --git a/contrib/kaitai-struct-files/018-Proxford__seed.ksy b/contrib/kaitai-struct-files/018-Proxford__seed.ksy index 26b500ada0e309e13866d8dd9cdd2598719afb65..60ed3a90a2b0c57c70f7fdc8d99ea265e8ecdf7a 100644 --- a/contrib/kaitai-struct-files/018-Proxford__seed.ksy +++ b/contrib/kaitai-struct-files/018-Proxford__seed.ksy @@ -1,8 +1,13 @@ meta: id: 018-Proxford__seed endian: be +types: + fixed_bytes: + seq: + - id: size + type: u4 + - id: value + size: size seq: -- id: size - type: u4 - id: fixed size (uint30) bytes - size: size + type: fixed_bytes diff --git a/contrib/kaitai-struct-files/alpha__nonce.ksy b/contrib/kaitai-struct-files/alpha__nonce.ksy index ec16631131a45994b3c0f9ff87aa816ca2379ec1..3e2270f4f9f10d5c8d6a467b7ee19ad80dddfddc 100644 --- a/contrib/kaitai-struct-files/alpha__nonce.ksy +++ b/contrib/kaitai-struct-files/alpha__nonce.ksy @@ -1,8 +1,13 @@ meta: id: alpha__nonce endian: be +types: + fixed_bytes: + seq: + - id: size + type: u4 + - id: value + size: size seq: -- id: size - type: u4 - id: fixed size (uint30) bytes - size: size + type: fixed_bytes diff --git a/contrib/kaitai-struct-files/alpha__seed.ksy b/contrib/kaitai-struct-files/alpha__seed.ksy index 35a45628034a4124294ed4dd66283bbd3de338c1..fe77ae8bea72e600b82a88ed36b39f22485848d0 100644 --- a/contrib/kaitai-struct-files/alpha__seed.ksy +++ b/contrib/kaitai-struct-files/alpha__seed.ksy @@ -1,8 +1,13 @@ meta: id: alpha__seed endian: be +types: + fixed_bytes: + seq: + - id: size + type: u4 + - id: value + size: size seq: -- id: size - type: u4 - id: fixed size (uint30) bytes - size: size + type: fixed_bytes diff --git a/contrib/kaitai-struct-files/distributed_db_version__name.ksy b/contrib/kaitai-struct-files/distributed_db_version__name.ksy index c75a2838934e38a7aa7531aba27959b6617ceb99..2da5656edc283de3881e4bb2bde7db525ad02bc9 100644 --- a/contrib/kaitai-struct-files/distributed_db_version__name.ksy +++ b/contrib/kaitai-struct-files/distributed_db_version__name.ksy @@ -1,8 +1,13 @@ meta: id: distributed_db_version__name endian: be +types: + fixed_bytes: + seq: + - id: size + type: u4 + - id: value + size: size seq: -- id: size - type: u4 - id: fixed size (uint30) bytes - size: size + type: fixed_bytes diff --git a/contrib/kaitai-struct-files/ground__bytes.ksy b/contrib/kaitai-struct-files/ground__bytes.ksy index 47f617d16f5de8bb27f7bafd6ab84f56b2564d2c..0d1bbdd5cd24b08cca55f00556478e99df0b187d 100644 --- a/contrib/kaitai-struct-files/ground__bytes.ksy +++ b/contrib/kaitai-struct-files/ground__bytes.ksy @@ -1,8 +1,13 @@ meta: id: ground__bytes endian: be +types: + fixed_bytes: + seq: + - id: size + type: u4 + - id: value + size: size seq: -- id: size - type: u4 - id: fixed size (uint30) bytes - size: size + type: fixed_bytes diff --git a/contrib/kaitai-struct-files/ground__string.ksy b/contrib/kaitai-struct-files/ground__string.ksy index 8fcd83b23f1b588f773e320de7abac9092554cd9..d6b4d37d3118d942482608bf9caa8aebfa7541b8 100644 --- a/contrib/kaitai-struct-files/ground__string.ksy +++ b/contrib/kaitai-struct-files/ground__string.ksy @@ -1,8 +1,13 @@ meta: id: ground__string endian: be +types: + fixed_bytes: + seq: + - id: size + type: u4 + - id: value + size: size seq: -- id: size - type: u4 - id: fixed size (uint30) bytes - size: size + type: fixed_bytes diff --git a/contrib/kaitai-struct-files/ground__variable__bytes.ksy b/contrib/kaitai-struct-files/ground__variable__bytes.ksy index faf668732b0068f11f893c78fb6ee534254f0bd0..c29fa429aa946a28e119e72b737882c656a1f3b1 100644 --- a/contrib/kaitai-struct-files/ground__variable__bytes.ksy +++ b/contrib/kaitai-struct-files/ground__variable__bytes.ksy @@ -1,8 +1,13 @@ meta: id: ground__variable__bytes endian: be +types: + fixed_bytes: + seq: + - id: size + type: u4 + - id: value + size: size seq: -- id: size - type: u4 - id: fixed size (uint30) bytes - size: size + type: fixed_bytes diff --git a/contrib/kaitai-struct-files/ground__variable__string.ksy b/contrib/kaitai-struct-files/ground__variable__string.ksy index 075992e65bcf968994c4070f0331678b7307e80f..d93d4d190394fd3fdf5e772a44abec85f97b6099 100644 --- a/contrib/kaitai-struct-files/ground__variable__string.ksy +++ b/contrib/kaitai-struct-files/ground__variable__string.ksy @@ -1,8 +1,13 @@ meta: id: ground__variable__string endian: be +types: + fixed_bytes: + seq: + - id: size + type: u4 + - id: value + size: size seq: -- id: size - type: u4 - id: fixed size (uint30) bytes - size: size + type: fixed_bytes diff --git a/contrib/kaitai-struct-files/sapling__transaction__binding_sig.ksy b/contrib/kaitai-struct-files/sapling__transaction__binding_sig.ksy index 9f4786ebf85b1611cb800147478eba88739bbea0..3d6cbbeb8364b73b952e74db68bffd6ef0f7e0eb 100644 --- a/contrib/kaitai-struct-files/sapling__transaction__binding_sig.ksy +++ b/contrib/kaitai-struct-files/sapling__transaction__binding_sig.ksy @@ -1,8 +1,13 @@ meta: id: sapling__transaction__binding_sig endian: be +types: + fixed_bytes: + seq: + - id: size + type: u4 + - id: value + size: size seq: -- id: size - type: u4 - id: fixed size (uint30) bytes - size: size + type: fixed_bytes diff --git a/contrib/kaitai-struct-files/sapling__transaction__commitment.ksy b/contrib/kaitai-struct-files/sapling__transaction__commitment.ksy index ef1ba4f8f3a049b985b3a2112529f08863d81efb..97b80e8e0aec60f32b5ae8f40c73ce8495e561bd 100644 --- a/contrib/kaitai-struct-files/sapling__transaction__commitment.ksy +++ b/contrib/kaitai-struct-files/sapling__transaction__commitment.ksy @@ -1,8 +1,13 @@ meta: id: sapling__transaction__commitment endian: be +types: + fixed_bytes: + seq: + - id: size + type: u4 + - id: value + size: size seq: -- id: size - type: u4 - id: fixed size (uint30) bytes - size: size + type: fixed_bytes diff --git a/contrib/kaitai-struct-files/sapling__transaction__commitment_hash.ksy b/contrib/kaitai-struct-files/sapling__transaction__commitment_hash.ksy index 69d4e7487b5e19769c51cf2656b5137020819a94..22be564a8a8b92f17a5e38720d77d6479c6905a4 100644 --- a/contrib/kaitai-struct-files/sapling__transaction__commitment_hash.ksy +++ b/contrib/kaitai-struct-files/sapling__transaction__commitment_hash.ksy @@ -1,8 +1,13 @@ meta: id: sapling__transaction__commitment_hash endian: be +types: + fixed_bytes: + seq: + - id: size + type: u4 + - id: value + size: size seq: -- id: size - type: u4 - id: fixed size (uint30) bytes - size: size + type: fixed_bytes diff --git a/contrib/kaitai-struct-files/sapling__transaction__commitment_value.ksy b/contrib/kaitai-struct-files/sapling__transaction__commitment_value.ksy index 914f53df80e381d2dd17c78d0cf66a9bca3c7c12..41ca8ccbc5e68a78584c04d4fd569316e7bf13ec 100644 --- a/contrib/kaitai-struct-files/sapling__transaction__commitment_value.ksy +++ b/contrib/kaitai-struct-files/sapling__transaction__commitment_value.ksy @@ -1,8 +1,13 @@ meta: id: sapling__transaction__commitment_value endian: be +types: + fixed_bytes: + seq: + - id: size + type: u4 + - id: value + size: size seq: -- id: size - type: u4 - id: fixed size (uint30) bytes - size: size + type: fixed_bytes diff --git a/contrib/kaitai-struct-files/sapling__transaction__nullifier.ksy b/contrib/kaitai-struct-files/sapling__transaction__nullifier.ksy index 780eac9d6d5d035ce081e68e06db452e039f6327..5c13d3477e24bf33d01bf3bcd2a8689f34769ff7 100644 --- a/contrib/kaitai-struct-files/sapling__transaction__nullifier.ksy +++ b/contrib/kaitai-struct-files/sapling__transaction__nullifier.ksy @@ -1,8 +1,13 @@ meta: id: sapling__transaction__nullifier endian: be +types: + fixed_bytes: + seq: + - id: size + type: u4 + - id: value + size: size seq: -- id: size - type: u4 - id: fixed size (uint30) bytes - size: size + type: fixed_bytes diff --git a/contrib/kaitai-struct-files/sapling__transaction__rcm.ksy b/contrib/kaitai-struct-files/sapling__transaction__rcm.ksy index 175f1368fb48368cb5a0bdab1d4e405cbbe22462..57bec9ffba0c4bff9981e14e211b2f9decb0e30b 100644 --- a/contrib/kaitai-struct-files/sapling__transaction__rcm.ksy +++ b/contrib/kaitai-struct-files/sapling__transaction__rcm.ksy @@ -1,8 +1,13 @@ meta: id: sapling__transaction__rcm endian: be +types: + fixed_bytes: + seq: + - id: size + type: u4 + - id: value + size: size seq: -- id: size - type: u4 - id: fixed size (uint30) bytes - size: size + type: fixed_bytes diff --git a/contrib/lib_kaitai_of_data_encoding/ground.ml b/contrib/lib_kaitai_of_data_encoding/ground.ml index 486c5d2a424098c5a49f6f8f38e9ac49de3e75bc..02388eb136092d791c54f74638ee939154188733 100644 --- a/contrib/lib_kaitai_of_data_encoding/ground.ml +++ b/contrib/lib_kaitai_of_data_encoding/ground.ml @@ -25,6 +25,7 @@ (*****************************************************************************) open Kaitai.Types +open Helpers let default_doc_spec = DocSpec.{summary = None; refs = []} @@ -46,20 +47,6 @@ let default_attr_spec = module Enum = struct type map = (string * Kaitai.Types.EnumSpec.t) list - let add enums ((k, e) as enum) = - let rec add = function - | [] -> enum :: enums - | ee :: _ when enum = ee -> - (* [enum] is already present in [enums] *) - enums - | (kk, ee) :: _ when String.equal kk k && not (ee = e) -> - (* [enum] key is already present in [enums], but for a different - [enum]. *) - raise (Invalid_argument "Enum.add: duplicate keys") - | _ :: enums -> add enums - in - add enums - let bool = ( "bool", EnumSpec. @@ -140,6 +127,9 @@ module Attr = struct let s2 = int_multi_type_atrr_spec ~id:"int16" ~signed:true DataType.W2 + let u4 ?(id = "uint32") () = + int_multi_type_atrr_spec ~id ~signed:false DataType.W4 + let s4 = int_multi_type_atrr_spec ~id:"int32" ~signed:true DataType.W4 let s8 = int_multi_type_atrr_spec ~id:"int64" ~signed:true DataType.W8 @@ -152,16 +142,49 @@ module Attr = struct let f8 = float_multi_type_attr_spec ~id:"float" + let fixed_size_header_class_spec = + { + (default_class_spec ~encoding_name:"fixed_bytes") with + seq = u4 ~id:"size" () :: [bytes_limit_type_attr_spec ~id:"value"]; + isTopLevel = false; + } + let bytes = (* TODO: https://gitlab.com/tezos/tezos/-/issues/6260 We fix size header to [`Uint30] for now. This corresponds to size header of ground bytes encoding. Later on we want to add support for [`Uint16], [`Uint8] and [`N]. *) - bytes_limit_type_attr_spec ~id:"fixed size (uint30) bytes" + { + default_attr_spec with + id = "fixed size (uint30) bytes"; + dataType = + DataType.(ComplexDataType (UserType fixed_size_header_class_spec)); + } - let string = - (* TODO: https://gitlab.com/tezos/tezos/-/issues/6260 - Same as with [Bytes] above, i.e. we need to add support for [`Uint16], - [`Uint8] and [`N] size header as well. *) - bytes_limit_type_attr_spec ~id:"fixed size (uint30) bytes" + let string = bytes +end + +module Class = struct + let bool ~encoding_name = + class_spec_of_attr ~encoding_name ~enums:[Enum.bool] Attr.bool + + let uint8 ~encoding_name = class_spec_of_attr ~encoding_name Attr.u1 + + let int8 ~encoding_name = class_spec_of_attr ~encoding_name Attr.s1 + + let uint16 ~encoding_name = class_spec_of_attr ~encoding_name Attr.u2 + + let int16 ~encoding_name = class_spec_of_attr ~encoding_name Attr.s2 + + let int32 ~encoding_name = class_spec_of_attr ~encoding_name Attr.s4 + + let int64 ~encoding_name = class_spec_of_attr ~encoding_name Attr.s8 + + let int31 ~encoding_name = class_spec_of_attr ~encoding_name Attr.int31 + + let float ~encoding_name = class_spec_of_attr ~encoding_name Attr.f8 + + let bytes ~encoding_name = class_spec_of_attr ~encoding_name Attr.bytes + + let string ~encoding_name = class_spec_of_attr ~encoding_name Attr.string end diff --git a/contrib/lib_kaitai_of_data_encoding/ground.mli b/contrib/lib_kaitai_of_data_encoding/ground.mli index c6e36b16082a7a151932e856c2ffb084e8e482d2..a2868362c025d8cec9f5f37d4476a0cff61c0a30 100644 --- a/contrib/lib_kaitai_of_data_encoding/ground.mli +++ b/contrib/lib_kaitai_of_data_encoding/ground.mli @@ -26,9 +26,6 @@ open Kaitai.Types -(** [default_doc_spec] is without summary and references. *) -val default_doc_spec : DocSpec.t - (** [Enum] module defines enum definitions needed for describing data-encoding ground types. *) module Enum : sig @@ -38,11 +35,6 @@ module Enum : sig (** [bool] is a mapping for boolean type. *) val bool : string * EnumSpec.t - - (** [add enums enum] returns a list of enum mappings. If [enums] don't contain - [enum], then new list with it is returned, otherwise existing [enums] list - is returned. *) - val add : map -> string * EnumSpec.t -> map end (** [Attr] is module for getting [AttrSpec.t] of ground types. *) @@ -81,3 +73,40 @@ module Attr : sig (** [string] returns [AttrSpec.t] definition of [Data_encoding.string]. *) val string : AttrSpec.t end + +(** [Class] module consists of [ClassSpec.t] for ground types. *) +module Class : sig + (** [bool] returns [ClassSpec.t] definition of bool ground type. *) + val bool : encoding_name:string -> ClassSpec.t + + (** [uint8] returns [ClassSpec.t] definition of 8-bit unsigned integer. *) + val uint8 : encoding_name:string -> ClassSpec.t + + (** [int8] returns [ClassSpec.t] definition of 8-bit signed integer. *) + val int8 : encoding_name:string -> ClassSpec.t + + (** [uin16] returns [ClassSpec.t] definition of 16-bit unsigned integer. *) + val uint16 : encoding_name:string -> ClassSpec.t + + (** [int16] returns [ClassSpec.t] definition of 16-bit signed integer. *) + val int16 : encoding_name:string -> ClassSpec.t + + (** [int32] returns [ClassSpec.t] definition of 32-bit signed integer. *) + val int32 : encoding_name:string -> ClassSpec.t + + (** [int64] returns [ClassSpec.t] definition of 64-bit signed integer. *) + val int64 : encoding_name:string -> ClassSpec.t + + (** [int31] returns [ClassSpec.t] definition of 31-bit signed integer. + For more about this type see [Data_encoding.int31]. *) + val int31 : encoding_name:string -> ClassSpec.t + + (** [float] returns [ClassSpec.t] definition of 64-bit float. *) + val float : encoding_name:string -> ClassSpec.t + + (** [bytes] returns [ClassSpec.t] definition of [Data_encoding.bytes]. *) + val bytes : encoding_name:string -> ClassSpec.t + + (** [string] returns [ClassSpec.t] definition of [Data_encoding.string]. *) + val string : encoding_name:string -> ClassSpec.t +end diff --git a/contrib/lib_kaitai_of_data_encoding/helpers.ml b/contrib/lib_kaitai_of_data_encoding/helpers.ml new file mode 100644 index 0000000000000000000000000000000000000000..bc3936f9e123ff7460f2e22b1e6720794fa661db --- /dev/null +++ b/contrib/lib_kaitai_of_data_encoding/helpers.ml @@ -0,0 +1,82 @@ +(*****************************************************************************) +(* *) +(* SPDX-License-Identifier: MIT *) +(* Copyright (c) 2023 Marigold, *) +(* Copyright (c) 2023 Nomadic Labs, *) +(* *) +(*****************************************************************************) + +open Kaitai.Types + +let default_doc_spec = DocSpec.{summary = None; refs = []} + +let cond_no_cond = + AttrSpec.ConditionalSpec.{ifExpr = None; repeat = RepeatSpec.NoRepeat} + +let default_attr_spec = + AttrSpec. + { + path = []; + id = ""; + dataType = DataType.AnyType; + cond = cond_no_cond; + valid = None; + doc = default_doc_spec; + enum = None; + } + +let default_meta_spec ~encoding_name = + MetaSpec. + { + path = []; + isOpaque = false; + id = Some encoding_name; + endian = Some `BE; + bitEndian = None; + encoding = None; + forceDebug = false; + opaqueTypes = None; + zeroCopySubstream = None; + imports = []; + } + +let default_class_spec ~encoding_name = + ClassSpec. + { + fileName = None; + path = []; + meta = default_meta_spec ~encoding_name; + isTopLevel = true; + doc = default_doc_spec; + toStringExpr = None; + params = []; + seq = []; + types = []; + instances = []; + enums = []; + } + +let add_uniq_assoc mappings ((k, v) as mapping) = + match List.assoc_opt k mappings with + | None -> mapping :: mappings + | Some vv -> + if v = vv then mappings + else raise (Invalid_argument "Mappings.add: duplicate keys") + +let types_field_from_attr_seq attributes = + let types = + List.filter_map + (fun {AttrSpec.dataType; _} -> + match dataType with + | DataType.ComplexDataType (UserType class_spec) -> ( + match class_spec.meta.id with + | Some id -> Some (id, class_spec) + | None -> failwith "User defined type has no name") + | _ -> None) + attributes + in + List.fold_left add_uniq_assoc [] types + +let class_spec_of_attr ~encoding_name ?(enums = []) attr = + let types = types_field_from_attr_seq [attr] in + {(default_class_spec ~encoding_name) with seq = [attr]; enums; types} diff --git a/contrib/lib_kaitai_of_data_encoding/helpers.mli b/contrib/lib_kaitai_of_data_encoding/helpers.mli new file mode 100644 index 0000000000000000000000000000000000000000..dbc9a4e5380b9b6fcf4b511036abbe24d9446b11 --- /dev/null +++ b/contrib/lib_kaitai_of_data_encoding/helpers.mli @@ -0,0 +1,76 @@ +(*****************************************************************************) +(* *) +(* Open Source License *) +(* Copyright (c) 2023 Marigold, *) +(* Copyright (c) 2023 Nomadic Labs, *) +(* *) +(* Permission is hereby granted, free of charge, to any person obtaining a *) +(* copy of this software and associated documentation files (the "Software"),*) +(* to deal in the Software without restriction, including without limitation *) +(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *) +(* and/or sell copies of the Software, and to permit persons to whom the *) +(* Software is furnished to do so, subject to the following conditions: *) +(* *) +(* The above copyright notice and this permission notice shall be included *) +(* in all copies or substantial portions of the Software. *) +(* *) +(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*) +(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *) +(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *) +(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*) +(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *) +(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *) +(* DEALINGS IN THE SOFTWARE. *) +(* *) +(*****************************************************************************) + +(* This module consists of helpers for building kaitai specifications. *) + +open Kaitai.Types + +(** [default_doc_spec] is without summary and references. *) +val default_doc_spec : DocSpec.t + +(** [cond_no_cond] is default conditional specification that has no [if] + expression and no repetition. *) +val cond_no_cond : AttrSpec.ConditionalSpec.t + +(** [default_attr_spec] is initialized with default (empty) values. *) +val default_attr_spec : AttrSpec.t + +(** [default_meta_spec ~encoding_name] returns default [MetaSpec.t]. + + The following meta section properties are set: + - [endian] is set to [BE] (as per data-encoding default). + - [id] is set to [~encoding_name]. + - Other fields are [[]] or [None]. *) +val default_meta_spec : encoding_name:string -> MetaSpec.t + +(** [default_class_spec ~encoding_name] builds an default (empty) [ClassSpec.t]. + + @param ~encoding_name is added to meta section as [id]. *) +val default_class_spec : encoding_name:string -> ClassSpec.t + +(** [add_uniq_assoc kvs kv] returns an association list with associations from + [kvs] as well as [kv]. + + If [kvs] already contains [kv], then [kvs] is returned. + + @raises Invalid_argument if [kvs] includes an association with the same key + as but a different value than [kv]. *) +val add_uniq_assoc : (string * 'a) list -> string * 'a -> (string * 'a) list + +(** [class_spec_of_attr ~encoding_name ?enums attr] returns a [ClassSpet.t] + for [attr]. + + In case of [attr] being of [ComplexDataType UserType _] type, then [types] + section of returned [ClassSpec.t] is automatically populated with + an appropriate type. + + @param ~encoding_name is added to meta section as [id]. + @param ?enums is added to class specification if present. *) +val class_spec_of_attr : + encoding_name:string -> + ?enums:(string * EnumSpec.t) list -> + AttrSpec.t -> + ClassSpec.t diff --git a/contrib/lib_kaitai_of_data_encoding/test/test_translation_of_ground_types.ml b/contrib/lib_kaitai_of_data_encoding/test/test_translation_of_ground_types.ml index da6a626ad4a1f99a84121089d9bc5ead91c6a6b9..59348c1146b29ac51c372f0c0566204cc51c4434 100644 --- a/contrib/lib_kaitai_of_data_encoding/test/test_translation_of_ground_types.ml +++ b/contrib/lib_kaitai_of_data_encoding/test/test_translation_of_ground_types.ml @@ -192,20 +192,21 @@ let%expect_test "test fixed size bytes translation" = Data_encoding.bytes in print_endline (Kaitai.Print.print s) ; - (* TODO: https://gitlab.com/tezos/tezos/-/issues/6258 - Consider adding support for user defined types, - and using this feature to write a more kaitai - idiomatic spec file for [ground_bytes]. *) [%expect {| meta: id: ground_bytes endian: be + types: + fixed_bytes: + seq: + - id: size + type: u4 + - id: value + size: size seq: - - id: size - type: u4 - id: fixed size (uint30) bytes - size: size + type: fixed_bytes |}] let%expect_test "test fixed size string translation" = @@ -220,9 +221,14 @@ let%expect_test "test fixed size string translation" = meta: id: ground_string endian: be + types: + fixed_bytes: + seq: + - id: size + type: u4 + - id: value + size: size seq: - - id: size - type: u4 - id: fixed size (uint30) bytes - size: size + type: fixed_bytes |}] diff --git a/contrib/lib_kaitai_of_data_encoding/translate.ml b/contrib/lib_kaitai_of_data_encoding/translate.ml index 20d7c18b9ea079a31dd8cd3a0c1034ac470742a8..1d3a798d6b7d52e9a604b48c1f7706ca176f05f5 100644 --- a/contrib/lib_kaitai_of_data_encoding/translate.ml +++ b/contrib/lib_kaitai_of_data_encoding/translate.ml @@ -31,36 +31,6 @@ open Kaitai.Types than the public module [Data_encoding.Encoding]. *) open Data_encoding__Encoding -let default_meta_spec ~encoding_name = - MetaSpec. - { - path = []; - isOpaque = false; - id = Some encoding_name; - endian = Some `BE; - bitEndian = None; - encoding = None; - forceDebug = false; - opaqueTypes = None; - zeroCopySubstream = None; - imports = []; - } - -let default_class_spec ~encoding_name = - ClassSpec. - { - fileName = None; - path = []; - meta = default_meta_spec ~encoding_name; - doc = Ground.default_doc_spec; - toStringExpr = None; - params = []; - seq = []; - types = []; - instances = []; - enums = []; - } - let rec seq_field_of_data_encoding : type a. (string * EnumSpec.t) list -> @@ -72,7 +42,7 @@ let rec seq_field_of_data_encoding : | Empty -> (enums, []) | Ignore -> (enums, []) | Constant _ -> (enums, []) - | Bool -> (Ground.Enum.add enums Ground.Enum.bool, [Ground.Attr.bool]) + | Bool -> (Helpers.add_uniq_assoc enums Ground.Enum.bool, [Ground.Attr.bool]) | Uint8 -> (enums, [Ground.Attr.u1]) | Conv {encoding; _} -> seq_field_of_data_encoding enums encoding | Tup e -> @@ -95,21 +65,18 @@ let rec seq_field_of_data_encoding : let rec from_data_encoding : type a. encoding_name:string -> a Data_encoding.t -> ClassSpec.t = fun ~encoding_name {encoding; json_encoding = _} -> - let class_spec_of_ground ?(enums = []) ground = - {(default_class_spec ~encoding_name) with seq = [ground]; enums} - in match encoding with - | Bool -> class_spec_of_ground ~enums:[Ground.Enum.bool] Ground.Attr.bool - | Uint8 -> class_spec_of_ground Ground.Attr.u1 - | Int8 -> class_spec_of_ground Ground.Attr.s1 - | Uint16 -> class_spec_of_ground Ground.Attr.u2 - | Int16 -> class_spec_of_ground Ground.Attr.s2 - | Int32 -> class_spec_of_ground Ground.Attr.s4 - | Int64 -> class_spec_of_ground Ground.Attr.s8 - | Int31 -> class_spec_of_ground Ground.Attr.int31 - | Float -> class_spec_of_ground Ground.Attr.f8 - | Bytes (_kind_length, _) -> class_spec_of_ground Ground.Attr.bytes - | String (_kind_length, _) -> class_spec_of_ground Ground.Attr.string + | Bool -> Ground.Class.bool ~encoding_name + | Uint8 -> Ground.Class.uint8 ~encoding_name + | Int8 -> Ground.Class.int8 ~encoding_name + | Uint16 -> Ground.Class.uint16 ~encoding_name + | Int16 -> Ground.Class.int16 ~encoding_name + | Int32 -> Ground.Class.int32 ~encoding_name + | Int64 -> Ground.Class.int64 ~encoding_name + | Int31 -> Ground.Class.int31 ~encoding_name + | Float -> Ground.Class.float ~encoding_name + | Bytes (_kind_length, _) -> Ground.Class.bytes ~encoding_name + | String (_kind_length, _) -> Ground.Class.string ~encoding_name | Tup e -> (* Naked Tup likely due to [tup1]. We simply ignore this constructor. *) from_data_encoding ~encoding_name e @@ -122,7 +89,7 @@ let rec from_data_encoding : (fun i attr -> AttrSpec.{attr with id = Printf.sprintf "field_%d" i}) seq in - {(default_class_spec ~encoding_name) with seq; enums} + {(Helpers.default_class_spec ~encoding_name) with seq; enums} | Conv {encoding; _} -> from_data_encoding ~encoding_name encoding | Describe {encoding; _} -> (* TODO: patch the documentation to include available information *)