byte_buffer *rl_compress(char *data, long size) {
    byte_buffer *buff = new_buffer((3 * size)/4);
    long i;
    int k;

    // 1. Determine the longest sequence of 1s or 0s.
    int longest_seq = 1, current_seq = 0;
    uchar current = is_bit_set(data[0], 0);
    for (i = 0; i < size; i++) {
        for (k = 0; k < 8; ++k) {
            if (is_bit_set(data[i], k) == current) {
                current_seq++;
            } else {
                if (current_seq > longest_seq)
                    longest_seq = current_seq;
                current_seq = 1;
                current = (current == 0)? 1 : 0;
            }
        }
    }

    // 2. Determine number of bits required to represent that sequence.
    const ushort NUM_BITS = (const ushort)ceil(log((double)longest_seq)/log(2.0));

    // 3. First 16 bits used to store length of the run lengths in bits.
    uchar *byte_representation = (uchar *)(&NUM_BITS);
    for (i = 0; i < sizeof(NUM_BITS); ++i) {
        append_byte_to_buffer(buff, byte_representation[i]);
    }

    // 4. Start with 0s, write length, then write 1s length, etc.
    if (is_bit_set(data[0], 0)) {
        write_length(buff, 0, NUM_BITS);
    }

    current_seq = 0;
    current = is_bit_set(data[0], 0);
    for (i = 0; i < size; i++) {
        for (k = 0; k < 8; ++k) {
            if (is_bit_set(data[i], k) == current) {
                current_seq++;
            } else {
                write_length(buff, current_seq, NUM_BITS);
                current_seq = 1;
                current = is_bit_set(data[i], k);
            }
        }
    }

    if (current_seq > 0) {
        write_length(buff, current_seq, NUM_BITS);
    }

    return buff;
}
/*
	1 integer id
	2 string collection
	3 integer number
	4 cursor_id (8 bytes string/ 64bit)

	return string package
 */
static int
op_get_more(lua_State *L) {
	int id = luaL_checkinteger(L, 1);
	size_t sz = 0;
	const char * name = luaL_checklstring(L,2,&sz);
	int number = luaL_checkinteger(L, 3);
	size_t cursor_len = 0;
	const char * cursor_id = luaL_tolstring(L, 4, &cursor_len);
	if (cursor_len != 8) {
		return luaL_error(L, "Invalid cursor id");
	}

	struct buffer buf;
	buffer_create(&buf);
		int len = reserve_length(&buf);
		write_int32(&buf, id);
		write_int32(&buf, 0);
		write_int32(&buf, OP_GET_MORE);
		write_int32(&buf, 0);
		write_string(&buf, name, sz);
		write_int32(&buf, number);
		write_bytes(&buf, cursor_id, 8);
		write_length(&buf, buf.size, len);

		lua_pushlstring(L, (const char *)buf.ptr, buf.size);
	buffer_destroy(&buf);

	return 1;
}
/*
	1 string collection
	2 integer single remove
	3 document selector

	return string package
 */
static int
op_delete(lua_State *L) {
	document selector  = lua_touserdata(L,3);
	if (selector == NULL) {
		luaL_error(L, "Invalid param");
	}
	size_t sz = 0;
	const char * name = luaL_checklstring(L,1,&sz);

	luaL_Buffer b;
	luaL_buffinit(L,&b);

	struct buffer buf;
	buffer_create(&buf);
		int len = reserve_length(&buf);
		write_int32(&buf, 0);
		write_int32(&buf, 0);
		write_int32(&buf, OP_DELETE);
		write_int32(&buf, 0);
		write_string(&buf, name, sz);
		write_int32(&buf, lua_tointeger(L,2));

		int32_t selector_len = get_length(selector);
		int total = buf.size + selector_len;
		write_length(&buf, total, len);

		luaL_addlstring(&b, (const char *)buf.ptr, buf.size);
	buffer_destroy(&buf);

	luaL_addlstring(&b, (const char *)selector, selector_len);
	luaL_pushresult(&b);

	return 1;
}
/*
	1 string cursor_id
	return string package
 */
