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 static unsigned char is_little_endian = 0;
31 inline static int _is_little_endian(void)
34 return ((char *)&i)[0];
38 inline unsigned int mmfile_io_be_uint32(unsigned int value)
40 return (is_little_endian == 0) ? value : ((unsigned int)((((value) & 0xFF000000) >> 24) | (((value) & 0x00FF0000) >> 8) | (((value) & 0x0000FF00) << 8) | (((value) & 0x000000FF) << 24)));
44 inline unsigned int mmfile_io_le_uint32(unsigned int value)
46 return (is_little_endian == 1) ? value : ((unsigned int)((((value) & 0xFF000000) >> 24) | (((value) & 0x00FF0000) >> 8) | (((value) & 0x0000FF00) << 8) | (((value) & 0x000000FF) << 24)));
50 inline int mmfile_io_be_int32(unsigned int value)
52 return (is_little_endian == 0) ? value : ((unsigned int)((((value) & 0xFF000000) >> 24) | (((value) & 0x00FF0000) >> 8) | (((value) & 0x0000FF00) << 8) | (((value) & 0x000000FF) << 24)));
56 inline int mmfile_io_le_int32(unsigned int value)
58 return (is_little_endian == 1) ? value : ((unsigned int)((((value) & 0xFF000000) >> 24) | (((value) & 0x00FF0000) >> 8) | (((value) & 0x0000FF00) << 8) | (((value) & 0x000000FF) << 24)));
62 inline unsigned short mmfile_io_be_uint16(unsigned short value)
64 return (is_little_endian == 0) ? value : ((unsigned short)((((value) & 0xFF00) >> 8) | (((value) & 0x00FF) << 8)));
68 inline unsigned short mmfile_io_le_uint16(unsigned short value)
70 return (is_little_endian == 1) ? value : ((unsigned short)((((value) & 0xFF00) >> 8) | (((value) & 0x00FF) << 8)));
74 inline short mmfile_io_be_int16(unsigned short value)
76 return (is_little_endian == 0) ? value : ((short)((((value) & 0xFF00) >> 8) | (((value) & 0x00FF) << 8)));
80 inline short mmfile_io_le_int16(unsigned short value)
82 return (is_little_endian == 1) ? value : ((short)((((value) & 0xFF00) >> 8) | (((value) & 0x00FF) << 8)));
88 MMFileIOFunc *first_io_func = NULL;
90 static int _mmfile_open(MMFileIOHandle **handle, struct MMFileIOFunc *Func, const char *filename, int flags)
92 MMFileIOHandle *pHandle = NULL;
96 if (!handle || !Func || !filename || !Func->mmfile_open) {
97 debug_error(DEBUG, "invalid param\n");
98 err = MMFILE_IO_FAILED;
102 pHandle = mmfile_malloc(sizeof(MMFileIOHandle));
104 debug_error(DEBUG, "mmfile_malloc: pHandle\n");
105 err = MMFILE_IO_FAILED;
111 pHandle->iofunc = Func;
112 pHandle->flags = flags;
113 pHandle->privateData = NULL;
114 fileNameLen = strlen(filename);
116 debug_msg(RELEASE, "[%d, %s]\n", fileNameLen, filename);
118 pHandle->fileName = mmfile_malloc(fileNameLen + 1);
119 if (!pHandle->fileName) {
120 debug_error(DEBUG, "mmfile_malloc: pHandle->fileName\n");
121 err = MMFILE_IO_FAILED;
125 SAFE_STRLCPY(pHandle->fileName, filename, fileNameLen + 1);
127 err = Func->mmfile_open(pHandle, filename, flags);
129 debug_error(DEBUG, "mmfile_open: pHandle->fileName\n");
130 err = MMFILE_IO_FAILED;
134 return MMFILE_IO_SUCCESS;
137 if (handle && *handle) { /* fix for prevent */
138 mmfile_close(*handle);
146 int mmfile_open(MMFileIOHandle **handle, const char *filename, int flags)
148 MMFileIOFunc *pFuc = NULL;
149 const char *pFile = NULL;
150 char handle_str[256] = {0, };
151 char *pHandleName = NULL;
153 if (!handle || !filename) {
154 debug_error(DEBUG, "invalid param\n");
155 return MMFILE_IO_FAILED;
158 memset(handle_str, 0x00, sizeof(handle_str));
161 pHandleName = handle_str;
163 /* scan to find ':' */
164 while (*pFile != '\0' && *pFile != ':') {
165 if (!isalpha(*pFile)) {
169 if ((pHandleName - handle_str) < (int)sizeof(handle_str) - 1) {
170 *pHandleName++ = *pFile;
175 if (*pFile == '\0') {
177 SAFE_STRLCPY(handle_str, "file", sizeof(handle_str));
182 pFuc = first_io_func;
184 while (pFuc != NULL) {
185 if (!strcmp(handle_str, pFuc->handleName)) {
186 return _mmfile_open(handle, pFuc, filename, flags);
193 return MMFILE_IO_FAILED;
197 int mmfile_read(MMFileIOHandle *handle, unsigned char *buf, int size)
200 if (!handle || (handle->flags & MMFILE_WRONLY)) {
201 return MMFILE_IO_FAILED;
204 if (!handle->iofunc || !handle->iofunc->mmfile_read) {
205 return MMFILE_IO_FAILED;
208 ret = handle->iofunc->mmfile_read(handle, buf, size);
213 int mmfile_write(MMFileIOHandle *handle, unsigned char *buf, int size)
216 if (!handle || !(handle->flags & (MMFILE_WRONLY | MMFILE_RDWR))) {
217 return MMFILE_IO_FAILED;
220 if (!handle->iofunc || !handle->iofunc->mmfile_write) {
221 return MMFILE_IO_FAILED;
224 ret = handle->iofunc->mmfile_write(handle, buf, size);
229 int64_t mmfile_seek(MMFileIOHandle *handle, int64_t pos, int whence)
232 if (!handle || !handle->iofunc || !handle->iofunc->mmfile_seek) {
233 return MMFILE_IO_FAILED;
236 ret = handle->iofunc->mmfile_seek(handle, pos, whence);
241 long long mmfile_tell(MMFileIOHandle *handle)
244 if (!handle || !handle->iofunc || !handle->iofunc->mmfile_tell) {
245 return MMFILE_IO_FAILED;
248 ret = handle->iofunc->mmfile_tell(handle);
253 int mmfile_close(MMFileIOHandle *handle)
257 if (!handle || !handle->iofunc || !handle->iofunc->mmfile_close) {
258 return MMFILE_IO_FAILED;
261 ret = handle->iofunc->mmfile_close(handle);
263 if (handle->fileName) {
264 mmfile_free(handle->fileName);
267 if (handle) mmfile_free(handle);
273 int mmfile_register_io_func(MMFileIOFunc *iofunc)
275 MMFileIOFunc **ptr = NULL;
278 return MMFILE_IO_FAILED;
281 ptr = &first_io_func;
282 while (*ptr != NULL) {
288 return MMFILE_IO_SUCCESS;
292 int mmfile_register_io_all()
294 static int initialized = 0;
297 return MMFILE_IO_FAILED;
300 is_little_endian = _is_little_endian();
303 extern MMFileIOFunc mmfile_file_io_handler;
304 extern MMFileIOFunc mmfile_mem_io_handler;
305 extern MMFileIOFunc mmfile_mmap_io_handler;
307 mmfile_register_io_func(&mmfile_file_io_handler);
308 mmfile_register_io_func(&mmfile_mem_io_handler);
309 mmfile_register_io_func(&mmfile_mmap_io_handler);
311 return MMFILE_IO_SUCCESS;