//----------------------------------------------------------------// bool MOAILuaState::Encode ( int idx, ZLStreamWriter& writer ) { if ( !this->IsType ( idx, LUA_TSTRING )) return false; size_t len; cc8* buffer = lua_tolstring ( this->mState, idx, &len ); if ( !len ) return false; ZLMemStream stream; writer.Open ( stream ); writer.WriteBytes ( buffer, len ); writer.Close (); len = stream.GetLength (); void* temp = malloc ( len ); stream.Seek ( 0, SEEK_SET ); stream.ReadBytes (( void* )temp, len ); lua_pushlstring ( this->mState, ( cc8* )temp, len ); free ( temp ); return true; }
//----------------------------------------------------------------// bool MOAILuaState::Decode ( int idx, ZLStreamReader& reader ) { if ( !this->IsType ( idx, LUA_TSTRING )) return false; size_t len; void* buffer = ( void* )lua_tolstring ( this->mState, idx, &len ); if ( !len ) return false; ZLByteStream cryptStream; cryptStream.SetBuffer ( buffer, len ); cryptStream.SetLength ( len ); ZLMemStream plainStream; reader.Open ( cryptStream ); plainStream.WriteStream ( reader ); reader.Close (); len = plainStream.GetLength (); buffer = malloc ( len ); plainStream.Seek ( 0, SEEK_SET ); plainStream.ReadBytes ( buffer, len ); lua_pushlstring ( this->mState, ( cc8* )buffer, len ); free ( buffer ); return true; }
//----------------------------------------------------------------// bool MOAIDataBuffer::Encode ( ZLStreamWriter& writer ) { this->mMutex.Lock (); ZLMemStream stream; writer.Open ( stream ); writer.WriteBytes ( this->mBytes, this->mBytes.Size ()); writer.Close (); size_t len = stream.GetLength (); this->mBytes.Init ( len ); stream.Seek ( 0, SEEK_SET ); stream.ReadBytes ( this->mBytes, len ); this->mMutex.Unlock (); return true; }
//----------------------------------------------------------------// bool MOAIDataBuffer::Decode ( ZLStreamReader& reader ) { this->mMutex.Lock (); ZLByteStream cryptStream; cryptStream.SetBuffer ( this->mBytes, this->mBytes.Size ()); cryptStream.SetLength ( this->mBytes.Size ()); ZLMemStream plainStream; reader.Open ( cryptStream ); plainStream.WriteStream ( reader ); reader.Close (); size_t len = plainStream.GetLength (); this->mBytes.Init ( len ); plainStream.Seek ( 0, SEEK_SET ); plainStream.ReadBytes ( this->mBytes, len ); this->mMutex.Unlock (); return true; }
//----------------------------------------------------------------// STLString ZLStream::ReadToken ( cc8* delimiters ) { STLString str; if ( this->IsAtEnd ()) return str; char stackBuffer [ LOCAL_BUFFER ]; ZLMemStream memStream; memStream.SetChunkSize ( LOCAL_BUFFER ); memStream.SetGuestBuffer ( stackBuffer, LOCAL_BUFFER ); char c = 0; size_t size = 0; do { c = this->Read < char >( 0 ); if ( delimiters && c ) { bool isDelimiter = false; for ( size_t i = 0; delimiters [ i ]; ++i ) { if ( delimiters [ i ] == c ) { isDelimiter = true; break; } } if ( isDelimiter ) { if ( size ) { c = 0; } else { continue; } } } memStream.Write < char >( c ); size++; } while ( c ); if ( size ) { str.reserve ( size + 1 ); memStream.Seek ( 0, SEEK_SET ); while ( size > 0 ) { char buffer [ LOCAL_BUFFER ]; size_t readSize = size; if ( LOCAL_BUFFER < readSize ) { readSize = LOCAL_BUFFER; } memStream.ReadBytes ( buffer, readSize ); str.append ( buffer, readSize ); size -= readSize; } } return str; }