1 /*
2  * %CopyrightBegin%
3  *
4  * Copyright Ericsson AB 1998-2016. 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 #include <string.h>
21 #include "eidef.h"
22 #include "eiext.h"
23 #include "putget.h"
24 
ei_decode_port(const char * buf,int * index,erlang_port * p)25 int ei_decode_port(const char *buf, int *index, erlang_port *p)
26 {
27   const char *s = buf + *index;
28   const char *s0 = s;
29   const char tag = get8(s);
30 
31   switch (tag) {
32   case ERL_V4_PORT_EXT:
33   case ERL_NEW_PORT_EXT:
34   case ERL_PORT_EXT:
35       break;
36   default:
37       return -1;
38   }
39 
40   if (p) {
41     if (get_atom(&s, p->node, NULL) < 0) return -1;
42     switch (tag) {
43     case ERL_V4_PORT_EXT:
44         p->id = get64be(s);
45         p->creation = get32be(s);
46         break;
47     case ERL_NEW_PORT_EXT:
48         p->id = (EI_ULONGLONG) get32be(s);
49         p->creation = get32be(s);
50         break;
51     case ERL_PORT_EXT:
52         p->id = (EI_ULONGLONG) get32be(s);
53         p->creation = get8(s) & 0x03;
54         break;
55     }
56   }
57   else {
58       if (get_atom(&s, NULL, NULL) < 0) return -1;
59       switch (tag) {
60       case ERL_V4_PORT_EXT:
61           s += 12;
62           break;
63       case ERL_NEW_PORT_EXT:
64           s += 8;
65           break;
66       case ERL_PORT_EXT:
67           s += 5;
68           break;
69       }
70   }
71 
72   *index += s-s0;
73 
74   return 0;
75 }
76