Example #1
0
void merge_sort(size_t n, int *array) {
  size_t left_size, right_size;
  int *left_part, *right_part;

  if (n < 2)
    return;

  left_size = n / 2;
  left_part = (int*) malloc(left_size * sizeof(*left_part));
  if (left_part == NULL) {
    fprintf(stderr, "Can't allocate memory\n");
    exit(1);
  }

  right_size = n - left_size;
  right_part = (int*) malloc(right_size * sizeof(*right_part));
  if (right_part == NULL) {
    fprintf(stderr, "Can't allocate memory\n");
    exit(1);
  }

  memmove(left_part, array, left_size * sizeof(*left_part));
  memmove(right_part, array + left_size, right_size * sizeof(*right_part));
  merge_sort(left_size, left_part);
  merge_sort(right_size, right_part);
  merge(left_size, left_part, right_size, right_part, array);

  free(left_part);
  free(right_part);
}
Example #2
0
/* ARGSUSED */
void
chargen_dg(int s, struct servtab *sep)
{
	struct sockaddr_storage ss;
	static char *rs;
	int len;
	socklen_t size;
	char text[LINESIZ+2];

	if (endring == 0) {
		initring();
		rs = ring;
	}

	size = sizeof(ss);
	if (recvfrom(s, text, sizeof(text), 0,
		     (struct sockaddr *)&ss, &size) < 0)
		return;

	if (check_loop((struct sockaddr *)&ss, sep))
		return;

	if ((len = endring - rs) >= LINESIZ)
		memmove(text, rs, LINESIZ);
	else {
		memmove(text, rs, len);
		memmove(text + len, ring, LINESIZ - len);
	}
	if (++rs == endring)
		rs = ring;
	text[LINESIZ] = '\r';
	text[LINESIZ + 1] = '\n';
	(void) sendto(s, text, sizeof(text), 0, (struct sockaddr *)&ss, size);
}
Example #3
0
File: root.c Project: aahud/harvey
int
vtrootunpack(VtRoot *r, uint8_t *p)
{
	uint8_t *op = p;
	uint vers;
	memset(r, 0, sizeof(*r));

	vers = U16GET(p);
	if(vers != VtRootVersion) {
		werrstr("unknown root version");
		return -1;
	}
	p += 2;
	memmove(r->name, p, sizeof(r->name));
	r->name[sizeof(r->name)-1] = 0;
	p += sizeof(r->name);
	memmove(r->type, p, sizeof(r->type));
	r->type[sizeof(r->type)-1] = 0;
	p += sizeof(r->type);
	memmove(r->score, p, VtScoreSize);
	p +=  VtScoreSize;
	r->blocksize = U16GET(p);
	if(checksize(r->blocksize) < 0)
		return -1;
	p += 2;
	memmove(r->prev, p, VtScoreSize);
	p += VtScoreSize;

	assert(p-op == VtRootSize);
	return 0;
}
static void
play_tree_add_basepath(play_tree_t* pt, char* bp) {
    int i,bl = strlen(bp),fl;

    if(pt->child) {
        play_tree_t* i;
        for(i = pt->child ; i != NULL ; i = i->next)
            play_tree_add_basepath(i,bp);
        return;
    }

    if(!pt->files)
        return;

    for(i = 0 ; pt->files[i] != NULL ; i++) {
        fl = strlen(pt->files[i]);
        // if we find a full unix path, url:// or X:\ at the beginning,
        // don't mangle it.
        if(fl <= 0 || strstr(pt->files[i],"://") || (strstr(pt->files[i],":\\") == pt->files[i] + 1) || (pt->files[i][0] == '/') )
            continue;
        // if the path begins with \ then prepend drive letter to it.
        if (pt->files[i][0] == '\\') {
            if (pt->files[i][1] == '\\')
                continue;
            pt->files[i] = (char*)realloc(pt->files[i],2+fl+1);
            memmove(pt->files[i] + 2,pt->files[i],fl+1);
            memcpy(pt->files[i],bp,2);
            continue;
        }
        pt->files[i] = (char*)realloc(pt->files[i],bl+fl+1);
        memmove(pt->files[i] + bl,pt->files[i],fl+1);
        memcpy(pt->files[i],bp,bl);
    }
}
Example #5
0
// Test that the block cache works, by smashing the superblock and
// reading it back.
static void
check_bc(void)
{
	struct Super backup;

	// back up super block
	memmove(&backup, diskaddr(1), sizeof backup);

	// smash it 
	strcpy(diskaddr(1), "OOPS!\n");
	flush_block(diskaddr(1));
	assert(va_is_mapped(diskaddr(1)));
	assert(!va_is_dirty(diskaddr(1)));

	// clear it out
	sys_page_unmap(0, diskaddr(1));
	assert(!va_is_mapped(diskaddr(1)));

	// read it back in
	assert(strcmp(diskaddr(1), "OOPS!\n") == 0);

	// fix it
	memmove(diskaddr(1), &backup, sizeof backup);
	flush_block(diskaddr(1));

	cprintf("block cache is good\n");
}
void TextureAtlas::moveQuadsFromIndex(ssize_t oldIndex, ssize_t amount, ssize_t newIndex)
{
    CCASSERT(oldIndex>=0 && amount>=0 && newIndex>=0, "values must be >= 0");
    CCASSERT(newIndex + amount <= _totalQuads, "insertQuadFromIndex:atIndex: Invalid index");
    CCASSERT(oldIndex < _totalQuads, "insertQuadFromIndex:atIndex: Invalid index");

    if( oldIndex == newIndex )
    {
        return;
    }
    //create buffer
    size_t quadSize = sizeof(V3F_C4B_T2F_Quad);
    V3F_C4B_T2F_Quad* tempQuads = (V3F_C4B_T2F_Quad*)malloc( quadSize * amount);
    memcpy( tempQuads, &_quads[oldIndex], quadSize * amount );

    if (newIndex < oldIndex)
    {
        // move quads from newIndex to newIndex + amount to make room for buffer
        memmove( &_quads[newIndex], &_quads[newIndex+amount], (oldIndex-newIndex)*quadSize);
    }
    else
    {
        // move quads above back
        memmove( &_quads[oldIndex], &_quads[oldIndex+amount], (newIndex-oldIndex)*quadSize);
    }
    memcpy( &_quads[newIndex], tempQuads, amount*quadSize);

    free(tempQuads);

    _dirty = true;
}
Example #7
0
/* Scroll a region up, moving the top line into the history. */
void
grid_scroll_history_region(struct grid *gd, u_int upper, u_int lower, u_int bg)
{
	struct grid_line	*gl_history, *gl_upper;
	u_int			 yy;

	/* Create a space for a new line. */
	yy = gd->hsize + gd->sy;
	gd->linedata = xreallocarray(gd->linedata, yy + 1,
	    sizeof *gd->linedata);

	/* Move the entire screen down to free a space for this line. */
	gl_history = &gd->linedata[gd->hsize];
	memmove(gl_history + 1, gl_history, gd->sy * sizeof *gl_history);

	/* Adjust the region and find its start and end. */
	upper++;
	gl_upper = &gd->linedata[upper];
	lower++;

	/* Move the line into the history. */
	memcpy(gl_history, gl_upper, sizeof *gl_history);

	/* Then move the region up and clear the bottom line. */
	memmove(gl_upper, gl_upper + 1, (lower - upper) * sizeof *gl_upper);
	grid_empty_line(gd, lower, bg);

	/* Move the history offset down over the line. */
	gd->hscrolled++;
	gd->hsize++;
}
Example #8
0
void add_dnet(
    RT_ENTRY * route_info,
    uint16_t net,
    BACNET_ADDRESS addr)
{

    DNET *dnet = route_info->dnets;
    DNET *tmp;

    if (dnet == NULL) {
        route_info->dnets = (DNET *) malloc(sizeof(DNET));
        memmove(&route_info->dnets->mac_len, &addr.len, 1);
        memmove(&route_info->dnets->mac[0], &addr.adr[0], MAX_MAC_LEN);
        route_info->dnets->net = net;
        route_info->dnets->state = true;
        route_info->dnets->next = NULL;
    } else {

        while (dnet != NULL) {
            if (dnet->net == net)       /* make sure NETs are not repeated */
                return;
            tmp = dnet;
            dnet = dnet->next;
        }

        dnet = (DNET *) malloc(sizeof(DNET));
        memmove(&dnet->mac_len, &addr.len, 1);
        memmove(&dnet->mac[0], &addr.adr[0], MAX_MAC_LEN);
        dnet->net = net;
        dnet->state = true;
        dnet->next = NULL;
        tmp->next = dnet;
    }
}
Example #9
0
static int
wait4data(Serialport *p, uchar *data, int count)
{
    int d;
    Serial *ser;

    ser = p->s;

    qunlock(ser);
    d = sendul(p->w4data, 1);
    qlock(ser);
    if(d <= 0)
        return -1;
    if(p->ndata >= count)
        p->ndata -= count;
    else {
        count = p->ndata;
        p->ndata = 0;
    }
    memmove(data, p->data, count);
    if(p->ndata != 0)
        memmove(p->data, p->data+count, p->ndata);
    recvul(p->gotdata);
    return count;
}
Example #10
0
void buf_insert(struct buf *buf, char *s, size_t pos) {
  size_t len = strlen(s);
  size_t new_len = buf->len + len;

  size_t new_cap = buf->cap;
  if (new_len + 1 >= new_cap) {
    while (new_len + 1 >= new_cap) {
      new_cap *= 2;
    }
    buf_grow(buf, new_cap);
  }

  // Move the bit after the insertion...
  memmove(
      buf->buf + pos + len,
      buf->buf + pos,
      buf->len - pos);

  // Stitch the new part in.
  memmove(
      buf->buf + pos,
      s,
      len);

  buf->len += len;
  buf->buf[buf->len] = '\0';
}
Example #11
0
bool Base32::Encode32(unsigned char* in, int inLen, unsigned char* out)
{
   if((in == 0) || (inLen <= 0) || (out == 0)) return false;

   int d = inLen / 5;
   int r = inLen % 5;

   unsigned char outBuff[8];

   for(int j = 0; j < d; j++)
   {
      if(!Encode32Block(&in[j * 5], &outBuff[0])) return false;
      memmove(&out[j * 8], &outBuff[0], sizeof(unsigned char) * 8);
   }

   unsigned char padd[5];
   memset(padd, 0, sizeof(unsigned char) * 5);
   for(int i = 0; i < r; i++)
   {
      padd[i] = in[inLen - r + i];
   }
   if(!Encode32Block(&padd[0], &outBuff[0])) return false;
   memmove(&out[d * 8], &outBuff[0], sizeof(unsigned char) * GetEncode32Length(r));

   return true;
}
Example #12
0
int
cmdinsert(Win *w, Rune *r, int nr, int rp)
{
	Rune *s;

	if(nr < 0)
		for(nr = 0, s = r; *s++ != 0; nr++)
			;
	if(rp < 0 || rp > w->nrunes)
		rp = w->nrunes;
	if(w->nrunes + nr > w->arunes){
		w->runes = realloc(w->runes, w->arunes = w->arunes + (nr + RUNEBLK - 1) & ~(RUNEBLK - 1));
		if(w->runes == nil)
			sysfatal("realloc: %r");
	}
	if(rp != w->nrunes)
		memmove(w->runes + rp, w->runes + rp + nr, (w->nrunes - rp) * sizeof(Rune));
	memmove(w->runes + rp, r, nr * sizeof(Rune));
	w->nrunes += nr;
	if(w->toprune > rp)
		w->toprune += nr;
	else{
		frinsert(&w->fr, w->runes + rp, w->runes + rp + nr, rp - w->toprune);
		if(rp == w->nrunes - nr){
			if(w->fr.lastlinefull)
				cmdscroll(w, 1);
		}
	}
	if(w->opoint > rp)
		w->opoint += nr;
	return nr;
}
Example #13
0
/*
 * BT_BROOT -- Fix up the btree root page after it has been split.
 *
 * Parameters:
 *	t:	tree
 *	h:	root page
 *	l:	left page
 *	r:	right page
 *
 * Returns:
 *	RET_ERROR, RET_SUCCESS
 */
