libdvdcss: update bbappend for 1.2.13
[opendreambox.git] / meta-opendreambox / recipes-multimedia / vlc / libdvdcss / use-cached-reads.patch
1 --- libdvdcss-1.2.10.org/src/device.c   2008-07-13 14:25:49.000000000 +0200
2 +++ libdvdcss-1.2.10/src/device.c       2013-11-18 22:39:50.000000000 +0100
3 @@ -383,7 +383,12 @@
4  
5      return 0;
6  #else
7 -    close( dvdcss->i_fd );
8 +    if ( dvdcss->p_read_file )
9 +    {
10 +        fclose( dvdcss->p_read_file );
11 +        free ( dvdcss->p_read_buf );
12 +        dvdcss->p_read_file = NULL;
13 +    }
14  
15      if( dvdcss->i_raw_fd >= 0 )
16      {
17 @@ -403,7 +408,28 @@
18  static int libc_open ( dvdcss_t dvdcss, char const *psz_device )
19  {
20  #if !defined( WIN32 )
21 -    dvdcss->i_fd = dvdcss->i_read_fd = open( psz_device, 0 );
22 +    dvdcss->p_read_file = fopen( psz_device, "r" );
23 +    if ( dvdcss->p_read_file )
24 +    {
25 +        dvdcss->p_read_buf = malloc( 128*1024 );
26 +        if ( !dvdcss->p_read_buf )
27 +        {
28 +            print_debug( dvdcss, "allocate buffer failed!" );
29 +            fclose( dvdcss->p_read_file );
30 +            dvdcss->p_read_file = NULL;
31 +        }
32 +        else if ( setvbuf( dvdcss->p_read_file, dvdcss->p_read_buf, _IOFBF, 128*1024 ) )
33 +        {
34 +            print_debug( dvdcss, "setvbuf failed (%s)", strerror(errno) );
35 +            free( dvdcss->p_read_buf );
36 +            fclose( dvdcss->p_read_file );
37 +            dvdcss->p_read_file = NULL;
38 +        }
39 +    }
40 +    if (dvdcss->p_read_file)
41 +        dvdcss->i_fd = dvdcss->i_read_fd = fileno( dvdcss->p_read_file );
42 +    else
43 +        dvdcss->i_fd = dvdcss->i_read_fd = -1;
44  #else
45      dvdcss->i_fd = dvdcss->i_read_fd = open( psz_device, O_BINARY );
46  #endif
47 @@ -592,7 +618,18 @@
48      }
49  
50      i_seek = (off_t)i_blocks * (off_t)DVDCSS_BLOCK_SIZE;
51 -    i_seek = lseek( dvdcss->i_read_fd, i_seek, SEEK_SET );
52 +
53 +    if ( dvdcss->i_raw_fd >= 0 )
54 +        i_seek = lseek( dvdcss->i_read_fd, i_seek, SEEK_SET );
55 +    else
56 +    {
57 +        i_seek = fseeko( dvdcss->p_read_file, i_seek, SEEK_SET );
58 +        if ( !i_seek )
59 +        {
60 +            dvdcss->i_pos = i_blocks;
61 +            return i_blocks;
62 +        }
63 +    }
64  
65      if( i_seek < 0 )
66      {
67 @@ -673,36 +710,53 @@
68   *****************************************************************************/
69  static int libc_read ( dvdcss_t dvdcss, void *p_buffer, int i_blocks )
70  {
71 -    off_t i_size, i_ret;
72 -
73 -    i_size = (off_t)i_blocks * (off_t)DVDCSS_BLOCK_SIZE;
74 -    i_ret = read( dvdcss->i_read_fd, p_buffer, i_size );
75 -
76 -    if( i_ret < 0 )
77 +    if ( dvdcss->i_raw_fd >= 0 )
78      {
79 -        print_error( dvdcss, "read error" );
80 -        dvdcss->i_pos = -1;
81 -        return i_ret;
82 -    }
83 +        off_t i_size, i_ret;
84  
85 -    /* Handle partial reads */
86 -    if( i_ret != i_size )
87 -    {
88 -        int i_seek;
89 +        i_size = (off_t)i_blocks * (off_t)DVDCSS_BLOCK_SIZE;
90 +        i_ret = read( dvdcss->i_read_fd, p_buffer, i_size );
91  
92 -        dvdcss->i_pos = -1;
93 -        i_seek = libc_seek( dvdcss, i_ret / DVDCSS_BLOCK_SIZE );
94 -        if( i_seek < 0 )
95 +        if( i_ret < 0 )
96          {
97 -            return i_seek;
98 +            print_error( dvdcss, "read error" );
99 +            dvdcss->i_pos = -1;
100 +            return i_ret;
101          }
102  
103 -        /* We have to return now so that i_pos isn't clobbered */
104 +        /* Handle partial reads */
105 +        if( i_ret != i_size )
106 +        {
107 +            int i_seek;
108 +
109 +            dvdcss->i_pos = -1;
110 +            i_seek = libc_seek( dvdcss, i_ret / DVDCSS_BLOCK_SIZE );
111 +            if( i_seek < 0 )
112 +            {
113 +                return i_seek;
114 +            }
115 +
116 +            /* We have to return now so that i_pos isn't clobbered */
117 +            return i_ret / DVDCSS_BLOCK_SIZE;
118 +        }
119 +
120 +        dvdcss->i_pos += i_ret / DVDCSS_BLOCK_SIZE;
121          return i_ret / DVDCSS_BLOCK_SIZE;
122      }
123 +    else
124 +    {
125 +        size_t i_ret = fread( p_buffer, DVDCSS_BLOCK_SIZE, i_blocks, dvdcss->p_read_file );
126 +
127 +        if ( i_ret < 0 )
128 +        {
129 +            print_error( dvdcss, "read error" );
130 +            dvdcss->i_pos = -1;
131 +            return i_ret;
132 +        }
133  
134 -    dvdcss->i_pos += i_ret / DVDCSS_BLOCK_SIZE;
135 -    return i_ret / DVDCSS_BLOCK_SIZE;
136 +        dvdcss->i_pos += i_ret;
137 +        return i_ret;
138 +    }
139  }
140  
141  #if defined( WIN32 )
142 @@ -794,16 +848,72 @@
143      dvdcss->i_pos += i_total / DVDCSS_BLOCK_SIZE;
144      return i_total / DVDCSS_BLOCK_SIZE;
145  #else
146 -    int i_read = readv( dvdcss->i_read_fd, p_iovec, i_blocks );
147 -
148 -    if( i_read < 0 )
149 +    if ( dvdcss->i_raw_fd >= 0 )
150      {
151 -        dvdcss->i_pos = -1;
152 -        return i_read;
153 +        int i_read = readv( dvdcss->i_read_fd, p_iovec, i_blocks );
154 +
155 +        if( i_read < 0 )
156 +        {
157 +            dvdcss->i_pos = -1;
158 +            return i_read;
159 +        }
160 +
161 +        dvdcss->i_pos += i_read / DVDCSS_BLOCK_SIZE;
162 +        return i_read / DVDCSS_BLOCK_SIZE;
163      }
164 +    else
165 +    {
166 +        int i_index, i_len, i_total = 0;
167 +        unsigned char *p_base;
168 +
169 +        for( i_index = i_blocks;
170 +             i_index;
171 +             i_index--, p_iovec++ )
172 +        {
173 +            int i_num_read;
174 +            i_len  = p_iovec->iov_len;
175 +            p_base = p_iovec->iov_base;
176  
177 -    dvdcss->i_pos += i_read / DVDCSS_BLOCK_SIZE;
178 -    return i_read / DVDCSS_BLOCK_SIZE;
179 +            if( i_len <= 0 )
180 +            {
181 +                continue;
182 +            }
183 +
184 +            i_num_read = fread( p_base, i_len, 1, dvdcss->p_read_file );
185 +
186 +            if( i_num_read != 1 )
187 +            {
188 +                /* We reached the end of the file or a signal interrupted
189 +                 * the read. Return a partial read. */
190 +                int i_seek;
191 +
192 +                if (!i_total)
193 +                {
194 +                    /* One of the reads failed, too bad.
195 +                     * We won't even bother returning the reads that went ok,
196 +                     * and as in the posix spec the file postition is left
197 +                     * unspecified after a failure */
198 +                    dvdcss->i_pos = -1;
199 +                    return -1;
200 +                }
201 +
202 +                dvdcss->i_pos = -1;
203 +                i_seek = libc_seek( dvdcss, i_total / DVDCSS_BLOCK_SIZE );
204 +                if( i_seek < 0 )
205 +                {
206 +                    return i_seek;
207 +                }
208 +
209 +                /* We have to return now so that i_pos isn't clobbered */
210 +                return i_total / DVDCSS_BLOCK_SIZE;
211 +            }
212 +
213 +            i_total += i_len;
214 +        }
215 +
216 +        dvdcss->i_pos += i_total / DVDCSS_BLOCK_SIZE;
217 +        return i_total / DVDCSS_BLOCK_SIZE;
218 +    }
219  #endif
220  }
221  
222 --- libdvdcss-1.2.10.org/src/libdvdcss.c        2008-08-29 20:41:51.000000000 +0200
223 +++ libdvdcss-1.2.10/src/libdvdcss.c    2013-11-17 13:15:03.000000000 +0100
224 @@ -184,6 +184,7 @@
225      /*
226       *  Initialize structure with default values
227       */
228 +    dvdcss->p_read_file = NULL;
229  #ifndef WIN32
230      dvdcss->i_raw_fd = -1;
231  #endif
232 --- libdvdcss-1.2.10.org/src/libdvdcss.h        2008-08-29 20:39:56.000000000 +0200
233 +++ libdvdcss-1.2.10/src/libdvdcss.h    2013-11-17 13:15:03.000000000 +0100
234 @@ -35,6 +35,9 @@
235      int    i_read_fd;
236      int    i_pos;
237  
238 +    FILE * p_read_file;
239 +    void * p_read_buf;
240 +
241      /* File handling */
242      int ( * pf_seek )  ( dvdcss_t, int );
243      int ( * pf_read )  ( dvdcss_t, void *, int );