libbtc
bitcoinclibrary
Data Structures | Typedefs | Enumerations | Functions
script.h File Reference
#include "btc.h"
#include "cstr.h"
#include "ecc_key.h"
#include "vector.h"

Go to the source code of this file.

Data Structures

struct  btc_script_op_
 

Typedefs

typedef struct btc_script_op_ btc_script_op
 

Enumerations

enum  { SIGHASH_ALL = 1, SIGHASH_NONE = 2, SIGHASH_SINGLE = 3, SIGHASH_ANYONECANPAY = 0x80 }
 
enum  opcodetype {
  OP_0 = 0x00, OP_FALSE = OP_0, OP_PUSHDATA1 = 0x4c, OP_PUSHDATA2 = 0x4d,
  OP_PUSHDATA4 = 0x4e, OP_1NEGATE = 0x4f, OP_RESERVED = 0x50, OP_1 = 0x51,
  OP_TRUE = OP_1, OP_2 = 0x52, OP_3 = 0x53, OP_4 = 0x54,
  OP_5 = 0x55, OP_6 = 0x56, OP_7 = 0x57, OP_8 = 0x58,
  OP_9 = 0x59, OP_10 = 0x5a, OP_11 = 0x5b, OP_12 = 0x5c,
  OP_13 = 0x5d, OP_14 = 0x5e, OP_15 = 0x5f, OP_16 = 0x60,
  OP_NOP = 0x61, OP_VER = 0x62, OP_IF = 0x63, OP_NOTIF = 0x64,
  OP_VERIF = 0x65, OP_VERNOTIF = 0x66, OP_ELSE = 0x67, OP_ENDIF = 0x68,
  OP_VERIFY = 0x69, OP_RETURN = 0x6a, OP_TOALTSTACK = 0x6b, OP_FROMALTSTACK = 0x6c,
  OP_2DROP = 0x6d, OP_2DUP = 0x6e, OP_3DUP = 0x6f, OP_2OVER = 0x70,
  OP_2ROT = 0x71, OP_2SWAP = 0x72, OP_IFDUP = 0x73, OP_DEPTH = 0x74,
  OP_DROP = 0x75, OP_DUP = 0x76, OP_NIP = 0x77, OP_OVER = 0x78,
  OP_PICK = 0x79, OP_ROLL = 0x7a, OP_ROT = 0x7b, OP_SWAP = 0x7c,
  OP_TUCK = 0x7d, OP_CAT = 0x7e, OP_SUBSTR = 0x7f, OP_LEFT = 0x80,
  OP_RIGHT = 0x81, OP_SIZE = 0x82, OP_INVERT = 0x83, OP_AND = 0x84,
  OP_OR = 0x85, OP_XOR = 0x86, OP_EQUAL = 0x87, OP_EQUALVERIFY = 0x88,
  OP_RESERVED1 = 0x89, OP_RESERVED2 = 0x8a, OP_1ADD = 0x8b, OP_1SUB = 0x8c,
  OP_2MUL = 0x8d, OP_2DIV = 0x8e, OP_NEGATE = 0x8f, OP_ABS = 0x90,
  OP_NOT = 0x91, OP_0NOTEQUAL = 0x92, OP_ADD = 0x93, OP_SUB = 0x94,
  OP_MUL = 0x95, OP_DIV = 0x96, OP_MOD = 0x97, OP_LSHIFT = 0x98,
  OP_RSHIFT = 0x99, OP_BOOLAND = 0x9a, OP_BOOLOR = 0x9b, OP_NUMEQUAL = 0x9c,
  OP_NUMEQUALVERIFY = 0x9d, OP_NUMNOTEQUAL = 0x9e, OP_LESSTHAN = 0x9f, OP_GREATERTHAN = 0xa0,
  OP_LESSTHANOREQUAL = 0xa1, OP_GREATERTHANOREQUAL = 0xa2, OP_MIN = 0xa3, OP_MAX = 0xa4,
  OP_WITHIN = 0xa5, OP_RIPEMD160 = 0xa6, OP_SHA1 = 0xa7, OP_SHA256 = 0xa8,
  OP_HASH160 = 0xa9, OP_HASH256 = 0xaa, OP_CODESEPARATOR = 0xab, OP_CHECKSIG = 0xac,
  OP_CHECKSIGVERIFY = 0xad, OP_CHECKMULTISIG = 0xae, OP_CHECKMULTISIGVERIFY = 0xaf, OP_NOP1 = 0xb0,
  OP_NOP2 = 0xb1, OP_CHECKLOCKTIMEVERIFY = OP_NOP2, OP_NOP3 = 0xb2, OP_NOP4 = 0xb3,
  OP_NOP5 = 0xb4, OP_NOP6 = 0xb5, OP_NOP7 = 0xb6, OP_NOP8 = 0xb7,
  OP_NOP9 = 0xb8, OP_NOP10 = 0xb9, OP_SMALLINTEGER = 0xfa, OP_PUBKEYS = 0xfb,
  OP_PUBKEYHASH = 0xfd, OP_PUBKEY = 0xfe, OP_INVALIDOPCODE = 0xff
}
 
