コード例 #1
0
ファイル: block.cpp プロジェクト: PatrickGuo/ndn-cxx-master
Block
Block::fromStream(std::istream& is)
{
  std::istream_iterator<uint8_t> tmp_begin(is);
  std::istream_iterator<uint8_t> tmp_end;

  uint32_t type = Tlv::readType(tmp_begin, tmp_end);
  uint64_t length = Tlv::readVarNumber(tmp_begin, tmp_end);

  if (length > MAX_SIZE_OF_BLOCK_FROM_STREAM)
    throw Tlv::Error("Length of block from stream is too large");

  // We may still have some problem here, if some exception happens,
  // we may completely lose all the bytes extracted from the stream.

  char buf[MAX_SIZE_OF_BLOCK_FROM_STREAM];
  buf[0] = *tmp_begin;
  is.read(buf+1, length-1);

  if (length != static_cast<uint64_t>(is.gcount()) + 1) {
    throw Tlv::Error("Not enough data in the buffer to fully parse TLV");
  }

  return dataBlock(type, buf, length);
}
コード例 #2
0
bool GifDecoder::loadFromMemory(const uint8_t* data, uint32_t size)
{
	DataBlock dataBlock(data, size);
	if (!readHeader(&dataBlock)) {
		return false;
	}
	return readContents(&dataBlock);
}
コード例 #3
0
ファイル: data.cpp プロジェクト: alvyC/ndn-cxx
Data&
Data::setContent(const uint8_t* content, size_t contentLength)
{
  onChanged();

  m_content = dataBlock(tlv::Content, content, contentLength);

  return *this;
}
コード例 #4
0
ファイル: key-locator.cpp プロジェクト: alvyC/ndn-cxx
KeyLocator&
KeyLocator::setKeyDigest(const ConstBufferPtr& keyDigest)
{
  // WARNING: ConstBufferPtr is shared_ptr<const Buffer>
  // This function takes a constant reference of a shared pointer.
  // It MUST NOT change the reference count of that shared pointer.

  return this->setKeyDigest(dataBlock(tlv::KeyDigest, keyDigest->get(), keyDigest->size()));
}
コード例 #5
0
ファイル: data.cpp プロジェクト: alvyC/ndn-cxx
const Block&
Data::getContent() const
{
  if (m_content.empty())
    m_content = dataBlock(tlv::Content, reinterpret_cast<const uint8_t*>(0), 0);

  if (!m_content.hasWire())
      m_content.encode();
  return m_content;
}
コード例 #6
0
ファイル: view.c プロジェクト: brandondyck/plan9port
Tnode*
initxroot(char *name, uchar score[VtScoreSize])
{
    Block *b;

    b = dataBlock(score, BtDir, RootTag);
    if(b == nil)
        return stringnode("read data block %V: %r", score);
    return initxblock(b, smprint("'%s' fs root", name), xvacrootgen, nil);
}
コード例 #7
0
ファイル: view.c プロジェクト: brandondyck/plan9port
Tnode*
initxlocalroot(char *name, u32int addr)
{
    uchar score[VtScoreSize];
    Block *b;

    localToGlobal(addr, score);
    b = dataBlock(score, BtDir, RootTag);
    if(b == nil)
        return stringnode("read data block %#ux: %r", addr);
    return initxblock(b, smprint("'%s' fs root", name), xlocalrootgen, nil);
}
コード例 #8
0
ファイル: interest.cpp プロジェクト: vusirikala/ndn-cxx
Interest&
Interest::setNonce(uint32_t nonce)
{
  if (m_wire.hasWire() && m_nonce.value_size() == sizeof(uint32_t)) {
    std::memcpy(const_cast<uint8_t*>(m_nonce.value()), &nonce, sizeof(nonce));
  }
  else {
    m_nonce = dataBlock(tlv::Nonce,
                        reinterpret_cast<const uint8_t*>(&nonce),
                        sizeof(nonce));
    m_wire.reset();
  }
  return *this;
}
コード例 #9
0
    void object::test<2>()
    {
        std::ostringstream two_requests;
        two_requests << "GET /home/krasimir/Desktop/index.html HTTP/1.1\r\n"
        << "Host: 127.0.0.1\r\n"
        << "User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.12) Gecko/20050922 Fedora/1.0.7-1.1.fc4 Firefox/1.0.7\r\n"
        << "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n"
        << "Accept-Language: en-us,en;q=0.5\r\n"
        << "Accept-Encoding: gzip,deflate\r\n"
        << "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n"
        << "Keep-Alive: 300\r\n"
        << "Connection: keep-alive\r\n"
        << "\r\n"
        << "GET /home/krasimir/Desktop/index.html HTTP/1.1\r\n"
        << "Host: 127.0.0.1\r\n"
        << "User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.12) Gecko/20050922 Fedora/1.0.7-1.1.fc4 Firefox/1.0.7\r\n"
        << "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n"
        << "Accept-Language: en-us,en;q=0.5\r\n"
        << "Accept-Encoding: gzip,deflate\r\n"
        << "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n"
        << "Keep-Alive: 300\r\n"
        << "Connection: keep-alive\r\n"
        << "\r\n";

        std::string sTwoRequests = two_requests.str();
        http::CDataBlock dataBlock(sTwoRequests.data(), sTwoRequests.size());
        http::CHttpRequest* pRequest = new http::CHttpRequest();
        int iNumberOfRequests = 0;
        while(1)
        {
            int r = pRequest->parse(&dataBlock);
            if(r == 0 || r < 0)
            {
                break;
            }
            else if(r == 1) //parsed request
            {
                iNumberOfRequests++;
                delete pRequest;
                pRequest = new http::CHttpRequest();
            }
        }

        delete pRequest;

        ensure("two http requests", iNumberOfRequests == 2);
    }
