2006-10-28 19:45:02 +00:00
|
|
|
/*
|
|
|
|
* writer.cpp
|
|
|
|
*
|
|
|
|
* This file is a part of NSIS.
|
|
|
|
*
|
2021-01-01 20:27:52 +00:00
|
|
|
* Copyright (C) 1999-2021 Nullsoft and Contributors
|
2006-10-28 19:45:02 +00:00
|
|
|
*
|
|
|
|
* Licensed under the zlib/libpng license (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
*
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
|
|
|
* This software is provided 'as-is', without any express or implied
|
|
|
|
* warranty.
|
2010-03-24 17:22:56 +00:00
|
|
|
*
|
|
|
|
* Unicode support by Jim Park -- 08/13/2007
|
2006-10-28 19:45:02 +00:00
|
|
|
*/
|
|
|
|
|
2006-03-11 11:13:07 +00:00
|
|
|
#include "exehead/config.h"
|
|
|
|
#include "writer.h"
|
|
|
|
#include "growbuf.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdexcept>
|
2010-03-24 17:22:56 +00:00
|
|
|
#include "tchar.h"
|
2006-03-11 11:13:07 +00:00
|
|
|
|
|
|
|
void writer_sink::write_byte(const unsigned char b)
|
|
|
|
{
|
|
|
|
write_data(&b, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void writer_sink::write_short(const short s)
|
|
|
|
{
|
|
|
|
short fs = FIX_ENDIAN_INT16(s);
|
|
|
|
write_data(&fs, sizeof(short));
|
|
|
|
}
|
|
|
|
|
|
|
|
void writer_sink::write_int(const int i)
|
|
|
|
{
|
|
|
|
int fi = FIX_ENDIAN_INT32(i);
|
|
|
|
write_data(&fi, sizeof(int));
|
|
|
|
}
|
2014-02-08 00:13:52 +00:00
|
|
|
void writer_sink::write_int64(const INT64 i)
|
|
|
|
{
|
|
|
|
INT64 fi = FIX_ENDIAN_INT64(i);
|
|
|
|
write_data(&fi, sizeof(INT64));
|
|
|
|
}
|
2006-03-11 11:13:07 +00:00
|
|
|
|
|
|
|
void writer_sink::write_int_array(const int i[], const size_t len)
|
|
|
|
{
|
|
|
|
for (size_t l = 0; l < len; l++)
|
|
|
|
{
|
|
|
|
write_int(i[l]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-24 17:22:56 +00:00
|
|
|
// size in this case is the length of the string to write.
|
2010-04-12 16:00:17 +00:00
|
|
|
void writer_sink::write_string(const TCHAR *s, size_t size)
|
2006-03-11 11:13:07 +00:00
|
|
|
{
|
2010-04-12 16:00:17 +00:00
|
|
|
#ifdef _UNICODE
|
2015-09-18 15:55:56 +00:00
|
|
|
if (m_ti.is_unicode())
|
2010-04-12 16:00:17 +00:00
|
|
|
{
|
2010-06-14 10:07:22 +00:00
|
|
|
bool strEnd = false;
|
2014-07-19 19:21:43 +00:00
|
|
|
TCHAR ch = L'\0';
|
2010-06-14 10:07:22 +00:00
|
|
|
for (; size ; size--)
|
2010-04-12 16:00:17 +00:00
|
|
|
{
|
2010-06-14 10:07:22 +00:00
|
|
|
if (!strEnd)
|
|
|
|
{
|
|
|
|
ch = *s++;
|
|
|
|
if (ch == _T('\0'))
|
|
|
|
strEnd = true;
|
|
|
|
}
|
|
|
|
write_short(ch);
|
2010-04-12 16:00:17 +00:00
|
|
|
}
|
|
|
|
}
|
2010-06-14 10:07:22 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
char *wb = new char[size];
|
|
|
|
memset(wb, 0, size);
|
2014-02-08 00:13:52 +00:00
|
|
|
WideCharToMultiByte(CP_ACP, 0, s, -1, wb, (int)size, NULL, NULL);
|
2010-06-14 10:07:22 +00:00
|
|
|
write_data(wb, size);
|
|
|
|
delete [] wb;
|
|
|
|
}
|
2010-04-12 16:00:17 +00:00
|
|
|
#else
|
2015-09-18 15:55:56 +00:00
|
|
|
//TODO: Why does this allocate memory? It could just write the string and a manual zero character?
|
2006-03-11 11:13:07 +00:00
|
|
|
char *wb = new char[size];
|
|
|
|
memset(wb, 0, size);
|
|
|
|
strncpy(wb, s, size);
|
|
|
|
write_data(wb, size);
|
|
|
|
delete [] wb;
|
2010-04-12 16:00:17 +00:00
|
|
|
#endif
|
2006-03-11 11:13:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void writer_sink::write_growbuf(const IGrowBuf *b)
|
|
|
|
{
|
|
|
|
write_data(b->get(), b->getlen());
|
|
|
|
}
|
|
|
|
|
2015-09-17 14:30:07 +00:00
|
|
|
namespace hlp {
|
|
|
|
template<class T> static inline bool issigned() { return T(-1) < T(0); }
|
|
|
|
template<class T> static inline bool issigned(const T&t) { return issigned<T>(); }
|
|
|
|
}
|
|
|
|
|
2006-03-11 11:13:07 +00:00
|
|
|
void growbuf_writer_sink::write_data(const void *data, const size_t size)
|
|
|
|
{
|
2015-09-17 14:30:07 +00:00
|
|
|
// TODO: Replace all of this with a simple call when GrowBuf is changed to use size_t
|
|
|
|
if (sizeof(size) == sizeof(sink_type::size_type) && hlp::issigned(size) == hlp::issigned<sink_type::size_type>())
|
|
|
|
{
|
|
|
|
m_buf->add(data, truncate_cast(sink_type::size_type, size));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
size_t left = size;
|
|
|
|
sink_type::size_type cbmaxadd = INT_MAX, cb;
|
|
|
|
for (char *p = (char *) data; left; p += cb, left -= cb)
|
|
|
|
{
|
|
|
|
cb = left >= (size_t) cbmaxadd ? cbmaxadd : (sink_type::size_type) left;
|
|
|
|
m_buf->add(p, cb);
|
|
|
|
}
|
|
|
|
}
|
2006-03-11 11:13:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void file_writer_sink::write_data(const void *data, const size_t size)
|
|
|
|
{
|
|
|
|
if (fwrite(data, 1, size, m_fp) != size)
|
|
|
|
{
|
|
|
|
throw std::runtime_error("error writing");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef NSIS_CONFIG_CRC_SUPPORT
|
2006-06-16 14:12:04 +00:00
|
|
|
#include "crc32.h"
|
2006-03-11 11:13:07 +00:00
|
|
|
|
|
|
|
void crc_writer_sink::write_data(const void *data, const size_t size)
|
|
|
|
{
|
2015-09-17 14:30:07 +00:00
|
|
|
*m_crc = CRC32(*m_crc, (const unsigned char *) data, size);
|
2006-03-11 11:13:07 +00:00
|
|
|
}
|
|
|
|
#endif
|