<commit_before># encoding: ascii-8bit
require 'zlib'

class ImageSpec
  module Parser
    class SWF

      CONTENT_TYPE = 'application/x-shockwave-flash'

      def self.attributes(stream)
        width, height = dimensions(stream)
        {:width => width, :height => height, :content_type => CONTENT_TYPE, :dimensions => [width, height], :file_size => size(stream)}
      end

      def self.detected?(stream)
        stream.rewind
        stream.read(3) =~ /(F|C)WS/ ? true : false
      end

      def self.dimensions(stream)
        # Read the entire stream into memory because the
        # dimensions aren't stored in a standard location
        stream.rewind
        contents = stream.read.force_encoding("ASCII-8BIT")

        # Our 'signature' is the first 3 bytes
        # Either FWS or CWS.  CWS indicates compression
        signature = contents[0..2]

        # Determine the length of the uncompressed stream
        length = contents[4..7].unpack('V').join.to_i

        # If we do, in fact, have compression
        if signature == 'CWS'
          # Decompress the body of the SWF
          body = Zlib::Inflate.inflate( contents[8..length] )

          # And reconstruct the stream contents to the first 8 bytes (header)
          # Plus our decompressed body
          contents = contents[0..7] + body
        end

        # Determine the nbits of our dimensions rectangle
        nbits =contents.unpack('c'*contents.length)[8] >> 3<commit_msg>Nbits determination was using 'c' for a signed integer, should've been 'C' for unsigned int.<commit_after># encoding: ascii-8bit
require 'zlib'

class ImageSpec
  module Parser
    class SWF

      CONTENT_TYPE = 'application/x-shockwave-flash'

      def self.attributes(stream)
        width, height = dimensions(stream)
        {:width => width, :height => height, :content_type => CONTENT_TYPE, :dimensions => [width, height], :file_size => size(stream)}
      end

      def self.detected?(stream)
        stream.rewind
        stream.read(3) =~ /(F|C)WS/ ? true : false
      end

      def self.dimensions(stream)
        # Read the entire stream into memory because the
        # dimensions aren't stored in a standard location
        stream.rewind
        contents = stream.read.force_encoding("ASCII-8BIT")

        # Our 'signature' is the first 3 bytes
        # Either FWS or CWS.  CWS indicates compression
        signature = contents[0..2]

        # Determine the length of the uncompressed stream
        length = contents[4..7].unpack('V').join.to_i

        # If we do, in fact, have compression
        if signature == 'CWS'
          # Decompress the body of the SWF
          body = Zlib::Inflate.inflate( contents[8..length] )

          # And reconstruct the stream contents to the first 8 bytes (header)
          # Plus our decompressed body
          contents = contents[0..7] + body
        end

        # Determine the nbits of our dimensions rectangle
        nbits = contents.unpack('C'*contents.length)[8] >> 3
{'fraction_non_alphanumeric': 0.09740691489361702, 'fraction_numerical': 0.009308510638297872, 'mean_word_length': 2.3847019122609674, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2834168', 'n_tokens_mistral': 875, 'n_tokens_neox': 833, 'n_words': 275}