Commit d419a847 authored by Niels Möller's avatar Niels Möller
Browse files

* src/sexp.c (sexp_uint32): New function.

(sexp2uint32): New function.

Rev: src/sexp.c:1.34
Rev: src/sexp.h:1.31
parent 58dcd123
......@@ -711,7 +711,7 @@ sexp_z(const char *s)
return sexp_s(NULL, ssh_format("%lz", s));
}
/* mpz->atom */
/* mpz->sexp */
struct sexp *
sexp_un(const mpz_t n)
{
......@@ -739,7 +739,16 @@ sexp_sn(const mpz_t n)
return sexp_s(NULL, s);
}
/* Small unsigned int -> sexp */
struct sexp *sexp_uint32(UINT32 n)
{
/* FIXME: Eliminate redundant leading zeroes. */
struct lsh_string *digits = lsh_string_alloc(4);
WRITE_UINT32(digits->data, n);
return sexp_s(NULL, digits);
}
struct lsh_string *
sexp_format(struct sexp *e, int style, unsigned indent)
{
......@@ -899,6 +908,47 @@ sexp2bignum_u(struct sexp *e, mpz_t n, UINT32 limit)
return 0;
}
int
sexp2uint32(struct sexp *e, UINT32 *n)
{
struct lsh_string *digits = sexp2string(e);
if (!digits)
return 0;
switch(digits->length)
{
case 0:
return 0;
case 1:
*n = digits->data[0];
return 1;
case 2:
*n = READ_UINT16(digits->data);
return 1;
case 3:
*n = READ_UINT24(digits->data);
return 1;
case 4:
*n = READ_UINT32(digits->data);
return 1;
default:
{
UINT32 i;
UINT32 left;
for (i = 0, left = digits->length;
left > 4;
i++, left--)
if (digits->data[i])
return 0;
*n = READ_UINT32(digits->data + i);
return 1;
}
}
}
int
sexp_eq(struct sexp *e, UINT32 length, const UINT8 *name)
{
......
......@@ -121,10 +121,13 @@ struct sexp *sexp_a(const int a);
/* cstring->sexp */
struct sexp *sexp_z(const char *s);
/* mpz->atom */
/* mpz->sexp */
struct sexp *sexp_un(const mpz_t n);
struct sexp *sexp_sn(const mpz_t n);
/* Small unsigned int -> sexp */
struct sexp *sexp_uint32(UINT32 n);
/* cons */
struct sexp *sexp_c(struct sexp *car, struct sexp_cons *cdr);
......@@ -159,6 +162,9 @@ int sexp2atom(struct sexp *e);
int
sexp2bignum_u(struct sexp *e, mpz_t n, UINT32 limit);
int
sexp2uint32(struct sexp *e, UINT32 *n);
/* int sexp_null_cdr(struct sexp *e); */
struct lsh_string *sexp_contents(const struct sexp *e);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment