@param string $buffer

@return int
185517
private function readMessages(string $buffer) : int
    {
        $emitted_messages = 0;
        $i = 0;
        while (($buffer[$i] ?? '') !== '') {
            $this->buffer .= $buffer[$i++];
            switch ($this->parsing_mode) {
                case self::PARSE_HEADERS:
                    if ($this->buffer === "\r\n") {
                        $this->parsing_mode = self::PARSE_BODY;
                        $this->content_length = (int)$this->headers['Content-Length'];
                        $this->buffer = '';
                    } elseif (substr($this->buffer, -2) === "\r\n") {
                        $parts = explode(':', $this->buffer);
                        $this->headers[$parts[0]] = trim($parts[1]);
                        $this->buffer = '';
                    }
                    break;
                case self::PARSE_BODY:
                    if (strlen($this->buffer) === $this->content_length) {
                        if (!$this->is_accepting_new_requests) {
                            // If we fork, don't read any bytes in the input buffer from the worker process.
                            $this->emitClose();
                            return $emitted_messages;
                        }
                        // MessageBody::parse can throw an Error, maybe log an error?
                        try {
                            $msg = new Message(MessageBody::parse($this->buffer), $this->headers);
                        } catch (Exception $_) {
                            $msg = null;
                        }
                        if ($msg) {
                            $emitted_messages++;
                            $this->emit('message', [$msg]);
                            /** @psalm-suppress DocblockTypeContradiction */
                            if (!$this->is_accepting_new_requests) {
                                // If we fork, don't read any bytes in the input buffer from the worker process.
                                $this->emitClose();
                                return $emitted_messages;
                            }
                        }
                        $this->parsing_mode = self::PARSE_HEADERS;
                        $this->headers = [];
                        $this->buffer = '';
                    }
                    break;
            }
        }
        return $emitted_messages;
    }
564442
protected function processRules(Buffer $buffer)
    {

        // Set the result to a new result instance
        $result = new Result();

        // Number of rules
        $result->add('num_rules', $buffer->readInt16());

        // Run until we run out of buffer
        while ($buffer->getLength()) {
            $result->add($buffer->readPascalString(), $buffer->readPascalString());
        }

        unset($buffer);

        return $result->fetch();
    }
243168
public function shouldBuffer($buffer)
    {
        if ('' === $buffer) {
            return false;
        }

        $endOfBuffer = substr($buffer, -1);
        foreach ((array) $this->search as $needle) {
            if (false !== strpos($needle, $endOfBuffer)) {
                return true;
            }
        }

        return false;
    }
1031268
public function consumeBits(Buffer $buffer, $n)
    {
        $bits = [];
        $value = $buffer->consumeUint8();
        for ($i = 0; $i < $n; ++$i) {
            $bits[] = ($value & (1 << $i)) > 0;
        }
        return $bits;
    }
795690
public function load($buffer){
        $this->fin = ord($buffer[0]) >> 7;
        $this->opcode = ord($buffer[0]) & (pow(2,4)-1);
        $this->mask = ord($buffer[1]) >> 7;
        if($this->isCtlFrame()) return ;
        
        $this->payload_len = ord($buffer[1]) & 127;
        
        $ext_bytes = 0;
        if($this->payload_len < 126) {
            $this->payload_ext_len = 0;
        }elseif($this->payload_len === 126 ) {
            $this->payload_ext_len = unpack('n', substr($buffer,2,2));
            $ext_bytes=2;
        }else if($this->payload_len === 127) {
            $this->payload_ext_len = unpack('J', substr($buffer,2,8));
            $ext_bytes=8;
        }        
        
        if($this->mask) {
            $this->mask_key = substr($buffer, $ext_bytes+2, 4);
            $data = substr($buffer, $ext_bytes+6);
            for ($index = 0; $index < strlen($data); $index++) {
                $this->body .= $data[$index] ^ $this->mask_key[$index % 4];
            }
            
        }else{
            $this->mask_key = '';
            $this->body = substr($buffer, $ext_bytes+2);
        }
    }
300735
public function parse($buffer)
    {
        // strip BOM (byte order marker)
        if (substr($buffer, 0, 3) === "\xef\xbb\xbf") {
            $buffer = substr($buffer, 3);
        }

        $this->buffer          = rtrim($buffer, "\x00..\x1f");
        $this->count           = 0;
        $this->env             = null;
        $this->inParens        = false;
        $this->eatWhiteDefault = true;

        $this->saveEncoding();
        $this->extractLineNumbers($buffer);

        $this->pushBlock(null); // root block
        $this->whitespace();
        $this->pushBlock(null);
        $this->popBlock();

        while ($this->parseChunk()) {
            ;
        }

        if ($this->count !== strlen($this->buffer)) {
            $this->throwParseError();
        }

        if (! empty($this->env->parent)) {
            $this->throwParseError('unclosed block');
        }

        if ($this->charset) {
            array_unshift($this->env->children, $this->charset);
        }

        $this->env->isRoot    = true;

        $this->restoreEncoding();

        return $this->env;
    }
493567
public static function charCount($char): int
    {
        self::handleIncomingChar($char);
        $stringChar = (string) $char;

        return mb_ord($stringChar, mb_detect_encoding($stringChar)) > 0xFFFF ? 2 : 1;
    }
