Hi,

We will need an HB_STRXOR(). Does this seem okay (UCHAR vs. BYTE might be wrong here):

Oh, yes, I forgot it's my function, not Harbour's. I use code:

HB_FUNC( HB_STRXOR )
{
   PHB_ITEM    pItem, pItem2;
   ULONG       ulLen1, ulLen2, ul, ul2;
   const char  *pStr1, *pStr2;
   char*       pRet;

   pItem = hb_param( 1, HB_IT_STRING );
   if( pItem )
   {
      pStr1 = hb_itemGetCPtr( pItem );
      ulLen1 = hb_itemGetCLen( pItem );

      if( ( pItem2 = hb_param( 2, HB_IT_STRING ) ) != NULL )
      {
         ulLen2 = hb_itemGetCLen( pItem2 );
         if( ulLen2 )
         {
            pStr2 = hb_itemGetCPtr( pItem2 );

            pRet = ( char* ) hb_xgrab( ulLen1 + 1 );
            memcpy( pRet, pStr1, ulLen1 + 1 );
            ul2 = 0;
            for( ul = 0; ul < ulLen1; ul++ )
            {
               pRet[ ul ] ^= pStr2[ ul2 ];
               if( ++ul2 == ulLen2 )
                  ul2 = 0;
            }
            hb_retclen_buffer( pRet, ulLen1 );
         }
         else
            hb_itemReturn( pItem );

         return;
      }
      else if( ( pItem2 = hb_param( 2, HB_IT_NUMERIC ) ) != NULL )
      {
         char  bChar = ( char ) hb_itemGetNI( pItem2 );

         if( bChar )
         {
            pRet = ( char* ) hb_xgrab( ulLen1 + 1 );
            memcpy( pRet, pStr1, ulLen1 + 1 );
            for( ul = 0; ul < ulLen1; ul++ )
               pRet[ ul ] ^= bChar;

            hb_retclen_buffer( pRet, ulLen1 );
         }
         else
            hb_itemReturn( pItem );

         return;
      }
   }
hb_errRT_BASE_SubstR( EG_ARG, 3012, NULL, HB_ERR_FUNCNAME, HB_ERR_ARGS_BASEPARAMS );
}


It has syntax:
   HB_STRXOR( cString, cKey ) --> cXoredString
or
   HB_STRXOR( cString, nByte ) --> cXoredString

Some parameter error detection is done (I'm not sure that error subcode we should use, so, I've used 3012, just like in HB_STRTOHEX()). String is not reallocated if xor'ing to empty string or zero byte. Feel free to commit this code.


Best regards,
Mindaugas

_______________________________________________
Harbour mailing list
Harbour@harbour-project.org
http://lists.harbour-project.org/mailman/listinfo/harbour

Reply via email to