Exemplo n.º 1
0
/**
 * Generic function that loads a given list of BAM files according to
 * the values of the other parameters.  It starts with creating the
 * bam schema and the header tables if these do not exist yet.  Then
 * it initializes bam_wrapper structs for all bam files and reads the
 * headers for all BAM files.  If the pairwise storage schema has to
 * be used, It then creates a thread for every file that reads all
 * alignments for this file.
 *
 */
static str
bam_loader(Client cntxt, MalBlkPtr mb, str * filenames, int nr_files,
	   sht dbschema, sht nr_threads)
{
	bam_wrapper *bws = NULL;
	MT_Id *reader_threads = NULL;
	reader_thread_data *r_thread_data = NULL;
	mvc *m = NULL;
	sql_schema *s = NULL;
	sql_table *files_table = NULL;
	lng cur_file_id;
	char buf_threads_msg[4096] = "There were reader threads that contained errors:\n";
	int threads_msg_len = strlen(buf_threads_msg);
	int i, errnr;
	str msg = MAL_SUCCEED;

	TO_LOG("<bam_loader>: Loader started for %d BAM file%s...\n",
		   nr_files, (nr_files != 1 ? "s" : ""));

	/* Check sanity of input */
	if (dbschema != 0 && dbschema != 1) {
		msg = createException(MAL, "bam_loader",
					  "Wrong value for dbschema: '%d' (0=straightforward storage schema, 1=pairwise storage schema)",
					  dbschema);
		goto cleanup;
	}
	if (nr_threads <= 0) {
		nr_threads = 1;
	} else if(nr_threads > 4) {
		nr_threads = 4;
	}

	/* Get SQL context */
	if ((msg = getSQLContext(cntxt, mb, &m, NULL)) != MAL_SUCCEED) {
		/* Here, and in multiple other locations in this code,
		 * new message is stored in tmp var, since the old msg
		 * needs to be freed after construction of the new
		 * msg */
		REUSE_EXCEPTION(msg, MAL, "bam_loader",
				"Could not retrieve SQLContext: %s", msg);
		goto cleanup;
	}

	/* Start with binding bam schema and the files table */
	if ((msg =
		 bind_bam_schema(m, &s)) != MAL_SUCCEED)
		goto cleanup;
	if((msg = 
		 bind_table(m, s, "files", &files_table)) != MAL_SUCCEED)
		goto cleanup;

	/* Get next file id from files table */
	TO_LOG("<bam_loader> Retrieving next file id...\n");
	if ((msg = next_file_id(m, files_table, &cur_file_id)) != MAL_SUCCEED) {
		goto cleanup;
	}

	/* Init bam_wrapper structs */
	if ((bws =
		 (bam_wrapper *) GDKmalloc(nr_files * sizeof(bam_wrapper))) ==
		NULL) {
		msg = createException(MAL, "bam_loader", MAL_MALLOC_FAIL);
		goto cleanup;
	}

	/* Enables cleanup to check which bam_wrappers to clear */
	memset(bws, 0, nr_files * sizeof(bam_wrapper));

	for (i = 0; i < nr_files; ++i) {
		int fln = strlen(filenames[i]);
		TO_LOG("<bam_loader> Initializing BAM wrapper for file '%s'...\n", filenames[i]);
		if ((msg =
			 init_bam_wrapper(bws + i, (IS_BAM(filenames[i], fln) ? BAM : SAM),
					  filenames[i], cur_file_id++, dbschema)) != MAL_SUCCEED) {
			goto cleanup;
		}
	}

	/* Parse all headers */
	for (i = 0; i < nr_files; ++i) {
		TO_LOG("<bam_loader> Parsing header for file '%s'...\n",
			   filenames[i]);
		if ((msg = process_header(bws + i)) != MAL_SUCCEED) {
			goto cleanup;
		}
	}

	/* If we have to load the BAM data into the pairwise storage
	 * schema, make sure that all input BAM files are sorted on
	 * QNAME */
	if (dbschema == 1) {
		for (i = 0; i < nr_files; ++i) {
			TO_LOG("<bam_loader> Checking sortedness for BAM file '%s'...\n", filenames[i]);
			if (bws[i].ord != ORDERING_QUERYNAME) {
				msg = createException(MAL, "bam_loader",
							  "Only BAM files that are sorted on queryname can be inserted into the pairwise storage schema; "
							  "BAM file '%s' has ordering '%s'",
							  bws[i].file_location,
							  ordering_str(bws[i].
								   ord));
				goto cleanup;
			}
		}
	}

	/* Create alignment storage */
	for (i = 0; i < nr_files; ++i) {
		TO_LOG("<bam_loader> Creating alignment tables for file '%s'...\n", filenames[i]);
		if ((dbschema == 0
			 && (msg = create_alignment_storage_0(cntxt,
								  "bam.create_storage_0",
								  bws + i)) != MAL_SUCCEED)
			|| (dbschema == 1
				&& (msg = create_alignment_storage_1(cntxt,
								  "bam.create_storage_1",
								  bws + i)) != MAL_SUCCEED)) {
			goto cleanup;
		}
	}


	/* Now create threads to read alignment data of different files */
	TO_LOG("<bam_loader> Creating reader threads...\n");
	if ((reader_threads =
		 (MT_Id *) GDKmalloc(nr_threads * sizeof(MT_Id))) == NULL) {
		msg = createException(MAL, "bam_loader", MAL_MALLOC_FAIL);
		goto cleanup;
	}

	if ((r_thread_data =
		 create_reader_thread_data(bws, nr_files, nr_threads)) == NULL) {
		msg = createException(MAL, "bam_loader", MAL_MALLOC_FAIL);
		goto cleanup;
	}

	for (i = 0; i < nr_threads; ++i) {
		if ((errnr =
			 MT_create_thread(&reader_threads[i],
					  run_process_bam_alignments,
					  &r_thread_data[i],
					  MT_THR_JOINABLE)) != 0) {
			msg = createException(MAL, "bam_loader",
						  "Could not create thread to process alignments (errnr %d)",
						  errnr);
			goto cleanup;
		}
	}

	TO_LOG("<bam_loader> Waiting for reader threads to finish...\n");
	/* Wait until all threads finish and collect their
	 * messages. Though it is not very likely, it could be the
	 * case that more than 1 thread generates an error message (not
	 * likely because threads exit once they notice that another
	 * thread has failed).  Therefore, we collect all error
	 * messages in one big error string
	 */
	for (i = 0; i < nr_threads; ++i) {
		if ((errnr = MT_join_thread(reader_threads[i])) != 0) {
			msg = createException(MAL, "bam_loader",
						  "Could not join alignment processing thread (errnr %d)",
						  errnr);
			goto cleanup;
		}
		/* Thread finished ok, append its error message, if any */
		if (r_thread_data[i].msg != MAL_SUCCEED) {
			int step;

			if (msg == MAL_SUCCEED) {
				/* First encountered thread error,
				 * indicate this by pointing to error
				 * buf */
				msg = buf_threads_msg;
			}
			/* snprintf returns -1 on failure; since we
			 * don't want to fail when snprintf fails, we
			 * use MAX to make sure we don't add a
			 * negative amount to threads_msg_len */
			step = snprintf(msg + threads_msg_len,
					4096 - threads_msg_len, "* %s\n",
					r_thread_data[i].msg);
			threads_msg_len += MAX(0, step);
			GDKfree(r_thread_data[i].msg);
		}
	}

	/* Fail if any thread has failed */
	if (msg != MAL_SUCCEED) {
		/* Do not use REUSE_EXCEPTION here, since msg was not
		 * malloced. Instead, just copy buffer contents to
		 * malloced buffer */
		msg = GDKstrdup(msg);
		goto cleanup;
	}

	TO_LOG("<bam_loader> Copying data into DB...\n");
	/* All threads finished succesfully, copy all data into DB */
	for (i = 0; i < nr_files; ++i) {
		if ((msg = copy_into_db(cntxt, bws + i)) != MAL_SUCCEED) {
			goto cleanup;
		}
	}

	  cleanup:
	if (bws) {
		for (i = 0; i < nr_files; ++i) {
			if (bws + i)
				clear_bam_wrapper(bws + i);
		}
		GDKfree(bws);
	}
	if (reader_threads)
		GDKfree(reader_threads);
	if (r_thread_data)
		destroy_reader_thread_data(r_thread_data);

	if (msg != MAL_SUCCEED) {
		TO_LOG("<bam_loader> Error on processing BAM files: %s\n",
			   msg);
	}

	TO_LOG("<bam_loader>: Loader finished processing %d BAM file%s...\n",
		   nr_files, (nr_files != 1 ? "s" : ""));
	return msg;
}
Exemplo n.º 2
0
/* this is called for each file to process */
enum codec_status codec_run(void)
{
    int error = CODEC_ERROR;