enum  btc_tx_out_type {
  BTC_TX_NONSTANDARD, BTC_TX_PUBKEY, BTC_TX_PUBKEYHASH, BTC_TX_SCRIPTHASH,
  BTC_TX_MULTISIG
}
 

Functions

btc_bool btc_script_copy_without_op_codeseperator (const cstring *scriptin, cstring *scriptout)
 
LIBBTC_API btc_script_opbtc_script_op_new ()
 
LIBBTC_API void btc_script_op_free (btc_script_op *script_op)
 
void btc_script_op_free_cb (void *data)
 
btc_bool btc_script_get_ops (const cstring *script_in, vector *ops_out)
 
enum btc_tx_out_type btc_script_classify (vector *ops)
 
LIBBTC_API btc_bool btc_script_build_multisig (cstring *script_in, unsigned int required_signatures, vector *pubkeys_chars)
 
LIBBTC_API btc_bool btc_script_build_p2pkh (cstring *script, const uint8_t *hash160)
 
LIBBTC_API btc_bool btc_script_build_p2sh (cstring *script_in, const uint8_t *hash160)
 

Typedef Documentation

typedef struct btc_script_op_ btc_script_op

Enumeration Type Documentation

anonymous enum

Signature hash types/flags

Enumerator
SIGHASH_ALL 
SIGHASH_NONE 
SIGHASH_SINGLE 
SIGHASH_ANYONECANPAY 

Definition at line 41 of file script.h.

41  {
42  SIGHASH_ALL = 1,
43  SIGHASH_NONE = 2,
44  SIGHASH_SINGLE = 3,
45  SIGHASH_ANYONECANPAY = 0x80,
46 };
Enumerator
BTC_TX_NONSTANDARD 
BTC_TX_PUBKEY 
BTC_TX_PUBKEYHASH 
BTC_TX_SCRIPTHASH 
BTC_TX_MULTISIG 

Definition at line 193 of file script.h.

193  {
195  // 'standard' transaction types:
200 };
enum opcodetype

Script opcodes

