Commit 4c74de7d authored by Tomáš Stefan's avatar Tomáš Stefan

Removed unnecessary cast to sigil_err_t

parent 0b8c58ce
......@@ -39,12 +39,12 @@ sigil_err_t skip_leading_whitespaces(FILE *in)
;
if (c == EOF)
return (sigil_err_t)ERR_PDF_CONT;
return ERR_PDF_CONT;
if (ungetc(c, in) != c)
return (sigil_err_t)ERR_IO;
return ERR_IO;
return (sigil_err_t)ERR_NO;
return ERR_NO;
}
sigil_err_t skip_dictionary(FILE *in)
......@@ -60,20 +60,20 @@ sigil_err_t skip_dictionary(FILE *in)
switch (c) {
case '>':
if (fgetc(in) == '>')
return (sigil_err_t)ERR_NO;
return ERR_NO;
break;
case '<':
if (fgetc(in) != '<')
break;
if ((err = skip_dictionary(in)) != ERR_NO)
return err;
return (sigil_err_t)ERR_NO;
return ERR_NO;
default:
break;
}
}
return (sigil_err_t)ERR_PDF_CONT;
return ERR_PDF_CONT;
}
sigil_err_t skip_dict_unknown_value(FILE *in)
......@@ -85,20 +85,20 @@ sigil_err_t skip_dict_unknown_value(FILE *in)
switch (c) {
case '/':
if (ungetc(c, in) != c)
return (sigil_err_t)ERR_IO;
return (sigil_err_t)ERR_NO;
return ERR_IO;
return ERR_NO;
case '<':
if (fgetc(in) != '<')
break;
if ((err = skip_dictionary(in)) != ERR_NO)
return err;
return (sigil_err_t)ERR_NO;
return ERR_NO;
default:
break;
}
}
return (sigil_err_t)ERR_PDF_CONT;
return ERR_PDF_CONT;
}
sigil_err_t parse_number(FILE *in, size_t *number)
......@@ -117,18 +117,18 @@ sigil_err_t parse_number(FILE *in, size_t *number)
while ((c = fgetc(in)) != EOF) {
if (!is_digit(c)) {
if (ungetc(c, in) != c)
return (sigil_err_t)ERR_IO;
return ERR_IO;
if (digits > 0) {
return (sigil_err_t)ERR_NO;
return ERR_NO;
} else {
return (sigil_err_t)ERR_PDF_CONT;
return ERR_PDF_CONT;
}
}
*number = 10 * *number + c - '0';
digits++;
}
return (sigil_err_t)ERR_PDF_CONT;
return ERR_PDF_CONT;
}
sigil_err_t parse_keyword(FILE *in, keyword_t *keyword)
......@@ -148,28 +148,28 @@ sigil_err_t parse_keyword(FILE *in, keyword_t *keyword)
while ((c = fgetc(in)) != EOF) {
if (is_whitespace(c)) {
if (count <= 0)
return (sigil_err_t)ERR_PDF_CONT;
return ERR_PDF_CONT;
if (ungetc(c, in) != c)
return (sigil_err_t)ERR_IO;
return ERR_IO;
if (strncmp(tmp, "xref", 4) == 0) {
*keyword = KEYWORD_xref;
return (sigil_err_t)ERR_NO;
return ERR_NO;
}
if (strncmp(tmp, "trailer", 7) == 0) {
*keyword = KEYWORD_trailer;
return (sigil_err_t)ERR_NO;
return ERR_NO;
}
return (sigil_err_t)ERR_PDF_CONT;
return ERR_PDF_CONT;
} else {
if (count >= keyword_max - 1)
return (sigil_err_t)ERR_PDF_CONT;
return ERR_PDF_CONT;
tmp[count] = c;
count++;
}
}
return (sigil_err_t)ERR_PDF_CONT;
return ERR_PDF_CONT;
}
sigil_err_t parse_free_indicator(FILE *in, free_indicator_t *result)
......@@ -186,12 +186,12 @@ sigil_err_t parse_free_indicator(FILE *in, free_indicator_t *result)
switch(c) {
case 'f':
*result = FREE_ENTRY;
return (sigil_err_t)ERR_NO;
return ERR_NO;
case 'n':
*result = IN_USE_ENTRY;
return (sigil_err_t)ERR_NO;
return ERR_NO;
default:
return (sigil_err_t)ERR_PDF_CONT;
return ERR_PDF_CONT;
}
}
......@@ -209,8 +209,8 @@ sigil_err_t parse_indirect_reference(FILE *in, reference_t *ref)
if (err != ERR_NO)
return err;
if (fgetc(in) != 'R')
return (sigil_err_t)ERR_PDF_CONT;
return (sigil_err_t)ERR_NO;
return ERR_PDF_CONT;
return ERR_NO;
}
// parse the key of the couple key - value in the dictionary
......@@ -236,44 +236,44 @@ sigil_err_t parse_dict_key(FILE *in, dict_key_t *dict_key)
break;
case '>':
if ((c = fgetc(in)) == '>')
return (sigil_err_t)ERR_PDF_CONT;
return ERR_PDF_CONT;
if (ungetc(c, in) != c)
return (sigil_err_t)ERR_IO;
return (sigil_err_t)ERR_PDF_CONT;
return ERR_IO;
return ERR_PDF_CONT;
default:
return (sigil_err_t)ERR_PDF_CONT;
return ERR_PDF_CONT;
}
while ((c = fgetc(in)) != EOF) {
if (is_whitespace(c)) {
if (count <= 0)
return (sigil_err_t)ERR_PDF_CONT;
return ERR_PDF_CONT;
if (ungetc(c, in) != c)
return (sigil_err_t)ERR_IO;
return ERR_IO;
if (strncmp(tmp, "Size", 4) == 0) {
*dict_key = DICT_KEY_Size;
return (sigil_err_t)ERR_NO;
return ERR_NO;
}
if (strncmp(tmp, "Prev", 4) == 0) {
*dict_key = DICT_KEY_Prev;
return (sigil_err_t)ERR_NO;
return ERR_NO;
}
if (strncmp(tmp, "Root", 4) == 0) {
*dict_key = DICT_KEY_Root;
return (sigil_err_t)ERR_NO;
return ERR_NO;
}
*dict_key = DICT_KEY_unknown;
return (sigil_err_t)ERR_NO;
return ERR_NO;
} else {
if (count >= dict_key_max - 1)
return (sigil_err_t)ERR_ALLOC;
return ERR_ALLOC;
tmp[count] = c;
count++;
}
}
return (sigil_err_t)ERR_PDF_CONT;
return ERR_PDF_CONT;
}
void print_module_name(const char *module_name, int verbosity)
......
......@@ -10,11 +10,11 @@ sigil_err_t process_header(sigil_t *sgl)
{
// function parameter checks
if (sgl == NULL || sgl->file == NULL)
return (sigil_err_t)ERR_PARAM;
return ERR_PARAM;
if (ftell(sgl->file) != 0) {
if (fseek(sgl->file, 0, SEEK_SET) != 0 || ftell(sgl->file) != 0)
return (sigil_err_t)ERR_IO;
return ERR_IO;
}
const char_t expected[] = {'%', 'P', 'D', 'F', '-'};
......@@ -67,14 +67,14 @@ sigil_err_t process_header(sigil_t *sgl)
}
if (found != 8)
return (sigil_err_t)ERR_PDF_CONT;
return ERR_PDF_CONT;
// offset counted with header -> subtract header size
sgl->pdf_start_offset = offset - found;
if (sgl->pdf_start_offset > HEADER_SEARCH_OFFSET)
return (sigil_err_t)ERR_PDF_CONT;
return ERR_PDF_CONT;
return (sigil_err_t)ERR_NO;
return ERR_NO;
}
int sigil_header_self_test(int verbosity)
......
......@@ -13,12 +13,12 @@ sigil_err_t sigil_init(sigil_t **sgl)
{
// function parameter checks
if (sgl == NULL)
return (sigil_err_t)ERR_PARAM;
return ERR_PARAM;
*sgl = malloc(sizeof(sigil_t));
if (*sgl == NULL)
return (sigil_err_t)ERR_ALLOC;
return ERR_ALLOC;
// set default values
(*sgl)->file = NULL;
......@@ -32,7 +32,7 @@ sigil_err_t sigil_init(sigil_t **sgl)
(*sgl)->pdf_start_offset = 0;
(*sgl)->startxref = 0;
return (sigil_err_t)ERR_NO;
return ERR_NO;
}
sigil_err_t sigil_verify(sigil_t *sgl, const char *filepath)
......@@ -41,11 +41,11 @@ sigil_err_t sigil_verify(sigil_t *sgl, const char *filepath)
// function parameter checks
if (sgl == NULL || filepath == NULL)
return (sigil_err_t)ERR_PARAM;
return ERR_PARAM;
// open provided file
if ((sgl->file = fopen(filepath, "r")) == NULL)
return (sigil_err_t)ERR_IO;
return ERR_IO;
// process header - %PDF-<pdf_x>.<pdf_y>
err = process_header(sgl);
......@@ -63,7 +63,7 @@ sigil_err_t sigil_verify(sigil_t *sgl, const char *filepath)
// TODO
return (sigil_err_t)ERR_NO;
return ERR_NO;
}
void sigil_free(sigil_t *sgl)
......
......@@ -13,14 +13,14 @@ sigil_err_t process_trailer(sigil_t *sgl)
// function parameter checks
if (sgl == NULL || sgl->file == NULL)
return (sigil_err_t)ERR_PARAM;
return ERR_PARAM;
// read "trailer"
err = parse_keyword(sgl->file, &keyword);
if (err != ERR_NO)
return err;
if (keyword != KEYWORD_trailer)
return (sigil_err_t)ERR_PDF_CONT;
return ERR_PDF_CONT;
err = skip_leading_whitespaces(sgl->file);
if (err != ERR_NO)
......@@ -55,7 +55,7 @@ sigil_err_t process_trailer(sigil_t *sgl)
return err;
break;
default:
return (sigil_err_t)ERR_PDF_CONT;
return ERR_PDF_CONT;
}
}
......
......@@ -12,7 +12,7 @@
static sigil_err_t determine_xref_type(sigil_t *sgl)
{
if (fseek(sgl->file, sgl->startxref, SEEK_SET) != 0)
return (sigil_err_t)ERR_IO;
return ERR_IO;
char_t c = fgetc(sgl->file);
if (c == 'x') {
......@@ -20,10 +20,10 @@ static sigil_err_t determine_xref_type(sigil_t *sgl)
} else if (is_digit(c)) {
sgl->xref_type = XREF_TYPE_STREAM;
} else {
return (sigil_err_t)ERR_PDF_CONT;
return ERR_PDF_CONT;
}
return (sigil_err_t)ERR_NO;
return ERR_NO;
}
static sigil_err_t
......@@ -32,7 +32,7 @@ add_xref_entry(xref_t *xref, size_t obj, size_t offset, size_t generation)
int resize_factor = 1;
if (xref == NULL)
return (sigil_err_t)ERR_PARAM;
return ERR_PARAM;
// resize if needed
while (obj > resize_factor * xref->capacity - 1)
......@@ -40,7 +40,7 @@ add_xref_entry(xref_t *xref, size_t obj, size_t offset, size_t generation)
if (resize_factor != 1) {
xref->entry = realloc(xref->entry,sizeof(xref_entry_t *) * xref->capacity * resize_factor);
if (xref->entry == NULL)
return (sigil_err_t)ERR_ALLOC;
return ERR_ALLOC;
sigil_zeroize(xref->entry + xref->capacity, sizeof(xref_entry_t *) * (xref->capacity * (resize_factor - 1)));
xref->capacity *= resize_factor;
}
......@@ -50,18 +50,18 @@ add_xref_entry(xref_t *xref, size_t obj, size_t offset, size_t generation)
xref->entry[obj]->byte_offset = offset;
xref->entry[obj]->generation_num = generation;
} else {
return (sigil_err_t)ERR_NO;
return ERR_NO;
}
} else {
xref->entry[obj] = malloc(sizeof(xref_entry_t));
if (xref->entry[obj] == NULL)
return (sigil_err_t)ERR_ALLOC;
return ERR_ALLOC;
xref->entry[obj]->byte_offset = offset;
xref->entry[obj]->generation_num = generation;
}
return (sigil_err_t)ERR_ALLOC;
return ERR_ALLOC;
}
static void free_xref_entry(xref_entry_t *entry)
......@@ -108,40 +108,40 @@ sigil_err_t read_startxref(sigil_t *sgl)
{
// function parameter checks
if (sgl == NULL || sgl->file == NULL) {
return (sigil_err_t)ERR_PARAM;
return ERR_PARAM;
}
// jump to the end of file
if (fseek(sgl->file, 0, SEEK_END) != 0) {
return (sigil_err_t)ERR_IO;
return ERR_IO;
}
// get file size
if (sgl->file_size <= 0) {
sgl->file_size = ftell(sgl->file);
if (sgl->file_size < 0) {
return (sigil_err_t)ERR_IO;
return ERR_IO;
}
}
// jump max XREF_SEARCH_OFFSET bytes from end
size_t jump_pos = MAX(0, (ssize_t)sgl->file_size - XREF_SEARCH_OFFSET);
if (fseek(sgl->file, jump_pos, SEEK_SET) != 0) {
return (sigil_err_t)ERR_IO;
return ERR_IO;
}
// 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) {
return (sigil_err_t)ERR_ALLOC;
return ERR_ALLOC;
}
// copy data from the end of file
size_t read = fread(buf, sizeof(*buf), buf_len - 1, sgl->file);
if (read <= 0) {
free(buf);
return (sigil_err_t)ERR_IO;
return ERR_IO;
}
buf[read] = '\0';
......@@ -154,7 +154,7 @@ sigil_err_t read_startxref(sigil_t *sgl)
}
if (!is_digit(buf[i])) {
free(buf);
return (sigil_err_t)ERR_PDF_CONT;
return ERR_PDF_CONT;
}
sgl->startxref = 0;
while (i < read && is_digit(buf[i])) {
......@@ -168,9 +168,9 @@ sigil_err_t read_startxref(sigil_t *sgl)
free(buf);
if (sgl->startxref == 0)
return (sigil_err_t)ERR_PDF_CONT;
return ERR_PDF_CONT;
return (sigil_err_t)ERR_NO;
return ERR_NO;
}
sigil_err_t read_xref_table(sigil_t *sgl)
......@@ -187,17 +187,17 @@ sigil_err_t read_xref_table(sigil_t *sgl)
if (sgl->xref == NULL)
sgl->xref = xref_init();
if (sgl->xref == NULL)
return (sigil_err_t)ERR_ALLOC;
return ERR_ALLOC;
if (fseek(sgl->file, sgl->startxref, SEEK_SET) != 0)
return (sigil_err_t)ERR_IO;
return ERR_IO;
// read "xref"
if (fgets(tmp, 5, sgl->file) == NULL)
return (sigil_err_t)ERR_IO;
return ERR_IO;
if (strncmp(tmp, "xref", 4) != 0)
return (sigil_err_t)ERR_PDF_CONT;
return ERR_PDF_CONT;
while (!xref_end) { // for all xref sections
while (1) {
......@@ -232,7 +232,7 @@ sigil_err_t read_xref_table(sigil_t *sgl)
}
}
}
return (sigil_err_t)ERR_NO;
return ERR_NO;
}
sigil_err_t process_xref(sigil_t *sgl)
......@@ -241,7 +241,7 @@ sigil_err_t process_xref(sigil_t *sgl)
// function parameter checks
if (sgl == NULL || sgl->file == NULL || sgl->startxref == 0) {
return (sigil_err_t)ERR_PARAM;
return ERR_PARAM;
}
err = read_startxref(sgl);
......@@ -257,12 +257,12 @@ sigil_err_t process_xref(sigil_t *sgl)
read_xref_table(sgl);
break;
case XREF_TYPE_STREAM:
return (sigil_err_t)ERR_NOT_IMPL; // TODO
return ERR_NOT_IMPL; // TODO
default:
return (sigil_err_t)ERR_PDF_CONT;
return ERR_PDF_CONT;
}
return (sigil_err_t)ERR_NO;
return ERR_NO;
}
void print_xref(xref_t *xref)
......
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