Revision 2050b9b0 src/ChunkTrading/chunk_encoding.c

View differences:

src/ChunkTrading/chunk_encoding.c
14 14
#include "trade_msg_la.h"
15 15
#include "int_coding.h"
16 16

  
17
#define CHUNK_HEADER_SIZE 20
18

  
17 19
int encodeChunk(const struct chunk *c, uint8_t *buff, int buff_len)
18 20
{
19 21
  uint32_t half_ts;
20 22

  
21
  if (buff_len < 20 + c->size + c->attributes_size) {
23
  if (buff_len < CHUNK_HEADER_SIZE + c->size + c->attributes_size) {
22 24
    /* Not enough space... */
23 25
    return -1;
24 26
  }
......
30 32
  int_cpy(buff + 8, half_ts);
31 33
  int_cpy(buff + 12, c->size);
32 34
  int_cpy(buff + 16, c->attributes_size);
33
  memcpy(buff + 20, c->data, c->size);
35
  memcpy(buff + CHUNK_HEADER_SIZE, c->data, c->size);
34 36
  if (c->attributes_size) {
35
    memcpy(buff + 20 + c->size, c->attributes, c->attributes_size);
37
    memcpy(buff + CHUNK_HEADER_SIZE + c->size, c->attributes, c->attributes_size);
36 38
  }
37 39

  
38
  return 20 + c->size + c->attributes_size;
40
  return CHUNK_HEADER_SIZE + c->size + c->attributes_size;
39 41
}
40 42

  
41 43
int decodeChunk(struct chunk *c, const uint8_t *buff, int buff_len)
42 44
{
43
  if (buff_len < 20) {
45
  if (buff_len < CHUNK_HEADER_SIZE) {
44 46
    return -1;
45 47
  }
46 48
  c->id = int_rcpy(buff);
......
50 52
  c->size = int_rcpy(buff + 12);
51 53
  c->attributes_size = int_rcpy(buff + 16);
52 54

  
53
  if (buff_len < c->size + 20) {
55
  if (buff_len < c->size + CHUNK_HEADER_SIZE) {
54 56
    return -2;
55 57
  }
56 58
  c->data = malloc(c->size);
57 59
  if (c->data == NULL) {
58 60
    return -3;
59 61
  }
60
  memcpy(c->data, buff + 20, c->size);
62
  memcpy(c->data, buff + CHUNK_HEADER_SIZE, c->size);
61 63

  
62 64
  if (c->attributes_size > 0) {
63 65
    if (buff_len < c->size + c->attributes_size) {
......
67 69
    if (c->attributes == NULL) {
68 70
      return -5;
69 71
    }
70
    memcpy(c->attributes, buff + 20 + c->size, c->attributes_size);
72
    memcpy(c->attributes, buff + CHUNK_HEADER_SIZE + c->size, c->attributes_size);
71 73
  }
72 74

  
73
  return 20 + c->size + c->attributes_size;
75
  return CHUNK_HEADER_SIZE + c->size + c->attributes_size;
74 76
}

Also available in: Unified diff