LCOV - code coverage report
Current view: top level - gcr - gcr-simple-certificate.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 37 37 100.0 %
Date: 2022-09-04 10:20:22 Functions: 11 11 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 16 28 57.1 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * gnome-keyring
       3                 :            :  *
       4                 :            :  * Copyright (C) 2008 Stefan Walter
       5                 :            :  *
       6                 :            :  * This program is free software; you can redistribute it and/or modify
       7                 :            :  * it under the terms of the GNU Lesser General Public License as
       8                 :            :  * published by the Free Software Foundation; either version 2.1 of
       9                 :            :  * the License, or (at your option) any later version.
      10                 :            :  *
      11                 :            :  * This program is distributed in the hope that it will be useful, but
      12                 :            :  * WITHOUT ANY WARRANTY; without even the implied warranty of
      13                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14                 :            :  * Lesser General Public License for more details.
      15                 :            :  *
      16                 :            :  * You should have received a copy of the GNU Lesser General Public
      17                 :            :  * License along with this program; if not, see <http://www.gnu.org/licenses/>.
      18                 :            :  */
      19                 :            : 
      20                 :            : #include "config.h"
      21                 :            : 
      22                 :            : #include "gcr-certificate.h"
      23                 :            : #include "gcr-internal.h"
      24                 :            : #include "gcr-simple-certificate.h"
      25                 :            : 
      26                 :            : #include <string.h>
      27                 :            : 
      28                 :            : /**
      29                 :            :  * GcrSimpleCertificate:
      30                 :            :  *
      31                 :            :  * An implementation of [iface@Certificate] which loads a certificate from DER
      32                 :            :  * data already located in memory.
      33                 :            :  *
      34                 :            :  * To create an object, use the [ctor@SimpleCertificate.new] or
      35                 :            :  * [ctor@SimpleCertificate.new_static] functions.
      36                 :            :  */
      37                 :            : 
      38                 :            : struct _GcrSimpleCertificatePrivate {
      39                 :            :         GBytes *bytes;
      40                 :            : };
      41                 :            : 
      42                 :            : /* Forward declarations */
      43                 :            : static void gcr_simple_certificate_iface_init (GcrCertificateIface *iface);
      44                 :            : 
      45   [ +  +  +  -  :        367 : G_DEFINE_TYPE_WITH_CODE (GcrSimpleCertificate, gcr_simple_certificate, G_TYPE_OBJECT,
                   +  + ]
      46                 :            :         G_ADD_PRIVATE (GcrSimpleCertificate);
      47                 :            :         G_IMPLEMENT_INTERFACE (GCR_TYPE_CERTIFICATE, gcr_simple_certificate_iface_init);
      48                 :            : );
      49                 :            : 
      50                 :            : /* -----------------------------------------------------------------------------
      51                 :            :  * OBJECT
      52                 :            :  */
      53                 :            : 
      54                 :            : static void
      55                 :         88 : gcr_simple_certificate_init (GcrSimpleCertificate *self)
      56                 :            : {
      57                 :         88 :         self->pv = gcr_simple_certificate_get_instance_private (self);
      58                 :         88 : }
      59                 :            : 
      60                 :            : static void
      61                 :         88 : gcr_simple_certificate_real_finalize (GObject *obj)
      62                 :            : {
      63                 :         88 :         GcrSimpleCertificate *self = GCR_SIMPLE_CERTIFICATE (obj);
      64                 :            : 
      65         [ +  - ]:         88 :         g_clear_pointer (&self->pv->bytes, g_bytes_unref);
      66                 :            : 
      67                 :         88 :         G_OBJECT_CLASS (gcr_simple_certificate_parent_class)->finalize (obj);
      68                 :         88 : }
      69                 :            : 
      70                 :            : static void
      71                 :          5 : gcr_simple_certificate_class_init (GcrSimpleCertificateClass *klass)
      72                 :            : {
      73                 :          5 :         GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
      74                 :            : 
      75                 :          5 :         gobject_class->finalize = gcr_simple_certificate_real_finalize;
      76                 :          5 :         gobject_class->get_property = gcr_certificate_mixin_get_property;
      77                 :            : 
      78                 :          5 :         gcr_certificate_mixin_class_init (gobject_class);
      79                 :          5 :         _gcr_initialize_library ();
      80                 :          5 : }
      81                 :            : 
      82                 :            : static const guchar *
      83                 :         87 : gcr_simple_certificate_get_der_data (GcrCertificate *cert,
      84                 :            :                                      gsize *n_data)
      85                 :            : {
      86                 :         87 :         GcrSimpleCertificate *self = GCR_SIMPLE_CERTIFICATE (cert);
      87                 :            : 
      88   [ -  +  +  -  :         87 :         g_return_val_if_fail (GCR_IS_CERTIFICATE (self), NULL);
             -  +  -  + ]
      89         [ -  + ]:         87 :         g_return_val_if_fail (n_data, NULL);
      90         [ -  + ]:         87 :         g_return_val_if_fail (self->pv->bytes, NULL);
      91                 :            : 
      92                 :            :         /* This is called when we're not a base class */
      93                 :         87 :         return g_bytes_get_data (self->pv->bytes, n_data);
      94                 :            : }
      95                 :            : 
      96                 :            : static void
      97                 :          5 : gcr_simple_certificate_iface_init (GcrCertificateIface *iface)
      98                 :            : {
      99                 :          5 :         iface->get_der_data = gcr_simple_certificate_get_der_data;
     100                 :          5 : }
     101                 :            : 
     102                 :            : /* -----------------------------------------------------------------------------
     103                 :            :  * PUBLIC
     104                 :            :  */
     105                 :            : 
     106                 :            : /**
     107                 :            :  * gcr_simple_certificate_new:
     108                 :            :  * @data: (array length=n_data): the raw DER certificate data
     109                 :            :  * @n_data: The length of @data
     110                 :            :  *
     111                 :            :  * Create a new #GcrSimpleCertificate for the raw DER data. The @data memory is
     112                 :            :  * copied so you can dispose of it after this function returns.
     113                 :            :  *
     114                 :            :  * Returns: (transfer full) (type Gcr.SimpleCertificate): a new #GcrSimpleCertificate
     115                 :            :  */
     116                 :            : GcrCertificate *
     117                 :         82 : gcr_simple_certificate_new (const guchar *data,
     118                 :            :                             gsize n_data)
     119                 :            : {
     120                 :            :         GcrSimpleCertificate *cert;
     121                 :            : 
     122         [ -  + ]:         82 :         g_return_val_if_fail (data, NULL);
     123         [ -  + ]:         82 :         g_return_val_if_fail (n_data, NULL);
     124                 :            : 
     125                 :         82 :         cert = g_object_new (GCR_TYPE_SIMPLE_CERTIFICATE, NULL);
     126                 :         82 :         cert->pv->bytes = g_bytes_new (data, n_data);
     127                 :         82 :         return GCR_CERTIFICATE (cert);
     128                 :            : }
     129                 :            : 
     130                 :            : /**
     131                 :            :  * gcr_simple_certificate_new_static: (skip)
     132                 :            :  * @data: (array length=n_data): The raw DER certificate data
     133                 :            :  * @n_data: The length of @data
     134                 :            :  *
     135                 :            :  * Create a new #GcrSimpleCertificate for the raw DER data. The @data memory is
     136                 :            :  * not copied and must persist until the #GcrSimpleCertificate object is
     137                 :            :  * destroyed.
     138                 :            :  *
     139                 :            :  * Returns: (transfer full) (type Gcr.SimpleCertificate): a new #GcrSimpleCertificate
     140                 :            :  */
     141                 :            : GcrCertificate *
     142                 :          6 : gcr_simple_certificate_new_static (const guchar *data,
     143                 :            :                                    gsize n_data)
     144                 :            : {
     145                 :            :         GcrSimpleCertificate *cert;
     146                 :            : 
     147         [ -  + ]:          6 :         g_return_val_if_fail (data, NULL);
     148         [ -  + ]:          6 :         g_return_val_if_fail (n_data, NULL);
     149                 :            : 
     150                 :          6 :         cert = g_object_new (GCR_TYPE_SIMPLE_CERTIFICATE, NULL);
     151                 :          6 :         cert->pv->bytes = g_bytes_new_static (data, n_data);
     152                 :          6 :         return GCR_CERTIFICATE (cert);
     153                 :            : }

Generated by: LCOV version 1.14