パッキング(perl pack)

『チャーリーとチョコレート工場』(2005公開)
チョコレートの部屋は子供たちが夢見る素敵な空間です。誰もが皆チョコレートの川は皆泳いでみたいでしょう。私の場合はアメリカナイズなお菓子より、和菓子が好きなのでチョコレートの川とか、ちょっと堪忍。でももし私が苺大福工場の見学に招待されたら、って思うとワクワク(子供の頃なら)します。そして、あずきの川だったらよろこんで……、飛び込みません。川状態のあずきって基本煮えたぎってますし。

『チャーリーとチョコレート工場』では、チョコレートのパッキングシーンがオープニングを飾りますが、今回のコマンドラインはバイナリパッキングがテーマです。

perlで数値1〜10の各1Byte、計10Byteのバイナリを作成したいとき、このような方法がありますね。

~$ seq 10 | perl -ne 'print chr' | od -tx1 -An
 01 02 03 04 05 06 07 08 09 0a
~$ 

とても簡単にバイナリデータにパッキングできます。

では数値が1Byte以外、つまり2Byteや4Byteを扱うにはどうするかというと、chrの代わりに、packを使います。
まずは2Byte単位。

~$ seq 10 | perl -ne 'print pack S,$_' | od -tx1 -An
 01 00 02 00 03 00 04 00 05 00 06 00 07 00 08 00
 09 00 0a 00
~$ 

桁の変わり目はどうでしょうか。251(0xfb)〜260(0x104)を見てみます。

~$ seq 251 260 | perl -ne 'print pack S,$_' | od -tx1 -An
 fb 00 fc 00 fd 00 fe 00 ff 00 00 01 01 01 02 01
 03 01 04 01
~$ 

次に、4Byte単位です。

~$ seq 10 | perl -ne 'print pack L,$_' | od -tx1 -An
 01 00 00 00 02 00 00 00 03 00 00 00 04 00 00 00
 05 00 00 00 06 00 00 00 07 00 00 00 08 00 00 00
 09 00 00 00 0a 00 00 00
~$ seq 251 260 | perl -ne 'print pack L,$_' | od -tx1 -An
 fb 00 00 00 fc 00 00 00 fd 00 00 00 fe 00 00 00
 ff 00 00 00 00 01 00 00 01 01 00 00 02 01 00 00
 03 01 00 00 04 01 00 00
~$ seq 65531 65540 | perl -ne 'print pack L,$_' | od -tx1 -An
 fb ff 00 00 fc ff 00 00 fd ff 00 00 fe ff 00 00
 ff ff 00 00 00 00 01 00 01 00 01 00 02 00 01 00
 03 00 01 00 04 00 01 00
~$ 

pack関数は数をバイナリデータにパッキングします。
書式
pack 型指定子, 文字列

さきほど作った型指定子は、”S”と”L”でした(“”は省略できます)。

C C言語のunsigned char型と同じ1Byte
S C言語のunsigned short型と同じ2Byte
L C言語のlong型と同じ4Byte

1Byteの場合は、chrを使ってましたが、packを使うこともできます。

~$ seq 10 | perl -ne 'print pack C,$_' | od -tx1 -An
 01 02 03 04 05 06 07 08 09 0a
~$ 

Rubyの場合。

~$ seq 10 | ruby -ne 'print [$_.to_i].pack "c"' | od -tx1 -An
 01 02 03 04 05 06 07 08 09 0a
~$ 

以下はpackを使った遊びです。

~$ cat secret.txt | perl -ne 'print pack H32,$_' > a.bin
-rw-r--r-- 1 takk takk 1792  6月 23 21:52 a.bin
~$ 

16Byte毎のバイナリダンプをすることで、メッセージが読めるようになります。

