public static byte [] DecodeData(byte[] data, int offset, long size) { #if MULTI_ENCODING Encoding enc = GetEncodingOfData(data); // LogUtil.TraceFormat("data {0} {1} {2} {3} {4} {5}", data[0], data[1], data[2], data[3], data[4], data[5]); if (enc == Encoding.Unity) { return(data); } if (size < 2) { throw new Exception("BundleEncoder DecodeData invalid size " + size); } // FIXME // Using returned (unsigned char *) as byte [] crashes // Currently have to copy data for all byte[] buf = new byte[size - 2]; Array.Copy(data, offset + 2, buf, 0, size - 2); if (enc == Encoding.Plain) { return(buf); } else if (enc == Encoding.Rc4) { LuaDLL.arc4_decrypt_easy(buf, 0, buf.Length, LBootApp.BUNDLE_IV, LBootApp.BUNDLE_IV.Length); return(buf); } else if (enc == Encoding.AES_256_CBC) { byte[] temp = new byte[LBootApp.BUNDLE_IV.Length]; Array.Copy(LBootApp.BUNDLE_IV, temp, LBootApp.BUNDLE_IV.Length); LuaDLL.aes_256_cbc_decrypt_easy(buf, buf, 0, buf.Length, LBootApp.BUNDLE_KEY, LBootApp.BUNDLE_KEY.Length, temp, temp.Length); return(buf); } else if (enc == Encoding.Chacha20) { byte[] outbuf = new byte[buf.Length - 16]; int res = LuaDLL.crypto_secretbox_open_easy(outbuf, buf, buf.Length, LBootApp.BUNDLE_NONCE, LBootApp.BUNDLE_KEY); if (res != 0) { throw new Exception("BundleEncoder Chacha20 decode return: " + res); } return(outbuf); } else { LogUtil.WarnFormat("invalid encoding of bundle data: {0} {1} (probably not encoded)", data[0], data[1]); return(data); // try to read as is } #else return(data); #endif // MULTI_ENCODING }