os/kernelhwsrv/kerneltest/f32test/plugins/version_2beta/hex/t_hexhook.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of the License "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 // f32test\plugins\hex\t_hexhook.cpp
    15 // 
    16 //
    17 
    18 #include "t_hexhook.h"
    19 #include <f32pluginutils.h>
    20 #include "hex.h"
    21 
    22 _LIT(KHexPluginName, "This is a test plugin which converts binary data to hex");
    23 
    24 
    25 /**
    26 Leaving New function for the plugin
    27 @internalComponent
    28 */
    29 CTestHexHook* CTestHexHook::NewL()
    30 	{
    31 	return new(ELeave) CTestHexHook;
    32 	}
    33 
    34 
    35 /**
    36 Constructor for the plugin
    37 @internalComponent
    38 */
    39 CTestHexHook::CTestHexHook()
    40 	{
    41 	}
    42 
    43 
    44 /**
    45 The destructor for the test hex plugin hook. 
    46 @internalComponent
    47 */
    48 CTestHexHook::~CTestHexHook()
    49 	{
    50 	iFs.Close();
    51 	}
    52 
    53 /**
    54 Initialise the hex plugin.
    55 @internalComponent
    56 */
    57 void CTestHexHook::InitialiseL()
    58 	{
    59 	User::LeaveIfError(RegisterIntercept(EFsFileOpen,			EPreIntercept));
    60 	User::LeaveIfError(RegisterIntercept(EFsFileRead,			EPrePostIntercept));
    61 //	User::LeaveIfError(RegisterIntercept(EFsFileWrite,			EPreIntercept));
    62 
    63 	User::LeaveIfError(iFs.Connect());
    64 	}
    65 
    66 /**
    67 @internalComponent
    68 */
    69 TInt CTestHexHook::DoRequestL(TFsPluginRequest& aRequest)
    70 	{
    71 	TInt err = KErrNotSupported;
    72 
    73 	TInt function = aRequest.Function();
    74 	
    75 	iDrvNumber = aRequest.DriveNumber();
    76 
    77 	switch(function)
    78 		{
    79 		case EFsFileOpen:
    80 			err = HexFileOpen(aRequest);
    81 			break;
    82 
    83 		case EFsFileRead:
    84 			// Post intercept does nothing except prove that it is possible and that no deadlock occurs.
    85 			// plugin always calls FileRead() when receiving a EFsFileRead, and so the mesage gets completed
    86 			// by the plugin and has to be post intercepted by the plugin (if registered to post-intercept the request) 
    87 			// and any plugins above it.
    88 
    89 			if (!(aRequest.IsPostOperation()))
    90 				err = HexFileRead(aRequest);
    91 			break;
    92 
    93 		default:
    94 			break;
    95 		}
    96 
    97 	return err;
    98 	}
    99 
   100 
   101 /**
   102 @internalComponent
   103 */
   104 TInt CTestHexHook::HexFileOpen(TFsPluginRequest& aRequest)
   105 	{
   106 	TFileName fileName;
   107 
   108 	
   109 	
   110 //	TInt driveNumber = aRequest.DriveNumber();
   111 	
   112 	TInt err = GetName(&aRequest, fileName);
   113 	if(err != KErrNone)
   114 		return(err);
   115 	
   116 //	err = ScanFile(fileName);
   117 
   118 	return err;
   119 	}
   120 
   121 
   122 /**
   123 @internalComponent
   124 */
   125 TInt CTestHexHook::HexFileRead(TFsPluginRequest& aRequest)
   126 	{
   127 	TFileName fileName;
   128 	
   129 //	TInt driveNumber = aRequest.DriveNumber();
   130 	
   131 	TInt r = GetName(&aRequest, fileName);
   132 	if(r != KErrNone)
   133 		return(r);
   134 
   135 	TInt len, pos;
   136 	r = GetFileAccessInfo(&aRequest, len, pos);
   137 	if (r != KErrNone)
   138 		return r;
   139 
   140 	// if length is ODD, then it can't be hex
   141 	if (len & 0x01)
   142 		return KErrCorrupt;
   143 
   144 	TInt offset = 0;
   145 	while(len > 0)
   146 		{
   147 		TInt readLen = Min(len<<1, iHexBuf.MaxLength());
   148 
   149 		// read from file
   150 		TPtr8 ptrHex((TUint8*) iHexBuf.Ptr(), readLen, readLen);
   151 		r = FileRead(aRequest, ptrHex, pos<<1);
   152 		if (r != KErrNone)
   153 			return r;
   154 		readLen = ptrHex.Length();
   155 		if (readLen == 0)
   156 			return KErrCompletion;
   157 
   158 		TInt binLen = readLen>>1;
   159 		TPtr8 ptrBin((TUint8*) iBinBuf.Ptr(), binLen, binLen);
   160 		DeHex(ptrHex, ptrBin);
   161 
   162 		// write back to client (may be an app or another plugin)
   163 		r = ClientWrite(aRequest, ptrBin, offset);
   164 		offset+= binLen;
   165 		len-= binLen;
   166 		pos+= readLen;
   167 		}
   168 	
   169 	return KErrCompletion;
   170 	}
   171 
   172 
   173 
   174 /**
   175 @internalComponent
   176 */
   177 TInt CTestHexHook::HexPluginName(TDes& aName)
   178 	{
   179 	aName = KHexPluginName;
   180 	return KErrNone;
   181 	}
   182 
   183 
   184 
   185 
   186 //factory functions
   187 
   188 class CHexHookFactory : public CFsPluginFactory
   189 	{
   190 public:
   191 	CHexHookFactory();
   192 	virtual TInt Install();			
   193 	virtual CFsPlugin* NewPluginL();
   194 	virtual CFsPlugin* NewPluginConnL();
   195 	virtual TInt UniquePosition();
   196 	};
   197 
   198 /**
   199 Constructor for the plugin factory
   200 @internalComponent
   201 */
   202 CHexHookFactory::CHexHookFactory()
   203 	{
   204 	}
   205 
   206 /**
   207 Install function for the plugin factory
   208 @internalComponent
   209 */
   210 TInt CHexHookFactory::Install()
   211 	{
   212 	iSupportedDrives = KPluginAutoAttach;
   213 
   214 	_LIT(KHexHookName,"HexHook");
   215 	return(SetName(&KHexHookName));
   216 	}
   217 
   218 /**
   219 @internalComponent
   220 */
   221 TInt CHexHookFactory::UniquePosition()
   222 	{
   223 	return(0x4EC);
   224 	}
   225 
   226 /**
   227 Plugin factory function
   228 @internalComponent
   229 */
   230 CFsPlugin* CHexHookFactory::NewPluginL()
   231 
   232 	{
   233 	return CTestHexHook::NewL();
   234 	}
   235 
   236 /**
   237 Plugin factory function
   238 @internalComponent
   239 */
   240 CFsPlugin* CHexHookFactory::NewPluginConnL()
   241 
   242 	{
   243 	return CTestHexHook::NewL();
   244 	}
   245 
   246 /**
   247 Create a new Plugin
   248 @internalComponent
   249 */
   250 extern "C" {
   251 
   252 EXPORT_C CFsPluginFactory* CreateFileSystem()
   253 	{
   254 	return(new CHexHookFactory());
   255 	}
   256 }
   257