summaryrefslogtreecommitdiff
blob: b19b587c2af59212da605b7361c78ac0870c5b00 (plain)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/bin/bash

ORIGFILE="$1"

is_relative() {
    local path="$1"
    shift

    [ "${path:0:1}" != "/" ]
    return
}

set_offsets() {
	# This won't work with non-GNU stat.
	FILE_SIZE=`stat -L --format "%s" "$1"`
	local offset=$(($FILE_SIZE - 4))
	
	MAGIC_OFFSET=$offset
	offset=$(($offset - 4))
	
	CHECKSUM_OFFSET=$offset
	offset=$(($offset - 4))
	
	VERSION_OFFSET=$offset
	offset=$(($offset - 4))
	
	PREPAYLOAD_OFFSET=$offset
	offset=$(($offset - 4))
	
	PREPAYLOAD_SIZE_OFFSET=$offset
	offset=$(($offset - 4))
	
	LAUNCHER_SIZE_OFFSET=$offset
	offset=$(($offset - 4))
	
	PAYLOAD_OFFSET=$offset
	offset=$(($offset - 4))
	
	PAYLOAD_SIZE_OFFSET=$offset
	offset=$(($offset - 4))
}

set_lengths() {
   local file="$1"
   if [ ! -s "$file" ]; then
      echo "$file does not exist"
      exit 1
   fi

   # XXX: put extraction in its own function
   MAGIC_NUMBER=`od -An -t u4 -N 4 -j $MAGIC_OFFSET "$file" | tr -d ' '`

   if [ "$MAGIC_NUMBER" != "907380241" ]; then
      echo "magic number does not match"
      exit 1
   fi

   LAUNCHER_SIZE=`od -An -t u4 -N 4 -j $LAUNCHER_SIZE_OFFSET "$file" | tr -d ' '`
   PAYLOAD_SIZE=`od -An -t u4 -N 4 -j $PAYLOAD_SIZE_OFFSET "$file" | tr -d ' '`
   PREPAYLOAD_SIZE=`od -An -t u4 -N 4 -j $PREPAYLOAD_SIZE_OFFSET "$file" | tr -d ' '`

   SKIP_BYTES=$(($PREPAYLOAD_SIZE + $LAUNCHER_SIZE))

   return 0
}

if is_relative "${ORIGFILE}"; then
	ORIGFILE="`pwd`/${ORIGFILE}"
fi


set_offsets ${ORIGFILE}
set_lengths ${ORIGFILE}

echo "Unbundling" ${ORIGFILE}

PREPAYLOAD="prepayload"
PAYLOAD="payload"

# Unpack the pre-payload file
mkdir ${PREPAYLOAD}
cd ${PREPAYLOAD}
dd if="${ORIGFILE}" ibs=$LAUNCHER_SIZE obs=1024 skip=1 | tar -xzf - 2> /dev/null
cd ..

# Unpack the main file
mkdir ${PAYLOAD}
cd ${PAYLOAD}
dd if="${ORIGFILE}" ibs=$SKIP_BYTES obs=1024 skip=1 | tar -xzf - 2> /dev/null
cd ..