Enumerator
OP_0 
OP_FALSE 
OP_PUSHDATA1 
OP_PUSHDATA2 
OP_PUSHDATA4 
OP_1NEGATE 
OP_RESERVED 
OP_1 
OP_TRUE 
OP_2 
OP_3 
OP_4 
OP_5 
OP_6 
OP_7 
OP_8 
OP_9 
OP_10 
OP_11 
OP_12 
OP_13 
OP_14 
OP_15 
OP_16 
OP_NOP 
OP_VER 
OP_IF 
OP_NOTIF 
OP_VERIF 
OP_VERNOTIF 
OP_ELSE 
OP_ENDIF 
OP_VERIFY 
OP_RETURN 
OP_TOALTSTACK 
OP_FROMALTSTACK 
OP_2DROP 
OP_2DUP 
OP_3DUP 
OP_2OVER 
OP_2ROT 
OP_2SWAP 
OP_IFDUP 
OP_DEPTH 
OP_DROP 
OP_DUP 
OP_NIP 
OP_OVER 
OP_PICK 
OP_ROLL 
OP_ROT 
OP_SWAP 
OP_TUCK 
OP_CAT 
OP_SUBSTR 
OP_LEFT 
OP_RIGHT 
OP_SIZE 
OP_INVERT 
OP_AND 
OP_OR 
OP_XOR 
OP_EQUAL 
OP_EQUALVERIFY 
OP_RESERVED1 
OP_RESERVED2 
OP_1ADD 
OP_1SUB 
OP_2MUL 
OP_2DIV 
OP_NEGATE 
OP_ABS 
OP_NOT 
OP_0NOTEQUAL 
OP_ADD 
OP_SUB 
OP_MUL 
OP_DIV 
OP_MOD 
OP_LSHIFT 
OP_RSHIFT 
OP_BOOLAND 
OP_BOOLOR 
OP_NUMEQUAL 
OP_NUMEQUALVERIFY 
OP_NUMNOTEQUAL 
OP_LESSTHAN 
OP_GREATERTHAN 
OP_LESSTHANOREQUAL 
OP_GREATERTHANOREQUAL 
OP_MIN 
OP_MAX 
OP_WITHIN 
OP_RIPEMD160 
OP_SHA1 
OP_SHA256 
OP_HASH160 
OP_HASH256 
OP_CODESEPARATOR 
OP_CHECKSIG 
OP_CHECKSIGVERIFY 
OP_CHECKMULTISIG 
OP_CHECKMULTISIGVERIFY 
OP_NOP1 
OP_NOP2 
OP_CHECKLOCKTIMEVERIFY 
OP_NOP3 
OP_NOP4 
OP_NOP5 
OP_NOP6 
OP_NOP7 
OP_NOP8 
OP_NOP9 
OP_NOP10 
OP_SMALLINTEGER 
OP_PUBKEYS 
OP_PUBKEYHASH 
OP_PUBKEY 
OP_INVALIDOPCODE 

Definition at line 49 of file script.h.

