Exemple #1
0
static int cmd_exec(int fd, struct cmd_t cmd){
    FILE *f;
    int temp;
    int read_status;
    f = fdopen(fd, "w");
    if (f == NULL) return 1;

    if(strcmp( cmd.name , "get_single_temp" ) == 0){
        read_status = buf_get(&temp);
        if (read_status == 1) fprintf(f,"napaka pri branju bufferja\n");
        if (read_status == 2) fprintf(f,"buf je prazen\n");
        if (!read_status) fprintf(f,"%d\n",temp);

    }else if(strcmp( cmd.name , "get_last_temp" ) == 0){
        fprintf(f,"%d\n",lastTemp); // last temp bi mogla biti sinhronizirana

    }else if(strcmp( cmd.name , "get_temp_all" ) == 0){
        read_status = buf_get(&temp);
        if (read_status == 2) fprintf(f,"buf je prazen\n");
        while (!read_status){
            fprintf(f,"%d\n", temp);
            read_status = buf_get(&temp);
        }
        if (read_status == 1) fprintf(f,"napaka pri branju bufferja\n");
    }

    fclose(f);
    return 0;
}
Exemple #2
0
/* The character after the character after the word is returned. dr->buf.tail is one past that. dr->buf.str will point to the
 * token which will be '\0' terminated.
 */
static char
read_name_token(SaxDrive dr) {
    char        c;

    dr->buf.str = dr->buf.tail;
    c = buf_get(&dr->buf);
    if (is_white(c)) {
        c = buf_next_non_white(&dr->buf);
        dr->buf.str = dr->buf.tail - 1;
    }
    while (1) {
	switch (c) {
	case ' ':
	case '\t':
	case '\f':
	case '?':
	case '=':
	case '/':
	case '>':
	case '<':
	case '\n':
	case '\r':
            *(dr->buf.tail - 1) = '\0';
	    return c;
	case '\0':
            /* documents never terminate after a name token */
            ox_sax_drive_error(dr, NO_TERM "document not terminated");
            return '\0';
	default:
	    break;
	}
        c = buf_get(&dr->buf);
    }
    return '\0';
}
Exemple #3
0
static void
read_content(SaxDrive dr, char *content, size_t len) {
    char	c;
    char	*end = content + len;

    while ('\0' != (c = buf_get(&dr->buf))) {
	if (end < content) {
	    ox_sax_drive_error(dr, "processing instruction content too large");
	    return;
	}
	if ('?' == c) {
	    if ('\0' == (c = buf_get(&dr->buf))) {
		ox_sax_drive_error(dr, NO_TERM "document not terminated");
	    }
	    if ('>' == c) {
		*content = '\0';
		return;
	    } else {
		*content++ = c;
	    }
	} else {
	    *content++ = c;
	}
    }
    *content = '\0';
}
void parseCommand(void)
{
	char byte1;
	char byte2;
	char byte3;
	char byte4;
	
	buf_get(&byte1);
	buf_get(&byte2);
	buf_get(&byte3);
	buf_get(&byte4);
	
	if(byte1 & CMD_TYPE_bm){
		CMDstate = Speed;
		if(byte1 & ML_DIR_bm){
			ioport_set_pin_level(PE0,1);
		}//if
		else{
			ioport_set_pin_level(PE0,0);
		}//else
		mlDutyCycle = byte2;
		
		if(byte1 & MR_DIR_bm){
			ioport_set_pin_level(PE2,0);
		}//if
		else{
			ioport_set_pin_level(PE2,1);
		}//else
		mrDutyCycle = byte3;
		
	}//if
	else{
		if(byte1 & POS_GET){
			sendPositionFlag = true;	
		}//if
		else{
			if(byte1 & ML_POS_bm){
				CMDstate = Position;
				if(byte2 <= 192){
					mlDestination = byte2;
				}//if
			}//if
			if(byte1 & MR_POS_bm){
				CMDstate = Position;
				if(byte3 <= 192){
					mrDestination = byte3;
				}//if
			}//if
		}//else
	}//else
}//parseCommand
Exemple #5
0
/* Entered after the "<!DOCTYPE" sequence. Ready to read the rest.
 */
