asn1-ut-objid-decode.cpp revision e5df77e70b59a10327e0d2bdd21af0a5c8a551e2
/* $Id$ */
/** @file
* IPRT - ASN.1, OBJECT IDENTIFIER Type, Decoder.
*/
/*
* 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 *
*******************************************************************************/
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
DECLHIDDEN(int) rtAsn1ObjId_InternalFormatComponent(uint32_t uValue, char **ppszObjId, size_t *pcbObjId); /* asn1-ut-objid.cpp */
/**
* Internal worker for RTAsn1ObjId_DecodeAsn1 that formats a component, with a
* leading dot.
*
* @returns VBox status code (caller complains on failure).
* @param uValue The component ID value.
* @param ppszObjId Pointer to the output buffer pointer.
* @param pcbObjId Pointer to the remaining size of the output buffer.
*/
DECLHIDDEN(int) rtAsn1ObjId_InternalFormatComponent(uint32_t uValue, char **ppszObjId, size_t *pcbObjId)
{
/*
* Format the number backwards.
*/
char szTmp[32];
*psz = '\0';
do
{
uValue /= 10;
} while (uValue > 0);
/*
* Do we have enough space?
* We add a dot and save space for the terminator.
*/
{
*pszObjId = '.';
return VINF_SUCCESS;
}
AssertFailed();
}
/**
* Reads one object ID component, returning it's value and encoded length.
*
* @returns The encoded length (positive) on success, negative IPRT status code
* on failure.
* @param pbContent The start of the component to parse.
* @param cbContent The number of content bytes left.
* @param puValue Where to return the value.
*/
static int rtAsn1ObjId_ReadComponent(uint8_t const *pbContent, uint32_t cbContent, uint32_t *puValue)
{
if (cbContent >= 1)
{
/* The first byte. */
if (!(b & 0x80))
{
*puValue = b;
return 1;
}
/* Encoded as more than one byte. Make sure that it's efficently
encoded as 8.19.2 indicates it must. */
if (b != 0x80)
{
{
uValue <<= 7;
uValue |= b & 0x7f;
if (!(b & 0x80))
{
return (int)off;
}
}
}
return VERR_ASN1_INVALID_OBJID_ENCODING;
}
return VERR_NO_DATA;
}
/**
* This function parses the binary content of an OBJECT IDENTIFIER, check the
* encoding as well as calculating the storage requirements.
*
* @returns IPRT status code
* @param pbContent Pointer to the content.
* @param cbContent The length of the content.
* @param pCursor The cursor (for error reporting).
* @param pszErrorTag The error tag.
* @param pcComponents Where to return the component count.
* @param pcchObjId Where to return the length of the dotted string
* representation.
*/
static int rtAsn1ObjId_PreParse(uint8_t const *pbContent, uint32_t cbContent, PRTASN1CURSOR pCursor, const char *pszErrorTag,
{
int rc;
{
/*
* Decode the first two numbers. Monkey business: X*40 + Y
* Where X is the first number, X in {0,1,2}, and Y is the second
* one. The range of Y is {0,...,39} for X in {0,1}, but has a
* free range for X = 2.
*/
if (rc > 0)
{
do
{
cComponents++;
/* Figure the encoded string length, binary search fashion. */
if (uValue < 10000)
{
if (uValue < 100)
{
if (uValue < 10)
else
}
else
{
if (uValue < 1000)
else
}
}
else
{
if (uValue < 1000000)
{
if (uValue < 100000)
else
}
else
{
if (uValue < 10000000)
else if (uValue < 100000000)
else
}
}
/* advance. */
if (!cbContent)
{
if (cComponents < 128)
{
{
return VINF_SUCCESS;
}
"Object ID has a too long string form: %#x (max %#x)",
}
"Object ID has too many components: %#x (max 127)", cComponents);
}
/* next */
} while (rc > 0);
}
}
else if (cbContent)
rc = RTAsn1CursorSetInfo(pCursor, VERR_ASN1_INVALID_OBJID_ENCODING, "Object ID content is loo long: %#x", cbContent);
else
rc = RTAsn1CursorSetInfo(pCursor, VERR_ASN1_INVALID_OBJID_ENCODING, "Zero length object ID content");
return rc;
}
RTDECL(int) RTAsn1ObjId_DecodeAsn1(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1OBJID pThis, const char *pszErrorTag)
{
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
/*
* Validate and count things first.
*/
rc = rtAsn1ObjId_PreParse(pCursor->pbCur, pThis->Asn1Core.cb, pCursor, pszErrorTag, &cComponents, &cchObjId);
if (RT_SUCCESS(rc))
{
/*
* Allocate memory for the components array, either out of the
* string buffer or off the heap.
*/
#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)];
else
#endif
if (RT_SUCCESS(rc))
{
/*
* Deal with the two first components first since they are
* encoded in a weird way to save a byte.
*/
if (RT_SUCCESS(rc))
{
if (uValue < 80)
{
}
else
{
pauComponents[0] = 2;
}
if (RT_SUCCESS(rc))
{
/*
* The other components are encoded in less complicated manner.
*/
{
pauComponents[i] = uValue;
}
if (RT_SUCCESS(rc))
{
*pszObjId = '\0';
return VINF_SUCCESS;
}
}
}
}
}
}
}
return rc;
}
/*
* 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-asn1-decoder.h>