1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/mm/mmplugins/lib3gp/impl/src/buffer.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,341 @@
1.4 +// Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +//
1.18 +
1.19 +#include "mp4atom.h"
1.20 +#include <3gplibrary/mp4config.h>
1.21 +#include "mp4buffer.h"
1.22 +#include "mp4memwrap.h"
1.23 +#include "mp4file.h"
1.24 +#include "mp4list.h"
1.25 +
1.26 +
1.27 +
1.28 +/*
1.29 + * Function:
1.30 + *
1.31 + * mp4_i32 addData(MP4HandleImp handle,
1.32 + * mp4_u8 *buffer,
1.33 + * mp4_u32 bytestowrite)
1.34 + *
1.35 + * Description:
1.36 + *
1.37 + * This function allocates memory for the data and copies it from
1.38 + * buffer to the new allocated buffer.
1.39 + *
1.40 + * Parameters:
1.41 + *
1.42 + * handle MP4 library handle
1.43 + * buffer Buffer containing data
1.44 + * bytestowrite Size of buffer in bytes
1.45 + *
1.46 + * Return value:
1.47 + *
1.48 + * Negative Error
1.49 + * 0 Success
1.50 + *
1.51 + */
1.52 +mp4_i32 addData(MP4HandleImp handle, mp4_u8 *buffer, mp4_u32 bytestowrite)
1.53 +{
1.54 + void *newBuffer;
1.55 +
1.56 + newBuffer = mp4malloc(bytestowrite);
1.57 + if (newBuffer == NULL)
1.58 + return -1;
1.59 +
1.60 + mp4memcpy(newBuffer, buffer, bytestowrite);
1.61 +
1.62 + if (listAppend(handle->mem, newBuffer, bytestowrite)) /* Success */
1.63 + return 0;
1.64 + else
1.65 + {
1.66 + if (newBuffer)
1.67 + {
1.68 + delete newBuffer;
1.69 + newBuffer = NULL;
1.70 + }
1.71 + return -1;
1.72 + }
1.73 +}
1.74 +
1.75 +
1.76 +/*
1.77 + * Function:
1.78 + *
1.79 + * mp4_u32 getBufferedBytes(MP4HandleImp handle)
1.80 + *
1.81 + * Description:
1.82 + *
1.83 + * This function returns the number of bytes in the library internal
1.84 + * buffers.
1.85 + *
1.86 + * Parameters:
1.87 + *
1.88 + * handle MP4 library handle
1.89 + *
1.90 + * Return value:
1.91 + *
1.92 + * 0 The input is in a file and therefore no memory is used to store MP4
1.93 + * data or no memory in buffers.
1.94 + * >0 Number of bytes stored in the library internal buffers
1.95 + *
1.96 + */
1.97 +mp4_u32 getBufferedBytes(MP4HandleImp handle)
1.98 +{
1.99 + if (handle->file)
1.100 + return 0;
1.101 +
1.102 + return listBytesInList(handle->mem);
1.103 +}
1.104 +
1.105 +
1.106 +/*
1.107 + * Function:
1.108 + *
1.109 + * mp4_u32 getCumulativeBufferedBytes(MP4HandleImp handle)
1.110 + *
1.111 + * Description:
1.112 + *
1.113 + * This function returns the number of bytes passed through the library
1.114 + * internal buffers.
1.115 + *
1.116 + * Parameters:
1.117 + *
1.118 + * handle MP4 library handle
1.119 + *
1.120 + * Return value:
1.121 + *
1.122 + * 0 The input is in a file and therefore no memory is used to store MP4
1.123 + * data or no memory in buffers.
1.124 + * >0 Number of bytes stored in the library internal buffers
1.125 + *
1.126 + */
1.127 +mp4_u32 getCumulativeBufferedBytes(MP4HandleImp handle)
1.128 +{
1.129 + if (handle->file)
1.130 + return 0;
1.131 +
1.132 + return listCumulativeBytesInList(handle->mem);
1.133 +}
1.134 +
1.135 +
1.136 +/*
1.137 + * Function:
1.138 + *
1.139 + * mp4_i32 readData(MP4HandleImp handle,
1.140 + * mp4_u8 *buffer,
1.141 + * mp4_u32 bytestoread)
1.142 + *
1.143 + * Description:
1.144 + *
1.145 + * This function reads bytestoread bytes from memory buffers or file
1.146 + * to buffer.
1.147 + *
1.148 + * Parameters:
1.149 + *
1.150 + * handle MP4 library handle
1.151 + * buffer Caller allocated buffer for the data
1.152 + * bytestoread Number of bytes to read
1.153 + *
1.154 + * Return value:
1.155 + *
1.156 + * >= 0 Success. Value tells the number of bytes read.
1.157 + * -1 File has not been opened
1.158 + * -2 End of file or file error
1.159 + * -10 Not enough data in memory
1.160 + *
1.161 + */
1.162 +mp4_i32 readData(MP4HandleImp handle, mp4_u8 *buffer, mp4_u32 bytestoread)
1.163 +{
1.164 + if (handle->file) /* Input is in a file */
1.165 + {
1.166 + switch (readFile(handle, buffer, bytestoread))
1.167 + {
1.168 + case -2: /* EOF or error */
1.169 + return -2;
1.170 + case -1: /* File not open */
1.171 + return -1;
1.172 + case 0: /* Ok */
1.173 + return bytestoread;
1.174 + default:
1.175 + break;
1.176 + }
1.177 + }
1.178 + else /* Input is in memory list */
1.179 + {
1.180 + mp4_u32 i, j;
1.181 + node_s *node;
1.182 +
1.183 + if (handle->mem->bytesInList - handle->absPosition < bytestoread)
1.184 + return -10;
1.185 +
1.186 + i = 0;
1.187 + j = handle->absPosition;
1.188 +
1.189 + node = handle->mem->first;
1.190 +
1.191 + while (i < bytestoread)
1.192 + {
1.193 + if ((mp4_i32)(node->dataSize - j) <= 0)
1.194 + {
1.195 + j -= node->dataSize;
1.196 + node = node->next;
1.197 + continue;
1.198 + }
1.199 +
1.200 + {
1.201 + mp4_u32 k;
1.202 +
1.203 + k = node->dataSize - j >= bytestoread - i ? bytestoread - i : node->dataSize - j;
1.204 +
1.205 + mp4memcpy(buffer + i, ((mp4_u8 *)node->data) + j, k);
1.206 + i += k;
1.207 + j += k;
1.208 + }
1.209 + }
1.210 +
1.211 + handle->position = j;
1.212 + handle->absPosition += bytestoread;
1.213 +
1.214 + node = handle->mem->first;
1.215 + }
1.216 +
1.217 + return bytestoread;
1.218 +}
1.219 +
1.220 +/*
1.221 + * Function:
1.222 + *
1.223 + * mp4_i32 peekData(MP4HandleImp handle,
1.224 + * mp4_u8 *buffer,
1.225 + * mp4_u32 bytestoread)
1.226 + *
1.227 + * Description:
1.228 + *
1.229 + * This function reads bytestoread bytes from memory buffers or file
1.230 + * to buffer but doesn't change the internal position in the file/stream.
1.231 + *
1.232 + * Parameters:
1.233 + *
1.234 + * handle MP4 library handle
1.235 + * buffer Caller allocated buffer for the data
1.236 + * bytestoread Number of bytes to read
1.237 + *
1.238 + * Return value:
1.239 + *
1.240 + * >= 0 Success. Value tells the number of bytes read.
1.241 + * -1 File has not been opened
1.242 + * -2 End of file or file error
1.243 + * -3 fseek failed
1.244 + * -10 Not enough data in memory
1.245 + *
1.246 + */
1.247 +mp4_i32 peekData(MP4HandleImp handle, mp4_u8 *buffer, mp4_u32 bytestoread)
1.248 +{
1.249 + if (handle->file) /* Input is in a file */
1.250 + {
1.251 + switch (peekFile(handle, buffer, bytestoread))
1.252 + {
1.253 + case -3: /* fseek failed */
1.254 + return -3;
1.255 + case -2: /* EOF or error */
1.256 + return -2;
1.257 + case -1: /* File not open */
1.258 + return -1;
1.259 + case 0: /* Ok */
1.260 + return bytestoread;
1.261 + default:
1.262 + break;
1.263 + }
1.264 + }
1.265 + else /* Input is in memory list */
1.266 + {
1.267 + mp4_u32 i, j;
1.268 + node_s *node;
1.269 +
1.270 + if ((mp4_i32)(handle->mem->bytesInList - handle->absPosition) < (mp4_i32)bytestoread)
1.271 + return -10;
1.272 +
1.273 + i = 0;
1.274 + j = handle->absPosition;
1.275 +
1.276 + node = handle->mem->first;
1.277 +
1.278 + while (i < bytestoread)
1.279 + {
1.280 + if ((mp4_i32)(node->dataSize - j) <= 0)
1.281 + {
1.282 + j -= node->dataSize;
1.283 + node = node->next;
1.284 + continue;
1.285 + }
1.286 +
1.287 + {
1.288 + mp4_u32 k;
1.289 +
1.290 + k = node->dataSize - j >= bytestoread - i ? bytestoread - i : node->dataSize - j;
1.291 +
1.292 + mp4memcpy(buffer + i, ((mp4_u8 *)node->data) + j, k);
1.293 + i += k;
1.294 + j += k;
1.295 + }
1.296 + }
1.297 + }
1.298 +
1.299 + return bytestoread;
1.300 +}
1.301 +
1.302 +
1.303 +/*
1.304 + * Function:
1.305 + *
1.306 + * mp4_i32 discardData(MP4HandleImp handle,
1.307 + * mp4_i32 bytesToDiscard)
1.308 + *
1.309 + * Description:
1.310 + *
1.311 + * This function reads and discards bytesToDiscard bytes from file/stream.
1.312 + *
1.313 + * Parameters:
1.314 + *
1.315 + * handle MP4 library handle
1.316 + * bytesToDiscard This many bytes are discarded
1.317 + *
1.318 + * Return value:
1.319 + *
1.320 + * Negative integer Error
1.321 + * >= 0 Success. Value tells how many bytes were read.
1.322 + *
1.323 + */
1.324 +mp4_i32 discardData(MP4HandleImp handle, mp4_i32 bytesToDiscard)
1.325 +{
1.326 + mp4_i32 bytesread;
1.327 + mp4_i32 bytestoread;
1.328 + mp4_i32 totalbytesread = 0;
1.329 +
1.330 + while (totalbytesread < bytesToDiscard)
1.331 + {
1.332 + bytestoread = bytesToDiscard - totalbytesread;
1.333 + if (bytestoread > TMPBUFSIZE)
1.334 + bytestoread = TMPBUFSIZE;
1.335 +
1.336 + bytesread = readData(handle, handle->buf, bytestoread);
1.337 + if (bytesread < 0)
1.338 + return -1;
1.339 + totalbytesread += bytesread;
1.340 + }
1.341 +
1.342 + return totalbytesread;
1.343 +}
1.344 +// End of File