static char
read_doctype(SaxDrive dr) {
    int		line = dr->buf.line;
    int		col = dr->buf.col - 10;
    char	*s;

    buf_backup(&dr->buf); /* back up to the start in case the cdata is empty */
    buf_protect(&dr->buf);
    read_delimited(dr, '>');
    if (dr->options.smart && 0 == dr->hints) {
	for (s = dr->buf.str; is_white(*s); s++) { }
	if (0 == strncasecmp("HTML", s, 4)) {
	    dr->hints = ox_hints_html();
	}
    }
    *(dr->buf.tail - 1) = '\0';
    if (dr->has.doctype) {
        VALUE       args[1];

	if (dr->has.line) {
	    rb_ivar_set(dr->handler, ox_at_line_id, LONG2NUM(line));
	}
	if (dr->has.column) {
	    rb_ivar_set(dr->handler, ox_at_column_id, LONG2NUM(col));
	}
        args[0] = rb_str_new2(dr->buf.str);
        rb_funcall2(dr->handler, ox_doctype_id, 1, args);
    }
    dr->buf.str = 0;

    return buf_get(&dr->buf);
}
Exemple #6
0
int use_blk(ALLOC * a, handle_t h, void *buf, size_t len)
{
	size_t need = len_need(len);
	size_t padding = len2atom(len) * ATOM_LEN - need;
	unsigned char *b;

	if ((b = buf_get(a, need + padding)) == NULL)
		return -1;

	if (len <= CTBLK_MAXSHORT) {
		b[0] = CTBLK_SHORT;
		b[1] = (unsigned char)len;
		memcpy(&b[2], buf, len);
		bzero(&b[2 + len], padding);
	} else {
		b[0] = CTBLK_LONG;
		uint16tob(&b[1], len);
		memcpy(&b[3], buf, len);
		bzero(&b[3 + len], padding);
	}
	if (writeat(a->fd, b, need + padding, hdl2off(h)) != need + padding) {
		buf_put(a, b);
		return -1;
	}
	buf_put(a, b);
	return 0;
}
Exemple #7
0
handle_t alloc_record(ALLOC * a, table_t * t, record_t * r)
{
    unsigned char *buf, *p;
    handle_t h = 0;

    if (_validate_record(a, t, r) < 0 || _validate_unique(a, t, r) < 0)
        return 0;
    if ((buf = buf_get(a, _sizeof_record(t, r))) == NULL)
        return 0;

    r->prev = t->tail;
    r->next = 0;
    p = record2b(buf, t, r);
    h = alloc_blk(a, buf, p - buf);
    buf_put(a, buf);
    if (h == 0)
        return 0;
    r->self = h;
    if (_list_add_record(a, t, r) < 0)
        return 0;

    for (int i = 0; i < t->ncols; i++)
        if (t->cols[i].index != 0)
            index_set(t->cols[i].idx, r, i);

    return h;
}
Exemple #8
0
int match_naive(circular_buffer *text, const char *target)
{
    /*
     * Performs O(n*m) naive matching
     */
    int target_idx = 0;
    int text_idx = 0;;

    while (!text->ended)
    {
        for(target_idx = 0; *(target + target_idx); target_idx++)
        {
            if (buf_get(text, text_idx + target_idx) != *(target + target_idx))
            {
                break;
            }
        }
        if (*(target + target_idx) == 0) /* end of target */
        {
            return 1;
        }
        else /* more text to check */
        {
            text_idx++;
        }
    }
    return 0;
}
Exemple #9
0
int match_kmp(circular_buffer *text, const char *target) {
    int m = strlen(target);
    int *jumps = kmp_jump_table(target, m);
    int target_idx = 0;
    int text_idx = 0;
    char text_char = 0;

    while ((text_char = buf_get(text, text_idx)) != -1)
    {
        if (target_idx >= m) /* match condition */
        {
            return 1;
        }

        if (!is_legal(text_char))
        {
            target_idx = 0;
        }
        else
        {
            target_idx = jumps[get_idx(target_idx, text_char)];
        }
        text_idx++;
    }

    free(jumps);
    return target_idx >= m;
}
Exemple #10
0
int
decode_citrix(u_char *buf, int len, u_char *obuf, int olen)
{
	struct buf inbuf, outbuf;
	u_char key, c, t[2];
	int i;
	
	buf_init(&inbuf, buf, len);
	buf_init(&outbuf, obuf, olen);
	
	while ((i = buf_index(&inbuf, ica_magic, sizeof(ica_magic))) >= 0) {
		buf_skip(&inbuf, i);
		
		if (buf_len(&inbuf) < 60)
			break;
		
		buf_skip(&inbuf, 17);
		
		if (buf_get(&inbuf, &key, 1) != 1)
			break;
		
		buf_skip(&inbuf, 42);
		
		if (buf_get(&inbuf, &c, 1) != 1)
			break;

		c ^= ('C' | key);
		
		buf_put(&outbuf, &c, 1);
		
		i = 0;
		while (buf_get(&inbuf, t, 2) == 2) {
			c = t[0] ^ t[1] ^ key;
			
			if (c == '\0') {
				buf_put(&outbuf, "\n", 1);
				if (++i > 2) break;
			}
			buf_put(&outbuf, &c, 1);
		}
	}
	buf_end(&outbuf);
	
	return (buf_len(&outbuf));
}
Exemple #11
0
/* The character after the quote or if there is no quote, the character after the word is returned. dr->buf.tail is one past
 * that. dr->buf.str will point to the token which will be '\0' terminated.
 */
