LZVN encode

Apple did not release the source code of the new LZVN functions, and you may not need it, because you can add the (private) PackageKit.framework to your development project. Here is an example taken from kernelcache.c to show you how it works:

if (compressionType == COMP_TYPE_FASTLIB)
	size_t outSize = 0;
	void * work_space = malloc(lzvn_encode_work_size());
	if (work_space != NULL)
		kernelHeader->compressType = OSSwapHostToBigInt32(COMP_TYPE_FASTLIB);
		outSize = lzvn_encode(buf + offset, bufsize, (u_int8_t *)CFDataGetBytePtr(prelinkImage), CFDataGetLength(prelinkImage), work_space);

		if (outSize != 0)
			bufend = buf + offset + outSize;

The call to lzvn_encode_work_size() returns 0x80000 and thus malloc() will allocate a 512 KB buffer (work_space). And this call is used in IOGraphicsFamily.kext

extern "C" size_t lzvn_decode(void * __restrict dst, size_t dst_size, const void * __restrict src, size_t src_size);

In short. You can also use this for kexts that you (plan to) develop. However. It won’t work on Linux and Windows, of course, so you may want to look at my LZVN Github repository where you can find the source code of a command line tool that I wrote to encode (compress) a file.

Have fun!


Disassembled code removed and link to my Github repository added.

3 thoughts on “LZVN encode

  1. Hi Pike,
    I forked your LZVN branch and made some interesting changes 😉


    It now supports a make installer command which auto builds a proper package.
    Built installer package is also included on the github fork.
    Both the binary and the package are signed with my proper Apple developer ID certificate.
    I also converted lzvn_encode_work_size to C (not hard lol).
    Other changes where done to lzvn_encode.s which is now lzvn_enc.s.

    Take a peek, you’ll be pleasantly surprised… 😀

    Check of package with Apple’s signature verification tool:
    check-signature lzvn.pkg
    (c) 2014 Apple Inc. All rights reserved.

    Check of lzvn binary with Apple’s signature verification tool:
    check-signature /usr/bin/lzvn
    (c) 2014 Apple Inc. All rights reserved.

    which means it is fully Gatekeeper compliant 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s