Coverage Report

Created: 2026-07-16 06:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/yara/libyara/modules/math/math.c
Line
Count
Source
1
/*
2
Copyright (c) 2014-2021. 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 <math.h>
31
#include <stdlib.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 math
38
39
#define PI 3.141592653589793
40
// This is more than enough space to hold the maximum signed 64bit integer as a
41
// string in decimal, hex or octal, including the sign and NULL terminator.
42
0
#define INT64_MAX_STRING 30
43
44
// log2 is not defined by math.h in VC++
45
46
#if defined(_MSC_VER) && _MSC_VER < 1800
47
static double log2(double n)
48
{
49
  return log(n) / log(2.0);
50
}
51
#endif
52
53
uint32_t* get_distribution(
54
    int64_t offset,
55
    int64_t length,
56
    YR_SCAN_CONTEXT* context)
57
9.06k
{
58
9.06k
  bool past_first_block = false;
59
60
9.06k
  size_t i;
61
62
9.06k
  uint32_t* data = (uint32_t*) yr_calloc(256, sizeof(uint32_t));
63
64
9.06k
  if (data == NULL)
65
0
    return NULL;
66
67
9.06k
  YR_MEMORY_BLOCK* block = first_memory_block(context);
68
9.06k
  YR_MEMORY_BLOCK_ITERATOR* iterator = context->iterator;
69
70
9.06k
  if (block == NULL || offset < 0 || length < 0 || offset < block->base)
71
0
  {
72
0
    yr_free(data);
73
0
    return NULL;
74
0
  }
75
76
9.06k
  foreach_memory_block(iterator, block)
77
9.06k
  {
78
9.06k
    if (offset >= block->base && offset < block->base + block->size)
79
9.06k
    {
80
9.06k
      size_t data_offset = (size_t) (offset - block->base);
81
9.06k
      size_t data_len = (size_t) yr_min(
82
9.06k
          length, (size_t) (block->size - data_offset));
83
84
9.06k
      const uint8_t* block_data = yr_fetch_block_data(block);
85
86
9.06k
      if (block_data == NULL)
87
0
      {
88
0
        yr_free(data);
89
0
        return NULL;
90
0
      }
91
92
9.06k
      offset += data_len;
93
9.06k
      length -= data_len;
94
95
126M
      for (i = 0; i < data_len; i++)
96
126M
      {
97
126M
        uint8_t c = *(block_data + data_offset + i);
98
126M
        data[c]++;
99
126M
      }
100
101
9.06k
      past_first_block = true;
102
9.06k
    }
103
0
    else if (past_first_block)
104
0
    {
105
      // If offset is not within current block and we already
106
      // past the first block then the we are trying to compute
107
      // the distribution over a range of non contiguous blocks. As
108
      // range contains gaps of undefined data the distribution is
109
      // undefined.
110
111
0
      yr_free(data);
112
0
      return NULL;
113
0
    }
114
115
9.06k
    if (block->base + block->size >= offset + length)
116
9.06k
      break;
117
9.06k
  }
118
119
9.06k
  if (!past_first_block)
120
0
  {
121
0
    yr_free(data);
122
0
    return NULL;
123
0
  }
124
9.06k
  return data;
125
9.06k
}
126
127
uint32_t* get_distribution_global(YR_SCAN_CONTEXT* context)
128
0
{
129
0
  size_t i;
130
131
0
  int64_t expected_next_offset = 0;
132
133
0
  uint32_t* data = (uint32_t*) yr_calloc(256, sizeof(uint32_t));
134
135
0
  if (data == NULL)
136
0
    return NULL;
137
138
0
  YR_MEMORY_BLOCK* block;
139
0
  YR_MEMORY_BLOCK_ITERATOR* iterator = context->iterator;
140
0
  foreach_memory_block(iterator, block)
141
0
  {
142
0
    if (expected_next_offset != block->base)
143
0
    {
144
      // If offset is not directly after the current block then
145
      // we are trying to compute the distribution over a range of non
146
      // contiguous blocks. As the range contains gaps of
147
      // undefined data the distribution is undefined.
148
0
      yr_free(data);
149
0
      return NULL;
150
0
    }
151
0
    const uint8_t* block_data = yr_fetch_block_data(block);
152
153
0
    if (block_data == NULL)
154
0
    {
155
0
      yr_free(data);
156
0
      return NULL;
157
0
    }
158
159
0
    for (i = 0; i < block->size; i++)
160
0
    {
161
0
      uint8_t c = *(block_data + i);
162
0
      data[c] += 1;
163
0
    }
164
0
    expected_next_offset = block->base + block->size;
165
0
  }
166
0
  return data;
167
0
}
168
169
define_function(string_entropy)
170
0
{
171
0
  size_t i;
172
0
  double entropy = 0.0;
173
174
0
  SIZED_STRING* s = sized_string_argument(1);
175
176
0
  uint32_t* data = (uint32_t*) yr_calloc(256, sizeof(uint32_t));
177
178
0
  if (data == NULL)
179
0
    return_float(YR_UNDEFINED);
180
181
0
  for (i = 0; i < s->length; i++)
182
0
  {
183
0
    uint8_t c = s->c_string[i];
184
0
    data[c] += 1;
185
0
  }
186
187
0
  for (i = 0; i < 256; i++)
188
0
  {
189
0
    if (data[i] != 0)
190
0
    {
191
0
      double x = (double) (data[i]) / s->length;
192
0
      entropy -= x * log2(x);
193
0
    }
194
0
  }
195
196
0
  yr_free(data);
197
0
  return_float(entropy);
198
0
}
199
200
define_function(data_entropy)
201
1.86k
{
202
1.86k
  double entropy = 0.0;
203
204
1.86k
  int64_t offset = integer_argument(1);  // offset where to start
205
1.86k
  int64_t length = integer_argument(2);  // length of bytes we want entropy on
206
207
1.86k
  YR_SCAN_CONTEXT* context = yr_scan_context();
208
209
1.86k
  size_t i;
210
211
1.86k
  size_t total_len = 0;
212
213
1.86k
  uint32_t* data = get_distribution(offset, length, context);
214
215
1.86k
  if (data == NULL)
216
1.86k
    return_float(YR_UNDEFINED);
217
218
478k
  for (i = 0; i < 256; i++)
219
476k
  {
220
476k
    total_len += data[i];
221
476k
  }
222
223
478k
  for (i = 0; i < 256; i++)
224
476k
  {
225
476k
    if (data[i] != 0)
226
61.7k
    {
227
61.7k
      double x = (double) (data[i]) / total_len;
228
61.7k
      entropy -= x * log2(x);
229
61.7k
    }
230
476k
  }
231
232
1.86k
  yr_free(data);
233
1.86k
  return_float(entropy);
234
0
}
235
236
define_function(string_deviation)
237
0
{
238
0
  SIZED_STRING* s = sized_string_argument(1);
239
240
0
  double mean = float_argument(2);
241
0
  double sum = 0.0;
242
243
0
  size_t i;
244
245
0
  for (i = 0; i < s->length; i++) sum += fabs(((double) s->c_string[i]) - mean);
246
247
0
  return_float(sum / s->length);
248
0
}
249
250
define_function(data_deviation)
251
1.86k
{
252
1.86k
  int64_t offset = integer_argument(1);
253
1.86k
  int64_t length = integer_argument(2);
254
255
1.86k
  double mean = float_argument(3);
256
1.86k
  double sum = 0.0;
257
258
1.86k
  size_t total_len = 0;
259
1.86k
  size_t i;
260
261
1.86k
  YR_SCAN_CONTEXT* context = yr_scan_context();
262
263
1.86k
  uint32_t* data = get_distribution(offset, length, context);
264
265
1.86k
  if (data == NULL)
266
1.86k
    return_float(YR_UNDEFINED);
267
268
478k
  for (i = 0; i < 256; i++)
269
476k
  {
270
476k
    total_len += data[i];
271
476k
    sum += fabs(((double) i) - mean) * data[i];
272
476k
  }
273
274
1.86k
  yr_free(data);
275
1.86k
  return_float(sum / total_len);
276
0
}
277
278
define_function(string_mean)
279
0
{
280
0
  size_t i;
281
0
  double sum = 0.0;
282
283
0
  SIZED_STRING* s = sized_string_argument(1);
284
285
0
  for (i = 0; i < s->length; i++) sum += (double) s->c_string[i];
286
287
0
  return_float(sum / s->length);
288
0
}
289
290
define_function(data_mean)
291
1.86k
{
292
1.86k
  double sum = 0.0;
293
294
1.86k
  int64_t offset = integer_argument(1);
295
1.86k
  int64_t length = integer_argument(2);
296
297
1.86k
  YR_SCAN_CONTEXT* context = yr_scan_context();
298
299
1.86k
  size_t total_len = 0;
300
1.86k
  size_t i;
301
302
1.86k
  uint32_t* data = get_distribution(offset, length, context);
303
304
1.86k
  if (data == NULL)
305
1.86k
    return_float(YR_UNDEFINED);
306
307
478k
  for (i = 0; i < 256; i++)
308
476k
  {
309
476k
    total_len += data[i];
310
476k
    sum += ((double) i) * data[i];
311
476k
  }
312
313
1.86k
  yr_free(data);
314
1.86k
  return_float(sum / total_len);
315
0
}
316
317
define_function(data_serial_correlation)
318
1.86k
{
319
1.86k
  int past_first_block = false;
320
321
1.86k
  size_t total_len = 0;
322
1.86k
  size_t i;
323
324
1.86k
  int64_t offset = integer_argument(1);
325
1.86k
  int64_t length = integer_argument(2);
326
327
1.86k
  YR_SCAN_CONTEXT* context = yr_scan_context();
328
1.86k
  YR_MEMORY_BLOCK* block = first_memory_block(context);
329
1.86k
  YR_MEMORY_BLOCK_ITERATOR* iterator = context->iterator;
330
331
1.86k
  if (block == NULL)
332
1.86k
    return_float(YR_UNDEFINED);
333
334
1.86k
  double sccun = 0;
335
1.86k
  double sccfirst = 0;
336
1.86k
  double scclast = 0;
337
1.86k
  double scct1 = 0;
338
1.86k
  double scct2 = 0;
339
1.86k
  double scct3 = 0;
340
1.86k
  double scc = 0;
341
342
1.86k
  if (offset < 0 || length < 0 || offset < block->base)
343
1.86k
    return_float(YR_UNDEFINED);
344
345
1.86k
  foreach_memory_block(iterator, block)
346
1.86k
  {
347
1.86k
    if (offset >= block->base && offset < block->base + block->size)
348
1.86k
    {
349
1.86k
      size_t data_offset = (size_t) (offset - block->base);
350
1.86k
      size_t data_len = (size_t) yr_min(
351
1.86k
          length, (size_t) (block->size - data_offset));
352
353
1.86k
      const uint8_t* block_data = yr_fetch_block_data(block);
354
355
1.86k
      if (block_data == NULL)
356
1.86k
        return_float(YR_UNDEFINED);
357
358
1.86k
      total_len += data_len;
359
1.86k
      offset += data_len;
360
1.86k
      length -= data_len;
361
362
25.2M
      for (i = 0; i < data_len; i++)
363
25.2M
      {
364
25.2M
        sccun = (double) *(block_data + data_offset + i);
365
25.2M
        if (i == 0)
366
1.86k
        {
367
1.86k
          sccfirst = sccun;
368
1.86k
        }
369
25.2M
        scct1 += scclast * sccun;
370
25.2M
        scct2 += sccun;
371
25.2M
        scct3 += sccun * sccun;
372
25.2M
        scclast = sccun;
373
25.2M
      }
374
375
1.86k
      past_first_block = true;
376
1.86k
    }
377
0
    else if (past_first_block)
378
0
    {
379
      // If offset is not within current block and we already
380
      // past the first block then the we are trying to compute
381
      // the checksum over a range of non contiguous blocks. As
382
      // range contains gaps of undefined data the checksum is
383
      // undefined.
384
0
      return_float(YR_UNDEFINED);
385
0
    }
386
387
1.86k
    if (block->base + block->size >= offset + length)
388
1.86k
      break;
389
1.86k
  }
390
391
1.86k
  if (!past_first_block)
392
1.86k
    return_float(YR_UNDEFINED);
393
394
1.86k
  scct1 += scclast * sccfirst;
395
1.86k
  scct2 *= scct2;
396
397
1.86k
  scc = total_len * scct3 - scct2;
398
399
1.86k
  if (scc == 0)
400
25
    scc = -100000;
401
1.83k
  else
402
1.83k
    scc = (total_len * scct1 - scct2) / scc;
403
404
1.86k
  return_float(scc);
405
0
}
406
407
define_function(string_serial_correlation)
408
0
{
409
0
  SIZED_STRING* s = sized_string_argument(1);
410
411
0
  double sccun = 0;
412
0
  double scclast = 0;
413
0
  double scct1 = 0;
414
0
  double scct2 = 0;
415
0
  double scct3 = 0;
416
0
  double scc = 0;
417
418
0
  size_t i;
419
420
0
  for (i = 0; i < s->length; i++)
421
0
  {
422
0
    sccun = (double) s->c_string[i];
423
0
    scct1 += scclast * sccun;
424
0
    scct2 += sccun;
425
0
    scct3 += sccun * sccun;
426
0
    scclast = sccun;
427
0
  }
428
429
0
  if (s->length > 0)
430
0
  {
431
0
    scct1 += scclast * (double) s->c_string[0];
432
0
  }
433
0
  scct2 *= scct2;
434
435
0
  scc = s->length * scct3 - scct2;
436
437
0
  if (scc == 0)
438
0
    scc = -100000;
439
0
  else
440
0
    scc = (s->length * scct1 - scct2) / scc;
441
442
0
  return_float(scc);
443
0
}
444
445
define_function(data_monte_carlo_pi)
446
1.74k
{
447
1.74k
  int past_first_block = false;
448
1.74k
  int mcount = 0;
449
1.74k
  int inmont = 0;
450
451
1.74k
  double INCIRC = pow(pow(256.0, 3.0) - 1, 2.0);
452
1.74k
  double mpi = 0;
453
454
1.74k
  size_t i;
455
456
1.74k
  int64_t offset = integer_argument(1);
457
1.74k
  int64_t length = integer_argument(2);
458
459
1.74k
  YR_SCAN_CONTEXT* context = yr_scan_context();
460
1.74k
  YR_MEMORY_BLOCK* block = first_memory_block(context);
461
1.74k
  YR_MEMORY_BLOCK_ITERATOR* iterator = context->iterator;
462
463
1.74k
  if (block == NULL)
464
1.74k
    return_float(YR_UNDEFINED);
465
466
1.74k
  if (offset < 0 || length < 0 || offset < block->base)
467
1.74k
    return_float(YR_UNDEFINED);
468
469
1.74k
  foreach_memory_block(iterator, block)
470
1.74k
  {
471
1.74k
    if (offset >= block->base && offset < block->base + block->size)
472
1.74k
    {
473
1.74k
      unsigned int monte[6];
474
475
1.74k
      size_t data_offset = (size_t) (offset - block->base);
476
1.74k
      size_t data_len = (size_t) yr_min(
477
1.74k
          length, (size_t) (block->size - data_offset));
478
479
1.74k
      const uint8_t* block_data = yr_fetch_block_data(block);
480
481
1.74k
      if (block_data == NULL)
482
1.74k
        return_float(YR_UNDEFINED);
483
484
1.74k
      offset += data_len;
485
1.74k
      length -= data_len;
486
487
25.2M
      for (i = 0; i < data_len; i++)
488
25.2M
      {
489
25.2M
        monte[i % 6] = (unsigned int) *(block_data + data_offset + i);
490
491
25.2M
        if (i % 6 == 5)
492
4.20M
        {
493
4.20M
          double mx = 0;
494
4.20M
          double my = 0;
495
4.20M
          int j;
496
497
4.20M
          mcount++;
498
499
16.8M
          for (j = 0; j < 3; j++)
500
12.6M
          {
501
12.6M
            mx = (mx * 256.0) + monte[j];
502
12.6M
            my = (my * 256.0) + monte[j + 3];
503
12.6M
          }
504
505
4.20M
          if ((mx * mx + my * my) <= INCIRC)
506
3.41M
            inmont++;
507
4.20M
        }
508
25.2M
      }
509
510
1.74k
      past_first_block = true;
511
1.74k
    }
512
0
    else if (past_first_block)
513
0
    {
514
      // If offset is not within current block and we already
515
      // past the first block then the we are trying to compute
516
      // the checksum over a range of non contiguous blocks. As
517
      // range contains gaps of undefined data the checksum is
518
      // undefined.
519
0
      return_float(YR_UNDEFINED);
520
0
    }
521
522
1.74k
    if (block->base + block->size >= offset + length)
523
1.74k
      break;
524
1.74k
  }
525
526
1.74k
  if (!past_first_block || mcount == 0)
527
1.74k
    return_float(YR_UNDEFINED);
528
529
1.74k
  mpi = 4.0 * ((double) inmont / mcount);
530
531
1.74k
  return_float(fabs((mpi - PI) / PI));
532
0
}
533
534
define_function(string_monte_carlo_pi)
535
0
{
536
0
  SIZED_STRING* s = sized_string_argument(1);
537
538
0
  double INCIRC = pow(pow(256.0, 3.0) - 1, 2.0);
539
0
  double mpi = 0;
540
541
0
  unsigned int monte[6];
542
543
0
  int mcount = 0;
544
0
  int inmont = 0;
545
546
0
  size_t i;
547
548
0
  for (i = 0; i < s->length; i++)
549
0
  {
550
0
    monte[i % 6] = (unsigned int) s->c_string[i];
551
552
0
    if (i % 6 == 5)
553
0
    {
554
0
      double mx = 0;
555
0
      double my = 0;
556
557
0
      int j;
558
559
0
      mcount++;
560
561
0
      for (j = 0; j < 3; j++)
562
0
      {
563
0
        mx = (mx * 256.0) + monte[j];
564
0
        my = (my * 256.0) + monte[j + 3];
565
0
      }
566
567
0
      if ((mx * mx + my * my) <= INCIRC)
568
0
        inmont++;
569
0
    }
570
0
  }
571
572
0
  if (mcount == 0)
573
0
    return_float(YR_UNDEFINED);
574
575
0
  mpi = 4.0 * ((double) inmont / mcount);
576
0
  return_float(fabs((mpi - PI) / PI));
577
0
}
578
579
define_function(in_range)
580
0
{
581
0
  double test = float_argument(1);
582
0
  double lower = float_argument(2);
583
0
  double upper = float_argument(3);
584
585
0
  return_integer((lower <= test && test <= upper) ? 1 : 0);
586
0
}
587
588
// Undefine existing "min" and "max" macros in order to avoid conflicts with
589
// function names.
590
#undef min
591
#undef max
592
593
define_function(min)
594
1.74k
{
595
1.74k
  uint64_t i = integer_argument(1);
596
1.74k
  uint64_t j = integer_argument(2);
597
598
1.74k
  return_integer(i < j ? i : j);
599
0
}
600
601
define_function(max)
602
1.74k
{
603
1.74k
  uint64_t i = integer_argument(1);
604
1.74k
  uint64_t j = integer_argument(2);
605
606
1.74k
  return_integer(i > j ? i : j);
607
0
}
608
609
define_function(to_number)
610
0
{
611
0
  return_integer(integer_argument(1) ? 1 : 0);
612
0
}
613
614
define_function(yr_math_abs)
615
0
{
616
0
  return_integer(llabs(integer_argument(1)));
617
0
}
618
619
define_function(count_range)
620
1.74k
{
621
1.74k
  int64_t byte = integer_argument(1);
622
1.74k
  int64_t offset = integer_argument(2);
623
1.74k
  int64_t length = integer_argument(3);
624
625
1.74k
  if (byte < 0 || byte > 255)
626
1.74k
    return_integer(YR_UNDEFINED);
627
628
1.74k
  YR_SCAN_CONTEXT* context = yr_scan_context();
629
630
1.74k
  uint32_t* distribution = get_distribution(offset, length, context);
631
632
1.74k
  if (distribution == NULL)
633
1.74k
    return_integer(YR_UNDEFINED);
634
635
1.74k
  int64_t count = (int64_t) distribution[byte];
636
1.74k
  yr_free(distribution);
637
1.74k
  return_integer(count);
638
0
}
639
640
define_function(count_global)
641
0
{
642
0
  int64_t byte = integer_argument(1);
643
644
0
  if (byte < 0 || byte > 255)
645
0
    return_integer(YR_UNDEFINED);
646
647
0
  YR_SCAN_CONTEXT* context = yr_scan_context();
648
649
0
  uint32_t* distribution = get_distribution_global(context);
650
651
0
  if (distribution == NULL)
652
0
    return_integer(YR_UNDEFINED);
653
654
0
  int64_t count = (int64_t) distribution[byte];
655
0
  yr_free(distribution);
656
0
  return_integer(count);
657
0
}
658
659
define_function(percentage_range)
660
1.74k
{
661
1.74k
  int64_t byte = integer_argument(1);
662
1.74k
  int64_t offset = integer_argument(2);
663
1.74k
  int64_t length = integer_argument(3);
664
665
1.74k
  if (byte < 0 || byte > 255)
666
1.74k
    return_float(YR_UNDEFINED);
667
668
1.74k
  YR_SCAN_CONTEXT* context = yr_scan_context();
669
670
1.74k
  uint32_t* distribution = get_distribution(offset, length, context);
671
672
1.74k
  if (distribution == NULL)
673
1.74k
    return_float(YR_UNDEFINED);
674
675
1.74k
  int64_t count = (int64_t) distribution[byte];
676
1.74k
  int64_t total_count = 0;
677
1.74k
  int64_t i;
678
679
447k
  for (i = 0; i < 256; i++) total_count += distribution[i];
680
681
1.74k
  yr_free(distribution);
682
1.74k
  return_float(((float) count) / ((float) total_count));
683
0
}
684
685
define_function(percentage_global)
686
0
{
687
0
  int64_t byte = integer_argument(1);
688
689
0
  if (byte < 0 || byte > 255)
690
0
    return_float(YR_UNDEFINED);
691
692
0
  YR_SCAN_CONTEXT* context = yr_scan_context();
693
694
0
  uint32_t* distribution = get_distribution_global(context);
695
696
0
  if (distribution == NULL)
697
0
    return_float(YR_UNDEFINED);
698
699
0
  int64_t count = (int64_t) distribution[byte];
700
0
  int64_t total_count = 0;
701
0
  int64_t i;
702
703
0
  for (i = 0; i < 256; i++) total_count += distribution[i];
704
705
0
  yr_free(distribution);
706
0
  return_float(((float) count) / ((float) total_count));
707
0
}
708
709
define_function(mode_range)
710
0
{
711
0
  int64_t offset = integer_argument(1);
712
0
  int64_t length = integer_argument(2);
713
714
0
  YR_SCAN_CONTEXT* context = yr_scan_context();
715
716
0
  uint32_t* distribution = get_distribution(offset, length, context);
717
718
0
  if (distribution == NULL)
719
0
    return_integer(YR_UNDEFINED);
720
721
0
  int64_t most_common = 0;
722
0
  size_t i;
723
724
0
  for (i = 0; i < 256; i++)
725
0
  {
726
0
    if (distribution[i] > distribution[most_common])
727
0
      most_common = (int64_t) i;
728
0
  }
729
730
0
  yr_free(distribution);
731
0
  return_integer(most_common);
732
0
}
733
734
define_function(mode_global)
735
0
{
736
0
  YR_SCAN_CONTEXT* context = yr_scan_context();
737
738
0
  uint32_t* distribution = get_distribution_global(context);
739
740
0
  if (distribution == NULL)
741
0
    return_integer(YR_UNDEFINED);
742
743
0
  int64_t most_common = 0;
744
0
  size_t i;
745
746
0
  for (i = 0; i < 256; i++)
747
0
  {
748
0
    if (distribution[i] > distribution[most_common])
749
0
      most_common = (int64_t) i;
750
0
  }
751
752
0
  yr_free(distribution);
753
0
  return_integer(most_common);
754
0
}
755
756
define_function(to_string)
757
0
{
758
0
  int64_t i = integer_argument(1);
759
0
  char str[INT64_MAX_STRING];
760
0
  snprintf(str, INT64_MAX_STRING, "%" PRId64, i);
761
0
  return_string(&str);
762
0
}
763
764
define_function(to_string_base)
765
0
{
766
0
  int64_t i = integer_argument(1);
767
0
  int64_t base = integer_argument(2);
768
0
  char str[INT64_MAX_STRING];
769
0
  char* fmt;
770
0
  switch (base)
771
0
  {
772
0
  case 10:
773
0
    fmt = "%" PRId64;
774
0
    break;
775
0
  case 8:
776
0
    fmt = "%" PRIo64;
777
0
    break;
778
0
  case 16:
779
0
    fmt = "%" PRIx64;
780
0
    break;
781
0
  default:
782
0
    return_string(YR_UNDEFINED);
783
0
  }
784
0
  snprintf(str, INT64_MAX_STRING, fmt, i);
785
0
  return_string(&str);
786
0
}
787
788
1.86k
begin_declarations
789
1.86k
  declare_float("MEAN_BYTES");
790
1.86k
  declare_function("in_range", "fff", "i", in_range);
791
1.86k
  declare_function("deviation", "iif", "f", data_deviation);
792
1.86k
  declare_function("deviation", "sf", "f", string_deviation);
793
1.86k
  declare_function("mean", "ii", "f", data_mean);
794
1.86k
  declare_function("mean", "s", "f", string_mean);
795
1.86k
  declare_function("serial_correlation", "ii", "f", data_serial_correlation);
796
1.86k
  declare_function("serial_correlation", "s", "f", string_serial_correlation);
797
1.86k
  declare_function("monte_carlo_pi", "ii", "f", data_monte_carlo_pi);
798
1.86k
  declare_function("monte_carlo_pi", "s", "f", string_monte_carlo_pi);
799
1.86k
  declare_function("entropy", "ii", "f", data_entropy);
800
1.86k
  declare_function("entropy", "s", "f", string_entropy);
801
1.86k
  declare_function("min", "ii", "i", min);
802
1.86k
  declare_function("max", "ii", "i", max);
803
1.86k
  declare_function("to_number", "b", "i", to_number);
804
1.86k
  declare_function("abs", "i", "i", yr_math_abs);
805
1.86k
  declare_function("count", "iii", "i", count_range);
806
1.86k
  declare_function("count", "i", "i", count_global);
807
1.86k
  declare_function("percentage", "iii", "f", percentage_range);
808
1.86k
  declare_function("percentage", "i", "f", percentage_global);
809
1.86k
  declare_function("mode", "ii", "i", mode_range);
810
1.86k
  declare_function("mode", "", "i", mode_global);
811
1.86k
  declare_function("to_string", "i", "s", to_string);
812
1.86k
  declare_function("to_string", "ii", "s", to_string_base);
813
1.86k
end_declarations
814
815
int module_initialize(YR_MODULE* module)
816
2
{
817
2
  return ERROR_SUCCESS;
818
2
}
819
820
int module_finalize(YR_MODULE* module)
821
0
{
822
0
  return ERROR_SUCCESS;
823
0
}
824
825
int module_load(
826
    YR_SCAN_CONTEXT* context,
827
    YR_OBJECT* module_object,
828
    void* module_data,
829
    size_t module_data_size)
830
1.86k
{
831
1.86k
  yr_set_float(127.5, module_object, "MEAN_BYTES");
832
1.86k
  return ERROR_SUCCESS;
833
1.86k
}
834
835
int module_unload(YR_OBJECT* module_object)
836
1.86k
{
837
1.86k
  return ERROR_SUCCESS;
838
1.86k
}