    SpeexBits bits;
    int eof = 0;
    spx_ogg_sync_state oy;
    spx_ogg_page og;
    spx_ogg_packet op;
    spx_ogg_stream_state os;
    spx_int64_t page_granule = 0;
    spx_int64_t cur_granule = 0;
    int enh_enabled = 1;
    int nframes = 2;
    int eos = 0;
    SpeexStereoState *stereo;
    int channels = -1;
    int samplerate = ci->id3->frequency;
    int extra_headers = 0;
    int stream_init = 0;
    /* rockbox: comment 'set but unused' variables
    int page_nb_packets;
    */
    int frame_size;
    int packet_count = 0;
    int lookahead;
    int headerssize = 0;
    unsigned long strtoffset = ci->id3->offset;
    void *st = NULL;
    int j = 0;
    intptr_t param;

    memset(&bits, 0, sizeof(bits));
    memset(&oy, 0, sizeof(oy));

    /* Ogg handling still uses mallocs, so reset the malloc buffer per track */
    if (codec_init()) {
        goto exit;
    }

    ci->seek_buffer(0);
    ci->set_elapsed(0);

    stereo = speex_stereo_state_init();
    spx_ogg_sync_init(&oy);
    spx_ogg_alloc_buffer(&oy,2*CHUNKSIZE);

    codec_set_replaygain(ci->id3);

    eof = 0;
    while (!eof) {
        enum codec_command_action action = ci->get_command(&param);

        if (action == CODEC_ACTION_HALT)
            break;

        /*seek (seeks to the page before the position) */
        if (action == CODEC_ACTION_SEEK_TIME) {
            if(samplerate!=0&&packet_count>1){
                LOGF("Speex seek page:%lld,%lld,%ld,%lld,%d\n",
                     ((spx_int64_t)param/1000) *
                     (spx_int64_t)samplerate,
                     page_granule, param,
                     (page_granule/samplerate)*1000, samplerate);

                speex_seek_page_granule(((spx_int64_t)param/1000) *
                                        (spx_int64_t)samplerate,
                                        page_granule, &oy, headerssize);
            }

            ci->set_elapsed(param);
            ci->seek_complete();
        }

next_page:
        /*Get the ogg buffer for writing*/
        if(get_more_data(&oy)<1){/*read error*/
            goto done;
        }

        /* Loop for all complete pages we got (most likely only one) */
        while (spx_ogg_sync_pageout(&oy, &og) == 1) {
            int packet_no;
            if (stream_init == 0) {
                spx_ogg_stream_init(&os, spx_ogg_page_serialno(&og));
                stream_init = 1;
            }

            /* Add page to the bitstream */
            spx_ogg_stream_pagein(&os, &og);

            page_granule = spx_ogg_page_granulepos(&og);
            /* page_nb_packets = spx_ogg_page_packets(&og); */

            cur_granule = page_granule;

            /* Extract all available packets */
            packet_no=0;

            while (!eos && spx_ogg_stream_packetout(&os, &op)==1){
                /* If first packet, process as Speex header */
                if (packet_count==0){
                    st = process_header(&op, enh_enabled, &frame_size,
                                         &samplerate, &nframes, &channels,
                                         stereo, &extra_headers);

                    speex_decoder_ctl(st, SPEEX_GET_LOOKAHEAD, &lookahead);
                    if (!nframes)
                        nframes=1;

                    if (!st){
                        goto done;
                    }

                    ci->configure(DSP_SET_FREQUENCY, ci->id3->frequency);
                    ci->configure(DSP_SET_SAMPLE_DEPTH, 16);
                    if (channels == 2) {
                        ci->configure(DSP_SET_STEREO_MODE, STEREO_INTERLEAVED);
                    } else if (channels == 1) {
                        ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO);
                    }

                    /* Speex header in its own page, add the whole page
                       headersize */
                    headerssize += og.header_len+og.body_len;

                } else if (packet_count<=1+extra_headers){
                    /* add packet to headersize */
                    headerssize += op.bytes;

                    /* Ignore extra headers */
                } else {
                    if (packet_count <= 2+extra_headers) {
                        if (strtoffset) {
                            ci->seek_buffer(strtoffset);
                            spx_ogg_sync_reset(&oy);
                            packet_count++;
                            goto next_page;
                        }
                    }
                    packet_no++;

                    if (op.e_o_s) /* End of stream condition */
                        eos=1;

                    /* Set Speex bitstream to point to Ogg packet */
                    speex_bits_set_bit_buffer(&bits, (char *)op.packet,
                                                     op.bytes);
                    for (j = 0; j != nframes; j++){
                        int ret;

                        /* Decode frame */
                        ret = speex_decode_int(st, &bits, output);

                        if (ret == -1)
                            break;

                        if (ret == -2)
                            break;

                        if (speex_bits_remaining(&bits) < 0)
                            break;

                        if (channels == 2)
                            speex_decode_stereo_int(output, frame_size, stereo);

                        if (frame_size > 0) {
                            spx_int16_t *frame_start = output + lookahead;

                            if (channels == 2)
                                frame_start += lookahead;
                            ci->pcmbuf_insert(frame_start, NULL,
                                              frame_size - lookahead);
                            lookahead = 0;
                            /* 2 bytes/sample */
                            cur_granule += frame_size / 2;

                            ci->set_offset((long) ci->curpos);

                            ci->set_elapsed((samplerate == 0) ? 0 :
                                             cur_granule * 1000 / samplerate);
                         }
                    }
                }
                packet_count++;
            }
        }
    }

    error = CODEC_OK;
done:
    /* Clean things up for the next track */
    speex_bits_destroy(&bits);

    if (st)
        speex_decoder_destroy(st);

    if (stream_init)
       spx_ogg_stream_destroy(&os);

    spx_ogg_sync_destroy(&oy);

exit:
    return error;
}
Exemplo n.º 3
0
/*
 * brute force search sysent
 * this method works in all versions
 * returns a pointer to the sysent structure
 * Note: 32/64 bits compatible
 */
