Update contrib.
1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
17 #include <3gplibrary/mp4config.h>
18 #include "mp4buffer.h"
19 #include "mp4memwrap.h"
28 * mp4_i32 addData(MP4HandleImp handle,
30 * mp4_u32 bytestowrite)
34 * This function allocates memory for the data and copies it from
35 * buffer to the new allocated buffer.
39 * handle MP4 library handle
40 * buffer Buffer containing data
41 * bytestowrite Size of buffer in bytes
49 mp4_i32 addData(MP4HandleImp handle, mp4_u8 *buffer, mp4_u32 bytestowrite)
53 newBuffer = mp4malloc(bytestowrite);
54 if (newBuffer == NULL)
57 mp4memcpy(newBuffer, buffer, bytestowrite);
59 if (listAppend(handle->mem, newBuffer, bytestowrite)) /* Success */
76 * mp4_u32 getBufferedBytes(MP4HandleImp handle)
80 * This function returns the number of bytes in the library internal
85 * handle MP4 library handle
89 * 0 The input is in a file and therefore no memory is used to store MP4
90 * data or no memory in buffers.
91 * >0 Number of bytes stored in the library internal buffers
94 mp4_u32 getBufferedBytes(MP4HandleImp handle)
99 return listBytesInList(handle->mem);
106 * mp4_u32 getCumulativeBufferedBytes(MP4HandleImp handle)
110 * This function returns the number of bytes passed through the library
115 * handle MP4 library handle
119 * 0 The input is in a file and therefore no memory is used to store MP4
120 * data or no memory in buffers.
121 * >0 Number of bytes stored in the library internal buffers
124 mp4_u32 getCumulativeBufferedBytes(MP4HandleImp handle)
129 return listCumulativeBytesInList(handle->mem);
136 * mp4_i32 readData(MP4HandleImp handle,
138 * mp4_u32 bytestoread)
142 * This function reads bytestoread bytes from memory buffers or file
147 * handle MP4 library handle
148 * buffer Caller allocated buffer for the data
149 * bytestoread Number of bytes to read
153 * >= 0 Success. Value tells the number of bytes read.
154 * -1 File has not been opened
155 * -2 End of file or file error
156 * -10 Not enough data in memory
159 mp4_i32 readData(MP4HandleImp handle, mp4_u8 *buffer, mp4_u32 bytestoread)
161 if (handle->file) /* Input is in a file */
163 switch (readFile(handle, buffer, bytestoread))
165 case -2: /* EOF or error */
167 case -1: /* File not open */
175 else /* Input is in memory list */
180 if (handle->mem->bytesInList - handle->absPosition < bytestoread)
184 j = handle->absPosition;
186 node = handle->mem->first;
188 while (i < bytestoread)
190 if ((mp4_i32)(node->dataSize - j) <= 0)
200 k = node->dataSize - j >= bytestoread - i ? bytestoread - i : node->dataSize - j;
202 mp4memcpy(buffer + i, ((mp4_u8 *)node->data) + j, k);
208 handle->position = j;
209 handle->absPosition += bytestoread;
211 node = handle->mem->first;
220 * mp4_i32 peekData(MP4HandleImp handle,
222 * mp4_u32 bytestoread)
226 * This function reads bytestoread bytes from memory buffers or file
227 * to buffer but doesn't change the internal position in the file/stream.
231 * handle MP4 library handle
232 * buffer Caller allocated buffer for the data
233 * bytestoread Number of bytes to read
237 * >= 0 Success. Value tells the number of bytes read.
238 * -1 File has not been opened
239 * -2 End of file or file error
241 * -10 Not enough data in memory
244 mp4_i32 peekData(MP4HandleImp handle, mp4_u8 *buffer, mp4_u32 bytestoread)
246 if (handle->file) /* Input is in a file */
248 switch (peekFile(handle, buffer, bytestoread))
250 case -3: /* fseek failed */
252 case -2: /* EOF or error */
254 case -1: /* File not open */
262 else /* Input is in memory list */
267 if ((mp4_i32)(handle->mem->bytesInList - handle->absPosition) < (mp4_i32)bytestoread)
271 j = handle->absPosition;
273 node = handle->mem->first;
275 while (i < bytestoread)
277 if ((mp4_i32)(node->dataSize - j) <= 0)
287 k = node->dataSize - j >= bytestoread - i ? bytestoread - i : node->dataSize - j;
289 mp4memcpy(buffer + i, ((mp4_u8 *)node->data) + j, k);
303 * mp4_i32 discardData(MP4HandleImp handle,
304 * mp4_i32 bytesToDiscard)
308 * This function reads and discards bytesToDiscard bytes from file/stream.
312 * handle MP4 library handle
313 * bytesToDiscard This many bytes are discarded
317 * Negative integer Error
318 * >= 0 Success. Value tells how many bytes were read.
321 mp4_i32 discardData(MP4HandleImp handle, mp4_i32 bytesToDiscard)
325 mp4_i32 totalbytesread = 0;
327 while (totalbytesread < bytesToDiscard)
329 bytestoread = bytesToDiscard - totalbytesread;
330 if (bytestoread > TMPBUFSIZE)
331 bytestoread = TMPBUFSIZE;
333 bytesread = readData(handle, handle->buf, bytestoread);
336 totalbytesread += bytesread;
339 return totalbytesread;