summaryrefslogtreecommitdiff
path: root/lib/erl_interface/src/misc/get_type.c
blob: d67a6a80d3c0e31abacbc418e8f0f54a387f8cc2 (plain)
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
/*
 * %CopyrightBegin%
 * 
 * Copyright Ericsson AB 1998-2009. All Rights Reserved.
 * 
 * The contents of this file are subject to the Erlang Public License,
 * Version 1.1, (the "License"); you may not use this file except in
 * compliance with the License. You should have received a copy of the
 * Erlang Public License along with this software. If not, it can be
 * retrieved online at http://www.erlang.org/.
 * 
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and limitations
 * under the License.
 * 
 * %CopyrightEnd%
 *

 */
#include "eidef.h"
#include "eiext.h"
#include "putget.h"

/* report type identifier from the start of the buffer */
/* for types with meaningful length attributes, return the length too.
   In other cases, return length 0 */

/* FIXME working on this one.... */

int ei_get_type(const char *buf, const int *index, int *type, int *len)
{
    return ei_get_type_internal(buf, index, type, len);
}

#if 0
int ei_get_type(const char *buf, const int *index, int *type, int *len)
{
    const char *s = buf + *index;
    int itype = get8(s);	/* Internal type */

    *len = 0;
  
    switch (*type) {

    case ERL_SMALL_INTEGER_EXT:
    case ERL_INTEGER_EXT:
    case ERL_SMALL_BIG_EXT:
    case ERL_LARGE_BIG_EXT:
	*type = EI_TYPE_INTEGER;
	break;

    case ERL_FLOAT_EXT:
	*type = EI_TYPE_FLOAT;
	break;

    case ERL_SMALL_TUPLE_EXT:
	*len  = get8(s);
	break;
    
    case ERL_ATOM_EXT:
    case ERL_STRING_EXT:
	*len  = get16be(s);
	break;
    
    case ERL_LARGE_TUPLE_EXT:
    case ERL_LIST_EXT:
    case ERL_BINARY_EXT:
	*len  = get32be(s);
	break;
    
    case ERL_SMALL_BIG_EXT:
	*len  = (get8(s)+1)/2; /* big arity */
	break;

    case ERL_LARGE_BIG_EXT:
	*len  = (get32be(s)+1)/2; /* big arity */
	break;

    case ERL_BINARY_EXT:
	*type = EI_TYPE_BINARY;
	break;

    case ERL_PID_EXT:
	*type = EI_TYPE_PID;
	break;

    case ERL_PORT_EXT:
	*type = EI_TYPE_PORT;
	break;

    case ERL_REFERENCE_EXT:
    case ERL_NEW_REFERENCE_EXT:
	*type = EI_TYPE_REF;
	break;

    default:
	break;
    }

    /* leave index unchanged */
    return 0;
}
#endif


/* Old definition of function above */
   
int ei_get_type_internal(const char *buf, const int *index,
			 int *type, int *len)
{
  const char *s = buf + *index;

  *type = get8(s);
  
  switch (*type) {
  case ERL_SMALL_TUPLE_EXT:
    *len = get8(s);
    break;
    
  case ERL_ATOM_EXT:
  case ERL_STRING_EXT:
    *len = get16be(s);
    break;
    
  case ERL_LARGE_TUPLE_EXT:
  case ERL_LIST_EXT:
  case ERL_BINARY_EXT:
    *len = get32be(s);
    break;
    
  case ERL_SMALL_BIG_EXT:
    *len = get8(s); /* #digit_bytes */
    break;

  case ERL_LARGE_BIG_EXT:
    *len = get32be(s); /* #digit_bytes */
    break;

  default:
    *len = 0;
    break;
  }

  /* leave index unchanged */
  return 0;
}