LCOV - code coverage report
Current view: top level - src - SerializationFormat.cc (source / functions) Hit Total Coverage
Test: app.info Lines: 66 242 27.3 %
Date: 2010-12-13 Functions: 20 69 29.0 %
Branches: 15 127 11.8 %

           Branch data     Line data    Source code
       1                 :            : // $Id: SerializationFormat.cc 5873 2008-06-28 19:25:03Z vern $
       2                 :            : 
       3                 :            : #include <ctype.h>
       4                 :            : 
       5                 :            : #include "net_util.h"
       6                 :            : #include "SerializationFormat.h"
       7                 :            : #include "Serializer.h"
       8                 :            : 
       9                 :          6 : SerializationFormat::SerializationFormat()
      10                 :            :         {
      11                 :          6 :         output = 0;
      12                 :          6 :         }
      13                 :            : 
      14                 :          2 : SerializationFormat::~SerializationFormat()
      15                 :            :         {
      16 [ #  # ][ #  # ]:          2 :         delete [] output;
                 [ +  + ]
      17 [ #  # ][ #  # ]:          2 :         }
                 [ -  + ]
      18                 :            : 
      19                 :          0 : void SerializationFormat::StartRead(char* data, uint32 arg_len)
      20                 :            :         {
      21                 :          0 :         input = data;
      22                 :          0 :         input_len = arg_len;
      23                 :          0 :         input_pos = 0;
      24                 :          0 :         }
      25                 :            : 
      26                 :          0 : void SerializationFormat::EndRead()
      27                 :            :         {
      28                 :          0 :         input = 0;
      29                 :          0 :         }
      30                 :            : 
      31                 :          1 : void SerializationFormat::StartWrite()
      32                 :            :         {
      33 [ -  + ][ #  # ]:          1 :         if ( output && output_size > INITIAL_SIZE )
      34                 :            :                 {
      35         [ #  # ]:          0 :                 delete [] output;
      36                 :          0 :                 output = 0;
      37                 :            :                 }
      38                 :            : 
      39         [ +  - ]:          1 :         if ( ! output )
      40                 :            :                 {
      41                 :          1 :                 output = new char[INITIAL_SIZE];
      42                 :          1 :                 output_size = INITIAL_SIZE;
      43                 :            :                 }
      44                 :            : 
      45                 :          1 :         output_pos = 0;
      46                 :          1 :         bytes_written = 0;
      47                 :          1 :         }
      48                 :            : 
      49                 :          1 : uint32 SerializationFormat::EndWrite(char** data)
      50                 :            :         {
      51                 :          1 :         *data = new char[output_pos];
      52                 :          1 :         memcpy(*data, output, output_pos);
      53                 :          1 :         return output_pos;
      54                 :            :         }
      55                 :            : 
      56                 :          0 : bool SerializationFormat::ReadData(void* b, size_t count)
      57                 :            :         {
      58         [ #  # ]:          0 :         if ( input_pos + count > input_len )
      59                 :            :                 {
      60                 :          0 :                 error("data underflow during read in binary format");
      61                 :          0 :                 abort();
      62                 :            :                 return false;
      63                 :            :                 }
      64                 :            : 
      65                 :          0 :         memcpy(b, input + input_pos, count);
      66                 :          0 :         input_pos += count;
      67                 :          0 :         return true;
      68                 :            :         }
      69                 :            : 
      70                 :        760 : bool SerializationFormat::WriteData(const void* b, size_t count)
      71                 :            :         {
      72                 :            :         // Increase buffer if necessary.
      73         [ -  + ]:        760 :         while ( output_pos + count > output_size )
      74                 :            :                 {
      75                 :          0 :                 output_size = output_pos + count + INITIAL_SIZE;
      76                 :          0 :                 char* tmp = new char[output_size];
      77                 :          0 :                 memcpy(tmp, output, output_pos);
      78         [ #  # ]:          0 :                 delete [] output;
      79                 :          0 :                 output = tmp;
      80                 :            :                 }
      81                 :            : 
      82                 :        760 :         memcpy(output + output_pos, b, count);
      83                 :        760 :         output_pos += count;
      84                 :        760 :         bytes_written += count;
      85                 :            : 
      86                 :        760 :         return true;
      87                 :            :         }
      88                 :            : 
      89                 :          6 : BinarySerializationFormat::BinarySerializationFormat()
      90                 :            :         {
      91                 :          6 :         }
      92                 :            : 
      93                 :          2 : BinarySerializationFormat::~BinarySerializationFormat()
      94                 :            :         {
      95 [ +  - ][ #  # ]:          2 :         }
                 [ #  # ]
      96                 :            : 
      97                 :          0 : bool BinarySerializationFormat::Read(int* v, const char* tag)
      98                 :            :         {
      99                 :            :         uint32 tmp;
     100         [ #  # ]:          0 :         if ( ! ReadData(&tmp, sizeof(tmp)) )
     101                 :          0 :                 return false;
     102                 :            : 
     103                 :          0 :         *v = (int) ntohl(tmp);
     104                 :          0 :         DBG_LOG(DBG_SERIAL, "Read int %d [%s]", *v, tag);
     105                 :          0 :         return true;
     106                 :            :         }
     107                 :            : 
     108                 :          0 : bool BinarySerializationFormat::Read(uint16* v, const char* tag)
     109                 :            :         {
     110         [ #  # ]:          0 :         if ( ! ReadData(v, sizeof(*v)) )
     111                 :          0 :                 return false;
     112                 :            : 
     113                 :          0 :         *v = ntohs(*v);
     114                 :          0 :         DBG_LOG(DBG_SERIAL, "Read uint16 %hu [%s]", *v, tag);
     115                 :          0 :         return true;
     116                 :            :         }
     117                 :            : 
     118                 :          0 : bool BinarySerializationFormat::Read(uint32* v, const char* tag)
     119                 :            :         {
     120         [ #  # ]:          0 :         if ( ! ReadData(v, sizeof(*v)) )
     121                 :          0 :                 return false;
     122                 :            : 
     123                 :          0 :         *v = ntohl(*v);
     124                 :          0 :         DBG_LOG(DBG_SERIAL, "Read uint32 %ld [%s]", *v, tag);
     125                 :          0 :         return true;
     126                 :            :         }
     127                 :            : 
     128                 :            : 
     129                 :          0 : bool BinarySerializationFormat::Read(int64* v, const char* tag)
     130                 :            :         {
     131                 :            :         uint32 x[2];
     132         [ #  # ]:          0 :         if ( ! ReadData(x, sizeof(x)) )
     133                 :          0 :                 return false;
     134                 :            : 
     135                 :          0 :         *v = ((int64(ntohl(x[0]))) << 32) | ntohl(x[1]);
     136                 :          0 :         DBG_LOG(DBG_SERIAL, "Read int64 %lld [%s]", *v, tag);
     137                 :          0 :         return true;
     138                 :            :         }
     139                 :            : 
     140                 :          0 : bool BinarySerializationFormat::Read(uint64* v, const char* tag)
     141                 :            :         {
     142                 :            :         uint32 x[2];
     143         [ #  # ]:          0 :         if ( ! ReadData(x, sizeof(x)) )
     144                 :          0 :                 return false;
     145                 :            : 
     146                 :          0 :         *v = ((uint64(ntohl(x[0]))) << 32) | ntohl(x[1]);
     147                 :          0 :         DBG_LOG(DBG_SERIAL, "Read uint64 %llu [%s]", *v, tag);
     148                 :          0 :         return true;
     149                 :            :         }
     150                 :            : 
     151                 :          0 : bool BinarySerializationFormat::Read(bool* v, const char* tag)
     152                 :            :         {
     153                 :            :         char c;
     154         [ #  # ]:          0 :         if ( ! ReadData(&c, 1) )
     155                 :          0 :                 return false;
     156                 :            : 
     157                 :          0 :         *v = c == '\1' ? true : false;
     158         [ #  # ]:          0 :         DBG_LOG(DBG_SERIAL, "Read bool %s [%s]", *v ? "true" : "false", tag);
     159                 :          0 :         return true;
     160                 :            :         }
     161                 :            : 
     162                 :          0 : bool BinarySerializationFormat::Read(double* d, const char* tag)
     163                 :            :         {
     164         [ #  # ]:          0 :         if ( ! ReadData(d, sizeof(*d)) )
     165                 :          0 :                 return false;
     166                 :            : 
     167                 :          0 :         *d = ntohd(*d);
     168                 :          0 :         DBG_LOG(DBG_SERIAL, "Read double %.6f [%s]", *d, tag);
     169                 :          0 :         return true;
     170                 :            :         }
     171                 :            : 
     172                 :          0 : bool BinarySerializationFormat::Read(char* v, const char* tag)
     173                 :            :         {
     174                 :          0 :         bool ret = ReadData(v, 1);
     175                 :          0 :         DBG_LOG(DBG_SERIAL, "Read char %s [%s]", fmt_bytes(v, 1), tag);
     176                 :          0 :         return ret;
     177                 :            :         }
     178                 :            : 
     179                 :          0 : bool BinarySerializationFormat::Read(char** str, int* len, const char* tag)
     180                 :            :         {
     181                 :            :         int l;
     182         [ #  # ]:          0 :         if ( ! ReadData(&l, sizeof(l)) )
     183                 :          0 :                 return false;
     184                 :            : 
     185                 :          0 :         l = ntohl(l);
     186                 :          0 :         char* s = new char[l + 1];
     187                 :            : 
     188         [ #  # ]:          0 :         if ( ! ReadData(s, l) )
     189                 :            :                 {
     190         [ #  # ]:          0 :                 delete [] s;
     191                 :          0 :                 *str = 0;
     192                 :          0 :                 return false;
     193                 :            :                 }
     194                 :            : 
     195         [ #  # ]:          0 :         if ( len )
     196                 :          0 :                 *len = l;
     197                 :            :         else
     198                 :            :                 {
     199                 :            :                 // If len isn't given, make sure that the string
     200                 :            :                 // doesn't contain any nulls.
     201         [ #  # ]:          0 :                 for ( int i = 0; i < l; i++ )
     202         [ #  # ]:          0 :                         if ( ! s[i] )
     203                 :            :                                 {
     204                 :          0 :                                 error("binary Format: string contains null; replaced by '_'");
     205                 :          0 :                                 s[i] = '_';
     206                 :            :                                 }
     207                 :            :                 }
     208                 :            : 
     209                 :          0 :         s[l] = '\0';
     210                 :            : 
     211                 :          0 :         *str = s;
     212                 :            : 
     213                 :          0 :         DBG_LOG(DBG_SERIAL, "Read %d bytes |%s| [%s]", l, fmt_bytes(*str, l), tag);
     214                 :          0 :         return true;
     215                 :            :         }
     216                 :            : 
     217                 :         48 : bool BinarySerializationFormat::Write(char v, const char* tag)
     218                 :            :         {
     219                 :         48 :         DBG_LOG(DBG_SERIAL, "Write char %s [%s]", fmt_bytes(&v, 1), tag);
     220                 :         48 :         return WriteData(&v, 1);
     221                 :            :         }
     222                 :            : 
     223                 :         65 : bool BinarySerializationFormat::Write(uint16 v, const char* tag)
     224                 :            :         {
     225                 :         65 :         DBG_LOG(DBG_SERIAL, "Write uint16 %hu [%s]", v, tag);
     226                 :         65 :         v = htons(v);
     227                 :         65 :         return WriteData(&v, sizeof(v));
     228                 :            :         }
     229                 :            : 
     230                 :          0 : bool BinarySerializationFormat::Write(uint32 v, const char* tag)
     231                 :            :         {
     232                 :          0 :         DBG_LOG(DBG_SERIAL, "Write uint32 %ld [%s]", v, tag);
     233                 :          0 :         v = htonl(v);
     234                 :          0 :         return WriteData(&v, sizeof(v));
     235                 :            :         }
     236                 :            : 
     237                 :        155 : bool BinarySerializationFormat::Write(int v, const char* tag)
     238                 :            :         {
     239                 :        155 :         DBG_LOG(DBG_SERIAL, "Write int %d [%s]", v, tag);
     240                 :        155 :         uint32 tmp = htonl((uint32) v);
     241                 :        155 :         return WriteData(&tmp, sizeof(tmp));
     242                 :            :         }
     243                 :            : 
     244                 :        110 : bool BinarySerializationFormat::Write(uint64 v, const char* tag)
     245                 :            :         {
     246                 :        110 :         DBG_LOG(DBG_SERIAL, "Write uint64 %lu [%s]", v, tag);
     247                 :            :         uint32 x[2];
     248                 :        110 :         x[0] = htonl(v >> 32);
     249                 :        110 :         x[1] = htonl(v & 0xffffffff);
     250                 :        110 :         return WriteData(x, sizeof(x));
     251                 :            :         }
     252                 :            : 
     253                 :          0 : bool BinarySerializationFormat::Write(int64 v, const char* tag)
     254                 :            :         {
     255                 :          0 :         DBG_LOG(DBG_SERIAL, "Write int64 %ld [%s]", v, tag);
     256                 :            :         uint32 x[2];
     257                 :          0 :         x[0] = htonl(v >> 32);
     258                 :          0 :         x[1] = htonl(v & 0xffffffff);
     259                 :          0 :         return WriteData(x, sizeof(x));
     260                 :            :         }
     261                 :            : 
     262                 :          4 : bool BinarySerializationFormat::Write(double d, const char* tag)
     263                 :            :         {
     264                 :          4 :         DBG_LOG(DBG_SERIAL, "Write double %.6f [%s]", d, tag);
     265                 :          4 :         d = htond(d);
     266                 :          4 :         return WriteData(&d, sizeof(d));
     267                 :            :         }
     268                 :            : 
     269                 :        294 : bool BinarySerializationFormat::Write(bool v, const char* tag)
     270                 :            :         {
     271         [ +  + ]:        294 :         DBG_LOG(DBG_SERIAL, "Write bool %s [%s]", v ? "true" : "false", tag);
     272         [ +  + ]:        294 :         char c = v ? '\1' : '\0';
     273                 :        294 :         return WriteData(&c, 1);
     274                 :            :         }
     275                 :            : 
     276                 :         42 : bool BinarySerializationFormat::Write(const char* s, const char* tag)
     277                 :            :         {
     278                 :         42 :         return Write(s, strlen(s), tag);
     279                 :            :         }
     280                 :            : 
     281                 :         85 : bool BinarySerializationFormat::WriteOpenTag(const char* tag)
     282                 :            :         {
     283                 :         85 :         return true;
     284                 :            :         }
     285                 :            : 
     286                 :         84 : bool BinarySerializationFormat::WriteCloseTag(const char* tag)
     287                 :            :         {
     288                 :         84 :         return true;
     289                 :            :         }
     290                 :            : 
     291                 :          1 : bool BinarySerializationFormat::WriteSeparator()
     292                 :            :         {
     293                 :          1 :         return true;
     294                 :            :         }
     295                 :            : 
     296                 :         42 : bool BinarySerializationFormat::Write(const char* buf, int len, const char* tag)
     297                 :            :         {
     298                 :         42 :         DBG_LOG(DBG_SERIAL, "Write bytes |%s| [%s]", fmt_bytes(buf, len), tag);
     299                 :         42 :         uint32 l = htonl(len);
     300   [ +  -  +  - ]:         42 :         return WriteData(&l, sizeof(l)) && WriteData(buf, len);
     301                 :            :         }
     302                 :            : 
     303                 :          0 : XMLSerializationFormat::XMLSerializationFormat()
     304                 :            :         {
     305                 :          0 :         }
     306                 :            : 
     307                 :          0 : XMLSerializationFormat::~XMLSerializationFormat()
     308                 :            :         {
     309 [ #  # ][ #  # ]:          0 :         }
                 [ #  # ]
     310                 :            : 
     311                 :          0 : bool XMLSerializationFormat::Read(int* v, const char* tag)
     312                 :            :         {
     313                 :          0 :         internal_error("no reading of xml");
     314                 :            :         return false;
     315                 :            :         }
     316                 :            : 
     317                 :          0 : bool XMLSerializationFormat::Read(uint16* v, const char* tag)
     318                 :            :         {
     319                 :          0 :         internal_error("no reading of xml");
     320                 :            :         return false;
     321                 :            :         }
     322                 :            : 
     323                 :          0 : bool XMLSerializationFormat::Read(uint32* v, const char* tag)
     324                 :            :         {
     325                 :          0 :         internal_error("no reading of xml");
     326                 :            :         return false;
     327                 :            :         }
     328                 :            : 
     329                 :          0 : bool XMLSerializationFormat::Read(int64* v, const char* tag)
     330                 :            :         {
     331                 :          0 :         internal_error("no reading of xml");
     332                 :            :         return false;
     333                 :            :         }
     334                 :            : 
     335                 :          0 : bool XMLSerializationFormat::Read(uint64* v, const char* tag)
     336                 :            :         {
     337                 :          0 :         internal_error("no reading of xml");
     338                 :            :         return false;
     339                 :            :         }
     340                 :            : 
     341                 :          0 : bool XMLSerializationFormat::Read(bool* v, const char* tag)
     342                 :            :         {
     343                 :          0 :         internal_error("no reading of xml");
     344                 :            :         return false;
     345                 :            :         }
     346                 :            : 
     347                 :          0 : bool XMLSerializationFormat::Read(double* d, const char* tag)
     348                 :            :         {
     349                 :          0 :         internal_error("no reading of xml");
     350                 :            :         return false;
     351                 :            :         }
     352                 :            : 
     353                 :          0 : bool XMLSerializationFormat::Read(char* v, const char* tag)
     354                 :            :         {
     355                 :          0 :         internal_error("no reading of xml");
     356                 :            :         return false;
     357                 :            :         }
     358                 :            : 
     359                 :          0 : bool XMLSerializationFormat::Read(char** str, int* len, const char* tag)
     360                 :            :         {
     361                 :          0 :         internal_error("no reading of xml");
     362                 :            :         return false;
     363                 :            :         }
     364                 :            : 
     365                 :          0 : bool XMLSerializationFormat::Write(char v, const char* tag)
     366                 :            :         {
     367                 :          0 :         return WriteElem(tag, "char", &v, 1);
     368                 :            :         }
     369                 :            : 
     370                 :          0 : bool XMLSerializationFormat::Write(uint16 v, const char* tag)
     371                 :            :         {
     372                 :          0 :         const char* tmp = fmt("%u", v);
     373                 :          0 :         return WriteElem(tag, "uint16", tmp, strlen(tmp));
     374                 :            :         }
     375                 :            : 
     376                 :          0 : bool XMLSerializationFormat::Write(uint32 v, const char* tag)
     377                 :            :         {
     378                 :          0 :         const char* tmp = fmt("%u", v);
     379                 :          0 :         return WriteElem(tag, "uint32", tmp, strlen(tmp));
     380                 :            :         }
     381                 :            : 
     382                 :          0 : bool XMLSerializationFormat::Write(uint64 v, const char* tag)
     383                 :            :         {
     384                 :          0 :         const char* tmp = fmt("%llu", v);
     385                 :          0 :         return WriteElem(tag, "uint64", tmp, strlen(tmp));
     386                 :            :         }
     387                 :            : 
     388                 :          0 : bool XMLSerializationFormat::Write(int64 v, const char* tag)
     389                 :            :         {
     390                 :          0 :         const char* tmp = fmt("%lld", v);
     391                 :          0 :         return WriteElem(tag, "int64", tmp, strlen(tmp));
     392                 :            :         }
     393                 :            : 
     394                 :          0 : bool XMLSerializationFormat::Write(int v, const char* tag)
     395                 :            :         {
     396                 :          0 :         const char* tmp = fmt("%d", v);
     397                 :          0 :         return WriteElem(tag, "int", tmp, strlen(tmp));
     398                 :            :         }
     399                 :            : 
     400                 :          0 : bool XMLSerializationFormat::Write(double d, const char* tag)
     401                 :            :         {
     402                 :          0 :         const char* tmp = fmt("%f", d);
     403                 :          0 :         return WriteElem(tag, "double", tmp, strlen(tmp));
     404                 :            :         }
     405                 :            : 
     406                 :          0 : bool XMLSerializationFormat::Write(bool v, const char* tag)
     407                 :            :         {
     408         [ #  # ]:          0 :         if ( v )
     409                 :          0 :                 return WriteElem(tag, "bool", "true", 4);
     410                 :            :         else
     411                 :          0 :                 return WriteElem(tag, "bool", "false", 5);
     412                 :            :         }
     413                 :            : 
     414                 :          0 : bool XMLSerializationFormat::Write(const char* s, const char* tag)
     415                 :            :         {
     416                 :          0 :         return WriteElem(tag, "string", s, strlen(s));
     417                 :            :         }
     418                 :            : 
     419                 :          0 : bool XMLSerializationFormat::WriteOpenTag(const char* tag)
     420                 :            :         {
     421 [ #  # ][ #  # ]:          0 :         return WriteData("<", 1) && WriteData(tag, strlen(tag) && WriteData(">", 1));
         [ #  # ][ #  # ]
     422                 :            :         }
     423                 :            : 
     424                 :          0 : bool XMLSerializationFormat::WriteCloseTag(const char* tag)
     425                 :            :         {
     426                 :            :         return WriteData("</", 2) && WriteData(tag, strlen(tag))
     427 [ #  # ][ #  # ]:          0 :                 && WriteData(">", 1);
                 [ #  # ]
     428                 :            :         }
     429                 :            : 
     430                 :          0 : bool XMLSerializationFormat::WriteSeparator()
     431                 :            :         {
     432                 :          0 :         return WriteData("\n", 1);
     433                 :            :         }
     434                 :            : 
     435                 :          0 : bool XMLSerializationFormat::Write(const char* buf, int len, const char* tag)
     436                 :            :         {
     437                 :          0 :         return WriteElem(tag, "string", buf, len);
     438                 :            :         }
     439                 :            : 
     440                 :          0 : bool XMLSerializationFormat::WriteEncodedString(const char* s, int len)
     441                 :            :         {
     442         [ #  # ]:          0 :         while ( len-- )
     443                 :            :                 {
     444                 :          0 :                 int success = false;
     445         [ #  # ]:          0 :                 if ( ! isprint(*s) )
     446                 :            :                         {
     447                 :          0 :                         const char* tmp = fmt("%.4x", (int)* s++);
     448                 :            :                         success = WriteData("&#x", 3) && WriteData(tmp, 4) &&
     449   [ #  #  #  # ]:          0 :                         WriteData(";", 1);
                 [ #  # ]
     450                 :            :                         }
     451                 :            :                 else
     452                 :            :                         {
     453   [ #  #  #  #  :          0 :                         switch ( *s ) {
                      # ]
     454                 :            :                         case '"':
     455                 :          0 :                                 success = WriteData("&quot;", 6);
     456                 :          0 :                                 break;
     457                 :            :                         case '&':
     458                 :          0 :                                 success = WriteData("&amp;", 5);
     459                 :          0 :                                 break;
     460                 :            :                         case '<':
     461                 :          0 :                                 success = WriteData("&lt;", 4);
     462                 :          0 :                                 break;
     463                 :            :                         case '>':
     464                 :          0 :                                 success = WriteData("&gt;", 4);
     465                 :          0 :                                 break;
     466                 :            :                         default:
     467                 :          0 :                                 success = WriteData(s, 1);
     468                 :            :                         }
     469                 :            : 
     470         [ #  # ]:          0 :                         if ( ! success )
     471                 :          0 :                                 return false;
     472                 :            : 
     473                 :          0 :                         ++s;
     474                 :            :                         }
     475                 :            :                 }
     476                 :          0 :         return true;
     477                 :            :         }
     478                 :            : 
     479                 :            : bool XMLSerializationFormat::WriteElem(const char* tag, const char* type,
     480                 :          0 :                                         const char* content, int len)
     481                 :            :         {
     482         [ #  # ]:          0 :         if ( ! tag )
     483                 :          0 :                 return true;
     484                 :            : 
     485                 :            :         return WriteData("<", 1) &&
     486                 :            :                 WriteData(tag, strlen(tag)) &&
     487                 :            : #if 0
     488                 :            :                 WriteData(" type=\"", 7) &&
     489                 :            :                 WriteData(type, strlen(type)) &&
     490                 :            :                 WriteData("\"", 1) &&
     491                 :            : #endif
     492                 :            :                 WriteData(">", 1) &&
     493                 :            :                 WriteEncodedString(content, len) &&
     494                 :            :                 WriteData("</", 2) &&
     495                 :            :                 WriteData(tag, strlen(tag)) &&
     496 [ #  # ][ #  # ]:          0 :                 WriteData(">", 1);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     497 [ +  - ][ +  - ]:          6 :         }

Generated by: LCOV version 1.8