getOpCodes function

List<Tuple3<int, int, Uint8List>> getOpCodes(
  1. Uint8List data, [
  2. dynamic offset = 0
])

Implementation

List<Tuple3<int, int, Uint8List>> getOpCodes(Uint8List data, [offset = 0]) {
  final ops = <Tuple3<int, int, Uint8List>>[];
  int ptr = offset;

  while (ptr < data.length) {
    final op = data[ptr];
    var opTuple = Tuple3(op, ptr, Uint8List.fromList([]));
    ptr += 1;

    if (op <= 0x4e) {
      // These are OP_PUSHes
      int len;
      int lenSize;

      if (op < 0x4c) {
        len = op;
        lenSize = 0;
      } else if (op == 0x4c) {
        len = data[ptr];
        lenSize = 1;
      } else if (op == 0x4d) {
        len = data
            .sublist(ptr, ptr + 2)
            .buffer
            .asByteData()
            .getUint16(0, Endian.little);
        lenSize = 2;
      } else {
        len = data
            .sublist(ptr, ptr + 4)
            .buffer
            .asByteData()
            .getUint32(0, Endian.little);
        lenSize = 4;
      }

      ptr += lenSize;
      try {
        opTuple = Tuple3(op, ptr + len, data.sublist(ptr, ptr + len));
      } on RangeError {
        opTuple = Tuple3(-1, data.length - 1, data.sublist(ptr));
        ops.add(opTuple);
        break;
      }
      ptr += len;
    }
    ops.add(opTuple);
  }

  return ops;
}