#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main( int argc, char *argv[])
{
FILE *fp = NULL;
FILE *fpout = NULL;
char *PUSI_data;
char PUSI_data_A[1024] = "";
char *in_file_name;
char *out_file_name;
unsigned char hex[1024] = "";
int each = 0;
int af_length = 0;
size_t bytes = 0;
size_t used = 0;
size_t offset = 0;
unsigned int PID = 0x200;
// get PID argument
PID = strtol(argv[2],0,16);
// open input ts file
if ( ( fp = fopen (argv[1], "rb")) == NULL) {
printf ( "could not open file\n");
return 1;
}
// compute outout filename
in_file_name=argv[1];
printf("InFile %s\n",in_file_name);
char *token = strtok(in_file_name, ".");
out_file_name= strcat(token ,"_PUSI.txt");
printf("OutFile %s\n",out_file_name);
//open output txt file
fpout = fopen(out_file_name, "w");
// exiting program
if (fpout == NULL) {
printf("Error!");
exit(1);
}
// PID = strtol(argv[2],0,16);
printf ( "Scanning for PUSI in PID: 0x%04X\n",PID);
//loop as long as there are bytes to read
while ( ( bytes = fread ( &hex, 1, 188, fp)) > 0) {
PUSI_data="";
// look for 0x47 Sync
if ( hex[0] == 0x47)
// look for Pusi flag and PID
{
if (((hex[1] & 0x40) == 0x40) & ((hex[1] & 0x3f) == (PID >> 8 & 0xff)) & ((hex[2] & 0xff) == (PID & 0xff)) )
{
if ((hex[3] & 0xC0) == 0x00) { fprintf (fpout, "C "); }
if ((hex[3] & 0xC0) == 0x40) { fprintf (fpout, "X "); }
if ((hex[3] & 0xC0) == 0x80) { fprintf (fpout, "E "); }
if ((hex[3] & 0xC0) == 0xC0) { fprintf (fpout, "O "); }
// check for Adaption Field 0x80
af_length=0;
if ((hex[3] & 0x30) == 0x00) { fprintf (fpout, "X ");}
if ((hex[3] & 0x30) == 0x10) { fprintf (fpout, "Payld: ");}
if ((hex[3] & 0x30) == 0x20) { fprintf (fpout, "AF: %02X ",hex[4] ); af_length=hex[4]+1;}
if ((hex[3] & 0x30) == 0x30) { fprintf (fpout, "AF: %02X ",hex[4] ); af_length=hex[4]+1;}
int j;
fprintf (fpout, "PUSI:");
for(j=0; j<4; j++){fprintf (fpout,"%02x ", hex[j]); }
fprintf (fpout, "CB0: ");
for(j=4; j<12; j++){fprintf (fpout,"%02x ", hex[j+af_length]); }
fprintf (fpout, "CB1: ");
for(j=12; j<15; j++){fprintf (fpout,"%02x ", hex[j+af_length]); }
fprintf (fpout, "\n");
}
}
}
printf ( "End scanning file\n");
fclose ( fp);
fclose( fpout);
return 0;
}