Commit a069df3c authored by Tomáš Stefan's avatar Tomáš Stefan

Cleanup

removed error module
added module types + constants
other small improvements
parent 4c74de7d
......@@ -15,8 +15,8 @@ INCLUDE_DIR=include
TEST_DIR=test
BUILD_DIR=build
# library files
LIB_SRC=auxiliary.c config.c error.c header.c sigil.c trailer.c xref.c
LIB_H=$(LIB_SRC:%.c=%.h)
LIB_SRC=auxiliary.c config.c header.c sigil.c trailer.c xref.c
LIB_H=$(LIB_SRC:%.c=%.h) types.h constants.h
LIB_O=$(LIB_SRC:%.c=$(BUILD_DIR)/$(LIB_DIR)/%.o)
# test files
TEST_SRC=test.c
......
#ifndef PDF_SIGIL_AUXILIARY_H
#define PDF_SIGIL_AUXILIARY_H
#include <stdio.h>
#include <stdint.h>
#include <stdio.h> // size_t, FILE
#include "types.h"
#ifndef CHAR_T
#define CHAR_T
typedef unsigned char char_t;
#endif /* CHAR_T */
#ifndef SIGIL_ERR_T
#define SIGIL_ERR_T
typedef uint32_t sigil_err_t;
#endif /* SIGIL_ERR_T */
#ifndef KEYWORD_T
#define KEYWORD_T
typedef uint32_t keyword_t;
#endif /* KEYWORD_T */
#ifndef FREE_INDICATOR_T
#define FREE_INDICATOR_T
typedef uint32_t free_indicator_t;
#endif /* FREE_INDICATOR_T */
#ifndef REREFENCE_T
#define REREFENCE_T
typedef struct {
size_t object_num;
size_t generation_num;
} reference_t;
#endif /* REREFENCE_T */
#ifndef DICT_KEY_T
#define DICT_KEY_T
typedef uint32_t dict_key_t;
#endif /* DICT_KEY_T */
#define KEYWORD_xref 0
#define KEYWORD_trailer 1
#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 COLOR_RED "\x1b[31m"
#define COLOR_GREEN "\x1b[32m"
#define COLOR_RESET "\x1b[0m"
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
......@@ -71,6 +24,8 @@ 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);
const char *sigil_err_string(sigil_err_t err);
void print_module_name(const char *module_name, int verbosity);
void print_module_result(int result, int verbosity);
void print_test_item(const char *test_name, int verbosity);
......
#ifndef PDF_SIGIL_CONSTANTS_H
#define PDF_SIGIL_CONSTANTS_H
#define COLOR_RED "\x1b[31m"
#define COLOR_GREEN "\x1b[32m"
#define COLOR_RESET "\x1b[0m"
#define KEYWORD_xref 0
#define KEYWORD_trailer 1
#define XREF_TYPE_UNSET 0
#define XREF_TYPE_TABLE 1
#define XREF_TYPE_STREAM 2
#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 ERR_NO 0
#define ERR_ALLOC 1
#define ERR_PARAM 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
#endif /* PDF_SIGIL_CONSTANTS_H */
#ifndef PDF_SIGIL_ERROR_H
#define PDF_SIGIL_ERROR_H
#include <stdint.h>
#ifndef CHAR_T
#define CHAR_T
typedef unsigned char char_t;
#endif /* CHAR_T */
#ifndef SIGIL_ERR_T
#define SIGIL_ERR_T
typedef uint32_t sigil_err_t;
#endif /* SIGIL_ERR_T */
#define ERR_NO 0x0000 // [_|_|_|x] 0000 0000
#define ERR_ALLOC 0x0001 // [_|_|_|x] 0000 0001
#define ERR_PARAM 0x0002 // [_|_|_|x] 0000 0010
#define ERR_IO 0x0004 // [_|_|_|x] 0000 0100
#define ERR_PDF_CONT 0x0008 // [_|_|_|x] 0000 1000
#define ERR_NOT_IMPL 0x0010 // [_|_|_|x] 0001 0000
#define ERR_6 0x0020 // [_|_|_|x] 0010 0000
#define ERR_7 0x0040 // [_|_|_|x] 0100 0000
#define ERR_8 0x0080 // [_|_|_|x] 1000 0000
#define ERR_9 0x0100 // [_|_|x|_] 0000 0001
#define ERR_10 0x0200 // [_|_|x|_] 0000 0010
#define ERR_11 0x0400 // [_|_|x|_] 0000 0100
#define ERR_12 0x0800 // [_|_|x|_] 0000 1000
#define ERR_13 0x1000 // [_|_|x|_] 0001 0000
#define ERR_14 0x2000 // [_|_|x|_] 0010 0000
#define ERR_15 0x4000 // [_|_|x|_] 0100 0000
#define ERR_16 0x8000 // [_|_|x|_] 1000 0000
const char *sigil_err_string(sigil_err_t err);
int sigil_error_self_test(int verbosity);
#endif /* PDF_SIGIL_ERROR_H */
#ifndef PDF_SIGIL_HEADER_H
#define PDF_SIGIL_HEADER_H
#include "sigil.h"
#ifndef SIGIL_ERR_T
#define SIGIL_ERR_T
typedef uint32_t sigil_err_t;
#endif /* SIGIL_ERR_T */
#include "types.h"
sigil_err_t process_header(sigil_t *sgl);
......
#ifndef PDF_SIGIL_SIGIL_H
#define PDF_SIGIL_SIGIL_H
#include <stdio.h>
#include "types.h"
#ifndef CHAR_T
#define CHAR_T
typedef unsigned char char_t;
#endif /* CHAR_T */
#ifndef SIGIL_ERR_T
#define SIGIL_ERR_T
typedef uint32_t sigil_err_t;
#endif /* SIGIL_ERR_T */
#ifndef XREF_T
#define XREF_T
typedef struct {
size_t byte_offset;
size_t generation_num;
} xref_entry_t;
typedef struct {
xref_entry_t **entry;
size_t capacity;
size_t size_from_trailer;
size_t prev_section;
} xref_t;
#endif /* XREF_T */
#ifndef REREFENCE_T
#define REREFENCE_T
typedef struct {
size_t object_num;
size_t generation_num;
} reference_t;
#endif /* REREFENCE_T */
#define XREF_TYPE_UNSET 0
#define XREF_TYPE_TABLE 1
#define XREF_TYPE_STREAM 2
typedef struct {
FILE *file;
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;
sigil_err_t sigil_init(sigil_t **sgl);
......
#ifndef PDF_SIGIL_TRAILER_H
#define PDF_SIGIL_TRAILER_H
#ifndef SIGIL_ERR_T
#define SIGIL_ERR_T
typedef uint32_t sigil_err_t;
#endif /* SIGIL_ERR_T */
#include "types.h"
sigil_err_t process_trailer(sigil_t *sgl);
......
#ifndef PDF_SIGIL_TYPES_H
#define PDF_SIGIL_TYPES_H
#include <stdint.h> // uint32_t
typedef unsigned char char_t;
typedef uint32_t sigil_err_t;
typedef uint32_t keyword_t;
typedef uint32_t free_indicator_t;
typedef uint32_t dict_key_t;
typedef struct {
size_t object_num;
size_t generation_num;
} reference_t;
typedef struct {
size_t byte_offset;
size_t generation_num;
} xref_entry_t;
typedef struct {
xref_entry_t **entry;
size_t capacity;
size_t size_from_trailer;
size_t prev_section;
} xref_t;
typedef struct {
FILE *file;
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;
#endif /* PDF_SIGIL_TYPES_H */
#ifndef PDF_SIGIL_XREF_H
#define PDF_SIGIL_XREF_H
#include "sigil.h"
#ifndef SIGIL_ERR_T
#define SIGIL_ERR_T
typedef uint32_t sigil_err_t;
#endif /* SIGIL_ERR_T */
#ifndef XREF_T
#define XREF_T
typedef struct {
size_t byte_offset;
size_t generation_num;
} xref_entry_t;
typedef struct {
xref_entry_t **entry;
size_t capacity;
size_t size_from_trailer;
size_t prev_section;
} xref_t;
#endif /* XREF_T */
#include "types.h"
xref_t *xref_init();
......
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "auxiliary.h"
#include "error.h"
#include "constants.h"
void sigil_zeroize(void *a, size_t bytes)
{
......@@ -47,6 +47,7 @@ sigil_err_t skip_leading_whitespaces(FILE *in)
return ERR_NO;
}
// without leading "<<"
sigil_err_t skip_dictionary(FILE *in)
{
sigil_err_t err;
......@@ -67,7 +68,7 @@ sigil_err_t skip_dictionary(FILE *in)
break;
if ((err = skip_dictionary(in)) != ERR_NO)
return err;
return ERR_NO;
break;
default:
break;
}
......@@ -276,6 +277,26 @@ sigil_err_t parse_dict_key(FILE *in, dict_key_t *dict_key)
return ERR_PDF_CONT;
}
const char *sigil_err_string(sigil_err_t err)
{
switch (err) {
case ERR_NO:
return "NO ERROR";
case ERR_ALLOC:
return "ERROR during allocation";
case ERR_PARAM:
return "ERROR wrong parameter";
case ERR_IO:
return "ERROR input/output";
case ERR_PDF_CONT:
return "ERROR corrupted PDF file";
case ERR_NOT_IMPL:
return "ERROR not implemented";
default:
return "ERROR unknown";
}
}
void print_module_name(const char *module_name, int verbosity)
{
if (verbosity < 1)
......@@ -420,6 +441,60 @@ int sigil_auxiliary_self_test(int verbosity)
print_test_result(1, verbosity);
// TEST: fn skip_dictionary
print_test_item("fn skip_dictionary", verbosity);
{
char *sstream = "/First 2 0 R\n" \
"/Second 37 " \
"/Third true " \
"/Fourth [<86C><BA3>] " \
">>x";
FILE *file;
file = fmemopen(sstream,
(strlen(sstream) + 1) * sizeof(*sstream),
"r");
if (file == NULL)
goto failed;
if (skip_dictionary(file) != ERR_NO ||
fgetc(file) != 'x')
{
goto failed;
}
fclose(file);
}
print_test_result(1, verbosity);
// TEST: fn skip_dict_unknown_value
print_test_item("fn skip_dict_unknown_value", verbosity);
{
char *sstream = "<</First /NameVal\n" \
"/Second <</Nested -32 " \
">> >>x";
FILE *file;
file = fmemopen(sstream,
(strlen(sstream) + 1) * sizeof(*sstream),
"r");
if (file == NULL)
goto failed;
if (skip_dict_unknown_value(file) != ERR_NO ||
fgetc(file) != 'x')
{
goto failed;
}
fclose(file);
}
print_test_result(1, verbosity);
// TEST: fn parse_number
print_test_item("fn parse_number", verbosity);
......
#include <stdio.h>
#include "auxiliary.h"
#include "config.h"
......
#include <stdio.h>
#include <string.h>
#include "auxiliary.h"
#include "error.h"
const char *sigil_err_string(sigil_err_t err)
{
if (err == ERR_NO)
return "NO ERROR";
if (err & ERR_ALLOC)
return "ERROR during allocation";
if (err & ERR_PARAM)
return "ERROR wrong parameter";
if (err & ERR_IO)
return "ERROR input/output";
if (err & ERR_PDF_CONT)
return "ERROR corrupted PDF file";
if (err & ERR_NOT_IMPL)
return "ERROR not implemented";
return "ERROR unknown";
}
int sigil_error_self_test(int verbosity)
{
print_module_name("error", verbosity);
// TEST: error codes
print_test_item("error codes", verbosity);
if ((ERR_NO + ERR_ALLOC + ERR_PARAM +
ERR_IO + ERR_PDF_CONT + ERR_NOT_IMPL +
ERR_6 + ERR_7 + ERR_8 +
ERR_9 + ERR_10 + ERR_11 +
ERR_12 + ERR_13 + ERR_14 +
ERR_15 + ERR_16
) != 0xffff || ERR_NO != 0)
{
goto failed;
}
print_test_result(1, verbosity);
// TEST: fn sigil_err_string
print_test_item("fn sigil_err_string", verbosity);
if (strcmp(sigil_err_string(ERR_NO ), "NO ERROR" ) != 0 ||
strcmp(sigil_err_string(ERR_ALLOC), "ERROR during allocation") != 0 ||
strcmp(sigil_err_string(0x800000 ), "ERROR unknown" ) != 0 )
{
goto failed;
}
print_test_result(1, verbosity);
// all tests done
print_module_result(1, verbosity);
return 0;
failed:
print_test_result(0, verbosity);
print_module_result(0, verbosity);
return 1;
}
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "auxiliary.h"
#include "config.h"
#include "error.h"
#include "constants.h"
#include "header.h"
#include "sigil.h"
sigil_err_t process_header(sigil_t *sgl)
......
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "auxiliary.h"
#include "error.h"
#include "constants.h"
#include "header.h"
#include "sigil.h"
#include "trailer.h"
#include "xref.h"
sigil_err_t sigil_init(sigil_t **sgl)
{
// function parameter checks
......@@ -20,6 +18,8 @@ sigil_err_t sigil_init(sigil_t **sgl)
if (*sgl == NULL)
return ERR_ALLOC;
sigil_zeroize(*sgl, sizeof(*sgl));
// set default values
(*sgl)->file = NULL;
(*sgl)->pdf_x = 0;
......
#include <string.h>
#include "auxiliary.h"
#include "error.h"
#include "sigil.h"
#include "xref.h"
#include "constants.h"
#include "trailer.h"
sigil_err_t process_trailer(sigil_t *sgl)
{
......
#if 0
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
......@@ -6,7 +7,15 @@
#include "error.h"
#include "sigil.h"
#include "xref.h"
#endif
#include <stdlib.h>
#include <string.h>
#include "auxiliary.h"
#include "config.h"
#include "constants.h"
#include "sigil.h"
#include "xref.h"
// Determine whether this file is using Cross-reference table or stream
static sigil_err_t determine_xref_type(sigil_t *sgl)
......@@ -56,6 +65,7 @@ add_xref_entry(xref_t *xref, size_t obj, size_t offset, size_t generation)
xref->entry[obj] = malloc(sizeof(xref_entry_t));
if (xref->entry[obj] == NULL)
return ERR_ALLOC;
sigil_zeroize(xref->entry[obj], sizeof(xref->entry[obj]));
xref->entry[obj]->byte_offset = offset;
xref->entry[obj]->generation_num = generation;
......@@ -75,13 +85,14 @@ xref_t *xref_init()
xref_t *xref = malloc(sizeof(xref_t));
if (xref == NULL)
return NULL;
sigil_zeroize(xref, sizeof(xref));
xref->entry = malloc(sizeof(xref_entry_t *) * XREF_PREALLOCATION);
if (xref->entry == NULL) {
free(xref);
return NULL;
}
sigil_zeroize(xref->entry, sizeof(xref_entry_t *) * xref->capacity);
sigil_zeroize(xref->entry, sizeof(*(xref->entry)) * xref->capacity);
xref->capacity = XREF_PREALLOCATION;
xref->size_from_trailer = 0;
xref->prev_section = 0;
......@@ -133,9 +144,9 @@ sigil_err_t read_startxref(sigil_t *sgl)
// prepare buffer for data
size_t buf_len = sgl->file_size - jump_pos + 1;
char_t *buf = malloc(buf_len * sizeof(char_t));
if (buf == NULL) {
if (buf == NULL)
return ERR_ALLOC;
}
sigil_zeroize(buf, buf_len * sizeof(*buf));
// copy data from the end of file
size_t read = fread(buf, sizeof(*buf), buf_len - 1, sgl->file);
......@@ -339,27 +350,34 @@ int sigil_xref_self_test(int verbosity)
// TEST: fn read_startxref
print_test_item("fn read_startxref", verbosity);
char *correct_1 = "startxref\n" \
"1234567890\n" \
"\045\045EOF"; // %%EOF
sgl->file = fmemopen(correct_1,
(strlen(correct_1) + 1) * sizeof(*correct_1),
"r");
if (sgl->file == NULL) {
goto failed;
}
if (read_startxref(sgl) != ERR_NO ||
sgl->file_size != 27 ||
sgl->startxref != 1234567890 )
{
goto failed;
sgl = NULL;
char *sstream_1 = "startxref\n" \
"1234567890\n"\
"\045\045EOF"; // %%EOF
err = sigil_init(&sgl);
if (err != ERR_NO || sgl == NULL)
goto failed;
sgl->file = fmemopen(sstream_1,
(strlen(sstream_1) + 1) * sizeof(*sstream_1),
"r");
if (sgl->file == NULL)
goto failed;
if (read_startxref(sgl) != ERR_NO ||
sgl->file_size != 27 ||
sgl->startxref != 1234567890)
{
goto failed;
}
sigil_free(sgl);
}
print_test_result(1, verbosity);
fclose(sgl->file);
// all tests done
print_module_result(1, verbosity);
return 0;
......
......@@ -2,7 +2,6 @@
#include <string.h>
#include "auxiliary.h"
#include "config.h"
#include "error.h"
#include "header.h"
#include "sigil.h"
#include "trailer.h"
......@@ -50,8 +49,6 @@ int main(int argc, char **argv)
failed++;
if (sigil_auxiliary_self_test(verbosity) != 0)
failed++;
if (sigil_error_self_test(verbosity) != 0)
failed++;
if (sigil_header_self_test(verbosity) != 0)
failed++;
if (sigil_trailer_self_test(verbosity) != 0)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment