Exemple #1
0
cli::Error_e cmdRHash( CLIARGS args, cli::Param_t prm) {
 
   ValStore<char> VS;

   MemBuf mb; 
   std::string s;
   UINT uint4 = 0; 
   __int64 uint8 = 0; 
   
   char bindata[] = "ABCDEFGHIJKLM" ;
   VS.SetVal( "binary", (PBYTE)bindata, strlen(bindata) );
   VS.SetVal( "uint4" , (UINT)12345678    );   
   VS.SetVal( "uint8" , (__int64)12345678901234L );
   VS.SetVal( "string", "Hello World!" );
   
   VS.GetVal( "binary", mb    ); printf( "binary = %s\n", FmtHex( s, mb, mb.size() ));
   VS.GetVal( "uint4" , uint4 ); printf( "uint4  = %u\n", uint4 );
   VS.GetVal( "uint8" , uint8 ); printf( "uint8  = %I64u\n", uint8 );   
   VS.GetVal( "string", s     ); printf( "string = %s\n", s.c_str() );      
   
   VS.Serialize  ( "valStore000" );
   VS.Deserialize( "valStore000" );   
   
   VS.GetVal( "binary", mb    ); printf( "binary = %s\n", FmtHex( s, mb, mb.size() ));
   VS.GetVal( "uint4" , uint4 ); printf( "uint4  = %u\n", uint4 );
   VS.GetVal( "uint8" , uint8 ); printf( "uint8  = %I64u\n", uint8 );   
   VS.GetVal( "string", s     ); printf( "string = %s\n", s.c_str() );   

   return cli::ERR_NOERROR;         
}
Exemple #2
0
void GopherEngine::infoFile (const string &fname, const string &path, GopherMenu &tmp)
{
  string file_path;
  file_path.append (abs_path);
  file_path.append ("/");
  file_path.append (path);
  file_path.append ("/");
  file_path.append (fname);

  File f;
  int state;
  size_t fsize;
  state = f.openFile (file_path.c_str (), File::READ);
  if (!state)
    {
      MemBuf buff;
      size_t real;
      fsize = f.getFileSize ();
      buff.setLength (fsize);
      f.read (buff.getBuffer (), fsize, &real);
      if(real == fsize)
        tmp.addIntro (buff.getBuffer (), hostname, port);
      f.close ();
    }
}
Exemple #3
0
 MemBuf::MemBuf(const MemBuf &orig) : OcfaObject("MemBuf", "ocfa"),mMembuf(0),mSize(0),mDoDelete(false){
   mMembuf = static_cast<unsigned char *>(malloc(orig.getSize() * sizeof(unsigned char)));
   if (mMembuf) {
      mDoDelete=true;
      memcpy(mMembuf, orig.getPointer(), orig.getSize());
      mSize = orig.getSize();
   }
 }
