rs.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #ifndef __RS_H_
  2. #define __RS_H_
  3. /* use small value to save memory */
  4. #ifndef DATA_SHARDS_MAX
  5. #define DATA_SHARDS_MAX (255)
  6. #endif
  7. /* use other memory allocator */
  8. #ifndef RS_MALLOC
  9. #define RS_MALLOC(x) malloc(x)
  10. #endif
  11. #ifndef RS_FREE
  12. #define RS_FREE(x) free(x)
  13. #endif
  14. #ifndef RS_CALLOC
  15. #define RS_CALLOC(n, x) calloc(n, x)
  16. #endif
  17. typedef struct _reed_solomon {
  18. int data_shards;
  19. int parity_shards;
  20. int shards;
  21. unsigned char* m;
  22. unsigned char* parity;
  23. } reed_solomon;
  24. /**
  25. * MUST initial one time
  26. * */
  27. void fec_init(void);
  28. reed_solomon* reed_solomon_new(int data_shards, int parity_shards);
  29. void reed_solomon_release(reed_solomon* rs);
  30. /**
  31. * encode one shard
  32. * input:
  33. * rs
  34. * data_blocks[rs->data_shards][block_size]
  35. * fec_blocks[rs->data_shards][block_size]
  36. * */
  37. int reed_solomon_encode(reed_solomon* rs,
  38. unsigned char** data_blocks,
  39. unsigned char** fec_blocks,
  40. int block_size);
  41. /**
  42. * decode one shard
  43. * input:
  44. * rs
  45. * original data_blocks[rs->data_shards][block_size]
  46. * dec_fec_blocks[nr_fec_blocks][block_size]
  47. * fec_block_nos: fec pos number in original fec_blocks
  48. * erased_blocks: erased blocks in original data_blocks
  49. * nr_fec_blocks: the number of erased blocks
  50. * */
  51. int reed_solomon_decode(reed_solomon* rs,
  52. unsigned char **data_blocks,
  53. int block_size,
  54. unsigned char **dec_fec_blocks,
  55. unsigned int *fec_block_nos,
  56. unsigned int *erased_blocks,
  57. int nr_fec_blocks);
  58. /**
  59. * encode a big size of buffer
  60. * input:
  61. * rs
  62. * nr_shards: assert(0 == nr_shards % rs->data_shards)
  63. * shards[nr_shards][block_size]
  64. * */
  65. int reed_solomon_encode2(reed_solomon* rs, unsigned char** shards, int nr_shards, int block_size);
  66. /**
  67. * reconstruct a big size of buffer
  68. * input:
  69. * rs
  70. * nr_shards: assert(0 == nr_shards % rs->data_shards)
  71. * shards[nr_shards][block_size]
  72. * marks[nr_shards] marks as errors
  73. * */
  74. int reed_solomon_reconstruct(reed_solomon* rs, unsigned char** shards, unsigned char* marks, int nr_shards, int block_size);
  75. #endif