Example #1
0
void
BWFAudioFile::close()
{
    if (m_outFile == 0)
        return ;

    m_outFile->seekp(0, std::ios::end);
    unsigned int totalSize = m_outFile->tellp();

    // seek to first length position
    m_outFile->seekp(4, std::ios::beg);

    // write complete file size minus 8 bytes to here
    putBytes(m_outFile, getLittleEndianFromInteger(totalSize - 8, 4));

    // reseek from start forward 40
    m_outFile->seekp(40, std::ios::beg);

    // write the data chunk size to end
    putBytes(m_outFile, getLittleEndianFromInteger(totalSize - 44, 4));

    m_outFile->close();

    delete m_outFile;
    m_outFile = 0;
}
void TableInfo::put(RecordType &rec, UINT n, const String &v) const {
  if(v.length() > m_columns[n].getMaxStringLen()) {
    throwSqlError(SQL_STRING_TOO_LONG,_T("%s:String too long to fit in keyfield[%d]. len=%d. maxlen=%d")
                                     ,__TFUNCTION__,n,(UINT)v.length(),m_columns[n].getMaxStringLen());
  }
  USES_CONVERSION;
  switch(getColumn(n).getType()) {
  case DBTYPE_CSTRING    :
  case DBTYPE_CSTRINGN   :
    { char tmp[MAXRECSIZE+1];
      memset(tmp,0,sizeof(tmp));
      strcpy(tmp,T2A((TCHAR*)v.cstr()));
      putBytes(rec,n,tmp);
    }
    break;
  case DBTYPE_WSTRING    :
  case DBTYPE_WSTRINGN   :
    { wchar_t tmp[MAXRECSIZE+1];
      memset(tmp,0,sizeof(tmp));
      wcscpy(tmp,T2W((TCHAR*)v.cstr()));
      putBytes(rec,n,tmp);
    }
    break;
  default: THROWFIELDNOTSTRING(n);
  }
}
Example #3
0
	size_type DataPage::addSingleRecord(const RecordId& recordId, const AddedValueRef& valueRef)
	{
		const size_type valueInlineSize = static_cast<size_type>(valueRef.value().size());
		const size_type recordInlineSize = recordId.recordOverheadSize() + valueInlineSize; // record overhead (record id size + inline value size (2)) + value or large value reference.
		const bool canAdd = freeSpace() >= sizeof(uint16_t) + recordId.recordOverheadSize() + valueInlineSize; // record pointer (2) + record.

		if (canAdd) {
			// Compute offsets.
			const size_type endOfFreeArea = getEndOfFreeArea();

			const size_type keyOffset = endOfFreeArea - recordInlineSize;
			const size_type valueOffset = endOfFreeArea - valueInlineSize;
			const size_type valueSizeOffset = valueOffset - sizeof(uint16_t);

			// Copy the key, value size and value.
			putBytes(keyOffset, recordId.value());

			const uint16_t valueSizeOrTag = valueRef.valueSizeOrTag();
			this->operator[](valueSizeOffset)     = (valueSizeOrTag & 0xff);
			this->operator[](valueSizeOffset + 1) = ((valueSizeOrTag >> 8) & 0xff);

			const boost::string_ref value = valueRef.value();
			putBytes(valueOffset, value);

			// Add new pointer to the start of the key.
			addRecordOffset(keyOffset);

			// Set new "end of free area".
			setEndOfFreeArea(keyOffset);
		}

		return (canAdd)? recordInlineSize : 0;
	}
Example #4
0
static void sendReply(int sock,uint32_t opt,uint32_t reply_type, size_t datasize, void* data)
{
	uint64_t magic = htonll(0x3e889045565a9LL);
	reply_type = htonl(reply_type);
	uint32_t datsize = htonl(datasize);       
        
	doLog("sendReply");
	putBytes(sock,&magic,sizeof(magic));
	putBytes(sock,&opt,sizeof(opt));
	putBytes(sock,&reply_type,sizeof(reply_type));
	putBytes(sock,&datsize,sizeof(datsize));
	if(datasize) putBytes(sock,data,datasize);
}
Example #5
0
/**
 * Create
 * Create and return a byte array of an HTTP request, built from the variables of this HTTPRequest
 *
 * @return Byte array of this HTTPRequest to be sent over the wire
 */
