Index: trunk/Cocoa/F-Script Anywhere/Source/English.lproj/FSAInterpreterPanel.nib/data.dependency
===================================================================
--- trunk/Cocoa/F-Script Anywhere/Source/English.lproj/FSAInterpreterPanel.nib/data.dependency	(revision 155)
+++ trunk/Cocoa/F-Script Anywhere/Source/English.lproj/FSAInterpreterPanel.nib/data.dependency	(revision 217)
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<!DOCTYPE plist SYSTEM "file://localhost/System/Library/DTDs/PropertyList.dtd">
-<plist version="0.9">
+<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
 <dict>
 	<key>IBPaletteDependency</key>
Index: trunk/Cocoa/F-Script Anywhere/Source/English.lproj/FSAInterpreterPanel.nib/info.nib
===================================================================
--- trunk/Cocoa/F-Script Anywhere/Source/English.lproj/FSAInterpreterPanel.nib/info.nib	(revision 155)
+++ trunk/Cocoa/F-Script Anywhere/Source/English.lproj/FSAInterpreterPanel.nib/info.nib	(revision 217)
@@ -1,10 +1,10 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<!DOCTYPE plist SYSTEM "file://localhost/System/Library/DTDs/PropertyList.dtd">
-<plist version="0.9">
+<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
 <dict>
 	<key>IBDocumentLocation</key>
-	<string>99 33 356 240 0 0 1600 1178 </string>
+	<string>74 99 356 240 0 0 1280 1002 </string>
 	<key>IBFramework Version</key>
-	<string>263.2</string>
+	<string>443.0</string>
 	<key>IBOpenObjects</key>
 	<array>
@@ -12,5 +12,5 @@
 	</array>
 	<key>IBSystem Version</key>
-	<string>5S66</string>
+	<string>8H14</string>
 </dict>
 </plist>
Index: trunk/Cocoa/F-Script Anywhere/Source/English.lproj/MainMenu.nib/info.nib
===================================================================
--- trunk/Cocoa/F-Script Anywhere/Source/English.lproj/MainMenu.nib/info.nib	(revision 155)
+++ trunk/Cocoa/F-Script Anywhere/Source/English.lproj/MainMenu.nib/info.nib	(revision 217)
@@ -4,19 +4,19 @@
 <dict>
 	<key>IBDocumentLocation</key>
-	<string>113 10 356 240 0 0 1280 832 </string>
+	<string>113 87 356 240 0 0 1280 1002 </string>
 	<key>IBEditorPositions</key>
 	<dict>
 		<key>29</key>
-		<string>53 175 213 44 0 0 1280 832 </string>
+		<string>53 213 232 44 0 0 1280 1002 </string>
 	</dict>
 	<key>IBFramework Version</key>
-	<string>349.0</string>
+	<string>443.0</string>
 	<key>IBOpenObjects</key>
 	<array>
+		<integer>29</integer>
 		<integer>195</integer>
-		<integer>29</integer>
 	</array>
 	<key>IBSystem Version</key>
-	<string>7D24</string>
+	<string>8H14</string>
 </dict>
 </plist>
Index: trunk/Cocoa/F-Script Anywhere/Source/FSAApp.mm
===================================================================
--- trunk/Cocoa/F-Script Anywhere/Source/FSAApp.mm	(revision 155)
+++ trunk/Cocoa/F-Script Anywhere/Source/FSAApp.mm	(revision 217)
@@ -90,5 +90,6 @@
     if ((err = AEGetAttributePtr(theAE, keyEventIDAttr, typeType, &actualType, &eventID, sizeof(OSType), &actualSize)) != noErr)
         return;
-    
+ 
+#if 0
     if (eventID == kSCLoadResult) {
         OSStatus 
@@ -98,4 +99,5 @@
         printf("patch bundle %s loaded with error status %d\n", bundleID, error);
     }
+#endif
     
 
@@ -234,5 +236,5 @@
     if (err == noErr)
         err = patchController->PatchProcess(&psn);
-    
+
     if (err != noErr)
         [self installationError: err inAppWithPID: pid];
Index: trunk/Cocoa/F-Script Anywhere/Source/FSAnywhere.h
===================================================================
--- trunk/Cocoa/F-Script Anywhere/Source/FSAnywhere.h	(revision 155)
+++ trunk/Cocoa/F-Script Anywhere/Source/FSAnywhere.h	(revision 217)
@@ -10,5 +10,5 @@
 #define FSA_VERSION "1.2b1"
 #define FSA_FSCRIPT_MIN_VERSION 0x1252001 /* 1.2.5d1 */
-#define FSA_DEBUG
+#undef FSA_DEBUG
 
 #ifdef FSA_DEBUG
Index: trunk/Cocoa/F-Script Anywhere/Source/FSAnywhere.m
===================================================================
--- trunk/Cocoa/F-Script Anywhere/Source/FSAnywhere.m	(revision 155)
+++ trunk/Cocoa/F-Script Anywhere/Source/FSAnywhere.m	(revision 217)
@@ -11,3 +11,3 @@
 
 NSString * FSA_FScriptURL = @"http://www.fscript.org/";
-NSString * FSA_FScriptMinimumVersion = @"1.2.5";
+NSString * FSA_FScriptMinimumVersion = @"1.3.0";
Index: trunk/Cocoa/F-Script Anywhere/Source/SCPatch/SCPatchClient/SCPatchClient.cpp
===================================================================
--- trunk/Cocoa/F-Script Anywhere/Source/SCPatch/SCPatchClient/SCPatchClient.cpp	(revision 155)
+++ trunk/Cocoa/F-Script Anywhere/Source/SCPatch/SCPatchClient/SCPatchClient.cpp	(revision 217)
@@ -37,9 +37,9 @@
 
 	if(client->Initialize(client->mClientBundleID, client->mControllerBundleID, getpid()) != noErr)
