kaitai_struct

C#源码 2025-08-11

Kaitai结构

注意:如果要更改项目,请不要为此存储库kaitai_struct提供。而是选择要在上方的文件树中修改的组件,然后选择单个组件。

这是一个雨伞存储库,仅包含组件作为子模型,以便更容易地检查整个项目。除非您想修改此读数,否则它不是可以进行编辑的回购。

什么是Kaitai结构?

Kaitai结构是一种声明性语言,用于描述文件或内存中列出的各种二进制数据结构:即二进制文件格式,网络流数据包格式等。

主要的想法是,仅在Kaitai结构语言中描述了一种特定格式,然后可以用ksc编译为一种受支持的编程语言之一,以源为源文件。这些模块将包含一个用于解析器的生成代码,该代码可以从文件 /流中读取所描述的数据结构,并以易于易于理解的API访问它。

它用什么?

您是否曾经发现自己编写重复性,容易出错的和难以删除的代码,该代码从文件 /网络流读取二进制数据结构,并以某种方式代表它们在内存中以更轻松地访问?

Kaitai Struct试图使这项工作更容易 - 您只需要一次描述二进制格式,然后每个人都可以从他们的编程语言中使用它 - 跨语言,跨平台。

Kaitai结构包括越来越多的格式描述集合,可提供格式的子模块存储库。

你能给我一个快速的例子吗?

当然。考虑此简单的.ksy格式描述文件,该文件描述了GIF文件的标题(流行的Web映像格式):

 meta :
  id : gif
  file-extension : gif
  endian : le
seq :
  - id : header
    type : header
  - id : logical_screen
    type : logical_screen
types :
  header :
    seq :
      - id : magic
        contents : ' GIF '
      - id : version
        size : 3
  logical_screen :
    seq :
      - id : image_width
        type : u2
      - id : image_height
        type : u2
      - id : flags
        type : u1
      - id : bg_color_index
        type : u1
      - id : pixel_aspect_ratio
        type : u1

它声明GIF文件通常具有.gif扩展名,并使用Little-endian Integer编码。该文件本身以两个块开始:第一个header ,然后是logical_screen

  • “标头”由3个字节(“ gif”)的“魔术”字符串组成,该字符串识别它是gif文件启动,然后还有3个字节识别格式版本( 87a89a )。
  • “逻辑屏幕描述符”是整数块:
    • image_widthimage_height是2个字节UNSIGNED INTS
    • flagsbg_color_indexpixel_aspect_ratio取1字节UNSIGNED INT

可以将此.ksy文件汇编为Gif.cs / Gif.java / Gif.js / Gif.php / gif.py / gif.rb ,然后立即可以加载.gif文件和访问,例如,它的宽度和高度。

在C#中

 Gif g = Gif . FromFile ( "path/to/some.gif" ) ;
Console . WriteLine ( "width = " + g . LogicalScreen . ImageWidth ) ;
Console . WriteLine ( "height = " + 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 ());

在JavaScript中

请参阅文档中的JavaScript注释,以获取更完整的快速启动指南。

 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

在PHP中

 $ g = Gif:: fromFile ( ' path/to/some.gif ' );
printf ( " width = %d \n" , $ g -> logicalScreen ()-> imageWidth ());
printf ( " height = %d \n" , $ g -> logicalScreen ()-> imageHeight ());

在Python

 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 } "

当然,此示例仅显示Kaitai结构可以做什么的非常有限的子集。请参阅教程和文档以获取更多见解。

支持的语言

Kaitai官方结构编译器现在支持以下语言中的.ksy汇编为源模块:

  • C#
  • 爪哇
  • JavaScript
  • 卢阿
  • 尼姆
  • php
  • Python
  • 红宝石

下载和安装

查看整个Kaitai结构项目的最简单方法是下载已经将所有其他零件以subsodules导入的主要项目存储库。使用:

 git clone --recursive https://git*h**ub.com/kaitai-io/kaitai_struct.git

注意--recursive选项。

另外,可以查看构成Kaitai结构套件的单个子弹。他们是:

  • kaitai_struct _compiler - 将.ksy转换为用目标编程语言编写的解析器源代码的编译器
  • kaitai_struct _tests - 测试和规格,以确保编译器按计划工作
  • 运行时库
    • kaitai_struct _cpp_stl_runtime-对于C ++/STL
    • kaitai_struct _csharp_runtime-对于C#
    • kaitai_struct _java_runtime-对于java
    • kaitai_struct _javascript_runtime-用于JavaScript
    • kaitai_struct _nim_runtime-对于nim
    • kaitai_struct _lua_runtime- for lua
    • kaitai_struct _python_runtime-对于Python
    • kaitai_struct _ruby_runtime-对于Ruby
    • kaitai_struct _swift_runtime-对于迅速
  • kaitai_struct _formats - 广泛使用的格式和二进制结构的库描述为.ksy文件

在您的项目中使用KS

通常,使用项目中KS中描述的格式涉及以下步骤:

  • 描述格式 - 即创建.ksy文件
  • .ksy文件编译到目标语言源文件中,并将该文件包含在项目中
  • 将您的特定语言的KS运行时库添加到您的项目中(不用担心,它很小,并且主要是为了确保生成的代码的可读性)
  • 使用生成的类(ES)来解析您的二进制文件 /流并访问其组件

查看教程和文档以获取更多信息。

许可

  • 编译器 - GPLV3+
  • 运行时库 - MIT或Apache V2(=>您甚至可以将生成的代码包括在专有应用中) - 有关详细信息,请参见各个库
下载源码

通过命令行克隆项目:

git clone https://github.com/kaitai-io/kaitai_struct.git