Net Neutrality (EU edition)

Network neutrality in Europe is under threat. Not in some vague possible future; right now. In one week, on Tuesday the fifth of May, the European Parliament will vote on the second reading of the Telecoms Package and amendments.

I urge all EU citizens to contact an MEP, via e-mail or (preferably) phone, and encourage them to support the Citizens’ Rights Amendments, which reaffirm net neutrality and anti-censorship positions adopted by the European Parliament in the first reading but subsequently removed by the Council of Ministers in their “common position”.

The primary purpose of these amendments is to make it explicit that the European Convention on Human Rights applies to internet and telecoms legislation. This should be obvious, but it is clear from the actions of politicians that it is not; it is all too clear that many see the internet as a frivolous toy, and civil rights obviously do not apply to toys. (It occurs to me that politicians are probably among those in the western world least affected by the digital communications revolution; if they need to communicate, they talk to their secretaries.)

Draft texts of the amendments (not yet numbered) can be found here: part I, part II, part III. There’s a (rather bad) campaign site here, and a less bad editorial – for all that it’s in a pointy-haired e-mag – in Computer World UK here.

Posted in Politics | Tagged | Leave a comment

Constant objects for fun and non-profit

The @"foo" operator for constant NSString objects in Objective-C is extremely convenient. Indeed, if it wasn’t there, programming with Cocoa would be a royal pain. Many of us have at various points wished there was equivalent syntax for NSNumbers, and possibly collections.

As an early Christmas present to fellow lovers of twisted, evil code that should never have seen the light of day under any circumstances whatsoever, I hereby present an implementation of the first half of that wish.

Continue reading

Posted in Cocoa, Code | Tagged | 3 Comments

Fun with the Objective-C Runtime

Ever since Leopard came out, I’ve wanted to do something useful with resolveInstanceMethod:. The opportunity has yet to present itself. However, I have done a couple of really silly things with it, which have until now languished in obscurity in the depths of paste.lisp.org. So here they are. Continue reading

Posted in Cocoa, Code | Tagged | 4 Comments

Migratory Code

Of the two people I know of who have tried to get at some of my code releases in the past year, a full 50 % have given up and written a new implementation from scratch because downloading and opening a zip file is too much work (as is actually complaining to someone who can fix it). On top of that, some form of version control is good to have, and my local set-ups keep breaking when I do silly things like getting a new computer or installing a new OS. So now most of my released code lives at Google Code. Yay and stuff.

Specifically, the projects in question are:

For information about the various code bits and bobs, see here.

I fully expect that the next person who tries to grab some of my code will find it to onerous to dig through the subversion repo. To that person, my message is: tough.

Posted in Code | Tagged , , | Leave a comment

Key to QWERTY

Recently on the Twitterwebs, Peter Hosey asked for a library that takes a character and returns the corresponding virtual key code (implicitly, in the U.S. keyboard layout). The intention was to use it to specify default key bindings when binding to physical keys rather than characters.

Now personally, I like to specify my defaults in plists, and if I was hard-coding them I’d use an enum. However, I thought of a mildly amusing implementation, so I wrote it (MIT/X11 license). It turned out to be less interestingly ugly than I expected, but did give me an opportunity to play with __builtin_constant_p(). This means that (if you’re using gcc) KEY_FROM_QWERTY_BODY(ex), where ex is a constant expression, will itself be a constant expression, and if ex is otherwise known at compile time to be constant (e.g. a locally-defined const int, which is not a constant expression in C) it will be constant-folded; in other cases, a function call will be generated.

Also included is code to generate the header, specifically the big ?:-based expression that does the work, using whatever keyboard layout happens to be active. Note, however, that the current implementation only includes keys that generate a single ASCII character, and skips dead keys. It includes capital letters as aliases for their lowercase equivalents, but not other shifted keys.

Conveniently, I’ve also written a library to get a display string for a virtual key code. That one does use the user’s current keyboard layout.

Posted in Code | Leave a comment

The Ubiquitous Wiretapping Bill

Krig är inte fred. Slaveri är inte frihet. Avlyssning är inte integritet. 17 juni.

This is not about programming. It’s about something that actually matters.