-		return err_threadEntry_init_failed;
+		return err_threadEntry_image_not_found;
 	
 	// Create a port to receive messages from our patch controller
 	if((err = client->StartListening(client->mClientBundleID, true, true)) != noErr)
-		return err_threadEntry_init_failed;
+		return err_threadEntry_image_not_found;
 	
 	if((err = GetCurrentProcess(&psn)) != noErr ||
@@ -48,5 +48,5 @@
 								  CFStringGetCStringPtr(client->mClientBundleID, kCFStringEncodingMacRoman),
 								  sizeof(ProcessSerialNumber), &psn)) != noErr)
-		return err_threadEntry_init_failed;
+		return err_threadEntry_image_not_found;
 
 	return noErr; 
Index: trunk/Cocoa/F-Script Anywhere/Source/SCPatch/SCPatchController/SCPatchController.cpp
===================================================================
--- trunk/Cocoa/F-Script Anywhere/Source/SCPatch/SCPatchController/SCPatchController.cpp	(revision 155)
+++ trunk/Cocoa/F-Script Anywhere/Source/SCPatch/SCPatchController/SCPatchController.cpp	(revision 217)
@@ -17,5 +17,5 @@
 //-------------------------------------------------------------------------------------------------------------
 OSErr				SCOSErrFrom_mac_err(mach_error_t error) {
-	return (error & err_mac) ? (err & ^err_mac) : noErr;
+	return (error & err_mac) ? (err_mac) : noErr;
 }
 
@@ -323,9 +323,9 @@
 			if(err == noErr)
 				error = InjectPatches(psn, params);
-			
 			free(params);
 		}
 		CFRelease(str);
 	}
+
 	if (error != err_none) return error;
 	else return mac_err(err);
@@ -436,7 +436,13 @@
 	// Find the loader
 	CFURLRef loaderURL = NULL;
+#if defined(__ppc__) || defined(__ppc64__)
 	if(!err)
 		if((loaderURL = CFBundleCopyResourceURL(mBundle, CFSTR("SCPatchLoader"), CFSTR("bundle"), NULL)) == NULL)
 			err = err_couldnt_find_injection_bundle;
+#else
+	if(!err)
+		if((loaderURL = CFBundleCopyResourceURL(mBundle, CFSTR("SCPatchLoader-intel"), CFSTR("bundle"), NULL)) == NULL)
+			err = err_couldnt_find_injection_bundle;
+#endif	
 
 #if 0
@@ -483,5 +489,5 @@
 		fprintf(stderr, "Patcher got error %d\n", err);
 	}
-	
+
 	//	Clean up.
 	if(loaderURL)
Index: trunk/Cocoa/F-Script Anywhere/Source/SCPatch/SCPatchController/SCPatchLoader.c
===================================================================
--- trunk/Cocoa/F-Script Anywhere/Source/SCPatch/SCPatchController/SCPatchLoader.c	(revision 155)
+++ trunk/Cocoa/F-Script Anywhere/Source/SCPatch/SCPatchController/SCPatchLoader.c	(revision 217)
@@ -199,5 +199,5 @@
 
 //-------------------------------------------------------------------------------------------------------------
