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 /* since Erlang sends int-lists as either lists or strings, this
21 * function can be used when the caller needs an array but doesn't
22 * know which type to decode
23 */
24 #include "eidef.h"
25 #include "eiext.h"
26 #include "putget.h"
27
ei_decode_intlist(const char * buf,int * index,long * a,int * count)28 int ei_decode_intlist(const char *buf, int *index, long *a, int *count)
29 {
30 const unsigned char *s = (const unsigned char *)(buf + *index);
31 const unsigned char *s0 = s;
32 int idx;
33 int len;
34 int i;
35
36 switch (get8(s)) {
37 case ERL_STRING_EXT:
38 len = get16be(s);
39
40 /* transfer and cast chars one at a time into array */
41 if (a) {
42 for (i=0; i<len; i++) {
43 a[i] = (long)(s[i]);
44 }
45 }
46 if (count) *count = len;
47 s += len;
48 break;
49
50 case ERL_LIST_EXT:
51 len = get32be(s);
52 idx = 0;
53
54 if (a) {
55 for (i=0; i<len; i++) {
56 if (ei_decode_long((char*)s,&idx,a+i) < 0) {
57 if (count) *count = i;
58 return -1;
59 }
60 }
61 }
62 else {
63 for (i=0; i<len; i++) {
64 if (ei_decode_long((char*)s,&idx,NULL) < 0) {
65 if (count) *count = i;
66 return -1;
67 }
68 }
69 }
70
71 if (count) *count = len;
72 s += idx;
73 break;
74
75 default:
76 return -1;
77 }
78
79
80 *index += s-s0;
81
82 return 0;
83 }
84