4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Haejeong Kim <backto.kim@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
26 #include "mm_file_debug.h"
27 #include "mm_file_utils.h"
29 #define HANDLE_STRING_MAX 256
31 static unsigned char is_little_endian = 0;
33 inline static int _is_little_endian(void)
36 return ((char *)&i)[0];
40 inline unsigned int mmfile_io_be_uint32(unsigned int value)
42 return (is_little_endian == 0) ? value : ((unsigned int)((((value) & 0xFF000000) >> 24) | (((value) & 0x00FF0000) >> 8) | (((value) & 0x0000FF00) << 8) | (((value) & 0x000000FF) << 24)));
46 inline unsigned int mmfile_io_le_uint32(unsigned int value)
48 return (is_little_endian == 1) ? value : ((unsigned int)((((value) & 0xFF000000) >> 24) | (((value) & 0x00FF0000) >> 8) | (((value) & 0x0000FF00) << 8) | (((value) & 0x000000FF) << 24)));
52 inline int mmfile_io_be_int32(unsigned int value)
54 return (is_little_endian == 0) ? value : ((unsigned int)((((value) & 0xFF000000) >> 24) | (((value) & 0x00FF0000) >> 8) | (((value) & 0x0000FF00) << 8) | (((value) & 0x000000FF) << 24)));
58 inline int mmfile_io_le_int32(unsigned int value)
60 return (is_little_endian == 1) ? value : ((unsigned int)((((value) & 0xFF000000) >> 24) | (((value) & 0x00FF0000) >> 8) | (((value) & 0x0000FF00) << 8) | (((value) & 0x000000FF) << 24)));
64 inline unsigned short mmfile_io_be_uint16(unsigned short value)
66 return (is_little_endian == 0) ? value : ((unsigned short)((((value) & 0xFF00) >> 8) | (((value) & 0x00FF) << 8)));
70 inline unsigned short mmfile_io_le_uint16(unsigned short value)
72 return (is_little_endian == 1) ? value : ((unsigned short)((((value) & 0xFF00) >> 8) | (((value) & 0x00FF) << 8)));
76 inline short mmfile_io_be_int16(unsigned short value)
78 return (is_little_endian == 0) ? value : ((short)((((value) & 0xFF00) >> 8) | (((value) & 0x00FF) << 8)));
82 inline short mmfile_io_le_int16(unsigned short value)
84 return (is_little_endian == 1) ? value : ((short)((((value) & 0xFF00) >> 8) | (((value) & 0x00FF) << 8)));
88 inline float mmfile_io_be_float32(float value)
90 /* Additional variables were introduced to avoid dereferencing type-punned pointer compilation error */
91 float *value_ptr = &value;
92 unsigned int result_uint = ((((*(unsigned int*)value_ptr) & 0x000000FF) << 24) | (((*(unsigned int*)value_ptr) & 0x0000FF00) << 8) | (((*(unsigned int*)value_ptr) & 0x00FF0000) >> 8) | (((*(unsigned int*)value_ptr) & 0xFF000000) >> 24));
93 float *result = (float*)&result_uint;
94 return (is_little_endian == 0) ? value : *result;
97 static MMFileIOFunc *first_io_func = NULL;
99 static int _mmfile_open(MMFileIOHandle **handle, struct MMFileIOFunc *Func, const char *filename, int flags)
101 MMFileIOHandle *pHandle = NULL;
104 if (!handle || !Func || !filename || !Func->mmfile_open) {
105 debug_error(DEBUG, "invalid param\n");
106 err = MMFILE_UTIL_FAIL;
110 pHandle = g_new0(MMFileIOHandle, 1);
114 pHandle->iofunc = Func;
115 pHandle->flags = flags;
116 pHandle->privateData = NULL;
118 debug_msg(RELEASE, "[%s]\n", filename);
120 pHandle->fileName = g_strdup(filename);
122 err = Func->mmfile_open(pHandle, filename, flags);
124 debug_error(DEBUG, "mmfile_open: pHandle->fileName\n");
125 err = MMFILE_UTIL_FAIL;
129 return MMFILE_UTIL_SUCCESS;
132 if (handle && *handle) { /* fix for prevent */
133 mmfile_close(*handle);
141 int mmfile_open(MMFileIOHandle **handle, const char *filename, int flags)
143 MMFileIOFunc *pFuc = NULL;
144 const char *pFile = NULL;
145 char handle_str[HANDLE_STRING_MAX] = {0, };
146 char *pHandleName = NULL;
148 if (!handle || !filename) {
149 debug_error(DEBUG, "invalid param\n");
150 return MMFILE_UTIL_FAIL;
153 memset(handle_str, 0x00, HANDLE_STRING_MAX);
156 pHandleName = handle_str;
158 /* scan to find ':' */
159 while (*pFile != '\0' && *pFile != ':') {
160 if (!isalpha(*pFile)) {
164 if ((pHandleName - handle_str) < HANDLE_STRING_MAX - 1) {
165 *pHandleName++ = *pFile;
170 if (*pFile == '\0') {
172 if (g_strlcpy(handle_str, "file", HANDLE_STRING_MAX) >= HANDLE_STRING_MAX)
173 debug_error(DEBUG, "truncation occurred.");
178 pFuc = first_io_func;
180 while (pFuc != NULL) {
181 if (!strcmp(handle_str, pFuc->handleName)) {
182 return _mmfile_open(handle, pFuc, filename, flags);
189 return MMFILE_UTIL_FAIL;
193 int mmfile_read(MMFileIOHandle *handle, unsigned char *buf, int size)
197 return MMFILE_UTIL_FAIL;
200 if (!handle->iofunc || !handle->iofunc->mmfile_read) {
201 return MMFILE_UTIL_FAIL;
204 ret = handle->iofunc->mmfile_read(handle, buf, size);
209 int mmfile_write(MMFileIOHandle *handle, unsigned char *buf, int size)
213 return MMFILE_UTIL_FAIL;
216 if (!handle->iofunc || !handle->iofunc->mmfile_write) {
217 return MMFILE_UTIL_FAIL;
220 ret = handle->iofunc->mmfile_write(handle, buf, size);
225 int64_t mmfile_seek(MMFileIOHandle *handle, int64_t pos, int whence)
228 if (!handle || !handle->iofunc || !handle->iofunc->mmfile_seek) {
229 return MMFILE_UTIL_FAIL;
232 ret = handle->iofunc->mmfile_seek(handle, pos, whence);
237 long long mmfile_tell(MMFileIOHandle *handle)
240 if (!handle || !handle->iofunc || !handle->iofunc->mmfile_tell) {
241 return MMFILE_UTIL_FAIL;
244 ret = handle->iofunc->mmfile_tell(handle);
249 long long mmfile_get_size(MMFileIOHandle *handle)
251 long long filesize = 0;
252 long long cur_pos = 0;
254 cur_pos = mmfile_tell(handle);
256 return MMFILE_UTIL_FAIL;
258 if (mmfile_seek(handle, 0L, MMFILE_SEEK_SET) < 0)
259 return MMFILE_UTIL_FAIL;
261 if (mmfile_seek(handle, 0L, MMFILE_SEEK_END) < 0)
262 return MMFILE_UTIL_FAIL;
264 filesize = mmfile_tell(handle);
266 if (mmfile_seek(handle, cur_pos, MMFILE_SEEK_SET) < 0)
267 debug_error(DEBUG, "failed to restore current position");
273 int mmfile_close(MMFileIOHandle *handle)
277 if (!handle || !handle->iofunc || !handle->iofunc->mmfile_close) {
278 return MMFILE_UTIL_FAIL;
281 ret = handle->iofunc->mmfile_close(handle);
283 mmfile_free(handle->fileName);
290 int mmfile_register_io_func(MMFileIOFunc *iofunc)
292 MMFileIOFunc **ptr = NULL;
295 return MMFILE_UTIL_FAIL;
298 ptr = &first_io_func;
299 while (*ptr != NULL) {
305 return MMFILE_UTIL_SUCCESS;
309 int mmfile_register_io_all()
311 static int initialized = 0;
314 return MMFILE_UTIL_FAIL;
317 is_little_endian = _is_little_endian();
320 extern MMFileIOFunc mmfile_file_io_handler;
321 extern MMFileIOFunc mmfile_mem_io_handler;
322 extern MMFileIOFunc mmfile_mmap_io_handler;
324 mmfile_register_io_func(&mmfile_file_io_handler);
325 mmfile_register_io_func(&mmfile_mem_io_handler);
326 mmfile_register_io_func(&mmfile_mmap_io_handler);
328 return MMFILE_UTIL_SUCCESS;