asn1-ut-time-decode.cpp revision d0a1dd60cd22f0f9de6fb8f2889a9dc75480338b
/* $Id$ */
/** @file
* IPRT - ASN.1, UTC TIME and GENERALIZED TIME Types, Decoding.
*/
/*
* 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 *
*******************************************************************************/
/**
* Common code for UTCTime and GeneralizedTime converters that normalizes the
* converted time and checks that the input values doesn't change.
*
* @returns IPRT status code.
* @param pCursor The cursor to use when reporting an error.
* @param pThis The time to normalize and check.
* @param pszType The type name.
* @param pszErrorTag The error tag.
*/
static int rtAsn1Time_NormalizeTime(PRTASN1CURSOR pCursor, PRTASN1TIME pThis, const char *pszType, const char *pszErrorTag)
{
int rc;
{
{
return VINF_SUCCESS;
"%s: Normalized result not the same as %s: '%.*s'",
}
else
"%s: RTTimeNormalize failed on %s: '%.*s'",
}
else
"%s: Bad %s values: '%.*s'; mth=%u h=%u min=%u sec=%u",
return rc;
}
/**
* Converts the UTCTime string into an the RTTIME member of RTASN1TIME.
*
* @returns IPRT status code.
* @param pCursor The cursor to use when reporting an error.
* @param pThis The time to parse.
* @param pszErrorTag The error tag.
*/
static int rtAsn1Time_ConvertUTCTime(PRTASN1CURSOR pCursor, PRTASN1TIME pThis, const char *pszErrorTag)
{
/*
* While the current spec says the seconds field is not optional, this
* restriction was added later on. So, when parsing UTCTime we must deal
* with it being absent.
*/
int rc;
{
/* Basic encoding validation. */
&& ( !fHaveSeconds
)
{
/* Basic conversion. */
if (fHaveSeconds)
else
/* Check the convered data and normalize the time structure. */
if (RT_SUCCESS(rc))
return rc;
}
else
rc = RTAsn1CursorSetInfo(pCursor, VERR_ASN1_INVALID_UTC_TIME_ENCODING, "%s: Bad UTCTime encoding: '%.*s'",
}
else
rc = RTAsn1CursorSetInfo(pCursor, VERR_ASN1_INVALID_UTC_TIME_ENCODING, "%s: Bad UTCTime length: %#x",
return rc;
}
/**
* Converts the fraction part of a generalized time into nanoseconds.
*
* @returns IPRT status code.
* @param pCursor The cursor to use when reporting an error.
* @param pchFraction Pointer to the start of the fraction (dot).
* @param cchFraction The length of the fraction.
* @param pThis The time object we're working on,
* Time.u32Nanoseconds will be update.
* @param pszErrorTag The error tag.
*/
static int rtAsn1Time_ConvertGeneralizedTimeFraction(PRTASN1CURSOR pCursor, const char *pchFraction, uint32_t cchFraction,
{
/*
* Check the dot.
*/
if (*pchFraction != '.')
"%s: Expected GeneralizedTime fraction dot, found: '%c' ('%.*s')",
pchFraction++;
cchFraction--;
if (!cchFraction)
"%s: No digit following GeneralizedTime fraction dot: '%.*s'",
/*
* Do the conversion.
*/
char chLastDigit;
do
{
if (!RT_C_IS_DIGIT(chDigit))
"%s: Bad GeneralizedTime fraction digit: '%.*s'",
/* Advance */
cchFraction--;
pchFraction++;
uMult /= 10;
} while (cchFraction > 0 && uMult > 0);
/*
* Lazy bird: For now, we don't permit higher resolution than we can
* internally represent. Deal with this if it ever becomes an issue.
*/
if (cchFraction > 0)
"%s: Bad GeneralizedTime fraction too long: '%.*s'",
if (chLastDigit == '0')
"%s: Trailing zeros not allowed for GeneralizedTime: '%.*s'",
return VINF_SUCCESS;
}
/**
* Converts the GeneralizedTime string into an the RTTIME member of RTASN1TIME.
*
* @returns IPRT status code.
* @param pCursor The cursor to use when reporting an error.
* @param pThis The time to parse.
* @param pszErrorTag The error tag.
*/
static int rtAsn1Time_ConvertGeneralizedTime(PRTASN1CURSOR pCursor, PRTASN1TIME pThis, const char *pszErrorTag)
{
int rc;
{
/* Basic encoding validation. */
)
{
/* Basic conversion. */
/* Optional fraction part. */
rc = VINF_SUCCESS;
if (cchLeft > 0)
rc = rtAsn1Time_ConvertGeneralizedTimeFraction(pCursor, pachTime + 14, cchLeft, pThis, pszErrorTag);
/* Check the convered data and normalize the time structure. */
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
return VINF_SUCCESS;
}
}
else
"%s: Bad GeneralizedTime encoding: '%.*s'",
}
else
"%s: Bad GeneralizedTime length: %#x",
return rc;
}
RTDECL(int) RTAsn1Time_DecodeAsn1(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1TIME pThis, const char *pszErrorTag)
{
if (RT_SUCCESS(rc))
{
{
{
}
{
}
rc = RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_TAG_MISMATCH, "%s: Not UTCTime nor GeneralizedTime: uTag=%#x",
}
else
"%s: Not UTCTime nor GeneralizedTime: fClass=%#x / uTag=%#x",
}
return rc;
}
RTDECL(int) RTAsn1UtcTime_DecodeAsn1(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1TIME pThis, const char *pszErrorTag)
{
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
}
}
return rc;
}
RTDECL(int) RTAsn1GeneralizedTime_DecodeAsn1(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1TIME pThis, const char *pszErrorTag)
{
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
}
}
return rc;
}
/*
* Generate code for the associated collection types.
*/
#define RTASN1TMPL_TEMPLATE_FILE "../common/asn1/asn1-ut-time-template.h"
#include <iprt/asn1-generator-internal-header.h>
#include <iprt/asn1-generator-asn1-decoder.h>