streamencoder.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* libanode: the Anode C reference implementation
  2. * Copyright (C) 2009 Adam Ierymenko <[email protected]>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>. */
  16. #ifndef _SPARK_STREAMENCODER_H
  17. #define _SPARK_STREAMENCODER_H
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. typedef struct
  22. {
  23. unsigned char *input_buf;
  24. unsigned long input_buf_capacity;
  25. unsigned long input_length;
  26. unsigned char *stream_out_buf;
  27. unsigned long stream_out_buf_capacity;
  28. unsigned long stream_out_length;
  29. void (*data_segment_add_func)(const void *data,unsigned long len,const void *global_hash,unsigned long global_hash_len);
  30. } SparkStreamEncoder;
  31. /**
  32. * Initialize a spark stream encoder
  33. *
  34. * @param enc Encoder structure to initialize
  35. * @param data_segment_add_func Function to call to store or cache data
  36. */
  37. void SparkStreamEncoder_init(
  38. SparkStreamEncoder *enc,
  39. void (*data_segment_add_func)(
  40. const void *data,
  41. unsigned long len,
  42. const void *global_hash,
  43. unsigned long global_hash_len));
  44. /**
  45. * Clean up a spark stream encoder structure
  46. *
  47. * @param enc Structure to clear
  48. */
  49. void SparkStreamEncoder_destroy(SparkStreamEncoder *enc);
  50. /**
  51. * Add data to encode
  52. *
  53. * @param enc Encoder structure
  54. * @param data Data to encode
  55. * @param len Length of data in bytes
  56. * @return Number of bytes of result stream now available
  57. */
  58. unsigned long SparkStreamEncoder_put(
  59. SparkStreamEncoder *enc,
  60. const void *data,
  61. unsigned long len);
  62. /**
  63. * Flush all data currently in input buffer
  64. *
  65. * @param enc Encoder structure to flush
  66. */
  67. void SparkStreamEncoder_flush(SparkStreamEncoder *enc);
  68. /**
  69. * @return Number of bytes of output stream available
  70. */
  71. static inline unsigned long SparkStreamEncoder_available(SparkStreamEncoder *enc)
  72. {
  73. return enc->stream_out_length;
  74. }
  75. /**
  76. * @return Pointer to result stream bytes (may return null if none available)
  77. */
  78. static inline const void *SparkStreamEncoder_get(SparkStreamEncoder *enc)
  79. {
  80. return (const void *)(enc->stream_out_buf);
  81. }
  82. /**
  83. * @return "Consume" result stream bytes after they're read or sent
  84. */
  85. static inline void SparkStreamEncoder_consume(SparkStreamEncoder *enc,unsigned long len)
  86. {
  87. unsigned long i;
  88. for(i=len;i<enc->stream_out_length;++i)
  89. enc->stream_out_buf[i - len] = enc->stream_out_buf[i];
  90. }
  91. #ifdef __cplusplus
  92. }
  93. #endif
  94. #endif