Method: SectionStripper#initialize

Defined in:
lib/kindlestrip.rb

#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