Vulkan SDK for Android 1.1.1 Mali Developer Center
android_assets.cpp
1 /* Copyright (c) 2016-2017, ARM Limited and Contributors
2  *
3  * SPDX-License-Identifier: MIT
4  *
5  * Permission is hereby granted, free of charge,
6  * to any person obtaining a copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
14  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19  */
20 
21 #include "android_assets.hpp"
22 
23 using namespace std;
24 
25 namespace MaliSDK
26 {
27 Result AndroidAssetManager::readBinaryFile(const char *pPath, void **ppData, size_t *pSize)
28 {
29  if (!pManager)
30  {
31  LOGE("Asset manager does not exist.");
32  return RESULT_ERROR_GENERIC;
33  }
34 
35  AAsset *asset = AAssetManager_open(pManager, pPath, AASSET_MODE_BUFFER);
36  if (!asset)
37  {
38  LOGE("AAssetManager_open() failed to load file: %s.", pPath);
39  return RESULT_ERROR_IO;
40  }
41 
42  const void *buffer = AAsset_getBuffer(asset);
43  if (buffer)
44  {
45  *pSize = AAsset_getLength(asset);
46  *ppData = malloc(*pSize);
47  if (!*ppData)
48  {
49  LOGE("Failed to allocate buffer for asset: %s.", pPath);
50  AAsset_close(asset);
51  return RESULT_ERROR_OUT_OF_MEMORY;
52  }
53 
54  memcpy(*ppData, buffer, *pSize);
55  AAsset_close(asset);
56  return RESULT_SUCCESS;
57  }
58  else
59  {
60  LOGE("Failed to obtain buffer for asset: %s.", pPath);
61  AAsset_close(asset);
62  return RESULT_ERROR_IO;
63  }
64 }
65 }