MOBI_RET mobi_process_replica(unsigned char *pdf, const char *text, size_t *length) {
    MOBI_RET ret = MOBI_SUCCESS;
    MOBIBuffer *buf = mobi_buffer_init_null((unsigned char*) text, *length);
    if (buf == NULL) {
        debug_print("%s\n", "Memory allocation failed");
        return MOBI_MALLOC_FAILED;
    }
    mobi_buffer_setpos(buf, 12);
    size_t pdf_offset = mobi_buffer_get32(buf); /* offset 12 */
    size_t pdf_length = mobi_buffer_get32(buf); /* 16 */
    if (pdf_length > *length) {
        debug_print("PDF size from replica header too large: %zu", pdf_length);
        mobi_buffer_free_null(buf);
        return MOBI_DATA_CORRUPT;
    }
    mobi_buffer_setpos(buf, pdf_offset);
    mobi_buffer_getraw(pdf, buf, pdf_length);
    ret = buf->error;
    mobi_buffer_free_null(buf);
    *length = pdf_length;
    return ret;
}
MOBI_RET mobi_process_replica(unsigned char *pdf, const char *text, size_t *length) {
    MOBI_RET ret = MOBI_SUCCESS;
    MOBIBuffer *buf = mobi_buffer_init_null((unsigned char*) text, *length);
    if (buf == NULL) {
        debug_print("%s\n", "Memory allocation failed");
        return MOBI_MALLOC_FAILED;
    }
    mobi_buffer_setpos(buf, 12);
    size_t pdf_offset = mobi_buffer_get32(buf);  
    size_t pdf_length = mobi_buffer_get32(buf);  
    if (pdf_length > *length) {
        debug_print("PDF size from replica header too large: %zu", pdf_length);
        mobi_buffer_free_null(buf);
        return MOBI_DATA_CORRUPT;
    }
    mobi_buffer_setpos(buf, pdf_offset);
    mobi_buffer_getraw(pdf, buf, pdf_length);
    ret = buf->error;
    mobi_buffer_free_null(buf);
    *length = pdf_length;
    return ret;
}