static char
read_quoted_value(SaxDrive dr) {
    char	c;

    c = buf_get(&dr->buf);
    if (is_white(c)) {
        c = buf_next_non_white(&dr->buf);
    }
    if ('"' == c || '\'' == c) {
	char	term = c;

        dr->buf.str = dr->buf.tail;
        while (term != (c = buf_get(&dr->buf))) {
            if ('\0' == c) {
                ox_sax_drive_error(dr, NO_TERM "quoted value not terminated");
                return '\0';
            }
        }
	// dr->buf.tail is one past quote char
	*(dr->buf.tail - 1) = '\0'; /* terminate value */
	c = buf_get(&dr->buf);
	return c;
    }
    // not quoted, look for something that terminates the string
    dr->buf.str = dr->buf.tail - 1;
    ox_sax_drive_error(dr, WRONG_CHAR "attribute value not in quotes");
    while ('\0' != (c = buf_get(&dr->buf))) {
	switch (c) {
	case ' ':
	case '/':
	case '>':
	case '?': // for instructions
	case '\t':
	case '\n':
	case '\r':
	    *(dr->buf.tail - 1) = '\0'; /* terminate value */
	    // dr->buf.tail is in the correct position, one after the word terminator
	    return c;
	default:
	    break;
	}
    }
    return '\0'; // should never get here
}
Exemple #12
0
static char
read_text(SaxDrive dr) {
    char        c;
    int		line = dr->buf.line;
    int		col = dr->buf.col - 1;

    buf_backup(&dr->buf);
    buf_protect(&dr->buf);
    while ('<' != (c = buf_get(&dr->buf))) {
        if ('\0' == c) {
            ox_sax_drive_error(dr, NO_TERM "text not terminated");
	    break;
        }
    }
    if ('\0' != c) {
	*(dr->buf.tail - 1) = '\0';
    }
    if (dr->has.value) {
        VALUE   args[1];

	if (dr->has.line) {
	    rb_ivar_set(dr->handler, ox_at_line_id, LONG2NUM(line));
	}
	if (dr->has.column) {
	    rb_ivar_set(dr->handler, ox_at_column_id, LONG2NUM(col));
	}
	*args = dr->value_obj;
        rb_funcall2(dr->handler, ox_value_id, 1, args);
    } else if (dr->has.text) {
        VALUE   args[1];

        if (dr->options.convert_special) {
            ox_sax_collapse_special(dr, dr->buf.str, line, col);
        }
        args[0] = rb_str_new2(dr->buf.str);
#if HAS_ENCODING_SUPPORT
        if (0 != dr->encoding) {
            rb_enc_associate(args[0], dr->encoding);
        }
#elif HAS_PRIVATE_ENCODING
        if (Qnil != dr->encoding) {
	    rb_funcall(args[0], ox_force_encoding_id, 1, dr->encoding);
        }
#endif
	if (dr->has.line) {
	    rb_ivar_set(dr->handler, ox_at_line_id, LONG2NUM(line));
	}
	if (dr->has.column) {
	    rb_ivar_set(dr->handler, ox_at_column_id, LONG2NUM(col));
	}
        rb_funcall2(dr->handler, ox_text_id, 1, args);
    }
    dr->buf.str = 0;

    return c;
}
Exemple #13
0
/*
 * Get an empty, disassociated buffer of given size.
 */
