/*## (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 CarboUpgradeDownloader extends MPEGFilter {
 private Object syncobj;
 private boolean inited;
 private int sec_cnt;
 private int total_cnt;
 private boolean received[];

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

 public boolean match(byte data[]) {
  boolean res=false;

  if (!inited) {
   sec_cnt=0;
   total_cnt=(((((int)data[7])&0xff)<<8)|(((int)data[8])&0xff))+1;

   received=new boolean[total_cnt];

   try {
    Output.integer(total_cnt);
   } catch(Throwable t) {}

   inited=true;
  }

  int section_id=(((data[5]&0xff)<<8)|(data[6]&0xff));

  if (!received[section_id]) {
   res=true;
  }

  return res;
 }

 public void action(byte data[]) {
  try {
   Output.data(data);
   Output.flush();

   int section_id=(((data[5]&0xff)<<8)|(data[6]&0xff));
   received[section_id]=true;

   sec_cnt++;

   if (sec_cnt==total_cnt) {
    synchronized(syncobj) {
     syncobj.notify();
    }
   }
  } catch(Throwable t) {}
 }

 public void collect_sections() throws Throwable {
  synchronized(syncobj) {
   syncobj.wait();
  }
 }

 public static CarboUpgradeDownloader create(int pid,int table_id,int download_id) {
  CarboUpgradeDownloader f=null;

  byte posfdef[]=new byte[2];
  posfdef[0]=(byte)((download_id>>8)&0xff);
  posfdef[1]=(byte)(download_id&0xff);

  byte posfmask[]=new byte[2];
  posfmask[0]=(byte)-1;
  posfmask[1]=(byte)-1;

  try {
   DvbLocator dvbloc=(DvbLocator)SrvUtils.current_service().getLocator();

   int onid=dvbloc.getOriginalNetworkId();
   int tsid=dvbloc.getTransportStreamId();

   String name="Carbo Upgrade Downloader [\""+dvbloc.toExternalForm()+"\",pid=0x"+Utils.hex_value(pid,4)+"]";

   f=new CarboUpgradeDownloader(name,onid,tsid,pid,table_id,posfdef,posfmask);
  } catch(Throwable t) {}

  return f;
 }

}
