Coverage Report

Created: 2023-09-25 07:12

/src/yara/libyara/modules/string/string.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
Copyright (c) 2014-2022. The YARA Authors. All Rights Reserved.
3
4
Redistribution and use in source and binary forms, with or without modification,
5
are permitted provided that the following conditions are met:
6
7
1. Redistributions of source code must retain the above copyright notice, this
8
list of conditions and the following disclaimer.
9
10
2. Redistributions in binary form must reproduce the above copyright notice,
11
this list of conditions and the following disclaimer in the documentation and/or
12
other materials provided with the distribution.
13
14
3. Neither the name of the copyright holder nor the names of its contributors
15
may be used to endorse or promote products derived from this software without
16
specific prior written permission.
17
18
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
22
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
*/
29
30
#include <stdlib.h>
31
#include <errno.h>
32
#include <yara/mem.h>
33
#include <yara/modules.h>
34
#include <yara/strutils.h>
35
#include <yara/utils.h>
36
37
#define MODULE_NAME string
38
39
bool string_to_int(char* s, int base, int64_t* result)
40
0
{
41
0
  char* endp = s;
42
43
0
  errno = 0;
44
0
  *result = strtoll(s, &endp, base);
45
46
0
  if (errno != 0) {
47
    // Error while parsing the string.
48
0
    return false;
49
0
  }
50
0
  if (endp == s) {
51
    // No digits were found.
52
0
    return false;
53
0
  }
54
0
  if (*endp != '\0') {
55
    // Parsing did not reach the end of the string.
56
0
    return false;
57
0
  }
58
59
0
  return true;
60
0
}
61
62
define_function(to_int)
63
0
{
64
0
  char* s = string_argument(1);
65
0
  int64_t result = 0;
66
67
0
  if (string_to_int(s, 0, &result)) {
68
0
      return_integer(result);
69
0
  } else {
70
0
      return_integer(YR_UNDEFINED);
71
0
  }
72
0
}
73
74
define_function(to_int_base)
75
0
{
76
0
  char* s = string_argument(1);
77
0
  int64_t base = integer_argument(2);
78
0
  int64_t result = 0;
79
80
0
  if (!(base == 0 || (base >= 2 && base <= 36))) {
81
0
      return_integer(YR_UNDEFINED);
82
0
  }
83
0
  if (string_to_int(s, base, &result)) {
84
0
      return_integer(result);
85
0
  } else {
86
0
      return_integer(YR_UNDEFINED);
87
0
  }
88
0
}
89
90
define_function(string_length)
91
0
{
92
0
  SIZED_STRING* s = sized_string_argument(1);
93
0
  return_integer(s->length);
94
0
}
95
96
0
begin_declarations
97
0
  declare_function("to_int", "s", "i", to_int);
98
0
  declare_function("to_int", "si", "i", to_int_base);
99
0
  declare_function("length", "s", "i", string_length);
100
0
end_declarations
101
102
int module_initialize(YR_MODULE* module)
103
2
{
104
2
  return ERROR_SUCCESS;
105
2
}
106
107
int module_finalize(YR_MODULE* module)
108
0
{
109
0
  return ERROR_SUCCESS;
110
0
}
111
112
int module_load(
113
    YR_SCAN_CONTEXT* context,
114
    YR_OBJECT* module_object,
115
    void* module_data,
116
    size_t module_data_size)
117
0
{
118
0
  return ERROR_SUCCESS;
119
0
}
120
121
int module_unload(YR_OBJECT* module_object)
122
0
{
123
0
  return ERROR_SUCCESS;
124
0
}