static int
op_kill(lua_State *L) {
	size_t cursor_len = 0;
	const char * cursor_id = luaL_tolstring(L, 1, &cursor_len);
	if (cursor_len != 8) {
		return luaL_error(L, "Invalid cursor id");
	}

	struct buffer buf;
	buffer_create(&buf);

	int len = reserve_length(&buf);
	write_int32(&buf, 0);
	write_int32(&buf, 0);
	write_int32(&buf, OP_KILL_CURSORS);

	write_int32(&buf, 0);
	write_int32(&buf, 1);
	write_bytes(&buf, cursor_id, 8);

	write_length(&buf, buf.size, len);

	lua_pushlstring(L, (const char *)buf.ptr, buf.size);
	buffer_destroy(&buf);

	return 1;
}
Exemple #5
0
            static OutIt encode(codepoint_type c, OutIt dest) {

                size_t len = write_length(c);

                unsigned char res[4] = {};

                // loop to catch remaining
                for (size_t i = len; i != 1; --i) {
                    // select lower 6 bits
                    res[i-1] = (c & 0x3f) | 0x80;
                    c = c >> 6;
                }

                // switch on first byte
                switch (len) {
                    case 1: res[0] = c; break;
                    case 2: res[0] = c | 0xc0; break;
                    case 3: res[0] = c | 0xe0; break;
                    case 4: res[0] = c | 0xf0; break;
                    default:
                        assert(false && "bad utf8 codeunit");
                };

                for (size_t i = 0; i < len; ++i) {
                    *dest = res[i];
                    ++dest;
                }

                return dest;
            }