static int
bt_broot(BTREE *t, PAGE *h, PAGE *l, PAGE *r)
{
	BINTERNAL *bi;
	BLEAF *bl;
	u_int32_t nbytes;
	char *dest;

	/*
	 * If the root page was a leaf page, change it into an internal page.
	 * We copy the key we split on (but not the key's data, in the case of
	 * a leaf page) to the new root page.
	 *
	 * The btree comparison code guarantees that the left-most key on any
	 * level of the tree is never used, so it doesn't need to be filled in.
	 */
	nbytes = NBINTERNAL(0);
	h->linp[0] = h->upper = t->bt_psize - nbytes;
	dest = (char *)h + h->upper;
	WR_BINTERNAL(dest, 0, l->pgno, 0);

	switch (h->flags & P_TYPE) {
	case P_BLEAF:
		bl = GETBLEAF(r, 0);
		nbytes = NBINTERNAL(bl->ksize);
		__PAST_END(h->linp, 1) = h->upper -= nbytes;
		dest = (char *)h + h->upper;
		WR_BINTERNAL(dest, bl->ksize, r->pgno, 0);
		memmove(dest, bl->bytes, bl->ksize);

		/*
		 * If the key is on an overflow page, mark the overflow chain
		 * so it isn't deleted when the leaf copy of the key is deleted.
		 */
		if (bl->flags & P_BIGKEY &&
		    bt_preserve(t, *(pgno_t *)bl->bytes) == RET_ERROR)
			return (RET_ERROR);
		break;
	case P_BINTERNAL:
		bi = GETBINTERNAL(r, 0);
		nbytes = NBINTERNAL(bi->ksize);
		__PAST_END(h->linp, 1) = h->upper -= nbytes;
		dest = (char *)h + h->upper;
		memmove(dest, bi, nbytes);
		((BINTERNAL *)dest)->pgno = r->pgno;
		break;
	default:
		abort();
	}

	/* There are two keys on the page. */
	h->lower = BTDATAOFF + 2 * sizeof(indx_t);

	/* Unpin the root page, set to btree internal page. */
	h->flags &= ~P_TYPE;
	h->flags |= P_BINTERNAL;
	mpool_put(t->bt_mp, h, MPOOL_DIRTY);

	return (RET_SUCCESS);
}
Example #14
0
void
PrReplaceData (
    char                    *Buffer,
    UINT32                  LengthToRemove,
    char                    *BufferToAdd,
    UINT32                  LengthToAdd)
{
    UINT32                  BufferLength;


    /* Buffer is a string, so the length must include the terminating zero */

    BufferLength = strlen (Buffer) + 1;

    if (LengthToRemove != LengthToAdd)
    {
        /*
         * Move some of the existing data
         * 1) If adding more bytes than removing, make room for the new data
         * 2) if removing more bytes than adding, delete the extra space
         */
        if (LengthToRemove > 0)
        {
            memmove ((Buffer + LengthToAdd), (Buffer + LengthToRemove),
                (BufferLength - LengthToRemove));
        }
    }

    /* Now we can move in the new data */

    if (LengthToAdd > 0)
    {
        memmove (Buffer, BufferToAdd, LengthToAdd);
    }
}
Example #15
0
/* Implementation of SHA1-HMAC.  We're rolling our own just to
 * double-check that the calls libotr makes to libgcrypt are in fact
 * doing the right thing. */
