parse.c 2.09 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1
2
3
4
5
/* parse.c
 *
 */

#include "parse.h"
Niels Möller's avatar
Niels Möller committed
6
#include "transport.h"
Niels Möller's avatar
Niels Möller committed
7
8
9
10
11
12
13
14
15

void simple_buffer_init(struct simple_buffer *buffer,
			UINT32 capacity, UINT8 *data)
{
  buffer->capacity = capacity;
  buffer->pos = 0;
  buffer->data = data;
}

Niels Möller's avatar
Niels Möller committed
16
#define LEFT (buffer->capacity - buffer->pos)
Niels Möller's avatar
Niels Möller committed
17
18
19
20
21
#define HERE (buffer->data + buffer->pos)
#define ADVANCE(n) (buffer->pos  += (n))

int parse_uint32(struct simple_buffer *buffer, UINT32 *result)
{
Niels Möller's avatar
Niels Möller committed
22
  if (LEFT < 4)
Niels Möller's avatar
Niels Möller committed
23
24
    return 0;

Niels Möller's avatar
Niels Möller committed
25
  *result = READ_UINT32(HERE);
Niels Möller's avatar
Niels Möller committed
26
27
28
29
30
31
32
33
34
35
36
37
  ADVANCE(4);
  return 1;
}

int parse_string(struct simple_buffer *buffer,
		 UINT32 *length, UINT8 **start)
{
  UINT32 l;

  if (!parse_uint32(buffer, &l))
    return 0;

Niels Möller's avatar
Niels Möller committed
38
  if (LEFT < l)
Niels Möller's avatar
Niels Möller committed
39
40
41
42
43
    return 0;

  *length = l;
  *start = HERE;
  ADVANCE(l);
Niels Möller's avatar
Niels Möller committed
44
  return 1;
Niels Möller's avatar
Niels Möller committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
}

/* Initializes subbuffer to parse a string from buffer */
int parse_sub_buffer(struct simple_buffer *buffer,
		     struct simple_buffer *subbuffer)
{
  UINT32 length;
  UINT8 *data;

  if (!parse_string(buffer, &length, &data))
    return 0;

  simple_buffer_init(subbuffer, length, data);
  return 1;
}

Niels Möller's avatar
Niels Möller committed
61
int parse_uint8(struct simple_buffer *buffer, UINT8 *result)
Niels Möller's avatar
Niels Möller committed
62
63
64
65
66
67
68
69
70
{
  if (!LEFT)
    return 0;

  *result = HERE[0];
  ADVANCE(1);
  return 1;
}

Niels Möller's avatar
Niels Möller committed
71
int parse_bignum(struct simple_buffer *buffer, bignum *result)
Niels Möller's avatar
Niels Möller committed
72
73
74
75
{
  UINT32 length;
  UINT8 *digits;

Niels Möller's avatar
Niels Möller committed
76
  if (!parse_string(buffer, &length, &digits))
Niels Möller's avatar
Niels Möller committed
77
78
    return 0;

Niels Möller's avatar
Niels Möller committed
79
  bignum_parse(result, length, digits);
Niels Möller's avatar
Niels Möller committed
80
81

  return 1;
Niels Möller's avatar
Niels Möller committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
}

/* Returns 1 on success, 0 on failure, and -1 at end of buffer.
 * Unknown atoms sets result to zero. */

/* NOTE: This functions record the fact that it has read to the end of
 * the buffer by setting the position to *beyond* the end of the
 * buffer. */
int parse_next_atom(struct simple_buffer *buffer, int *result)
{
  UINT32 i;

  if (buffer->pos > buffer->capacity)
    return -1;

Niels Möller's avatar
Niels Möller committed
97
  for(i = 0; i < LEFT; i++)
Niels Möller's avatar
Niels Möller committed
98
99
100
    if (HERE[i] == ',')
      {
	*result = lookup_atom(HERE, i);
Niels Möller's avatar
Niels Möller committed
101
	ADVANCE(i+1);
Niels Möller's avatar
Niels Möller committed
102
103
104
	return 1;
      }

Niels Möller's avatar
Niels Möller committed
105
  *result = lookup_atom(HERE, i);
Niels Möller's avatar
Niels Möller committed
106
107
108
109
110
  ADVANCE(i+1);  /* Beyond end of buffer */
  return 1;
}

/* Returns success (i.e. 1) iff there is no data left */
Niels Möller's avatar
Niels Möller committed
111
int parse_eod(struct simple_buffer *buffer)
Niels Möller's avatar
Niels Möller committed
112
113
114
{
  return !LEFT;
}