libmobi
C library for handling MOBI format ebook documents
Macros | Functions | Variables
common.c File Reference

common tools functions More...

#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
#include <mobi.h>
#include "common.h"

Macros

#define UNUSED(x)   (void)(x)
 
#define min(a, b)   ((a) < (b) ? (a) : (b))
 
#define LIBMOBI_MSG_COUNT   ARRAYSIZE(libmobi_messages)
 

Functions

const char * libmobi_msg (const MOBI_RET ret)
 Return message for given libmobi return code. More...
 
void split_fullpath (const char *fullpath, char *dirname, char *basename, const size_t buf_len)
 Parse file name into file path and base name. Dirname or basename can be skipped by setting to null. More...
 
int make_directory (const char *path)
 Make directory. More...
 
int create_subdir (char *newdir, const size_t buf_len, const char *parent_dir, const char *subdir_name)
 Create subfolder in directory. More...
 
int write_file (const unsigned char *buffer, const size_t len, const char *path)
 Open file descriptor and write buffer to it. More...
 
int write_to_dir (const char *dir, const char *name, const unsigned char *buffer, const size_t len)
 Write content to file in directory. More...
 
bool dir_exists (const char *path)
 Check whether given path exists and is a directory. More...
 
void normalize_path (char *path)
 Make sure we use consistent separators on Windows builds. More...
 
void print_summary (const MOBIData *m)
 Print summary meta information. More...
 
void print_exth (const MOBIData *m)
 Print all loaded EXTH record tags. More...
 
int set_decryption_pid (MOBIData *m, const char *pid)
 Set PID for decryption. More...
 
int set_decryption_serial (MOBIData *m, const char *serial)
 Set device serial number for decryption. More...
 
int set_decryption_key (MOBIData *m, const char *serial, const char *pid)
 Set key for decryption. Use user supplied pid or device serial number. More...
 
int save_mobi (MOBIData *m, const char *fullpath, const char *suffix)
 Save mobi file. More...
 

Variables

const char separator = '/'
 
bool outdir_opt = false
 
char outdir [FILENAME_MAX]
 
const char * libmobi_messages []
 Messages for libmobi return codes For reference see enum MOBI_RET in mobi.h. More...
 

Detailed Description

common tools functions

Function Documentation

◆ create_subdir()

int create_subdir ( char *  newdir,
const size_t  buf_len,
const char *  parent_dir,
const char *  subdir_name 
)

Create subfolder in directory.

Parameters
[in,out]newdirPath to created subfolder
[in]buf_lenBuffer size fo created subfolder
[in]parent_dirDirectory path
[in]subdir_nameSubfolder name
Returns
SUCCESS or ERROR
Examples
common.c, and mobitool.c.

◆ dir_exists()

bool dir_exists ( const char *  path)

Check whether given path exists and is a directory.

Parameters
[in]pathPath to be tested
Returns
True if directory exists, false otherwise
Examples
common.c, and mobitool.c.

◆ libmobi_msg()

const char* libmobi_msg ( const MOBI_RET  ret)

Return message for given libmobi return code.

Parameters
[in]retLibmobi return code
Returns
Message string
Examples
common.c, mobimeta.c, and mobitool.c.

◆ make_directory()

int make_directory ( const char *  path)

Make directory.

Parameters
[in]pathPath
Returns
SUCCESS or ERROR
Examples
common.c, and mobitool.c.

◆ normalize_path()

void normalize_path ( char *  path)

Make sure we use consistent separators on Windows builds.

Parameters
[in,out]pathPath to be fixed
Examples
common.c, mobimeta.c, and mobitool.c.

◆ print_exth()

void print_exth ( const MOBIData m)

Print all loaded EXTH record tags.

Parameters
[in]mMOBIData structure
Examples
common.c, mobimeta.c, and mobitool.c.

◆ print_summary()

void print_summary ( const MOBIData m)

Print summary meta information.

Parameters
[in]mMOBIData structure
Examples
common.c, mobimeta.c, and mobitool.c.

◆ save_mobi()

int save_mobi ( MOBIData m,
const char *  fullpath,
const char *  suffix 
)

Save mobi file.

Parameters
[in,out]mMOBIData struicture
[in]fullpathFull file path
[in]suffixSuffix appended to file name
Returns
SUCCESS or ERROR
Examples
common.c, and mobitool.c.

◆ set_decryption_key()

int set_decryption_key ( MOBIData m,
const char *  serial,
const char *  pid 
)

Set key for decryption. Use user supplied pid or device serial number.

Parameters
[in,out]mMOBIData structure
[in]serialSerial number
[in]pidPid
Returns
SUCCESS or error code
Examples
common.c, mobimeta.c, and mobitool.c.

◆ set_decryption_pid()

int set_decryption_pid ( MOBIData m,
const char *  pid 
)

Set PID for decryption.

Parameters
[in,out]mMOBIData structure
[in]pidSerial number
Returns
SUCCESS or error code
Examples
common.c.

◆ set_decryption_serial()

int set_decryption_serial ( MOBIData m,
const char *  serial 
)

Set device serial number for decryption.

Parameters
[in,out]mMOBIData structure
[in]serialSerial number
Returns
SUCCESS or error code
Examples
common.c.

◆ split_fullpath()

void split_fullpath ( const char *  fullpath,
char *  dirname,
char *  basename,
const size_t  buf_len 
)

Parse file name into file path and base name. Dirname or basename can be skipped by setting to null.

Parameters
[in]fullpathFull file path
[in,out]dirnameWill be set to full dirname
[in,out]basenameWill be set to file basename
[in]buf_lenSize of each ouput buffer: dirname and basename
Examples
common.c, and mobitool.c.

◆ write_file()

int write_file ( const unsigned char *  buffer,
const size_t  len,
const char *  path 
)

Open file descriptor and write buffer to it.

Parameters
[in]bufferBuffer
[in]lenBuffer length
[in]pathFile path
Returns
SUCCESS or ERROR
Examples
common.c, and mobitool.c.

◆ write_to_dir()

int write_to_dir ( const char *  dir,
const char *  name,
const unsigned char *  buffer,
const size_t  len 
)

Write content to file in directory.

Parameters
[in]dirDirectory path
[in]nameFile name
[in]bufferBuffer
[in]lenBuffer length
Returns
SUCCESS or ERROR
Examples
common.c, and mobitool.c.

Variable Documentation

◆ libmobi_messages

const char* libmobi_messages[]
Initial value:
= {
"Success",
"Generic error",
"Wrong function parameter",
"Corrupted data",
"File not found",
"Document is encrypted",
"Unsupported document format",
"Memory allocation error",
"Initialization error",
"Buffer error",
"XML error",
"Invalid DRM pid",
"DRM key not found",
"DRM support not included",
"Write failed",
"DRM expired"
}

Messages for libmobi return codes For reference see enum MOBI_RET in mobi.h.

Examples
common.c.