Protecting certain functions to be used

Protecting certain functions to be used

Postby Antonio Linares » Tue May 02, 2023 9:22 am

If you allow your users to define and execute a macro or use the runtime Harbour compiler (hbcplr.lib), sometimes you may want to avoid the use of certain Harbour functions. This code allows you to protect the symbols that you don't want to be used:

Fill the array { "QOut" } to include all the symbols that you want to protect. Next version will allow to restore them when you want.

protect.prg
Code: Select all  Expand view  RUN
extern Dummy

function Main()

   Protect( { "QOut" } )
   ? Test()

return nil

function Test()

return 2 + 2    

#pragma BEGINDUMP

#define _HB_API_INTERNAL_
#include <hbvmpub.h>

extern HB_EXPORT HB_SIZE hb_parinfa( int iParamNum, HB_SIZE nArrayIndex );
extern HB_EXPORT const char * hb_parvc( int iParam, ... );
extern HB_EXPORT PHB_DYNS hb_dynsymFindName( const char * szName );
extern HB_EXPORT PHB_SYMB hb_dynsymSymbol( PHB_DYNS pDynSym );

HB_FUNC( DUMMY )
{
   hb_ret();  
}

HB_FUNC( PROTECT )
{
   int iLen = ( int ) hb_parinfa( 1, 0 );  
   int i;

   for( i = 0; i < iLen; i++ )
   {
      const char * szSymbolName = hb_parvc( 1, i + 1 );

      hb_dynsymSymbol( hb_dynsymFindName( szSymbolName ) )->value.pFunPtr =
         hb_dynsymSymbol( hb_dynsymFindName( "DUMMY" ) )->value.pFunPtr;
   }
}

#pragma ENDDUMP
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42122
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Protecting certain functions to be used

Postby Antonio Linares » Tue May 02, 2023 4:51 pm

Enhanced version: functions Protect() and UnProtect()

Code: Select all  Expand view  RUN
extern Dummy

function Main()

   Protect( { "Test" } )
   ? Test()
   UnProtect( { "Test" } )
   ? Test()

return nil

function Test()

return 2 + 2    

#pragma BEGINDUMP

#define _HB_API_INTERNAL_
#include <hbvmpub.h>

extern HB_EXPORT HB_SIZE hb_parinfa( int iParamNum, HB_SIZE nArrayIndex );
extern HB_EXPORT const char * hb_parvc( int iParam, ... );
extern HB_EXPORT PHB_DYNS hb_dynsymFindName( const char * szName );
extern HB_EXPORT PHB_SYMB hb_dynsymSymbol( PHB_DYNS pDynSym );
extern HB_EXPORT void hb_ret( void );

HB_FUNC( DUMMY )
{
   hb_ret();  
}

HB_FUNC( PROTECT )
{
   int iLen = ( int ) hb_parinfa( 1, 0 );  
   int i;

   for( i = 0; i < iLen; i++ )
   {
      const char * szSymbolName = hb_parvc( 1, i + 1 );

      hb_dynsymSymbol( hb_dynsymFindName( szSymbolName ) )->scope.pointer =
         hb_dynsymSymbol( hb_dynsymFindName( szSymbolName ) )->value.pFunPtr;

      hb_dynsymSymbol( hb_dynsymFindName( szSymbolName ) )->value.pFunPtr =
         hb_dynsymSymbol( hb_dynsymFindName( "DUMMY" ) )->value.pFunPtr;
   }
}

HB_FUNC( UNPROTECT )
{
   int iLen = ( int ) hb_parinfa( 1, 0 );  
   int i;

   for( i = 0; i < iLen; i++ )
   {
      const char * szSymbolName = hb_parvc( 1, i + 1 );

      hb_dynsymSymbol( hb_dynsymFindName( szSymbolName ) )->value.pFunPtr =
         hb_dynsymSymbol( hb_dynsymFindName( szSymbolName ) )->scope.pointer;
   }
}

#pragma ENDDUMP
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42122
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Protecting certain functions to be used

Postby Antonio Linares » Tue May 02, 2023 6:19 pm

Simpler and Harbour generates an error as the functions becomes undefined, even if the functions are linked. Once Unprotect()ed they become available again :-)

Code: Select all  Expand view  RUN
function Main()

   Protect( { "FErase" } )
   FErase()

return nil

#pragma BEGINDUMP

#define _HB_API_INTERNAL_
#include <hbvmpub.h>

extern HB_EXPORT HB_SIZE hb_parinfa( int iParamNum, HB_SIZE nArrayIndex );
extern HB_EXPORT const char * hb_parvc( int iParam, ... );
extern HB_EXPORT PHB_DYNS hb_dynsymFindName( const char * szName );
extern HB_EXPORT PHB_SYMB hb_dynsymSymbol( PHB_DYNS pDynSym );

HB_FUNC( PROTECT )
{
   int iLen = ( int ) hb_parinfa( 1, 0 );  
   int i;

   for( i = 0; i < iLen; i++ )
   {
      const char * szSymbolName = hb_parvc( 1, i + 1 );

      hb_dynsymSymbol( hb_dynsymFindName( szSymbolName ) )->scope.pointer =
         hb_dynsymSymbol( hb_dynsymFindName( szSymbolName ) )->value.pFunPtr;

      hb_dynsymSymbol( hb_dynsymFindName( szSymbolName ) )->value.pFunPtr = NULL;
   }
}

HB_FUNC( UNPROTECT )
{
   int iLen = ( int ) hb_parinfa( 1, 0 );  
   int i;

   for( i = 0; i < iLen; i++ )
   {
      const char * szSymbolName = hb_parvc( 1, i + 1 );

      hb_dynsymSymbol( hb_dynsymFindName( szSymbolName ) )->value.pFunPtr =
         hb_dynsymSymbol( hb_dynsymFindName( szSymbolName ) )->scope.pointer;
   }
}

