Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame^] | 1 | #include "protocol/TBinaryProtocol.h" |
| 2 | using namespace std; |
| 3 | |
| 4 | string TBinaryProtocol::readFunction(TBuf& buf) const { |
| 5 | // Let readString increment the buffer position |
| 6 | return readString(buf); |
| 7 | } |
| 8 | |
| 9 | string TBinaryProtocol::writeFunction(const string& name, |
| 10 | const string& args) const{ |
| 11 | return writeString(name) + args; |
| 12 | } |
| 13 | |
| 14 | map<uint32_t, TBuf> TBinaryProtocol::readStruct(TBuf& buf) const { |
| 15 | map<uint32_t, TBuf> fieldMap; |
| 16 | |
| 17 | if (buf.len < 4) { |
| 18 | return fieldMap; |
| 19 | } |
| 20 | uint32_t total_size = readU32(buf); |
| 21 | if (buf.len < total_size) { |
| 22 | // Data looks corrupt, we don't have that much, we will try to read what |
| 23 | // we can but be sure not to go over |
| 24 | total_size = buf.len; |
| 25 | } |
| 26 | |
| 27 | // Field headers are 8 bytes, 4 byte fid + 4 byte length |
| 28 | while (total_size > 0 && buf.len > 8) { |
| 29 | uint32_t fid = readU32(buf); |
| 30 | uint32_t flen = readU32(buf); |
| 31 | if (flen > buf.len) { |
| 32 | // flen corrupt, there isn't that much data left |
| 33 | break; |
| 34 | } |
| 35 | fieldMap.insert(make_pair(fid, TBuf(buf.data, flen))); |
| 36 | buf.data += flen; |
| 37 | buf.len -= flen; |
| 38 | total_size -= 8 + flen; |
| 39 | } |
| 40 | |
| 41 | return fieldMap; |
| 42 | } |
| 43 | |
| 44 | string TBinaryProtocol::writeStruct(const map<uint32_t,string>& s) const { |
| 45 | string result = ""; |
| 46 | map<uint32_t,string>::const_iterator s_iter; |
| 47 | for (s_iter = s.begin(); s_iter != s.end(); ++s_iter) { |
| 48 | result += writeU32(s_iter->first); |
| 49 | result += writeU32(s_iter->second.size()); |
| 50 | result += s_iter->second; |
| 51 | } |
| 52 | return writeU32(result.size()) + result; |
| 53 | } |
| 54 | |
| 55 | string TBinaryProtocol::readString(TBuf& buf) const { |
| 56 | uint32_t len = readU32(buf); |
| 57 | if (len == 0) { |
| 58 | return ""; |
| 59 | } |
| 60 | string result((const char*)(buf.data), len); |
| 61 | buf.data += len; |
| 62 | buf.len -= len; |
| 63 | return result; |
| 64 | } |
| 65 | |
| 66 | uint8_t TBinaryProtocol::readByte(TBuf& buf) const { |
| 67 | if (buf.len == 0) { |
| 68 | return 0; |
| 69 | } |
| 70 | uint8_t result = (uint8_t)buf.data[0]; |
| 71 | buf.data += 1; |
| 72 | buf.len -= 1; |
| 73 | return result; |
| 74 | } |
| 75 | |
| 76 | uint32_t TBinaryProtocol::readU32(TBuf& buf) const { |
| 77 | if (buf.len < 4) { |
| 78 | return 0; |
| 79 | } |
| 80 | uint32_t result = *(uint32_t*)buf.data; |
| 81 | buf.data += 4; |
| 82 | buf.len -= 4; |
| 83 | return result; |
| 84 | } |
| 85 | |
| 86 | int32_t TBinaryProtocol::readI32(TBuf& buf) const { |
| 87 | if (buf.len < 4) { |
| 88 | return 0; |
| 89 | } |
| 90 | int32_t result = *(int32_t*)buf.data; |
| 91 | buf.data += 4; |
| 92 | buf.len -= 4; |
| 93 | return result; |
| 94 | } |
| 95 | |
| 96 | uint64_t TBinaryProtocol::readU64(TBuf& buf) const { |
| 97 | if (buf.len < 8) { |
| 98 | return 0; |
| 99 | } |
| 100 | uint64_t result = *(uint64_t*)buf.data; |
| 101 | buf.data += 8; |
| 102 | buf.len -= 8; |
| 103 | return result; |
| 104 | } |
| 105 | |
| 106 | int64_t TBinaryProtocol::readI64(TBuf& buf) const { |
| 107 | if (buf.len < 8) { |
| 108 | return 0; |
| 109 | } |
| 110 | int64_t result = *(int64_t*)buf.data; |
| 111 | buf.data += 8; |
| 112 | buf.len -= 8; |
| 113 | return result; |
| 114 | } |
| 115 | |
| 116 | string TBinaryProtocol::writeString(const string& str) const { |
| 117 | uint32_t size = str.size(); |
| 118 | string result = string((const char*)&size, 4); |
| 119 | return result + str; |
| 120 | } |
| 121 | |
| 122 | string TBinaryProtocol::writeByte(const uint8_t byte) const { |
| 123 | return string((const char*)&byte, 1); |
| 124 | } |
| 125 | |
| 126 | string TBinaryProtocol::writeU32(const uint32_t u32) const { |
| 127 | return string((const char*)&u32, 4); |
| 128 | } |
| 129 | |
| 130 | string TBinaryProtocol::writeI32(int32_t i32) const { |
| 131 | return string((const char*)&i32, 4); |
| 132 | } |
| 133 | |
| 134 | string TBinaryProtocol::writeU64(uint64_t u64) const { |
| 135 | return string((const char*)&u64, 8); |
| 136 | } |
| 137 | |
| 138 | string TBinaryProtocol::writeI64(int64_t i64) const { |
| 139 | return string((const char*)&i64, 8); |
| 140 | } |