Пример #1
0
/*
 * Returns: Number of bytes read or 0 if no match, -1 if error.
 */
int ber_decinteger(const unsigned char *buf, Odr_int *val, int max)
{
    const unsigned char *b = buf;
    unsigned char fill;
    int res, len, remains;
    union { int i; unsigned char c[sizeof(int)]; } tmp;

    if ((res = ber_declen(b, &len, max)) < 0)
        return -1;
    if (len+res > max || len < 0) /* out of bounds or indefinite encoding */
        return -1;  
    if (len > (int) sizeof(int))  /* let's be reasonable, here */
        return -1;
    b+= res;

    remains = sizeof(int) - len;
    memcpy(tmp.c + remains, b, len);
    if (*b & 0X80)
        fill = 0XFF;
    else
        fill = 0X00;
    memset(tmp.c, fill, remains);
    *val = ntohl(tmp.i);

    b += len;
    return b - buf;
}
Пример #2
0
Файл: ber_any.c Проект: nla/yaz
int completeBER_n(const unsigned char *buf, int len, int level)
{
    int res, ll, zclass, tag, cons;
    const unsigned char *b = buf;

    if (level > 1000)
    {
#if BER_ANY_DEBUG
        yaz_log(YLOG_LOG, "completeBER lev=%d len=%d", level, len);
#endif
        return -2;
    }
    if (len < 2)
        return 0;
    if (!buf[0] && !buf[1])
        return -2;
    if ((res = ber_dectag(b, &zclass, &tag, &cons, len)) <= 0)
        return 0;
    b += res;
    len -= res;
    assert (len >= 0);
    res = ber_declen(b, &ll, len);
    if (res == -2)
    {
#if BER_ANY_DEBUG
        yaz_log(YLOG_LOG, "<<<<<<<<< return1 lev=%d res=%d", level, res);
#endif
        return -1;  /* error */
    }
    if (res == -1)
    {
#if BER_ANY_DEBUG
        yaz_log(YLOG_LOG, "<<<<<<<<< return2 lev=%d res=%d", level, res);
#endif
        return 0;    /* incomplete length */
    }
    b += res;
    len -= res;
    if (ll >= 0)
    {   /* definite length */
        if (len < ll)
        {
#if BER_ANY_DEBUG
            yaz_log(YLOG_LOG, "<<<<<<<<< return5 lev=%d len=%d ll=%d",
                    level, len, ll);
#endif
            return 0;
        }
        return ll + (b-buf);
    }
    /* indefinite length */
    if (!cons)
    {   /* if primitive, it's an error */
#if BER_ANY_DEBUG
        yaz_log(YLOG_LOG, "<<<<<<<<< return6 lev=%d ll=%d len=%d res=%d",
                level, ll, len, res);
#endif
        return -1;   /* error */
    }
    /* constructed - cycle through children */
    while (len >= 2)
    {
        if (b[0] == 0 && b[1] == 0)
            break;
        res = completeBER_n(b, len, level+1);
        if (res <= 0)
            return res;
        b += res;
        len -= res;
    }
    if (len < 2)
        return 0;
    return (b - buf) + 2;
}
Пример #3
0
int ber_bitstring(ODR o, Odr_bitmask *p, int cons)
{
    int res, len;
    const char *base;

    switch (o->direction)
    {
    case ODR_DECODE:
        if ((res = ber_declen(o->op->bp, &len, odr_max(o))) < 0)
        {
            odr_seterror(o, OPROTO, 4);
            return 0;
        }
        o->op->bp += res;
        if (cons)       /* fetch component strings */
        {
            base = o->op->bp;
            while (odp_more_chunks(o, base, len))
                if (!odr_bitstring(o, &p, 0, 0))
                    return 0;
            return 1;
        }
        /* primitive bitstring */
        if (len < 0)
        {
            odr_seterror(o, OOTHER, 5);
            return 0;
        }
        if (len == 0)
            return 1;
        if (len - 1 > ODR_BITMASK_SIZE)
        {
            odr_seterror(o, OOTHER, 6);
            return 0;
        }
        if (len > odr_max(o))
        {
            odr_seterror(o, OOTHER, 7);
            return 0;
        }
        o->op->bp++;      /* silently ignore the unused-bits field */
        len--;
        memcpy(p->bits + p->top + 1, o->op->bp, len);
        p->top += len;
        o->op->bp += len;
        return 1;
    case ODR_ENCODE:
        if ((res = ber_enclen(o, p->top + 2, 5, 0)) < 0)
            return 0;
        if (odr_putc(o, 0) < 0)    /* no unused bits here */
            return 0;
        if (p->top < 0)
            return 1;
        if (odr_write(o, p->bits, p->top + 1) < 0)
            return 0;
        return 1;
    case ODR_PRINT:
        return 1;
    default:
        odr_seterror(o, OOTHER, 8);
        return 0;
    }
}