size_t
Encoder::appendVarNumber(uint64_t varNumber)
{
  if (varNumber < 253) {
    appendByte(static_cast<uint8_t>(varNumber));
    return 1;
  }
  else if (varNumber <= std::numeric_limits<uint16_t>::max()) {
    appendByte(253);
    uint16_t value = htobe16(static_cast<uint16_t>(varNumber));
    appendByteArray(reinterpret_cast<const uint8_t*>(&value), 2);
    return 3;
  }
  else if (varNumber <= std::numeric_limits<uint32_t>::max()) {
    appendByte(254);
    uint32_t value = htobe32(static_cast<uint32_t>(varNumber));
    appendByteArray(reinterpret_cast<const uint8_t*>(&value), 4);
    return 5;
  }
  else {
    appendByte(255);
    uint64_t value = htobe64(varNumber);
    appendByteArray(reinterpret_cast<const uint8_t*>(&value), 8);
    return 9;
  }
}
Exemple #2
0
void ByteArray::alignSize(int alignment)
{
	if (alignment <= 0) return;

	while (size_ % alignment)
	{
		appendByte(0);
	}
}
boolean Lakrits::processIO() {
 if(!processOBuffer()) {  
   if(printer->available() > 0) {
     byte b = (byte) printer->read();
     appendByte(b); 
   }else{
     if(!processIBuffer()) {
	  return false;
     } 
   }
 }

 return true;
}
Exemple #4
0
HttpMessage *httpc_parser_parse(HttpMessage *saved, 
		const char *data, int len, int *used)
{
	ParserData *pd;
	HttpMessage *msg = saved;
	const char *p = data;

	*used = 0;

	if (!msg) {
		msg = httpc_Message_create();
		msg->pri_data = malloc(sizeof(ParserData));
		pd = (ParserData *)msg->pri_data;
		pd->linedata = 0;
		pd->line_bufsize = 0;
		pd->line_datasize = 0;
		pd->state = HTTP_STARTLINE;
	}
	else {
		pd = (ParserData *)msg->pri_data;
	}

	while (pd->state != HTTP_COMPLETE && len > 0) {
		if (pd->state == HTTP_BODY) {
			const char *v = 0;
			int rc;
			int bodylen, expect;

			httpc_Message_getValue(msg, "Content-Length", &v);
			bodylen = atoi(v);
			expect = bodylen - msg->bodylen;
			rc = appendBody(p, len > expect ? expect : len, msg);
			p += rc;
			len -= rc;
			*used += rc;
		}
		else {
			int rc = appendByte(*p, msg);
			p += rc;
			len -= rc;
			*used += rc;
		}
	}

	return msg;
}
size_t
Encoder::appendNonNegativeInteger(uint64_t varNumber)
{
  if (varNumber <= std::numeric_limits<uint8_t>::max()) {
    return appendByte(static_cast<uint8_t>(varNumber));
  }
  else if (varNumber <= std::numeric_limits<uint16_t>::max()) {
    uint16_t value = htobe16(static_cast<uint16_t>(varNumber));
    return appendByteArray(reinterpret_cast<const uint8_t*>(&value), 2);
  }
  else if (varNumber <= std::numeric_limits<uint32_t>::max()) {
    uint32_t value = htobe32(static_cast<uint32_t>(varNumber));
    return appendByteArray(reinterpret_cast<const uint8_t*>(&value), 4);
  }
  else {
    uint64_t value = htobe64(varNumber);
    return appendByteArray(reinterpret_cast<const uint8_t*>(&value), 8);
  }
}