49  {
50  // push value
51  OP_0 = 0x00,
52  OP_FALSE = OP_0,
53  OP_PUSHDATA1 = 0x4c,
54  OP_PUSHDATA2 = 0x4d,
55  OP_PUSHDATA4 = 0x4e,
56  OP_1NEGATE = 0x4f,
57  OP_RESERVED = 0x50,
58  OP_1 = 0x51,
59  OP_TRUE = OP_1,
60  OP_2 = 0x52,
61  OP_3 = 0x53,
62  OP_4 = 0x54,
63  OP_5 = 0x55,
64  OP_6 = 0x56,
65  OP_7 = 0x57,
66  OP_8 = 0x58,
67  OP_9 = 0x59,
68  OP_10 = 0x5a,
69  OP_11 = 0x5b,
70  OP_12 = 0x5c,
71  OP_13 = 0x5d,
72  OP_14 = 0x5e,
73  OP_15 = 0x5f,
74  OP_16 = 0x60,
75 
76  // control
77  OP_NOP = 0x61,
78  OP_VER = 0x62,
79  OP_IF = 0x63,
80  OP_NOTIF = 0x64,
81  OP_VERIF = 0x65,
82  OP_VERNOTIF = 0x66,
83  OP_ELSE = 0x67,
84  OP_ENDIF = 0x68,
85  OP_VERIFY = 0x69,
86  OP_RETURN = 0x6a,
87 
88  // stack ops
89  OP_TOALTSTACK = 0x6b,
90  OP_FROMALTSTACK = 0x6c,
91  OP_2DROP = 0x6d,
92  OP_2DUP = 0x6e,
93  OP_3DUP = 0x6f,
94  OP_2OVER = 0x70,
95  OP_2ROT = 0x71,
96  OP_2SWAP = 0x72,
97  OP_IFDUP = 0x73,
98  OP_DEPTH = 0x74,
99  OP_DROP = 0x75,
100  OP_DUP = 0x76,
101  OP_NIP = 0x77,
102  OP_OVER = 0x78,
103  OP_PICK = 0x79,
104  OP_ROLL = 0x7a,
105  OP_ROT = 0x7b,
106  OP_SWAP = 0x7c,
107  OP_TUCK = 0x7d,
108 
109  // splice ops
110  OP_CAT = 0x7e,
111  OP_SUBSTR = 0x7f,
112  OP_LEFT = 0x80,
113  OP_RIGHT = 0x81,
114  OP_SIZE = 0x82,
115 
116  // bit logic
117  OP_INVERT = 0x83,
118  OP_AND = 0x84,
119  OP_OR = 0x85,
120  OP_XOR = 0x86,
121  OP_EQUAL = 0x87,
122  OP_EQUALVERIFY = 0x88,
123  OP_RESERVED1 = 0x89,
124  OP_RESERVED2 = 0x8a,
125 
126  // numeric
127  OP_1ADD = 0x8b,
128  OP_1SUB = 0x8c,
129  OP_2MUL = 0x8d,
130  OP_2DIV = 0x8e,
131  OP_NEGATE = 0x8f,
132  OP_ABS = 0x90,
133  OP_NOT = 0x91,
134  OP_0NOTEQUAL = 0x92,
135 
136  OP_ADD = 0x93,
137  OP_SUB = 0x94,
138  OP_MUL = 0x95,
139  OP_DIV = 0x96,
140  OP_MOD = 0x97,
141  OP_LSHIFT = 0x98,
142  OP_RSHIFT = 0x99,
143 
144  OP_BOOLAND = 0x9a,
145  OP_BOOLOR = 0x9b,
146  OP_NUMEQUAL = 0x9c,
147  OP_NUMEQUALVERIFY = 0x9d,
148  OP_NUMNOTEQUAL = 0x9e,
149  OP_LESSTHAN = 0x9f,
150  OP_GREATERTHAN = 0xa0,
151  OP_LESSTHANOREQUAL = 0xa1,
152  OP_GREATERTHANOREQUAL = 0xa2,
153  OP_MIN = 0xa3,
154  OP_MAX = 0xa4,
155 
156  OP_WITHIN = 0xa5,
157 
158  // crypto
159  OP_RIPEMD160 = 0xa6,
160  OP_SHA1 = 0xa7,
161  OP_SHA256 = 0xa8,
162  OP_HASH160 = 0xa9,
163  OP_HASH256 = 0xaa,
164  OP_CODESEPARATOR = 0xab,
165  OP_CHECKSIG = 0xac,
166  OP_CHECKSIGVERIFY = 0xad,
167  OP_CHECKMULTISIG = 0xae,
168  OP_CHECKMULTISIGVERIFY = 0xaf,
169 
170  // expansion
171  OP_NOP1 = 0xb0,
172  OP_NOP2 = 0xb1,
174  OP_NOP3 = 0xb2,
175  OP_NOP4 = 0xb3,
176  OP_NOP5 = 0xb4,
177  OP_NOP6 = 0xb5,
178  OP_NOP7 = 0xb6,
179  OP_NOP8 = 0xb7,
180  OP_NOP9 = 0xb8,
181  OP_NOP10 = 0xb9,
182 
183 
184  // template matching params
185  OP_SMALLINTEGER = 0xfa,
186  OP_PUBKEYS = 0xfb,
187  OP_PUBKEYHASH = 0xfd,
188  OP_PUBKEY = 0xfe,
189 
190  OP_INVALIDOPCODE = 0xff,
191 };
Definition: script.h:133
Definition: script.h:62
Definition: script.h:118
Definition: script.h:101
Definition: script.h:154
Definition: script.h:93
Definition: script.h:79
Definition: script.h:72
Definition: script.h:68
Definition: script.h:95
Definition: script.h:132
Definition: script.h:60
Definition: script.h:139
Definition: script.h:66
Definition: script.h:61
Definition: script.h:153
Definition: script.h:74
Definition: script.h:70
Definition: script.h:119
Definition: script.h:63
Definition: script.h:140
Definition: script.h:110
Definition: script.h:65
Definition: script.h:58
Definition: script.h:77
Definition: script.h:137
Definition: script.h:105
Definition: script.h:138
Definition: script.h:83
Definition: script.h:67
Definition: script.h:99
Definition: script.h:92
Definition: script.h:64
Definition: script.h:71
Definition: script.h:120
Definition: script.h:69
Definition: script.h:78
Definition: script.h:59
Definition: script.h:51
Definition: script.h:136
Definition: script.h:73
Definition: script.h:100

