parse.c 2.21 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/* parse.c
 *
 */

#include "parse.h"

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
15
#define LEFT (buffer->capacity - buffer->pos)
Niels Möller's avatar
Niels Möller committed
16
17
18
19
20
#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
21
  if (LEFT < 4)
Niels Möller's avatar
Niels Möller committed
22
23
    return 0;

Niels Möller's avatar
Niels Möller committed
24
  *result = READ_UINT32(HERE);
Niels Möller's avatar
Niels Möller committed
25
26
27
28
29
30
31
32
33
34
35
36
  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
37
  if (LEFT < l)
Niels Möller's avatar
Niels Möller committed
38
39
40
41
42
    return 0;

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

/* 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
60
int parse_uint8(struct simple_buffer *buffer, UINT8 *result)
Niels Möller's avatar
Niels Möller committed
61
62
63
64
65
66
67
68
69
{
  if (!LEFT)
    return 0;

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

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

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

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

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

/* 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
96
  for(i = 0; i < LEFT; i++)
Niels Möller's avatar
Niels Möller committed
97
98
99
100
101
102
103
104
    {
      if (HERE[i] == ',')
	break;
      if (i == 64)
	/* Atoms can be no larger than 64 characters */
	return 0;
    }
  
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
  ADVANCE(i+1);  /* If the atom was terminated at the end of the
		  * buffer, rather than by a comma, this points beyond
		  * the end of the buffer */
Niels Möller's avatar
Niels Möller committed
109
110
111
112
  return 1;
}

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