/*
 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
 * 
 * The contents of this file constitute Original Code as defined in and are
 * subject to the Apple Public Source License Version 1.2 (the 'License').
 * You may not use this file except in compliance with the License. Please obtain
 * a copy of the License at http://www.apple.com/publicsource and read it before
 * using this file.
 * 
 * This Original Code and all software distributed under the License are
 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
 * specific language governing rights and limitations under the License.
 */


/*
 * asn_oid.c - BER encode, decode, print and free routines for the
 *             ASN.1 OBJECT IDENTIFIER type.
 *
 * MS 92
 * Copyright (C) 1992 Michael Sample and the University of British Columbia
 *
 * This library is free software; you can redistribute it and/or
 * modify it provided that this copyright/license information is retained
 * in original form.
 *
 * If you modify this file, you must clearly indicate your changes.
 *
 * This source code is distributed in the hope that it will be
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * $Header: /cvs/Darwin/src/live/Security/SecuritySNACCRuntime/c-lib/src/asn-oid.c,v 1.1.1.1 2001/05/18 23:14:08 mb Exp $
 * $Log: asn-oid.c,v $
 * Revision 1.1.1.1  2001/05/18 23:14:08  mb
 * Move from private repository to open source repository
 *
 * Revision 1.2  2001/05/05 00:59:25  rmurphy
 * Adding darwin license headers
 *
 * Revision 1.1.1.1  1999/03/16 18:06:31  aram
 * Originals from SMIME Free Library.
 *
 * Revision 1.3  1995/07/24 21:04:53  rj
 * changed `_' to `-' in file names.
 *
 * Revision 1.2  1994/09/01  00:06:21  rj
 * reduce the risk of unwanted surprises with macro expansion by properly separating the C tokens.
 *
 * Revision 1.1  1994/08/28  09:45:59  rj
 * first check-in. for a list of changes to the snacc-1.1 distribution please refer to the ChangeLog.
 *
 */

#include "asn-config.h"
#include "asn-len.h"
#include "asn-tag.h"
#include "asn-octs.h"
#include "asn-oid.h"


/*
 * encodes universal TAG LENGTH and Contents of and ASN.1 OBJECT ID
 */
AsnLen
BEncAsnOid PARAMS ((b, data),
    BUF_TYPE     b _AND_
    AsnOid *data)
{
    AsnLen len;

    len =  BEncAsnOidContent (b, data);
    len += BEncDefLen (b, len);
    len += BEncTag1 (b, UNIV, PRIM, OID_TAG_CODE);
    return len;
}  /* BEncAsnOid */


/*
 * decodes universal TAG LENGTH and Contents of and ASN.1 OBJECT ID
 */
void
BDecAsnOid PARAMS ((b, result, bytesDecoded, env),
    BUF_TYPE   b _AND_
    AsnOid    *result _AND_
    AsnLen *bytesDecoded _AND_
    jmp_buf env)
{
    AsnTag tag;
    AsnLen elmtLen;

    if ((tag = BDecTag (b, bytesDecoded, env)) != MAKE_TAG_ID (UNIV, PRIM, INTEGER_TAG_CODE))
    {
         Asn1Error ("BDecAsnOid: ERROR - wrong tag on OBJECT IDENTIFIER.\n");
         longjmp (env, -40);
    }

    elmtLen = BDecLen (b, bytesDecoded, env);
    BDecAsnOidContent (b, tag, elmtLen, result, bytesDecoded, env);

}  /* BDecAsnOid */



/*
 * Decodes just the content of the OID.
 * AsnOid is handled the same as a primtive octet string
 */
void
BDecAsnOidContent PARAMS ((b, tagId, len, result, bytesDecoded, env),
    BUF_TYPE b _AND_
    AsnTag tagId _AND_
    AsnLen len _AND_
    AsnOid *result _AND_
    AsnLen *bytesDecoded _AND_
    jmp_buf env)
{
    result->octetLen = len;
    result->octs =  Asn1Alloc (len);
    BufCopy (result->octs, b, len);
    if (BufReadError (b))
    {
         Asn1Error ("BDecAsnOidContent: ERROR - decoded past end of data\n");
         longjmp (env, -21);
    }
    (*bytesDecoded) += len;
}  /* BDecAsnOidContent */



/*
 * Prints the given OID to the given FILE * in ASN.1 Value Notation.
 * Since the internal rep of an OID is 'encoded', this routine
 * decodes each individual arc number to print it.
 */
void
PrintAsnOid PARAMS ((f,v, indent),
    FILE *f _AND_
    AsnOid *v _AND_
    unsigned short int indent)
{
    unsigned short int firstArcNum;
    unsigned long int arcNum;
    int i;

    fprintf (f,"{");

    /* un-munge first two arc numbers */
    for (arcNum = 0, i=0; (i < v->octetLen) && (v->octs[i] & 0x80);i++)
        arcNum = (arcNum << 7) + (v->octs[i] & 0x7f);

    arcNum = (arcNum << 7) + (v->octs[i] & 0x7f);
    i++;
    firstArcNum = arcNum/40;
    if (firstArcNum > 2)
        firstArcNum = 2;

    fprintf (f,"%u %u", firstArcNum, arcNum - (firstArcNum * 40));

    for (; i < v->octetLen; )
    {
        for (arcNum = 0; (i < v->octetLen) && (v->octs[i] & 0x80);i++)
            arcNum = (arcNum << 7) + (v->octs[i] & 0x7f);

        arcNum = (arcNum << 7) + (v->octs[i] & 0x7f);
        i++;
        fprintf (f," %u", arcNum);
    }
    fprintf (f,"}");

} /* PrintAsnOid */



