asn1-ut-objid.cpp revision 13493ab7596e827b8d0caab2c89e635dd65f78f9
/* $Id$ */
/** @file
* IPRT - ASN.1, OBJECT IDENTIFIER Type.
*/
/*
* Copyright (C) 2006-2014 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
/*******************************************************************************
* Global Variables *
*******************************************************************************/
static char const g_szDefault[] = "2.16.840.1.113894";
static uint8_t const g_abDefault[] =
{
2*40 + 16, 0x80 | (840 >> 7), 840 & 0x7f, 1, 0x80 | (113894 >> 14), 0x80 | ((113894 >> 7) & 0x7f), 113894 & 0x7f
};
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
DECLHIDDEN(int) rtAsn1ObjId_InternalFormatComponent(uint32_t uValue, char **ppszObjId, size_t *pcbObjId); /* asn1-ut-objid.cpp */
/** @todo check if we really need this. */
/*
* ASN.1 OBJECT IDENTIFIER - Special Methods.
*/
/**
* Encodes the ASN.1 byte sequence for a set of components.
*
* @returns IPRT status code.
* @param cComponents The number of components. Must be at least two.
* @param pauComponents The components array.
* @param pbEncoded The output buffer.
* @param pcbEncoded On input, this holds the size of the output buffer.
* On successful return it's the encoded size in bytes.
*/
{
/* The first two componets are encoded together to save a byte, so the loop
organization is a little special. */
AssertReturn(pauComponents[1] <= (pauComponents[0] < 2 ? 39 : UINT32_MAX - 80), VERR_ASN1_INTERNAL_ERROR_1);
uint32_t i = 1;
for (;;)
{
if (uValue < 0x80)
{
return VERR_BUFFER_OVERFLOW;
cbLeft -= 1;
}
else if (uValue < 0x4000)
{
return VERR_BUFFER_OVERFLOW;
cbLeft -= 2;
pbCur += 2;
}
else if (uValue < 0x200000)
{
return VERR_BUFFER_OVERFLOW;
cbLeft -= 3;
pbCur += 3;
}
else if (uValue < 0x10000000)
{
return VERR_BUFFER_OVERFLOW;
cbLeft -= 4;
pbCur += 4;
}
else
{
return VERR_BUFFER_OVERFLOW;
cbLeft -= 5;
pbCur += 5;
}
/* Advance / return. */
i++;
if (i >= cComponents)
{
return VINF_SUCCESS;
}
uValue = pauComponents[i];
}
}
RTDECL(int) RTAsn1ObjId_InitFromString(PRTASN1OBJID pThis, const char *pszObjId, PCRTASN1ALLOCATORVTABLE pAllocator)
{
/*
* Check the string, counting the number of components and checking their validity.
*/
/* Special checking of the first component. It has only three valid values: 0,1,2. */
/* The 2nd component. It the first is 0 or 1, it has a max of 39. */
if (chFirst < '2')
{
if (*psz == '.')
{
}
else
{
if (*psz != '.')
}
cComponents++;
}
else
psz--;
/* Subsequent components have max values of UINT32_MAX - 80. */
{
/* Special treatment of the first digit. Need to make sure it isn't an
unnecessary leading 0. */
/* The rest of the digits. */
{
psz++;
}
/* Check the value range. */
cComponents++;
}
/*
* Find space for the component array, either at the unused end of szObjId
* or on the heap.
*/
int rc;
#if 0 /** @todo breaks with arrays of ObjIds or structs containing them. They get resized and repositioned in memory, thus invalidating the pointer. Add recall-pointers callback, or just waste memory? Or maybe make all arrays pointer-arrays? */
{
pThis->pauComponents = (uint32_t *)&pThis->szObjId[sizeof(pThis->szObjId) - cComponents * sizeof(uint32_t)];
rc = VINF_SUCCESS;
}
else
#endif
rc = RTAsn1MemAllocZ(&pThis->Allocation, (void **)&pThis->pauComponents, cComponents * sizeof(uint32_t));
if (RT_SUCCESS(rc))
{
/*
* Fill the elements array.
*/
rc = VINF_SUCCESS;
for (uint32_t i = 0; i < cComponents; i++)
{
if (rc == VWRN_TRAILING_CHARS)
{
pauComponents[i] = uValue;
psz++;
}
else if (rc == VINF_SUCCESS)
{
pauComponents[i] = uValue;
}
else if (RT_FAILURE(rc))
break;
else
{
break;
}
}
{
/*
* Initialize the core structure before we start on the encoded bytes.
*/
/*
* Encode the value into the string buffer. This will NOT overflow
* because the string representation is much less efficient than the
* binary ASN.1 representation (base-10 + separators vs. base-128).
*/
if (RT_SUCCESS(rc))
{
/*
* Now, find a place for the encoded bytes. There might be
* enough room left in the szObjId for it if we're lucky.
*/
#if 0 /** @todo breaks with arrays of ObjIds or structs containing them. They get resized and repositioned in memory, thus invalidating the pointer. Add recall-pointers callback, or just waste memory? Or maybe make all arrays pointer-arrays? */
pThis->Asn1Core.uData.pv = memmove(&pThis->szObjId[cbObjId], &pThis->szObjId[0], pThis->Asn1Core.cb);
else
#endif
if (RT_SUCCESS(rc))
{
/*
* Finally, copy the dotted string.
*/
return VINF_SUCCESS;
}
}
else
{
}
}
else
}
return rc;
}
{
}
{
}
{
return pThis->cComponents;
}
{
return UINT32_MAX;
}
{
}
/*
* ASN.1 OBJECT IDENTIFIER - Standard Methods.
*/
{
"RTAsn1ObjId",
sizeof(RTASN1OBJID),
0,
NULL,
NULL,
};
{
return VINF_SUCCESS;
}
RTDECL(int) RTAsn1ObjId_Clone(PRTASN1OBJID pThis, PCRTASN1OBJID pSrc, PCRTASN1ALLOCATORVTABLE pAllocator)
{
if (RTAsn1ObjId_IsPresent(pSrc))
{
/* Copy the dotted string representation. */
/* Copy the integer component array. Try fit it in the unused space of
the dotted object string buffer. We place it at the end of the
buffer as that is simple alignment wise and avoid wasting bytes that
could be used to sequueze in the content bytes (see below). */
int rc;
#if 0 /** @todo breaks with arrays of ObjIds or structs containing them. They get resized and repositioned in memory, thus invalidating the pointer. Add recall-pointers callback, or just waste memory? Or maybe make all arrays pointer-arrays? */
{
pThis->pauComponents = (uint32_t *)&pThis->szObjId[sizeof(pThis->szObjId) - pSrc->cComponents * sizeof(uint32_t)];
memcpy((uint32_t *)pThis->pauComponents, pSrc->pauComponents, pSrc->cComponents * sizeof(uint32_t));
rc = VINF_SUCCESS;
}
else
#endif
{
}
if (RT_SUCCESS(rc))
{
/* See if we can fit the content value into the szObjId as well.
It will follow immediately after the string as the component
array is the end of the string buffer, when present. */
#if 0 /** @todo breaks with arrays of ObjIds or structs containing them. They get resized and repositioned in memory, thus invalidating the pointer. Add recall-pointers callback, or just waste memory? Or maybe make all arrays pointer-arrays? */
{
if (RT_SUCCESS(rc))
{
return VINF_SUCCESS;
}
}
else
#endif
{
if (RT_SUCCESS(rc))
return VINF_SUCCESS;
}
}
/* failed, clean up. */
return rc;
}
return VINF_SUCCESS;
}
{
if ( pThis
{
}
}
RTDECL(int) RTAsn1ObjId_Enum(PRTASN1OBJID pThis, PFNRTASN1ENUMCALLBACK pfnCallback, uint32_t uDepth, void *pvUser)
{
/* No children to enumerate. */
return VINF_SUCCESS;
}
{
for (uint32_t i = 0; i < cComponents; i++)
return 0;
}
RTDECL(int) RTAsn1ObjId_CheckSanity(PCRTASN1OBJID pThis, uint32_t fFlags, PRTERRINFO pErrInfo, const char *pszErrorTag)
{
return VINF_SUCCESS;
}
/*
* Generate code for the associated collection types.
*/
#define RTASN1TMPL_TEMPLATE_FILE "../common/asn1/asn1-ut-objid-template.h"
#include <iprt/asn1-generator-internal-header.h>
#include <iprt/asn1-generator-core.h>
#include <iprt/asn1-generator-init.h>
#include <iprt/asn1-generator-sanity.h>