Exemplo n.º 1
0
std::string Message::encode(){
	std::string buffer;
	std::map<int, std::string>::const_iterator it;
	for(it=fields.begin(); it!=fields.end(); it++){
		int tag = it->first;
		if(tag == 8 || tag == 9 || tag == 10){
			continue;
		}
		const std::string &val = it->second;
		buffer.append(encode_field(tag, val));
	}
	body_len_ = (int)buffer.size();

	std::string header;
	header.append(encode_field(8, this->version()));
	header.append(encode_field(9, str(body_len_)));
	
	checksum_ = 0;
	for(int i=0; i<(int)header.size(); i++){
		unsigned char byte = (unsigned char)header[i];
		checksum_ += byte;
	}
	for(int i=0; i<(int)buffer.size(); i++){
		unsigned char byte = (unsigned char)buffer[i];
		checksum_ += byte;
	}
	checksum_ %= 256;
	
	char buf[4];
	snprintf(buf, sizeof(buf), "%03d", checksum_);
	buffer.append(encode_field(10, buf));
	
	return header + buffer;
}
Exemplo n.º 2
0
std::string Message::encode() const{
	std::string buffer;
	buffer.append(encode_field(0, this->type()));
	std::map<int, std::string>::const_iterator it;
	for(it=fields_.begin(); it!=fields_.end(); it++){
		int tag = it->first;
		if(tag == 0){
			continue;
		}
		const std::string &val = it->second;
		buffer.append(encode_field(tag, val));
	}
	buffer[buffer.size()-1] = sim::MSG_END_BYTE;
	return buffer;
}
Exemplo n.º 3
0
int add_field(char *buf, int buf_len, const char *field_name, const char *val)
{
    int dummy_idx = 0;
    int i = 0;
    int rv;

    /* skip to the end, ignoring crc (don't want to further corrupt partially
     * corrupt data) */
    while(i < buf_len) {
        uint8_t field_len = buf[i];

        if(field_len == 0xff)
            break;

        /* skip past `field_len' bytes, 2 byte CRC field, and 1 byte len
         * field */
        i += field_len + 3;
    }


    rv = encode_field(buf + i, buf_len - i, &dummy_idx, field_name, val);
    if(rv < 0)
        return rv;

    return 0;
}
Exemplo n.º 4
0
/* Default handler for extension fields. Expects to have a pb_field_t
 * pointer in the extension->type->arg field. */