There’s been a little media coverage recently about a Swedish law that’s been in the works for a while. (The Register, Slashdot on The Register, The Local Edit: and IDG/PCWorld, European Digital Rights, Ars Technica; new portal site in Swedish). The law will permit the National Defense Radio Establishment (FRA, Försvarets radioanstalt) to carry out surveillance over the air and in cables crossing the Swedish border.

Surveillance over the air has been carried out since the 1940s, and satellite surveillance since 1976. This has been unconstitutional since 1995, when the European Convention on Human Rights was adopted as part of the Swedish constitution. The former second-in-command of FRA, Anders Wik, admitted this in a tape recording made public last saturday. This has received almost no attention in the Swedish press. Creating a law that permits the activity will make it legal under the EHCR¹.

The law was originally drafted by the Social Democratic Party, but withdrawn because it “received an unfortunate amount of attention”. It was reintroduced by the current four-party alliance in late 2006 (with a quick consultation carried out in the week between Christmas and New Year), but was suspended for one year by a rare minority veto instigated by the Social Democrats, who suddenly felt it should be adjusted to provide more checks and balances.

Defenders of the new law point out that it specifies that “military intelligence activity” [försvarsunderrättelseverksamhet] may only target “foreign matters” [utrikes förhållanden]. That restriction is actually already in place. However, it is the stated legal opinion of FRA that when they work for non-military authorities, such as the police, the customs office or the tax board, this is not “military intelligence activity” and is therefore not restricted to “foreign matters”. Besides that, determining that, for instance, an e-mail message is to or from a Swede involves analyzing it; as pointed out by the Council on Legislation, personal integrity is violated by opening the envelope, not just by copying the contents.

The obvious problem with this type of legislation is that it can easily be abused, especially when it’s written in very broad terms. However, there is another problem: even if it never is abused, the very existence of surveillance is oppressive and changes people’s patterns of communication.

A recent survey from Germany shows that a large portion of the population has changed or under certain circumstances would change their behaviour due to Germany’s implementation of the data retention directive. This “only” involves storing information about the participants in communication and their location. A law that allows essentially arbitrary civil authorities to order searches of the contents of people’s communication can be reasonably expected to affect behaviour more extensively. As such, citizens’ freedom of expression is curtailed. Without a functioning freedom of expression, democracy means nothing.

The proposed law is not a technical adjustment of foreign intelligence activity, as its defenders wish us to believe. It is a direct assault on democracy itself. It is expected to pass with a seven-vote majority eleven days from now, unless we can once again raise an “unfortunate amount of attention.”

Footnote: I know that we’re already being spied on by foreign agencies, but that’s less important. Foreign governments are not in a position to oppress us, except by taking over the government of Sweden. This is reflected by the Swedish bill of rights, which in several cases protects² the citizen “in his relations with the public institutions” [gentemot det allmänna].

¹ If you accept that it is “necessary in a democratic society in the interests of national security, public safety or the economic well-being of the country, for the prevention of disorder or crime, for the protection of health or morals, or for the protection of the rights and freedoms of others.”
² Sort of. Except for the EHCR, the Swedish constitution is not enforceable.

Posted in Politics | 1 Comment

Hacking your Log Messages

More nasty code inspired by IRC conversations, but this time aimed at the iPhone SDK – although it will work on Mac OS X as well, and on WebObjects 4 for Windows NT, and probably on at least some flavours of NextStep. Simply drop this file into your project, and all NSLog() output will be redirected to stdout, using a private but documented Foundation function.

Why would you want to do that? Ahh, well, that part is covered by the iPhone SDK NDA. Lawyers, eh? It must be a pretty good reason, though, given testimonials like this.

Posted in Cocoa, Code | Tagged , , | 5 Comments

Property List Extractors

Continuing a theme of tearing out useful bits of functionality from Oolite, and inspired by recent Twitter exchanges with Craig Hockenberry and Rainer Brockerhoff, here’s some really boring code for you all – gruntwork that you don’t want to repeat, but should probably be using.

Executive summary for people who don’t like rambling anecdotes: property lists and user defaults should always be type-checked, and this makes it easy.

