1 #ifndef ISPN_HOTROD_JBASICMARSHALLER_H 2 #define ISPN_HOTROD_JBASICMARSHALLER_H 21 class JBasicMarshallerHelper {
24 enum {MARSHALL_VERSION = 0x03, SMALL_STRING = 0x3e, MEDIUM_STRING = 0x3f, INTEGER=0x4b};
25 static void noRelease(std::vector<char>*) { }
26 static void release(std::vector<char> *buf) {
29 template <
class T>
static T
unmarshall(
char *);
32 template <>
inline std::string JBasicMarshallerHelper::unmarshall(
char *b) {
33 if (b[0]!=JBasicMarshallerHelper::MARSHALL_VERSION)
34 throw Exception(
"JBasicMarshallerHelper: bad version");
35 if ( (b[1]!=JBasicMarshallerHelper::SMALL_STRING) && (b[1]!=JBasicMarshallerHelper::MEDIUM_STRING) )
36 throw Exception(
"JBasicMarshallerHelper: not a string");
37 return std::string(b+3,b[2]);
40 template <>
inline int JBasicMarshallerHelper::unmarshall(
char *b) {
41 if (b[0]!=JBasicMarshallerHelper::MARSHALL_VERSION)
42 throw Exception(
"JBasicMarshallerHelper: bad version");
43 if (b[1]!=JBasicMarshallerHelper::INTEGER)
44 throw Exception(
"JBasicMarshallerHelper: not a integer");
46 for (
int i = 0; i < 4 ; i++) {
48 result ^= (int) *(b+i+2) & 0xFF;
60 void marshall(
const std::string& s, std::vector<char>& b) {
61 if (s.size() <= 0x100) {
68 std::string*
unmarshall(
const std::vector<char>& b) {
69 std::string* s =
new std::string(b.data()+3, b.size()-3);
73 static std::string addPreamble(std::string &s) {
77 res = addPreambleSmall(s);
81 res = addPreambleMedium(s);
87 static std::string addPreambleSmall(std::string& s) {
88 std::string res(
"\x03\x3e");
89 res.append(1, (
char)s.size());
93 static std::string addPreambleMedium(std::string& s) {
94 std::string res(
"\x03\x3f");
95 res.append(1, (
char)(s.size()>>8));
96 res.append(1, (
char)(s.size()&& 0xff));
101 void marshallSmall(
const std::string& s, std::vector<char>& b) {
102 b.resize(s.size() + 3);
103 char* buf = b.data();
105 buf[0] = JBasicMarshallerHelper::MARSHALL_VERSION;
106 buf[1] = JBasicMarshallerHelper::SMALL_STRING;
107 buf[2] = (char)s.size();
108 memcpy(buf + 3, s.data(), s.size());
111 void marshallMedium(
const std::string& s, std::vector<char>& b) {
112 b.resize(s.size() + 4);
113 char* buf = b.data();
115 buf[0] = JBasicMarshallerHelper::MARSHALL_VERSION;
116 buf[1] = JBasicMarshallerHelper::MEDIUM_STRING;
117 buf[2] = (char)(s.size() >> 8);
118 buf[3] = s.size() & 0xff;
120 memcpy(buf + 4, s.data(), s.size());
127 void marshall(
const int& s, std::vector<char>& b) {
130 buf[0] = JBasicMarshallerHelper::MARSHALL_VERSION;
131 buf[1] = JBasicMarshallerHelper::INTEGER;
132 for (
int i = 0 ; i < 4 ; i++) {
133 buf[5-i] = (char) ((s) >> (8*i));
135 b.assign(buf, buf+6);
139 for (
int i = 0; i < 4 ; i++) {
141 result ^= (int) *(b.data()+i+2) & 0xFF;
143 int* s =
new int(result);
virtual T * unmarshall(const std::vector< char > &buff)=0
Definition: JBasicMarshaller.h:18
virtual void marshall(const T &obj, std::vector< char > &buff)=0
Definition: AuthenticationConfiguration.h:10
Definition: Marshaller.h:12