aboutsummaryrefslogtreecommitdiffstats
path: root/lib/erl_interface/src/decode/decode_fun.c
blob: db71007505dabd3ab0e0762f03b5d849badd8c7f (plain) (blame)
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
/*
 * %CopyrightBegin%
 * 
 * Copyright Ericsson AB 2001-2016. All Rights Reserved.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * %CopyrightEnd%
 */
#include <string.h>
#include <stdlib.h>

#include "eidef.h"
#include "eiext.h"
#include "ei_malloc.h"
#include "decode_skip.h"
#include "putget.h"

int ei_decode_fun(const char *buf, int *index, erlang_fun *p)
{
    const char *s = buf + *index;
    const char *s0 = s;
    int i, ix, ix0, n;
    erlang_pid* p_pid;
    char* p_module;
    long* p_index;
    long* p_uniq;
    long* p_old_index;

    if (p != NULL) {
	p_pid = &p->u.closure.pid;
	p_module = &p->module[0];
	p_index = &p->u.closure.index;
	p_uniq = &p->u.closure.uniq;
	p_old_index = &p->u.closure.old_index;
    }
    else {
	p_index = NULL; p_uniq = NULL; p_old_index = NULL;
	p_pid = NULL; p_module = NULL;
    }

    switch (get8(s)) {
    case ERL_FUN_EXT:
	/* mark as old (R7 and older) external fun */
	if (p != NULL) p->arity = -1;
	/* first number of free vars (environment) */
	n = get32be(s);
	/* then the pid */
	ix = 0;
	if (ei_decode_pid(s, &ix, p_pid) < 0)
	    return -1;
	/* then the module (atom) */
	if (ei_decode_atom_as(s, &ix, p_module, MAXATOMLEN_UTF8, ERLANG_UTF8,
                              NULL, NULL) < 0)
	    return -1;
	/* then the index */
	if (ei_decode_long(s, &ix, p_index) < 0)
	    return -1;
	/* then the uniq */
	if (ei_decode_long(s, &ix, p_uniq) < 0)
	    return -1;
	/* finally the free vars */
	ix0 = ix;
	for (i = 0; i < n; ++i) {
	    if (ei_skip_term(s, &ix) < 0)
		return -1;
	}
	if (p != NULL) {
	    p->u.closure.n_free_vars = n;
            p->u.closure.free_var_len = ix - ix0;
            if (p->u.closure.free_var_len > 0) {
                p->u.closure.free_vars = ei_malloc(p->u.closure.free_var_len);
                if (!(p->u.closure.free_vars)) return -1;
                memcpy(p->u.closure.free_vars, s + ix0, p->u.closure.free_var_len);
            }
	}
	s += ix;
	*index += s-s0;
        return 0;
	break;
    case ERL_NEW_FUN_EXT:
	/* first total size */
	n = get32be(s);
	/* then the arity */
	i = get8(s);
	if (p != NULL) {
            p->type = EI_FUN_CLOSURE;
            p->arity = i;
            /* then md5 */
            memcpy(p->u.closure.md5, s, 16);
        }
	s += 16;
	/* then index */
	i = get32be(s);
	if (p != NULL) p->u.closure.index = i;
	/* then the number of free vars (environment) */
	i = get32be(s);
	if (p != NULL) p->u.closure.n_free_vars = i;
	/* then the module (atom) */
	ix = 0;
	if (ei_decode_atom_as(s, &ix, p_module, MAXATOMLEN_UTF8, ERLANG_UTF8,
                              NULL, NULL) < 0)
	    return -1;
	/* then the old_index */
	if (ei_decode_long(s, &ix, p_old_index) < 0)
	    return -1;
	/* then the old_uniq */
	if (ei_decode_long(s, &ix, p_uniq) < 0)
	    return -1;
	/* the the pid */
	if (ei_decode_pid(s, &ix, p_pid) < 0)
	    return -1;
	/* finally the free vars */
	s += ix;
	n = n - (s - s0) + 1;
	if (n < 0) return -1;
	if (p != NULL) {
	    p->u.closure.free_var_len = n;
	    if (n > 0) {
		p->u.closure.free_vars = malloc(n);
		if (!(p->u.closure.free_vars)) return -1;
		memcpy(p->u.closure.free_vars, s, n);
	    }
	}
	s += n;
	*index += s-s0;
        return 0;
	break;
    case ERL_EXPORT_EXT: {
        char* p_func;
        long* p_arity;
        int used;

        if (p) {
            p->type = EI_FUN_EXPORT;
            p_arity = &p->arity;
        }
        else {
            p_arity = NULL;
        }
	ix = 0;
        if (ei_decode_atom_as(s, &ix, p_module, MAXATOMLEN_UTF8, ERLANG_UTF8,
                              NULL, NULL) < 0)
            return -1;
        if (p) {
            /* try use module buffer for function name */
            used = strlen(p->module) + 1;
            p_func = p->module + used;
            p->u.exprt.func = p_func;
            p->u.exprt.func_allocated = 0;
        }
        else {
            used = 0;
            p_func = NULL;
        }
        while (ei_decode_atom_as(s, &ix, p_func, MAXATOMLEN_UTF8-used,
                                 ERLANG_UTF8, NULL, NULL) < 0) {
            if (!used)
                return -1;
            p_func = malloc(MAXATOMLEN_UTF8);
            p->u.exprt.func = p_func;
            p->u.exprt.func_allocated = 1;
            used = 0;
        }
        if (ei_decode_long(s, &ix, p_arity) < 0)
            return -1;
	s += ix;
	*index += s - s0;
        return 0;
    }
    default:
	return -1;
    }
}

void free_fun(erlang_fun* f)
{
    switch (f->type) {
    case EI_FUN_CLOSURE:
        if (f->u.closure.free_var_len > 0)
            ei_free(f->u.closure.free_vars);
        break;
    case EI_FUN_EXPORT:
        if (f->u.exprt.func_allocated)
            ei_free(f->u.exprt.func);
        break;
    }
}