00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "opensync.h"
00022 #include "opensync_internals.h"
00023
00024 #include "opensync_marshal.h"
00025 #include "opensync_marshal_private.h"
00026
00027 OSyncMarshal *osync_marshal_sized_new(unsigned int size, OSyncError **error)
00028 {
00029 OSyncMarshal *marshal = osync_try_malloc0(sizeof(OSyncMarshal), error);
00030 if (!marshal)
00031 return NULL;
00032
00033 marshal->ref_count = 1;
00034
00035 if (size > 0)
00036 marshal->buffer = g_byte_array_sized_new( size );
00037 else
00038 marshal->buffer = g_byte_array_new();
00039
00040 marshal->buffer_read_pos = 0;
00041
00042 return marshal;
00043 }
00044
00045 OSyncMarshal *osync_marshal_new(OSyncError **error)
00046 {
00047 return osync_marshal_sized_new(0, error);
00048 }
00049
00050 OSyncMarshal *osync_marshal_ref(OSyncMarshal *marshal)
00051 {
00052 g_atomic_int_inc(&(marshal->ref_count));
00053
00054 return marshal;
00055 }
00056
00057 void osync_marshal_unref(OSyncMarshal *marshal)
00058 {
00059 if (g_atomic_int_dec_and_test(&(marshal->ref_count))) {
00060
00061 g_byte_array_free(marshal->buffer, TRUE);
00062
00063 osync_free(marshal);
00064 }
00065 }
00066
00067 unsigned int osync_marshal_get_marshal_size(OSyncMarshal *marshal)
00068 {
00069 osync_assert(marshal);
00070 return marshal->buffer->len;
00071 }
00072
00073 void osync_marshal_set_marshal_size(OSyncMarshal *marshal, unsigned int size)
00074 {
00075 osync_assert(marshal);
00076 marshal->buffer->len = size;
00077 }
00078
00079 void osync_marshal_get_buffer(OSyncMarshal *marshal, char **data, unsigned int *size)
00080 {
00081 osync_assert(marshal);
00082
00083 if (data)
00084 *data = (char *)marshal->buffer->data;
00085
00086 if (size)
00087 *size = marshal->buffer->len;
00088 }
00089
00090 void osync_marshal_write_int(OSyncMarshal *marshal, int value)
00091 {
00092 g_byte_array_append( marshal->buffer, (unsigned char*)&value, sizeof( int ) );
00093 }
00094
00095 void osync_marshal_write_uint(OSyncMarshal *marshal, unsigned int value)
00096 {
00097 g_byte_array_append( marshal->buffer, (unsigned char*)&value, sizeof( unsigned int ) );
00098 }
00099
00100 void osync_marshal_write_long_long_int(OSyncMarshal *marshal, long long int value)
00101 {
00102 g_byte_array_append( marshal->buffer, (unsigned char*)&value, sizeof( long long int ) );
00103 }
00104
00105 void osync_marshal_write_string(OSyncMarshal *marshal, const char *value)
00106 {
00107 int length = 0;
00108 if (value == NULL) {
00109 length = -1;
00110 g_byte_array_append( marshal->buffer, (unsigned char*)&length, sizeof( int ) );
00111 } else {
00112 int length = strlen( value ) + 1;
00113 g_byte_array_append( marshal->buffer, (unsigned char*)&length, sizeof( int ) );
00114 g_byte_array_append( marshal->buffer, (unsigned char*)value, length );
00115 }
00116 }
00117
00118 void osync_marshal_write_data(OSyncMarshal *marshal, const void *value, int size)
00119 {
00120
00121 g_byte_array_append( marshal->buffer, value, size );
00122 }
00123
00124 void osync_marshal_write_buffer(OSyncMarshal *marshal, const void *value, int size)
00125 {
00126
00127
00128 osync_marshal_write_int(marshal, size);
00129 if (size > 0)
00130 osync_marshal_write_data(marshal, value, size);
00131 }
00132
00133 void osync_marshal_read_int(OSyncMarshal *marshal, int *value)
00134 {
00135 osync_assert(marshal->buffer->len >= marshal->buffer_read_pos + sizeof(int));
00136
00137 memcpy(value, &(marshal->buffer->data[ marshal->buffer_read_pos ]), sizeof(int));
00138 marshal->buffer_read_pos += sizeof(int);
00139 }
00140
00141 void osync_marshal_read_uint(OSyncMarshal *marshal, unsigned int *value)
00142 {
00143 osync_assert(marshal->buffer->len >= marshal->buffer_read_pos + sizeof(unsigned int));
00144
00145 memcpy(value, &(marshal->buffer->data[ marshal->buffer_read_pos ]), sizeof(unsigned int));
00146 marshal->buffer_read_pos += sizeof(unsigned int);
00147 }
00148
00149 void osync_marshal_read_long_long_int(OSyncMarshal *marshal, long long int *value)
00150 {
00151 osync_assert(marshal->buffer->len >= marshal->buffer_read_pos + sizeof(long long int));
00152
00153 memcpy(value, &(marshal->buffer->data[ marshal->buffer_read_pos ]), sizeof(long long int));
00154 marshal->buffer_read_pos += sizeof(long long int);
00155 }
00156
00157
00158 void osync_marshal_read_const_string(OSyncMarshal *marshal, char **value)
00159 {
00160 int length = 0;
00161 osync_marshal_read_int(marshal, &length);
00162
00163 if (length == -1) {
00164 *value = NULL;
00165 return;
00166 }
00167
00168 osync_assert(marshal->buffer->len >= marshal->buffer_read_pos + length);
00169 *value = (char *)&(marshal->buffer->data[marshal->buffer_read_pos]);
00170 marshal->buffer_read_pos += length;
00171 }
00172
00173 void osync_marshal_read_string(OSyncMarshal *marshal, char **value)
00174 {
00175 int length = 0;
00176 osync_marshal_read_int(marshal, &length);
00177
00178 if (length == -1) {
00179 *value = NULL;
00180 return;
00181 }
00182
00183 osync_assert(marshal->buffer->len >= marshal->buffer_read_pos + length);
00184
00185
00186 *value = (char*) osync_try_malloc0(length, NULL);
00187 if (!*value)
00188 return;
00189
00190 memcpy(*value, &(marshal->buffer->data[ marshal->buffer_read_pos ]), length );
00191 marshal->buffer_read_pos += length;
00192 }
00193
00194 void osync_marshal_read_const_data(OSyncMarshal *marshal, void **value, int size)
00195 {
00196 osync_assert(marshal->buffer->len >= marshal->buffer_read_pos + size);
00197
00198 *value = &(marshal->buffer->data[marshal->buffer_read_pos]);
00199 marshal->buffer_read_pos += size;
00200 }
00201
00202 void osync_marshal_read_data(OSyncMarshal *marshal, void *value, int size)
00203 {
00204 osync_assert(marshal->buffer->len >= marshal->buffer_read_pos + size);
00205
00206 memcpy(value, &(marshal->buffer->data[ marshal->buffer_read_pos ]), size );
00207 marshal->buffer_read_pos += size;
00208 }
00209
00210 void osync_marshal_read_buffer(OSyncMarshal *marshal, void **value, int *size)
00211 {
00212
00213 osync_marshal_read_int(marshal, size);
00214
00215 if (*size > 0) {
00216 *value = g_malloc0(*size);
00217 osync_marshal_read_data(marshal, *value, *size);
00218 }
00219 }
00220