[C#] Free array buffers passed to C# after we've decoded them
[ldk-java] / c_sharp / src / org / ldk / util / InternalUtils.cs
1 using System;
2 using System.Text;
3 using org.ldk.util;
4 using org.ldk.impl;
5
6 internal class InternalUtils {
7         public static T[] check_arr_len<T>(T[] arr, int length) {
8                 if (arr != null && arr.Length != length) {
9                         throw new ArgumentException("Array must be of fixed size " + length + " but was of length " + arr.Length);
10                 }
11                 return arr;
12         }
13
14         public static byte[] convUInt5Array(UInt5[] u5s) {
15                 if (u5s == null) return null;
16                 byte[] res = new byte[u5s.Length];
17                 for (int i = 0; i < u5s.Length; i++) {
18                         res[i] = u5s[i].getVal();
19                 }
20                 return res;
21         }
22
23         public static T[] mapArray<F, T>(F[] arr, Func<F, T> f) {
24                 if (arr == null) return null;
25                 T[] ret = new T[arr.Length];
26                 for (int i = 0; i < arr.Length; i++) ret[i] = f(arr[i]);
27                 return ret;
28         }
29
30         public static string decodeString(long strptr) {
31                 byte[] bytes = decodeUint8Array(strptr);
32                 return Encoding.UTF8.GetString(bytes);
33         }
34
35         public static long encodeString(string s) {
36                 byte[] bytes = Encoding.UTF8.GetBytes(s);
37                 return encodeUint8Array(bytes);
38         }
39
40
41         public static int getArrayLength(long arrptr) {
42                 long len;
43                 unsafe {
44                         long* arrlen = (long*) arrptr;
45                         len = *arrlen;
46                 }
47                 if (len > 0xffffffffL) {
48                         throw new ArgumentException("Array length was out of bounds");
49                 }
50                 return (int)len;
51         }
52
53
54         public static long getU64ArrayElem(long arrptr, int idx) {
55                 unsafe {
56                         long* arr = (long*) (arrptr + 8);
57                         return arr[idx];
58                 }
59         }
60         public static int getU32ArrayElem(long arrptr, int idx) {
61                 unsafe {
62                         int* arr = (int*) (arrptr + 8);
63                         return arr[idx];
64                 }
65         }
66         public static short getU16ArrayElem(long arrptr, int idx) {
67                 unsafe {
68                         short* arr = (short*) (arrptr + 8);
69                         return arr[idx];
70                 }
71         }
72         public static byte getU8ArrayElem(long arrptr, int idx) {
73                 unsafe {
74                         byte* arr = (byte*) (arrptr + 8);
75                         return arr[idx];
76                 }
77         }
78
79
80         public static long encodeUint8Array(byte[] arr) {
81                 long buf = bindings.allocate_buffer(arr.Length + 8);
82                 unsafe { *((long*)buf) = (long)arr.Length; }
83                 for (int i = 0; i < arr.Length; i++) {
84                         unsafe {
85                                 ((byte*)(buf + 8))[i] = arr[i];
86                         }
87                 }
88                 return buf;
89         }
90         public static long encodeUint16Array(short[] arr) {
91                 long buf = bindings.allocate_buffer(arr.Length * 2 + 8);
92                 unsafe { *((long*)buf) = (long)arr.Length; }
93                 for (int i = 0; i < arr.Length; i++) {
94                         unsafe {
95                                 ((short*)(buf + 8))[i] = arr[i];
96                         }
97                 }
98                 return buf;
99         }
100         public static long encodeUint32Array(int[] arr) {
101                 long buf = bindings.allocate_buffer(arr.Length * 4 + 8);
102                 unsafe { *((long*)buf) = (long)arr.Length; }
103                 for (int i = 0; i < arr.Length; i++) {
104                         unsafe {
105                                 ((int*)(buf + 8))[i] = arr[i];
106                         }
107                 }
108                 return buf;
109         }
110         public static long encodeUint64Array(long[] arr) {
111                 long buf = bindings.allocate_buffer(arr.Length * 8 + 8);
112                 unsafe { *((long*)buf) = (long)arr.Length; }
113                 for (int i = 0; i < arr.Length; i++) {
114                         unsafe {
115                                 ((long*)(buf + 8))[i] = arr[i];
116                         }
117                 }
118                 return buf;
119         }
120
121
122         public static byte[] decodeUint8Array(long arrptr) {
123                 int len = getArrayLength(arrptr);
124                 byte[] res = new byte[len];
125                 for (int i = 0; i < len; i++)
126                         res[i] = getU8ArrayElem(arrptr, i);
127                 bindings.free_buffer(arrptr);
128                 return res;
129         }
130         public static short[] decodeUint16Array(long arrptr) {
131                 int len = getArrayLength(arrptr);
132                 short[] res = new short[len];
133                 for (int i = 0; i < len; i++)
134                         res[i] = getU16ArrayElem(arrptr, i);
135                 bindings.free_buffer(arrptr);
136                 return res;
137         }
138         public static long[] decodeUint64Array(long arrptr) {
139                 int len = getArrayLength(arrptr);
140                 long[] res = new long[len];
141                 for (int i = 0; i < len; i++)
142                         res[i] = getU64ArrayElem(arrptr, i);
143                 bindings.free_buffer(arrptr);
144                 return res;
145         }
146 }