parse.c 2.87 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 "xalloc.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
72
73
74
75
76
77
78
79
int parse_boolean(struct simple_buffer *buffer, int *result)
{
  if (!LEFT)
    return 0;
  *result = HERE[0];
  ADVANCE(1);
  return 1;
}

Niels Möller's avatar
Niels Möller committed
80
int parse_bignum(struct simple_buffer *buffer, bignum *result)
Niels Möller's avatar
Niels Möller committed
81
82
83
84
{
  UINT32 length;
  UINT8 *digits;

Niels Möller's avatar
Niels Möller committed
85
  if (!parse_string(buffer, &length, &digits))
Niels Möller's avatar
Niels Möller committed
86
87
    return 0;

Niels Möller's avatar
Niels Möller committed
88
  bignum_parse(result, length, digits);
Niels Möller's avatar
Niels Möller committed
89
90

  return 1;
Niels Möller's avatar
Niels Möller committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
}

/* 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
106
  for(i = 0; i < LEFT; i++)
Niels Möller's avatar
Niels Möller committed
107
108
109
110
111
112
113
114
    {
      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
115
  *result = lookup_atom(HERE, i);
Niels Möller's avatar
Niels Möller committed
116
117
118
  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
119
120
121
  return 1;
}

Niels Möller's avatar
Niels Möller committed
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
int *parse_atom_list(struct simple_buffer *buffer)
{
  int count;
  int i;
  int *res;
  
  /* Count commas (no commas means one atom) */
  for (i = buffer->pos, count = 1; i < buffer->capacity; i++)
    if (buffer->data[i] == ',')
      count++;

  res = xalloc(sizeof(int) * (count+1));

  for (i = 0; i < count; i++)
    {
      switch(parse_next_atom(buffer, res+i))
	{
	case 1:
	  continue;
	case 0:
	  lsh_free(res);
	  return NULL;
	default:
	  fatal("Internal error\n");
	}
    }
  res[i] = -1;
  return res;
}

Niels Möller's avatar
Niels Möller committed
152
/* Returns success (i.e. 1) iff there is no data left */
Niels Möller's avatar
Niels Möller committed
153
int parse_eod(struct simple_buffer *buffer)
Niels Möller's avatar
Niels Möller committed
154
155
156
{
  return !LEFT;
}