1 /*
2 * %CopyrightBegin%
3 *
4 * Copyright Ericsson AB 1998-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 "eidef.h"
23 #include "eiext.h"
24 #include "putget.h"
25
26 /* report type identifier from the start of the buffer */
27 /* for types with meaningful length attributes, return the length too.
28 In other cases, return length 0 */
29
ei_get_type(const char * buf,const int * index,int * type,int * len)30 int ei_get_type(const char *buf, const int *index, int *type, int *len)
31 {
32 const char *s = buf + *index;
33
34 *type = get8(s);
35 *len = 0;
36
37 switch (*type) {
38 case ERL_SMALL_ATOM_EXT:
39 case ERL_SMALL_ATOM_UTF8_EXT:
40 *type = ERL_ATOM_EXT;
41 case ERL_SMALL_TUPLE_EXT:
42 *len = get8(s);
43 break;
44
45 case ERL_ATOM_UTF8_EXT:
46 *type = ERL_ATOM_EXT;
47 case ERL_ATOM_EXT:
48 case ERL_STRING_EXT:
49 *len = get16be(s);
50 break;
51
52 case ERL_FLOAT_EXT:
53 case NEW_FLOAT_EXT:
54 *type = ERL_FLOAT_EXT;
55 break;
56
57 case ERL_LARGE_TUPLE_EXT:
58 case ERL_LIST_EXT:
59 case ERL_MAP_EXT:
60 case ERL_BINARY_EXT:
61 case ERL_BIT_BINARY_EXT:
62 *len = get32be(s);
63 break;
64
65 case ERL_SMALL_BIG_EXT:
66 *len = get8(s); /* #digit_bytes */
67 break;
68
69 case ERL_LARGE_BIG_EXT:
70 *len = get32be(s); /* #digit_bytes */
71 break;
72
73 case ERL_NEW_PID_EXT:
74 *type = ERL_PID_EXT;
75 break;
76 case ERL_V4_PORT_EXT:
77 case ERL_NEW_PORT_EXT:
78 *type = ERL_PORT_EXT;
79 break;
80 case ERL_NEWER_REFERENCE_EXT:
81 *type = ERL_NEW_REFERENCE_EXT;
82 break;
83 }
84
85 /* leave index unchanged */
86 return 0;
87 }
88
89
90