libmobi
C library for handling MOBI format ebook documents
Libmobi

C library for handling Mobipocket/Kindle (MOBI) ebook format documents.

Library comes with several command line tools for working with mobi ebooks. The tools source may also be used as an example on how to use the library.

Features:

  • reading and parsing:
    • some older text Palmdoc formats (pdb),
    • Mobipocket files (prc, mobi),
    • newer MOBI files including KF8 format (azw, azw3),
    • Replica Print files (azw4)
  • recreating source files using indices
  • reconstructing references (links and embedded) in html files
  • reconstructing source structure that can be fed back to kindlegen
  • reconstructing dictionary markup (orth, infl tags)
  • writing back loaded documents
  • metadata editing
  • handling encrypted documents
  • encrypting documents for use on eInk Kindles

Todo:

  • improve writing
  • serialize rawml into raw records
  • process RESC records

Doxygen documentation:

Source:

Installation:

[for git] $ ./autogen.sh
$ ./configure
$ make
[optionally] $ make test
$ sudo make install

On macOS, you can install via Homebrew with brew install libmobi.

Alternative build systems

  • The supported way of building project is by using autotools.
  • Optionally project provides basic support for CMake, Xcode and MSVC++ systems. However these alternative configurations are not covering all options of autotools project. They are also not tested and not updated regularly.

Usage

  • single include file: #include <mobi.h>
  • linker flag: -lmobi
  • basic usage:
    #include <mobi.h>
    /* Initialize main MOBIData structure */
    /* Must be deallocated with mobi_free() when not needed */
    if (m == NULL) {
    return ERROR;
    }
    /* Open file for reading */
    FILE *file = fopen(fullpath, "rb");
    if (file == NULL) {
    return ERROR;
    }
    /* Load file into MOBIData structure */
    /* This structure will hold raw data/metadata from mobi document */
    MOBI_RET mobi_ret = mobi_load_file(m, file);
    fclose(file);
    if (mobi_ret != MOBI_SUCCESS) {
    return ERROR;
    }
    /* Initialize MOBIRawml structure */
    /* Must be deallocated with mobi_free_rawml() when not needed */
    /* In the next step this structure will be filled with parsed data */
    if (rawml == NULL) {
    return ERROR;
    }
    /* Raw data from MOBIData will be converted to html, css, fonts, media resources */
    /* Parsed data will be available in MOBIRawml structure */
    mobi_ret = mobi_parse_rawml(rawml, m);
    if (mobi_ret != MOBI_SUCCESS) {
    return ERROR;
    }
    /* Do something useful here */
    /* ... */
    /* For examples how to access data in MOBIRawml structure see mobitool.c */
    /* Free MOBIRawml structure */
    /* Free MOBIData structure */
    return SUCCESS;
    MOBI_RET
    Error codes returned by functions.
    Definition: mobi.h:59
    @ MOBI_SUCCESS
    Definition: mobi.h:60
    void mobi_free(MOBIData *m)
    Free MOBIData structure and all its children.
    Definition: memory.c:177
    MOBI_EXPORT MOBI_RET mobi_load_file(MOBIData *m, FILE *file)
    Read MOBI document from file into MOBIData structure.
    Definition: read.c:846
    MOBI_EXPORT MOBI_RET mobi_parse_rawml(MOBIRawml *rawml, const MOBIData *m)
    Parse raw records into html flow parts, markup parts, resources and indices.
    Definition: parse_rawml.c:1983
    void mobi_free_rawml(MOBIRawml *rawml)
    Free MOBIRawml structure allocated by mobi_init_rawml()
    Definition: memory.c:422
    MOBIRawml * mobi_init_rawml(const MOBIData *m)
    Initialize and return MOBIRawml structure.
    Definition: memory.c:236
    MOBIData * mobi_init(void)
    Initializer for MOBIData structure.
    Definition: memory.c:25
    Libmobi main header file.
    Main structure holding all metadata and unparsed records data.
    Definition: mobi.h:380
    Main structure containing reconstructed source parts and indices.
    Definition: mobi.h:463
  • for examples of usage, see tools

Requirements

  • compiler supporting C99
  • zlib (optional, configure –with-zlib=no to use included miniz.c instead)
  • libxml2 (optional, configure –with-libxml2=no to use internal xmlwriter)
  • tested with gcc (>=4.2.4), clang (llvm >=3.4), sun c (>=5.13), MSVC++ (2015)
  • builds on Linux, MacOS, Windows (MSVC++, MinGW), Android, Solaris
  • tested architectures: x86, x86-64, arm, ppc
  • works cross-compiled on Kindle :)

Tests

  • Github Action status
  • Travis status
  • Coverity status

Projects using libmobi

License:

  • LGPL, either version 3, or any later

Credits:

  • The huffman decompression and KF8 parsing algorithms were learned by studying python source code of KindleUnpack.
  • Thanks to all contributors of Mobileread MOBI wiki