def repl_jpeg2(swf, offset, jpeg)
      swf.force_encoding("ASCII-8BIT") if swf.respond_to? :force_encoding
      jpeg.force_encoding("ASCII-8BIT") if jpeg.respond_to? :force_encoding
      record_header = swf[offset, 2].unpack("v").first
      # tag check (対象がSWF全体なので、効率の面からTagインスタンスを作らないでチェック)
      raise ReplaceTargetError if (record_header >> 6) & 1023 != 21
      # calc length
      org_jpeg_length = record_header & 63 - 2
      before_long_header = false
      after_long_header = false
      if org_jpeg_length == 61
        before_long_header = true
        org_jpeg_length = swf[offset+2, 4].unpack("i").first - 2
      end
      target_jpeg_length = jpeg.length + 4
      delta_jpeg_length = target_jpeg_length - org_jpeg_length
      after_long_header = true if target_jpeg_length + 2 >= 63
      # replace jpeg and rewrite length
      # タグのヘッダ長さが入れ替え前後で変化する場合があるので場合分けする
      if before_long_header and after_long_header
        swf[offset+8, org_jpeg_length] = [0xff, 0xd9, 0xff, 0xd8].pack("C*") + jpeg
        swf[offset+2, 4] = [target_jpeg_length + 2].pack("i")
        swf[4, 4] = [swf[4, 4].unpack("V").first + delta_jpeg_length].pack("V")
      elsif before_long_header and !after_long_header
        swf[offset+8, org_jpeg_length] = [0xff, 0xd9, 0xff, 0xd8].pack("C*") + jpeg
        swf[offset, 4] = [(((record_header >> 6) & 1023) << 6) + target_jpeg_length + 2].pack("v")
        swf[4, 4] = [swf[4, 4].unpack("V").first + delta_jpeg_length - 4].pack("V")
      elsif !before_long_header and after_long_header
        swf[offset+4, org_jpeg_length] = [0xff, 0xd9, 0xff, 0xd8].pack("C*") + jpeg
        swf[offset, 2] = [(((record_header >> 6) & 1023) << 6) + 63].pack("v") + [target_jpeg_length + 2].pack("i")
        swf[4, 4] = [swf[4, 4].unpack("V").first + delta_jpeg_length + 4].pack("V")
      elsif !before_long_header and !after_long_header
        swf[offset+4, org_jpeg_length] = [0xff, 0xd9, 0xff, 0xd8].pack("C*") + jpeg
        swf[offset, 2] = [(((record_header >> 6) & 1023) << 6) + target_jpeg_length + 2].pack("v")
        swf[4, 4] = [swf[4, 4].unpack("V").first + delta_jpeg_length].pack("V")
      end
      swf
    end
def repl_jpeg2(swf, offset, jpeg)
      swf.force_encoding("ASCII-8BIT") if swf.respond_to? :force_encoding
      jpeg.force_encoding("ASCII-8BIT") if jpeg.respond_to? :force_encoding
      record_header = swf[offset, 2].unpack("v").first
      # tag check (対象がSWF全体なので、効率の面からTagインスタンスを作らないでチェック)
      raise ReplaceTargetError if (record_header >> 6) & 1023 != 21
      # calc length
      org_jpeg_length = record_header & 63 - 2
      before_long_header = false
      after_long_header = false
      if org_jpeg_length == 61
        before_long_header = true
        org_jpeg_length = swf[offset+2, 4].unpack("i").first - 2
      end
      target_jpeg_length = jpeg.length + 4
      delta_jpeg_length = target_jpeg_length - org_jpeg_length
      after_long_header = true if target_jpeg_length + 2 >= 63
      # replace jpeg and rewrite length
      # タグのヘッダ長さが入れ替え前後で変化する場合があるので場合分けする
      if before_long_header and after_long_header
        swf[offset+8, org_jpeg_length] = [0xff, 0xd9, 0xff, 0xd8].pack("C*") + jpeg
        swf[offset+2, 4] = [target_jpeg_length + 2].pack("i")
        swf[4, 4] = [swf[4, 4].unpack("V").first + delta_jpeg_length].pack("V")
      elsif before_long_header and !after_long_header
        swf[offset+8, org_jpeg_length] = [0xff, 0xd9, 0xff, 0xd8].pack("C*") + jpeg
        swf[offset, 4] = [(((record_header >> 6) & 1023) << 6) + target_jpeg_length + 2].pack("v")
        swf[4, 4] = [swf[4, 4].unpack("V").first + delta_jpeg_length - 4].pack("V")
      elsif !before_long_header and after_long_header
        swf[offset+4, org_jpeg_length] = [0xff, 0xd9, 0xff, 0xd8].pack("C*") + jpeg
        swf[offset, 2] = [(((record_header >> 6) & 1023) << 6) + 63].pack("v") + [target_jpeg_length + 2].pack("i")
        swf[4, 4] = [swf[4, 4].unpack("V").first + delta_jpeg_length + 4].pack("V")
      elsif !before_long_header and !after_long_header
        swf[offset+4, org_jpeg_length] = [0xff, 0xd9, 0xff, 0xd8].pack("C*") + jpeg
        swf[offset, 2] = [(((record_header >> 6) & 1023) << 6) + target_jpeg_length + 2].pack("v")
        swf[4, 4] = [swf[4, 4].unpack("V").first + delta_jpeg_length].pack("V")
      end
      swf
    end
DefineBitsJpeg2のイメージバイナリを置換.