static bool checkreturn default_extension_encoder(pb_ostream_t *stream,
    const pb_extension_t *extension)
{
    const pb_field_t *field = (const pb_field_t*)extension->type->arg;
    
    if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
    {
        /* For pointer extensions, the pointer is stored directly
         * in the extension structure. This avoids having an extra
         * indirection. */
        return encode_field(stream, field, &extension->dest);
    }
    else
    {
        return encode_field(stream, field, extension->dest);
    }
}
Exemplo n.º 5
0
std::string encode_text(str_ref v, bool add_quote_class)
{
	std::string r;
	r.reserve(v.size() << 1);
	while (v)
	{
		str_ref line = read_until(v, '\n');
		r += add_quote_class && boost::istarts_with(line, "> ") ? "<span class=quote>" + encode_field(line) + "</span>" : encode_field(line);
		r += "<br>";
	}
	return r;
}
Exemplo n.º 6
0
static inline void fill_fpga_metadata_page(uint8_t *metadata,
                                           size_t actual_bitstream_len)
{
    char len_str[10];
    int idx = 0;

    memset(len_str, 0, sizeof(len_str));
    memset(metadata, 0xff, BLADERF_FLASH_PAGE_SIZE);

    snprintf(len_str, sizeof(len_str), "%u",
             (unsigned int)actual_bitstream_len);

    encode_field((char *)metadata, BLADERF_FLASH_PAGE_SIZE,
                 &idx, "LEN", len_str);
}
Exemplo n.º 7
0
std::string encode_text(const std::string& v, const std::string& local_domain_url, bool add_span)
{
	std::string r;
	r.reserve(v.length() << 1);
	for (size_t i = 0; i < v.length(); )
	{
		size_t p = v.find('\n', i);
		if (p == std::string::npos)
			p = v.length();
		std::string line = v.substr(i, p - i);
		line = encode_field(line, local_domain_url);
		r += add_span && boost::istarts_with(line, "> ") ? "<span class=quote>" + line + "</span>" : line;
		r += "<br>";
		i = p + 1;
	}
	return r;
}
Exemplo n.º 8
0
bool checkreturn pb_encode(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct)
{
    pb_field_iter_t iter;
    if (!pb_field_iter_begin(&iter, fields, remove_const(src_struct)))
        return true; /* Empty message type */
    
    do {
        if (PB_LTYPE(iter.pos->type) == PB_LTYPE_EXTENSION)
        {
            /* Special case for the extension field placeholder */
            if (!encode_extension_field(stream, iter.pos, iter.pData))
                return false;
        }
        else
        {
            /* Regular field */
            if (!encode_field(stream, iter.pos, iter.pData))
                return false;
        }
    } while (pb_field_iter_next(&iter));
    
    return true;
}
bool checkreturn pb_encode(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct)
{
    const pb_field_t *field = fields;
    const void *pData = src_struct;
    size_t prev_size = 0;
    
    while (field->tag != 0)
    {
        pData = (const char*)pData + prev_size + field->data_offset;
        prev_size = field->data_size;
        
        /* Special case for static arrays */
        if (PB_ATYPE(field->type) == PB_ATYPE_STATIC &&
            PB_HTYPE(field->type) == PB_HTYPE_REPEATED)
        {
            prev_size *= field->array_size;
        }
        
        if (PB_LTYPE(field->type) == PB_LTYPE_EXTENSION)
        {
            /* Special case for the extension field placeholder */
            if (!encode_extension_field(stream, field, pData))
                return false;
        }
        else
        {
            /* Regular field */
            if (!encode_field(stream, field, pData))
                return false;
        }
    
        field++;
    }
    
    return true;
}
Exemplo n.º 10
0
string bbformat(str_ref s)
{
	string d;
	str_ref a0;
	while (1)
	{
		switch (get_next(s, a0))
		{
		case bb_literal:
			d += encode_field(a0, true);
			break;
		case bb_none:
			break;
		case bb_bold:
			d += "<b>";
			break;
		case bb_bold_close:
			d += "</b>";
			break;
		case bb_center:
			d += "<center>";
			break;
		case bb_center_close:
			d += "</center>";
			break;
		case bb_color:
			d += "<font color=\"" + encode_field(a0) + "\">"; // escape a0
			break;
		case bb_color_close:
			d += "</font>";
			break;
		case bb_quote:
			if (a0)
				d += "<b>" + encode_field(a0) + " wrote:</b>";
			d += "<blockquote class=bq>";
			break;
		case bb_quote_close:
			d += "</blockquote>";
			break;
		case bb_strike:
			d += "<s>";
			break;
		case bb_strike_close:
			d += "</s>";
			break;
		case bb_underline:
			d += "<u>";
			break;
		case bb_underline_close:
			d += "</u>";
			break;
		case bb_url:
			d += encode_field(a0, true) + " ";
			break;
		case bb_video:
			d += encode_field(a0, true) + " ";
			break;
		case bb_unknown:
			d += "[" + encode_field(a0) + "]";
			break;
		case bb_end:
			return d;
		default:
			assert(false);
		}
	}
}
Exemplo n.º 11
0
bool perf_object_t::encode(xml_document_t& doc, xml_element_t& xml_val) const
{
    do
    {
        if (!__skip_bool_val && !encode_field(bool_val, "bool_val", doc, xml_val)) break;
        if (!__skip_int8_val && !encode_field(int8_val, "int8_val", doc, xml_val)) break;
        if (!__skip_uint8_val && !encode_field(uint8_val, "uint8_val", doc, xml_val)) break;
        if (!__skip_int16_val && !encode_field(int16_val, "int16_val", doc, xml_val)) break;
        if (!__skip_uint16_val && !encode_field(uint16_val, "uint16_val", doc, xml_val)) break;
        if (!__skip_int32_val && !encode_field(int32_val, "int32_val", doc, xml_val)) break;
        if (!__skip_uint32_val && !encode_field(uint32_val, "uint32_val", doc, xml_val)) break;
        if (!__skip_int64_val && !encode_field(int64_val, "int64_val", doc, xml_val)) break;
        if (!__skip_uint64_val && !encode_field(uint64_val, "uint64_val", doc, xml_val)) break;
        if (!__skip_float_val && !encode_field(float_val, "float_val", doc, xml_val)) break;
        if (!__skip_double_val && !encode_field(double_val, "double_val", doc, xml_val)) break;
        if (!__skip_str_val && !encode_field(str_val, "str_val", doc, xml_val)) break;
        if (!__skip_vec_val && !encode_field(vec_val, "vec_val", doc, xml_val)) break;
        if (!__skip_dict_val && !encode_field(dict_val, "dict_val", doc, xml_val)) break;

        return true;
    } while (0);

    return false;
}
Exemplo n.º 12
0
bool sample_struct_t::encode(xml_document_t& doc, xml_element_t& xml_val) const
{
    do
    {
        if (!__skip_bool_val && !encode_field(bool_val, "bool_val", doc, xml_val)) break;
        if (!__skip_str_val && !encode_field(str_val, "str_val", doc, xml_val)) break;
        if (!__skip_int_val && !encode_field(int_val, "int_val", doc, xml_val)) break;
        if (!__skip_uint_val && !encode_field(uint_val, "uint_val", doc, xml_val)) break;
        if (!__skip_double_val && !encode_field(double_val, "double_val", doc, xml_val)) break;
        if (!__skip_char_val && !encode_field(char_val, "char_val", doc, xml_val)) break;
        if (!__skip_uchar_val && !encode_field(uchar_val, "uchar_val", doc, xml_val)) break;
        if (!__skip_short_val && !encode_field(short_val, "short_val", doc, xml_val)) break;
        if (!__skip_ushort_val && !encode_field(ushort_val, "ushort_val", doc, xml_val)) break;
        if (!__skip_int64_val && !encode_field(int64_val, "int64_val", doc, xml_val)) break;
        if (!__skip_uint64_val && !encode_field(uint64_val, "uint64_val", doc, xml_val)) break;
        if (!__skip_float_val && !encode_field(float_val, "float_val", doc, xml_val)) break;
        if (!__skip_vec_val && !encode_field(vec_val, "vec_val", doc, xml_val)) break;
        if (!__skip_str_map_val && !encode_field(str_map_val, "str_map_val", doc, xml_val)) break;

        return true;
    } while (0);

    return false;
}
Exemplo n.º 13
0
/* Default handler for extension fields. Expects to have a pb_field_t
 * pointer in the extension->type->arg field. */