Exemple #4
0
// ----------------------------------------------------------------------------
// Password-based key derivation algorithm. 
// - text  : typically, a user-entered password or phrase
// - salt  : caller's "entropy"
// - count : number of times to iterate the hashing loop.
// - length: the desired number of bytes in the returned byte array
// - out   : memory buffer filled with the requested number of bytes
// ----------------------------------------------------------------------------
BYTE *PBKDF2(PCBYTE text, int textlen, PCBYTE salt, int saltlen, int count, int length, BYTE *out) 
{
    // Loop until we've generated the requested number of bytes.
   UINT more = length;
   for (int i=1; 0<more; i++)
   {
      // Where the magic happens.
      MemBuf outF;
      F( MemBuf( text, (UINT)textlen ), MemBuf( salt, (UINT)saltlen), count, i, outF );
      
      // Append as many bytes of hash as needed to the key buffer.  
      UINT nCopyCount = min(more, outF.size());
      memcpy( &out[length-more], outF, nCopyCount);

      // Reduce the "more" counter by the number of bytes we just copied.
      more -= nCopyCount;
   }
   
   return out;
}
Exemple #5
0
bool PartHandler::getMemoryData( MemBuf& target )
{
   if( m_str_stream != 0 )
   {
      int64 pos = m_str_stream->tell();
      byte* data = m_str_stream->closeToBuffer();
      target.setData( data, (uint32) pos, memFree );

      delete m_str_stream;
      m_str_stream = 0;
      m_stream = 0;
      return true;
   }

   return false;
}
Exemple #6
0
static void describe_internal( VMachine *vm, String &tgt, const Item *elem, int32 level, int32 maxLevel, int32 maxSize )
{
   uint32 count;

   // return if we reached the maximum level.
   if ( maxLevel >= 0 && level > maxLevel )
   {
      tgt += "...";
      return;
   }

   switch( elem->type() )
   {
      case FLC_ITEM_NIL:
         tgt += "Nil";
      break;

      case FLC_ITEM_UNB:
         tgt += "_";
      break;


      case FLC_ITEM_BOOL:
         tgt += elem->asBoolean() ? "true" : "false";
      break;


      case FLC_ITEM_INT:
         tgt.writeNumber( elem->asInteger() );
      break;

      case FLC_ITEM_NUM:
         tgt.writeNumber( elem->asNumeric(), "%g" );
      break;

      case FLC_ITEM_RANGE:
         elem->toString(tgt);
      break;

      case FLC_ITEM_STRING:
         tgt += "\"";
         if ( maxSize < 0 || elem->asString()->length() < (uint32) maxSize )
         {
            tgt += *elem->asString();
            tgt += "\"";
         }
         else {
            tgt += elem->asString()->subString(0, maxSize );
            tgt += " ... \"";
         }
      break;

      case FLC_ITEM_LBIND:
         tgt += "&";
         tgt += *elem->asLBind();
         if (elem->isFutureBind())
         {
            tgt +="|";
            describe_internal( vm, tgt, &elem->asFutureBind(), level+1, maxLevel, maxSize );
         }
      break;

      case FLC_ITEM_MEMBUF:
      {
         MemBuf *mb = elem->asMemBuf();
         tgt += "MB(";
         tgt.writeNumber( (int64) mb->length() );
         tgt += ",";
         tgt.writeNumber( (int64) mb->wordSize() );
         tgt += ")";

         tgt += " [";

         String fmt;
         int limit = 0;
         switch ( mb->wordSize() )
         {
            case 1: fmt = "%02" LLFMT "X"; limit = 24; break;
            case 2: fmt = "%04" LLFMT "X"; limit = 12; break;
            case 3: fmt = "%06" LLFMT "X"; limit = 9; break;
            case 4: fmt = "%08" LLFMT "X"; limit = 6; break;
         }

         uint32 max = maxSize < 0 || mb->length() < (uint32) maxSize ? mb->length() : (uint32) maxSize;
         for( count = 0; count < max; count++ )
         {
            tgt.writeNumber( (int64)  mb->get( count ), fmt );
            tgt += " ";
         }
         if ( count == (uint32) maxSize )
            tgt += " ...";
         tgt += "]";
      }
      break;

      case FLC_ITEM_ARRAY:
      {
         CoreArray *arr = elem->asArray();
         tgt += "[";

         if ( level == maxLevel )
         {
            tgt += "...]";
            break;
         }

         for( count = 0; count < arr->length(); count++ ) {
            if ( count == 0 ) tgt += " ";

            describe_internal( vm, tgt, & ((*arr)[count]), level + 1, maxLevel, maxSize );

            if ( count + 1 < arr->length() )
               tgt += ", ";
         }

         tgt +="]";
      }
      break;

      case FLC_ITEM_DICT:
      {
         CoreDict *dict = elem->asDict();
         if( dict->isBlessed() )
            tgt += "*";

         tgt += "[";

         if ( level == maxLevel )
         {
            tgt += "...=>...]";
            break;
         }

         if ( dict->length() == 0 )
         {
            tgt += "=>]";
            break;
         }

         Item key, value;
         Iterator iter( &dict->items() );

         // separate the first loop to be able to add ", "
         describe_internal( vm, tgt, &iter.getCurrentKey(), level + 1, maxLevel, maxSize );
         tgt += " => ";
         describe_internal( vm, tgt, &iter.getCurrent(), level + 1, maxLevel, maxSize );
         iter.next();
         while( iter.hasCurrent() )
         {
            tgt += ", ";
            describe_internal( vm, tgt, &iter.getCurrentKey(), level + 1, maxLevel, maxSize );
            tgt += " => ";
            describe_internal( vm, tgt, &iter.getCurrent(), level + 1, maxLevel, maxSize );
            iter.next();
         }

         tgt += "]";
      }
      break;

      case FLC_ITEM_OBJECT:
      {
         CoreObject *arr = elem->asObjectSafe();
         tgt += arr->generator()->symbol()->name() + "(){ ";

         if ( level == maxLevel )
         {
            tgt += "...}";
            break;
         }

         const PropertyTable &pt = arr->generator()->properties();

         for( count = 0; count < pt.added() ; count++ )
         {
            const String &propName = *pt.getKey( count );
			
			// write only?
			if ( pt.getEntry( count ).isWriteOnly() )
			{
				tgt.A( "(" ).A( propName ).A(")");
			}
			else
			{
				Item dummy;
				arr->getProperty( propName, dummy );

				// in describe skip methods.
				if ( dummy.isFunction() || dummy.isMethod() )
				   continue;
				
				tgt += propName + " = ";

				describe_internal( vm, tgt, &dummy, level + 1, maxLevel, maxSize );
			}

            if (count+1 < pt.added())
            {
               tgt += ", ";
            }
         }

         tgt += "}";
      }
      break;

      case FLC_ITEM_CLASS:
         tgt += "Class " + elem->asClass()->symbol()->name();
      break;

      case FLC_ITEM_METHOD:
      {
         tgt += "(";
         Item itemp;
         elem->getMethodItem( itemp );
         describe_internal( vm, tgt, &itemp, level + 1, maxLevel, maxSize );
         tgt += ").";
         tgt += elem->asMethodFunc()->name() + "()";
      }
      break;

      case FLC_ITEM_CLSMETHOD:
         tgt += "Class ";
         tgt += elem->asMethodClassOwner()->generator()->symbol()->name();
         tgt += "." + elem->asMethodClass()->symbol()->name() + "()";
      break;

      case FLC_ITEM_FUNC:
      {
         const Symbol *funcSym = elem->asFunction()->symbol();
         tgt += funcSym->name() + "()";
      }
      break;

      case FLC_ITEM_REFERENCE:
         tgt += "->";
         describe_internal( vm, tgt, elem->dereference(), level + 1, maxLevel, maxSize );
      break;

      default:
         tgt += "?";
   }
}
Exemple #7
0
void inspect_internal( VMachine *vm, const Item *elem, int32 level, int32 maxLevel, int32 maxSize, Item* i_stream, bool add, bool addline )
{

   uint32 count;
   int32 i;
   Stream *stream = i_stream != 0 ?
         dyncast<Stream*>(i_stream->asObjectSafe()->getFalconData()) :
         vm->stdErr();

   // return if we reached the maximum level.
   if ( maxLevel >= 0 && level > maxLevel )
   {
      stream->writeString( "..." );
      if ( addline )
         stream->writeString( "\n" );
      return;
   }

   if ( stream == 0 )
   {
      stream = vm->stdOut();
      if ( stream == 0 )
         return;
   }

   if ( add )
      for ( i = 0; i < level*3; i ++ )
      {
         stream->put( 0x20 ); // space
      }

   if ( elem == 0 ) {
      stream->writeString( "Nothing" );
      if ( addline )
         stream->writeString( "\n" );
      return;
   }

   String temp;

   switch( elem->type() )
   {
      case FLC_ITEM_NIL:
         stream->writeString( "Nil" );
      break;

      case FLC_ITEM_UNB:
            stream->writeString( "_" );
      break;

      case FLC_ITEM_BOOL:
         stream->writeString( elem->asBoolean() ? "true" : "false" );
      break;


      case FLC_ITEM_INT:
         temp.writeNumber( elem->asInteger() );
         stream->writeString( "int(" );
         stream->writeString( temp );
         stream->writeString( ")" );
      break;

      case FLC_ITEM_NUM:
         temp.writeNumber( elem->asNumeric(), "%g" );
         stream->writeString( "num(" );
         stream->writeString( temp );
         stream->writeString( ")" );
      break;

      case FLC_ITEM_RANGE:
         elem->toString(temp);
         stream->writeString( temp );
      break;

      case FLC_ITEM_STRING:
         stream->writeString( "\"" );
         if ( maxSize < 0 || elem->asString()->length() < (uint32) maxSize )
         {
            stream->writeString( *elem->asString() );
            stream->writeString( "\"" );
         }
         else {
            stream->writeString( elem->asString()->subString(0, maxSize ) );
            stream->writeString( " ... \"" );
         }
      break;

      case FLC_ITEM_LBIND:
         stream->writeString( "&" );
         stream->writeString( *elem->asLBind() );
      break;

      case FLC_ITEM_MEMBUF:
      {
         MemBuf *mb = elem->asMemBuf();
         temp = "MemBuf(";
         temp.writeNumber( (int64) mb->length() );
         temp += ",";
         temp.writeNumber( (int64) mb->wordSize() );
         temp += ")";

         if ( maxSize == 0 )
            stream->writeString( temp );
         else {
            temp += " [\n";

            String fmt;
            int limit = 0;
            switch ( mb->wordSize() )
            {
               case 1: fmt = "%02" LLFMT "X"; limit = 24; break;
               case 2: fmt = "%04" LLFMT "X"; limit = 12; break;
               case 3: fmt = "%06" LLFMT "X"; limit = 9; break;
               case 4: fmt = "%08" LLFMT "X"; limit = 6; break;
            }

            int written = 0;
            uint32 max = maxSize < 0 || mb->length() < (uint32) maxSize ? mb->length() : (uint32) maxSize;
            for( count = 0; count < max; count++ )
            {
               temp.writeNumber( (int64)  mb->get( count ), fmt );
               temp += " ";
               written ++;
               if ( written == limit )
               {
                  temp += "\n";
                  written = 0;
               }
               stream->writeString( temp );
               temp = "";
            }
            if ( count == (uint32) maxSize )
               stream->writeString( " ... " );
            stream->writeString( "]" );
         }
      }
      break;

      case FLC_ITEM_ARRAY:
      {
         CoreArray *arr = elem->asArray();
         temp = "Array[";
         temp.writeNumber( (int64) arr->length() );
         temp += "]";
         stream->writeString( temp );

         if ( level == maxLevel )
         {
            stream->writeString( "{...}" );
            break;
         }

         stream->writeString( "{\n" );

         for( count = 0; count < arr->length(); count++ ) {
            inspect_internal( vm, & ((*arr)[count]), level + 1, maxLevel, maxSize, i_stream, true, true );
         }

         for ( i = 0; i < level; i ++ )
         {
            stream->writeString( "   " );
         }
         stream->writeString( "}" );
      }
      break;

      case FLC_ITEM_DICT:
      {
         CoreDict *dict = elem->asDict();
         temp = "Dict[";
         temp.writeNumber( (int64) dict->length() );
         temp += "]";
         stream->writeString( temp );

         if ( level == maxLevel )
         {
            stream->writeString( "{...}" );
            break;
         }

         stream->writeString( "{\n" );

         Iterator iter( &dict->items() );
         while( iter.hasCurrent() )
         {
            inspect_internal( vm, &iter.getCurrentKey(), level + 1, maxLevel, maxSize, i_stream, true, false );
            stream->writeString( " => " );
            inspect_internal( vm, &iter.getCurrent(), level + 1, maxLevel, maxSize, i_stream, false, true );
            iter.next();
         }
         for ( i = 0; i < level; i ++ )
         {
            stream->writeString("   ");
         }
         stream->writeString( "}" );
      }
      break;

      case FLC_ITEM_OBJECT:
      {
         CoreObject *arr = elem->asObjectSafe();
         stream->writeString( "Object of class " + arr->generator()->symbol()->name() );
         if ( level == maxLevel )
         {
            stream->writeString( "{...}" );
            break;
         }

         stream->writeString( " {\n" );
         const PropertyTable &pt = arr->generator()->properties();

         for( count = 0; count < pt.added() ; count++ )
         {
            for ( i = 0; i < (level+1); i ++ )
            {
               stream->writeString("   ");
            }
            const String &propName = *pt.getKey( count );
			
			if ( pt.getEntry(count).isWriteOnly() )
			{
				stream->writeString( "(" );
				stream->writeString( propName + ")\n" );
			}
			else 
			{
				stream->writeString( propName + " => " );
				Item dummy;
				arr->getProperty( propName, dummy);
				inspect_internal( vm, &dummy, level + 1, maxLevel, maxSize, i_stream, false, true );
			}
         }
         for ( i = 0; i < level; i ++ )
         {
            stream->writeString("   ");
         }
         stream->writeString( "}" );
      }
      break;

      case FLC_ITEM_CLASS:
         stream->writeString( "Class " + elem->asClass()->symbol()->name() );
      break;

      case FLC_ITEM_METHOD:
      {
         temp = "Method ";
         temp += "->" + elem->asMethodFunc()->name();
         stream->writeString( temp );

         Item itemp;
         elem->getMethodItem( itemp );

         inspect_internal( vm, &itemp, level + 1, maxLevel, maxSize, i_stream, true, true );
         for ( i = 0; i < level; i ++ )
         {
            stream->writeString("   ");
         }
         stream->writeString( "}" );
      }
      break;

      case FLC_ITEM_CLSMETHOD:
         temp = "Cls.Method 0x";
         temp.writeNumberHex( (uint64) elem->asMethodClassOwner() );
         temp += "->" + elem->asMethodClass()->symbol()->name();
         stream->writeString( temp );
      break;

      case FLC_ITEM_FUNC:
      {
         const Symbol *funcSym = elem->asFunction()->symbol();

         if ( funcSym->isExtFunc() )
         {
            stream->writeString( "Ext. Function " + funcSym->name() );
         }
         else {
            stream->writeString( "Function " + funcSym->name() );

            FuncDef *def = funcSym->getFuncDef();
            uint32 itemId = def->onceItemId();
            if ( itemId != FuncDef::NO_STATE )
            {
               if ( elem->asFunction()->liveModule()->globals()[ itemId ].isNil() )
                  stream->writeString( "{ not called }");
               else
                  stream->writeString( "{ called }");
            }
         }
      }
      break;

      case FLC_ITEM_REFERENCE:
         stream->writeString( "Ref to " );
         inspect_internal( vm, elem->dereference(), level + 1, maxLevel, maxSize, i_stream, false, true );
      break;

      default:
         stream->writeString( "Invalid type");
   }
   if ( addline )
      stream->writeString( "\n" );

   stream->flush();
}
Exemple #8
0
/*!
  Flush the server reply to the client.
 */
