Skip to content

Commit

Permalink
openjpeg.c: avoid casts of function pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault committed Jun 7, 2021
1 parent adb8889 commit 7e4e09a
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions src/lib/openjp2/openjpeg.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,16 @@ OPJ_BOOL OPJ_CALLCONV opj_set_error_handler(opj_codec_t * p_codec,
/* ---------------------------------------------------------------------- */

static OPJ_SIZE_T opj_read_from_file(void * p_buffer, OPJ_SIZE_T p_nb_bytes,
FILE * p_file)
void * p_user_data)
{
OPJ_SIZE_T l_nb_read = fread(p_buffer, 1, p_nb_bytes, p_file);
FILE* p_file = (FILE*)p_user_data;
OPJ_SIZE_T l_nb_read = fread(p_buffer, 1, p_nb_bytes, (FILE*)p_file);
return l_nb_read ? l_nb_read : (OPJ_SIZE_T) - 1;
}

static OPJ_UINT64 opj_get_data_length_from_file(FILE * p_file)
static OPJ_UINT64 opj_get_data_length_from_file(void * p_user_data)
{
FILE* p_file = (FILE*)p_user_data;
OPJ_OFF_T file_length = 0;

OPJ_FSEEK(p_file, 0, SEEK_END);
Expand All @@ -107,29 +109,38 @@ static OPJ_UINT64 opj_get_data_length_from_file(FILE * p_file)
}

static OPJ_SIZE_T opj_write_from_file(void * p_buffer, OPJ_SIZE_T p_nb_bytes,
FILE * p_file)
void * p_user_data)
{
FILE* p_file = (FILE*)p_user_data;
return fwrite(p_buffer, 1, p_nb_bytes, p_file);
}

static OPJ_OFF_T opj_skip_from_file(OPJ_OFF_T p_nb_bytes, FILE * p_user_data)
static OPJ_OFF_T opj_skip_from_file(OPJ_OFF_T p_nb_bytes, void * p_user_data)
{
if (OPJ_FSEEK(p_user_data, p_nb_bytes, SEEK_CUR)) {
FILE* p_file = (FILE*)p_user_data;
if (OPJ_FSEEK(p_file, p_nb_bytes, SEEK_CUR)) {
return -1;
}

return p_nb_bytes;
}

static OPJ_BOOL opj_seek_from_file(OPJ_OFF_T p_nb_bytes, FILE * p_user_data)
static OPJ_BOOL opj_seek_from_file(OPJ_OFF_T p_nb_bytes, void * p_user_data)
{
if (OPJ_FSEEK(p_user_data, p_nb_bytes, SEEK_SET)) {
FILE* p_file = (FILE*)p_user_data;
if (OPJ_FSEEK(p_file, p_nb_bytes, SEEK_SET)) {
return OPJ_FALSE;
}

return OPJ_TRUE;
}

static void opj_close_from_file(void* p_user_data)
{
FILE* p_file = (FILE*)p_user_data;
fclose(p_file);
}

/* ---------------------------------------------------------------------- */
#ifdef _WIN32
#ifndef OPJ_STATIC
Expand Down Expand Up @@ -1074,15 +1085,14 @@ opj_stream_t* OPJ_CALLCONV opj_stream_create_file_stream(
return NULL;
}

opj_stream_set_user_data(l_stream, p_file,
(opj_stream_free_user_data_fn) fclose);
opj_stream_set_user_data(l_stream, p_file, opj_close_from_file);
opj_stream_set_user_data_length(l_stream,
opj_get_data_length_from_file(p_file));
opj_stream_set_read_function(l_stream, (opj_stream_read_fn) opj_read_from_file);
opj_stream_set_read_function(l_stream, opj_read_from_file);
opj_stream_set_write_function(l_stream,
(opj_stream_write_fn) opj_write_from_file);
opj_stream_set_skip_function(l_stream, (opj_stream_skip_fn) opj_skip_from_file);
opj_stream_set_seek_function(l_stream, (opj_stream_seek_fn) opj_seek_from_file);
opj_stream_set_skip_function(l_stream, opj_skip_from_file);
opj_stream_set_seek_function(l_stream, opj_seek_from_file);

return l_stream;
}
Expand Down

0 comments on commit 7e4e09a

Please sign in to comment.