/*## (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 org.davic.mpeg.*;
import org.davic.net.tuning.*;
import org.dvb.service.selection.*;

public class MPEGSniffer {
 public static TransportStream current_ts() {
  TransportStream ts=null;
 
  try {
   DvbServiceContext sc=(DvbServiceContext)SrvUtils.current_service_context();
   NetworkInterface ni=sc.getNetworkInterface();

   ts=ni.getCurrentTransportStream();
  } catch(Throwable t) {}

  return ts;
 }

 public static byte[] get_section(int pid) {
  return get_section(null,pid);
 }

 public static byte[] get_section(int pid,int tid) {
  return get_section(null,pid,tid);
 }

 public static byte[] get_section(String locator,int pid) {
  return get_section(locator,pid,-1,null,null);
 }

 public static byte[] get_section(String locator,int pid,int tid) {
  return get_section(locator,pid,tid,null,null);
 }

 public static byte[] get_section(String locator,int pid,int tid,byte posfdef[],byte posfmask[]) {
  byte data[]=null;

  try {
   OneTimeFilter otf=OneTimeFilter.create(locator,pid,tid,posfdef,posfmask);

   if (otf!=null) {
    otf.start();

    data=otf.get_data();
 
    otf.stop();
   }
  } catch(Throwable t) {}

  return data;
 }

 public static byte[] get_PAT() {
  byte pat[]=get_section(null,MPEGDefs.PAT_PID,MPEGDefs.program_association_section,null,null);

  return pat;
 }

 public static byte[] get_CAT() {
  byte cat[]=get_section(null,MPEGDefs.CAT_PID,MPEGDefs.conditional_access_section,null,null);

  return cat;
 }

 public static byte[] get_PMT(int pid,int svid) {
  byte posfdef[]=new byte[2];
  posfdef[0]=(byte)((svid>>8)&0xff);
  posfdef[1]=(byte)((svid)&0xff);

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

  byte pmt[]=get_section(null,pid,MPEGDefs.TS_program_map_section,posfdef,posfmask);

  return pmt;
 }

 public static byte[] get_NIT() {
  int nit_pid=MPEG.pid_by_svid(0);

  if (nit_pid>0) {
   byte nit[]=get_section(null,nit_pid,MPEGDefs.network_information_section,null,null);
   return nit;
  } else return null;
 }
}
