aboutsummaryrefslogtreecommitdiffstats
path: root/lib/erl_interface/src/decode/decode_intlist.c
blob: 65552a4ed45ac24eb04063723743c1286358ac1f (plain) (blame)
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
/*
 * %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%
 */
/* since Erlang sends int-lists as either lists or strings, this
 * function can be used when the caller needs an array but doesn't
 * know which type to decode
 */
#include "eidef.h"
#include "eiext.h"
#include "putget.h"

int ei_decode_intlist(const char *buf, int *index, long *a, int *count)
{
  const unsigned char *s = (const unsigned char *)(buf + *index);
  const unsigned char *s0 = s;
  int idx;
  int len;
  int i;

  switch (get8(s)) {
  case ERL_STRING_EXT:
    len = get16be(s);

    /* transfer and cast chars one at a time into array */
    if (a) {
      for (i=0; i<len; i++) {
	a[i] = (long)(s[i]);
      }
    }
    if (count) *count = len;
    s += len;
    break;

  case ERL_LIST_EXT:
    len = get32be(s);
    idx = 0;
    
    if (a) {
      for (i=0; i<len; i++) {
	if (ei_decode_long((char*)s,&idx,a+i) < 0) {
	  if (count) *count = i;
	  return -1;
	}
      }
    }
    else {
      for (i=0; i<len; i++) {
	if (ei_decode_long((char*)s,&idx,NULL) < 0) {
	  if (count) *count = i;
	  return -1;
	}
      }
    }

    if (count) *count = len;
    s += idx;
    break;

  default:
    return -1;
  }


  *index += s-s0; 

  return 0; 
}