static int
op_query(lua_State *L) {
	int id = luaL_checkinteger(L,1);
	document query = lua_touserdata(L,6);
	if (query == NULL) {
		return luaL_error(L, "require query document");
	}
	document selector = lua_touserdata(L,7);
	int flags = luaL_checkinteger(L, 2);
	size_t nsz = 0;
	const char *name = luaL_checklstring(L,3,&nsz);
	int skip = luaL_checkinteger(L, 4);
	int number = luaL_checkinteger(L, 5);

	luaL_Buffer b;
	luaL_buffinit(L,&b);

	struct buffer buf;
	buffer_create(&buf);
		int len = reserve_length(&buf);
		write_int32(&buf, id);
		write_int32(&buf, 0);
		write_int32(&buf, OP_QUERY);
		write_int32(&buf, flags);
		write_string(&buf, name, nsz);
		write_int32(&buf, skip);
		write_int32(&buf, number);

		int32_t query_len = get_length(query);
		int total = buf.size + query_len;
		int32_t selector_len = 0;
		if (selector) {
			selector_len = get_length(selector);
			total += selector_len;
		}

		write_length(&buf, total, len);
		luaL_addlstring(&b, (const char *)buf.ptr, buf.size);
	buffer_destroy(&buf);

	luaL_addlstring(&b, (const char *)query, query_len);

	if (selector) {
		luaL_addlstring(&b, (const char *)selector, selector_len);
	}

	luaL_pushresult(&b);

	return 1;
}
Exemple #7
0
int write_socket(int fd, const void *buf, size_t nbyte)
{
	int ret;

	ret = wait_write_select(fd, 5);
	if (ret < 1) {
		if (!ret)
			LOGNOTICE("Select timed out in write_socket");
		else
			LOGNOTICE("Select failed in write_socket");
		goto out;
	}
	ret = write_length(fd, buf, nbyte);
	if (ret < 0)
		LOGNOTICE("Failed to write in write_socket");
out:
	return ret;
}
// 1 integer flags
// 2 string collection
// 3 documents
// return string package
static int
op_insert(lua_State *L) {
	size_t sz = 0;
	const char * name = luaL_checklstring(L,2,&sz);
	int dsz = document_length(L);

	luaL_Buffer b;
	luaL_buffinit(L, &b);

	struct buffer buf;
	buffer_create(&buf);
		// make package header, don't raise L error
		int len = reserve_length(&buf);
		write_int32(&buf, 0);
		write_int32(&buf, 0);
		write_int32(&buf, OP_INSERT);
		write_int32(&buf, lua_tointeger(L,1));
		write_string(&buf, name, sz);

		int total = buf.size + dsz;
		write_length(&buf, total, len);

		luaL_addlstring(&b, (const char *)buf.ptr, buf.size);
	buffer_destroy(&buf);
	
	if (lua_isuserdata(L,3)) {
		document doc = lua_touserdata(L,3);
		luaL_addlstring(&b, (const char *)doc, get_length(doc));
	} else {
		int s = lua_rawlen(L, 3);
		int i;
		for (i=1;i<=s;i++) {
			lua_rawgeti(L,3,i);
			document doc = lua_touserdata(L,3);
			luaL_addlstring(&b, (const char *)doc, get_length(doc));
			lua_pop(L,1);
		}
	}

	luaL_pushresult(&b);

	return 1;
}
Exemple #9
0
// output compressed data to file name
void write_file( ofstream *p_outfile, unsigned char *data, int length, match_result *match_results  )
{
int position = 0;
int offset;

	bitcount = bitdata = 0;

	outfile = p_outfile;

	// set bit / buffer position
	buffer_position = 1;
	bit_position = 0;

	// loop while there's more compressed data to process
	while ( position < length )
	{
		// if it's a match
		if ( match_results[ position ].first > 1 )
		{        
			// write match marker
			write_bit( 1 );
			
			// write length of offset
			write_length( match_results[ position ].first - 2 );
			
			// calculate offset
			offset = position - match_results[ position ].second - 1;
                        
			// if long offset
			if ( offset > 127 )
			{
				// write lowest 7 bits of offset, plus long offset marker
				write_byte( offset | 128 ); 
				
				// write remaing offset bits
				write_bit( offset & 1024 );
				write_bit( offset & 512 );
				write_bit( offset & 256 );
				write_bit( offset & 128 );
			}
			else
			{
				// write short offset
				write_byte( offset );
			}

			// updated current position in compressed data
			position += match_results[ position ].first;
		}
		else
		{
			// write literal byte marker
			write_bit( 0 );
			
			// write literal byte
			write_byte( data[ position++ ] );
		}
	}

	
	// write RLE marker
	write_bit( 1 );		
	
	// write eof marker
	write_bit( 1 ); write_bit( 1 ); write_bit( 1 ); write_bit( 1 );   
	write_bit( 1 ); write_bit( 1 ); write_bit( 1 ); write_bit( 1 );  
	write_bit( 1 ); write_bit( 1 ); write_bit( 1 ); write_bit( 1 );   
	write_bit( 1 ); write_bit( 1 ); write_bit( 1 ); write_bit( 1 );  
	write_bit( 1 ); write_bit( 1 ); write_bit( 1 ); write_bit( 1 );   
	write_bit( 1 ); write_bit( 1 ); write_bit( 1 ); write_bit( 1 );  
	write_bit( 1 ); write_bit( 1 ); write_bit( 1 ); write_bit( 1 );   
	write_bit( 1 ); write_bit( 1 ); write_bit( 1 ); write_bit( 1 );    
			
	// write all remaining data
	flush_rest();
}
Exemple #10
0
void main(void) {
    unsigned char cal_length, cal_dia;
    unsigned int length, dia, dia_offset, latest_l, latest_d;
    unsigned int tot_acc, mass_acc, timb_acc;
    unsigned int tot_num, mass_num, timb_num;
    unsigned int count1, count2, count3;
    OSCCON = 255;
    OSCTUNEbits.PLLEN = 1;

    TRISA = 0b00011111;
    PORTA = 0;
    ADCON1 = 0b00001101;
    TRISB = 0;
    PORTB = 0;
    TRISC = 0b00010000;
    PORTC = 0b00000000;
    TRISD = 0;
    PORTD = 0;
    TRISE = 0;
    PORTE = 0;

    init_display();
    init_counters();

    LED1 = 0;
    LED2 = 0;
    LED3 = 0;

    latest_l = 0;
    latest_d = 0;
    tot_acc = 0;
    tot_num = 0;
    timb_acc = 0;
    timb_num = 0;
    mass_acc = 0;
    mass_num = 0;

    cal_length = read_ad(0);
    cal_dia = read_ad(1);

    dia_offset = 2;
    
    while (1) {
        length = read_length(cal_length);
        dia = dia_offset + read_diameter(cal_dia);

        write_length(length);
        write_dia(dia);

        if (!CALIBRATE) {
            count1 = read_counter(1);
            count2 = read_counter(2);
            count3 = read_counter(3);
            cal_length = read_ad(0);
            cal_dia = read_ad(1);
            write_debug_info(cal_length, cal_dia, count1, count2, count3);
        }

        if (!RESET_DIA) {
            reset_dia();
        }
        
        if (!RESET_LENGTH) {
            delay(20000);
            delay(20000);
            if (!RESET_LENGTH) {
                LED1 = 1;
                if (length >= 100) {
                    tot_num = tot_num + 1;
                    tot_acc = tot_acc + length / 100;
                    latest_l = length;
                    latest_d = dia;
                    if (dia >= 15) {
                        timb_num = timb_num + 1;
                        timb_acc = timb_acc + length / 100;
                    } else {
                        mass_num = mass_num + 1;
                        mass_acc = mass_acc + length / 100;
                    }
                }
                reset_length();
                while (!RESET_LENGTH) {
                    write_length(0);
                    write_dia(0);
                    write_length(8);
                    write_dia(8);
                }
                write_stats(latest_l, latest_d, tot_num, tot_acc, timb_num, timb_acc, mass_num, mass_acc);
                LED1 = 0;
            }
        }
    }
}