コグノスケ


link 未来から過去へ表示  link 過去から未来へ表示(*)

link もっと前
2022年7月18日 >>> 2022年7月18日
link もっと後

2022年7月18日

GNU tarのアーカイブ圧縮方式の判定

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() で見ています。結構シンプルな仕組みですね。

編集者:すずき(2022/08/21 03:05)

コメント一覧

  • コメントはありません。
open/close この記事にコメントする



link もっと前
2022年7月18日 >>> 2022年7月18日
link もっと後

管理用メニュー

link 記事を新規作成

<2022>
<<<07>>>
-----12
3456789
10111213141516
17181920212223
24252627282930
31------

最近のコメント5件

  • link 24年6月17日
    すずきさん (06/23 00:12)
    「ありがとうございます。バルコニーではない...」
  • link 24年6月17日
    hdkさん (06/22 22:08)
    「GPSの最初の同期を取る時は見晴らしのい...」
  • link 24年5月16日
    すずきさん (05/21 11:41)
    「あー、確かにdpkg-reconfigu...」
  • link 24年5月16日
    hdkさん (05/21 08:55)
    「システム全体のlocale設定はDebi...」
  • link 24年5月17日
    すずきさん (05/20 13:16)
    「そうですねえ、普通はStandardなの...」

最近の記事3件

  • link 22年3月18日
    すずき (06/22 17:32)
    「[射的 - まとめリンク] 目次: 射的一覧が欲しくなったので作りました。ガスガン その1ガスガン その2ガスガンが増えました...」
  • link 23年11月25日
    すずき (06/22 17:31)
    「[JTSA Limited大会参加2023] 目次: 射的JTSA Limitedの大会に参加しました。いつも使っているエアガ...」
  • link 24年5月26日
    すずき (06/22 17:16)
    「[JTSA Unlimited大会参加2024] 目次: 射的JTSA Unlimitedの大会に参加しました。去年は選手登録...」
link もっとみる

こんてんつ

open/close wiki
open/close Linux JM
open/close Java API

過去の日記

open/close 2002年
open/close 2003年
open/close 2004年
open/close 2005年
open/close 2006年
open/close 2007年
open/close 2008年
open/close 2009年
open/close 2010年
open/close 2011年
open/close 2012年
open/close 2013年
open/close 2014年
open/close 2015年
open/close 2016年
open/close 2017年
open/close 2018年
open/close 2019年
open/close 2020年
open/close 2021年
open/close 2022年
open/close 2023年
open/close 2024年
open/close 過去日記について

その他の情報

open/close アクセス統計
open/close サーバ一覧
open/close サイトの情報

合計:  counter total
本日:  counter today

link About www2.katsuster.net
RDFファイル RSS 1.0

最終更新: 06/23 00:12