sjhill

Squashfs - Function to unsquashfs a single file.

Oct 3rd, 2012
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 36.98 KB | None | 0 0
  1. /*
  2.  * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
  3.  * Phillip Lougher <[email protected]>
  4.  *
  5.  * Copyright (c) 2010 LG Electronics
  6.  * Chan Jeong <[email protected]>
  7.  *
  8.  * Copyright (c) 2012 Reality Diluted, LLC
  9.  * Steven J. Hill <[email protected]>
  10.  *
  11.  * This program is free software; you can redistribute it and/or
  12.  * modify it under the terms of the GNU General Public License
  13.  * as published by the Free Software Foundation; either version 2,
  14.  * or (at your option) any later version.
  15.  *
  16.  * This program is distributed in the hope that it will be useful,
  17.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.  * GNU General Public License for more details.
  20.  *
  21.  * You should have received a copy of the GNU General Public License
  22.  * along with this program; if not, write to the Free Software
  23.  * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  24.  *
  25.  * unsquashfs-single-file.c
  26.  *
  27.  * Unsquash a squashfs filesystem with minimal support. This code is
  28.  * for little endian only, ignores uid/gid, ignores xattr, only works
  29.  * for squashfs version >4.0, only supports zlib and lzo compression,
  30.  * is only for Linux, is not multi-threaded and does not support any
  31.  * regular expressions. You have been warned.
  32.  *    -Steve
  33.  *
  34.  * To build as a stand-alone test application:
  35.  *
  36.  *    gcc -O2 -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_GNU_SOURCE \
  37.  *       -DTESTING -Wall unsquashfs-single-file.c -lz -llzo2 -o unsquashfs
  38.  *
  39.  * To build as a part of a library or application compile this file
  40.  * and link with the following CFLAGS and LDFLAGS:
  41.  *
  42.  *    CFLAGS += -O2 -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_GNU_SOURCE
  43.  *    LDFLAGS += -lz -llzo2
  44.  */
  45.  
  46. #define TRUE 1
  47. #define FALSE 0
  48. #include <stdio.h>
  49. #include <stdlib.h>
  50. #include <unistd.h>
  51. #include <fcntl.h>
  52. #include <errno.h>
  53. #include <string.h>
  54. #include <fnmatch.h>
  55. #include <sys/types.h>
  56. #include <sys/stat.h>
  57. #include <sys/mman.h>
  58.  
  59. #include <zlib.h>
  60. #include <lzo/lzoconf.h>
  61. #include <lzo/lzo1x.h>
  62.  
  63. #define SQUASHFS_CACHED_FRAGMENTS   CONFIG_SQUASHFS_FRAGMENT_CACHE_SIZE
  64. #define SQUASHFS_MAGIC          0x73717368
  65. #define SQUASHFS_START          0
  66.  
  67. /* size of metadata (inode and directory) blocks */
  68. #define SQUASHFS_METADATA_SIZE      8192
  69.  
  70. /* default size of data blocks */
  71. #define SQUASHFS_FILE_SIZE      131072
  72. #define SQUASHFS_FILE_MAX_SIZE      1048576
  73.  
  74. /* Max length of filename (not 255) */
  75. #define SQUASHFS_NAME_LEN       256
  76.  
  77. #define SQUASHFS_INVALID_FRAG       ((unsigned int) 0xffffffff)
  78.  
  79. /* Max number of types and file types */
  80. #define SQUASHFS_DIR_TYPE       1
  81. #define SQUASHFS_FILE_TYPE      2
  82. #define SQUASHFS_LDIR_TYPE      8
  83.  
  84. /* Flag whether block is compressed or uncompressed, bit is set if block is
  85.  * uncompressed */
  86. #define SQUASHFS_COMPRESSED_BIT     (1 << 15)
  87.  
  88. #define SQUASHFS_COMPRESSED_SIZE(B) (((B) & ~SQUASHFS_COMPRESSED_BIT) ? \
  89.         (B) & ~SQUASHFS_COMPRESSED_BIT :  SQUASHFS_COMPRESSED_BIT)
  90.  
  91. #define SQUASHFS_COMPRESSED(B)      (!((B) & SQUASHFS_COMPRESSED_BIT))
  92.  
  93. #define SQUASHFS_COMPRESSED_BIT_BLOCK       (1 << 24)
  94.  
  95. #define SQUASHFS_COMPRESSED_SIZE_BLOCK(B)   ((B) & \
  96.     ~SQUASHFS_COMPRESSED_BIT_BLOCK)
  97.  
  98. #define SQUASHFS_COMPRESSED_BLOCK(B)    (!((B) & SQUASHFS_COMPRESSED_BIT_BLOCK))
  99.  
  100. /*
  101.  * Inode number ops.  Inodes consist of a compressed block number, and an
  102.  * uncompressed  offset within that block
  103.  */
  104. #define SQUASHFS_INODE_BLK(a)       ((unsigned int) ((a) >> 16))
  105.  
  106. #define SQUASHFS_INODE_OFFSET(a)    ((unsigned int) ((a) & 0xffff))
  107.  
  108. /* fragment and fragment table defines */
  109. #define SQUASHFS_FRAGMENT_BYTES(A)  ((A) * sizeof(struct squashfs_fragment_entry))
  110.  
  111. #define SQUASHFS_FRAGMENT_INDEX(A)  (SQUASHFS_FRAGMENT_BYTES(A) / \
  112.                     SQUASHFS_METADATA_SIZE)
  113.  
  114. #define SQUASHFS_FRAGMENT_INDEX_OFFSET(A)   (SQUASHFS_FRAGMENT_BYTES(A) % \
  115.                         SQUASHFS_METADATA_SIZE)
  116.  
  117. #define SQUASHFS_FRAGMENT_INDEXES(A)    ((SQUASHFS_FRAGMENT_BYTES(A) + \
  118.                     SQUASHFS_METADATA_SIZE - 1) / \
  119.                     SQUASHFS_METADATA_SIZE)
  120.  
  121. #define SQUASHFS_FRAGMENT_INDEX_BYTES(A)    (SQUASHFS_FRAGMENT_INDEXES(A) *\
  122.                         sizeof(long long))
  123.  
  124. /*
  125.  * definitions for structures on disk
  126.  */
  127.  
  128. typedef long long       squashfs_block;
  129. typedef long long       squashfs_inode;
  130.  
  131. #define ZLIB_COMPRESSION    1
  132. #define LZO_COMPRESSION     3
  133.  
  134. struct squashfs_super_block {
  135.     unsigned int        s_magic;
  136.     unsigned int        inodes;
  137.     unsigned int        mkfs_time /* time of filesystem creation */;
  138.     unsigned int        block_size;
  139.     unsigned int        fragments;
  140.     unsigned short      compression;
  141.     unsigned short      block_log;
  142.     unsigned short      flags;
  143.     unsigned short      res0;
  144.     unsigned short      s_major;
  145.     unsigned short      s_minor;
  146.     squashfs_inode      root_inode;
  147.     long long       bytes_used;
  148.     long long       res1;
  149.     long long       res2;
  150.     long long       inode_table_start;
  151.     long long       directory_table_start;
  152.     long long       fragment_table_start;
  153.     long long       res3;
  154. };
  155.  
  156. struct squashfs_dir_index {
  157.     unsigned int        index;
  158.     unsigned int        start_block;
  159.     unsigned int        size;
  160.     unsigned char       name[0];
  161. };
  162.  
  163. struct squashfs_base_inode_header {
  164.     unsigned short      inode_type;
  165.     unsigned short      mode;
  166.     unsigned short      uid;
  167.     unsigned short      guid;
  168.     unsigned int        mtime;
  169.     unsigned int        inode_number;
  170. };
  171.  
  172. struct squashfs_reg_inode_header {
  173.     unsigned short      inode_type;
  174.     unsigned short      mode;
  175.     unsigned short      uid;
  176.     unsigned short      guid;
  177.     unsigned int        mtime;
  178.     unsigned int        inode_number;
  179.     unsigned int        start_block;
  180.     unsigned int        fragment;
  181.     unsigned int        offset;
  182.     unsigned int        file_size;
  183.     unsigned int        block_list[0];
  184. };
  185.  
  186. struct squashfs_dir_inode_header {
  187.     unsigned short      inode_type;
  188.     unsigned short      mode;
  189.     unsigned short      uid;
  190.     unsigned short      guid;
  191.     unsigned int        mtime;
  192.     unsigned int        inode_number;
  193.     unsigned int        start_block;
  194.     unsigned int        nlink;
  195.     unsigned short      file_size;
  196.     unsigned short      offset;
  197.     unsigned int        parent_inode;
  198. };
  199.  
  200. struct squashfs_ldir_inode_header {
  201.     unsigned short      inode_type;
  202.     unsigned short      mode;
  203.     unsigned short      uid;
  204.     unsigned short      guid;
  205.     unsigned int        mtime;
  206.     unsigned int        inode_number;
  207.     unsigned int        nlink;
  208.     unsigned int        file_size;
  209.     unsigned int        start_block;
  210.     unsigned int        parent_inode;
  211.     unsigned short      i_count;
  212.     unsigned short      offset;
  213.     unsigned int        res0;
  214.     struct squashfs_dir_index   index[0];
  215. };
  216.  
  217. union squashfs_inode_header {
  218.     struct squashfs_base_inode_header   base;
  219.     struct squashfs_reg_inode_header    reg;
  220.     struct squashfs_dir_inode_header    dir;
  221.     struct squashfs_ldir_inode_header   ldir;
  222. };
  223.    
  224. struct squashfs_dir_entry {
  225.     unsigned short      offset;
  226.     short           inode_number;
  227.     unsigned short      type;
  228.     unsigned short      size;
  229.     char            name[0];
  230. };
  231.  
  232. struct squashfs_dir_header {
  233.     unsigned int        count;
  234.     unsigned int        start_block;
  235.     unsigned int        inode_number;
  236. };
  237.  
  238. struct squashfs_fragment_entry {
  239.     long long       start_block;
  240.     unsigned int        size;
  241.     unsigned int        unused;
  242. };
  243.  
  244. #ifdef SQUASHFS_TRACE
  245. #define TRACE(s, args...) \
  246.         do { \
  247.             printf("\n"); \
  248.             printf("unsquashfs: "s, ## args); \
  249.         } while(0)
  250. #else
  251. #define TRACE(s, args...)
  252. #endif
  253.  
  254. #define ERROR(s, args...) \
  255.         do { \
  256.             fprintf(stderr, "\n"); \
  257.             fprintf(stderr, s, ## args); \
  258.         } while(0)
  259.  
  260. #define EXIT_UNSQUASH(s, args...) \
  261.         do { \
  262.             fprintf(stderr, "FATAL ERROR aborting: "s, ## args); \
  263.             exit(1); \
  264.         } while(0)
  265.  
  266. #define CALCULATE_HASH(start)   (start & 0xffff)
  267.  
  268. struct hash_table_entry {
  269.     long long   start;
  270.     int     bytes;
  271.     struct hash_table_entry *next;
  272. };
  273.  
  274. struct inode {
  275.     int blocks;
  276.     char *block_ptr;
  277.     long long data;
  278.     int fragment;
  279.     int frag_bytes;
  280.     int inode_number;
  281.     int mode;
  282.     int offset;
  283.     long long start;
  284.     time_t time;
  285.     int type;
  286.     char sparse;
  287. };
  288.  
  289. /* Cache status struct */
  290. struct cache {
  291.     int buffer_size;
  292.     struct cache_entry *hash_table[65536];
  293. };
  294.  
  295. /* struct describing a cache entry */
  296. struct cache_entry {
  297.     struct cache *cache;
  298.     long long block;
  299.     int size;
  300.     char *data;
  301. };
  302.  
  303. /* default size of fragment buffer in Mbytes */
  304. #define FRAGMENT_BUFFER_DEFAULT 256
  305. /* default size of data buffer in Mbytes */
  306. #define DATA_BUFFER_DEFAULT 256
  307.  
  308. #define DIR_ENT_SIZE    16
  309.  
  310. struct dir_ent  {
  311.     char        name[SQUASHFS_NAME_LEN + 1];
  312.     unsigned int    start_block;
  313.     unsigned int    offset;
  314.     unsigned int    type;
  315. };
  316.  
  317. struct dir {
  318.     int     dir_count;
  319.     int         cur_entry;
  320.     unsigned int    mode;
  321.     unsigned int    mtime;
  322.     struct dir_ent  *dirs;
  323. };
  324.  
  325. struct file_entry {
  326.     int offset;
  327.     int size;
  328.     struct cache_entry *buffer;
  329. };
  330.  
  331. struct squashfs_file {
  332.     int fd;
  333.     int blocks;
  334.     long long file_size;
  335.     int mode;
  336.     time_t time;
  337.     char *pathname;
  338.     char sparse;
  339. };
  340.  
  341. struct path_entry {
  342.     char *name;
  343.     struct pathname *paths;
  344. };
  345.  
  346. struct pathname {
  347.     int names;
  348.     struct path_entry *name;
  349. };
  350.  
  351. struct pathnames {
  352.     int count;
  353.     struct pathname *path[0];
  354. };
  355. #define PATHS_ALLOC_SIZE 10
  356.  
  357.  
  358. struct squashfs_super_block sBlk;
  359. struct squashfs_fragment_entry *fragment_table;
  360.  
  361. int bytes = 0, file_count = 0, dir_count = 0;
  362. struct cache *fragment_cache, *data_cache;
  363. char *inode_table = NULL, *directory_table = NULL;
  364. struct hash_table_entry *inode_table_hash[65536], *directory_table_hash[65536];
  365. int fd;
  366. unsigned int block_size;
  367. unsigned int block_log;
  368. char **created_inode;
  369. char *zero_data = NULL;
  370. unsigned int cur_blocks = 0;
  371.  
  372. int lookup_type[] = {
  373.     0,
  374.     S_IFDIR,
  375.     S_IFREG,
  376.     S_IFLNK,
  377.     S_IFBLK,
  378.     S_IFCHR,
  379.     S_IFIFO,
  380.     S_IFSOCK,
  381.     S_IFDIR,
  382.     S_IFREG,
  383.     S_IFLNK,
  384.     S_IFBLK,
  385.     S_IFCHR,
  386.     S_IFIFO,
  387.     S_IFSOCK
  388. };
  389.  
  390. #ifndef TESTING
  391. /* buffer to return to caller*/
  392. static char *private_buffer;
  393. static int private_count;
  394. #endif
  395.  
  396.  
  397. /* forward delcarations */
  398. int read_fs_bytes(int fd, long long byte, int bytes, void *buff);
  399. int read_block(int fd, long long start, long long *next, void *block);
  400. int lookup_entry(struct hash_table_entry *hash_table[], long long start);
  401. void *reader(void *arg);
  402. void *writer(void *arg);
  403. void *deflator(void *arg);
  404.  
  405.  
  406. void read_block_list(unsigned int *block_list, char *block_ptr, int blocks)
  407. {
  408.     TRACE("read_block_list: blocks %d\n", blocks);
  409.  
  410.     memcpy(block_list, block_ptr, blocks * sizeof(unsigned int));
  411. }
  412.  
  413. int read_fragment_table()
  414. {
  415.     int res, i, indexes = SQUASHFS_FRAGMENT_INDEXES(sBlk.fragments);
  416.     long long fragment_table_index[indexes];
  417.  
  418.     TRACE("read_fragment_table: %d fragments, reading %d fragment indexes "
  419.         "from 0x%llx\n", sBlk.fragments, indexes,
  420.         sBlk.fragment_table_start);
  421.  
  422.     if(sBlk.fragments == 0)
  423.         return TRUE;
  424.  
  425.     fragment_table = malloc(sBlk.fragments *
  426.         sizeof(struct squashfs_fragment_entry));
  427.     if(fragment_table == NULL)
  428.         EXIT_UNSQUASH("read_fragment_table: failed to allocate "
  429.             "fragment table\n");
  430.  
  431.     res = read_fs_bytes(fd, sBlk.fragment_table_start,
  432.         SQUASHFS_FRAGMENT_INDEX_BYTES(sBlk.fragments),
  433.         fragment_table_index);
  434.     if(res == FALSE) {
  435.         ERROR("read_fragment_table: failed to read fragment table "
  436.             "index\n");
  437.         return FALSE;
  438.     }
  439.  
  440.     for(i = 0; i < indexes; i++) {
  441.         int length = read_block(fd, fragment_table_index[i], NULL,
  442.             ((char *) fragment_table) + (i *
  443.             SQUASHFS_METADATA_SIZE));
  444.         TRACE("Read fragment table block %d, from 0x%llx, length %d\n",
  445.             i, fragment_table_index[i], length);
  446.         if(length == FALSE) {
  447.             ERROR("read_fragment_table: failed to read fragment "
  448.                 "table index\n");
  449.             return FALSE;
  450.         }
  451.     }
  452.  
  453.     return TRUE;
  454. }
  455.  
  456. void read_fragment(unsigned int fragment, long long *start_block, int *size)
  457. {
  458.     TRACE("read_fragment: reading fragment %d\n", fragment);
  459.  
  460.     struct squashfs_fragment_entry *fragment_entry;
  461.  
  462.     fragment_entry = &fragment_table[fragment];
  463.     *start_block = fragment_entry->start_block;
  464.     *size = fragment_entry->size;
  465. }
  466.  
  467. struct inode *read_inode(unsigned int start_block, unsigned int offset)
  468. {
  469.     static union squashfs_inode_header header;
  470.     long long start = sBlk.inode_table_start + start_block;
  471.     int bytes = lookup_entry(inode_table_hash, start);
  472.     char *block_ptr = inode_table + bytes + offset;
  473.     static struct inode i;
  474.  
  475.     TRACE("read_inode: reading inode [%d:%d]\n", start_block,  offset);
  476.  
  477.     if(bytes == -1)
  478.         EXIT_UNSQUASH("read_inode: inode table block %lld not found\n",
  479.             start);        
  480.  
  481.     memcpy(&header.base, block_ptr, sizeof(*(&header.base)));
  482.  
  483.     i.mode = lookup_type[header.base.inode_type] | header.base.mode;
  484.     i.type = header.base.inode_type;
  485.     i.time = header.base.mtime;
  486.     i.inode_number = header.base.inode_number;
  487.  
  488.     switch(header.base.inode_type) {
  489.         case SQUASHFS_DIR_TYPE: {
  490.             struct squashfs_dir_inode_header *inode = &header.dir;
  491.  
  492.             memcpy(inode, block_ptr, sizeof(*(inode)));
  493.  
  494.             i.data = inode->file_size;
  495.             i.offset = inode->offset;
  496.             i.start = inode->start_block;
  497.             break;
  498.         }
  499.         case SQUASHFS_FILE_TYPE: {
  500.             struct squashfs_reg_inode_header *inode = &header.reg;
  501.  
  502.             memcpy(inode, block_ptr, sizeof(*(inode)));
  503.  
  504.             i.data = inode->file_size;
  505.             i.frag_bytes = inode->fragment == SQUASHFS_INVALID_FRAG
  506.                 ?  0 : inode->file_size % sBlk.block_size;
  507.             i.fragment = inode->fragment;
  508.             i.offset = inode->offset;
  509.             i.blocks = inode->fragment == SQUASHFS_INVALID_FRAG ?
  510.                 (i.data + sBlk.block_size - 1) >>
  511.                 sBlk.block_log :
  512.                 i.data >> sBlk.block_log;
  513.             i.start = inode->start_block;
  514.             i.sparse = 0;
  515.             i.block_ptr = block_ptr + sizeof(*inode);
  516.             break;
  517.         }  
  518.                 case SQUASHFS_LDIR_TYPE: {
  519.                         struct squashfs_ldir_inode_header *inode = &header.ldir;
  520.  
  521.             memcpy(inode, block_ptr, sizeof(*(inode)));
  522.  
  523.                         i.data = inode->file_size;
  524.                         i.offset = inode->offset;
  525.                         i.start = inode->start_block;
  526.                         break;
  527.                 }
  528.         default:
  529.             TRACE("read_inode: skipping inode type %d\n", header.base.inode_type);
  530.             return NULL;
  531.     }
  532.     return &i;
  533. }
  534.  
  535. struct dir *squashfs_opendir(unsigned int block_start, unsigned int offset,
  536.     struct inode **i)
  537. {
  538.     struct squashfs_dir_header dirh;
  539.     char buffer[sizeof(struct squashfs_dir_entry) + SQUASHFS_NAME_LEN + 1]
  540.         __attribute__((aligned));
  541.     struct squashfs_dir_entry *dire = (struct squashfs_dir_entry *) buffer;
  542.     long long start;
  543.     int bytes;
  544.     int dir_count, size;
  545.     struct dir_ent *new_dir;
  546.     struct dir *dir;
  547.  
  548.     TRACE("squashfs_opendir: inode start block %d, offset %d\n",
  549.         block_start, offset);
  550.  
  551.     *i = read_inode(block_start, offset);
  552.     start = sBlk.directory_table_start + (*i)->start;
  553.     bytes = lookup_entry(directory_table_hash, start);
  554.  
  555.     if(bytes == -1)
  556.         EXIT_UNSQUASH("squashfs_opendir: directory block %d not "
  557.             "found!\n", block_start);
  558.  
  559.     bytes += (*i)->offset;
  560.     size = (*i)->data + bytes - 3;
  561.  
  562.     dir = malloc(sizeof(struct dir));
  563.     if(dir == NULL)
  564.         EXIT_UNSQUASH("squashfs_opendir: malloc failed!\n");
  565.  
  566.     dir->dir_count = 0;
  567.     dir->cur_entry = 0;
  568.     dir->mode = (*i)->mode;
  569.     dir->mtime = (*i)->time;
  570.     dir->dirs = NULL;
  571.  
  572.     while(bytes < size) {          
  573.         memcpy(&dirh, directory_table + bytes, sizeof(*(&dirh)));
  574.    
  575.         dir_count = dirh.count + 1;
  576.         TRACE("squashfs_opendir: Read directory header @ byte position "
  577.             "%d, %d directory entries\n", bytes, dir_count);
  578.         bytes += sizeof(dirh);
  579.  
  580.         while(dir_count--) {
  581.             memcpy(dire, directory_table + bytes, sizeof(*(dire)));
  582.  
  583.             bytes += sizeof(*dire);
  584.  
  585.             memcpy(dire->name, directory_table + bytes,
  586.                 dire->size + 1);
  587.             dire->name[dire->size + 1] = '\0';
  588.             TRACE("squashfs_opendir: directory entry %s, inode "
  589.                 "%d:%d, type %d\n", dire->name,
  590.                 dirh.start_block, dire->offset, dire->type);
  591.             if((dir->dir_count % DIR_ENT_SIZE) == 0) {
  592.                 new_dir = realloc(dir->dirs, (dir->dir_count +
  593.                     DIR_ENT_SIZE) * sizeof(struct dir_ent));
  594.                 if(new_dir == NULL)
  595.                     EXIT_UNSQUASH("squashfs_opendir: "
  596.                         "realloc failed!\n");
  597.                 dir->dirs = new_dir;
  598.             }
  599.             strcpy(dir->dirs[dir->dir_count].name, dire->name);
  600.             dir->dirs[dir->dir_count].start_block =
  601.                 dirh.start_block;
  602.             dir->dirs[dir->dir_count].offset = dire->offset;
  603.             dir->dirs[dir->dir_count].type = dire->type;
  604.             dir->dir_count ++;
  605.             bytes += dire->size + 1;
  606.         }
  607.     }
  608.  
  609.     return dir;
  610. }
  611.  
  612. int squashfs_uncompress(void *d, void *s, int size, int block_size, int *error)
  613. {
  614.     unsigned long bytes_zlib;
  615.     lzo_uint bytes_lzo;
  616.  
  617.     if (sBlk.compression == ZLIB_COMPRESSION) {
  618.         bytes_zlib = block_size;
  619.         *error = uncompress(d, &bytes_zlib, s, size);
  620.         return *error == Z_OK ? (int) bytes_zlib : -1;
  621.     } else {
  622.         bytes_lzo = block_size;
  623.         *error = lzo1x_decompress_safe(s, size, d, &bytes_lzo, NULL);
  624.         return *error == LZO_E_OK ? bytes_lzo : -1;
  625.     }
  626. }
  627.  
  628. struct cache *cache_init(int buffer_size)
  629. {
  630.     struct cache *cache = malloc(sizeof(struct cache));
  631.  
  632.     if(cache == NULL)
  633.         EXIT_UNSQUASH("Out of memory in cache_init\n");
  634.  
  635.     cache->buffer_size = buffer_size;
  636.     memset(cache->hash_table, 0, sizeof(struct cache_entry *) * 65536);
  637.  
  638.     return cache;
  639. }
  640.  
  641. struct cache_entry *cache_get(struct cache *cache, long long block, int size)
  642. {
  643.     /*
  644.      * Get a block out of the cache.  If the block isn't in the cache
  645.      * it is added and queued to the reader() and deflate() threads for
  646.      * reading off disk and decompression.  The cache grows until max_blocks
  647.      * is reached, once this occurs existing discarded blocks on the free
  648.      * list are reused
  649.      */
  650.     struct cache_entry *entry;
  651.  
  652.     entry = malloc(sizeof(struct cache_entry));
  653.     if(entry == NULL)
  654.         EXIT_UNSQUASH("Out of memory in cache_get\n");
  655.     entry->data = malloc(cache->buffer_size);
  656.     if(entry->data == NULL)
  657.         EXIT_UNSQUASH("Out of memory in cache_get\n");
  658.  
  659.     entry->cache = cache;
  660.     entry->block = block;
  661.     entry->size = size;
  662.  
  663.     return reader(entry);
  664. }
  665.    
  666. void add_entry(struct hash_table_entry *hash_table[], long long start,
  667.     int bytes)
  668. {
  669.     int hash = CALCULATE_HASH(start);
  670.     struct hash_table_entry *hash_table_entry;
  671.  
  672.     hash_table_entry = malloc(sizeof(struct hash_table_entry));
  673.     if(hash_table_entry == NULL)
  674.         EXIT_UNSQUASH("Out of memory in add_entry\n");
  675.  
  676.     hash_table_entry->start = start;
  677.     hash_table_entry->bytes = bytes;
  678.     hash_table_entry->next = hash_table[hash];
  679.     hash_table[hash] = hash_table_entry;
  680. }
  681.  
  682. int lookup_entry(struct hash_table_entry *hash_table[], long long start)
  683. {
  684.     int hash = CALCULATE_HASH(start);
  685.     struct hash_table_entry *hash_table_entry;
  686.  
  687.     for(hash_table_entry = hash_table[hash]; hash_table_entry;
  688.                 hash_table_entry = hash_table_entry->next)
  689.  
  690.         if(hash_table_entry->start == start)
  691.             return hash_table_entry->bytes;
  692.  
  693.     return -1;
  694. }
  695.  
  696. int read_fs_bytes(int fd, long long byte, int bytes, void *buff)
  697. {
  698.     off_t off = byte;
  699.     int res, count;
  700.  
  701.     TRACE("read_bytes: reading from position 0x%llx, bytes %d\n", byte,
  702.         bytes);
  703.  
  704.     if(lseek(fd, off, SEEK_SET) == -1) {
  705.         ERROR("Lseek failed because %s\n", strerror(errno));
  706.         return FALSE;
  707.     }
  708.  
  709.     for(count = 0; count < bytes; count += res) {
  710.         res = read(fd, buff + count, bytes - count);
  711.         if(res < 1) {
  712.             if(res == 0) {
  713.                 ERROR("Read on filesystem failed because "
  714.                     "EOF\n");
  715.                 return FALSE;
  716.             } else if(errno != EINTR) {
  717.                 ERROR("Read on filesystem failed because %s\n",
  718.                         strerror(errno));
  719.                 return FALSE;
  720.             } else
  721.                 res = 0;
  722.         }
  723.     }
  724.  
  725.     return TRUE;
  726. }
  727.  
  728. int read_block(int fd, long long start, long long *next, void *block)
  729. {
  730.     unsigned short c_byte;
  731.     int offset = 2;
  732.    
  733.     if(read_fs_bytes(fd, start, 2, &c_byte) == FALSE)
  734.         goto failed;
  735.  
  736.     TRACE("read_block: block @0x%llx, %d %s bytes\n", start,
  737.         SQUASHFS_COMPRESSED_SIZE(c_byte), SQUASHFS_COMPRESSED(c_byte) ?
  738.         "compressed" : "uncompressed");
  739.  
  740.     if(SQUASHFS_COMPRESSED(c_byte)) {
  741.         char buffer[SQUASHFS_METADATA_SIZE];
  742.         int error, res;
  743.  
  744.         c_byte = SQUASHFS_COMPRESSED_SIZE(c_byte);
  745.         if(read_fs_bytes(fd, start + offset, c_byte, buffer) == FALSE)
  746.             goto failed;
  747.  
  748.         res = squashfs_uncompress(block, buffer, c_byte,
  749.             SQUASHFS_METADATA_SIZE, &error);
  750.  
  751.         if(res == -1) {
  752.             ERROR("uncompress failed with error code %d\n", error);
  753.             goto failed;
  754.         }
  755.         if(next)
  756.             *next = start + offset + c_byte;
  757.         return res;
  758.     } else {
  759.         c_byte = SQUASHFS_COMPRESSED_SIZE(c_byte);
  760.         if(read_fs_bytes(fd, start + offset, c_byte, block) == FALSE)
  761.             goto failed;
  762.         if(next)
  763.             *next = start + offset + c_byte;
  764.         return c_byte;
  765.     }
  766.  
  767. failed:
  768.     ERROR("read_block: failed to read block @0x%llx\n", start);
  769.     return FALSE;
  770. }
  771.  
  772. void uncompress_inode_table(long long start, long long end)
  773. {
  774.     int size = 0, bytes = 0, res;
  775.  
  776.     TRACE("uncompress_inode_table: start %lld, end %lld\n", start, end);
  777.     while(start < end) {
  778.         if(size - bytes < SQUASHFS_METADATA_SIZE) {
  779.             inode_table = realloc(inode_table, size +=
  780.                 SQUASHFS_METADATA_SIZE);
  781.             if(inode_table == NULL)
  782.                 EXIT_UNSQUASH("Out of memory in "
  783.                     "uncompress_inode_table");
  784.         }
  785.         TRACE("uncompress_inode_table: reading block 0x%llx\n", start);
  786.         add_entry(inode_table_hash, start, bytes);
  787.         res = read_block(fd, start, &start, inode_table + bytes);
  788.         if(res == 0) {
  789.             free(inode_table);
  790.             EXIT_UNSQUASH("uncompress_inode_table: failed to read "
  791.                 "block \n");
  792.         }
  793.         bytes += res;
  794.     }
  795. }
  796.  
  797. int write_bytes(int fd, char *buff, int bytes)
  798. {
  799. #ifdef TESTING
  800.     int res, count;
  801.  
  802.     for(count = 0; count < bytes; count += res) {
  803.         res = write(fd, buff + count, bytes - count);
  804.         if(res == -1) {
  805.             if(errno != EINTR) {
  806.                 ERROR("Write on output file failed because "
  807.                     "%s\n", strerror(errno));
  808.                 return -1;
  809.             }
  810.             res = 0;
  811.         }
  812.     }
  813. #else
  814.     if (private_count < 4096) {
  815.         if (bytes > 4096)
  816.             private_count = 4096;
  817.         else
  818.             private_count += bytes;
  819.         memcpy(private_buffer, buff, private_count);
  820.     }
  821. #endif
  822.  
  823.     return 0;
  824. }
  825.  
  826. int write_block(int file_fd, char *buffer, int size, long long hole, int sparse)
  827. {
  828.     if(hole) {
  829.         if(sparse == FALSE && zero_data == NULL) {
  830.             if((zero_data = malloc(block_size)) == NULL)
  831.                 EXIT_UNSQUASH("write_block: failed to alloc "
  832.                     "zero data block\n");
  833.             memset(zero_data, 0, block_size);
  834.         }
  835.  
  836.         if(sparse == FALSE) {
  837.             int blocks = (hole + block_size -1) / block_size;
  838.             int avail_bytes, i;
  839.             for(i = 0; i < blocks; i++, hole -= avail_bytes) {
  840.                 avail_bytes = hole > block_size ? block_size :
  841.                     hole;
  842.                 if(write_bytes(file_fd, zero_data, avail_bytes)
  843.                         == -1)
  844.                     goto failure;
  845.             }
  846.         }
  847.     }
  848.  
  849.     if(write_bytes(file_fd, buffer, size) == -1)
  850.         goto failure;
  851.  
  852.     return TRUE;
  853.  
  854. failure:
  855.     return FALSE;
  856. }
  857.  
  858. int write_file(struct inode *inode, char *pathname)
  859. {
  860.     unsigned int file_fd, i;
  861.     unsigned int *block_list;
  862.     int file_end = inode->data / block_size;
  863.     long long start = inode->start;
  864.     struct squashfs_file *file;
  865.  
  866.     TRACE("write_file: regular file, blocks %d\n", inode->blocks);
  867.  
  868. #ifdef TESTING
  869.     file_fd = open(pathname, O_CREAT | O_WRONLY,
  870.         (mode_t) inode->mode & 0777);
  871.     if(file_fd == -1) {
  872.         ERROR("write_file: failed to create file %s, because %s\n",
  873.             pathname, strerror(errno));
  874.         return FALSE;
  875.     }
  876. #else
  877.     file_fd = 0;
  878. #endif
  879.  
  880.     block_list = malloc(inode->blocks * sizeof(unsigned int));
  881.     if(block_list == NULL)
  882.         EXIT_UNSQUASH("write_file: unable to malloc block list\n");
  883.  
  884.     read_block_list(block_list, inode->block_ptr, inode->blocks);
  885.  
  886.     file = malloc(sizeof(struct squashfs_file));
  887.     if(file == NULL)
  888.         EXIT_UNSQUASH("write_file: unable to malloc file\n");
  889.  
  890.     /*
  891.      * the writer thread is queued a squashfs_file structure describing the
  892.      * file.  If the file has one or more blocks or a fragments they are
  893.      * queued separately (references to blocks in the cache).
  894.      */
  895.     file->fd = file_fd;
  896.     file->file_size = inode->data;
  897.     file->mode = inode->mode;
  898.     file->time = inode->time;
  899.     file->pathname = strdup(pathname);
  900.     file->blocks = inode->blocks + (inode->frag_bytes > 0);
  901.     file->sparse = inode->sparse;
  902.     writer(file);
  903.  
  904.     for(i = 0; i < inode->blocks; i++) {
  905.         int c_byte = SQUASHFS_COMPRESSED_SIZE_BLOCK(block_list[i]);
  906.         struct file_entry *block = malloc(sizeof(struct file_entry));
  907.  
  908.         if(block == NULL)
  909.             EXIT_UNSQUASH("write_file: unable to malloc file\n");
  910.         block->offset = 0;
  911.         block->size = i == file_end ? inode->data & (block_size - 1) :
  912.             block_size;
  913.         if(block_list[i] == 0) /* sparse file */
  914.             block->buffer = NULL;
  915.         else {
  916.             block->buffer = cache_get(data_cache, start,
  917.                 block_list[i]);
  918.             start += c_byte;
  919.         }
  920.         writer(block);
  921.     }
  922.  
  923.     if(inode->frag_bytes) {
  924.         int size;
  925.         long long start;
  926.         struct file_entry *block = malloc(sizeof(struct file_entry));
  927.  
  928.         if(block == NULL)
  929.             EXIT_UNSQUASH("write_file: unable to malloc file\n");
  930.         read_fragment(inode->fragment, &start, &size);
  931.         block->buffer = cache_get(fragment_cache, start, size);
  932.         block->offset = inode->offset;
  933.         block->size = inode->frag_bytes;
  934.         writer(block);
  935.     }
  936.  
  937.     free(block_list);
  938.     return TRUE;
  939. }
  940.  
  941. int create_inode(char *pathname, struct inode *i)
  942. {
  943.     TRACE("create_inode: pathname %s\n", pathname);
  944.  
  945.     if(created_inode[i->inode_number - 1]) {
  946.         ERROR("create_inode: hardlinks not allowed\n");
  947.         return FALSE;
  948.     }
  949.  
  950.     switch(i->type) {
  951.         case SQUASHFS_FILE_TYPE:
  952.             TRACE("create_inode: regular file, file_size %lld, "
  953.                 "blocks %d\n", i->data, i->blocks);
  954.  
  955.             if(write_file(i, pathname))
  956.                 file_count ++;
  957.             break;
  958.         default:
  959.             ERROR("Unknown inode type %d in create_inode_table!\n",
  960.                 i->type);
  961.             return FALSE;
  962.     }
  963.  
  964.     created_inode[i->inode_number - 1] = strdup(pathname);
  965.  
  966.     return TRUE;
  967. }
  968.  
  969. void uncompress_directory_table(long long start, long long end)
  970. {
  971.     int bytes = 0, size = 0, res;
  972.  
  973.     TRACE("uncompress_directory_table: start %lld, end %lld\n", start, end);
  974.  
  975.     while(start < end) {
  976.         if(size - bytes < SQUASHFS_METADATA_SIZE) {
  977.             directory_table = realloc(directory_table, size +=
  978.                 SQUASHFS_METADATA_SIZE);
  979.             if(directory_table == NULL)
  980.                 EXIT_UNSQUASH("Out of memory in "
  981.                     "uncompress_directory_table\n");
  982.         }
  983.         TRACE("uncompress_directory_table: reading block 0x%llx\n",
  984.                 start);
  985.         add_entry(directory_table_hash, start, bytes);
  986.         res = read_block(fd, start, &start, directory_table + bytes);
  987.         if(res == 0)
  988.             EXIT_UNSQUASH("uncompress_directory_table: failed to "
  989.                 "read block\n");
  990.         bytes += res;
  991.     }
  992. }
  993.  
  994. int squashfs_readdir(struct dir *dir, char **name, unsigned int *start_block,
  995. unsigned int *offset, unsigned int *type)
  996. {
  997.     if(dir->cur_entry == dir->dir_count)
  998.         return FALSE;
  999.  
  1000.     *name = dir->dirs[dir->cur_entry].name;
  1001.     *start_block = dir->dirs[dir->cur_entry].start_block;
  1002.     *offset = dir->dirs[dir->cur_entry].offset;
  1003.     *type = dir->dirs[dir->cur_entry].type;
  1004.     dir->cur_entry ++;
  1005.  
  1006.     return TRUE;
  1007. }
  1008.  
  1009. void squashfs_closedir(struct dir *dir)
  1010. {
  1011.     free(dir->dirs);
  1012.     free(dir);
  1013. }
  1014.  
  1015. char *get_component(char *target, char *targname)
  1016. {
  1017.     while(*target == '/')
  1018.         target ++;
  1019.  
  1020.     while(*target != '/' && *target!= '\0')
  1021.         *targname ++ = *target ++;
  1022.  
  1023.     *targname = '\0';
  1024.  
  1025.     return target;
  1026. }
  1027.  
  1028. void free_path(struct pathname *paths)
  1029. {
  1030.     int i;
  1031.  
  1032.     for(i = 0; i < paths->names; i++) {
  1033.         if(paths->name[i].paths)
  1034.             free_path(paths->name[i].paths);
  1035.         free(paths->name[i].name);
  1036.     }
  1037.  
  1038.     free(paths);
  1039. }
  1040.  
  1041. struct pathname *add_path(struct pathname *paths, char *target, char *alltarget)
  1042. {
  1043.     char targname[1024];
  1044.     int i;
  1045.  
  1046.     TRACE("add_path: adding \"%s\" extract file\n", target);
  1047.  
  1048.     target = get_component(target, targname);
  1049.  
  1050.     if(paths == NULL) {
  1051.         paths = malloc(sizeof(struct pathname));
  1052.         if(paths == NULL)
  1053.             EXIT_UNSQUASH("failed to allocate paths\n");
  1054.  
  1055.         paths->names = 0;
  1056.         paths->name = NULL;
  1057.     }
  1058.  
  1059.     for(i = 0; i < paths->names; i++)
  1060.         if(strcmp(paths->name[i].name, targname) == 0)
  1061.             break;
  1062.  
  1063.     if(i == paths->names) {
  1064.         /*
  1065.          * allocate new name entry
  1066.          */
  1067.         paths->names ++;
  1068.         paths->name = realloc(paths->name, (i + 1) *
  1069.             sizeof(struct path_entry));
  1070.         if(paths->name == NULL)
  1071.             EXIT_UNSQUASH("Out of memory in add_path\n");  
  1072.         paths->name[i].name = strdup(targname);
  1073.         paths->name[i].paths = NULL;
  1074.  
  1075.         if(target[0] == '\0')
  1076.             /*
  1077.              * at leaf pathname component
  1078.             */
  1079.             paths->name[i].paths = NULL;
  1080.         else
  1081.             /*
  1082.              * recurse adding child components
  1083.              */
  1084.             paths->name[i].paths = add_path(NULL, target, alltarget);
  1085.     } else {
  1086.         /*
  1087.          * existing matching entry
  1088.          */
  1089.         if(paths->name[i].paths == NULL) {
  1090.             /*
  1091.              * No sub-directory which means this is the leaf
  1092.              * component of a pre-existing extract which subsumes
  1093.              * the extract currently being added, in which case stop
  1094.              * adding components
  1095.              */
  1096.         } else if(target[0] == '\0') {
  1097.             /*
  1098.              * at leaf pathname component and child components exist
  1099.              * from more specific extracts, delete as they're
  1100.              * subsumed by this extract
  1101.              */
  1102.             free_path(paths->name[i].paths);
  1103.             paths->name[i].paths = NULL;
  1104.         } else
  1105.             /*
  1106.              * recurse adding child components
  1107.              */
  1108.             add_path(paths->name[i].paths, target, alltarget);
  1109.     }
  1110.  
  1111.     return paths;
  1112. }
  1113.  
  1114. struct pathnames *init_subdir()
  1115. {
  1116.     struct pathnames *new = malloc(sizeof(struct pathnames));
  1117.     if(new == NULL)
  1118.         EXIT_UNSQUASH("Out of memory in init_subdir\n");
  1119.     new->count = 0;
  1120.     return new;
  1121. }
  1122.  
  1123. struct pathnames *add_subdir(struct pathnames *paths, struct pathname *path)
  1124. {
  1125.     if(paths->count % PATHS_ALLOC_SIZE == 0) {
  1126.         paths = realloc(paths, sizeof(struct pathnames *) +
  1127.             (paths->count + PATHS_ALLOC_SIZE) *
  1128.             sizeof(struct pathname *));
  1129.         if(paths == NULL)
  1130.             EXIT_UNSQUASH("Out of memory in add_subdir\n");
  1131.     }
  1132.  
  1133.     paths->path[paths->count++] = path;
  1134.     return paths;
  1135. }
  1136.  
  1137. void free_subdir(struct pathnames *paths)
  1138. {
  1139.     free(paths);
  1140. }
  1141.  
  1142. int matches(struct pathnames *paths, char *name, struct pathnames **new)
  1143. {
  1144.     int i, n;
  1145.  
  1146.     if(paths == NULL) {
  1147.         *new = NULL;
  1148.         return TRUE;
  1149.     }
  1150.  
  1151.     *new = init_subdir();
  1152.  
  1153.     for(n = 0; n < paths->count; n++) {
  1154.         struct pathname *path = paths->path[n];
  1155.         for(i = 0; i < path->names; i++) {
  1156.             int match = fnmatch(path->name[i].name,
  1157.                 name, FNM_PATHNAME|FNM_PERIOD|FNM_EXTMATCH) ==
  1158.                 0;
  1159.             if(match && path->name[i].paths == NULL)
  1160.                 /*
  1161.                  * match on a leaf component, any subdirectories
  1162.                  * will implicitly match, therefore return an
  1163.                  * empty new search set
  1164.                  */
  1165.                 goto empty_set;
  1166.  
  1167.             if(match)
  1168.                 /*
  1169.                  * match on a non-leaf component, add any
  1170.                  * subdirectories to the new set of
  1171.                  * subdirectories to scan for this name
  1172.                  */
  1173.                 *new = add_subdir(*new, path->name[i].paths);
  1174.         }
  1175.     }
  1176.  
  1177.     if((*new)->count == 0) {
  1178.         /*
  1179.          * no matching names found, delete empty search set, and return
  1180.          * FALSE
  1181.          */
  1182.         free_subdir(*new);
  1183.         *new = NULL;
  1184.         return FALSE;
  1185.     }
  1186.  
  1187.     /*
  1188.      * one or more matches with sub-directories found (no leaf matches),
  1189.      * return new search set and return TRUE
  1190.      */
  1191.     return TRUE;
  1192.  
  1193. empty_set:
  1194.     /*
  1195.      * found matching leaf exclude, return empty search set and return TRUE
  1196.      */
  1197.     free_subdir(*new);
  1198.     *new = NULL;
  1199.     return TRUE;
  1200. }
  1201.  
  1202. void pre_scan(char *parent_name, unsigned int start_block, unsigned int offset,
  1203.     struct pathnames *paths)
  1204. {
  1205.     unsigned int type;
  1206.     char *name, pathname[1024];
  1207.     struct pathnames *new;
  1208.     struct inode *i;
  1209.     struct dir *dir = squashfs_opendir(start_block, offset, &i);
  1210.  
  1211.     while(squashfs_readdir(dir, &name, &start_block, &offset, &type)) {
  1212.         struct inode *i;
  1213.  
  1214.         TRACE("pre_scan: name %s, start_block %d, offset %d, type %d\n",
  1215.             name, start_block, offset, type);
  1216.  
  1217.         if(!matches(paths, name, &new))
  1218.             continue;
  1219.  
  1220.         strcat(strcat(strcpy(pathname, parent_name), "/"), name);
  1221.  
  1222.         if(type == SQUASHFS_DIR_TYPE)
  1223.             pre_scan(parent_name, start_block, offset, new);
  1224.         else if(new == NULL) {
  1225.             if(type == SQUASHFS_FILE_TYPE) {
  1226.                 i = read_inode(start_block, offset);
  1227.                 if(created_inode[i->inode_number - 1] == NULL)
  1228.                     created_inode[i->inode_number - 1] =
  1229.                         (char *) i;
  1230.             }
  1231.         }
  1232.  
  1233.         free_subdir(new);
  1234.     }
  1235.  
  1236.     squashfs_closedir(dir);
  1237. }
  1238.  
  1239. void dir_scan(char *parent_name, unsigned int start_block, unsigned int offset,
  1240.     struct pathnames *paths)
  1241. {
  1242.     unsigned int type;
  1243.     char *name, pathname[1024];
  1244.     struct pathnames *new;
  1245.     struct inode *i;
  1246.     struct dir *dir = squashfs_opendir(start_block, offset, &i);
  1247.  
  1248. #ifdef TESTING
  1249.     if(mkdir(parent_name, (mode_t) dir->mode) == -1 && errno != EEXIST) {
  1250.         ERROR("dir_scan: failed to make directory %s, because %s\n",
  1251.             parent_name, strerror(errno));
  1252.         squashfs_closedir(dir);
  1253.         return;
  1254.     }
  1255. #endif
  1256.  
  1257.     while(squashfs_readdir(dir, &name, &start_block, &offset, &type)) {
  1258.         TRACE("dir_scan: name %s, start_block %d, offset %d, type %d\n",
  1259.             name, start_block, offset, type);
  1260.  
  1261.  
  1262.         if(!matches(paths, name, &new)) {
  1263.             TRACE("dir_scan: duplicate entry for name %s\n", name);
  1264.             continue;
  1265.         }
  1266.  
  1267.         strcat(strcat(strcpy(pathname, parent_name), "/"), name);
  1268.  
  1269.         if(type == SQUASHFS_DIR_TYPE)
  1270.             dir_scan(pathname, start_block, offset, new);
  1271.         else if(new == NULL) {
  1272.             i = read_inode(start_block, offset);
  1273.             if(i != NULL)
  1274.                 create_inode(pathname, i);
  1275.         }
  1276.  
  1277.         free_subdir(new);
  1278.     }
  1279.  
  1280.     squashfs_closedir(dir);
  1281.     dir_count ++;
  1282. }
  1283.  
  1284. int read_super(char *source)
  1285. {
  1286.     /*
  1287.      * Try to read a Squashfs 4 superblock
  1288.      */
  1289.     read_fs_bytes(fd, SQUASHFS_START, sizeof(struct squashfs_super_block),
  1290.         &sBlk);
  1291.  
  1292.     if(sBlk.s_magic == SQUASHFS_MAGIC && sBlk.s_major == 4 &&
  1293.             sBlk.s_minor == 0) {
  1294.         return TRUE;
  1295.     } else {
  1296.         ERROR("Can't find a SQUASHFS superblock on %s\n", source);
  1297.         return FALSE;
  1298.     }
  1299. }
  1300.  
  1301. struct pathname *process_extract_files(struct pathname *path, char *filename)
  1302. {
  1303.     FILE *fd;
  1304.     char name[16384];
  1305.  
  1306.     fd = fopen(filename, "r");
  1307.     if(fd == NULL)
  1308.         EXIT_UNSQUASH("Could not open %s, because %s\n", filename,
  1309.             strerror(errno));
  1310.  
  1311.     while(fscanf(fd, "%16384[^\n]\n", name) != EOF)
  1312.         path = add_path(path, name, name);
  1313.  
  1314.     fclose(fd);
  1315.     return path;
  1316. }
  1317.  
  1318. /*
  1319.  * reader thread.  This thread processes read requests queued by the
  1320.  * cache_get() routine.
  1321.  */
  1322. void *reader(void *arg)
  1323. {
  1324.     struct cache_entry *entry = (struct cache_entry *)arg;
  1325.  
  1326.     int res = read_fs_bytes(fd, entry->block,
  1327.         SQUASHFS_COMPRESSED_SIZE_BLOCK(entry->size),
  1328.         entry->data);
  1329.  
  1330.     if(res && SQUASHFS_COMPRESSED_BLOCK(entry->size))
  1331.         /*
  1332.          * queue successfully read block to the deflate
  1333.          * thread(s) for further processing
  1334.          */
  1335.         deflator(entry);
  1336.  
  1337.     return entry;
  1338. }
  1339.  
  1340. /*
  1341.  * writer thread.  This processes file write requests queued by the
  1342.  * write_file() routine.
  1343.  */
  1344. void *writer(void *arg)
  1345. {
  1346.     static struct squashfs_file *file = NULL;
  1347.     static long long hole = 0;
  1348.     struct file_entry *block;
  1349.  
  1350.     if (file == NULL) {
  1351.         file = (struct squashfs_file *)arg;
  1352.         return file;
  1353.     }
  1354.  
  1355.     TRACE("writer: regular file, blocks %d\n", file->blocks);
  1356.  
  1357.     block = (struct file_entry *)arg;
  1358.  
  1359.     if(block->buffer == 0) { /* sparse file */
  1360.         hole += block->size;
  1361.     } else {
  1362.         write_block(file->fd, block->buffer->data +
  1363.             block->offset, block->size, hole, file->sparse);
  1364.     }
  1365.     free(block);
  1366.  
  1367.     if (++cur_blocks == file->blocks) {
  1368.         close(file->fd);
  1369.         free(file->pathname);
  1370.         cur_blocks = 0;
  1371.         hole = 0;
  1372.         free(file);
  1373.         file = NULL;
  1374.     }
  1375.     return NULL;
  1376. }
  1377.  
  1378. /*
  1379.  * decompress thread.  This decompresses buffers queued by the read thread
  1380.  */
  1381. void *deflator(void *arg)
  1382. {
  1383.     struct cache_entry *entry = (struct cache_entry *)arg;
  1384.     char tmp[block_size];
  1385.     int error, res;
  1386.  
  1387.         res = squashfs_uncompress(tmp, entry->data,
  1388.             SQUASHFS_COMPRESSED_SIZE_BLOCK(entry->size), block_size,
  1389.             &error);
  1390.  
  1391.         if(res == -1)
  1392.             ERROR("uncompress failed with error code %d\n", error);
  1393.         else
  1394.             memcpy(entry->data, tmp, res);
  1395.  
  1396.         return entry;
  1397. }
  1398.  
  1399. #ifdef TESTING
  1400. int main(int argc, char *argv[])
  1401. #else
  1402. char *unsquashfs_single_file(char *image_name, char *file_name)
  1403. #endif
  1404. {
  1405.     char *dest = "squashfs-root";
  1406.     struct pathnames *paths = NULL;
  1407.     struct pathname *path = NULL;
  1408.  
  1409. #ifdef TESTING
  1410.     if(argc < 3) {
  1411.         EXIT_UNSQUASH("SYNTAX: %s [options] filesystem "
  1412.             "[file to extract]\n", argv[0]);
  1413.     }
  1414.  
  1415.     path = add_path(path, argv[2], argv[2]);
  1416.  
  1417.     if((fd = open(argv[1], O_RDONLY)) == -1) {
  1418.         EXIT_UNSQUASH("Could not open %s, because %s\n", argv[1],
  1419.             strerror(errno));
  1420.     }
  1421.  
  1422.     if(read_super(argv[1]) == FALSE)
  1423.         EXIT_UNSQUASH("Could not read superblock\n");
  1424. #else
  1425.     if((fd = open(image_name, O_RDONLY)) == -1) {
  1426.         EXIT_UNSQUASH("Could not open %s, because %s\n", image_name,
  1427.             strerror(errno));
  1428.     }
  1429.  
  1430.     if(read_super(image_name) == FALSE)
  1431.         EXIT_UNSQUASH("Could not read superblock\n");
  1432.  
  1433.     private_buffer = malloc(4096);
  1434.         if(private_buffer == NULL)
  1435.         EXIT_UNSQUASH("Unable to allocate private buffer");
  1436.  
  1437.     memset(private_buffer, 0, 4096);
  1438.  
  1439.     path = add_path(path, file_name, file_name);
  1440. #endif
  1441.  
  1442.     if ((sBlk.compression != ZLIB_COMPRESSION) && (sBlk.compression != LZO_COMPRESSION)) {
  1443.         EXIT_UNSQUASH("No decompressors available:\n");
  1444.     }
  1445.  
  1446.     block_size = sBlk.block_size;
  1447.     block_log = sBlk.block_log;
  1448.  
  1449.     fragment_cache = cache_init(block_size);
  1450.     data_cache = cache_init(block_size);
  1451.  
  1452.     created_inode = malloc(sBlk.inodes * sizeof(char *));
  1453.     if(created_inode == NULL)
  1454.         EXIT_UNSQUASH("failed to allocate created_inode\n");
  1455.  
  1456.     memset(created_inode, 0, sBlk.inodes * sizeof(char *));
  1457.  
  1458.     if(read_fragment_table() == FALSE)
  1459.         EXIT_UNSQUASH("failed to read fragment table\n");
  1460.  
  1461.     uncompress_inode_table(sBlk.inode_table_start,
  1462.         sBlk.directory_table_start);
  1463.  
  1464.     uncompress_directory_table(sBlk.directory_table_start,
  1465.         sBlk.fragment_table_start);
  1466.  
  1467.     if(path) {
  1468.         paths = init_subdir();
  1469.         paths = add_subdir(paths, path);
  1470.     }
  1471.  
  1472.     pre_scan(dest, SQUASHFS_INODE_BLK(sBlk.root_inode),
  1473.         SQUASHFS_INODE_OFFSET(sBlk.root_inode), paths);
  1474.  
  1475.     memset(created_inode, 0, sBlk.inodes * sizeof(char *));
  1476.  
  1477.     dir_scan(dest, SQUASHFS_INODE_BLK(sBlk.root_inode),
  1478.         SQUASHFS_INODE_OFFSET(sBlk.root_inode), paths);
  1479.  
  1480. #ifdef TESTING
  1481.     printf("\n");
  1482.     printf("created %d files\n", file_count);
  1483.     printf("created %d directories\n", dir_count);
  1484.  
  1485.     return 0;
  1486. #else
  1487.     return private_buffer;
  1488. #endif
  1489. }
Advertisement
Add Comment
Please, Sign In to add comment