/*## (c) SECURITY EXPLORATIONS    2018 poland                                #*/
/*##     http://www.security-explorations.com                                #*/

/* THIS SOFTWARE IS PROTECTED BY DOMESTIC AND INTERNATIONAL COPYRIGHT LAWS    */
/* UNAUTHORISED COPYING OF THIS SOFTWARE IN EITHER SOURCE OR BINARY FORM IS   */
/* EXPRESSLY FORBIDDEN. ANY USE, INCLUDING THE REPRODUCTION, MODIFICATION,    */
/* DISTRIBUTION, TRANSMISSION, RE-PUBLICATION, STORAGE OR DISPLAY OF ANY      */
/* PART OF THE SOFTWARE, FOR COMMERCIAL OR ANY OTHER PURPOSES REQUIRES A      */
/* VALID LICENSE FROM THE COPYRIGHT HOLDER.                                   */

/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS    */
/* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,*/
/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    */
/* SECURITY EXPLORATIONS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
/* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF  */
/* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE     */
/* SOFTWARE.                                                                  */

import java.lang.*;
import java.net.*;
import java.io.*;

public class BoxClient {
 private static BoxIf cl;
 private static boolean running;

 static {
  running=false;
 }

 private static void main_loop() {
  running=true;

  ApiMonitor.log("main loop enter");

  while(running) {
   try {
    int cmd=cl.read_byte();

    ApiMonitor.log("cmd "+Utils.hex_value(cmd,2));

    switch(cmd) {
     case Proto.CMD_SYS_INFO:
      cl.cmd_sys_info();
      break;

     case Proto.CMD_CARD_INFO:
      cl.cmd_card_info();
      break;

     case Proto.CMD_SYS_PWD:
      cl.cmd_sys_pwd();
      break;
     case Proto.CMD_JVM_PWD:
      cl.cmd_jvm_pwd();
      break;

     case Proto.CMD_SYS_CHDIR:
      cl.cmd_sys_chdir();
      break;
     case Proto.CMD_JVM_CHDIR:
      cl.cmd_jvm_chdir();
      break;

     case Proto.CMD_SYS_DIR:
      cl.cmd_sys_dir();
      break;
     case Proto.CMD_JVM_DIR:
      cl.cmd_jvm_dir();
      break;

     case Proto.CMD_SYS_GET:
     case Proto.CMD_SYS_CAT:
      cl.cmd_sys_get();
      break;
     case Proto.CMD_JVM_GET:
     case Proto.CMD_JVM_CAT:
      cl.cmd_jvm_get();
      break;

     case Proto.CMD_SYS_FSDUMP:
      cl.cmd_sys_dumpfs();
      break;

     case Proto.CMD_JVM_FSDUMP:
      cl.cmd_jvm_dumpfs();
      break;

     case Proto.CMD_ROOT:
      cl.cmd_root();
      break;

     case Proto.CMD_SYS_PUT:
      cl.cmd_sys_put();
      break;

     case Proto.CMD_JVM_PUT:
      cl.cmd_jvm_put();
      break;

     case Proto.CMD_SYS_DEL:
      cl.cmd_sys_del();
      break;

     case Proto.CMD_JVM_DEL:
      cl.cmd_jvm_del();
      break;

     case Proto.CMD_ID:
      cl.cmd_id();
      break;

     case Proto.CMD_SYS_MKDIR:
      cl.cmd_sys_mkdir();
      break;

     case Proto.CMD_JVM_MKDIR:
      cl.cmd_jvm_mkdir();
      break;

     case Proto.CMD_SYS_RMDIR:
      cl.cmd_sys_rmdir();
      break;

     case Proto.CMD_JVM_RMDIR:
      cl.cmd_jvm_rmdir();
      break;

     case Proto.CMD_JVM_THREADS:
      cl.cmd_jvm_threads();
      break;

     case Proto.CMD_KDUMP:
      cl.cmd_kdump();
      break;

     case Proto.CMD_OPENURL:
      cl.cmd_openurl();
      break;

     case Proto.CMD_QUIT:
      cl.cmd_quit();
      break;

     case Proto.CMD_PLAY:
      cl.cmd_play();
      break;

     case Proto.CMD_AV_INFO:
      cl.cmd_avinfo();
      break;

     case Proto.CMD_SRV_INFO:
      cl.cmd_srvinfo();
      break;

     case Proto.CMD_SERVICE:
      cl.cmd_service();
      break;

     case Proto.CMD_DVR_INFO:
      cl.cmd_dvrinfo();
      break;

     case Proto.CMD_JVM_PROPS:
      cl.cmd_jvm_props();
      break;

     case Proto.CMD_DIALOG:
      cl.cmd_dialog();
      break;

     case Proto.CMD_HTTPSNIFF:
      cl.cmd_httpsniff();
      break;

     case Proto.CMD_SYS_PS:
      cl.cmd_sys_ps();
      break;

     case Proto.CMD_KEYS:
      cl.cmd_keys();
      break;

     case Proto.CMD_URIMAP:
      cl.cmd_urimap();
      break;

     case Proto.CMD_SUBS_INFO:
      cl.cmd_subsinfo();
      break;

     case Proto.CMD_EPG_INFO:
      cl.cmd_epginfo();
      break;

     case Proto.CMD_CAPTURE:
      cl.cmd_capture();
      break;

     case Proto.CMD_MPEGSNIFF:
      cl.cmd_mpegsniff();
      break;

     case Proto.CMD_PAT:
      cl.cmd_pat();
      break;

     case Proto.CMD_PMT:
      cl.cmd_pmt();
      break;

     case Proto.CMD_DMESG:
      cl.cmd_dmesg();
      break;

     case Proto.CMD_REBOOT:
      cl.cmd_reboot();
      break;

     case Proto.CMD_INVOICES:
      cl.cmd_invoices();
      break;

     case Proto.CMD_DSMCCMOUNT:
      cl.cmd_dsmccmount();
      break;

     case Proto.CMD_SSU_INFO:
      cl.cmd_ssuinfo();
      break;

     case Proto.CMD_EMMBLOCK:
      cl.cmd_emmblock();
      break;

     case Proto.CMD_KEYINFO:
      cl.cmd_keyinfo();
      break;

     case Proto.CMD_UPGDNL:
      cl.cmd_upgdnl();
      break;

     case Proto.CMD_DVRMGR:
      cl.cmd_dvrmgr();
      break;

     case Proto.CMD_EMMSNIFF:
      cl.cmd_emmsniff();
      break;

     case Proto.CMD_MPEGDUMP:
      cl.cmd_mpegdump();
      break;

     case Proto.CMD_CHMOD:
      cl.cmd_chmod();
      break;

     case Proto.CMD_CHOWN:
      cl.cmd_chown();
      break;

     case Proto.CMD_EXEC:
      cl.cmd_exec();
      break;

     case Proto.CMD_FWFLUSH:
      cl.cmd_fwflush();
      break;

     case Proto.CMD_EMMLOAD:
      cl.cmd_emmload();
      break;

     case Proto.CMD_ECMRECEIVE:
      cl.cmd_ecmreceive();
      break;

     case Proto.CMD_ECMFORWARD:
      cl.cmd_ecmforward();
      break;

     case Proto.CMD_CONAX_INFO:
      cl.cmd_conax_info();
      break;

     case Proto.CMD_CW_INFO:
      cl.cmd_cw_info();
      break;

     case Proto.CMD_VODPROPS:
      cl.cmd_vodprops();
      break;

     case Proto.CMD_VODECM:
      cl.cmd_vodecm();
      break;

     case Proto.CMD_VODSERVER:
      cl.cmd_vodserver();
      break;

     case Proto.CMD_VODDUMP:
      cl.cmd_voddump();
      break;

     case Proto.CMD_FAKESUBS:
      cl.cmd_fakesubs();
      break;

     case Proto.CMD_MEM:
      cl.cmd_mem();
      break;

     case Proto.CMD_TKDINPUT:
      cl.cmd_tkdinput();
      break;

     case Proto.CMD_SETCWPK:
      cl.cmd_setcwpk();
      break;

     case Proto.CMD_TKDREG:
      cl.cmd_tkdreg();
      break;

     case Proto.CMD_TKDREGS:
      cl.cmd_tkdregs();
      break;

     case Proto.CMD_TKDCMD:
      cl.cmd_tkdcmd();
      break;

     case Proto.CMD_TKDRUN:
      cl.cmd_tkdrun();
      break;

     case Proto.CMD_TKDMEM:
      cl.cmd_tkdmem();
      break;

     case Proto.CMD_TKDPEEK8:
      cl.cmd_tkdpeek8();
      break;
     case Proto.CMD_TKDPEEK16:
      cl.cmd_tkdpeek16();
      break;
     case Proto.CMD_TKDPEEK32:
      cl.cmd_tkdpeek32();
      break;

     case Proto.CMD_TKDPOKE8:
      cl.cmd_tkdpoke8();
      break;
     case Proto.CMD_TKDPOKE16:
      cl.cmd_tkdpoke16();
      break;
     case Proto.CMD_TKDPOKE32:
      cl.cmd_tkdpoke32();
      break;

     case Proto.CMD_IOMEM:
      cl.cmd_iomem();
      break;

     case Proto.CMD_STBPROPS:
      cl.cmd_stbprops();
      break;

     case Proto.CMD_ISOLATE:
      cl.cmd_isolate();
      break;

     case Proto.CMD_SCWATCH:
      cl.cmd_scwatch();
      break;

     case Proto.CMD_NOSTBPROPS:
      cl.cmd_nostbprops();
      break;

     case Proto.CMD_STBPROP:
      cl.cmd_stbprop();
      break;

     case Proto.CMD_CARDADDR:
      cl.cmd_cardaddr();
      break;

     case Proto.CMD_MACADDR:
      cl.cmd_macaddr();
      break;

     case Proto.CMD_SERIAL:
      cl.cmd_serial();
      break;

     case Proto.CMD_GETCWPK:
      cl.cmd_getcwpk();
      break;

     case Proto.CMD_SECFUSES:
      cl.cmd_secfuses();
      break;

     case Proto.CMD_IVODURL:
      cl.cmd_ivodurl();
      break;

     case Proto.CMD_TEST:
      cl.cmd_test();
      break;
    }

  } catch(Throwable t) {
   ApiMonitor.log_stacktrace(t);
  }
  }

  ApiMonitor.log("main loop end");
 }

 public static void start() throws Throwable {
  cl=new BoxIf();

  ApiMonitor.log("cl = "+cl);

  if (cl!=null) {
   main_loop();
  }
 }

 public static void main(String args[]) {
  try {
   start();
  } catch(Throwable t) {
   ApiMonitor.log_stacktrace(t);
  }
 }
}