struct buf *
geteblk(int size)
{
	struct buf *bp;

	while ((bp = buf_get(NULL, 0, size)) == NULL)
		;

	return (bp);
}
Exemple #14
0
/*-------------------------------------------------------------------------*\
* Reads everything until the connection is closed (buffered)
\*-------------------------------------------------------------------------*/
static int recvall(p_buf buf, luaL_Buffer *b) {
    int err = IO_DONE;
    while (err == IO_DONE) {
        const char *data; size_t count;
        err = buf_get(buf, &data, &count);
        luaL_addlstring(b, data, count);
        buf_skip(buf, count);
    }
    if (err == IO_CLOSED) return IO_DONE;
    else return err;
}
Exemple #15
0
static char
skipBOM(SaxDrive dr) {
    char        c = buf_get(&dr->buf);

    if (0xEF == (uint8_t)c) { /* only UTF8 is supported */
	if (0xBB == (uint8_t)buf_get(&dr->buf) && 0xBF == (uint8_t)buf_get(&dr->buf)) {
#if HAS_ENCODING_SUPPORT
	    dr->encoding = ox_utf8_encoding;
#elif HAS_PRIVATE_ENCODING
	    dr->encoding = ox_utf8_encoding;
#else
	    dr->encoding = UTF8_STR;
#endif
	    c = buf_get(&dr->buf);
	} else {
	    ox_sax_drive_error(dr, BAD_BOM "invalid BOM or a binary file.");
	    c = '\0';
	}
    }
    return c;
}
Exemple #16
0
static char
read_delimited(SaxDrive dr, char end) {
    char	c;

    if ('"' == end || '\'' == end) {
	while (end != (c = buf_get(&dr->buf))) {
	    if ('\0' == c) {
		ox_sax_drive_error(dr, NO_TERM "doctype not terminated");
		return c;
	    }
	}
    } else {
	while (1) {
	    c = buf_get(&dr->buf);
	    if (end == c) {
		return c;
	    }
	    switch (c) {
	    case '\0':
		ox_sax_drive_error(dr, NO_TERM "doctype not terminated");
		return c;
	    case '"':
		c = read_delimited(dr, c);
		break;
	    case '\'':
		c = read_delimited(dr, c);
		break;
	    case '[':
		c = read_delimited(dr, ']');
		break;
	    case '<':
		c = read_delimited(dr, '>');
		break;
	    default:
		break;
	    }
	}
    }
    return c;
}
Exemple #17
0
/*-------------------------------------------------------------------------*\
* Reads a fixed number of bytes (buffered)
\*-------------------------------------------------------------------------*/
static int recvraw(p_buf buf, size_t wanted, luaL_Buffer *b) {
    int err = IO_DONE;
    size_t total = 0;
    while (total < wanted && err == IO_DONE) {
        size_t count; const char *data;
        err = buf_get(buf, &data, &count);
        count = MIN(count, wanted - total);
        luaL_addlstring(b, data, count);
        buf_skip(buf, count);
        total += count;
    }
    return err;
}
Exemple #18
0
/*
 * Get a block of requested size that is associated with
 * a given vnode and block offset. If it is found in the
 * block cache, mark it as having been found, make it busy
 * and return it. Otherwise, return an empty block of the
 * correct size. It is up to the caller to ensure that the
 * cached blocks be of the correct size.
 */
