private static void LoadLangSpec() { if (langSpec != null) { return; } string json = File.ReadAllText("langspec.json"); langSpec = JsonConvert.DeserializeObject <LangSpec>(json); // Reader reader; // try { // reader = new InputStreamReader( // Logic.class.getResourceAsStream("/langspec.json"), // "UTF-8" // ); // } catch (UnsupportedEncodingException ex) //{ // throw new IllegalStateException("langspec opening error"); //} //Gson g = new GsonBuilder().create(); //langSpec = g.fromJson(reader, LangSpec.class); //reader.close(); }
private static void LoadLangSpec() { if (langSpec != null) { return; } string json = GetFromResources("langspec.json"); langSpec = JsonConvert.DeserializeObject <LangSpec>(json); }
/// <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); } } } } }