Permalink
Browse files

Revision: 21213

  • Loading branch information...
1 parent 5558e14 commit 5229598dc73b06947a10fbb409dfe91ffa1be5f2 @wangzhonnew wangzhonnew committed Feb 16, 2016
Showing with 5,305 additions and 1,949 deletions.
  1. +5 −2 SequoiaDB/engine/SConscript
  2. +14 −7 SequoiaDB/engine/bson/bson-inl.h
  3. +2 −2 SequoiaDB/engine/bson/bsonassert.h
  4. +34 −13 SequoiaDB/engine/bson/util/builder.h
  5. +23 −1 SequoiaDB/engine/cat/catMainController.cpp
  6. +69 −57 SequoiaDB/engine/client/base64c.c
  7. +25 −17 SequoiaDB/engine/client/bson/bson.c
  8. +16 −8 SequoiaDB/engine/client/cJSON.c
  9. +8 −1 SequoiaDB/engine/client/cJSON.h
  10. +70 −15 SequoiaDB/engine/client/client.c
  11. +27 −0 SequoiaDB/engine/client/client.h
  12. +37 −3 SequoiaDB/engine/client/client.hpp
  13. +9 −0 SequoiaDB/engine/client/clientImpl.hpp
  14. +41 −11 SequoiaDB/engine/client/clientcpp.cpp
  15. +183 −97 SequoiaDB/engine/client/jstobs.c
  16. +7 −1 SequoiaDB/engine/client/jstobs.h
  17. +1 −0 SequoiaDB/engine/client/network.c
  18. +32 −22 SequoiaDB/engine/cls/clsCatalogPredicate.cpp
  19. +1 −0 SequoiaDB/engine/cls/clsReplBucket.cpp
  20. +11 −1 SequoiaDB/engine/cls/clsReplSession.cpp
  21. +41 −11 SequoiaDB/engine/cls/clsReplicateSet.cpp
  22. +34 −5 SequoiaDB/engine/cls/clsShardSession.cpp
  23. +3 −3 SequoiaDB/engine/cls/clsTask.cpp
  24. +15 −4 SequoiaDB/engine/cls/clsVSSecondary.cpp
  25. +10 −9 SequoiaDB/engine/cls/clsVoteStatus.cpp
  26. +33 −5 SequoiaDB/engine/dms/dmsCB.cpp
  27. +413 −0 SequoiaDB/engine/dms/dmsIndexBuilder.cpp
  28. +367 −0 SequoiaDB/engine/dms/dmsIndexBuilderImpl.cpp
  29. +2 −2 SequoiaDB/engine/dms/dmsInspect.cpp
  30. +9 −0 SequoiaDB/engine/dms/dmsScanner.cpp
  31. +15 −15 SequoiaDB/engine/dms/dmsStorageData.cpp
  32. +79 −210 SequoiaDB/engine/dms/dmsStorageIndex.cpp
  33. +8 −8 SequoiaDB/engine/dms/dmsStorageLob.cpp
  34. +3 −2 SequoiaDB/engine/dms/dmsStorageUnit.cpp
  35. +68 −20 SequoiaDB/engine/dps/dpsLogRecord.cpp
  36. +12 −2 SequoiaDB/engine/dps/dpsReplicaLogMgr.cpp
  37. +32 −10 SequoiaDB/engine/dps/dpsTransCB.cpp
  38. +44 −0 SequoiaDB/engine/dps/dpsTransLock.cpp
  39. +10 −1 SequoiaDB/engine/dps/dpsTransLockBucket.cpp
  40. +46 −0 SequoiaDB/engine/dps/dpsTransLockDef.cpp
  41. +2 −1 SequoiaDB/engine/include/clsCatalogPredicate.hpp
  42. +3 −0 SequoiaDB/engine/include/clsMgr.hpp
  43. +2 −0 SequoiaDB/engine/include/clsReplSession.hpp
  44. +3 −1 SequoiaDB/engine/include/clsReplicateSet.hpp
  45. +1 −0 SequoiaDB/engine/include/clsShardSession.hpp
  46. +3 −0 SequoiaDB/engine/include/clsVSSecondary.hpp
  47. +2 −0 SequoiaDB/engine/include/dms.hpp
  48. +8 −0 SequoiaDB/engine/include/dmsCB.hpp
  49. +103 −0 SequoiaDB/engine/include/dmsIndexBuilder.hpp
  50. +88 −0 SequoiaDB/engine/include/dmsIndexBuilderImpl.hpp
  51. +101 −0 SequoiaDB/engine/include/dmsIxmKeySorter.hpp
  52. +14 −3 SequoiaDB/engine/include/dmsStorageIndex.hpp
  53. +2 −1 SequoiaDB/engine/include/dmsStorageUnit.hpp
  54. +4 −0 SequoiaDB/engine/include/dpsLogRecord.hpp
  55. +18 −2 SequoiaDB/engine/include/dpsTransCB.hpp
  56. +16 −0 SequoiaDB/engine/include/dpsTransLock.hpp
  57. +2 −0 SequoiaDB/engine/include/dpsTransLockBucket.hpp
  58. +9 −1 SequoiaDB/engine/include/dpsTransLockDef.hpp
  59. +10 −0 SequoiaDB/engine/include/msgDef.h
  60. +27 −1 SequoiaDB/engine/include/mthMatcher.hpp
  61. +1 −1 SequoiaDB/engine/include/mthNodePool.hpp
  62. +9 −3 SequoiaDB/engine/include/mthSAction.hpp
  63. +0 −2 SequoiaDB/engine/include/mthSColumnMatrix.hpp
  64. +9 −0 SequoiaDB/engine/include/netEventHandler.hpp
  65. +1 −1 SequoiaDB/engine/include/optAccessPlan.hpp
  66. +3 −3 SequoiaDB/engine/include/ossUtil.hpp
  67. +2 −1 SequoiaDB/engine/include/ossVer.h
  68. +1 −1 SequoiaDB/engine/include/ossVer_Autogen.h
  69. +9 −0 SequoiaDB/engine/include/pmdEDU.hpp
  70. +2 −0 SequoiaDB/engine/include/pmdEDUMgr.hpp
  71. +5 −2 SequoiaDB/engine/include/pmdObjBase.hpp
  72. +4 −1 SequoiaDB/engine/include/pmdOptionsMgr.hpp
  73. +12 −4 SequoiaDB/engine/include/qgmDef.hpp
  74. +1 −0 SequoiaDB/engine/include/qgmHintDef.hpp
  75. +3 −0 SequoiaDB/engine/include/qgmOptiNLJoin.hpp
  76. +4 −1 SequoiaDB/engine/include/qgmOptiSelect.hpp
  77. +3 −0 SequoiaDB/engine/include/qgmUtil.hpp
  78. +2 −1 SequoiaDB/engine/include/restAdaptor.hpp
  79. +2 −1 SequoiaDB/engine/include/rtn.hpp
  80. +31 −0 SequoiaDB/engine/include/rtnCommand.hpp
  81. +4 −0 SequoiaDB/engine/include/rtnCommandDef.hpp
  82. +44 −1 SequoiaDB/engine/include/rtnContext.hpp
  83. +30 −0 SequoiaDB/engine/include/rtnCoordCommands.hpp
  84. +2 −1 SequoiaDB/engine/include/rtnCoordCommon.hpp
  85. +0 −3 SequoiaDB/engine/include/rtnCoordOperator.hpp
  86. +81 −0 SequoiaDB/engine/include/rtnIxmKeySorter.hpp
  87. +14 −10 SequoiaDB/engine/include/spd.h
  88. +13 −2 SequoiaDB/engine/include/utilArray.hpp
  89. +267 −147 SequoiaDB/engine/mth/mthMatcher.cpp
  90. +1 −1 SequoiaDB/engine/mth/mthModifier.cpp
  91. +2 −1 SequoiaDB/engine/mth/mthSAction.cpp
  92. +3 −2 SequoiaDB/engine/mth/mthSColumn.cpp
  93. +1 −0 SequoiaDB/engine/net/netEventHandler.cpp
  94. +32 −13 SequoiaDB/engine/net/netFrame.cpp
  95. +12 −6 SequoiaDB/engine/oss/ossIO.cpp
  96. +1 −1 SequoiaDB/engine/oss/ossMem.cpp
  97. +32 −26 SequoiaDB/engine/oss/ossNPipe.cpp
  98. +8 −0 SequoiaDB/engine/oss/ossPath.cpp
  99. +1 −1 SequoiaDB/engine/oss/ossUtil.cpp
  100. +23 −2 SequoiaDB/engine/pmd/pmdCBMgrEntryPoint.cpp
  101. +40 −1 SequoiaDB/engine/pmd/pmdEDU.cpp
  102. +18 −0 SequoiaDB/engine/pmd/pmdEDUMgr.cpp
  103. +1 −0 SequoiaDB/engine/pmd/pmdMain.cpp
  104. +15 −2 SequoiaDB/engine/pmd/pmdOptionsMgr.cpp
  105. +3 −15 SequoiaDB/engine/pmd/pmdRestSession.cpp
  106. +4 −1 SequoiaDB/engine/pmd/pmdSession.cpp
  107. +2 −2 SequoiaDB/engine/pmd/sdbimprt.cpp
  108. +2 −2 SequoiaDB/engine/pmd/sdbinspt.cpp
  109. +15 −1 SequoiaDB/engine/pmd/sdbtop.cpp
  110. +1 −1 SequoiaDB/engine/qgm/qgmBuilder.cpp
  111. +67 −0 SequoiaDB/engine/qgm/qgmOptiNLJoin.cpp
  112. +62 −0 SequoiaDB/engine/qgm/qgmOptiSelect.cpp
  113. +53 −0 SequoiaDB/engine/qgm/qgmUtil.cpp
  114. +47 −13 SequoiaDB/engine/rest/restAdaptor.cpp
  115. +2 −2 SequoiaDB/engine/rtn/rtnBackgroundJob.cpp
  116. +29 −1 SequoiaDB/engine/rtn/rtnCB.cpp
  117. +90 −2 SequoiaDB/engine/rtn/rtnCommand.cpp
  118. +3 −2 SequoiaDB/engine/rtn/rtnCommandImpl.cpp
  119. +178 −7 SequoiaDB/engine/rtn/rtnContext.cpp
  120. +2 −0 SequoiaDB/engine/rtn/rtnCoord.cpp
  121. +187 −0 SequoiaDB/engine/rtn/rtnCoordCommands.cpp
  122. +6 −2 SequoiaDB/engine/rtn/rtnCoordCommon.cpp
  123. +6 −2 SequoiaDB/engine/rtn/rtnCoordDelete.cpp
  124. +5 −8 SequoiaDB/engine/rtn/rtnCoordInsert.cpp
  125. +1 −23 SequoiaDB/engine/rtn/rtnCoordOperator.cpp
  126. +5 −2 SequoiaDB/engine/rtn/rtnCoordUpdate.cpp
  127. +213 −0 SequoiaDB/engine/rtn/rtnIxmKeySorter.cpp
  128. +15 −9 SequoiaDB/engine/rtn/rtnPredicate.cpp
  129. +4 −2 SequoiaDB/engine/rtn/rtnReorg.cpp
  130. +73 −14 SequoiaDB/engine/rtn/rtnTransaction.cpp
  131. +15 −5 SequoiaDB/engine/spd/spdFMPMgr.cpp
  132. +98 −37 SequoiaDB/engine/spt/dbClasses.cpp
  133. +49 −36 SequoiaDB/engine/spt/sptConvertor.cpp
  134. +27 −14 SequoiaDB/engine/spt/sptConvertor2.cpp
  135. +27 −18 SequoiaDB/engine/util/csv2rawbson.cpp
  136. +20 −17 SequoiaDB/engine/util/rawbson2csv.c
  137. +2 −0 client/admin/admintpl/php/ajax/a-data_w.php
  138. +28 −6 client/admin/admintpl/php/error_cn.php
  139. +30 −8 client/admin/admintpl/php/error_en.php
  140. +73 −20 driver/C#.Net/Driver/base/DBCollection.cs
  141. +1 −1 driver/C#.Net/Driver/base/Domain.cs
  142. +1 −1 driver/C#.Net/Driver/base/ReplicaGroup.cs
  143. +2 −0 driver/C#.Net/Driver/base/SequoiadbConstants.cs
  144. +89 −5 driver/C#.Net/DriverTest/DBCollectionTest.cs
  145. +80 −24 driver/java/src/main/java/com/sequoiadb/base/DBCollection.java
  146. +3 −1 driver/java/src/main/java/com/sequoiadb/base/SequoiadbConstants.java
  147. +1 −1 driver/java/src/main/java/com/sequoiadb/util/SDBMessageHelper.java
  148. +7 −5 driver/php5/ext/function_sdb.cpp
  149. +6 −5 driver/php5/ext/function_sdb.h
  150. +29 −7 driver/php5/ext/sequoiadb.cpp
  151. +8 −2 driver/php5/ext/sequoiadb.h
  152. +3 −23 driver/python/pysequoiadb/client.py
  153. +252 −30 driver/python/pysequoiadb/collection.py
  154. +0 −1 driver/python/pysequoiadb/collectionspace.py
  155. +77 −738 driver/python/pysequoiadb/pysequoiadb.cpp
  156. +63 −8 driver/python/pysequoiadb/replicagroup.py
  157. +3 −0 driver/python/pysequoiadb/util.hpp
  158. +71 −0 driver/test/java/com/sequoiadb/test/TestIndex.java
  159. +48 −0 driver/test/python/sequoiadb/DBCollectionTruncate.py
  160. +8 −6 driver/test/python/sequoiadb/TestPySequoiadb.py
  161. +65 −0 driver/test/python/sequoiadb/findAndModify.py
  162. +16 −0 misc/autogen/optlist.xml
  163. +2 −2 misc/autogen/rclist.xml