Function Documentation

LIBBTC_API btc_bool btc_script_build_multisig ( cstring script_in,
unsigned int  required_signatures,
vector pubkeys_chars 
)

Definition at line 298 of file script.c.

References BTC_ECKEY_COMPRESSED_LENGTH, BTC_ECKEY_UNCOMPRESSED_LENGTH, btc_encode_op_n(), btc_script_append_pushdata(), btc_pubkey_::compressed, cstr_append_buf(), cstr_resize(), vector::data, vector::len, OP_CHECKMULTISIG, and btc_pubkey_::pubkey.

299 {
300  cstr_resize(script_in, 0); //clear script
301 
302  if(required_signatures > 16 || pubkeys_chars->len > 16)
303  return false;
304  enum opcodetype op_req_sig = btc_encode_op_n(required_signatures);
305  cstr_append_buf(script_in, &op_req_sig, 1);
306 
307  int i;
308  for (i = 0; i < (int)pubkeys_chars->len; i++)
309  {
310  btc_pubkey *pkey = pubkeys_chars->data[i];
312  }
313 
314  enum opcodetype op_pub_len = btc_encode_op_n(pubkeys_chars->len);
315  cstr_append_buf(script_in, &op_pub_len, 1);
316 
317  enum opcodetype op_checkmultisig = OP_CHECKMULTISIG;
318  cstr_append_buf(script_in, &op_checkmultisig, 1);
319 
320  return true;
321 }
void ** data
Definition: vector.h:41
static void btc_script_append_pushdata(cstring *script_in, unsigned char *data, size_t datalen)
Definition: script.c:272
#define BTC_ECKEY_COMPRESSED_LENGTH
Definition: btc.h:68
#define BTC_ECKEY_UNCOMPRESSED_LENGTH
Definition: btc.h:67
opcodetype
Definition: script.h:49
btc_bool cstr_resize(cstring *s, size_t new_sz)
Definition: cstr.c:81
static enum opcodetype btc_encode_op_n(int n)
Definition: script.c:257
size_t len
Definition: vector.h:42
btc_bool compressed
Definition: ecc_key.h:46
btc_bool cstr_append_buf(cstring *s, const void *buf, size_t sz)
Definition: cstr.c:106
uint8_t pubkey[BTC_ECKEY_UNCOMPRESSED_LENGTH]
Definition: ecc_key.h:47
LIBBTC_API btc_bool btc_script_build_p2pkh ( cstring script,
const uint8_t *  hash160 
)

Definition at line 323 of file script.c.

References btc_script_append_op(), btc_script_append_pushdata(), cstr_resize(), OP_CHECKSIG, OP_DUP, OP_EQUALVERIFY, and OP_HASH160.

Referenced by btc_tx_add_p2pkh_hash160_out().

324 {
325  cstr_resize(script_in, 0); //clear script
326 
327  btc_script_append_op(script_in, OP_DUP);
328  btc_script_append_op(script_in, OP_HASH160);
329 
330 
331  btc_script_append_pushdata(script_in, hash160, 20);
333  btc_script_append_op(script_in, OP_CHECKSIG);
334 
335  return true;
336 }
static void btc_script_append_pushdata(cstring *script_in, unsigned char *data, size_t datalen)
Definition: script.c:272
btc_bool cstr_resize(cstring *s, size_t new_sz)
Definition: cstr.c:81
static void btc_script_append_op(cstring *script_in, enum opcodetype op)
Definition: script.c:266
Definition: script.h:100
LIBBTC_API btc_bool btc_script_build_p2sh ( cstring script_in,
const uint8_t *  hash160 
)

Definition at line 338 of file script.c.

References btc_script_append_op(), btc_script_append_pushdata(), cstr_resize(), OP_EQUAL, and OP_HASH160.

Referenced by btc_tx_add_p2sh_hash160_out().

