Skip to content
Permalink
Browse files

Implement internal buffer to allow reading and writing in bigger chunks

and for future OpenMP support.
  • Loading branch information...
decke committed Jan 2, 2015
1 parent a3de3bd commit f4a49456fb1fddadec8cd33ec7737d88c2564590
Showing with 129 additions and 0 deletions.
  1. +94 −0 buffer.c
  2. +35 −0 buffer.h
@@ -0,0 +1,94 @@
/* drmdecrypt -- DRM decrypting tool for Samsung TVs
*
* Copyright (C) 2014 - Bernhard Froehlich <decke@bluelife.at>
* All rights reserved.
*
* This software may be modified and distributed under the terms
* of the GPL v2 license. See the LICENSE file for details.
*/

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "buffer.h"

int pbinit(struct packetbuffer *pb)
{
if(pb == NULL)
return 1;

pbfree(pb);

pb->buffer = malloc(BUFFERSIZE);
if(pb->buffer == NULL){
printf("malloc failed\n");
return 1;
}
memset(pb->buffer, 0, BUFFERSIZE);

pb->startp = pb->buffer;
pb->workp = pb->buffer;
pb->endp = pb->buffer;
pb->end = 0;

return 0;
}

int pbfree(struct packetbuffer *pb)
{
if(pb == NULL)
return 1;

if(pb->buffer != NULL)
free(pb->buffer);

pb->startp = NULL;
pb->workp = NULL;
pb->endp = NULL;
pb->end = 0;

return 0;
}

int pbread(struct packetbuffer *pb)
{
ssize_t tmp;

/* read chunks of READSIZE to fill up buffer */
while(pb->buffer + BUFFERSIZE - pb->endp >= READSIZE && pb->end == 0){
tmp = read(pb->fdread, pb->endp, READSIZE);
if(tmp < 1)
pb->end = 1;

pb->endp += tmp;
}

return 0;
}

int pbwrite(struct packetbuffer *pb)
{
/* write chunks of WRITESIZE */
while(pb->workp - pb->startp >= WRITESIZE)
pb->startp += write(pb->fdwrite, pb->startp, WRITESIZE);

/* write remaining bytes at end of file */
while(pb->workp - pb->startp > 0 && pb->end == 1)
pb->startp += write(pb->fdwrite, pb->startp, pb->workp - pb->startp);

/* copy over remaining bytes */
if(pb->endp - pb->startp > 0)
memcpy(pb->buffer, pb->startp, pb->endp - pb->startp);

pb->endp = pb->buffer + (pb->endp - pb->startp);
pb->workp = pb->buffer + (pb->workp - pb->startp);
pb->startp = pb->buffer;

return 0;
}

@@ -0,0 +1,35 @@
/* drmdecrypt -- DRM decrypting tool for Samsung TVs
*
* Copyright (C) 2014 - Bernhard Froehlich <decke@bluelife.at>
* All rights reserved.
*
* This software may be modified and distributed under the terms
* of the GPL v2 license. See the LICENSE file for details.
*/

#ifndef _BUFFER_H_
#define _BUFFER_H_

#define READSIZE 4096
#define WRITESIZE 4096
#define PACKETSIZE 188
#define BUFFERSIZE (READSIZE+READSIZE+PACKETSIZE)

struct packetbuffer
{
char *buffer;
char *startp;
char *workp;
char *endp;
int end;
int fdread;
int fdwrite;
};

extern int pbinit(struct packetbuffer *pb);
extern int pbfree(struct packetbuffer *pb);
extern int pbread(struct packetbuffer *pb);
extern int pbwrite(struct packetbuffer *pb);

#endif /* _BUFFER_H_ */

0 comments on commit f4a4945

Please sign in to comment.
You can’t perform that action at this time.