uint8_t* HTTPRequest::create() {
    // Clear the bytebuffer in the event this isn't the first call of create()
	clear();
    
    // Insert the initial line: <method> <path> <version>\r\n
    std::string mstr = "";
    mstr = methodIntToStr(method);
    if(mstr.empty()) {
        printf("Could not create HTTPRequest, unknown method id: %i\n", method);
        return NULL;
    }
    putLine(mstr + " " + requestUri + " " + version);
    
    // Put all headers
    putHeaders();
    
    // If theres body data, add it now
    if((data != NULL) && dataLen > 0) {
        putBytes(data, dataLen);
    }
    
    // Allocate space for the returned byte array and return it
	uint8_t* createRetData = new uint8_t[size()];
	setReadPos(0);
	getBytes(createRetData, size());
    
    return createRetData;
}
Example #6
0
inline BOOL ByteStreamOutArrayBE::put32bitsLE(const U8* bytes)
{
  swapped[0] = bytes[3];
  swapped[1] = bytes[2];
  swapped[2] = bytes[1];
  swapped[3] = bytes[0];
  return putBytes(swapped, 4);
}
Example #7
0
/**
 * Put Line
 * Append a line (string) to the backing ByteBuffer at the current position
 *
 * @param str String to put into the byte buffer
 * @param crlf_end If true (default), end the line with a \r\n
 */
void HTTPMessage::putLine(string str, bool crlf_end) {
    // Terminate with crlf if flag set
    if(crlf_end)
        str += "\r\n";
    
    // Put the entire contents of str into the buffer
    putBytes((byte*)str.c_str(), str.size());
}
Example #8
0
ByteBuffer::ByteBuffer(byte* arr, unsigned int size){
    if(arr == NULL){
	buf.reserve(size);
	clear();
    } else {
	buf.reserve(size);
	clear();
	putBytes(arr, size);
    }
}
Example #9
0
 void put(const void *src)
 {
     if (compressed) {
         if (first) {
             first = false;
             memcpy(prev,src,size);
         }
         else if (remaining()>=maxcompsize) {
             size32_t sz = DiffCompress(src,ptr,prev,size);
             fpos += sz;
             ptr += sz;
             return;
         }
         else { 
             putBytes(aux, DiffCompress(src,aux,prev,size));
             return;
         }
     }
     putBytes(src, size);
 }
Example #10
0
inline BOOL ByteStreamOutArrayBE::put64bitsLE(const U8* bytes)
{
  swapped[0] = bytes[7];
  swapped[1] = bytes[6];
  swapped[2] = bytes[5];
  swapped[3] = bytes[4];
  swapped[4] = bytes[3];
  swapped[5] = bytes[2];
  swapped[6] = bytes[1];
  swapped[7] = bytes[0];
  return putBytes(swapped, 8);
}
Example #11
0
kByteBuffer::kByteBuffer(byte* arr, unsigned int size) {
    // If the provided array is NULL, allocate a blank buffer of the provided size
    if(arr == NULL) {
        buf.reserve(size);
        clear();
    } else {
        // Consume the provided array
        buf.reserve(size);
        clear();
        putBytes(arr, size);
    }
}
Example #12
0
 void putn(const void *src, unsigned numRecs)
 {
     if (compressed) {
         while (numRecs) {
             put(src);
             src = (byte *)src+size;
             numRecs--;
         }
     }
     else
         putBytes(src, size*numRecs);
 }
Example #13
0
bool
RIFFAudioFile::appendSamples(const std::string &buffer)
{
    /*
    if (m_outFile == 0 || m_type != WAV)
        return false;
        */

    // write out
    putBytes(m_outFile, buffer);

    return true;
}
Example #14
0
void doConnectionMade(int sock)
{
	struct {
		volatile uint64_t passwd __attribute__((packed));
		volatile uint64_t magic  __attribute__((packed));
		volatile uint16_t flags  __attribute__((packed));
	} nbd;
    
	memcpy(&nbd,INIT_PASSWD,8);
	nbd.magic  = htonll(OPTS_MAGIC);
	nbd.flags  = htons(NBD_FLAG_FIXED_NEWSTYLE);
    
	doLog("Connection Made");
	putBytes(sock,&nbd,sizeof(nbd));
}
Example #15
0
/**
* ByteBuffer constructor
* Consume an entire uint8_t array of length len in the ByteBuffer
*
* @param arr uint8_t array of data (should be of length len)
* @param size Size of space to allocate
*/
ByteBuffer::ByteBuffer(uint8_t* arr, uint32_t size) {
	// If the provided array is NULL, allocate a blank buffer of the provided size
	if(arr == NULL) {
		buf.reserve(size);
		clear();
	} else { // Consume the provided array
		buf.reserve(size);
		clear();
		putBytes(arr, size);
	}

#ifdef BB_UTILITY
	name = "";
#endif
}
Example #16
0
int p16a_program_page (unsigned int ptr, unsigned char num, unsigned char slow)
    {
//	unsigned char i;
    if (verbose>2) flsprintf(stdout,"Programming page of %d bytes at 0x%4.4x\n", num,ptr);
    putByte(0x08);
    putByte(num+2);
    putByte(num);
    putByte(slow);
    /*
    for (i=0;i<num;i++)
    	putByte(file_image[ptr+i]);
    	*/
    putBytes(&file_image[ptr],num);
    getByte();
    return 0;
    }
Example #17
0
/**
 * Login Packet Create
 * Populate the backing ByteBuffer with the expected LoginPacket structure as defined in the protocol
 *
 * Struct:
 * INT: Protocol Version
 * INT: Size of Username
 * A_STR: Username
 *
 * @return Byte array of the same size as the ByteBuffer (pkt->size()) of the built packet
 */
byte* LoginPacket::create(bool force) {
	// Check for cached create data
	if(checkCreate(force))
		return createData;

	// Build the packet
	put(OP(LOGIN));
	putInt(PROTOCOL_VERSION); // The protocol version the program was compiled with
	putInt((int)username.size()+1);
	putBytes((byte *)username.c_str(), (int)username.size()+1);

	// Save created data
	saveCreate();

	// Return the created byte array
    return createData;
}
Example #18
0
	size_type DataPage::addSingleRecord(const boost::string_ref& recordInlineData)
	{
		const size_type recordInlineSize = static_cast<size_type>(recordInlineData.size());
		const bool canAdd = freeSpace() >= sizeof(uint16_t) + recordInlineSize;

		if (canAdd) {
			// Compute offsets.
			const size_type endOfFreeArea = getEndOfFreeArea();
			const size_type recordOffset = endOfFreeArea - recordInlineSize;

			// Copy the record, add pointer to its start and adjust "end of free area".
			putBytes(recordOffset, recordInlineData);
			addRecordOffset(recordOffset);
			setEndOfFreeArea(recordOffset);
		}

		return (canAdd)? recordInlineSize : 0;
	}
Example #19
0
byte* HttpResponse::create() {
    clear();

    std::stringstream sline;
    sline << version << " " << status << " " << reason;
    putLine(sline.str());

    putHeaders();

    if((data != NULL) && dataLen > 0) {
        putBytes(data, dataLen);
    }

    byte* createRetData = new byte[size()];
    setReadPos(0);
    getBytes(createRetData, size());

    return createRetData;
}
Example #20
0
inline BOOL ByteStreamOutNil::put32bitsLE(const U8* bytes)
{
  return putBytes(bytes, 4);
}
Example #21
0
inline BOOL ByteStreamOutNil::put64bitsBE(const U8* bytes)
{
  return putBytes(bytes, 8);
}
Example #22
0
// Write out the format chunk from our internal data
//
void
RIFFAudioFile::writeFormatChunk()
{
    if (m_outFile == 0 || m_type != WAV)
        return ;

    std::string outString;

    // RIFF type is all we support for the moment
    outString += AUDIO_RIFF_ID;

    // Now write the total length of the file minus these first 8 bytes.
    // We won't know this until we've finished recording the file.
    //
    outString += "0000";

    // WAV file is all we support
    //
    outString += AUDIO_WAVE_ID;

    // Begin the format chunk
    outString += AUDIO_FORMAT_ID;

    // length
    //cout << "LENGTH = " << getLittleEndianFromInteger(0x10, 4) << endl;
    outString += getLittleEndianFromInteger(0x10, 4);

    // 1 for PCM, 3 for float
    if (m_subFormat == PCM) {
        outString += getLittleEndianFromInteger(0x01, 2);
    } else {
        outString += getLittleEndianFromInteger(0x03, 2);
    }

    // channel
    outString += getLittleEndianFromInteger(m_channels, 2);

    // sample rate
    outString += getLittleEndianFromInteger(m_sampleRate, 4);

    // bytes per second
    outString += getLittleEndianFromInteger(m_bytesPerSecond, 4);

    // bytes per sample
    outString += getLittleEndianFromInteger(m_bytesPerFrame, 2);

    // bits per sample
    outString += getLittleEndianFromInteger(m_bitsPerSample, 2);

    // Now mark the beginning of the "data" chunk and leave the file
    // open for writing.
    outString += "data";

    // length of data to follow - again needs to be written after
    // we've completed the file.
    //
    outString += "0000";

    // write out
    //
    putBytes(m_outFile, outString);
}
Example #23
0
bool
RIFFAudioFile::appendSamples(const char *buf, unsigned int frames)
{
    putBytes(m_outFile, buf, frames * m_bytesPerFrame);
    return true;
}
Example #24
0
 static U8* serialize(U8* buffer, Type0 value0, Types... values) {
     const int s = sizeof(Type0);
     buffer = putBytes(buffer, value0);
     return serialize<N-s>(buffer, values...);
 }
