X-Git-Url: https://git.saurik.com/cydget.git/blobdiff_plain/3d796c95ba30398901f3acef1ef186318dd509a9..672fa884bee5352a88404de7015b608ad83015a0:/LockScreen.mm diff --git a/LockScreen.mm b/LockScreen.mm index 5207b15..56999ca 100644 --- a/LockScreen.mm +++ b/LockScreen.mm @@ -1,5 +1,5 @@ -/* CydgetScript - open-source IntelliDial replacement - * Copyright (C) 2009 Jay Freeman (saurik) +/* Cydget - open-source AwayView plugin multiplexer + * Copyright (C) 2009-2011 Jay Freeman (saurik) */ /* @@ -36,11 +36,13 @@ */ #include +#include #import #import #import +#import #import #import @@ -69,16 +71,12 @@ extern NSString * const kCAFilterNearest; #include #include -//#include "Parser.h" #include "JSGlobalData.h" -@interface WebView (UICaboodle) -- (void) setScriptDebugDelegate:(id)delegate; -- (void) _setFormDelegate:(id)delegate; -- (void) _setUIKitDelegate:(id)delegate; -- (void) setWebMailDelegate:(id)delegate; -- (void) _setLayoutInterval:(float)interval; -@end +#include "SourceCode.h" + +#include +#include #define _transient #define _forever for (;;) @@ -86,19 +84,36 @@ extern NSString * const kCAFilterNearest; _disused static unsigned trace_; #define _trace() do { \ - NSLog(@"_trace(%u)@%s:%u[%s]\n", \ - trace_++, __FILE__, __LINE__, __FUNCTION__\ + NSLog(@"_trace(%u)@%s:%u[%s](%p)\n", \ + trace_++, __FILE__, __LINE__, __FUNCTION__, pthread_self() \ ); \ } while (false) +#define _assert(test) do \ + if (!(test)) { \ + fprintf(stderr, "_assert(%d:%s)@%s:%u[%s]\n", errno, #test, __FILE__, __LINE__, __FUNCTION__); \ + exit(-1); \ + } \ +while (false) + +#define _syscall(expr) \ + do if ((long) (expr) != -1) \ + break; \ + else switch (errno) { \ + case EINTR: \ + continue; \ + default: \ + _assert(false); \ + } while (true) + @protocol CydgetController - (NSDictionary *) currentConfiguration; @end -static void (*CYSetupContext)(JSGlobalContextRef); - static Class $CydgetController(objc_getClass("CydgetController")); +static bool iOS32, iOS4; + @interface NSString (UIKit) - (NSString *) stringByAddingPercentEscapes; @end @@ -129,123 +144,107 @@ static Class $CydgetController(objc_getClass("CydgetController")); @end -/* WebCycript Delegate {{{ */ -@interface WebCycriptDelegate : NSObject { - _transient volatile id delegate_; -} +/* Perl-Compatible RegEx {{{ */ +class Pcre { + private: + pcre *code_; + pcre_extra *study_; + int capture_; + int *matches_; + const char *data_; + + public: + Pcre(const char *regex, int options = 0) : + study_(NULL) + { + const char *error; + int offset; + code_ = pcre_compile(regex, options, &error, &offset, NULL); + + if (code_ == NULL) + @throw [NSException exceptionWithName:NSInvalidArgumentException reason:[NSString stringWithFormat:@"*** Pcre(,): [%u] %s", offset, error] userInfo:nil]; + + pcre_fullinfo(code_, study_, PCRE_INFO_CAPTURECOUNT, &capture_); + matches_ = new int[(capture_ + 1) * 3]; + } -- (void) setDelegate:(id)delegate; -- (id) initWithDelegate:(id)delegate; -@end + ~Pcre() { + pcre_free(code_); + delete matches_; + } -@implementation WebCycriptDelegate + NSString *operator [](size_t match) { + return [[[NSString alloc] initWithBytes:(data_ + matches_[match * 2]) length:(matches_[match * 2 + 1] - matches_[match * 2]) encoding:NSUTF8StringEncoding] autorelease]; + } -- (void) setDelegate:(id)delegate { - delegate_ = delegate; -} + bool operator ()(NSString *data) { + // XXX: length is for characters, not for bytes + return operator ()([data UTF8String], [data length]); + } -- (id) initWithDelegate:(id)delegate { - delegate_ = delegate; - return self; -} + bool operator ()(const char *data, size_t size) { + data_ = data; + return pcre_exec(code_, study_, data, size, 0, 0, matches_, (capture_ + 1) * 3) >= 0; + } +}; +/* }}} */ -- (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame { - if (delegate_ != nil) - return [delegate_ webView:sender didClearWindowObject:window forFrame:frame]; -} +static float CYScrollViewDecelerationRateNormal; -- (void) webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame { - if (delegate_ != nil) - return [delegate_ webView:sender didCommitLoadForFrame:frame]; -} +@interface UIScrollView (Apple) +- (void) setDecelerationRate:(float)value; +- (void) setScrollingEnabled:(BOOL)enabled; +@end -- (void) webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame { - if (delegate_ != nil) - return [delegate_ webView:sender didFailLoadWithError:error forFrame:frame]; -} +@interface UIWebView (Apple) +- (void) setDataDetectorTypes:(int)types; +- (UIScrollView *) _scrollView; +- (UIScroller *) _scroller; +- (void) webView:(WebView *)view addMessageToConsole:(NSDictionary *)message; +- (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame; +@end -- (void) webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame { - if (delegate_ != nil) - return [delegate_ webView:sender didFailProvisionalLoadWithError:error forFrame:frame]; -} +@interface WebView (Apple) +- (void) _setLayoutInterval:(float)interval; +- (void) _setAllowsMessaging:(BOOL)allows; +- (void) setShouldUpdateWhileOffscreen:(BOOL)update; +@end -- (void) webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame { - if (delegate_ != nil) - return [delegate_ webView:sender didFinishLoadForFrame:frame]; -} +@protocol CydgetWebViewDelegate // +- (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame; +@end -/*- (void) webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame { - if (delegate_ != nil) - return [delegate_ webView:sender didReceiveTitle:title forFrame:frame]; -}*/ +@class UIWebViewWebViewDelegate; -- (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame { - if (delegate_ != nil) - return [delegate_ webView:sender didStartProvisionalLoadForFrame:frame]; +@interface CydgetWebView : UIWebView { } -/*- (void) webView:(WebView *)sender resource:(id)identifier didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge fromDataSource:(WebDataSource *)source { - if (delegate_ != nil) - return [delegate_ webView:sender resource:identifier didReceiveAuthenticationChallenge:challenge fromDataSource:source]; -}*/ - -/*- (NSURLRequest *) webView:(WebView *)sender resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(WebDataSource *)source { - if (delegate_ != nil) - return [delegate_ webView:sender resource:identifier willSendRequest:request redirectResponse:redirectResponse fromDataSource:source]; - return nil; -}*/ +@end -- (IMP) methodForSelector:(SEL)sel { - if (IMP method = [super methodForSelector:sel]) - return method; - fprintf(stderr, "methodForSelector:[%s] == NULL\n", sel_getName(sel)); - return NULL; -} +@implementation CydgetWebView -- (BOOL) respondsToSelector:(SEL)sel { - if ([super respondsToSelector:sel]) - return YES; - // XXX: WebThreadCreateNSInvocation returns nil - //fprintf(stderr, "[%s]R?%s\n", class_getName(self->isa), sel_getName(sel)); - return delegate_ == nil ? NO : [delegate_ respondsToSelector:sel]; +- (void) webView:(WebView *)view decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id)listener { + [listener use]; } -- (NSMethodSignature *) methodSignatureForSelector:(SEL)sel { - if (NSMethodSignature *method = [super methodSignatureForSelector:sel]) - return method; - //fprintf(stderr, "[%s]S?%s\n", class_getName(self->isa), sel_getName(sel)); - if (delegate_ != nil) - if (NSMethodSignature *sig = [delegate_ methodSignatureForSelector:sel]) - return sig; - // XXX: I fucking hate Apple so very very bad - return [NSMethodSignature signatureWithObjCTypes:"v@:"]; +- (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame { + NSObject *delegate([self delegate]); + if ([delegate respondsToSelector:@selector(webView:didClearWindowObject:forFrame:)]) + [delegate webView:view didClearWindowObject:window forFrame:frame]; + if ([UIWebView instancesRespondToSelector:@selector(webView:didClearWindowObject:forFrame:)]) + [super webView:view didClearWindowObject:window forFrame:frame]; } -- (void) forwardInvocation:(NSInvocation *)inv { - SEL sel = [inv selector]; - if (delegate_ != nil && [delegate_ respondsToSelector:sel]) - [inv invokeWithTarget:delegate_]; +- (void) webView:(WebView *)view addMessageToConsole:(NSDictionary *)message { + NSLog(@"addMessageToConsole:%@", message); } @end -/* }}} */ @interface WebCydgetLockScreenView : UIView { - WebCycriptDelegate *indirect_; - UIProgressIndicator *indicator_; - UIScroller *scroller_; - UIWebDocumentView *document_; - - bool cycript_; - float width_; - CGSize size_; - bool editing_; - - NSNumber *confirm_; - - NSMutableSet *loading_; - bool error_; - bool reloading_; + CydgetWebView *webview_; + UIScrollView *scroller_; + NSString *cycript_; } @end @@ -255,88 +254,13 @@ static Class $CydgetController(objc_getClass("CydgetController")); //#include "UICaboodle/UCInternal.h" - (void) dealloc { - WebThreadLock(); - - WebView *webview([document_ webView]); - [webview setFrameLoadDelegate:nil]; - [webview setResourceLoadDelegate:nil]; - [webview setUIDelegate:nil]; - [webview setScriptDebugDelegate:nil]; - [webview setPolicyDelegate:nil]; - - /* XXX: these are set by UIWebDocumentView - [webview setDownloadDelegate:nil]; - [webview _setFormDelegate:nil]; - [webview _setUIKitDelegate:nil]; - [webview setEditingDelegate:nil];*/ - - /* XXX: no one sets this, ever - [webview setWebMailDelegate:nil];*/ - - [document_ setDelegate:nil]; - [document_ setGestureDelegate:nil]; - [document_ setFormEditingDelegate:nil]; - [document_ setInteractionDelegate:nil]; - - [indirect_ setDelegate:nil]; - - [webview close]; - [document_ release]; - - [indirect_ release]; - - WebThreadUnlock(); - - [scroller_ setDelegate:nil]; - - if (confirm_ != nil) - [confirm_ release]; - - [scroller_ release]; - [indicator_ release]; + [webview_ setDelegate:nil]; + [webview_ release]; [super dealloc]; } -+ (float) defaultWidth { - return 980; -} - -- (void) _setTileDrawingEnabled:(BOOL)enabled { - //[document_ setTileDrawingEnabled:enabled]; -} - -- (void) willStartGesturesInView:(UIView *)view forEvent:(GSEventRef)event { - [self _setTileDrawingEnabled:NO]; -} - -- (void) didFinishGesturesInView:(UIView *)view forEvent:(GSEventRef)event { - [self _setTileDrawingEnabled:YES]; - [document_ redrawScaledDocument]; -} - -- (void) setViewportWidth:(float)width { - width_ = width != 0 ? width : [[self class] defaultWidth]; - [document_ setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10]; -} - -- (void) scrollerWillStartDragging:(UIScroller *)scroller { - [self _setTileDrawingEnabled:NO]; -} - -- (void) scrollerDidEndDragging:(UIScroller *)scroller willSmoothScroll:(BOOL)smooth { - [self _setTileDrawingEnabled:YES]; -} - -- (void) scrollerDidEndDragging:(UIScroller *)scroller { - [self _setTileDrawingEnabled:YES]; -} - - (void) loadRequest:(NSURLRequest *)request { - error_ = false; - - WebThreadLock(); - [document_ loadRequest:request]; - WebThreadUnlock(); + [webview_ loadRequest:request]; } - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy { @@ -353,369 +277,678 @@ static Class $CydgetController(objc_getClass("CydgetController")); - (id) init { CGRect frame = {{0, 0}, {320, 480}}; - frame.size.height -= GSDefaultStatusBarHeight(); + frame.size.height -= 20; //[[[$SBStatusBarController sharedStatusBarController] statusBarView] frame].size.height; if ((self = [super initWithFrame:frame]) != nil) { - struct CGRect bounds([self bounds]); - - scroller_ = [[UIScroller alloc] initWithFrame:bounds]; - [self addSubview:scroller_]; - - [scroller_ setFixedBackgroundPattern:YES]; - [scroller_ setBackgroundColor:[UIColor blackColor]]; + CGRect bounds([self bounds]); + bounds.size.height -= [TPBottomLockBar defaultHeight]; - [scroller_ setScrollingEnabled:YES]; - [scroller_ setClipsSubviews:YES]; - [scroller_ setAllowsRubberBanding:YES]; + webview_ = [[CydgetWebView alloc] initWithFrame:bounds]; + [webview_ setDelegate:self]; + [self addSubview:webview_]; - [scroller_ setDelegate:self]; - [scroller_ setBounces:YES]; - [scroller_ setScrollHysteresis:8]; - [scroller_ setThumbDetectionEnabled:NO]; - [scroller_ setDirectionalScrolling:YES]; - [scroller_ setScrollDecelerationFactor:0.99]; /* 0.989324 */ - [scroller_ setEventMode:YES]; - [scroller_ setShowBackgroundShadow:NO]; /* YES */ - [scroller_ setAllowsRubberBanding:YES]; /* Vertical */ - [scroller_ setAdjustForContentSizeChange:YES]; /* NO */ + if ([webview_ respondsToSelector:@selector(setDataDetectorTypes:)]) + [webview_ setDataDetectorTypes:0x80000000]; + else + [webview_ setDetectsPhoneNumbers:NO]; - CGRect rect([scroller_ bounds]); - //rect.size.height = 0; + [webview_ setScalesPageToFit:YES]; - WebThreadLock(); + UIWebDocumentView *document([webview_ _documentView]); + WebView *webview([document webView]); + WebPreferences *preferences([webview preferences]); - document_ = [[UIWebDocumentView alloc] initWithFrame:rect]; - WebView *webview([document_ webView]); + [document setTileSize:CGSizeMake(bounds.size.width, 500)]; - [document_ setBackgroundColor:[UIColor blackColor]]; - if ([document_ respondsToSelector:@selector(setDrawsBackground:)]) - [document_ setDrawsBackground:NO]; - [webview setDrawsBackground:NO]; + [document setBackgroundColor:[UIColor blackColor]]; + [document setDrawsBackground:NO]; [webview setPreferencesIdentifier:@"WebCycript"]; - [document_ setTileSize:CGSizeMake(rect.size.width, 500)]; + if ([webview respondsToSelector:@selector(_setLayoutInterval:)]) + [webview _setLayoutInterval:0]; + else + [preferences _setLayoutInterval:0]; - if ([document_ respondsToSelector:@selector(enableReachability)]) - [document_ enableReachability]; + [preferences setCacheModel:WebCacheModelDocumentViewer]; + [preferences setJavaScriptCanOpenWindowsAutomatically:YES]; + [preferences setOfflineWebApplicationCacheEnabled:YES]; - [document_ setAllowsMessaging:YES]; + if ([webview respondsToSelector:@selector(setShouldUpdateWhileOffscreen:)]) + [webview setShouldUpdateWhileOffscreen:NO]; - if ([document_ respondsToSelector:@selector(useSelectionAssistantWithMode:)]) - [document_ useSelectionAssistantWithMode:0]; + if ([document respondsToSelector:@selector(setAllowsMessaging:)]) + [document setAllowsMessaging:YES]; + if ([webview respondsToSelector:@selector(_setAllowsMessaging:)]) + [webview _setAllowsMessaging:YES]; - [document_ setTilingEnabled:YES]; - [document_ setDrawsGrid:NO]; - [document_ setLogsTilingChanges:NO]; - [document_ setTileMinificationFilter:kCAFilterNearest]; + if ([webview_ respondsToSelector:@selector(_scrollView)]) { + scroller_ = [webview_ _scrollView]; - if ([document_ respondsToSelector:@selector(setDataDetectorTypes:)]) - /* XXX: abstractify */ - [document_ setDataDetectorTypes:0x80000000]; - else - [document_ setDetectsPhoneNumbers:NO]; + [scroller_ setDirectionalLockEnabled:YES]; + [scroller_ setDecelerationRate:CYScrollViewDecelerationRateNormal]; + [scroller_ setDelaysContentTouches:NO]; + + [scroller_ setCanCancelContentTouches:YES]; - [document_ setAutoresizes:YES]; + [scroller_ setAlwaysBounceVertical:NO]; + } else if ([webview_ respondsToSelector:@selector(_scroller)]) { + UIScroller *scroller([webview_ _scroller]); + scroller_ = (UIScrollView *) scroller; - [document_ setMinimumScale:0.25f forDocumentTypes:0x10]; - [document_ setMaximumScale:5.00f forDocumentTypes:0x10]; - [document_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x10]; - //[document_ setViewportSize:CGSizeMake(980, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10]; + [scroller setDirectionalScrolling:YES]; + [scroller setScrollDecelerationFactor:CYScrollViewDecelerationRateNormal]; /* 0.989324 */ + [scroller setScrollHysteresis:0]; /* 8 */ - [document_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x2]; + [scroller setThumbDetectionEnabled:NO]; + } - [document_ setMinimumScale:1.00f forDocumentTypes:0x8]; - [document_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x8]; - [document_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x8]; + [scroller_ setFixedBackgroundPattern:YES]; + [scroller_ setBackgroundColor:[UIColor blackColor]]; + [scroller_ setClipsSubviews:NO]; - [document_ _setDocumentType:0x4]; + [scroller_ setBounces:YES]; + [scroller_ setShowBackgroundShadow:NO]; /* YES */ - if ([document_ respondsToSelector:@selector(setZoomsFocusedFormControl:)]) - [document_ setZoomsFocusedFormControl:YES]; - [document_ setContentsPosition:7]; - [document_ setEnabledGestures:0xa]; - [document_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeIsZoomRubberBandEnabled]; - [document_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeUpdatesScroller]; + [self setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)]; + [webview_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)]; - [document_ setSmoothsFonts:YES]; - [document_ setAllowsImageSheet:YES]; - [webview _setUsesLoaderCache:YES]; + NSDictionary *configuration([$CydgetController currentConfiguration]); - [webview setGroupName:@"CydgetGroup"]; + cycript_ = [configuration objectForKey:@"CycriptURLs"]; - WebPreferences *preferences([webview preferences]); + [scroller_ setScrollingEnabled:[[configuration objectForKey:@"Scrollable"] boolValue]]; - if ([webview respondsToSelector:@selector(_setLayoutInterval:)]) - [webview _setLayoutInterval:0]; - else - [preferences _setLayoutInterval:0]; + NSString *homepage([configuration objectForKey:@"Homepage"]); + [self loadURL:[NSURL URLWithString:homepage]]; + } return self; +} - [self setViewportWidth:0]; +- (void) webView:(WebView *)webview didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame { + if (cycript_ != nil) + if (NSString *href = [[[[frame dataSource] request] URL] absoluteString]) + if (Pcre([cycript_ UTF8String], 0 /*XXX:PCRE_UTF8*/)(href)) + if (void *handle = dlopen("/usr/lib/libcycript.dylib", RTLD_LAZY | RTLD_GLOBAL)) + if (void (*CYSetupContext)(JSGlobalContextRef) = reinterpret_cast(dlsym(handle, "CydgetSetupContext"))) { + WebFrame *frame([webview mainFrame]); + JSGlobalContextRef context([frame globalContext]); + CYSetupContext(context); + } +} - [document_ setDelegate:self]; - [document_ setGestureDelegate:self]; - [document_ setFormEditingDelegate:self]; - [document_ setInteractionDelegate:self]; +@end - [scroller_ addSubview:document_]; +@interface WebCycriptLockScreenController : SBAwayViewPluginController { +} - //NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; +@end - indirect_ = [[WebCycriptDelegate alloc] initWithDelegate:self]; +#include - [webview setFrameLoadDelegate:indirect_]; - [webview setPolicyDelegate:indirect_]; - [webview setResourceLoadDelegate:indirect_]; - [webview setUIDelegate:indirect_]; +struct State { + unsigned state; +}; - /* XXX: do not turn this on under penalty of extreme pain */ - [webview setScriptDebugDelegate:nil]; +// String Helpers {{{ +static const UChar *(*_ZNK7WebCore6String10charactersEv)(const WebCore::String *); +static const UChar *(*_ZN7WebCore6String29charactersWithNullTerminationEv)(const WebCore::String *); +static unsigned (*_ZNK7WebCore6String6lengthEv)(const WebCore::String *); - WebThreadUnlock(); +static bool StringGet(const WebCore::String &string, const UChar *&data, size_t &length) { + bool terminated; - CGSize indsize([UIProgressIndicator defaultSizeForStyle:UIProgressIndicatorStyleMediumWhite]); - indicator_ = [[UIProgressIndicator alloc] initWithFrame:CGRectMake(281, 12, indsize.width, indsize.height)]; - [indicator_ setStyle:UIProgressIndicatorStyleMediumWhite]; + if (_ZNK7WebCore6String10charactersEv != NULL) { + data = (*_ZNK7WebCore6String10charactersEv)(&string); + terminated = false; + } else if (_ZN7WebCore6String29charactersWithNullTerminationEv != NULL) { + data = (*_ZN7WebCore6String29charactersWithNullTerminationEv)(&string); + terminated = true; + } else return false; - [self setAutoresizingMask:UIViewAutoresizingFlexibleHeight]; - [scroller_ setAutoresizingMask:UIViewAutoresizingFlexibleHeight]; + if (_ZNK7WebCore6String6lengthEv != NULL) + length = (*_ZNK7WebCore6String6lengthEv)(&string); + else if (terminated) + for (length = 0; data[length] != 0; ++length); + else return false; - NSDictionary *configuration([$CydgetController currentConfiguration]); - cycript_ = [[configuration objectForKey:@"Cycript"] boolValue]; - NSString *homepage([configuration objectForKey:@"Homepage"]); - [self loadURL:[NSURL URLWithString:homepage]]; - } return self; + return true; } -- (void) webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame { - [self retain]; +static bool StringEquals(const WebCore::String &string, const char *value) { + const UChar *data; + size_t size; + if (!StringGet(string, data, size)) + return false; - UIActionSheet *sheet = [[[UIActionSheet alloc] - initWithTitle:nil - buttons:[NSArray arrayWithObjects:@"OK", nil] - defaultButtonIndex:0 - delegate:self - context:@"alert" - ] autorelease]; + size_t length(strlen(value)); + if (size != length) + return false; - [sheet setBodyText:message]; - [sheet popupAlertAnimated:YES]; -} + for (size_t index(0); index != length; ++index) + if (data[index] != value[index]) + return false; -- (BOOL) webView:(WebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame { - [self retain]; + return true; +} +// }}} +// State Machine {{{ +static bool cycript_; - UIActionSheet *sheet = [[[UIActionSheet alloc] - initWithTitle:nil - buttons:[NSArray arrayWithObjects:@"OK", @"CANCEL", nil] - defaultButtonIndex:0 - delegate:indirect_ - context:@"confirm" - ] autorelease]; +MSHook(bool, _ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE, const WebCore::String &mime) { + if (!StringEquals(mime, "text/cycript")) { + cycript_ = false; + return __ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE(mime); + } - [sheet setNumberOfRows:1]; - [sheet setBodyText:message]; - [sheet popupAlertAnimated:YES]; + static void *handle(dlopen("/usr/lib/libcycript.dylib", RTLD_LAZY | RTLD_GLOBAL)); + if (handle == NULL) + return false; - NSRunLoop *loop([NSRunLoop currentRunLoop]); - NSDate *future([NSDate distantFuture]); + cycript_ = true; + return true; +} +// }}} +// Script Compiler {{{ +static void Log(const WebCore::String &string) { +#if 0 + const UChar *data; + size_t length; + if (!StringGet(string, data, length)) + return; - while (confirm_ == nil && [loop runMode:NSDefaultRunLoopMode beforeDate:future]); + UChar terminated[length + 1]; + terminated[length] = 0; + memcpy(terminated, data, length * 2); + NSLog(@"wtf %p:%zu:%S:", &string, length, terminated); +#endif +} - NSNumber *confirm([confirm_ autorelease]); - confirm_ = nil; +static void Cycriptify(apr_pool_t *pool, const uint16_t *&data, size_t &size) { + cycript_ = false; - [self autorelease]; - return [confirm boolValue]; + if (void *handle = dlopen("/usr/lib/libcycript.dylib", RTLD_LAZY | RTLD_GLOBAL)) + if (void (*CydgetPoolParse)(apr_pool_t *, const uint16_t **, size_t *) = reinterpret_cast(dlsym(handle, "CydgetPoolParse"))) + CydgetPoolParse(pool, &data, &size); } -/* XXX: WebThreadLock? */ -- (void) _fixScroller:(CGRect)bounds { - float extra; - if (!editing_) - extra = 0; - else { - UIFormAssistant *assistant([UIFormAssistant sharedFormAssistant]); - CGRect peripheral([assistant peripheralFrame]); - extra = peripheral.size.height; +static void (*_ZN7WebCore6String6appendEPKtj)(WebCore::String *, const UChar *, unsigned); +static void (*_ZN7WebCore6String8truncateEj)(WebCore::String *, unsigned); + +static void Cycriptify(const WebCore::String &source, int *psize = NULL) { + if (!cycript_) + return; + + const UChar *data; + size_t length; + + if (!StringGet(source, data, length)) { + return; } - CGRect subrect([scroller_ frame]); - subrect.size.height -= [TPBottomLockBar defaultHeight]; - subrect.size.height -= extra; - [scroller_ setScrollerIndicatorSubrect:subrect]; + size_t size(length); -#undef NSSize - NSSize visible(NSMakeSize(subrect.size.width, subrect.size.height)); - [document_ setValue:[NSValue valueWithSize:visible] forGestureAttribute:UIGestureAttributeVisibleSize]; + apr_pool_t *pool; + apr_pool_create(&pool, NULL); - CGSize size(size_); - size.height += extra; - size.height += [TPBottomLockBar defaultHeight]; - [scroller_ setContentSize:size]; + Cycriptify(pool, data, size); - [scroller_ releaseRubberBandIfNecessary]; -} + WebCore::String &script(const_cast(source)); + + _ZN7WebCore6String8truncateEj(&script, 0); + _ZN7WebCore6String6appendEPKtj(&script, data, size); + + if (psize != NULL) + *psize = size; -- (void) fixScroller { - CGRect bounds([document_ documentBounds]); - [self _fixScroller:bounds]; + apr_pool_destroy(pool); + + Log(source); } +// }}} + +extern "C" void *_ZN3JSC7UString3Rep14nullBaseStringE __attribute__((__weak_import__)); +extern "C" void *_ZN3JSC7UString3Rep7destroyEv __attribute__((__weak_import__)); +extern "C" void *_ZN3JSC7UStringC1EPKti __attribute__((__weak_import__)); +extern "C" void *_ZN3JSC7UStringC1EPKc __attribute__((__weak_import__)); +extern "C" void *_ZNK3JSC7UString6substrEii __attribute__((__weak_import__)); +extern "C" void *_ZN3WTF10fastMallocEm __attribute__((__weak_import__)); +extern "C" void WTFReportAssertionFailure(const char *, int, const char *, const char *) __attribute__((__weak_import__)); +extern "C" void *_ZN3WTF8fastFreeEPv __attribute__((__weak_import__)); -- (void) view:(UIView *)sender didSetFrame:(CGRect)frame { - size_ = frame.size; - [self _fixScroller:frame]; +bool CYWeakHell() { + return + &_ZN3JSC7UString3Rep14nullBaseStringE == NULL || + &_ZN3JSC7UString3Rep7destroyEv == NULL || + &_ZN3JSC7UStringC1EPKti == NULL || + &_ZN3JSC7UStringC1EPKc == NULL || + &_ZNK3JSC7UString6substrEii == NULL || + &_ZN3WTF10fastMallocEm == NULL || + &WTFReportAssertionFailure == NULL || + &_ZN3WTF8fastFreeEPv == NULL || + false; } -- (void) view:(UIView *)sender didSetFrame:(CGRect)frame oldFrame:(CGRect)old { - [self view:sender didSetFrame:frame]; +static WebCore::String *string; + +// iOS 2.x +MSHook(State, _ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i, void *_this, const WebCore::String &string, State state, const WebCore::String &url, int line) { + Cycriptify(string); + return __ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i(_this, string, state, url, line); } -- (void) webView:(WebView *)sender willBeginEditingFormElement:(id)element { - editing_ = true; +// iOS 3.x +MSHook(void, _ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE, JSC::SourceCode **_this, JSC::JSGlobalData *global, int *line, JSC::UString *message) { + if (cycript_) { + JSC::SourceCode *source(_this[iOS32 ? 6 : 0]); + const uint16_t *data(source->data()); + size_t size(source->length()); + + apr_pool_t *pool; + apr_pool_create(&pool, NULL); + + Cycriptify(pool, data, size); + source->~SourceCode(); + // XXX: I actually don't have the original URL here: pants + new (source) JSC::SourceCode(JSC::UStringSourceProvider::create(JSC::UString(data, size), "cycript://"), 1); + + apr_pool_destroy(pool); + + } + + return __ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE(_this, global, line, message); } -- (void) webView:(WebView *)sender didBeginEditingFormElement:(id)element { - [self fixScroller]; +// iOS 4.x cdata +MSHook(void, _ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi, void *_this, const WebCore::String &source, const WebCore::KURL &url, int line) { + Cycriptify(source); + return __ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi(_this, source, url, line); } -- (void) webViewDidEndEditingFormElements:(WebView *)sender { - editing_ = false; - [self fixScroller]; +// iOS 4.x @src= +MSHook(const WebCore::String &, _ZN7WebCore12CachedScript6scriptEv, void *_this) { + const WebCore::String &script(__ZN7WebCore12CachedScript6scriptEv(_this)); + string = const_cast(&script); + Log(script); + return script; } -- (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame { - if (cycript_ && CYSetupContext != NULL) { - WebView *webview([document_ webView]); - WebFrame *frame([webview mainFrame]); - JSGlobalContextRef context([frame globalContext]); - CYSetupContext(context); +// iOS 4.x @src= +MSHook(State, _ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE, void *_this, void *source, State state) { + if (string != NULL) { + if (iOS4) + Cycriptify(*string, reinterpret_cast(source) + 3); + else + Cycriptify(*string); } + string = NULL; + return __ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE(_this, source, state); } -- (bool) isLoading { - return [loading_ count] != 0; +/* Cydget:// Protocol {{{ */ +@interface CydgetURLProtocol : NSURLProtocol { } -- (void) reloadButtons { - if ([self isLoading]) - [indicator_ startAnimation]; - else - [indicator_ stopAnimation]; +@end + +@implementation CydgetURLProtocol + ++ (BOOL) canInitWithRequest:(NSURLRequest *)request { + NSURL *url([request URL]); + if (url == nil) + return NO; + NSString *scheme([[url scheme] lowercaseString]); + if (scheme == nil || ![scheme isEqualToString:@"cydget"]) + return NO; + return YES; } -- (void) _finishLoading { - size_t count([loading_ count]); - /*if (count == 0) - [self autorelease];*/ - if (reloading_ || count != 0) - return; - [self reloadButtons]; ++ (NSURLRequest *) canonicalRequestForRequest:(NSURLRequest *)request { + return request; +} + +- (void) _returnPNGWithImage:(UIImage *)icon forRequest:(NSURLRequest *)request { + id client([self client]); + if (icon == nil) + [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist userInfo:nil]]; + else { + NSData *data(UIImagePNGRepresentation(icon)); + + NSURLResponse *response([[[NSURLResponse alloc] initWithURL:[request URL] MIMEType:@"image/png" expectedContentLength:-1 textEncodingName:nil] autorelease]); + [client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed]; + [client URLProtocol:self didLoadData:data]; + [client URLProtocolDidFinishLoading:self]; + } } -- (BOOL) webView:(WebView *)sender shouldScrollToPoint:(struct CGPoint)point forFrame:(WebFrame *)frame { - return [document_ webView:sender shouldScrollToPoint:point forFrame:frame]; +- (void) startLoading { + id client([self client]); + NSURLRequest *request([self request]); + + NSURL *url([request URL]); + NSString *href([url absoluteString]); + + NSString *path([href substringFromIndex:9]); + NSRange slash([path rangeOfString:@"/"]); + + NSString *command; + if (slash.location == NSNotFound) { + command = path; + path = nil; + } else { + command = [path substringToIndex:slash.location]; + path = [path substringFromIndex:(slash.location + 1)]; + } + + if ([command isEqualToString:@"_UIImageWithName"]) { + if (path == nil) + goto fail; + path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; + UIImage *icon(_UIImageWithName(path)); + [self _returnPNGWithImage:icon forRequest:request]; + } else fail: { + [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorResourceUnavailable userInfo:nil]]; + } } -- (void) webView:(WebView *)sender didReceiveViewportArguments:(id)arguments forFrame:(WebFrame *)frame { - return [document_ webView:sender didReceiveViewportArguments:arguments forFrame:frame]; +- (void) stopLoading { } -- (void) webView:(WebView *)sender needsScrollNotifications:(id)notifications forFrame:(WebFrame *)frame { - return [document_ webView:sender needsScrollNotifications:notifications forFrame:frame]; +@end +/* }}} */ +/* Cydget-CGI:// Protocol {{{ */ +@interface CydgetCGIURLProtocol : NSURLProtocol { + pid_t pid_; + CFHTTPMessageRef http_; + NSFileHandle *handle_; } -- (void) webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame { - return [document_ webView:sender didCommitLoadForFrame:frame]; +@end + +@implementation CydgetCGIURLProtocol + ++ (BOOL) canInitWithRequest:(NSURLRequest *)request { + NSURL *url([request URL]); + if (url == nil) + return NO; + NSString *scheme([[url scheme] lowercaseString]); + if (scheme == nil || ![scheme isEqualToString:@"cydget-cgi"]) + return NO; + return YES; } -- (void) webView:(WebView *)sender didReceiveDocTypeForFrame:(WebFrame *)frame { - return [document_ webView:sender didReceiveDocTypeForFrame:frame]; ++ (NSURLRequest *) canonicalRequestForRequest:(NSURLRequest *)request { + return request; } -- (void) webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame { - [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]]; - [self _finishLoading]; - return [document_ webView:sender didFinishLoadForFrame:frame]; +- (id) initWithRequest:(NSURLRequest *)request cachedResponse:(NSCachedURLResponse *)response client:(id)client { + if ((self = [super initWithRequest:request cachedResponse:response client:client]) != nil) { + pid_ = -1; + } return self; } -- (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame { - /*if ([loading_ count] == 0) - [self retain];*/ - [loading_ addObject:[NSValue valueWithNonretainedObject:frame]]; +- (void) startLoading { + id client([self client]); + NSURLRequest *request([self request]); + NSURL *url([request URL]); - if ([frame parentFrame] == nil) { - [document_ resignFirstResponder]; + NSString *path([url path]); + if (path == nil) { + [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorResourceUnavailable userInfo:nil]]; + return; + } - reloading_ = false; + NSFileManager *manager([NSFileManager defaultManager]); + if (![manager fileExistsAtPath:path]) { + [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist userInfo:nil]]; + return; + } - [scroller_ scrollPointVisibleAtTopLeft:CGPointZero]; + int fds[2]; + _assert(pipe(fds) != -1); - if ([scroller_ respondsToSelector:@selector(setZoomScale:duration:)]) - [scroller_ setZoomScale:1 duration:0]; - else if ([scroller_ respondsToSelector:@selector(_setZoomScale:duration:)]) - [scroller_ _setZoomScale:1 duration:0]; - /*else if ([scroller_ respondsToSelector:@selector(setZoomScale:animated:)]) - [scroller_ setZoomScale:1 animated:NO];*/ + _assert(pid_ == -1); + pid_ = fork(); + if (pid_ == -1) { + _assert(close(fds[0]) != -1); + _assert(close(fds[1]) != -1); + [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorResourceUnavailable userInfo:nil]]; + return; + } + + if (pid_ == 0) { + const char *script([path UTF8String]); + + setenv("GATEWAY_INTERFACE", "CGI/1.1", true); + setenv("SCRIPT_FILENAME", script, true); + NSString *query([url query]); + if (query != nil) + setenv("QUERY_STRING", [query UTF8String], true); - CGRect rect([scroller_ bounds]); - //rect.size.height = 0; - [document_ setFrame:rect]; + _assert(dup2(fds[1], 1) != -1); + _assert(close(fds[0]) != -1); + _assert(close(fds[1]) != -1); + + execl(script, script, NULL); + exit(1); + _assert(false); } - [self reloadButtons]; + _assert(close(fds[1]) != -1); + + _assert(http_ == NULL); + http_ = CFHTTPMessageCreateEmpty(kCFAllocatorDefault, FALSE); + CFHTTPMessageAppendBytes(http_, (const uint8_t *) "HTTP/1.1 200 OK\r\n", 17); + + _assert(handle_ == nil); + handle_ = [[NSFileHandle alloc] initWithFileDescriptor:fds[0] closeOnDealloc:YES]; + + [[NSNotificationCenter defaultCenter] + addObserver:self + selector:@selector(onRead:) + name:@"NSFileHandleReadCompletionNotification" + object:handle_ + ]; + + [handle_ readInBackgroundAndNotify]; } -- (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame { - /*if ([frame parentFrame] == nil) - [self autorelease];*/ +- (void) onRead:(NSNotification *)notification { + NSFileHandle *handle([notification object]); - [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]]; - [self _finishLoading]; + NSData *data([[notification userInfo] objectForKey:NSFileHandleNotificationDataItem]); - if (reloading_) - return; + if (size_t length = [data length]) { + CFHTTPMessageAppendBytes(http_, reinterpret_cast([data bytes]), length); + [handle readInBackgroundAndNotify]; + } else { + id client([self client]); + + CFStringRef mime(CFHTTPMessageCopyHeaderFieldValue(http_, CFSTR("Content-type"))); + if (mime == NULL) + [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorBadServerResponse userInfo:nil]]; + else { + NSURLRequest *request([self request]); + + NSURLResponse *response([[[NSURLResponse alloc] initWithURL:[request URL] MIMEType:(NSString *)mime expectedContentLength:-1 textEncodingName:nil] autorelease]); + CFRelease(mime); + + [client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed]; + + CFDataRef body(CFHTTPMessageCopyBody(http_)); + [client URLProtocol:self didLoadData:(NSData *)body]; + CFRelease(body); - if ([frame parentFrame] == nil) { - [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@", - [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString], - [[error localizedDescription] stringByAddingPercentEscapes] - ]]]; + [client URLProtocolDidFinishLoading:self]; + } - error_ = true; + CFRelease(http_); + http_ = NULL; } } -- (void) webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame { - [self _didFailWithError:error forFrame:frame]; -} + //[client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorNetworkConnectionLost userInfo:nil]]; + +- (void) stopLoading_ { + [[NSNotificationCenter defaultCenter] removeObserver:self]; + + if (handle_ != nil) { + [handle_ release]; + handle_ = nil; + } -- (void) webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame { - [self _didFailWithError:error forFrame:frame]; + if (pid_ != -1) { + kill(pid_, SIGTERM); + int status; + _syscall(waitpid(pid_, &status, 0)); + pid_ = -1; + } } -- (void) webView:(WebView *)sender addMessageToConsole:(NSDictionary *)dictionary { - fprintf(stderr, "Console:%s\n", [[dictionary description] UTF8String]); +- (void) stopLoading { + [self + performSelectorOnMainThread:@selector(stopLoading_) + withObject:nil + waitUntilDone:NO + ]; } @end +/* }}} */ -@interface WebCycriptLockScreenController : SBAwayViewPluginController { +template +static void nlset(Type_ &function, struct nlist *nl, size_t index) { + struct nlist &name(nl[index]); + uintptr_t value(name.n_value); + if ((name.n_desc & N_ARM_THUMB_DEF) != 0) + value |= 0x00000001; + function = reinterpret_cast(value); } -@end +template +static void dlset(Type_ &function, const char *name) { + function = reinterpret_cast(dlsym(RTLD_DEFAULT, name)); +} -/*extern "C" void _ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE(JSC::Parser *, JSC::JSGlobalData *, int *, JSC::UString *); -MSHook(void, _ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE, JSC::Parser *_this, JSC::JSGlobalData *data, int *line, JSC::UString *message) { - return __ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE(_this, data, line, message); -}*/ +template +static void msset_(Type_ &function, const char *name, MSImageRef handle) { + function = reinterpret_cast(MSFindSymbol(handle, name)); +} + +#define msset(function, handle) \ + msset_(function, "_" #function, handle) @implementation WebCycriptLockScreenController +static void $UIWebViewWebViewDelegate$webView$addMessageToConsole$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, NSDictionary *message) { + UIWebView *uiWebView(MSHookIvar(self, "uiWebView")); + if ([uiWebView respondsToSelector:@selector(webView:addMessageToConsole:)]) + [uiWebView webView:view addMessageToConsole:message]; +} + +static void $UIWebViewWebViewDelegate$webView$didClearWindowObject$forFrame$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, WebScriptObject *window, WebFrame *frame) { + UIWebView *uiWebView(MSHookIvar(self, "uiWebView")); + if ([uiWebView respondsToSelector:@selector(webView:didClearWindowObject:forFrame:)]) + [uiWebView webView:view didClearWindowObject:window forFrame:frame]; +} + + (void) initialize { - //MSHookFunction(&_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE, MSHake(_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE)); - if (void *handle = dlopen("/usr/lib/libcycript.dylib", RTLD_LAZY | RTLD_GLOBAL)) - CYSetupContext = reinterpret_cast(dlsym(handle, "CYSetupContext")); + if (Class $UIWebViewWebViewDelegate = objc_getClass("UIWebViewWebViewDelegate")) { + class_addMethod($UIWebViewWebViewDelegate, @selector(webView:addMessageToConsole:), (IMP) &$UIWebViewWebViewDelegate$webView$addMessageToConsole$, "v16@0:4@8@12"); + class_addMethod($UIWebViewWebViewDelegate, @selector(webView:didClearWindowObject:forFrame:), (IMP) &$UIWebViewWebViewDelegate$webView$didClearWindowObject$forFrame$, "v20@0:4@8@12@16"); + } + + if (float *_UIScrollViewDecelerationRateNormal = reinterpret_cast(dlsym(RTLD_DEFAULT, "UIScrollViewDecelerationRateNormal"))) + CYScrollViewDecelerationRateNormal = *_UIScrollViewDecelerationRateNormal; + else // XXX: this actually might be fast on some older systems: we should look into this + CYScrollViewDecelerationRateNormal = 0.998; + + iOS4 = kCFCoreFoundationVersionNumber >= 550.32; + iOS32 = !iOS4 && kCFCoreFoundationVersionNumber >= 478.61; + + int maxproc; + size_t size(sizeof(maxproc)); + if (sysctlbyname("kern.maxproc", &maxproc, &size, NULL, 0) == -1) + NSLog(@"sysctlbyname(\"kern.maxproc\", ?)"); + else if (maxproc < 72) { + maxproc = 72; + if (sysctlbyname("kern.maxproc", NULL, NULL, &maxproc, sizeof(maxproc)) == -1) + NSLog(@"sysctlbyname(\"kern.maxproc\", #)"); + } + + apr_initialize(); + + [NSURLProtocol registerClass:[CydgetURLProtocol class]]; + [NSURLProtocol registerClass:[CydgetCGIURLProtocol class]]; + + if (!iOS4) { + void (*_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE)(JSC::SourceCode **, JSC::JSGlobalData *, int *, JSC::UString *); + dlset(_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE, "_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE"); + if (_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE != NULL) + MSHookFunction(_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE, MSHake(_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE)); + } + + struct nlist nl[9]; + memset(nl, 0, sizeof(nl)); + + nl[0].n_un.n_name = (char *) "__ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE"; + + nl[1].n_un.n_name = (char *) "__ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi"; + + nl[2].n_un.n_name = (char *) "__ZN7WebCore12CachedScript6scriptEv"; + nl[3].n_un.n_name = (char *) "__ZNK7WebCore20StringSourceProvider6sourceEv"; + + nl[4].n_un.n_name = (char *) "__ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i"; + nl[5].n_un.n_name = (char *) "__ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE"; + + nl[6].n_un.n_name = (char *) "__ZN7WebCore6String6appendEPKtj"; + nl[7].n_un.n_name = (char *) "__ZN7WebCore6String8truncateEj"; + + nlist("/System/Library/PrivateFrameworks/WebCore.framework/WebCore", nl); + + bool (*_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE)(const WebCore::String &); + nlset(_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE, nl, 0); + if (_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE != NULL) + MSHookFunction(_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE, MSHake(_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE)); + + void (*_ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi)(void *, const WebCore::String &, const WebCore::KURL &, int); + nlset(_ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi, nl, 1); + if (_ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi != NULL) + MSHookFunction(_ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi, MSHake(_ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi)); + + if (iOS4) { + const WebCore::String &(*_ZN7WebCore12CachedScript6scriptEv)(void *); + nlset(_ZN7WebCore12CachedScript6scriptEv, nl, 2); + if (_ZN7WebCore12CachedScript6scriptEv != NULL) + MSHookFunction(_ZN7WebCore12CachedScript6scriptEv, MSHake(_ZN7WebCore12CachedScript6scriptEv)); + } + + State (*_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i)(void *, const WebCore::String &, State, const WebCore::String &, int); + nlset(_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i, nl, 4); + if (_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i != NULL) + MSHookFunction(_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i, MSHake(_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i)); + + if (iOS4) { + State (*_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE)(void *, void *, State); + nlset(_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE, nl, 5); + if (_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE != NULL) + MSHookFunction(_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE, MSHake(_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE)); + } + + nlset(_ZN7WebCore6String6appendEPKtj, nl, 6); + nlset(_ZN7WebCore6String8truncateEj, nl, 7); + + MSImageRef JavaScriptCore(MSGetImageByName("/System/Library/PrivateFrameworks/JavaScriptCore.framework/JavaScriptCore")); + MSImageRef WebCore(MSGetImageByName("/System/Library/PrivateFrameworks/WebCore.framework/WebCore")); + + if (_ZN7WebCore6String6appendEPKtj == NULL) + msset(_ZN7WebCore6String6appendEPKtj, JavaScriptCore); + + if (_ZN7WebCore6String8truncateEj == NULL) + msset(_ZN7WebCore6String8truncateEj, JavaScriptCore); + + msset(_ZNK7WebCore6String10charactersEv, WebCore); + msset(_ZN7WebCore6String29charactersWithNullTerminationEv, JavaScriptCore); + msset(_ZNK7WebCore6String6lengthEv, WebCore); } + (id) rootViewController { @@ -727,3 +960,10 @@ MSHook(void, _ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE, JSC::Parse } @end + +MSClassHook(WebView) +MSMetaClassHook(WebView) + +MSClassMessageHook0(void, WebView, enableWebThread) { + NSLog(@"-[WebView enableWebThread]"); +}