static bool checkreturn default_extension_encoder(pb_ostream_t *stream,
    const pb_extension_t *extension)
{
    const pb_field_t *field = (const pb_field_t*)extension->type->arg;
    return encode_field(stream, field, extension->dest);
}
Exemplo n.º 14
0
int bladerf_flash_fpga(struct bladerf *dev, const char *fpga_file)
{
    int status;
    char fpga_len[10];
    uint8_t *buf, *buf_padded, *ver;
    size_t buf_size, buf_size_padded;
    int hp_idx = 0;

    if (strcmp("X", fpga_file) == 0) {
        printf("Disabling FPGA flash auto-load\n");
        return (dev->fn->erase_flash(dev, flash_from_sectors(4), BLADERF_FLASH_SECTOR_SIZE) != BLADERF_FLASH_SECTOR_SIZE);
    }

    status = file_read_buffer(fpga_file, &buf, &buf_size);
    if (status == 0) {
        if ((getenv("BLADERF_SKIP_FPGA_SIZE_CHECK") == 0)  &&
                (buf_size < (1 * 1024 * 1024) || (buf_size > (5 * 1024 * 1024)))) {
            log_info("Detected potentially invalid firmware file.\n");
            log_info("Define BLADERF_SKIP_FPGA_SIZE_CHECK in your evironment "
                       "to skip this check.\n");
            status = BLADERF_ERR_INVAL;
        } else {
            const size_t page_size = BLADERF_FLASH_SECTOR_SIZE;
            size_t buf_size_padding = page_size - (buf_size % page_size);

            /* Pad firmare data out to a flash page size */
            buf_size_padded = buf_size + buf_size_padding + page_size;
            buf_padded = (uint8_t*)realloc(buf, buf_size_padded);
            if (buf_padded == NULL) {
                status = BLADERF_ERR_MEM;
            } else {
                buf = buf_padded;
                memset(buf + buf_size, 0xFF, buf_size_padded - buf_size - BLADERF_FLASH_SECTOR_SIZE);
                memmove(&buf[BLADERF_FLASH_PAGE_SIZE], buf, buf_size_padded - BLADERF_FLASH_SECTOR_SIZE);
                snprintf(fpga_len, 9, "%d", (int)buf_size);
                memset(buf, 0xff, BLADERF_FLASH_PAGE_SIZE);
                encode_field((char *)buf, BLADERF_FLASH_PAGE_SIZE, &hp_idx, "LEN", fpga_len);

                if (status == 0) {
                    status = dev->fn->erase_flash(dev, flash_from_sectors(4), (uint32_t)buf_size_padded);
                }

                if (status >= 0) {
                    status = dev->fn->write_flash(dev, flash_from_pages(1024), buf, (uint32_t)buf_size_padded);
                }

                ver = (uint8_t *)malloc(buf_size_padded);
                if (!ver)
                    status = BLADERF_ERR_MEM;

                if (status >= 0) {
                    status = dev->fn->read_flash(dev, flash_from_pages(1024), ver, (uint32_t)buf_size_padded);
                }

                if ((size_t)status == buf_size_padded) {
                    status = memcmp(buf, ver, buf_size_padded);
                }

                free(ver);
            }
        }
        free(buf);
    }

    return status;
}