Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals  

Marshal.c

Go to the documentation of this file.
00001 /*
00002  *    gui - [gega user interface] the flexible solution for user interface problems
00003  *    Copyright (C) 2002  Gergely Gati
00004  *
00005  *    This program is free software; you can redistribute it and/or modify
00006  *    it under the terms of the GNU General Public License as published by
00007  *    the Free Software Foundation; version 2 of the License.
00008  *
00009  *    This program is distributed in the hope that it will be useful,
00010  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *    GNU General Public License for more details.
00013  *
00014  *    You should have received a copy of the GNU General Public License
00015  *    along with this program; if not, write to the Free Software
00016  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017  *
00018  *    Gergely Gati
00019  *      email:           g.gati@freemail.hu
00020  *      AIM screenname:  GatiGergely
00021  *      ICQ number:      93131690
00022  *
00023  */
00024 /* Marshal.c */
00025 
00026 #include <string.h>
00027 
00028 #include "debug.h"
00029 #include "types.h"
00030 #include "Tag.h"
00031 #include "Endian.h"
00032 #include "Memory.h"
00033 #include "Marshal.h"
00034 
00035 
00036 inline u8 *marshal_PutLong(u8 *msg, u32 value)
00037 {
00038   debug_Begin();
00039 
00040   *((u32 *)msg)=value;
00041 
00042   debug_End();
00043 
00044   return(msg+4);
00045 }
00046 inline u8 *marshal_PutLongC(u8 *msg, u32 value)
00047 {
00048   debug_Begin();
00049 
00050   *((u32 *)msg)=endian_ToAlien32(value);
00051 
00052   debug_End();
00053 
00054   return(msg+4);
00055 }
00056 
00057 
00058 inline u8 *marshal_PutString(u8 *msg, char *value)
00059 {
00060   u8 *ret;
00061   u32 len;
00062 
00063   debug_Begin();
00064 
00065   len=(u32)strlen(value)+1;
00066   ret=msg+len+4;
00067   *((u32 *)msg)=len;
00068   strcpy(msg+4,value);
00069 
00070   debug_End();
00071 
00072   return(ret);
00073 }
00074 inline u8 *marshal_PutStringC(u8 *msg, char *value)
00075 {
00076   u8 *ret;
00077   u32 len;
00078 
00079   debug_Begin();
00080 
00081   len=(u32)strlen(value)+1;
00082   ret=msg+len+4;
00083   *((u32 *)msg)=endian_ToAlien32(len);
00084   strcpy(msg+4,value);
00085 
00086   debug_End();
00087 
00088   return(ret);
00089 }
00090 
00091 
00092 inline u8 *marshal_PutBinary(u8 *msg, u8 *value, u32 len)
00093 {
00094   u8 *ret;
00095 
00096   debug_Begin();
00097 
00098   ret=msg+len+4;
00099   *((u32 *)msg)=len;
00100   if(len>0) memcpy(msg+4,value,len);
00101 
00102   debug_End();
00103 
00104   return(ret);
00105 }
00106 inline u8 *marshal_PutBinaryC(u8 *msg, u8 *value, u32 len)
00107 {
00108   u8 *ret;
00109 
00110   debug_Begin();
00111 
00112   ret=msg+len+4;
00113   *((u32 *)msg)=endian_ToAlien32(len);
00114   if(len>0) memcpy(msg+4,value,len);
00115 
00116   debug_End();
00117 
00118   return(ret);
00119 }
00120 
00121 
00122 u8 *marshal_PutTaglist(u8 *msg, tag *taglist)
00123 {
00124   tag *tags;
00125   u8 *st;
00126   int tag_count;
00127 
00128   debug_Begin();
00129 
00130   st=msg;
00131   for(msg+=4,tag_count=0,tags=taglist;tags->Name!=TAG_DONE;tags=tag_GetNextTag(tags),tag_count++)
00132   {
00133     msg=marshal_PutLong(msg,tags->Name);
00134     if((tags->Name&TAGT_STRING)!=0)
00135     {
00136       msg=marshal_PutString(msg,(char *)tags->Data);
00137     }
00138     else if((tags->Name&TAGT_RECT)!=0)
00139     {
00140       msg=marshal_PutLong(msg,((Rect_t *)tags->Data)->left);
00141       msg=marshal_PutLong(msg,((Rect_t *)tags->Data)->top);
00142       msg=marshal_PutLong(msg,((Rect_t *)tags->Data)->width);
00143       msg=marshal_PutLong(msg,((Rect_t *)tags->Data)->height);
00144     }
00145     else if((tags->Name&TAGT_VECTOR)!=0)
00146     {
00147       msg=marshal_PutLong(msg,((Vector_t *)tags->Data)->size);
00148       msg=marshal_PutBinary(msg,((Vector_t *)tags->Data)->data,((Vector_t *)tags->Data)->size);
00149     }
00150     else if((tags->Name&TAGT_PIXELS)!=0)
00151     {
00152       msg=marshal_PutLong(msg,((Vector_t *)tags->Data)->size);
00153       msg=marshal_PutBinary(msg,((Vector_t *)tags->Data)->data,((Vector_t *)tags->Data)->size);
00154     }
00155     else msg=marshal_PutLong(msg,tags->Data);
00156   }
00157   marshal_PutLong(st,(u32)tag_count);
00158 
00159   debug_End();
00160 
00161   return(msg);
00162 }
00163 u8 *marshal_PutTaglistC(u8 *msg, tag *taglist)
00164 {
00165   tag *tags;
00166   u8 *st;
00167   int tag_count;
00168 
00169   debug_Begin();
00170 
00171   st=msg;
00172   for(msg+=4,tag_count=0,tags=taglist;tags->Name!=TAG_DONE;tags=tag_GetNextTag(tags),tag_count++)
00173   {
00174     msg=marshal_PutLongC(msg,tags->Name);
00175     if((tags->Name&TAGT_STRING)!=0)
00176     {
00177       msg=marshal_PutStringC(msg,(char *)tags->Data);
00178     }
00179     else if((tags->Name&TAGT_RECT)!=0)
00180     {
00181       msg=marshal_PutLongC(msg,((Rect_t *)tags->Data)->left);
00182       msg=marshal_PutLongC(msg,((Rect_t *)tags->Data)->top);
00183       msg=marshal_PutLongC(msg,((Rect_t *)tags->Data)->width);
00184       msg=marshal_PutLongC(msg,((Rect_t *)tags->Data)->height);
00185     }
00186     else if((tags->Name&TAGT_VECTOR)!=0)
00187     {
00188       msg=marshal_PutBinaryC(msg,((Vector_t *)tags->Data)->data,((Vector_t *)tags->Data)->size);
00189     }
00190     else if((tags->Name&TAGT_PIXELS)!=0)
00191     {
00192       msg=marshal_PutBinaryC(msg,((Vector_t *)tags->Data)->data,((Vector_t *)tags->Data)->size);
00193     }
00194     else msg=marshal_PutLongC(msg,tags->Data);
00195   }
00196   marshal_PutLongC(st,(u32)tag_count);
00197 
00198   debug_End();
00199 
00200   return(msg);
00201 }
00202 
00203 
00204 inline u32 marshal_GetLong(u8 **msg)
00205 {
00206   u32 ret;
00207 
00208   debug_Begin();
00209 
00210   ret=*((u32 *)*msg);
00211   *msg+=4;
00212 
00213   debug_End();
00214 
00215   return(ret);
00216 }
00217 inline u32 marshal_GetLongC(u8 **msg)
00218 {
00219   u32 ret;
00220 
00221   debug_Begin();
00222 
00223   ret=endian_FromAlien32(*((u32 *)*msg));
00224   *msg+=4;
00225 
00226   debug_End();
00227 
00228   return(ret);
00229 }
00230 
00231 
00232 inline char *marshal_GetString(u8 **msg)
00233 {
00234   char *str;
00235   u32 len;
00236 
00237   debug_Begin();
00238 
00239   len=*((u32 *)(*msg));
00240   if(NULL!=(str=mem_malloc(len)))
00241   {
00242     memcpy(str,(*msg)+4,len);
00243     str[len-1]='\0';
00244   }
00245   *msg+=len+4;
00246 
00247   debug_End();
00248 
00249   return(str);
00250 }
00251 inline char *marshal_GetStringC(u8 **msg)
00252 {
00253   char *str;
00254   u32 len;
00255 
00256   debug_Begin();
00257 
00258   len=endian_FromAlien32(*((u32 *)(*msg)));
00259   if(NULL!=(str=mem_malloc(len)))
00260   {
00261     memcpy(str,(*msg)+4,len);
00262     str[len-1]='\0';
00263   }
00264   *msg+=len+4;
00265 
00266   debug_End();
00267 
00268   return(str);
00269 }
00270 
00271 
00272 inline u8 *marshal_GetBinary(u8 **msg, u32 *len)
00273 {
00274   u8 *data;
00275   u32 blen;
00276 
00277   debug_Begin();
00278 
00279   blen=*((u32 *)*msg);
00280   if(NULL!=(data=mem_malloc(blen)))
00281   {
00282     memcpy(data,(*msg)+4,blen);
00283     if(len!=NULL) *len=blen;
00284   }
00285   *msg+=blen+4;
00286 
00287   debug_End();
00288 
00289   return(data);
00290 }
00291 inline u8 *marshal_GetBinaryC(u8 **msg, u32 *len)
00292 {
00293   u8 *data;
00294   u32 blen;
00295 
00296   debug_Begin();
00297 
00298   blen=endian_FromAlien32(*((u32 *)*msg));
00299   if(NULL!=(data=mem_malloc(blen)))
00300   {
00301     memcpy(data,(*msg)+4,blen);
00302     if(len!=NULL) *len=blen;
00303   }
00304   *msg+=blen+4;
00305 
00306   debug_End();
00307 
00308   return(data);
00309 }
00310 
00311 
00312 u32 marshal_GetBinaryLen(u8 **msg)
00313 {
00314   u32 blen;
00315 
00316   debug_Begin();
00317 
00318   blen=*((u32 *)*msg);
00319 
00320   debug_End();
00321 
00322   return(blen);
00323 }
00324 u32 marshal_GetBinaryLenC(u8 **msg)
00325 {
00326   u32 blen;
00327 
00328   debug_Begin();
00329 
00330   blen=endian_FromAlien32(*((u32 *)*msg));
00331 
00332   debug_End();
00333 
00334   return(blen);
00335 }
00336 
00337 
00338 
00339 tag *marshal_GetTaglist(u8 **msg)
00340 {
00341   tag *ret,*tags;
00342   int tag_count,i;
00343 
00344   debug_Begin();
00345 
00346   tag_count=(int)marshal_GetLong(msg);
00347   if(NULL!=(ret=mem_malloc((tag_count+1)*sizeof(tag))))
00348   {
00349     for(tags=ret,i=0;i<tag_count;i++,tags++)
00350     {
00351       tags->Name=marshal_GetLong(msg);
00352       if((tags->Name&TAGT_STRING)!=0)
00353       {
00354         tags->Data=(u32)marshal_GetString(msg);
00355       }
00356       if((tags->Name&TAGT_RECT)!=0)
00357       {
00358         if(0L!=(tags->Data=(u32)mem_malloc(sizeof(Rect_t))))
00359         {
00360           ((Rect_t *)tags->Data)->left=marshal_GetLong(msg);
00361           ((Rect_t *)tags->Data)->top=marshal_GetLong(msg);
00362           ((Rect_t *)tags->Data)->width=marshal_GetLong(msg);
00363           ((Rect_t *)tags->Data)->height=marshal_GetLong(msg);
00364         }
00365       }
00366       if((tags->Name&TAGT_VECTOR)!=0)
00367       {
00368         if(0L!=(tags->Data=(u32)mem_malloc(sizeof(Vector_t))))
00369         {
00370           ((Vector_t *)tags->Data)->data=marshal_GetBinary(msg,(u32 *)&((Vector_t *)tags->Data)->size);
00371         }
00372       }
00373       if((tags->Name&TAGT_PIXELS)!=0)
00374       {
00375         if(0L!=(tags->Data=(u32)mem_malloc(sizeof(Vector_t))))
00376         {
00377           ((Vector_t *)tags->Data)->data=marshal_GetBinary(msg,(u32 *)&((Vector_t *)tags->Data)->size);
00378         }
00379       }
00380       else tags->Data=marshal_GetLong(msg);
00381     }
00382     tags->Name=TAG_DONE;
00383   }
00384 
00385   debug_End();
00386 
00387   return(ret);
00388 }
00389 tag *marshal_GetTaglistC(u8 **msg)
00390 {
00391   tag *ret,*tags;
00392   int tag_count,i;
00393 
00394   debug_Begin();
00395 
00396   tag_count=(int)marshal_GetLongC(msg);
00397   if(NULL!=(ret=mem_malloc((tag_count+1)*sizeof(tag))))
00398   {
00399     for(tags=ret,i=0;i<tag_count;i++,tags++)
00400     {
00401       tags->Name=marshal_GetLongC(msg);
00402       if((tags->Name&TAGT_STRING)!=0)
00403       {
00404         tags->Data=(u32)marshal_GetStringC(msg);
00405       }
00406       if((tags->Name&TAGT_RECT)!=0)
00407       {
00408         if(0L!=(tags->Data=(u32)mem_malloc(sizeof(Rect_t))))
00409         {
00410           ((Rect_t *)tags->Data)->left=marshal_GetLongC(msg);
00411           ((Rect_t *)tags->Data)->top=marshal_GetLongC(msg);
00412           ((Rect_t *)tags->Data)->width=marshal_GetLongC(msg);
00413           ((Rect_t *)tags->Data)->height=marshal_GetLongC(msg);
00414         }
00415       }
00416       if((tags->Name&TAGT_VECTOR)!=0)
00417       {
00418         if(0L!=(tags->Data=(u32)mem_malloc(sizeof(Vector_t))))
00419         {
00420           ((Vector_t *)tags->Data)->data=marshal_GetBinaryC(msg,(u32 *)&((Vector_t *)tags->Data)->size);
00421         }
00422       }
00423       if((tags->Name&TAGT_PIXELS)!=0)
00424       {
00425         if(0L!=(tags->Data=(u32)mem_malloc(sizeof(Vector_t))))
00426         {
00427           ((Vector_t *)tags->Data)->data=marshal_GetBinaryC(msg,(u32 *)&((Vector_t *)tags->Data)->size);
00428         }
00429       }
00430       else tags->Data=marshal_GetLongC(msg);
00431     }
00432     tags->Name=TAG_DONE;
00433   }
00434 
00435   debug_End();
00436 
00437   return(ret);
00438 }
00439 
00440 
00441 void marshal_FreeTaglist(tag *taglist)
00442 {
00443   tag *tags;
00444 
00445   debug_Begin();
00446 
00447   if(taglist!=NULL)
00448   {
00449     for(tags=taglist;tags->Name!=TAG_DONE;tags=tag_GetNextTag(tags))
00450     {
00451       if((tags->Name&TAGT_STRING)!=0&&tags->Data!=0L) mem_free((void *)tags->Data);
00452       else if((tags->Name&TAGT_RECT)!=0&&tags->Data!=0L) mem_free((void *)tags->Data);
00453       else if(((tags->Name&TAGT_VECTOR)!=0||(tags->Name&TAGT_PIXELS)!=0)&&tags->Data!=0L)
00454       {
00455         if(NULL!=((Vector_t *)tags->Data)->data) mem_free(((Vector_t *)tags->Data)->data);
00456         mem_free((void *)tags->Data);
00457       }
00458     }
00459     mem_free(taglist);
00460   }
00461 
00462   debug_End();
00463 }
00464 
00465 
00466 inline int marshal_GetStringMsgLen(char *str)
00467 {
00468   int ret=4;
00469 
00470   debug_Begin();
00471 
00472   if(str!=NULL) ret+=strlen(str)+1;
00473 
00474   debug_End();
00475 
00476   return(ret);
00477 }
00478 
00479 
00480 int marshal_GetTaglistMsgLen(tag *taglist)
00481 {
00482   int ret;
00483   tag *tags;
00484 
00485   debug_Begin();
00486 
00487   for(ret=4,tags=taglist;tags->Name!=TAG_DONE;tags=tag_GetNextTag(tags),ret+=4)
00488   {
00489     if((tags->Name&TAGT_STRING)!=0) ret+=marshal_GetStringMsgLen((char *)tags->Data);
00490     else if((tags->Name&TAGT_RECT)!=0&&tags->Data!=0L) ret+=4*sizeof(u32);
00491     else if (((tags->Name&TAGT_VECTOR)!=0||(tags->Name&TAGT_PIXELS)!=0)&&tags->Data!=0L) ret+=marshal_GetBinaryMsgLen(((Vector_t *)tags->Data)->size);
00492     else ret+=4;
00493   }
00494 
00495   debug_End();
00496 
00497   return(ret);
00498 }

Generated on Tue Jan 7 12:11:22 2003 for THEGUI by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002