| 277 | } |
| 278 | |
| 279 | bool GetScriptOp(CScriptBase::const_iterator& pc, CScriptBase::const_iterator end, opcodetype& opcodeRet, std::vector<unsigned char>* pvchRet) |
| 280 | { |
| 281 | opcodeRet = OP_INVALIDOPCODE; |
| 282 | if (pvchRet) |
| 283 | pvchRet->clear(); |
| 284 | if (pc >= end) |
| 285 | return false; |
| 286 | |
| 287 | // Read instruction |
| 288 | if (end - pc < 1) |
| 289 | return false; |
| 290 | unsigned int opcode = *pc++; |
| 291 | |
| 292 | // Immediate operand |
| 293 | if (opcode <= OP_PUSHDATA4) |
| 294 | { |
| 295 | unsigned int nSize = 0; |
| 296 | if (opcode < OP_PUSHDATA1) |
| 297 | { |
| 298 | nSize = opcode; |
| 299 | } |
| 300 | else if (opcode == OP_PUSHDATA1) |
| 301 | { |
| 302 | if (end - pc < 1) |
| 303 | return false; |
| 304 | nSize = *pc++; |
| 305 | } |
| 306 | else if (opcode == OP_PUSHDATA2) |
| 307 | { |
| 308 | if (end - pc < 2) |
| 309 | return false; |
| 310 | nSize = ReadLE16(&pc[0]); |
| 311 | pc += 2; |
| 312 | } |
| 313 | else if (opcode == OP_PUSHDATA4) |
| 314 | { |
| 315 | if (end - pc < 4) |
| 316 | return false; |
| 317 | nSize = ReadLE32(&pc[0]); |
| 318 | pc += 4; |
| 319 | } |
| 320 | if (end - pc < 0 || (unsigned int)(end - pc) < nSize) |
| 321 | return false; |
| 322 | if (pvchRet) |
| 323 | pvchRet->assign(pc, pc + nSize); |
| 324 | pc += nSize; |
| 325 | } |
| 326 | |
| 327 | opcodeRet = static_cast<opcodetype>(opcode); |
| 328 | return true; |
| 329 | } |