-
Notifications
You must be signed in to change notification settings - Fork 1
/
cliffi_testlib.c
858 lines (752 loc) · 21.7 KB
/
cliffi_testlib.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
#include <ctype.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Simple function that adds two integers
int add(int a, int b) {
return a + b;
}
// Function that concatenates two strings
const char* concat(const char* a, const char* b) {
int total_length = strlen(a) + strlen(b);
char* result = malloc(total_length + 1);
result[0] = '\0'; // Initialize the string
strcpy(result, a);
strcat(result, b);
return result;
}
// Function that multiplies two floating-point numbers
double multiply(double a, double b) {
return a * b;
}
// A function that takes a pointer to an integer, increments it, and returns the result
int increment_at_pointer(int* a) { //deliberately unsafe
(*a)++;
return *a;
}
int* get_array_of_int(int a, size_t size) {
int* arr = calloc(size, sizeof(int));
for (size_t i = 0; i < size; i++) {
arr[i] = a;
}
return arr;
}
// A function that returns a static string
char* get_message() {
return "Hello, cliffi!"; // this maybe shows us we need to handle string literals in data?
}
// Struct for demonstration
typedef struct Point {
int x;
double y;
} Point;
void hexdump(const void* data, size_t size) {
const unsigned char* byte = (const unsigned char*)data;
size_t i, j;
bool multiline = size > 16;
if (multiline) printf("(Hexvalue)\nOffset\n");
for (i = 0; i < size; i += 16) {
if (multiline) printf("%08zx ", i); // Offset
// Hex bytes
for (j = 0; j < 16; j++) {
if (j == 8) printf(" "); // Add space between the two halves of the hexdump
if (i + j < size) {
printf("%02x ", byte[i + j]);
} else {
if (multiline) printf(" "); // Fill space if less than 16 bytes in the line
}
}
if (multiline) printf(" ");
if (!multiline) printf("= ");
// ASCII characters
for (j = 0; j < 16; j++) {
if (i + j < size) {
printf("%c", isprint(byte[i + j]) ? byte[i + j] : '.');
}
}
printf("\n");
}
}
int get_x(Point p) {
// print a hexdump of the struct
hexdump(&p, sizeof(Point));
printf("p->x: %d\n", p.x);
printf("p->y: %f\n", p.y);
return p.x;
}
int get_x_from_structpointer(Point* p) {
hexdump(p, sizeof(Point));
printf("p->x: %d\n", p->x);
printf("p->y: %f\n", p->y);
if (p) {
return p->x;
}
return 0;
}
// Function that modifies a struct
void modify_point(Point* p, int x, double y) {
hexdump(p, sizeof(Point));
printf("p->x: %d\n", p->x);
printf("p->y: %f\n", p->y);
printf("Adding %d to x and %f to y\n", x, y);
// if (p) {
p->x += x;
p->y += y;
printf("p->x: %d\n", p->x);
printf("p->y: %f\n", p->y);
// }
}
Point get_point(int x, double y) {
Point p = {x, y};
return p;
}
Point* get_p_point(int x, double y) {
Point* p = malloc(sizeof(Point));
p->x = x;
p->y = y;
return p;
}
typedef struct ComplexStruct {
unsigned char c;
int x;
double y;
unsigned char c2;
Point p;
int x2;
double y2;
Point* p2;
} ComplexStruct;
void test_complex_struct(ComplexStruct s) {
hexdump(&s, sizeof(ComplexStruct));
printf("s.c: %c\n", s.c);
printf("s.x: %d\n", s.x);
printf("s.y: %f\n", s.y);
printf("s.c2: %c\n", s.c2);
// printf("s.p->x: %d\n", (*s.p)->x);
// printf("s.p->y: %f\n", (*s.p)->y);
// hexdump(*s.p, sizeof(Point));
printf("s.p->x: %d\n", s.p.x);
printf("s.p->y: %f\n", s.p.y);
hexdump(&s.p, sizeof(Point));
printf("s.x2: %d\n", s.x2);
printf("s.y2: %f\n", s.y2);
printf("s.p2.x: %d\n", s.p2->x);
printf("s.p2.y: %f\n", s.p2->y);
hexdump(s.p2, sizeof(Point));
}
void test_p_complex_struct(ComplexStruct* s) {
hexdump(s, sizeof(ComplexStruct));
printf("s->c: %c\n", s->c);
printf("s->x: %d\n", s->x);
printf("s->y: %f\n", s->y);
printf("s->c2: %c\n", s->c2);
// printf("s->p->x: %d\n", (*s->p)->x);
// printf("s->p->y: %f\n", (*s->p)->y);
// hexdump(*s->p, sizeof(Point));
printf("s->p->x: %d\n", s->p.x);
printf("s->p->y: %f\n", s->p.y);
hexdump(&s->p, sizeof(Point));
printf("s->x2: %d\n", s->x2);
printf("s->y2: %f\n", s->y2);
printf("s->p2.x: %d\n", s->p2->x);
printf("s->p2.y: %f\n", s->p2->y);
hexdump(s->p2, sizeof(Point));
}
ComplexStruct get_complex_struct(unsigned char c, int x, double y, unsigned char c2, int x2, double y2, int x3, double y3, int x4, double y4) {
ComplexStruct s = {};
// typedef struct ComplexStruct {
// unsigned char c;
// int x;
// double y;
// unsigned char c2;
// Point p;
// int x2;
// double y2;
// Point* p2;
// } ComplexStruct;
s.c = c;
s.x = x;
s.y = y;
s.c2 = c2;
s.p = get_point(x2, y2);
s.x2 = x3;
s.y2 = y3;
s.p2 = get_p_point(x4, y4);
return s;
}
ComplexStruct* get_p_complex_struct(unsigned char c, int x, double y, unsigned char c2, int x2, double y2, int x3, double y3, int x4, double y4) {
ComplexStruct* s = malloc(sizeof(ComplexStruct));
s->c = c;
s->x = x;
s->y = y;
s->c2 = c2;
s->p = get_point(x2, y2);
s->x2 = x3;
s->y2 = y3;
s->p2 = get_p_point(x4, y4);
return s;
}
struct larger_struct {
int x;
double y;
char c;
char* s;
};
struct struct_containing_larger_struct {
char c;
struct larger_struct s;
int x;
};
struct larger_struct get_larger_struct(int x, double y, char c, const char* s) {
struct larger_struct larger_struct;
larger_struct.x = x;
larger_struct.y = y;
larger_struct.c = c;
larger_struct.s = malloc(strlen(s) + 1);
strcpy(larger_struct.s, s);
return larger_struct;
}
struct larger_struct* get_p_larger_struct(int x, double y, char c, const char* s) {
struct larger_struct* larger_struct_ptr = malloc(sizeof(struct larger_struct));
larger_struct_ptr->x = x;
larger_struct_ptr->y = y;
larger_struct_ptr->c = c;
larger_struct_ptr->s = malloc(strlen(s) + 1);
strcpy(larger_struct_ptr->s, s);
return larger_struct_ptr;
}
void test_nested_large_struct(struct struct_containing_larger_struct s) {
hexdump(&s, sizeof(struct struct_containing_larger_struct));
printf("s.c: %c\n", s.c);
printf("s.s.x: %d\n", s.s.x);
printf("s.s.y: %f\n", s.s.y);
printf("s.s.c: %c\n", s.s.c);
printf("s.s.s: %s\n", s.s.s);
printf("s.x: %d\n", s.x);
}
struct struct_containing_larger_struct get_nested_larger_struct(char c, int x, double y, char c2, const char* s, int x2) {
struct struct_containing_larger_struct my_struct = {};
my_struct.c = c;
my_struct.s.x = x;
my_struct.s.y = y;
my_struct.s.c = c2;
my_struct.s.s = strdup(s);
my_struct.x = x2;
return my_struct;
}
struct struct_containing_larger_struct* get_p_nested_larger_struct(char c, int x, double y, char c2, const char* s, int x2) {
struct struct_containing_larger_struct* my_struct = malloc(sizeof(struct struct_containing_larger_struct));
my_struct->c = c;
my_struct->s.x = x;
my_struct->s.y = y;
my_struct->s.c = c2;
my_struct->s.s = strdup(s);
my_struct->x = x2;
return my_struct;
}
void test_fixed_buffer_10_chars_arg_then_double_arg(char* arr, double d) {
hexdump(arr, 10);
printf("s: %s\n", arr);
printf("d: %f\n", d);
}
struct simple_struct_embedded_array_10_chars_then_double {
char s[10];
double d;
};
void test_simple_struct_embedded_array_10_chars_then_double(struct simple_struct_embedded_array_10_chars_then_double s) {
hexdump(&s, sizeof(struct simple_struct_embedded_array_10_chars_then_double));
printf("s.s: %s\n", s.s);
printf("s.d: %f\n", s.d);
}
struct even_simpler_struct_embedded_array_10_chars {
char s[10];
};
void test_even_simpler_struct_embedded_array_10_chars(struct even_simpler_struct_embedded_array_10_chars s) {
hexdump(&s, sizeof(struct even_simpler_struct_embedded_array_10_chars));
printf("s.s: %s\n", s.s);
}
struct struct_with_embedded_array_pointer {
char (*s)[10];
};
void test_struct_with_char_pointer(struct struct_with_embedded_array_pointer s) {
hexdump(&s, sizeof(struct struct_with_embedded_array_pointer));
printf("s: %s\n", *s.s);
for (int i = 0; i < 10; i++) { // 10th element is null terminator
printf("s.s[%d]: %c\n", i, (*s.s)[i]);
}
}
struct struct_with_embedded_array_pointer return_struct_with_embedded_array_pointer() {
struct struct_with_embedded_array_pointer s = {};
s.s = malloc(10);
strcpy(*s.s, "hello");
return s;
}
void test_p_struct_with_char_pointer(struct struct_with_embedded_array_pointer* s) {
hexdump(s, sizeof(struct struct_with_embedded_array_pointer));
hexdump(s->s, 10);
printf("s: %s\n", *s->s);
for (int i = 0; i < 10; i++) { // 10th element is null terminator
printf("s.s[%d]: %c\n", i, (*s->s)[i]);
}
}
struct struct_with_embedded_array_pointer* return_p_struct_with_embedded_array_pointer() {
struct struct_with_embedded_array_pointer* s = malloc(sizeof(struct struct_with_embedded_array_pointer));
s->s = malloc(10);
strcpy(*s->s, "hello");
return s;
}
struct struct_with_embedded_array {
int x;
char s[10];
int a[3];
int y;
};
struct struct_with_embedded_array_intonly {
int x;
int a[3];
int y;
};
struct struct_with_embedded_array_charonly {
int x;
char s[10];
int y;
};
void test_struct_with_embedded_array(struct struct_with_embedded_array s) {
hexdump(&s, sizeof(struct struct_with_embedded_array));
printf("s.x: %d\n", s.x);
printf("s.s: %s\n", s.s);
hexdump(s.s, 10);
printf("s.a[0]: %d\n", s.a[0]);
printf("s.a[1]: %d\n", s.a[1]);
printf("s.a[2]: %d\n", s.a[2]);
printf("s.y: %d\n", s.y);
}
void test_p_struct_with_embedded_array(struct struct_with_embedded_array* s) {
hexdump(s, sizeof(struct struct_with_embedded_array));
printf("s->x: %d\n", s->x);
printf("s->s: %s\n", s->s);
hexdump(s->s, 10);
printf("s->a[0]: %d\n", s->a[0]);
printf("s->a[1]: %d\n", s->a[1]);
printf("s->a[2]: %d\n", s->a[2]);
printf("s->y: %d\n", s->y);
}
void test_p_struct_with_embedded_array_intonly(struct struct_with_embedded_array_intonly* s) {
hexdump(s, sizeof(struct struct_with_embedded_array_intonly));
printf("s->x: %d\n", s->x);
printf("s->a[0]: %d\n", s->a[0]);
printf("s->a[1]: %d\n", s->a[1]);
printf("s->a[2]: %d\n", s->a[2]);
printf("s->y: %d\n", s->y);
}
struct struct_with_embedded_array_intonly test_p_struct_with_embedded_array_intonly_return_struct(struct struct_with_embedded_array_intonly* s) {
hexdump(s, sizeof(struct struct_with_embedded_array_intonly));
printf("s->x: %d\n", s->x);
printf("s->a[0]: %d\n", s->a[0]);
printf("s->a[1]: %d\n", s->a[1]);
printf("s->a[2]: %d\n", s->a[2]);
printf("s->y: %d\n", s->y);
return *s;
}
struct struct_with_embedded_array_intonly return_struct_with_embedded_array_intonly() {
struct struct_with_embedded_array_intonly s = {};
s.x = 1;
s.a[0] = 2;
s.a[1] = 3;
s.a[2] = 4;
s.y = 5;
return s;
}
struct simple_struct_embedded_array {
int ints[3];
};
struct simple_struct_embedded_array return_simple_struct_embedded_array() {
struct simple_struct_embedded_array s = {};
s.ints[0] = 1;
s.ints[1] = 2;
s.ints[2] = 3;
return s;
}
struct struct_with_embedded_array_intonly* test_p_struct_with_embedded_array_intonly_return_p_struct(struct struct_with_embedded_array_intonly* s) {
hexdump(s, sizeof(struct struct_with_embedded_array_intonly));
printf("s->x: %d\n", s->x);
printf("s->a[0]: %d\n", s->a[0]);
printf("s->a[1]: %d\n", s->a[1]);
printf("s->a[2]: %d\n", s->a[2]);
printf("s->y: %d\n", s->y);
return s;
}
void test_p_struct_with_embedded_array_charonly(struct struct_with_embedded_array_charonly* s) {
hexdump(s, sizeof(struct struct_with_embedded_array_charonly));
printf("s->x: %d\n", s->x);
printf("s->s: %s\n", s->s);
printf("s->y: %d\n", s->y);
}
void test_p_struct_with_embedded_array_changed(struct struct_with_embedded_array* s) {
hexdump(s, sizeof(struct struct_with_embedded_array));
s->x = 5;
strcpy(s->s, "changed");
s->a[0] = 10;
s->a[1] = 11;
s->a[2] = 12;
s->y = 15;
}
void test_struct_with_embedded_array_changed(struct struct_with_embedded_array s) {
hexdump(&s, sizeof(struct struct_with_embedded_array));
s.x = 5;
strcpy(s.s, "changed");
s.a[0] = 10;
s.a[1] = 11;
s.a[2] = 12;
s.y = 15;
}
struct struct_with_embedded_array return_struct_with_embedded_array() {
struct struct_with_embedded_array s = {};
s.x = 1;
strcpy(s.s, "hello");
s.a[0] = 2;
s.a[1] = 3;
s.a[2] = 4;
s.y = 5;
return s;
}
struct embedded_with_pointer {
int* x;
char* s;
};
struct parent_with_embedded_pointer {
char x;
struct embedded_with_pointer p;
struct embedded_with_pointer* p2;
int y;
};
void test_parent_with_embedded_pointer(struct parent_with_embedded_pointer s) {
hexdump(&s, sizeof(struct parent_with_embedded_pointer));
printf("s.p.x: %d\n", *s.p.x);
printf("s.p.s: %s\n", s.p.s);
printf("s.p2->x: %d\n", *s.p2->x);
printf("s.p2->s: %s\n", s.p2->s);
s.p.x[0]++;
//capitalize the string
for (int i = 0; i < strlen(s.p.s); i++) {
s.p.s[i] = toupper(s.p.s[i]);
}
s.p2->x[0]++;
//capitalize the string
for (int i = 0; i < strlen(s.p2->s); i++) {
s.p2->s[i] = toupper(s.p2->s[i]);
}
}
struct simpler_parent_with_embedded_pointer {
char x;
struct embedded_with_pointer p;
};
void simple_test_parent_with_embedded_pointer(struct simpler_parent_with_embedded_pointer s) {
hexdump(&s, sizeof(struct parent_with_embedded_pointer));
printf("s.p.x: %d\n", *s.p.x);
printf("s.p.s: %s\n", s.p.s);
s.p.x[0]++;
//capitalize the string
for (int i = 0; i < strlen(s.p.s); i++) {
s.p.s[i] = toupper(s.p.s[i]);
}
}
struct simpler_parent_with_pointer_struct_embedded_pointer {
char x;
struct embedded_with_pointer *p;
};
void simple_test_parent_with_pointer_struct_embedded_pointer(struct simpler_parent_with_pointer_struct_embedded_pointer s) {
hexdump(&s, sizeof(struct simpler_parent_with_pointer_struct_embedded_pointer));
printf("s.x: %c\n", s.x);
printf("s.p: %p\n", s.p);
hexdump(s.p, sizeof(struct embedded_with_pointer));
printf("s.p->x: %d\n", *s.p->x);
printf("s.p->s: %s\n", s.p->s);
(*s.p->x)++;
//capitalize the string
for (int i = 0; i < strlen(s.p->s); i++) {
s.p->s[i] = toupper(s.p->s[i]);
}
}
void simple_test_int_pointer(int* x) {
hexdump(x, sizeof(int));
printf("x: %d\n", *x);
(*x)++;
}
struct with_int_pointer {
int* x;
};
void test_struct_with_int_pointer(struct with_int_pointer s) {
hexdump(&s, sizeof(struct with_int_pointer));
printf("s.x: %d\n", *s.x);
(*s.x)++;
}
// Function that takes an array of integers and returns the sum
int sum_array(int* arr, int size) {
int sum = 0;
for (int i = 0; i < size; i++) {
sum += arr[i];
}
return sum;
}
// Function that demonstrates deeper pointer depths
int increment_at_pointer_pointer(int** pp) {
if (pp && *pp) {
(**pp)++;
return **pp;
}
return -1;
}
// Function returning a dynamically allocated array of doubles
double* get_array_of_doubles(size_t size) {
double* arr = (double*)malloc(size * sizeof(double));
for (size_t i = 0; i < size; ++i) {
arr[i] = i * 0.5; // Arbitrary values
}
return arr;
}
const char* concat_str_array(const char** strs, int count) {
int total_length = 0;
for (int i = 0; i < count; i++) {
total_length += strlen(strs[i]);
}
char* result = (char*)malloc(total_length + 1);
result[0] = '\0'; // Initialize the string
for (int i = 0; i < count; i++) {
strncat(result, strs[i], total_length);
}
return result;
}
const char* buffer_as_return(const char* buffer, size_t size) {
char* retval = malloc(size);
memcpy(retval, buffer, size);
return retval;
}
struct __attribute__((packed)) PackedStruct {
char c;
int i;
char s[2];
double d;
int a[3];
char c2;
};
struct UnPackedStruct {
char c;
int i;
char s[2];
double d;
int a[3];
char c2;
};
struct PackedStruct get_packed_struct() {
struct PackedStruct s = {};
s.c = 'a';
s.i = 42;
s.s[0] = 'b';
s.s[1] = 'c';
s.d = 3.14;
s.a[0] = 1;
s.a[1] = 2;
s.a[2] = 3;
s.c2 = 'd';
return s;
}
struct UnPackedStruct get_unpacked_struct() {
struct UnPackedStruct s = {};
s.c = 'a';
s.i = 42;
s.s[0] = 'b';
s.s[1] = 'c';
s.d = 3.14;
s.a[0] = 1;
s.a[1] = 2;
s.a[2] = 3;
s.c2 = 'd';
return s;
}
// called as v -SK: c i ac2 d ai3 c :S
struct PackedStruct* get_p_packed_struct() {
struct PackedStruct* s = malloc(sizeof(struct PackedStruct));
s->c = 'a';
s->i = 42;
s->s[0] = 'b';
s->s[1] = 'c';
s->d = 3.14;
s->a[0] = 1;
s->a[1] = 2;
s->a[2] = 3;
s->c2 = 'd';
return s;
}
void test_packed_struct(struct PackedStruct s) {
printf("sizeof(struct PackedStruct): %zu\n", sizeof(struct PackedStruct));
hexdump(&s, sizeof(struct PackedStruct));
printf("s.c: %c\n", s.c);
printf("s.i: %d\n", s.i);
printf("s.s[0]: %c\n", s.s[0]);
printf("s.s[1]: %c\n", s.s[1]);
printf("s.d: %f\n", s.d);
printf("s.a: %d %d %d\n", s.a[0], s.a[1], s.a[2]);
printf("s.c2: %c\n", s.c2);
}
void test_p_packed_struct(struct PackedStruct* s) {
hexdump(s, sizeof(struct PackedStruct));
printf("s->c: %c\n", s->c);
printf("s->i: %d\n", s->i);
printf("s->s: %c%c\n", s->s[0], s->s[1]);
printf("s->d: %f\n", s->d);
printf("s->a: %d %d %d\n", s->a[0], s->a[1], s->a[2]);
printf("s->c2: %c\n", s->c2);
}
void test_unpacked_struct(struct UnPackedStruct s) {
hexdump(&s, sizeof(struct UnPackedStruct));
printf("s.c: %c\n", s.c);
printf("s.i: %d\n", s.i);
printf("s.s[0]: %c\n", s.s[0]);
printf("s.s[1]: %c\n", s.s[1]);
printf("s.d: %f\n", s.d);
printf("s.a: %d %d %d\n", s.a[0], s.a[1], s.a[2]);
printf("s.c2: %c\n", s.c2);
}
int sum_func_with_int_varargs(int count, ...) {
va_list args;
int sum = 0;
va_start(args, count);
for (int i = 0; i < count; i++) {
int arg = va_arg(args, int);
sum += arg;
printf("got vararg %d: %d\n", i, arg);
}
va_end(args);
return sum;
}
void my_printf(const char* format, ...) {
va_list args;
va_start(args, format);
vprintf(format, args);
va_end(args);
}
void varargs_structs(int count, ...) {
va_list args;
va_start(args, count);
for (int i = 0; i < count; i++) {
Point p = va_arg(args, Point);
printf("got vararg %d: %d, %f\n", i, p.x, p.y);
}
va_end(args);
}
void varargs_p_structs(int count, ...) {
va_list args;
va_start(args, count);
for (int i = 0; i < count; i++) {
Point* p = va_arg(args, Point*);
printf("got vararg %d: %d, %f\n", i, p->x, p->y);
}
va_end(args);
}
char get_char() {
return 'a';
}
short get_short() {
return 1;
}
short get_short_negative5() {
return -5;
}
unsigned char get_uchar_255() {
return 255;
}
unsigned short get_ushort_65535() {
return 65535;
}
float get_float() {
return 1.5;
}
void return_some_numbers(int** numbers, size_t* count) {
*count = 3;
*numbers = malloc(3 * sizeof(int));
(*numbers)[0] = 1;
(*numbers)[1] = 2;
(*numbers)[2] = 3;
return;
}
long add_long(long a, long b) {
return a + b;
}
unsigned long add_ulong(unsigned long a, unsigned long b) {
return a + b;
}
struct has_array_of_int_pointers {
int* arr[3];
};
int test_array_of_pointers_in_struct(struct has_array_of_int_pointers s) {
int sum = 0;
for (int i = 0; i < 3; i++) {
sum += *s.arr[i];
}
return sum;
}
int test_array_of_int_pointers_simple(int* arr[3]) {
int sum = 0;
for (int i = 0; i < 3; i++) {
sum += *arr[i];
}
return sum;
}
// int** grand_test_of_arrays_of_pointers(struct has_array_of_int_pointers s, struct has_array_of_int_pointers* s2, int arr[3], int (*arr2)[3]);
int** grand_test_of_arrays_of_int_pointers(struct has_array_of_int_pointers s, struct has_array_of_int_pointers* s2, int* arr[3], int* (**arr2)[3]) {
int** return_ints = malloc(12 * sizeof(int*));
for (int i = 0; i < 3; i++) {
printf("s.arr[%d]: %d\n", i, *s.arr[i]);
return_ints[i] = s.arr[i];
}
for (int i = 0; i < 3; i++) {
printf("s2->arr[%d]: %d\n", i, *s2->arr[i]);
return_ints[i + 3] = s2->arr[i];
}
for (int i = 0; i < 3; i++) {
printf("arr[%d]: %d\n", i, *arr[i]);
return_ints[i + 6] = arr[i];
}
for (int i = 0; i < 3; i++) {
printf("arr2[%d]: %d\n", i, *(**arr2)[i]);
return_ints[i + 9] = (**arr2)[i];
}
return return_ints;
}
int global_int = 0;
int increment_global() {
return ++global_int;
}
void* get_address_of_global() {
return &global_int;
}
char global_buffer1[500] = {0};
char global_buffer2[500] = {0};
void* get_address_of_global_buffer1() {
return global_buffer1;
}
void* get_address_of_global_buffer2() {
return global_buffer2;
}
char global_string[200] = "Hello, world!";
char* get_global_string() {
return global_string;
}
char (*global_string_ptr)[200] = &global_string;
char (*get_global_string_ptr())[200] {
return global_string_ptr;
}
bool is_null(void* ptr) {
return ptr == NULL;
}
bool is_pointer_to_null(void** ptr) {
return *ptr == NULL;
}
// Entry point to prevent the compiler from complaining when compiling as a shared library
// This function will not be called; it's just to satisfy the linker.
int main() {
return 0;
}