Example #1
0
  /**
   * Decodes a ClientScript.
   *
   * @param buffer The Buffer.
   * @return The ClientScript.
   */
  @SuppressWarnings("unused")
  public static ClientScript666 decode(DataBuffer buffer) {
    ClientScript666 cs = new ClientScript666();

    buffer.position(buffer.limit() - 2);
    int trailerLength = buffer.getShort() & 0xFFFF;
    int trailerPosition = buffer.limit() - 18 - trailerLength;
    buffer.position(trailerPosition);

    int operations = buffer.getInt();
    int intVariables = buffer.getUnsignedShort();
    int stringVariables = buffer.getUnsignedShort();
    int longVariables = buffer.getUnsignedShort();

    int intParameterCount = buffer.getUnsignedShort();
    int stringParameterCount = buffer.getUnsignedShort();
    int longParameterCount = buffer.getUnsignedShort();

    int switches = buffer.getUnsignedByte();

    List<Map<Integer, Integer>> tables = new ArrayList<>(switches);
    for (int index = 0; index < switches; index++) {
      int cases = buffer.getUnsignedShort();
      Map<Integer, Integer> table = new HashMap<>();

      while (cases-- > 0) {
        int value = buffer.getInt();
        int offset = buffer.getInt();
        table.put(value, offset);
      }

      tables.add(table);
    }

    cs.switchTables = tables;

    buffer.position(0);

    String name = buffer.getCString();
    cs.name = name;
    cs.opcodes = new int[operations];
    cs.intOperands = new int[operations];
    cs.stringOperands = new String[operations];
    cs.longOperands = new long[operations];

    int index = 0;
    while (buffer.position() < trailerPosition) {
      int opcode = buffer.getUnsignedShort();

      if (opcode == 3) {
        cs.stringOperands[index] = buffer.getCString();
      } else if (opcode == 54) {
        cs.longOperands[index] = buffer.getLong();
      } else if (opcode >= 150 || opcode == 21 || opcode == 38 || opcode == 39) {
        cs.intOperands[index] = buffer.getUnsignedByte();
      } else {
        cs.intOperands[index] = buffer.getInt();
      }

      cs.opcodes[index++] = opcode;
    }

    return cs;
  }