1 /*
2 * %CopyrightBegin%
3 *
4 * Copyright Ericsson AB 2001-2020. All Rights Reserved.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * %CopyrightEnd%
19 *
20
21 */
22 #include <string.h>
23
24 #include "eidef.h"
25 #include "eiext.h"
26 #include "ei_decode_term.h"
27 #include "putget.h"
28
29 /* Returns 1 on successful encoding, -1 on error, and 0 if the term seems
30 alright, but does not fit in the term structure. If it returns 1, the
31 index will be incremented, and the term contains the decoded term. */
32
ei_decode_ei_term(const char * buf,int * index,ei_term * term)33 int ei_decode_ei_term(const char* buf, int* index, ei_term* term)
34 {
35 const char* s = buf + *index, * s0 = s;
36 int n, sign;
37 char c;
38
39 if (term == NULL) return -1;
40 c = term->ei_type = get8(s);
41 switch (c) {
42 case ERL_SMALL_INTEGER_EXT:
43 term->value.i_val = get8(s);
44 break;
45 case ERL_INTEGER_EXT:
46 term->value.i_val = get32be(s);
47 break;
48 case ERL_FLOAT_EXT:
49 case NEW_FLOAT_EXT:
50 term->ei_type = ERL_FLOAT_EXT;
51 return (ei_decode_double(buf, index, &term->value.d_val) < 0
52 ? -1 : 1);
53 case ERL_ATOM_EXT:
54 case ERL_ATOM_UTF8_EXT:
55 case ERL_SMALL_ATOM_EXT:
56 case ERL_SMALL_ATOM_UTF8_EXT:
57 term->ei_type = ERL_ATOM_EXT;
58 return (ei_decode_atom(buf, index, term->value.atom_name) < 0
59 ? -1 : 1);
60 case ERL_REFERENCE_EXT:
61 case ERL_NEW_REFERENCE_EXT:
62 case ERL_NEWER_REFERENCE_EXT:
63 term->ei_type = ERL_NEW_REFERENCE_EXT;
64 return (ei_decode_ref(buf, index, &term->value.ref) < 0
65 ? -1 : 1);
66 case ERL_PORT_EXT:
67 case ERL_NEW_PORT_EXT:
68 term->ei_type = ERL_PORT_EXT;
69 return (ei_decode_port(buf, index, &term->value.port) < 0
70 ? -1 : 1);
71 case ERL_PID_EXT:
72 case ERL_NEW_PID_EXT:
73 term->ei_type = ERL_PID_EXT;
74 return (ei_decode_pid(buf, index, &term->value.pid) < 0
75 ? -1 : 1);
76 case ERL_SMALL_TUPLE_EXT:
77 term->arity = get8(s);
78 break;
79 case ERL_LARGE_TUPLE_EXT:
80 term->arity = get32be(s);
81 break;
82 case ERL_NIL_EXT:
83 term->arity = 0;
84 break;
85 case ERL_STRING_EXT:
86 term->size = get16be(s);
87 return 0;
88 case ERL_LIST_EXT:
89 case ERL_MAP_EXT:
90 term->arity = get32be(s);
91 break;
92 case ERL_BINARY_EXT:
93 term->size = get32be(s);
94 return 0;
95 case ERL_BIT_BINARY_EXT: {
96 int bytes = get32be(s);
97 int last_bits = get8(s);
98 if (((last_bits==0) != (bytes==0)) || last_bits > 8)
99 return -1;
100 term->size = bytes;
101 return 0;
102 }
103 case ERL_SMALL_BIG_EXT:
104 if ((term->arity = get8(s)) != 4) return -1;
105 sign = get8(s);
106 /* Little Endian, and n always positive, except for LONG_MIN */
107 n = get32le(s);
108 if (sign) {
109 /* check for overflow */
110 if ((n - 1) < 0) return -1;
111 n = -n;
112 } else {
113 /* check for overflow */
114 if (n < 0) return -1;
115 }
116 break;
117 case ERL_LARGE_BIG_EXT:
118 return 0;
119 case ERL_PASS_THROUGH:
120 return 0;
121 case ERL_NEW_CACHE:
122 return -1;
123 case ERL_CACHED_ATOM:
124 return -1;
125 default:
126 return -1;
127 }
128 *index += s-s0;
129 return 1;
130 }
131