<commit_before>/* --------------------------------------------------------------------------

   MusicBrainz -- The Internet music metadatabase

   Copyright (C) 2000 <NAME>
   Copyright (C) 2000 EMusic.com
   
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.
   
   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.
   
   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

     $Id$

----------------------------------------------------------------------------*/
#include <stdio.h> 
#include <sys/types.h>
#include <sys/stat.h>
#include <ctype.h>
#ifndef WIN32
#include <sys/time.h>
#endif
#include <fcntl.h>    
#include <errno.h>    

#ifdef WIN32
#include <winsock.h>
#include <io.h>
#else
#undef socklen_t
#include <netinet/in.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#define SOCKET int
#define closesocket(x) close(x)
#include <arpa/inet.h>
#endif  

#if defined(__BEOS__)
#include <OS.h>
#define usleep snooze
#endif

#if defined(sun)      // Solaris headers do not define INADDR_NONE
#define INADDR_NONE   0xffffffff
#endif

#include "http.h"
#include "musicbrainz.h"
#ifdef WIN32
#include "../config_win32.h"
#else
#include "config.h"
#endif

#ifdef __QNX__
#include <strings.h>
#endif

using namespace std;

const unsigned short kHttpPort = 80;
const unsigned int kMaxHostNameLen = 64;
const unsigned int kMaxURLLen = 1024;
const unsigned int kBufferSize=8192;
const unsigned int iSleepTime = 10000; // time to sleep between selects in usec
const unsigned int iSleepCount = 3000; // how many sleep times before timing out

#define DB printf("%s:%d\n", __FILE__, __LINE__);

MBHttp::MBHttp(void)
{
    m_exit = false;
    m_file = NULL;
    m_buffer = NULL;
    m_bytesInBuffer = 0;
    m_bufferSize = 0;
}

MBHttp::~MBHttp(void)
{
    if (m_buffer)
       delete [] m_buffer;

    if (m_file)
        fclose(m_file);
}

void MBHttp::SetProxyURL(const string &proxy)
{
    m_proxy = proxy;    
}

Error MBHttp::DownloadToString(const string &url, 
                               const string &xml, 
                               string &page)
{
    Error eRet;

    eRet = Download(url, xml, false);
    if (IsntError(eRet))
        page = string((char *)m_buffer, m_bytesInBuffer);

    if (m_buffer)
    {
       delete [] m_buffer;
       m_buffer = NULL;
    }

    return eRet;
}

Error MBHttp::DownloadToFile(const string &url, 
                             const string &xml, 
                             const string &destPath)
{
    Error eRet;

    m_destPath = destPath;
    eRet = Download(url, xml, true);
    if (m_file)
    {
        fclose(m_file);
        m_file = NULL;
    }

    return eRet;
}

int MBHttp::WriteToBuffer(unsigned char *buffer, unsigned int size)
{
    if (m_buffer == NULL)
    {
        m_bufferSize = kBufferSize;
        m_buffer = new unsigned char[m_bufferSize];
    }
    if (m_bytesInBuffer + size > m_bufferSize)
    {
        unsigned char *pTemp;

        m_bufferSize += (kBufferSize < size) ? kBufferSize : kBufferSize + size;
        pTemp = new unsigned char[m_bufferSize];

        memcpy(pTemp, m_buffer, m_bytesInBuffer);
        delete [] m_buffer;
        m_buffer = pTemp;
    }

    memcpy(m_buffer + m_bytesInBuffer, buffer, size);
    m_bytesInBuffer += size;
    m_buffer[m_bytesInBuffer] = 0;<commit_msg>Fix buffer overflow in MBHttp::WriteToBuffer. (Closes #2273)<commit_after>/* --------------------------------------------------------------------------

   MusicBrainz -- The Internet music metadatabase

   Copyright (C) 2000 <NAME>
   Copyright (C) 2000 EMusic.com
   
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.
   
   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.
   
   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

     $Id$

----------------------------------------------------------------------------*/
#include <stdio.h> 
#include <sys/types.h>
#include <sys/stat.h>
#include <ctype.h>
#ifndef WIN32
#include <sys/time.h>
#endif
#include <fcntl.h>    
#include <errno.h>    

#ifdef WIN32
#include <winsock.h>
#include <io.h>
#else
#undef socklen_t
#include <netinet/in.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#define SOCKET int
#define closesocket(x) close(x)
#include <arpa/inet.h>
#endif  

#if defined(__BEOS__)
#include <OS.h>
#define usleep snooze
#endif

#if defined(sun)      // Solaris headers do not define INADDR_NONE
#define INADDR_NONE   0xffffffff
#endif

#include "http.h"
#include "musicbrainz.h"
#ifdef WIN32
#include "../config_win32.h"
#else
#include "config.h"
#endif

#ifdef __QNX__
#include <strings.h>
#endif

using namespace std;

const unsigned short kHttpPort = 80;
const unsigned int kMaxHostNameLen = 64;
const unsigned int kMaxURLLen = 1024;
const unsigned int kBufferSize=8192;
const unsigned int iSleepTime = 10000; // time to sleep between selects in usec
const unsigned int iSleepCount = 3000; // how many sleep times before timing out

#define DB printf("%s:%d\n", __FILE__, __LINE__);

MBHttp::MBHttp(void)
{
    m_exit = false;
    m_file = NULL;
    m_buffer = NULL;
    m_bytesInBuffer = 0;
    m_bufferSize = 0;
}

MBHttp::~MBHttp(void)
{
    if (m_buffer)
       delete [] m_buffer;

    if (m_file)
        fclose(m_file);
}

void MBHttp::SetProxyURL(const string &proxy)
{
    m_proxy = proxy;    
}

Error MBHttp::DownloadToString(const string &url, 
                               const string &xml, 
                               string &page)
{
    Error eRet;

    eRet = Download(url, xml, false);
    if (IsntError(eRet))
        page = string((char *)m_buffer, m_bytesInBuffer);

    if (m_buffer)
    {
       delete [] m_buffer;
       m_buffer = NULL;
    }

    return eRet;
}

Error MBHttp::DownloadToFile(const string &url, 
                             const string &xml, 
                             const string &destPath)
{
    Error eRet;

    m_destPath = destPath;
    eRet = Download(url, xml, true);
    if (m_file)
    {
        fclose(m_file);
        m_file = NULL;
    }

    return eRet;
}

int MBHttp::WriteToBuffer(unsigned char *buffer, unsigned int size)
{
    if (m_buffer == NULL)
    {
        m_bufferSize = kBufferSize;
        m_buffer = new unsigned char[m_bufferSize + 1];
    }
    if (m_bytesInBuffer + size > m_bufferSize)
    {
        unsigned char *pTemp;

        m_bufferSize += ((size / kBufferSize) + 1) * kBufferSize;
        pTemp = new unsigned char[m_bufferSize + 1];

        memcpy(pTemp, m_buffer, m_bytesInBuffer);
        delete [] m_buffer;
        m_buffer = pTemp;
    }

    memcpy(m_buffer + m_bytesInBuffer, buffer, size);
    m_bytesInBuffer += size;
    m_buffer[m_bytesInBuffer] = 0;
{'fraction_non_alphanumeric': 0.13224730631092868, 'fraction_numerical': 0.0155207798871216, 'mean_word_length': 2.457649667405765, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1941729', 'n_tokens_mistral': 2582, 'n_tokens_neox': 2452, 'n_words': 784}