Salome HOME
Updated copyright comment
[modules/yacs.git] / src / engine / Any.cxx
index a3c5cfa6b12f69c33b3e05e4ba660bd61fb68e01..cda84b110d7e5bea36988cc02db4039941bdcf9d 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2006-2019  CEA/DEN, EDF R&D
+// Copyright (C) 2006-2024  CEA, EDF
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
@@ -99,11 +99,13 @@ std::string YACS::ENGINE::ToBase64(const std::string& bytes)
 
 constexpr unsigned MAX_VAL_TAB2=123;
 
-constexpr unsigned char TAB2[MAX_VAL_TAB2] = { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 0, 128, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 128, 128, 128, 1, 128, 128, 128, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 128, 128, 128, 128, 128, 128, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63 };
+constexpr unsigned NOT_OK_VAL = 128;
+
+constexpr unsigned char TAB2[MAX_VAL_TAB2] = { NOT_OK_VAL, NOT_OK_VAL, NOT_OK_VAL, NOT_OK_VAL, NOT_OK_VAL, NOT_OK_VAL, NOT_OK_VAL, NOT_OK_VAL, NOT_OK_VAL, NOT_OK_VAL, NOT_OK_VAL, NOT_OK_VAL, NOT_OK_VAL, NOT_OK_VAL, NOT_OK_VAL, NOT_OK_VAL, NOT_OK_VAL, NOT_OK_VAL, NOT_OK_VAL, NOT_OK_VAL, NOT_OK_VAL, NOT_OK_VAL, NOT_OK_VAL, NOT_OK_VAL, NOT_OK_VAL, NOT_OK_VAL, NOT_OK_VAL, NOT_OK_VAL, NOT_OK_VAL, NOT_OK_VAL, NOT_OK_VAL, NOT_OK_VAL, NOT_OK_VAL, NOT_OK_VAL, NOT_OK_VAL, NOT_OK_VAL, NOT_OK_VAL, NOT_OK_VAL, NOT_OK_VAL, NOT_OK_VAL, NOT_OK_VAL, NOT_OK_VAL, NOT_OK_VAL, NOT_OK_VAL, NOT_OK_VAL, NOT_OK_VAL, 0, NOT_OK_VAL, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, NOT_OK_VAL, NOT_OK_VAL, NOT_OK_VAL, 1, NOT_OK_VAL, NOT_OK_VAL, NOT_OK_VAL, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, NOT_OK_VAL, NOT_OK_VAL, NOT_OK_VAL, NOT_OK_VAL, NOT_OK_VAL, NOT_OK_VAL, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63 };
 
 unsigned char BitAtPosSimple2(char val, std::size_t bitPos)
 {
-  return (val >> 5-bitPos) & 0x1;
+  return ( val >> (5-bitPos) ) & 0x1;
 }
 
 char BitAtPosOnChunk(char pt0, char pt1, std::size_t bitPos)
@@ -114,10 +116,22 @@ char BitAtPosOnChunk(char pt0, char pt1, std::size_t bitPos)
     return BitAtPosSimple2(pt1,bitPos-6);
 }
 
+unsigned char CheckEntry(char c)
+{
+  if( ((unsigned) c) < MAX_VAL_TAB2 )
+    {
+      unsigned char ret(TAB2[(unsigned char)c]);
+      if(ret != NOT_OK_VAL)
+        return ret;
+      throw YACS::Exception("Invalid character found !");
+    }
+  throw YACS::Exception("Invalid character found !");
+}
+
 char ByteInternal(char c0, char c1, std::size_t startBitIdInByte)
 {
   unsigned char ret(0);
-  char ct0(TAB2[(unsigned char)c0]),ct1(TAB2[(unsigned char)c1]);
+  char ct0(CheckEntry(c0)),ct1(CheckEntry(c1));
   for(int i = 7; i>=0; --i)
     {
       ret |= BitAtPosOnChunk(ct0,ct1,startBitIdInByte+i);
@@ -147,6 +161,21 @@ std::string YACS::ENGINE::FromBase64(const std::string& bytes)
   return ret;
 }
 
+/*!
+ * Method used at load time in case of non base64 bytes in input (a throw during decoding). If so, the input bytes is returned.
+ */
+std::string YACS::ENGINE::FromBase64Safe(const std::string& bytes)
+{
+  try
+    {
+      return FromBase64(bytes);
+    }
+  catch(const YACS::Exception& e)
+    {
+      return bytes;
+    }
+}
+
 StringOnHeap::StringOnHeap(const char *val):_str(strdup(val)),_len(strlen(val)),_dealloc(0)
 {
 }
@@ -251,9 +280,14 @@ AtomAny::AtomAny(const std::string& val):Any(Runtime::_tc_string)
   _value._s=new StringOnHeap(val);
 }
 
