目次: Linux
GNU tarのxオプションは圧縮されたtarでも正しく展開してくれます。圧縮方法はgzip, bzip, xz, ... など複数存在しますが、どのように判定しているんでしょう?まさか拡張子でしょうか?
こういうときにOSSは便利です。ソースコードを見ればわかるはず。バージョンはどれでも仕組み自体は同じだと思いますが、とりあえず最新版リリースGNU tar 1.34にしました。gitリポジトリへのリンクも貼っておきます。
// src/buffer.c
/* Compression detection */
enum compress_type {
ct_none, /* Unknown compression type */
ct_tar, /* Plain tar file */
ct_compress,
ct_gzip,
ct_bzip2,
ct_lzip,
ct_lzma,
ct_lzop,
ct_xz,
ct_zstd
};
static enum compress_type archive_compression_type = ct_none;
struct zip_magic
{
enum compress_type type;
size_t length;
char const *magic;
};
struct zip_program
{
enum compress_type type;
char const *program;
char const *option;
};
static struct zip_magic const magic[] = {
{ ct_none, 0, 0 },
{ ct_tar, 0, 0 },
{ ct_compress, 2, "\037\235" },
{ ct_gzip, 2, "\037\213" },
{ ct_bzip2, 3, "BZh" },
{ ct_lzip, 4, "LZIP" },
{ ct_lzma, 6, "\xFFLZMA" },
{ ct_lzop, 4, "\211LZO" },
{ ct_xz, 6, "\xFD" "7zXZ" },
{ ct_zstd, 4, "\x28\xB5\x2F\xFD" },
};
各圧縮方式に固有のマジックヘッダを定義している部分がありました。さすがに拡張子だけ見る雑な判定ではなさそうです。そりゃそうか。
// src/buffer.c
/* Check if the file ARCHIVE is a compressed archive. */
static enum compress_type
check_compressed_archive (bool *pshort)
{
struct zip_magic const *p;
bool sfr;
bool temp;
if (!pshort)
pshort = &temp;
/* Prepare global data needed for find_next_block: */
record_end = record_start; /* set up for 1st record = # 0 */
sfr = read_full_records;
read_full_records = true; /* Suppress fatal error on reading a partial
record */
*pshort = find_next_block () == 0;
/* Restore global values */
read_full_records = sfr;
if (record_start != record_end /* no files smaller than BLOCKSIZE */
&& (strcmp (record_start->header.magic, TMAGIC) == 0
|| strcmp (record_start->buffer + offsetof (struct posix_header,
magic),
OLDGNU_MAGIC) == 0)
&& tar_checksum (record_start, true) == HEADER_SUCCESS)
/* Probably a valid header */
return ct_tar;
for (p = magic + 2; p < magic + NMAGIC; p++) //★★magicはさきほど示したコードで定義していた配列★★
if (memcmp (record_start->buffer, p->magic, p->length) == 0) //★★ここで比較★★
return p->type;
return ct_none;
}
各圧縮方式に固有のバイナリ列が存在するか?をmemcmp() で見ています。結構シンプルな仕組みですね。
この記事にコメントする
Might and Magic Book One TAS US版をさらに6秒くらい短縮(7:32.39 → 7:25.80)しました(ニコニコ動画へのリンク)。
TASVideosに再投稿するなら何かもう1つくらいは改善してから送りたいですが、良いアイデアが思いつかないです……。
この記事にコメントする
目次: マンガ紹介
面白かった漫画の紹介です。知名度はあまり気にせず紹介します。
その他のマンガの話。
目次: 一覧の一覧
この記事にコメントする
| < | 2022 | > | ||||
| << | < | 07 | > | >> | ||
| 日 | 月 | 火 | 水 | 木 | 金 | 土 |
| - | - | - | - | - | 1 | 2 |
| 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| 10 | 11 | 12 | 13 | 14 | 15 | 16 |
| 17 | 18 | 19 | 20 | 21 | 22 | 23 |
| 24 | 25 | 26 | 27 | 28 | 29 | 30 |
| 31 | - | - | - | - | - | - |
25年10月15日
25年10月18日
22年5月5日
25年10月19日
23年4月11日
06年4月22日
25年10月17日
25年10月6日
25年10月13日
20年10月23日
25年10月12日
20年8月29日
19年1月13日
18年10月13日
18年9月3日
18年8月20日
18年7月23日
18年7月22日
18年10月14日
18年11月10日
wiki
Linux JM
Java API
2002年
2003年
2004年
2005年
2006年
2007年
2008年
2009年
2010年
2011年
2012年
2013年
2014年
2015年
2016年
2017年
2018年
2019年
2020年
2021年
2022年
2023年
2024年
2025年
過去日記について
アクセス統計
サーバ一覧
サイトの情報合計:
本日: