Debian Codegraph @

Source/writer.cpp

raw
1 /*
2 * writer.cpp
3 *
4 * This file is a part of NSIS.
5 *
6 * Copyright (C) 1999-2026 Nullsoft and Contributors
7 *
8 * Licensed under the zlib/libpng license (the "License");
9 * you may not use this file except in compliance with the License.
10 *
11 * Licence details can be found in the file COPYING.
12 *
13 * This software is provided 'as-is', without any express or implied
14 * warranty.
15 *
16 * Unicode support by Jim Park -- 08/13/2007
17 */
18
19 #include "exehead/config.h"
20 #include "writer.h"
21 #include "growbuf.h"
22 #include "util.h"
23 #include <string.h>
24 #include <stdlib.h>
25 #include <stdexcept>
26 #include "tchar.h"
27
28 void writer_sink::write_byte(const unsigned char b)
29 {
30 write_data(&b, 1);
31 }
32
33 void writer_sink::write_short(const short s)
34 {
35 short fs = FIX_ENDIAN_INT16(s);
36 write_data(&fs, sizeof(short));
37 }
38
39 void writer_sink::write_int(const int i)
40 {
41 int fi = FIX_ENDIAN_INT32(i);
42 write_data(&fi, sizeof(int));
43 }
44 void writer_sink::write_int64(const INT64 i)
45 {
46 INT64 fi = FIX_ENDIAN_INT64(i);
47 write_data(&fi, sizeof(INT64));
48 }
49
50 void writer_sink::write_int_array(const int i[], const size_t len)
51 {
52 for (size_t l = 0; l < len; l++)
53 {
54 write_int(i[l]);
55 }
56 }
57
58 // size in this case is the length of the string to write.
59 void writer_sink::write_string(const TCHAR *s, size_t size)
60 {
61 #ifdef _UNICODE
62 if (m_ti.is_unicode())
63 {
64 bool strEnd = false;
65 TCHAR ch = L'\0';
66 for (; size ; size--)
67 {
68 if (!strEnd)
69 {
70 ch = *s++;
71 if (ch == _T('\0'))
72 strEnd = true;
73 }
74 write_short(ch);
75 }
76 }
77 else
78 {
79 char *wb = new char[size];
80 memset(wb, 0, size);
81 WideCharToMultiByte(CP_ACP, 0, s, -1, wb, (int)size, NULL, NULL);
82 write_data(wb, size);
83 delete [] wb;
84 }
85 #else
86 //TODO: Why does this allocate memory? It could just write the string and a manual zero character?
87 char *wb = new char[size];
88 memset(wb, 0, size);
89 strncpy(wb, s, size);
90 write_data(wb, size);
91 delete [] wb;
92 #endif
93 }
94
95 void writer_sink::write_growbuf(const IGrowBuf *b)
96 {
97 write_data(b->get(), b->getlen());
98 }
99
100 namespace hlp {
101 template<class T> static inline bool issigned() { return T(-1) < T(0); }
102 template<class T> static inline bool issigned(const T&t) { return issigned<T>(); }
103 }
104
105 void growbuf_writer_sink::write_data(const void *data, const size_t size)
106 {
107 // TODO: Replace all of this with a simple call when GrowBuf is changed to use size_t
108 if (sizeof(size) == sizeof(sink_type::size_type) && hlp::issigned(size) == hlp::issigned<sink_type::size_type>())
109 {
110 m_buf->add(data, truncate_cast(sink_type::size_type, size));
111 }
112 else
113 {
114 size_t left = size;
115 sink_type::size_type cbmaxadd = INT_MAX, cb;
116 for (char *p = (char *) data; left; p += cb, left -= cb)
117 {
118 cb = left >= (size_t) cbmaxadd ? cbmaxadd : (sink_type::size_type) left;
119 m_buf->add(p, cb);
120 }
121 }
122 }
123
124 void file_writer_sink::write_data(const void *data, const size_t size)
125 {
126 if (fwrite(data, 1, size, m_fp) != size)
127 {
128 throw std::runtime_error("error writing");
129 }
130 }
131
132 #ifdef NSIS_CONFIG_CRC_SUPPORT
133 #include "crc32.h"
134
135 void crc_writer_sink::write_data(const void *data, const size_t size)
136 {
137 *m_crc = CRC32(*m_crc, (const unsigned char *) data, size);
138 }
139 #endif