Clone wiki

lrc / Library

The Linux Resource Compiler Library liblrc

To extract resource data from a compiled resource data file the lrc project provides a small library to do so.

The main two classes in this library are the ResourceManager and the Resource class.

ResourceManager class

The constructor expects the filename of a .rdf file, its compression type if the whole file was compressed and its encryption type and password if the whole file was encrypted. Provide None and nullptr if the file was not compressed or encrypted. If the provided file does not exist a lrcFileNotFound exception will be thrown.

#include "ResourceManager.hxx"

int main(void)
    ResourceManager *gfx;

    gfx = new ResourceManager("gfx.rdf", lrc::NoneCompression, lrc::NoneEncryption, nullptr);

For directly linked resources, there is a second constructor. This one expects the start and the end address of the embedded resource, and just like the first one the compression type (if the wohle file was compressed) and the encryption type and password (if the whole file was encrypted). The start and end address will be two external symbols of the following name: _binary_<file>_rdf_start and _binary_<file>_rdf_end. Have a look at the example:

#include "ResourceManager.hxx"

extern "C" {
	extern unsigned char _binary_EmbeddedLogos_rdf_start;
	extern unsigned char _binary_EmbeddedLogos_rdf_end;

int main(int argc, char **argv)
	ResourceManager *logos;

	logos = new ResourceManager(&_binary_EmbeddedLogos_rdf_start, &_binary_EmbeddedLogos_rdf_end,
	  lrc::NoneCompression, lrc::NoneEncryption, nullptr);

The ResourceManager class provides two public methods

char **get_resource_ids(int &);
Resource *get_resource(const char *);

The first method returns a list of all resource identifiers of the resource data file. The given parameter is an out parameter and contains the number of resources in the file.

Resource *get_resource(const char *) returns the desired resource as Resource class. The desired resource is defined by the given resource identifier. If the resource with the given identifier cannot be found nullptr is returned.

Resource class

The Resource class which will be used to return a resource has three public methods

char *get_ID(void);
unsigned char *get_res_data(void);
size_t get_res_size(void);

The first method char *get_ID(void) returns the identifier of the resource.
unsigned char *get_res_data(void) returns a pointer to the actual resource data.
size_t get_res_size(void) finally returns the size of the resource data.


A small example

#include "ResourceManager.hxx"
#include "Resource.hxx"

int main(void)
    ResourceManager *gfx, *sfx;
    Resource *heroImg, *titleImg, *titleMusic;

    gfx = new ResourceManager("gfx.rdf", lrc::zLibCompression, lrc::NoneEncryption, nullptr);
    sfx = new ResourceManager("sfx.rdf", lrc::NoneCompression, lrc::SerpentEncryption, "secrect");

    heroImg = gfx->get_resource("HeroPic");
    titleImg = gfx->get_resource("Title");
    titleMusic = sfx->get_resource("Title");

    // Do something to show the title image and
    // to play the title music

    // Do more stuff

    delete titleMusic;
    delete titleImg;
    delete heroImg;

    delete sfx;
    delete gfx;

As this example also shows is the caller responsible to free/delete the returned instances of the Resource class.