/* From SFTP RFC: All packets transmitted over the secure connection are of the following format: uint32 length byte type uint32 request-id ... type specific fields ... 'length' The length of the entire packet, excluding the length field itself, such that, for example, for a packet type containing no type specific fields, the length field would be 5, and 9 bytes of data would be sent on the wire. (This is the packet format used in [RFC4253].) All packet descriptions in this document omit the length field for brevity; the length field MUST be included in any case. The maximum size of a packet is in practice determined by the client (the maximum size of read or write requests that it sends, plus a few bytes of packet overhead). All servers SHOULD support packets of at least 34000 bytes (where the packet size refers to the full length, including the header above). This should allow for reads and writes of at most 32768 bytes. 'type' The type code for the packet. 'request-id' Each request from the client contains a 'request-id' field. Each response from the server includes that same 'request-id' from the request that the server is responding to. One possible implementation is for the client to us a monotonically increasing request sequence number (modulo 2^32). There is, however, no particular requirement the 'request-id' fields be unique. */ int32 SftpBinaryPacket::init(uint32 sftpDataLen, uint8 sftpType, uint32 requestID, uint32 remoteChannelNum) { int32 result = PTSSH_SUCCESS; uint32 sftpPacketLen = 4 + //length 1 + //type 4 + //request-id sftpDataLen, //Number of bytes of data in the Sftp packet BPLen = 1 + //byte SSH_MSG_CHANNEL_DATA 4 + //uint32 recipient channel 4 + sftpPacketLen; //string data if ( BinaryPacket::init(BPLen)) { //Write the SSH specific header writeByte( SSH_MSG_CHANNEL_DATA); //SSH CHANNEL_DATA writeUint32( remoteChannelNum); //SSH Channel number writeUint32( sftpPacketLen); //SHH channel data length //Write the length, type and request-id writeUint32( sftpPacketLen - 4); //SFTP packet length writeByte( sftpType); //SFTP Type writeUint32( requestID); //SFTP requestID //Now the SSH Binary packet has the ssh header and sftp headers in place //Any additional writes* will be put in the proper place in the packet } else result = PTSSH_ERR_CouldNotAllocateMemory; return result; }
char * TOptIAPrefix::storeSelf(char* buf) { buf = writeUint16(buf, OptType); buf = writeUint16(buf, getSize()-4); buf = writeUint32(buf, PrefLifetime_); buf = writeUint32(buf, ValidLifetime_); *(char*)buf = PrefixLength_; buf+=1; memcpy(buf, Prefix_->getAddr(), 16); buf+=16; buf=storeSubOpt(buf); return buf; }
char * TOptIAAddress::storeSelf( char* buf) { buf = writeUint16(buf, OptType); buf = writeUint16(buf, getSize() - 4 ); memcpy(buf, Addr_->getAddr(), 16); buf += 16; buf = writeUint32(buf, PrefLifetime_); buf = writeUint32(buf, ValidLifetime_); buf = storeSubOpt(buf); return buf; }
void cVirtualMemoryAccesser::writeUint(addressNumericValue address, uint value) { // If this change, than the implementation must be changed also ASSERT(sizeof(uint) == sizeof(uint32)); #if defined(XSTL_32BIT) uint8 buf[4]; writeUint32(buf, (uint32)value); #elif defined(XSTL_64BIT) uint8 buf[8]; writeUint64(buf, (uint64)value); #else #error Please add machine word support #endif if (!write(address, buf, sizeof(buf))) XSTL_THROW(cException, EXCEPTION_OUT_OF_RANGE); }
char * TOptVendorSpecInfo::storeSelf( char* buf) { // option-code OPTION_VENDOR_OPTS (2 bytes long) buf = writeUint16(buf, OptType); // option-len size of total option-data buf = writeUint16(buf, getSize()-4); // enterprise-number (4 bytes long) buf = writeUint32(buf, Vendor_); SPtr<TOpt> opt; firstOption(); while (opt = getOption()) { buf = opt->storeSelf(buf); } return buf; }
//SlimStatePacket //============================================================================== //Write //============================================================================== char* writeSlimStatePacket(const struct Packet* p, char* buffer, int ascii) { //BinaryMode if (!ascii) { uint8_t lenght = 9; //PACKET LENGTH buffer = writeUint8(lenght, buffer); //PACKET ID buffer = writeUint8(SlimStatePacketID, buffer); buffer = writeUint32(p->seq, buffer); buffer = writeUint16(p->slimState.leftEncoder, buffer); buffer = writeUint16(p->slimState.rightEncoder, buffer); }//Ascii mode else { buffer = writeHeaderAscii(buffer); buffer = writeUint8Ascii(SlimStatePacketID, buffer); buffer = writeUint32Ascii(p->seq, buffer); buffer = writeUint16Ascii(p->slimState.leftEncoder, buffer); buffer = writeUint16Ascii(p->slimState.rightEncoder, buffer); buffer = writeFooterAscii(buffer); } return buffer; }
bool eaio::writeString(IStream* pOS, const char16_t* pBuffer, size_t nCount, Endian endianSource) { bool bResult(true); if(nCount == kLengthNull) { nCount = 0; // For maximal portability and modularity, we invent our own strlen function here. const char16_t* pCurrent = pBuffer; while(*pCurrent++) ++nCount; } // Embed the string's length at the beginning of the buffer. bResult = writeUint32(pOS, (uint32_t)nCount, endianSource); if(bResult && nCount) bResult = writeUint16(pOS, (const uint16_t*)pBuffer, nCount, endianSource); return bResult; }
void writeFloat(float f, FILE* out) { int32_t intval = (int32_t) ((1<<16)*f); writeUint32(intval, out); }
int main(int argc, char **argv) { if (argc > 1 && !strcmp(argv[1], "--help")) { help(); } if (argc < 4) { usage(); exit(1); } const char *type = argv[1]; const char *dirname = argv[2]; const char *out = argv[3]; uint8_t g_type; if (!strcmp(type, "--grim")) { g_type = GT_GRIM; } else if (!strcmp(type, "--emi")) { g_type = GT_EMI; } else { usage(); exit(1); } DIR *dir = opendir(dirname); if (dir == 0) { printf("Can not open source dir: %s\n", dirname); exit(2); } lab_header head; head.num_entries = 0; head.string_table_size = 0; countFiles(&head, dir, dirname, 0); // printf("%d files, string table of size %d\n", head.num_entries, head.string_table_size); lab_entry *entries = (lab_entry *)malloc(head.num_entries * sizeof(lab_entry)); char *str_table = (char *)malloc(head.string_table_size); if (!str_table || !entries) { printf("Could not allocate memory\n"); exit(3); } rewinddir(dir); uint32_t offset = 16 + head.num_entries * sizeof(lab_entry) + head.string_table_size + 16; createEntries(dir, entries, str_table, dirname, offset); closedir(dir); // Open the output file after we've finished with the dir, so that we're sure // we don't include the lab into itself if it was asked to be created into the same dir. FILE *outfile = fopen(out, "wb"); if (!outfile) { printf("Could not open file %s for writing\n", out); exit(2); } fwrite("LABN", 1, 4, outfile); fwrite("\x00\x00\x01\x00", 1, 4, outfile); //version writeUint32(outfile, head.num_entries); writeUint32(outfile, head.string_table_size); if (g_type == GT_GRIM) { uint32_t s_offset = 0; // First entry of the table has offset 0 for Grim fwrite(&s_offset, 1, 4, outfile); fseek(outfile, -4, SEEK_CUR); } else { // EMI has an offset instead. writeUint32(outfile, 20 + head.num_entries * sizeof(lab_entry) + 0x13d0f); } fwrite(entries, 1, head.num_entries * sizeof(lab_entry), outfile); if (g_type == GT_GRIM) { fwrite(str_table, 1, head.string_table_size, outfile); } else { char *s = (char *)malloc(head.string_table_size); memset(s, 0, head.string_table_size); for (uint32_t j = 0; j < head.string_table_size; j++) { if (str_table[j] != 0) s[j] = str_table[j] ^ 0x96; } fwrite(s, 1, head.string_table_size, outfile); free(s); } uint32_t bufsize = 1024*1024; char *buf = (char *)malloc(bufsize); for (uint32_t i = 0; i < head.num_entries; ++i) { lab_entry &entry = entries[i]; const char *fname = str_table + READ_LE_UINT32(&entry.fname_offset); char *path = appendPath(fname, dirname); FILE *file = fopen(path, "rb"); free(path); uint32_t offset = READ_LE_UINT32(&entry.start); uint32_t size = READ_LE_UINT32(&entry.size); if (size > bufsize) { char *newbuf = (char *)realloc(buf, size); if (!newbuf) { free(buf); printf("Could not allocate memory\n"); exit(3); } bufsize = size; buf = newbuf; } // printf("writing file %s, at offset %d and of size %d\n", fname, offset, size); fread(buf, 1, size, file); fseek(outfile, offset, SEEK_SET); fwrite(buf, 1, size, outfile); fclose(file); } fclose(outfile); free(buf); free(entries); free(str_table); return 0; }
bool eaio::writeFloat(IStream* pOS, const float* value, size_type count, Endian endianDestination) { return writeUint32(pOS, (const uint32_t*)(const char*)value, count, endianDestination); }
bool eaio::writeInt32(IStream* pOS, const int32_t* value, size_type count, Endian endianDestination) { return writeUint32(pOS, (uint32_t*)value, count, endianDestination); }
bool eaio::writeInt32(IStream* pOS, int32_t value, Endian endianDestination) { return writeUint32(pOS, (uint32_t)value, endianDestination); }