828502
protected function processStatus(Buffer $buffer)
    {
        // Set the result to a new result instance
        $result = new Result();

        // Lets peek and see if the data starts with a \
        if ($buffer->lookAhead(1) == '\\') {
            // Burn the first one
            $buffer->skip(1);
        }

        // Explode the data
        $data = explode('\\', $buffer->getBuffer());

        // No longer needed
        unset($buffer);

        $itemCount = count($data);

        // Now lets loop the array
        for ($x = 0; $x < $itemCount; $x += 2) {
            // Set some local vars
            $key = $data[$x];
            $val = $data[$x + 1];

            if (in_array($key, ['challenge'])) {
                continue; // skip
            }

            // Regular variable so just add the value.
            $result->add($key, $val);
        }

        /*var_dump($data);
        var_dump($result->fetch());

        exit;*/

        return $result->fetch();
    }
1228597
public function writeLine(string $bytes): int
    {
        $isAcceptable = $this->predicate;
        if ($isAcceptable($bytes)) {
            return $this->outputStream->writeLine($bytes);
        }

        return 0;
    }
925302
public function compress($buffer)
    {
        $dictionary = array();
        $data       = str_split($buffer . '');
        $out        = array();
        $phrase     = $data[0];
        $code       = 256;
        $countData  = count($data);

        for ($i = 1; $i < $countData; ++$i) {
            $currentCharacter = $data[$i];

            if (isset($dictionary[$phrase.$currentCharacter])) {
                $phrase .= $currentCharacter;

            } else {
                $out[] = strlen($phrase) > 1 ? $dictionary[$phrase] : $this->charCodeAt($phrase,0);
                $dictionary[$phrase.$currentCharacter] = $code;
                $code++;
                $phrase = $currentCharacter;
            }
        }

        $out[]    = strlen($phrase) > 1 ? $dictionary[$phrase] : $this->charCodeAt($phrase,0);
        $countOut = count($out);

        for ($i = 0; $i < $countOut; ++$i) {
            $out[$i] = $this->unichr($out[$i]);
        }

        return implode('', $out);
    }
1065489
public static function len($string): int
    {
        if (self::isMBString()) {
            return mb_strlen($string, self::$encoding);
        }

        return strlen($string); // @codeCoverageIgnore
    }
636464
public function consumeFieldValue(Buffer $buffer)
    {
        $fieldType = $buffer->consumeUint8();

        switch ($fieldType) {
            case Constants::FIELD_BOOLEAN:
                return $buffer->consumeUint8() > 0;
            case Constants::FIELD_SHORT_SHORT_INT:
                return $buffer->consumeInt8();
            case Constants::FIELD_SHORT_SHORT_UINT:
                return $buffer->consumeUint8();
            case Constants::FIELD_SHORT_INT:
                return $buffer->consumeInt16();
            case Constants::FIELD_SHORT_UINT:
                return $buffer->consumeUint16();
            case Constants::FIELD_LONG_INT:
                return $buffer->consumeInt32();
            case Constants::FIELD_LONG_UINT:
                return $buffer->consumeUint32();
            case Constants::FIELD_LONG_LONG_INT:
                return $buffer->consumeInt64();
            case Constants::FIELD_LONG_LONG_UINT:
                return $buffer->consumeUint64();
            case Constants::FIELD_FLOAT:
                return $buffer->consumeFloat();
            case Constants::FIELD_DOUBLE:
                return $buffer->consumeDouble();
            case Constants::FIELD_DECIMAL_VALUE:
                return $this->consumeDecimalValue($buffer);
            case Constants::FIELD_SHORT_STRING:
                return $buffer->consume($buffer->consumeUint8());
            case Constants::FIELD_LONG_STRING:
                return $buffer->consume($buffer->consumeUint32());
            case Constants::FIELD_ARRAY:
                return $this->consumeArray($buffer);
            case Constants::FIELD_TIMESTAMP:
                return $this->consumeTimestamp($buffer);
            case Constants::FIELD_TABLE:
                return $this->consumeTable($buffer);
            case Constants::FIELD_NULL:
                return null;

            default:
                throw new ProtocolException(
                    sprintf("Unhandled field type 0x%02x", $fieldType) .
                    (ctype_print(chr($fieldType)) ? " ('" . chr($fieldType) . "')" : "") .
                    "."
                );
        }
    }
1353386
public function consumeDecimalValue(Buffer $buffer)
    {
        $scale = $buffer->consumeUint8();
        $value = $buffer->consumeUint32();
        return $value * pow(10, $scale);
    }
687307
public function count(string $column = '*'): int
    {
        $select = clone $this;

        //To be escaped in compiler
        $select->columns = ["COUNT({$column})"];
        $select->ordering = [];
        $select->grouping = [];

        $st = $select->run();
        try {
            return (int)$st->fetchColumn();
        } finally {
            $st->close();
        }
    }
422947
public function setBuffer($buffer)
    {
        $this->buffer = $buffer;

        $this->seq       = $this->buffer->readShort(self::SEQ_INDEX);
        $this->timestamp = $this->buffer->readInt(self::TIMESTAMP_INDEX);
        $this->ssrc      = $this->buffer->readInt(self::SSRC_INDEX);

        return $this;
    }
89388
public function concat(Buffer $buffer)
    {
        // size-protected through Buffer class
        $this->buffer = $this->buffer . $buffer->getBinary();
        $this->size  += $buffer->getSize();
        return $this;
    }
1353812
protected function processRules(Buffer $buffer)
    {

        // Set the result to a new result instance
        $result = new Result();

        // Named values
        $inc = -1;
        while ($buffer->getLength()) {
            // Grab the key
            $key = $buffer->readPascalString(1);

            // Make sure mutators don't overwrite each other
            if ($key === 'Mutator') {
                $key .= ++$inc;
            }

            $result->add(strtolower($key), utf8_encode($buffer->readPascalString(1)));
        }

        unset($buffer);

        return $result->fetch();
    }