Method: SectionStripper#updateEXTH121

Defined in:
lib/kindlestrip.rb

#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