Class: SectionStripper

Inherits:
Object
  • Object
show all
Defined in:
lib/kindlestrip.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(datain, verbose = true) ⇒ SectionStripper

Returns a new instance of SectionStripper.



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/kindlestrip.rb', line 119

def initialize(datain, verbose = true)
  @verbose = verbose
  if datain[0x3C...0x3C+8] != "BOOKMOBI"
    raise StripException, "invalid file format"
  end
  @num_sections, = datain[76...78].unpack("n")

  # get mobiheader and check SRCS section number and count
  offset0, = datain.unpack("@78N")
  offset1, = datain.unpack("@86N")
  mobiheader = datain[offset0 ... offset1]
  srcs_secnum, srcs_cnt = mobiheader.unpack("@224NN")
  if srcs_secnum == 0xffffffff || srcs_cnt == 0
    raise StripException, "File doesn't contain the sources section."
  end

  puts "Found SRCS section number %d, and count %d" % [srcs_secnum, srcs_cnt] if @verbose
  # find its offset and length
  _next = srcs_secnum + srcs_cnt
  srcs_offset, = datain.unpack("@#{78+srcs_secnum*8}NN")
  next_offset, = datain.unpack("@#{78+_next*8}NN")
  srcs_length = next_offset - srcs_offset
  if datain[srcs_offset ... srcs_offset+4] != "SRCS"
    raise StripException, "SRCS section num does not point to SRCS."
  end
  puts "   beginning at offset %0x and ending at offset %0x" % [srcs_offset, srcs_length] if @verbose

  # it appears bytes 68-71 always contain (2*num_sections) + 1
  # this is not documented anyplace at all but it appears to be some sort of next 
  # available unique_id used to identify specific sections in the palm db
  @data_file = datain[0, 68] + [(@num_sections - srcs_cnt) * 2 + 1].pack("N")
  @data_file += datain[72...76]

  # write out the number of sections reduced by srtcs_cnt
  @data_file = @data_file + [@num_sections - srcs_cnt].pack("n")

  # we are going to remove srcs_cnt SRCS sections so the offset of every entry in the table
  # up to the srcs secnum must begin 8 bytes earlier per section removed (each table entry is 8 )
  delta = -8 * srcs_cnt
  srcs_secnum.times do |i|
    offset, flgval = datain.unpack("@#{78+i*8}NN")
    offset += delta
    @data_file += [offset].pack("N") + [flgval].pack("N")
  end

  # for every record after the srcs_cnt SRCS records we must start it
  # earlier by 8*srcs_cnt + the length of the srcs sections themselves)
  delta = delta - srcs_length
  (srcs_secnum + srcs_cnt ... @num_sections).each do |i|
    offset, = datain.unpack("@#{78+i*8}NN")
    offset += delta
    flgval = 2 * (i - srcs_cnt)
    @data_file += [offset].pack("N") + [flgval].pack("N")
  end

  # now pad it out to begin right at the first offset
  # typically this is 2 bytes of nulls
  first_offset, = @data_file.unpack("@78NN")
  @data_file += "\0" * (first_offset - @data_file.length)

  # now finally add on every thing up to the original src_offset
  @data_file += datain[offset0...srcs_offset]
  
  # and everything afterwards
  @data_file += datain[srcs_offset + srcs_length .. -1]

  #store away the SRCS section in case the user wants it output
  @stripped_data_header = datain[srcs_offset ... srcs_offset + 16]
  @stripped_data = datain[srcs_offset + 16 ... srcs_offset + srcs_length]

  # update the number of sections count
  @num_section = @num_sections - srcs_cnt

  # update the srcs_secnum and srcs_cnt in the mobiheader
  offset0, = @data_file.unpack("@78NN")
  offset1, = @data_file.unpack("@86NN")
  mobiheader = @data_file[offset0 ... offset1]
  mobiheader = mobiheader[0, 0xe0] + [-1].pack("N") + [0].pack("N") + mobiheader[0xe8 .. -1]

  # if K8 mobi, handle metadata 121 in old mobiheader
  mobiheader = updateEXTH121(srcs_secnum, srcs_cnt, mobiheader)
  @data_file = @data_file[0, offset0] + mobiheader + @data_file[offset1 .. -1]
  puts "done" if @verbose
end

Class Method Details

.strip(infile, outfile = nil, verbose = true) ⇒ Object



216
217
218
219
220
221
222
# File 'lib/kindlestrip.rb', line 216

def self.strip(infile, outfile = nil, verbose = true)
  outfile = infile unless outfile
  data_file = File.binread(infile)
  stripped_file = new(data_file, verbose)
  File.binwrite(outfile, stripped_file.get_result)
  stripped_file
end

Instance Method Details

#get_headerObject



212
213
214
# File 'lib/kindlestrip.rb', line 212

def get_header
  @stripped_data_header
end

#get_resultObject



204
205
206
# File 'lib/kindlestrip.rb', line 204

def get_result
  @data_file
end

#get_stripped_dataObject



208
209
210
# File 'lib/kindlestrip.rb', line 208

def get_stripped_data
  @stripped_data
end

#load_section(section) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/kindlestrip.rb', line 59

def load_section(section)
  if section + 1 == @num_sections
    endoff = @data_file.length
  else
    endoff = @sections[section + 1][0]
  end
  off = @sections[section][0]
  @data_file[off...endoff]
end

#patch(off, _new) ⇒ Object



69
70
71
# File 'lib/kindlestrip.rb', line 69

def patch(off, _new)
  @data_file = @data_file[0, off] + new + @data_file[off + _new.length .. -1]
end

#patch_section(section, _new, in_off = 0) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/kindlestrip.rb', line 77

def patch_section(section, _new, in_off = 0)
  if section + 1 == @num_sections
    endoff = @data_file.length
  else
    endoff = @sections[section + 1][0]
  end
  raise unless off + in_off + _new.length <= endoff
  patch(off + in_off, _new)
end

#strip(off, len) ⇒ Object



73
74
75
# File 'lib/kindlestrip.rb', line 73

def strip(off, len)
  @data_file = @data_file[0, off] + @data_file[off + len .. -1]
end

#updateEXTH121(srcs_secnum, srcs_cnt, mobiheader) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/kindlestrip.rb', line 87

def updateEXTH121(srcs_secnum, srcs_cnt, mobiheader)
  mobi_length, = mobiheader[0x14...0x18].unpack("N")
  exth_flag, = mobiheader[0x80...0x84].unpack("N")
  exth = "NONE"
  begin
    if exth_flag & 0x40 != 0
      exth = mobiheader[16 + mobi_length .. -1]
      if exth.length >= 4 && exth[0, 4] == "EXTH"
        nitems, = exth[8...12].unpack("N")
        pos = 12
        nitems.times do
          type, size = exth[pos ... pos + 8].unpack("NN")
          #puts "#{type}, #{size}"
          if type == 121
            boundaryptr, = exth[pos + 8 ... pos + size].unpack("N")
            if srcs_secnum <= boundaryptr
              boundaryptr -= srcs_cnt
              prefix = mobiheader[0, 16 + mobi_length + pos + 8]
              suffix = mobiheader[16 + mobi_length + pos + 8 + 4 .. -1]
              nval = [boundaryptr].pack("N")
              mobiheader = prefix + nval + suffix
            end
          end
          pos += size
        end
      end
    end
  rescue
  end
  mobiheader
end