Kaitai Struct:声明式二进制格式解析语言
Kaitai Struct: declarative binary format parsing language

原始链接: https://kaitai.io/

这段文字描述了如何使用Kaitai Struct解析GIF文件,Kaitai Struct是一种用于解析二进制文件格式的声明式语言。`.gif`文件通常使用小端编码,并以头部和逻辑屏幕块开始。 Kaitai Struct允许您在`.ksy`文件中定义GIF文件结构,然后将其编译成C++、C#、Go、Java、JavaScript、Lua、Nim、Perl、PHP、Python、Ruby和Rust等多种编程语言的源代码。 提供的代码片段演示了如何在这些语言中加载GIF文件,并通过`logical_screen`对象访问图像宽度和高度等基本信息。 基本上,Kaitai Struct自动化了复杂的二进制文件解析过程,提供了一种方便的方法来从GIF文件中提取数据,跨越不同的平台和语言。 文档提供了关于Kaitai Struct功能的更多细节。

## Kaitai Struct:声明式二进制格式解析 Kaitai Struct 是一种流行的声明式语言,用于解析二进制文件格式。用户称赞它能够清晰高效地记录格式,用于反向工程、科学数据解析和游戏格式分析等任务。最近的一个重要更新(v0.11)为 Python 和 Java 添加了序列化功能,允许不仅读取数据,还可以生成数据——这对于模糊测试和程序分析非常有用。 虽然不是最快的选择,但 Kaitai Struct 擅长从单个规范创建多种语言的解析器。它通常与 Wuffs 和 Protodata 等工具进行比较,提供了一种不同的方法,侧重于描述和解码,而不是完整实现或转换。 该项目拥有一个 Web IDE ([https://ide.kaitai.io/](https://ide.kaitai.io/)),方便实验,并且最近获得了 Rust 的支持,以及对 Nixpkgs 等包管理器的贡献。一些用户指出 YAML 格式可能冗长,并且目前缺少纯 C 后端,但 Zig 目标正在开发中。
相关文章

原文

It declares that a GIF file usually has a .gif extension and uses little-endian integer encoding. The file itself starts with two blocks: first comes header and then comes logical_screen:

This .ksy file can be compiled into gif.cpp / Gif.cs / gif.go / Gif.java / Gif.js / gif.lua / gif.nim / Gif.pm / Gif.php / gif.py / gif.rb and then one can instantly load a .gif file and access, for example, its width and height.

std::ifstream ifs("path/to/some.gif", std::ifstream::binary);
kaitai::kstream ks(&ifs);
gif_t g = gif_t(&ks);

std::cout << "width = " << g.logical_screen()->image_width() << std::endl;
std::cout << "height = " << g.logical_screen()->image_height() << std::endl;
Gif g = Gif.FromFile("path/to/some.gif");

Console.WriteLine("width = " + g.LogicalScreen.ImageWidth);
Console.WriteLine("height = " + g.LogicalScreen.ImageHeight);
file, err := os.Open("path/to/some.gif")
g := NewGif()
err = g.Read(kaitai.NewStream(file), nil, g)

fmt.Printf("width = %d\n", g.LogicalScreen.ImageWidth)
fmt.Printf("height = %d\n", g.LogicalScreen.ImageHeight)
Gif g = Gif.fromFile("path/to/some.gif");

System.out.println("width = " + g.logicalScreen().imageWidth());
System.out.println("height = " + g.logicalScreen().imageHeight());
var g = new Gif(new KaitaiStream(someArrayBuffer));

console.log("width = " + g.logicalScreen.imageWidth);
console.log("height = " + g.logicalScreen.imageHeight);
local g = Gif:from_file("path/to/some.gif")

print("width = " .. g.logical_screen.image_width)
print("height = " .. g.logical_screen.image_height)
let g = Gif.fromFile("path/to/some.gif")

echo "width = " & $g.logicalScreen.imageWidth
echo "height = " & $g.logicalScreen.imageHeight
my $g = Gif->from_file("path/to/some.gif");

print("width = ", $g->logical_screen()->image_width(), "\n");
print("height = ", $g->logical_screen()->image_height(), "\n");
$g = Gif::fromFile("path/to/some.gif");

print("width = " . $g->logicalScreen()->imageWidth() . "\n");
print("height = " . $g->logicalScreen()->imageHeight() . "\n");
g = Gif.from_file("path/to/some.gif")

print("width = %d" % (g.logical_screen.image_width))
print("height = %d" % (g.logical_screen.image_height))
g = Gif.from_file("path/to/some.gif")

puts "width = #{g.logical_screen.image_width}"
puts "height = #{g.logical_screen.image_height}"
let bytes = fs::read("path/to/some.gif").unwrap();
let io = BytesReader::from(bytes);
let g: OptRc<Gif> = Gif::read_into(&io, None, None).unwrap();

println!("width = {}", *g.logical_screen().image_width());
println!("height = {}", *g.logical_screen().image_height());
Of course, this example shows only a very limited subset of what Kaitai Struct can do. Please refer to the documentation for more insights.
联系我们 contact @ memedata.com