1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Hex Fiend の Binary Template で長さ不定の文字列を検出する

Posted at

macOS 用のバイナリエディタである Hex Fiend には Binary Templates (beta) 機能があり、バイナリの読み書きを手助けしてくれる。

Template には Tcl でバイナリの構造を以下のように記述していく。

# 1 byte の Header と 8 bytes の Name から成るバイナリの例
uint8 "Header"
str 8 "utf8" "Name"

長さが固定でないフィールドが登場する場合は少し工夫が必要で、例えば長さ不定だが + が後に続くことが分かっている文字列なら以下のように書くことができる。

proc str_ul { follower encoding label } {
    set found 0
    set p 0
    while {![end]} {
        set x [hex 1]
        incr p 1
        if {$x == $follower} {
            set found [expr $p - 1]
            break
        }
    }
    move [expr -1 * $p]
    if {$found} {
        str [expr $p - 1] $encoding $label
    }
}

str_ul 0x2B "utf8" "String"
str eof "utf8" "Remaining"

上記をテンプレートとして保存し、464F4F2B424152 を読んだ結果:
unknown-length-string.png

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?