예제 #1
0
void buffer_test(void)
{
    uint8 d1[] = { 1,2,3,4,5,0xf0,5,6,0xff,0xf0,0xf1,0xf0,0x02,0xff,4,5};
    uint8 d2[] = { 1,2,3,4,5,0xf0,5,6,0xff,0xf0,0xf2,0x0,0x02,0xff,4,5};
    uint8 d3[] = { 1,2,3,4,5,0xf0,5,6,0xff,0xf3,0x0,0x02,0xf0,4,5};
    uint8 d4[] = { 1,2,3,4,5,0xf0,5,6,0xff,0xf4,0x0,0x02,0xff,4,5};

    buffer_write(d1,sizeof(d1)); while(buffer_check());
    buffer_write(d2,sizeof(d2)); while(buffer_check());
    buffer_write(d3,sizeof(d3)); while(buffer_check());
    buffer_write(d4,sizeof(d4)); while(buffer_check());
}
예제 #2
0
void TestGame::scan_line(int y, Vertex * va, Vertex * vb, Vertex * vc, 
    Vertex * vd, Texture * texture) {
    float gradient1 = va->coordinates.y != vb->coordinates.y ?
        (y - va->coordinates.y) / (vb->coordinates.y - va->coordinates.y) : 1,
        gradient2 = vc->coordinates.y != vd->coordinates.y ?
        (y - vc->coordinates.y) / (vd->coordinates.y - vc->coordinates.y) : 1;

    int sx = (int)interpolate(va->coordinates.x, vb->coordinates.x, gradient1),
        ex = (int)interpolate(vc->coordinates.x, vd->coordinates.x, gradient2);

    float z1 = interpolate(va->coordinates.z, vb->coordinates.z, gradient1),
        z2 = interpolate(vc->coordinates.z, vd->coordinates.z, gradient2),
        su = interpolate(va->texture_coordinates.x, vb->texture_coordinates.x,
            gradient1),
        eu = interpolate(vc->texture_coordinates.x, vd->texture_coordinates.x,
            gradient2),

        sv = interpolate(va->texture_coordinates.y, vb->texture_coordinates.y,
            gradient1),
        ev = interpolate(vc->texture_coordinates.y, vd->texture_coordinates.y,
            gradient2);

    for (int x = sx; x < ex; x++) {
        float gradient = (float)(x - sx) / (ex - sx);
        float z = interpolate(z1, z2, gradient);

        if (!buffer_check(x, y, z)) continue;

        buffer_set(x, y, z, texture->map(
            interpolate(su, eu, gradient),
            interpolate(sv, ev, gradient)));
    }
}
예제 #3
0
static void ebs_overflow_handler(struct perf_event *event, struct perf_sample_data *data, struct pt_regs *regs)
#endif
{
	unsigned int value, delta, cpu = smp_processor_id(), buftype = EVENT_BUF;

	if (event != per_cpu(pevent, cpu))
		return;

	if (buffer_check_space(cpu, buftype, 5 * MAXSIZE_PACK32 + MAXSIZE_PACK64)) {
		value = local64_read(&event->count);
		delta = value - per_cpu(prev_value, cpu);
		per_cpu(prev_value, cpu) = value;

		// Counters header
		gator_buffer_write_packed_int(cpu, buftype, MESSAGE_COUNTERS);     // type
		gator_buffer_write_packed_int64(cpu, buftype, gator_get_time());   // time

		// Output counter
		gator_buffer_write_packed_int(cpu, buftype, 2);                    // length
		gator_buffer_write_packed_int(cpu, buftype, per_cpu(key, cpu));    // key
		gator_buffer_write_packed_int(cpu, buftype, delta);                // delta

		// End Counters, length of zero
		gator_buffer_write_packed_int(cpu, buftype, 0);
	}

	// Output backtrace
	if (buffer_check_space(cpu, buftype, gator_backtrace_depth * 2 * MAXSIZE_PACK32))
		gator_add_sample(cpu, buftype, regs);

	// Check and commit; commit is set to occur once buffer is 3/4 full
	buffer_check(cpu, buftype);
}
예제 #4
0
/*
 * Convert the names of personal mailboxes, using the namespace specified
 * by the mail server, from internal to mail server format.
 */
const char *
apply_namespace(const char *mbox, char *prefix, char delim)
{
	int n;
	char *c;

	if ((prefix == NULL && delim == '\0') ||
	    (prefix == NULL && delim == '/') ||
	    !strcasecmp(mbox, "INBOX"))
		return mbox;

	buffer_reset(&nbuf);

	n = snprintf(nbuf.data, nbuf.size + 1, "%s%s", (prefix ? prefix : ""),
	    mbox);
	if (n > (int)nbuf.size) {
		buffer_check(&nbuf, n);
		snprintf(nbuf.data, nbuf.size + 1, "%s%s",
		    (prefix ? prefix : ""), mbox);
	}
	c = nbuf.data;
	while ((c = strchr(c, '/')))
		*(c++) = delim;

	debug("namespace: '%s' -> '%s'\n", mbox, nbuf.data);

	return nbuf.data;
}
예제 #5
0
/*
 * Convert the names of personal mailboxes, using the namespace specified by
 * the mail server, from mail server format to internal format.
 */
const char *
reverse_namespace(const char *mbox, char *prefix, char delim)
{
	int n, o;
	char *c;

	if ((prefix == NULL && delim == '\0') ||
	    (prefix == NULL && delim == '/') ||
	    !strcasecmp(mbox, "INBOX"))
		return mbox;

	buffer_reset(&nbuf);

	o = strlen(prefix ? prefix : "");
	if (strncasecmp(mbox, (prefix ? prefix : ""), o))
		o = 0;

	n = snprintf(nbuf.data, nbuf.size + 1, "%s", mbox + o);
	if (n > (int)nbuf.size) {
		buffer_check(&nbuf, n);
		snprintf(nbuf.data, nbuf.size + 1, "%s", mbox + o);
	}
	c = nbuf.data;
	while ((c = strchr(c, delim)))
		*(c++) = '/';

	debug("namespace: '%s' <- '%s'\n", mbox, nbuf.data);

	return nbuf.data;
}
예제 #6
0
파일: response.c 프로젝트: booo/imapfilter
/*
 * Get server data and make sure there is a continuation response inside them.
 */
