1
// Copyright (c) 2006, Rodrigo Braz Monteiro
2
// All rights reserved.
4
// Redistribution and use in source and binary forms, with or without
5
// modification, are permitted provided that the following conditions are met:
7
// * Redistributions of source code must retain the above copyright notice,
8
// this list of conditions and the following disclaimer.
9
// * Redistributions in binary form must reproduce the above copyright notice,
10
// this list of conditions and the following disclaimer in the documentation
11
// and/or other materials provided with the distribution.
12
// * Neither the name of the Aegisub Group nor the names of its contributors
13
// may be used to endorse or promote products derived from this software
14
// without specific prior written permission.
16
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26
// POSSIBILITY OF SUCH DAMAGE.
28
// -----------------------------------------------------------------------------
32
// Website: http://aegisub.cellosoft.com
33
// Contact: mailto:zeratul@cellosoft.com
41
#include <wx/wfstream.h>
42
#include <wx/filename.h>
43
#include "ass_attachment.h"
48
AssAttachment::AssAttachment(wxString _name) {
51
wxFileName fname(GetFileName());
52
wxString ext = fname.GetExt().Lower();
54
if (ext == _T("ttf")) {
55
name = fname.GetName() + _T("_0.") + ext;
61
data = boost::shared_ptr<AttachData> (new AttachData);
67
AssAttachment::~AssAttachment() {
73
AssEntry *AssAttachment::Clone() const {
75
AssAttachment *clone = new AssAttachment(filename);
88
const DataVec &AssAttachment::GetData() {
89
return data->GetData();
95
void AssAttachment::AddData(wxString _data) {
100
//////////////////////
101
// Finish adding data
102
void AssAttachment::Finish() {
107
/////////////////////////////////////
108
// Get encoded data to write on file
109
const wxString AssAttachment::GetEntryData() {
111
const DataVec &dat = data->GetData();
113
int size = dat.size();
115
unsigned char src[3];
116
unsigned char dst[4];
120
if (group == _T("[Fonts]")) entryData = _T("fontname: ");
121
else entryData = _T("filename: ");
122
entryData += filename + _T("\r\n");
127
int read = size - pos;
128
if (read > 3) read = 3;
132
if (read >= 2) src[1] = dat[pos+1];
134
if (read == 3) src[2] = dat[pos+2];
139
dst[0] = src[0] >> 2;
140
dst[1] = ((src[0] & 0x3) << 4) | ((src[1] & 0xF0) >> 4);
141
dst[2] = ((src[1] & 0xF) << 2) | ((src[2] & 0xC0) >> 6);
142
dst[3] = src[2] & 0x3F;
145
int toWrite = read+1;
148
for (int i=0;i<toWrite;i++) {
149
entryData += wxChar(dst[i]+33);
153
if (written == 80 && pos < size) {
155
entryData += _T("\r\n");
165
/////////////////////
167
void AssAttachment::Extract(wxString filename) {
169
wxFileOutputStream fp(filename);
170
if (!fp.Ok()) return;
171
fp.Write(&data->GetData()[0],data->GetData().size());
175
/////////////////////////////
176
// Read a file as attachment
177
void AssAttachment::Import(wxString filename) {
179
DataVec &datavec = data->GetData();
181
// Open file and get size
182
wxFileInputStream fp(filename);
183
if (!fp.Ok()) throw _T("Failed opening file");
184
int size = fp.SeekI(0,wxFromEnd);
185
fp.SeekI(0,wxFromStart);
188
datavec.resize(size);
189
fp.Read(&datavec[0],size);
195
wxString AssAttachment::GetFileName(bool raw) {
197
if (raw || filename.Right(4).Lower() != _T(".ttf")) return filename;
199
// Remove stuff after last underscore if it's a font
201
for (size_t i=0;i<filename.Length();i++) {
202
if (filename[i] == _T('_')) lastUnder = i;
206
wxString final = filename;
207
if (lastUnder != -1) {
208
final = filename.Left(lastUnder) + _T(".ttf");
215
/////////////////// Attachment //////////////////
218
AttachData::AttachData() {
224
AttachData::~AttachData() {
230
DataVec &AttachData::GetData() {
237
void AttachData::AddData(wxString data) {
244
void AttachData::Finish() {
245
// Source and dest buffers
246
unsigned char src[4];
247
unsigned char dst[3];
253
// Find characters left
254
int read = buffer.Length() - bufPos;
255
if (read > 4) read = 4;
258
// At least four, proceed normally
260
// Move 4 bytes from buffer to src
261
for (int i=0;i<read;i++) {
262
src[i] = (unsigned char) buffer[bufPos] - 33;
265
for (int i=read;i<4;i++) src[i] = 0;
276
// Convert the 4 bytes from source to 3 in dst
277
dst[0] = (src[0] << 2) | (src[1] >> 4);
278
dst[1] = ((src[1] & 0xF) << 4) | (src[2] >> 2);
279
dst[2] = ((src[2] & 0x3) << 6) | (src[3]);
282
size_t size = data.size();
283
data.resize(size+nbytes);
284
for (int i=0;i<nbytes;i++) data[size+i] = dst[i];