コード例 #10
0
  NdnAutoconfigServer(const std::string& hubFaceUri, const PrefixCollection& routablePrefixes)
  {
    KeyChain m_keyChain;

    // pre-create hub Data
    m_hubData = make_shared<Data>(Name(LOCALHOP_HUB).appendVersion());
    m_hubData->setFreshnessPeriod(time::hours(1)); // 1 hour
    m_hubData->setContent(dataBlock(tlv::nfd::Uri,
                                    reinterpret_cast<const uint8_t*>(hubFaceUri.c_str()),
                                    hubFaceUri.size()));
    m_keyChain.sign(*m_hubData);

    // pre-create routable prefix Data
    if (!routablePrefixes.empty()) {
      Name routablePrefixesDataName(LOCALHOP_ROUTABLE_PREFIXES);
      routablePrefixesDataName.appendVersion();
      routablePrefixesDataName.appendSegment(0);
      m_routablePrefixesData = make_shared<Data>(routablePrefixesDataName);
      m_routablePrefixesData->setContent(routablePrefixes.wireEncode());
      m_routablePrefixesData->setFinalBlockId(routablePrefixesDataName.get(-1));
      m_routablePrefixesData->setFreshnessPeriod(time::seconds(5)); // 5s
      m_keyChain.sign(*m_routablePrefixesData);
    }
  }