339 {
340  cstr_resize(script_in, 0); //clear script
341  btc_script_append_op(script_in, OP_HASH160);
342  btc_script_append_pushdata(script_in, hash160, 20);
343  btc_script_append_op(script_in, OP_EQUAL);
344 
345  return true;
346 }
static void btc_script_append_pushdata(cstring *script_in, unsigned char *data, size_t datalen)
Definition: script.c:272
btc_bool cstr_resize(cstring *s, size_t new_sz)
Definition: cstr.c:81
static void btc_script_append_op(cstring *script_in, enum opcodetype op)
Definition: script.c:266
enum btc_tx_out_type btc_script_classify ( vector ops)

Definition at line 243 of file script.c.

References btc_script_is_multisig(), btc_script_is_pubkey(), btc_script_is_pubkeyhash(), btc_script_is_scripthash(), BTC_TX_MULTISIG, BTC_TX_NONSTANDARD, BTC_TX_PUBKEY, BTC_TX_PUBKEYHASH, and BTC_TX_SCRIPTHASH.

244 {
245  if (btc_script_is_pubkeyhash(ops))
246  return BTC_TX_PUBKEYHASH;
247  if (btc_script_is_scripthash(ops))
248  return BTC_TX_SCRIPTHASH;
249  if (btc_script_is_pubkey(ops))
250  return BTC_TX_PUBKEY;
251  if (btc_script_is_multisig(ops))
252  return BTC_TX_MULTISIG;
253 
254  return BTC_TX_NONSTANDARD;
255 }
btc_bool btc_script_is_scripthash(vector *ops)
Definition: script.c:213
btc_bool btc_script_is_multisig(vector *ops)
Definition: script.c:227
btc_bool btc_script_is_pubkey(vector *ops)
Definition: script.c:194
btc_bool btc_script_is_pubkeyhash(vector *ops)
Definition: script.c:202
btc_bool btc_script_copy_without_op_codeseperator ( const cstring scriptin,
cstring scriptout 
)

Definition at line 36 of file script.c.

References cstr_append_buf(), deser_bytes(), deser_skip(), deser_u16(), deser_u32(), const_buffer::len, cstring::len, OP_CODESEPARATOR, OP_PUSHDATA1, OP_PUSHDATA2, OP_PUSHDATA4, const_buffer::p, and cstring::str.

Referenced by btc_tx_sighash().

37 {
38  if (script_in->len == 0)
39  return false; /* EOF */
40 
41  struct const_buffer buf = {script_in->str, script_in->len};
42  unsigned char opcode;
43  while (buf.len > 0) {
44  if (!deser_bytes(&opcode, &buf, 1))
45  goto err_out;
46 
47  uint32_t data_len;
48 
49  if (opcode == OP_CODESEPARATOR)
50  continue;
51 
52  else if (opcode == OP_PUSHDATA1) {
53  uint8_t v8;
54  if (!deser_bytes(&v8, &buf, 1))
55  goto err_out;
56  data_len = v8;
57  } else if (opcode == OP_PUSHDATA2) {
58  uint16_t v16;
59  if (!deser_u16(&v16, &buf))
60  goto err_out;
61  data_len = v16;
62  } else if (opcode == OP_PUSHDATA4) {
63  uint32_t v32;
64  if (!deser_u32(&v32, &buf))
65  goto err_out;
66  data_len = v32;
67  } else {
68  cstr_append_buf(script_out, &opcode, 1);
69  continue;
70  }
71 
72  cstr_append_buf(script_out, buf.p, data_len);
73  if (!deser_skip(&buf, data_len))
74  goto err_out;
75  }
76 
77 err_out:
78  return false;
79 }
size_t len
Definition: buffer.h:22
btc_bool deser_skip(struct const_buffer *buf, size_t len)
Definition: serialize.c:92
const void * p
Definition: buffer.h:21
btc_bool deser_u32(uint32_t *vo, struct const_buffer *buf)
Definition: serialize.c:126
btc_bool deser_bytes(void *po, struct const_buffer *buf, size_t len)
Definition: serialize.c:103
btc_bool deser_u16(uint16_t *vo, struct const_buffer *buf)
Definition: serialize.c:115
btc_bool cstr_append_buf(cstring *s, const void *buf, size_t sz)
Definition: cstr.c:106
btc_bool btc_script_get_ops ( const cstring script_in,
vector ops_out 
)

Definition at line 108 of file script.c.

