Browse Source

Implement internal buffer to allow reading and writing in bigger chunks

and for future OpenMP support.
Bernhard Fröhlich 4 years ago
parent
commit
f4a49456fb
2 changed files with 129 additions and 0 deletions
  1. 94
    0
      buffer.c
  2. 35
    0
      buffer.h

+ 94
- 0
buffer.c View File

@@ -0,0 +1,94 @@
1
+/* drmdecrypt -- DRM decrypting tool for Samsung TVs
2
+ *
3
+ * Copyright (C) 2014 - Bernhard Froehlich <decke@bluelife.at>
4
+ * All rights reserved.
5
+ *
6
+ * This software may be modified and distributed under the terms
7
+ * of the GPL v2 license.  See the LICENSE file for details.
8
+ */
9
+
10
+#include <sys/types.h>
11
+#include <sys/stat.h>
12
+#include <fcntl.h>
13
+#include <stdlib.h>
14
+#include <stdio.h>
15
+#include <string.h>
16
+#include <unistd.h>
17
+
18
+#include "buffer.h"
19
+
20
+int pbinit(struct packetbuffer *pb)
21
+{
22
+   if(pb == NULL)
23
+      return 1;
24
+
25
+   pbfree(pb);
26
+
27
+   pb->buffer = malloc(BUFFERSIZE);
28
+   if(pb->buffer == NULL){
29
+      printf("malloc failed\n");
30
+      return 1;
31
+   }
32
+   memset(pb->buffer, 0, BUFFERSIZE);
33
+
34
+   pb->startp = pb->buffer;
35
+   pb->workp = pb->buffer;
36
+   pb->endp = pb->buffer;
37
+   pb->end = 0;
38
+
39
+   return 0;
40
+}
41
+
42
+int pbfree(struct packetbuffer *pb)
43
+{
44
+   if(pb == NULL)
45
+      return 1;
46
+
47
+   if(pb->buffer != NULL)
48
+      free(pb->buffer);
49
+   
50
+   pb->startp = NULL;
51
+   pb->workp = NULL;
52
+   pb->endp = NULL;
53
+   pb->end = 0;
54
+
55
+   return 0;
56
+}
57
+
58
+int pbread(struct packetbuffer *pb)
59
+{
60
+   ssize_t tmp;
61
+
62
+   /* read chunks of READSIZE to fill up buffer */
63
+   while(pb->buffer + BUFFERSIZE - pb->endp >= READSIZE && pb->end == 0){
64
+      tmp = read(pb->fdread, pb->endp, READSIZE);
65
+      if(tmp < 1)
66
+         pb->end = 1;
67
+
68
+      pb->endp += tmp;
69
+   }
70
+
71
+   return 0;
72
+}
73
+
74
+int pbwrite(struct packetbuffer *pb)
75
+{
76
+   /* write chunks of WRITESIZE */
77
+   while(pb->workp - pb->startp >= WRITESIZE)
78
+      pb->startp += write(pb->fdwrite, pb->startp, WRITESIZE);
79
+
80
+   /* write remaining bytes at end of file */
81
+   while(pb->workp - pb->startp > 0 && pb->end == 1)
82
+      pb->startp += write(pb->fdwrite, pb->startp, pb->workp - pb->startp);
83
+
84
+   /* copy over remaining bytes */
85
+   if(pb->endp - pb->startp > 0)
86
+      memcpy(pb->buffer, pb->startp, pb->endp - pb->startp);
87
+
88
+   pb->endp = pb->buffer + (pb->endp - pb->startp);
89
+   pb->workp = pb->buffer + (pb->workp - pb->startp);
90
+   pb->startp = pb->buffer;
91
+
92
+   return 0;
93
+}
94
+

+ 35
- 0
buffer.h View File

@@ -0,0 +1,35 @@
1
+/* drmdecrypt -- DRM decrypting tool for Samsung TVs
2
+ *
3
+ * Copyright (C) 2014 - Bernhard Froehlich <decke@bluelife.at>
4
+ * All rights reserved.
5
+ *
6
+ * This software may be modified and distributed under the terms
7
+ * of the GPL v2 license.  See the LICENSE file for details.
8
+ */
9
+
10
+#ifndef _BUFFER_H_
11
+#define _BUFFER_H_
12
+
13
+#define READSIZE    4096
14
+#define WRITESIZE   4096
15
+#define PACKETSIZE  188
16
+#define BUFFERSIZE  (READSIZE+READSIZE+PACKETSIZE)
17
+
18
+struct packetbuffer
19
+{
20
+   char *buffer;
21
+   char *startp;
22
+   char *workp;
23
+   char *endp;
24
+   int end;
25
+   int fdread;
26
+   int fdwrite;
27
+};
28
+
29
+extern int pbinit(struct packetbuffer *pb);
30
+extern int pbfree(struct packetbuffer *pb);
31
+extern int pbread(struct packetbuffer *pb);
32
+extern int pbwrite(struct packetbuffer *pb);
33
+
34
+#endif /* _BUFFER_H_ */
35
+

Loading…
Cancel
Save