1855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi/*
2855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * Copyright (C) 2011 The Android Open Source Project
3855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi *
4855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * Licensed under the Apache License, Version 2.0 (the "License");
5855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * you may not use this file except in compliance with the License.
6855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * You may obtain a copy of the License at
7855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi *
8855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi *      http://www.apache.org/licenses/LICENSE-2.0
9855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi *
10855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * Unless required by applicable law or agreed to in writing, software
11855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * distributed under the License is distributed on an "AS IS" BASIS,
12855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * See the License for the specific language governing permissions and
14855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * limitations under the License.
15855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi */
16855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi/**
17855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi ************************************************************************
18855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @file         M4OSA_FileCommon.c
19855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @brief        File common for Android
20855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @note         This file implements functions used by both the file writer
21855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi *               and file reader.
22855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi ************************************************************************
23855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi*/
24855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
25855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi#ifndef USE_STAGEFRIGHT_CODECS
26855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi#error "USE_STAGEFRIGHT_CODECS is not defined"
27d67e9da63323d965760337913f86de28024531a4Basavapatna Dattaguru#endif /*USE_STAGEFRIGHT_CODECS*/
28855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
29855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi#ifdef UTF_CONVERSION
30855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi#include <string.h>
31855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi#endif /*UTF_CONVERSION*/
32855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
33855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi#include <sys/stat.h>
34855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi#include <errno.h>
35855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
36855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi#ifdef M4OSA_FILE_BLOCK_WITH_SEMAPHORE
37855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi#include "M4OSA_Semaphore.h"
38855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi#endif /* M4OSA_FILE_BLOCK_WITH_SEMAPHORE */
39855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
40855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
41855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi#include "M4OSA_Debug.h"
42855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi#include "M4OSA_FileCommon.h"
43855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi#include "M4OSA_FileCommon_priv.h"
44855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi#include "M4OSA_Memory.h"
45855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi#include "M4OSA_CharStar.h"
46855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
47855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi/**
48855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi ************************************************************************
49855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @brief      This function opens the provided URL and returns its context.
50855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi *             If an error occured, the context is set to NULL.
51855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @param      core_id: (IN) Core ID of the caller (M4OSA_FILE_READER or M4OSA_FILE_WRITER)
52855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @param      context: (OUT) Context of the core file reader
53855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @param      url: (IN) URL of the input file
54855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @param      fileModeAccess: (IN) File mode access
55855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @return     M4NO_ERROR: there is no error
56855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @return     M4ERR_PARAMETER: at least one parameter is NULL
57855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @return     M4ERR_ALLOC: there is no more memory available
58855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @return     M4ERR_NOT_IMPLEMENTED: the URL does not match with the supported
59855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi *             file
60855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @return     M4ERR_FILE_NOT_FOUND: the file cannot be found
61855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @return     M4ERR_FILE_LOCKED: the file is locked by an other
62855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi *             application/process
63855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @return     M4ERR_FILE_BAD_MODE_ACCESS: the file mode access is not correct
64855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi ************************************************************************
65855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi*/
66855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray KundargiM4OSA_ERR M4OSA_fileCommonOpen(M4OSA_UInt16 core_id, M4OSA_Context* pContext,
67855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi                               M4OSA_Char* pUrl, M4OSA_FileModeAccess fileModeAccess)
68855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi{
69855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
70855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_Int32 i            = 0;
71855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_Int32 iMode        = 0;
72855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_Int32 iSize        = 0;
73855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_Int32 iSavePos    = 0;
74855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
75855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_Char  mode[4]            = "";
76855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_Char* pReadString        = (M4OSA_Char*)"r";
77855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_Char* pWriteString    = (M4OSA_Char*)"w";
78855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_Char* pAppendString    = (M4OSA_Char*)"a";
79855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_Char* pBinaryString    = (M4OSA_Char*)"b";
80855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_Char* pPlusString        = (M4OSA_Char*)"+";
81855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
82855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_ERR err = M4NO_ERROR;
83855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
84855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    FILE* pFileHandler = M4OSA_NULL;
85855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_FileContext *pFileContext    = M4OSA_NULL;
86855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
87945956bb7126e483c3bcf7583c9f7acaae952830Dharmaray Kundargi
88855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi#ifdef UTF_CONVERSION
89855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    /*FB: to test the UTF16->UTF8 conversion into Video Artist*/
90855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    /*Convert the URL from UTF16 to UTF8*/
91855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_Void* tempConversionBuf;
92855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_UInt32 tempConversionSize = 1000;
93855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
94694816d7291f17364502ac5d3319684a0b180860Shyam Pallapothu    tempConversionBuf = (M4OSA_Char*)M4OSA_32bitAlignedMalloc(tempConversionSize +1, 0, "conversion buf");
95855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    if(tempConversionBuf == M4OSA_NULL)
96855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    {
97855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        M4OSA_TRACE1_0("Error when allocating conversion buffer\n");
98855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        return M4ERR_PARAMETER;
99855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    }
100855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_ToUTF8_OSAL(pUrl, tempConversionBuf, &tempConversionSize);
101855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    ((M4OSA_Char*)tempConversionBuf)[tempConversionSize ] = '\0';
102855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
103855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    printf("file open %s\n", tempConversionBuf);
104855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi#endif /*UTF CONVERSION*/
105855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
106855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_TRACE3_4("M4OSA_fileCommonOpen\t\tM4OSA_UInt16 %d\tM4OSA_Context* 0x%x\t"
107855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        "M4OSA_Char* %s\tfileModeAccess %d", core_id, pContext, pUrl, fileModeAccess);
108855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
109855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_DEBUG_IF2(M4OSA_NULL == pContext,    M4ERR_PARAMETER,    "M4OSA_fileCommonOpen: pContext is M4OSA_NULL");
110855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_DEBUG_IF2(M4OSA_NULL == pUrl,        M4ERR_PARAMETER,    "M4OSA_fileCommonOpen: pUrl  is M4OSA_NULL");
111855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_DEBUG_IF2(0 == fileModeAccess,    M4ERR_PARAMETER,    "M4OSA_fileCommonOpen: fileModeAccess is 0");
112855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
113855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    /* Read mode not set for the reader */
114855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_DEBUG_IF1((M4OSA_FILE_READER == core_id) && !(fileModeAccess & M4OSA_kFileRead),
115855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        M4ERR_FILE_BAD_MODE_ACCESS, "M4OSA_fileCommonOpen: M4OSA_kFileRead");
116855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
117855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    /* Read mode not set for the reader */
118855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_DEBUG_IF1((M4OSA_FILE_READER == core_id) && !(fileModeAccess & M4OSA_kFileRead),
119855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        M4ERR_FILE_BAD_MODE_ACCESS, "M4OSA_fileCommonOpen: M4OSA_kFileRead");
120855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
121855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    /* M4OSAfileReadOpen cannot be used with Write file mode access */
122855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_DEBUG_IF1((M4OSA_FILE_READER == core_id) && (fileModeAccess & M4OSA_kFileWrite),
123855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        M4ERR_FILE_BAD_MODE_ACCESS, "M4OSA_fileCommonOpen: M4OSA_kFileWrite");
124855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
125855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    /* Append and Create flags cannot be used with Read */
126855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_DEBUG_IF1((M4OSA_FILE_READER == core_id) && (fileModeAccess & M4OSA_kFileAppend),
127855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        M4ERR_FILE_BAD_MODE_ACCESS, "M4OSA_fileCommonOpen: M4OSA_kFileAppend");
128855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
129855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_DEBUG_IF1((M4OSA_FILE_READER == core_id) && (fileModeAccess & M4OSA_kFileCreate),
130855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        M4ERR_FILE_BAD_MODE_ACCESS, "M4OSA_fileCommonOpen: M4OSA_kFileCreate");
131855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
132855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    /* Write mode not set for the writer */
133855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_DEBUG_IF1((M4OSA_FILE_WRITER == core_id) && !(fileModeAccess & M4OSA_kFileWrite),
134855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        M4ERR_FILE_BAD_MODE_ACCESS, "M4OSA_fileCommonOpen: M4OSA_kFileWrite");
135855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
136855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    /* Create flag necessary for opening file */
137855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    if ((fileModeAccess & M4OSA_kFileRead) &&
138855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        (fileModeAccess & M4OSA_kFileWrite)&&(fileModeAccess & M4OSA_kFileCreate))
139855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    {
140947721ba962c19913b76658cdbb19ae8c77c9c50Dheeraj Sharma        strncat((char *)mode, (const char *)pWriteString, (size_t)1);
141947721ba962c19913b76658cdbb19ae8c77c9c50Dheeraj Sharma        strncat((char *)mode, (const char *)pPlusString, (size_t)1);
142855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    }
143855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    else
144855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    {
145855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        if(fileModeAccess & M4OSA_kFileAppend)
146855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        {
147947721ba962c19913b76658cdbb19ae8c77c9c50Dheeraj Sharma            strncat((char *)mode, (const char *)pAppendString, (size_t)1);
148855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        }
149855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        else if(fileModeAccess & M4OSA_kFileRead)
150855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        {
151947721ba962c19913b76658cdbb19ae8c77c9c50Dheeraj Sharma            strncat((char *)mode, (const char *)pReadString, (size_t)1);
152855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        }
153855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        else if(fileModeAccess & M4OSA_kFileWrite)
154855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        {
155947721ba962c19913b76658cdbb19ae8c77c9c50Dheeraj Sharma            strncat((char *)mode, (const char *)pWriteString, (size_t)1);
156855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        }
157855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
158855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        if((fileModeAccess & M4OSA_kFileRead)&&(fileModeAccess & M4OSA_kFileWrite))
159855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        {
160947721ba962c19913b76658cdbb19ae8c77c9c50Dheeraj Sharma            strncat((char *)mode,(const char *)pPlusString, (size_t)1);
161855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        }
162855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    }
163855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
164855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    if(!(fileModeAccess & M4OSA_kFileIsTextMode))
165855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    {
166947721ba962c19913b76658cdbb19ae8c77c9c50Dheeraj Sharma        strncat((char *)mode, (const char *)pBinaryString,(size_t)1);
167855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    }
168855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
169855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    /*Open the file*/
170855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
171855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi#ifdef UTF_CONVERSION
172855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    /*Open the converted path*/
173855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    pFileHandler = fopen((const char *)tempConversionBuf, (const char *)mode);
174855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    /*Free the temporary decoded buffer*/
175694816d7291f17364502ac5d3319684a0b180860Shyam Pallapothu    free(tempConversionBuf);
1765d33cc161e7d13459b4833dbc8b769093c3f80b0Dheeraj Sharma#else /* UTF_CONVERSION */
1775d33cc161e7d13459b4833dbc8b769093c3f80b0Dheeraj Sharma    pFileHandler = fopen((const char *)pUrl, (const char *)mode);
178855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi#endif /* UTF_CONVERSION */
179855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
180855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    if (M4OSA_NULL == pFileHandler)
181855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    {
182855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        switch(errno)
183855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        {
184855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        case ENOENT:
185855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi            {
186855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi                M4OSA_DEBUG(M4ERR_FILE_NOT_FOUND, "M4OSA_fileCommonOpen: No such file or directory");
187855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi                M4OSA_TRACE1_1("File not found: %s", pUrl);
188855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi                return M4ERR_FILE_NOT_FOUND;
189855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi            }
190855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        case EACCES:
191855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi            {
192855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi                M4OSA_DEBUG(M4ERR_FILE_LOCKED, "M4OSA_fileCommonOpen: Permission denied");
193855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi                return M4ERR_FILE_LOCKED;
194855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi            }
195855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi         case EINVAL:
196855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi         {
197855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi            M4OSA_DEBUG(M4ERR_FILE_BAD_MODE_ACCESS, "M4OSA_fileCommonOpen: Invalid Argument");
198855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi            return M4ERR_FILE_BAD_MODE_ACCESS;
199855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi         }
200855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        case EMFILE:
201855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi         case ENOSPC:
202855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        case ENOMEM:
203855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi            {
204855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi                M4OSA_DEBUG(M4ERR_ALLOC, "M4OSA_fileCommonOpen: Too many open files");
205855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi                return M4ERR_ALLOC;
206855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi            }
207855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        default:
208855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi            {
209855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi                M4OSA_DEBUG(M4ERR_NOT_IMPLEMENTED, "M4OSA_fileCommonOpen");
210855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi                return M4ERR_NOT_IMPLEMENTED;
211855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi            }
212855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        }
213855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    }
214855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
215855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    /* Allocate the file context */
216694816d7291f17364502ac5d3319684a0b180860Shyam Pallapothu    pFileContext = (M4OSA_FileContext*) M4OSA_32bitAlignedMalloc(sizeof(M4OSA_FileContext),
217855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi                    core_id, (M4OSA_Char*)"M4OSA_fileCommonOpen: file context");
218855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    if (M4OSA_NULL == pFileContext)
219855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    {
220855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        fclose(pFileHandler);
221855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        M4OSA_DEBUG(M4ERR_ALLOC, "M4OSA_fileCommonOpen");
222855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        return M4ERR_ALLOC;
223855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    }
224855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
225855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    pFileContext->file_desc        = pFileHandler;
226855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    pFileContext->access_mode    = fileModeAccess;
227855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    pFileContext->current_seek    = SeekNone;
228855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    pFileContext->b_is_end_of_file    = M4OSA_FALSE;
229855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
230855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    /**
231855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi     * Note: Never use this expression "i = (value1 == value2) ? x: y;"
232855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi     * because that doens't compile on other platforms (ADS for example)
233855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi     * Use: if(value1 == value2)
234855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi     *        { i= x; ..etc
235855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi     */
236855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    pFileContext->coreID_write = 0;
237855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    pFileContext->coreID_read = 0;
238855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    pFileContext->m_DescrModeAccess = M4OSA_kDescNoneAccess;
239855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
240855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    if (M4OSA_FILE_READER == core_id)
241855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    {
242855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        pFileContext->coreID_read = core_id;
243855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        pFileContext->m_DescrModeAccess = M4OSA_kDescReadAccess;
244855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    }
245855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    else if (M4OSA_FILE_WRITER == core_id)
246855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    {
247855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        pFileContext->coreID_write = core_id;
248855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        pFileContext->m_DescrModeAccess = M4OSA_kDescWriteAccess;
249855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    }
250855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
251aa382f3637a68361989d5b70e3184bddcc472d3dhongteng    pFileContext->read_position = 0;
252aa382f3637a68361989d5b70e3184bddcc472d3dhongteng    pFileContext->write_position = 0;
253855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
254855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    /* Allocate the memory to store the URL string */
255694816d7291f17364502ac5d3319684a0b180860Shyam Pallapothu    pFileContext->url_name = (M4OSA_Char*) M4OSA_32bitAlignedMalloc(strlen((const char *)pUrl)+1,
256855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi                        core_id, (M4OSA_Char*)"M4OSA_fileCommonOpen: URL name");
257855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    if (M4OSA_NULL == pFileContext->url_name)
258855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    {
259855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        fclose(pFileHandler);
260694816d7291f17364502ac5d3319684a0b180860Shyam Pallapothu        free(pFileContext);
261855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        M4OSA_DEBUG(M4ERR_ALLOC, "M4OSA_fileCommonOpen");
262855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        return M4ERR_ALLOC;
263855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    }
264947721ba962c19913b76658cdbb19ae8c77c9c50Dheeraj Sharma    M4OSA_chrNCopy(pFileContext->url_name, pUrl, strlen((const char *)pUrl)+1);
265855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
266855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    /* Get the file name */
267855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    err = M4OSA_fileCommonGetFilename(pUrl, &pFileContext->file_name);
268855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    if(M4NO_ERROR != err)
269855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    {
270855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        fclose(pFileHandler);
271694816d7291f17364502ac5d3319684a0b180860Shyam Pallapothu        free(pFileContext->url_name);
272694816d7291f17364502ac5d3319684a0b180860Shyam Pallapothu        free(pFileContext);
273855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        M4OSA_DEBUG(err, "M4OSA_fileCommonOpen");
274855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        return err;
275855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    }
276855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
277855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi#ifdef M4OSA_FILE_BLOCK_WITH_SEMAPHORE
278855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_semaphoreOpen(&(pFileContext->semaphore_context), 1); /* Allocate the semaphore */
279855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi#endif /* M4OSA_FILE_BLOCK_WITH_SEMAPHORE */
280855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
281855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
282855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
283855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi#ifdef USE_STAGEFRIGHT_CODECS
284855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    // Workaround for file system bug on Stingray/Honeycomb where a file re-created will keep
285855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    // the original file's size filled with 0s. Do not seek to the end to avoid ill effects
286855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    if(fileModeAccess & M4OSA_kFileAppend) {
287855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        /* Get the file size */
288855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        iSavePos = ftell(pFileHandler);            /*    1- Check the first position */
289855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        fseek(pFileHandler, 0, SEEK_END);        /*    2- Go to the end of the file*/
290855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        iSize = ftell(pFileHandler);            /*    3- Check the file size        */
291855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        fseek(pFileHandler, iSavePos, SEEK_SET);/*    4- go to the first position */
292855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    } else {
293855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        iSize = 0;
294855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    }
295855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi#else /* USE_STAGEFRIGHT_CODECS */
296855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    /* Get the file size */
297855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    iSavePos = ftell(pFileHandler);            /*    1- Check the first position */
298855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    fseek(pFileHandler, 0, SEEK_END);        /*    2- Go to the end of the file*/
299855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    iSize = ftell(pFileHandler);            /*    3- Check the file size        */
300855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    fseek(pFileHandler, iSavePos, SEEK_SET);/*    4- go to the first position */
301855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi#endif /* USE_STAGEFRIGHT_CODECS */
302855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
303855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
304855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
305855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    /* Warning possible overflow if the file is higher than 2GBytes */
306855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    pFileContext->file_size = iSize;
307855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
308855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    *pContext = pFileContext;
309855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
310855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    return M4NO_ERROR;
311855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi}
312855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
313855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
314855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi/**
315855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi ************************************************************************
316855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @brief      This function convert from UTF16 to UTF8
317855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @param      pBufferIn: (IN) UTF16 input path
318855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @param      pBufferOut: (OUT) UTF8 output path
319855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @param      bufferOutSize: (IN/OUT) size of the output path
320855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @return     M4NO_ERROR: there is no error
321855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @return     M4ERR_PARAMETER: the output path size is not enough to contain
322855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi *               the decoded path
323855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi ************************************************************************
324855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi*/
325855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi#ifdef UTF_CONVERSION
326855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray KundargiM4OSA_ERR M4OSA_ToUTF8_OSAL (M4OSA_Void   *pBufferIn, M4OSA_UInt8  *pBufferOut,
327855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi                                                    M4OSA_UInt32 *bufferOutSize)
328855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi{
329855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_UInt16 i;
330855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    wchar_t      *w_str = (wchar_t *) pBufferIn;
331855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_UInt32 len, size_needed, size_given;
332855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    if (pBufferIn == NULL)
333855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    {
334855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        *pBufferOut=NULL;
335855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        *bufferOutSize=1;
336855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    }
337855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    else
338855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    {
339855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        len         = wcslen(w_str);
340855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        size_needed = len+1;
341855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        size_given  = *bufferOutSize;
342855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
343855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi       *bufferOutSize=size_needed;
344855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        if (size_given < size_needed )
345855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        {
346855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi            return M4ERR_PARAMETER;
347855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        }
348855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        else
349855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        {
350855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi            for (i=0; i<len; i++)
351855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi            {
352855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi                pBufferOut[i]=(M4OSA_UInt8)w_str[i];
353855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi            }
354855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi            pBufferOut[len]=0;
355855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        }
356855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    }
357855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    return M4NO_ERROR;
358855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi}
359855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi#endif /*UTF CONVERSION*/
360855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
361855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi/**
362855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi ************************************************************************
363855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @brief      This function seeks at the provided position.
364855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @param      context: (IN/OUT) Context of the core file reader
365855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @param      seekMode: (IN) Seek access mode
366855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @param      position: (IN/OUT) Position in the file
367855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @return     M4NO_ERROR: there is no error
368855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @return     M4ERR_PARAMETER: at least one parameter is NULL
369855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @return     M4ERR_BAD_CONTEXT: provided context is not a valid one
370855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @return     M4ERR_ALLOC: there is no more memory available
371855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @return     M4ERR_FILE_INVALID_POSITION: the position cannot be reached
372855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi ************************************************************************
373855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi*/
374855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray KundargiM4OSA_ERR M4OSA_fileCommonSeek(M4OSA_Context pContext,
375855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi                               M4OSA_FileSeekAccessMode seekMode,
376855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi                               M4OSA_FilePosition* pFilePos)
377855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi{
378855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_FileContext* pFileContext = pContext;
379855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_FilePosition fpos_current;
380855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_FilePosition fpos_seek;
381aa382f3637a68361989d5b70e3184bddcc472d3dhongteng    M4OSA_FilePosition fpos_null = 0;
382aa382f3637a68361989d5b70e3184bddcc472d3dhongteng    M4OSA_FilePosition fpos_neg_un = -1;
383855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_FilePosition fpos_file_size;
384855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_FilePosition fpos_seek_from_beginning;
385855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
386855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_TRACE3_3("M4OSA_fileCommonSeek\t\tM4OSA_Context 0x%x\t M4OSA_FileSeekAccessMode %d\tM4OSA_FilePosition* 0x%x",
387855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        pContext, seekMode, pFilePos);
388855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
389855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_DEBUG_IF2(M4OSA_NULL == pContext, M4ERR_PARAMETER, "M4OSA_fileCommonSeek");
390855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_DEBUG_IF2(0 == seekMode, M4ERR_PARAMETER, "M4OSA_fileCommonSeek");
391855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_DEBUG_IF2(M4OSA_NULL == pFilePos, M4ERR_PARAMETER, "M4OSA_fileCommonSeek");
392855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
393aa382f3637a68361989d5b70e3184bddcc472d3dhongteng    fpos_file_size = pFileContext->file_size;
394855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
395855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    if(SeekRead == pFileContext->current_seek)
396855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    {
397aa382f3637a68361989d5b70e3184bddcc472d3dhongteng        fpos_current = pFileContext->read_position;
398855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    }
399855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    else if(SeekWrite == pFileContext->current_seek)
400855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    {
401aa382f3637a68361989d5b70e3184bddcc472d3dhongteng        fpos_current = pFileContext->write_position;
402855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    }
403855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    else
404855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    {
405aa382f3637a68361989d5b70e3184bddcc472d3dhongteng        fpos_current = 0;
406855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    }
407855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
408855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    switch(seekMode)
409855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    {
410855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    case M4OSA_kFileSeekCurrent:
411855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        {
412aa382f3637a68361989d5b70e3184bddcc472d3dhongteng            fpos_seek = *pFilePos;
413855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi            break;
414855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        }
415855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    case M4OSA_kFileSeekBeginning:
416855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        {
417aa382f3637a68361989d5b70e3184bddcc472d3dhongteng            fpos_seek = *pFilePos - fpos_current;
418aa382f3637a68361989d5b70e3184bddcc472d3dhongteng            break;
419855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        }
420855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    case M4OSA_kFileSeekEnd:
421855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        {
422aa382f3637a68361989d5b70e3184bddcc472d3dhongteng            fpos_seek = *pFilePos + fpos_file_size - fpos_current;
423855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi            break;
424855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        }
425855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    default:
426855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        {
427855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi            return M4ERR_PARAMETER;
428855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        }
429855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    }
430855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
431aa382f3637a68361989d5b70e3184bddcc472d3dhongteng    fpos_seek_from_beginning = fpos_current + fpos_seek;
432855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
433855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    if(fseek(pFileContext->file_desc, fpos_seek, SEEK_CUR) != 0)
434855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    {
435855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        switch(errno)
436855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        {
437855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        case EINVAL:
438855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi            {
439855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi            /* meaning the value for origin is invalid or the position
440855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi                specified by offset is before the beginning of the file */
441855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi                return M4ERR_FILE_INVALID_POSITION;
442855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi            }
443855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
444855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        case EBADF:
445855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        default:
446855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi            {
447855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi                return M4ERR_BAD_CONTEXT;/* file handle is invalid */
448855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi            }
449855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        }
450855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    }
451855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
452855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    /* Set the returned position from the beginning of the file */
453aa382f3637a68361989d5b70e3184bddcc472d3dhongteng    *pFilePos = fpos_seek_from_beginning;
454855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
455855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    /* SEEK done, reset end of file value */
456855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    pFileContext->b_is_end_of_file = M4OSA_FALSE;
457855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
458855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    return M4NO_ERROR;
459855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi}
460855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
461855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
462855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi/**
463855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi ************************************************************************
464855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @brief      This function asks to close the file (associated to the context)
465855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @note       The context of the core file reader/writer is freed.
466855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @param      context: (IN/OUT) Context of the core file reader
467855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @return     M4NO_ERROR: there is no error
468855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @return     M4ERR_PARAMETER: at least one parameter is NULL
469855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @return     M4ERR_BAD_CONTEXT: provided context is not a valid one
470855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @return     M4ERR_ALLOC: there is no more memory available
471855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi ************************************************************************
472855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi*/
473855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
474855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray KundargiM4OSA_ERR M4OSA_fileCommonClose(M4OSA_UInt16 core_id, M4OSA_Context pContext)
475855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi{
476855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_FileContext* pFileContext = pContext;
477855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_Int32 i32_err_code=0;
478855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
479855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_TRACE3_2("M4OSA_fileCommonClose\tM4OSA_UInt16 %d\tM4OSA_Context 0x%x",
480855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi                                                             core_id, pContext);
481855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_DEBUG_IF2(M4OSA_NULL == pContext,
482855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi              M4ERR_PARAMETER, "M4OSA_fileCommonClose: pContext is M4OSA_NULL");
483855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi#ifdef M4OSA_FILE_BLOCK_WITH_SEMAPHORE
484855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_DEBUG_IF2(M4OSA_NULL == pFileContext->semaphore_context, M4ERR_BAD_CONTEXT,
485855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi                     "M4OSA_fileCommonClose: semaphore_context is M4OSA_NULL");
486855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi#endif /* M4OSA_FILE_BLOCK_WITH_SEMAPHORE */
487855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
488694816d7291f17364502ac5d3319684a0b180860Shyam Pallapothu    free(pFileContext->url_name);
489855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    pFileContext->url_name = M4OSA_NULL;
490855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
491694816d7291f17364502ac5d3319684a0b180860Shyam Pallapothu    free(pFileContext->file_name);
492855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    pFileContext->file_name = M4OSA_NULL;
493855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
494855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    i32_err_code = fclose(pFileContext->file_desc);
495855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
496855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    pFileContext->file_desc = M4OSA_NULL;
497855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
498855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi#ifdef M4OSA_FILE_BLOCK_WITH_SEMAPHORE
499855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_semaphoreClose(pFileContext->semaphore_context);/* free the semaphore */
500855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi#endif /* M4OSA_FILE_BLOCK_WITH_SEMAPHORE */
501855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
502694816d7291f17364502ac5d3319684a0b180860Shyam Pallapothu    free(pFileContext);
503855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
504855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    if (i32_err_code != 0)
505855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    {
506855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        M4OSA_DEBUG(M4ERR_BAD_CONTEXT, "M4OSA_fileCommonClose");
507855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        return M4ERR_BAD_CONTEXT;
508855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    }
509855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
510855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    return M4NO_ERROR;
511855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi}
512855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
513855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
514855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi/**
515855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi ************************************************************************
516855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @brief      This function gets the file attributes (associated to the
517855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi *             context)
518855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @param      context: (IN) Context of the core file reader
519855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @param      attribute: (OUT) The file attribute (allocated by the caller)
520855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @return     M4NO_ERROR: there is no error
521855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @return     M4ERR_PARAMETER: at least one parameter is NULL
522855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @return     M4ERR_BAD_CONTEXT: provided context is not a valid one
523855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi ************************************************************************
524855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi*/
525855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray KundargiM4OSA_ERR M4OSA_fileCommonGetAttribute(M4OSA_Context pContext, M4OSA_FileAttribute* pAttribute)
526855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi{
527855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
528855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_FileContext* fileContext = pContext;
529855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
530855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    struct stat TheStat;
531855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
532855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_TRACE3_2("M4OSA_fileCommonGetAttribute\tM4OSA_Context 0x%x\t"
533855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        "M4OSA_FileAttribute* 0x%x", pContext, pAttribute);
534855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
535855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_DEBUG_IF2(M4OSA_NULL == pContext,        M4ERR_PARAMETER, "M4OSA_fileCommonGetAttribute");
536855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_DEBUG_IF2(M4OSA_NULL == pAttribute,    M4ERR_PARAMETER, "M4OSA_fileCommonGetAttribute");
537855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
538855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    if(stat((char*)fileContext->url_name, &TheStat) != 0)
539855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    {
540855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        M4OSA_DEBUG(M4ERR_BAD_CONTEXT, "M4OSA_fileCommonGetAttribute");
541855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        return M4ERR_BAD_CONTEXT;
542855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    }
543855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
544aa382f3637a68361989d5b70e3184bddcc472d3dhongteng    pAttribute->creationDate.time = (M4OSA_Time)TheStat.st_ctime;
545aa382f3637a68361989d5b70e3184bddcc472d3dhongteng    pAttribute->lastAccessDate.time = (M4OSA_Time)TheStat.st_atime;
546aa382f3637a68361989d5b70e3184bddcc472d3dhongteng    pAttribute->modifiedDate.time = (M4OSA_Time)TheStat.st_mtime;
547855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
548aa382f3637a68361989d5b70e3184bddcc472d3dhongteng    pAttribute->creationDate.timeScale = 1;
549aa382f3637a68361989d5b70e3184bddcc472d3dhongteng    pAttribute->lastAccessDate.timeScale = 1;
550aa382f3637a68361989d5b70e3184bddcc472d3dhongteng    pAttribute->modifiedDate.timeScale = 1;
551855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
552aa382f3637a68361989d5b70e3184bddcc472d3dhongteng    pAttribute->creationDate.referenceYear = 1970;
553aa382f3637a68361989d5b70e3184bddcc472d3dhongteng    pAttribute->lastAccessDate.referenceYear = 1970;
554aa382f3637a68361989d5b70e3184bddcc472d3dhongteng    pAttribute->modifiedDate.referenceYear = 1970;
555855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
556855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    pAttribute->modeAccess = fileContext->access_mode;
557855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
558855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    return M4NO_ERROR;
559855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi}
560855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
561855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi/**
562855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi ************************************************************************
563855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @brief      This function gets the file URL (associated to the context).
564855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @note
565855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @param      context: (IN) Context of the core file reader
566855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @param      url: (OUT) The buffer containing the URL (allocated by
567855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi *             M4OSA_fileCommonGetURL)
568855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @return     M4NO_ERROR: there is no error
569855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @return     M4ERR_PARAMETER: at least one parameter is NULL
570855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @return     M4ERR_BAD_CONTEXT: provided context is not a valid one
571855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @return     M4ERR_ALLOC: there is no more memory available
572855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi ************************************************************************
573855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi*/
574855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray KundargiM4OSA_ERR M4OSA_fileCommonGetURL(M4OSA_Context pContext, M4OSA_Char** pUrl)
575855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi{
576855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_FileContext* pFileContext = pContext;
577855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_UInt32    uiLength;
578855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
579855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_TRACE3_2("M4OSA_fileCommonGetURL\tM4OSA_Context 0x%x\tM4OSA_Char** 0x%x",
580855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi                    pContext, pUrl);
581855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
582855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_DEBUG_IF2(M4OSA_NULL == pContext,    M4ERR_PARAMETER,
583855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi                              "M4OSA_fileCommonGetURL: pContext is M4OSA_NULL");
584855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_DEBUG_IF2(M4OSA_NULL == pUrl,    M4ERR_PARAMETER,
585855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi                                  "M4OSA_fileCommonGetURL: pUrl is M4OSA_NULL");
586855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
587947721ba962c19913b76658cdbb19ae8c77c9c50Dheeraj Sharma    uiLength = strlen((const char *)pFileContext->url_name)+1;
588855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
589855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    /* Allocate the memory to store the url_name */
590694816d7291f17364502ac5d3319684a0b180860Shyam Pallapothu    *pUrl = (M4OSA_Char*)M4OSA_32bitAlignedMalloc(uiLength, M4OSA_FILE_COMMON,
591855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi                                    (M4OSA_Char*)"M4OSA_fileCommonGetURL: url");
592855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    if(M4OSA_NULL == *pUrl)
593855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    {
594855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        M4OSA_DEBUG(M4ERR_ALLOC, "M4OSA_fileCommonGetURL");
595855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        return M4ERR_ALLOC;
596855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    }
597855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
598855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_chrNCopy(*pUrl, pFileContext->url_name, uiLength);
599855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
600855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    return M4NO_ERROR;
601855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi}
602855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
603855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
604855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi/**
605855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi ************************************************************************
606855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @brief      This function gets a string containing the file name associated
607855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi *             to the input URL.
608855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @note       The user should not forget to delete the output string using
609855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi *             M4OSA_strDestroy
610855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @param      pUrl:            (IN) The buffer containing the URL
611855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @param      pFileName:    (OUT) The string containing the URL. It is
612855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi *                            allocated inside this function
613855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @return     M4NO_ERROR: there is no error
614855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @return     M4ERR_NOT_IMPLEMENTED: the URL does not match with the supported
615855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi *             file
616855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi * @return     M4ERR_ALLOC: there is no more memory available
617855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi ************************************************************************
618855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi*/
619855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray KundargiM4OSA_ERR M4OSA_fileCommonGetFilename(M4OSA_Char* pUrl, M4OSA_Char** pFileName)
620855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi{
621855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_Int32 i            = 0;
622855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_Int32 iUrlLen        = 0;
623855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_Int32 FileNameLen = 0;
624855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
625855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_Char* ptrUrl        = M4OSA_NULL;
626855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_Char* ptrFilename    = M4OSA_NULL;
627855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
628855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_TRACE3_2("M4OSA_fileCommonGetURL\tM4OSA_Char* %s\tM4OSA_Char** 0x%x",
629855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi                                                               pUrl, pFileName);
630855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
631855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_DEBUG_IF2(M4OSA_NULL == pUrl,    M4ERR_PARAMETER,
632855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi                             "M4OSA_fileCommonGetFilename: pUrl is M4OSA_NULL");
633855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_DEBUG_IF2(M4OSA_NULL == pFileName,    M4ERR_PARAMETER,
634855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi                        "M4OSA_fileCommonGetFilename: pFileName is M4OSA_NULL");
635855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
636855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    *pFileName = M4OSA_NULL;
637855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
638855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    /*Parse URL*/
639947721ba962c19913b76658cdbb19ae8c77c9c50Dheeraj Sharma    iUrlLen = strlen((const char *)pUrl);
640855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    for(i=iUrlLen-1; i>=0; i--)
641855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    {
642855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        if (pUrl[i] != '\\' && pUrl[i] != '/')
643855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        {
644855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi            FileNameLen++;
645855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        }
646855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        else
647855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        {
648855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi            break; /* find the beginning of the file name */
649855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        }
650855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    }
651855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
652694816d7291f17364502ac5d3319684a0b180860Shyam Pallapothu    ptrFilename = (M4OSA_Char*) M4OSA_32bitAlignedMalloc(FileNameLen+1, M4OSA_FILE_COMMON,
653855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi                    (M4OSA_Char*)"M4OSA_fileCommonGetFilename: Filename string");
654855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    if (ptrFilename == M4OSA_NULL)
655855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    {
656855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        M4OSA_DEBUG(M4ERR_ALLOC, "M4OSA_fileCommonGetFilename");
657855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi        return M4ERR_ALLOC;
658855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    }
659855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
660855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    ptrUrl = pUrl + (iUrlLen - FileNameLen);
661855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    M4OSA_chrNCopy(ptrFilename, ptrUrl, FileNameLen+1);
662855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
663855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    *pFileName = ptrFilename;
664855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
665855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi    return M4NO_ERROR;
666855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi}
667855ec7c4be7ad642721909d5837a8d25a117c22fDharmaray Kundargi
668