struct buf *
getblk(struct vnode *vp, daddr_t blkno, int size, int slpflag, int slptimeo)
{
	struct buf *bp;
	struct buf b;
	int s, error;

	/*
	 * XXX
	 * The following is an inlined version of 'incore()', but with
	 * the 'invalid' test moved to after the 'busy' test.  It's
	 * necessary because there are some cases in which the NFS
	 * code sets B_INVAL prior to writing data to the server, but
	 * in which the buffers actually contain valid data.  In this
	 * case, we can't allow the system to allocate a new buffer for
	 * the block until the write is finished.
	 */
start:
	s = splbio();
	b.b_lblkno = blkno;
	bp = RB_FIND(buf_rb_bufs, &vp->v_bufs_tree, &b);
	if (bp != NULL) {
		if (ISSET(bp->b_flags, B_BUSY)) {
			SET(bp->b_flags, B_WANTED);
			error = tsleep(bp, slpflag | (PRIBIO + 1), "getblk",
			    slptimeo);
			splx(s);
			if (error)
				return (NULL);
			goto start;
		}

		if (!ISSET(bp->b_flags, B_INVAL)) {
			bcstats.cachehits++;
			SET(bp->b_flags, B_CACHE);
			bufcache_take(bp);
			buf_acquire(bp);
			splx(s);
			return (bp);
		}
	}
	splx(s);

	if ((bp = buf_get(vp, blkno, size)) == NULL)
		goto start;

	return (bp);
}
Exemple #19
0
static int WriteBuffer(dtaudio_output_t *aout) {
    aout_sys_t *sys = (aout_sys_t *) aout->ao_priv;
    const int unit_size = sys->samples_per_buf * bytesPerSample(aout);

    /* Check if we can fill at least one buffer unit by chaining blocks */
    if (sys->dbt.level < unit_size) {
        return false;
    }

    SLAndroidSimpleBufferQueueState st;
    SLresult res = GetState(sys->playerBufferQueue, &st);
    if (unlikely(res != SL_RESULT_SUCCESS)) {
        return false;
    }

    if (st.count == OPENSLES_BUFFERS)
        return false;

    int done = 0;
    while (done < unit_size) {
        int cur = buf_level(&sys->dbt);
        if (cur > unit_size - done)
            cur = unit_size - done;

        //memcpy(&sys->buf[unit_size * sys->next_buf + done], b->p_buffer, cur);
        buf_get(&sys->dbt, &sys->buf[unit_size * sys->next_buf + done], cur);
        done += cur;

        if (done == unit_size)
            break;
    }

    SLresult r = Enqueue(sys->playerBufferQueue,
                         &sys->buf[unit_size * sys->next_buf], unit_size);

    sys->samples -= sys->samples_per_buf;
    //__android_log_print(ANDROID_LOG_DEBUG,TAG, "minus sampels, %d minus %d \n",sys->samples, sys->samples_per_buf);

    if (r == SL_RESULT_SUCCESS) {
        if (++sys->next_buf == OPENSLES_BUFFERS)
            sys->next_buf = 0;
        return true;
    } else {
        /* XXX : if writing fails, we don't retry */
        return false;
    }
}
Exemple #20
0
void *_read_blk(ALLOC * a, handle_t handle, void *buf, size_t * len)
{
	if (handle == 0)
		return NULL;
	off_t offset;
	size_t need;
	unsigned char bytes[ATOM_LEN];
	int newbuf = 0, redirect = 0;

 Retry:
	offset = hdl2off(handle);
	if (readat(a->fd, bytes, 8, offset) != 8)
		return NULL;
	switch (bytes[0]) {
	case CTBLK_SHORT:
		need = (size_t) bytes[1];
		offset += 2;
		break;
	case CTBLK_LONG:
		need = (size_t) b2uint16(&bytes[1]);
		offset += 3;
		break;
	case REBLK:
		if (redirect)	// allow redirect only once
			return NULL;
		handle = b2hdl(&bytes[1]);
		redirect = 1;
		goto Retry;
	default:
		xerrno = FATAL_BLKTAG;
		return NULL;
	}
	if (buf == NULL || need > *len) {
		if ((buf = buf_get(a, need)) == NULL)
			return NULL;
		*len = need;
		newbuf = 1;
	}
	if (readat(a->fd, buf, need, offset) != need) {
		if (newbuf)
			buf_put(a, buf);
		return NULL;
	}
	return buf;
}
Exemple #21
0
/*-------------------------------------------------------------------------*\
* Reads a line terminated by a CR LF pair or just by a LF. The CR and LF 
* are not returned by the function and are discarded from the buffer
\*-------------------------------------------------------------------------*/
static int recvline(p_buf buf, luaL_Buffer *b) {
    int err = IO_DONE;
    while (err == IO_DONE) {
        size_t count, pos; const char *data;
        err = buf_get(buf, &data, &count);
        pos = 0;
        while (pos < count && data[pos] != '\n') {
            /* we ignore all \r's */
            if (data[pos] != '\r') luaL_putchar(b, data[pos]);
            pos++;
        }
        if (pos < count) { /* found '\n' */
            buf_skip(buf, pos+1); /* skip '\n' too */
            break; /* we are done */
        } else /* reached the end of the buffer */
            buf_skip(buf, pos);
    }
    return err;
}
Exemple #22
0
/**
 * @brief Send a message using shared memory.
 *
 * @param[in] buf
 * @param[in] signaled
 *
 * @return status
 */
