1 #include "dmStringUtil.h" 2 #include "xpl_Logger.h" 3 #include "dmt.hpp" 4 #include "dmMemory.h" 5 6 extern "C" { 7 #include "xpt-b64.h" 8 } 9 10 DmtData::DmtData() 11 { 12 metaFormat = SYNCML_DM_DATAFORMAT_UNDEFINED; 13 } 14 15 /** 16 * Combine STRING, DATE and TIME type objects through a single 17 * contructor 18 */ 19 DmtData::DmtData(CPCHAR szStr):metaFormat(SYNCML_DM_DATAFORMAT_STRING) 20 { 21 SetString(szStr, SYNCML_DM_DATAFORMAT_STRING); 22 } 23 24 DmtData::DmtData(CPCHAR szStr, SYNCML_DM_DATAFORMAT_T type): 25 metaFormat(type) 26 { 27 SetString(szStr, type); 28 } 29 30 DmtData::DmtData(INT32 integer) 31 { 32 SetInt(integer); 33 } 34 35 DmtData::DmtData(BOOLEAN b) 36 { 37 SetBoolean(b); 38 } 39 40 DmtData::DmtData(BOOLTYPE b) 41 { 42 SetBoolean(b?1:0); 43 } 44 45 DmtData::DmtData( const UINT8 * bin, INT32 len) 46 { 47 SetBinary(bin,len); 48 } 49 50 DmtData::DmtData( const DMStringVector& aChildren ) 51 { 52 SetNodeValue(aChildren); 53 } 54 55 DmtData::~DmtData() 56 { 57 } 58 59 SYNCML_DM_RET_STATUS_T DmtData::SetString(CPCHAR szStr, 60 SYNCML_DM_DATAFORMAT_T type) 61 { 62 // Extend this method to initialize other text/plain type 63 // DM data. 64 // 65 if (szStr==NULL) 66 { 67 metaFormat = SYNCML_DM_DATAFORMAT_NULL; 68 } else 69 { 70 m_strValue = szStr; 71 if ( szStr[0] && m_strValue.GetBuffer() == NULL ) 72 { 73 XPL_LOG_DM_API_Error(("DmtData::SetString : unable allocate memory\n")); 74 metaFormat = SYNCML_DM_DATAFORMAT_UNDEFINED; 75 return SYNCML_DM_DEVICE_FULL; 76 } 77 else if ( type == SYNCML_DM_DATAFORMAT_INT ) 78 { 79 m_nValue = DmAtoi(szStr); 80 } 81 else if ( type == SYNCML_DM_DATAFORMAT_BOOL ) 82 { 83 m_bValue = DmStrcasecmp(szStr, "false"); 84 } 85 else if ( type == SYNCML_DM_DATAFORMAT_FLOAT || 86 type == SYNCML_DM_DATAFORMAT_DATE || 87 type == SYNCML_DM_DATAFORMAT_TIME ) 88 { 89 m_strValue.trim(); 90 } 91 metaFormat = type; 92 } 93 return SYNCML_DM_SUCCESS; 94 } 95 96 SYNCML_DM_RET_STATUS_T DmtData::SetBoolean(BOOLEAN bValue) 97 { 98 m_bValue = bValue; 99 metaFormat = SYNCML_DM_DATAFORMAT_BOOL; 100 return SYNCML_DM_SUCCESS; 101 } 102 103 SYNCML_DM_RET_STATUS_T DmtData::SetInt(INT32 nValue) 104 { 105 m_nValue = nValue; 106 char numbuf[MAX_INT_STRING_LENGTH]; 107 DmSprintf( numbuf, "%d", m_nValue ); 108 m_strValue = numbuf; 109 metaFormat = SYNCML_DM_DATAFORMAT_INT; 110 return SYNCML_DM_SUCCESS; 111 112 } 113 114 SYNCML_DM_RET_STATUS_T DmtData::SetFloat(CPCHAR sFloat) 115 { 116 return SetString(sFloat, SYNCML_DM_DATAFORMAT_FLOAT); 117 } 118 119 120 SYNCML_DM_RET_STATUS_T DmtData::SetDate(CPCHAR sDate) 121 { 122 return SetString(sDate, SYNCML_DM_DATAFORMAT_DATE); 123 } 124 125 SYNCML_DM_RET_STATUS_T DmtData::SetTime(CPCHAR sTime) 126 { 127 return SetString(sTime, SYNCML_DM_DATAFORMAT_TIME); 128 } 129 130 SYNCML_DM_RET_STATUS_T DmtData::SetBinary(const UINT8 * buf, INT32 len) 131 { 132 if ( buf && len > 0 ) 133 { 134 m_pBinValue.set_size( len ); 135 if ( m_pBinValue.size() != len ) 136 { 137 XPL_LOG_DM_API_Error(("DmtData::SetBinary : unable allocate memory\n")); 138 m_pBinValue.clear(); 139 metaFormat = SYNCML_DM_DATAFORMAT_UNDEFINED; 140 return SYNCML_DM_DEVICE_FULL; 141 } 142 memcpy( m_pBinValue.get_data(), buf, len ); 143 } 144 else 145 m_pBinValue.clear(); 146 147 metaFormat = SYNCML_DM_DATAFORMAT_BIN; 148 return SYNCML_DM_SUCCESS; 149 } 150 151 SYNCML_DM_RET_STATUS_T DmtData::SetNodeValue( const DMStringVector& aChildren ) 152 { 153 154 m_astrNodeValue = aChildren; 155 if ( m_astrNodeValue.size() != aChildren.size() ) 156 { 157 XPL_LOG_DM_API_Error(("DmtData::SetNodeValue : unable allocate memory\n")); 158 metaFormat = SYNCML_DM_DATAFORMAT_UNDEFINED; 159 m_astrNodeValue.clear(); 160 return SYNCML_DM_DEVICE_FULL; 161 } 162 metaFormat = SYNCML_DM_DATAFORMAT_NODE; 163 return SYNCML_DM_SUCCESS; 164 } 165 166 167 SYNCML_DM_RET_STATUS_T DmtData::Set( const DmtData & oData ) 168 { 169 SYNCML_DM_RET_STATUS_T res = SYNCML_DM_SUCCESS; 170 switch (oData.GetType()) 171 { 172 case SYNCML_DM_DATAFORMAT_STRING: 173 case SYNCML_DM_DATAFORMAT_DATE: 174 case SYNCML_DM_DATAFORMAT_TIME: 175 res = SetString(oData.GetStringValue(), oData.GetType()); 176 break; 177 178 case SYNCML_DM_DATAFORMAT_BIN: 179 { 180 const DMVector<UINT8> & value = oData.GetBinaryValue(); 181 res = SetBinary(((DMVector<UINT8> &)value).get_data(),value.size()); 182 } 183 break; 184 185 case SYNCML_DM_DATAFORMAT_NODE: 186 res = SetNodeValue(oData.GetNodeValue()); 187 break; 188 189 case SYNCML_DM_DATAFORMAT_FLOAT: 190 case SYNCML_DM_DATAFORMAT_BOOL: 191 case SYNCML_DM_DATAFORMAT_INT: 192 case SYNCML_DM_DATAFORMAT_NULL: 193 case SYNCML_DM_DATAFORMAT_UNDEFINED: 194 *this = oData; 195 break; 196 197 } 198 return res; 199 } 200 201 SYNCML_DM_RET_STATUS_T DmtData::AddNodeValue( const DMString & sChild ) 202 { 203 204 INT32 size = m_astrNodeValue.size() + 1; 205 m_astrNodeValue.push_back(sChild); 206 if ( size != m_astrNodeValue.size() ) 207 { 208 XPL_LOG_DM_API_Error(("DmtData::SetNodeValue : unable allocate memory\n")); 209 return SYNCML_DM_DEVICE_FULL; 210 } 211 metaFormat = SYNCML_DM_DATAFORMAT_NODE; 212 return SYNCML_DM_SUCCESS; 213 } 214 215 const DMString & DmtData::GetStringValue() const 216 { 217 return m_strValue; 218 } 219 220 const DMVector<UINT8> & DmtData::GetBinaryValue() const 221 { 222 return m_pBinValue; 223 } 224 225 const DMStringVector & DmtData::GetNodeValue() const 226 { 227 return m_astrNodeValue; 228 } 229 230 SYNCML_DM_RET_STATUS_T DmtData::GetString( DMString& str ) const 231 { 232 if (metaFormat == SYNCML_DM_DATAFORMAT_BIN) 233 { 234 /* Base64 encode for the ./CSIM/Provobj node. */ 235 UINT32 dataSize = m_pBinValue.size(); 236 237 if (dataSize == 0) { 238 str = ""; 239 XPL_LOG_DM_API_Error(("DmtData::GetString() on empty BIN node returning empty string")); 240 return SYNCML_DM_SUCCESS; 241 } 242 243 UINT32 encLen = base64GetSize(dataSize); 244 XPL_LOG_DM_API_Error(("DmtData::GetString() dataSize=%d encLen=%d\n", dataSize, encLen)); 245 XPL_LOG_DM_API_Error(("DmtData::GetString() [0]=%02x [1]=%02x [%d]=%02x\n", m_pBinValue.get_data()[0], 246 m_pBinValue.get_data()[1], dataSize - 1, m_pBinValue.get_data()[dataSize - 1])); 247 248 UINT8* pEncData = (UINT8 *)DmAllocMem(encLen+1); 249 memset(pEncData, 0, encLen+1); 250 UINT32 offset = 0; 251 int totalSize = base64Encode(pEncData, encLen, (DataBuffer_t)m_pBinValue.get_data(), 252 (BufferSize_t *)&dataSize, (BufferSize_t *)&offset, 0, NULL); 253 254 str = DMString( reinterpret_cast<CPCHAR>(pEncData) ); 255 DmFreeMem(pEncData); 256 XPL_LOG_DM_API_Error(("DmtData::GetString() on BIN node returning: %s\n", str.c_str())); 257 return SYNCML_DM_SUCCESS; 258 } 259 260 if (metaFormat == SYNCML_DM_DATAFORMAT_NODE) 261 { 262 /* NODE conversion to String not allowed */ 263 return SYNCML_DM_INVALID_PARAMETER; 264 } 265 266 if ( (metaFormat == SYNCML_DM_DATAFORMAT_STRING) || 267 (metaFormat == SYNCML_DM_DATAFORMAT_INT) || 268 (metaFormat == SYNCML_DM_DATAFORMAT_FLOAT) || 269 (metaFormat == SYNCML_DM_DATAFORMAT_DATE) || 270 (metaFormat == SYNCML_DM_DATAFORMAT_TIME) ) 271 { 272 str = m_strValue; 273 return SYNCML_DM_SUCCESS; 274 } 275 276 if (metaFormat == SYNCML_DM_DATAFORMAT_BOOL) 277 { 278 str = m_bValue ? "true" : "false"; 279 return SYNCML_DM_SUCCESS; 280 } 281 282 if (metaFormat == SYNCML_DM_DATAFORMAT_NULL) 283 { 284 str = ""; 285 return SYNCML_DM_SUCCESS; 286 } 287 288 289 /* unknown type - string conversiob is not allowed */ 290 return SYNCML_DM_INVALID_PARAMETER; 291 } 292 293 SYNCML_DM_RET_STATUS_T DmtData::GetBoolean( BOOLEAN& bValue ) const 294 { 295 bValue = FALSE; 296 if (metaFormat != SYNCML_DM_DATAFORMAT_BOOL) 297 return SYNCML_DM_INVALID_PARAMETER; 298 299 bValue = m_bValue; 300 return SYNCML_DM_SUCCESS; 301 } 302 303 SYNCML_DM_RET_STATUS_T DmtData::GetBoolean( BOOLTYPE& bValue ) const 304 { 305 bValue = FALSE; 306 if (metaFormat != SYNCML_DM_DATAFORMAT_BOOL) 307 return SYNCML_DM_INVALID_PARAMETER; 308 309 bValue = m_bValue?TRUE:FALSE; 310 return SYNCML_DM_SUCCESS; 311 } 312 313 SYNCML_DM_RET_STATUS_T DmtData::GetInt( INT32& nValue ) const 314 { 315 if (metaFormat != SYNCML_DM_DATAFORMAT_INT) 316 return SYNCML_DM_INVALID_PARAMETER; 317 318 nValue = m_nValue; 319 return SYNCML_DM_SUCCESS; 320 } 321 322 SYNCML_DM_RET_STATUS_T DmtData::GetFloat( DMString& sFloat ) const 323 { 324 if (metaFormat != SYNCML_DM_DATAFORMAT_FLOAT) 325 return SYNCML_DM_INVALID_PARAMETER; 326 327 sFloat = m_strValue; 328 return SYNCML_DM_SUCCESS; 329 } 330 331 SYNCML_DM_RET_STATUS_T DmtData::GetDate( DMString& sDate ) const 332 { 333 if (metaFormat != SYNCML_DM_DATAFORMAT_DATE) 334 return SYNCML_DM_INVALID_PARAMETER; 335 336 sDate = m_strValue; 337 return SYNCML_DM_SUCCESS; 338 } 339 340 SYNCML_DM_RET_STATUS_T DmtData::GetTime( DMString& sTime ) const 341 { 342 if (metaFormat != SYNCML_DM_DATAFORMAT_TIME) 343 return SYNCML_DM_INVALID_PARAMETER; 344 345 sTime = m_strValue; 346 return SYNCML_DM_SUCCESS; 347 } 348 349 SYNCML_DM_RET_STATUS_T DmtData::GetBinary(DMVector<UINT8>& buffer ) const 350 { 351 if (metaFormat != SYNCML_DM_DATAFORMAT_BIN) 352 return SYNCML_DM_INVALID_PARAMETER; 353 354 buffer = m_pBinValue; 355 return SYNCML_DM_SUCCESS; 356 } 357 358 SYNCML_DM_RET_STATUS_T DmtData::GetNodeValue( DMStringVector& aChildren ) const 359 { 360 if (metaFormat != SYNCML_DM_DATAFORMAT_NODE) 361 return SYNCML_DM_INVALID_PARAMETER; 362 363 aChildren = m_astrNodeValue; 364 return SYNCML_DM_SUCCESS; 365 } 366 //-------------------------------------------------------------------------------------------- 367 // FUNCTION : DmtData::GetSize 368 // DESCRIPTION : Get data size 369 // ARGUMENTS PASSED: 370 // 371 // RETURN VALUE : SYNCML_DM_RET_STATUS_T : Returns SYNCML_DM_SUCCESS if success, otherwise fails 372 // 373 //-------------------------------------------------------------------------------------------- 374 SYNCML_DM_RET_STATUS_T DmtData::GetSize(INT32 &dataSize) const 375 { 376 SYNCML_DM_RET_STATUS_T res = SYNCML_DM_SUCCESS; 377 switch(metaFormat) 378 { 379 case SYNCML_DM_DATAFORMAT_STRING: 380 dataSize = m_strValue.length(); 381 break; 382 383 case SYNCML_DM_DATAFORMAT_BIN: 384 dataSize = m_pBinValue.size(); 385 break; 386 387 case SYNCML_DM_DATAFORMAT_NODE: 388 dataSize = m_astrNodeValue.size(); 389 break; 390 391 case SYNCML_DM_DATAFORMAT_BOOL: 392 case SYNCML_DM_DATAFORMAT_INT: 393 case SYNCML_DM_DATAFORMAT_NULL: 394 case SYNCML_DM_DATAFORMAT_FLOAT: 395 case SYNCML_DM_DATAFORMAT_DATE: 396 case SYNCML_DM_DATAFORMAT_TIME: 397 case SYNCML_DM_DATAFORMAT_UNDEFINED: 398 { DMString strValue; 399 res = GetString(strValue); 400 if(res == SYNCML_DM_SUCCESS) 401 dataSize = strValue.length(); 402 } 403 break; 404 405 } 406 return res; 407 } 408 409