void sha1hmac(unsigned char digest[20], unsigned char key[20],
	unsigned char *data, size_t datalen)
{
    unsigned char ipad[64], opad[64];
    size_t i;
    gcry_md_hd_t sha1;
    gcry_error_t err;
    unsigned char hash[20];

    memset(ipad, 0, 64);
    memset(opad, 0, 64);
    memmove(ipad, key, 20);
    memmove(opad, key, 20);
    for(i=0;i<64;++i) {
	ipad[i] ^= 0x36;
	opad[i] ^= 0x5c;
    }

    err = gcry_md_open(&sha1, GCRY_MD_SHA1, 0);
    if (err) {
	fprintf(stderr, "Error: %s\n", gcry_strerror(err));
	exit(1);
    }
    gcry_md_write(sha1, ipad, 64);
    gcry_md_write(sha1, data, datalen);
    memmove(hash, gcry_md_read(sha1, 0), 20);
    gcry_md_reset(sha1);
    gcry_md_write(sha1, opad, 64);
    gcry_md_write(sha1, hash, 20);
    memmove(digest, gcry_md_read(sha1, 0), 20);
    gcry_md_close(sha1);
}
Example #16
0
static
char * alloc_quoted_token(
  const char * buffer,
  int          length,
  bool         strip_quote_marks)
{
  char * token;
  if(!strip_quote_marks)
  {
    token = util_calloc( (length + 1) , sizeof * token );
    memmove(token, &buffer[0], length * sizeof * token );
    token[length] = '\0';
  }
  else
  {
    token = util_calloc( (length - 1) , sizeof * token);
    memmove(token, &buffer[1], (length -1) * sizeof * token);
    token[length-2] = '\0';
    /**
      Removed escape char before any escaped quotation starts.
    */
    {
      char expr[3];
      char subs[2];
      expr[0] = PARSER_ESCAPE_CHAR;
      expr[1] = buffer[0];
      expr[2] = '\0';
      subs[0] = buffer[0];
      subs[1] = '\0';
      util_string_replace_inplace(&token, expr, subs);
    }
  }
  return token;
}
Example #17
0
void Delete() {
    int id, j;
    int gap, occupied, num_gaps;
    char opt;
    printf("Please enter product ID to perform deletion:\n");
    scanf("%d", &id);
    while (getchar() != '\n');
    if (product_list[--id] == NULL)
        printf("No product found. Double check your product ID.\n");
    else {
        product_list[id] = NULL;
        printf("Product deleted.\nDo you want to update product IDs (will remove gaps between IDs): (y/n)\n");
        if ((opt = getchar()) == 'y') {
            printf("All product IDs updated.\n");
            while (first_occupied(first_available(0) - 1) != -1) {
                gap = first_available(0) - 1;
                occupied = first_occupied(gap) - 1;
                num_gaps = occupied - gap;
                memmove(product_list + gap, product_list + occupied, 30 - occupied);
                memmove(price_list + gap, price_list + occupied, 30 - occupied);
                for (j = 30 - num_gaps; j < 30; j++)
                    product_list[j] = NULL;
            }
        } else {
            printf("No update made.\n");
        }
        while (getchar() != '\n');
    }
    Menu();
}
Example #18
0
File: u_deep.c Project: patope/PUAE
static void reconst(void) {
    USHORT i, j, k, f, l;

    /* collect leaf nodes in the first half of the table */
    /* and replace the freq by (freq + 1) / 2. */
    j = 0;
    for (i = 0; i < T; i++) {
        if (son[i] >= T) {
            freq[j] = (USHORT) ((freq[i] + 1) / 2);
            son[j] = son[i];
            j++;
        }
    }
    /* begin constructing tree by connecting sons */
    for (i = 0, j = N_CHAR; j < T; i += 2, j++) {
        k = (USHORT) (i + 1);
        f = freq[j] = (USHORT) (freq[i] + freq[k]);
        for (k = (USHORT)(j - 1); f < freq[k]; k--);
        k++;
        l = (USHORT)((j - k) * 2);
        memmove(&freq[k + 1], &freq[k], (size_t)l);
        freq[k] = f;
        memmove(&son[k + 1], &son[k], (size_t)l);
        son[k] = i;
    }
    /* connect prnt */
    for (i = 0; i < T; i++) {
        if ((k = son[i]) >= T) {
            prnt[k] = i;
        } else {
            prnt[k] = prnt[k + 1] = i;
        }
    }
}
// Update the text area of pid at the area starting at where. The data copied
// should be in the new_text buffer whose size is given by len. If old_text is
// not null, the original text data will be copied into it. Therefore old_text
// must have the same size as new_text.
int poke_text(pid_t pid, void *where, void *new_text, void *old_text,
              size_t len) {
  if (len % sizeof(void *) != 0) {
    printf("invalid len, not a multiple of %zd\n", sizeof(void *));
    return -1;
  }

  long poke_data;
  for (size_t copied = 0; copied < len; copied += sizeof(poke_data)) {
    memmove(&poke_data, new_text + copied, sizeof(poke_data));
    if (old_text != NULL) {
      errno = 0;
      long peek_data = ptrace(PTRACE_PEEKTEXT, pid, where + copied, NULL);
      if (peek_data == -1 && errno) {
        perror("PTRACE_PEEKTEXT");
        return -1;
      }
      memmove(old_text + copied, &peek_data, sizeof(peek_data));
    }
    if (ptrace(PTRACE_POKETEXT, pid, where + copied, (void *)poke_data) < 0) {
      perror("PTRACE_POKETEXT");
      return -1;
    }
  }
  return 0;
}
Example #20
0
void
tabs_move(view_t *view, int where_to)
{
	int future = MAX(0, MIN(tabs_count(view) - 1, where_to));
	const int current = tabs_current(view);
	const int from = (current <= future ? current + 1 : future);
	const int to = (current <= future ? current : future + 1);

	/* Second check is for the case when the value was already truncated by MIN()
	 * above. */
	if(current < future && where_to < tabs_count(view))
	{
		--future;
	}

	if(cfg.pane_tabs)
	{
		pane_tab_t *const ptabs = get_pane_tabs(view)->tabs;
		const pane_tab_t ptab = ptabs[current];
		memmove(ptabs + to, ptabs + from, sizeof(*ptabs)*abs(future - current));
		ptabs[future] = ptab;
		get_pane_tabs(view)->current = future;
	}
	else
	{
		const global_tab_t gtab = gtabs[current];
		memmove(gtabs + to, gtabs + from, sizeof(*gtabs)*abs(future - current));
		gtabs[future] = gtab;
		current_tab = future;
	}
}
Example #21
0
bool write_to_socket(int fd, buffer_t *buf)
{
    bool close_connection = false;

    // add message length at the beginning of the message
    uint16_t msg_length = htons((uint16_t) buf->msg_length);
    memmove(&buf->buffer[sizeof(msg_length)], &buf->buffer[0], buf->msg_length);
    memcpy(&buf->buffer[0], (char*)&msg_length, sizeof(msg_length));
    buf->in_buffer += sizeof(msg_length);
    buf->msg_length += sizeof(msg_length);

    do {
        ssize_t bytes_send = send(fd, &buf->buffer[0], buf->msg_length, 0);
        if (bytes_send < 0) {
            if (errno != EWOULDBLOCK)
                syserr("send() failed");
        }

        // remove send bytes
        buf->msg_length -= bytes_send;
        memmove(&buf->buffer[0], &buf->buffer[bytes_send], buf->msg_length);
    } while (buf->msg_length != 0);

    // clean buffer
    clean_buffer(buf, true);
    return close_connection;
}
Example #22
0
//-----------------------------------------------------------------------------------------//
bool Insert( hash_table_t *ht,
             uint8_t *key,
             size_t keySize,
             uint8_t *value,
             size_t valueSize )
{
    uint32_t h = Hash( key, keySize );
    uint32_t i = h % BUCKET_SIZE;
    bucket_t *b = ht->buckets + i;

    while( b->next != NULL )
    {
        b = (bucket_t*)b->next;
    }

    uint8_t *tmp = NULL;

    tmp = new uint8_t[keySize];
    memmove( tmp, key, keySize );
    b->key = tmp;
    b->keySize = keySize;

    tmp = new uint8_t[valueSize];
    memmove( tmp, value, valueSize );
    b->value = tmp;
    b->valueSize = valueSize;

    return true;
}
Example #23
0
File: main.c Project: dimatura/sxiv
void remove_file(int n, bool manual) {
	if (n < 0 || n >= filecnt)
		return;

	if (filecnt == 1) {
		if (!manual)
			fprintf(stderr, "sxiv: no more files to display, aborting\n");
		cleanup();
		exit(manual ? EXIT_SUCCESS : EXIT_FAILURE);
	}

	if (files[n].path != files[n].name)
		free((void*) files[n].path);
	free((void*) files[n].name);

	if (n + 1 < filecnt)
		memmove(files + n, files + n + 1, (filecnt - n - 1) * sizeof(fileinfo_t));
	if (n + 1 < tns.cnt) {
		memmove(tns.thumbs + n, tns.thumbs + n + 1, (tns.cnt - n - 1) *
		        sizeof(thumb_t));
		memset(tns.thumbs + tns.cnt - 1, 0, sizeof(thumb_t));
	}

	filecnt--;
	if (n < tns.cnt)
		tns.cnt--;
}
Example #24
0
qint64 OutputALSA::writeAudio(unsigned char *data, qint64 maxSize)
{
    if((maxSize = qMin(maxSize, m_prebuf_size - m_prebuf_fill)) > 0)
    {
        memmove(m_prebuf + m_prebuf_fill, data, maxSize);
        m_prebuf_fill += maxSize;
    }

    snd_pcm_uframes_t l = snd_pcm_bytes_to_frames(pcm_handle, m_prebuf_fill);

    while (l >= m_chunk_size)
    {
        snd_pcm_wait(pcm_handle, 10);
        long m;
        if ((m = alsa_write(m_prebuf, m_chunk_size)) >= 0)
        {
            l -= m;
            m = snd_pcm_frames_to_bytes(pcm_handle, m); // convert frames to bytes
            m_prebuf_fill -= m;
            memmove(m_prebuf, m_prebuf + m, m_prebuf_fill); //move data to begin
        }
        else
            return -1;
    }
    return maxSize;
}
Example #25
0
/* Rotate in a new DH public key for our correspondent.  Be sure to keep
 * the sesskeys array in sync. */
