void lt_XMLTags::write(ByteStream &bs,bool const top) const { if(name.length()) { GUTF8String tag="<"+name; for(GPosition pos=args;pos;++pos) { tag+=GUTF8String(' ')+args.key(pos)+GUTF8String("=\42")+args[pos].toEscaped()+GUTF8String("\42"); } GPosition tags=content; if(tags||raw.length()) { tag+=">"; bs.writall((const char *)tag,tag.length()); tag="</"+name+">"; if(raw.length()) { bs.writestring(raw); } for(;tags;++tags) { content[tags].write(bs); } }else if(!raw.length()) { tag+="/>"; } bs.writall((const char *)tag,tag.length()); } if(top) { bs.writall("\n",1); } }
void Decoder::decodeTextSection(MoveNode* node, ByteStream& text) { for ( ; node; node = node->next()) { if (node->hasSupplement()) { if (uint8_t flag = node->commentFlag()) { mstl::string buf; Comment comment; if (flag & comm::Ante) { text.get(buf); comment.swap(buf, bool(flag & comm::Ante_Eng), bool(flag & comm::Ante_Oth)); node->setComment(comment, move::Ante); } if (flag & comm::Post) { text.get(buf); comment.swap(buf, bool(flag & comm::Post_Eng), bool(flag & comm::Post_Oth)); node->setComment(comment, move::Post); } } for (unsigned i = 0; i < node->variationCount(); ++i) decodeTextSection(node->variation(i), text); } } }
void Ball::exportToWindowsIcon(Path filename, Path directory) { //16x16 //32x32 //48x48 //256x256 ByteStream stream; writeIcoHeader(stream); writeIconDirEntry(stream, 16, 16, iconHeaderSize + iconDirEntrySize * 4); writeIconDirEntry(stream, 32, 32, iconHeaderSize + iconDirEntrySize * 4 + getBMPSize(16, 16)); writeIconDirEntry(stream, 48, 48, iconHeaderSize + iconDirEntrySize * 4 + getBMPSize(16, 16) + getBMPSize(32, 32)); writeIconDirEntry(stream, 256, 256, iconHeaderSize + iconDirEntrySize * 4 + getBMPSize(16, 16) + getBMPSize(32, 32) + getBMPSize(48, 48)); writeBMP(stream, scale(16, 16, transparent, directory)); writeBMP(stream, scale(32, 32, transparent, directory)); writeBMP(stream, scale(48, 48, transparent, directory)); scale(256, 256, transparent, directory)->save(stream); std::vector<byte> pngSize = convertIntToByteArrayLE(static_cast<int>(stream.size()) - (iconHeaderSize + iconDirEntrySize * 4 + getBMPSize(16, 16) + getBMPSize(32, 32) + getBMPSize(48, 48))); for (int i = 0; i < 4; ++i) stream.set(i + iconHeaderSize + iconDirEntrySize * 3 + 8, pngSize[i]); stream.save(filename); }
void serializeParseTree() { ByteStream b; ParseTree *t, *tmodel; t = makeParseTree(); tmodel = makeParseTree(); verifyParseTree(t); //sanity check on the test itself CPPUNIT_ASSERT(*t == *tmodel); CPPUNIT_ASSERT(!(*t != *tmodel)); ObjectReader::writeParseTree(t, b); delete t; t = ObjectReader::createParseTree(b); CPPUNIT_ASSERT(b.length() == 0); CPPUNIT_ASSERT(t != NULL); verifyParseTree(t); CPPUNIT_ASSERT(*t == *tmodel); CPPUNIT_ASSERT(!(*t != *tmodel)); delete t; delete tmodel; }
void serializeConstantColumn() { ConstantColumn c1, c2; TreeNode *t; ByteStream b; t = &c2; CPPUNIT_ASSERT(c1 == c2); CPPUNIT_ASSERT(!(c1 != c2)); CPPUNIT_ASSERT(c1 == t); CPPUNIT_ASSERT(!(c1 != t)); c1.type(5); c1.constval("ConstantColumn test"); c1.data("c1"); CPPUNIT_ASSERT(c1 != c2); CPPUNIT_ASSERT(!(c1 == c2)); CPPUNIT_ASSERT(c1 != t); CPPUNIT_ASSERT(!(c1 == t)); c1.serialize(b); CPPUNIT_ASSERT(c2.constval() == ""); c2.unserialize(b); CPPUNIT_ASSERT(b.length() == 0); CPPUNIT_ASSERT(c2.type() == 5); CPPUNIT_ASSERT(c2.constval() == "ConstantColumn test"); CPPUNIT_ASSERT(c1 == c2); CPPUNIT_ASSERT(!(c1 != c2)); CPPUNIT_ASSERT(c1 == t); CPPUNIT_ASSERT(!(c1 != t)); }
bool JoinPartition::getNextPartition(vector<RGData> *smallData, uint64_t *partitionID, JoinPartition **jp) { if (fileMode) { ByteStream bs; RGData rgData; if (nextPartitionToReturn > 0) return false; //cout << "reading the small side" << endl; nextSmallOffset = 0; while (1) { readByteStream(0, &bs); if (bs.length() == 0) break; rgData.deserialize(bs); //smallRG.setData(&rgData); //cout << "read a smallRG with " << smallRG.getRowCount() << " rows" << endl; smallData->push_back(rgData); } nextPartitionToReturn = 1; *partitionID = uniqueID; *jp = this; return true; } bool ret = false; while (!ret && nextPartitionToReturn < bucketCount) { ret = buckets[nextPartitionToReturn]->getNextPartition(smallData, partitionID, jp); if (!ret) nextPartitionToReturn++; } return ret; }
static void display_th44(ByteStream & out_str, IFFByteStream & iff, GUTF8String, size_t, DjVmInfo & djvminfo, int counter) { int start_page=-1; if (djvminfo.dir) { GPList<DjVmDir::File> files_list=djvminfo.dir->get_files_list(); for(GPosition pos=files_list;pos;++pos) { GP<DjVmDir::File> frec=files_list[pos]; if (iff.tell()>=frec->offset && iff.tell()<frec->offset+frec->size) { while(pos && !files_list[pos]->is_page()) ++pos; if (pos) start_page=files_list[pos]->get_page_num(); break; } } } if (start_page>=0) out_str.format( "Thumbnail icon for page %d", start_page+counter+1); else out_str.format( "Thumbnail icon"); }
void serializeOperator() { Operator o1, o2; TreeNode *t; ByteStream b; t = &o2; CPPUNIT_ASSERT(o1 == o2); CPPUNIT_ASSERT(!(o1 != o2)); CPPUNIT_ASSERT(o1 == t); CPPUNIT_ASSERT(!(o1 != t)); o1.data("="); CPPUNIT_ASSERT(o1 != o2); CPPUNIT_ASSERT(!(o1 == o2)); CPPUNIT_ASSERT(o1 != t); CPPUNIT_ASSERT(!(o1 == t)); o1.serialize(b); CPPUNIT_ASSERT(o2.data() == ""); o2.unserialize(b); CPPUNIT_ASSERT(b.length() == 0); CPPUNIT_ASSERT(o2.data() == "="); CPPUNIT_ASSERT(o1 == o2); CPPUNIT_ASSERT(!(o1 != o2)); CPPUNIT_ASSERT(o1 == t); CPPUNIT_ASSERT(!(o1 != t)); }
void InetStreamSocket::do_write(const ByteStream &msg, uint32_t whichMagic, Stats *stats) const { uint32_t msglen = msg.length(); uint32_t magic = whichMagic; uint32_t *realBuf; if (msglen == 0) return; /* buf.fCurOutPtr points to the data to send; ByteStream guarantees that there are at least 8 bytes before that for the magic & length fields */ realBuf = (uint32_t *)msg.buf(); realBuf -= 2; realBuf[0] = magic; realBuf[1] = msglen; try { written(fSocketParms.sd(), (const uint8_t*)realBuf, msglen + sizeof(msglen) + sizeof(magic)); } catch (std::exception& ex) { string errorMsg(ex.what()); errorMsg += " -- write from " + toString(); throw runtime_error(errorMsg); } if (stats) stats->dataSent(msglen + sizeof(msglen) + sizeof(magic)); }
void InitializeGameProcess::Main() { FILE* fp = fopen("GameData.dat","rb"); if(fp) { fseek(fp,0,SEEK_END); size_t sz = ftell(fp); fseek(fp,0,SEEK_SET); SetTaskName(FormatString("Reading %lu bytes (1/2)",sz)); ByteStream* Data = new ByteStream(); for(i32 i = 0;i < sz;i++) { Data->WriteByte(getc(fp)); SetProgress(((Scalar)i) / ((Scalar)sz)); } fclose(fp); SetTaskName("Deserializing data (2/2)"); SetProgress(0.0f); } m_Completed = true; }
size_t BuiltinProtocolHandlersLocal::write(const ByteStream &byteStream) { if (d->m_fd == -1) { return 0; } return ::write(d->m_fd, byteStream.data(), byteStream.size()); }
int stream_seekfn(struct iw_context *ctx, struct iw_iodescr *iodescr, iw_int64 offset, int whence) { //FILE *fp = (FILE*)iodescr->fp; //fseek(fp, (long)offset, whence); ByteStream* stream = (ByteStream*)iodescr->fp; stream->seek(whence); return 1; }
MessageQueueClient* sendCSEP(CalpontSelectExecutionPlan* csep) { scoped_ptr<CalpontSelectExecutionPlan> cleaner(csep); ByteStream bs; MessageQueueClient* mqc=0; mqc = new MessageQueueClient("ExeMgr1"); auto_ptr<MessageQueueClient> smqc(mqc); bs.reset(); ByteStream::quadbyte wantTuples=4; bs << wantTuples; mqc->write(bs); bs.reset(); csep->serialize(bs); mqc->write(bs); SBS sbs; sbs = mqc->read(); *sbs >> wantTuples; //cerr << "got flag: " << wantTuples << endl; string msg; sbs = mqc->read(); *sbs >> msg; //cerr << "got msg: " << msg << endl; if (wantTuples != 0) throw runtime_error(msg); smqc.release(); return mqc; }
static void writeText( ByteStream & str_out, const GUTF8String &textUTF8, const DjVuTXT::Zone &zone, const int WindowHeight ) { // DEBUG_MSG( "--zonetype=" << zone.ztype << "\n" ); const GUTF8String xindent(indent( 2 * zone.ztype + 2 )); GPosition pos=zone.children; // Build attribute string if( ! pos ) { GUTF8String coords; coords.format("coords=\"%d,%d,%d,%d\"", zone.rect.xmin, WindowHeight - 1 - zone.rect.ymin, zone.rect.xmax, WindowHeight - 1 - zone.rect.ymax); const int start=zone.text_start; const int end=textUTF8.firstEndSpace(start,zone.text_length); str_out.writestring(start_tag(zone.ztype,coords)); str_out.writestring(textUTF8.substr(start,end-start).toEscaped()); str_out.writestring(end_tag(zone.ztype)); } else { writeText(str_out,textUTF8,zone.ztype,zone.children,WindowHeight); } }
void NefDecoder::readCoolpixMangledRaw(ByteStream &input, iPoint2D& size, iPoint2D& offset, int inputPitch) { uchar8* data = mRaw->getData(); uint32 outPitch = mRaw->pitch; uint32 w = size.x; uint32 h = size.y; uint32 cpp = mRaw->getCpp(); if (input.getRemainSize() < (inputPitch*h)) { if ((int)input.getRemainSize() > inputPitch) h = input.getRemainSize() / inputPitch - 1; else ThrowIOE("readUncompressedRaw: Not enough data to decode a single line. Image file truncated."); } if (offset.y > mRaw->dim.y) ThrowRDE("readUncompressedRaw: Invalid y offset"); if (offset.x + size.x > mRaw->dim.x) ThrowRDE("readUncompressedRaw: Invalid x offset"); uint32 y = offset.y; h = MIN(h + (uint32)offset.y, (uint32)mRaw->dim.y); w *= cpp; BitPumpMSB32 *in = new BitPumpMSB32(&input); for (; y < h; y++) { ushort16* dest = (ushort16*) & data[offset.x*sizeof(ushort16)*cpp+y*outPitch]; for (uint32 x = 0 ; x < w; x++) { dest[x] = in->getBits(12); } } }
void DamageSpellEffect::Serialize(ByteStream& stream) const { BaseClass::Serialize(stream); stream.Write8(static_cast<unsigned char>(m_damageType.Type())); stream.Write8(m_ucRestriction); }
void serializeFilter() { Filter f1, f2; TreeNode *t; ByteStream b; t = &f2; CPPUNIT_ASSERT(f1 == f2); CPPUNIT_ASSERT(!(f1 != f2)); CPPUNIT_ASSERT(f1 == t); CPPUNIT_ASSERT(!(f1 != t)); f1.data("Filter test"); CPPUNIT_ASSERT(f1 != f2); CPPUNIT_ASSERT(!(f1 == f2)); CPPUNIT_ASSERT(f1 != t); CPPUNIT_ASSERT(!(f1 == t)); f1.serialize(b); CPPUNIT_ASSERT(f2.data() == ""); f2.unserialize(b); CPPUNIT_ASSERT(b.length() == 0); CPPUNIT_ASSERT(f2.data() == "Filter test"); CPPUNIT_ASSERT(f1 == f2); CPPUNIT_ASSERT(!(f1 != f2)); CPPUNIT_ASSERT(f1 == t); CPPUNIT_ASSERT(!(f1 != t)); }
static void display_djvm_dirm(ByteStream & out_str, IFFByteStream & iff, GUTF8String head, size_t, DjVmInfo& djvminfo, int) { GP<DjVmDir> dir = DjVmDir::create(); dir->decode(iff.get_bytestream()); GPList<DjVmDir::File> list = dir->get_files_list(); if (dir->is_indirect()) { out_str.format( "Document directory (indirect, %d files %d pages)", dir->get_files_num(), dir->get_pages_num()); for (GPosition p=list; p; ++p) out_str.format( "\n%s%s -> %s", (const char*)head, (const char*)list[p]->get_load_name(), (const char*)list[p]->get_save_name() ); } else { out_str.format( "Document directory (bundled, %d files %d pages)", dir->get_files_num(), dir->get_pages_num()); djvminfo.dir = dir; djvminfo.map.empty(); for (GPosition p=list; p; ++p) djvminfo.map[list[p]->offset] = list[p]; } }
/** * This processes each (control, data) tuple according to the state machine */ void RDMSniffer::ProcessTuple(uint8_t control_byte, uint8_t data_byte) { if (control_byte & DATA_MASK) { // this is an actual byte of data switch (m_state) { case IDLE: case MAB: m_state = DATA; m_frame.Reset(); case DATA: m_frame.AddByte(data_byte); break; default: OLA_WARN << "Unknown transition from state " << m_state << ", with data 0x" << std::hex << static_cast<int>(control_byte) << " 0x" << static_cast<int>(data_byte); } } else { // control byte if (data_byte == 0) { switch (m_state) { case BREAK: m_state = MAB; break; default: OLA_WARN << "Unknown transition from state " << m_state << ", with data 0x" << std::hex << static_cast<int>(control_byte) << " 0x" << static_cast<int>(data_byte); } } else if (data_byte == 1) { switch (m_state) { case IDLE: m_state = BREAK; break; case DATA: ProcessFrame(); m_state = BREAK; break; default: OLA_WARN << "Unknown transition from state " << m_state << ", with data 0x" << std::hex << static_cast<int>(control_byte) << " 0x" << static_cast<int>(data_byte); } } else if (data_byte == 2) { switch (m_state) { case IDLE: case BREAK: case MAB: break; case DATA: m_state = IDLE; ProcessFrame(); } } else { OLA_WARN << "Unknown transition from state " << m_state << ", with data 0x" << std::hex << static_cast<int>(control_byte) << " 0x" << static_cast<int>(data_byte); } } }
TEST(ByteStreamTest, testReadByte) { ByteStream byteStream; byteStream.addByte(BYTE_ADD); const size_t previous = byteStream.getSize(); const uint8_t byte = byteStream.readByte(); ASSERT_EQ(BYTE_ADD, byte); ASSERT_EQ(previous - 1, byteStream.getSize()); }
TEST(ByteStreamTest, testWriteEmptyString) { ByteStream byteStream; byteStream.addString(""); ASSERT_EQ(1, byteStream.getSize()); const std::string empty = byteStream.readString(); ASSERT_EQ("", empty); ASSERT_EQ(0, byteStream.getSize()); }
TEST(ByteStreamTest, testReadInt) { ByteStream byteStream; byteStream.addInt(INT_ADD); const size_t previous = byteStream.getSize(); int32_t dword = byteStream.readInt(); ASSERT_EQ(INT_ADD, dword); ASSERT_EQ(previous - 4, byteStream.getSize()); }
TEST(ByteStreamTest, testReadShort) { ByteStream byteStream; byteStream.addShort(SHORT_ADD); const size_t previous = byteStream.getSize(); const int16_t word = byteStream.readShort(); ASSERT_EQ(SHORT_ADD, word); ASSERT_EQ(previous - 2, byteStream.getSize()); }
ByteStream::ByteStream(const ByteStream & source): allocatedSize(source.getSize()), // only allocate what is really there, be opportinistic when grow()'ing data(source.data), size(source.getSize()) { source.data->ref(); beginReadIterator = ReadIterator(*this); }
/// deserialize message by reading bytes from stream void Deserialize(ByteStream& stream) { m_cszName = stream.ReadString(64); m_cBackgroundColor.m_color[Color::red] = stream.Read8(); m_cBackgroundColor.m_color[Color::green] = stream.Read8(); m_cBackgroundColor.m_color[Color::blue] = stream.Read8(); m_fBrightness = stream.Read8() / 255.f; }
/// serialize message by putting bytes to stream void Serialize(ByteStream& stream) const { stream.WriteString(m_cszName); stream.Write8(m_cBackgroundColor.m_color[Color::red]); stream.Write8(m_cBackgroundColor.m_color[Color::green]); stream.Write8(m_cBackgroundColor.m_color[Color::blue]); stream.Write8(static_cast<unsigned char>(m_fBrightness * 255.f)); }
int stream_writefn(struct iw_context *ctx, struct iw_iodescr *iodescr, const void *buf, size_t nbytes) { //fwrite(buf, 1, nbytes, (FILE*)iodescr->fp); ByteStream* stream = (ByteStream*)iodescr->fp; for (size_t i = 0; i < nbytes; ++i) { stream->put(((byte*)buf)[i]); } return 1; }
uint64_t JoinPartition::writeByteStream(int which, ByteStream &bs) { size_t &offset = (which == 0 ? nextSmallOffset : nextLargeOffset); fstream &fs = (which == 0 ? smallFile : largeFile); const char *filename = (which == 0 ? smallFilename.c_str() : largeFilename.c_str()); fs.open(filename, ios::binary | ios::out | ios::app); int saveErrno = errno; if (!fs) { fs.close(); ostringstream os; os << "Disk join could not open file (write access) " << filename << ": " << strerror(saveErrno) << endl; throw IDBExcept(os.str().c_str(), ERR_DBJ_FILE_IO_ERROR); } uint64_t ret = 0; size_t len = bs.length(); idbassert(len != 0); fs.seekp(offset); if (!useCompression) { ret = len + 4; fs.write((char *) &len, sizeof(len)); fs.write((char *) bs.buf(), len); saveErrno = errno; if (!fs) { fs.close(); ostringstream os; os << "Disk join could not write file " << filename << ": " << strerror(saveErrno) << endl; throw IDBExcept(os.str().c_str(), ERR_DBJ_FILE_IO_ERROR); } totalBytesWritten += sizeof(len) + len; } else { uint64_t maxSize = compressor.maxCompressedSize(len); size_t actualSize; boost::scoped_array<uint8_t> compressed(new uint8_t[maxSize]); compressor.compress((char *) bs.buf(), len, (char *) compressed.get(), &actualSize); ret = actualSize + 4; fs.write((char *) &actualSize, sizeof(actualSize)); fs.write((char *) compressed.get(), actualSize); saveErrno = errno; if (!fs) { fs.close(); ostringstream os; os << "Disk join could not write file " << filename << ": " << strerror(saveErrno) << endl; throw IDBExcept(os.str().c_str(), ERR_DBJ_FILE_IO_ERROR); } totalBytesWritten += sizeof(actualSize) + actualSize; } bs.advance(len); offset = fs.tellp(); fs.close(); return ret; }
TEST(ByteStreamTest, testReadFloat) { ByteStream byteStream; const float expected = 0.1f; byteStream.addFloat(expected); const size_t previous = byteStream.getSize(); const float dword = byteStream.readFloat(); ASSERT_DOUBLE_EQ(expected, dword); ASSERT_EQ(previous - 4, byteStream.getSize()); }
TEST(ByteStreamTest, testReadString) { ByteStream byteStream; const std::string str = "hello IT!"; byteStream.addString(str); const size_t previous = byteStream.getSize(); const std::string readstr = byteStream.readString(); ASSERT_EQ(str, readstr); ASSERT_EQ(previous - str.length() - 1, byteStream.getSize()); }