Delta.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /* Delta.c -- Delta converter
  2. 2009-05-26 : Igor Pavlov : Public domain */
  3. #include "Precomp.h"
  4. #include "Delta.h"
  5. void Delta_Init(Byte *state)
  6. {
  7. unsigned i;
  8. for (i = 0; i < DELTA_STATE_SIZE; i++)
  9. state[i] = 0;
  10. }
  11. static void MyMemCpy(Byte *dest, const Byte *src, unsigned size)
  12. {
  13. unsigned i;
  14. for (i = 0; i < size; i++)
  15. dest[i] = src[i];
  16. }
  17. void Delta_Encode(Byte *state, unsigned delta, Byte *data, SizeT size)
  18. {
  19. Byte buf[DELTA_STATE_SIZE];
  20. unsigned j = 0;
  21. MyMemCpy(buf, state, delta);
  22. {
  23. SizeT i;
  24. for (i = 0; i < size;)
  25. {
  26. for (j = 0; j < delta && i < size; i++, j++)
  27. {
  28. Byte b = data[i];
  29. data[i] = (Byte)(b - buf[j]);
  30. buf[j] = b;
  31. }
  32. }
  33. }
  34. if (j == delta)
  35. j = 0;
  36. MyMemCpy(state, buf + j, delta - j);
  37. MyMemCpy(state + delta - j, buf, j);
  38. }
  39. void Delta_Decode(Byte *state, unsigned delta, Byte *data, SizeT size)
  40. {
  41. Byte buf[DELTA_STATE_SIZE];
  42. unsigned j = 0;
  43. MyMemCpy(buf, state, delta);
  44. {
  45. SizeT i;
  46. for (i = 0; i < size;)
  47. {
  48. for (j = 0; j < delta && i < size; i++, j++)
  49. {
  50. buf[j] = data[i] = (Byte)(buf[j] + data[i]);
  51. }
  52. }
  53. }
  54. if (j == delta)
  55. j = 0;
  56. MyMemCpy(state, buf + j, delta - j);
  57. MyMemCpy(state + delta - j, buf, j);
  58. }