static gcry_error_t rotate_y_keys(ConnContext *context, gcry_mpi_t new_y)
{
    gcry_error_t err;

    /* Rotate the public key */
    gcry_mpi_release(context->their_old_y);
    context->their_old_y = context->their_y;

    /* Rotate the session keys */
    err = reveal_macs(context, &(context->sesskeys[0][1]),
	    &(context->sesskeys[1][1]));
    if (err) return err;
    otrl_dh_session_free(&(context->sesskeys[0][1]));
    otrl_dh_session_free(&(context->sesskeys[1][1]));
    memmove(&(context->sesskeys[0][1]), &(context->sesskeys[0][0]),
	    sizeof(DH_sesskeys));
    memmove(&(context->sesskeys[1][1]), &(context->sesskeys[1][0]),
	    sizeof(DH_sesskeys));

    /* Copy in the new public key */
    context->their_y = gcry_mpi_copy(new_y);
    context->their_keyid++;

    /* Make the session keys */
    err = otrl_dh_session(&(context->sesskeys[0][0]),
	    &(context->our_dh_key), context->their_y);
    if (err) return err;
    err = otrl_dh_session(&(context->sesskeys[1][0]),
	    &(context->our_old_dh_key), context->their_y);
    if (err) return err;

    return gcry_error(GPG_ERR_NO_ERROR);
}
Example #26
0
void
wclosewin(Window *w)
{
	print_func_entry();
	int i;

	w->deleted = TRUE;
	if(w == input){
		input = nil;
	}
	if(w == wkeyboard)
		wkeyboard = nil;
	for(i=0; i<nhidden; i++)
		if(hidden[i] == w){
			--nhidden;
			memmove(hidden+i, hidden+i+1, (nhidden-i)*sizeof(hidden[0]));
			hidden[nhidden] = nil;
			break;
		}
	for(i=0; i<nwindow; i++)
		if(window[i] == w){
			--nwindow;
			memmove(window+i, window+i+1, (nwindow-i)*sizeof(Window*));
			w->deleted = TRUE;
			print_func_exit();
			return;
		}
	error("unknown window in closewin");
	print_func_exit();
}
Example #27
0
/* ARGSUSED */
void
chargen_stream(int s, struct servtab *sep)
{
	int len;
	char *rs, text[LINESIZ+2];

	inetd_setproctitle(sep->se_service, s);

	if (!endring) {
		initring();
		rs = ring;
	}

	text[LINESIZ] = '\r';
	text[LINESIZ + 1] = '\n';
	for (rs = ring;;) {
		if ((len = endring - rs) >= LINESIZ)
			memmove(text, rs, LINESIZ);
		else {
			memmove(text, rs, len);
			memmove(text + len, ring, LINESIZ - len);
		}
		if (++rs == endring)
			rs = ring;
		if (write(s, text, sizeof(text)) != sizeof(text))
			break;
	}
	exit(0);
}
Example #28
0
int crypto_sign_open(
  unsigned char *sm, unsigned long long smlen,
  const unsigned char *pk
)
{
  unsigned char scopy[32];
  unsigned char h[64];
  unsigned char rcheck[32];
  ge_p3 A;
  ge_p2 R;

  if (smlen < 64) goto badsig;
  if (sm[63] & 224) goto badsig;
  if (ge_frombytes_negate_vartime(&A,pk) != 0) goto badsig;

  memmove(scopy,sm + 32,32);

  memmove(sm + 32,pk,32);
  crypto_hash_sha512(h,sm,smlen);
  sc_reduce(h);

  ge_double_scalarmult_vartime(&R,h,&A,scopy);
  ge_tobytes(rcheck,&R);
  if (crypto_verify_32(rcheck,sm) == 0)
    return 0;

badsig:
  return -1;
}
Example #29
0
static void EscapesToBinary(char *str)
{
    for(; *str; str++)
    {
        if(*str!='\\') continue;
        if(str[1]=='n') {
            *str++='\r';
            *str='\n';
            continue;
        }
        if(str[1]=='0')
        {
            char *codeend;
            *str=(char)strtol(str+1,&codeend,8);
            if(*str==0) {
                *str='\\';
                continue;
            }
            memmove(str+1,codeend,strlennull(codeend)+1);
            continue;
        }
        for(int i=0; i<SIZEOF(escapes); i+=2)
            if(str[1]==escapes[i])
            {
                *str=escapes[i+1];
                memmove(str+1,str+2,strlennull(str)-1);
                break;
            }
    }
}
Example #30
0
runtime·getenv(int8 *s)
{
	int32 fd, n, r;
	intgo len;
	byte file[128];
	byte *p;

	len = runtime·findnull((byte*)s);
	if(len > sizeof file-6)
		return nil;

	runtime·memclr(file, sizeof file);
	runtime·memmove((void*)file, (void*)"/env/", 5);
	runtime·memmove((void*)(file+5), (void*)s, len);

	fd = runtime·open((int8*)file, OREAD, 0);
	if(fd < 0)
		return nil;
	n = runtime·seek(fd, 0, 2);
	p = runtime·malloc(n+1);
	r = runtime·pread(fd, p, n, 0);
	runtime·close(fd);
	if(r < 0)
		return nil;
	return p;
}