Coverage Report

Created: 2026-12-01 06:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/yara/libyara/object.c
Line
Count
Source
1
/*
2
Copyright (c) 2014. 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 <assert.h>
31
#include <ctype.h>
32
#include <math.h>
33
#include <stdarg.h>
34
#include <stdio.h>
35
#include <stdlib.h>
36
#include <string.h>
37
#include <yara/error.h>
38
#include <yara/exec.h>
39
#include <yara/globals.h>
40
#include <yara/mem.h>
41
#include <yara/object.h>
42
#include <yara/strutils.h>
43
#include <yara/utils.h>
44
45
////////////////////////////////////////////////////////////////////////////////
46
// Creates a new object with the given type and identifier. If a parent is
47
// specified the new object is owned by the parent and it will be destroyed when
48
// the parent is destroyed. You must not call yr_object_destroy on an objected
49
// that has a parent, you should destroy the parent instead.
50
//
51
int yr_object_create(
52
    int8_t type,
53
    const char* identifier,
54
    YR_OBJECT* parent,
55
    YR_OBJECT** object)
56
16.2M
{
57
16.2M
  YR_OBJECT* obj;
58
16.2M
  size_t object_size = 0;
59
60
16.2M
  assert(parent != NULL || object != NULL);
61
16.2M
  assert(identifier != NULL);
62
63
16.2M
  switch (type)
64
16.2M
  {
65
2.83M
  case OBJECT_TYPE_STRUCTURE:
66
2.83M
    object_size = sizeof(YR_OBJECT_STRUCTURE);
67
2.83M
    break;
68
128k
  case OBJECT_TYPE_ARRAY:
69
128k
    object_size = sizeof(YR_OBJECT_ARRAY);
70
128k
    break;
71
8.20k
  case OBJECT_TYPE_DICTIONARY:
72
8.20k
    object_size = sizeof(YR_OBJECT_DICTIONARY);
73
8.20k
    break;
74
9.32M
  case OBJECT_TYPE_INTEGER:
75
9.32M
    object_size = sizeof(YR_OBJECT);
76
9.32M
    break;
77
0
  case OBJECT_TYPE_FLOAT:
78
0
    object_size = sizeof(YR_OBJECT);
79
0
    break;
80
3.79M
  case OBJECT_TYPE_STRING:
81
3.79M
    object_size = sizeof(YR_OBJECT);
82
3.79M
    break;
83
123k
  case OBJECT_TYPE_FUNCTION:
84
123k
    object_size = sizeof(YR_OBJECT_FUNCTION);
85
123k
    break;
86
0
  default:
87
0
    assert(false);
88
0
    return ERROR_INVALID_ARGUMENT;
89
16.2M
  }
90
91
16.2M
  obj = (YR_OBJECT*) yr_malloc(object_size);
92
93
16.2M
  if (obj == NULL)
94
0
    return ERROR_INSUFFICIENT_MEMORY;
95
96
16.2M
  obj->type = type;
97
16.2M
  obj->identifier = yr_strdup(identifier);
98
16.2M
  obj->parent = parent;
99
16.2M
  obj->data = NULL;
100
101
16.2M
  switch (type)
102
16.2M
  {
103
9.32M
  case OBJECT_TYPE_INTEGER:
104
9.32M
    obj->value.i = YR_UNDEFINED;
105
9.32M
    break;
106
0
  case OBJECT_TYPE_FLOAT:
107
0
    obj->value.d = NAN;
108
0
    break;
109
3.79M
  case OBJECT_TYPE_STRING:
110
3.79M
    obj->value.ss = NULL;
111
3.79M
    break;
112
2.83M
  case OBJECT_TYPE_STRUCTURE:
113
2.83M
    object_as_structure(obj)->members = NULL;
114
2.83M
    break;
115
128k
  case OBJECT_TYPE_ARRAY:
116
128k
    object_as_array(obj)->items = NULL;
117
128k
    object_as_array(obj)->prototype_item = NULL;
118
128k
    break;
119
8.20k
  case OBJECT_TYPE_DICTIONARY:
120
8.20k
    object_as_dictionary(obj)->items = NULL;
121
8.20k
    object_as_dictionary(obj)->prototype_item = NULL;
122
8.20k
    break;
123
123k
  case OBJECT_TYPE_FUNCTION:
124
123k
    object_as_function(obj)->return_obj = NULL;
125
1.35M
    for (int i = 0; i < YR_MAX_OVERLOADED_FUNCTIONS; i++)
126
1.23M
    {
127
1.23M
      object_as_function(obj)->prototypes[i].arguments_fmt = NULL;
128
1.23M
      object_as_function(obj)->prototypes[i].code = NULL;
129
1.23M
    }
130
123k
    break;
131
16.2M
  }
132
133
16.2M
  if (obj->identifier == NULL)
134
0
  {
135
0
    yr_free(obj);
136
0
    return ERROR_INSUFFICIENT_MEMORY;
137
0
  }
138
139
16.2M
  if (parent != NULL)
140
2.68M
  {
141
2.68M
    assert(
142
2.68M
        parent->type == OBJECT_TYPE_STRUCTURE ||
143
2.68M
        parent->type == OBJECT_TYPE_ARRAY ||
144
2.68M
        parent->type == OBJECT_TYPE_DICTIONARY ||
145
2.68M
        parent->type == OBJECT_TYPE_FUNCTION);
146
147
    // Objects with a parent take the canary from it.
148
2.68M
    obj->canary = parent->canary;
149
150
2.68M
    switch (parent->type)
151
2.68M
    {
152
2.47M
    case OBJECT_TYPE_STRUCTURE:
153
2.47M
      FAIL_ON_ERROR_WITH_CLEANUP(yr_object_structure_set_member(parent, obj), {
154
2.47M
        yr_free((void*) obj->identifier);
155
2.47M
        yr_free(obj);
156
2.47M
      });
157
2.47M
      break;
158
159
73.8k
    case OBJECT_TYPE_ARRAY:
160
73.8k
      object_as_array(parent)->prototype_item = obj;
161
73.8k
      break;
162
163
8.20k
    case OBJECT_TYPE_DICTIONARY:
164
8.20k
      object_as_dictionary(parent)->prototype_item = obj;
165
8.20k
      break;
166
167
123k
    case OBJECT_TYPE_FUNCTION:
168
123k
      object_as_function(parent)->return_obj = obj;
169
123k
      break;
170
2.68M
    }
171
2.68M
  }
172
173
16.2M
  if (object != NULL)
174
13.9M
    *object = obj;
175
176
16.2M
  return ERROR_SUCCESS;
177
16.2M
}
178
179
void yr_object_set_canary(YR_OBJECT* object, int canary)
180
8.20k
{
181
8.20k
  object->canary = canary;
182
8.20k
}
183
184
int yr_object_function_create(
185
    const char* identifier,
186
    const char* arguments_fmt,
187
    const char* return_fmt,
188
    YR_MODULE_FUNC code,
189
    YR_OBJECT* parent,
190
    YR_OBJECT** function)
191
254k
{
192
254k
  YR_OBJECT* return_obj;
193
254k
  YR_OBJECT* o = NULL;
194
254k
  YR_OBJECT_FUNCTION* f = NULL;
195
196
254k
  int8_t return_type;
197
198
  // The parent of a function must be a structure.
199
254k
  assert(parent != NULL && parent->type == OBJECT_TYPE_STRUCTURE);
200
201
254k
  switch (*return_fmt)
202
254k
  {
203
254k
  case 'i':
204
254k
    return_type = OBJECT_TYPE_INTEGER;
205
254k
    break;
206
0
  case 's':
207
0
    return_type = OBJECT_TYPE_STRING;
208
0
    break;
209
0
  case 'f':
210
0
    return_type = OBJECT_TYPE_FLOAT;
211
0
    break;
212
0
  default:
213
0
    return ERROR_INVALID_FORMAT;
214
254k
  }
215
216
  // Try to find if the structure already has a function
217
  // with that name. In that case this is a function overload.
218
254k
  f = object_as_function(yr_object_lookup_field(parent, identifier));
219
220
  // Overloaded functions must have the same return type.
221
254k
  if (f != NULL && return_type != f->return_obj->type)
222
0
    return ERROR_WRONG_RETURN_TYPE;
223
224
254k
  if (f == NULL)  // Function doesn't exist yet
225
123k
  {
226
123k
    FAIL_ON_ERROR(
227
123k
        yr_object_create(OBJECT_TYPE_FUNCTION, identifier, parent, &o));
228
229
    // In case of failure while creating return_obj we don't need to free the
230
    // previously created "o" object, as it is already associated with its
231
    // parent and will be destroyed when the parent is destroyed.
232
123k
    FAIL_ON_ERROR(yr_object_create(return_type, "result", o, &return_obj));
233
234
123k
    f = object_as_function(o);
235
123k
  }
236
237
574k
  for (int i = 0; i < YR_MAX_OVERLOADED_FUNCTIONS; i++)
238
574k
  {
239
574k
    if (f->prototypes[i].arguments_fmt == NULL)
240
254k
    {
241
254k
      f->prototypes[i].arguments_fmt = arguments_fmt;
242
254k
      f->prototypes[i].code = code;
243
244
254k
      break;
245
254k
    }
246
574k
  }
247
248
254k
  if (function != NULL)
249
254k
    *function = (YR_OBJECT*) f;
250
251
254k
  return ERROR_SUCCESS;
252
254k
}
253
254
int yr_object_from_external_variable(
255
    YR_EXTERNAL_VARIABLE* external,
256
    YR_OBJECT** object)
257
0
{
258
0
  YR_OBJECT* obj;
259
0
  int result;
260
0
  uint8_t obj_type = 0;
261
262
0
  switch (external->type)
263
0
  {
264
0
  case EXTERNAL_VARIABLE_TYPE_INTEGER:
265
0
  case EXTERNAL_VARIABLE_TYPE_BOOLEAN:
266
0
    obj_type = OBJECT_TYPE_INTEGER;
267
0
    break;
268
269
0
  case EXTERNAL_VARIABLE_TYPE_FLOAT:
270
0
    obj_type = OBJECT_TYPE_FLOAT;
271
0
    break;
272
273
0
  case EXTERNAL_VARIABLE_TYPE_STRING:
274
0
  case EXTERNAL_VARIABLE_TYPE_MALLOC_STRING:
275
0
    obj_type = OBJECT_TYPE_STRING;
276
0
    break;
277
278
0
  default:
279
    // The type comes from the compiled rules file, a corrupt or hand-crafted
280
    // one can carry a value that is not any of the EXTERNAL_VARIABLE_TYPE_X.
281
0
    return ERROR_CORRUPT_FILE;
282
0
  }
283
284
0
  result = yr_object_create(obj_type, external->identifier, NULL, &obj);
285
286
0
  if (result == ERROR_SUCCESS)
287
0
  {
288
0
    switch (external->type)
289
0
    {
290
0
    case EXTERNAL_VARIABLE_TYPE_INTEGER:
291
0
    case EXTERNAL_VARIABLE_TYPE_BOOLEAN:
292
0
      result = yr_object_set_integer(external->value.i, obj, NULL);
293
0
      break;
294
295
0
    case EXTERNAL_VARIABLE_TYPE_FLOAT:
296
0
      result = yr_object_set_float(external->value.f, obj, NULL);
297
0
      break;
298
299
0
    case EXTERNAL_VARIABLE_TYPE_STRING:
300
0
    case EXTERNAL_VARIABLE_TYPE_MALLOC_STRING:
301
0
      result = yr_object_set_string(
302
0
          external->value.s, strlen(external->value.s), obj, NULL);
303
0
      break;
304
0
    }
305
306
0
    if (result == ERROR_SUCCESS)
307
0
    {
308
0
      *object = obj;
309
0
    }
310
0
    else
311
0
    {
312
0
      yr_object_destroy(obj);
313
0
    }
314
0
  }
315
316
0
  return result;
317
0
}
318
319
////////////////////////////////////////////////////////////////////////////////
320
// Destroy an objects, and any other object that is a child of it. For example,
321
// destroying a struct will destroy all its members.
322
//
323
void yr_object_destroy(YR_OBJECT* object)
324
16.2M
{
325
16.2M
  YR_STRUCTURE_MEMBER* member;
326
16.2M
  YR_STRUCTURE_MEMBER* next_member;
327
16.2M
  YR_ARRAY_ITEMS* array_items;
328
16.2M
  YR_DICTIONARY_ITEMS* dict_items;
329
330
16.2M
  if (object == NULL)
331
0
    return;
332
333
16.2M
  switch (object->type)
334
16.2M
  {
335
2.83M
  case OBJECT_TYPE_STRUCTURE:
336
2.83M
    member = object_as_structure(object)->members;
337
338
16.1M
    while (member != NULL)
339
13.3M
    {
340
13.3M
      next_member = member->next;
341
13.3M
      yr_object_destroy(member->object);
342
13.3M
      yr_free(member);
343
13.3M
      member = next_member;
344
13.3M
    }
345
2.83M
    break;
346
347
3.79M
  case OBJECT_TYPE_STRING:
348
3.79M
    if (object->value.ss != NULL)
349
2.07M
      yr_free(object->value.ss);
350
3.79M
    break;
351
352
128k
  case OBJECT_TYPE_ARRAY:
353
128k
    if (object_as_array(object)->prototype_item != NULL)
354
128k
      yr_object_destroy(object_as_array(object)->prototype_item);
355
356
128k
    array_items = object_as_array(object)->items;
357
358
128k
    if (array_items != NULL)
359
65.5k
    {
360
2.70M
      for (int i = 0; i < array_items->length; i++)
361
2.64M
        if (array_items->objects[i] != NULL)
362
2.64M
          yr_object_destroy(array_items->objects[i]);
363
65.5k
    }
364
365
128k
    yr_free(array_items);
366
128k
    break;
367
368
8.20k
  case OBJECT_TYPE_DICTIONARY:
369
8.20k
    if (object_as_dictionary(object)->prototype_item != NULL)
370
8.20k
      yr_object_destroy(object_as_dictionary(object)->prototype_item);
371
372
8.20k
    dict_items = object_as_dictionary(object)->items;
373
374
8.20k
    if (dict_items != NULL)
375
249
    {
376
3.45k
      for (int i = 0; i < dict_items->used; i++)
377
3.20k
      {
378
3.20k
        if (dict_items->objects[i].key != NULL)
379
3.20k
          yr_free(dict_items->objects[i].key);
380
381
3.20k
        if (dict_items->objects[i].obj != NULL)
382
3.20k
          yr_object_destroy(dict_items->objects[i].obj);
383
3.20k
      }
384
249
    }
385
386
8.20k
    yr_free(dict_items);
387
8.20k
    break;
388
389
123k
  case OBJECT_TYPE_FUNCTION:
390
123k
    yr_object_destroy(object_as_function(object)->return_obj);
391
123k
    break;
392
16.2M
  }
393
394
16.2M
  yr_free((void*) object->identifier);
395
16.2M
  yr_free(object);
396
16.2M
}
397
398
YR_OBJECT* yr_object_lookup_field(YR_OBJECT* object, const char* field_name)
399
38.6M
{
400
38.6M
  YR_STRUCTURE_MEMBER* member;
401
402
38.6M
  assert(object != NULL);
403
38.6M
  assert(object->type == OBJECT_TYPE_STRUCTURE);
404
405
38.6M
  member = object_as_structure(object)->members;
406
407
644M
  while (member != NULL)
408
630M
  {
409
630M
    if (strcmp(member->object->identifier, field_name) == 0)
410
25.2M
      return member->object;
411
412
605M
    member = member->next;
413
605M
  }
414
415
13.4M
  return NULL;
416
38.6M
}
417
418
static YR_OBJECT* _yr_object_lookup(
419
    YR_OBJECT* object,
420
    int flags,
421
    const char* pattern,
422
    va_list args)
423
10.7M
{
424
10.7M
  YR_OBJECT* obj = object;
425
426
10.7M
  const char* p = pattern;
427
10.7M
  const char* key = NULL;
428
429
10.7M
  char str[256];
430
431
10.7M
  int i;
432
10.7M
  int index = -1;
433
434
25.0M
  while (obj != NULL)
435
25.0M
  {
436
25.0M
    i = 0;
437
438
289M
    while (*p != '\0' && *p != '.' && *p != '[' && i < sizeof(str) - 1)
439
264M
    {
440
264M
      str[i++] = *p++;
441
264M
    }
442
443
25.0M
    str[i] = '\0';
444
445
25.0M
    if (obj->type != OBJECT_TYPE_STRUCTURE)
446
0
      return NULL;
447
448
25.0M
    obj = yr_object_lookup_field(obj, str);
449
450
25.0M
    if (obj == NULL)
451
0
      return NULL;
452
453
25.0M
    if (*p == '[')
454
14.2M
    {
455
14.2M
      p++;
456
457
14.2M
      if (*p == '%')
458
14.2M
      {
459
14.2M
        p++;
460
461
14.2M
        switch (*p++)
462
14.2M
        {
463
14.2M
        case 'i':
464
14.2M
          index = va_arg(args, int);
465
14.2M
          break;
466
5.42k
        case 's':
467
5.42k
          key = va_arg(args, const char*);
468
5.42k
          break;
469
470
0
        default:
471
0
          return NULL;
472
14.2M
        }
473
14.2M
      }
474
0
      else if (*p >= '0' && *p <= '9')
475
0
      {
476
0
        index = (int) strtol(p, (char**) &p, 10);
477
0
      }
478
0
      else if (*p == '"')
479
0
      {
480
0
        i = 0;
481
0
        p++;  // skip the opening quotation mark
482
483
0
        while (*p != '"' && *p != '\0' && i < sizeof(str) - 1) str[i++] = *p++;
484
485
0
        str[i] = '\0';
486
0
        p++;  // skip the closing quotation mark
487
0
        key = str;
488
0
      }
489
0
      else
490
0
      {
491
0
        return NULL;
492
0
      }
493
494
14.2M
      assert(*p == ']');
495
14.2M
      p++;
496
14.2M
      assert(*p == '.' || *p == '\0');
497
498
14.2M
      switch (obj->type)
499
14.2M
      {
500
14.2M
      case OBJECT_TYPE_ARRAY:
501
14.2M
        assert(index != -1);
502
14.2M
        obj = yr_object_array_get_item(obj, flags, index);
503
14.2M
        break;
504
505
5.42k
      case OBJECT_TYPE_DICTIONARY:
506
5.42k
        assert(key != NULL);
507
5.42k
        obj = yr_object_dict_get_item(obj, flags, key);
508
5.42k
        break;
509
14.2M
      }
510
14.2M
    }
511
512
25.0M
    if (*p == '\0')
513
10.7M
      break;
514
515
14.3M
    p++;
516
14.3M
  }
517
518
10.7M
  return obj;
519
10.7M
}
520
521
YR_OBJECT* yr_object_lookup(
522
    YR_OBJECT* object,
523
    int flags,
524
    const char* pattern,
525
    ...)
526
0
{
527
0
  YR_OBJECT* result;
528
529
0
  va_list args;
530
0
  va_start(args, pattern);
531
532
0
  result = _yr_object_lookup(object, flags, pattern, args);
533
534
0
  va_end(args);
535
536
0
  return result;
537
0
}
538
539
int yr_object_copy(YR_OBJECT* object, YR_OBJECT** object_copy)
540
13.5M
{
541
13.5M
  YR_OBJECT* copy;
542
13.5M
  YR_OBJECT* o;
543
544
13.5M
  YR_STRUCTURE_MEMBER* structure_member;
545
546
13.5M
  *object_copy = NULL;
547
548
13.5M
  FAIL_ON_ERROR(
549
13.5M
      yr_object_create(object->type, object->identifier, NULL, &copy));
550
551
13.5M
  copy->canary = object->canary;
552
553
13.5M
  switch (object->type)
554
13.5M
  {
555
7.13M
  case OBJECT_TYPE_INTEGER:
556
7.13M
    copy->value.i = object->value.i;
557
7.13M
    break;
558
559
0
  case OBJECT_TYPE_FLOAT:
560
0
    copy->value.d = object->value.d;
561
0
    break;
562
563
3.64M
  case OBJECT_TYPE_STRING:
564
565
3.64M
    if (object->value.ss != NULL)
566
0
      copy->value.ss = ss_dup(object->value.ss);
567
3.64M
    else
568
3.64M
      copy->value.ss = NULL;
569
570
3.64M
    break;
571
572
0
  case OBJECT_TYPE_FUNCTION:
573
574
0
    FAIL_ON_ERROR_WITH_CLEANUP(
575
0
        yr_object_copy(
576
0
            object_as_function(object)->return_obj,
577
0
            &object_as_function(copy)->return_obj),
578
        // cleanup
579
0
        yr_object_destroy(copy));
580
581
0
    for (int i = 0; i < YR_MAX_OVERLOADED_FUNCTIONS; i++)
582
0
      object_as_function(copy)->prototypes[i] =
583
0
          object_as_function(object)->prototypes[i];
584
585
0
    break;
586
587
2.69M
  case OBJECT_TYPE_STRUCTURE:
588
589
2.69M
    structure_member = object_as_structure(object)->members;
590
591
13.5M
    while (structure_member != NULL)
592
10.8M
    {
593
10.8M
      FAIL_ON_ERROR_WITH_CLEANUP(
594
10.8M
          yr_object_copy(structure_member->object, &o),
595
10.8M
          yr_object_destroy(copy));
596
597
10.8M
      FAIL_ON_ERROR_WITH_CLEANUP(yr_object_structure_set_member(copy, o),
598
                                 // cleanup
599
10.8M
                                 yr_free(o);
600
10.8M
                                 yr_object_destroy(copy));
601
602
10.8M
      structure_member = structure_member->next;
603
10.8M
    }
604
605
2.69M
    break;
606
607
2.69M
  case OBJECT_TYPE_ARRAY:
608
609
54.1k
    FAIL_ON_ERROR_WITH_CLEANUP(
610
54.1k
        yr_object_copy(object_as_array(object)->prototype_item, &o),
611
54.1k
        yr_object_destroy(copy));
612
613
54.1k
    object_as_array(copy)->prototype_item = o;
614
615
54.1k
    break;
616
617
0
  case OBJECT_TYPE_DICTIONARY:
618
619
0
    FAIL_ON_ERROR_WITH_CLEANUP(
620
0
        yr_object_copy(object_as_dictionary(object)->prototype_item, &o),
621
0
        yr_object_destroy(copy));
622
623
0
    object_as_dictionary(copy)->prototype_item = o;
624
625
0
    break;
626
627
0
  default:
628
0
    assert(false);
629
13.5M
  }
630
631
13.5M
  *object_copy = copy;
632
633
13.5M
  return ERROR_SUCCESS;
634
13.5M
}
635
636
int yr_object_structure_set_member(YR_OBJECT* object, YR_OBJECT* member)
637
13.3M
{
638
13.3M
  YR_STRUCTURE_MEMBER* sm;
639
640
13.3M
  assert(object->type == OBJECT_TYPE_STRUCTURE);
641
642
  // Check if the object already have a member with the same identifier
643
644
13.3M
  if (yr_object_lookup_field(object, member->identifier) != NULL)
645
0
    return ERROR_DUPLICATED_STRUCTURE_MEMBER;
646
647
13.3M
  sm = (YR_STRUCTURE_MEMBER*) yr_malloc(sizeof(YR_STRUCTURE_MEMBER));
648
649
13.3M
  if (sm == NULL)
650
0
    return ERROR_INSUFFICIENT_MEMORY;
651
652
13.3M
  member->parent = object;
653
13.3M
  sm->object = member;
654
13.3M
  sm->next = object_as_structure(object)->members;
655
656
13.3M
  object_as_structure(object)->members = sm;
657
658
13.3M
  return ERROR_SUCCESS;
659
13.3M
}
660
661
YR_API int yr_object_array_length(YR_OBJECT* object)
662
0
{
663
0
  YR_OBJECT_ARRAY* array;
664
665
0
  assert(object->type == OBJECT_TYPE_ARRAY);
666
0
  array = object_as_array(object);
667
668
0
  if (array->items == NULL)
669
0
    return 0;
670
671
0
  return array->items->length;
672
0
}
673
674
YR_API YR_OBJECT* yr_object_array_get_item(YR_OBJECT* object, int flags,
675
                                           int index)
676
14.3M
{
677
14.3M
  YR_OBJECT* result = NULL;
678
14.3M
  YR_OBJECT_ARRAY* array;
679
680
14.3M
  assert(object->type == OBJECT_TYPE_ARRAY);
681
682
14.3M
  if (index < 0)
683
0
    return NULL;
684
685
14.3M
  array = object_as_array(object);
686
687
14.3M
  if (array->items != NULL && array->items->capacity > index)
688
14.2M
    result = array->items->objects[index];
689
690
14.3M
  if (result == NULL && flags & OBJECT_CREATE)
691
2.64M
  {
692
2.64M
    yr_object_copy(array->prototype_item, &result);
693
694
2.64M
    if (result != NULL)
695
2.64M
      yr_object_array_set_item(object, result, index);
696
2.64M
  }
697
698
14.3M
  return result;
699
14.3M
}
700
701
int yr_object_array_set_item(YR_OBJECT* object, YR_OBJECT* item, int index)
702
2.64M
{
703
2.64M
  YR_OBJECT_ARRAY* array;
704
705
2.64M
  int capacity;
706
707
2.64M
  assert(index >= 0);
708
2.64M
  assert(object->type == OBJECT_TYPE_ARRAY);
709
710
2.64M
  array = object_as_array(object);
711
712
2.64M
  if (array->items == NULL)
713
65.5k
  {
714
65.5k
    capacity = 64;
715
716
65.5k
    while (capacity <= index) capacity *= 2;
717
718
65.5k
    array->items = (YR_ARRAY_ITEMS*) yr_malloc(
719
65.5k
        sizeof(YR_ARRAY_ITEMS) + capacity * sizeof(YR_OBJECT*));
720
721
65.5k
    if (array->items == NULL)
722
0
      return ERROR_INSUFFICIENT_MEMORY;
723
724
65.5k
    memset(array->items->objects, 0, capacity * sizeof(YR_OBJECT*));
725
726
65.5k
    array->items->capacity = capacity;
727
65.5k
    array->items->length = 0;
728
65.5k
  }
729
2.57M
  else if (index >= array->items->capacity)
730
10.9k
  {
731
10.9k
    capacity = array->items->capacity * 2;
732
733
10.9k
    while (capacity <= index) capacity *= 2;
734
735
10.9k
    array->items = (YR_ARRAY_ITEMS*) yr_realloc(
736
10.9k
        array->items, sizeof(YR_ARRAY_ITEMS) + capacity * sizeof(YR_OBJECT*));
737
738
10.9k
    if (array->items == NULL)
739
0
      return ERROR_INSUFFICIENT_MEMORY;
740
741
2.57M
    for (int i = array->items->capacity; i < capacity; i++)
742
2.56M
      array->items->objects[i] = NULL;
743
744
10.9k
    array->items->capacity = capacity;
745
10.9k
  }
746
747
2.64M
  item->parent = object;
748
2.64M
  array->items->objects[index] = item;
749
750
2.64M
  if (index >= array->items->length)
751
2.64M
    array->items->length = index + 1;
752
753
2.64M
  return ERROR_SUCCESS;
754
2.64M
}
755
756
YR_OBJECT* yr_object_dict_get_item(
757
    YR_OBJECT* object,
758
    int flags,
759
    const char* key)
760
5.42k
{
761
5.42k
  YR_OBJECT* result = NULL;
762
5.42k
  YR_OBJECT_DICTIONARY* dict;
763
764
5.42k
  assert(object->type == OBJECT_TYPE_DICTIONARY);
765
766
5.42k
  dict = object_as_dictionary(object);
767
768
5.42k
  if (dict->items != NULL)
769
5.17k
  {
770
365k
    for (int i = 0; i < dict->items->used; i++)
771
360k
    {
772
360k
      if (strcmp(dict->items->objects[i].key->c_string, key) == 0)
773
2.21k
        result = dict->items->objects[i].obj;
774
360k
    }
775
5.17k
  }
776
777
5.42k
  if (result == NULL && flags & OBJECT_CREATE)
778
3.20k
  {
779
3.20k
    yr_object_copy(dict->prototype_item, &result);
780
781
3.20k
    if (result != NULL)
782
3.20k
      yr_object_dict_set_item(object, result, key);
783
3.20k
  }
784
785
5.42k
  return result;
786
5.42k
}
787
788
int yr_object_dict_set_item(YR_OBJECT* object, YR_OBJECT* item, const char* key)
789
3.20k
{
790
3.20k
  YR_OBJECT_DICTIONARY* dict;
791
792
3.20k
  int count;
793
794
3.20k
  assert(object->type == OBJECT_TYPE_DICTIONARY);
795
796
3.20k
  dict = object_as_dictionary(object);
797
798
3.20k
  if (dict->items == NULL)
799
249
  {
800
249
    count = 64;
801
802
249
    dict->items = (YR_DICTIONARY_ITEMS*) yr_malloc(
803
249
        sizeof(YR_DICTIONARY_ITEMS) + count * sizeof(dict->items->objects[0]));
804
805
249
    if (dict->items == NULL)
806
0
      return ERROR_INSUFFICIENT_MEMORY;
807
808
249
    memset(dict->items->objects, 0, count * sizeof(dict->items->objects[0]));
809
810
249
    dict->items->free = count;
811
249
    dict->items->used = 0;
812
249
  }
813
2.95k
  else if (dict->items->free == 0)
814
22
  {
815
22
    count = dict->items->used * 2;
816
22
    dict->items = (YR_DICTIONARY_ITEMS*) yr_realloc(
817
22
        dict->items,
818
22
        sizeof(YR_DICTIONARY_ITEMS) + count * sizeof(dict->items->objects[0]));
819
820
22
    if (dict->items == NULL)
821
0
      return ERROR_INSUFFICIENT_MEMORY;
822
823
3.03k
    for (int i = dict->items->used; i < count; i++)
824
3.00k
    {
825
3.00k
      dict->items->objects[i].key = NULL;
826
3.00k
      dict->items->objects[i].obj = NULL;
827
3.00k
    }
828
829
22
    dict->items->free = dict->items->used;
830
22
  }
831
832
3.20k
  item->parent = object;
833
834
3.20k
  dict->items->objects[dict->items->used].key = ss_new(key);
835
3.20k
  dict->items->objects[dict->items->used].obj = item;
836
837
3.20k
  dict->items->used++;
838
3.20k
  dict->items->free--;
839
840
3.20k
  return ERROR_SUCCESS;
841
3.20k
}
842
843
bool yr_object_has_undefined_value(YR_OBJECT* object, const char* field, ...)
844
0
{
845
0
  YR_OBJECT* field_obj;
846
847
0
  va_list args;
848
0
  va_start(args, field);
849
850
0
  if (field != NULL)
851
0
    field_obj = _yr_object_lookup(object, 0, field, args);
852
0
  else
853
0
    field_obj = object;
854
855
0
  va_end(args);
856
857
0
  if (field_obj == NULL)
858
0
    return true;
859
860
0
  switch (field_obj->type)
861
0
  {
862
0
  case OBJECT_TYPE_FLOAT:
863
0
    return yr_isnan(field_obj->value.d);
864
0
  case OBJECT_TYPE_STRING:
865
0
    return field_obj->value.ss == NULL;
866
0
  case OBJECT_TYPE_INTEGER:
867
0
    return field_obj->value.i == YR_UNDEFINED;
868
0
  }
869
870
0
  return false;
871
0
}
872
873
int64_t yr_object_get_integer(YR_OBJECT* object, const char* field, ...)
874
53.2k
{
875
53.2k
  YR_OBJECT* integer_obj;
876
877
53.2k
  va_list args;
878
53.2k
  va_start(args, field);
879
880
53.2k
  if (field != NULL)
881
53.2k
    integer_obj = _yr_object_lookup(object, 0, field, args);
882
0
  else
883
0
    integer_obj = object;
884
885
53.2k
  va_end(args);
886
887
53.2k
  if (integer_obj == NULL)
888
1.06k
    return YR_UNDEFINED;
889
890
52.1k
  assertf(
891
52.1k
      integer_obj->type == OBJECT_TYPE_INTEGER,
892
52.1k
      "type of \"%s\" is not integer\n",
893
52.1k
      field);
894
895
52.1k
  return integer_obj->value.i;
896
52.1k
}
897
898
double yr_object_get_float(YR_OBJECT* object, const char* field, ...)
899
0
{
900
0
  YR_OBJECT* double_obj;
901
902
0
  va_list args;
903
0
  va_start(args, field);
904
905
0
  if (field != NULL)
906
0
    double_obj = _yr_object_lookup(object, 0, field, args);
907
0
  else
908
0
    double_obj = object;
909
910
0
  va_end(args);
911
912
0
  if (double_obj == NULL)
913
0
    return NAN;
914
915
0
  assertf(
916
0
      double_obj->type == OBJECT_TYPE_FLOAT,
917
0
      "type of \"%s\" is not double\n",
918
0
      field);
919
920
0
  return double_obj->value.d;
921
0
}
922
923
SIZED_STRING* yr_object_get_string(YR_OBJECT* object, const char* field, ...)
924
0
{
925
0
  YR_OBJECT* string_obj;
926
927
0
  va_list args;
928
0
  va_start(args, field);
929
930
0
  if (field != NULL)
931
0
    string_obj = _yr_object_lookup(object, 0, field, args);
932
0
  else
933
0
    string_obj = object;
934
935
0
  va_end(args);
936
937
0
  if (string_obj == NULL)
938
0
    return NULL;
939
940
0
  assertf(
941
0
      string_obj->type == OBJECT_TYPE_STRING,
942
0
      "type of \"%s\" is not string\n",
943
0
      field);
944
945
0
  return string_obj->value.ss;
946
0
}
947
948
int yr_object_set_integer(
949
    int64_t value,
950
    YR_OBJECT* object,
951
    const char* field,
952
    ...)
953
8.57M
{
954
8.57M
  YR_OBJECT* integer_obj;
955
956
8.57M
  va_list args;
957
8.57M
  va_start(args, field);
958
959
8.57M
  if (field != NULL)
960
8.57M
    integer_obj = _yr_object_lookup(object, OBJECT_CREATE, field, args);
961
2.09k
  else
962
2.09k
    integer_obj = object;
963
964
8.57M
  va_end(args);
965
966
8.57M
  if (integer_obj == NULL)
967
0
  {
968
0
    if (field != NULL)
969
0
      return ERROR_INSUFFICIENT_MEMORY;
970
0
    else
971
0
      return ERROR_INVALID_ARGUMENT;
972
0
  }
973
974
8.57M
  assert(integer_obj->type == OBJECT_TYPE_INTEGER);
975
976
8.57M
  integer_obj->value.i = value;
977
978
8.57M
  return ERROR_SUCCESS;
979
8.57M
}
980
981
int yr_object_set_float(double value, YR_OBJECT* object, const char* field, ...)
982
0
{
983
0
  YR_OBJECT* double_obj;
984
985
0
  va_list args;
986
0
  va_start(args, field);
987
988
0
  if (field != NULL)
989
0
    double_obj = _yr_object_lookup(object, OBJECT_CREATE, field, args);
990
0
  else
991
0
    double_obj = object;
992
993
0
  va_end(args);
994
995
0
  if (double_obj == NULL)
996
0
  {
997
0
    if (field != NULL)
998
0
      return ERROR_INSUFFICIENT_MEMORY;
999
0
    else
1000
0
      return ERROR_INVALID_ARGUMENT;
1001
0
  }
1002
1003
0
  assert(double_obj->type == OBJECT_TYPE_FLOAT);
1004
1005
0
  double_obj->value.d = value;
1006
1007
0
  return ERROR_SUCCESS;
1008
0
}
1009
1010
int yr_object_set_string(
1011
    const char* value,
1012
    size_t len,
1013
    YR_OBJECT* object,
1014
    const char* field,
1015
    ...)
1016
2.07M
{
1017
2.07M
  YR_OBJECT* string_obj;
1018
1019
2.07M
  va_list args;
1020
2.07M
  va_start(args, field);
1021
1022
2.07M
  if (field != NULL)
1023
2.07M
    string_obj = _yr_object_lookup(object, OBJECT_CREATE, field, args);
1024
0
  else
1025
0
    string_obj = object;
1026
1027
2.07M
  va_end(args);
1028
1029
2.07M
  if (string_obj == NULL)
1030
0
  {
1031
0
    if (field != NULL)
1032
0
      return ERROR_INSUFFICIENT_MEMORY;
1033
0
    else
1034
0
      return ERROR_INVALID_ARGUMENT;
1035
0
  }
1036
1037
2.07M
  assert(string_obj->type == OBJECT_TYPE_STRING);
1038
1039
2.07M
  if (string_obj->value.ss != NULL)
1040
2.21k
    yr_free(string_obj->value.ss);
1041
1042
2.07M
  if (value != NULL)
1043
2.07M
  {
1044
2.07M
    string_obj->value.ss = (SIZED_STRING*) yr_malloc(
1045
2.07M
        len + sizeof(SIZED_STRING));
1046
1047
2.07M
    if (string_obj->value.ss == NULL)
1048
0
      return ERROR_INSUFFICIENT_MEMORY;
1049
1050
2.07M
    string_obj->value.ss->length = (uint32_t) len;
1051
2.07M
    string_obj->value.ss->flags = 0;
1052
1053
2.07M
    memcpy(string_obj->value.ss->c_string, value, len);
1054
2.07M
    string_obj->value.ss->c_string[len] = '\0';
1055
2.07M
  }
1056
718
  else
1057
718
  {
1058
718
    string_obj->value.ss = NULL;
1059
718
  }
1060
1061
2.07M
  return ERROR_SUCCESS;
1062
2.07M
}
1063
1064
YR_OBJECT* yr_object_get_root(YR_OBJECT* object)
1065
2.09k
{
1066
2.09k
  YR_OBJECT* o = object;
1067
1068
4.19k
  while (o->parent != NULL) o = o->parent;
1069
1070
2.09k
  return o;
1071
2.09k
}
1072
1073
YR_API void yr_object_print_data(
1074
    YR_OBJECT* object,
1075
    int indent,
1076
    int print_identifier)
1077
0
{
1078
0
  YR_DICTIONARY_ITEMS* dict_items;
1079
0
  YR_STRUCTURE_MEMBER* member;
1080
1081
0
  char indent_spaces[32];
1082
1083
0
  indent = yr_min(indent, sizeof(indent_spaces) - 1);
1084
1085
0
  memset(indent_spaces, '\t', indent);
1086
0
  indent_spaces[indent] = '\0';
1087
1088
0
  if (print_identifier && object->type != OBJECT_TYPE_FUNCTION)
1089
0
    printf("%s%s", indent_spaces, object->identifier);
1090
1091
0
  switch (object->type)
1092
0
  {
1093
0
  case OBJECT_TYPE_FLOAT:
1094
0
    if (object->value.i != YR_UNDEFINED)
1095
0
      printf(" = %f", object->value.d);
1096
0
    else
1097
0
      printf(" = YR_UNDEFINED");
1098
1099
0
    break;
1100
1101
0
  case OBJECT_TYPE_INTEGER:
1102
1103
0
    if (object->value.i != YR_UNDEFINED)
1104
0
      printf(" = %" PRId64, object->value.i);
1105
0
    else
1106
0
      printf(" = YR_UNDEFINED");
1107
1108
0
    break;
1109
1110
0
  case OBJECT_TYPE_STRING:
1111
1112
0
    if (object->value.ss != NULL)
1113
0
    {
1114
0
      printf(" = \"");
1115
1116
0
      for (size_t l = 0; l < object->value.ss->length; l++)
1117
0
      {
1118
0
        char c = object->value.ss->c_string[l];
1119
1120
0
        if (isprint((unsigned char) c))
1121
0
          printf("%c", c);
1122
0
        else
1123
0
          printf("\\x%02x", (unsigned char) c);
1124
0
      }
1125
1126
0
      printf("\"");
1127
0
    }
1128
0
    else
1129
0
    {
1130
0
      printf(" = YR_UNDEFINED");
1131
0
    }
1132
1133
0
    break;
1134
1135
0
  case OBJECT_TYPE_STRUCTURE:
1136
1137
0
    member = object_as_structure(object)->members;
1138
1139
0
    while (member != NULL)
1140
0
    {
1141
0
      if (member->object->type != OBJECT_TYPE_FUNCTION)
1142
0
      {
1143
0
        printf("\n");
1144
0
        yr_object_print_data(member->object, indent + 1, 1);
1145
0
      }
1146
0
      member = member->next;
1147
0
    }
1148
1149
0
    break;
1150
1151
0
  case OBJECT_TYPE_ARRAY:
1152
0
    for (int i = 0; i < yr_object_array_length(object); i++)
1153
0
    {
1154
0
      YR_OBJECT* o = yr_object_array_get_item(object, 0, i);
1155
1156
0
      if (o != NULL)
1157
0
      {
1158
0
        printf("\n%s\t[%d]", indent_spaces, i);
1159
0
        yr_object_print_data(o, indent + 1, 0);
1160
0
      }
1161
0
    }
1162
0
    break;
1163
1164
0
  case OBJECT_TYPE_DICTIONARY:
1165
1166
0
    dict_items = object_as_dictionary(object)->items;
1167
1168
0
    if (dict_items != NULL)
1169
0
    {
1170
0
      for (int i = 0; i < dict_items->used; i++)
1171
0
      {
1172
0
        printf("\n%s\t%s", indent_spaces, dict_items->objects[i].key->c_string);
1173
1174
0
        yr_object_print_data(dict_items->objects[i].obj, indent + 1, 0);
1175
0
      }
1176
0
    }
1177
1178
0
    break;
1179
0
  }
1180
0
}