278 points · by goranmoomin · 5y ago · 59 comments
I wound up doing this a while ago for a similar toy project. After some poking around, it turned out that dictionary bundles are entirely supported by system APIs in CoreServices! The APIs are private, but Apple accidentally shipped a header file with documentation for them in the 10.7 SDK [1]. You can load a dictionary with `IDXCreateIndexObject()`, read through its indices with the search methods (and the convenient `kIDXSearchAllMatch`), and get pointers to its entry data with `IDXGetFieldDataPtrs()`.
It takes a bit of fiddling to figure out the structure (there are multiple indices for headwords, search keywords, cross-references, etc., and the API is a general-purpose trie library) and request the right fields, but those property lists in the bundle are there to help! (As the author of this article discovered, the entries are compressed and are proceeded with a 4-byte length marker.)
[1] https://github.com/phracker/MacOSX-SDKs/blob/master/MacOSX10...
To find the Kit in question, simply Google "com.apple.TrieAccessMethod" and you should find it online.
binwalk --dd='.*' *.asset
Edit: should have read the comments before I posted this, because enragedcacti already mentioned this tool an hour ago.I used it and was able to figure out the remaining bits of the file format thanks to you and other tips in this thread.
https://josephg.com/blog/reverse-engineering-apple-dictionar...
There's also a command-line tool that can query the dictionary:
https://github.com/takumakei/osx-dictionary
Something I haven't yet reverse-engineered is Apple's word segmentation. I can get the word breaks in Chinese by pressing option + right arrow + space, repeatedly. But I have no idea how the backend for that works.
Unless they changed it, it's probably similar to CFStringTokenizer which used ICU Boundary Analysis (and maybe mecab for Japanese).
https://unicode-org.github.io/icu/userguide/boundaryanalysis...
Is that the same as the macOS dictionary being parsed here? It seems like a pretty big file to grep every time!
I assume at compile time it's converted to a more efficient query format
However, I'm concerned that the dictionary is copyrighted. Is there any precedent that says whether such a tool would be legal/illegal?
https://linux.die.net/man/1/dictfmt http://www.catb.org/jargon/oldversions/jarg447.txt
What's it good for?
I've used your article and other previous attempts to clear up the remaining unknown bits of the file format and build apple-peeler, a tool for extracting the XML from the dictionary files.
Just to mention that Core Foundation includes tokenisers (CFStringTokenizer) and lemmatisers (NSLinguisticTagger) of its own in case you ever wanted to avoid the Python processing!
But be aware (i.e. "beware") that Apple can pull the rug out of unpublished APIs, without warning.
I have been caught out, by this, myself.
So the difficulty we’re seeing here isn’t deliberate obfuscation, but rather just an dev using a database structure to make app design and word lookup easier.
If there really was licensing concerns I would expect there to at-least be some basic encryption, not just some basic compression.
http://toastytech.com/guis/ns20mail.png
(Edit: beaten by 30 seconds!)
Maybe not all of it. But I wouldn't doubt if bits of it went all the way back to the early days where a fancy dictionary was one of the star features of NeXTStep.
After I wrote my post about Apple's dictionary files, I got a mysterious email showing up in my inbox. The email was from someone who's spent some time writing code to do the same thing, but doesn't want to post it under his own name in case he falls fowl of his country's DMCA equivalent. Crazy. He said I could post his code under the condition that I took his name off it.
https://github.com/nikvdp/dictionary-api/blob/master/convert...