static int shmem_send_message(buf_t *buf, int from_init)
{
    /* Keep a reference on the buffer so it doesn't get freed. will be
     * returned by the remote side with type=BUF_SHMEM_RETURN. */
    assert(buf->obj.obj_pool->type == POOL_SBUF);
    buf_get(buf);

    buf->type = BUF_SHMEM_SEND;

    if (buf->mem_buf) {
        buf->dest.shmem.local_rank = buf->mem_buf->shmem.index_owner;
    }

    buf->shmem.index_owner = buf->obj.obj_ni->mem.index;

    shmem_enqueue(buf->obj.obj_ni, buf, buf->dest.shmem.local_rank);

    return PTL_OK;
}
Exemple #23
0
// functions
////////////////////////////////////////////////////////////////////////////////
int getter() {
    // Description
    // This function repeatedly reads a character from the buffer and writes it
    // to stdin. Each time, if the buffer is empty, it waits for a position to
    // become full. The function returns when the q or Q character is read from
    // the buffer and has been written to stdin,
    //
    // Returns
    // putter returns 0 on successful completion or -1 in case of failure.

    // variable declaration
    char c;
    int return_value;  // integer placeholder for error checking

    printf("The characters read from the circular buffer will be printed.\n");
    printf("The program will terminate when the \"q\" or \"Q\" character\n");
    printf("is read from the buffer.\n");
    printf("\n");

    // Get characters out of the buffer.
    do {
        do {
            return_value = buf_get(&c);
            if ((return_value != 1) && (return_value != 0)) {
                //printf("error, buf_get\n");  // debug message
                return -1;
            }
        } while (return_value == 0);

        printf("%c", c);
        return_value = fflush(stdout);
        if (return_value == EOF) {
            perror("error, fflush");
            return -1;
        }

    } while ((c != 'q') && (c != 'Q'));

    printf("\n");

    return 0;
}
Exemple #24
0
/*===========================================================================*
 *				read_hook				     *
 *===========================================================================*/