Oolite is highly moddable, with over 150500 expansion packs. Most of them are interoperable, and typically many will be in use at once. Essentially all of these contain property lists, generally written by hand by non-programmers, often on systems without plutil or Property List Editor. As such, many contain parse errors or structural problems.

Previous versions of Oolite blithely ignored the imperfections of mere humans. Unparseable property lists were effectively ignored, leading to unexpected behaviour, often quite subtle. Structural errors, however, caused exceptions, most of them seemingly at random.

The current test releases address the syntax issues by dumping the error string from NSPropertyListSerialization and the path to the relevant file to the game’s run log. A certain amount of context-sensitive type checking is done in certain areas, such as scripting (yes, property lists are used for scripting, although that’ll be deprecated in the next stable release).

The most significant fix, however, has been to replace most uses of -objectForKey: and -objectAtIndex: with methods that check types and perform conversions where they make sense. In particular, strings are automatically converted to numbers and booleans where appropriate, which is needed to work with the more pleasant OpenStep plist syntax. When a plist value can’t be converted to the required type, or simply isn’t there, an appropriate default value is used. The game will no longer try to get keyed objects from an array, or find the first character of a dictionary. Instead of code like this:

// Hope plist has correct structure.
unsigned myMagicNumber = [[[plist objectForKey:@"someArray"] objectAtIndex:3] unsignedIntValue];

or more fault-tolerant code like this:

unsigned myMagicNumber = 0;
id array = [plist objectForKey:@"someArray"];
if ([array isKindOfClass:[NSArray class]] && [array count] > 3)
{
    id number = [array objectAtIndex:3];
    if ([number respondsToSelector:@selector(unsignedIntValue)])
    {
        myMagicNumber = [number unsignedIntValue];
    }
}

we now have code like this:

unsigned myMagicNumber = [[plist arrayForKey:@"someArray"] unsignedIntAtIndex:3];

which will not throw an exception if @"someArray" is not an array or if the array has less than four members, but will instead return 0. It’s also shorter than the broken version. Default values other than 0 can be provided in the form [array integerAtIndex:3 defaultValue:-1] or [dictionary unsignedShortForKey:@"value" defaultValue:14].

Oolite is obviously an extreme case, but many applications deal with property lists. Even more deal with NSUserDefaults, which of course is a wrapper around property lists. You may think that you know what you’re putting into user defaults and can therefore rely on it to be correctly structured, but if you’re thinking that you’re wrong. First, users will modify your preferences file, break it, and forget they did it. Second, your code is buggy. If you write any sort of collection to your prefs, there’s a chance that you’ll put an object of the wrong class there if the phase of the moon is unusual. If you don’t check it when you read it, this will cause an exception, and you’ll end up guiding a user through digging around in ~/Library to clean up your mess. Better to be fault-tolerant to start with.

So, here it is: JAPropertyListAccessors 1.2 (MIT/X11 license). It provides:

  • A non-throwing variant of -[NSArray objectAtIndex:].
  • Functions to convert objects (NSNumber or NSString) to all standard integer types, as well as float and double, clamping the results rather than overflowing, and returning a user-specified default value if no conversion can be performed.
  • Convenience methods for accessing integers, floats, strings, arrays, dictionaries, sets, or objects of a specified class from NSArray, NSDictionary or NSUserDefaults. The following transparent conversions are performed when appropriate: NSString to numbers; NSNumber and NSDate to NSString; NSArray to NSSet.
  • Convenience methods to set integers and floating-point values in NSMutableArray, NSMutableDictionary and NSUserDefaults.
  • Unit tests for the primitive conversion functions.
  • Compatible with 32-bit and 64-bit runtimes, and with garbage-collected and non-GC code.
  • Very little in the way of comments.
  • Update (version 1.0.1): Default value (rather than nil) is now returned when conversion to string fails. String conversion now uses -stringValue if available.

Posted in Cocoa, Code, Oolite | Tagged , | 1 Comment

Hacking your Crash Reports

In a conversation on #macdev, it was pointed out that the Crash Reporter has an “Application Specific Information:” line when certain Apple apps crash. This obviously warranted investigation, and through the powers of Google it was determined that said investigation had in fact happened a few months ago. How fortunate that we didn’t spend time investigating it and writing test cases, only to find someone had already done a better job.

