vfs: check userland buffers before reading them.
[haiku.git] / src / add-ons / media / plugins / ape_reader / MAClib / APELink.cpp
blob27fc5cbfc8dded65321a68a426f8965e067a33f4
1 #include "NoWindows.h"
3 #include "All.h"
4 #include "APELink.h"
5 #include "CharacterHelper.h"
6 #include IO_HEADER_FILE
8 #define APE_LINK_HEADER "[Monkey's Audio Image Link File]"
9 #define APE_LINK_IMAGE_FILE_TAG "Image File="
10 #define APE_LINK_START_BLOCK_TAG "Start Block="
11 #define APE_LINK_FINISH_BLOCK_TAG "Finish Block="
13 CAPELink::CAPELink(const str_utf16 * pFilename)
15 // empty
16 m_bIsLinkFile = FALSE;
17 m_nStartBlock = 0;
18 m_nFinishBlock = 0;
19 m_cImageFilename[0] = 0;
21 // open the file
22 IO_CLASS_NAME ioLinkFile;
23 if (ioLinkFile.Open(pFilename) == ERROR_SUCCESS)
25 // create a buffer
26 CSmartPtr<char> spBuffer(new char [1024], TRUE);
28 // fill the buffer from the file and null terminate it
29 unsigned int nBytesRead = 0;
30 ioLinkFile.Read(spBuffer.GetPtr(), 1023, &nBytesRead);
31 spBuffer[nBytesRead] = 0;
33 // call the other constructor (uses a buffer instead of opening the file)
34 ParseData(spBuffer, pFilename);
38 CAPELink::CAPELink(const char * pData, const str_utf16 * pFilename)
40 ParseData(pData, pFilename);
43 CAPELink::~CAPELink()
47 void CAPELink::ParseData(const char * pData, const str_utf16 * pFilename)
49 // empty
50 m_bIsLinkFile = FALSE;
51 m_nStartBlock = 0;
52 m_nFinishBlock = 0;
53 m_cImageFilename[0] = 0;
55 if (pData != NULL)
57 // parse out the information
58 char * pHeader = strstr(pData, APE_LINK_HEADER);
59 char * pImageFile = strstr(pData, APE_LINK_IMAGE_FILE_TAG);
60 char * pStartBlock = strstr(pData, APE_LINK_START_BLOCK_TAG);
61 char * pFinishBlock = strstr(pData, APE_LINK_FINISH_BLOCK_TAG);
63 if (pHeader && pImageFile && pStartBlock && pFinishBlock)
65 if ((_strnicmp(pHeader, APE_LINK_HEADER, strlen(APE_LINK_HEADER)) == 0) &&
66 (_strnicmp(pImageFile, APE_LINK_IMAGE_FILE_TAG, strlen(APE_LINK_IMAGE_FILE_TAG)) == 0) &&
67 (_strnicmp(pStartBlock, APE_LINK_START_BLOCK_TAG, strlen(APE_LINK_START_BLOCK_TAG)) == 0) &&
68 (_strnicmp(pFinishBlock, APE_LINK_FINISH_BLOCK_TAG, strlen(APE_LINK_FINISH_BLOCK_TAG)) == 0))
70 // get the start and finish blocks
71 m_nStartBlock = atoi(&pStartBlock[strlen(APE_LINK_START_BLOCK_TAG)]);
72 m_nFinishBlock = atoi(&pFinishBlock[strlen(APE_LINK_FINISH_BLOCK_TAG)]);
74 // get the path
75 char cImageFile[MAX_PATH + 1]; int nIndex = 0;
76 char * pImageCharacter = &pImageFile[strlen(APE_LINK_IMAGE_FILE_TAG)];
77 while ((*pImageCharacter != 0) && (*pImageCharacter != '\r') && (*pImageCharacter != '\n'))
78 cImageFile[nIndex++] = *pImageCharacter++;
79 cImageFile[nIndex] = 0;
81 CSmartPtr<str_utf16> spImageFileUTF16(GetUTF16FromUTF8((UCHAR *) cImageFile), TRUE);
83 // process the path
84 // SHINTA: w_char -> char -->
85 if (strrchr(spImageFileUTF16, '\\') == NULL)
87 str_utf16 cImagePath[MAX_PATH + 1];
88 strcpy(cImagePath, pFilename);
89 strcpy(strrchr(cImagePath, '\\') + 1, spImageFileUTF16);
90 strcpy(m_cImageFilename, cImagePath);
92 else
94 strcpy(m_cImageFilename, spImageFileUTF16);
96 // <-- SHINTA
98 // this is a valid link file
99 m_bIsLinkFile = TRUE;
105 int CAPELink::GetStartBlock()
107 return m_nStartBlock;
110 int CAPELink::GetFinishBlock()
112 return m_nFinishBlock;
115 const str_utf16 * CAPELink::GetImageFilename()
117 return m_cImageFilename;
120 BOOL CAPELink::GetIsLinkFile()
122 return m_bIsLinkFile;