Browse Source

Integrate new internal buffer for improved I/O

tags/v1.1
Bernhard Fröhlich 4 years ago
parent
commit
961bd8a7ad
3 changed files with 52 additions and 47 deletions
  1. 2
    2
      Makefile
  2. 0
    1
      README.md
  3. 50
    44
      drmdecrypt.c

+ 2
- 2
Makefile View File

@@ -47,8 +47,8 @@ RELDIR	= drmdecrypt-$(VERSION)
47 47
 
48 48
 ##########################
49 49
 
50
-SRC	= AES.c AESNI.c drmdecrypt.c
51
-OBJS	= AES.o AESNI.o drmdecrypt.o
50
+SRC	= AES.c AESNI.c buffer.c drmdecrypt.c
51
+OBJS	= AES.o AESNI.o buffer.o drmdecrypt.o
52 52
 
53 53
 all:	drmdecrypt
54 54
 

+ 0
- 1
README.md View File

@@ -55,7 +55,6 @@ H (2014)   | not supported (unknown key)
55 55
 
56 56
 ## TODO
57 57
 
58
-- Internal I/O buffer for decrypting
59 58
 - default is BUFSIZ or better struct fstat blocksize
60 59
 - I/O write: write() + O_DIRECT + posix_memalign()
61 60
 - I/O read: fread(188) + setvbuf(32k+8, _IOFBF)

+ 50
- 44
drmdecrypt.c View File

@@ -7,9 +7,12 @@
7 7
  * of the GPL v2 license.  See the LICENSE file for details.
8 8
  */
9 9
 
10
+#include <sys/types.h>
11
+#include <sys/stat.h>
10 12
 #include <stdlib.h>
11 13
 #include <stdio.h>
12 14
 #include <string.h>
15
+#include <fcntl.h>
13 16
 #include <libgen.h>
14 17
 #include <limits.h>
15 18
 #include <unistd.h>
@@ -17,6 +20,7 @@
17 20
 
18 21
 #include "aes.h"
19 22
 #include "trace.h"
23
+#include "buffer.h"
20 24
 
21 25
 /* Helper macros */
22 26
 #define STR_HELPER(x) #x
@@ -224,17 +228,18 @@ int decrypt_aes128cbc(unsigned char *pin, int len, unsigned char *pout)
224 228
  *
225 229
  * See: http://en.wikipedia.org/wiki/MPEG_transport_stream
226 230
  */
227
-int decode_packet(unsigned char *data, unsigned char *outdata)
231
+int decode_packet(unsigned char *data)
228 232
 {
233
+   unsigned char tmp[PACKETSIZE];
229 234
    int offset;
230 235
 
231 236
    if(data[0] != 0x47)
232 237
    {
233 238
       trace(TRC_ERROR, "Not a valid MPEG packet!");
234
-      return 0;
239
+      return 1;
235 240
    }
236 241
 
237
-   memcpy(outdata, data, 188);
242
+   memcpy(tmp, data, PACKETSIZE);
238 243
    
239 244
    trace(TRC_DEBUG, "-------------------");
240 245
    trace(TRC_DEBUG, "Trans. Error Indicator: 0x%x", data[2] & 0x80);
@@ -255,12 +260,14 @@ int decode_packet(unsigned char *data, unsigned char *outdata)
255 260
       offset += (data[4]+1);
256 261
 
257 262
    /* remove scrambling bits */
258
-   outdata[3] &= 0x3f;
263
+   tmp[3] &= 0x3f;
259 264
 
260 265
    /* decrypt only full blocks (they seem to avoid padding) */
261
-   decrypt_aes128cbc(data + offset, ((188 - offset)/BLOCK_SIZE)*BLOCK_SIZE, outdata + offset);
266
+   decrypt_aes128cbc(data + offset, ((PACKETSIZE - offset)/BLOCK_SIZE)*BLOCK_SIZE, tmp + offset);
262 267
 
263
-   return 1;
268
+   memcpy(data, tmp, sizeof(tmp));
269
+
270
+   return 0;
264 271
 }
265 272
 
266 273
 int decryptsrf(char *srffile, char *outdir)
@@ -268,13 +275,12 @@ int decryptsrf(char *srffile, char *outdir)
268 275
    char mdbfile[PATH_MAX];
269 276
    char inffile[PATH_MAX];
270 277
    char outfile[PATH_MAX];
271
-   FILE *srffp, *outfp;
278
+   struct packetbuffer pb;
272 279
    int retries, sync_find = 0;
273
-   unsigned long filesize = 0, foffset = 0;
280
+   unsigned long filesize = 0;
274 281
    unsigned long i;
275
-   unsigned char buf[1024];
276
-   unsigned char outdata[1024];
277 282
 
283
+   memset(&pb, '\0', sizeof(pb));
278 284
    memset(inffile, '\0', sizeof(inffile));
279 285
    memset(mdbfile, '\0', sizeof(mdbfile));
280 286
    memset(outfile, '\0', sizeof(outfile));
@@ -299,22 +305,26 @@ int decryptsrf(char *srffile, char *outdir)
299 305
 
300 306
    trace(TRC_INFO, "Writing to %s", outfile);
301 307
 
302
-   if((outfp = fopen(outfile, "wb")) == NULL)
308
+   pbinit(&pb);
309
+
310
+   pb.fdwrite = open(outfile, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
311
+   if(pb.fdwrite == -1)
303 312
    {
304 313
       trace(TRC_ERROR, "Cannot open %s for writing", outfile);
305 314
       return 1;
306 315
    }
307 316
 
308
-   if((srffp = fopen(srffile, "rb")) == NULL)
317
+   pb.fdread = open(srffile, O_RDONLY);
318
+   if(pb.fdread == -1)
309 319
    {
310 320
       trace(TRC_ERROR, "Cannot open %s for reading", srffile);
321
+      return 1;
311 322
    }
312 323
 	
313 324
 
314 325
    /* calculate filesize */
315
-   fseek(srffp, 0, 2); 
316
-   filesize = ftell(srffp); 
317
-   rewind(srffp);
326
+   filesize = lseek(pb.fdread, 0, SEEK_END);
327
+   lseek(pb.fdread, 0, SEEK_SET);
318 328
 
319 329
    trace(TRC_INFO, "Filesize %ld", filesize);
320 330
 
@@ -323,26 +333,20 @@ resync:
323 333
    /* try to sync */
324 334
    sync_find = 0;
325 335
    retries = 10;
326
-   fseek(srffp, foffset, SEEK_SET);
327 336
 
328 337
    while(sync_find == 0 && retries-- > 0)
329 338
    {
330
-      if(fread(buf, sizeof(unsigned char), sizeof(buf), srffp) != sizeof(buf))
331
-      {
332
-         trace(TRC_INFO, "short read while resyncing");
333
-         break;
334
-      }
339
+      pbread(&pb);
335 340
 
336
-      /* search 188byte packets starting with 0x47 */
337
-      for(i=0; i < (sizeof(buf)-188-188); i++)
341
+      /* search packets starting with 0x47 */
342
+      for(i=0; i < (BUFFERSIZE-PACKETSIZE-PACKETSIZE); i++)
338 343
       {
339
-         if (buf[i] == 0x47 && buf[i+188] == 0x47 && buf[i+188+188] == 0x47)
344
+         if (*(pb.workp+i) == 0x47 && *(pb.workp+i+PACKETSIZE) == 0x47 && *(pb.workp+i+PACKETSIZE+PACKETSIZE) == 0x47)
340 345
          {
341 346
             sync_find = 1;
342
-            foffset += i;
343
-            fseek(srffp, foffset, SEEK_SET);
347
+            pb.workp += i;
344 348
 
345
-            trace(TRC_INFO, "synced at offset %ld", foffset);
349
+            trace(TRC_INFO, "synced at offset %ld", pb.workp-pb.startp);
346 350
 
347 351
             break;
348 352
          }
@@ -351,31 +355,33 @@ resync:
351 355
 
352 356
    if (sync_find)
353 357
    {
354
-      for(i=0; foffset+i < filesize; i+= 188)
358
+      while(pb.end == 0)
355 359
       {
356
-         if(fread(buf, sizeof(unsigned char), 188, srffp) != 188)
357
-         {
358
-            trace(TRC_INFO, "short read while reading stream");
359
-            break;
360
-         }
360
+         pbread(&pb);
361 361
 
362
-         if (buf[0] == 0x47)
362
+         while(pb.workp+PACKETSIZE <= pb.endp)
363 363
          {
364
-            decode_packet(buf, outdata);
365
-            fwrite(outdata, sizeof(unsigned char), 188, outfp);
364
+            if (*(pb.workp) == 0x47)
365
+            {
366
+               decode_packet((unsigned char *)pb.workp);
367
+               pb.workp += PACKETSIZE;
368
+            }
369
+            else
370
+            {
371
+               pbwrite(&pb);
372
+               goto resync;
373
+            }
366 374
          }
367
-         else
368
-         {
369
-            foffset += i;
370
-            trace(TRC_WARN, "lost sync at %ld", foffset);
371 375
 
372
-            goto resync;
373
-         }
376
+         pbwrite(&pb);
374 377
       }
375 378
    }
376 379
 
377
-   fclose(srffp);
378
-   fclose(outfp);
380
+   pbwrite(&pb);
381
+
382
+   close(pb.fdwrite);
383
+   close(pb.fdread);
384
+   pbfree(&pb);
379 385
 
380 386
    return 0;
381 387
 }

Loading…
Cancel
Save