LCOV - code coverage report
Current view: top level - source/simulation2/serialization - IDeserializer.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 76 105 72.4 %
Date: 2023-01-19 00:18:29 Functions: 17 22 77.3 %

          Line data    Source code
       1             : /* Copyright (C) 2011 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #include "precompiled.h"
      19             : 
      20             : #include "IDeserializer.h"
      21             : 
      22             : #include "lib/byte_order.h"
      23             : #include "lib/utf8.h"
      24             : #include "ps/CStr.h"
      25             : 
      26          96 : IDeserializer::~IDeserializer()
      27             : {
      28          96 : }
      29             : 
      30         212 : void IDeserializer::NumberU8(const char* name, uint8_t& out, uint8_t lower, uint8_t upper)
      31             : {
      32             :     uint8_t value;
      33         212 :     Get(name, (u8*)&value, sizeof(uint8_t));
      34             : 
      35         212 :     if (!(lower <= value && value <= upper))
      36           0 :         throw PSERROR_Deserialize_OutOfBounds(name);
      37             : 
      38         212 :     out = value;
      39         212 : }
      40             : 
      41           0 : void IDeserializer::NumberI8(const char* name, int8_t& out, int8_t lower, int8_t upper)
      42             : {
      43             :     int8_t value;
      44           0 :     Get(name, (u8*)&value, sizeof(uint8_t));
      45             : 
      46           0 :     if (!(lower <= value && value <= upper))
      47           0 :         throw PSERROR_Deserialize_OutOfBounds(name);
      48             : 
      49           0 :     out = value;
      50           0 : }
      51             : 
      52           0 : void IDeserializer::NumberU16(const char* name, uint16_t& out, uint16_t lower, uint16_t upper)
      53             : {
      54             :     uint16_t value;
      55           0 :     Get(name, (u8*)&value, sizeof(uint16_t));
      56           0 :     value = to_le16(value);
      57             : 
      58           0 :     if (!(lower <= value && value <= upper))
      59           0 :         throw PSERROR_Deserialize_OutOfBounds(name);
      60             : 
      61           0 :     out = value;
      62           0 : }
      63             : 
      64           0 : void IDeserializer::NumberI16(const char* name, int16_t& out, int16_t lower, int16_t upper)
      65             : {
      66             :     int16_t value;
      67           0 :     Get(name, (u8*)&value, sizeof(uint16_t));
      68           0 :     value = (i16)to_le16((u16)value);
      69             : 
      70           0 :     if (!(lower <= value && value <= upper))
      71           0 :         throw PSERROR_Deserialize_OutOfBounds(name);
      72             : 
      73           0 :     out = value;
      74           0 : }
      75             : 
      76          15 : void IDeserializer::NumberU32(const char* name, uint32_t& out, uint32_t lower, uint32_t upper)
      77             : {
      78             :     uint32_t value;
      79          15 :     Get(name, (u8*)&value, sizeof(uint32_t));
      80          15 :     value = to_le32(value);
      81             : 
      82          15 :     if (!(lower <= value && value <= upper))
      83           0 :         throw PSERROR_Deserialize_OutOfBounds(name);
      84             : 
      85          15 :     out = value;
      86          15 : }
      87             : 
      88         101 : void IDeserializer::NumberI32(const char* name, int32_t& out, int32_t lower, int32_t upper)
      89             : {
      90             :     int32_t value;
      91         101 :     Get(name, (u8*)&value, sizeof(uint32_t));
      92         101 :     value = (i32)to_le32((u32)value);
      93             : 
      94         101 :     if (!(lower <= value && value <= upper))
      95           0 :         throw PSERROR_Deserialize_OutOfBounds(name);
      96             : 
      97         101 :     out = value;
      98         101 : }
      99             : 
     100         357 : void IDeserializer::NumberU8_Unbounded(const char* name, uint8_t& out)
     101             : {
     102         357 :     Get(name, (u8*)&out, sizeof(uint8_t));
     103         357 : }
     104             : 
     105           1 : void IDeserializer::NumberI8_Unbounded(const char* name, int8_t& out)
     106             : {
     107           1 :     Get(name, (u8*)&out, sizeof(int8_t));
     108           1 : }
     109             : 
     110           1 : void IDeserializer::NumberU16_Unbounded(const char* name, uint16_t& out)
     111             : {
     112             :     uint16_t value;
     113           1 :     Get(name, (u8*)&value, sizeof(uint16_t));
     114           1 :     out = to_le16(value);
     115           1 : }
     116             : 
     117           1 : void IDeserializer::NumberI16_Unbounded(const char* name, int16_t& out)
     118             : {
     119             :     int16_t value;
     120           1 :     Get(name, (u8*)&value, sizeof(int16_t));
     121           1 :     out = (i16)to_le16((u16)value);
     122           1 : }
     123             : 
     124         450 : void IDeserializer::NumberU32_Unbounded(const char* name, uint32_t& out)
     125             : {
     126             :     uint32_t value;
     127         450 :     Get(name, (u8*)&value, sizeof(uint32_t));
     128         450 :     out = to_le32(value);
     129         450 : }
     130             : 
     131          40 : void IDeserializer::NumberI32_Unbounded(const char* name, int32_t& out)
     132             : {
     133             :     int32_t value;
     134          40 :     Get(name, (u8*)&value, sizeof(int32_t));
     135          40 :     out = (i32)to_le32((u32)value);
     136          40 : }
     137             : 
     138           1 : void IDeserializer::NumberFloat_Unbounded(const char* name, float& out)
     139             : {
     140           1 :     Get(name, (u8*)&out, sizeof(float));
     141           1 : }
     142             : 
     143          19 : void IDeserializer::NumberDouble_Unbounded(const char* name, double& out)
     144             : {
     145          19 :     Get(name, (u8*)&out, sizeof(double));
     146          19 : }
     147             : 
     148          28 : void IDeserializer::NumberFixed_Unbounded(const char* name, fixed& out)
     149             : {
     150             :     int32_t n;
     151          28 :     NumberI32_Unbounded(name, n);
     152          28 :     out.SetInternalValue(n);
     153          28 : }
     154             : 
     155         206 : void IDeserializer::Bool(const char* name, bool& out)
     156             : {
     157             :     uint8_t i;
     158         206 :     NumberU8(name, i, 0, 1);
     159         206 :     out = (i != 0);
     160         206 : }
     161             : 
     162          15 : void IDeserializer::StringASCII(const char* name, std::string& out, uint32_t minlength, uint32_t maxlength)
     163             : {
     164             :     uint32_t len;
     165          15 :     NumberU32("string length", len, minlength, maxlength);
     166             : 
     167          15 :     RequireBytesInStream(len);
     168          15 :     out.resize(len);
     169          15 :     Get(name, (u8*)out.data(), len);
     170             : 
     171         196 :     for (size_t i = 0; i < out.length(); ++i)
     172         181 :         if (out[i] == 0 || (unsigned char)out[i] >= 128)
     173           0 :             throw PSERROR_Deserialize_InvalidCharInString();
     174          15 : }
     175             : 
     176          17 : void IDeserializer::String(const char* name, std::wstring& out, uint32_t minlength, uint32_t maxlength)
     177             : {
     178          34 :     std::string str;
     179             :     uint32_t len;
     180          17 :     NumberU32_Unbounded("string length", len);
     181             : 
     182          17 :     RequireBytesInStream(len);
     183          17 :     str.resize(len);
     184          17 :     Get(name, (u8*)str.data(), len);
     185             : 
     186             :     Status err;
     187          17 :     out = wstring_from_utf8(str, &err);
     188          17 :     if (err)
     189           0 :         throw PSERROR_Deserialize_InvalidCharInString();
     190             : 
     191          17 :     if (!(minlength <= out.length() && out.length() <= maxlength))
     192           0 :         throw PSERROR_Deserialize_OutOfBounds(name);
     193          17 : }
     194             : 
     195          25 : void IDeserializer::RawBytes(const char* name, u8* data, size_t len)
     196             : {
     197          25 :     Get(name, data, len);
     198          25 : }
     199             : 
     200           0 : int IDeserializer::GetVersion() const
     201             : {
     202           0 :     debug_warn(L"GetVersion() not implemented in this subclass");
     203           0 :     return 0;
     204             : }

Generated by: LCOV version 1.13