libmobi
C library for handling MOBI format ebook documents
Data Structures | Functions
buffer.h File Reference
#include "config.h"
#include "mobi.h"

Go to the source code of this file.

Data Structures

struct  MOBIBuffer
 Buffer to read to/write from. More...
 

Functions

MOBIBuffermobi_buffer_init (const size_t len)
 Initializer for MOBIBuffer structure. More...
 
MOBIBuffermobi_buffer_init_null (unsigned char *data, const size_t len)
 Initializer for MOBIBuffer structure. More...
 
void mobi_buffer_resize (MOBIBuffer *buf, const size_t newlen)
 Resize buffer. More...
 
void mobi_buffer_add8 (MOBIBuffer *buf, const uint8_t data)
 Adds 8-bit value to MOBIBuffer. More...
 
void mobi_buffer_add16 (MOBIBuffer *buf, const uint16_t data)
 Adds 16-bit value to MOBIBuffer. More...
 
void mobi_buffer_add32 (MOBIBuffer *buf, const uint32_t data)
 Adds 32-bit value to MOBIBuffer. More...
 
void mobi_buffer_addraw (MOBIBuffer *buf, const unsigned char *data, const size_t len)
 Adds raw data to MOBIBuffer. More...
 
void mobi_buffer_addstring (MOBIBuffer *buf, const char *str)
 Adds string to MOBIBuffer without null terminator. More...
 
void mobi_buffer_addzeros (MOBIBuffer *buf, const size_t count)
 Adds count of zeroes to MOBIBuffer. More...
 
uint8_t mobi_buffer_get8 (MOBIBuffer *buf)
 Reads 8-bit value from MOBIBuffer. More...
 
uint16_t mobi_buffer_get16 (MOBIBuffer *buf)
 Reads 16-bit value from MOBIBuffer. More...
 
uint32_t mobi_buffer_get32 (MOBIBuffer *buf)
 Reads 32-bit value from MOBIBuffer. More...
 
uint32_t mobi_buffer_get_varlen (MOBIBuffer *buf, size_t *len)
 Reads variable length value from MOBIBuffer. More...
 
uint32_t mobi_buffer_get_varlen_dec (MOBIBuffer *buf, size_t *len)
 Reads variable length value from MOBIBuffer going backwards. More...
 
void mobi_buffer_dup8 (uint8_t **val, MOBIBuffer *buf)
 Read 8-bit value from MOBIBuffer into allocated memory. More...
 
void mobi_buffer_dup16 (uint16_t **val, MOBIBuffer *buf)
 Read 16-bit value from MOBIBuffer into allocated memory. More...
 
void mobi_buffer_dup32 (uint32_t **val, MOBIBuffer *buf)
 Read 32-bit value from MOBIBuffer into allocated memory. More...
 
void mobi_buffer_getstring (char *str, MOBIBuffer *buf, const size_t len)
 Reads raw data from MOBIBuffer and pads it with zero character. More...
 
void mobi_buffer_appendstring (char *str, MOBIBuffer *buf, const size_t len)
 Reads raw data from MOBIBuffer, appends it to a string and pads it with zero character. More...
 
void mobi_buffer_getraw (void *data, MOBIBuffer *buf, const size_t len)
 Reads raw data from MOBIBuffer. More...
 
unsigned char * mobi_buffer_getpointer (MOBIBuffer *buf, const size_t len)
 Get pointer to MOBIBuffer data at offset. More...
 
void mobi_buffer_copy8 (MOBIBuffer *dest, MOBIBuffer *source)
 Copy 8-bit value from one MOBIBuffer into another. More...
 
void mobi_buffer_move (MOBIBuffer *buf, const int offset, const size_t len)
 Copy raw value within one MOBIBuffer. More...
 
void mobi_buffer_copy (MOBIBuffer *dest, MOBIBuffer *source, const size_t len)
 Copy raw value from one MOBIBuffer into another. More...
 
bool mobi_buffer_match_magic (MOBIBuffer *buf, const char *magic)
 Check if buffer data header contains magic signature. More...
 
bool mobi_buffer_match_magic_offset (MOBIBuffer *buf, const char *magic, const size_t offset)
 Check if buffer contains magic signature at given offset. More...
 
void mobi_buffer_seek (MOBIBuffer *buf, const int diff)
 Move current buffer offset by diff bytes. More...
 
void mobi_buffer_setpos (MOBIBuffer *buf, const size_t pos)
 Set buffer offset to pos position. More...
 
void mobi_buffer_free (MOBIBuffer *buf)
 Free pointer to MOBIBuffer structure and pointer to data. More...
 
void mobi_buffer_free_null (MOBIBuffer *buf)
 Free pointer to MOBIBuffer structure. More...
 

Detailed Description

Copyright (c) 2014 Bartek Fabiszewski http://www.fabiszewski.net

This file is part of libmobi. Licensed under LGPL, either version 3, or any later. See http://www.gnu.org/licenses/

Function Documentation

◆ mobi_buffer_add16()

void mobi_buffer_add16 ( MOBIBuffer buf,
const uint16_t  data 
)

Adds 16-bit value to MOBIBuffer.