+AtomAny::AtomAny(const std::string& val, TypeCode* type):Any(type)
+{
+  _value._s=new StringOnHeap(val);
+}
+
 AtomAny::AtomAny(const AtomAny& other):Any(other)
 {
-  if(_type->isA(Runtime::_tc_string))
+  if(_type->isA(Runtime::_tc_string) || _type->kind()==YACS::ENGINE::Objref)
     {
       StringOnHeap *cpy=(other._value._s)->deepCopy();
       memcpy(&_value._s,&cpy,_type->getSizeInByteOfAnyReprInSeq());
@@ -273,7 +307,7 @@ AtomAny::AtomAny(char *val, Deallocator deAlloc):Any(Runtime::_tc_string)
 
 AtomAny::AtomAny(char *data, TypeCode* type):Any(type)
 {
-  if(type->isA(Runtime::_tc_string))
+  if(type->isA(Runtime::_tc_string) || _type->kind()==YACS::ENGINE::Objref)
     {
       void **tmp=(void **)data;
       StringOnHeap *cpy=((StringOnHeap *)(*tmp))->deepCopy();
@@ -297,12 +331,17 @@ AtomAny *AtomAny::New(char *val,Deallocator dealloc)
   return new AtomAny(val,dealloc);
 }
 
-AnyPtr AtomAny::operator[](int i) const throw(YACS::Exception)
+AtomAny *AtomAny::New(const std::string& val, TypeCode *type)
+{
+  return new AtomAny(val,type);
+}
+
+AnyPtr AtomAny::operator[](int i) const
 {
   throw InvalidExtractionException(_type->kind(),Sequence);
 }
 
-AnyPtr AtomAny::operator[](const char *key) const throw(YACS::Exception)
+AnyPtr AtomAny::operator[](const char *key) const
 {
   throw Exception("AtomAny::operator[] : try to get a part of a partitionned data whereas atomical.");
 }
@@ -318,13 +357,13 @@ bool AtomAny::operator ==(const Any& other) const
     return _value._i==otherC._value._i;
   else if(_type->isA(Runtime::_tc_bool))
     return _value._b==otherC._value._b;
-  else if(_type->isA(Runtime::_tc_string))
+  else if(_type->isA(Runtime::_tc_string) || _type->kind()==Objref)
     return (*_value._s)==*(otherC._value._s);
   else
     return false;
 }
 
-int AtomAny::getIntValue() const throw(YACS::Exception)
+int AtomAny::getIntValue() const
 {
   if(_type->isA(Runtime::_tc_int))
     return _value._i;
@@ -332,7 +371,7 @@ int AtomAny::getIntValue() const throw(YACS::Exception)
     throw Exception("Value is not an int");
 }
 
-bool AtomAny::getBoolValue() const throw(YACS::Exception)
+bool AtomAny::getBoolValue() const
 {
   if(_type->isA(Runtime::_tc_bool))
     return _value._b;
@@ -340,7 +379,7 @@ bool AtomAny::getBoolValue() const throw(YACS::Exception)
     throw Exception("Value is not a bool");
 }
 
-double AtomAny::getDoubleValue() const throw(YACS::Exception)
+double AtomAny::getDoubleValue() const
 {
   if(_type->isA(Runtime::_tc_double))
     return _value._d;
@@ -348,9 +387,9 @@ double AtomAny::getDoubleValue() const throw(YACS::Exception)
     throw Exception("Value is not a double");
 }
 
-std::string AtomAny::getStringValue() const throw(YACS::Exception)
+std::string AtomAny::getStringValue() const
 {
-  if(_type->isA(Runtime::_tc_string))
+  if(_type->isA(Runtime::_tc_string) || _type->kind()==YACS::ENGINE::Objref)
     {
       std::size_t sz(_value._s->size());
       if(sz==0)
@@ -364,7 +403,7 @@ std::string AtomAny::getStringValue() const throw(YACS::Exception)
 
 const char *AtomAny::getBytesValue(std::size_t& len) const
 {
-  if(_type->isA(Runtime::_tc_string))
+  if(_type->isA(Runtime::_tc_string) || _type->kind()==YACS::ENGINE::Objref)
     {
       len=_value._s->size();
       return _value._s->cStr();
@@ -382,7 +421,7 @@ const char *AtomAny::getBytesValue(std::size_t& len) const
  */
 void AtomAny::putMyReprAtPlace(char *data) const
 {
-  if(_type->isA(Runtime::_tc_string))
+  if(_type->isA(Runtime::_tc_string) || _type->kind()==YACS::ENGINE::Objref)
     {
       StringOnHeap *tmp=_value._s->deepCopy();
       memcpy(data,&tmp,_type->getSizeInByteOfAnyReprInSeq());
@@ -408,7 +447,7 @@ void AtomAny::putMyReprAtPlace(char *data) const
  */
 void AtomAny::putReprAtPlace(char *data, const char *src, const TypeCode *type, bool deepCpy)
 {
-  if(type->isA(Runtime::_tc_string))
+  if(type->isA(Runtime::_tc_string) || type->kind()==YACS::ENGINE::Objref)
     {
       void **tmp1=(void **)src;
       StringOnHeap *tmp=((const StringOnHeap *)(*tmp1))->deepCopy();
@@ -429,7 +468,7 @@ void AtomAny::putReprAtPlace(char *data, const char *src, const TypeCode *type,
 void AtomAny::destroyReprAtPlace(char *data, const TypeCode *type)
 {
   DynType typ=type->kind();
-  if(typ==String)
+  if(typ==String || typ==Objref)
     {
       if(!Any::IsNull(data))
         {
@@ -454,7 +493,7 @@ bool AtomAny::takeInChargeStorageOf(TypeCode *type)
 
 AtomAny::~AtomAny()
 {
-  if(_type->kind() == String)
+  if(_type->kind() == String || _type->kind()==Objref)
     delete _value._s;
 }
 
@@ -468,33 +507,33 @@ ComposedAny::ComposedAny(TypeCode* type, bool isNew):Any(type)
     _type->decrRef();
 }
 
-AnyPtr ComposedAny::operator[](const char *key) const throw(YACS::Exception)
+AnyPtr ComposedAny::operator[](const char *key) const
 {
   throw Exception("AtomAny::operator[] : try to get a part of a partitionned data not localizable by a string.");
 }
 
-void ComposedAny::checkTypeOf(const Any *elem) const throw(YACS::Exception)
+void ComposedAny::checkTypeOf(const Any *elem) const
 {
   if(!elem->getType()->isA(_type->contentType()))
     throw Exception("ComposedAny::checkTypeOf : invalid type.");
 }
 
-int ComposedAny::getIntValue() const throw(YACS::Exception)
+int ComposedAny::getIntValue() const
 {
  throw InvalidExtractionException(_type->kind(),Runtime::_tc_int->kind());
 }
 
-bool ComposedAny::getBoolValue() const throw(YACS::Exception)
+bool ComposedAny::getBoolValue() const
 {
   throw InvalidExtractionException(_type->kind(),Runtime::_tc_bool->kind());
 }
 
-double ComposedAny::getDoubleValue() const throw(YACS::Exception)
+double ComposedAny::getDoubleValue() const
 {
   throw InvalidExtractionException(_type->kind(),Runtime::_tc_double->kind());
 }
 
-std::string ComposedAny::getStringValue() const throw(YACS::Exception)
+std::string ComposedAny::getStringValue() const
 {
   throw InvalidExtractionException(_type->kind(),Runtime::_tc_string->kind());
 }
@@ -654,14 +693,14 @@ bool SequenceAny::operator ==(const Any& other) const
   return true;
 }
 
-void SequenceAny::setEltAtRank(int i, const Any *elem) throw(YACS::Exception)
+void SequenceAny::setEltAtRank(int i, const Any *elem)
 {
   checkTypeOf(elem);
   _alloc.destroy(_alloc._start+i*_alloc._sizeOf1Elm,_type->contentType());
   _alloc.construct(_alloc._start+i*_alloc._sizeOf1Elm,elem);
 }
 
-AnyPtr SequenceAny::operator[](int i) const throw(YACS::Exception)
+AnyPtr SequenceAny::operator[](int i) const
 {
   return _type->contentType()->getOrBuildAnyFromZippedData(_alloc._start+i*_alloc._sizeOf1Elm);
 }
@@ -730,23 +769,11 @@ SequenceAny *SequenceAny::removeUnsetItemsFromThis() const
 
 SequenceAny *SequenceAny::New(const TypeCode *typeOfContent)
 {
-  if(typeOfContent->kind() == Objref)
-    {
-      //In case of Objref, use a sequence of string
-      return new SequenceAny(Runtime::_tc_string);
-    }
-  else
     return new SequenceAny(typeOfContent);
 }
 
 SequenceAny *SequenceAny::New(const TypeCode *typeOfContent, unsigned lgth)
 {
-  if(typeOfContent->kind() == Objref)
-    {
-      //In case of Objref, use a sequence of string
-      return new SequenceAny(Runtime::_tc_string,lgth);
-    }
-  else
     return new SequenceAny(typeOfContent,lgth);
 }
 
@@ -945,7 +972,7 @@ ArrayAny::ArrayAny(const std::vector<std::string>& val):ComposedAny(new TypeCode
     }
 }
 
-void ArrayAny::setEltAtRank(int i, const Any *elem) throw(YACS::Exception)
+void ArrayAny::setEltAtRank(int i, const Any *elem)
 {
   checkTypeOf(elem);
   const TypeCode *subType=_type->contentType();
@@ -964,7 +991,7 @@ bool ArrayAny::operator ==(const Any& other) const
   return true;
 }
 
-AnyPtr ArrayAny::operator[](int i) const throw(YACS::Exception)
+AnyPtr ArrayAny::operator[](int i) const
 {
   const TypeCode *subType=_type->contentType();
   unsigned sizePerContent=subType->getSizeInByteOfAnyReprInSeq();
@@ -1042,13 +1069,13 @@ bool StructAny::operator ==(const Any& other) const
   return true;
 }
 
-AnyPtr StructAny::operator[](int i) const throw(YACS::Exception)
+AnyPtr StructAny::operator[](int i) const
 {
   const char what[]="StructAny::operator[](int i) : Struct key are strings not integers.";
   throw Exception(what);
 }
 
-AnyPtr StructAny::operator[](const char *key) const throw(YACS::Exception)
+AnyPtr StructAny::operator[](const char *key) const
 {
   const TypeCodeStruct *typeC=(const TypeCodeStruct *)_type;
   char *whereToGet=_data;
@@ -1066,13 +1093,13 @@ AnyPtr StructAny::operator[](const char *key) const throw(YACS::Exception)
   return (*iter).second->getOrBuildAnyFromZippedData(whereToGet);
 }
 
-void StructAny::setEltAtRank(int i, const Any *elem) throw(YACS::Exception)
+void StructAny::setEltAtRank(int i, const Any *elem)
 {
   const char what[]="Struct key are strings not integers.";
   throw Exception(what);
 }
 
-void StructAny::setEltAtRank(const char *key, const Any *elem) throw(YACS::Exception)
+void StructAny::setEltAtRank(const char *key, const Any *elem)
 {
   const TypeCodeStruct *typeC=(const TypeCodeStruct *)_type;
   unsigned offset;