static void *
bruteforce_sysent(mach_vm_address_t *out_kernel_base)
{
    // retrieves the address of the IDT
    mach_vm_address_t idt_address = 0;
    get_addr_idt(&idt_address);
    LOG_DEBUG("IDT Address is 0x%llx", idt_address);
    // calculate the address of the int80 handler
    mach_vm_address_t int80_address = calculate_int80address(idt_address);
    // search backwards for the kernel base address (mach-o header)
    mach_vm_address_t kernel_base = find_kernel_base(int80_address);
    *out_kernel_base = kernel_base;
    uint64_t data_address = 0;
    uint64_t data_size = 0;
    // search for the __DATA segment
    process_header(kernel_base, &data_address, &data_size);
    uint64_t data_limit = data_address + data_size;
    // bruteforce search for sysent in __DATA segment
    while (data_address <= data_limit)
    {
        if (version_major == YOSEMITE || version_major == EL_CAPITAN)
        {
            struct sysent_yosemite *table = (struct sysent_yosemite*)data_address;
            if((void*)table != NULL &&
               table[SYS_exit].sy_narg      == 1 &&
               table[SYS_fork].sy_narg      == 0 &&
               table[SYS_read].sy_narg      == 3 &&
               table[SYS_wait4].sy_narg     == 4 &&
               table[SYS_ptrace].sy_narg    == 4 &&
               table[SYS_getxattr].sy_narg  == 6 &&
               table[SYS_listxattr].sy_narg == 4 &&
               table[SYS_recvmsg].sy_narg   == 3 )
            {
                LOG_DEBUG("exit() address is %p", (void*)table[SYS_exit].sy_call);
                LOG_DEBUG("no. of args for exit()  %d", table[SYS_exit].sy_narg);
                LOG_DEBUG("Success!! Done here.");
                return (void*)data_address;
            }
        }
        /* mavericks or higher */
        else if (version_major == MAVERICKS)
        {
            struct sysent_mavericks *table = (struct sysent_mavericks*)data_address;
            if((void*)table != NULL &&
               table[SYS_exit].sy_narg      == 1 &&
               table[SYS_fork].sy_narg      == 0 &&
               table[SYS_read].sy_narg      == 3 &&
               table[SYS_wait4].sy_narg     == 4 &&
               table[SYS_ptrace].sy_narg    == 4 &&
               table[SYS_getxattr].sy_narg  == 6 &&
               table[SYS_listxattr].sy_narg == 4 &&
               table[SYS_recvmsg].sy_narg   == 3 )
            {
                LOG_DEBUG("exit() address is %p", (void*)table[SYS_exit].sy_call);
                return (void*)data_address;
            }
        }
        /* all previous versions */
        else
        {
            struct sysent *table = (struct sysent*)data_address;
            if((void*)table != NULL &&
               table[SYS_exit].sy_narg      == 1 &&
               table[SYS_fork].sy_narg      == 0 &&
               table[SYS_read].sy_narg      == 3 &&
               table[SYS_wait4].sy_narg     == 4 &&
               table[SYS_ptrace].sy_narg    == 4 &&
               table[SYS_getxattr].sy_narg  == 6 &&
               table[SYS_listxattr].sy_narg == 4 &&
               table[SYS_recvmsg].sy_narg   == 3 )
            {
                LOG_DEBUG("exit() address is %p", (void*)table[SYS_exit].sy_call);
                return (void*)data_address;
            }
        }
        data_address++;
    }
    return NULL;
}
Exemplo n.º 4
0
http_status http_server::http_request_state::parse_header_line(const char* hline)
// Parse a header line.  Call this multiple lines, once with each
// header line, to build up m_req.
//
// Sets m_request_state to PARSE_BODY if hline is the last header line.
//
// Returns an error code >= 400 on error; < 400 on success.
{
	assert(m_request_state == PARSE_HEADER);

	if (hline[0] == '\r' && hline[1] == '\n')
	{
		// End of the header.
		
		// Finish the previous key.
		if (m_parse_key.length())
		{
			m_req.m_header.set(m_parse_key, m_parse_value);
		}

		return process_header();
	}

	if (is_linear_whitepace(hline[0]))
	{
		// Looks like a continuation.
		if (m_parse_key.length())
		{
			m_parse_value += " ";
			m_parse_value += trim_whitespace(hline);
			return HTTP_OK;
		}
		else
		{
			// Non-continuation line starts with whitespace.
			return HTTP_BAD_REQUEST;
		}
	}
	
	// Finish any previous key.
	if (m_parse_key.length())
	{
		m_req.m_header.set(m_parse_key, m_parse_value);
		m_parse_key = "";
		m_parse_value = "";
	}

	const char* first_colon = strchr(hline, ':');
	if (first_colon == NULL || first_colon == hline)
	{
		return HTTP_BAD_REQUEST;
	}

	// Start a key/value pair.
	m_parse_key = tu_string(hline, first_colon - hline).utf8_to_lower();

	// Fill in any value-so-far from an earlier occurrence of this header.
	assert(m_parse_value.length() == 0);
	m_req.m_header.get(m_parse_key, &m_parse_value);
	if (m_parse_value.length())
	{
		m_parse_value += ",";
	}
	
	m_parse_value += trim_whitespace(first_colon + 1);

	return HTTP_OK;
}
Exemplo n.º 5
0
static int run_test(const unsigned char *k,
                    const unsigned char *h,
                    const unsigned long long hlen,
                    const unsigned char *expected_m,
                    unsigned long long mlen,
                    const unsigned char *expected_c,
                    const unsigned long long expected_clen, 
                    const unsigned char *expected_t)
{
    poet_ctx_t ctx;
    const unsigned long long expected_mlen = mlen;
    unsigned char* c = (expected_clen == 0) ? 
        NULL : (unsigned char*)malloc((size_t)expected_clen);
    unsigned char* m = (mlen == 0) ? 
        NULL : (unsigned char*)malloc((size_t)mlen);
    unsigned char t[TAGLEN];
    unsigned long long clen;

    keysetup(&ctx, k);
    process_header(&ctx, h, hlen);
    encrypt_final(&ctx, expected_m, mlen, c, &clen, t);

    if (clen != expected_clen) {
        printf("Expected ciphertext length %llu, but was %llu bytes \n", 
            expected_clen, clen);
    }

    if (memcmp(expected_c, c, expected_clen)
        || memcmp(expected_t, t, TAGLEN)) {
        test_output(&ctx, k, KEYLEN, h, hlen, expected_m, mlen, 
            c, expected_clen, t, TAGLEN);
        puts("Encryption produced incorrect result");
        free(m);
        free(c);
        return -1;
    }

    keysetup(&ctx, k);
    process_header(&ctx, h, hlen);

    const int result = decrypt_final(&ctx, c, clen, t, m, &mlen);

    if (mlen != expected_mlen) {
        printf("Expected plaintext length %llu, but was %llu bytes \n", 
            expected_mlen, mlen);
    }

    test_output(&ctx, k, KEYLEN, h, hlen, m, mlen, c, expected_clen, t, TAGLEN);

    if (memcmp(expected_m, m, expected_mlen)) {
        puts("Decryption produced incorrect result");
        free(m);
        free(c);
        return -1;
    }
    
    if (result != 0) {
        puts("Verification failed");
    }
    
    free(m);
    free(c);
    return result;
}
Exemplo n.º 6
0
int main(int argc, char **argv)
{
   int c;
   int option_index = 0;
   char *inFile, *outFile;
   FILE *fin, *fout=NULL;
   short out[MAX_FRAME_SIZE];
   short output[MAX_FRAME_SIZE];
   int frame_size=0, granule_frame_size=0;
   void *st=NULL;
   CELTMode *mode=NULL;
   int packet_count=0;
   int stream_init = 0;
   int quiet = 0;
   ogg_int64_t page_granule=0, last_granule=0;
   int skip_samples=0, page_nb_packets;
   struct option long_options[] =
   {
      {"help", no_argument, NULL, 0},
      {"quiet", no_argument, NULL, 0},
      {"version", no_argument, NULL, 0},
      {"version-short", no_argument, NULL, 0},
      {"rate", required_argument, NULL, 0},
      {"mono", no_argument, NULL, 0},
      {"stereo", no_argument, NULL, 0},
      {"packet-loss", required_argument, NULL, 0},
      {0, 0, 0, 0}
   };
   ogg_sync_state oy;
   ogg_page       og;
   ogg_packet     op;
   ogg_stream_state os;
   int enh_enabled;
   int nframes=2;
   int print_bitrate=0;
   int close_in=0;
   int eos=0;
   int forceMode=-1;
   int audio_size=0;
   float loss_percent=-1;
   int channels=-1;
   int rate=0;
   int extra_headers=0;
   int wav_format=0;
   int lookahead=0;
   int celt_serialno = -1;
   int firstpacket = 1;

   enh_enabled = 1;

   /*Process options*/
   while(1)
   {
      c = getopt_long (argc, argv, "hvV",
                       long_options, &option_index);
      if (c==-1)
         break;
      
      switch(c)
      {
      case 0:
         if (strcmp(long_options[option_index].name,"help")==0)
         {
            usage();
            exit(0);
         } else if (strcmp(long_options[option_index].name,"quiet")==0)
         {
            quiet = 1;
         } else if (strcmp(long_options[option_index].name,"version")==0)
         {
            version();
            exit(0);
         } else if (strcmp(long_options[option_index].name,"version-short")==0)
         {
            version_short();
            exit(0);
         } else if (strcmp(long_options[option_index].name,"mono")==0)
         {
            channels=1;
         } else if (strcmp(long_options[option_index].name,"stereo")==0)
         {
            channels=2;
         } else if (strcmp(long_options[option_index].name,"rate")==0)
         {
            rate=atoi (optarg);
         } else if (strcmp(long_options[option_index].name,"packet-loss")==0)
         {
            loss_percent = atof(optarg);
         }
         break;
      case 'h':
         usage();
         exit(0);
         break;
      case 'v':
         version();
         exit(0);
         break;
      case 'V':
         print_bitrate=1;
         break;
      case '?':
         usage();
         exit(1);
         break;
      }
   }
   if (argc-optind!=2 && argc-optind!=1)
   {
      usage();
      exit(1);
   }
   inFile=argv[optind];

   if (argc-optind==2)
      outFile=argv[optind+1];
   else
      outFile = "";
   wav_format = strlen(outFile)>=4 && (
                                       strcmp(outFile+strlen(outFile)-4,".wav")==0
                                       || strcmp(outFile+strlen(outFile)-4,".WAV")==0);
   /*Open input file*/
   if (strcmp(inFile, "-")==0)
   {
#if defined WIN32 || defined _WIN32
      _setmode(_fileno(stdin), _O_BINARY);
#endif
      fin=stdin;
   }
   else 
   {
      fin = fopen(inFile, "rb");
      if (!fin)
      {
         perror(inFile);
         exit(1);
      }
      close_in=1;
   }


   /*Init Ogg data struct*/
   ogg_sync_init(&oy);
   
   /*Main decoding loop*/
   
   while (1)
   {
      char *data;
      int i, nb_read;
      /*Get the ogg buffer for writing*/
      data = ogg_sync_buffer(&oy, 200);
      /*Read bitstream from input file*/
      nb_read = fread(data, sizeof(char), 200, fin);      
      ogg_sync_wrote(&oy, nb_read);

      /*Loop for all complete pages we got (most likely only one)*/
      while (ogg_sync_pageout(&oy, &og)==1)
      {
         if (stream_init == 0) {
            ogg_stream_init(&os, ogg_page_serialno(&og));
            stream_init = 1;
         }
	 if (ogg_page_serialno(&og) != os.serialno) {
	    /* so all streams are read. */
	    ogg_stream_reset_serialno(&os, ogg_page_serialno(&og));
	 }
         /*Add page to the bitstream*/
         ogg_stream_pagein(&os, &og);
         page_granule = ogg_page_granulepos(&og);
         page_nb_packets = ogg_page_packets(&og);
         if (page_granule>0 && frame_size)
         {
            /* FIXME: shift the granule values if --force-* is specified */
            skip_samples = frame_size*(page_nb_packets*granule_frame_size*nframes - (page_granule-last_granule))/granule_frame_size;
            if (ogg_page_eos(&og))
               skip_samples = -skip_samples;
            /*else if (!ogg_page_bos(&og))
               skip_samples = 0;*/
         } else
         {
            skip_samples = 0;
         }
         /*printf ("page granulepos: %d %d %d\n", skip_samples, page_nb_packets, (int)page_granule);*/
         last_granule = page_granule;
         /*Extract all available packets*/
         while (!eos && ogg_stream_packetout(&os, &op) == 1 && op.bytes>=8)
         {
	    if (!memcmp(op.packet, "CELT    ", 8)) {
	       celt_serialno = os.serialno;
	    }
	    if (celt_serialno == -1 || os.serialno != celt_serialno)
	       break;
            /*If first packet, process as CELT header*/
            if (packet_count==0)
            {
               st = process_header(&op, enh_enabled, &frame_size, &granule_frame_size, &rate, &nframes, forceMode, &channels, &lookahead, &extra_headers, quiet, &mode);
               if (!st)
                  exit(1);
               if (!nframes)
                  nframes=1;
               fout = out_file_open(outFile, rate, &channels);

            } else if (packet_count==1)
            {
               if (!quiet)
                  print_comments((char*)op.packet, op.bytes);
            } else if (packet_count<=1+extra_headers)
            {
               /* Ignore extra headers */
            } else {
               int lost=0;
               if (loss_percent>0 && 100*((float)rand())/RAND_MAX<loss_percent)
                  lost=1;

               /*End of stream condition*/
               if (op.e_o_s && os.serialno == celt_serialno) /* don't care for anything except celt eos */
                  eos=1;
	       
               {
                  int ret;
                  /*Decode frame*/
                  if (!lost)
                     ret = celt_decode(st, (unsigned char*)op.packet, op.bytes, output);
                  else
                     ret = celt_decode(st, NULL, 0, output);

                  /*for (i=0;i<frame_size*channels;i++)
                    printf ("%d\n", (int)output[i]);*/

                  if (ret!=0)
                  {
                     fprintf (stderr, "Decoding error: corrupted stream?\n");
                     break;
                  }

                  if (print_bitrate) {
                     celt_int32 tmp=op.bytes;
                     char ch=13;
                     fputc (ch, stderr);
                     fprintf (stderr, "Bitrate in use: %d bytes/packet     ", tmp);
                  }
                  /*Convert to short and save to output file*/
                  if (strlen(outFile)!=0)
                  {
                     for (i=0;i<frame_size*channels;i++)
                        out[i]=le_short(output[i]);
                  } else {
                     for (i=0;i<frame_size*channels;i++)
                        out[i]=output[i];
                  }
                  {
                     int frame_offset = 0;
                     int new_frame_size = frame_size;
                     /*printf ("packet %d %d\n", packet_no, skip_samples);*/
                     /*fprintf (stderr, "packet %d %d %d\n", packet_no, skip_samples, lookahead);*/
                     if (firstpacket == 1)
                     {
                        /*printf ("chopping first packet\n");*/
                        new_frame_size -= lookahead;
                        frame_offset = lookahead;
                        firstpacket = 0;
                     }
                     if (new_frame_size>0)
                     {  
#if defined WIN32 || defined _WIN32
                        if (strlen(outFile)==0)
                           WIN_Play_Samples (out+frame_offset*channels, sizeof(short) * new_frame_size*channels);
                        else
#endif
                           fwrite(out+frame_offset*channels, sizeof(short), new_frame_size*channels, fout);
                  
                        audio_size+=sizeof(short)*new_frame_size*channels;
                     }
                  }
               }
            }
            packet_count++;
         }
      }
      if (feof(fin))
         break;

   }

   if (fout && wav_format)
   {
      if (fseek(fout,4,SEEK_SET)==0)
      {
         int tmp;
         tmp = le_int(audio_size+36);
         fwrite(&tmp,4,1,fout);
         if (fseek(fout,32,SEEK_CUR)==0)
         {
            tmp = le_int(audio_size);
            fwrite(&tmp,4,1,fout);
         } else
         {
            fprintf (stderr, "First seek worked, second didn't\n");
         }
      } else {
         fprintf (stderr, "Cannot seek on wave file, size will be incorrect\n");
      }
   }

   if (st)
   {
      celt_decoder_destroy(st);
      celt_mode_destroy(mode);
   } else {
      fprintf (stderr, "This doesn't look like a CELT file\n");
   }
   if (stream_init)
      ogg_stream_clear(&os);
   ogg_sync_clear(&oy);

#if defined WIN32 || defined _WIN32
   if (strlen(outFile)==0)
      WIN_Audio_close ();
#endif

   if (close_in)
      fclose(fin);
   if (fout != NULL)
      fclose(fout);   

   return 0;
}
int main(int argc, char **argv)
{
   int c;
   int option_index = 0;
   char *inFile, *outFile;
   FILE *fin, *fout=NULL, *frange=NULL;
   float *output;
   int frame_size=0;
   OpusMSDecoder *st=NULL;
   opus_int64 packet_count=0;
   int total_links=0;
   int stream_init = 0;
   int quiet = 0;
   ogg_int64_t page_granule=0;
   ogg_int64_t link_out=0;
   struct option long_options[] =
   {
      {"help", no_argument, NULL, 0},
      {"quiet", no_argument, NULL, 0},
      {"version", no_argument, NULL, 0},
      {"version-short", no_argument, NULL, 0},
      {"rate", required_argument, NULL, 0},
      {"gain", required_argument, NULL, 0},
      {"no-dither", no_argument, NULL, 0},
      {"packet-loss", required_argument, NULL, 0},
      {"save-range", required_argument, NULL, 0},
      {0, 0, 0, 0}
   };
   ogg_sync_state oy;
   ogg_page       og;
   ogg_packet     op;
   ogg_stream_state os;
   int close_in=0;
   int eos=0;
   ogg_int64_t audio_size=0;
   double last_coded_seconds=0;
   float loss_percent=-1;
   float manual_gain=0;
   int channels=-1;
   int mapping_family;
   int rate=0;
   int wav_format=0;
   int preskip=0;
   int gran_offset=0;
   int has_opus_stream=0;
   ogg_int32_t opus_serialno;
   int dither=1;
   shapestate shapemem;
   SpeexResamplerState *resampler=NULL;
   float gain=1;
   int streams=0;
   size_t last_spin=0;
#ifdef WIN_UNICODE
   int argc_utf8;
   char **argv_utf8;
#endif

   if(query_cpu_support()){
     fprintf(stderr,"\n\n** WARNING: This program with compiled with SSE%s\n",query_cpu_support()>1?"2":"");
     fprintf(stderr,"            but this CPU claims to lack these instructions. **\n\n");
   }

#ifdef WIN_UNICODE
   (void)argc;
   (void)argv;

   init_console_utf8();
   init_commandline_arguments_utf8(&argc_utf8, &argv_utf8);
#endif

   output=0;
   shapemem.a_buf=0;
   shapemem.b_buf=0;
   shapemem.mute=960;
   shapemem.fs=0;

   /*Process options*/
   while(1)
   {
      c = getopt_long (argc_utf8, argv_utf8, "hV",
                       long_options, &option_index);
      if (c==-1)
         break;

      switch(c)
      {
      case 0:
         if (strcmp(long_options[option_index].name,"help")==0)
         {
            usage();
            quit(0);
         } else if (strcmp(long_options[option_index].name,"quiet")==0)
         {
            quiet = 1;
         } else if (strcmp(long_options[option_index].name,"version")==0)
         {
            version();
            quit(0);
         } else if (strcmp(long_options[option_index].name,"version-short")==0)
         {
            version_short();
            quit(0);
         } else if (strcmp(long_options[option_index].name,"no-dither")==0)
         {
            dither=0;
         } else if (strcmp(long_options[option_index].name,"rate")==0)
         {
            rate=atoi (optarg);
         } else if (strcmp(long_options[option_index].name,"gain")==0)
         {
            manual_gain=atof (optarg);
         }else if(strcmp(long_options[option_index].name,"save-range")==0){
          frange=fopen_utf8(optarg,"w");
          if(frange==NULL){
            perror(optarg);
            fprintf(stderr,"Could not open save-range file: %s\n",optarg);
            fprintf(stderr,"Must provide a writable file name.\n");
            quit(1);
          }
         } else if (strcmp(long_options[option_index].name,"packet-loss")==0)
         {
            loss_percent = atof(optarg);
         }
         break;
      case 'h':
         usage();
         quit(0);
         break;
      case 'V':
         version();
         quit(0);
         break;
      case '?':
         usage();
         quit(1);
         break;
      }
   }
   if (argc_utf8-optind!=2 && argc_utf8-optind!=1)
   {
      usage();
      quit(1);
   }
   inFile=argv_utf8[optind];

   /*Output to a file or playback?*/
   if (argc_utf8-optind==2){
     /*If we're outputting to a file, should we apply a wav header?*/
     int i;
     char *ext;
     outFile=argv_utf8[optind+1];
     ext=".wav";
     i=strlen(outFile)-4;
     wav_format=i>=0;
     while(wav_format&&ext&&outFile[i]) {
       wav_format&=tolower(outFile[i++])==*ext++;
     }
   }else {
     outFile="";
     wav_format=0;
     /*If playing to audio out, default the rate to 48000
       instead of the original rate. The original rate is
       only important for minimizing surprise about the rate
       of output files and preserving length, which aren't
       relevant for playback. Many audio devices sound
       better at 48kHz and not resampling also saves CPU.*/
     if(rate==0)rate=48000;
   }

   /*Open input file*/
   if (strcmp(inFile, "-")==0)
   {
#if defined WIN32 || defined _WIN32
      _setmode(_fileno(stdin), _O_BINARY);
#endif
      fin=stdin;
   }
   else
   {
      fin = fopen_utf8(inFile, "rb");
      if (!fin)
      {
         perror(inFile);
         quit(1);
      }
      close_in=1;
   }

   /* .opus files use the Ogg container to provide framing and timekeeping.
    * http://tools.ietf.org/html/draft-terriberry-oggopus
    * The easiest way to decode the Ogg container is to use libogg, so
    *  thats what we do here.
    * Using libogg is fairly straight forward-- you take your stream of bytes
    *  and feed them to ogg_sync_ and it periodically returns Ogg pages, you
    *  check if the pages belong to the stream you're decoding then you give
    *  them to libogg and it gives you packets. You decode the packets. The
    *  pages also provide timing information.*/
   ogg_sync_init(&oy);

   /*Main decoding loop*/
   while (1)
   {
      char *data;
      int i, nb_read;
      /*Get the ogg buffer for writing*/
      data = ogg_sync_buffer(&oy, 200);
      /*Read bitstream from input file*/
      nb_read = fread(data, sizeof(char), 200, fin);
      ogg_sync_wrote(&oy, nb_read);

      /*Loop for all complete pages we got (most likely only one)*/
      while (ogg_sync_pageout(&oy, &og)==1)
      {
         if (stream_init == 0) {
            ogg_stream_init(&os, ogg_page_serialno(&og));
            stream_init = 1;
         }
         if (ogg_page_serialno(&og) != os.serialno) {
            /* so all streams are read. */
            ogg_stream_reset_serialno(&os, ogg_page_serialno(&og));
         }
         /*Add page to the bitstream*/
         ogg_stream_pagein(&os, &og);
         page_granule = ogg_page_granulepos(&og);
         /*Extract all available packets*/
         while (ogg_stream_packetout(&os, &op) == 1)
         {
            /*OggOpus streams are identified by a magic string in the initial
              stream header.*/
            if (op.b_o_s && op.bytes>=8 && !memcmp(op.packet, "OpusHead", 8)) {
               if(!has_opus_stream)
               {
                 opus_serialno = os.serialno;
                 has_opus_stream = 1;
                 link_out = 0;
                 packet_count = 0;
                 eos = 0;
                 total_links++;
               } else {
                 fprintf(stderr,"Warning: ignoring opus stream %" I64FORMAT "\n",(long long)os.serialno);
               }
            }
            if (!has_opus_stream || os.serialno != opus_serialno)
               break;
            /*If first packet in a logical stream, process the Opus header*/
            if (packet_count==0)
            {
               st = process_header(&op, &rate, &mapping_family, &channels, &preskip, &gain, manual_gain, &streams, wav_format, quiet);
               if (!st)
                  quit(1);

               /*Remember how many samples at the front we were told to skip
                 so that we can adjust the timestamp counting.*/
               gran_offset=preskip;

               /*Setup the memory for the dithered output*/
               if(!shapemem.a_buf)
               {
                  shapemem.a_buf=calloc(channels,sizeof(float)*4);
                  shapemem.b_buf=calloc(channels,sizeof(float)*4);
                  shapemem.fs=rate;
               }
               if(!output)output=malloc(sizeof(float)*MAX_FRAME_SIZE*channels);

               /*Normal players should just play at 48000 or their maximum rate,
                 as described in the OggOpus spec.  But for commandline tools
                 like opusdec it can be desirable to exactly preserve the original
                 sampling rate and duration, so we have a resampler here.*/
               if (rate != 48000)
               {
                  int err;
                  resampler = speex_resampler_init(channels, 48000, rate, 5, &err);
                  if (err!=0)
                     fprintf(stderr, "resampler error: %s\n", speex_resampler_strerror(err));
                  speex_resampler_skip_zeros(resampler);
               }
               if(!fout)fout=out_file_open(outFile, &wav_format, rate, mapping_family, &channels);
            } else if (packet_count==1)
            {
               if (!quiet)
                  print_comments((char*)op.packet, op.bytes);
            } else {
               int ret;
               opus_int64 maxout;
               opus_int64 outsamp;
               int lost=0;
               if (loss_percent>0 && 100*((float)rand())/RAND_MAX<loss_percent)
                  lost=1;

               /*End of stream condition*/
               if (op.e_o_s && os.serialno == opus_serialno)eos=1; /* don't care for anything except opus eos */

               /*Are we simulating loss for this packet?*/
               if (!lost){
                  /*Decode Opus packet*/
                  ret = opus_multistream_decode_float(st, (unsigned char*)op.packet, op.bytes, output, MAX_FRAME_SIZE, 0);
               } else {
                  /*Extract the original duration.
                    Normally you wouldn't have it for a lost packet, but normally the
                    transports used on lossy channels will effectively tell you.
                    This avoids opusdec squaking when the decoded samples and
                    granpos mismatches.*/
                  opus_int32 lost_size;
                  lost_size = MAX_FRAME_SIZE;
                  if(op.bytes>0){
                    opus_int32 spp;
                    spp=opus_packet_get_nb_frames(op.packet,op.bytes);
                    if(spp>0){
                      spp*=opus_packet_get_samples_per_frame(op.packet,48000/*decoding_rate*/);
                      if(spp>0)lost_size=spp;
                    }
                  }
                  /*Invoke packet loss concealment.*/
                  ret = opus_multistream_decode_float(st, NULL, 0, output, lost_size, 0);
               }

               if(!quiet){
                  /*Display a progress spinner while decoding.*/
                  static const char spinner[]="|/-\\";
                  double coded_seconds = (double)audio_size/(channels*rate*sizeof(short));
                  if(coded_seconds>=last_coded_seconds+1){
                     fprintf(stderr,"\r[%c] %02d:%02d:%02d", spinner[last_spin&3],
                             (int)(coded_seconds/3600),(int)(coded_seconds/60)%60,
                             (int)(coded_seconds)%60);
                     fflush(stderr);
                     last_spin++;
                     last_coded_seconds=coded_seconds;
                  }
               }

               /*If the decoder returned less than zero, we have an error.*/
               if (ret<0)
               {
                  fprintf (stderr, "Decoding error: %s\n", opus_strerror(ret));
                  break;
               }
               frame_size = ret;

               /*If we're collecting --save-range debugging data, collect it now.*/
               if(frange!=NULL){
                 OpusDecoder *od;
                 opus_uint32 rngs[256];
                 for(i=0;i<streams;i++){
                   ret=opus_multistream_decoder_ctl(st,OPUS_MULTISTREAM_GET_DECODER_STATE(i,&od));
                   ret=opus_decoder_ctl(od,OPUS_GET_FINAL_RANGE(&rngs[i]));
                 }
                 save_range(frange,frame_size*(48000/48000/*decoding_rate*/),op.packet,op.bytes,
                            rngs,streams);
               }

               /*Apply header gain, if we're not using an opus library new
                 enough to do this internally.*/
               if (gain!=0){
                 for (i=0;i<frame_size*channels;i++)
                    output[i] *= gain;
               }

               /*This handles making sure that our output duration respects
                 the final end-trim by not letting the output sample count
                 get ahead of the granpos indicated value.*/
               maxout=((page_granule-gran_offset)*rate/48000)-link_out;
               outsamp=audio_write(output, channels, frame_size, fout, resampler, &preskip, dither?&shapemem:0, strlen(outFile)!=0,0>maxout?0:maxout);
               link_out+=outsamp;
               audio_size+=sizeof(short)*outsamp*channels;
            }
            packet_count++;
         }
         /*We're done, drain the resampler if we were using it.*/
         if(eos && resampler)
         {
            float *zeros;
            int drain;

            zeros=(float *)calloc(100*channels,sizeof(float));
            drain = speex_resampler_get_input_latency(resampler);
            do {
               opus_int64 outsamp;
               int tmp = drain;
               if (tmp > 100)
                  tmp = 100;
               outsamp=audio_write(zeros, channels, tmp, fout, resampler, NULL, &shapemem, strlen(outFile)!=0, ((page_granule-gran_offset)*rate/48000)-link_out);
               link_out+=outsamp;
               audio_size+=sizeof(short)*outsamp*channels;
               drain -= tmp;
            } while (drain>0);
            free(zeros);
            speex_resampler_destroy(resampler);
            resampler=NULL;
         }
         if(eos)
         {
            has_opus_stream=0;
            if(st)opus_multistream_decoder_destroy(st);
            st=NULL;
         }
      }
      if (feof(fin)) {
         if(!quiet) {
           fprintf(stderr, "\rDecoding complete.        \n");
           fflush(stderr);
         }
         break;
      }
   }

   /*If we were writing wav, go set the duration.*/
   if (strlen(outFile)!=0 && fout && wav_format>=0 && audio_size<0x7FFFFFFF)
   {
      if (fseek(fout,4,SEEK_SET)==0)
      {
         int tmp;
         tmp = le_int(audio_size+20+wav_format);
         if(fwrite(&tmp,4,1,fout)!=1)fprintf(stderr,"Error writing end length.\n");
         if (fseek(fout,16+wav_format,SEEK_CUR)==0)
         {
            tmp = le_int(audio_size);
            if(fwrite(&tmp,4,1,fout)!=1)fprintf(stderr,"Error writing header length.\n");
         } else
         {
            fprintf (stderr, "First seek worked, second didn't\n");
         }
      } else {
         fprintf (stderr, "Cannot seek on wave file, size will be incorrect\n");
      }
   }

   /*Did we make it to the end without recovering ANY opus logical streams?*/
   if(!total_links)fprintf (stderr, "This doesn't look like a Opus file\n");

   if (stream_init)
      ogg_stream_clear(&os);
   ogg_sync_clear(&oy);

#if defined WIN32 || defined _WIN32
   if (strlen(outFile)==0)
      WIN_Audio_close ();
#endif

   if(shapemem.a_buf)free(shapemem.a_buf);
   if(shapemem.b_buf)free(shapemem.b_buf);

   if(output)free(output);

   if(frange)fclose(frange);

   if (close_in)
      fclose(fin);
   if (fout != NULL)
      fclose(fout);

#ifdef WIN_UNICODE
   free_commandline_arguments_utf8(&argc_utf8, &argv_utf8);
   uninit_console_utf8();
#endif

   return 0;
}
Exemplo n.º 8
0
void AudioStreamPlaybackSpeex::reload() {

	if (active)
		unload();

	if (!data.size())
		return;

	ogg_sync_init(&oy);
	speex_bits_init(&bits);

	read_ofs = 0;
	//	char *buf;

	int packet_count = 0;
	int extra_headers = 0;
	int stream_init = 0;

	page_granule = 0;
	last_granule = 0;
	skip_samples = 0;
	page_nb_packets = 0;
	packets_available = false;
	packet_no = 0;

	int eos = 0;

	do {

		/*Get the ogg buffer for writing*/
		int nb_read = MIN(data.size() - read_ofs, READ_CHUNK);
		char *ogg_dst = ogg_sync_buffer(&oy, nb_read);
		/*Read bitstream from input file*/
		copymem(ogg_dst, &data[read_ofs], nb_read);
		read_ofs += nb_read;
		ogg_sync_wrote(&oy, nb_read);

		/*Loop for all complete pages we got (most likely only one)*/
		while (ogg_sync_pageout(&oy, &og) == 1) {

			if (stream_init == 0) {
				ogg_stream_init(&os, ogg_page_serialno(&og));
				stream_init = 1;
			}
			/*Add page to the bitstream*/
			ogg_stream_pagein(&os, &og);
			page_granule = ogg_page_granulepos(&og);
			page_nb_packets = ogg_page_packets(&og);
			if (page_granule > 0 && frame_size) {
				skip_samples = page_nb_packets * frame_size * nframes - (page_granule - last_granule);
				if (ogg_page_eos(&og))
					skip_samples = -skip_samples;
				/*else if (!ogg_page_bos(&og))
				  skip_samples = 0;*/
			} else {
				skip_samples = 0;
			}

			last_granule = page_granule;
			/*Extract all available packets*/
			while (!eos && ogg_stream_packetout(&os, &op) == 1) {
				/*If first packet, process as Speex header*/
				if (packet_count == 0) {
					int rate = 0;
					int channels;
					st = process_header(&op, &frame_size, &rate, &nframes, &channels, &extra_headers);
					if (!nframes)
						nframes = 1;
					if (!st) {
						unload();
						return;
					};

					page_size = nframes * frame_size;
					stream_srate = rate;
					stream_channels = channels;
					stream_minbuff_size = page_size;

				} else if (packet_count == 1) {
				} else if (packet_count <= 1 + extra_headers) {
					/* Ignore extra headers */
				};
			};
			++packet_count;
		};

	} while (packet_count <= extra_headers);

	active = true;
}
Exemplo n.º 9
0
int read_seq (FILE *IN, seq_t *seq_holder, char *alphabet) {

  char *BUFF, *stock, *p, *q;
  int i, state, l;
  size_t buffsize, bufflen, seq_len;

  /* check if there is something to read */
  if ((i = fgetc(IN)) == EOF && feof(IN) != 0) {
    return NO_SEQ; }

  ungetc(i, IN);

  state = SEQ_NONE;
  seq_len = bufflen = 0;
  buffsize = BUFFSIZE;
  seq_holder->seq = NULL;

  if ((BUFF = (char *) malloc(sizeof(char) * buffsize+1 )) == NULL)
    error_fatal ("memory1" , NULL);
  if ((stock = (char *) malloc(sizeof(char) * buffsize+1 )) == NULL)
    error_fatal ("memory2" , NULL);

  *stock = '\0';
  q = stock;

  while ((i = fgetc(IN)) != EOF) {
    /* skip empty lines */
    if (isspace(i)) {continue;}
    
    if ( ungetc(i, IN) == EOF ) error_fatal ("ungetc", NULL);

    /* end entry or start entry */
    if ( i == '>' ) {
      if (state == SEQ_NONE )  state = HEADER;
      if (state == SEQ) break ;
    }

    if (fgets(BUFF, BUFFSIZE + 1, IN) == NULL)  break;

    /* header processing */
    if ( state == HEADER ) {

      /* memcopy with '/0' inclusion */
      memcpy(q, BUFF, BUFFSIZE+1);
      /* is header line completly read */
      if (strrchr(BUFF, '\n') == NULL) {
	if((stock = realloc(stock, buffsize + BUFFSIZE + 1)) == NULL)
	  error_fatal("realloc", NULL);
	q = stock + buffsize ;
	buffsize += BUFFSIZE ;
	continue;
      }
      process_header(seq_holder, stock);
      state = SEQ;
      buffsize = BUFFSIZE;
      p = stock;
      *p ='\0';
      continue;
    }

    if ( state == SEQ || state == DUMP ){
      /* we clean the buffer before any further use */
      if ((l = clean_buff(&BUFF,  alphabet)) == -1)
	error_fatal(seq_holder->id, "sequence contain spurious characters");
    }

    if (state == SEQ ) {
      bufflen = l;
      /* is stock buffer long enough */
      if ( seq_len + bufflen >= buffsize ) {
	buffsize += BUFFSIZE;
	if ((stock = realloc(stock, sizeof(char) * buffsize+1)) == NULL)
	  error_fatal("Memory3", "Reallocating seq");
      }
      q = stock + seq_len;
      strncpy (q, BUFF, bufflen);
      seq_len += bufflen;
    }

    if ( state == SEQ_NONE ) {
      error_fatal("Sequence", "is NOT fasta formated");
    }
  }


  free(BUFF);
  if ( state == SEQ ) {
    *(stock+seq_len) = '\0';
    seq_holder->seq = stock;
  }
  seq_holder->size = seq_len;

  return state;

}
Exemplo n.º 10
0
Error_t parser_parse_http(Parser_t* par, char* buf, int size, uint8_t start)
{
	char* cur = buf;
	char* end = buf + size;

	if (start)
		init_data (par);

	while (cur < end )
	{
		char c = *cur;
		++cur;

		switch(par->state)
		{
		case HTTP_START_LINE:
			if(c == '\n')
				par->state = HTTP_HEADER_NAME;

			break;

		case HTTP_HEADER_NAME:
			if (is_token(c))
			{
				*par->hn_pos = c;
				++par->hn_pos;
			}
			else if(c == ':')
			{
				// Finished to read header name, start reading header value
				par->state = HTTP_HEADER_VAL;
				*par->hn_pos=0;
				par->hn_pos = par->header_name;
			}
			else if (c == '\n' && par->hn_pos == par->header_name)
			{
				// '/n' met twice and no headers between then => end of the header state
				par->state 	= HTTP_BODY;
				par->header_len += cur-buf;
				par->msg_len 	= par->header_len + par->content_len;
			}

			break;

		case HTTP_HEADER_VAL:
			if(c == '\n')
			{
				// Finished to read header value, process this header and start reading the next one
				par->state 	= HTTP_HEADER_NAME;
				*par->hv_pos 	= 0;
				par->hv_pos		= par->header_val;

				process_header(par);
			}
			else
			{
				*par->hv_pos = c;
				++par->hv_pos;
			}

			break;

		case HTTP_BODY:
		default:
			// TODO Add chunked body parsing here
			return ERR_OK;
		}

	}

	if(par->state != HTTP_BODY)
		par->header_len = cur-buf;

	return ERR_OK;
}
Exemplo n.º 11
0
HRESULT CEASpliterFilter::CreateOutputs(IAsyncReader* pAsyncReader)
{
	SVP_LogMsg5(L" CEASpliterFilter::CreateOutputs");
	CheckPointer(pAsyncReader, E_POINTER);

	HRESULT hr = E_FAIL;

	m_pFile.Free();

	m_pFile.Attach(new CEAFile(pAsyncReader, hr));
	if(!m_pFile) return E_OUTOFMEMORY;
	if(FAILED(hr)) {m_pFile.Free(); return hr;}

	m_rtNewStart = m_rtCurrent = 0;
	m_rtNewStop = m_rtStop = m_rtDuration = 0;

	m_pFile->Seek(0);
	DWORD EAFileTag = bswap_32((DWORD)m_pFile->BitRead(32));

	switch(EAFileTag)
	{
		case ISNh_TAG:
		case SCHl_TAG:
		case SEAD_TAG:
		case SHEN_TAG:
		case kVGT_TAG:
		case MADk_TAG:
		case MPCh_TAG:
		case MVhd_TAG:
		case MVIh_TAG:
			SVP_LogMsg5(L"GotEA Tag");
			break;
		default:
			return hr;
	}

	m_pFile->Seek(0);
	EaDemuxContext *ea = (EaDemuxContext *)m_pea;
	SVP_LogMsg5(L"EABefore Info %x %x", ea->audio_codec, ea->video_codec);;
	process_header();
	SVP_LogMsg5(L"GotEA Info  %d %d", ea->audio_codec, ea->video_codec);;
	int idPin = 0;
	while(ea->video_codec){
		CMediaType mt;
		mt.SetSampleSize(1);
		mt.subtype = GUID_NULL;
		switch(ea->video_codec){

			case CODEC_ID_VP6:
				{
					mt.majortype = MEDIATYPE_Video;
					mt.formattype = FORMAT_VideoInfo;
					VIDEOINFOHEADER* vih = (VIDEOINFOHEADER*)mt.AllocFormatBuffer(sizeof(VIDEOINFOHEADER));
					memset(vih, 0, sizeof(VIDEOINFOHEADER));
					BITMAPINFOHEADER* bih = &vih->bmiHeader;
					vih->bmiHeader.biWidth = ea->width;
					vih->bmiHeader.biHeight = ea->height;
					bih->biCompression = '26PV';
					mt.subtype = MEDIASUBTYPE_VP62;
				}
				break;
			case CODEC_ID_MPEG2VIDEO:
				//TODO: handle MPEG2 in vp6
				/*mt.formattype = FORMAT_MPEG2Video;
				MPEG2VIDEOINFO* vih = (MPEG2VIDEOINFO*)mt.AllocFormatBuffer(FIELD_OFFSET(MPEG2VIDEOINFO, dwSequenceHeader) + headerSize);
				memset(vih, 0, mt.FormatLength());
				vih->hdr.bmiHeader.biSize = sizeof(vih->hdr.bmiHeader);
				vih->hdr.bmiHeader.biPlanes = 1;
				vih->hdr.bmiHeader.biBitCount = 24;
				vih->hdr.bmiHeader.biWidth =  ea->width;
				vih->hdr.bmiHeader.biHeight = ea->height;
				*/
			case CODEC_ID_MDEC:
			case CODEC_ID_CMV:
			case CODEC_ID_TGV:
			case CODEC_ID_TGQ:
			case CODEC_ID_TQI:
			case CODEC_ID_MAD:
				SVP_LogMsg5(L"sorry we cant handle this now");
				break;
		}
		
		if(mt.subtype == GUID_NULL)
			break;
		CAtlArray<CMediaType> mts;
		mts.Add(mt);
		CAutoPtr<CBaseSplitterOutputPin> pPinOut(new CBaseSplitterOutputPin(mts, L"Video", this, this, &hr));
		EXECUTE_ASSERT(SUCCEEDED(AddOutputPin(idPin, pPinOut)));
		
		idPin++;
		break;
	}
	if(ea->audio_codec){
		CMediaType mt;
		mt.SetSampleSize(1);
		

		mt.majortype = MEDIATYPE_Audio;
		mt.formattype = FORMAT_WaveFormatEx;
		
		switch(ea->audio_codec){
			case CODEC_ID_MP3:
				mt.subtype = MEDIASUBTYPE_MP3;
				{
					WAVEFORMATEX* wfe = (WAVEFORMATEX*)mt.AllocFormatBuffer(sizeof(WAVEFORMATEX));
					memset(wfe, 0, sizeof(WAVEFORMATEX));
					wfe->nSamplesPerSec = ea->sample_rate;
					wfe->wBitsPerSample = ea->bytes * 8;
					wfe->nChannels = ea->num_channels;
				}
				break;
			case CODEC_ID_ADPCM_EA:
			case CODEC_ID_ADPCM_EA_R1:
			case CODEC_ID_ADPCM_EA_R2:
			case CODEC_ID_ADPCM_EA_R3:
			case CODEC_ID_PCM_S16LE_PLANAR:
			case CODEC_ID_PCM_S8:
			case CODEC_ID_PCM_S16LE:
			case CODEC_ID_PCM_MULAW:
			case CODEC_ID_ADPCM_IMA_EA_EACS:
			case CODEC_ID_ADPCM_IMA_EA_SEAD:
				break;
		}
		
		if(mt.subtype != GUID_NULL){

			CAtlArray<CMediaType> mts;
			mts.Add(mt);
			CAutoPtr<CBaseSplitterOutputPin> pPinOut(new CBaseSplitterOutputPin(mts, L"Audio", this, this, &hr));
			EXECUTE_ASSERT(SUCCEEDED(AddOutputPin(idPin, pPinOut)));
		}

	}
	
	SVP_LogMsg5(L"EA Out %d", m_pOutputs.GetCount());
	m_rtNewStop = m_rtStop = m_rtDuration ;

	return m_pOutputs.GetCount() > 0 ? S_OK : E_FAIL;
}
Exemplo n.º 12
0
/* Read the speex header. Return 0 on error. */
static int read_speex_header (struct spx_data *data)
{
	int packet_count = 0;
	int stream_init = 0;
	char *buf;
	ssize_t nb_read;
	int header_packets = 2;

	while (packet_count < header_packets) {

		/* Get the ogg buffer for writing */
		buf = ogg_sync_buffer (&data->oy, 200);

		/* Read bitstream from input file */
		nb_read = io_read (data->stream, buf, 200);

		if (nb_read < 0) {
			decoder_error (&data->error, ERROR_FATAL, 0,
					"Can't open speex file: IO error: %s",
					io_strerror(data->stream));
			return 0;
		}

		if (nb_read == 0) {
			decoder_error (&data->error, ERROR_FATAL, 0,
					"Can't open speex header");
			return 0;
		}

		ogg_sync_wrote (&data->oy, nb_read);

		/* Loop for all complete pages we got (most likely only one) */
		while (ogg_sync_pageout(&data->oy, &data->og) == 1) {

			if (stream_init == 0) {
				ogg_stream_init(&data->os,
						ogg_page_serialno(&data->og));
				stream_init = 1;
			}

			/* Add page to the bitstream */
			ogg_stream_pagein (&data->os, &data->og);

			/* Extract all available packets FIXME: EOS! */
			while (ogg_stream_packetout(&data->os, &data->op) == 1) {

				/* If first packet, process as Speex header */
				if (packet_count == 0) {
					data->st = process_header (data);

					if (!data->st) {
						ogg_stream_clear (&data->os);
						return 0;
					}

					data->rate = data->header->rate;
					data->nchannels
						= data->header->nb_channels;
					data->frames_per_packet
						= data->header->frames_per_packet;
					/*data->vbr = data->header->vbr; */

					if (!data->frames_per_packet)
						data->frames_per_packet=1;

					data->output = xmalloc (data->frame_size *
							data->nchannels *
							data->frames_per_packet *
							sizeof(int16_t));
					data->output_start = 0;
					data->output_left = 0;

					header_packets += data->header->extra_headers;
				}
				else if (packet_count == 1) {
					data->comment_packet_len
						= data->op.bytes;
					data->comment_packet = xmalloc (
							sizeof(char) *
							data->comment_packet_len);
					memcpy (data->comment_packet,
							data->op.packet,
							data->comment_packet_len);
				}

				packet_count++;
			}
		}
	}

	return 1;
}
Exemplo n.º 13
0
int
main(int argc, char ** argv)
{
    	
	header();
    	
	// we need to run this as root
	if (getuid() != 0)
	{
		printf("[ERROR] Please run me as root!\n");
		exit(1);
	}
	
	int8_t kernel_type = get_kernel_type();
	if (kernel_type == -1)
	{
		printf("[ERROR] Unable to retrieve kernel type!\n");
		exit(1);
	}
	
	if((fd_kmem = open("/dev/kmem",O_RDWR)) == -1)
	{
		fprintf(stderr,"[ERROR] Error while opening /dev/kmem. Is /dev/kmem enabled?\n");
		fprintf(stderr,"Add parameter kmem=1 to /Library/Preferences/SystemConfiguration/com.apple.Boot.plist\n");
		exit(1);
	}
	    
	// retrieve int80 address
    idt_t idt_address = get_addr_idt(kernel_type);
    uint64_t int80_address = calculate_int80address(idt_address, kernel_type);
    
    uint64_t kernel_base = find_kernel_base(int80_address, kernel_type);
    if (kernel_base == 0)
    {
        fprintf(stderr, "[ERROR] Could not find kernel base address!\n");
        exit(1);
    }
    uint64_t data_address = 0;
    uint64_t data_size    = 0;
    
    process_header(kernel_base, &data_address, &data_size);
    
    uint8_t *read = malloc((size_t)data_size);
	if (read == NULL)
    {
        printf("[ERROR] Memory allocation failed!\n");
        exit(1);
    }

	// read kernel memory and find sysent
    readkmem(fd_kmem, read, data_address, (size_t)data_size);
    uint64_t sysent_address = find_sysent(read, data_address, data_size);
    
    if (sysent_address)
    {
        printf("[OK] Found sysent address at %p\n",(void*)sysent_address);
    }
    else
    {
        printf("[ERROR] Could not found sysent address!\n");
    }

    free(read);
	return 0;
}