Pol  Revision:cb584c9
unicode.cpp
Go to the documentation of this file.
1 
8 #include "unicode.h"
9 
10 #include <stddef.h>
11 
12 #include "../bscript/bobject.h"
13 #include "../clib/clib_endian.h"
14 
15 namespace Pol
16 {
17 namespace Core
18 {
19 // Warning: Make sure that your buffer if large enough! Don't forget to add space for null
20 // terminator if requested.
21 bool convertArrayToUC( Bscript::ObjArray*& in_text, u16* out_wtext, size_t textlen,
22  bool ConvToBE /*false*/, bool nullterm /*true*/ )
23 {
24  u16 value;
25  size_t pos = 0;
26  for ( size_t i = 0; i < textlen; i++ )
27  {
28  Bscript::BObject* bo = in_text->ref_arr[i].get();
29  if ( bo == nullptr )
30  continue;
31  if ( !bo->isa( Bscript::BObjectImp::OTLong ) )
32  return false;
33  Bscript::BObjectImp* imp = bo->impptr();
34  Bscript::BLong* blong = static_cast<Bscript::BLong*>( imp );
35  value = ( u16 )( blong->value() & 0xFFFF );
36  out_wtext[pos++] = ConvToBE ? ctBEu16( value ) : value;
37  // Jump out on a nullptr (EOS) value (should stop exploits, too?)
38  if ( value == 0L )
39  return true;
40  }
41  if ( nullterm )
42  out_wtext[pos] = (u16)0L;
43  return true;
44 }
45 
46 bool convertUCtoArray( const u16* in_wtext, Bscript::ObjArray*& out_text, size_t textlen,
47  bool ConvFromBE /*false*/ )
48 {
49  u16 value;
50  out_text = new Bscript::ObjArray();
51  for ( size_t i = 0; i < textlen; i++ )
52  {
53  value = ConvFromBE ? cfBEu16( in_wtext[i] ) : in_wtext[i];
54  // Jump out on a nullptr (EOS) value (should stop exploits, too?)
55  if ( value == 0L )
56  return true;
57  out_text->addElement( new Bscript::BLong( value ) );
58  }
59  return true;
60 }
61 }
62 }
int value() const
Definition: bobject.h:592
bool isa(BObjectType type) const
Definition: bobject.h:353
#define ctBEu16(x)
Definition: clib_endian.h:46
unsigned short u16
Definition: rawtypes.h:26
bool convertArrayToUC(Bscript::ObjArray *&in_text, u16 *out_wtext, size_t textlen, bool ConvToBE, bool nullterm)
Definition: unicode.cpp:21
#define cfBEu16(x)
Definition: clib_endian.h:44
bool convertUCtoArray(const u16 *in_wtext, Bscript::ObjArray *&out_text, size_t textlen, bool ConvFromBE)
Definition: unicode.cpp:46
void addElement(BObjectImp *imp)
Definition: object.cpp:1399
Definition: berror.cpp:12