#pragma ENDDUMP
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42122
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Protecting certain functions to be used

Postby paquitohm » Tue May 02, 2023 6:39 pm

Muchísimas gracias. Grandisimo aporte Antonio
Recuerdo, creo que en el foro de harbour users, una vez alguien pidió exactamente esto mismo
paquitohm
 
Posts: 266
Joined: Fri Jan 14, 2022 8:37 am

Re: Protecting certain functions to be used

Postby Antonio Linares » Tue May 02, 2023 7:05 pm

Gracias Paco,

versión simplificada:
Code: Select all  Expand view  RUN
function Main()

   HB_Protect( { "memoWrit" } )
   memoWrit()

return nil

#pragma BEGINDUMP

#define _HB_API_INTERNAL_
#include <hbvmpub.h>

extern HB_EXPORT HB_SIZE hb_parinfa( int iParamNum, HB_SIZE nArrayIndex );
extern HB_EXPORT const char * hb_parvc( int iParam, ... );
extern HB_EXPORT PHB_DYNS hb_dynsymFindName( const char * szName );
extern HB_EXPORT PHB_SYMB hb_dynsymSymbol( PHB_DYNS pDynSym );

HB_FUNC( HB_PROTECT )
{
   int iLen = ( int ) hb_parinfa( 1, 0 );  
   int i;

   for( i = 0; i < iLen; i++ )
   {
      const char * szSymbolName = hb_parvc( 1, i + 1 );
      PHB_SYMB pSym = hb_dynsymSymbol( hb_dynsymFindName( szSymbolName ) );

      if( pSym )
      {
         pSym->scope.pointer = pSym->value.pFunPtr;
         pSym->value.pFunPtr = NULL;
      }
   }  
}

HB_FUNC( HB_UNPROTECT )
{
   int iLen = ( int ) hb_parinfa( 1, 0 );  
   int i;

   for( i = 0; i < iLen; i++ )
   {
      const char * szSymbolName = hb_parvc( 1, i + 1 );
      PHB_SYMB pSym = hb_dynsymSymbol( hb_dynsymFindName( szSymbolName ) );

      if( pSym )
         pSym->value.pFunPtr = pSym->scope.pointer;
   }
}

#pragma ENDDUMP
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42122
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Protecting certain functions to be used

Postby Antonio Linares » Wed May 03, 2023 6:44 am

I just realized that scope.pointer is member of an union and not a member of a struct, so modifying scope.pointer will be affecting the scope of the symbol.

This would be the right way to do it:
Code: Select all  Expand view  RUN
function Main()

   local pFunPtr := HB_Protect( "memoWrit" )

   memoWrit()

   // later on
   // HB_Unprotect( "memoWrit", pFunPtr )

return nil

#pragma BEGINDUMP

#define _HB_API_INTERNAL_
#include <hbvmpub.h>

extern HB_EXPORT const char * hb_parvc( int iParam, ... );
extern HB_EXPORT PHB_DYNS hb_dynsymFindName( const char * szName );
extern HB_EXPORT PHB_SYMB hb_dynsymSymbol( PHB_DYNS pDynSym );
extern HB_EXPORT void * hb_parptr( int iParam );
extern HB_EXPORT void hb_retptr( void * ptr );

HB_FUNC( HB_PROTECT )
{
   const char * szSymbolName = hb_parvc( 1 );
   PHB_SYMB pSym = hb_dynsymSymbol( hb_dynsymFindName( szSymbolName ) );

   if( pSym )
   {
      hb_retptr( pSym->value.pFunPtr );
      pSym->value.pFunPtr = NULL;
   }
}

HB_FUNC( HB_UNPROTECT )
{
   const char * szSymbolName = hb_parvc( 1 );
   PHB_SYMB pSym = hb_dynsymSymbol( hb_dynsymFindName( szSymbolName ) );

   if( pSym )
      pSym->value.pFunPtr = hb_parptr( 2 );
}

#pragma ENDDUMP
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42122
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Protecting certain functions to be used

Postby Antonio Linares » Wed May 03, 2023 6:49 am

simpler:

Code: Select all  Expand view  RUN
function Main()

   local pFunPtr := HB_Protect( "memoWrit" )

   memoWrit()

   // later on
   // HB_Unprotect( "memoWrit", pFunPtr )

return nil

#pragma BEGINDUMP

#define _HB_API_INTERNAL_
#include <hbvmpub.h>

extern HB_EXPORT const char * hb_parvc( int iParam, ... );
extern HB_EXPORT PHB_DYNS hb_dynsymFindName( const char * szName );
extern HB_EXPORT PHB_SYMB hb_dynsymSymbol( PHB_DYNS pDynSym );
extern HB_EXPORT void * hb_parptr( int iParam );
extern HB_EXPORT void hb_retptr( void * ptr );

HB_FUNC( HB_PROTECT )
{
   PHB_SYMB pSym = hb_dynsymSymbol( hb_dynsymFindName( hb_parvc( 1 ) ) );

   if( pSym )
   {
      hb_retptr( pSym->value.pFunPtr );
      pSym->value.pFunPtr = NULL;
   }
}

HB_FUNC( HB_UNPROTECT )
{
   PHB_SYMB pSym = hb_dynsymSymbol( hb_dynsymFindName( hb_parvc( 1 ) ) );

   if( pSym )
      pSym->value.pFunPtr = hb_parptr( 2 );
}

#pragma ENDDUMP
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42122
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 34 guests