News for hackers. Stuff that matters.
Re:Kaitai: Describe the structure of data, not how you read ... by carom on Saturday December 05, 2020 @02:14PM (#25317380)
I do love kaitai and recently contributed a grammar, but both the title and the copy talk about writing.

> Reading and writing binary formats is hard... Kaitai Struct tries to make this job easier...

Kaitai cannot write data back out. [1] This is a major limitation for me. It would be nice to use it as a mutation engine for fuzzing, but without being able to write it back out, it is mostly just beneficial for analysis.

1. https://doc.kaitai.io/faq.html#writing

Re:Kaitai: Describe the structure of data, not how you read ... by kevinherron on Saturday December 05, 2020 @02:45PM (#25317658)
Yikes, huge limitation. Guess I won't be looking into this any further.
Re:Kaitai: Describe the structure of data, not how you read ... by imtringued on Saturday December 05, 2020 @03:26PM (#25317980)
It would be a CS breakthrough if it could... You're asking a lot...
Re:Kaitai: Describe the structure of data, not how you read ... by nextaccountic on Saturday December 05, 2020 @07:33PM (#25319810)
This breakthrough happened already. This is called bidirectional or invertible parsing. See it discussed here https://news.ycombinator.com/item?id=16392654

And this paper https://dl.acm.org/doi/10.1145/1863523.1863525 "Invertible syntax descriptions: unifying parsing and pretty printing"

And this Haskell library https://hackage.haskell.org/package/roundtrip among others

Re:Kaitai: Describe the structure of data, not how you read ... by jacb on Saturday December 05, 2020 @03:41PM (#25318125)
I might be missing something - why would this be a breakthrough? It sounds complicated to generate the interfaces, sure, but is there a theoretical problem blocking this, or just practical?
Re:Kaitai: Describe the structure of data, not how you read ... by convolvatron on Saturday December 05, 2020 @03:57PM (#25318274)
I've looked at this problem quite a bit over the years...I agree with you completely. there isn't anything fundamental here, just the normal cultural adoption issues, usability, etc. There may be some compilation/complexity issues around formats with variable length fields and self-description, but certainly less problematic than general purpose programming.

I really wish though that there were more traction here, as I really believe that we should be quite prepared do deal with bits and not protobufs by default. nothing wrong with protobufs for quite a number of uses. I just don't know why people are so afraid of and/or biased against bit strings.

Re:Kaitai: Describe the structure of data, not how you read ... by joshspankit on Saturday December 05, 2020 @04:33PM (#25318606)
Through this thread I really feel like I’m missing something.

Are we not talking about writing binary files that conform to the spec?

Like, in the case of a GIF: simply writing valid garbage data should produce a file that presents as a valid GIF with noise for the image. Similarly: reading the file through the parser and writing it out unmodified should create an identical file (assuming no stenography).

Right?

Re:Kaitai: Describe the structure of data, not how you read ... by IceDane on Saturday December 05, 2020 @07:10PM (#25319678)
You're right. The person saying this would be a breakthrough doesn't understand what this is doing.

There are already similarly declarative tools which can accomplish this. Haskell has binary parsing libraries which work similarly and give you both reading and writing capabilities.

Re:Kaitai: Describe the structure of data, not how you read ... by spullara on Saturday December 05, 2020 @05:21PM (#25318970)
I honestly don't get it either. The inverse of the read spec is the write spec. My guess having not dug deeply is that they don't distinguish between required and optional fields, that said, they should still be able to write what they have based on the read spec but could potentially be still an invalid file.
Re:Kaitai: Describe the structure of data, not how you read ... by armsforsharks on Saturday December 05, 2020 @06:53PM (#25319574)
It's a really interesting idea! I'm also surprised there hasn't been much traction here. I've started a Rust library for this: https://github.com/sharksforarms/deku

It's a declarative bit-level symmetrical reader writer generator library.

Re:Kaitai: Describe the structure of data, not how you read ... by afranchuk on Saturday December 05, 2020 @09:23PM (#25320457)
Yeah I was also confused. I wrote a bidirectional parser/writer layer for yaml in a haskell program I had at work. The yaml structure mappings were all declarative in the code and even allowed documentation for the structures to be printed out. It's not that hard once you define the primitive bidirectional (higher-order) mapping function to go from a `Configurable a` to a `Configurable b`, the rest kind of unfolds from there.
Re:Kaitai: Describe the structure of data, not how you read ... by HelloNurse on Sunday December 06, 2020 @10:37AM (#25324157)
Binary file formats can be vastly more convoluted than YAML. For example, consider roundtripping ZIP archives or PDF documents, or both at the same time (see https://www.alchemistowl.org/pocorgtfo/).
Re:Kaitai: Describe the structure of data, not how you read ... by IceDane on Saturday December 05, 2020 @07:08PM (#25319663)
How exactly would it be a cs breakthrough? Are you sure you understand what this does?
Re:Kaitai: Describe the structure of data, not how you read ... by kevinherron on Saturday December 05, 2020 @03:49PM (#25318188)
Are we talking about different things?

ASN.1 does more or less what I was hoping Kaitai can do. Where's the breakthrough?

I just want to be able to describe network protocols in some "language" and generate code that can serialize/deserialize it.

Re:Kaitai: Describe the structure of data, not how you read ... by comex on Saturday December 05, 2020 @04:13PM (#25318426)
Kaitai is designed to describe arbitrary, preexisting binary formats. You can’t do that with ASN.1.
Re:Kaitai: Describe the structure of data, not how you read ... by touisteur on Sunday December 06, 2020 @02:40AM (#25321868)
Err. I'm not sure what asn.1 is missing for this? I've seen lots of people use asn.1 exactly for this (i.e. writing a grammar for an arbitrary pre-existing binary format not readily described in asn.1).
Re:Kaitai: Describe the structure of data, not how you read ... by ithkuil on Sunday December 06, 2020 @03:09AM (#25321972)
asn.1 distinguishes between schema and encoding; there are many binary encodings and you can technically devise a custom one that would let you describe the high level structure with an asn.1 grammar and then lay out the actual bits with a custom encoding format so that it matches the pre-existing format you're writing the new serde for). This may work as many formats have this leveled approach. E.g. the lowest level of the spec may tell something about how to encode integers (all integers are 32-bit big endian, or varlen encoded ...), sequences (ength prefixed, or terminated by a sentinel.

Any chances you have some reference to what you saw?

Re:Kaitai: Describe the structure of data, not how you read ... by touisteur on Sunday December 06, 2020 @07:16AM (#25323023)
Right I think we were working with PER (tagless) to match a complex multi-layer protocol. Can't share the exemple but let's say asn.1 helped generate saner code than the handrolled one AND allowed other languages to decode...

I understand what you're saying though. Memories of using this were... unpleasant. I think the 'best' alternative for me would be RecordFlux. Ada-like syntax, generation of AoRTE-provable SPARK code, and recently the expressivity of the tool has increased. And the whole thing is in python, easily extensible to build things from the type description: generators, fuzzers, advanced specific parsers (need only one field and want the control fields' positions and sizes), wireshark plugins, Postgres extensions...

I really like what they're doing there. Might be the one of the low-effort (for the user!) lead-bullets for safer software.

Re:Kaitai: Describe the structure of data, not how you read ... by kevinherron on Saturday December 05, 2020 @04:41PM (#25318673)
I know, I was just using it as kind of an example of what I want and of a similar problem. Maybe not the right example.
Re:Kaitai: Describe the structure of data, not how you read ... by OJFord on Saturday December 05, 2020 @10:31PM (#25320775)
I think I'm missing something, but rust's `serde`?
Re:Kaitai: Describe the structure of data, not how you read ... by quiescant_dodo on Saturday December 05, 2020 @03:39PM (#25318101)
Is this a known theoretical CS problem?
Re:Kaitai: Describe the structure of data, not how you read ... by floppy123 on Saturday December 05, 2020 @02:59PM (#25317764)
Any other lib that also generates Encoder/Writer code?
Re:Kaitai: Describe the structure of data, not how you read ... by comex on Saturday December 05, 2020 @03:33PM (#25318049)
The Construct library for Python can do it:

https://construct.readthedocs.io/en/latest/intro.html#exampl...

I’ve long searched for something better than Construct, but so far I have yet to find even an equal.

Re:Kaitai: Describe the structure of data, not how you read ... by attheicearcade on Saturday December 05, 2020 @04:26PM (#25318538)
It can, but it can get incredibly slow for large formats. I was using it to reverse engineer some binary game formats but the parser would take a couple of minutes to complete. I rewrote it using struct and that time dropped to a few seconds. Useful for probing an unknown format, but I prefer the 010 editor since it’s more interactive.
Re:Kaitai: Describe the structure of data, not how you read ... by wpietri on Saturday December 05, 2020 @04:24PM (#25318516)
Ooh, exciting! I built a parser [1] for AIS messages [2], a quirky ship-to-ship protocol. My lower-level stuff always felt clumsy to me. I'll have to see if this cleans it up.

[1] https://github.com/wpietri/simpleais [2] https://gpsd.gitlab.io/gpsd/AIVDM.html

Re:Kaitai: Describe the structure of data, not how you read ... by tprynn on Saturday December 05, 2020 @10:14PM (#25320694)
BinData (https://github.com/dmendel/bindata) is a Ruby gem for this, basically using a DSL in Ruby to declaratively define binary data formats that can be both read and written.
Re:Kaitai: Describe the structure of data, not how you read ... by jchw on Saturday December 05, 2020 @09:07PM (#25320363)
I wrote a sort-of adjacent library for Go at one point. I’m a bit stuck trying to figure out exactly what to do on 2.0 but it has a lot of Kaitai like features including an expression language for transforming things (on master version) and it supports writing structures out.

https://github.com/go-restruct/restruct

Re:Kaitai: Describe the structure of data, not how you read ... by armsforsharks on Saturday December 05, 2020 @04:48PM (#25318737)
I have been working on Deku: a declarative binary reading and writing: bit-level, symmetric, serialization/deserialization library. https://github.com/sharksforarms/deku
Re:Kaitai: Describe the structure of data, not how you read ... by floppy123 on Sunday December 06, 2020 @04:25AM (#25322280)
and still there is no generator that creates efficient and partialy streamed readers/writers for high performance protocols or resource constraint environments (as less pre allocations as possible, zero copy concepts,..., streamed reading/writing, good inline possibility, ...)

100% fix formats with no self reference or for example checksums that sits in front of the checksumd data (no streaming possible)...

Re:Kaitai: Describe the structure of data, not how you read ... by touisteur on Sunday December 06, 2020 @02:38AM (#25321864)
Recordflux?
Re:Kaitai: Describe the structure of data, not how you read ... by layoutIfNeeded on Saturday December 05, 2020 @07:38PM (#25319840)
I guess the submission title is incorrect then:

>Kaitai: Describe the structure of data, not how you read or write it

Re:Kaitai: Describe the structure of data, not how you read ... by junon on Monday December 07, 2020 @05:36AM (#25330915)
Yep this is why I dropped it.

I started working on an alternative that supported writing but didn't follow through since I didn't think many people were interested in Kaitai to begin with.

Re:Kaitai: Describe the structure of data, not how you read ... by iab on Saturday December 05, 2020 @02:57PM (#25317750)
Wow - thanks for the heads-up