LCOV - code coverage report
Current view: top level - src - common.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 16 16 100.0 %
Date: 2022-06-17 11:31:56 Functions: 3 3 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 5 8 62.5 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * common.c: Utility functions for libcacard
       3                 :            :  *
       4                 :            :  * Copyright (C) 2016 - 2018 Red Hat, Inc.
       5                 :            :  *
       6                 :            :  * Authors: Robert Relyea <rrelyea@redhat.com>
       7                 :            :  *          Jakub Jelen <jjelen@redhat.com>
       8                 :            :  *
       9                 :            :  * This library is free software; you can redistribute it and/or
      10                 :            :  * modify it under the terms of the GNU Lesser General Public
      11                 :            :  * License as published by the Free Software Foundation; either
      12                 :            :  * version 2.1 of the License, or (at your option) any later version.
      13                 :            :  *
      14                 :            :  * This library is distributed in the hope that it will be useful,
      15                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      16                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      17                 :            :  * Lesser General Public License for more details.
      18                 :            :  *
      19                 :            :  * You should have received a copy of the GNU Lesser General Public
      20                 :            :  * License along with this library; if not, write to the Free Software
      21                 :            :  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
      22                 :            :  */
      23                 :            : 
      24                 :            : #include "config.h"
      25                 :            : 
      26                 :            : #include <stdio.h>
      27                 :            : #include <string.h>
      28                 :            : 
      29                 :            : #include "vcard.h"
      30                 :            : #include "common.h"
      31                 :            : 
      32                 :            : unsigned char *
      33                 :         64 : ushort2lebytes(unsigned char *buf, unsigned short x)
      34                 :            : {
      35         [ +  - ]:         64 :     if (buf != NULL) {
      36                 :         64 :         buf[0] = (unsigned char) (x & 0xff);
      37                 :         64 :         buf[1] = (unsigned char) ((x >> 8) & 0xff);
      38                 :            :     }
      39                 :         64 :     return buf;
      40                 :            : }
      41                 :            : 
      42                 :            : unsigned short
      43                 :          1 : lebytes2ushort(const unsigned char *buf)
      44                 :            : {
      45         [ +  - ]:          1 :     if (buf == NULL)
      46                 :            :         return 0U;
      47                 :          1 :     return (unsigned short)buf[1] << 8 | (unsigned short)buf[0];
      48                 :            : }
      49                 :            : 
      50                 :            : #define MAX_STATIC_BYTES 1024
      51                 :            : static char hexdump_buffer[5*MAX_STATIC_BYTES + 1];
      52                 :            : /*
      53                 :            :  * Creates printable representation in hexadecimal format of the data
      54                 :            :  * provided in the  buf  buffer. A static buffer will be used, which
      55                 :            :  * can hold up to 1024 bytes (longer will get truncated).
      56                 :            :  *
      57                 :            :  * The dumping loop will print 5 visible characters at a time, but since it's
      58                 :            :  * using sprintf, we also need to account for the '\0' it appends to the end of
      59                 :            :  * the string on the last iteration, or we'll overflow the buffer we are
      60                 :            :  * printing to.
      61                 :            :  */
      62                 :            : char *
      63                 :        115 : hex_dump(const unsigned char *buf, size_t buflen)
      64                 :            : {
      65                 :            :     char *p, *start;
      66                 :            :     size_t i;
      67                 :            : 
      68         [ +  - ]:        115 :     if (buflen <= 0)
      69                 :            :         return NULL;
      70                 :            : 
      71                 :            :     start = hexdump_buffer;
      72                 :        115 :     buflen = MIN(buflen, MAX_STATIC_BYTES);
      73                 :            : 
      74                 :            :     p = start;
      75         [ +  + ]:      11658 :     for (i = 0; i < buflen; i++) {
      76                 :      11543 :         sprintf(p, "0x%02X ", buf[i]);
      77                 :      11543 :         p += 5;
      78                 :            :     }
      79                 :            :     /* terminate */
      80                 :        115 :     *--p = '\x00';
      81                 :        115 :     return start;
      82                 :            : }
      83                 :            : 
      84                 :            : #if !GLIB_CHECK_VERSION(2,68,0)
      85                 :            : void* g_memdup2(const void *ptr, size_t size)
      86                 :            : {
      87                 :            :     void *dst = NULL;
      88                 :            : 
      89                 :            :     if (ptr && size != 0) {
      90                 :            :         dst = g_malloc(size);
      91                 :            :         memcpy(dst, ptr, size);
      92                 :            :     }
      93                 :            :     return dst;
      94                 :            : }
      95                 :            : #endif
      96                 :            : 
      97                 :            : /* vim: set ts=4 sw=4 tw=0 noet expandtab: */

Generated by: LCOV version 1.14