| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace BitWasp\Buffertools; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use BitWasp\Buffertools\Exceptions\ParserOutOfRange; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Mdanter\Ecc\EccFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Mdanter\Ecc\Math\GmpMathInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | class Parser | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     private $string; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * @var \Mdanter\Ecc\Math\GmpMathInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     private $math; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @var int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     private $position = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * Instantiate class, optionally taking Buffer or HEX. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @param null|string|BufferInterface $input | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @param GmpMathInterface|null $math | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     public function __construct($input = null, GmpMathInterface $math = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $this->math = $math ?: EccFactory::getAdapter(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         if (!$input instanceof BufferInterface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             $input = Buffer::hex($input, null, $this->math); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $this->string = $input->getBinary(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $this->position = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * Get the position pointer of the parser - ie, how many bytes from 0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     public function getPosition() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         return $this->position; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * Parse $bytes bytes from the string, and return the obtained buffer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @param  integer $bytes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @param  bool $flipBytes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @return Buffer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     public function readBytes($bytes, $flipBytes = false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $string = substr($this->string, $this->getPosition(), $bytes); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $length = strlen($string); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         if ($length == 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             throw new ParserOutOfRange('Could not parse string of required length (empty)'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         } elseif ($length < $bytes) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             throw new ParserOutOfRange('Could not parse string of required length (too short)'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $this->position += $bytes; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         if ($flipBytes) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             $string = Buffertools::flipBytes($string); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         return new Buffer($string, $length, $this->math); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * Write $data as $bytes bytes. Can be flipped if needed. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * @param  integer $bytes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * @param  $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * @param  bool $flipBytes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     public function writeBytes($bytes, $data, $flipBytes = false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         // Treat $data to ensure it's a buffer, with the correct size | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         if ($data instanceof SerializableInterface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             $data = $data->getBuffer(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         if (is_string($data)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             // Convert to a buffer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             $data = Buffer::hex($data, $bytes, $this->math); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         } else if (!($data instanceof BufferInterface)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             throw new \RuntimeException('Invalid data passed to Parser::writeBytes'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         $this->writeBuffer($bytes, $data, $flipBytes); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * Write $data as $bytes bytes. Can be flipped if needed. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * @param  integer $bytes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * @param  string $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * @param  bool $flipBytes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     public function writeRawBinary($bytes, $data, $flipBytes = false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         return $this->writeBuffer($bytes, new Buffer($data, $bytes), $flipBytes); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      * @param BufferInterface $buffer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      * @param bool $flipBytes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      * @param int $bytes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     public function writeBuffer($bytes, BufferInterface $buffer, $flipBytes = false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         // only create a new buffer if the size does not match | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         if ($buffer->getSize() != $bytes) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |             $buffer = new Buffer($buffer->getBinary(), $bytes, $this->math); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         $this->appendBuffer($buffer, $flipBytes); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |      * @param BufferInterface $buffer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |      * @param bool $flipBytes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |      * @return $this | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 144 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 145 |  |  |     public function appendBuffer(BufferInterface $buffer, $flipBytes = false) | 
            
                                                                        
                            
            
                                    
            
            
                | 146 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 147 |  |  |         if ($flipBytes) { | 
            
                                                                        
                            
            
                                    
            
            
                | 148 |  |  |             $buffer = $buffer->flip(); | 
            
                                                                        
                            
            
                                    
            
            
                | 149 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 151 |  |  |         $this->string .= $buffer->getBinary(); | 
            
                                                                        
                            
            
                                    
            
            
                | 152 |  |  |         return $this; | 
            
                                                                        
                            
            
                                    
            
            
                | 153 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |      * Take an array containing serializable objects. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |      * @param SerializableInterface[]|Buffer[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |     public function writeArray($serializable) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         $parser = new Parser(Buffertools::numToVarInt(count($serializable)), $this->math); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         foreach ($serializable as $object) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |             if ($object instanceof SerializableInterface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |                 $object = $object->getBuffer(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |             if ($object instanceof BufferInterface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |                 $parser->writeBytes($object->getSize(), $object); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |                 throw new \RuntimeException('Input to writeArray must be Buffer[], or SerializableInterface[]'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |         $this->string .= $parser->getBuffer()->getBinary(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |      * Return the string as a buffer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |      * @return BufferInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |     public function getBuffer() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |         return new Buffer($this->string, null, $this->math); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 189 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 190 |  |  |  | 
            
                        
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.