DRM decrypting tool for Samsung TVs PVR
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

buffer.c 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <fcntl.h>
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <unistd.h>
  16. #include "buffer.h"
  17. int pbinit(struct packetbuffer *pb)
  18. {
  19. if(pb == NULL)
  20. return 1;
  21. pbfree(pb);
  22. pb->buffer = malloc(BUFFERSIZE);
  23. if(pb->buffer == NULL){
  24. printf("malloc failed\n");
  25. return 1;
  26. }
  27. memset(pb->buffer, 0, BUFFERSIZE);
  28. pb->startp = pb->buffer;
  29. pb->workp = pb->buffer;
  30. pb->endp = pb->buffer;
  31. pb->end = 0;
  32. return 0;
  33. }
  34. int pbfree(struct packetbuffer *pb)
  35. {
  36. if(pb == NULL)
  37. return 1;
  38. if(pb->buffer != NULL)
  39. free(pb->buffer);
  40. pb->startp = NULL;
  41. pb->workp = NULL;
  42. pb->endp = NULL;
  43. pb->end = 0;
  44. return 0;
  45. }
  46. int pbread(struct packetbuffer *pb)
  47. {
  48. ssize_t tmp;
  49. /* read chunks of READSIZE to fill up buffer */
  50. while(pb->buffer + BUFFERSIZE - pb->endp >= READSIZE && pb->end == 0){
  51. tmp = read(pb->fdread, pb->endp, READSIZE);
  52. if(tmp < 1)
  53. pb->end = 1;
  54. pb->endp += tmp;
  55. }
  56. return 0;
  57. }
  58. int pbwrite(struct packetbuffer *pb)
  59. {
  60. /* write chunks of WRITESIZE */
  61. while(pb->workp - pb->startp >= WRITESIZE)
  62. pb->startp += write(pb->fdwrite, pb->startp, WRITESIZE);
  63. /* write remaining bytes at end of file */
  64. while(pb->workp - pb->startp > 0 && pb->end == 1)
  65. pb->startp += write(pb->fdwrite, pb->startp, pb->workp - pb->startp);
  66. /* copy over remaining bytes */
  67. if(pb->endp - pb->startp > 0)
  68. memcpy(pb->buffer, pb->startp, pb->endp - pb->startp);
  69. pb->endp = pb->buffer + (pb->endp - pb->startp);
  70. pb->workp = pb->buffer + (pb->workp - pb->startp);
  71. pb->startp = pb->buffer;
  72. return 0;
  73. }