unsquash-2.c revision d12247df03571cd8b684346038fea2fdfa92cfb8
1/* 2 * Unsquash a squashfs filesystem. This is a highly compressed read only filesystem. 3 * 4 * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 5 * Phillip Lougher <phillip@lougher.demon.co.uk> 6 * 7 * This program is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU General Public License 9 * as published by the Free Software Foundation; either version 2, 10 * or (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 20 * 21 * unsquash-2.c 22 */ 23 24#include "unsquashfs.h" 25 26static squashfs_fragment_entry_2 *fragment_table; 27 28void read_block_list_2(unsigned int *block_list, char *block_ptr, int blocks) 29{ 30 if(swap) { 31 unsigned int sblock_list[blocks]; 32 memcpy(sblock_list, block_ptr, blocks * sizeof(unsigned int)); 33 SQUASHFS_SWAP_INTS_3(block_list, sblock_list, blocks); 34 } else 35 memcpy(block_list, block_ptr, blocks * sizeof(unsigned int)); 36} 37 38 39void read_fragment_table_2() 40{ 41 int i, indexes = SQUASHFS_FRAGMENT_INDEXES_2(sBlk.fragments); 42 unsigned int fragment_table_index[indexes]; 43 44 TRACE("read_fragment_table: %d fragments, reading %d fragment indexes from 0x%llx\n", sBlk.fragments, indexes, sBlk.fragment_table_start); 45 46 if(sBlk.fragments == 0) 47 return; 48 49 if((fragment_table = malloc(sBlk.fragments * 50 sizeof(squashfs_fragment_entry_2))) == NULL) 51 EXIT_UNSQUASH("read_fragment_table: failed to allocate fragment table\n"); 52 53 if(swap) { 54 unsigned int sfragment_table_index[indexes]; 55 56 read_bytes(sBlk.fragment_table_start, SQUASHFS_FRAGMENT_INDEX_BYTES_2(sBlk.fragments), (char *) sfragment_table_index); 57 SQUASHFS_SWAP_FRAGMENT_INDEXES_2(fragment_table_index, sfragment_table_index, indexes); 58 } else 59 read_bytes(sBlk.fragment_table_start, SQUASHFS_FRAGMENT_INDEX_BYTES_2(sBlk.fragments), (char *) fragment_table_index); 60 61 for(i = 0; i < indexes; i++) { 62 int length = read_block(fragment_table_index[i], NULL, 63 ((char *) fragment_table) + (i * SQUASHFS_METADATA_SIZE)); 64 TRACE("Read fragment table block %d, from 0x%x, length %d\n", i, fragment_table_index[i], length); 65 } 66 67 if(swap) { 68 squashfs_fragment_entry_2 sfragment; 69 for(i = 0; i < sBlk.fragments; i++) { 70 SQUASHFS_SWAP_FRAGMENT_ENTRY_2((&sfragment), (&fragment_table[i])); 71 memcpy((char *) &fragment_table[i], (char *) &sfragment, sizeof(squashfs_fragment_entry_2)); 72 } 73 } 74} 75 76 77void read_fragment_2(unsigned int fragment, long long *start_block, int *size) 78{ 79 TRACE("read_fragment: reading fragment %d\n", fragment); 80 81 squashfs_fragment_entry_2 *fragment_entry = &fragment_table[fragment]; 82 *start_block = fragment_entry->start_block; 83 *size = fragment_entry->size; 84} 85 86 87struct inode *read_inode_2(unsigned int start_block, unsigned int offset) 88{ 89 static squashfs_inode_header_2 header; 90 long long start = sBlk.inode_table_start + start_block; 91 int bytes = lookup_entry(inode_table_hash, start); 92 char *block_ptr = inode_table + bytes + offset; 93 static struct inode i; 94 95 if(bytes == -1) 96 goto error; 97 98 if(swap) { 99 squashfs_base_inode_header_2 sinode; 100 memcpy(&sinode, block_ptr, sizeof(header.base)); 101 SQUASHFS_SWAP_BASE_INODE_HEADER_2(&header.base, &sinode, sizeof(squashfs_base_inode_header_2)); 102 } else 103 memcpy(&header.base, block_ptr, sizeof(header.base)); 104 105 i.uid = (uid_t) uid_table[header.base.uid]; 106 i.gid = header.base.guid == SQUASHFS_GUIDS ? i.uid : (uid_t) guid_table[header.base.guid]; 107 i.mode = lookup_type[header.base.inode_type] | header.base.mode; 108 i.type = header.base.inode_type; 109 i.time = sBlk.mkfs_time; 110 i.inode_number = inode_number++; 111 112 switch(header.base.inode_type) { 113 case SQUASHFS_DIR_TYPE: { 114 squashfs_dir_inode_header_2 *inode = &header.dir; 115 116 if(swap) { 117 squashfs_dir_inode_header_2 sinode; 118 memcpy(&sinode, block_ptr, sizeof(header.dir)); 119 SQUASHFS_SWAP_DIR_INODE_HEADER_2(&header.dir, &sinode); 120 } else 121 memcpy(&header.dir, block_ptr, sizeof(header.dir)); 122 123 i.data = inode->file_size; 124 i.offset = inode->offset; 125 i.start = inode->start_block; 126 i.time = inode->mtime; 127 break; 128 } 129 case SQUASHFS_LDIR_TYPE: { 130 squashfs_ldir_inode_header_2 *inode = &header.ldir; 131 132 if(swap) { 133 squashfs_ldir_inode_header_2 sinode; 134 memcpy(&sinode, block_ptr, sizeof(header.ldir)); 135 SQUASHFS_SWAP_LDIR_INODE_HEADER_2(&header.ldir, &sinode); 136 } else 137 memcpy(&header.ldir, block_ptr, sizeof(header.ldir)); 138 139 i.data = inode->file_size; 140 i.offset = inode->offset; 141 i.start = inode->start_block; 142 i.time = inode->mtime; 143 break; 144 } 145 case SQUASHFS_FILE_TYPE: { 146 squashfs_reg_inode_header_2 *inode = &header.reg; 147 148 if(swap) { 149 squashfs_reg_inode_header_2 sinode; 150 memcpy(&sinode, block_ptr, sizeof(sinode)); 151 SQUASHFS_SWAP_REG_INODE_HEADER_2(inode, &sinode); 152 } else 153 memcpy(inode, block_ptr, sizeof(*inode)); 154 155 i.data = inode->file_size; 156 i.time = inode->mtime; 157 i.frag_bytes = inode->fragment == SQUASHFS_INVALID_FRAG ? 158 0 : inode->file_size % sBlk.block_size; 159 i.fragment = inode->fragment; 160 i.offset = inode->offset; 161 i.blocks = inode->fragment == SQUASHFS_INVALID_FRAG ? 162 (inode->file_size + sBlk.block_size - 1) >> 163 sBlk.block_log : inode->file_size >> sBlk.block_log; 164 i.start = inode->start_block; 165 i.block_ptr = block_ptr + sizeof(*inode); 166 break; 167 } 168 case SQUASHFS_SYMLINK_TYPE: { 169 squashfs_symlink_inode_header_2 *inodep = &header.symlink; 170 171 if(swap) { 172 squashfs_symlink_inode_header_2 sinodep; 173 memcpy(&sinodep, block_ptr, sizeof(sinodep)); 174 SQUASHFS_SWAP_SYMLINK_INODE_HEADER_2(inodep, &sinodep); 175 } else 176 memcpy(inodep, block_ptr, sizeof(*inodep)); 177 178 strncpy(i.symlink, block_ptr + sizeof(squashfs_symlink_inode_header_2), inodep->symlink_size); 179 i.symlink[inodep->symlink_size] = '\0'; 180 i.data = inodep->symlink_size; 181 break; 182 } 183 case SQUASHFS_BLKDEV_TYPE: 184 case SQUASHFS_CHRDEV_TYPE: { 185 squashfs_dev_inode_header_2 *inodep = &header.dev; 186 187 if(swap) { 188 squashfs_dev_inode_header_2 sinodep; 189 memcpy(&sinodep, block_ptr, sizeof(sinodep)); 190 SQUASHFS_SWAP_DEV_INODE_HEADER_2(inodep, &sinodep); 191 } else 192 memcpy(inodep, block_ptr, sizeof(*inodep)); 193 194 i.data = inodep->rdev; 195 break; 196 } 197 case SQUASHFS_FIFO_TYPE: 198 case SQUASHFS_SOCKET_TYPE: 199 i.data = 0; 200 break; 201 default: 202 ERROR("Unknown inode type %d in read_inode_header_2!\n", header.base.inode_type); 203 return NULL; 204 } 205 return &i; 206 207error: 208 return NULL; 209} 210