home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / rxendeco.zip / RXAPI.C < prev    next >
C/C++ Source or Header  |  2001-07-15  |  8KB  |  250 lines

  1. /* +------------------------------------------------------------------------+
  2.    |                                                                        |
  3.    | RXAPI.C   :  RBI Rexx API                                              |
  4.    | Author    :  Dave Lewis                                                |
  5.    | Date      :  Mar   1996                                                |
  6.    |                                                                        |
  7.    | Modification Log:                                                      |
  8.    |                                                                        |
  9.    +------------------------------------------------------------------------+ */
  10.  
  11. #pragma strings(readonly)
  12.  
  13. #define INCL_DOS
  14. #define INCL_SUB
  15. #define INCL_REXXSAA
  16.  
  17. #include <os2.h>
  18.  
  19. #include <rexxsaa.h>
  20. #include <memory.h>
  21. #include <malloc.h>
  22. #include <ctype.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26.  
  27. #include "base64.h"
  28.  
  29. int utTxtToQuoted ( unsigned char **trget, unsigned char *source, int length );
  30. int utQuotedToTxt ( unsigned char *target, unsigned char *source, int length );
  31.  
  32. /* +------------------------------------------------------------------------+
  33.    | Declare all exported functions as REXX functions.                      |
  34.    +------------------------------------------------------------------------+ */
  35.  
  36. RexxFunctionHandler rxLoadFuncs;
  37.  
  38. RexxFunctionHandler rxSetPathInfo;
  39. RexxFunctionHandler rxBinToBase64;
  40. RexxFunctionHandler rxBase64ToBin;
  41. RexxFunctionHandler rxTxtToQuoted;
  42. RexxFunctionHandler rxQuotedToTxt;
  43.  
  44. PSZ rxFunctions[] =
  45. {
  46.  "rxSetPathInfo",
  47.  "rxBinToBase64",
  48.  "rxBase64ToBin",
  49.  "rxTxtToQuoted",
  50.  "rxQuotedToTxt" 
  51. };
  52.  
  53. /* +------------------------------------------------------------------------+
  54.    | Function:  rxLoadFuncts                                                |
  55.    +------------------------------------------------------------------------+ */
  56.  
  57. ULONG APIENTRY rxLoadFuncs( PUCHAR func, ULONG ac, PRXSTRING av,
  58.                              PSZ    que, PRXSTRING ret )
  59. {
  60.   int i;
  61.  
  62.   for ( i = 0; i < sizeof( rxFunctions ) / sizeof(rxFunctions[0]); i++ )
  63.     RexxRegisterFunctionDll( rxFunctions[ i ], "RXAPI", rxFunctions[ i ] );
  64.  
  65.   return( FALSE );
  66. }
  67.  
  68. /* +------------------------------------------------------------------------+
  69.    | Function:  rxTxtToQuoted                                               |
  70.    | Syntax:    quoteddata = rxTxtToQuoted( source );                       |
  71.    +------------------------------------------------------------------------+ */
  72.  
  73. ULONG APIENTRY rxTxtToQuoted( PUCHAR func, ULONG ac, PRXSTRING av,
  74.                                PSZ    que, PRXSTRING ret )
  75. {
  76.   PUCHAR quoted;
  77.   int retlen;
  78.  
  79.   if ( ac != 1 || !RXVALIDSTRING(av[0]) )
  80.   {
  81.     ret -> strptr[0] = 0;
  82.     ret -> strlength = 0;
  83.     return ( FALSE );
  84.   }
  85.  
  86.   retlen = utTxtToQuoted ( "ed, av[0].strptr, av[0].strlength );
  87.  
  88.   if ( retlen > 255 )
  89.     DosAllocMem( (PVOID)&ret -> strptr, retlen + 8, PAG_READ|PAG_WRITE|PAG_COMMIT );
  90.  
  91.   ret -> strlength = retlen;
  92.  
  93.   memcpy( ret -> strptr, quoted, retlen );
  94.  
  95.   free ( quoted );
  96.  
  97.   return( FALSE );
  98. }
  99.  
  100. /* +------------------------------------------------------------------------+
  101.    | Function:  rxQuotedToTxt                                               |
  102.    | Syntax:    quoteddata = rxQuotedToTxt( source );                       |
  103.    +------------------------------------------------------------------------+ */
  104.  
  105. ULONG APIENTRY rxQuotedToTxt( PUCHAR func, ULONG ac, PRXSTRING av,
  106.                                PSZ    que, PRXSTRING ret )
  107. {
  108.   if ( ac != 1 || !RXVALIDSTRING(av[0]) )
  109.   {
  110.     ret -> strptr[0] = 0;
  111.     ret -> strlength = 0;
  112.     return ( FALSE );
  113.   }
  114.  
  115.   if ( av[0].strlength > 255 )
  116.     DosAllocMem( (PVOID)&ret -> strptr, av[0].strlength, PAG_READ|PAG_WRITE|PAG_COMMIT );
  117.  
  118.   ret -> strlength = utQuotedToTxt ( ret -> strptr, av[0].strptr, av[0].strlength );
  119.  
  120.   return( FALSE );
  121. }
  122.  
  123. /* +------------------------------------------------------------------------+
  124.    | Function:  rxBinToBase64                                               |
  125.    | Syntax:    base64data = rxBinToBase64( source );                       |
  126.    +------------------------------------------------------------------------+ */
  127.  
  128. ULONG APIENTRY rxBinToBase64( PUCHAR func, ULONG ac, PRXSTRING av,
  129.                                PSZ    que, PRXSTRING ret )
  130. {
  131.   int retlen;
  132.  
  133.   if ( ac != 1 || !RXVALIDSTRING(av[0]) )
  134.   {
  135.     ret -> strptr[0] = 0;
  136.     ret -> strlength = 0;
  137.     return ( FALSE );
  138.   }
  139.  
  140.   retlen = ( av[0].strlength + 2 ) / 3 * 4;
  141.  
  142.   if ( retlen > 255 )
  143.     DosAllocMem( (PVOID)&ret -> strptr, retlen + 8, PAG_READ|PAG_WRITE|PAG_COMMIT );
  144.  
  145.   ret -> strlength = utBinToBase64( ret -> strptr, av[0].strptr, av[0].strlength );
  146.  
  147.   return( FALSE );
  148. }
  149.  
  150. /* +------------------------------------------------------------------------+
  151.    | Function:  rxBase64ToBin                                               |
  152.    | Syntax:    data = rxBinToBase64( base64data );                         |
  153.    +------------------------------------------------------------------------+ */
  154.  
  155. ULONG APIENTRY rxBase64ToBin( PUCHAR func, ULONG ac, PRXSTRING av,
  156.                                PSZ    que, PRXSTRING ret )
  157. {
  158.   int retlen;
  159.  
  160.   if ( ac != 1 || !RXVALIDSTRING(av[0]) )
  161.   {
  162.     ret -> strptr[0] = 0;
  163.     ret -> strlength = 0;
  164.     return ( FALSE );
  165.   }
  166.  
  167.   retlen = av[0].strlength / 4 * 3;
  168.  
  169.   if ( retlen > 255 )
  170.     DosAllocMem( (PVOID)&ret -> strptr, retlen + 8, PAG_READ|PAG_WRITE|PAG_COMMIT );
  171.  
  172.   ret -> strlength = utBase64ToBin( ret -> strptr, av[0].strptr, av[0].strlength );
  173.  
  174.   return( FALSE );
  175. }
  176.  
  177. /* +------------------------------------------------------------------------+
  178.    | Function:  rxSetFileAttribs                                            |
  179.    | Syntax:    call rxSetPathInfo pathname, [date/time],[ attribs ]        |
  180.    | Return:    return code from DosSetPathInfo                             |
  181.    +------------------------------------------------------------------------+ */
  182.  
  183. ULONG APIENTRY rxSetPathInfo( PUCHAR func, ULONG ac, PRXSTRING av,
  184.                                PSZ    que, PRXSTRING ret )
  185. {
  186.   LONG       rc;
  187.   FILESTATUS3 fsts;
  188.  
  189.   if ( ac < 2 || ac > 3 || !RXVALIDSTRING(av[0]) )
  190.   {
  191.     strcpy( ret -> strptr, "-8" );
  192.     ret -> strlength = 2;
  193.     return ( FALSE );
  194.   }
  195.  
  196.   rc = DosQueryPathInfo( av[0].strptr, FIL_STANDARD, &fsts, sizeof(fsts) );
  197.  
  198.   if ( !rc )
  199.   {
  200.     if ( RXVALIDSTRING(av[1]) )
  201.     {
  202.        /* YYYYMMDDHHMMSS */
  203.  
  204.       int varlen = strlen( av[1].strptr );
  205.  
  206.       if ( varlen > 12 )
  207.       {
  208.         fsts.ftimeLastWrite.twosecs = atoi( av[1].strptr + 12 ) / 2;
  209.         av[1].strptr[12] = 0;
  210.       }
  211.  
  212.       if ( varlen > 10 )
  213.       {
  214.         fsts.ftimeLastWrite.minutes = atoi( av[1].strptr + 10 );
  215.         av[1].strptr[10] = 0;
  216.       }
  217.  
  218.       if ( varlen >  8 )
  219.       {
  220.         fsts.ftimeLastWrite.hours   = atoi( av[1].strptr + 8  );
  221.         av[1].strptr[8]  = 0;
  222.       }
  223.  
  224.       if ( varlen >  6 )
  225.       {
  226.         fsts.fdateLastWrite.day     = atoi( av[1].strptr + 6  );
  227.         av[1].strptr[6]  = 0;
  228.       }
  229.  
  230.       if ( varlen >  4 )
  231.       {
  232.         fsts.fdateLastWrite.month   = atoi( av[1].strptr + 4  );
  233.         av[1].strptr[4]  = 0;
  234.       }
  235.  
  236.       fsts.fdateLastWrite.year      = atoi( av[1].strptr ) - 1980;
  237.     }
  238.  
  239.     if ( ac == 3 && RXVALIDSTRING(av[2]) )
  240.       fsts.attrFile  = atoi( av[2].strptr );
  241.  
  242.     rc = DosSetPathInfo( av[0].strptr, FIL_STANDARD, &fsts, sizeof( fsts ), DSPI_WRTTHRU );
  243.   }
  244.  
  245.   sprintf( ret -> strptr, "%d", rc );
  246.   ret -> strlength = strlen ( ret -> strptr );
  247.   return( FALSE );
  248. }
  249.  
  250.