int read_hook(struct inode *node, off_t off, char **ptr,
	size_t *len, cbdata_t cbdata)
{
	/* Regular file read hook. Call the appropriate callback function to
	 * generate and return the data.
	 */

	buf_init(off, *len);

	/* Populate the buffer with the proper content. */
	if (get_inode_index(node) != NO_INDEX) {
		pid_read(node);
	} else {
		((void (*) (void)) cbdata)();
	}

	*len = buf_get(ptr);

	return OK;
}
Exemple #25
0
int main(int argc, char * argv[]) {
    
    buf_init(80);
    //printf("buf_init returns: %d\n", buf_init(3));

    char buf;
    /* arxi empeirou kodika */
    while( 1337 + 1337) {
        buf_get(&buf);
        //printf("We got : %c\n", buf);
        if ( buf == 'q' ) {
            //printf("\n Bye! :)\n");
            break;
        }
    }
    /* telos empeirou kodika */

    buf_destroy();
    //printf("buf_destroy returns: %d\n", buf_destroy() );
    return 0;
}
Exemple #26
0
void *read_blk(ALLOC * a, handle_t handle, void *buf, size_t * len)
{
	void *cbuf, *ret;
	size_t need;

	if ((cbuf = cache_get(a, handle, &need)) != NULL) {
		if (buf == NULL || need > *len) {
			if ((buf = buf_get(a, need)) == NULL)
				return NULL;
			*len = need;
		}
		memcpy(buf, cbuf, need);
		return buf;
	}

	ret = _read_blk(a, handle, buf, len);
	if (ret != NULL)
		cache_set(a, handle, ret, *len);

	return ret;
}
Exemple #27
0
int
decode_icq(u_char *buf, int len, u_char *obuf, int olen)
{
	struct buf inbuf, outbuf;
	u_short version, cmd;
	u_int32_t uin;
	u_char *p;

	buf_init(&inbuf, buf, len);
	buf_init(&outbuf, obuf, olen);

	if (buf_get(&inbuf, &version, sizeof(version)) != sizeof(version))
		return (0);

	version = pletohs(&version);
	
	switch (version) {
	case 2:
		if (buf_seek(&inbuf, ICQ2_CMD_OFFSET, SEEK_SET) < 0)
			return (0);

		if (buf_get(&inbuf, &cmd, sizeof(cmd)) != sizeof(cmd))
			return (0);

		if ((cmd = pletohs(&cmd)) != 1000)
			return (0);

		if (buf_seek(&inbuf, ICQ2_UIN_OFFSET, SEEK_SET) < 0)
			return (0);

		if (buf_get(&inbuf, &uin, sizeof(uin)) != sizeof(uin))
			return (0);
		
		uin = pletohl(&uin);

		if (buf_seek(&inbuf, ICQ2_PASS_OFFSET, SEEK_SET) < 0)
			return (0);
		break;
		
	case 5:
	{
		u_int32_t a1, a2, a3, a4, a5, c, key, i, k;
		
		if (buf_seek(&inbuf, ICQ5_CKSUM_OFFSET, SEEK_SET) < 0)
			return (0);
		
		if (buf_get(&inbuf, &c, sizeof(c)) != sizeof(c))
			return (0);
		
		c = pletohl(&c);
		
		a1 = c & 0x0001f000; a1 = a1 >> 0x0c;
		a2 = c & 0x07c007c0; a2 = a2 >> 0x01;
		a3 = c & 0x003e0001; a3 = a3 << 0x0a;
		a4 = c & 0xf8000000; a4 = a4 >> 0x10;
		a5 = c & 0x0000083e; a5 = a5 << 0x0f;
		
		key = len * 0x68656C6C;
		key += a1 + a2 + a3 + a4 + a5;

		p = inbuf.base;
		
		for (i = 0x0a; i < inbuf.end + 3; i += 4) {
			k = key + icq5_table[i & 0xff];
			if (i != 0x16) {
				p[i] ^= (u_char)(k & 0xff);
				p[i + 1] ^= (u_char)((k & 0xff00) >> 8);
			}
			if (i != 0x12) {
				p[i + 2] ^= (u_char)((k & 0xff0000) >> 16);
				p[i + 3] ^= (u_char)((k & 0xff000000) >> 24);
			}
		}
		if (buf_seek(&inbuf, ICQ5_CMD_OFFSET, SEEK_SET) < 0)
			return (0);

		if (buf_get(&inbuf, &cmd, sizeof(cmd)) != sizeof(cmd))
			return (0);

		if ((cmd = pletohs(&cmd)) != 1000)
			return (0);

		if (buf_seek(&inbuf, ICQ5_UIN_OFFSET, SEEK_SET) < 0)
			return (0);

		if (buf_get(&inbuf, &uin, sizeof(uin)) != sizeof(uin))
			return (0);
		
		uin = pletohl(&uin);

		if (buf_seek(&inbuf, ICQ5_PASS_OFFSET, SEEK_SET) < 0)
			return (0);
	}
Exemple #28
0
int
decode_mmxp(u_char *buf, int len, u_char *obuf, int olen)
{
	struct buf inbuf, outbuf;
	u_char *p, c;
	u_int32_t i;
	int encrypt;

	buf_init(&inbuf, buf, len);
	buf_init(&outbuf, obuf, len);

	while ((i = buf_index(&inbuf, "\x00\x00\x24\x55", 4)) != -1) {
		buf_skip(&inbuf, i + 4);

		if (buf_cmp(&inbuf, "\x7f\xff", 2) == 0)
			encrypt = 1;
		else if (buf_cmp(&inbuf, "\xff\xff", 2) == 0)
			encrypt = 0;
		else continue;

		buf_skip(&inbuf, 4);
		
		/* LPPPg? */
		if (buf_get(&inbuf, &i, sizeof(i)) < 0)
			break;

		i = ntohl(i);
		if (buf_skip(&inbuf, i + 4 + 4) < 0)
			continue;

		/* Server. */
		if (buf_get(&inbuf, &c, 1) != 1) break;
		if (buf_len(&inbuf) < c) break;
		
		buf_put(&outbuf, buf_ptr(&inbuf), c);
		buf_put(&outbuf, "\n", 1);
		buf_skip(&inbuf, c + 4);
		
		/* Username. */
		if (buf_get(&inbuf, &c, 1) != 1) break;
		if (buf_len(&inbuf) < c) break;
		
		buf_put(&outbuf, buf_ptr(&inbuf), c);
		buf_put(&outbuf, "\n", 1);
		buf_skip(&inbuf, c + 4);
	
		/* Password. */
		if (buf_get(&inbuf, &c, 1) != 1) break;
		if (buf_len(&inbuf) < c) break;

		p = buf_ptr(&inbuf);
		
		if (encrypt) {
			for (i = 0; i < c; i++)
				p[i] ^= mm_xor[i % (sizeof(MM_SECRET) - 1)];
		}
		buf_put(&outbuf, p, c);
		buf_put(&outbuf, "\n", 1);
	}
	buf_end(&outbuf);
		
	return (buf_len(&outbuf));
}
Exemple #29
0
static void
write_hrm_data(BUF *files, const char* directory, int format)
{
	const char *suffix;
	workout_t *w;
	FILE* f;
	BUF *buf;
	time_t ft;
	int	offset;
	int count;

	suffix = format_to_str(format);

	buf = buf_alloc(0);
	offset = 0;
	count = 0;
	while (files_split(files, &offset, buf)) {
		char tmbuf[128];
		char fnbuf[BUFSIZ];

		count++;
		ft = files_timestamp(buf, 0);
		strftime(tmbuf, sizeof(tmbuf), "%Y%m%dT%H%M%S", localtime(&ft));
		snprintf(fnbuf, sizeof(fnbuf), "%s/%s.%s", directory, tmbuf, suffix);

		if (format == FORMAT_SRD) {
			f = fopen(fnbuf, "w");
			if (f) {
				log_writeln("File %02d: Saved as %s", count, fnbuf);
				fwrite(buf_get(buf), buf_len(buf), 1, f);
				fclose(f);
			} else {
				log_writeln("File %02d: Unable to save %s: %s",
							count, fnbuf, strerror(errno));
			}
		} else {
			w = workout_read_buf(buf);
			if (w) {
				f = fopen(fnbuf, "w");
				if (f) {
					log_writeln("File %02d: Saved as %s", count, fnbuf);
					if (format == FORMAT_HRM) {
						workout_print_hrm(w, f);
					} else if (format == FORMAT_TCX) {
						workout_print_tcx(w, f);
					} else if (format == FORMAT_TXT) {
						workout_print_txt(w, f, S725_WORKOUT_FULL);
					}
					fclose(f);
				} else {
					log_writeln("File %02d: Unable to save %s: %s",
								count, fnbuf, strerror(errno));
				}
				workout_free(w);
			} else {
				log_writeln("Failed to parse workout for %s", fnbuf);
			}
		}
	}
	buf_free(buf);
}
Exemple #30
0
int v2body(BUFFER *body)
{
  int i, n;
  BUFFER *to, *newsgroups;
  BUFFER *temp, *out;
  BUFFER *line;
  int type = MSG_MAIL;
  int subject = 0;

  line = buf_new();
  to = buf_new();
  newsgroups = buf_new();
  temp = buf_new();
  out = buf_new();

  n = buf_getc(body);
  for (i = 0; i < n; i++) {
    buf_get(body, line, 80);
    buf_chop(line);
    if (bufileft(line, "null:"))
      goto end;
    if (bufileft(line, "post:")) {
      type = MSG_POST;
      if (line->length > 5) {
	int j = 5;

	while (j < line->length && isspace(line->data[j]))
	  j++;
	if (newsgroups->length > 0)
	  buf_appends(newsgroups, ",");
	buf_append(newsgroups, line->data + j, line->length - j);
      }
    } else {
      if (to->length > 0)
	buf_appends(to, ",");
      buf_cat(to, line);
    }
  }
  if (to->length > 0) {
    buf_appends(out, "To: ");
    buf_cat(out, to);
    buf_nl(out);
  }
  if (newsgroups->length > 0) {
    buf_appends(out, "Newsgroups: ");
    buf_cat(out, newsgroups);
    buf_nl(out);
  }
  n = buf_getc(body);
  for (i = 0; i < n; i++) {
    buf_get(body, line, 80);
    buf_chop(line);
    if (bufileft(line, "Subject:"))
      subject = 1;
    buf_cat(out, line);
    buf_nl(out);
  }

  buf_rest(temp, body);
  buf_uncompress(temp);
  buf_set(body, temp);
  buf_reset(temp);

  if (buf_lookahead(body, line) == 0 && isline(line, HASHMARK)) {
    buf_getline(body, line);
    while (buf_getline(body, line) == 0) {
      if (bufileft(line, "subject:"))
	subject = 1;
      buf_cat(out, line);
      buf_nl(out);
    }
  }
  if (type == MSG_POST && !subject)
    buf_appends(out, "Subject: (no subject)\n");

  buf_nl(out);
  buf_rest(out, body);
  buf_reset(body);
  mix_pool(out, type, -1);

end:
  buf_free(line);
  buf_free(to);
  buf_free(newsgroups);
  buf_free(temp);
  buf_free(out);
  return (0);
}