| 1 |
/* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */ |
| 2 |
|
| 3 |
/* libmwaw: tools |
| 4 |
* Version: MPL 2.0 / LGPLv2+ |
| 5 |
* |
| 6 |
* The contents of this file are subject to the Mozilla Public License Version |
| 7 |
* 2.0 (the "License"); you may not use this file except in compliance with |
| 8 |
* the License or as specified alternatively below. You may obtain a copy of |
| 9 |
* the License at http://www.mozilla.org/MPL/ |
| 10 |
* |
| 11 |
* Software distributed under the License is distributed on an "AS IS" basis, |
| 12 |
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
| 13 |
* for the specific language governing rights and limitations under the |
| 14 |
* License. |
| 15 |
* |
| 16 |
* Major Contributor(s): |
| 17 |
* Copyright (C) 2011, 2012 Alonso Laurent ([email protected]) |
| 18 |
* |
| 19 |
* |
| 20 |
* All Rights Reserved. |
| 21 |
* |
| 22 |
* For minor contributions see the git repository. |
| 23 |
* |
| 24 |
* Alternatively, the contents of this file may be used under the terms of |
| 25 |
* the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"), |
| 26 |
* in which case the provisions of the LGPLv2+ are applicable |
| 27 |
* instead of those above. |
| 28 |
*/ |
| 29 |
#ifdef _WIN32 |
| 30 |
# define MWAWZIP_CDECL __cdecl |
| 31 |
#else |
| 32 |
# define MWAWZIP_CDECL |
| 33 |
#endif |
| 34 |
|
| 35 |
#include <sys/stat.h> |
| 36 |
#include <sys/types.h> |
| 37 |
#include <stdio.h> |
| 38 |
#include <stdlib.h> |
| 39 |
#include <string.h> |
| 40 |
#include <unistd.h> |
| 41 |
|
| 42 |
#include <fstream> |
| 43 |
#include <iostream> |
| 44 |
#include <string> |
| 45 |
#include <vector> |
| 46 |
|
| 47 |
#include "input.h" |
| 48 |
#include "xattr.h" |
| 49 |
#include "zip.h" |
| 50 |
#include "zip_internal.h" |
| 51 |
|
| 52 |
#ifndef VERSION |
| 53 |
#define VERSION "UNKNOWN VERSION" |
| 54 |
#endif |
| 55 |
|
| 56 |
static void usage(const char *cmdname) |
| 57 |
{ |
| 58 |
std::cerr << "Usage: " << cmdname << " [-h][-x][-D] FILENAME ZIPFILE\n"; |
| 59 |
std::cerr << "\n"; |
| 60 |
std::cerr << "try to zip the content of FILENAME in ZIPFILE.\n"; |
| 61 |
std::cerr << "\n"; |
| 62 |
std::cerr << "Options:\n"; |
| 63 |
std::cerr << "\t -h: print this help,\n"; |
| 64 |
std::cerr << "\t -x: do not zip a BinHex, an OLE2 or a Zip file,\n"; |
| 65 |
std::cerr << "\t -v: output mwawZip version\n"; |
| 66 |
std::cerr << "\t -D: only zip the file containing a resource fork or finder information.\n"; |
| 67 |
} |
| 68 |
|
| 69 |
static int printVersion() |
| 70 |
{ |
| 71 |
std::cerr << "mwawZip " << VERSION << "\n"; |
| 72 |
return 0; |
| 73 |
} |
| 74 |
|
| 75 |
int MWAWZIP_CDECL main(int argc, char **argv) |
| 76 |
{ |
| 77 |
bool checkZip=false; |
| 78 |
bool verbose=false; |
| 79 |
bool doNotCompressSimpleFile=false; |
| 80 |
int ch; |
| 81 |
while ((ch = getopt(argc, argv, "hvxD")) != -1) { |
| 82 |
switch (ch) { |
| 83 |
case 'v': |
| 84 |
printVersion(); |
| 85 |
return 0; |
| 86 |
case 'x': |
| 87 |
checkZip=true; |
| 88 |
break; |
| 89 |
case 'D': |
| 90 |
doNotCompressSimpleFile=true; |
| 91 |
break; |
| 92 |
default: |
| 93 |
verbose=true; |
| 94 |
break; |
| 95 |
} |
| 96 |
} |
| 97 |
if (argc != 2+optind || verbose) { |
| 98 |
usage(argv[0]); |
| 99 |
return 1; |
| 100 |
} |
| 101 |
|
| 102 |
// check if it is a regular file |
| 103 |
struct stat status; |
| 104 |
if (stat(argv[optind], &status) || !S_ISREG(status.st_mode)) { |
| 105 |
std::cerr << argv[0] << ": the file " << argv[optind] << " is a not a regular file\n"; |
| 106 |
return 1; |
| 107 |
} |
| 108 |
if (checkZip) { |
| 109 |
// we need to open the file to look for the file signature |
| 110 |
try { |
| 111 |
std::ifstream file(argv[optind], std::ios::binary); |
| 112 |
if (file.bad()) { |
| 113 |
std::cerr << argv[0] << ": the file " << argv[optind] << " seems bad\n"; |
| 114 |
return 1; |
| 115 |
} |
| 116 |
file.seekg(0, std::ios::beg); |
| 117 |
char buff[4] = {'\0', '\0','\0','\0'}; |
| 118 |
file.read(buff,4); |
| 119 |
// look for a zip file signature |
| 120 |
if (buff[0]=='P' && buff[1]=='K') { |
| 121 |
if (((buff[2]==char(3)||buff[2]==char(5)||buff[2]==char(7)) && buff[3]==buff[2]+char(1)) || |
| 122 |
(buff[2]=='L'&&buff[3]=='I') || (buff[2]=='S'&&buff[3]=='p')) |
| 123 |
return 2; |
| 124 |
} |
| 125 |
// look for an ole file signature |
| 126 |
else if (buff[0]==char(0xd0) && buff[1]==char(0xcf) && buff[2]==char(0x11) && buff[3]==char(0xe0)) { |
| 127 |
file.read(buff,4); |
| 128 |
if (buff[0]==char(0xa1)&&buff[1]==char(0xb1) && buff[2]==char(0x1a) && buff[3]==char(0xe1)) |
| 129 |
return 2; |
| 130 |
} |
| 131 |
// look for a binhex file signature |
| 132 |
else if (buff[0]=='('&&buff[1]=='T'&&buff[2]=='h'&&buff[3]=='i') { |
| 133 |
file.read(buff,4); |
| 134 |
if (buff[0]=='s'&&buff[1]==' '&&buff[2]=='f'&&buff[3]=='i') |
| 135 |
return 2; |
| 136 |
} |
| 137 |
} |
| 138 |
catch (...) { |
| 139 |
} |
| 140 |
} |
| 141 |
std::string resultFile(argv[optind+1]); |
| 142 |
// check if the file exists |
| 143 |
if (stat(resultFile.c_str(), &status)==0) { |
| 144 |
std::cerr << argv[0] << ": the file " << resultFile << " already exists\n"; |
| 145 |
return 1; |
| 146 |
} |
| 147 |
|
| 148 |
std::string originalFile(argv[optind]); |
| 149 |
#ifdef WIN32 |
| 150 |
for (auto &c : originalFile) { |
| 151 |
if (c=='\\') c='/'; |
| 152 |
} |
| 153 |
#endif |
| 154 |
/** find folder and base file name*/ |
| 155 |
size_t sPos=originalFile.rfind('/'); |
| 156 |
std::string folder(""), file(""); |
| 157 |
if (sPos==std::string::npos) |
| 158 |
file = originalFile; |
| 159 |
else { |
| 160 |
folder=originalFile.substr(0,sPos+1); |
| 161 |
file=originalFile.substr(sPos+1); |
| 162 |
} |
| 163 |
|
| 164 |
try { |
| 165 |
//! the main data fork |
| 166 |
std::shared_ptr<libmwaw_zip::FileStream> fStream(new libmwaw_zip::FileStream(originalFile.c_str())); |
| 167 |
if (!fStream->ok()) { |
| 168 |
fprintf(stderr, "Failed to create stream for %s\n", originalFile.c_str()); |
| 169 |
return 1; |
| 170 |
} |
| 171 |
//! the attributes |
| 172 |
libmwaw_zip::XAttr xattr(originalFile.c_str()); |
| 173 |
// check first the classic attributes (which are no longer reconstructed) |
| 174 |
auto auxiStream = xattr.getClassicStream(); |
| 175 |
if (!auxiStream) |
| 176 |
auxiStream = xattr.getStream(); |
| 177 |
if (!auxiStream) { |
| 178 |
// look for a resource file |
| 179 |
std::string name=folder+"._"+file; |
| 180 |
if (stat(name.c_str(), &status) || !S_ISREG(status.st_mode)) { |
| 181 |
name=folder+"__MACOSX/._"+file; |
| 182 |
if (stat(name.c_str(), &status) || !S_ISREG(status.st_mode)) |
| 183 |
name = ""; |
| 184 |
} |
| 185 |
if (name.length()) { |
| 186 |
std::shared_ptr<libmwaw_zip::FileStream> fAuxiStream(new libmwaw_zip::FileStream(name.c_str())); |
| 187 |
if (fAuxiStream->ok()) |
| 188 |
auxiStream = fAuxiStream; |
| 189 |
} |
| 190 |
} |
| 191 |
if (!auxiStream && doNotCompressSimpleFile) |
| 192 |
return 2; |
| 193 |
libmwaw_zip::Zip zip; |
| 194 |
if (!zip.open(argv[optind+1])) |
| 195 |
return 1; |
| 196 |
zip.add(fStream, file.c_str()); |
| 197 |
if (auxiStream) { |
| 198 |
std::string name="._"+file; |
| 199 |
zip.add(auxiStream, name.c_str()); |
| 200 |
} |
| 201 |
zip.close(); |
| 202 |
} |
| 203 |
catch (...) { |
| 204 |
std::cerr << argv[0] << ": error when zipping file " << argv[optind] << "\n"; |
| 205 |
return -1; |
| 206 |
} |
| 207 |
|
| 208 |
return 0; |
| 209 |
} |
| 210 |
|
| 211 |
// vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: |