@@ -205,7 +205,8 @@ rtnFiles = [
"rtn/rtnCoordLob.cpp",
"rtn/rtnContextShdOfLob.cpp",
"rtn/rtnLobFetcher.cpp",
- "rtn/rtnContextListLob.cpp"
+ "rtn/rtnContextListLob.cpp",
+ "rtn/rtnIxmKeySorter.cpp"
]
msgFiles = [
@@ -236,7 +237,9 @@ dmsFiles = [
"dms/dmsStorageLobData.cpp",
"dms/dmsLobDirectBuffer.cpp",
"dms/dmsLobDirectInBuffer.cpp",
- "dms/dmsLobDirectOutBuffer.cpp"
+ "dms/dmsLobDirectOutBuffer.cpp",
+ "dms/dmsIndexBuilder.cpp",
+ "dms/dmsIndexBuilderImpl.cpp"
]
ixmFiles = [
@@ -131,7 +131,7 @@ namespace bson {
ss << " first element: " << e.toString();
}
catch ( ... ) { }
- massert( 10334 , ss.str() , 0 );
+ massert( 10334 , ss.str().c_str() , 0 );
}
/* the idea with NOINLINE_DECL here is to keep this from inlining in the
@@ -839,15 +839,22 @@ namespace bson {
char *pBase64Buf = NULL ;
const char* data = binDataClean(len);
base64_size = getEnBase64Size( len ) ;
- pBase64Buf = (char *)malloc( base64_size + 1 ) ;
- if ( pBase64Buf )
+ if( len > 0 )
{
- memset( pBase64Buf, 0, base64_size + 1 ) ;
- if ( base64Encode( data, len, pBase64Buf, base64_size ) )
+ pBase64Buf = (char *)malloc( base64_size + 1 ) ;
+ if ( pBase64Buf )
{
- s << pBase64Buf << "\", \"$type\": \"" << binDataType() ;
+ memset( pBase64Buf, 0, base64_size + 1 ) ;
+ if ( base64Encode( data, len, pBase64Buf, base64_size ) >= 0 )
+ {
+ s << pBase64Buf << "\", \"$type\": \"" << binDataType() ;
+ }
+ free( pBase64Buf ) ;
}
- free( pBase64Buf ) ;
+ }
+ else if( len == 0 )
+ {
+ s << "\", \"$type\": \"" << binDataType() ;
}
}
s << "\" }" ;
@@ -72,15 +72,15 @@ namespace bson {
throw bson::assertion( msgid , s );
}
- inline void uassert(unsigned msgid, std::string msg, bool expr) {
+ inline void uassert(unsigned msgid, const char* msg, bool expr) {
if( !expr )
uasserted( msgid , msg );
}
inline void msgasserted(int msgid, const char *msg) {
throw bson::assertion( msgid , msg );
}
inline void msgasserted(int msgid, const std::string &msg) { msgasserted(msgid, msg.c_str()); }
- inline void massert(unsigned msgid, std::string msg, bool expr) {
+ inline void massert(unsigned msgid, const char* msg, bool expr) {
if(!expr) {
std::cout << "assertion failure in bson library: " << msgid << ' ' << msg << std::endl;
throw bson::assertion( msgid , msg );
@@ -17,6 +17,7 @@
#pragma once
+#include <cfloat>
#include <string>
#include <string.h>
#include <stdio.h>
@@ -244,36 +245,49 @@ accesses) is the same as if
#pragma warning( disable : 4996 )
#endif
+#if defined(_WIN32) && _MSC_VER < 1900
+#pragma push_macro("snprintf")
+#define snprintf _snprintf
+#endif
+
/** stringstream deals with locale so this is a lot faster than std::stringstream for UTF8 */
class StringBuilder {
public:
+ static const size_t SDB_DBL_SIZE = 3 + DBL_MANT_DIG - DBL_MIN_EXP + 1;
+ static const size_t SDB_S32_SIZE = 12;
+ static const size_t SDB_U32_SIZE = 11;
+ static const size_t SDB_S64_SIZE = 23;
+ static const size_t SDB_U64_SIZE = 22;
+ static const size_t SDB_S16_SIZE = 7;
+ static const size_t SDB_PTR_SIZE = 19;
+
StringBuilder( int initsize=256 )
: _buf( initsize ) {
}
StringBuilder& operator<<( double x ) {
- return SBNUM( x , 25 , "%g" );
+ return SBNUM( x , SDB_DBL_SIZE, "%g" );
}
StringBuilder& operator<<( int x ) {
- return SBNUM( x , 11 , "%d" );
+ return SBNUM( x , SDB_S32_SIZE, "%d" );
}
StringBuilder& operator<<( unsigned x ) {
- return SBNUM( x , 11 , "%u" );
+ return SBNUM( x , SDB_U32_SIZE, "%u" );
}
StringBuilder& operator<<( long x ) {
- return SBNUM( x , 22 , "%ld" );
+ return SBNUM( x , SDB_S64_SIZE, "%ld" );
}
StringBuilder& operator<<( unsigned long x ) {
- return SBNUM( x , 22 , "%lu" );
+ return SBNUM( x , SDB_U64_SIZE, "%lu" );
}
StringBuilder& operator<<( long long x ) {
- return SBNUM( x , 22 , "%lld" );
+ return SBNUM( x , SDB_S64_SIZE, "%lld" );
}
StringBuilder& operator<<( unsigned long long x ) {
- return SBNUM( x , 22 , "%llu" );
+ return SBNUM( x , SDB_U64_SIZE, "%llu" );
}
StringBuilder& operator<<( short x ) {
- return SBNUM( x , 8 , "%hd" );
+ return SBNUM( x , SDB_S16_SIZE, "%hd" );
}
StringBuilder& operator<<( char c ) {
_buf.grow( 1 )[0] = c;
@@ -282,12 +296,14 @@ accesses) is the same as if
void appendDoubleNice( double x ) {
int prev = _buf.l;
- char * start = _buf.grow( 32 );
- int z = sprintf( start , "%.16g" , x );
+ const int maxSize = 32 ;
+ char * start = _buf.grow( maxSize );
+ int z = snprintf( start, maxSize, "%.16g" , x );
assert( z >= 0 );
_buf.l = prev + z;
- if( strchr(start, '.') == 0 && strchr(start, 'E') == 0
- && strchr(start, 'N') == 0 ) {
+ if( strchr(start, '.') == 0 && strchr(start, 'E') == 0 &&
+ strchr(start, 'N') == 0 && strchr(start, 'e') == 0 &&
+ strchr(start, 'n') == 0 ) {
write( ".0" , 2 );
}
}
@@ -318,7 +334,7 @@ accesses) is the same as if
template <typename T>
StringBuilder& SBNUM(T val,int maxSize,const char *macro) {
int prev = _buf.l;
- int z = sprintf( _buf.grow(maxSize) , macro , (val) );
+ int z = snprintf( _buf.grow(maxSize) , maxSize, macro , (val) );
assert( z >= 0 );
_buf.l = prev + z;
return *this;
@@ -329,4 +345,9 @@ accesses) is the same as if
#pragma warning( pop )
#endif
+#if defined(_WIN32) && _MSC_VER < 1900
+#undef snprintf
+#pragma pop_macro("snprintf")
+#endif
+
} // namespace bson
@@ -236,7 +236,29 @@ namespace engine
PD_TRACE1 ( SDB_CATMAINCT_HANDLEMSG,
PD_PACK_INT ( header->opCode ) ) ;
- rc = _postMsg( handle, header ) ;
+ if ( (UINT32)MSG_SYSTEM_INFO_LEN == (UINT32)header->messageLength )
+ {
+ MsgSysInfoReply reply ;
+ MsgSysInfoReply *pReply = &reply ;
+ INT32 replySize = sizeof(reply) ;
+
+ rc = msgBuildSysInfoReply ( (CHAR**)&pReply, &replySize ) ;
+ if ( rc )
+ {
+ PD_LOG( PDERROR, "Failed to build sys info reply, rc: %d", rc ) ;
+ rc = SDB_NET_BROKEN_MSG ;
+ }
+ else
+ {
+ rc = _pCatCB->netWork()->syncSendRaw( handle,
+ (const CHAR *)pReply,
+ (UINT32)replySize ) ;
+ }
+ }
+ else
+ {
+ rc = _postMsg( handle, header ) ;
+ }
PD_TRACE_EXITRC ( SDB_CATMAINCT_HANDLEMSG, rc ) ;
return rc ;
@@ -33,6 +33,10 @@ int getEnBase64Size ( int size )
{
int len = size ;
int zeroize = len % 3 ;
+ if( size == 0 )
+ {
+ return 0 ;
+ }
len = ( len + ( zeroize ? 3 - zeroize : 0 ) ) / 3 * 4 + 1 ;
return len ;
}
@@ -41,6 +45,18 @@ int getDeBase64Size ( const char *s )
{
int len = strlen ( s ) ;
int zeroize = 0 ;
+ if( !s )
+ {
+ return -1 ;
+ }
+ if( len == 0 )
+ {
+ return 0 ;
+ }
+ else if( len % 4 > 0 )
+ {
+ return -1 ;
+ }
if ( '=' == s [ len - 2 ] )
zeroize = 2 ;
else if ( '=' == s [ len - 1 ] )
@@ -55,47 +71,51 @@ int base64Encode ( const char *s, int in_size, char *_ret, int out_size )
char t = 0x00 ;
int vLen = 0 ;
int len = in_size ;
- if ( out_size < getEnBase64Size ( in_size ) )
+ if( !s || !_ret )
+ {
+ return -1 ;
+ }
+ /* empty string */
+ if( len == 0 )
+ {
return 0 ;
+ }
+ if ( out_size < getEnBase64Size ( in_size ) )
+ {
+ return -1 ;
+ }
while ( len > 0 )
{
- /* This is not support chinese
- *_ret++ = B64[ ( s [ 0 ] >> 2 ) & 0x3F ] ;
- if ( len > 2 )
- {
- *_ret++ = B64[ ( ( s [ 0 ] & 3 ) << 4 ) | ( s [ 1 ] >> 4 ) ] ;
- *_ret++ = B64[ ( ( s [ 1 ] & 0xF ) << 2 ) | ( s [ 2 ] >> 6 ) ] ;
- *_ret++ = B64[ ( s [ 2 ] & 0x3F ) ] ;
- }*/
- /* This is support chinese */
c = ( s [ 0 ] >> 2 ) ;
c = c & 0x3F;
*_ret++ = B64[ (int)c ] ;
if ( len > 2 )
{
- c = ( s [ 0 ] & 3 ) << 4 ;
- t = ( s [ 1 ] >> 4 ) & 0x0F ;
+ c = ( (unsigned char)s[0] & 3 ) << 4 ;
+ t = ( (unsigned char)s[1] >> 4 ) & 0x0F ;
*_ret++ = B64 [ ( c ) | ( t ) ] ;
- c = ( s [ 1 ] & 0xF ) << 2 ;
- t = ( s [ 2 ] >> 6 ) & 0x3 ;
+ c = ( (unsigned char)s[1] & 0xF ) << 2 ;
+ t = ( (unsigned char)s[2] >> 6 ) & 0x3 ;
*_ret++ = B64 [ ( c ) | ( t ) ] ;
- c = s [ 2 ] & 0x3F ;
+ c = ( (unsigned char)s[2] & 0x3F ) ;
*_ret++ = B64 [ (int)c ] ;
}
else
{
switch ( len )
{
- case 1:
- *_ret++ = B64[ ( s [ 0 ] & 3 ) << 4 ] ;
- *_ret++ = '=' ;
- *_ret++ = '=' ;
- break ;
- case 2:
- *_ret++ = B64[ ( ( s [ 0 ] & 3 ) << 4 ) | ( s [ 1 ] >> 4 ) ] ;
- *_ret++ = B64[ ( ( s [ 1 ] & 0x0F ) << 2 ) | ( s [ 2 ] >> 6 ) ] ;
- *_ret++ = '=' ;
- break ;
+ case 1:
+ *_ret++ = B64[ ( (unsigned char)s[0] & 3 ) << 4 ] ;
+ *_ret++ = '=' ;
+ *_ret++ = '=' ;
+ break ;
+ case 2:
+ *_ret++ = B64[ ( ( (unsigned char)s[0] & 3 ) << 4 ) |
+ ( (unsigned char)s[1] >> 4 ) ] ;
+ *_ret++ = B64[ ( ( (unsigned char)s[1] & 0x0F ) << 2 ) |
+ ( (unsigned char)s[2] >> 6 ) ] ;
+ *_ret++ = '=' ;
+ break ;
}
}
s += 3 ;
@@ -140,52 +160,44 @@ int base64Decode ( const char *s, char *_ret, int out_size )
static char lpCode [ 4 ] ;
int vLen = 0 ;
int len = strlen ( s ) ;
-
- /* base64 must be 4 bytes aligned */
- if ( ( len % 4 ) ||
- ( out_size < getDeBase64Size ( s ) ) )
+ if( !s || !_ret )
+ {
+ return -1 ;
+ }
+ /* empty base64 */
+ if( len == 0 )
+ {
return 0 ;
-
-/* this is error code
- while ( p < e )
- {
- memcpy ( unit, p, 4 ) ;
- if ( unit[3] == '=' )
- unit[3] = 0 ;
- if ( unit[2] == '=' )
- unit[2] = 0 ;
- p += 4 ;
-
- for ( i = 0 ; unit[0] != B64[i] && i < 64 ; i++ ) ;
- unit[0] = i==64 ? 0 : i ;
- for ( i = 0 ; unit[1] != B64[i] && i < 64 ; i++ ) ;
- unit[1] = i==64 ? 0 : i ;
- for ( i = 0 ; unit[2] != B64[i] && i < 64 ; i++ ) ;
- unit[2] = i==64 ? 0 : i ;
- for ( i = 0 ; unit[3] != B64[i] && i < 64 ; i++ ) ;
- unit[3] = i==64 ? 0 : i ;
- *r++ = (unit[0]<<2) | (unit[1]>>4) ;
- *r++ = (unit[1]<<4) | (unit[2]>>2) ;
- *r++ = (unit[2]<<6) | unit[3] ;
- }
- *r = 0 ;
- return _ret ;
-*/
- while ( len > 2 )
+ }
+ /* base64 must be 4 bytes aligned */
+ if( ( len % 4 ) ||
+ ( out_size < getDeBase64Size ( s ) ) )
+ {
+ return -1 ;
+ }
+ while( len >= 4 )
{
lpCode [ 0 ] = getCharIndex ( s [ 0 ] ) ;
lpCode [ 1 ] = getCharIndex ( s [ 1 ] ) ;
lpCode [ 2 ] = getCharIndex ( s [ 2 ] ) ;
lpCode [ 3 ] = getCharIndex ( s [ 3 ] ) ;
+ if( out_size <= 1 )
+ return vLen ;
*_ret++ = ( lpCode [ 0 ] << 2 ) | ( lpCode [ 1 ] >> 4 ) ;
+ --out_size ;
+ if( out_size <= 1 )
+ return vLen ;
*_ret++ = ( lpCode [ 1 ] << 4 ) | ( lpCode [ 2 ] >> 2 ) ;
+ --out_size ;
+ if( out_size <= 1 )
+ return vLen ;
*_ret++ = ( lpCode [ 2 ] << 6 ) | ( lpCode [ 3 ] ) ;
+ --out_size ;
s += 4 ;
len -= 4 ;
vLen += 3 ;
}
-
return vLen ;
}
Oops, something went wrong.

0 comments on commit 5229598

Please sign in to comment.