CallMachOFrameWork.c

Go to the documentation of this file.
00001 
00002 /*  $Id: CallMachOFrameWork.c 605 2006-03-09 11:08:50Z andreradke $    */
00003 
00004 /******************************************************************************
00005 
00006     UserLand Frontier(tm) -- High performance Web content management,
00007     object database, system-level and Internet scripting environment,
00008     including source code editing and debugging.
00009 
00010     Copyright (C) 1992-2004 UserLand Software, Inc.
00011 
00012     This program is free software; you can redistribute it and/or modify
00013     it under the terms of the GNU General Public License as published by
00014     the Free Software Foundation; either version 2 of the License, or
00015     (at your option) any later version.
00016 
00017     This program is distributed in the hope that it will be useful,
00018     but WITHOUT ANY WARRANTY; without even the implied warranty of
00019     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020     GNU General Public License for more details.
00021 
00022     You should have received a copy of the GNU General Public License
00023     along with this program; if not, write to the Free Software
00024     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025 
00026 ******************************************************************************/
00027 
00028 #include "frontier.h"
00029 #include "standard.h"
00030 
00031 #include "CallMachOFrameWork.h"
00032     
00033 
00034 OSStatus LoadFrameworkBundle(CFStringRef framework, CFBundleRef *bundlePtr) /*Adapted from Apple sample code*/
00035 {
00036     OSStatus    err;
00037     FSRef       frameworksFolderRef;
00038     CFURLRef    baseURL;
00039     CFURLRef    bundleURL;
00040     
00041     assert(bundlePtr != nil);
00042     
00043     *bundlePtr = nil;
00044     
00045     baseURL = nil;
00046     bundleURL = nil;
00047     
00048     err = FSFindFolder(kOnAppropriateDisk, kFrameworksFolderType, true, &frameworksFolderRef);
00049     if (err == noErr) {
00050         baseURL = CFURLCreateFromFSRef(kCFAllocatorSystemDefault, &frameworksFolderRef);
00051         if (baseURL == nil) {
00052             err = coreFoundationUnknownErr;
00053         }
00054     }
00055     if (err == noErr) {
00056         bundleURL = CFURLCreateCopyAppendingPathComponent(kCFAllocatorSystemDefault, baseURL, framework, false);
00057         if (bundleURL == nil) {
00058             err = coreFoundationUnknownErr;
00059         }
00060     }
00061     if (err == noErr) {
00062         *bundlePtr = CFBundleCreate(kCFAllocatorSystemDefault, bundleURL);
00063         if (*bundlePtr == nil) {
00064             err = coreFoundationUnknownErr;
00065         }
00066     }
00067     if (err == noErr) {
00068         if ( ! CFBundleLoadExecutable( *bundlePtr ) ) {
00069             err = coreFoundationUnknownErr;
00070         }
00071     }
00072 
00073     // Clean up.
00074     
00075     if (err != noErr && *bundlePtr != nil) {
00076         CFRelease(*bundlePtr);
00077         *bundlePtr = nil;
00078     }
00079     if (bundleURL != nil) {
00080         CFRelease(bundleURL);
00081     }   
00082     if (baseURL != nil) {
00083         CFRelease(baseURL);
00084     }   
00085     
00086     return err;
00087 } /*LoadFrameworkBundle*/
00088 
00089 
00090 void *getframeworkfuncptr (CFStringRef framework, CFStringRef functionname) {
00091 
00092     /*
00093     2005-01-15 aradke: give us the name of a framework and the name of a function
00094     in the framework and we will retrieve a callable pointer to that function.
00095     */
00096     
00097     CFBundleRef bundlePtr;
00098     
00099      if (LoadFrameworkBundle (framework, &bundlePtr) != noErr)
00100         return (nil);
00101     
00102     return (CFBundleGetFunctionPointerForName (bundlePtr, functionname));
00103     } /*getframeworkfuncptr*/
00104 
00105 
00106 static UInt32 gluetemplate[6] = {0x3D800000, 0x618C0000, 0x800C0000, 0x804C0004, 0x7C0903A6, 0x4E800420};
00107 
00108 void *convertcfmtomachofuncptr (void *cfmfp) {
00109     
00110     /*
00111     2004-11-28 aradke: adapted from Apple's CFM_MachO_CFM sample:
00112     
00113     http://developer.apple.com/samplecode/CFM_MachO_CFM/CFM_MachO_CFM.html
00114     
00115     allocate a block of cfm glue code with instructions for calling the CFM function.
00116     the caller is responsible for disposal via disposemachofuncptr 
00117     */
00118 
00119     UInt32 *mfp = (UInt32*) NewPtr (sizeof(gluetemplate));
00120 
00121     mfp[0] = gluetemplate[0] | ((UInt32) cfmfp >> 16);
00122     mfp[1] = gluetemplate[1] | ((UInt32) cfmfp & 0xFFFF);
00123     mfp[2] = gluetemplate[2];
00124     mfp[3] = gluetemplate[3];
00125     mfp[4] = gluetemplate[4];
00126     mfp[5] = gluetemplate[5];
00127 
00128     MakeDataExecutable (mfp, sizeof(gluetemplate));
00129 
00130     return ((void *) mfp);
00131     } /*convertcfmtomachofuncptr*/
00132 
00133 
00134 void disposemachofuncptr (void *mfp) {
00135     
00136     if (mfp != nil)
00137         DisposePtr (mfp);
00138     } /*disposemachofuncptr*/
00139 
00140 
00141 typedef struct tvectorstruct {
00142     ProcPtr procaddr;
00143     UInt32  toc;
00144     } tvectorstruct, *tvectorptr;
00145 
00146 
00147 void *convertmachotocfmfuncptr (void *mfp) {
00148     
00149     /*
00150     2004-11-28 aradke: allocate a fake TVector and set its procptr entry
00151     to the mach procptr handed to us by the caller. the toc entry can
00152     safely be set to nil since it's ignored in this context.
00153     */
00154     
00155     tvectorptr cfmfp = (tvectorptr) NewPtr (sizeof(tvectorstruct));
00156     
00157     if (MemError()  == noErr && cfmfp != nil) {
00158         
00159         cfmfp->procaddr = (ProcPtr) mfp;
00160         cfmfp->toc = 0; /*ignored*/
00161         }
00162 
00163     return ((void *) cfmfp);
00164     } /*convertmachotocfmfuncptr*/
00165 
00166 
00167 void disposecfmfuncptr (void *cfmfp) {
00168     
00169     if (cfmfp != nil)
00170         DisposePtr (cfmfp);
00171     } /*disposecfmfuncptr*/

Generated on Wed May 31 18:19:44 2006 for frontierkernel 10.1.10a by  doxygen 1.4.6