~$ od -An -vtx1 a.bin
 ee ee ee ee 11 11 11 11 11 11 11 ee ee ee ee ee
 ee ee ee ee ee ee ee 11 ee ee ee ee ee ee ee ee
 ee ee ee ee ee ee ee 11 ee ee ee ee ee ee ee ee
 ee ee ee ee ee ee ee 11 ee ee ee ee ee ee ee ee
 ee ee ee ee ee ee ee 11 ee ee ee ee ee ee ee ee
 ee ee ee ee ee ee ee 11 ee ee ee ee ee ee ee ee
 ee ee ee ee ee ee ee 11 ee ee ee ee ee ee ee ee
 ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee
 ee ee ee ee 11 ee ee ee ee ee 11 ee ee ee ee ee
 ee ee ee ee 11 ee ee ee ee ee 11 ee ee ee ee ee
 ee ee ee ee 11 ee ee ee ee ee 11 ee ee ee ee ee
 ee ee ee ee 11 11 11 11 11 11 11 ee ee ee ee ee
 ee ee ee ee 11 ee ee ee ee ee 11 ee ee ee ee ee
 ee ee ee ee 11 ee ee ee ee ee 11 ee ee ee ee ee
 ee ee ee ee 11 ee ee ee ee ee 11 ee ee ee ee ee
 ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee
 ee ee ee ee ee ee ee 11 ee ee ee ee ee ee ee ee
 ee ee ee ee ee ee 11 ee 11 ee ee ee ee ee ee ee
 ee ee ee ee ee 11 ee ee ee 11 ee ee ee ee ee ee
 ee ee ee ee 11 ee ee ee ee ee 11 ee ee ee ee ee
 ee ee ee ee 11 11 11 11 11 11 11 ee ee ee ee ee
 ee ee ee ee 11 ee ee ee ee ee 11 ee ee ee ee ee
 ee ee ee ee 11 ee ee ee ee ee 11 ee ee ee ee ee
 ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee
 ee ee ee ee 11 ee ee ee ee ee 11 ee ee ee ee ee
 ee ee ee ee 11 11 ee ee ee ee 11 ee ee ee ee ee
 ee ee ee ee 11 ee 11 ee ee ee 11 ee ee ee ee ee
 ee ee ee ee 11 ee ee 11 ee ee 11 ee ee ee ee ee
 ee ee ee ee 11 ee ee ee 11 ee 11 ee ee ee ee ee
 ee ee ee ee 11 ee ee ee ee 11 11 ee ee ee ee ee
 ee ee ee ee 11 ee ee ee ee ee 11 ee ee ee ee ee
 ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee
 ee ee ee ee 11 ee ee ee ee 11 ee ee ee ee ee ee
 ee ee ee ee 11 ee ee ee 11 ee ee ee ee ee ee ee
 ee ee ee ee 11 ee ee 11 ee ee ee ee ee ee ee ee
 ee ee ee ee 11 11 11 ee ee ee ee ee ee ee ee ee
 ee ee ee ee 11 ee ee 11 ee ee ee ee ee ee ee ee
 ee ee ee ee 11 ee ee ee 11 ee ee ee ee ee ee ee
 ee ee ee ee 11 ee ee ee ee 11 ee ee ee ee ee ee
 ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee
 ee ee ee ee 11 ee ee ee ee ee 11 ee ee ee ee ee
 ee ee ee ee 11 ee ee ee ee ee 11 ee ee ee ee ee
 ee ee ee ee 11 ee ee ee ee ee 11 ee ee ee ee ee
 ee ee ee ee 11 ee ee ee ee ee 11 ee ee ee ee ee
 ee ee ee ee 11 ee ee ee ee ee 11 ee ee ee ee ee
 ee ee ee ee 11 ee ee ee ee ee 11 ee ee ee ee ee
 ee ee ee ee ee 11 11 11 11 11 ee ee ee ee ee ee
 ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee
 ee ee ee ee 11 ee ee ee ee ee ee ee ee ee ee ee
 ee ee ee ee 11 ee ee ee ee 11 ee ee ee ee ee ee
 ee ee ee ee 11 ee ee ee ee 11 ee ee ee ee ee ee
 ee ee ee ee 11 11 11 11 11 11 11 ee ee ee ee ee
 ee ee ee ee ee ee ee ee ee 11 ee ee ee ee ee ee
 ee ee ee ee ee ee ee ee ee 11 ee ee ee ee ee ee
 ee ee ee ee ee ee ee ee ee 11 ee ee ee ee ee ee
 ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee
 ee ee ee ee 11 11 11 11 11 11 ee ee ee ee ee ee
 ee ee ee ee 11 ee ee ee ee ee 11 ee ee ee ee ee
 ee ee ee ee 11 ee ee ee ee ee 11 ee ee ee ee ee
 ee ee ee ee 11 11 11 11 11 11 ee ee ee ee ee ee
 ee ee ee ee 11 ee ee ee 11 ee ee ee ee ee ee ee
 ee ee ee ee 11 ee ee ee ee 11 ee ee ee ee ee ee
 ee ee ee ee 11 ee ee ee ee ee 11 ee ee ee ee ee
 ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee
 ee ee ee ee 11 11 11 11 11 11 11 ee ee ee ee ee
 ee ee ee ee 11 ee ee ee ee ee ee ee ee ee ee ee
 ee ee ee ee 11 ee ee ee ee ee ee ee ee ee ee ee
 ee ee ee ee 11 11 11 11 11 ee ee ee ee ee ee ee
 ee ee ee ee 11 ee ee ee ee ee ee ee ee ee ee ee
 ee ee ee ee 11 ee ee ee ee ee ee ee ee ee ee ee
 ee ee ee ee 11 11 11 11 11 11 11 ee ee ee ee ee
 ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee
 ee ee ee ee ee ee ee 11 ee ee ee ee ee ee ee ee
 ee ee ee ee ee ee 11 ee 11 ee ee ee ee ee ee ee
 ee ee ee ee ee 11 ee ee ee 11 ee ee ee ee ee ee
 ee ee ee ee 11 ee ee ee ee ee 11 ee ee ee ee ee
 ee ee ee ee 11 11 11 11 11 11 11 ee ee ee ee ee
 ee ee ee ee 11 ee ee ee ee ee 11 ee ee ee ee ee
 ee ee ee ee 11 ee ee ee ee ee 11 ee ee ee ee ee
 ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee
 ee ee ee ee 11 11 11 11 11 11 ee ee ee ee ee ee
 ee ee ee ee 11 ee ee ee ee ee 11 ee ee ee ee ee
 ee ee ee ee 11 ee ee ee ee ee 11 ee ee ee ee ee
 ee ee ee ee 11 ee ee ee ee ee 11 ee ee ee ee ee
 ee ee ee ee 11 ee ee ee ee ee 11 ee ee ee ee ee
 ee ee ee ee 11 ee ee ee ee ee 11 ee ee ee ee ee
 ee ee ee ee 11 11 11 11 11 11 ee ee ee ee ee ee
 ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee
 ee ee ee ee ee ee 11 11 11 ee ee ee ee ee ee ee
 ee ee ee ee ee ee ee 11 ee ee ee ee ee ee ee ee
 ee ee ee ee ee ee ee 11 ee ee ee ee ee ee ee ee
 ee ee ee ee ee ee ee 11 ee ee ee ee ee ee ee ee
 ee ee ee ee ee ee ee 11 ee ee ee ee ee ee ee ee
 ee ee ee ee ee ee ee 11 ee ee ee ee ee ee ee ee
 ee ee ee ee ee ee 11 11 11 ee ee ee ee ee ee ee
 ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee
 ee ee ee ee 11 ee ee ee ee ee 11 ee ee ee ee ee
 ee ee ee ee 11 11 ee ee ee ee 11 ee ee ee ee ee
 ee ee ee ee 11 ee 11 ee ee ee 11 ee ee ee ee ee
 ee ee ee ee 11 ee ee 11 ee ee 11 ee ee ee ee ee
 ee ee ee ee 11 ee ee ee 11 ee 11 ee ee ee ee ee
 ee ee ee ee 11 ee ee ee ee 11 11 ee ee ee ee ee
 ee ee ee ee 11 ee ee ee ee ee 11 ee ee ee ee ee
 ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee
 ee ee ee ee ee 11 11 11 11 11 ee ee ee ee ee ee
 ee ee ee ee 11 ee ee ee ee ee 11 ee ee ee ee ee
 ee ee ee ee 11 ee ee ee ee ee ee ee ee ee ee ee
 ee ee ee ee 11 ee ee 11 11 11 11 ee ee ee ee ee
 ee ee ee ee 11 ee ee ee ee ee 11 ee ee ee ee ee
 ee ee ee ee 11 ee ee ee ee ee 11 ee ee ee ee ee
 ee ee ee ee ee 11 11 11 11 11 ee ee ee ee ee ee
 ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee ee
~$ 

上記メッセージですが、このように作れます。

~$ for i in T H A N K U 4 R E A D I N G;do
 banner $i | sed 's/\(.\)/\1\1/g'| tr '\n' '\0' |
 xargs -0 printf "        %-24s\n" | tr ' #' 'e1';done > secret.txt

Leave a Reply

Your email address will not be published. Required fields are marked *

CAPTCHA