int Proxy::flushToClient (HttpThreadContext* td, Socket &client,
                          FiltersChain &out, bool onlyHeader, bool *kaClient)
{
  size_t read = 0;
  size_t headerLength;
  int ret;
  size_t nbw;
  bool validResponse = false;
  size_t contentLength = (size_t) - 1;


  td->response.free ();

  for (;;)
    {
      ret = client.recv (td->auxiliaryBuffer->getBuffer () + read,
                         td->auxiliaryBuffer->getRealLength () - read - 1,
                         0,
                         td->http->getTimeout ());

      if (ret == 0)
        break;

      read += ret;

      td->auxiliaryBuffer->getBuffer ()[read] = '\0';

      validResponse = HttpHeaders::buildHTTPResponseHeaderStruct
        (td->auxiliaryBuffer->getBuffer (), &td->response, &headerLength);

      if (validResponse)
        break;
    }

  if (read == 0)
    return td->http->raiseHTTPError (500);

  if (td->response.contentLength.length ())
    contentLength = atoll (td->response.contentLength.c_str ());

  string *tmp = td->request.getValue ("Host", NULL);
  const char *via = tmp ? tmp->c_str () : td->connection->getLocalIpAddr ();

  tmp = td->response.getValue ("Via", NULL);
  if (tmp)
    {
      tmp->append (", ");
      tmp->append (via);
      td->response.setValue ("Via", tmp->c_str ());
    }
  else
    td->response.setValue ("Via", via);

  tmp = td->response.getValue ("Connection", NULL);
  *kaClient = tmp && !strcasecmp (tmp->c_str (), "keep-alive");

  string transferEncoding;
  bool hasTransferEncoding = false;
  tmp = td->response.getValue ("Transfer-Encoding", NULL);
  if (tmp)
    {
      hasTransferEncoding = true;
      transferEncoding.assign (*tmp);
    }

  char tmpBuf[1024];
  MemBuf memBuf;
  MemoryStream memStream (&memBuf);
  memBuf.setExternalBuffer (tmpBuf, sizeof (tmpBuf));
  generateFiltersChain (td, Server::getInstance ()->getFiltersFactory (),
                        td->mime, memStream);

  /* At this point we can modify the response struct before send it to the
     client.  */
  chooseEncoding (td, td->response.contentLength.length ());
  HttpHeaders::sendHeader (td->response, *out.getStream (), *td->buffer, td);

  if (onlyHeader)
    return HttpDataHandler::RET_OK;

  td->sentData += HttpDataHandler::beginHTTPResponse (td, memStream);

  readPayLoad (td, &td->response, &client,
               td->auxiliaryBuffer->getBuffer () + headerLength,
               read - headerLength, td->http->getTimeout (), contentLength,
               hasTransferEncoding ? &transferEncoding : NULL);

  return HttpDataHandler::RET_OK;
}
Exemple #9
0
/*#
   @method CreateCursor SDL
   @brief Gets currently active cursor.
   @param mbData MemBuf containing visual bit data.
   @param mbMask Membuf containing visibility mask data.
   @param width Width of the cursor.
   @param height Height of the cursor.
   @param Xspot X position of the cursor hotspot.
   @param Yspot Y position of the cursor hotspot.
   @raise SDLError if the cursor couldn't be created.

   See SDL_CreateCursor documentation. Method @a SDL.MakeCursor is
   probably simpler to use.
*/
FALCON_FUNC sdl_CreateCursor( ::Falcon::VMachine *vm )
{
   Item *i_data, *i_mask;
   Item *i_width, *i_height, *i_xspot, *i_yspot;

   if( vm->paramCount() < 6 ||
      ! (i_data = vm->param(0) )->isMemBuf() ||
      ! (i_mask = vm->param(1) )->isMemBuf() ||
      ! (i_width = vm->param(2) )->isOrdinal() ||
      ! (i_height = vm->param(3) )->isOrdinal() ||
      ! (i_xspot = vm->param(4) )->isOrdinal() ||
      ! (i_yspot = vm->param(5) )->isOrdinal()
      )
   {
      throw new ParamError( ErrorParam( e_inv_params, __LINE__ ).
         extra( "M,M,N,N,N,N" ) ) ;
      return;
   }

   MemBuf *data = i_data->asMemBuf();
   MemBuf *mask = i_mask->asMemBuf();
   // we are not interested in their word size.
   if( data->size() || data->size() != mask->size() )
   {
      throw new ParamError( ErrorParam( e_param_type, __LINE__ ).
         extra( "Membuf must be of same size" ) ) ;
      return;
   }

   int width = (int) i_width->forceInteger();
   int height = (int) i_height->forceInteger();
   int xspot = (int) i_xspot->forceInteger();
   int yspot = (int) i_yspot->forceInteger();

   if( width < 8 || height < 1 || width % 8 != 0 )
   {
      throw new ParamError( ErrorParam( e_param_type, __LINE__ ).
         extra( "Invalid sizes" ) ) ;
      return;
   }

   if( data->size() != (uint32)( width/8 * height ) )
   {
      throw new ParamError( ErrorParam( e_param_type, __LINE__ ).
         extra( "Membuf doesn't match width and height" ) ) ;
      return;
   }

   if( xspot < 0  || xspot >= width || yspot < 0 || yspot >= height )
   {
      throw new ParamError( ErrorParam( e_param_type, __LINE__ ).
         extra( "Hotspot outside cursor" ) ) ;
      return;
   }

   // ok, all fine
   ::SDL_Cursor *cursor = SDL_CreateCursor( (Uint8 *) data->data(), (Uint8 *) mask->data(),
      width, height, xspot, yspot );

   if ( cursor == 0 )
   {
      throw new SDLError( ErrorParam( FALCON_SDL_ERROR_BASE + 11, __LINE__ )
         .desc( "SDL Create Cursor" )
         .extra( SDL_GetError() ) ) ;
      return;
   }

   Item *cls = vm->findWKI( "SDLCursor" );
   fassert( cls != 0 );
   CoreObject *obj = cls->asClass()->createInstance();
   obj->setUserData( new SDLCursorCarrier( cursor, true ) );
   vm->retval( obj );
}