-mach_error_t INJECT_ENTRY( ptrdiff_t codeOffset, void *paramBlock, size_t paramSize )
+mach_error_t INJECT_ENTRY( ptrdiff_t codeOffset, void *paramBlock, size_t paramSize, char *dummy_pthread_struct  )
 {
 	pthread_t			thread;
@@ -205,9 +205,16 @@
 	struct sched_param	sched;
 	int					policy;
-	
+#if defined (__i386__)
+	// On intel, per-pthread data is a zone of data that must be allocated.
+	// if not, all function trying to access per-pthread data (all mig functions for instance)
+	// will crash. 
+	extern void __pthread_set_self(char*);
+	__pthread_set_self(dummy_pthread_struct);
+#endif
     // We need to fix up function addresses in gcc code, but not for code generated by CW.
-#ifdef __MWERKS__
+	// It turns out that this is not needed for gcc 4.0
+//#ifdef __MWERKS__
     codeOffset = 0;
-#endif
+//#endif
     // Stash the code offset where we can get at it later.
     ((SCPatchLoaderParams *)paramBlock)->codeOffset = codeOffset;
Index: trunk/Cocoa/F-Script Anywhere/Source/bytesex.c
===================================================================
--- trunk/Cocoa/F-Script Anywhere/Source/bytesex.c	(revision 155)
+++ trunk/Cocoa/F-Script Anywhere/Source/bytesex.c	(revision 217)
@@ -1,36 +1,45 @@
 /*
- * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
- *
- * @APPLE_LICENSE_HEADER_START@
+ * Copyright (c) 2004, Apple Computer, Inc. All rights reserved.
  * 
- * Portions Copyright (c) 1999 Apple Computer, Inc.  All Rights
- * Reserved.  This file contains Original Code and/or Modifications of
- * Original Code as defined in and that are subject to the Apple Public
- * Source License Version 1.1 (the "License").  You may not use this file
- * except in compliance with the License.  Please obtain a copy of the
- * License at http://www.apple.com/publicsource and read it before using
- * this file.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1.  Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer. 
+ * 2.  Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution. 
+ * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
+ *     its contributors may be used to endorse or promote products derived
+ *     from this software without specific prior written permission. 
  * 
- * The Original Code and all software distributed under the License are
- * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
- * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
- * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT.  Please see the
- * License for the specific language governing rights and limitations
- * under the License.
- * 
- * @APPLE_LICENSE_HEADER_END@
+ * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
  */
 /* byte_sex.c */
 #include <string.h>
+#include "stuff/target_arch.h"
 #include <mach-o/fat.h>
 #include <mach-o/loader.h>
-#import <mach/m68k/thread_status.h>
-#import <mach/ppc/thread_status.h>
-#import <mach/m88k/thread_status.h>
-#import <mach/i860/thread_status.h>
-#import <mach/i386/thread_status.h>
-#import <mach/hppa/thread_status.h>
-#import <mach/sparc/thread_status.h>
+#include <mach/m68k/thread_status.h>
+#undef MACHINE_THREAD_STATE	/* need to undef these to avoid warnings */
+#undef MACHINE_THREAD_STATE_COUNT
+#include <mach/ppc/thread_status.h>
+#undef MACHINE_THREAD_STATE	/* need to undef these to avoid warnings */
+#undef MACHINE_THREAD_STATE_COUNT
+#include <mach/m88k/thread_status.h>
+#include <mach/i860/thread_status.h>
+#include <mach/i386/thread_status.h>
+#include <mach/hppa/thread_status.h>
+#include <mach/sparc/thread_status.h>
 #include <mach-o/nlist.h>
 #include <mach-o/reloc.h>
@@ -38,4 +47,25 @@
 #include "stuff/bool.h"
 #include "stuff/bytesex.h"
+
+__private_extern__
+long long
+SWAP_LONG_LONG(
+long long ll)
+{
+	union {
+	    char c[8];
+	    long long ll;
+	} in, out;
+	in.ll = ll;
+	out.c[0] = in.c[7];
+	out.c[1] = in.c[6];
+	out.c[2] = in.c[5];
+	out.c[3] = in.c[4];
+	out.c[4] = in.c[3];
+	out.c[5] = in.c[2];
+	out.c[6] = in.c[1];
+	out.c[7] = in.c[0];
+	return(out.ll);
+}
 
 __private_extern__
@@ -150,4 +180,24 @@
 __private_extern__
 void
+swap_mach_header_64(
+struct mach_header_64 *mh,
+enum byte_sex target_byte_sex)
+{
+#ifdef __MWERKS__
+    enum byte_sex dummy;
+        dummy = target_byte_sex;
+#endif
+	mh->magic = SWAP_LONG(mh->magic);
+	mh->cputype = SWAP_LONG(mh->cputype);
+	mh->cpusubtype = SWAP_LONG(mh->cpusubtype);
+	mh->filetype = SWAP_LONG(mh->filetype);
+	mh->ncmds = SWAP_LONG(mh->ncmds);
+	mh->sizeofcmds = SWAP_LONG(mh->sizeofcmds);
+	mh->flags = SWAP_LONG(mh->flags);
+	mh->reserved = SWAP_LONG(mh->reserved);
+}
+
+__private_extern__
+void
 swap_load_command(
 struct load_command *lc,
@@ -187,4 +237,27 @@
 __private_extern__
 void
+swap_segment_command_64(
+struct segment_command_64 *sg,
+enum byte_sex target_byte_sex)
+{
+#ifdef __MWERKS__
+    enum byte_sex dummy;
+        dummy = target_byte_sex;
+#endif
+	/* segname[16] */
+	sg->cmd = SWAP_LONG(sg->cmd);
+	sg->cmdsize = SWAP_LONG(sg->cmdsize);
+	sg->vmaddr = SWAP_LONG_LONG(sg->vmaddr);
+	sg->vmsize = SWAP_LONG_LONG(sg->vmsize);
+	sg->fileoff = SWAP_LONG_LONG(sg->fileoff);
+	sg->filesize = SWAP_LONG_LONG(sg->filesize);
+	sg->maxprot = SWAP_LONG(sg->maxprot);
+	sg->initprot = SWAP_LONG(sg->initprot);
+	sg->nsects = SWAP_LONG(sg->nsects);
+	sg->flags = SWAP_LONG(sg->flags);
+}
+
+__private_extern__
+void
 swap_section(
 struct section *s,
@@ -203,4 +276,32 @@
 	    s[i].addr = SWAP_LONG(s[i].addr);
 	    s[i].size = SWAP_LONG(s[i].size);
+	    s[i].offset = SWAP_LONG(s[i].offset);
+	    s[i].align = SWAP_LONG(s[i].align);
+	    s[i].reloff = SWAP_LONG(s[i].reloff);
+	    s[i].nreloc = SWAP_LONG(s[i].nreloc);
+	    s[i].flags = SWAP_LONG(s[i].flags);
+	    s[i].reserved1 = SWAP_LONG(s[i].reserved1);
+	    s[i].reserved2 = SWAP_LONG(s[i].reserved2);
+	}
+}
+
+__private_extern__
+void
+swap_section_64(
+struct section_64 *s,
+unsigned long nsects,
+enum byte_sex target_byte_sex)
+{
+    unsigned long i;
+#ifdef __MWERKS__
+    enum byte_sex dummy;
+        dummy = target_byte_sex;
+#endif
+
+	for(i = 0; i < nsects; i++){
+	    /* sectname[16] */
+	    /* segname[16] */
+	    s[i].addr = SWAP_LONG_LONG(s[i].addr);
+	    s[i].size = SWAP_LONG_LONG(s[i].size);
 	    s[i].offset = SWAP_LONG(s[i].offset);
 	    s[i].align = SWAP_LONG(s[i].align);
@@ -542,5 +643,52 @@
 	cpu->ctr = SWAP_LONG(cpu->ctr);
 	cpu->mq =  SWAP_LONG(cpu->mq);
-	cpu->pad = SWAP_LONG(cpu->pad);
+	cpu->vrsave = SWAP_LONG(cpu->vrsave);
+}
+
+__private_extern__
+void
+swap_ppc_thread_state64_t(
+ppc_thread_state64_t *cpu,
+enum byte_sex target_byte_sex)
+{
+	cpu->srr0 = SWAP_LONG_LONG(cpu->srr0);
+	cpu->srr1 = SWAP_LONG_LONG(cpu->srr1);
+	cpu->r0 = SWAP_LONG_LONG(cpu->r0);
+	cpu->r1 = SWAP_LONG_LONG(cpu->r1);
+	cpu->r2 = SWAP_LONG_LONG(cpu->r2);
+	cpu->r3 = SWAP_LONG_LONG(cpu->r3);
+	cpu->r4 = SWAP_LONG_LONG(cpu->r4);
+	cpu->r5 = SWAP_LONG_LONG(cpu->r5);
+	cpu->r6 = SWAP_LONG_LONG(cpu->r6);
+	cpu->r7 = SWAP_LONG_LONG(cpu->r7);
+	cpu->r8 = SWAP_LONG_LONG(cpu->r8);
+	cpu->r9 = SWAP_LONG_LONG(cpu->r9);
+	cpu->r10 = SWAP_LONG_LONG(cpu->r10);
+	cpu->r11 = SWAP_LONG_LONG(cpu->r11);
+	cpu->r12 = SWAP_LONG_LONG(cpu->r12);
+	cpu->r13 = SWAP_LONG_LONG(cpu->r13);
+	cpu->r14 = SWAP_LONG_LONG(cpu->r14);
+	cpu->r15 = SWAP_LONG_LONG(cpu->r15);
+	cpu->r16 = SWAP_LONG_LONG(cpu->r16);
+	cpu->r17 = SWAP_LONG_LONG(cpu->r17);
+	cpu->r18 = SWAP_LONG_LONG(cpu->r18);
+	cpu->r19 = SWAP_LONG_LONG(cpu->r19);
+	cpu->r20 = SWAP_LONG_LONG(cpu->r20);
+	cpu->r21 = SWAP_LONG_LONG(cpu->r21);
+	cpu->r22 = SWAP_LONG_LONG(cpu->r22);
+	cpu->r23 = SWAP_LONG_LONG(cpu->r23);
+	cpu->r24 = SWAP_LONG_LONG(cpu->r24);
+	cpu->r25 = SWAP_LONG_LONG(cpu->r25);
+	cpu->r26 = SWAP_LONG_LONG(cpu->r26);
+	cpu->r27 = SWAP_LONG_LONG(cpu->r27);
+	cpu->r28 = SWAP_LONG_LONG(cpu->r28);
+	cpu->r29 = SWAP_LONG_LONG(cpu->r29);
+	cpu->r30 = SWAP_LONG_LONG(cpu->r30);
+	cpu->r31 = SWAP_LONG_LONG(cpu->r31);
+	cpu->cr  = SWAP_LONG(cpu->cr);
+	cpu->xer = SWAP_LONG_LONG(cpu->xer);
+	cpu->lr  = SWAP_LONG_LONG(cpu->lr);
+	cpu->ctr = SWAP_LONG_LONG(cpu->ctr);
+	cpu->vrsave =  SWAP_LONG(cpu->vrsave);
 }
 
@@ -1690,4 +1838,26 @@
 __private_extern__
 void
+swap_routines_command_64(
+struct routines_command_64 *r_cmd,
+enum byte_sex target_byte_sex)
+{
+#ifdef __MWERKS__
+    enum byte_sex dummy;
+        dummy = target_byte_sex;
+#endif
+	r_cmd->cmd = SWAP_LONG(r_cmd->cmd);
+	r_cmd->cmdsize = SWAP_LONG(r_cmd->cmdsize);
+	r_cmd->init_address = SWAP_LONG_LONG(r_cmd->init_address);
+	r_cmd->init_module = SWAP_LONG_LONG(r_cmd->init_module);
+	r_cmd->reserved1 = SWAP_LONG_LONG(r_cmd->reserved1);
+	r_cmd->reserved2 = SWAP_LONG_LONG(r_cmd->reserved2);
+	r_cmd->reserved3 = SWAP_LONG_LONG(r_cmd->reserved3);
+	r_cmd->reserved4 = SWAP_LONG_LONG(r_cmd->reserved4);
+	r_cmd->reserved5 = SWAP_LONG_LONG(r_cmd->reserved5);
+	r_cmd->reserved6 = SWAP_LONG_LONG(r_cmd->reserved6);
+}
+
+__private_extern__
+void
 swap_twolevel_hints_command(
 struct twolevel_hints_command *hints_cmd,
@@ -1777,4 +1947,26 @@
 	    symbols[i].n_desc = SWAP_SHORT(symbols[i].n_desc);
 	    symbols[i].n_value = SWAP_LONG(symbols[i].n_value);
+	}
+}
+
+__private_extern__
+void
+swap_nlist_64(
+struct nlist_64 *symbols,
+unsigned long nsymbols,
+enum byte_sex target_byte_sex)
+{
+    unsigned long i;
+#ifdef __MWERKS__
+    enum byte_sex dummy;
+        dummy = target_byte_sex;
+#endif
+
+	for(i = 0; i < nsymbols; i++){
+	    symbols[i].n_un.n_strx = SWAP_LONG(symbols[i].n_un.n_strx);
+	    /* n_type */
+	    /* n_sect */
+	    symbols[i].n_desc = SWAP_SHORT(symbols[i].n_desc);
+	    symbols[i].n_value = SWAP_LONG_LONG(symbols[i].n_value);
 	}
 }
@@ -1963,4 +2155,36 @@
 __private_extern__
 void
+swap_dylib_module_64(
+struct dylib_module_64 *mods,
+unsigned long nmods,
+enum byte_sex target_byte_sex)
+{
+    unsigned long i;
+#ifdef __MWERKS__
+    enum byte_sex dummy;
+        dummy = target_byte_sex;
+#endif
+
+	for(i = 0; i < nmods; i++){
+	    mods[i].module_name = SWAP_LONG(mods[i].module_name);
+	    mods[i].iextdefsym  = SWAP_LONG(mods[i].iextdefsym);
+	    mods[i].nextdefsym  = SWAP_LONG(mods[i].nextdefsym);
+	    mods[i].irefsym     = SWAP_LONG(mods[i].irefsym);
+	    mods[i].nrefsym     = SWAP_LONG(mods[i].nrefsym);
+	    mods[i].ilocalsym   = SWAP_LONG(mods[i].ilocalsym);
+	    mods[i].nlocalsym   = SWAP_LONG(mods[i].nlocalsym);
+	    mods[i].iextrel     = SWAP_LONG(mods[i].iextrel);
+	    mods[i].nextrel     = SWAP_LONG(mods[i].nextrel);
+	    mods[i].iinit_iterm = SWAP_LONG(mods[i].iinit_iterm);
+	    mods[i].ninit_nterm = SWAP_LONG(mods[i].ninit_nterm);
+	    mods[i].objc_module_info_addr =
+				  SWAP_LONG_LONG(mods[i].objc_module_info_addr);
+	    mods[i].objc_module_info_size =
+				  SWAP_LONG_LONG(mods[i].objc_module_info_size);
+	}
+}
+
+__private_extern__
+void
 swap_dylib_table_of_contents(
 struct dylib_table_of_contents *tocs,
Index: trunk/Cocoa/F-Script Anywhere/Source/libMatch.c
===================================================================
--- trunk/Cocoa/F-Script Anywhere/Source/libMatch.c	(revision 155)
+++ trunk/Cocoa/F-Script Anywhere/Source/libMatch.c	(revision 217)
@@ -40,5 +40,5 @@
 #include <string.h>
 #include <mach-o/loader.h>
-#include "stuff/bytesex.h"
+//#include "stuff/bytesex.h"
 #include "stuff/ofile.h"
 
Index: trunk/Cocoa/F-Script Anywhere/Source/mach_inject/mach_inject.c
===================================================================
--- trunk/Cocoa/F-Script Anywhere/Source/mach_inject/mach_inject.c	(revision 155)
+++ trunk/Cocoa/F-Script Anywhere/Source/mach_inject/mach_inject.c	(revision 217)
@@ -1,42 +1,46 @@
-/****************************************************************************************
-	mach_inject.c $Revision: 1.1.1.1 $
-	
-	Copyright (c) 2003 Red Shed Software. All rights reserved.
-	by Jonathan 'Wolf' Rentzsch (jon * redshed * net)
-	
-	************************************************************************************/
-
-#ifdef __cplusplus
-extern "C" {
-#endif
+ /*******************************************************************************
+	mach_inject.c
+		Copyright (c) 2003-2005 Jonathan 'Wolf' Rentzsch: <http://rentzsch.com>
+		Some rights reserved: <http://creativecommons.org/licenses/by/2.0/>
+
+	***************************************************************************/
 
 #include	"mach_inject.h"
 
-#include	<mach-o/dyld.h>
-#include	<mach-o/getsect.h>
-#include	<mach/message.h>
-#include	<mach/mach.h>
-#include	<sys/stat.h>
-#include	<assert.h>
-#include	<stdio.h>
-#include	<errno.h>
-
-/* doesn't show up, for some reason */
-void       bzero(void *, size_t);
-
-#ifdef __cplusplus
-}
-#endif
-
-#ifndef	COMPILE_TIME_ASSERT
+#include <mach-o/dyld.h>
+#include <mach-o/getsect.h>
+#include <mach/mach.h>
+#include <sys/stat.h>
+#include <sys/errno.h>
+#include <assert.h>
+#include <stdlib.h> // for malloc()
+#include <stdio.h>  // for printf()
+#include <mach-o/fat.h> // for fat structure decoding
+#include <mach-o/arch.h> // to know which is local arch
+#include <fcntl.h> // for open/close
+// for mmap()
+#include <sys/types.h>
+#include <sys/mman.h>
+
+#ifndef	COMPILE_TIME_ASSERT( exp )
 	#define COMPILE_TIME_ASSERT( exp ) { switch (0) { case 0: case (exp):; } }
 #endif
-#define ASSERT_CAST( CAST_TO, CAST_FROM )	COMPILE_TIME_ASSERT( sizeof(CAST_TO)==sizeof(CAST_FROM) )
-
-/****************************************************************************************
+#define ASSERT_CAST( CAST_TO, CAST_FROM ) \
+	COMPILE_TIME_ASSERT( sizeof(CAST_TO)==sizeof(CAST_FROM) )
+
+#if defined(__i386__)
+void* fixedUpImageFromImage (
+		const void *image, 
+		unsigned long imageSize, 
+		unsigned int jumpTableOffset, 
+		unsigned int jumpTableSize,
+		ptrdiff_t fixUpOffset);
+#endif /* __i386__ */
+
+/*******************************************************************************
 *	
 *	Interface
 *	
-****************************************************************************************/
+*******************************************************************************/
 #pragma mark	-
 #pragma mark	(Interface)
@@ -48,51 +52,70 @@
 		size_t					paramSize,
 		pid_t					targetProcess,
-		vm_size_t				stackSize ) {
-	;//assertCodePtr( threadEntry );
-	;//assertPtrIfNotNull( paramBlock );
-	;//assertPositive( targetProcess );
-	;//assertIsTrue( stackSize == 0 || stackSize > 1024 );
+		vm_size_t				stackSize )
+{
+	assert( threadEntry );
+	assert( targetProcess > 0 );
+	assert( stackSize == 0 || stackSize > 1024 );
 	
 	//	Find the image.
 	const void		*image;
 	unsigned long	imageSize;
-	mach_error_t	err = machImageForPointer( threadEntry, &image, &imageSize );
-	
+	unsigned int	jumpTableOffset;
+	unsigned int	jumpTableSize;
+	mach_error_t	err = machImageForPointer( threadEntry, &image, &imageSize, &jumpTableOffset, &jumpTableSize );
+
 	//	Initialize stackSize to default if requested.
 	if( stackSize == 0 )
-		/** @bug We only want an 8K default, fix the plop-in-the-middle code below. */
+		/** @bug
+			We only want an 8K default, fix the plop-in-the-middle code below.
+		*/
 		stackSize = 16 * 1024;
 	
 	//	Convert PID to Mach Task ref.
 	mach_port_t	remoteTask = 0;
-	if( !err )
+	if( !err ) {
 		err = task_for_pid( mach_task_self(), targetProcess, &remoteTask );
-	
-	/** @todo	Would be nice to just allocate one block for both the remote stack
-				*and* the remoteCode (including the parameter data block once that's
-				written.
+#if defined(__i386__)
+		if (err == 5) fprintf(stderr, "Could not access task for pid %d. You probably need to add user to procmod group\n", targetProcess);
+#endif
+	}
+	
+	/** @todo
+		Would be nice to just allocate one block for both the remote stack
+		*and* the remoteCode (including the parameter data block once that's
+		written.
 	*/
 	
 	//	Allocate the remoteStack.
-	vm_address_t remoteStack = NULL;
+	vm_address_t remoteStack = (vm_address_t)NULL;
 	if( !err )
 		err = vm_allocate( remoteTask, &remoteStack, stackSize, 1 );
 	
 	//	Allocate the code.
-	vm_address_t remoteCode = NULL;
+	vm_address_t remoteCode = (vm_address_t)NULL;
 	if( !err )
 		err = vm_allocate( remoteTask, &remoteCode, imageSize, 1 );
 	if( !err ) {
 		ASSERT_CAST( pointer_t, image );
+#if defined (__ppc__) || defined (__ppc64__)
 		err = vm_write( remoteTask, remoteCode, (pointer_t) image, imageSize );
+#elif defined (__i386__)
+		// on intel, jump table use relative jump instructions (jmp), which means
+		// the offset needs to be corrected. We thus copy the image and fix the offset by hand. 
+		ptrdiff_t fixUpOffset = (ptrdiff_t) (image - remoteCode); 
+		void * fixedUpImage = fixedUpImageFromImage(image, imageSize, jumpTableOffset, jumpTableSize, fixUpOffset);
+		err = vm_write( remoteTask, remoteCode, (pointer_t) fixedUpImage, imageSize );
+		free(fixedUpImage);
+#endif
 	}
 	
 	//	Allocate the paramBlock if specified.
-	vm_address_t remoteParamBlock = NULL;
+	vm_address_t remoteParamBlock = (vm_address_t)NULL;
 	if( !err && paramBlock != NULL && paramSize ) {
 		err = vm_allocate( remoteTask, &remoteParamBlock, paramSize, 1 );
 		if( !err ) {
 			ASSERT_CAST( pointer_t, paramBlock );
-			err = vm_write( remoteTask, remoteParamBlock, (pointer_t) paramBlock, paramSize );
+			err = vm_write( remoteTask, remoteParamBlock,
+					(pointer_t) paramBlock, paramSize );
 		}
 	}
@@ -101,18 +124,21 @@
 	ptrdiff_t	threadEntryOffset, imageOffset;
 	if( !err ) {
-		;//assertIsWithinRange( threadEntry, image, image+imageSize );
+		//assert( (void*)threadEntry >= image && (void*)threadEntry <= (image+imageSize) );
 		ASSERT_CAST( void*, threadEntry );
-		threadEntryOffset = ((long) threadEntry) - (long) image;
+		threadEntryOffset = ((void*) threadEntry) - image;
 		
 		ASSERT_CAST( void*, remoteCode );
-		imageOffset = ((long) remoteCode) - (long) image;
+		imageOffset = ((void*) remoteCode) - image;
 	}
 	
 	//	Allocate the thread.
 	thread_act_t remoteThread;
+#if defined (__ppc__) || defined (__ppc64__)
 	if( !err ) {
 		ppc_thread_state_t remoteThreadState;
 		
-		/** @bug Stack math should be more sophisticated than this (ala redzone). */
+		/** @bug
+			Stack math should be more sophisticated than this (ala redzone).
+		*/
 		remoteStack += stackSize / 2;
 		
@@ -139,9 +165,12 @@
 		remoteThreadState.lr = (unsigned int) 0xDEADBEEF;
 		
+#if 0
 		printf( "remoteCode start: %p\n", (void*) remoteCode );
 		printf( "remoteCode size: %ld\n", imageSize );
 		printf( "remoteCode pc: %p\n", (void*) remoteThreadState.srr0 );
-		printf( "remoteCode end: %p\n", (void*) (((char*)remoteCode)+imageSize) );
+		printf( "remoteCode end: %p\n",
+			(void*) (((char*)remoteCode)+imageSize) );
 		fflush(0);
+#endif
 		
 		err = thread_create_running( remoteTask, PPC_THREAD_STATE,
@@ -149,4 +178,56 @@
 				&remoteThread );
 	}
+#elif defined (__i386__)
+	if( !err ) {
+		
+		i386_thread_state_t remoteThreadState;
+		bzero( &remoteThreadState, sizeof(remoteThreadState) );
+			
+		vm_address_t dummy_thread_struct = remoteStack;
+		remoteStack += (stackSize / 2); // this is the real stack
+		// (*) increase the stack, since we're simulating a CALL instruction, which normally pushes return address on the stack
+		remoteStack -= 4;
+		
+#define PARAM_COUNT 4
+#define STACK_CONTENTS_SIZE ((1+PARAM_COUNT) * sizeof(unsigned int))
+		unsigned int stackContents[1 + PARAM_COUNT]; // 1 for the return address and 1 for each param
+		// first entry is return address (see above *)
+		stackContents[0] = 0xDEADBEEF; // invalid return address.
+		// then we push function parameters one by one.
+		stackContents[1] =  imageOffset;
+		stackContents[2] = remoteParamBlock;
+		stackContents[3] = paramSize;
+		// We use the remote stack we allocated as the fake thread struct. We should probably use a dedicated memory zone. 
+		// We don't fill it with 0, vm_allocate did it for us
+		stackContents[4] = dummy_thread_struct; 
+		
+		// push stackContents
+		err = vm_write( remoteTask, remoteStack,
+						(pointer_t) stackContents, STACK_CONTENTS_SIZE);
+		
+		// set remote Program Counter
+		remoteThreadState.eip = (unsigned int) (remoteCode);
+		remoteThreadState.eip += threadEntryOffset;  
+		
+		// set remote Stack Pointer
+		ASSERT_CAST( unsigned int, remoteStack );
+		remoteThreadState.esp = (unsigned int) remoteStack;
+		
+#if 0
+		printf( "remoteCode start: %p\n", (void*) remoteCode );
+		printf( "remoteCode size: %ld\n", imageSize );
+		printf( "remoteCode pc: %p\n", (void*) remoteThreadState.eip );
+		printf( "remoteCode end: %p\n",
+				(void*) (((char*)remoteCode)+imageSize) );
+		fflush(0);
+#endif
+		// create thread and launch it
+		err = thread_create_running( remoteTask, i386_THREAD_STATE,
+									 (thread_state_t) &remoteThreadState, i386_THREAD_STATE_COUNT,
+									 &remoteThread );
+	}
+#else
+#error architecture not supported
+#endif
 	
 	if( err ) {
@@ -166,22 +247,32 @@
 		const void *pointer,
 		const void **image,
-		unsigned long *size ) {
-	;//assertCodePtr( pointer );
-	;//assertPtr( image );
-	;//assertPtr( size );
+		unsigned long *size,
+		unsigned int *jumpTableOffset,
+		unsigned int *jumpTableSize )
+{
+	assert( pointer );
+	assert( image );
+	assert( size );
 	
 	unsigned long p = (unsigned long) pointer;
+	
+	if (jumpTableOffset && jumpTableSize) {
+		*jumpTableOffset = 0;
+		*jumpTableSize = 0;
+	}
 	
 	unsigned long imageIndex, imageCount = _dyld_image_count();
 	for( imageIndex = 0; imageIndex < imageCount; imageIndex++ ) {
-		struct mach_header *header = _dyld_get_image_header( imageIndex );
-		const struct section *section = getsectbynamefromheader( header, SEG_TEXT, SECT_TEXT );
+		const struct mach_header *header = _dyld_get_image_header( imageIndex );
+		const struct section *section = getsectbynamefromheader( header,
+																	SEG_TEXT,
+																	SECT_TEXT );
 		long start = section->addr + _dyld_get_image_vmaddr_slide( imageIndex );
 		long stop = start + section->size;
 		if( p >= start && p <= stop ) {
-			//	It is truly insane we have to stat() the file system in order to
-			//	discover the size of an in-memory data structure.
-			char *imageName = _dyld_get_image_name( imageIndex );
-			;//assertPath( imageName );
+			//	It is truely insane we have to stat() the file system in order
+			//	to discover the size of an in-memory data structure.
+			const char *imageName = _dyld_get_image_name( imageIndex );
+			assert( imageName );
 			struct stat sb;
 			if( stat( imageName, &sb ) )
@@ -194,4 +285,48 @@
 				;//assertUInt32( st_size );
 				*size = sb.st_size;
+				
+				// needed for Universal binaries. Check if file is fat and get image size from there.
+				int fd = open (imageName, O_RDONLY);
+				size_t mapSize = *size;
+				char * fileImage = mmap (NULL, mapSize, PROT_READ, MAP_FILE, fd, 0);
+				
+				struct fat_header* fatHeader = (struct fat_header *)fileImage;
+				if (fatHeader->magic == OSSwapBigToHostInt32(FAT_MAGIC)) {
+					//printf("This is a fat binary\n");
+					uint32_t archCount = OSSwapBigToHostInt32(fatHeader->nfat_arch);
+					
+					NXArchInfo const *localArchInfo = NXGetLocalArchInfo();
+					
+					struct fat_arch* arch = (struct fat_arch *)(fileImage + sizeof(struct fat_header));
+					struct fat_arch* matchingArch = NULL;
+					
+					int archIndex = 0;
+					for (archIndex = 0; archIndex < archCount; archIndex++) {
+						cpu_type_t cpuType = OSSwapBigToHostInt32(arch[archIndex].cputype);
+						cpu_subtype_t cpuSubtype = OSSwapBigToHostInt32(arch[archIndex].cpusubtype);
+						
+						if (localArchInfo->cputype == cpuType) {
+							matchingArch = arch + archIndex;
+							if (localArchInfo->cpusubtype == cpuSubtype) break;
+						}
+					}
+					
+					if (matchingArch) {
+						*size = OSSwapBigToHostInt32(matchingArch->size);
+						//printf ("found arch-specific size : %p\n", *size);
+					}
+				}
+				
+				munmap (fileImage, mapSize);
+				close (fd);
+			}
+			if (jumpTableOffset && jumpTableSize) {
+				const struct section * jumpTableSection = getsectbynamefromheader( header,
+																				   "__IMPORT",
+																				   "__jump_table" );
+				if (jumpTableSection) {
+					*jumpTableOffset = jumpTableSection->offset;
+					*jumpTableSize = jumpTableSection->size;
+				}
 			}
 			return err_none;
@@ -201,2 +336,34 @@
 	return err_threadEntry_image_not_found;
 }
+
+#if defined(__i386__)
+void* fixedUpImageFromImage (
+		const void *image, 
+		unsigned long imageSize, 
+		unsigned int jumpTableOffset, 
+		unsigned int jumpTableSize, 
+		ptrdiff_t fixUpOffset)
+{
+	// first copy the full image
+	void *fixedUpImage = (void *) malloc ((size_t)imageSize);
+	bcopy(image, fixedUpImage, imageSize);
+	
+	// address of jump table in copied image
+	void *jumpTable = fixedUpImage + jumpTableOffset;
+	// each JMP instruction is 5 bytes (E9 xx xx xx xx) where E9 is the opcode for JMP
+	int jumpTableCount = jumpTableSize / 5;
+	
+	// skip first "E9"
+	jumpTable++;
+	
+	int entry=0;
+	for (entry = 0; entry < jumpTableCount; entry++) {
+		unsigned int jmpValue = *((unsigned int *)jumpTable);
+		jmpValue += fixUpOffset;
+		*((unsigned int *)jumpTable) = jmpValue;
+		jumpTable+=5;
+	}
+	
+	return fixedUpImage;
+}
+#endif /* __i386__ */
Index: trunk/Cocoa/F-Script Anywhere/Source/mach_inject/mach_inject.h
===================================================================
--- trunk/Cocoa/F-Script Anywhere/Source/mach_inject/mach_inject.h	(revision 155)
+++ trunk/Cocoa/F-Script Anywhere/Source/mach_inject/mach_inject.h	(revision 217)
@@ -1,54 +1,55 @@
-/****************************************************************************************
-	mach_inject.h $Revision: 1.1.1.1 $
+/*******************************************************************************
+	mach_inject.h
+		Copyright (c) 2003-2005 Jonathan 'Wolf' Rentzsch: <http://rentzsch.com>
+		Some rights reserved: <http://creativecommons.org/licenses/by/2.0/>
+
+	***************************************************************************/
 	
-	Copyright (c) 2003 Red Shed Software. All rights reserved.
-	by Jonathan 'Wolf' Rentzsch (jon * redshed * net)
+/***************************************************************************//**
+	@mainpage	mach_inject
+	@author		Jonathan 'Wolf' Rentzsch: <http://rentzsch.com>
 	
-	************************************************************************************/
-	
-/************************************************************************************/ /**
-	@mainpage	mach_inject
-	@author		Jonathan 'Wolf' Rentzsch (jon * redshed * net)
-	
-	This package, coded in C to the Mach API, allows you to "inject" code into an
-	arbitrary process. "Injection" means both 1) copying over the necessary code into the
-	target's address space and 2) remotely creating a new thread to execute the code.
+	This package, coded in C to the Mach API, allows you to "inject" code into
+	an arbitrary process. "Injection" means both 1) copying over the necessary
+	code into the target's address space and 2) remotely creating a new thread
+	to execute the code.
 
-	************************************************************************************/
+	***************************************************************************/
 
 #ifndef		_mach_inject_
 #define		_mach_inject_
 
-#ifdef __cplusplus
-extern "C" {
-#endif
-
 #include <sys/types.h>
 #include <mach/error.h>
 #include <mach/vm_types.h>
-#include <stddef.h>
+#include <stddef.h> // for ptrdiff_t
+
+#ifdef	__cplusplus
+	extern	"C"	{
+#endif
 
 #define	err_threadEntry_image_not_found			(err_local|1)
-#define	err_threadEntry_init_failed				(err_local|2)
 
 #define	INJECT_ENTRY		injectEntry
 #define	INJECT_ENTRY_SYMBOL	"injectEntry"
 
-typedef	void	(*mach_inject_entry)( ptrdiff_t codeOffset, void *paramBlock, size_t paramSize );
+typedef	void	(*mach_inject_entry)( ptrdiff_t codeOffset, void *paramBlock,
+				size_t paramSize, void* dummy_pthread_data );
 	
-/************************************************************************************//**
+/***************************************************************************//**
 	Starts executing threadEntry in a new thread in the process specified by
 	targetProcess.
 	
-	@param	threadEntry		->	Required pointer to injected thread's entry point.
-	@param	paramBlock		->	Optional pointer to block of memory to pass to the
-								injected thread.
+	@param	threadEntry		->	Required pointer to injected thread's entry
+								point.
+	@param	paramBlock		->	Optional pointer to block of memory to pass to
+								the injected thread.
 	@param	paramSize		->	Optional size of paramBlock.
 	@param	targetProcess	->	Required target process ID.
-	@param	stackSize		->	Optional stack size of threadEntry's thread. Set to zero
-								for default (currently 8K usable).
+	@param	stackSize		->	Optional stack size of threadEntry's thread. Set
+								to zero for default (currently 8K usable).
 	@result					<-	mach_error_t
 
-	************************************************************************************/
+	***************************************************************************/
 
 	mach_error_t
@@ -60,13 +61,16 @@
 		vm_size_t				stackSize );
 
-/************************************************************************************//**
+/***************************************************************************//**
 	Given a pointer, returns its Mach-O image and image size.
 	
-	@param	pointer	->	Required pointer.
-	@param	image	<-	Optional returned pointer to image (really a mach_header).
-	@param	size	<-	Optional returned size of the image.
-	@result			<-	mach_error_t
+	@param	pointer			->	Required pointer.
+	@param	image			<-	Optional returned pointer to image (really a
+								mach_header).
+	@param	size			<-	Optional returned size of the image.
+	@param  jumpTableOffset <-  Optional returned offset of jump table within image (useful on intel)
+	@param  jumpTableSize	<-  Optional returned size of jump table (useful on intel)
+	@result					<-	mach_error_t
 	
-	************************************************************************************/
+	***************************************************************************/
 
 	mach_error_t
@@ -74,9 +78,10 @@
 		const void *pointer,
 		const void **image,
-		unsigned long *size );
+		unsigned long *size,
+		unsigned int *jumpTableOffset,
+		unsigned int *jumpTableSize );
 
-#ifdef __cplusplus
-}
+#ifdef	__cplusplus
+	}
 #endif
-
 #endif	//	_mach_inject_
