static int CheckIntConstBlock(byte[] program, int pc) { int size = 1; VarintResult result = Uvarint.GetUvarint(JavaHelper <byte> .ArrayCopyRange(program, pc + size, program.Length)); if (result.length <= 0) { throw new ArgumentException(string.Format("could not decode int const block at pc=%d", pc)); } else { size = size + result.length; int numInts = result.value; for (int i = 0; i < numInts; ++i) { if (pc + size >= program.Length) { throw new ArgumentException("int const block exceeds program length"); } result = Uvarint.GetUvarint(JavaHelper <byte> .ArrayCopyRange(program, pc + size, program.Length)); if (result.length <= 0) { throw new ArgumentException(string.Format("could not decode int const[%d] block at pc=%d", i, pc + size)); } size += result.length; } return(size); } }
/// <summary> /// Performs basic program validation: instruction count and program cost /// </summary> /// <param name="program"></param> /// <param name="args"></param> /// <returns></returns> public static bool CheckProgram(byte[] program, List <byte[]> args) { if (langSpec == null) { var jsonStr = GetFromResources("langspec.json"); langSpec = JsonConvert.DeserializeObject <LangSpec>(jsonStr); } VarintResult result = Uvarint.GetUvarint(program); int vlen = result.length; if (vlen <= 0) { throw new ArgumentException("version parsing error"); } else { int version = result.value; if (version > langSpec.EvalMaxVersion) { throw new ArgumentException("unsupported version"); } else { if (args == null) { args = new List <byte[]>(); } int cost = 0; int length = program.Length; int pc; for (pc = 0; pc < args.Count; ++pc) { length += args[pc].Length; } if (length > 1000) { throw new ArgumentException("program too long"); } else { if (opcodes == null) { opcodes = new Logic.Operation[256]; for (pc = 0; pc < langSpec.Ops.Length; ++pc) { Logic.Operation op = langSpec.Ops[pc]; opcodes[op.Opcode] = op; } } int size; for (pc = vlen; pc < program.Length; pc += size) { int opcode = program[pc] & 255; Logic.Operation op = opcodes[opcode]; if (op == null) { throw new ArgumentException("invalid instruction: " + opcode); } cost += op.Cost; size = op.Size; if (size == 0) { switch (op.Opcode) { case 32: size = CheckIntConstBlock(program, pc); break; case 38: size = CheckByteConstBlock(program, pc); break; default: throw new ArgumentException("invalid instruction: " + op.Opcode); } } } if (cost > 20000) { throw new ArgumentException("program too costly to run"); } else { return(true); } } } } }
/// <summary> /// Performs basic program validation: instruction count and program cost /// </summary> /// <param name="program"></param> /// <param name="args"></param> /// <returns></returns> public static bool CheckProgram(byte[] program, List <byte[]> args) { if (langSpec == null) { // var jsonStr = (new StreamReader("langspec.json")).ReadToEnd(); // langSpec = JsonConvert.DeserializeObject<LangSpec>(jsonStr); // read file from embedded resources - not the file system var jsonStr = GetFromResources("langspec.json"); langSpec = JsonConvert.DeserializeObject <LangSpec>(jsonStr); // InputStreamReader reader; // try { // reader = new InputStreamReader(Logic.class.getResourceAsStream("/langspec.json"), "UTF-8"); // } catch (UnsupportedEncodingException var11) { // throw new IllegalStateException("langspec opening error"); // } // Gson g = (new GsonBuilder()).create(); // langSpec = (Logic.LangSpec) g.fromJson(reader, Logic.LangSpec.class); // reader.close(); } VarintResult result = Uvarint.GetUvarint(program); int vlen = result.length; if (vlen <= 0) { throw new ArgumentException("version parsing error"); } else { int version = result.value; if (version > langSpec.EvalMaxVersion) { throw new ArgumentException("unsupported version"); } else { if (args == null) { args = new List <byte[]>(); } int cost = 0; int length = program.Length; int pc; for (pc = 0; pc < args.Count; ++pc) { length += args[pc].Length; } if (length > 1000) { throw new ArgumentException("program too long"); } else { if (opcodes == null) { opcodes = new Logic.Operation[256]; for (pc = 0; pc < langSpec.Ops.Length; ++pc) { Logic.Operation op = langSpec.Ops[pc]; opcodes[op.Opcode] = op; } } int size; for (pc = vlen; pc < program.Length; pc += size) { int opcode = program[pc] & 255; Logic.Operation op = opcodes[opcode]; if (op == null) { throw new ArgumentException("invalid instruction: " + opcode); } cost += op.Cost; size = op.Size; if (size == 0) { switch (op.Opcode) { case 32: size = CheckIntConstBlock(program, pc); break; case 38: size = CheckByteConstBlock(program, pc); break; default: throw new ArgumentException("invalid instruction: " + op.Opcode); } } } if (cost > 20000) { throw new ArgumentException("program too costly to run"); } else { return(true); } } } } }