int main (int argc, char *argv[]) { const char *opn = GetOpName(OP_PUBKEY); assert(!strcmp(opn, "OP_PUBKEY")); opn = GetOpName(OP_INVALIDOPCODE); assert(!strcmp(opn, "OP_UNKNOWN")); runtest("data/blk120383.ser"); bitc_key_static_shutdown(); return 0; }
std::string FormatScript(const CScript& script) { std::string ret; CScript::const_iterator it = script.begin(); opcodetype op; while (it != script.end()) { CScript::const_iterator it2 = it; std::vector<unsigned char> vch; if (script.GetOp2(it, op, &vch)) { if (op == OP_0) { ret += "0 "; continue; } else if ((op >= OP_1 && op <= OP_16) || op == OP_1NEGATE) { ret += strprintf("%i ", op - OP_1NEGATE - 1); continue; } else if (op >= OP_NOP && op <= OP_NOP10) { std::string str(GetOpName(op)); if (str.substr(0, 3) == std::string("OP_")) { ret += str.substr(3, std::string::npos) + " "; continue; } } if (vch.size() > 0) { ret += strprintf("0x%x 0x%x ", HexStr(it2, it - vch.size()), HexStr(it - vch.size(), it)); } else { ret += strprintf("0x%x ", HexStr(it2, it)); } continue; } ret += strprintf("0x%x ", HexStr(it2, script.end())); break; } return ret.substr(0, ret.size() - 1); }
void CTDLFindTaskExpressionListCtrl::BuildListCtrl() { DeleteAllItems(); for (int nParam = 0; nParam < GetRuleCount(); nParam++) { const SEARCHPARAM& sp = m_aSearchParams[nParam]; // attrib CString sAttrib = m_cbAttributes.GetAttributeName(sp); int nItem = InsertItem(nParam, sAttrib); // operator CString sOp = GetOpName(sp.GetOperator()); SetItemText(nItem, OPERATOR, sOp); // value UpdateValueColumnText(nItem); // and/or (but not for last row) if (nParam < GetRuleCount() - 1) { CEnString sAndOr(sp.GetAnd() ? IDS_FP_AND : IDS_FP_OR); SetItemText(nItem, ANDOR, sAndOr); } } ValidateListData(); SetCurSel(0); }
/** * Create the assembly string representation of a CScript object. * @param[in] script CScript object to convert into the asm string representation. * @param[in] fAttemptSighashDecode Whether to attempt to decode sighash types on data within the script that matches the format * of a signature. Only pass true for scripts you believe could contain signatures. For example, * pass false, or omit the this argument (defaults to false), for scriptPubKeys. */ std::string ScriptToAsmStr(const CScript& script, const bool fAttemptSighashDecode) { std::string str; opcodetype opcode; std::vector<unsigned char> vch; CScript::const_iterator pc = script.begin(); while (pc < script.end()) { if (!str.empty()) { str += " "; } if (!script.GetOp(pc, opcode, vch)) { str += "[error]"; return str; } if (0 <= opcode && opcode <= OP_PUSHDATA4) { if (vch.size() <= static_cast<std::vector<unsigned char>::size_type>(4)) { str += strprintf("%d", CScriptNum(vch, false).getint()); } else { // the IsUnspendable check makes sure not to try to decode OP_RETURN data that may match the format of a signature if (fAttemptSighashDecode && !script.IsUnspendable()) { std::string strSigHashDecode; // goal: only attempt to decode a defined sighash type from data that looks like a signature within a scriptSig. // this won't decode correctly formatted public keys in Pubkey or Multisig scripts due to // the restrictions on the pubkey formats (see IsCompressedOrUncompressedPubKey) being incongruous with the // checks in CheckSignatureEncoding. uint32_t flags = SCRIPT_VERIFY_STRICTENC; if (vch.back() & SIGHASH_FORKID) { // If the transaction is using SIGHASH_FORKID, we need // to set the apropriate flag. // TODO: Remove after the Hard Fork. flags |= SCRIPT_ENABLE_SIGHASH_FORKID; } if (CheckSignatureEncoding(vch, flags, NULL)) { const uint8_t chSigHashType = vch.back() & 0xBF; if (mapSigHashTypes.count(chSigHashType)) { const bool forkIdSet = (vch.back() & SIGHASH_FORKID) == SIGHASH_FORKID; strSigHashDecode = "[" + mapSigHashTypes.find(chSigHashType)->second + (forkIdSet ? "|FORKID]" : "]"); vch.pop_back(); // remove the sighash type byte. it will be replaced by the decode. } } str += HexStr(vch) + strSigHashDecode; } else { str += HexStr(vch); } } } else { str += GetOpName(opcode); } } return str; }
int CTDLFindTaskExpressionListCtrl::InsertRule(int nRow, const SEARCHPARAM& sp) { m_aSearchParams.InsertAt(nRow, SEARCHPARAM(sp)); CString sItem = m_cbAttributes.GetAttributeName(sp); int nNew = InsertItem(nRow, sItem); SetItemText(nNew, OPERATOR, GetOpName(sp.GetOperator())); UpdateValueColumnText(nRow); // Fixup And/or column RefreshAndOrColumnText(); return nNew; }
/** * Create the assembly string representation of a CScript object. * @param[in] script CScript object to convert into the asm string representation. * @param[in] fAttemptSighashDecode Whether to attempt to decode sighash types on data within the script that matches the format * of a signature. Only pass true for scripts you believe could contain signatures. For example, * pass false, or omit the this argument (defaults to false), for scriptPubKeys. */ std::string ScriptToAsmStr(const CScript& script, const bool fAttemptSighashDecode) { std::string str; opcodetype opcode; std::vector<unsigned char> vch; CScript::const_iterator pc = script.begin(); while (pc < script.end()) { if (!str.empty()) { str += " "; } if (!script.GetOp(pc, opcode, vch)) { str += "[error]"; return str; } if (0 <= opcode && opcode <= OP_PUSHDATA4) { if (vch.size() <= static_cast<std::vector<unsigned char>::size_type>(4)) { str += strprintf("%d", CScriptNum(vch, false).getint()); } else { // the IsUnspendable check makes sure not to try to decode OP_RETURN data that may match the format of a signature if (fAttemptSighashDecode && !script.IsUnspendable()) { std::string strSigHashDecode; // goal: only attempt to decode a defined sighash type from data that looks like a signature within a scriptSig. // this won't decode correctly formatted public keys in Pubkey or Multisig scripts due to // the restrictions on the pubkey formats (see IsCompressedOrUncompressedPubKey) being incongruous with the // checks in CheckSignatureEncoding. if (CheckSignatureEncoding(vch, SCRIPT_VERIFY_STRICTENC | SCRIPT_ALLOW_NON_FORKID, nullptr)) { const unsigned char chSigHashType = vch.back(); if (mapSigHashTypes.count(chSigHashType)) { strSigHashDecode = "[" + mapSigHashTypes.find(chSigHashType)->second + "]"; vch.pop_back(); // remove the sighash type byte. it will be replaced by the decode. } } str += HexStr(vch) + strSigHashDecode; } else { str += HexStr(vch); } } } else { str += GetOpName(opcode); } } return str; }
int CTDLFindTaskExpressionListCtrl::InsertRule(int nRow, const SEARCHPARAM& sp) { m_aSearchParams.InsertAt(nRow, sp); CString sItem = m_cbAttributes.GetAttributeName(sp); int nNew = InsertItem(nRow, sItem); SetItemText(nNew, OPERATOR, GetOpName(sp.GetOperator())); UpdateValueColumnText(nRow); // omit and/or for last rule if (nRow < GetRuleCount() - 1) { CEnString sAndOr(sp.GetAnd() ? IDS_FP_AND : IDS_FP_OR); SetItemText(nNew, ANDOR, sAndOr); } return nNew; }
void CTDLFindTaskExpressionListCtrl::ValidateListData() const { #ifdef _DEBUG for (int nRule = 0; nRule < GetRuleCount(); nRule++) { const SEARCHPARAM& rule = m_aSearchParams[nRule]; // check matching attribute text CString sRuleAttrib = m_cbAttributes.GetAttributeName(rule); CString sListAttrib = GetItemText(nRule, ATTRIB); ASSERT (sRuleAttrib == sListAttrib); // check matching operator text CString sRuleOp = GetOpName(rule.GetOperator()); CString sListOp = GetItemText(nRule, OPERATOR); ASSERT (sListOp.IsEmpty() || sRuleOp == sListOp); // check valid operator ASSERT(rule.HasValidOperator()); } #endif }
void CTDLFindTaskExpressionListCtrl::AddOperatorToCombo(FIND_OPERATOR nOp) { int i = m_cbOperators.AddString(GetOpName(nOp)); m_cbOperators.SetItemData(i, (DWORD)nOp); }
int main( ) { PrettyPrint pp; ClassAdParser parser; string buffer = ""; string condString = "( ( MemoryRequirements < 234 ) || ( MemoryRequirements =?= undefined ) )"; cout << "-------------" << endl; cout << "BOOLEXPR TEST" << endl; cout << "-------------" << endl; cout << "condString = " << condString << endl; cout << endl; ExprTree *condTree = NULL; if( !( condTree = parser.ParseExpression( condString ) ) ) { cerr << "error parsing expression" << endl; } Condition *cond = new Condition( ); if( !( BoolExpr::ExprToCondition( condTree, cond ) ) ) { cerr << "error with ExprToCondition" << endl; } cond->ToString( buffer ); cout << "cond.ToString( ) = " << buffer << endl; buffer = ""; cout << endl; string attr; Operation::OpKind op1 = Operation::__NO_OP__; Operation::OpKind op2 = Operation::__NO_OP__; Value val1, val2; Value::ValueType type; cond->GetAttr( attr ); cout << "attr = " << attr << endl; cond->GetOp( op1 ); GetOpName( op1, buffer ); cout << "op1 = " << buffer << endl; cout << "op1 is op number " << (int)op1 << endl; buffer = ""; cond->GetOp2( op2 ); GetOpName( op2, buffer ); cout << "op2 = " << buffer << endl; buffer = ""; cond->GetVal( val1 ); pp.Unparse( buffer, val1 ); cout << "val1 = " << buffer << endl; buffer = ""; cond->GetVal2( val2 ); pp.Unparse( buffer, val2 ); cout << "val2 = " << buffer << endl; buffer = ""; cond->GetType( type ); GetTypeName( type, buffer ); cout << "type = " << buffer << endl; buffer = ""; }
CScript ParseScript(const std::string& s) { CScript result; static std::map<std::string, opcodetype> mapOpNames; if (mapOpNames.empty()) { for (unsigned int op = 0; op <= MAX_OPCODE; op++) { // Allow OP_RESERVED to get into mapOpNames if (op < OP_NOP && op != OP_RESERVED) continue; const char* name = GetOpName((opcodetype)op); if (strcmp(name, "OP_UNKNOWN") == 0) continue; std::string strName(name); mapOpNames[strName] = (opcodetype)op; // Convenience: OP_ADD and just ADD are both recognized: boost::algorithm::replace_first(strName, "OP_", ""); mapOpNames[strName] = (opcodetype)op; } } std::vector<std::string> words; boost::algorithm::split(words, s, boost::algorithm::is_any_of(" \t\n"), boost::algorithm::token_compress_on); for (std::vector<std::string>::const_iterator w = words.begin(); w != words.end(); ++w) { if (w->empty()) { // Empty string, ignore. (boost::split given '' will return one word) } else if (all(*w, boost::algorithm::is_digit()) || (boost::algorithm::starts_with(*w, "-") && all(std::string(w->begin()+1, w->end()), boost::algorithm::is_digit()))) { // Number int64_t n = atoi64(*w); result << n; } else if (boost::algorithm::starts_with(*w, "0x") && (w->begin()+2 != w->end()) && IsHex(std::string(w->begin()+2, w->end()))) { // Raw hex data, inserted NOT pushed onto stack: std::vector<unsigned char> raw = ParseHex(std::string(w->begin()+2, w->end())); result.insert(result.end(), raw.begin(), raw.end()); } else if (w->size() >= 2 && boost::algorithm::starts_with(*w, "'") && boost::algorithm::ends_with(*w, "'")) { // Single-quoted string, pushed as data. NOTE: this is poor-man's // parsing, spaces/tabs/newlines in single-quoted strings won't work. std::vector<unsigned char> value(w->begin()+1, w->end()-1); result << value; } else if (mapOpNames.count(*w)) { // opcode, e.g. OP_ADD or ADD: result << mapOpNames[*w]; } else { throw std::runtime_error("script parse error"); } } return result; }