/*## (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 javax.tv.service.*;
import javax.tv.locator.*;
import org.davic.net.dvb.*;

public class OneTimeFilter extends MPEGFilter {
 private byte data[];
 private Object syncobj;

 private OneTimeFilter(String name,int onid,int tsid,int pid,int table_id,byte posfdef[],byte posfmask[]) {
  super(name,onid,tsid,pid,table_id,posfdef,posfmask);
  data=null;
  syncobj=new Object();
 }

 public boolean match(byte data[]) {
  return true;
 }

 public void action(byte data[]) {
  if (this.data==null) {
   this.data=data;

   synchronized(syncobj) {
    syncobj.notify();
   }
  }
 }

 public byte[] get_data() throws Throwable {
  synchronized(syncobj) {
   syncobj.wait();
  }

  return data;
 }

 public static OneTimeFilter create(String locator,int pid) {
  return create_internal(locator,pid,-1,null,null);
 }

 public static OneTimeFilter create(String locator,int pid,int table_id,byte posfdef[],byte posfmask[]) {
  return create_internal(locator,pid,table_id,posfdef,posfmask);
 }

 public static OneTimeFilter create_internal(String locator,int pid,int table_id,byte posfdef[],byte posfmask[]) {
  OneTimeFilter f=null;

  try {
   DvbLocator dvbloc=null;

   if (locator!=null) {
    LocatorFactory lf=LocatorFactory.getInstance();
    dvbloc=(DvbLocator)lf.createLocator(locator);
   } else {
    while(dvbloc==null) {
     Service cursrv=SrvUtils.current_service();
     if (cursrv==null) continue;

     dvbloc=(DvbLocator)cursrv.getLocator();
    }
   }

   int onid=dvbloc.getOriginalNetworkId();
   int tsid=dvbloc.getTransportStreamId();
  
   String name="OT Filter [\""+dvbloc.toExternalForm()+"\",pid=0x"+Utils.hex_value(pid,4)+"]";

   f=new OneTimeFilter(name,onid,tsid,pid,table_id,posfdef,posfmask);
  } catch(Throwable t) {
   ApiMonitor.log("OneTimeFilter create_internal: "+t);
  }

  return f;
 }

}