void TableInfo::put(RecordType &rec, UINT n, const TupleField &v) const {
  if(!v.isDefined()) {
    setUndefined(rec,n);
    return;
  }
  switch(m_columns[n].getType()) {
  case DBTYPE_CHAR      :
  case DBTYPE_CHARN     : PUTTUP(char     ); break;
  case DBTYPE_UCHAR     :
  case DBTYPE_UCHARN    : PUTTUP(UCHAR    ); break;
  case DBTYPE_SHORT     :
  case DBTYPE_SHORTN    : PUTTUP(short    ); break;
  case DBTYPE_USHORT    :
  case DBTYPE_USHORTN   : PUTTUP(USHORT   ); break;
  case DBTYPE_INT       :
  case DBTYPE_INTN      : PUTTUP(int      ); break;
  case DBTYPE_UINT      :
  case DBTYPE_UINTN     : PUTTUP(UINT     ); break;
  case DBTYPE_LONG      :
  case DBTYPE_LONGN     : PUTTUP(long     ); break;
  case DBTYPE_ULONG     :
  case DBTYPE_ULONGN    : PUTTUP(ULONG    ); break;
  case DBTYPE_INT64     :
  case DBTYPE_INT64N    : PUTTUP(INT64    ); break;
  case DBTYPE_UINT64    :
  case DBTYPE_UINT64N   : PUTTUP(UINT64   ); break;
  case DBTYPE_FLOAT     :
  case DBTYPE_FLOATN    : PUTTUP(float    ); break;
  case DBTYPE_DOUBLE    :
  case DBTYPE_DOUBLEN   : PUTTUP(double   ); break;
  case DBTYPE_CSTRING   :
  case DBTYPE_CSTRINGN  :
  case DBTYPE_WSTRING   :
  case DBTYPE_WSTRINGN  : { String tmp; v.get(tmp); put(rec,n,tmp); } break;
  case DBTYPE_VARCHAR   :
  case DBTYPE_VARCHARN  : { DbAddr addr; v.get(addr); DbAddrFileFormat addrFF; addrFF = addr; putBytes(rec,n,&addrFF); } break;
  case DBTYPE_DATE      :
  case DBTYPE_DATEN     : PUTTUP(Date     ); break;
  case DBTYPE_TIME      :
  case DBTYPE_TIMEN     : PUTTUP(Time     ); break;
  case DBTYPE_TIMESTAMP :
  case DBTYPE_TIMESTAMPN: PUTTUP(Timestamp); break;
  default               : THROWINVALIDFIELDTYPE(n);
  }
}
Example #26
0
inline BOOL ByteStreamOutArrayLE::put64bitsLE(const U8* bytes)
{
  return putBytes(bytes, 8);
}
Example #27
0
void doSession(int sock)
{
	struct nbd_request request;
	struct nbd_reply reply;
	char buffer[1024*132];
	int readlen,bytes;
	int running = True;
	int db;

	doLog("Enter SESSION");
	doConnectionMade(sock);
	if(db=doNegotiate(sock)) {
	
		doLog("Processing DATA requests ...");
        		        
		do {
            
			getBytes(sock,&request,sizeof(request));
			off = ntohll(request.from);
			cmd = ntohl(request.type) & NBD_CMD_MASK_COMMAND;
			len = ntohl(request.len);
			reply.magic = htonl(NBD_REPLY_MAGIC);
			reply.error = 0;
			memcpy(reply.handle, request.handle, sizeof(reply.handle));
			
			if(debug) {	
				snprintf(pbuf,sizeof(pbuf),"%s - block [%04llx] %ld blocks, off=%lld, len=%ld",	
					cmds[cmd],						
					(long long unsigned int)(off/1024),		
					(long unsigned int)(len/1024),			
					(long long unsigned int)off,(long unsigned int)len	
				);								
				doLog(pbuf);							
			}	
			switch(cmd) {
				case NBD_READ:
					putBytes(sock,&reply,sizeof(reply));									
					if(lseek(db,off,SEEK_SET)==-1) running = doError("SEEK");
					while( len > 0 ) {
						readlen = len > sizeof(buffer)?sizeof(buffer):len;
						//syslog(LOG_ERR,"READ: %d, %lld %ld",db,(unsigned long long)off,(unsigned long) len);
						bytes = read(db,&buffer,readlen);
						if( bytes != readlen ) running = doError("READ");
						else putBytes(sock,&buffer,readlen);
						len -= readlen;
					}
				break;
                
			case NBD_WRITE:
				if(lseek(db,off,SEEK_SET)==-1) running = doError("SEEK");
				while( len > 0 ) {
					readlen = len > sizeof(buffer)?sizeof(buffer):len;
				        getBytes(sock,&buffer,readlen);
					bytes = write(db,&buffer,readlen);
					if( bytes != readlen ) running = doError("WRITE");
					len -= readlen;
				}
				putBytes(sock,&reply,sizeof(reply));
				break;
            
			case NBD_CLOSE:
				running = False;
				break;
			
			case NBD_TRIM:
				// FIXME :: TRIM Code needed
				putBytes(sock,&reply,sizeof(reply));
				break;
				
			case NBD_FLUSH:
				// FIXME :: FLUSH Code needed
				putBytes(sock,&reply,sizeof(reply));
				break;
                
			default:
				doError("Unknown Command");
			}	
		} while( running );          
	}
	if(db) close(db);
	doLog("Exit SESSION");
}
Example #28
0
int doNegotiate(int sock)
{
    struct {
	        volatile uint64_t magic __attribute__((packed));
	        volatile uint32_t opts  __attribute__((packed));
	} nbd;
    
	uint32_t flags;    
	uint32_t len;
	uint32_t opt;
	
	char 	*name;	
	char 	path[256];
	int 	status = False;
	int 	working = True;
	int	db;

	getBytes(sock,&flags,sizeof(flags));
	flags = htonl(flags);
	
	doLog("Enter NEGOTIATION");
	
	do {
		getBytes(sock,&nbd,sizeof(nbd));
		if(nbd.magic != htonll(OPTS_MAGIC)) {
			doError("Bad MAGIC from Client");
			status = False;
			working = False;	
			break;
		}
		opt = ntohl(nbd.opts);
		switch( opt )
		{
			case NBD_OPT_EXPORT_NAME:                
				getBytes(sock,&len,sizeof(len));
				len = ntohl(len);
				name = malloc(len+1);
				name[len]=0;
				getBytes(sock,name,len);
				syslog(LOG_INFO,"Incoming name = %s",name);
		
				sprintf(path,"/dev/vols/blocks/%s",name);
				db = open(path,O_RDWR|O_EXCL);
				if( db == -1) {
					sprintf(path,"/dev/vols/blocks/%s1",name);
					db = open(path,O_RDWR|O_EXCL);
					if( db ==-1) {
						doError("Unable to open BLOCK DEVICE");
						doError(path);
						status = False;
						working = False;
						break;
					}
				}
				if(db != -1) { syslog(LOG_INFO,"Opened [%s] with descriptor [%d]",path,db); }
				working = False;
				status = True;
				free(name);
				break;
	
			/*case NBD_OPT_LIST:
				doLog("Received LIST from client");
				getBytes(&len,sizeof(len));
				len=ntohl(len);
				if(len) sendReply(opt, NBD_REP_ERR_INVALID, 0, NULL);    
                
				char buf[128];
				char *ptr = (char*)&buf;
                
				len = htonl(4);
				memcpy(ptr,&len,sizeof(len));
				ptr += sizeof(len);
				memcpy(ptr,"demo",4);
				sendReply(opt, NBD_REP_SERVER, 8, buf);
				sendReply(opt, NBD_REP_ACK, 0, NULL);
				break; */
	
			case NBD_OPT_ABORT:
				doLog("Received ABORT from client");
				status = False;
				working = False;
				break;
	
			default:
				doError("Unknown command");
				break;
		}
	} while( working );
	
	if(!status) return False;
	
	
	int64_t size = 0;
	ioctl(db, BLKGETSIZE, &size);
	syslog(LOG_INFO,"Device Size = %lld\n",(unsigned long long)size);
	size = htonll(size*512);
	int16_t small = htons(1);
	char zeros[124];
	memset(zeros,0,sizeof(zeros));
	putBytes(sock,&size,sizeof(size));
	putBytes(sock,&small,sizeof(small));
	putBytes(sock,&zeros,sizeof(zeros));
	doLog("Exit NEGOTIATION [Ok]");
	return db;
}
Example #29
0
inline BOOL ByteStreamOutArrayBE::put16bitsLE(const U8* bytes)
{
  swapped[0] = bytes[1];
  swapped[1] = bytes[0];
  return putBytes(swapped, 2);
}
Example #30
0
inline BOOL ByteStreamOutArrayBE::put32bitsBE(const U8* bytes)
{
  return putBytes(bytes, 4);
}