Parameters
[in,out]bufMOBIBuffer structure to be filled with data
[in]dataInteger to be put into the buffer

◆ mobi_buffer_add32()

void mobi_buffer_add32 ( MOBIBuffer buf,
const uint32_t  data 
)

Adds 32-bit value to MOBIBuffer.

Parameters
[in,out]bufMOBIBuffer structure to be filled with data
[in]dataInteger to be put into the buffer

◆ mobi_buffer_add8()

void mobi_buffer_add8 ( MOBIBuffer buf,
const uint8_t  data 
)

Adds 8-bit value to MOBIBuffer.

Parameters
[in,out]bufMOBIBuffer structure to be filled with data
[in]dataInteger to be put into the buffer

◆ mobi_buffer_addraw()

void mobi_buffer_addraw ( MOBIBuffer buf,
const unsigned char *  data,
const size_t  len 
)

Adds raw data to MOBIBuffer.

Parameters
[in,out]bufMOBIBuffer structure to be filled with data
[in]dataPointer to read data
[in]lenSize of the read data

◆ mobi_buffer_addstring()

void mobi_buffer_addstring ( MOBIBuffer buf,
const char *  str 
)

Adds string to MOBIBuffer without null terminator.

Parameters
[in,out]bufMOBIBuffer structure to be filled with data
[in]strPointer to string

◆ mobi_buffer_addzeros()

void mobi_buffer_addzeros ( MOBIBuffer buf,
const size_t  count 
)

Adds count of zeroes to MOBIBuffer.

Parameters
[in,out]bufMOBIBuffer structure to be filled with data
[in]countNumber of zeroes to be put into the buffer

◆ mobi_buffer_appendstring()

void mobi_buffer_appendstring ( char *  str,
MOBIBuffer buf,
const size_t  len 
)

Reads raw data from MOBIBuffer, appends it to a string and pads it with zero character.

Parameters
[in,out]strA string to which data will be appended
[in]bufMOBIBuffer structure containing data
[in]lenLength of the data to be read from buffer

◆ mobi_buffer_copy()

void mobi_buffer_copy ( MOBIBuffer dest,
MOBIBuffer source,
const size_t  len 
)

Copy raw value from one MOBIBuffer into another.

Parameters
[out]destDestination buffer
[in]sourceSource buffer
[in]lenNumber of bytes to copy

◆ mobi_buffer_copy8()

void mobi_buffer_copy8 ( MOBIBuffer dest,
MOBIBuffer source 
)

Copy 8-bit value from one MOBIBuffer into another.

Parameters
[out]destDestination buffer
[in]sourceSource buffer

◆ mobi_buffer_dup16()

void mobi_buffer_dup16 ( uint16_t **  val,
MOBIBuffer buf 
)

Read 16-bit value from MOBIBuffer into allocated memory.

Read 16-bit value from buffer into allocated memory. Returns pointer to the value, which must be freed later. If the data is not accessible function will return null pointer.

Parameters
[out]valPointer to value or null pointer on failure
[in]bufMOBIBuffer structure containing data

◆ mobi_buffer_dup32()

void mobi_buffer_dup32 ( uint32_t **  val,
MOBIBuffer buf 
)

Read 32-bit value from MOBIBuffer into allocated memory.

Read 32-bit value from buffer into allocated memory. Returns pointer to the value, which must be freed later. If the data is not accessible function will return null pointer.

Parameters
[out]valPointer to value
[in]bufMOBIBuffer structure containing data

◆ mobi_buffer_dup8()

void mobi_buffer_dup8 ( uint8_t **  val,
MOBIBuffer buf 
)

Read 8-bit value from MOBIBuffer into allocated memory.

Read 8-bit value from buffer into memory allocated by the function. Returns pointer to the value, which must be freed later. If the data is not accessible function will return null pointer.

Parameters
[out]valPointer to value or null pointer on failure
[in]bufMOBIBuffer structure containing data

◆ mobi_buffer_free()

void mobi_buffer_free ( MOBIBuffer buf)

Free pointer to MOBIBuffer structure and pointer to data.

Free data initialized with mobi_buffer_init();

Parameters
[in]bufMOBIBuffer structure

◆ mobi_buffer_free_null()

void mobi_buffer_free_null ( MOBIBuffer buf)

Free pointer to MOBIBuffer structure.

Free data initialized with mobi_buffer_init_null(); Unlike mobi_buffer_free() it will not free pointer to buf->data

Parameters
[in]bufMOBIBuffer structure

◆ mobi_buffer_get16()

uint16_t mobi_buffer_get16 ( MOBIBuffer buf)

Reads 16-bit value from MOBIBuffer.

Parameters
[in]bufMOBIBuffer structure containing data
Returns
Read value, 0 if end of buffer is encountered

◆ mobi_buffer_get32()

uint32_t mobi_buffer_get32 ( MOBIBuffer buf)

Reads 32-bit value from MOBIBuffer.

Parameters
[in]bufMOBIBuffer structure containing data
Returns
Read value, 0 if end of buffer is encountered

◆ mobi_buffer_get8()

uint8_t mobi_buffer_get8 ( MOBIBuffer buf)

