LCOV - code coverage report
Current view: top level - lib/asn1 - ASN1OID.cpp (source / functions) Hit Total Coverage
Test: lcov_final.info Lines: 28 39 71.8 %
Date: 2024-02-15 08:20:41 Functions: 5 6 83.3 %

          Line data    Source code
       1             : /*
       2             :  *
       3             :  *    Copyright (c) 2020-2021 Project CHIP Authors
       4             :  *    Copyright (c) 2013-2017 Nest Labs, Inc.
       5             :  *    All rights reserved.
       6             :  *
       7             :  *    Licensed under the Apache License, Version 2.0 (the "License");
       8             :  *    you may not use this file except in compliance with the License.
       9             :  *    You may obtain a copy of the License at
      10             :  *
      11             :  *        http://www.apache.org/licenses/LICENSE-2.0
      12             :  *
      13             :  *    Unless required by applicable law or agreed to in writing, software
      14             :  *    distributed under the License is distributed on an "AS IS" BASIS,
      15             :  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      16             :  *    See the License for the specific language governing permissions and
      17             :  *    limitations under the License.
      18             :  */
      19             : 
      20             : /**
      21             :  *    @file
      22             :  *      This file implements methods for manipulating for writing
      23             :  *      Abstract Syntax Notation One (ASN.1) Object Identifiers
      24             :  *      (OIDs).
      25             :  *
      26             :  */
      27             : 
      28             : #include <stdint.h>
      29             : #include <stdlib.h>
      30             : #include <string.h>
      31             : 
      32             : #include <lib/support/DLLUtil.h>
      33             : #include <lib/support/SafeInt.h>
      34             : 
      35             : #define ASN1_DEFINE_OID_TABLE
      36             : #define ASN1_DEFINE_OID_NAME_TABLE
      37             : #include <lib/asn1/ASN1.h>
      38             : 
      39             : namespace chip {
      40             : namespace ASN1 {
      41             : 
      42        1519 : DLL_EXPORT OID ParseObjectID(const uint8_t * encodedOID, uint16_t encodedOIDLen)
      43             : {
      44        1519 :     if (encodedOID == nullptr or encodedOIDLen == 0)
      45             :     {
      46           0 :         return kOID_NotSpecified;
      47             :     }
      48             : 
      49       31095 :     for (const auto & tableEntry : sOIDTable)
      50             :     {
      51       31018 :         if (encodedOIDLen == tableEntry.EncodedOIDLen && memcmp(encodedOID, tableEntry.EncodedOID, encodedOIDLen) == 0)
      52             :         {
      53        1442 :             return tableEntry.EnumVal;
      54             :         }
      55             :     }
      56             : 
      57          77 :     return kOID_Unknown;
      58             : }
      59             : 
      60       33604 : bool GetEncodedObjectID(OID oid, const uint8_t *& encodedOID, uint16_t & encodedOIDLen)
      61             : {
      62      670863 :     for (const auto & tableEntry : sOIDTable)
      63             :     {
      64      670854 :         if (oid == tableEntry.EnumVal)
      65             :         {
      66       33595 :             encodedOID    = tableEntry.EncodedOID;
      67       33595 :             encodedOIDLen = tableEntry.EncodedOIDLen;
      68       33595 :             return true;
      69             :         }
      70             :     }
      71             : 
      72           9 :     return false;
      73             : }
      74             : 
      75        1101 : OIDCategory GetOIDCategory(OID oid)
      76             : {
      77        1101 :     if (oid == kOID_Unknown)
      78             :     {
      79           3 :         return kOIDCategory_Unknown;
      80             :     }
      81        1098 :     if (oid == kOID_NotSpecified)
      82             :     {
      83           0 :         return kOIDCategory_NotSpecified;
      84             :     }
      85        1098 :     return static_cast<OIDCategory>(oid & kOIDCategory_Mask);
      86             : }
      87             : 
      88           0 : const char * GetOIDName(OID oid)
      89             : {
      90           0 :     if (oid == kOID_Unknown)
      91             :     {
      92           0 :         return "Unknown";
      93             :     }
      94           0 :     if (oid == kOID_NotSpecified)
      95             :     {
      96           0 :         return "NotSpecified";
      97             :     }
      98           0 :     for (const auto & tableEntry : sOIDNameTable)
      99             :     {
     100           0 :         if (oid == tableEntry.EnumVal)
     101             :         {
     102           0 :             return tableEntry.Name;
     103             :         }
     104             :     }
     105           0 :     return "Unknown";
     106             : }
     107             : 
     108        1519 : CHIP_ERROR ASN1Reader::GetObjectId(OID & oid)
     109             : {
     110        1519 :     ReturnErrorCodeIf(Value == nullptr, ASN1_ERROR_INVALID_STATE);
     111        1519 :     ReturnErrorCodeIf(ValueLen < 1, ASN1_ERROR_INVALID_ENCODING);
     112        1519 :     ReturnErrorCodeIf(mElemStart + mHeadLen + ValueLen > mContainerEnd, ASN1_ERROR_UNDERRUN);
     113        1519 :     VerifyOrReturnError(CanCastTo<uint16_t>(ValueLen), ASN1_ERROR_INVALID_ENCODING);
     114        1519 :     oid = ParseObjectID(Value, static_cast<uint16_t>(ValueLen));
     115        1519 :     return CHIP_NO_ERROR;
     116             : }
     117             : 
     118       33604 : CHIP_ERROR ASN1Writer::PutObjectId(OID oid)
     119             : {
     120             :     const uint8_t * encodedOID;
     121             :     uint16_t encodedOIDLen;
     122             : 
     123       33604 :     VerifyOrReturnError(GetEncodedObjectID(oid, encodedOID, encodedOIDLen), ASN1_ERROR_UNKNOWN_OBJECT_ID);
     124             : 
     125       33595 :     return PutObjectId(encodedOID, encodedOIDLen);
     126             : }
     127             : 
     128             : } // namespace ASN1
     129             : } // namespace chip

Generated by: LCOV version 1.14