ffi 0.6.3 → 0.6.4

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of ffi might be problematic. Click here for more details.

@@ -0,0 +1,56 @@
1
+ #include <sys/types.h>
2
+ #include <stdint.h>
3
+
4
+ typedef int8_t s8;
5
+ typedef uint8_t u8;
6
+ typedef int16_t s16;
7
+ typedef uint16_t u16;
8
+ typedef int32_t s32;
9
+ typedef uint32_t u32;
10
+ typedef int64_t s64;
11
+ typedef uint64_t u64;
12
+ typedef signed long sL;
13
+ typedef unsigned long uL;
14
+ typedef float f32;
15
+ typedef double f64;
16
+ #if !defined(__OpenBSD__)
17
+ typedef unsigned long ulong;
18
+ #endif
19
+ typedef void* pointer;
20
+ typedef void* P;
21
+
22
+ #define GVAR(T) \
23
+ extern T gvar_##T; \
24
+ T gvar_##T = (T) -1; \
25
+ T gvar_##T##_get() { return gvar_##T; }; \
26
+ void gvar_##T##_set(T v) { gvar_##T = v; }
27
+
28
+ GVAR(s8);
29
+ GVAR(u8);
30
+ GVAR(s16);
31
+ GVAR(u16);
32
+ GVAR(s32);
33
+ GVAR(u32);
34
+ GVAR(s64);
35
+ GVAR(u64);
36
+ GVAR(long);
37
+ GVAR(ulong);
38
+ GVAR(pointer);
39
+
40
+ struct gstruct {
41
+ long data;
42
+ };
43
+
44
+ struct gstruct gvar_gstruct = { -1 };
45
+
46
+ struct gstruct*
47
+ gvar_gstruct_get(void)
48
+ {
49
+ return &gvar_gstruct;
50
+ }
51
+
52
+ void
53
+ gvar_gstruct_set(const struct gstruct* val)
54
+ {
55
+ gvar_gstruct = *val;
56
+ }
@@ -0,0 +1,41 @@
1
+ /*
2
+ * Copyright (c) 2008 Wayne Meissner. All rights reserved.
3
+ *
4
+ * Redistribution and use in source and binary forms, with or without
5
+ * modification, are permitted provided that the following conditions are met:
6
+ *
7
+ * Redistributions of source code must retain the above copyright notice, this
8
+ * list of conditions and the following disclaimer.
9
+ * Redistributions in binary form must reproduce the above copyright notice
10
+ * this list of conditions and the following disclaimer in the documentation
11
+ * and/or other materials provided with the distribution.
12
+ * Neither the name of the project nor the names of its contributors
13
+ * may be used to endorse or promote products derived from this software
14
+ * without specific prior written permission.
15
+ *
16
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
+ */
27
+ #if defined(_WIN32) || defined(__WIN32__)
28
+ # include <windows.h>
29
+ #else
30
+ # include <errno.h>
31
+ #endif
32
+
33
+ int setLastError(int error) {
34
+ #if defined(_WIN32) || defined(__WIN32__)
35
+ SetLastError(error);
36
+ #else
37
+ errno = error;
38
+ #endif
39
+ return -1;
40
+ }
41
+
@@ -0,0 +1,145 @@
1
+ /*
2
+ * Copyright (c) 2007 Wayne Meissner. All rights reserved.
3
+ *
4
+ * Redistribution and use in source and binary forms, with or without
5
+ * modification, are permitted provided that the following conditions are met:
6
+ *
7
+ * Redistributions of source code must retain the above copyright notice, this
8
+ * list of conditions and the following disclaimer.
9
+ * Redistributions in binary form must reproduce the above copyright notice
10
+ * this list of conditions and the following disclaimer in the documentation
11
+ * and/or other materials provided with the distribution.
12
+ * Neither the name of the project nor the names of its contributors
13
+ * may be used to endorse or promote products derived from this software
14
+ * without specific prior written permission.
15
+ *
16
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
+ */
27
+
28
+ #include <sys/types.h>
29
+ #include <stdio.h>
30
+ #include <string.h>
31
+ #include <stdint.h>
32
+
33
+ #ifdef __sparc
34
+ #define fix_mem_access __asm("ta 6")
35
+ #else
36
+ #define fix_mem_access
37
+ #endif
38
+
39
+ typedef int8_t s8;
40
+ typedef uint8_t u8;
41
+ typedef int16_t s16;
42
+ typedef uint16_t u16;
43
+ typedef int32_t s32;
44
+ typedef uint32_t u32;
45
+ typedef int64_t s64;
46
+ typedef uint64_t u64;
47
+ typedef signed long sL;
48
+ typedef unsigned long uL;
49
+ typedef float f32;
50
+ typedef double f64;
51
+ #if !defined(__OpenBSD__)
52
+ typedef unsigned long ulong;
53
+ #endif
54
+
55
+ #define ADD(T) T add_##T(T arg1, T arg2) { return arg1 + arg2; }
56
+ #define SUB(T) T sub_##T(T arg1, T arg2) { return arg1 - arg2; }
57
+ #define MUL(T) T mul_##T(T arg1, T arg2) { return arg1 * arg2; }
58
+ #define DIV(T) T div_##T(T arg1, T arg2) { return arg1 / arg2; }
59
+ #define RET(T) T ret_##T(T arg1) { return arg1; }
60
+ #define SET(T) static T T##_;void set_##T(T arg1) { T##_ = arg1; }
61
+ #define GET(T) T get_##T() { return T##_; }
62
+ typedef char* ptr;
63
+ #define TEST(T) ADD(T) SUB(T) MUL(T) DIV(T) RET(T) SET(T) GET(T)
64
+ TEST(s8);
65
+ TEST(u8);
66
+ TEST(s16);
67
+ TEST(u16);
68
+ TEST(s32);
69
+ TEST(u32);
70
+ TEST(s64);
71
+ TEST(u64);
72
+ TEST(float);
73
+ TEST(double);
74
+ TEST(long);
75
+ TEST(ulong);
76
+
77
+ #define ADD2(R, T1, T2) R add_##T1##T2##_##R(T1 arg1, T2 arg2) { return arg1 + arg2; }
78
+ #define SUB2(R, T1, T2) R sub_##T1##T2##_##R(T1 arg1, T2 arg2) { return arg1 - arg2; }
79
+ #define MUL2(R, T1, T2) R mul_##T1##T2##_##R(T1 arg1, T2 arg2) { return arg1 * arg2; }
80
+ #define DIV2(R, T1, T2) R div_##T1##T2##_##R(T1 arg1, T2 arg2) { return arg1 / arg2; }
81
+
82
+ #define T2__(R, T1, T2) ADD2(R, T1, T2) SUB2(R, T1, T2) MUL2(R, T1, T2) DIV2(R, T1, T2)
83
+ #define T2_(R, T1) \
84
+ T2__(R, T1, s8) T2__(R, T1, u8) \
85
+ T2__(R, T1, s16) T2__(R, T1, u16) \
86
+ T2__(R, T1, s32) T2__(R, T1, u32) \
87
+ T2__(R, T1, sL) T2__(R, T1, uL) \
88
+ T2__(R, T1, s64) T2__(R, T1, u64) \
89
+
90
+ #define TEST2(R) \
91
+ T2_(R, s8) T2_(R, u8) T2_(R, s16) T2_(R, u16) T2_(R, s32) T2_(R, u32) \
92
+ T2_(R, sL) T2_(R, uL) T2_(R, s64) T2_(R, u64)
93
+
94
+ #ifdef notyet
95
+ TEST2(s32)
96
+ TEST2(u32)
97
+ TEST2(s64)
98
+ TEST2(u64)
99
+ #endif
100
+
101
+ #define ADD3(R, T1, T2, T3) R add_##T1##T2##T3##_##R(T1 arg1, T2 arg2, T3 arg3) { return arg1 + arg2 + arg3; }
102
+ #define pack_f32(buf, v) do { float f = v; memcpy((buf), &f, sizeof(f)); } while(0)
103
+ #define pack_f64(buf, v) do { double f = v; memcpy((buf), &f, sizeof(f)); } while(0)
104
+ #define pack_int(buf, v) do { *(buf) = v; } while(0)
105
+ #define pack_s8 pack_int
106
+ #define pack_u8 pack_int
107
+ #define pack_s16 pack_int
108
+ #define pack_u16 pack_int
109
+ #define pack_s32 pack_int
110
+ #define pack_u32 pack_int
111
+ #define pack_s64 pack_int
112
+ #define pack_u64 pack_int
113
+ #define pack_sL pack_int
114
+ #define pack_uL pack_int
115
+
116
+ #define PACK3(R, T1, T2, T3) void pack_##T1##T2##T3##_##R(T1 arg1, T2 arg2, T3 arg3, R* r) { \
117
+ fix_mem_access; \
118
+ pack_##T1(&r[0], arg1); \
119
+ pack_##T2(&r[1], arg2); \
120
+ pack_##T3(&r[2], arg3); \
121
+ }
122
+
123
+ #define T3___(R, T1, T2, T3) PACK3(R, T1, T2, T3) /* SUB2(R, T1, T2) MUL2(R, T1, T2) DIV2(R, T1, T2) */
124
+ #define T3__(R, T1, T2) \
125
+ T3___(R, T1, T2, s8) T3___(R, T1, T2, u8) \
126
+ T3___(R, T1, T2, s16) T3___(R, T1, T2, u16) \
127
+ T3___(R, T1, T2, s32) T3___(R, T1, T2, u32) \
128
+ T3___(R, T1, T2, sL) T3___(R, T1, T2, uL) \
129
+ T3___(R, T1, T2, s64) T3___(R, T1, T2, u64) \
130
+ T3___(R, T1, T2, f32) T3___(R, T1, T2, f64) \
131
+
132
+ #define T3_(R, T1) \
133
+ T3__(R, T1, s8) T3__(R, T1, u8) \
134
+ T3__(R, T1, s16) T3__(R, T1, u16) \
135
+ T3__(R, T1, s32) T3__(R, T1, u32) \
136
+ T3__(R, T1, sL) T3__(R, T1, uL) \
137
+ T3__(R, T1, s64) T3__(R, T1, u64) \
138
+ T3__(R, T1, f32) T3__(R, T1, f64) \
139
+
140
+ #define TEST3(R) \
141
+ T3_(R, s8) T3_(R, u8) T3_(R, s16) T3_(R, u16) T3_(R, s32) T3_(R, u32) \
142
+ T3_(R, sL) T3_(R, uL) T3_(R, s64) T3_(R, u64) T3_(R, f32) T3_(R, f64)
143
+
144
+ TEST3(s64)
145
+
@@ -0,0 +1,84 @@
1
+ /*
2
+ * Copyright (c) 2007 Wayne Meissner. All rights reserved.
3
+ *
4
+ * Redistribution and use in source and binary forms, with or without
5
+ * modification, are permitted provided that the following conditions are met:
6
+ *
7
+ * Redistributions of source code must retain the above copyright notice, this
8
+ * list of conditions and the following disclaimer.
9
+ * Redistributions in binary form must reproduce the above copyright notice
10
+ * this list of conditions and the following disclaimer in the documentation
11
+ * and/or other materials provided with the distribution.
12
+ * Neither the name of the project nor the names of its contributors
13
+ * may be used to endorse or promote products derived from this software
14
+ * without specific prior written permission.
15
+ *
16
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
+ */
27
+
28
+ #include <sys/types.h>
29
+ #include <sys/param.h>
30
+ #include <stdint.h>
31
+ #include <stdio.h>
32
+ #include <stdlib.h>
33
+ #include <string.h>
34
+ typedef void* ptr;
35
+ typedef void* pointer;
36
+ #ifdef _WIN32
37
+ typedef char* caddr_t;
38
+ #endif
39
+
40
+ #define RET(T) T ptr_ret_##T(void* arg1, int offset) { \
41
+ T tmp; memcpy(&tmp, (caddr_t) arg1 + offset, sizeof(tmp)); return tmp; \
42
+ }
43
+ #define SET(T) void ptr_set_##T(void* arg1, int offset, T value) { \
44
+ memcpy((caddr_t) arg1 + offset, &value, sizeof(value)); \
45
+ }
46
+ #define TEST(T) SET(T) RET(T)
47
+
48
+ TEST(int8_t);
49
+ TEST(int16_t);
50
+ TEST(int32_t);
51
+ TEST(int64_t);
52
+ TEST(float);
53
+ TEST(double);
54
+ TEST(pointer);
55
+
56
+ void*
57
+ ptr_return_array_element(void **ptrArray, int arrayIndex)
58
+ {
59
+ return ptrArray[arrayIndex];
60
+ }
61
+
62
+ void
63
+ ptr_set_array_element(void **ptrArray, int arrayIndex, void *value)
64
+ {
65
+ ptrArray[arrayIndex] = value;
66
+ }
67
+
68
+ void*
69
+ ptr_malloc(int size)
70
+ {
71
+ return calloc(1, size);
72
+ }
73
+ void
74
+ ptr_free(void* ptr)
75
+ {
76
+ free(ptr);
77
+ }
78
+
79
+ void*
80
+ ptr_from_address(uintptr_t addr)
81
+ {
82
+ return (void *) addr;
83
+ }
84
+
@@ -0,0 +1,44 @@
1
+ /*
2
+ * Copyright (c) 2007 Wayne Meissner. All rights reserved.
3
+ *
4
+ * Redistribution and use in source and binary forms, with or without
5
+ * modification, are permitted provided that the following conditions are met:
6
+ *
7
+ * Redistributions of source code must retain the above copyright notice, this
8
+ * list of conditions and the following disclaimer.
9
+ * Redistributions in binary form must reproduce the above copyright notice
10
+ * this list of conditions and the following disclaimer in the documentation
11
+ * and/or other materials provided with the distribution.
12
+ * Neither the name of the project nor the names of its contributors
13
+ * may be used to endorse or promote products derived from this software
14
+ * without specific prior written permission.
15
+ *
16
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
+ */
27
+
28
+ #include <stdint.h>
29
+
30
+ #define REF(T) void ref_##T(T arg, T* result) { *result = arg; }
31
+ #define ADD(T) void ref_add_##T(T arg1, T arg2, T* result) { *result = arg1 + arg2; }
32
+ #define SUB(T) void ref_sub_##T(T arg1, T arg2, T* result) { *result = arg1 - arg2; }
33
+ #define MUL(T) void ref_mul_##T(T arg1, T arg2, T* result) { *result = arg1 * arg2; }
34
+ #define DIV(T) void ref_div_##T(T arg1, T arg2, T* result) { *result = arg1 / arg2; }
35
+ #define TEST(T) ADD(T) SUB(T) MUL(T) DIV(T) REF(T)
36
+
37
+ TEST(int8_t);
38
+ TEST(int16_t);
39
+ TEST(int32_t);
40
+ TEST(int64_t);
41
+ TEST(float);
42
+ TEST(double);
43
+
44
+
@@ -0,0 +1,55 @@
1
+ /*
2
+ * Copyright (c) 2007 Wayne Meissner. All rights reserved.
3
+ *
4
+ * Redistribution and use in source and binary forms, with or without
5
+ * modification, are permitted provided that the following conditions are met:
6
+ *
7
+ * Redistributions of source code must retain the above copyright notice, this
8
+ * list of conditions and the following disclaimer.
9
+ * Redistributions in binary form must reproduce the above copyright notice
10
+ * this list of conditions and the following disclaimer in the documentation
11
+ * and/or other materials provided with the distribution.
12
+ * Neither the name of the project nor the names of its contributors
13
+ * may be used to endorse or promote products derived from this software
14
+ * without specific prior written permission.
15
+ *
16
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
+ */
27
+
28
+ #include <string.h>
29
+
30
+ int
31
+ string_equals(const char* s1, const char* s2)
32
+ {
33
+ return strcmp(s1, s2) == 0;
34
+ }
35
+
36
+ void
37
+ string_set(char* s1, const char* s2)
38
+ {
39
+ strcpy(s1, s2);
40
+ }
41
+ void
42
+ string_concat(char* dst, const char* src)
43
+ {
44
+ strcat(dst, src);
45
+ }
46
+ void
47
+ string_dummy(char* dummy)
48
+ {
49
+ }
50
+ const char*
51
+ string_null(void)
52
+ {
53
+ return NULL;
54
+ }
55
+
@@ -0,0 +1,247 @@
1
+ /*
2
+ * Copyright (c) 2007 Wayne Meissner. All rights reserved.
3
+ *
4
+ * Redistribution and use in source and binary forms, with or without
5
+ * modification, are permitted provided that the following conditions are met:
6
+ *
7
+ * Redistributions of source code must retain the above copyright notice, this
8
+ * list of conditions and the following disclaimer.
9
+ * Redistributions in binary form must reproduce the above copyright notice
10
+ * this list of conditions and the following disclaimer in the documentation
11
+ * and/or other materials provided with the distribution.
12
+ * Neither the name of the project nor the names of its contributors
13
+ * may be used to endorse or promote products derived from this software
14
+ * without specific prior written permission.
15
+ *
16
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
+ */
27
+
28
+ #include <stdio.h>
29
+ #include <stdlib.h>
30
+ #include <stdbool.h>
31
+ #include <stdint.h>
32
+ #include <string.h>
33
+ #include <stdarg.h>
34
+
35
+ typedef char s8;
36
+ typedef short s16;
37
+ typedef int s32;
38
+ typedef long long s64;
39
+ typedef float f32;
40
+ typedef double f64;
41
+
42
+ typedef struct bugged_struct {
43
+ unsigned char visible;
44
+ unsigned int x;
45
+ unsigned int y;
46
+ short rx;
47
+ short ry;
48
+ unsigned char order;
49
+ unsigned char size;
50
+ } bugged_struct_t;
51
+
52
+ unsigned int
53
+ bugged_struct_size() {
54
+ return sizeof(bugged_struct_t);
55
+ }
56
+
57
+ struct test1 {
58
+ char b;
59
+ short s;
60
+ int i;
61
+ long long j;
62
+ long l;
63
+ float f;
64
+ double d;
65
+ char string[32];
66
+ };
67
+
68
+ struct struct_with_array {
69
+ char c;
70
+ int a[5];
71
+ };
72
+
73
+ struct nested {
74
+ int i;
75
+ };
76
+
77
+ struct container {
78
+ char first;
79
+ struct nested s;
80
+ };
81
+
82
+ int
83
+ struct_align_nested_struct(struct container* a) { return a->s.i; }
84
+
85
+ void*
86
+ struct_field_array(struct struct_with_array* s) { return &s->a; }
87
+
88
+ struct container*
89
+ struct_make_container_struct(int i)
90
+ {
91
+ static struct container cs;
92
+ memset(&cs, 0, sizeof(cs));
93
+ cs.first = 1;
94
+ cs.s.i = i;
95
+ return &cs;
96
+ }
97
+
98
+ #define T(x, type) \
99
+ type struct_field_##type(struct test1* t) { return t->x; } \
100
+ struct type##_align { char first; type value; }; \
101
+ type struct_align_##type(struct type##_align* a) { return a->value; }
102
+
103
+ T(b, s8);
104
+ T(s, s16);
105
+ T(i, s32);
106
+ T(j, s64);
107
+ T(f, f32);
108
+ T(d, f64);
109
+ T(l, long);
110
+
111
+ void
112
+ struct_set_string(struct test1* t, char* s)
113
+ {
114
+ strcpy(t->string, s);
115
+ }
116
+
117
+ struct test1*
118
+ struct_make_struct(char b, short s, int i, long long ll, float f, double d)
119
+ {
120
+ static struct test1 t;
121
+ memset(&t, 0, sizeof(t));
122
+ t.b = b;
123
+ t.s = s;
124
+ t.i = i;
125
+ t.j = ll;
126
+ t.f = f;
127
+ t.d = d;
128
+ return &t;
129
+ }
130
+
131
+ typedef int (*add_cb)(int a1, int a2);
132
+ typedef int (*sub_cb)(int a1, int a2);
133
+ struct test2 {
134
+ add_cb add_callback;
135
+ sub_cb sub_callback;
136
+ };
137
+
138
+ int
139
+ struct_call_add_cb(struct test2* t, int a1, int a2)
140
+ {
141
+ return t->add_callback(a1, a2);
142
+ }
143
+
144
+ int
145
+ struct_call_sub_cb(struct test2* t, int a1, int a2)
146
+ {
147
+ return t->sub_callback(a1, a2);
148
+ }
149
+
150
+
151
+ struct struct_with_array*
152
+ struct_make_struct_with_array(int a_0, int a_1, int a_2, int a_3, int a_4)
153
+ {
154
+ static struct struct_with_array s;
155
+
156
+ memset(&s, 0, sizeof(s));
157
+
158
+ s.a[0] = a_0;
159
+ s.a[1] = a_1;
160
+ s.a[2] = a_2;
161
+ s.a[3] = a_3;
162
+ s.a[4] = a_4;
163
+
164
+ return &s;
165
+
166
+ }
167
+
168
+ struct s8s32 {
169
+ char s8;
170
+ int s32;
171
+ };
172
+
173
+ struct s8s32
174
+ struct_return_s8s32()
175
+ {
176
+ struct s8s32 s;
177
+ s.s8 = 0x7f;
178
+ s.s32 = 0x12345678;
179
+
180
+ return s;
181
+ }
182
+
183
+ struct s8s32
184
+ struct_s8s32_set(char s8, int s32)
185
+ {
186
+ struct s8s32 s;
187
+
188
+ s.s8 = s8;
189
+ s.s32 = s32;
190
+
191
+ return s;
192
+ }
193
+
194
+ int
195
+ struct_s8s32_get_s8(struct s8s32 s)
196
+ {
197
+ return s.s8;
198
+ }
199
+
200
+ int
201
+ struct_s8s32_get_s32(struct s8s32 s)
202
+ {
203
+ return s.s32;
204
+ }
205
+
206
+ // Pass a struct and an int arg, ensure the int arg is passed correctly
207
+ int
208
+ struct_s8s32_s32_ret_s32(struct s8s32 s, int s32)
209
+ {
210
+ return s32;
211
+ }
212
+
213
+ // Pass a struct and a long long arg, ensure the long long arg is passed correctly
214
+ long long
215
+ struct_s8s32_s64_ret_s64(struct s8s32 s, long long s64)
216
+ {
217
+ return s64;
218
+ }
219
+
220
+ // Pass a char *, copy into buffer length struct
221
+ struct struct_string {
222
+ char *bytes;
223
+ int len;
224
+ };
225
+
226
+ struct struct_string
227
+ struct_varargs_ret_struct_string(int len, ...)
228
+ {
229
+ struct struct_string ss;
230
+ va_list vl;
231
+ char* cp = NULL;
232
+
233
+ va_start(vl, len);
234
+
235
+ ss.len = len;
236
+ ss.bytes = va_arg(vl, char *);
237
+ if (ss.bytes != NULL) {
238
+ cp = malloc(strlen(ss.bytes) + 1);
239
+ strcpy(cp, ss.bytes);
240
+ ss.bytes = cp;
241
+ }
242
+
243
+ va_end(vl);
244
+
245
+ return ss;
246
+ }
247
+