/*
 * given an OID, figures out the length for the encoded version
 */
AsnLen
EncodedOidLen PARAMS ((oid),
    OID *oid)
{
    AsnLen totalLen;
    unsigned long headArcNum;
    unsigned long tmpArcNum;
    OID *tmpOid;

    /*
     * oid must have at least 2 elmts
     */
    if (oid->next == NULL)
       return 0;

    headArcNum = (oid->arcNum * 40) + oid->next->arcNum;

    /*
     * figure out total encoded length of oid
     */
    tmpArcNum = headArcNum;
    for (totalLen = 1; (tmpArcNum >>= 7) != 0; totalLen++)
	;
    for (tmpOid = oid->next->next; tmpOid != NULL; tmpOid = tmpOid->next)
    {
        totalLen++;
        tmpArcNum = tmpOid->arcNum;
        for (; (tmpArcNum >>= 7) != 0; totalLen++)
	    ;
    }

    return totalLen;

}  /* EncodedOidLen */


/*
 * given an oid list and a pre-allocated ENC_OID
 * (use EncodedOidLen to figure out byte length needed)
 * fills the ENC_OID with a BER encoded version
 * of the oid.
 */
void
BuildEncodedOid PARAMS ((oid, result),
    OID *oid _AND_
    AsnOid *result)
{
    unsigned long len;
    unsigned long headArcNum;
    unsigned long tmpArcNum;
    char         *buf;
    int           i;
    OID          *tmpOid;

    buf = result->octs;

    /*
     * oid must have at least 2 elmts
     */
    if (oid->next == NULL)
       return;
    /*
     * munge together first two arcNum
     * note first arcnum must be <= 2
     * and second must be < 39 if first = 0 or 1
     * see (X.209) for ref to this stupidity
     */
    headArcNum = (oid->arcNum * 40) + oid->next->arcNum;

    tmpArcNum = headArcNum;

    /*
     * calc # bytes needed for head arc num
     */
    for (len = 0; (tmpArcNum >>= 7) != 0; len++)
	;

    /*
     * write more signifcant bytes (if any) of head arc num
     * with 'more' bit set
     */
    for (i=0; i < len; i++)
        *(buf++) = 0x80 | (headArcNum >> ((len-i)*7));

    /*
     * write least significant byte of head arc num
     */
    *(buf++) = 0x7f & headArcNum;


    /*
     * write following arc nums, if any
     */
    for (tmpOid = oid->next->next; tmpOid != NULL; tmpOid = tmpOid->next)
    {
        /*
         * figure out encoded length -1 of this arcNum
         */
        tmpArcNum = tmpOid->arcNum;
        for (len = 0; (tmpArcNum >>= 7) != 0; len++)
	    ;


        /*
         * write more signifcant bytes (if any)
         * with 'more' bit set
         */
        for (i=0; i < len; i++)
            *(buf++) = 0x80 | (tmpOid->arcNum >> ((len-i)*7));

        /*
         * write least significant byte
         */
        *(buf++) = 0x7f & tmpOid->arcNum;
    }

} /* BuildEncodedOid */


/*
 * convert an AsnOid into an OID (linked list)
 * NOT RECOMMENDED for use in protocol implementations
 */
void
UnbuildEncodedOid PARAMS ((eoid, result),
    AsnOid *eoid _AND_
    OID **result)
{
    OID **nextOid;
    OID *headOid;
    int arcNum;
    int i;
    int firstArcNum;
    int secondArcNum;

    for (arcNum = 0, i=0; (i < eoid->octetLen) && (eoid->octs[i] & 0x80);i++)
        arcNum = (arcNum << 7) + (eoid->octs[i] & 0x7f);

    arcNum = (arcNum << 7) + (eoid->octs[i] & 0x7f);
    i++;

    firstArcNum = arcNum / 40;
    if (firstArcNum > 2)
        firstArcNum = 2;

    secondArcNum = arcNum - (firstArcNum * 40);

    headOid = (OID*)malloc (sizeof (OID));
    headOid->arcNum = firstArcNum;
    headOid->next = (OID*)malloc (sizeof (OID));
    headOid->next->arcNum = secondArcNum;
    nextOid = &headOid->next->next;

    for (; i < eoid->octetLen; )
    {
        for (arcNum = 0; (i < eoid->octetLen) && (eoid->octs[i] & 0x80); i++)
            arcNum = (arcNum << 7) + (eoid->octs[i] & 0x7f);

        arcNum = (arcNum << 7) + (eoid->octs[i] & 0x7f);
        i++;
        *nextOid = (OID*)malloc (sizeof (OID));
        (*nextOid)->arcNum = arcNum;
        nextOid = &(*nextOid)->next;
    }

    *result = headOid;

} /* UnbuildEncodedOid */
