<commit_before>/* $Id$ */
/*****************************************************************************
File:     casn_num.c
Contents: Functions to handle numeris ASN.1 objects.
System:   Compact ASN development.
Created:
Author:   <NAME> <<EMAIL>>

Remarks:

*****************************************************************************/

char casn_num_sfcsid[] = "@(#)casn_num.c 854P";
#include "casn.h"

extern struct casn *_go_up(struct casn *);
extern void _clear_casn(struct casn *, ushort);

extern int _casn_obj_err(struct casn *, int),
    _check_filled(struct casn *casnp, int),
    _clear_error(struct casn *),
    _fill_upward(struct casn *, int),
    _write_casn(struct casn *casnp, uchar *c, int lth);

int _table_op(struct casn *casnp),
    _write_casn_num(struct casn *casnp, long val);

int diff_casn_num(struct casn *casnp, long val)
    {
    long tmp;

    if (read_casn_num(casnp, &tmp) < 0)
        return -2;

    if (tmp > val)
        return 1;
    else if (tmp < val)
        return -1;
    else
        return 0;
    }

int read_casn_num(struct casn *casnp, long *valp)
    {
    struct casn *tcasnp;
    int ansr, err = 0;
    uchar *c;

    if (_clear_error(casnp) < 0) return -1;
    if (casnp->type == ASN_NOTYPE)
	{
	if (!(tcasnp = _go_up(casnp))) err = ASN_TYPE_ERR;
	}
    else tcasnp = casnp;
    if (!err)
	{
        if ((ansr = _check_filled(tcasnp, 1)) < 0 ||
            (!ansr && !(tcasnp->flags & ASN_DEFAULT_FLAG))) return ansr; // looks wrong, but we left it (AC, DM): may return 0 instead of error if not filled
        if (tcasnp->type != ASN_INTEGER && tcasnp->type != ASN_ENUMERATED &&
            tcasnp->type != ASN_BOOLEAN)
    	    err = ASN_TYPE_ERR;
	else if (tcasnp->lth > sizeof(*valp) || tcasnp->lth <= 0)
            err = ASN_LENGTH_ERR;
	}
    if (err) return _casn_obj_err(casnp, err);
		// if not filled but has default
    if ((tcasnp->flags & (ASN_FILLED_FLAG | ASN_DEFAULT_FLAG)) ==
        ASN_DEFAULT_FLAG)
	{
	if (tcasnp->type == ASN_BOOLEAN)
    	    {
            uchar buf[8];
            memset(buf, 0, sizeof(buf)); 
    	    read_casn(tcasnp, buf);
	    *valp = buf[0];
	    return 1;
	    }
        *valp = 0;  // DEFINITELY WRONG
        return 0;   // MIGHT BE WRONG (depends on intended semantics when not filled in)
//	*valp = (int)tcasnp->ptr;
//      if ((ansr = *valp) < 0) ansr = -ansr;
//	for (c = casnp->startp; ansr; ansr >>= 8, c++);
	}
    else
        {
        if ((*tcasnp->startp & 0x80) && tcasnp->type == ASN_INTEGER)
            *valp = -1;
        else *valp = 0;<commit_msg>move check for length 0 below check for a not-filled default<commit_after>/* $Id$ */
/*****************************************************************************
File:     casn_num.c
Contents: Functions to handle numeris ASN.1 objects.
System:   Compact ASN development.
Created:
Author:   <NAME> <<EMAIL>>

Remarks:

*****************************************************************************/

char casn_num_sfcsid[] = "@(#)casn_num.c 854P";
#include "casn.h"

extern struct casn *_go_up(struct casn *);
extern void _clear_casn(struct casn *, ushort);

extern int _casn_obj_err(struct casn *, int),
    _check_filled(struct casn *casnp, int),
    _clear_error(struct casn *),
    _fill_upward(struct casn *, int),
    _write_casn(struct casn *casnp, uchar *c, int lth);

int _table_op(struct casn *casnp),
    _write_casn_num(struct casn *casnp, long val);

int diff_casn_num(struct casn *casnp, long val)
    {
    long tmp;

    if (read_casn_num(casnp, &tmp) < 0)
        return -2;

    if (tmp > val)
        return 1;
    else if (tmp < val)
        return -1;
    else
        return 0;
    }

int read_casn_num(struct casn *casnp, long *valp)
    {
    struct casn *tcasnp;
    int ansr, err = 0;
    uchar *c;

    if (_clear_error(casnp) < 0) return -1;
    if (casnp->type == ASN_NOTYPE)
	{
	if (!(tcasnp = _go_up(casnp))) err = ASN_TYPE_ERR;
	}
    else tcasnp = casnp;
    if (!err)
	{
        if ((ansr = _check_filled(tcasnp, 1)) < 0 ||
            (!ansr && !(tcasnp->flags & ASN_DEFAULT_FLAG))) return ansr; // looks wrong, but we left it (AC, DM): may return 0 instead of error if not filled
        if (tcasnp->type != ASN_INTEGER && tcasnp->type != ASN_ENUMERATED &&
            tcasnp->type != ASN_BOOLEAN)
    	    err = ASN_TYPE_ERR;
	else if (tcasnp->lth > sizeof(*valp) || tcasnp->lth < 0)
            err = ASN_LENGTH_ERR;
	}
    if (err) return _casn_obj_err(casnp, err);
		// if not filled but has default
    if ((tcasnp->flags & (ASN_FILLED_FLAG | ASN_DEFAULT_FLAG)) ==
        ASN_DEFAULT_FLAG)
	{
	if (tcasnp->type == ASN_BOOLEAN)
    	    {
            uchar buf[8];
            memset(buf, 0, sizeof(buf)); 
    	    read_casn(tcasnp, buf);
	    *valp = buf[0];
	    return 1;
	    }
        *valp = 0;  // DEFINITELY WRONG
        return 0;   // MIGHT BE WRONG (depends on intended semantics when not filled in)
//	*valp = (int)tcasnp->ptr;
//      if ((ansr = *valp) < 0) ansr = -ansr;
//	for (c = casnp->startp; ansr; ansr >>= 8, c++);
	}
    else
        {
        if (tcasnp->lth <= 0)
            return _casn_obj_err(tcasnp, ASN_LENGTH_ERR);
        if ((*tcasnp->startp & 0x80) && tcasnp->type == ASN_INTEGER)
            *valp = -1;
        else *valp = 0;
{'fraction_non_alphanumeric': 0.19029567854435178, 'fraction_numerical': 0.011372251705837756, 'mean_word_length': 2.6902097902097903, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2439168', 'n_tokens_mistral': 2050, 'n_tokens_neox': 1960, 'n_words': 503}