int
response_continuation(session *ssn, int tag)
{
	int r;
	ssize_t n;

	buffer_reset(&ibuf);

	do {
		buffer_check(&ibuf, ibuf.len + INPUT_BUF);
		if ((n = receive_response(ssn, ibuf.data + ibuf.len, 0, 1)) ==
		    -1)
			return -1;
		ibuf.len += n;

		if (check_bye(ibuf.data))
			return STATUS_BYE;
	} while ((r = check_tag(ibuf.data, ssn, tag)) == STATUS_NONE &&
	    !check_continuation(ibuf.data));

	if (r == STATUS_NO &&
	    (check_trycreate(ibuf.data) || get_option_boolean("create")))
		return STATUS_TRYCREATE;

	if (r == STATUS_NONE)
		return STATUS_CONTINUE;

	return r;
}
static int annotate_release(struct inode *inode, struct file *file)
{
	int cpu = 0;

	/* synchronize between cores */
	spin_lock(&annotate_lock);

	if (per_cpu(gator_buffer, cpu)[ANNOTATE_BUF] && buffer_check_space(cpu, ANNOTATE_BUF, MAXSIZE_PACK64 + 3 * MAXSIZE_PACK32)) {
		uint32_t pid = current->pid;

		gator_buffer_write_packed_int(cpu, ANNOTATE_BUF, get_physical_cpu());
		gator_buffer_write_packed_int(cpu, ANNOTATE_BUF, pid);
		/* time */
		gator_buffer_write_packed_int64(cpu, ANNOTATE_BUF, 0);
		/* size */
		gator_buffer_write_packed_int(cpu, ANNOTATE_BUF, 0);
	}

	/* Check and commit; commit is set to occur once buffer is 3/4 full */
	buffer_check(cpu, ANNOTATE_BUF, gator_get_time());

	spin_unlock(&annotate_lock);

	return 0;
}
예제 #8
0
static void marshal_backtrace_footer(u64 time)
{
	int cpu = get_physical_cpu();

	gator_buffer_write_packed_int(cpu, BACKTRACE_BUF, MESSAGE_END_BACKTRACE);

	/* Check and commit; commit is set to occur once buffer is 3/4 full */
	buffer_check(cpu, BACKTRACE_BUF, time);
}
void bufreverse_process_internal(t_bufreverse *x, t_symbol *sym, short argc, t_atom *argv)
{
    t_symbol *target = atom_getsym(argv++);
    t_symbol *source = atom_getsym(argv++);

    float *temp1;
    double *temp2;

    t_buffer_write_error error;

    AH_SIntPtr full_length = buffer_length(source);
    AH_SIntPtr i;

    double sample_rate = 0;
    t_atom_long read_chan = x->read_chan - 1;

    // Check source buffer

    if (buffer_check((t_object *) x, source, read_chan))
        return;
    sample_rate = buffer_sample_rate(source);

    // Allocate Memory

    temp1 = (float *) ALIGNED_MALLOC(full_length * (sizeof(double) + sizeof(float)));
    temp2 = (double *) (temp1 + full_length);

    // Check momory allocation

    if (!temp1)
    {
        object_error((t_object *)x, "could not allocate temporary memory for processing");
        free(temp1);
        return;
    }

    // Read from buffer

    buffer_read(source, read_chan, (float *) temp1, full_length);

    // Copy to double precision version

    for (i = 0; i < full_length; i++)
         temp2[i] = temp1[full_length - i - 1];

    // Copy out to buffer

    error = buffer_write(target, temp2, full_length, x->write_chan - 1, x->resize, sample_rate, 1.);
    buffer_write_error((t_object *)x, target, error);

    // Free Resources

    ALIGNED_FREE(temp1);

    if (!error)
        outlet_bang(x->process_done);
}
예제 #10
0
static void marshal_cookie(int cookie, const char *text)
{
	int cpu = get_physical_cpu();
	/* buffer_check_space already called by marshal_cookie_header */
	gator_buffer_write_packed_int(cpu, NAME_BUF, MESSAGE_COOKIE);
	gator_buffer_write_packed_int(cpu, NAME_BUF, cookie);
	gator_buffer_write_string(cpu, NAME_BUF, text);
	buffer_check(cpu, NAME_BUF, gator_get_time());
}
예제 #11
0
파일: response.c 프로젝트: booo/imapfilter
/*
 * Process the data that server sent due to IMAP FETCH BODY[] client request,
 * ie. FETCH BODY[HEADER], FETCH BODY[TEXT], FETCH BODY[HEADER.FIELDS
 * (<fields>)], FETCH BODY[<part>].
 */
int
response_fetchbody(session *ssn, int tag, char **body, size_t *len)
{
	int r, match;
	unsigned int offset;
	ssize_t n;
	regexp *re;

	if (tag == -1)
		return -1;

	buffer_reset(&ibuf);

	match = -1;
	offset = 0;
	
	re = &responses[RESPONSE_FETCH_BODY];

	do {
		buffer_check(&ibuf, ibuf.len + INPUT_BUF);
		if ((n = receive_response(ssn, ibuf.data + ibuf.len, 0, 1)) ==
		    -1)
			return -1;
		ibuf.len += n;

		if (match != 0) {
			match = regexec(re->preg, ibuf.data, re->nmatch,
			    re->pmatch, 0);
			
			if (match == 0 && re->pmatch[2].rm_so != -1 &&
			    re->pmatch[2].rm_eo != -1) {
				*len = strtoul(ibuf.data + re->pmatch[2].rm_so,
				    NULL, 10);
				offset = re->pmatch[0].rm_eo + *len;
			}
		}

		if (offset != 0 && ibuf.len >= offset) {
			if (check_bye(ibuf.data + offset))
				return STATUS_BYE;
		}
	} while (ibuf.len < offset || (r = check_tag(ibuf.data + offset, ssn,
	    tag)) == STATUS_NONE);

	if (match == 0) {
		if (re->pmatch[2].rm_so != -1 &&
		    re->pmatch[2].rm_eo != -1) {
			*body = ibuf.data + re->pmatch[0].rm_eo;
		} else {
			*body = ibuf.data + re->pmatch[3].rm_so;
			*len = re->pmatch[3].rm_eo - re->pmatch[3].rm_so;
		}
	}

	return r;
}
예제 #12
0
int canna_proto_recv_request(int id)
{
  int datalen, extflag, type;
  cannaheader_t *header;
  
  buffer_check(&packetbuf, 24);

  if (m_socket_read(client[id].sockfd, packetbuf.buf, 4) < 0) {
    /* read エラーはクライアントが勝手に落ちたと判断する */
    return -1;
  }
  
  header = (cannaheader_t *)(packetbuf.buf);
  
  if (header->type == 0x00) { /* Initialize */
    if (m_socket_read(client[id].sockfd, (char *)(&datalen), 4) < 0)
      return -1;
    datalen = LSBMSB32(datalen);

    buffer_check(&packetbuf, datalen);
    
    if (m_socket_read(client[id].sockfd, packetbuf.buf, datalen) < 0)
      return -1;

    return 0x01;
  } else {
    datalen = LSBMSB16(header->datalen);
    extflag = header->extra ? 0x1000 : 0x0000;
    type = header->type;

    if (datalen > 0) {
      buffer_check(&packetbuf, datalen + 4);
      
      if (m_socket_read(client[id].sockfd, &(packetbuf.buf[4]), datalen) < 0)
        return -1;
    }
    
    return (type | extflag);
  }
}
예제 #13
0
void
main()
{
  int i, j;
  int nsubpixels = 1 << (HIBITS+LOBITS);
  fixpoint zero, maxpix;

  zero = 0;
  maxpix = ((NPIXELS-1) << LOBITS) | LOMASK;

  InitScreen();

  if (verbose) {
	 printf("zero "); fp_print(zero); printf(", max "); fp_print(maxpix);
	 printf("\n");
  }

  for (i=0; i != maxpix + 1; i++) {
	 for (j=0; j != maxpix + 1; j++) {

		if (verbose) {
		  printf("\n\n************** New Iteration ************\n\n");
		  printf("%3d, %3d --> ", i, j);
		  fp_print(i); printf(", "); fp_print(j); printf("\n");
		}

		clear_buffer();
		clear_view_surface(fildes); 

		fill_color(fildes, 0.5, 0.5, 0.5);
		subpixel_triangle(zero, zero,  zero,  maxpix, i, j);

		fill_color(fildes, 0.0, 1.0, 0.0);
		subpixel_triangle(zero, zero,  maxpix,  zero, i, j);

		fill_color(fildes, 0.0, 0.0, 1.0);
		subpixel_triangle(zero,  maxpix,  maxpix,   maxpix, i, j);

		fill_color(fildes, 1.0, 0.0, 0.0);
		subpixel_triangle( maxpix, zero,  maxpix,   maxpix, i, j);

		buffer_check();
		make_picture_current(fildes);

		if (verbose) {
		  printf("  hit <return> for next iteration\n");
		  getchar();
		}

	 }
  }
}
예제 #14
0
/*
 * Sends to server data; a command.
 */
int
send_request(session *ssn, const char *fmt,...)
{
	int n;
	va_list args;
	int t = tag;

	if (ssn->socket == -1)
		return -1;

	buffer_reset(&obuf);
	obuf.len = snprintf(obuf.data, obuf.size + 1, "%04X ", tag);

	va_start(args, fmt);
	n = vsnprintf(obuf.data + obuf.len, obuf.size - obuf.len -
	    strlen("\r\n") + 1, fmt, args);
	if (n > (int)obuf.size) {
		buffer_check(&obuf, n);
		vsnprintf(obuf.data + obuf.len, obuf.size - obuf.len -
		    strlen("\r\n") + 1, fmt, args);
	}
	obuf.len = strlen(obuf.data);
	va_end(args);

	snprintf(obuf.data + obuf.len, obuf.size - obuf.len + 1, "\r\n");
	obuf.len = strlen(obuf.data);

	if (!strncasecmp(fmt, "LOGIN", strlen("LOGIN"))) {
		debug("sending command (%d):\n\n%.*s*\r\n\n", ssn->socket,
		    obuf.len - strlen(ssn->password) - strlen("\"\"\r\n"),
		    obuf.data);
		verbose("C (%d): %.*s*\r\n", ssn->socket, obuf.len -
		    strlen(ssn->password) - strlen("\"\"\r\n"),  obuf.data);
	} else {
		debug("sending command (%d):\n\n%s\n", ssn->socket, obuf.data);
		verbose("C (%d): %s", ssn->socket, obuf.data);
	}

	if (socket_write(ssn, obuf.data, obuf.len) == -1)
		return -1;

	if (tag == 0xFFFF)	/* Tag always between 0x1000 and 0xFFFF. */
		tag = 0x0FFF;
	tag++;

	return t;
}
예제 #15
0
static void marshal_idle(int core, int state)
{
	unsigned long flags, cpu;
	u64 time;

	local_irq_save(flags);
	cpu = get_physical_cpu();
	time = gator_get_time();
	if (buffer_check_space(cpu, IDLE_BUF, MAXSIZE_PACK64 + 2 * MAXSIZE_PACK32)) {
		gator_buffer_write_packed_int(cpu, IDLE_BUF, state);
		gator_buffer_write_packed_int64(cpu, IDLE_BUF, time);
		gator_buffer_write_packed_int(cpu, IDLE_BUF, core);
	}
	local_irq_restore(flags);
	/* Check and commit; commit is set to occur once buffer is 3/4 full */
	buffer_check(cpu, IDLE_BUF, time);
}
예제 #16
0
static void marshal_thread_name(int pid, char *name)
{
	unsigned long flags, cpu;
	u64 time;

	local_irq_save(flags);
	cpu = get_physical_cpu();
	time = gator_get_time();
	if (buffer_check_space(cpu, NAME_BUF, TASK_COMM_LEN + 3 * MAXSIZE_PACK32 + MAXSIZE_PACK64)) {
		gator_buffer_write_packed_int(cpu, NAME_BUF, MESSAGE_THREAD_NAME);
		gator_buffer_write_packed_int64(cpu, NAME_BUF, time);
		gator_buffer_write_packed_int(cpu, NAME_BUF, pid);
		gator_buffer_write_string(cpu, NAME_BUF, name);
	}
	local_irq_restore(flags);
	buffer_check(cpu, NAME_BUF, time);
}
예제 #17
0
static void marshal_link(int cookie, int tgid, int pid)
{
	unsigned long cpu = get_physical_cpu(), flags;
	u64 time;

	local_irq_save(flags);
	time = gator_get_time();
	if (buffer_check_space(cpu, ACTIVITY_BUF, MAXSIZE_PACK64 + 5 * MAXSIZE_PACK32)) {
		gator_buffer_write_packed_int(cpu, ACTIVITY_BUF, MESSAGE_LINK);
		gator_buffer_write_packed_int64(cpu, ACTIVITY_BUF, time);
		gator_buffer_write_packed_int(cpu, ACTIVITY_BUF, cookie);
		gator_buffer_write_packed_int(cpu, ACTIVITY_BUF, tgid);
		gator_buffer_write_packed_int(cpu, ACTIVITY_BUF, pid);
	}
	local_irq_restore(flags);
	/* Check and commit; commit is set to occur once buffer is 3/4 full */
	buffer_check(cpu, ACTIVITY_BUF, time);
}
예제 #18
0
static bool marshal_backtrace_header(int exec_cookie, int tgid, int pid, u64 time)
{
	int cpu = get_physical_cpu();

	if (!buffer_check_space(cpu, BACKTRACE_BUF, MAXSIZE_PACK64 + 5 * MAXSIZE_PACK32 + gator_backtrace_depth * 2 * MAXSIZE_PACK32)) {
		/* Check and commit; commit is set to occur once buffer is 3/4 full */
		buffer_check(cpu, BACKTRACE_BUF, time);

		return false;
	}

	gator_buffer_write_packed_int64(cpu, BACKTRACE_BUF, time);
	gator_buffer_write_packed_int(cpu, BACKTRACE_BUF, exec_cookie);
	gator_buffer_write_packed_int(cpu, BACKTRACE_BUF, tgid);
	gator_buffer_write_packed_int(cpu, BACKTRACE_BUF, pid);

	return true;
}
예제 #19
0
static void __maybe_unused marshal_event_single64(int core, int key, long long value)
{
	unsigned long flags, cpu;
	u64 time;

	local_irq_save(flags);
	cpu = get_physical_cpu();
	time = gator_get_time();
	if (buffer_check_space(cpu, COUNTER_BUF, 2 * MAXSIZE_PACK64 + 2 * MAXSIZE_PACK32)) {
		gator_buffer_write_packed_int64(cpu, COUNTER_BUF, time);
		gator_buffer_write_packed_int(cpu, COUNTER_BUF, core);
		gator_buffer_write_packed_int(cpu, COUNTER_BUF, key);
		gator_buffer_write_packed_int64(cpu, COUNTER_BUF, value);
	}
	local_irq_restore(flags);
	/* Check and commit; commit is set to occur once buffer is 3/4 full */
	buffer_check(cpu, COUNTER_BUF, time);
}
예제 #20
0
static void marshal_sched_trace_exit(int tgid, int pid)
{
	unsigned long cpu = get_physical_cpu(), flags;
	u64 time;

	if (!per_cpu(gator_buffer, cpu)[SCHED_TRACE_BUF])
		return;

	local_irq_save(flags);
	time = gator_get_time();
	if (buffer_check_space(cpu, SCHED_TRACE_BUF, MAXSIZE_PACK64 + 2 * MAXSIZE_PACK32)) {
		gator_buffer_write_packed_int(cpu, SCHED_TRACE_BUF, MESSAGE_SCHED_EXIT);
		gator_buffer_write_packed_int64(cpu, SCHED_TRACE_BUF, time);
		gator_buffer_write_packed_int(cpu, SCHED_TRACE_BUF, pid);
	}
	local_irq_restore(flags);
	/* Check and commit; commit is set to occur once buffer is 3/4 full */
	buffer_check(cpu, SCHED_TRACE_BUF, time);
}
예제 #21
0
static void marshal_activity_switch(int core, int key, int activity, int pid, int state)
{
	unsigned long cpu = get_physical_cpu(), flags;
	u64 time;

	if (!per_cpu(gator_buffer, cpu)[ACTIVITY_BUF])
		return;

	local_irq_save(flags);
	time = gator_get_time();
	if (buffer_check_space(cpu, ACTIVITY_BUF, MAXSIZE_PACK64 + 5 * MAXSIZE_PACK32)) {
		gator_buffer_write_packed_int(cpu, ACTIVITY_BUF, MESSAGE_SWITCH);
		gator_buffer_write_packed_int64(cpu, ACTIVITY_BUF, time);
		gator_buffer_write_packed_int(cpu, ACTIVITY_BUF, core);
		gator_buffer_write_packed_int(cpu, ACTIVITY_BUF, key);
		gator_buffer_write_packed_int(cpu, ACTIVITY_BUF, activity);
		gator_buffer_write_packed_int(cpu, ACTIVITY_BUF, pid);
		gator_buffer_write_packed_int(cpu, ACTIVITY_BUF, state);
	}
	local_irq_restore(flags);
	/* Check and commit; commit is set to occur once buffer is 3/4 full */
	buffer_check(cpu, ACTIVITY_BUF, time);
}
void irextract_process (t_irextract *x, t_symbol *rec_buffer, t_atom_long num_channels, double sample_rate)
{
	FFT_SETUP_D fft_setup;
	
	FFT_SPLIT_COMPLEX_D spectrum_1;
	FFT_SPLIT_COMPLEX_D spectrum_2;
	FFT_SPLIT_COMPLEX_D spectrum_3;

	void *excitation_sig;
	double *out_mem;
	float *rec_mem;
	float *filter_in;
	
	t_symbol *filter = filter_retriever(x->deconvolve_filter_specifier);
	
	double filter_specifier[HIRT_MAX_SPECIFIER_ITEMS];
	double range_specifier[HIRT_MAX_SPECIFIER_ITEMS];
	
	double test_pow;
	double max_pow;
	double deconvolve_phase = phase_retriever(x->deconvolve_phase);
	
	long deconvolve_mode = x->deconvolve_mode;
	long bandlimit = x->measure_mode == SWEEP ? x->bandlimit : 0;
	
	AH_SIntPtr rec_length = buffer_length(rec_buffer);
	AH_SIntPtr gen_length = 0;
	AH_SIntPtr filter_length = buffer_length(filter);
	AH_SIntPtr out_length_samps;
	
	AH_UIntPtr fft_size; 
	AH_UIntPtr fft_size_log2;
	AH_UIntPtr i;
	
	if (buffer_check((t_object *)x, rec_buffer) || !rec_length)
		return;
	
	switch (x->measure_mode)
	{	
		case SWEEP:
			gen_length = ess_get_length(&x->sweep_params);
			break;
			
		case MLS:
			gen_length = mls_get_length(&x->max_length_params);
			break;
			
		case NOISE:
			gen_length = coloured_noise_get_length(&x->noise_params);
			break;
	}
	
	// Check and calculate lengths
	
	fft_size = calculate_fft_size(rec_length + gen_length, &fft_size_log2);
	
	if (rec_length % num_channels)
		object_warn ((t_object *) x, "buffer length is not a multiple of the number of channels - number may be wrong");
		
	if (((rec_length / num_channels) - gen_length) < 1)
	{
		object_error ((t_object *) x, "buffer is not long enough for generated signal");
		return;
	}
	
	out_length_samps = ((rec_length / num_channels) - gen_length);
	
	if (x->out_length)
	{
		if (out_length_samps < (x->out_length * sample_rate))
			object_warn ((t_object *) x, "buffer is not long enough for requested output length");
		else
			out_length_samps = (AH_SIntPtr) (x->out_length * sample_rate);
	}
	
	// Allocate Temporary Memory
	
	fft_setup = hisstools_create_setup_d(fft_size_log2);
		
	excitation_sig = malloc(((gen_length > filter_length) ? gen_length : filter_length) * sizeof(double));
	
	spectrum_1.realp = ALIGNED_MALLOC((sizeof(double) * fft_size * 4));
	spectrum_1.imagp = spectrum_1.realp + (fft_size >> 1);
	spectrum_2.realp = spectrum_1.imagp + (fft_size >> 1);
	spectrum_2.imagp = spectrum_2.realp + (fft_size >> 1);
	spectrum_3.realp = spectrum_2.imagp + (fft_size >> 1);
	spectrum_3.imagp = spectrum_3.realp + fft_size;
		
	rec_mem = (float *) malloc(rec_length * sizeof(float));
	
	filter_in = filter_length ? ALIGNED_MALLOC(sizeof(float *) * filter_length) : 0; 
	
	if (!fft_setup || !excitation_sig || !spectrum_1.realp || (filter_length && !filter_in))
	{
		object_error ((t_object *) x, "could not allocate temporary memory for processing");
		
		hisstools_destroy_setup_d(fft_setup);
		free(excitation_sig);
		ALIGNED_FREE(spectrum_1.realp);
		ALIGNED_FREE(filter_in);
	
		return;
	}
	
	x->fft_size = fft_size;
	x->sample_rate = sample_rate;
	x->out_length_samps = out_length_samps;
	x->gen_length = gen_length;
	
	// Allocate output memory and get record memory
	
	out_mem = grow_mem_swap(&x->out_mem, fft_size * sizeof(double), fft_size);
	
	if (!out_mem) 
	{
		object_error ((t_object *) x, "could not allocate memory for output storage");
		free(excitation_sig);
		hisstools_destroy_setup_d(fft_setup);
		return;
	}

	// Generate Signal
	
	switch (x->measure_mode)
	{
		case SWEEP:
			ess_gen(&x->sweep_params, excitation_sig, true);
			break;
			
		case MLS:
			mls_gen(&x->max_length_params, excitation_sig, true);
			break;
			
		case NOISE:
			coloured_noise_gen(&x->noise_params, excitation_sig, true);
			break;
	}
	
	// Transform excitation signal into complex spectrum 2
	
	time_to_halfspectrum_double(fft_setup, excitation_sig, gen_length, spectrum_2, fft_size);
			 
	if (bandlimit)
	{
		// Calculate standard filter for bandlimited deconvolution (sweep * inv sweep)
		
		ess_igen(&x->sweep_params, excitation_sig, true, true);
		time_to_halfspectrum_double(fft_setup, excitation_sig, gen_length, spectrum_3, fft_size);
		convolve(spectrum_3, spectrum_2, fft_size, SPECTRUM_REAL);
							
		// Calculate full power spectrum from half spectrum - convert filter to have the required phase
		
		power_full_spectrum_from_half_spectrum(spectrum_3, fft_size);		
		variable_phase_from_power_spectrum(fft_setup, spectrum_3, fft_size, deconvolve_phase, true);	
		
		// Convert back to real format
		
		spectrum_3.imagp[0] = spectrum_3.realp[fft_size >> 1];		
	}
	else 
	{
		// Find maximum power to scale 
		
		for (i = 1, max_pow = 0; i < (fft_size >> 1); i++)
static ssize_t annotate_write(struct file *file, char const __user *buf, size_t count_orig, loff_t *offset)
{
	int pid, cpu, header_size, available, contiguous, length1, length2, size, count = count_orig & 0x7fffffff;
	bool interrupt_context;

	if (*offset)
		return -EINVAL;

	interrupt_context = in_interrupt();
	/* Annotations are not supported in interrupt context, but may work
	 * if you comment out the the next four lines of code. By doing so,
	 * annotations in interrupt context can result in deadlocks and lost
	 * data.
	 */
	if (interrupt_context) {
		pr_warning("gator: Annotations are not supported in interrupt context. Edit gator_annotate.c in the gator driver to enable annotations in interrupt context.\n");
		return -EINVAL;
	}

 retry:
	/* synchronize between cores and with collect_annotations */
	spin_lock(&annotate_lock);

	if (!collect_annotations) {
		/* Not collecting annotations, tell the caller everything was written */
		size = count_orig;
		goto annotate_write_out;
	}

	/* Annotation only uses a single per-cpu buffer as the data must be in order to the engine */
	cpu = 0;

        if (current == NULL)
		pid = 0;
	else
		pid = current->pid;

	/* determine total size of the payload */
	header_size = MAXSIZE_PACK32 * 3 + MAXSIZE_PACK64;
	available = buffer_bytes_available(cpu, ANNOTATE_BUF) - header_size;
	size = count < available ? count : available;

	if (size <= 0) {
		/* Buffer is full, wait until space is available */
		spin_unlock(&annotate_lock);

		/* Drop the annotation as blocking is not allowed in interrupt context */
		if (interrupt_context)
			return -EINVAL;

		wait_event_interruptible(gator_annotate_wait, buffer_bytes_available(cpu, ANNOTATE_BUF) > header_size || !collect_annotations);

		/* Check to see if a signal is pending */
		if (signal_pending(current))
			return -EINTR;

		goto retry;
	}

	/* synchronize shared variables annotateBuf and annotatePos */
	if (per_cpu(gator_buffer, cpu)[ANNOTATE_BUF]) {
		u64 time = gator_get_time();

		gator_buffer_write_packed_int(cpu, ANNOTATE_BUF, get_physical_cpu());
		gator_buffer_write_packed_int(cpu, ANNOTATE_BUF, pid);
		gator_buffer_write_packed_int64(cpu, ANNOTATE_BUF, time);
		gator_buffer_write_packed_int(cpu, ANNOTATE_BUF, size);

		/* determine the sizes to capture, length1 + length2 will equal size */
		contiguous = contiguous_space_available(cpu, ANNOTATE_BUF);
		if (size < contiguous) {
			length1 = size;
			length2 = 0;
		} else {
			length1 = contiguous;
			length2 = size - contiguous;
		}

		if (annotate_copy(file, buf, length1) != 0) {
			size = -EINVAL;
			goto annotate_write_out;
		}

		if (length2 > 0 && annotate_copy(file, &buf[length1], length2) != 0) {
			size = -EINVAL;
			goto annotate_write_out;
		}

		/* Check and commit; commit is set to occur once buffer is 3/4 full */
		buffer_check(cpu, ANNOTATE_BUF, time);
	}

annotate_write_out:
	spin_unlock(&annotate_lock);

	/* return the number of bytes written */
	return size;
}
void bufconvolve_process_internal (t_bufconvolve *x, t_symbol *sym, short argc, t_atom *argv)
{
	FFT_SETUP_D fft_setup;
	
	FFT_SPLIT_COMPLEX_D spectrum_1;
	FFT_SPLIT_COMPLEX_D spectrum_2;
	FFT_SPLIT_COMPLEX_D spectrum_3;

	double *out_buf;
	float *in_temp;
	float *filter_in;
	
	AH_Boolean convolve_mode = sym == gensym("convolve") ? true : false;

	t_symbol *target = atom_getsym(argv++);
	t_symbol *source_1 = atom_getsym(argv++);
	t_symbol *source_2 = atom_getsym(argv++);
	t_symbol *filter = filter_retriever(x->deconvolve_filter_specifier);
	
	double filter_specifier[HIRT_MAX_SPECIFIER_ITEMS];
	double range_specifier[HIRT_MAX_SPECIFIER_ITEMS];
	
	double time_mul = atom_getfloat(argv++);
	double sample_rate = buffer_sample_rate(source_1); 
	double deconvolve_phase = phase_retriever(x->deconvolve_phase);
	double deconvolve_delay;
		
	AH_SIntPtr source_length_1 = buffer_length(source_1);
	AH_SIntPtr source_length_2 = buffer_length(source_2);
	AH_SIntPtr filter_length = buffer_length(filter);
	
	AH_UIntPtr fft_size;
	AH_UIntPtr fft_size_log2; 
	
	long deconvolve_mode = x->deconvolve_mode;
	t_buffer_write_error error;
	
	// Check input buffers
	
	if (buffer_check((t_object *) x, source_1) || buffer_check((t_object *) x, source_2))
		return;
	
	// Check sample rates
	
	if (sample_rate != buffer_sample_rate(source_2))
		object_warn((t_object *) x, "sample rates do not match");
	
	// Check and calculate lengths
	
	if (convolve_mode == true)
		fft_size = (AH_UIntPtr) ((source_length_1 + source_length_2) * time_mul);
	else
		fft_size = (AH_UIntPtr) (source_length_1 < source_length_2 ? source_length_2 * time_mul : source_length_1 * time_mul);
		
	fft_size = calculate_fft_size(fft_size, &fft_size_log2);
	deconvolve_delay = delay_retriever(x->deconvolve_delay, fft_size, sample_rate);

	if (fft_size < 8)
	{
		object_error((t_object *) x, "input buffers are too short, or have no length");
		return;
	}
	
	// Allocate Memory (use pointer aliasing where possible for efficiency)
	
	fft_setup = hisstools_create_setup_d(fft_size_log2);

	spectrum_1.realp = ALIGNED_MALLOC(sizeof(double) * fft_size * (convolve_mode == true ? 3 : 4));
	spectrum_1.imagp = spectrum_1.realp + (fft_size >> 1);
	spectrum_2.realp = spectrum_1.imagp + (fft_size >> 1); 
	spectrum_2.imagp = spectrum_2.realp + (fft_size >> 1);
	spectrum_3.realp = spectrum_2.imagp + (fft_size >> 1); 
	spectrum_3.imagp = convolve_mode == true ? 0 : spectrum_3.realp + fft_size;
	
	filter_in = filter_length ? ALIGNED_MALLOC(sizeof(float *) * filter_length) : 0; 
	
	out_buf = spectrum_2.realp;
	in_temp = (float *) spectrum_3.realp;
	
	// Check memory allocations
	
	if (!fft_setup || !spectrum_1.realp || (filter_length && !filter_in))
	{
		object_error((t_object *) x, "could not allocate temporary memory for processing");
		
		hisstools_destroy_setup_d(fft_setup);
		ALIGNED_FREE(spectrum_1.realp);
		ALIGNED_FREE(filter_in);
		
		return;
	}

	// Get inputs - convert to frequency domain
	
	buffer_read(source_1, x->read_chan - 1, in_temp, source_length_1);
	time_to_halfspectrum_float(fft_setup, in_temp, source_length_1, spectrum_1, fft_size);
	buffer_read(source_2, x->read_chan - 1, in_temp, source_length_2);
	time_to_halfspectrum_float(fft_setup, in_temp, source_length_2, spectrum_2, fft_size);		
		
	// Do deconvolution or convolution
	
	if (convolve_mode == true)
		convolve(spectrum_1, spectrum_2, fft_size, SPECTRUM_REAL);	
	else
	{
		// Fill deconvolution filter specifiers - load filter from buffer (if specified) - deconvolve
		
		fill_power_array_specifier(filter_specifier, x->deconvolve_filter_specifier, x->deconvolve_num_filter_specifiers);
		fill_power_array_specifier(range_specifier, x->deconvolve_range_specifier, x->deconvolve_num_range_specifiers);
		buffer_read(filter, 0, filter_in, fft_size);
		deconvolve(fft_setup, spectrum_1, spectrum_2, spectrum_3, filter_specifier, range_specifier, 0.0, filter_in, filter_length, fft_size, SPECTRUM_REAL, deconvolve_mode, deconvolve_phase, deconvolve_delay, sample_rate);
	}
	
	// Convert to time domain - copy out to buffer
	
	spectrum_to_time(fft_setup, out_buf, spectrum_1, fft_size, SPECTRUM_REAL);	
	error = buffer_write(target, out_buf, (convolve_mode == true ? source_length_1 + source_length_2 - 1 : fft_size), x->write_chan - 1, x->resize, sample_rate, 1.);
	buffer_write_error((t_object *) x, target, error);
	
	// Free resources
	
	hisstools_destroy_setup_d(fft_setup);
	ALIGNED_FREE(spectrum_1.realp);
	ALIGNED_FREE(filter_in);
	
	if (!error)
		outlet_bang(x->process_done);
}
void ircropfade_process_internal(t_ircropfade *x, t_symbol *sym, short argc, t_atom *argv)
{
    // Load arguments

    t_symbol *target = atom_getsym(argv++);
    t_symbol *source = atom_getsym(argv++);
    t_atom_long crop1 = atom_getlong(argv++);
    t_atom_long crop2 = atom_getlong(argv++);
    double fade_start = atom_getfloat(argv++);
    double in_length = atom_getfloat(argv++);
    double fade_end = atom_getfloat(argv++);
    double out_length = atom_getfloat(argv++);

    // Set fade variables

    double fade_in_lo = fade_start - 1;
    double fade_in_hi = in_length > 0 ? fade_start + in_length : fade_start;
    double fade_out_lo = fade_end;
    double fade_out_hi = out_length > 0 ? fade_end - out_length : fade_end - 1;
    double fade_in_recip = 1. / (fade_in_hi - fade_in_lo);
    double fade_out_recip = 1. / (fade_out_hi - fade_out_lo);

    float *temp1;
    double *temp2;

    t_buffer_write_error error;

    AH_SIntPtr full_length = buffer_length(source);
    AH_SIntPtr final_length;
    AH_SIntPtr i;

    t_atom_long read_chan = x->read_chan - 1;

    double sample_rate = 0;

    // Check source buffer

    if (buffer_check((t_object *) x, source, read_chan))
        return;
    sample_rate = buffer_sample_rate(source);

    crop1 = crop1 < 0 ? 0 : crop1;
    crop2 = crop2 < 0 ? 0 : crop2;
    crop1 = crop1 > full_length - 1 ? full_length - 1: crop1;
    crop2 = crop2 > full_length ? full_length : crop2;

    if (crop1 >= crop2)
        return;

    final_length = crop2 - crop1;

    // Allocate Memory

    temp1 = (float *) ALIGNED_MALLOC(full_length  * sizeof(float) + final_length * sizeof(double));
    temp2 = (double *) (temp1 + full_length);

    // Check momory allocation

    if (!temp1)
    {
        object_error((t_object *)x, "could not allocate temporary memory for processing");
        free(temp1);
        return;
    }

    // Read from buffer

    buffer_read(source, read_chan, (float *) temp1, full_length);

    // Copy with crops / fades to double precision version

    for (i = 0; i < final_length; i++)
    {
        double in_val = temp1[i + crop1];
        double fade_in = calculate_fade((double) (i + crop1), fade_in_lo, fade_in_recip);
        double fade_out = calculate_fade((double) (i + crop1), fade_out_lo, fade_out_recip);

        temp2[i] = in_val * fade_in * fade_out;
    }

    // Copy out to buffer

    error = buffer_write(target, temp2, final_length, x->write_chan - 1, x->resize, sample_rate, 1.);
    buffer_write_error((t_object *)x, target, error);

    // Free Resources

    ALIGNED_FREE(temp1);

    if (!error)
        outlet_bang(x->process_done);
}
void irphase_process_internal (t_irphase *x, t_symbol *sym, short argc, t_atom *argv)
{
	FFT_SETUP_D fft_setup;
	
	FFT_SPLIT_COMPLEX_D spectrum_1;
	FFT_SPLIT_COMPLEX_D spectrum_2;
	FFT_SPLIT_COMPLEX_D spectrum_3;

	float *in;
	float *filter_in;
	double *out_buf;
	
	t_symbol *filter = filter_retriever(x->deconvolve_filter_specifier);
	t_symbol *target = atom_getsym(argv++);
	t_symbol *source = atom_getsym(argv++);
	
	double filter_specifier[HIRT_MAX_SPECIFIER_ITEMS];
	double range_specifier[HIRT_MAX_SPECIFIER_ITEMS];
	
	double phase = atom_getfloat(argv++);
	double time_mul = atom_getfloat(argv++);	
	double sample_rate = buffer_sample_rate(source);
	double deconvolve_delay;
	double deconvolve_phase;
	
	t_phase_type mode = (t_phase_type) atom_getlong(argv++);
	
	AH_UIntPtr fft_size;
	AH_UIntPtr fft_size_log2;
	AH_UIntPtr i;
	
	t_buffer_write_error error;
	long deconvolve_mode;
	
	// Get input buffer lengths
	
	AH_SIntPtr source_length_1 = buffer_length(source);
	AH_SIntPtr filter_length = buffer_length(filter);
	AH_SIntPtr max_length = source_length_1;
	
	// Check input buffers
	
	if (buffer_check((t_object *) x, source))
		return;
	
	// Calculate fft size
	
	time_mul = time_mul == 0. ? 1 : time_mul;		

	if (time_mul < 1)
	{
		object_warn((t_object *) x, " time multiplier cannot be less than 1 (using 1)");
		time_mul = 1;
	}
	
	fft_size = calculate_fft_size((long) (max_length * time_mul), &fft_size_log2);

	if (fft_size < 8)
	{
		object_error((t_object *) x, "buffers are too short, or have no length");
		return;
	}
	
	deconvolve_mode = x->deconvolve_mode;
	deconvolve_phase = phase_retriever(x->deconvolve_phase);
	deconvolve_delay = delay_retriever(x->deconvolve_delay, fft_size, sample_rate);
	
	// Allocate momory

	fft_setup = hisstools_create_setup_d(fft_size_log2);
	
	spectrum_1.realp = ALIGNED_MALLOC(sizeof(double) * fft_size * (mode == MODE_ALLPASS ? 6 : 3));
	spectrum_1.imagp = spectrum_1.realp + fft_size;
	spectrum_2.realp = spectrum_1.imagp + fft_size;
	spectrum_2.imagp = mode == MODE_ALLPASS ? spectrum_2.realp + fft_size : 0;
	spectrum_3.realp = mode == MODE_ALLPASS ? spectrum_2.imagp + fft_size : 0;
	spectrum_3.imagp = mode == MODE_ALLPASS ? spectrum_3.realp + fft_size : 0;
	
	filter_in = filter_length ? ALIGNED_MALLOC(sizeof(float *) * filter_length) : 0; 

	out_buf = mode == MODE_ALLPASS ? spectrum_3.realp : spectrum_2.realp;
	in = (float *) out_buf;

	if (!spectrum_1.realp || !fft_setup || (filter_length && !filter_in))
	{
		object_error((t_object *) x, "could not allocate temporary memory for processing");
		
		hisstools_destroy_setup_d(fft_setup);
		ALIGNED_FREE(spectrum_1.realp);
		ALIGNED_FREE(filter_in);
		
		return;
	}
	
	// Get input - convert to frequency domain - get power spectrum - convert phase
	
	buffer_read(source, x->read_chan - 1, in, fft_size);
	time_to_spectrum_float(fft_setup, in, source_length_1, spectrum_1, fft_size);
	power_spectrum(spectrum_1, fft_size, SPECTRUM_FULL);
	variable_phase_from_power_spectrum(fft_setup, spectrum_1, fft_size, phase, false);			

	if (mode == MODE_ALLPASS)
	{
		// Copy minimum phase spectrum to spectrum_2 
		
		for (i = 0; i < fft_size; i++) 
		{
			spectrum_2.realp[i] = spectrum_1.realp[i];
			spectrum_2.imagp[i] = spectrum_1.imagp[i]; 
			
		}
		
		// Get input again
	
		time_to_spectrum_float(fft_setup, in, source_length_1, spectrum_1, fft_size);
		
		// Fill deconvolution filter specifiers - read filter from buffer (if specified) - deconvolve input by minimum phase spectrum
		
		fill_power_array_specifier(filter_specifier, x->deconvolve_filter_specifier, x->deconvolve_num_filter_specifiers);
		fill_power_array_specifier(range_specifier, x->deconvolve_range_specifier, x->deconvolve_num_range_specifiers);
		buffer_read(filter, 0, filter_in, fft_size);
		deconvolve(fft_setup, spectrum_1, spectrum_2, spectrum_3, filter_specifier, range_specifier, 0, filter_in, filter_length, fft_size, SPECTRUM_FULL, deconvolve_mode, deconvolve_phase, deconvolve_delay, sample_rate);
	}
			
	// Convert to time domain - copy out to buffer
	
	spectrum_to_time(fft_setup, out_buf, spectrum_1, fft_size, SPECTRUM_FULL);
	error = buffer_write(target, out_buf, fft_size, x->write_chan - 1, x->resize, sample_rate, 1);
	buffer_write_error((t_object *) x, target, error);
	
	// Free memory
	
	hisstools_destroy_setup_d(fft_setup);
	ALIGNED_FREE(spectrum_1.realp);
	ALIGNED_FREE(filter_in);
	
	if (!error)
		outlet_bang(x->process_done);
}
예제 #27
0
/*
 * Convert a mailbox name to the modified UTF-7 encoding, according to RFC 3501
 * Section 5.1.3.
 */
const char *
apply_conversion(const char *mbox)
{
	iconv_t cd;
	char *inbuf, *outbuf;
	size_t inlen, outlen;
	char *c, *shift;
	unsigned char *r, *w;

	buffer_check(&nbuf, strlen(mbox)); 
	buffer_reset(&nbuf);
	xstrncpy(nbuf.data, mbox, nbuf.size);
	nbuf.len = strlen(nbuf.data);
	buffer_check(&cbuf, nbuf.len * 5);
	buffer_reset(&cbuf);

	r = (unsigned char *)nbuf.data;
	w = (unsigned char *)cbuf.data;
	inbuf = outbuf = NULL;
	inlen = outlen = 0;
	while (*r != '\0') {
		/* Skip non-printable ASCII characters. */
		if (*r < 0x20 || *r == 0x7F) {
			r++;
			continue;
		}
		/* Escape shift character for modified UTF-7. */
		if (*r == '&') {
			*w++ = '&';
			*w++ = '-';
			r++;
			continue;
		}
		/* Copy ASCII printable characters. */
		if (*r >= 0x20 && *r <= 0x7E) {
			*w++ = *r++;
			continue;
		}
		/* UTF-8 sequence will follow. */
		if (inbuf == NULL) {
			inbuf = (char *)r;
			inlen = 0;
		}
		if ((*r & 0xE0) == 0xC0) {	/* Two byte UTF-8. */
			inlen += 2;
			r += 2;
		} else if ((*r & 0xF0) == 0xE0) {	/* Three byte UTF-8. */
			inlen += 3;
			r += 3;
		} else if ((*r & 0xF8) == 0xF0) {	/* Four byte UTF-8. */
			inlen += 4;
			r += 4;
		}
		/* UTF-8 sequence has ended, convert it to UTF-7. */
		if (inbuf != NULL && (*r <= 0x7F || *r == '\0')) {
			outbuf = (char *)w;
			outlen = cbuf.size - (outbuf - cbuf.data);

			cd = iconv_open("UTF-7", "");
			if (cd == (iconv_t)-1) {
				error("converting mailbox name; %s\n",
				    strerror(errno));
				return mbox;
			}
			while (inlen > 0) {
				if (iconv(cd, &inbuf, &inlen, &outbuf, &outlen)
				    == -1) {
					if (errno == E2BIG) {
						buffer_check(&cbuf, cbuf.size *
						    2);
						break;
					} else {
						error("converting mailbox name;"
						    "%s\n", strerror(errno));
						return mbox;
					}
				} else {
					iconv(cd, NULL, NULL, &outbuf, &outlen);
				}
			}
			iconv_close(cd);

			w = (unsigned char *)outbuf;
			inbuf = outbuf = NULL;
			inlen = outlen = 0;
		}
	}

	if (*w != '\0')
		*w = '\0';

	/* Convert UTF-7 sequences to IMAP modified UTF-7. */
	for (c = cbuf.data, shift = NULL; *c != '\0'; c++)
		switch (*c) {
		case '+':
			*c = '&';
			shift = c;
			break;
		case '-':
			shift = NULL;
			break;
		case '/':
			if (shift != NULL)
				*c = ',';
			break;
		}
	if (shift != NULL) {
		*w++ = '-';
		*w = '\0';
	}

	debug("conversion: '%s' -> '%s'\n", nbuf.data, cbuf.data);

	return cbuf.data;
}
short buffer_multiple_names(t_object *x, t_symbol **in_bufs, t_symbol **out_bufs, AH_SIntPtr *lengths, short argc, t_atom *argv, t_atom_long read_chan, t_atom_long write_chan, long in_place, short max_bufs, AH_SIntPtr *overall_len_ret, AH_SIntPtr *max_len_ret, double *sample_rate_ret)
{
    AH_SIntPtr overall_length = 0;
    AH_SIntPtr max_length = 0;
    AH_SIntPtr new_length;
    short i;

    double sample_rate = 0.0;
    double new_sample_rate;

    if (!in_place)
    {
        if (argc % 2)
        {
            object_error((t_object *) x, "target buffer with no matching input buffer");
            return 0;
        }

        argc /= 2;
    }

    if (argc > max_bufs)
        argc = max_bufs;

    if (!argc)
    {
        object_error(x, "no buffers specified");
        return 0;
    }

    for (i = 0; i < argc; i++)
    {
        if (atom_gettype(argv + i) != A_SYM)
        {
            object_error(x, "name of buffer expected, but number given");
            return 0;
        }

        if (buffer_check(x, atom_getsym(argv + i), write_chan))
            return 0;

        if (in_place)
        {
            new_length = buffer_length (atom_getsym(argv + i));
            new_sample_rate = buffer_sample_rate(atom_getsym(argv + i));

            if (buffer_check(x, atom_getsym(argv + i), read_chan))
                return 0;
        }
        else
        {
            new_length = buffer_length (atom_getsym(argv + i + argc));
            new_sample_rate = buffer_sample_rate(atom_getsym(argv + i + argc));

            if (buffer_check(x, atom_getsym(argv + i + argc), read_chan))
                return 0;
        }

        if (new_length == 0)
        {
            object_error(x, "buffer %s has zero length ", atom_getsym(argv + i)->s_name);
            return 0;
        }

        // Store name and length

        out_bufs[i] = atom_getsym(argv + i);
        lengths[i] = new_length;

        if (in_place)
            in_bufs[i] = atom_getsym(argv + i);
        else
            in_bufs[i] = atom_getsym(argv + i + argc);

        if (new_length > max_length)
            max_length = new_length;

        overall_length += new_length;

        // Check sample rates

        if ((sample_rate != 0.0 && sample_rate != new_sample_rate) || new_sample_rate == 0.0)
            object_warn(x, "sample rates do not match for all source buffers");
        else
            sample_rate = new_sample_rate;
    }

    *overall_len_ret = overall_length;
    *max_len_ret = max_length;
    *sample_rate_ret = sample_rate;

    return argc;
}
예제 #29
0
/*
 * Convert a mailbox name from the modified UTF-7 encoding, according to RFC
 * 3501 Section 5.1.3.
 */
const char *
reverse_conversion(const char *mbox)
{
	iconv_t cd;
	char *inbuf, *outbuf;
	size_t inlen, outlen;
	char *c, *shift;
	
	buffer_check(&cbuf, strlen(mbox));
	buffer_reset(&cbuf);
	xstrncpy(cbuf.data, mbox, cbuf.size);

	/* Convert IMAP modified UTF-7 sequences to UTF-7. */
	for (c = cbuf.data, shift = NULL; *c != '\0'; c++)
		switch (*c) {
		case '&':
			*c = '+';
			shift = c;
			break;
		case '-':
			shift = NULL;
			break;
		case ',':
			if (shift != NULL)
				*c = '/';
			break;
		}

	do {
		inbuf = cbuf.data;
		inlen = strlen(cbuf.data);

		buffer_check(&nbuf, inlen);
		buffer_reset(&nbuf);

		outbuf = nbuf.data;
		outlen = nbuf.size;

		cd = iconv_open("", "UTF-7");
		if (cd == (iconv_t)-1) {
			error("converting mailbox name; %s\n", strerror(errno));
			return mbox;
		}
		while (inlen > 0) {
			if (iconv(cd, &inbuf, &inlen, &outbuf, &outlen) == -1) {
				if (errno == E2BIG) {
					buffer_check(&nbuf, nbuf.size * 2);
					break;
				} else {
					error("converting mailbox name; %s\n",
					    strerror(errno));
					return mbox;
				}
			} else {
				iconv(cd, NULL, NULL, &outbuf, &outlen);
			}
		}
		iconv_close(cd);
	} while (inlen > 0);

	*outbuf = '\0';
	for (c = nbuf.data; (c = strchr(c,'+')) != NULL; *c = '&');

	/* Convert UTF-7 sequences to IMAP modified UTF-7. */
	for (c = cbuf.data, shift = NULL; *c != '\0'; c++)
		switch (*c) {
		case '+':
			*c = '&';
			shift = c;
			break;
		case '-':
			shift = NULL;
			break;
		case '/':
			if (shift != NULL)
				*c = ',';
			break;
		}

	debug("conversion: '%s' <- '%s'\n", nbuf.data, cbuf.data);

	return nbuf.data;
}