/*## (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.util.*;
import java.io.*;
import java.util.zip.*;
import javax.tv.service.*;
import org.davic.mpeg.TransportStream;
import org.davic.net.dvb.*;

public class InvoiceFilter extends AdbEmmFilter {
 public static final int PAYLOAD_TABLE_ID   = 0x04;
 public static final int PAYLOAD_FILTER_OFF = 0x08;
 public static final int PAYLOAD_DATA_OFF   = 0x0f;

 private int print_type;

 private InvoiceFilter(String name,int onid,int tsid,int pid,int table_id,int filter_pos,byte posfdef[],byte posfmask[],int data_off,int invoice_cnt,int print_type) {
  super(name,onid,tsid,pid,table_id,filter_pos,posfdef,posfmask,data_off,invoice_cnt,new Integer(print_type));

  this.print_type=print_type;
 }

 public static InvoiceFilter create(byte cardaddr[],int print_type,int cnt) {
  byte mask[]=new byte[7];
  mask[0]=0;
  mask[1]=-1;
  mask[2]=-1;
  mask[3]=-1;
  mask[4]=-1;
  mask[5]=0;
  mask[6]=0;

  return create(cardaddr,mask,print_type,cnt);
 }

 public int process_data(byte data[],int data_cnt,Object appdata) {
  int cnt=0;

  try {
   String s=new String(data);

   int print_type=((Integer)appdata).intValue();

   if (print_type==2) {
    Output.text(s);
    cnt=1;
   } else {
    cnt=Invoice.print_invoices(s,print_type,data_cnt);
   }

  } catch(Throwable t) {}

  return cnt;
 }

 public int get_key(byte data[]) {
  return ((((int)data[8])&0xff)<<24)|((((int)data[9])&0xff)<<16)|((((int)data[3])&0xff)<<8)|(((int)data[4])&0xff);
 }

 public static InvoiceFilter create(byte cardaddr[],byte mask[],int print_type,int cnt) {
  InvoiceFilter f=null;

  try {
   TransportStream ts=MPEGSniffer.current_ts();

   int tsid=ts.getTransportStreamId();

   Service srv=SrvUtils.get_invoice_service(tsid);

   if (srv!=null) {
    ApiMonitor.log("srv "+srv.getName()+" "+srv.getLocator());

    int invoice_pid=SrvUtils.get_invoice_pid(srv);

    ApiMonitor.log("invoice_pid "+Utils.hex_value(invoice_pid,4));

    if (invoice_pid>0) {
     DvbLocator dvblocator=(DvbLocator)srv.getLocator();

     int onid=dvblocator.getOriginalNetworkId();

     if (dvblocator.getTransportStreamId()==tsid) {
      String name="Invoice Filter [\""+srv.getName()+"\",pid=0x"+Utils.hex_value(invoice_pid,4)+",cardaddr="+SCInfo.addr2string(cardaddr)+",mask="+SCInfo.addr2string(mask)+"]";

      byte fdef[]=new byte[0];
      byte fmask[]=new byte[0];

      f=new InvoiceFilter(name,onid,tsid,invoice_pid,PAYLOAD_TABLE_ID,PAYLOAD_FILTER_OFF,fdef,fmask,PAYLOAD_DATA_OFF,cnt,print_type);

      ApiMonitor.log("filter "+f);
      ApiMonitor.log("filter name: "+name);
     }
    }
   }
  } catch(Throwable t) {
   ApiMonitor.log("create filter: "+t);
  }

  return f;
 }
}
