<commit_before># encoding: BINARY

module EventMachine
  module WebSocket
    module Framing07
      
      def initialize_framing
        @data = MaskedString.new
        @application_data_buffer = '' # Used for MORE frames
      end
      
      def process_data(newdata)
        error = false

        while !error && @data.size >= 2
          pointer = 0

          fin = (@data.getbyte(pointer) & 0b10000000) == 0b10000000
          # Ignoring rsv1-3 for now
          opcode = @data.getbyte(pointer) & 0b00001111
          pointer += 1

          mask = (@data.getbyte(pointer) & 0b10000000) == 0b10000000
          length = @data.getbyte(pointer) & 0b01111111
          pointer += 1

          # raise WebSocketError, 'Data from client must be masked' unless mask

          payload_length = case length
          when 127 # Length defined by 8 bytes
            # Check buffer size
            if @data.getbyte(pointer+8-1) == nil
              debug [:buffer_incomplete, @data]
              error = true
              next
            end
            
            # Only using the last 4 bytes for now, till I work out how to
            # unpack 8 bytes. I'm sure 4GB frames will do for now :)
            l = @data.getbytes(pointer+4, 4).unpack('N').first
            pointer += 8
            l
          when 126 # Length defined by 2 bytes
            # Check buffer size
            if @data.getbyte(pointer+2-1) == nil
              debug [:buffer_incomplete, @data]
              error = true
              next
            end
            
            l = @data.getbytes(pointer, 2).unpack('n').first
            pointer += 2
            l
          else
            length
          end

          # Compute the expected frame length
          frame_length = pointer + payload_length
          frame_length += 4 if mask

          # Check buffer size
          if @data.getbyte(frame_length - 1) == nil
            debug [:buffer_incomplete, @data]
            error = true
            next
          end

          # Remove frame header
          @data.slice!(0...pointer)
          pointer = 0

          # Read application data (unmasked if required)
          @data.read_mask if mask
          pointer += 4 if mask
          application_data = @data.getbytes(pointer, payload_length)
          pointer += payload_length
          @data.unset_mask if mask
          
          # Throw away data up to pointer
          @data.slice!(0...pointer)<commit_msg>Add frame length check to Framing07<commit_after># encoding: BINARY

module EventMachine
  module WebSocket
    module Framing07
      
      def initialize_framing
        @data = MaskedString.new
        @application_data_buffer = '' # Used for MORE frames
      end
      
      def process_data(newdata)
        error = false

        while !error && @data.size >= 2
          pointer = 0

          fin = (@data.getbyte(pointer) & 0b10000000) == 0b10000000
          # Ignoring rsv1-3 for now
          opcode = @data.getbyte(pointer) & 0b00001111
          pointer += 1

          mask = (@data.getbyte(pointer) & 0b10000000) == 0b10000000
          length = @data.getbyte(pointer) & 0b01111111
          pointer += 1

          # raise WebSocketError, 'Data from client must be masked' unless mask

          payload_length = case length
          when 127 # Length defined by 8 bytes
            # Check buffer size
            if @data.getbyte(pointer+8-1) == nil
              debug [:buffer_incomplete, @data]
              error = true
              next
            end
            
            # Only using the last 4 bytes for now, till I work out how to
            # unpack 8 bytes. I'm sure 4GB frames will do for now :)
            l = @data.getbytes(pointer+4, 4).unpack('N').first
            pointer += 8
            l
          when 126 # Length defined by 2 bytes
            # Check buffer size
            if @data.getbyte(pointer+2-1) == nil
              debug [:buffer_incomplete, @data]
              error = true
              next
            end
            
            l = @data.getbytes(pointer, 2).unpack('n').first
            pointer += 2
            l
          else
            length
          end

          # Compute the expected frame length
          frame_length = pointer + payload_length
          frame_length += 4 if mask

          if frame_length > @connection.max_frame_size
            raise WSMessageTooBigError, "Frame length too long (#{frame_length} bytes)"
          end

          # Check buffer size
          if @data.getbyte(frame_length - 1) == nil
            debug [:buffer_incomplete, @data]
            error = true
            next
          end

          # Remove frame header
          @data.slice!(0...pointer)
          pointer = 0

          # Read application data (unmasked if required)
          @data.read_mask if mask
          pointer += 4 if mask
          application_data = @data.getbytes(pointer, payload_length)
          pointer += payload_length
          @data.unset_mask if mask
          
          # Throw away data up to pointer
          @data.slice!(0...pointer)
{'fraction_non_alphanumeric': 0.07612928348909657, 'fraction_numerical': 0.03465732087227414, 'mean_word_length': 1.4685247477174435, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 10, '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': '1658385', 'n_tokens_mistral': 1483, 'n_tokens_neox': 1321, 'n_words': 424}