References btc_script_op_free(), btc_script_op_new(), btc_script_op_::data, btc_script_op_::datalen, deser_bytes(), deser_skip(), deser_u16(), deser_u32(), const_buffer::len, cstring::len, btc_script_op_::op, OP_PUSHDATA1, OP_PUSHDATA2, OP_PUSHDATA4, const_buffer::p, cstring::str, and vector_add().

109 {
110  if (script_in->len == 0)
111  return false; /* EOF */
112 
113  struct const_buffer buf = {script_in->str, script_in->len};
114  unsigned char opcode;
115 
116  btc_script_op* op = NULL;
117  while (buf.len > 0) {
118  op = btc_script_op_new();
119 
120  if (!deser_bytes(&opcode, &buf, 1))
121  goto err_out;
122 
123  op->op = opcode;
124 
125  uint32_t data_len;
126 
127  if (opcode < OP_PUSHDATA1) {
128  data_len = opcode;
129  } else if (opcode == OP_PUSHDATA1) {
130  uint8_t v8;
131  if (!deser_bytes(&v8, &buf, 1))
132  goto err_out;
133  data_len = v8;
134  } else if (opcode == OP_PUSHDATA2) {
135  uint16_t v16;
136  if (!deser_u16(&v16, &buf))
137  goto err_out;
138  data_len = v16;
139  } else if (opcode == OP_PUSHDATA4) {
140  uint32_t v32;
141  if (!deser_u32(&v32, &buf))
142  goto err_out;
143  data_len = v32;
144  } else {
145  vector_add(ops_out, op);
146  continue;
147  }
148 
149  op->data = calloc(1, data_len);
150  memcpy(op->data, &buf.p, data_len);
151  op->datalen = data_len;
152 
153  vector_add(ops_out, op);
154 
155  if (!deser_skip(&buf, data_len))
156  goto err_out;
157  }
158 
159  return true;
160 err_out:
161  btc_script_op_free(op);
162  return false;
163 }
char * str
Definition: cstr.h:43
size_t len
Definition: buffer.h:22
btc_bool deser_skip(struct const_buffer *buf, size_t len)
Definition: serialize.c:92
btc_script_op * btc_script_op_new()
Definition: script.c:81
size_t datalen
Definition: script.h:206
const void * p
Definition: buffer.h:21
btc_bool deser_u32(uint32_t *vo, struct const_buffer *buf)
Definition: serialize.c:126
void btc_script_op_free(btc_script_op *script_op)
Definition: script.c:90
btc_bool deser_bytes(void *po, struct const_buffer *buf, size_t len)
Definition: serialize.c:103
btc_bool vector_add(vector *vec, void *data)
Definition: vector.c:113
btc_bool deser_u16(uint16_t *vo, struct const_buffer *buf)
Definition: serialize.c:115
size_t len
Definition: cstr.h:44
enum opcodetype op
Definition: script.h:204
unsigned char * data
Definition: script.h:205
LIBBTC_API void btc_script_op_free ( btc_script_op script_op)

Definition at line 90 of file script.c.

References btc_script_op_::data, btc_script_op_::datalen, btc_script_op_::op, and OP_0.

Referenced by btc_script_get_ops(), and btc_script_op_free_cb().

91 {
92  if (script_op->data) {
93  free(script_op->data);
94  script_op->data = NULL;
95  }
96  script_op->datalen = 0;
97  script_op->op = OP_0;
98 }
size_t datalen
Definition: script.h:206
enum opcodetype op
Definition: script.h:204
Definition: script.h:51
unsigned char * data
Definition: script.h:205
void btc_script_op_free_cb ( void *  data)

Definition at line 100 of file script.c.

References btc_script_op_free().

101 {
102  btc_script_op* script_op = data;
103  btc_script_op_free(script_op);
104 
105  free(script_op);
106 }
void btc_script_op_free(btc_script_op *script_op)
Definition: script.c:90
LIBBTC_API btc_script_op* btc_script_op_new ( )

Definition at line 81 of file script.c.

Referenced by btc_script_get_ops().

82 {
83  btc_script_op* script_op;
84  script_op = calloc(1, sizeof(*script_op));
85 
86  return script_op;
87 }