Anyway, this is an entirely unsupported thing to do, and the means of doing it may disappear at any time, so don’t. But if you do, do it cleanly, like this:

#ifndef NDEBUG
/*  Function to set “Application Specific Information” field in crash reporter
    log in Leopard. Extremely unsupported, so not used in release builds.
*/
static void InitCrashReporterInfo(void);
static void SetCrashReporterInfo(const char *info);
static BOOL sCrashReporterInfoAvailable = NO;
#else
#define InitCrashReporterInfo() do {} while (0)
#define SetCrashReporterInfo(i) do {} while (0)
#define sCrashReporterInfoAvailable 0
#endif

/* Insert code here. Or make it non-static, depending on what you’re doing. */

#ifndef NDEBUG
static char **sCrashReporterInfo = NULL;
static char *sOldCrashReporterInfo = NULL;
static NSLock *sCrashReporterInfoLock = nil;

static void InitCrashReporterInfo(void)
{
    // Load secret symbol __crashreporter_info__; don’t do anything if not found.
    sCrashReporterInfo = dlsym(RTLD_DEFAULT, "__crashreporter_info__");
    if (sCrashReporterInfo != NULL)
    {
        // We'll need a lock, too.
        sCrashReporterInfoLock = [[NSLock alloc] init];
        if (sCrashReporterInfoLock != nil)
        {
            sCrashReporterInfoAvailable = YES;
        }
        else
        {
            sCrashReporterInfo = NULL;
        }
    }
}

static void SetCrashReporterInfo(const char *info)
{
    char                    *copy = NULL, *old = NULL;
    
    /*  Don’t do anything if setup failed or the string is NULL or empty.
        (The NULL and empty checks may not be desirable in other uses.)
    */
    if (!sCrashReporterInfoAvailable || info == NULL || *info == '\0')  return;
    
    // Copy the string, which we assume to be dynamic...
    copy = strdup(info);
    if (copy == NULL)  return;
    
    /*  ...and swap it in.
        Note that we keep a separate pointer to the old value, in case
        something else overwrites __crashreporter_info__.
    */
    [sCrashReporterInfoLock lock];
    *sCrashReporterInfo = copy;
    old = sOldCrashReporterInfo;
    sOldCrashReporterInfo = copy;
    [sCrashReporterInfoLock unlock];
    
    // Delete our old string.
    if (old != NULL)  free(old);
}
#endif

For this to be actually useful, you’d want to be using Smart Crash Reports (apparently coming soonish to a Leopardy computer near you) or something similar.

Posted in Code | Tagged , , | 2 Comments

The Mysteries of iCal, Revealed!

Note: As of Snow Leopard, this functionality has been superseded by an official Dock tile plug-in mechanism.


One of the minor yet shiny new features of Mac OS X 10.5 is that iCal’s dock icon now shows the correct date even when iCal is not running. Some assumed this to be hard-coded functionality in the dock, but a few brave souls – well, one, being me – decided to find out.The following lines in iCal’s Info.plist are a bit of a hint:

<key>DockExtra</key>
<string>iCalDockExtra.bundle</string>

iCalDockExtra.bundle can be found in iCal’s Resources directory. 90 seconds with class-dump, otool, and class-dump again show that the bundle implements a subclass of DockExtra, which in turn is defined in the private SystemUIPlugin.framework.

Having retrieved the interface declaration for the DockExtra class from class-dump, it is a simple matter to implement a subclass which logs which methods are called and when. As it turns out, the only ones called are -_initWithController: and -dealloc, but through the power of my mighty brain I worked out that I could draw the dock icon whenever I wished, using -getContext: to get a drawing context and size, -setDockImageFromContext to update the image in the dock and -releaseContext to clean up afterwards.

And that’s pretty much it. Code is here. Read the Read Me first. (The most important caveat is that the bundle is never unloaded while SystemUIServer is running.) If someone actually uses this for something useful, please tell me about it.

Edit: I thought I’d said this earlier, but apparently not: as pointed out in the comments, Alacatia Labs published the same information a week before me, and did a slightly better job too. If I had used the power of the intergoogles, I would have known that, but then I’d have missed the fun of working it out.

Posted in Cocoa, Code | Tagged , , , , | 23 Comments