Skip to content

Commit 54a5a21

Browse files
committed
[MOVEONLY] Turn CScript::GetOp2 into a function and move to cpp
1 parent 6a7456a commit 54a5a21

File tree

3 files changed

+57
-54
lines changed

3 files changed

+57
-54
lines changed

src/core_write.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ std::string FormatScript(const CScript& script)
3434
while (it != script.end()) {
3535
CScript::const_iterator it2 = it;
3636
std::vector<unsigned char> vch;
37-
if (script.GetOp2(it, op, &vch)) {
37+
if (script.GetOp(it, op, vch)) {
3838
if (op == OP_0) {
3939
ret += "0 ";
4040
continue;

src/script/script.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,3 +280,55 @@ bool CScript::HasValidOps() const
280280
}
281281
return true;
282282
}
283+
284+
bool GetScriptOp(CScriptBase::const_iterator& pc, CScriptBase::const_iterator end, opcodetype& opcodeRet, std::vector<unsigned char>* pvchRet)
285+
{
286+
opcodeRet = OP_INVALIDOPCODE;
287+
if (pvchRet)
288+
pvchRet->clear();
289+
if (pc >= end)
290+
return false;
291+
292+
// Read instruction
293+
if (end - pc < 1)
294+
return false;
295+
unsigned int opcode = *pc++;
296+
297+
// Immediate operand
298+
if (opcode <= OP_PUSHDATA4)
299+
{
300+
unsigned int nSize = 0;
301+
if (opcode < OP_PUSHDATA1)
302+
{
303+
nSize = opcode;
304+
}
305+
else if (opcode == OP_PUSHDATA1)
306+
{
307+
if (end - pc < 1)
308+
return false;
309+
nSize = *pc++;
310+
}
311+
else if (opcode == OP_PUSHDATA2)
312+
{
313+
if (end - pc < 2)
314+
return false;
315+
nSize = ReadLE16(&pc[0]);
316+
pc += 2;
317+
}
318+
else if (opcode == OP_PUSHDATA4)
319+
{
320+
if (end - pc < 4)
321+
return false;
322+
nSize = ReadLE32(&pc[0]);
323+
pc += 4;
324+
}
325+
if (end - pc < 0 || (unsigned int)(end - pc) < nSize)
326+
return false;
327+
if (pvchRet)
328+
pvchRet->assign(pc, pc + nSize);
329+
pc += nSize;
330+
}
331+
332+
opcodeRet = static_cast<opcodetype>(opcode);
333+
return true;
334+
}

src/script/script.h

Lines changed: 4 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,8 @@ class CScriptNum
385385
*/
386386
typedef prevector<28, unsigned char> CScriptBase;
387387

388+
bool GetScriptOp(CScriptBase::const_iterator& pc, CScriptBase::const_iterator end, opcodetype& opcodeRet, std::vector<unsigned char>* pvchRet);
389+
388390
/** Serialized script, used inside transaction inputs and outputs */
389391
class CScript : public CScriptBase
390392
{
@@ -495,65 +497,14 @@ class CScript : public CScriptBase
495497

496498
bool GetOp(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet) const
497499
{
498-
return GetOp2(pc, opcodeRet, &vchRet);
500+
return GetScriptOp(pc, end(), opcodeRet, &vchRet);
499501
}
500502

501503
bool GetOp(const_iterator& pc, opcodetype& opcodeRet) const
502504
{
503-
return GetOp2(pc, opcodeRet, nullptr);
505+
return GetScriptOp(pc, end(), opcodeRet, nullptr);
504506
}
505507

506-
bool GetOp2(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>* pvchRet) const
507-
{
508-
opcodeRet = OP_INVALIDOPCODE;
509-
if (pvchRet)
510-
pvchRet->clear();
511-
if (pc >= end())
512-
return false;
513-
514-
// Read instruction
515-
if (end() - pc < 1)
516-
return false;
517-
unsigned int opcode = *pc++;
518-
519-
// Immediate operand
520-
if (opcode <= OP_PUSHDATA4)
521-
{
522-
unsigned int nSize = 0;
523-
if (opcode < OP_PUSHDATA1)
524-
{
525-
nSize = opcode;
526-
}
527-
else if (opcode == OP_PUSHDATA1)
528-
{
529-
if (end() - pc < 1)
530-
return false;
531-
nSize = *pc++;
532-
}
533-
else if (opcode == OP_PUSHDATA2)
534-
{
535-
if (end() - pc < 2)
536-
return false;
537-
nSize = ReadLE16(&pc[0]);
538-
pc += 2;
539-
}
540-
else if (opcode == OP_PUSHDATA4)
541-
{
542-
if (end() - pc < 4)
543-
return false;
544-
nSize = ReadLE32(&pc[0]);
545-
pc += 4;
546-
}
547-
if (end() - pc < 0 || (unsigned int)(end() - pc) < nSize)
548-
return false;
549-
if (pvchRet)
550-
pvchRet->assign(pc, pc + nSize);
551-
pc += nSize;
552-
}
553-
554-
opcodeRet = static_cast<opcodetype>(opcode);
555-
return true;
556-
}
557508

558509
/** Encode/decode small integers: */
559510
static int DecodeOP_N(opcodetype opcode)

0 commit comments

Comments
 (0)