2 Copyright (C) 2000-2003 SKYRIX Software AG
4 This file is part of OGo
6 OGo is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with OGo; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
27 # include <sys/types.h>
29 # if HAVE_SYS_SOCKET_H
30 # include <sys/socket.h>
32 # if HAVE_NETINET_IN_H
33 # include <netinet/in.h>
35 # include <arpa/inet.h>
39 #include "NGStream+serialization.h"
42 # include <objc/objc-class.h>
45 @implementation NGStream(serialization)
49 - (void)serializeChar:(char)_value {
50 NGStreamSerializeObjC(self, &_value, @encode(char), nil);
52 - (void)serializeShort:(short)_value {
53 NGStreamSerializeObjC(self, &_value, @encode(short), nil);
55 - (void)serializeInt:(int)_value {
56 NGStreamSerializeObjC(self, &_value, @encode(int), nil);
58 - (void)serializeLong:(long)_value {
59 NGStreamSerializeObjC(self, &_value, @encode(long), nil);
62 - (void)serializeFloat:(float)_value {
63 NGStreamSerializeObjC(self, &_value, @encode(float), nil);
65 - (void)serializeDouble:(double)_value {
66 NGStreamSerializeObjC(self, &_value, @encode(double), nil);
68 - (void)serializeLongLong:(long long)_value {
69 NGStreamSerializeObjC(self, &_value, @encode(long long), nil);
72 - (void)serializeCString:(const char *)_value {
73 NGStreamSerializeObjC(self, &_value, @encode(char *), nil);
77 - (void)serializeDataAt:(const void*)_value ofObjCType:(const char*)_type
78 context:(id<NSObjCTypeSerializationCallBack>)_callback
80 NGStreamSerializeObjC(self, _value, _type, _callback);
86 - (char)deserializeChar {
88 NGStreamDeserializeObjC(self, &c, @encode(char), nil);
91 - (short)deserializeShort {
93 NGStreamDeserializeObjC(self, &s, @encode(short), nil);
96 - (int)deserializeInt {
98 NGStreamDeserializeObjC(self, &i, @encode(int), nil);
101 - (long)deserializeLong {
103 NGStreamDeserializeObjC(self, &l, @encode(long), nil);
106 - (float)deserializeFloat {
108 NGStreamDeserializeObjC(self, &f, @encode(float), nil);
112 - (double)deserializeDouble {
114 NGStreamDeserializeObjC(self, &d, @encode(double), nil);
117 - (long long)deserializeLongLong {
119 NGStreamDeserializeObjC(self, &l, @encode(long long), nil);
123 - (char *)deserializeCString {
125 NGStreamDeserializeObjC(self, &result, @encode(char *), nil);
130 - (void)deserializeDataAt:(void *)_value ofObjCType:(const char *)_type
131 context:(id<NSObjCTypeSerializationCallBack>)_callback
133 NGStreamDeserializeObjC(self, _value, _type, _callback);
139 void NGStreamSerializeObjC(id<NGStream> self,
140 const void *_value, const char *_type,
142 id<NSObjCTypeSerializationCallBack> _callback
151 [_callback serializeObjectAt:(id *)_value
153 intoData:(NSMutableData *)self];
157 const char *cstr = *(char **)_value;
158 int len = cstr ? (int)strlen(cstr) : -1;
160 NGStreamSerializeObjC(self, &len, @encode(int), _callback);
162 [self safeWriteBytes:cstr count:len];
168 int i, offset, itemSize, count;
170 count = atoi(_type + 1); // skip '[' and get dimension
172 while (isdigit((int)*++_type)) ; // skip '[' and dimension
173 itemSize = objc_sizeof_type(_type);
175 for (i = offset = 0; i < count; i++, offset += itemSize)
176 NGStreamSerializeObjC(self, (char *)_value + offset, _type, _callback);
183 while ((*_type != _C_STRUCT_E) && (*_type++ != '=')) ; // skip '<name>='
186 NGStreamSerializeObjC(self, (char *)_value + offset, _type, _callback);
188 offset += objc_sizeof_type(_type);
189 _type = objc_skip_typespec(_type);
191 if (*_type != _C_STRUCT_E) {
192 int align, remainder;
194 align = objc_alignof_type(_type);
195 if ((remainder = offset % align))
196 offset += align - remainder;
198 else // done with structure
205 NGStreamSerializeObjC(self, *(char **)_value, _type + 1, _callback);
210 [self safeWriteBytes:_value count:1];
215 short netValue = htons(*(short *)_value);
216 [self safeWriteBytes:&netValue count:2];
222 int netValue = htonl(*(int *)_value);
223 [self safeWriteBytes:&netValue count:4];
229 long netValue = htonl(*(long *)_value);
230 [self safeWriteBytes:&netValue count:sizeof(long)];
239 fc.value = *(float *)_value;
240 fc.ul = htonl(fc.ul);
241 [self safeWriteBytes:&fc count:sizeof(unsigned long)];
245 [self safeWriteBytes:_value count:8];
250 NSCAssert1(0, @"unsupported C type %s ..", _type);
255 void NGStreamDeserializeObjC(id<NGStream> self,
256 void *_value, const char *_type,
258 id<NSObjCTypeSerializationCallBack> _callback
264 if ((_value == NULL) || (_type == NULL))
270 [_callback deserializeObjectAt:(id *)_value
272 fromData:(NSData *)self
276 case _C_CHARPTR: { // malloced C-string
279 NGStreamDeserializeObjC(self, &len, @encode(int), _callback);
281 if (len == -1) // NULL-string
282 *(char **)_value = NULL;
286 #if NeXT_Foundation_LIBRARY
287 result = NSZoneMalloc(NULL, len + 1);
289 result = NSZoneMallocAtomic(NULL, len + 1);
293 if (len > 0) [self safeReadBytes:result count:len];
294 *(char **)_value = result;
300 int i, offset, itemSize, count;
302 count = atoi(_type + 1); // skip '[' and get dimension
304 while (isdigit((int)*++_type)) ; // skip '[' and dimension
305 itemSize = objc_sizeof_type(_type);
307 for (i = offset = 0; i < count; i++, offset += itemSize)
308 NGStreamDeserializeObjC(self, (char *)_value + offset, _type, _callback);
316 while ((*_type != _C_STRUCT_E) && (*_type++ != '=')) ; // skip '<name>='
319 NGStreamDeserializeObjC(self, (char *)_value + offset, _type, _callback);
321 offset += objc_sizeof_type(_type);
322 _type = objc_skip_typespec(_type);
324 if (*_type != _C_STRUCT_E) {
325 int align, remainder;
327 align = objc_alignof_type(_type);
328 if ((remainder = offset % align))
329 offset += align - remainder;
331 else // done with structure
338 // skip '^', type of the value the ptr points to
341 result = NSZoneMalloc(NULL, objc_sizeof_type(_type + 1));
343 NGStreamDeserializeObjC(self, result, _type + 1, _callback);
345 *(char **)_value = result;
353 [self safeReadBytes:_value count:1];
358 [self safeReadBytes:_value count:2];
359 *(short *)_value = ntohs(*(short *)_value);
364 [self safeReadBytes:_value count:4];
365 *(int *)_value = ntohl(*(int *)_value);
370 [self safeReadBytes:_value count:4];
371 *(long *)_value = ntohl(*(long *)_value);
375 [self safeReadBytes:_value count:4];
376 *(long *)_value = ntohl(*(long *)_value);
380 [self safeReadBytes:_value count:8];
385 NSLog(@"unsupported C type %s ..", _type);
390 void __link_NGStream_serialization(void) {
391 __link_NGStream_serialization();