Coverage Report

Created: 2025-08-26 06:38

/src/opensips/md5utils.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (C) 2007 1&1 Internet AG
3
 * Copyright (C) 2001-2003 FhG Fokus
4
 *
5
 * This file is part of opensips, a free SIP server.
6
 *
7
 * opensips is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 2 of the License, or
10
 * (at your option) any later version
11
 *
12
 * opensips is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
20
 */
21
22
/*!
23
 * \file
24
 * \brief OpenSIPS MD5 handling functions
25
 */
26
27
28
#include <stdio.h>
29
#include <sys/stat.h>
30
31
#include "md5global.h"
32
#include "md5.h"
33
#include "md5utils.h"
34
#include "dprint.h"
35
#include "ut.h"
36
37
38
/*! \brief
39
  * Calculate a MD5 digests over a string array and stores
40
  * the result in the destination char array.
41
  * This function assumes 32 bytes in the destination buffer.
42
  * \param dest destination
43
  * \param src string input array
44
  * \param size elements in the input array
45
  */
46
void MD5StringArray(char *dest, str src[], unsigned int size)
47
0
{
48
0
  MD5_CTX context;
49
0
  char digest[16];
50
0
  int i, len;
51
0
  char *tmp;
52
53
0
  MD5Init (&context);
54
0
  for (i=0; i < size; i++) {
55
0
    trim_len(len, tmp, src[i]);
56
0
    MD5Update(&context, tmp, len);
57
0
  }
58
0
  MD5Final(digest, &context);
59
60
0
  string2hex(digest, 16, dest);
61
0
  LM_DBG("MD5 calculated: %.*s\n", MD5_LEN, dest);
62
0
}
63
64
/*! \brief
65
  * Calculate a MD5 digest over a file.
66
  * This function assumes 32 bytes in the destination buffer.
67
  * \param dest destination
68
  * \param file_name file for that the digest should be calculated
69
  * \return zero on success, negative on errors
70
  */
71
int MD5File(char *dest, const char *file_name)
72
0
{
73
0
  if (!dest || !file_name) {
74
0
    LM_ERR("invalid parameter value\n");
75
0
    return -1;
76
0
  }
77
78
0
  MD5_CTX context;
79
0
  FILE *input;
80
0
  char buffer[32768];
81
0
  char hash[16];
82
0
  unsigned int counter, size;
83
84
0
  struct stat stats;
85
0
    if (stat(file_name, &stats) != 0) {
86
0
    LM_ERR("could not stat file %s\n", file_name);
87
0
    return -1;
88
0
  }
89
0
  size = stats.st_size;
90
91
0
  MD5Init(&context);
92
0
  if((input = fopen(file_name, "rb")) == NULL) {
93
0
    LM_ERR("could not open file %s\n", file_name);
94
0
    return -1;
95
0
  }
96
97
0
  while(size) {
98
0
    counter = (size > sizeof(buffer)) ? sizeof(buffer) : size;
99
0
    if ((counter = fread(buffer, 1, counter, input)) <= 0) {
100
0
      fclose(input);
101
0
      return -1;
102
0
    }
103
0
    MD5Update(&context, buffer, counter);
104
0
    size -= counter;
105
0
  }
106
0
  fclose(input);
107
0
  MD5Final(hash, &context);
108
109
0
  string2hex(hash, 16, dest);
110
0
  LM_DBG("MD5 calculated: %.*s for file %s\n", MD5_LEN, dest, file_name);
111
112
0
  return 0;
113
0
}