DRM Content Format File Library

This small library reads and writes DCF files. DCF files are encrypted files according to the Open Mobile Alliance OMA v2 specification. I wrote this library, written in Java 1.5, simply because I had worked on some DRM systems previously and — frankly — the lack of programming tasks at work at the time made me want to write something that involved a lot of bit-fiddling. You can quickly gain an understanding of the library by looking at the test cases, for example:

 public void testSimple() throws Exception {

        DcfFile file = new DcfFile();

        ContainerBox container = getContainer();
        file.addDRMContainer(container);
        file.addDRMContainer(getContainer());

        MutableDRMInformation mutInfo = getMutableDRMInformation();
        file.setMutableDRMInformation(mutInfo);

        ByteArrayOutputStream os = new ByteArrayOutputStream();
        file.writeOn(os);
        byte[] bytes = os.toByteArray();
        assertTrue(bytes.length > 0);
        os.close();

        Debug.debug("DCF File is:");
        Debug.hexdump(bytes);

        DcfFile reRead = new DcfFile();
        reRead.parse(new ByteArrayInputStream(bytes));

        MutableDRMInformation mutableInfo = reRead.getMutableInfo();
        assertNotNull(mutableInfo);
        assertEquals(0, mutableInfo.getSize().compareTo(mutInfo.getSize()));

        List<ContainerBox> drmContainers = reRead.getDrmContainers();
        assertEquals(2, drmContainers.size());

        ContainerBox cont = (ContainerBox) drmContainers.get(0);
        assertEquals(0, cont.getSize().compareTo(container.getSize()));

    }

    protected MutableDRMInformation getMutableDRMInformation() {
        MutableDRMInformation orig = new MutableDRMInformation();

        TransactionTrackingBox track = new TransactionTrackingBox();
        track.setTransactionId("xid");
        orig.addBox(track);

        RightsObjectBox rid = new RightsObjectBox();
        rid.setData(PrimitiveTypes.toAsciiBytes("foo"));
        orig.addBox(rid);

        return orig;
    }

    protected ContainerBox getContainer() {
        ContainerBox container = new ContainerBox();

        DiscreteHeaderBox contentHeaders = new DiscreteHeaderBox();

        CommonHeadersBox common = new CommonHeadersBox();
        common.setContentId("contentId");

        contentHeaders.setCommonHeaders(common);
        contentHeaders.setContentType("text/html");

        UserDataBox userData = new UserDataBox();
        userData.setIconUri("http://icon");
        userData.setInfoUrl("http://info");
        contentHeaders.setUserData(userData);
        container.setContentHeaders(contentHeaders);

        ContentObjectBox drmContent = new ContentObjectBox();
        drmContent.setData(new ByteArrayData(PrimitiveTypes.toUTF8Bytes("xxxx")));
        container.setDrmContent(drmContent);

        DataBox extension = new DataBox(DCFBoxTypes.UNKN);
        extension.setData(PrimitiveTypes.toUTF8Bytes("foobar"));
        container.addExtension(extension);
        return container;
    }

Download the full source code right here.