Reads 8-bit value from MOBIBuffer.

Parameters
[in]bufMOBIBuffer structure containing data
Returns
Read value, 0 if end of buffer is encountered

◆ mobi_buffer_get_varlen()

uint32_t mobi_buffer_get_varlen ( MOBIBuffer buf,
size_t *  len 
)

Reads variable length value from MOBIBuffer.

Reads maximum 4 bytes from the buffer. Stops when byte has bit 7 set.

Parameters
[in]bufMOBIBuffer structure containing data
[out]lenValue will be increased by number of bytes read
Returns
Read value, 0 if end of buffer is encountered

◆ mobi_buffer_get_varlen_dec()

uint32_t mobi_buffer_get_varlen_dec ( MOBIBuffer buf,
size_t *  len 
)

Reads variable length value from MOBIBuffer going backwards.

Reads maximum 4 bytes from the buffer. Stops when byte has bit 7 set.

This function has a limitation. It will not read first byte in a buffer, as it would cause buffer offset to underflow. That means that it cannot read variable length values that are placed at the beginning of a buffer. This will result in an error.

Parameters
[in]bufMOBIBuffer structure containing data
[out]lenValue will be increased by number of bytes read
Returns
Read value, 0 if end of buffer is encountered

◆ mobi_buffer_getpointer()

unsigned char* mobi_buffer_getpointer ( MOBIBuffer buf,
const size_t  len 
)

Get pointer to MOBIBuffer data at offset.

Parameters
[in]bufMOBIBuffer structure containing data
[in]lenCheck if requested length is available in buffer
Returns
Pointer to offset, or NULL on failure

◆ mobi_buffer_getraw()

void mobi_buffer_getraw ( void *  data,
MOBIBuffer buf,
const size_t  len 
)

Reads raw data from MOBIBuffer.

Parameters
[out]dataDestination to which data will be appended
[in]bufMOBIBuffer structure containing data
[in]lenLength of the data to be read from buffer

◆ mobi_buffer_getstring()

void mobi_buffer_getstring ( char *  str,
MOBIBuffer buf,
const size_t  len 
)

Reads raw data from MOBIBuffer and pads it with zero character.

Parameters
[out]strDestination for string read from buffer. Length must be (len + 1)
[in]bufMOBIBuffer structure containing data
[in]lenLength of the data to be read from buffer

◆ mobi_buffer_init()

MOBIBuffer* mobi_buffer_init ( const size_t  len)

Initializer for MOBIBuffer structure.

It allocates memory for structure and for data. Memory should be freed with mobi_buffer_free().

Parameters
[in]lenSize of data to be allocated for the buffer
Returns
MOBIBuffer on success, NULL otherwise

◆ mobi_buffer_init_null()

MOBIBuffer* mobi_buffer_init_null ( unsigned char *  data,
const size_t  len 
)

Initializer for MOBIBuffer structure.

It allocates memory for structure but, unlike mobi_buffer_init(), it does not allocate memory for data. Instead it works on external data. Memory should be freed with mobi_buffer_free_null() (buf->data will not be deallocated).

Parameters
[in,out]dataSet data as buffer data
[in]lenSize of data held by the buffer
Returns
MOBIBuffer on success, NULL otherwise

◆ mobi_buffer_match_magic()

bool mobi_buffer_match_magic ( MOBIBuffer buf,
const char *  magic 
)

Check if buffer data header contains magic signature.

Parameters
[in]bufMOBIBuffer buffer containing data
[in]magicMagic signature
Returns
boolean true on match, false otherwise

◆ mobi_buffer_match_magic_offset()

bool mobi_buffer_match_magic_offset ( MOBIBuffer buf,
const char *  magic,
const size_t  offset 
)

Check if buffer contains magic signature at given offset.

Parameters
[in]bufMOBIBuffer buffer containing data
[in]magicMagic signature
[in]offsetOffset
Returns
boolean true on match, false otherwise

◆ mobi_buffer_move()

void mobi_buffer_move ( MOBIBuffer buf,
const int  offset,
const size_t  len 
)

Copy raw value within one MOBIBuffer.

Memmove len bytes from offset (relative to current position) to current position in buffer and advance buffer position. Data may overlap.

Parameters
[out]bufBuffer
[in]offsetOffset to read from
[in]lenNumber of bytes to copy

◆ mobi_buffer_resize()

void mobi_buffer_resize ( MOBIBuffer buf,
const size_t  newlen 
)

Resize buffer.

Smaller size than offset will cause data truncation.

Parameters
[in,out]bufMOBIBuffer structure to be filled with data
[in]newlenNew buffer size

◆ mobi_buffer_seek()

void mobi_buffer_seek ( MOBIBuffer buf,
const int  diff 
)

Move current buffer offset by diff bytes.

Parameters
[in,out]bufMOBIBuffer buffer containing data
[in]diffNumber of bytes by which the offset is adjusted

◆ mobi_buffer_setpos()

void mobi_buffer_setpos ( MOBIBuffer buf,
const size_t  pos 
)

Set buffer offset to pos position.

Parameters
[in,out]bufMOBIBuffer buffer containing data
[in]posNew position