コード例 #11
0
ファイル: view.c プロジェクト: brandondyck/plan9port
Tnode*
initxsource(Entry e, int dowrap)
{
    Block *b;
    Tnode *t, *tt;

    b = dataBlock(e.score, etype(e.flags, e.depth), e.tag);
    if(b == nil)
        return stringnode("dataBlock: %r");

    if((e.flags & VtEntryActive) == 0)
        return stringnode("inactive Entry");

    if(e.depth == 0) {
        if(e.flags & _VtEntryDir)
            tt = initxentryblock(b, copyEntry(e));
        else
            tt = initxdatablock(b, e.dsize);
    } else {
        tt = initxblock(b, smprint("%s+%d pointer", (e.flags & _VtEntryDir) ? "BtDir" : "BtData", e.depth),
                        ptrgen, copyEntry(e));
    }

    /*
     * wrap the contents of the Source in a Source node,
     * just so it's closer to what you see in the code.
     */
    if(dowrap) {
        t = stringnode("Source");
        t->nkid = 1;
        t->kid = mallocz(sizeof(Tnode*)*1, 1);
        t->kid[0] = tt;
        tt = t;
    }
    return tt;
}
コード例 #12
0
ファイル: fms.c プロジェクト: adeason/openafs
int
fileMarkSize(char *tapeDevice)
{
    afs_uint32 nFileMarks, nBlocks, nbfTape;
    double tpSize, fmSize;
    afs_uint32 bufferSize = 16384;
    usd_handle_t hTape;
    FILE *logFile;
    int count = 0;
    afs_uint32 countr;
    afs_int32 code = 0;

    code =
	usd_Open(tapeDevice, (USD_OPEN_RDWR | USD_OPEN_WLOCK), 0777, &hTape);
    if (code) {
	printf("Can't open tape device %s\n", tapeDevice);
	fflush(stdout);
	exit(1);
    }

    logFile = fopen("fms.log", "w+");
    if (logFile == NULL) {
	printf("Can't open log file\n");
	fflush(stdout);
	exit(1);
    }
    fprintf(logFile, "fms test started\n");
    fflush(logFile);

    code = rewindTape(hTape);
    if (code) {
	fprintf(logFile, "Can't rewind tape\n");
	fflush(logFile);
	ERROR(code);
    }

    /* measure capacity of tape */
    nbfTape = 0;
    countr = 0;
    while (1) {
	code = dataBlock(hTape, bufferSize);
	nbfTape++;
	count++;
	countr++;
	if (code)
	    break;

	if (count >= 5) {
	    count = 0;
	    printf("\rwrote block: %d", nbfTape);
	}

    }

    fprintf(logFile, "wrote %d blocks\n", nbfTape);
    fflush(logFile);
    printf("\rwrote %d blocks\n", nbfTape);
    printf("Finished data capacity test - rewinding\n");
    /* reset the tape device */
    code = USD_CLOSE(hTape);
    if (code) {
	fprintf(logFile, "Can't close tape device at end of pass 1\n");
	fflush(logFile);
	printf("Can't close tape device %s\n", tapeDevice);
	goto error_exit;
    }
    code =
	usd_Open(tapeDevice, (USD_OPEN_RDWR | USD_OPEN_WLOCK), 0777, &hTape);
    if (code) {
	fprintf(logFile, "Can't open tape device for pass 2\n");
	fflush(logFile);
	printf("Can't open tape device %s\n", tapeDevice);
	goto error_exit;
    }

    code = rewindTape(hTape);
    if (code) {
	fprintf(logFile, "Can't rewind tape\n");
	fflush(logFile);
	ERROR(code);
    }

    /* now measure file mark size */
    nFileMarks = 0;
    nBlocks = 0;
    count = 0;
    countr = 0;
    while (1) {
	code = dataBlock(hTape, bufferSize);
	nBlocks++;
	if (code)
	    break;
	code = fileMark(hTape);
	nFileMarks++;
	if (code)
	    break;
	count++;
	countr++;

	if (count >= 2) {
	    count = 0;
	    printf("\rwrote %d blocks, %d filemarks", nBlocks, nFileMarks);
	}

    }
    printf("\nFinished filemark test\n");
    tpSize = (double)nbfTape *(double)bufferSize;
    fmSize =
	(((double)nbfTape -
	  (double)nBlocks) * (double)bufferSize) / (double)nFileMarks;
    printf("Tape capacity is %.0f bytes\n", tpSize);
    printf("File marks are %.0f bytes\n", fmSize);
    fprintf(logFile, "Tape capacity is %.0f bytes\n", tpSize);
    fprintf(logFile, "File marks are %.0f bytes\n", fmSize);
    fflush(logFile);
    fclose(logFile);
  error_exit:
    USD_CLOSE(hTape);
    return (code);
}