Verified Commit 2f78f75d authored by Tomáš Stefan's avatar Tomáš Stefan
Browse files

Big refactoring

now possible to set data as one of the following:
 - FILE *
 - path to the file (const char *)
 - char *, pointing to the pdf content

all data related operations got their custom functions

if the input source is file and its size is under threshold defined
 in config.h, all the content is loaded into memory
parent a069df3c
Loading
Loading
Loading
Loading
+18 −8
Original line number Diff line number Diff line
@@ -14,15 +14,22 @@ void sigil_zeroize(void *a, size_t bytes);
int is_digit(const char_t c);
int is_whitespace(const char_t c);

sigil_err_t skip_leading_whitespaces(FILE *in);
sigil_err_t skip_dictionary(FILE *in);
sigil_err_t skip_dict_unknown_value(FILE *in);
sigil_err_t pdf_read(sigil_t *sgl, size_t size, char *result, size_t *res_size);
sigil_err_t pdf_get_char(sigil_t *sgl, char *result);
sigil_err_t pdf_peek_char(sigil_t *sgl, char *result);

sigil_err_t parse_number(FILE *in, size_t *number);
sigil_err_t parse_keyword(FILE *in, keyword_t *keyword);
sigil_err_t parse_free_indicator(FILE *in, free_indicator_t *result);
sigil_err_t parse_indirect_reference(FILE *in, reference_t *ref);
sigil_err_t parse_dict_key(FILE *in, dict_key_t *dict_key);
sigil_err_t pdf_move_pos_rel(sigil_t *sgl, ssize_t shift_bytes);
sigil_err_t pdf_move_pos_abs(sigil_t *sgl, size_t position);

sigil_err_t skip_leading_whitespaces(sigil_t *sgl);
sigil_err_t skip_dictionary(sigil_t *sgl);
sigil_err_t skip_dict_unknown_value(sigil_t *sgl);

sigil_err_t parse_number(sigil_t *sgl, size_t *number);
sigil_err_t parse_keyword(sigil_t *sgl, keyword_t *keyword);
sigil_err_t parse_free_indicator(sigil_t *sgl, free_indicator_t *result);
sigil_err_t parse_indirect_reference(sigil_t *sgl, reference_t *ref);
sigil_err_t parse_dict_key(sigil_t *sgl, dict_key_t *dict_key);

const char *sigil_err_string(sigil_err_t err);

@@ -31,6 +38,9 @@ void print_module_result(int result, int verbosity);
void print_test_item(const char *test_name, int verbosity);
void print_test_result(int result, int verbosity);

sigil_t *test_prepare_sgl_content(char *content, size_t size);
sigil_t *test_prepare_sgl_path(const char *path);

int sigil_auxiliary_self_test(int verbosity);

#endif /* PDF_SIGIL_AUXILIARY_H */
+6 −3
Original line number Diff line number Diff line
@@ -11,6 +11,9 @@
// capacity to choose for the first xref allocation
#define XREF_PREALLOCATION          10

// threshold in bytes for loading whole file into buffer
#define THRESHOLD_FILE_BUFFERING    10485760

// validate values
int sigil_config_self_test(int verbosity);

+19 −24
Original line number Diff line number Diff line
@@ -6,8 +6,9 @@
#define COLOR_GREEN        "\x1b[32m"
#define COLOR_RESET        "\x1b[0m"

#define KEYWORD_xref       0
#define KEYWORD_trailer    1
#define KEYWORD_UNSET      0
#define KEYWORD_xref       1
#define KEYWORD_trailer    2

#define XREF_TYPE_UNSET    0
#define XREF_TYPE_TABLE    1
@@ -16,27 +17,21 @@
#define IN_USE_ENTRY       0
#define FREE_ENTRY         1

#define DICT_KEY_Size      0
#define DICT_KEY_Prev      1
#define DICT_KEY_Root      2
#define DICT_KEY_unknown   3
#define DICT_KEY_UNKNOWN   0
#define DICT_KEY_Size      1
#define DICT_KEY_Prev      2
#define DICT_KEY_Root      3

#define DEALLOCATE_FILE    0x01
#define DEALLOCATE_BUFFER  0x02

#define ERR_NO                0
#define ERR_ALLOC          1
#define ERR_PARAM          2
#define ERR_ALLOCATION        1
#define ERR_PARAMETER         2
#define ERR_IO                3
#define ERR_PDF_CONT       4
#define ERR_NOT_IMPL       5
#define ERR_6              6
#define ERR_7              7
#define ERR_8              8
#define ERR_9              9
#define ERR_10            10
#define ERR_11            11
#define ERR_12            12
#define ERR_13            13
#define ERR_14            14
#define ERR_15            15
#define ERR_16            16
#define ERR_PDF_CONTENT       4
#define ERR_NOT_IMPLEMENTED   5
#define ERR_NO_DATA           6
#define ERR_END_OF_DICT       7

#endif /* PDF_SIGIL_CONSTANTS_H */
+6 −2
Original line number Diff line number Diff line
@@ -6,11 +6,15 @@

sigil_err_t sigil_init(sigil_t **sgl);

sigil_err_t sigil_verify(sigil_t *sgl, const char *filepath);
sigil_err_t sigil_set_pdf_file(sigil_t *sgl, FILE *pdf_file);
sigil_err_t sigil_set_pdf_path(sigil_t *sgl, const char *path_to_pdf);
sigil_err_t sigil_set_pdf_buffer(sigil_t *sgl, char *pdf_content, size_t size);

sigil_err_t sigil_verify(sigil_t *sgl);

// ... get functions TODO

void sigil_free(sigil_t *sgl);
void sigil_free(sigil_t **sgl);

int sigil_sigil_self_test(int verbosity);

+12 −5
Original line number Diff line number Diff line
@@ -33,12 +33,19 @@ typedef struct {

typedef struct {
    FILE    *file;
    char    *buffer;
    size_t   buf_pos;
    size_t   size;
    uint32_t deallocation_info;
} pdf_data_t;

typedef struct {
    pdf_data_t  pdf_data;
    short       pdf_x,             /* numbers from PDF header */
                pdf_y;             /*   %PDF-<pdf_x>.<pdf_y>  */
    short       xref_type;
    xref_t     *xref;
    reference_t ref_catalog_dict;
    size_t      file_size;
    size_t      pdf_start_offset;  /* offset of %PDF-x.y      */
    size_t      startxref;
} sigil_t;
Loading