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"
37 static int mmf_mem_open(MMFileIOHandle *handle, const char *filename, int flags)
39 MMFmemIOHandle *memHandle = NULL;
40 char **splitedString = NULL;
42 if (!handle || !filename || !handle->iofunc || !handle->iofunc->handleName) {
43 debug_error(DEBUG, "invalid param\n");
44 return MMFILE_IO_FAILED;
47 filename += strlen(handle->iofunc->handleName) + 3; /* ://%d:%d means (memory addr:mem size)*/
49 splitedString = mmfile_strsplit(filename, ":");
50 if (splitedString == NULL) {
51 debug_error(DEBUG, "invalid param\n");
52 return MMFILE_IO_FAILED;
55 if (!splitedString[0] || !splitedString[1]) {
56 debug_error(DEBUG, "invalid param\n");
60 memHandle = mmfile_malloc(sizeof(MMFmemIOHandle));
62 debug_error(DEBUG, "error: mmfile_malloc memHandle\n");
66 memHandle->ptr = (unsigned char *)atoll(splitedString[0]); /*memory allocation address changed. memHandle->ptr = (unsigned char*)atoi(splitedString[0]); */
67 memHandle->size = atoi(splitedString[1]);
68 memHandle->offset = 0;
71 handle->privateData = (void *) memHandle;
74 mmfile_strfreev(splitedString);
77 return MMFILE_IO_SUCCESS;
82 mmfile_strfreev(splitedString);
87 mmfile_free(memHandle);
88 handle->privateData = NULL;
92 return MMFILE_IO_FAILED;
95 static int mmf_mem_read(MMFileIOHandle *h, unsigned char *buf, int size)
97 MMFmemIOHandle *memHandle = NULL;
98 const unsigned char *c = NULL;
101 if (!h || !h->privateData || !buf || size <= 0) {
102 debug_error(DEBUG, "invalid para\n");
103 return MMFILE_IO_FAILED;
106 memHandle = h->privateData;
108 if (!memHandle->ptr) {
109 debug_error(DEBUG, "invalid para\n");
110 return MMFILE_IO_FAILED;
113 c = memHandle->ptr + memHandle->offset;
115 if (memHandle->state != EOF) {
117 if (len + memHandle->offset > memHandle->size) {
118 len = memHandle->size - memHandle->offset;
124 memHandle->offset += len;
126 if (memHandle->offset == memHandle->size) {
127 memHandle->state = EOF;
133 static int mmf_mem_write(MMFileIOHandle *h, unsigned char *buf, int size)
135 MMFmemIOHandle *memHandle = NULL;
136 unsigned char *c = NULL;
139 if (!h || !h->privateData || !buf) {
140 debug_error(DEBUG, "invalid para\n");
141 return MMFILE_IO_FAILED;
144 memHandle = h->privateData;
146 c = memHandle->ptr + memHandle->offset;
148 if (memHandle->state != EOF) {
150 if (len + memHandle->offset > memHandle->size) {
151 len = memHandle->size - memHandle->offset;
157 memHandle->offset += len;
159 if (memHandle->offset == memHandle->size) {
160 memHandle->state = EOF;
167 static int64_t mmf_mem_seek(MMFileIOHandle *h, int64_t pos, int whence)
169 MMFmemIOHandle *memHandle = NULL;
172 if (!h || !h->privateData) {
173 debug_error(DEBUG, "invalid para\n");
174 return MMFILE_IO_FAILED;
177 memHandle = h->privateData;
180 case MMFILE_SEEK_SET:
181 tmp_offset = 0 + pos;
183 case MMFILE_SEEK_CUR:
184 tmp_offset = memHandle->offset + pos;
186 case MMFILE_SEEK_END:
187 tmp_offset = memHandle->size + pos;
190 return MMFILE_IO_FAILED;
194 if (tmp_offset < 0) {
195 debug_error(DEBUG, "invalid file offset\n");
196 return MMFILE_IO_FAILED;
200 memHandle->state = (tmp_offset >= memHandle->size) ? EOF : !EOF;
201 memHandle->offset = tmp_offset;
203 return memHandle->offset;
207 static long long mmf_mem_tell(MMFileIOHandle *h)
209 MMFmemIOHandle *memHandle = NULL;
211 if (!h || !h->privateData) {
212 debug_error(DEBUG, "invalid para\n");
213 return MMFILE_IO_FAILED;
216 memHandle = h->privateData;
218 return memHandle->offset;
221 static int mmf_mem_close(MMFileIOHandle *h)
223 MMFmemIOHandle *memHandle = NULL;
225 if (!h || !h->privateData) {
226 debug_error(DEBUG, "invalid para\n");
227 return MMFILE_IO_FAILED;
230 memHandle = h->privateData;
233 mmfile_free(memHandle);
234 h->privateData = NULL;
237 return MMFILE_IO_SUCCESS;
241 MMFileIOFunc mmfile_mem_io_handler = {