Discussion:
[libdvdcss-devel] [PATCH 1/4] Mark struct iovec used in *_readv as const.
Diego Biurrun
2014-11-13 13:08:11 UTC
Permalink
---
src/device.c | 10 ++++++----
src/libdvdcss.h | 2 +-
2 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/src/device.c b/src/device.c
index fd53ea6..5030984 100644
--- a/src/device.c
+++ b/src/device.c
@@ -80,13 +80,13 @@
static int libc_open ( dvdcss_t, const char * );
static int libc_seek ( dvdcss_t, int );
static int libc_read ( dvdcss_t, void *, int );
-static int libc_readv ( dvdcss_t, struct iovec *, int );
+static int libc_readv ( dvdcss_t, const struct iovec *, int );

#ifdef WIN32
static int win2k_open ( dvdcss_t, const char * );
static int win2k_seek ( dvdcss_t, int );
static int win2k_read ( dvdcss_t, void *, int );
-static int win2k_readv ( dvdcss_t, struct iovec *, int );
+static int win2k_readv ( dvdcss_t, const struct iovec *, int );

#elif defined( __OS2__ )
static int os2_open ( dvdcss_t, const char * );
@@ -652,7 +652,8 @@ static int win2k_read ( dvdcss_t dvdcss, void *p_buffer, int i_blocks )
/*****************************************************************************
* Readv commands.
*****************************************************************************/
-static int libc_readv ( dvdcss_t dvdcss, struct iovec *p_iovec, int i_blocks )
+static int libc_readv ( dvdcss_t dvdcss, const struct iovec *p_iovec,
+ int i_blocks )
{
#if defined( WIN32 )
int i_index, i_len, i_total = 0;
@@ -725,7 +726,8 @@ static int libc_readv ( dvdcss_t dvdcss, struct iovec *p_iovec, int i_blocks )
/*****************************************************************************
* win2k_readv: vectored read using ReadFile for Win2K
*****************************************************************************/
-static int win2k_readv ( dvdcss_t dvdcss, struct iovec *p_iovec, int i_blocks )
+static int win2k_readv ( dvdcss_t dvdcss, const struct iovec *p_iovec,
+ int i_blocks )
{
int i_index;
int i_blocks_read, i_blocks_total = 0;
diff --git a/src/libdvdcss.h b/src/libdvdcss.h
index 9750c37..2d5400e 100644
--- a/src/libdvdcss.h
+++ b/src/libdvdcss.h
@@ -56,7 +56,7 @@ struct dvdcss_s
/* File handling */
int ( * pf_seek ) ( dvdcss_t, int );
int ( * pf_read ) ( dvdcss_t, void *, int );
- int ( * pf_readv ) ( dvdcss_t, struct iovec *, int );
+ int ( * pf_readv ) ( dvdcss_t, const struct iovec *, int );

/* Decryption stuff */
enum dvdcss_method i_method;
--
2.1.0
Diego Biurrun
2014-11-13 13:08:12 UTC
Permalink
There is no need to employ separate file descriptors.
---
src/device.c | 24 ++++++++----------------
src/libdvdcss.c | 3 ---
src/libdvdcss.h | 5 -----
3 files changed, 8 insertions(+), 24 deletions(-)

diff --git a/src/device.c b/src/device.c
index 5030984..ddb216e 100644
--- a/src/device.c
+++ b/src/device.c
@@ -392,9 +392,9 @@ int dvdcss_open_device ( dvdcss_t dvdcss )
#ifdef DVDCSS_RAW_OPEN
int dvdcss_raw_open ( dvdcss_t dvdcss, const char *psz_device )
{
- dvdcss->i_raw_fd = open( psz_device, 0 );
+ int i_fd = open( psz_device, 0 );

- if( dvdcss->i_raw_fd == -1 )
+ if( i_fd == -1 )
{
print_debug( dvdcss, "cannot open %s (%s)",
psz_device, strerror(errno) );
@@ -403,7 +403,7 @@ int dvdcss_raw_open ( dvdcss_t dvdcss, const char *psz_device )
}
else
{
- dvdcss->i_read_fd = dvdcss->i_raw_fd;
+ dvdcss->i_fd = i_fd;
}

return 0;
@@ -431,14 +431,6 @@ int dvdcss_close_device ( dvdcss_t dvdcss )
#else
close( dvdcss->i_fd );

-#ifdef DVDCSS_RAW_OPEN
- if( dvdcss->i_raw_fd >= 0 )
- {
- close( dvdcss->i_raw_fd );
- dvdcss->i_raw_fd = -1;
- }
-#endif
-
return 0;
#endif
}
@@ -450,7 +442,7 @@ int dvdcss_close_device ( dvdcss_t dvdcss )
*****************************************************************************/
static int libc_open ( dvdcss_t dvdcss, const char *psz_device )
{
- dvdcss->i_fd = dvdcss->i_read_fd = open( psz_device, O_BINARY );
+ dvdcss->i_fd = open( psz_device, O_BINARY );

if( dvdcss->i_fd == -1 )
{
@@ -526,7 +518,7 @@ static int os2_open ( dvdcss_t dvdcss, const char *psz_device )

setmode( hfile, O_BINARY );

- dvdcss->i_fd = dvdcss->i_read_fd = hfile;
+ dvdcss->i_fd = hfile;

dvdcss->i_pos = 0;

@@ -548,7 +540,7 @@ static int libc_seek( dvdcss_t dvdcss, int i_blocks )
}

i_seek = (off_t)i_blocks * (off_t)DVDCSS_BLOCK_SIZE;
- i_seek = lseek( dvdcss->i_read_fd, i_seek, SEEK_SET );
+ i_seek = lseek( dvdcss->i_fd, i_seek, SEEK_SET );

if( i_seek < 0 )
{
@@ -599,7 +591,7 @@ static int libc_read ( dvdcss_t dvdcss, void *p_buffer, int i_blocks )
off_t i_size, i_ret, i_ret_blocks;

i_size = (off_t)i_blocks * (off_t)DVDCSS_BLOCK_SIZE;
- i_ret = read( dvdcss->i_read_fd, p_buffer, i_size );
+ i_ret = read( dvdcss->i_fd, p_buffer, i_size );

if( i_ret < 0 )
{
@@ -708,7 +700,7 @@ static int libc_readv ( dvdcss_t dvdcss, const struct iovec *p_iovec,
dvdcss->i_pos += i_total;
return i_total;
#else
- int i_read = readv( dvdcss->i_read_fd, p_iovec, i_blocks );
+ int i_read = readv( dvdcss->i_fd, p_iovec, i_blocks );

if( i_read < 0 )
{
diff --git a/src/libdvdcss.c b/src/libdvdcss.c
index 034333a..44d740b 100644
--- a/src/libdvdcss.c
+++ b/src/libdvdcss.c
@@ -473,9 +473,6 @@ LIBDVDCSS_EXPORT dvdcss_t dvdcss_open ( const char *psz_target )
}

/* Initialize structure with default values. */
-#ifdef DVDCSS_RAW_OPEN
- dvdcss->i_raw_fd = -1;
-#endif
dvdcss->p_titles = NULL;
dvdcss->psz_device = strdup( psz_target );
dvdcss->psz_error = "no error";
diff --git a/src/libdvdcss.h b/src/libdvdcss.h
index 2d5400e..c08fadc 100644
--- a/src/libdvdcss.h
+++ b/src/libdvdcss.h
@@ -50,7 +50,6 @@ struct dvdcss_s
/* File descriptor */
char * psz_device;
int i_fd;
- int i_read_fd;
int i_pos;

/* File handling */
@@ -79,10 +78,6 @@ struct dvdcss_s
char * p_readv_buffer;
int i_readv_buf_size;
#endif /* WIN32 */
-
-#ifdef DVDCSS_RAW_OPEN
- int i_raw_fd;
-#endif
};

/*****************************************************************************
--
2.1.0
Diego Biurrun
2014-11-13 13:08:13 UTC
Permalink
Also fix a memory leak on close() failure.
---
src/device.c | 12 ++++++++----
src/libdvdcss.c | 7 +++----
2 files changed, 11 insertions(+), 8 deletions(-)

diff --git a/src/device.c b/src/device.c
index ddb216e..e824b54 100644
--- a/src/device.c
+++ b/src/device.c
@@ -412,6 +412,7 @@ int dvdcss_raw_open ( dvdcss_t dvdcss, const char *psz_device )

int dvdcss_close_device ( dvdcss_t dvdcss )
{
+ int i_ret;
#if defined( WIN32 )
if( dvdcss->b_file )
{
@@ -426,13 +427,16 @@ int dvdcss_close_device ( dvdcss_t dvdcss )
free( dvdcss->p_readv_buffer );
dvdcss->p_readv_buffer = NULL;
dvdcss->i_readv_buf_size = 0;
-
- return 0;
#else
- close( dvdcss->i_fd );
+ i_ret = close( dvdcss->i_fd );
+ if( i_ret < 0 )
+ {
+ print_error( dvdcss, "Failed to close fd, data loss possible." );
+ return i_ret;
+ }
+#endif

return 0;
-#endif
}

/* Following functions are local */
diff --git a/src/libdvdcss.c b/src/libdvdcss.c
index 44d740b..91f83cd 100644
--- a/src/libdvdcss.c
+++ b/src/libdvdcss.c
@@ -779,16 +779,15 @@ LIBDVDCSS_EXPORT int dvdcss_close ( dvdcss_t dvdcss )
p_title = p_tmptitle;
}

- i_ret = dvdcss_close_device( dvdcss );
+ free( dvdcss->psz_device );
+ free( dvdcss );

+ i_ret = dvdcss_close_device( dvdcss );
if( i_ret < 0 )
{
return i_ret;
}

- free( dvdcss->psz_device );
- free( dvdcss );
-
return 0;
}
--
2.1.0
Diego Biurrun
2014-11-13 13:08:14 UTC
Permalink
This is a more suitable place than libc_open().
---
src/device.c | 2 --
src/libdvdcss.c | 1 +
2 files changed, 1 insertion(+), 2 deletions(-)

diff --git a/src/device.c b/src/device.c
index e824b54..07e0a17 100644
--- a/src/device.c
+++ b/src/device.c
@@ -456,8 +456,6 @@ static int libc_open ( dvdcss_t dvdcss, const char *psz_device )
return -1;
}

- dvdcss->i_pos = 0;
-
return 0;
}

diff --git a/src/libdvdcss.c b/src/libdvdcss.c
index 91f83cd..b5911fd 100644
--- a/src/libdvdcss.c
+++ b/src/libdvdcss.c
@@ -473,6 +473,7 @@ LIBDVDCSS_EXPORT dvdcss_t dvdcss_open ( const char *psz_target )
}

/* Initialize structure with default values. */
+ dvdcss->i_pos = 0;
dvdcss->p_titles = NULL;
dvdcss->psz_device = strdup( psz_target );
dvdcss->psz_error = "no error";
--
2.1.0
Loading...