mirror of
https://github.com/hrydgard/ppsspp.git
synced 2026-10-12 14:00:27 +02:00
Initial commit
This commit is contained in:
commit
22dfdd2832
201 files changed
+33683
No files matched your search
@@ -0,0 +1,10 @@
|
||||
native
|
||||
======
|
||||
|
||||
This is my library of stuff that I use when writing C++ programs, mostly for Android. It has some basic OpenGL utility code, JSON read/write (two libraries that should be made more similar), 2D texture atlases and drawing code, ETC1 texture loading support, basic logging, etc. The associated tools to create ZIM texture files and atlases do not yet live here but I might move them here eventually.
|
||||
|
||||
If you find this useful for your own projects, drop me a line at hrydgard@gmail.com .
|
||||
|
||||
I hereby release this code under the BSD license.
|
||||
|
||||
Henrik Rydgård
|
||||
@@ -0,0 +1,11 @@
|
||||
set(SRCS
|
||||
mixer.cpp
|
||||
wav_read.cpp)
|
||||
|
||||
set(SRCS ${SRCS})
|
||||
|
||||
add_library(mixer STATIC ${SRCS})
|
||||
|
||||
if(UNIX)
|
||||
add_definitions(-fPIC)
|
||||
endif(UNIX)
|
||||
+195
@@ -0,0 +1,195 @@
|
||||
#include <string.h>
|
||||
|
||||
#include "audio/mixer.h"
|
||||
#include "audio/wav_read.h"
|
||||
#include "base/logging.h"
|
||||
#include "ext/stb_vorbis/stb_vorbis.h"
|
||||
#include "file/vfs.h"
|
||||
|
||||
// TODO:
|
||||
// * Vorbis streaming playback
|
||||
|
||||
struct ChannelEffectState {
|
||||
// Filter state
|
||||
};
|
||||
|
||||
enum CLIP_TYPE {
|
||||
CT_PCM16,
|
||||
CT_SYNTHFX,
|
||||
CT_VORBIS,
|
||||
// CT_PHOENIX?
|
||||
};
|
||||
|
||||
struct Clip {
|
||||
int type;
|
||||
|
||||
short *data;
|
||||
int length;
|
||||
int num_channels; // this is NOT stereo vs mono
|
||||
int sample_rate;
|
||||
int loop_start;
|
||||
int loop_end;
|
||||
};
|
||||
|
||||
// If current_clip == 0, the channel is free.
|
||||
|
||||
enum PlaybackState {
|
||||
PB_STOPPED = 0,
|
||||
PB_PLAYING = 1,
|
||||
};
|
||||
|
||||
|
||||
struct Channel {
|
||||
const Clip *current_clip;
|
||||
// Playback state
|
||||
PlaybackState state;
|
||||
int pos;
|
||||
PlayParams params;
|
||||
// Effect state
|
||||
ChannelEffectState effect_state;
|
||||
};
|
||||
|
||||
struct Mixer {
|
||||
Channel *channels;
|
||||
int sample_rate;
|
||||
int num_channels;
|
||||
int num_fixed_channels;
|
||||
};
|
||||
|
||||
Mixer *mixer_create(int sample_rate, int channels, int fixed_channels) {
|
||||
Mixer *mixer = new Mixer();
|
||||
memset(mixer, 0, sizeof(Mixer));
|
||||
mixer->channels = new Channel[channels];
|
||||
memset(mixer->channels, 0, sizeof(Channel) * channels);
|
||||
mixer->sample_rate = sample_rate;
|
||||
mixer->num_channels = channels;
|
||||
mixer->num_fixed_channels = fixed_channels;
|
||||
return mixer;
|
||||
}
|
||||
|
||||
void mixer_destroy(Mixer *mixer) {
|
||||
delete [] mixer->channels;
|
||||
delete mixer;
|
||||
}
|
||||
|
||||
static int get_free_channel(Mixer *mixer) {
|
||||
int chan_with_biggest_pos = -1;
|
||||
int biggest_pos = -1;
|
||||
for (int i = mixer->num_fixed_channels; i < mixer->num_channels; i++) {
|
||||
Channel *chan = &mixer->channels[i];
|
||||
if (!chan->current_clip) {
|
||||
return i;
|
||||
}
|
||||
if (chan->pos > biggest_pos) {
|
||||
biggest_pos = chan->pos;
|
||||
chan_with_biggest_pos = i;
|
||||
}
|
||||
}
|
||||
return chan_with_biggest_pos;
|
||||
}
|
||||
|
||||
Clip *clip_load(const char *filename) {
|
||||
short *data;
|
||||
int num_samples, sample_rate, num_channels;
|
||||
|
||||
if (!strcmp(filename + strlen(filename) - 4, ".ogg")) {
|
||||
// Ogg file. For now, directly decompress, no streaming support.
|
||||
uint8_t *filedata;
|
||||
size_t size;
|
||||
filedata = VFSReadFile(filename, &size);
|
||||
num_samples = stb_vorbis_decode_memory(filedata, size, &num_channels, &data);
|
||||
if (num_samples <= 0)
|
||||
return NULL;
|
||||
sample_rate = 44100;
|
||||
ILOG("read ogg %s, length %i, rate %i", filename, num_samples, sample_rate);
|
||||
} else {
|
||||
// Wav file. Easy peasy.
|
||||
data = wav_read(filename, &num_samples, &sample_rate, &num_channels);
|
||||
if (!data) {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
Clip *clip = new Clip();
|
||||
clip->type = CT_PCM16;
|
||||
clip->data = data;
|
||||
clip->length = num_samples;
|
||||
clip->num_channels = num_channels;
|
||||
clip->sample_rate = sample_rate;
|
||||
clip->loop_start = 0;
|
||||
clip->loop_end = 0;
|
||||
return clip;
|
||||
}
|
||||
|
||||
void clip_destroy(Clip *clip) {
|
||||
if (clip) {
|
||||
free(clip->data);
|
||||
delete clip;
|
||||
} else {
|
||||
ELOG("Can't destroy zero clip");
|
||||
}
|
||||
}
|
||||
|
||||
int clip_length(const Clip *clip) {
|
||||
return clip->length;
|
||||
}
|
||||
|
||||
void clip_set_loop(Clip *clip, int start, int end) {
|
||||
clip->loop_start = start;
|
||||
clip->loop_end = end;
|
||||
}
|
||||
|
||||
PlayParams *mixer_play_clip(Mixer *mixer, const Clip *clip, int channel) {
|
||||
if (channel == -1) {
|
||||
channel = get_free_channel(mixer);
|
||||
}
|
||||
|
||||
Channel *chan = &mixer->channels[channel];
|
||||
// Think about this order and make sure it's thread"safe" (not perfect but should not cause crashes).
|
||||
chan->pos = 0;
|
||||
chan->current_clip = clip;
|
||||
chan->state = PB_PLAYING;
|
||||
PlayParams *params = &chan->params;
|
||||
params->volume = 128;
|
||||
params->pan = 128;
|
||||
return params;
|
||||
}
|
||||
|
||||
void mixer_mix(Mixer *mixer, short *buffer, int num_samples) {
|
||||
// Clear the buffer.
|
||||
memset(buffer, 0, num_samples * sizeof(short) * 2);
|
||||
for (int i = 0; i < mixer->num_channels; i++) {
|
||||
Channel *chan = &mixer->channels[i];
|
||||
if (chan->state == PB_PLAYING) {
|
||||
const Clip *clip = chan->current_clip;
|
||||
if (clip->type == CT_PCM16) {
|
||||
// For now, only allow mono PCM
|
||||
CHECK(clip->num_channels == 1);
|
||||
if (true || chan->params.delta == 0) {
|
||||
// Fast playback of non pitched clips
|
||||
int cnt = num_samples;
|
||||
if (clip->length - chan->pos < cnt) {
|
||||
cnt = clip->length - chan->pos;
|
||||
}
|
||||
// TODO: Take pan into account.
|
||||
int left_volume = chan->params.volume;
|
||||
int right_volume = chan->params.volume;
|
||||
// TODO: NEONize. Can also make special loops for left_volume == right_volume etc.
|
||||
for (int s = 0; s < cnt; s++) {
|
||||
int cdata = clip->data[chan->pos];
|
||||
buffer[s * 2 ] += cdata * left_volume >> 8;
|
||||
buffer[s * 2 + 1] += cdata * right_volume >> 8;
|
||||
chan->pos++;
|
||||
}
|
||||
if (chan->pos >= clip->length) {
|
||||
chan->state = PB_STOPPED;
|
||||
chan->current_clip = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else if (clip->type == CT_VORBIS) {
|
||||
// For music
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
#include "base/basictypes.h"
|
||||
|
||||
// Simple mixer intended for sound effects for games.
|
||||
// Intended both for fire and forget sfx (auto channels) and for
|
||||
// realtime-modifiable sounds like pitched engine noises (fixed channels).
|
||||
|
||||
struct Mixer;
|
||||
struct Clip;
|
||||
struct Channel;
|
||||
|
||||
// This struct is public for easy manipulation of running channels.
|
||||
struct PlayParams {
|
||||
uint8_t volume; // 0-255
|
||||
uint8_t pan; // 0-255, 127 is dead center.
|
||||
int32_t delta;
|
||||
};
|
||||
|
||||
// Mixer
|
||||
// ==========================
|
||||
// For now, channels is required to be 2.
|
||||
Mixer *mixer_create(int sample_rate, int channels, int fixed_channels);
|
||||
void mixer_destroy(Mixer *mixer);
|
||||
|
||||
// Buffer must be r/w, for efficient mixing
|
||||
// TODO: Use local buffer instead.
|
||||
void mixer_mix(Mixer *mixer, short *buffer, int num_samples);
|
||||
|
||||
// Clip
|
||||
// ==========================
|
||||
Clip *clip_load(const char *filename);
|
||||
void clip_destroy(Clip *clip);
|
||||
|
||||
int clip_length();
|
||||
void clip_set_loop(int start, int end);
|
||||
|
||||
|
||||
// The returned PlayState pointer can be used to set the playback parameters,
|
||||
// but must not be kept around unless you are using a fixed channel.
|
||||
// Channel must be either -1 for auto assignment to a free channel, or less
|
||||
// than the number of fixed channels that the mixer was created with.
|
||||
PlayParams *mixer_play_clip(Mixer *mixer, const Clip *clip, int channel);
|
||||
@@ -0,0 +1,73 @@
|
||||
#include "base/basictypes.h"
|
||||
#include "base/logging.h"
|
||||
#include "audio/wav_read.h"
|
||||
#include "file/chunk_file.h"
|
||||
|
||||
short *wav_read(const char *filename,
|
||||
int *num_samples, int *sample_rate,
|
||||
int *num_channels) {
|
||||
ChunkFile cf(filename, true);
|
||||
if (cf.failed()) {
|
||||
WLOG("ERROR: Wave file %s could not be opened", filename);
|
||||
return 0;
|
||||
}
|
||||
|
||||
short *data = 0;
|
||||
int samplesPerSec, avgBytesPerSec,wBlockAlign,wBytesPerSample;
|
||||
if (cf.descend('RIFF')) {
|
||||
cf.readInt(); //get past 'WAVE'
|
||||
if (cf.descend('fmt ')) //enter the format chunk
|
||||
{
|
||||
int temp;
|
||||
temp = cf.readInt();
|
||||
int format = temp & 0xFFFF;
|
||||
if (format != 1) {
|
||||
cf.ascend();
|
||||
cf.ascend();
|
||||
ELOG("Error - bad format");
|
||||
return NULL;
|
||||
}
|
||||
*num_channels = temp >> 16;
|
||||
samplesPerSec = cf.readInt();
|
||||
avgBytesPerSec = cf.readInt();
|
||||
|
||||
temp = cf.readInt();
|
||||
wBlockAlign = temp & 0xFFFF;
|
||||
wBytesPerSample = temp >> 16;
|
||||
cf.ascend();
|
||||
// ILOG("got fmt data: %i", samplesPerSec);
|
||||
} else {
|
||||
ELOG("Error - no format chunk in wav");
|
||||
cf.ascend();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (cf.descend('data')) //enter the data chunk
|
||||
{
|
||||
int numBytes = cf.getCurrentChunkSize();
|
||||
int numSamples = numBytes / wBlockAlign;
|
||||
data = (short *)malloc(sizeof(short) * numSamples * *num_channels);
|
||||
*num_samples = numSamples;
|
||||
if (wBlockAlign == 2 && *num_channels == 1) {
|
||||
cf.readData((uint8*)data,numBytes);
|
||||
} else {
|
||||
ELOG("Error - bad blockalign or channels");
|
||||
free(data);
|
||||
return NULL;
|
||||
}
|
||||
// ILOG("got wave data: %i", numSamples);
|
||||
cf.ascend();
|
||||
} else {
|
||||
ELOG("Error - no data chunk in wav");
|
||||
cf.ascend();
|
||||
return NULL;
|
||||
}
|
||||
cf.ascend();
|
||||
} else {
|
||||
ELOG("Could not descend into RIFF file");
|
||||
return NULL;
|
||||
}
|
||||
*sample_rate = samplesPerSec;
|
||||
ILOG("read wav %s, length %i, rate %i", filename, *num_samples, *sample_rate);
|
||||
return data;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
#include "base/basictypes.h"
|
||||
|
||||
// Allocates a buffer that should be freed using free().
|
||||
short *wav_read(const char *filename,
|
||||
int *num_samples, int *sample_rate,
|
||||
int *num_channels);
|
||||
// TODO: Non-allocating version.
|
||||
@@ -0,0 +1,20 @@
|
||||
set(SRCS
|
||||
LAME.cpp
|
||||
LAMEApp.cpp
|
||||
LAMEString.cpp
|
||||
colorutil.cpp
|
||||
error_context.cpp)
|
||||
|
||||
set(SRCS ${SRCS})
|
||||
|
||||
add_library(base STATIC ${SRCS})
|
||||
|
||||
if(UNIX)
|
||||
add_definitions(-fPIC)
|
||||
endif(UNIX)
|
||||
|
||||
add_library(timeutil STATIC timeutil.cpp)
|
||||
|
||||
if(UNIX)
|
||||
add_definitions(-fPIC)
|
||||
endif(UNIX)
|
||||
@@ -0,0 +1,7 @@
|
||||
#include "LAME.h"
|
||||
#include "LAMEString.h"
|
||||
|
||||
String getUserName()
|
||||
{
|
||||
return String("user");
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
#ifndef __LAME_H__
|
||||
#define __LAME_H__
|
||||
|
||||
#include "base/basictypes.h"
|
||||
#include "base/logging.h"
|
||||
#include "base/color.h"
|
||||
|
||||
|
||||
#ifndef PI
|
||||
#define PI 3.141592653589793f
|
||||
#endif
|
||||
|
||||
template <class T>
|
||||
inline T MIN(T t1, T t2) {
|
||||
return t1<t2 ? t1 : t2;
|
||||
}
|
||||
template <class T>
|
||||
inline T MAX(T t1, T t2) {
|
||||
return t1>t2 ? t1 : t2;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline T ABS(T &t) {
|
||||
if (t<0) return -t; else return t;
|
||||
}
|
||||
|
||||
#endif //__LAME_H__
|
||||
@@ -0,0 +1,48 @@
|
||||
#ifndef _WIN32
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#include "LAMEApp.h"
|
||||
|
||||
LAMEApp::LAMEApp() {
|
||||
penDown=false;
|
||||
wannaQuit=false;
|
||||
active=0;
|
||||
|
||||
TCHAR tmp[256];
|
||||
#ifdef _WIN32
|
||||
hWnd=0;
|
||||
GetModuleFileName(0,tmp,255);
|
||||
#else
|
||||
char *discard = getcwd(tmp, 256);
|
||||
discard++;
|
||||
#endif
|
||||
|
||||
path=tmp;
|
||||
path=path.getPath()+String(TEXT("/"));
|
||||
}
|
||||
|
||||
LAMEApp::~LAMEApp() {
|
||||
}
|
||||
|
||||
bool LAMEApp::init(int width, int height) {
|
||||
this->width = width;
|
||||
this->height = height;
|
||||
return true;
|
||||
}
|
||||
|
||||
int LAMEApp::run() {
|
||||
return -1;
|
||||
}
|
||||
|
||||
void LAMEApp::close() {
|
||||
wannaQuit = true;
|
||||
}
|
||||
|
||||
void LAMEApp::startTimer(int ID, int interval) {
|
||||
// SetTimer(hWnd, ID, interval, 0);
|
||||
}
|
||||
|
||||
void LAMEApp::stopTimer(int ID) {
|
||||
// KillTimer(hWnd, ID);
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
#pragma once
|
||||
|
||||
//LAME: the Lean And MEan Extensions.
|
||||
|
||||
// #include "gfx/draw_buffer.h"
|
||||
#include "LAMEString.h"
|
||||
|
||||
class LAMEApp {
|
||||
protected:
|
||||
bool wannaQuit;
|
||||
bool active;
|
||||
int width;
|
||||
int height;
|
||||
#ifndef WINCE
|
||||
public:
|
||||
#endif
|
||||
bool penDown;
|
||||
protected:
|
||||
String path;
|
||||
public:
|
||||
LAMEApp();
|
||||
virtual ~LAMEApp();
|
||||
|
||||
bool init(int width, int height);
|
||||
virtual int run();
|
||||
void close();
|
||||
|
||||
void startTimer(int ID, int interval);
|
||||
void stopTimer(int ID);
|
||||
|
||||
void refresh();
|
||||
|
||||
virtual void onLoop() { }
|
||||
|
||||
virtual void onButtonDown(int b) {}
|
||||
virtual void onButtonUp(int b) {}
|
||||
|
||||
virtual void onPenDown(int x, int y) { }
|
||||
virtual void onPenMove(int x, int y, bool pressed) { }
|
||||
virtual void onPenUp(int x, int y) { }
|
||||
|
||||
virtual void onKeyDown(int key) { }
|
||||
virtual void onKeyChar(int key) { }
|
||||
virtual void onKeyUp(int key) { }
|
||||
|
||||
virtual void onLostFocus() { }
|
||||
virtual void onGotFocus() { }
|
||||
|
||||
virtual void onCreate() { }
|
||||
virtual void onDestroy() { }
|
||||
virtual void onTimer(int timerID) { }
|
||||
|
||||
virtual void draw() { }
|
||||
|
||||
int getWidth() const {return width;}
|
||||
int getHeight() const { return height;}
|
||||
String getPath() const {
|
||||
return path;
|
||||
}
|
||||
};
|
||||
|
||||
inline String getRelativePath(String temp)
|
||||
{
|
||||
#if defined(ANDROID) || defined(SDL)
|
||||
return temp;
|
||||
#else
|
||||
#error TODO: check this
|
||||
if (!theApp) return temp;
|
||||
#ifdef WINCE
|
||||
if (temp[0] != TCHAR('\\')) //if it's a relative path (assume to the program location), then let's expand it to a full path
|
||||
if (temp.find(theApp->getPath(),0)==-1)
|
||||
{
|
||||
return theApp->getPath() + temp;
|
||||
}
|
||||
#else
|
||||
if (temp[2] != TCHAR('\\')) //if it's a relative path (assume to the program location), then let's expand it to a full path
|
||||
if (temp.find(theApp->getPath(),0)==-1)
|
||||
{
|
||||
return theApp->getPath() + temp;
|
||||
}
|
||||
#endif
|
||||
return temp;
|
||||
#endif
|
||||
}
|
||||
@@ -0,0 +1,411 @@
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include "LAMEString.h"
|
||||
|
||||
#define assert(a)
|
||||
|
||||
const int MAX_LENGTH = 1024; // largest size string for input
|
||||
|
||||
#define _tcslen strlen
|
||||
#define _tcscpy strcpy
|
||||
#define _tcscmp strcmp
|
||||
#define _tcsncmp strncmp
|
||||
|
||||
|
||||
String::String()
|
||||
{
|
||||
Capacity = 1;
|
||||
CString = new TCHAR[Capacity];
|
||||
CString[0] = '\0'; // make c-style string zero length
|
||||
}
|
||||
|
||||
String::String(const TCHAR * s)
|
||||
{
|
||||
Capacity = (int)_tcslen(s) + 1;
|
||||
CString = new TCHAR[Capacity];
|
||||
_tcscpy(CString,s);
|
||||
}
|
||||
|
||||
String::String(const String & str)
|
||||
{
|
||||
Capacity = str.length() + 1;
|
||||
CString = new TCHAR[Capacity];
|
||||
_tcscpy(CString,str.CString);
|
||||
}
|
||||
|
||||
String::~String()
|
||||
{
|
||||
delete [] CString; // free memory
|
||||
}
|
||||
|
||||
const String & String::operator = (const String & rhs)
|
||||
{
|
||||
if (this != &rhs) { // check aliasing
|
||||
if (Capacity < rhs.length() + 1) { // more memory needed?
|
||||
delete[] CString; // delete old string
|
||||
Capacity = rhs.length() + 1; // add 1 for '\0'
|
||||
CString = new TCHAR[Capacity];
|
||||
}
|
||||
_tcscpy(CString,rhs.CString);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
const String & String::operator = (const TCHAR * s)
|
||||
{
|
||||
int len = 0; // length of newly constructed string
|
||||
assert(s != 0); // make sure s non-NULL
|
||||
len = (int)_tcslen(s); // # of characters in string
|
||||
|
||||
// free old string if necessary
|
||||
|
||||
if (Capacity < len + 1)
|
||||
{
|
||||
delete[] CString; // delete old string
|
||||
Capacity = len + 1; // add 1 for '\0'
|
||||
CString = new TCHAR[Capacity];
|
||||
}
|
||||
_tcscpy(CString,s);
|
||||
return *this;
|
||||
}
|
||||
|
||||
const String & String::operator = (TCHAR ch)
|
||||
{
|
||||
if (Capacity < 2)
|
||||
{
|
||||
delete [] CString;
|
||||
Capacity = 2;
|
||||
CString = new TCHAR[Capacity];
|
||||
}
|
||||
CString[0] = ch; // make string one character long
|
||||
CString[1] = '\0';
|
||||
return *this;
|
||||
}
|
||||
|
||||
int String::length() const {
|
||||
int myLength = 0;
|
||||
while (CString[myLength] != '\0')
|
||||
myLength++;
|
||||
return myLength;
|
||||
}
|
||||
|
||||
TCHAR * String::getPointer() const
|
||||
{
|
||||
return CString;
|
||||
}
|
||||
|
||||
TCHAR & String::operator [] (int k)
|
||||
{
|
||||
if (k < 0 || (int)_tcslen(CString) <= k)
|
||||
{
|
||||
//cerr << "index out of range: " << k << " string: " << CString << endl;
|
||||
assert(0 <= k && k < _tcslen(CString));
|
||||
}
|
||||
return CString[k];
|
||||
}
|
||||
|
||||
|
||||
const String & String::operator += (const String & str)
|
||||
{
|
||||
String copystring(str); // copy to avoid aliasing problems
|
||||
|
||||
int newLength = length() + str.length(); // self + added string
|
||||
int lastLocation = length(); // index of '\0'
|
||||
|
||||
// check to see if local buffer not big enough
|
||||
if (newLength >= Capacity)
|
||||
{
|
||||
Capacity = newLength + 1;
|
||||
TCHAR * newBuffer = new TCHAR[Capacity];
|
||||
_tcscpy(newBuffer,CString); // copy into new buffer
|
||||
delete [] CString; // delete old string
|
||||
CString = newBuffer;
|
||||
}
|
||||
|
||||
// now catenate str (copystring) to end of CString
|
||||
_tcscpy(CString+lastLocation,copystring.getPointer() );
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
const String & String::operator += (const TCHAR * s)
|
||||
{
|
||||
int newLength = length() + (int)_tcslen(s); // self + added string
|
||||
int lastLocation = length(); // index of '\0'
|
||||
|
||||
// check to see if local buffer not big enough
|
||||
if (newLength >= Capacity)
|
||||
{
|
||||
Capacity = newLength + 1;
|
||||
TCHAR * newBuffer = new TCHAR[Capacity];
|
||||
_tcscpy(newBuffer,CString); // copy into new buffer
|
||||
delete [] CString; // delete old string
|
||||
CString = newBuffer;
|
||||
}
|
||||
|
||||
// now catenate s to end of CString
|
||||
_tcscpy(CString+lastLocation,s);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
const String & String::operator += ( TCHAR ch )
|
||||
{
|
||||
String temp; // make string equivalent of ch
|
||||
temp = ch;
|
||||
*this += temp;
|
||||
return *this;
|
||||
}
|
||||
|
||||
String operator + (const String & lhs, const String & rhs)
|
||||
{
|
||||
String result(lhs); // copies lhs to result
|
||||
result += rhs; // catenate rhs
|
||||
return result; // returns a copy of result
|
||||
}
|
||||
|
||||
String operator + ( TCHAR ch, const String & str )
|
||||
{
|
||||
String result; // make string equivalent of ch
|
||||
result = ch;
|
||||
result += str;
|
||||
return result;
|
||||
}
|
||||
|
||||
String operator + ( const String & str, TCHAR ch )
|
||||
{
|
||||
String result(str);
|
||||
result += ch;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
String String::subString(int pos, int len) const
|
||||
{
|
||||
String result(*this); // make sure enough space allocated
|
||||
|
||||
if(pos < 0) // start at front when pos < 0
|
||||
{
|
||||
pos = 0;
|
||||
}
|
||||
|
||||
if(pos >= (int)_tcslen(CString))
|
||||
{
|
||||
result = TEXT(""); // empty string
|
||||
return result;
|
||||
}
|
||||
|
||||
int lastIndex = pos + len - 1; // last char's index (to copy)
|
||||
if(lastIndex >= (int)_tcslen(CString)) { // off end of string?
|
||||
lastIndex = (int)_tcslen(CString)-1;
|
||||
}
|
||||
|
||||
int j,k;
|
||||
for(j=0,k=pos; k <= lastIndex; j++,k++) {
|
||||
result.CString[j] = CString[k];
|
||||
}
|
||||
result.CString[j] = '\0'; // properly terminate C-string
|
||||
return result;
|
||||
}
|
||||
|
||||
int String::find(const String & str, int startpos) const
|
||||
{
|
||||
int len = str.length();
|
||||
int lastIndex = length() - len;
|
||||
int k;
|
||||
for (k=startpos; k <= lastIndex; k++) {
|
||||
if (_tcsncmp(CString + k,str.getPointer(),len) == 0) return k;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
int String::find(TCHAR ch, int startpos) const
|
||||
{
|
||||
int k;
|
||||
for(k=startpos; k < (int)_tcslen(CString); k++) {
|
||||
if (CString[k] == ch) {
|
||||
return k;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int String::findLast(TCHAR ch, int count)
|
||||
{
|
||||
for (int k=length()-1; k; k--)
|
||||
{
|
||||
if (CString[k] == ch)
|
||||
{
|
||||
if (count==0)
|
||||
return k;
|
||||
count--;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int String::parseIntoWords(String *words, int maxWords)
|
||||
{
|
||||
int numWords=0;
|
||||
String currentWord;
|
||||
for (int i=0; i<length()+1; i++)
|
||||
{
|
||||
TCHAR c = (i<length()) ? CString[i] : ' ';
|
||||
if (c==' ')
|
||||
{
|
||||
if (currentWord.length()>0)
|
||||
{
|
||||
words[numWords] = currentWord;
|
||||
currentWord=TEXT("");
|
||||
numWords++;
|
||||
if (numWords == maxWords)
|
||||
return numWords;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
currentWord += c;
|
||||
}
|
||||
}
|
||||
return numWords;
|
||||
}
|
||||
|
||||
void String::toLower()
|
||||
{
|
||||
for (int i=0; i<length(); i++)
|
||||
CString[i] = (TCHAR)tolower(CString[i]);
|
||||
}
|
||||
void String::toUpper()
|
||||
{
|
||||
for (int i=0; i<length(); i++)
|
||||
CString[i] = (TCHAR)toupper(CString[i]);
|
||||
}
|
||||
|
||||
bool operator == ( const String & lhs, const String & rhs )
|
||||
{
|
||||
return _tcscmp(lhs.getPointer(), rhs.getPointer()) == 0;
|
||||
}
|
||||
|
||||
bool operator != ( const String & lhs, const String & rhs )
|
||||
{
|
||||
return ! (lhs == rhs);
|
||||
}
|
||||
|
||||
bool operator < ( const String & lhs, const String & rhs )
|
||||
{
|
||||
return _tcscmp(lhs.getPointer(), rhs.getPointer()) < 0;
|
||||
}
|
||||
|
||||
bool operator <= ( const String & lhs, const String & rhs )
|
||||
{
|
||||
return lhs < rhs || lhs == rhs;
|
||||
}
|
||||
bool operator > ( const String & lhs, const String & rhs )
|
||||
{
|
||||
return rhs < lhs;
|
||||
}
|
||||
|
||||
bool operator >= ( const String & lhs, const String & rhs )
|
||||
{
|
||||
return rhs <= lhs;
|
||||
}
|
||||
/*
|
||||
int String::convertToInt() const
|
||||
{
|
||||
return(_ttoi(CString));
|
||||
}*/
|
||||
|
||||
|
||||
void String::reverseString()
|
||||
{
|
||||
int n = 0, i = 0;
|
||||
TCHAR *ny = new TCHAR[length()+1];
|
||||
|
||||
for (i=length()-1; i >= 0; i--, n++) {
|
||||
ny[i] = CString[n];
|
||||
}
|
||||
|
||||
ny[length()]='\0';
|
||||
|
||||
_tcscpy(CString, ny);
|
||||
|
||||
delete [] ny;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
String String::getPath()
|
||||
{
|
||||
int p=find(String(TEXT("\\")),0);
|
||||
int lastp=-1;
|
||||
while (p!=-1) {
|
||||
lastp=p;
|
||||
p=find(String(TEXT("\\")),p+1);
|
||||
}
|
||||
if (lastp!=-1) {
|
||||
return subString(0,lastp);
|
||||
} else {
|
||||
return String(TEXT(""));
|
||||
}
|
||||
}
|
||||
|
||||
String String::getFName()
|
||||
{
|
||||
int p=find(String(TEXT("\\")),0);
|
||||
int lastp=-1;
|
||||
while (p!=-1)
|
||||
{
|
||||
lastp=p;
|
||||
p=find(String(TEXT("\\")),p+1);
|
||||
}
|
||||
if (lastp!=-1)
|
||||
{
|
||||
return subString(lastp+1,100);
|
||||
}
|
||||
else
|
||||
{
|
||||
return String(TEXT(""));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void String::toUnicode(wchar_t *dest)
|
||||
{
|
||||
for (int i=0; i<length(); i++)
|
||||
dest[i]=(wchar_t)CString[i];
|
||||
}
|
||||
|
||||
|
||||
int wideLength(wchar_t *s)
|
||||
{
|
||||
int len=0;
|
||||
while (*s++)
|
||||
len++;
|
||||
return len;
|
||||
}
|
||||
|
||||
|
||||
void String::fromUnicode(wchar_t *src)
|
||||
{
|
||||
struct Local {
|
||||
static int clamp(int i) {
|
||||
return i>255?' ':i;
|
||||
}
|
||||
};
|
||||
|
||||
int newLength = wideLength(src);
|
||||
delete [] CString;
|
||||
Capacity = newLength + 1;
|
||||
CString = new TCHAR[Capacity];
|
||||
|
||||
int i;
|
||||
for (i=0; i<newLength; i++)
|
||||
{
|
||||
CString[i] = Local::clamp(src[i]);
|
||||
}
|
||||
CString[i]=0;
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "base/basictypes.h"
|
||||
|
||||
#ifdef UNICODE
|
||||
typedef wchar_t TCHAR;
|
||||
#else
|
||||
typedef char TCHAR;
|
||||
#endif
|
||||
|
||||
class String
|
||||
{
|
||||
public:
|
||||
// constructors/destructor
|
||||
String(); // construct empty string ""
|
||||
explicit String(const char * s); // construct from string literal
|
||||
String(const String & str); // copy constructor
|
||||
~String(); // destructor
|
||||
|
||||
|
||||
// accessors
|
||||
|
||||
int find(const String & str, int startpos=0) const; // index of first occurrence of str
|
||||
int find(TCHAR ch, int startpos=0) const; // index of first occurrence of ch
|
||||
int findLast(TCHAR ch,int count=0);
|
||||
String subString(int pos, int len) const; // substring of len chars
|
||||
|
||||
|
||||
int length() const; // number of chars
|
||||
|
||||
// starting at pos
|
||||
TCHAR * getPointer( ) const; // explicit conversion to char *
|
||||
const TCHAR *c_str() const {
|
||||
return getPointer();
|
||||
}
|
||||
|
||||
// assignment
|
||||
const String & operator = ( const String & str ); // assign str
|
||||
const String & operator = ( const TCHAR * s ); // assign s
|
||||
const String & operator = ( TCHAR ch ); // assign ch
|
||||
|
||||
//int operator == (String &other);
|
||||
//int operator != (String &other) {return !(*this == other);}
|
||||
// indexing
|
||||
TCHAR & operator [] ( int k ); // range-checked indexing
|
||||
|
||||
// modifiers
|
||||
const String & operator += ( const String & str ); // append str
|
||||
const String & operator += ( const TCHAR * s); // append s
|
||||
const String & operator += ( TCHAR ch ); // append char
|
||||
|
||||
int parseIntoWords(String *words, int maxWords);
|
||||
int convertToInt() const;
|
||||
/*
|
||||
static String fromInt(int i) {
|
||||
TCHAR temp[15];
|
||||
_itot_s(i,temp,15,10);
|
||||
return String(temp);
|
||||
}*/
|
||||
void reverseString();
|
||||
void toUnicode(wchar_t *dest);
|
||||
void fromUnicode(wchar_t *src);
|
||||
String getPath();
|
||||
String getFName();
|
||||
|
||||
void toUpper();
|
||||
void toLower();
|
||||
|
||||
private:
|
||||
int Capacity; // capacity of string
|
||||
TCHAR * CString; // storage for characters
|
||||
};
|
||||
|
||||
// The following free (non-member) functions operate on strings
|
||||
//
|
||||
// I/O functions
|
||||
/*
|
||||
ostream & operator << ( ostream & os, const String & str );
|
||||
istream & operator >> ( istream & is, String & str );
|
||||
istream & getline( istream & is, String & str );
|
||||
*/
|
||||
// comparison operators:
|
||||
|
||||
bool operator == ( const String & lhs, const String & rhs );
|
||||
bool operator != ( const String & lhs, const String & rhs );
|
||||
bool operator < ( const String & lhs, const String & rhs );
|
||||
bool operator <= ( const String & lhs, const String & rhs );
|
||||
bool operator > ( const String & lhs, const String & rhs );
|
||||
bool operator >= ( const String & lhs, const String & rhs );
|
||||
|
||||
// concatenation operator +
|
||||
|
||||
String operator + ( const String & lhs, const String & rhs );
|
||||
String operator + ( TCHAR ch, const String & str );
|
||||
String operator + ( const String & str, TCHAR ch );
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
#ifndef _BASE_BASICTYPES
|
||||
#define _BASE_BASICTYPES
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h> // for byte swapping
|
||||
|
||||
#ifdef _WIN32
|
||||
#pragma warning(disable:4244)
|
||||
#pragma warning(disable:4996)
|
||||
#pragma warning(disable:4305) // truncation from double to float
|
||||
#endif
|
||||
|
||||
#define DISALLOW_COPY_AND_ASSIGN(t) \
|
||||
private: \
|
||||
t(const t &other); \
|
||||
void operator =(const t &other);
|
||||
|
||||
|
||||
typedef uint8_t uint8;
|
||||
typedef uint16_t uint16;
|
||||
typedef uint32_t uint32;
|
||||
typedef uint64_t uint64;
|
||||
typedef int8_t sint8;
|
||||
typedef int16_t sint16;
|
||||
typedef int32_t sint32;
|
||||
typedef int64_t sint64;
|
||||
typedef int8_t int8;
|
||||
typedef int16_t int16;
|
||||
typedef int32_t int32;
|
||||
typedef int64_t int64;
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
#include <tchar.h>
|
||||
|
||||
#define ALIGNED16(x) __declspec(align(16)) x
|
||||
#define ALIGNED32(x) __declspec(align(32)) x
|
||||
#define ALIGNED64(x) __declspec(align(64)) x
|
||||
#define ALIGNED16_DECL(x) __declspec(align(16)) x
|
||||
#define ALIGNED64_DECL(x) __declspec(align(64)) x
|
||||
|
||||
#else
|
||||
|
||||
#define ALIGNED16(x) __attribute__((aligned(16))) x
|
||||
#define ALIGNED32(x) __attribute__((aligned(32))) x
|
||||
#define ALIGNED64(x) __attribute__((aligned(64))) x
|
||||
#define ALIGNED16_DECL(x) __attribute__((aligned(16))) x
|
||||
#define ALIGNED64_DECL(x) __attribute__((aligned(64))) x
|
||||
|
||||
#endif // _WIN32
|
||||
|
||||
#ifndef TEXT
|
||||
#define TEXT(x) x
|
||||
#endif
|
||||
|
||||
// Byteswapping
|
||||
|
||||
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
|
||||
|
||||
inline uint8 swap8(uint8 _data) {return _data;}
|
||||
|
||||
#ifdef _WIN32
|
||||
inline uint16 swap16(uint16 _data) {return _byteswap_ushort(_data);}
|
||||
inline uint32 swap32(uint32 _data) {return _byteswap_ulong (_data);}
|
||||
inline uint64 swap64(uint64 _data) {return _byteswap_uint64(_data);}
|
||||
#elif __linux__
|
||||
#include <byteswap.h>
|
||||
#undef swap16
|
||||
#undef swap32
|
||||
#undef swap64
|
||||
inline uint16 swap16(uint16 _data) {return bswap_16(_data);}
|
||||
inline uint32 swap32(uint32 _data) {return bswap_32(_data);}
|
||||
inline uint64 swap64(uint64 _data) {return bswap_64(_data);}
|
||||
#else
|
||||
// Slow generic implementation.
|
||||
inline uint16 swap16(uint16 data) {return (data >> 8) | (data << 8);}
|
||||
inline uint32 swap32(uint32 data) {return (swap16(data) << 16) | swap16(data >> 16);}
|
||||
inline uint64 swap64(uint64 data) {return ((uint64)swap32(data) << 32) | swap32(data >> 32);}
|
||||
#endif
|
||||
|
||||
inline uint16 swap16(const uint8* _pData) {return swap16(*(const uint16*)_pData);}
|
||||
inline uint32 swap32(const uint8* _pData) {return swap32(*(const uint32*)_pData);}
|
||||
inline uint64 swap64(const uint8* _pData) {return swap64(*(const uint64*)_pData);}
|
||||
|
||||
// Thread local storage
|
||||
#ifdef _WIN32
|
||||
#define __THREAD __declspec( thread )
|
||||
#else
|
||||
#define __THREAD __thread
|
||||
#endif
|
||||
|
||||
#endif // _BASE_BASICTYPES
|
||||
@@ -0,0 +1,43 @@
|
||||
#pragma once
|
||||
|
||||
// #define COLOR16
|
||||
|
||||
#ifdef COLOR16
|
||||
typedef unsigned short Color;
|
||||
#else
|
||||
typedef unsigned int Color;
|
||||
#endif
|
||||
|
||||
|
||||
//have to use a define to ensure constant folding.. with an inline I don't get that, sucks
|
||||
#ifdef COLOR16
|
||||
#error
|
||||
#define COLOR(i) (short16)(((i&0xF80000)>>8) | ((i&0xFC00)>>5) | ((i&0xF8)>>3))
|
||||
inline Color darkenColor(Color color) {
|
||||
return (color>>1)&0x7BEF;
|
||||
}
|
||||
inline Color whitenColor(Color color) {
|
||||
return ((color>>1)&0x7BEF)+0x7BEF;
|
||||
}
|
||||
//single multiplication 16-bit color alpha blending... pretty cool huh?
|
||||
inline Color colorInterpol(Color x, Color y, int n)
|
||||
{
|
||||
uint32 c1 = (x|(x<<16))&0x7E0F81F;
|
||||
uint32 c2 = (y|(y<<16))&0x7E0F81F;
|
||||
uint32 c = (c1 + ((c2 - c1)*n >> 5)) & 0x7E0F81F;
|
||||
return c | (c >> 16);
|
||||
}
|
||||
|
||||
#else
|
||||
#define COLOR(i) (((i << 16) & 0xFF0000) | (i & 0xFF00) | ((i >> 16) & 0xFF) | 0xFF000000)
|
||||
inline Color darkenColor(Color color) {
|
||||
return (color & 0xFF000000) | ((color >> 1)&0x7F7F7F);
|
||||
}
|
||||
inline Color whitenColor(Color color) {
|
||||
return ((color & 0xFF000000) | ((color >> 1)&0x7F7F7F)) + 0x7F7F7F;
|
||||
}
|
||||
inline Color colorInterpol(Color x, Color y, int n) {
|
||||
// TODO
|
||||
return x;
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,86 @@
|
||||
/* hsv2rgb.c
|
||||
* Convert Hue Saturation Value to Red Green Blue
|
||||
*
|
||||
* P.J. 08-Aug-98
|
||||
*
|
||||
* Reference:
|
||||
* D. F. Rogers
|
||||
* Procedural Elements for Computer Graphics
|
||||
* McGraw Hill 1985
|
||||
*/
|
||||
|
||||
#include "base/colorutil.h"
|
||||
|
||||
uint32_t whiteAlpha(float alpha) {
|
||||
if (alpha < 0.0f) alpha = 0.0f;
|
||||
if (alpha > 1.0f) alpha = 1.0f;
|
||||
uint32_t color = (int)(alpha*255)<<24;
|
||||
color |= 0xFFFFFF;
|
||||
return color;
|
||||
}
|
||||
|
||||
uint32_t blackAlpha(float alpha) {
|
||||
if (alpha < 0.0f) alpha = 0.0f;
|
||||
if (alpha > 1.0f) alpha = 1.0f;
|
||||
return (int)(alpha*255)<<24;
|
||||
}
|
||||
|
||||
uint32_t rgba(float r, float g, float b, float alpha) {
|
||||
uint32_t color = (int)(alpha*255)<<24;
|
||||
color |= (int)(b*255)<<16;
|
||||
color |= (int)(g*255)<<8;
|
||||
color |= (int)(r*255);
|
||||
return color;
|
||||
}
|
||||
|
||||
uint32_t rgba_clamp(float r, float g, float b, float a) {
|
||||
if (r > 1.0f) r = 1.0f;
|
||||
if (g > 1.0f) g = 1.0f;
|
||||
if (b > 1.0f) b = 1.0f;
|
||||
if (a > 1.0f) a = 1.0f;
|
||||
|
||||
if (r < 0.0f) r = 0.0f;
|
||||
if (g < 0.0f) g = 0.0f;
|
||||
if (b < 0.0f) b = 0.0f;
|
||||
if (a < 0.0f) a = 0.0f;
|
||||
|
||||
return rgba(r,g,b,a);
|
||||
}
|
||||
|
||||
uint32_t hsva(float H, float S, float V, float alpha) {
|
||||
/*
|
||||
* Purpose:
|
||||
* Convert HSV values to RGB values
|
||||
* All values are in the range [0.0 .. 1.0]
|
||||
*/
|
||||
float F, M, N, K;
|
||||
int I;
|
||||
float r, g, b;
|
||||
if ( S == 0.0 ) {
|
||||
// Achromatic case, set level of grey
|
||||
return rgba(V, V, V, alpha);
|
||||
} else {
|
||||
/*
|
||||
* Determine levels of primary colours.
|
||||
*/
|
||||
if (H >= 1.0) {
|
||||
H = 0.0;
|
||||
} else {
|
||||
H = H * 6;
|
||||
}
|
||||
I = (int) H; /* should be in the range 0..5 */
|
||||
F = H - I; /* fractional part */
|
||||
|
||||
M = V * (1 - S);
|
||||
N = V * (1 - S * F);
|
||||
K = V * (1 - S * (1 - F));
|
||||
|
||||
if (I == 0) { r = V; g = K; b = M; }
|
||||
if (I == 1) { r = N; g = V; b = M; }
|
||||
if (I == 2) { r = M; g = V; b = K; }
|
||||
if (I == 3) { r = M; g = N; b = V; }
|
||||
if (I == 4) { r = K; g = M; b = V; }
|
||||
if (I == 5) { r = V; g = M; b = N; }
|
||||
return rgba(r, g, b, alpha);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include "base/basictypes.h"
|
||||
|
||||
uint32_t whiteAlpha(float alpha);
|
||||
uint32_t blackAlpha(float alpha);
|
||||
uint32_t rgba(float r, float g, float b, float alpha);
|
||||
uint32_t rgba_clamp(float r, float g, float b, float alpha);
|
||||
uint32_t hsva(float h, float s, float v, float alpha);
|
||||
@@ -0,0 +1,39 @@
|
||||
#include "base/basictypes.h"
|
||||
#include "base/logging.h"
|
||||
#include "base/error_context.h"
|
||||
#include <vector>
|
||||
|
||||
#ifndef _WIN32
|
||||
#undef __THREAD
|
||||
#define __THREAD
|
||||
#endif
|
||||
|
||||
__THREAD std::vector<const char *> *_error_context_name;
|
||||
__THREAD std::vector<const char *> *_error_context_data;
|
||||
|
||||
_ErrorContext::_ErrorContext(const char *name, const char *data) {
|
||||
if (!_error_context_name) {
|
||||
_error_context_name = new std::vector<const char *>();
|
||||
_error_context_data = new std::vector<const char *>();
|
||||
_error_context_name->reserve(16);
|
||||
_error_context_data->reserve(16);
|
||||
}
|
||||
_error_context_name->push_back(name);
|
||||
_error_context_data->push_back(data);
|
||||
}
|
||||
|
||||
_ErrorContext::~_ErrorContext() {
|
||||
_error_context_name->pop_back();
|
||||
_error_context_data->pop_back();
|
||||
}
|
||||
|
||||
void _ErrorContext::Log(const char *message) {
|
||||
ILOG("EC: %s", message);
|
||||
for (size_t i = 0; i < _error_context_name->size(); i++) {
|
||||
if ((*_error_context_data)[i] != 0) {
|
||||
ILOG("EC: %s: %s", (*_error_context_name)[i], (*_error_context_data)[i]);
|
||||
} else {
|
||||
ILOG("EC: %s: %s", (*_error_context_name)[i], (*_error_context_data)[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
// do not inherit
|
||||
|
||||
// TODO: Have the crash handler investigate the error context.
|
||||
// Must only be constructed on the stack - DO NOT put these on the heap.
|
||||
class _ErrorContext
|
||||
{
|
||||
public:
|
||||
_ErrorContext(const char *name, const char *data = 0);
|
||||
~_ErrorContext();
|
||||
|
||||
// Logs the current context stack.
|
||||
static void Log(const char *message);
|
||||
};
|
||||
|
||||
#define ErrorContext(...) _ErrorContext __ec(__VA_ARGS__)
|
||||
#define LogErrorContext(msg) _ErrorContext::Log(msg)
|
||||
@@ -0,0 +1,59 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning (disable:4996) //strcpy may be dangerous
|
||||
#endif
|
||||
|
||||
// Logging
|
||||
#ifdef _WIN32
|
||||
|
||||
inline void Crash() { __asm { int 3 }; }
|
||||
|
||||
#else
|
||||
|
||||
#ifdef ANDROID
|
||||
|
||||
inline void Crash() {
|
||||
char *p = (char *)1337;
|
||||
*p = 1;
|
||||
}
|
||||
#else
|
||||
|
||||
inline void Crash() {
|
||||
asm("int $0x3");
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef ANDROID
|
||||
|
||||
#include <android/log.h>
|
||||
|
||||
#ifndef APP_NAME
|
||||
#define APP_NAME "Turboviking"
|
||||
#endif
|
||||
|
||||
#define ILOG(...) __android_log_print(ANDROID_LOG_INFO, APP_NAME, __VA_ARGS__);
|
||||
#define WLOG(...) __android_log_print(ANDROID_LOG_WARN, APP_NAME, __VA_ARGS__);
|
||||
#define ELOG(...) __android_log_print(ANDROID_LOG_ERROR, APP_NAME, __VA_ARGS__);
|
||||
#define FLOG(...) { __android_log_print(ANDROID_LOG_ERROR, APP_NAME, __VA_ARGS__); Crash(); }
|
||||
|
||||
#define MessageBox(a, b, c, d) __android_log_print(ANDROID_LOG_INFO, APP_NAME, "%s %s", (b), (c));
|
||||
|
||||
#else
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#define ILOG(...) {printf("I: %s:%i: ", __FILE__, __LINE__); printf("I: " __VA_ARGS__); printf("\n");}
|
||||
#define WLOG(...) {printf("W: %s:%i: ", __FILE__, __LINE__); printf("W: " __VA_ARGS__); printf("\n");}
|
||||
#define ELOG(...) {printf("E: %s:%i: ", __FILE__, __LINE__); printf("E: " __VA_ARGS__); printf("\n");}
|
||||
#define FLOG(...) {printf("F: %s:%i: ", __FILE__, __LINE__); printf("F: " __VA_ARGS__); printf("\n"); Crash();}
|
||||
|
||||
#ifndef MessageBox
|
||||
#define MessageBox(a, b, c, d) printf("MSG: %s %s\n", (b), (c));
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
#define CHECK(a) {if (!(a)) {FLOG("CHECK failed");}}
|
||||
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
template<class T>
|
||||
class scoped_ptr {
|
||||
public:
|
||||
scoped_ptr() : ptr_(0) {}
|
||||
scoped_ptr(T *p) : ptr_(p) {}
|
||||
~scoped_ptr() {
|
||||
delete ptr_;
|
||||
}
|
||||
void reset(T *p) {
|
||||
delete ptr_;
|
||||
ptr_ = p;
|
||||
}
|
||||
T *release() {
|
||||
T *p = ptr_;
|
||||
ptr_ = 0;
|
||||
return p;
|
||||
}
|
||||
T *operator->() { return ptr_; }
|
||||
const T *operator->() const { return ptr_; }
|
||||
|
||||
private:
|
||||
// don't copy that floppy
|
||||
scoped_ptr(const scoped_ptr<T> &other);
|
||||
void operator=(const scoped_ptr<T> &other);
|
||||
T *ptr_;
|
||||
};
|
||||
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
|
||||
#define STATS_ENABLE
|
||||
|
||||
#ifdef STATS_ENABLE
|
||||
|
||||
void IncrementStat(const char *name);
|
||||
|
||||
#define INCSTAT(name) IncrementStat(name);
|
||||
|
||||
#else
|
||||
|
||||
#define INCSTAT(name)
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string>
|
||||
|
||||
class ITOA {
|
||||
public:
|
||||
char buffer[16];
|
||||
const char *p(int i) {
|
||||
sprintf(buffer, "%i", i);
|
||||
return &buffer[0];
|
||||
}
|
||||
};
|
||||
|
||||
inline bool endsWith(const std::string &str, const std::string &what) {
|
||||
return str.substr(str.size() - what.size()) == what;
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
#include "base/basictypes.h"
|
||||
#include "base/timeutil.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <sys/time.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
#include <stdio.h>
|
||||
|
||||
static double curtime = 0;
|
||||
static float curtime_f = 0;
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
__int64 _frequency = 0;
|
||||
__int64 _starttime = 0;
|
||||
|
||||
double real_time_now(){
|
||||
if (_frequency == 0) {
|
||||
QueryPerformanceFrequency((LARGE_INTEGER*)&_frequency);
|
||||
QueryPerformanceCounter((LARGE_INTEGER*)&_starttime);
|
||||
curtime=0;
|
||||
}
|
||||
__int64 time;
|
||||
QueryPerformanceCounter((LARGE_INTEGER*)&time);
|
||||
return ((double) (time - _starttime) / (double) _frequency);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
double real_time_now() {
|
||||
static time_t start;
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv, NULL);
|
||||
if (start == 0) {
|
||||
start = tv.tv_sec;
|
||||
}
|
||||
tv.tv_sec -= start;
|
||||
return (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
void time_update() {
|
||||
curtime = real_time_now();
|
||||
curtime_f = (float)curtime;
|
||||
|
||||
//printf("curtime: %f %f\n", curtime, curtime_f);
|
||||
// also smooth time.
|
||||
//curtime+=float((double) (time-_starttime) / (double) _frequency);
|
||||
//curtime*=0.5f;
|
||||
//curtime+=1.0f/60.0f;
|
||||
//lastTime=curtime;
|
||||
//curtime_f = (float)curtime;
|
||||
}
|
||||
|
||||
float time_now() {
|
||||
return curtime_f;
|
||||
}
|
||||
|
||||
double time_now_d() {
|
||||
return curtime;
|
||||
}
|
||||
|
||||
int time_now_ms() {
|
||||
return int(curtime*1000.0);
|
||||
}
|
||||
|
||||
void sleep_ms(int ms) {
|
||||
#ifdef _WIN32
|
||||
#ifndef METRO
|
||||
Sleep(ms);
|
||||
#endif
|
||||
#else
|
||||
usleep(ms * 1000);
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
// http://linux.die.net/man/3/clock_gettime
|
||||
|
||||
// This time implementation caches the time for max performance (call time_now() as much as you like).
|
||||
// You need to call time_update() once per frame (or whenever you need the correct time right now).
|
||||
|
||||
void time_update();
|
||||
|
||||
// Seconds.
|
||||
float time_now();
|
||||
double time_now_d();
|
||||
|
||||
// Slower than the above cached time functions
|
||||
double real_time_now();
|
||||
|
||||
int time_now_ms();
|
||||
|
||||
void sleep_ms(int ms);
|
||||
@@ -0,0 +1,9 @@
|
||||
set(SRCS
|
||||
collision.cpp
|
||||
)
|
||||
|
||||
add_library(collision STATIC ${SRCS})
|
||||
|
||||
if(UNIX)
|
||||
add_definitions(-fPIC)
|
||||
endif(UNIX)
|
||||
@@ -0,0 +1,288 @@
|
||||
#include "base/logging.h"
|
||||
#include "math/lin/vec3.h"
|
||||
#include "math/ray_intersections.h"
|
||||
#include "collision/collision.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
// #define COLLISION_ENABLE_SCALING
|
||||
|
||||
static std::string V(const Vec3 &v) {
|
||||
char temp[100];
|
||||
sprintf(temp, "(%f %f %f)", v.x, v.y, v.z);
|
||||
return std::string(temp);
|
||||
}
|
||||
|
||||
Collidable::Collidable() {
|
||||
}
|
||||
|
||||
Collidable::~Collidable() {
|
||||
}
|
||||
|
||||
void Collision::Init(const Vec3 &source, const Vec3 &dest, const Vec3 &size) {
|
||||
this->size = size;
|
||||
scale.Set(1.0f / size.x, 1.0f / size.y, 1.0f / size.z);
|
||||
sourcePoint = source.scaledBy(scale);
|
||||
|
||||
velocity = (dest - source).scaledBy(scale);
|
||||
velocityLength = velocity.length();
|
||||
normalizedVelocity = velocity / velocityLength;
|
||||
|
||||
stuck = false;
|
||||
foundCollision = false;
|
||||
nearestDistance = velocityLength;
|
||||
}
|
||||
|
||||
void Collision::BeginTransform(const Matrix4x4 &inverse, const Matrix4x4 &transform) {
|
||||
this->transform = &transform;
|
||||
untransformed_sourcePoint = sourcePoint;
|
||||
untransformed_velocity = velocity;
|
||||
|
||||
sourcePoint = sourcePoint * inverse;
|
||||
velocity = velocity.rotatedBy(inverse);
|
||||
|
||||
normalizedVelocity = untransformed_velocity.normalized();
|
||||
}
|
||||
|
||||
void Collision::EndTransform() {
|
||||
sourcePoint = untransformed_sourcePoint;
|
||||
velocity = untransformed_velocity;
|
||||
|
||||
normalizedVelocity = untransformed_velocity.normalized();
|
||||
this->transform = 0;
|
||||
}
|
||||
|
||||
bool Collision::registerCollision(float t, const Vec3 &polyIPoint) {
|
||||
// Special handling for multiple contact points
|
||||
if (t < 0.0f) {
|
||||
return false;
|
||||
}
|
||||
Vec3 ipoint;
|
||||
if (transform) ipoint = polyIPoint * (*transform);
|
||||
else ipoint = polyIPoint;
|
||||
|
||||
if (t >= -0.0001 && t <= 0.0001 && num_contact_points < MAX_CONTACT_POINTS) {
|
||||
for (int i = 0; i < num_contact_points; i++) {
|
||||
// Check if identical - can happen on dupe edges (which we'll later remove).
|
||||
// TODO: Let's see if we can get rid of this fuzziness.
|
||||
if (ipoint.distance2To(contact_points[i]) < 0.00001)
|
||||
goto skip;
|
||||
}
|
||||
contact_points[num_contact_points++] = ipoint;
|
||||
}
|
||||
skip:
|
||||
if ((t >= 0.0) && (t <= velocityLength) && (t <= this->nearestDistance)) {
|
||||
// If we are hit we have a closest hit so far. We save the information
|
||||
this->nearestDistance = t;
|
||||
this->nearestPolygonIntersectionPoint = ipoint;
|
||||
this->foundCollision = true;
|
||||
//ILOG("Collision registered, t=%f", t);
|
||||
return true;
|
||||
} else {
|
||||
// Already had a closer collision.
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool Collision::Triangle(Vec3 p1, Vec3 p2, Vec3 p3) {
|
||||
Vec3 pNormal = (p2 - p1) % (p3 - p1);
|
||||
pNormal.normalize();
|
||||
return Triangle(p1, p2, p3, pNormal);
|
||||
}
|
||||
|
||||
bool Collision::Triangle(Vec3 p1, Vec3 p2, Vec3 p3, const Vec3 &pNormal) {
|
||||
#ifdef COLLISION_ENABLE_SCALING
|
||||
p1.scaleBy(scale);
|
||||
p2.scaleBy(scale);
|
||||
p3.scaleBy(scale);
|
||||
#endif
|
||||
|
||||
// Backface
|
||||
// Early out optimization, lose a square root
|
||||
if (dot(velocity, pNormal) >= 0.0f)
|
||||
return false;
|
||||
|
||||
// ILOG("normal. %s", V(pNormal).c_str());
|
||||
// find the plane intersection point
|
||||
float t;
|
||||
Vec3 polyIPoint, sIPoint = sourcePoint - pNormal;
|
||||
if (pointPlaneDistance(sIPoint, p1, pNormal) > 0.0f) {
|
||||
// Plane is embedded in ellipsoid.
|
||||
// Find plane intersection point by shooting a ray from the sphere intersection
|
||||
// point along the plane normal.
|
||||
t = intersectRayPlanePerp(sIPoint, pNormal, p1);
|
||||
polyIPoint = sIPoint + pNormal * t; // calculate plane intersection point
|
||||
} else {
|
||||
// shoot ray along the velocity vector
|
||||
t = intersectRayPlane(sIPoint, normalizedVelocity, p1, pNormal);
|
||||
if (t > velocityLength) {
|
||||
// Did not hit plane. No point in doing triangle tests.
|
||||
return false;
|
||||
}
|
||||
// calculate plane intersection point
|
||||
polyIPoint = sIPoint + normalizedVelocity * t;
|
||||
}
|
||||
|
||||
if (isPointInTriangle(polyIPoint, p1, p2, p3)) {
|
||||
// ILOG("Hit triangle! %s %s %s %s", V(polyIPoint).c_str(), V(p1).c_str(), V(p2).c_str(), V(p3).c_str());
|
||||
float depth2 = (polyIPoint - sourcePoint).length2();
|
||||
if (depth2 < 0.9999f) {
|
||||
// Here we do the error checking to see if we got ourself stuck last frame
|
||||
ELOG("Stuck in triangle! depth2 = %f", depth2);
|
||||
// ILOG("%s %s %s", V(p1).c_str(), V(p2).c_str(), V(p3).c_str());
|
||||
stuck = true;
|
||||
}
|
||||
return registerCollision(t, polyIPoint);
|
||||
} else {
|
||||
// ILOG("Missed this triangle - returning");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool Collision::EdgeLine(Vec3 p1, Vec3 p2) {
|
||||
// This ellipsoid scaling stuff will result in the edge not being really cylindric.
|
||||
// Should work for many purposes though.
|
||||
#ifdef COLLISION_ENABLE_SCALING
|
||||
p1.scaleBy(scale);
|
||||
p2.scaleBy(scale);
|
||||
#endif
|
||||
float t = 1000000000.0;
|
||||
Vec3 polyIPoint;
|
||||
if (intersectCylinder(sourcePoint, normalizedVelocity,
|
||||
p1, p2, &t, &polyIPoint)) {
|
||||
polyIPoint = closestPointOnLine(p1, p2, polyIPoint);
|
||||
return registerCollision(t, polyIPoint);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool Collision::Corner(Vec3 p1) {
|
||||
#ifdef COLLISION_ENABLE_SCALING
|
||||
p1.scaleBy(scale);
|
||||
#endif
|
||||
float t = intersectRayUnitSphere(sourcePoint, normalizedVelocity, p1);
|
||||
if (t != -1.0f) {
|
||||
return registerCollision(t, p1);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool Collision::Quad(const Vec3 &o, const Vec3 &dx, const Vec3 &dy) {
|
||||
bool hit = Triangle(o, o + dx, o + dx + dy);
|
||||
hit |= Triangle(o + dx + dy, o + dy, o);
|
||||
return hit;
|
||||
}
|
||||
|
||||
bool Collide(Collision *collision, Collidable *scene, Vec3 *out) {
|
||||
bool hit_anything = false;
|
||||
collision->num_contact_points = 0;
|
||||
collision->lastSafePosition = collision->sourcePoint;
|
||||
// Process max 8 slides to have a limit if we get into an impossible situation.
|
||||
int i = 0;
|
||||
for (i = 0; i < 4; i++) {
|
||||
// ILOG("Try %i! %s", i, V(collision->sourcePoint).c_str());
|
||||
scene->Collide(collision);
|
||||
if (!collision->foundCollision) {
|
||||
// Nothing left to do.
|
||||
collision->sourcePoint += collision->velocity;
|
||||
collision->velocity.setZero();
|
||||
break;
|
||||
}
|
||||
hit_anything = true;
|
||||
if (collision->nearestDistance < 0.0f) {
|
||||
ILOG("NegHit! %f %f", collision->velocityLength, collision->nearestDistance);
|
||||
} else {
|
||||
// ILOG("Hit! %f %f ", collision->velocityLength, collision->nearestDistance);
|
||||
}
|
||||
|
||||
Vec3 slidePlaneOrigin;
|
||||
Vec3 slidePlaneNormal;
|
||||
Vec3 newSourcePoint = collision->sourcePoint;
|
||||
|
||||
float adjusted_hit_distance = collision->nearestDistance - 0.0001f;
|
||||
if (adjusted_hit_distance < 0) adjusted_hit_distance = 0;
|
||||
|
||||
// Two cases - one where we have multiple contact points, one where we don't.
|
||||
if (collision->num_contact_points > 1) {
|
||||
ILOG("Resolve %i-ary collision, velocityLength = %f",
|
||||
collision->num_contact_points, collision->velocity.length());
|
||||
Vec3 avg_contact(0,0,0);
|
||||
for (int i = 0; i < collision->num_contact_points; i++) {
|
||||
ILOG("Contact point %s", V(collision->contact_points[i]).c_str());
|
||||
avg_contact += collision->contact_points[i];
|
||||
}
|
||||
avg_contact /= (float)collision->num_contact_points;
|
||||
|
||||
// The average of the contact points should be inside the sphere.
|
||||
float dist = sqrt(avg_contact.distance2To(collision->sourcePoint));
|
||||
if (dist > 1.0) {
|
||||
ILOG("Bogus contact points, for sure: %s vs %s, dist=%f", V(avg_contact).c_str(), V(collision->sourcePoint).c_str(), dist);
|
||||
}
|
||||
|
||||
Vec3 avg_contact_dir = avg_contact - collision->sourcePoint;
|
||||
avg_contact_dir.normalize();
|
||||
avg_contact = collision->sourcePoint + avg_contact_dir;
|
||||
|
||||
newSourcePoint += collision->normalizedVelocity * adjusted_hit_distance;
|
||||
slidePlaneOrigin = avg_contact;
|
||||
slidePlaneNormal = (newSourcePoint - slidePlaneOrigin).normalized();
|
||||
|
||||
// Push the ball out a bit along the normal to avoid re-collision.
|
||||
newSourcePoint += slidePlaneNormal * 0.01f;
|
||||
//collision->velocity += slidePlaneNormal * 0.01;
|
||||
|
||||
//slidePlaneNormal = avg_contact_dir;
|
||||
} else {
|
||||
// LOG(INFO) << "Adjusting newsourcepoint to end of collision";
|
||||
newSourcePoint += collision->normalizedVelocity * adjusted_hit_distance;
|
||||
// LOG(INFO) << "s: " << V(collision->sourcePoint);
|
||||
// LOG(INFO) << "nsp: " << V(newSourcePoint);
|
||||
// LOG(INFO) << "ip: " << V(collision->nearestPolygonIntersectionPoint);
|
||||
|
||||
// Now we must calculate the sliding plane
|
||||
slidePlaneOrigin = collision->nearestPolygonIntersectionPoint;
|
||||
slidePlaneNormal = (newSourcePoint - slidePlaneOrigin).normalized();
|
||||
}
|
||||
|
||||
// We now project the destination point onto the sliding plane
|
||||
Vec3 destinationPoint = collision->sourcePoint + collision->velocity;
|
||||
float l = intersectRayPlane(destinationPoint, slidePlaneNormal,
|
||||
slidePlaneOrigin, -slidePlaneNormal);
|
||||
|
||||
// We can now calculate a new destination point on the sliding plane
|
||||
Vec3 newDestinationPoint = destinationPoint + slidePlaneNormal * l;
|
||||
|
||||
// now we start over with the new position and velocity
|
||||
|
||||
collision->sourcePoint = newSourcePoint;
|
||||
// Generate the slide vector, which will become our new velocity vector
|
||||
Vec3 slide = newDestinationPoint - slidePlaneOrigin;
|
||||
slide += slidePlaneNormal * 0.0001f;
|
||||
|
||||
// LOG(INFO) << "NDP: " << V(newDestinationPoint);
|
||||
// ILOG("Slide: %s", V(slide).c_str());
|
||||
// Recompute to get ready!
|
||||
collision->velocity = slide;
|
||||
collision->velocityLength = collision->velocity.length();
|
||||
if (collision->velocityLength <= 0.0002) {
|
||||
//ILOG("Zero-length velocity vector. Break.");
|
||||
collision->velocity.setZero();
|
||||
break;
|
||||
} else {
|
||||
// ILOG("Velocity: %s", V(collision->velocity).c_str());
|
||||
}
|
||||
collision->nearestDistance = collision->velocityLength;
|
||||
collision->normalizedVelocity = collision->velocity / collision->velocityLength;
|
||||
}
|
||||
//ILOG("%i tries", i);
|
||||
|
||||
*out = collision->sourcePoint.scaledBy(collision->size);
|
||||
if (collision->stuck) {
|
||||
//ILOG("Stuck - resetting");
|
||||
// *out = collision->lastSafePosition.scaledBy(collision->size);
|
||||
}
|
||||
return hit_anything;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
// Collision detection engine. You pass in a class representing a scene,
|
||||
// and out comes a nice FPS-like "sliding physics" response.
|
||||
|
||||
// By hrydgard@gmail.com,
|
||||
|
||||
|
||||
#ifndef _COLLISION_H
|
||||
#define _COLLISION_H
|
||||
|
||||
#include "math/lin/vec3.h"
|
||||
#include "math/lin/matrix4x4.h"
|
||||
|
||||
class Collision;
|
||||
|
||||
class Collidable {
|
||||
public:
|
||||
Collidable();
|
||||
virtual ~Collidable();
|
||||
// Apply the collision functions to collision as appropriate.
|
||||
virtual void Collide(Collision *collision) = 0;
|
||||
};
|
||||
|
||||
class Collision {
|
||||
public:
|
||||
void Init(const Vec3 &source, const Vec3 &dest, const Vec3 &size);
|
||||
|
||||
// Collision functions
|
||||
// These should be called from Collidable::Collide and nowhere else
|
||||
// except the unit test.
|
||||
|
||||
// To correctly collide with meshes, just apply these in sequence. You MUST
|
||||
// collide against all external (convex) edges, otherwise you can get
|
||||
// quite stuck. There's PLENTY of optimization to do here.
|
||||
|
||||
bool Triangle(Vec3 p1, Vec3 p2, Vec3 p3);
|
||||
bool Triangle(Vec3 p1, Vec3 p2, Vec3 p3, const Vec3 &normal);
|
||||
bool EdgeLine(Vec3 p1, Vec3 p2); // Cylinder
|
||||
bool Quad(const Vec3 &origin, const Vec3 &dX, const Vec3 &dY);
|
||||
bool UnitCube(const Vec3 &origin);
|
||||
bool Corner(Vec3 p1); // Sphere
|
||||
bool Corners(Vec3 *p, int count);
|
||||
|
||||
// There's an opportunity to provide hyper optimized versions of
|
||||
// Edge for axis aligned edges. The cylinder intersection becomes trivial.
|
||||
|
||||
// These cannot be nested!
|
||||
void BeginTransform(const Matrix4x4 &inverse, const Matrix4x4 &transform);
|
||||
void EndTransform();
|
||||
|
||||
private:
|
||||
bool registerCollision(float t, const Vec3 &polyIPoint);
|
||||
|
||||
public:
|
||||
Vec3 sourcePoint;
|
||||
|
||||
float velocityLength;
|
||||
|
||||
Vec3 size;
|
||||
Vec3 scale;
|
||||
Vec3 velocity; // data about player movement
|
||||
Vec3 normalizedVelocity;
|
||||
|
||||
Vec3 lastSafePosition; // for error handling
|
||||
bool stuck;
|
||||
// data for collision response
|
||||
bool foundCollision;
|
||||
float nearestDistance; // nearest distance to hit
|
||||
Vec3 nearestPolygonIntersectionPoint; // on polygon
|
||||
|
||||
const Matrix4x4 *transform;
|
||||
|
||||
// Saved state during transformed operation
|
||||
Vec3 untransformed_sourcePoint;
|
||||
Vec3 untransformed_velocity;
|
||||
|
||||
enum {
|
||||
MAX_CONTACT_POINTS = 10
|
||||
};
|
||||
Vec3 contact_points[MAX_CONTACT_POINTS];
|
||||
int num_contact_points;
|
||||
};
|
||||
|
||||
// Sliding physics.
|
||||
bool Collide(Collision *collision, Collidable *scene, Vec3 *out);
|
||||
|
||||
// Bouncy physics
|
||||
// void CollideBouncy( ... )
|
||||
|
||||
#endif // _COLLISION_H
|
||||
@@ -0,0 +1,36 @@
|
||||
cmake_minimum_required(VERSION 2.6)
|
||||
|
||||
add_library(etcdec etcdec.cpp)
|
||||
#if UNIX
|
||||
add_definitions(-PIC)
|
||||
|
||||
add_library(etcpack etcpack.cpp)
|
||||
|
||||
add_definitions(-g)
|
||||
add_definitions(-O2)
|
||||
add_definitions(-Wall)
|
||||
add_definitions(-DSDL)
|
||||
add_definitions(-Wno-multichar)
|
||||
# add_definitions(-fno-strict-aliasing)
|
||||
add_definitions(-fstrict-aliasing)
|
||||
|
||||
set(LIBS ${LIBS})
|
||||
|
||||
add_executable(etctool etctool.cpp image.cpp)
|
||||
target_link_libraries(etctool etcdec etcpack)
|
||||
|
||||
#add_executable(zimtool zimtool.cpp)
|
||||
#target_link_libraries(zimtool png freetype z image file zip)
|
||||
|
||||
#set(SRCS
|
||||
# draw2d.cpp
|
||||
# fbo.cpp
|
||||
# glsl_program.cpp)
|
||||
#
|
||||
#set(SRCS ${SRCS})
|
||||
|
||||
#add_library(gfx_es2 STATIC ${SRCS})
|
||||
|
||||
#if(UNIX)
|
||||
# add_definitions(-fPIC)
|
||||
#endif(UNIX)
|
||||
@@ -0,0 +1,7 @@
|
||||
This folder contains code adapted from etcpack by ERICSSON ///.
|
||||
|
||||
The license comments have been left at the top of each file, but some code may have moved around.
|
||||
|
||||
See LICENSE.TXT for details. It says that use of the code is limited to applications using the
|
||||
code to compress textures for use with a Khronos-derived API. This game engine only supports
|
||||
OpenGL ES 2.0, which certainly qualifies, so there is no issue there.
|
||||
@@ -0,0 +1,418 @@
|
||||
/**
|
||||
|
||||
@~English
|
||||
@page licensing Licensing
|
||||
|
||||
@section etcdec etcdec.cxx License
|
||||
|
||||
etcdec.cxx is made available under the terms and conditions of the following
|
||||
License Agreement.
|
||||
|
||||
SOFTWARE LICENSE AGREEMENT
|
||||
|
||||
PLEASE REVIEW THE FOLLOWING TERMS AND CONDITIONS PRIOR TO USING THE
|
||||
ERICSSON TEXTURE COMPRESSION CODEC SOFTWARE (THE "SOFTWARE"). THE USE
|
||||
OF THE SOFTWARE IS SUBJECT TO THE TERMS AND CONDITIONS OF THE
|
||||
FOLLOWING LICENSE AGREEMENT (THE "LICENSE AGREEMENT"). IF YOU DO NOT
|
||||
ACCEPT SUCH TERMS AND CONDITIONS YOU MAY NOT USE THE SOFTWARE.
|
||||
|
||||
Under the terms and conditions of the License Agreement, Licensee
|
||||
hereby, receives a non-exclusive, non transferable, limited, free of
|
||||
charge, perpetual and worldwide license, to copy, use, distribute and
|
||||
modify the Software, but only for the purpose of developing,
|
||||
manufacturing, selling, using and distributing products including the
|
||||
Software, which products are used for (i) compression and/or
|
||||
decompression to create content creation tools for usage with a
|
||||
Khronos API, and/or (ii) compression and/or decompression for the
|
||||
purpose of usage with a middleware API that is built on top of a
|
||||
Khronos API, such as JCPs based on a Khronos API (in particular
|
||||
"Mobile 3D Graphics API for J2ME" and its future versions and "Java
|
||||
Bindings for OpenGL ES" and its future versions), and/or (iii)
|
||||
compression and/or decompression to implement a Khronos specification.
|
||||
|
||||
If Licensee institutes patent litigation against Ericsson or any
|
||||
licensee of the Software for using the Software for making,
|
||||
developing, manufacturing, selling, using and/or distributing products
|
||||
within the scope of the Khronos framework, Ericsson shall have the
|
||||
right to terminate this License Agreement with immediate
|
||||
effect. However, should Licensee institute patent litigation against
|
||||
any other licensee of the Software based on such licensee´s use of any
|
||||
other software distributed together with the Software then Ericsson
|
||||
shall have no right to terminate this License Agreement.
|
||||
|
||||
The License Agreement does not transfer to Licensee any ownership to
|
||||
any Ericsson or third party intellectual property rights.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS". ERICSSON MAKES NO REPRESENTATIONS OF
|
||||
ANY KIND, EXTENDS NO WARRANTIES OF ANY KIND, EITHER EXPRESS OR
|
||||
IMPLIED; INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK
|
||||
AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH THE
|
||||
LICENSEE. SHOULD THE SOFTWARE PROVE DEFECTIVE, THE LICENSEE ASSUMES
|
||||
THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. ERICSSON
|
||||
MAKES NO WARRANTY THAT THE MANUFACTURE, SALE, DISTRIBUTION, LEASE, USE
|
||||
OR IMPORTATION UNDER THE LICENSE AGREEMENT WILL BE FREE FROM
|
||||
INFRINGEMENT OF PATENTS, COPYRIGHTS OR OTHER INTELLECTUAL PROPERTY
|
||||
RIGHTS OF OTHERS, AND THE VALIDITY OF THE LICENSE AND THE LICENSE
|
||||
AGREEMENT IS SUBJECT TO LICENSEE'S SOLE RESPONSIBILITY TO MAKE SUCH
|
||||
DETERMINATION AND ACQUIRE SUCH LICENSES AS MAY BE NECESSARY WITH
|
||||
RESPECT TO PATENTS AND OTHER INTELLECTUAL PROPERTY OF THIRD PARTIES.
|
||||
|
||||
IN NO EVENT WILL ERICSSON BE LIABLE TO THE LICENSEE FOR DAMAGES,
|
||||
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES
|
||||
ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT
|
||||
NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR
|
||||
LOSSES SUSTAINED BY THE LICENSEE OR THIRD PARTIES OR A FAILURE OF THE
|
||||
SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR
|
||||
OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
|
||||
|
||||
Licensee acknowledges that "ERICSSON ///" is the corporate trademark
|
||||
of Telefonaktiebolaget LM Ericsson and that both "Ericsson" and the
|
||||
figure "///" are important features of the trade names of
|
||||
Telefonaktiebolaget LM Ericsson. Nothing contained in these terms and
|
||||
conditions shall be deemed to grant Licensee any right, title or
|
||||
interest in the word "Ericsson" or the figure "///".
|
||||
|
||||
The parties agree that this License Agreement based on these terms and
|
||||
conditions is governed by the laws of Sweden, and in the event of any
|
||||
dispute as a result of this License Agreement, the parties submit
|
||||
themselves to the exclusive jurisdiction of the Swedish Courts.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define CLAMP(ll,x,ul) (((x)<(ll)) ? (ll) : (((x)>(ul)) ? (ul) : (x)))
|
||||
#define GETBITS(source, size, startpos) (( (source) >> ((startpos)-(size)+1) ) & ((1<<(size)) -1))
|
||||
#define GETBITSHIGH(source, size, startpos) (( (source) >> (((startpos)-32)-(size)+1) ) & ((1<<(size)) -1))
|
||||
#define RED(img,width,x,y) img[3*(y*width+x)+0]
|
||||
#define GREEN(img,width,x,y) img[3*(y*width+x)+1]
|
||||
#define BLUE(img,width,x,y) img[3*(y*width+x)+2]
|
||||
|
||||
typedef unsigned char uint8;
|
||||
|
||||
int unscramble[4] = {2, 3, 1, 0};
|
||||
|
||||
static const int compressParams[16][4] = {{-8, -2, 2, 8}, {-8, -2, 2, 8}, {-17, -5, 5, 17}, {-17, -5, 5, 17}, {-29, -9, 9, 29}, {-29, -9, 9, 29}, {-42, -13, 13, 42}, {-42, -13, 13, 42}, {-60, -18, 18, 60}, {-60, -18, 18, 60}, {-80, -24, 24, 80}, {-80, -24, 24, 80}, {-106, -33, 33, 106}, {-106, -33, 33, 106}, {-183, -47, 47, 183}, {-183, -47, 47, 183}};
|
||||
|
||||
void decompressBlockDiffFlip(unsigned int block_part1, unsigned int block_part2, uint8 *img,int width,int height,int startx,int starty)
|
||||
{
|
||||
uint8 avg_color[3], enc_color1[3], enc_color2[3];
|
||||
char diff[3];
|
||||
int table;
|
||||
int index,shift;
|
||||
int r,g,b;
|
||||
int diffbit;
|
||||
int flipbit;
|
||||
unsigned int pixel_indices_MSB, pixel_indices_LSB;
|
||||
int x,y;
|
||||
|
||||
diffbit = (GETBITSHIGH(block_part1, 1, 33));
|
||||
flipbit = (GETBITSHIGH(block_part1, 1, 32));
|
||||
|
||||
if( !diffbit )
|
||||
{
|
||||
|
||||
// We have diffbit = 0.
|
||||
|
||||
// First decode left part of block.
|
||||
avg_color[0]= GETBITSHIGH(block_part1, 4, 63);
|
||||
avg_color[1]= GETBITSHIGH(block_part1, 4, 55);
|
||||
avg_color[2]= GETBITSHIGH(block_part1, 4, 47);
|
||||
|
||||
// Here, we should really multiply by 17 instead of 16. This can
|
||||
// be done by just copying the four lower bits to the upper ones
|
||||
// while keeping the lower bits.
|
||||
avg_color[0] |= (avg_color[0] <<4);
|
||||
avg_color[1] |= (avg_color[1] <<4);
|
||||
avg_color[2] |= (avg_color[2] <<4);
|
||||
|
||||
table = GETBITSHIGH(block_part1, 3, 39) << 1;
|
||||
|
||||
|
||||
|
||||
pixel_indices_MSB = GETBITS(block_part2, 16, 31);
|
||||
pixel_indices_LSB = GETBITS(block_part2, 16, 15);
|
||||
|
||||
if( (flipbit) == 0 )
|
||||
{
|
||||
// We should not flip
|
||||
shift = 0;
|
||||
for(x=startx; x<startx+2; x++)
|
||||
{
|
||||
for(y=starty; y<starty+4; y++)
|
||||
{
|
||||
index = ((pixel_indices_MSB >> shift) & 1) << 1;
|
||||
index |= ((pixel_indices_LSB >> shift) & 1);
|
||||
shift++;
|
||||
index=unscramble[index];
|
||||
|
||||
r=RED(img,width,x,y) =CLAMP(0,avg_color[0]+compressParams[table][index],255);
|
||||
g=GREEN(img,width,x,y)=CLAMP(0,avg_color[1]+compressParams[table][index],255);
|
||||
b=BLUE(img,width,x,y) =CLAMP(0,avg_color[2]+compressParams[table][index],255);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// We should flip
|
||||
shift = 0;
|
||||
for(x=startx; x<startx+4; x++)
|
||||
{
|
||||
for(y=starty; y<starty+2; y++)
|
||||
{
|
||||
index = ((pixel_indices_MSB >> shift) & 1) << 1;
|
||||
index |= ((pixel_indices_LSB >> shift) & 1);
|
||||
shift++;
|
||||
index=unscramble[index];
|
||||
|
||||
r=RED(img,width,x,y) =CLAMP(0,avg_color[0]+compressParams[table][index],255);
|
||||
g=GREEN(img,width,x,y)=CLAMP(0,avg_color[1]+compressParams[table][index],255);
|
||||
b=BLUE(img,width,x,y) =CLAMP(0,avg_color[2]+compressParams[table][index],255);
|
||||
}
|
||||
shift+=2;
|
||||
}
|
||||
}
|
||||
|
||||
// Now decode other part of block.
|
||||
avg_color[0]= GETBITSHIGH(block_part1, 4, 59);
|
||||
avg_color[1]= GETBITSHIGH(block_part1, 4, 51);
|
||||
avg_color[2]= GETBITSHIGH(block_part1, 4, 43);
|
||||
|
||||
// Here, we should really multiply by 17 instead of 16. This can
|
||||
// be done by just copying the four lower bits to the upper ones
|
||||
// while keeping the lower bits.
|
||||
avg_color[0] |= (avg_color[0] <<4);
|
||||
avg_color[1] |= (avg_color[1] <<4);
|
||||
avg_color[2] |= (avg_color[2] <<4);
|
||||
|
||||
table = GETBITSHIGH(block_part1, 3, 36) << 1;
|
||||
pixel_indices_MSB = GETBITS(block_part2, 16, 31);
|
||||
pixel_indices_LSB = GETBITS(block_part2, 16, 15);
|
||||
|
||||
if( (flipbit) == 0 )
|
||||
{
|
||||
// We should not flip
|
||||
shift=8;
|
||||
for(x=startx+2; x<startx+4; x++)
|
||||
{
|
||||
for(y=starty; y<starty+4; y++)
|
||||
{
|
||||
index = ((pixel_indices_MSB >> shift) & 1) << 1;
|
||||
index |= ((pixel_indices_LSB >> shift) & 1);
|
||||
shift++;
|
||||
index=unscramble[index];
|
||||
|
||||
r=RED(img,width,x,y) =CLAMP(0,avg_color[0]+compressParams[table][index],255);
|
||||
g=GREEN(img,width,x,y)=CLAMP(0,avg_color[1]+compressParams[table][index],255);
|
||||
b=BLUE(img,width,x,y) =CLAMP(0,avg_color[2]+compressParams[table][index],255);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// We should flip
|
||||
shift=2;
|
||||
for(x=startx; x<startx+4; x++)
|
||||
{
|
||||
for(y=starty+2; y<starty+4; y++)
|
||||
{
|
||||
index = ((pixel_indices_MSB >> shift) & 1) << 1;
|
||||
index |= ((pixel_indices_LSB >> shift) & 1);
|
||||
shift++;
|
||||
index=unscramble[index];
|
||||
|
||||
r=RED(img,width,x,y) =CLAMP(0,avg_color[0]+compressParams[table][index],255);
|
||||
g=GREEN(img,width,x,y)=CLAMP(0,avg_color[1]+compressParams[table][index],255);
|
||||
b=BLUE(img,width,x,y) =CLAMP(0,avg_color[2]+compressParams[table][index],255);
|
||||
|
||||
}
|
||||
shift += 2;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
// We have diffbit = 1.
|
||||
|
||||
// 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32
|
||||
// ---------------------------------------------------------------------------------------------------
|
||||
// | base col1 | dcol 2 | base col1 | dcol 2 | base col 1 | dcol 2 | table | table |diff|flip|
|
||||
// | R1' (5 bits) | dR2 | G1' (5 bits) | dG2 | B1' (5 bits) | dB2 | cw 1 | cw 2 |bit |bit |
|
||||
// ---------------------------------------------------------------------------------------------------
|
||||
//
|
||||
//
|
||||
// c) bit layout in bits 31 through 0 (in both cases)
|
||||
//
|
||||
// 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
|
||||
// --------------------------------------------------------------------------------------------------
|
||||
// | most significant pixel index bits | least significant pixel index bits |
|
||||
// | p| o| n| m| l| k| j| i| h| g| f| e| d| c| b| a| p| o| n| m| l| k| j| i| h| g| f| e| d| c | b | a |
|
||||
// --------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
// First decode left part of block.
|
||||
enc_color1[0]= GETBITSHIGH(block_part1, 5, 63);
|
||||
enc_color1[1]= GETBITSHIGH(block_part1, 5, 55);
|
||||
enc_color1[2]= GETBITSHIGH(block_part1, 5, 47);
|
||||
|
||||
|
||||
// Expand from 5 to 8 bits
|
||||
avg_color[0] = (enc_color1[0] <<3) | (enc_color1[0] >> 2);
|
||||
avg_color[1] = (enc_color1[1] <<3) | (enc_color1[1] >> 2);
|
||||
avg_color[2] = (enc_color1[2] <<3) | (enc_color1[2] >> 2);
|
||||
|
||||
|
||||
table = GETBITSHIGH(block_part1, 3, 39) << 1;
|
||||
|
||||
pixel_indices_MSB = GETBITS(block_part2, 16, 31);
|
||||
pixel_indices_LSB = GETBITS(block_part2, 16, 15);
|
||||
|
||||
if( (flipbit) == 0 )
|
||||
{
|
||||
// We should not flip
|
||||
shift = 0;
|
||||
for(x=startx; x<startx+2; x++)
|
||||
{
|
||||
for(y=starty; y<starty+4; y++)
|
||||
{
|
||||
index = ((pixel_indices_MSB >> shift) & 1) << 1;
|
||||
index |= ((pixel_indices_LSB >> shift) & 1);
|
||||
shift++;
|
||||
index=unscramble[index];
|
||||
|
||||
r=RED(img,width,x,y) =CLAMP(0,avg_color[0]+compressParams[table][index],255);
|
||||
g=GREEN(img,width,x,y)=CLAMP(0,avg_color[1]+compressParams[table][index],255);
|
||||
b=BLUE(img,width,x,y) =CLAMP(0,avg_color[2]+compressParams[table][index],255);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// We should flip
|
||||
shift = 0;
|
||||
for(x=startx; x<startx+4; x++)
|
||||
{
|
||||
for(y=starty; y<starty+2; y++)
|
||||
{
|
||||
index = ((pixel_indices_MSB >> shift) & 1) << 1;
|
||||
index |= ((pixel_indices_LSB >> shift) & 1);
|
||||
shift++;
|
||||
index=unscramble[index];
|
||||
|
||||
r=RED(img,width,x,y) =CLAMP(0,avg_color[0]+compressParams[table][index],255);
|
||||
g=GREEN(img,width,x,y)=CLAMP(0,avg_color[1]+compressParams[table][index],255);
|
||||
b=BLUE(img,width,x,y) =CLAMP(0,avg_color[2]+compressParams[table][index],255);
|
||||
}
|
||||
shift+=2;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Now decode right part of block.
|
||||
|
||||
|
||||
diff[0]= GETBITSHIGH(block_part1, 3, 58);
|
||||
diff[1]= GETBITSHIGH(block_part1, 3, 50);
|
||||
diff[2]= GETBITSHIGH(block_part1, 3, 42);
|
||||
|
||||
enc_color2[0]= enc_color1[0] + diff[0];
|
||||
enc_color2[1]= enc_color1[1] + diff[1];
|
||||
enc_color2[2]= enc_color1[2] + diff[2];
|
||||
|
||||
// Extend sign bit to entire byte.
|
||||
diff[0] = (diff[0] << 5);
|
||||
diff[1] = (diff[1] << 5);
|
||||
diff[2] = (diff[2] << 5);
|
||||
diff[0] = diff[0] >> 5;
|
||||
diff[1] = diff[1] >> 5;
|
||||
diff[2] = diff[2] >> 5;
|
||||
|
||||
// Calculale second color
|
||||
enc_color2[0]= enc_color1[0] + diff[0];
|
||||
enc_color2[1]= enc_color1[1] + diff[1];
|
||||
enc_color2[2]= enc_color1[2] + diff[2];
|
||||
|
||||
// Expand from 5 to 8 bits
|
||||
avg_color[0] = (enc_color2[0] <<3) | (enc_color2[0] >> 2);
|
||||
avg_color[1] = (enc_color2[1] <<3) | (enc_color2[1] >> 2);
|
||||
avg_color[2] = (enc_color2[2] <<3) | (enc_color2[2] >> 2);
|
||||
|
||||
|
||||
table = GETBITSHIGH(block_part1, 3, 36) << 1;
|
||||
pixel_indices_MSB = GETBITS(block_part2, 16, 31);
|
||||
pixel_indices_LSB = GETBITS(block_part2, 16, 15);
|
||||
|
||||
if( (flipbit) == 0 )
|
||||
{
|
||||
// We should not flip
|
||||
shift=8;
|
||||
for(x=startx+2; x<startx+4; x++)
|
||||
{
|
||||
for(y=starty; y<starty+4; y++)
|
||||
{
|
||||
index = ((pixel_indices_MSB >> shift) & 1) << 1;
|
||||
index |= ((pixel_indices_LSB >> shift) & 1);
|
||||
shift++;
|
||||
index=unscramble[index];
|
||||
|
||||
r=RED(img,width,x,y) =CLAMP(0,avg_color[0]+compressParams[table][index],255);
|
||||
g=GREEN(img,width,x,y)=CLAMP(0,avg_color[1]+compressParams[table][index],255);
|
||||
b=BLUE(img,width,x,y) =CLAMP(0,avg_color[2]+compressParams[table][index],255);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// We should flip
|
||||
shift=2;
|
||||
for(x=startx; x<startx+4; x++)
|
||||
{
|
||||
for(y=starty+2; y<starty+4; y++)
|
||||
{
|
||||
index = ((pixel_indices_MSB >> shift) & 1) << 1;
|
||||
index |= ((pixel_indices_LSB >> shift) & 1);
|
||||
shift++;
|
||||
index=unscramble[index];
|
||||
|
||||
r=RED(img,width,x,y) =CLAMP(0,avg_color[0]+compressParams[table][index],255);
|
||||
g=GREEN(img,width,x,y)=CLAMP(0,avg_color[1]+compressParams[table][index],255);
|
||||
b=BLUE(img,width,x,y) =CLAMP(0,avg_color[2]+compressParams[table][index],255);
|
||||
}
|
||||
shift += 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int bswap(unsigned int x) {
|
||||
return ((x & 0xFF000000) >> 24) |
|
||||
((x & 0x00FF0000) >> 8) |
|
||||
((x & 0x0000FF00) << 8) |
|
||||
((x & 0x000000FF) << 24);
|
||||
}
|
||||
|
||||
void DecompressBlock(const uint8 *compressed, uint8 *out, int out_width, int alpha=255) {
|
||||
uint8 rgb[4*4*3];
|
||||
unsigned int block_part1, block_part2;
|
||||
memcpy(&block_part1, compressed, 4);
|
||||
memcpy(&block_part2, compressed + 4, 4);
|
||||
block_part1 = bswap(block_part1);
|
||||
block_part2 = bswap(block_part2);
|
||||
decompressBlockDiffFlip(block_part1, block_part2, rgb, 4, 4, 0, 0);
|
||||
for (int y = 0; y < 4; y++) {
|
||||
for (int x = 0; x < 4; x++) {
|
||||
out[(y * out_width + x) * 4 + 0] = rgb[(4 * y + x) * 3 + 0];
|
||||
out[(y * out_width + x) * 4 + 1] = rgb[(4 * y + x) * 3 + 1];
|
||||
out[(y * out_width + x) * 4 + 2] = rgb[(4 * y + x) * 3 + 2];
|
||||
out[(y * out_width + x) * 4 + 3] = alpha;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
typedef unsigned char uint8;
|
||||
|
||||
/* In etcdec.cxx */
|
||||
void decompressBlockDiffFlip(unsigned int block_part1, unsigned int block_part2, uint8 *img,int width,int height,int startx,int starty);
|
||||
|
||||
// Writes RGBA output instead of RGB.
|
||||
void DecompressBlock(const uint8 *compressed, uint8 *out, int out_width, int alpha=255);
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,26 @@
|
||||
#pragma once
|
||||
|
||||
#define PERCEPTUAL_WEIGHT_R_SQUARED 0.299
|
||||
#define PERCEPTUAL_WEIGHT_G_SQUARED 0.587
|
||||
#define PERCEPTUAL_WEIGHT_B_SQUARED 0.114
|
||||
|
||||
typedef unsigned char uint8;
|
||||
|
||||
// These functions take RGB888.
|
||||
void compressBlockDiffFlipSlow(uint8 *img,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2);
|
||||
|
||||
void compressBlockDiffFlipMedium(uint8 *img,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2);
|
||||
|
||||
void compressBlockDiffFlipFast(uint8 *img, uint8 *imgdec,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2);
|
||||
|
||||
void compressBlockDiffFlipSlowPerceptual(uint8 *img,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2);
|
||||
|
||||
void compressBlockDiffFlipMediumPerceptual(uint8 *img,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2);
|
||||
|
||||
void compressBlockDiffFlipFastPerceptual(uint8 *img, uint8 *imgdec,int width,int height,int startx,int starty, unsigned int &compressed1, unsigned int &compressed2);
|
||||
|
||||
|
||||
// This one takes RGBA8888 and converts to RGB first.
|
||||
// Writes 64 bits of compressed data to output[0..7].
|
||||
// Quality is 0 to 5. The odd numbers use perceptual metrics.
|
||||
void CompressBlock(const uint8 *in, int in_width, uint8 *output, int quality);
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,212 @@
|
||||
// image.cxx
|
||||
//
|
||||
// NO WARRANTY
|
||||
//
|
||||
// BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, ERICSSON MAKES NO
|
||||
// REPRESENTATIONS OF ANY KIND, EXTENDS NO WARRANTIES OF ANY KIND; EITHER
|
||||
// EXPRESS OR IMPLIED; INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
// PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
|
||||
// PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME
|
||||
// THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. ERICSSON
|
||||
// MAKES NO WARRANTY THAT THE MANUFACTURE, SALE, LEASE, USE OR
|
||||
// IMPORTATION WILL BE FREE FROM INFRINGEMENT OF PATENTS, COPYRIGHTS OR
|
||||
// OTHER INTELLECTUAL PROPERTY RIGHTS OF OTHERS, AND IT SHALL BE THE SOLE
|
||||
// RESPONSIBILITY OF THE LICENSEE TO MAKE SUCH DETERMINATION AS IS
|
||||
// NECESSARY WITH RESPECT TO THE ACQUISITION OF LICENSES UNDER PATENTS
|
||||
// AND OTHER INTELLECTUAL PROPERTY OF THIRD PARTIES;
|
||||
//
|
||||
// IN NO EVENT WILL ERICSSON, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
|
||||
// GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF
|
||||
// THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO
|
||||
// LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
|
||||
// YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY
|
||||
// OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED
|
||||
// OF THE POSSIBILITY OF SUCH DAMAGES.
|
||||
//
|
||||
// (C) Ericsson AB 2005. All Rights Reserved.
|
||||
//
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "image.h"
|
||||
|
||||
// Removes comments in a .ppm file
|
||||
// (i.e., lines starting with #)
|
||||
//
|
||||
// Written by Jacob Strom
|
||||
//
|
||||
void removeComments(FILE *f1)
|
||||
{
|
||||
int c;
|
||||
|
||||
while((c = getc(f1)) == '#')
|
||||
{
|
||||
char line[1024];
|
||||
fgets(line, 1024, f1);
|
||||
}
|
||||
ungetc(c, f1);
|
||||
}
|
||||
|
||||
|
||||
// Removes white spaces in a .ppm file
|
||||
//
|
||||
// Written by Jacob Strom
|
||||
//
|
||||
void removeSpaces(FILE *f1)
|
||||
{
|
||||
int c;
|
||||
|
||||
c = getc(f1);
|
||||
while(c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r')
|
||||
{
|
||||
c = getc(f1);
|
||||
}
|
||||
ungetc(c, f1);
|
||||
}
|
||||
|
||||
// fReadPPM
|
||||
//
|
||||
// Written by Jacob Strom
|
||||
//
|
||||
// reads a ppm file with P6 header (meaning binary, as opposed to P5, which is ascII)
|
||||
// and returns the image in pixels.
|
||||
//
|
||||
// The header must look like this:
|
||||
//
|
||||
// P6
|
||||
// # Comments (not necessary)
|
||||
// width height
|
||||
// 255
|
||||
//
|
||||
// after that follows RGBRGBRGB...
|
||||
//
|
||||
bool fReadPPM(const char *filename, int &width, int &height, unsigned char *&pixels, bool reverse_y)
|
||||
{
|
||||
FILE *f1;
|
||||
int mustbe255;
|
||||
f1 = fopen(filename, "rb");
|
||||
|
||||
if(f1)
|
||||
{
|
||||
char line[255];
|
||||
|
||||
removeSpaces(f1);
|
||||
removeComments(f1);
|
||||
removeSpaces(f1);
|
||||
|
||||
fscanf(f1, "%s", line);
|
||||
|
||||
if(strcmp(line, "P6")!=0)
|
||||
{
|
||||
printf("Error: %s is not binary\n");
|
||||
printf("(Binary .ppm files start with P6).\n");
|
||||
fclose(f1);
|
||||
return false;
|
||||
}
|
||||
removeSpaces(f1);
|
||||
removeComments(f1);
|
||||
removeSpaces(f1);
|
||||
|
||||
fscanf(f1, "%d %d", &width, &height);
|
||||
if( width<=0 || height <=0)
|
||||
{
|
||||
printf("Error: width or height negative. File: %s\n",filename);
|
||||
fclose(f1);
|
||||
return false;
|
||||
}
|
||||
|
||||
removeSpaces(f1);
|
||||
removeComments(f1);
|
||||
removeSpaces(f1);
|
||||
|
||||
fscanf(f1, "%d", &mustbe255);
|
||||
if( mustbe255!= 255 )
|
||||
{
|
||||
printf("Error: Color resolution must be 255. File: %s\n",filename);
|
||||
fclose(f1);
|
||||
return false;
|
||||
}
|
||||
|
||||
// We need to remove the newline.
|
||||
char c = 0;
|
||||
while(c != '\n')
|
||||
fscanf(f1, "%c", &c);
|
||||
|
||||
|
||||
pixels = (unsigned char*) malloc(3*width*height);
|
||||
|
||||
if(!pixels)
|
||||
{
|
||||
printf("Error: Could not allocate memory for image. File: %s\n", filename);
|
||||
fclose(f1);
|
||||
return false;
|
||||
}
|
||||
|
||||
if(reverse_y)
|
||||
{
|
||||
for(int yy = 0; yy<height; yy++)
|
||||
{
|
||||
if(fread(&pixels[(height-yy-1)*width*3], 3*width, 1, f1) != 1)
|
||||
{
|
||||
printf("Error: Could not read all pixels. File: %s\n", filename);
|
||||
free(pixels);
|
||||
fclose(f1);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if(fread(pixels, 3*width*height, 1, f1) != 1)
|
||||
{
|
||||
printf("Error: Could not read all pixels. File: %s\n", filename);
|
||||
free(pixels);
|
||||
fclose(f1);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// If we have reached this point, we have successfully loaded the image.
|
||||
|
||||
fclose(f1);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Error: Coult not open file %s\n", filename);
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Write PPM --- Written by Jacob Strom
|
||||
bool fWritePPM(const char *filename, int width, int height, unsigned char *pixels, bool reverse_y)
|
||||
{
|
||||
FILE *fsave;
|
||||
fsave = fopen(filename, "wb");
|
||||
|
||||
|
||||
if(fsave)
|
||||
{
|
||||
int q;
|
||||
fprintf(fsave, "P6\n%d %d\n255\n", width, height);
|
||||
for(q = 0; q< height; q++)
|
||||
{
|
||||
unsigned char *adr;
|
||||
if(reverse_y)
|
||||
adr = pixels+3*width*(height-1-q);
|
||||
else
|
||||
adr = pixels+3*width*q;
|
||||
fwrite(adr, 3*width, 1, fsave);
|
||||
}
|
||||
fclose(fsave);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Error: Could not open the file %s.\n",filename);
|
||||
return(false);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
// NO WARRANTY
|
||||
//
|
||||
// BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, ERICSSON MAKES NO
|
||||
// REPRESENTATIONS OF ANY KIND, EXTENDS NO WARRANTIES OF ANY KIND; EITHER
|
||||
// EXPRESS OR IMPLIED; INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
// PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
|
||||
// PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME
|
||||
// THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. ERICSSON
|
||||
// MAKES NO WARRANTY THAT THE MANUFACTURE, SALE, LEASE, USE OR
|
||||
// IMPORTATION WILL BE FREE FROM INFRINGEMENT OF PATENTS, COPYRIGHTS OR
|
||||
// OTHER INTELLECTUAL PROPERTY RIGHTS OF OTHERS, AND IT SHALL BE THE SOLE
|
||||
// RESPONSIBILITY OF THE LICENSEE TO MAKE SUCH DETERMINATION AS IS
|
||||
// NECESSARY WITH RESPECT TO THE ACQUISITION OF LICENSES UNDER PATENTS
|
||||
// AND OTHER INTELLECTUAL PROPERTY OF THIRD PARTIES;
|
||||
//
|
||||
// IN NO EVENT WILL ERICSSON, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
|
||||
// GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF
|
||||
// THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO
|
||||
// LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
|
||||
// YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY
|
||||
// OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED
|
||||
// OF THE POSSIBILITY OF SUCH DAMAGES.
|
||||
//
|
||||
// (C) Ericsson AB 2005. All Rights Reserved.
|
||||
//
|
||||
#ifndef IMAGE_H
|
||||
#define IMAGE_H
|
||||
|
||||
bool fReadPPM(const char *filename, int &width, int &height, unsigned char *&pixels,bool reverse_y);
|
||||
bool fWritePPM(const char *filename, int width, int height, unsigned char *pixels,bool reverse_y);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,69 @@
|
||||
SOFTWARE LICENSE AGREEMENT
|
||||
|
||||
PLEASE REVIEW THE FOLLOWING TERMS AND CONDITIONS PRIOR TO USING THE
|
||||
ERICSSON TEXTURE COMPRESSION CODEC SOFTWARE (THE "SOFTWARE"). THE USE
|
||||
OF THE SOFTWARE IS SUBJECT TO THE TERMS AND CONDITIONS OF THE
|
||||
FOLLOWING LICENSE AGREEMENT (THE "LICENSE AGREEMENT"). IF YOU DO NOT
|
||||
ACCEPT SUCH TERMS AND CONDITIONS YOU MAY NOT USE THE SOFTWARE.
|
||||
|
||||
Under the terms and conditions of the License Agreement, Licensee
|
||||
hereby, receives a non-exclusive, non transferable, limited, free of
|
||||
charge, perpetual and worldwide license, to copy, use, distribute and
|
||||
modify the Software, but only for the purpose of developing,
|
||||
manufacturing, selling, using and distributing products including the
|
||||
Software, which products are used for (i) compression and/or
|
||||
decompression to create content creation tools for usage with a
|
||||
Khronos API, and/or (ii) compression and/or decompression for the
|
||||
purpose of usage with a middleware API that is built on top of a
|
||||
Khronos API, such as JCPs based on a Khronos API (in particular
|
||||
"Mobile 3D Graphics API for J2ME" and its future versions and "Java
|
||||
Bindings for OpenGL ES" and its future versions), and/or (iii)
|
||||
compression and/or decompression to implement a Khronos API.
|
||||
|
||||
If Licensee institutes patent litigation against Ericsson or any
|
||||
licensee of the Software for using the Software for making,
|
||||
developing, manufacturing, selling, using and/or distributing products
|
||||
within the scope of the Khronos framework, Ericsson shall have the
|
||||
right to terminate this License Agreement with immediate
|
||||
effect. However, should Licensee institute patent litigation against
|
||||
any other licensee of the Software based on such licensee´s use of any
|
||||
other software distributed together with the Software then Ericsson
|
||||
shall have no right to terminate this License Agreement.
|
||||
|
||||
The License Agreement does not transfer to Licensee any ownership to
|
||||
any Ericsson or third party intellectual property rights.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS". ERICSSON MAKES NO REPRESENTATIONS OF
|
||||
ANY KIND, EXTENDS NO WARRANTIES OF ANY KIND, EITHER EXPRESS OR
|
||||
IMPLIED; INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK
|
||||
AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH THE
|
||||
LICENSEE. SHOULD THE SOFTWARE PROVE DEFECTIVE, THE LICENSEE ASSUMES
|
||||
THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. ERICSSON
|
||||
MAKES NO WARRANTY THAT THE MANUFACTURE, SALE, DISTRIBUTION, LEASE, USE
|
||||
OR IMPORTATION UNDER THE LICENSE AGREEMENT WILL BE FREE FROM
|
||||
INFRINGEMENT OF PATENTS, COPYRIGHTS OR OTHER INTELLECTUAL PROPERTY
|
||||
RIGHTS OF OTHERS, AND THE VALIDITY OF THE LICENSE AND THE LICENSE
|
||||
AGREEMENT IS SUBJECT TO LICENSEE'S SOLE RESPONSIBILITY TO MAKE SUCH
|
||||
DETERMINATION AND ACQUIRE SUCH LICENSES AS MAY BE NECESSARY WITH
|
||||
RESPECT TO PATENTS AND OTHER INTELLECTUAL PROPERTY OF THIRD PARTIES.
|
||||
|
||||
IN NO EVENT WILL ERICSSON BE LIABLE TO THE LICENSEE FOR DAMAGES,
|
||||
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES
|
||||
ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT
|
||||
NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR
|
||||
LOSSES SUSTAINED BY THE LICENSEE OR THIRD PARTIES OR A FAILURE OF THE
|
||||
SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR
|
||||
OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
|
||||
|
||||
Licensee acknowledges that "ERICSSON ///" is the corporate trademark
|
||||
of Telefonaktiebolaget LM Ericsson and that both "Ericsson" and the
|
||||
figure "///" are important features of the trade names of
|
||||
Telefonaktiebolaget LM Ericsson. Nothing contained in these terms and
|
||||
conditions shall be deemed to grant Licensee any right, title or
|
||||
interest in the word "Ericsson" or the figure "///".
|
||||
|
||||
The parties agree that this License Agreement based on these terms and
|
||||
conditions is governed by the laws of Sweden, and in the event of any
|
||||
dispute as a result of this License Agreement, the parties submit
|
||||
themselves to the exclusive jurisdiction of the Swedish Courts.
|
||||
@@ -0,0 +1,49 @@
|
||||
ETCPACK v1.06
|
||||
--------------------------------------------------------------------------
|
||||
Changes in v1.06 against v1.05:
|
||||
|
||||
1. Updated this file so it has the correct version number.
|
||||
|
||||
--------------------------------------------------------------------------
|
||||
Changes in v1.05 against v1.04:
|
||||
|
||||
1. Updated license agreement to include item (iii) inside
|
||||
files. Removed old license text.
|
||||
|
||||
--------------------------------------------------------------------------
|
||||
Changes in v1.04 against v1.03:
|
||||
|
||||
1. Updated license agreement to include item (iii).
|
||||
|
||||
--------------------------------------------------------------------------
|
||||
Changes in v1.03 against v1.02:
|
||||
|
||||
1. Partinioned code in unpacking (etcdec.cxx) and the rest.
|
||||
2. Also made changes so that PSNR values are calculated using double
|
||||
internally.
|
||||
|
||||
--------------------------------------------------------------------------
|
||||
Changes in v1.02 against v1.01:
|
||||
|
||||
Added possibility to flip images vertically. Default is to have the first
|
||||
pixel (first three bytes in the .ppm file) ending up in s=0, t=0.
|
||||
|
||||
--------------------------------------------------------------------------
|
||||
Changes in v1.01 against v1.00:
|
||||
|
||||
Four bugs fixed:
|
||||
|
||||
1. In method uncompressFile, after memory is allocated the newimg
|
||||
pointer, the code checked whether the img pointer, not the newimg
|
||||
pointer is NULL. This has been fixed.
|
||||
|
||||
2. In method compressFile the array srcimg is never
|
||||
deleted/freed. This has been fixed.
|
||||
|
||||
3. When compressing smaller images than 4*4 (Mipmaps) with
|
||||
fast/nonperceptual the application sometimes crashes. This has been
|
||||
fixed.
|
||||
|
||||
4. Memory leak --- missing free(imgdec) in the end of
|
||||
compressImageFile() inserted, and thus the bug has been fixed.
|
||||
--------------------------------------------------------------------------
|
||||
@@ -0,0 +1,77 @@
|
||||
# Copyright (C) 2009 The Android Open Source Project
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
LOCAL_PATH := $(call my-dir)
|
||||
|
||||
include $(CLEAR_VARS)
|
||||
|
||||
LOCAL_MODULE := libzip
|
||||
LOCAL_SRC_FILES :=\
|
||||
zip_add.c \
|
||||
zip_add_dir.c \
|
||||
zip_close.c \
|
||||
zip_delete.c \
|
||||
zip_dirent.c \
|
||||
zip_entry_free.c \
|
||||
zip_entry_new.c \
|
||||
zip_err_str.c \
|
||||
zip_error.c \
|
||||
zip_error_clear.c \
|
||||
zip_error_get.c \
|
||||
zip_error_get_sys_type.c \
|
||||
zip_error_strerror.c \
|
||||
zip_error_to_str.c \
|
||||
zip_fclose.c \
|
||||
zip_file_error_clear.c \
|
||||
zip_file_error_get.c \
|
||||
zip_file_get_offset.c \
|
||||
zip_file_strerror.c \
|
||||
zip_filerange_crc.c \
|
||||
zip_fopen.c \
|
||||
zip_fopen_index.c \
|
||||
zip_fread.c \
|
||||
zip_free.c \
|
||||
zip_get_archive_comment.c \
|
||||
zip_get_archive_flag.c \
|
||||
zip_get_file_comment.c \
|
||||
zip_get_num_files.c \
|
||||
zip_get_name.c \
|
||||
zip_memdup.c \
|
||||
zip_name_locate.c \
|
||||
zip_new.c \
|
||||
zip_open.c \
|
||||
zip_rename.c \
|
||||
zip_replace.c \
|
||||
zip_set_archive_comment.c \
|
||||
zip_set_archive_flag.c \
|
||||
zip_set_file_comment.c \
|
||||
zip_source_buffer.c \
|
||||
zip_source_file.c \
|
||||
zip_source_filep.c \
|
||||
zip_source_free.c \
|
||||
zip_source_function.c \
|
||||
zip_source_zip.c \
|
||||
zip_set_name.c \
|
||||
zip_stat.c \
|
||||
zip_stat_index.c \
|
||||
zip_stat_init.c \
|
||||
zip_strerror.c \
|
||||
zip_unchange.c \
|
||||
zip_unchange_all.c \
|
||||
zip_unchange_archive.c \
|
||||
zip_unchange_data.c
|
||||
|
||||
LOCAL_LDLIBS := -lz
|
||||
|
||||
include $(BUILD_STATIC_LIBRARY)
|
||||
@@ -0,0 +1,64 @@
|
||||
set(SRCS
|
||||
mkstemp.c
|
||||
zip_add.c
|
||||
zip_add_dir.c
|
||||
zip_close.c
|
||||
zip_delete.c
|
||||
zip_dirent.c
|
||||
zip_entry_free.c
|
||||
zip_entry_new.c
|
||||
zip_err_str.c
|
||||
zip_error.c
|
||||
zip_error_clear.c
|
||||
zip_error_get.c
|
||||
zip_error_get_sys_type.c
|
||||
zip_error_strerror.c
|
||||
zip_error_to_str.c
|
||||
zip_fclose.c
|
||||
zip_file_error_clear.c
|
||||
zip_file_error_get.c
|
||||
zip_file_get_offset.c
|
||||
zip_file_strerror.c
|
||||
zip_filerange_crc.c
|
||||
zip_fopen.c
|
||||
zip_fopen_index.c
|
||||
zip_fread.c
|
||||
zip_free.c
|
||||
zip_get_archive_comment.c
|
||||
zip_get_archive_flag.c
|
||||
zip_get_file_comment.c
|
||||
zip_get_name.c
|
||||
zip_get_num_files.c
|
||||
zip_memdup.c
|
||||
zip_name_locate.c
|
||||
zip_new.c
|
||||
zip_open.c
|
||||
zip_rename.c
|
||||
zip_replace.c
|
||||
zip_set_archive_comment.c
|
||||
zip_set_archive_flag.c
|
||||
zip_set_file_comment.c
|
||||
zip_set_name.c
|
||||
zip_source_buffer.c
|
||||
zip_source_file.c
|
||||
zip_source_filep.c
|
||||
zip_source_free.c
|
||||
zip_source_function.c
|
||||
zip_source_zip.c
|
||||
zip_stat.c
|
||||
zip_stat_index.c
|
||||
zip_stat_init.c
|
||||
zip_strerror.c
|
||||
zip_unchange.c
|
||||
zip_unchange_all.c
|
||||
zip_unchange_archive.c
|
||||
zip_unchange_data.c)
|
||||
|
||||
set(SRCS ${SRCS})
|
||||
|
||||
add_library(zip STATIC ${SRCS})
|
||||
|
||||
if(UNIX)
|
||||
add_definitions(-fPIC)
|
||||
endif(UNIX)
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
/* Config header for Android */
|
||||
/* Define to 1 if you have the declaration of `tzname', and to 0 if you don't.
|
||||
*/
|
||||
/* #undef HAVE_DECL_TZNAME */
|
||||
|
||||
/* Define to 1 if you have the <dlfcn.h> header file. */
|
||||
#define HAVE_DLFCN_H 0
|
||||
|
||||
/* Define to 1 if you have the `fseeko' function. */
|
||||
#define HAVE_FSEEKO 0
|
||||
|
||||
/* Define to 1 if you have the `ftello' function. */
|
||||
#define HAVE_FTELLO 0
|
||||
|
||||
/* Define to 1 if you have the <inttypes.h> header file. */
|
||||
#define HAVE_INTTYPES_H 0
|
||||
|
||||
/* Define to 1 if you have the `z' library (-lz). */
|
||||
#define HAVE_LIBZ 1
|
||||
|
||||
/* Define to 1 if you have the <memory.h> header file. */
|
||||
#define HAVE_MEMORY_H 0
|
||||
|
||||
/* Define to 1 if you have the `mkstemp' function. */
|
||||
#define HAVE_MKSTEMP 0
|
||||
|
||||
/* Define to 1 if you have the `MoveFileExA' function. */
|
||||
/* #undef HAVE_MOVEFILEEXA */
|
||||
|
||||
/* Define to 1 if you have the <stdint.h> header file. */
|
||||
#define HAVE_STDINT_H 1
|
||||
|
||||
/* Define to 1 if you have the <stdlib.h> header file. */
|
||||
#define HAVE_STDLIB_H 1
|
||||
|
||||
/* Define to 1 if you have the <strings.h> header file. */
|
||||
#define HAVE_STRINGS_H 1
|
||||
|
||||
/* Define to 1 if you have the <string.h> header file. */
|
||||
#define HAVE_STRING_H 1
|
||||
|
||||
/* Define to 1 if `tm_zone' is a member of `struct tm'. */
|
||||
#define HAVE_STRUCT_TM_TM_ZONE 1
|
||||
|
||||
/* Define to 1 if you have the <sys/stat.h> header file. */
|
||||
#define HAVE_SYS_STAT_H 1
|
||||
|
||||
/* Define to 1 if you have the <sys/types.h> header file. */
|
||||
#define HAVE_SYS_TYPES_H 1
|
||||
|
||||
/* Define to 1 if your `struct tm' has `tm_zone'. Deprecated, use
|
||||
`HAVE_STRUCT_TM_TM_ZONE' instead. */
|
||||
#define HAVE_TM_ZONE 1
|
||||
|
||||
/* Define to 1 if you don't have `tm_zone' but do have the external array
|
||||
`tzname'. */
|
||||
/* #undef HAVE_TZNAME */
|
||||
|
||||
/* Define to 1 if you have the <unistd.h> header file. */
|
||||
#define HAVE_UNISTD_H 1
|
||||
|
||||
/* Define to the sub-directory in which libtool stores uninstalled libraries.
|
||||
*/
|
||||
#define LT_OBJDIR ".libs/"
|
||||
|
||||
/* Define to 1 if your C compiler doesn't accept -c and -o together. */
|
||||
/* #undef NO_MINUS_C_MINUS_O */
|
||||
|
||||
/* Name of package */
|
||||
#define PACKAGE "libzip"
|
||||
|
||||
/* Define to the address where bug reports for this package should be sent. */
|
||||
#define PACKAGE_BUGREPORT "libzip@nih.at"
|
||||
|
||||
/* Define to the full name of this package. */
|
||||
#define PACKAGE_NAME "libzip"
|
||||
|
||||
/* Define to the full name and version of this package. */
|
||||
#define PACKAGE_STRING "libzip 0.9.3"
|
||||
|
||||
/* Define to the one symbol short name of this package. */
|
||||
#define PACKAGE_TARNAME "libzip"
|
||||
|
||||
/* Define to the home page for this package. */
|
||||
#define PACKAGE_URL ""
|
||||
|
||||
/* Define to the version of this package. */
|
||||
#define PACKAGE_VERSION "0.9.3"
|
||||
|
||||
/* Define to 1 if you have the ANSI C header files. */
|
||||
#define STDC_HEADERS 1
|
||||
|
||||
/* Define to 1 if your <sys/time.h> declares `struct tm'. */
|
||||
/* #undef TM_IN_SYS_TIME */
|
||||
|
||||
/* Version number of package */
|
||||
#define VERSION "0.9.3"
|
||||
@@ -0,0 +1,41 @@
|
||||
# libzip.la - a libtool library file
|
||||
# Generated by ltmain.sh (GNU libtool) 2.2.6b
|
||||
#
|
||||
# Please DO NOT delete this file!
|
||||
# It is necessary for linking the library.
|
||||
|
||||
# The name that we can dlopen(3).
|
||||
dlname='libzip.so.1'
|
||||
|
||||
# Names of this library.
|
||||
library_names='libzip.so.1.0.0 libzip.so.1 libzip.so'
|
||||
|
||||
# The name of the static archive.
|
||||
old_library='libzip.a'
|
||||
|
||||
# Linker flags that can not go in dependency_libs.
|
||||
inherited_linker_flags=''
|
||||
|
||||
# Libraries that this one depends upon.
|
||||
dependency_libs=' -lz'
|
||||
|
||||
# Names of additional weak libraries provided by this library
|
||||
weak_library_names=''
|
||||
|
||||
# Version information for libzip.
|
||||
current=1
|
||||
age=0
|
||||
revision=0
|
||||
|
||||
# Is this an already installed library?
|
||||
installed=no
|
||||
|
||||
# Should we warn about portability when linking against -modules?
|
||||
shouldnotlink=no
|
||||
|
||||
# Files to dlopen/dlpreopen
|
||||
dlopen=''
|
||||
dlpreopen=''
|
||||
|
||||
# Directory that this library needs to be installed in:
|
||||
libdir='/usr/local/lib'
|
||||
@@ -0,0 +1,140 @@
|
||||
/* $NiH: mkstemp.c,v 1.3 2006/04/23 14:51:45 wiz Exp $ */
|
||||
|
||||
/* Adapted from NetBSB libc by Dieter Baron */
|
||||
|
||||
/* NetBSD: gettemp.c,v 1.13 2003/12/05 00:57:36 uebayasi Exp */
|
||||
|
||||
/*
|
||||
* Copyright (c) 1987, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#ifndef O_BINARY
|
||||
#define O_BINARY 0
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
int
|
||||
_zip_mkstemp(char *path)
|
||||
{
|
||||
int fd;
|
||||
char *start, *trv;
|
||||
struct stat sbuf;
|
||||
pid_t pid;
|
||||
|
||||
/* To guarantee multiple calls generate unique names even if
|
||||
the file is not created. 676 different possibilities with 7
|
||||
or more X's, 26 with 6 or less. */
|
||||
static char xtra[2] = "aa";
|
||||
int xcnt = 0;
|
||||
|
||||
pid = getpid();
|
||||
|
||||
/* Move to end of path and count trailing X's. */
|
||||
for (trv = path; *trv; ++trv)
|
||||
if (*trv == 'X')
|
||||
xcnt++;
|
||||
else
|
||||
xcnt = 0;
|
||||
|
||||
/* Use at least one from xtra. Use 2 if more than 6 X's. */
|
||||
if (*(trv - 1) == 'X')
|
||||
*--trv = xtra[0];
|
||||
if (xcnt > 6 && *(trv - 1) == 'X')
|
||||
*--trv = xtra[1];
|
||||
|
||||
/* Set remaining X's to pid digits with 0's to the left. */
|
||||
while (*--trv == 'X') {
|
||||
*trv = (pid % 10) + '0';
|
||||
pid /= 10;
|
||||
}
|
||||
|
||||
/* update xtra for next call. */
|
||||
if (xtra[0] != 'z')
|
||||
xtra[0]++;
|
||||
else {
|
||||
xtra[0] = 'a';
|
||||
if (xtra[1] != 'z')
|
||||
xtra[1]++;
|
||||
else
|
||||
xtra[1] = 'a';
|
||||
}
|
||||
|
||||
/*
|
||||
* check the target directory; if you have six X's and it
|
||||
* doesn't exist this runs for a *very* long time.
|
||||
*/
|
||||
for (start = trv + 1;; --trv) {
|
||||
if (trv <= path)
|
||||
break;
|
||||
if (*trv == '/') {
|
||||
*trv = '\0';
|
||||
if (stat(path, &sbuf))
|
||||
return (0);
|
||||
if (!S_ISDIR(sbuf.st_mode)) {
|
||||
errno = ENOTDIR;
|
||||
return (0);
|
||||
}
|
||||
*trv = '/';
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
if ((fd=open(path, O_CREAT|O_EXCL|O_RDWR|O_BINARY, 0600)) >= 0)
|
||||
return (fd);
|
||||
if (errno != EEXIST)
|
||||
return (0);
|
||||
|
||||
/* tricky little algorithm for backward compatibility */
|
||||
for (trv = start;;) {
|
||||
if (!*trv)
|
||||
return (0);
|
||||
if (*trv == 'z')
|
||||
*trv++ = 'a';
|
||||
else {
|
||||
if (isdigit((unsigned char)*trv))
|
||||
*trv = 'a';
|
||||
else
|
||||
++*trv;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
/*NOTREACHED*/
|
||||
}
|
||||
@@ -0,0 +1,230 @@
|
||||
#ifndef _HAD_ZIP_H
|
||||
#define _HAD_ZIP_H
|
||||
|
||||
/*
|
||||
zip.h -- exported declarations.
|
||||
Copyright (C) 1999-2008 Dieter Baron and Thomas Klausner
|
||||
|
||||
This file is part of libzip, a library to manipulate ZIP archives.
|
||||
The authors can be contacted at <libzip@nih.at>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. The names of the authors may not be used to endorse or promote
|
||||
products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#ifndef ZIP_EXTERN
|
||||
#ifdef _MSC_VER
|
||||
#define ZIP_EXTERN __declspec(dllexport)
|
||||
#else
|
||||
#define ZIP_EXTERN
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
/* flags for zip_open */
|
||||
|
||||
#define ZIP_CREATE 1
|
||||
#define ZIP_EXCL 2
|
||||
#define ZIP_CHECKCONS 4
|
||||
|
||||
|
||||
/* flags for zip_name_locate, zip_fopen, zip_stat, ... */
|
||||
|
||||
#define ZIP_FL_NOCASE 1 /* ignore case on name lookup */
|
||||
#define ZIP_FL_NODIR 2 /* ignore directory component */
|
||||
#define ZIP_FL_COMPRESSED 4 /* read compressed data */
|
||||
#define ZIP_FL_UNCHANGED 8 /* use original data, ignoring changes */
|
||||
#define ZIP_FL_RECOMPRESS 16 /* force recompression of data */
|
||||
|
||||
/* archive global flags flags */
|
||||
|
||||
#define ZIP_AFL_TORRENT 1 /* torrent zipped */
|
||||
|
||||
/* libzip error codes */
|
||||
|
||||
#define ZIP_ER_OK 0 /* N No error */
|
||||
#define ZIP_ER_MULTIDISK 1 /* N Multi-disk zip archives not supported */
|
||||
#define ZIP_ER_RENAME 2 /* S Renaming temporary file failed */
|
||||
#define ZIP_ER_CLOSE 3 /* S Closing zip archive failed */
|
||||
#define ZIP_ER_SEEK 4 /* S Seek error */
|
||||
#define ZIP_ER_READ 5 /* S Read error */
|
||||
#define ZIP_ER_WRITE 6 /* S Write error */
|
||||
#define ZIP_ER_CRC 7 /* N CRC error */
|
||||
#define ZIP_ER_ZIPCLOSED 8 /* N Containing zip archive was closed */
|
||||
#define ZIP_ER_NOENT 9 /* N No such file */
|
||||
#define ZIP_ER_EXISTS 10 /* N File already exists */
|
||||
#define ZIP_ER_OPEN 11 /* S Can't open file */
|
||||
#define ZIP_ER_TMPOPEN 12 /* S Failure to create temporary file */
|
||||
#define ZIP_ER_ZLIB 13 /* Z Zlib error */
|
||||
#define ZIP_ER_MEMORY 14 /* N Malloc failure */
|
||||
#define ZIP_ER_CHANGED 15 /* N Entry has been changed */
|
||||
#define ZIP_ER_COMPNOTSUPP 16 /* N Compression method not supported */
|
||||
#define ZIP_ER_EOF 17 /* N Premature EOF */
|
||||
#define ZIP_ER_INVAL 18 /* N Invalid argument */
|
||||
#define ZIP_ER_NOZIP 19 /* N Not a zip archive */
|
||||
#define ZIP_ER_INTERNAL 20 /* N Internal error */
|
||||
#define ZIP_ER_INCONS 21 /* N Zip archive inconsistent */
|
||||
#define ZIP_ER_REMOVE 22 /* S Can't remove file */
|
||||
#define ZIP_ER_DELETED 23 /* N Entry has been deleted */
|
||||
|
||||
|
||||
/* type of system error value */
|
||||
|
||||
#define ZIP_ET_NONE 0 /* sys_err unused */
|
||||
#define ZIP_ET_SYS 1 /* sys_err is errno */
|
||||
#define ZIP_ET_ZLIB 2 /* sys_err is zlib error code */
|
||||
|
||||
/* compression methods */
|
||||
|
||||
#define ZIP_CM_DEFAULT -1 /* better of deflate or store */
|
||||
#define ZIP_CM_STORE 0 /* stored (uncompressed) */
|
||||
#define ZIP_CM_SHRINK 1 /* shrunk */
|
||||
#define ZIP_CM_REDUCE_1 2 /* reduced with factor 1 */
|
||||
#define ZIP_CM_REDUCE_2 3 /* reduced with factor 2 */
|
||||
#define ZIP_CM_REDUCE_3 4 /* reduced with factor 3 */
|
||||
#define ZIP_CM_REDUCE_4 5 /* reduced with factor 4 */
|
||||
#define ZIP_CM_IMPLODE 6 /* imploded */
|
||||
/* 7 - Reserved for Tokenizing compression algorithm */
|
||||
#define ZIP_CM_DEFLATE 8 /* deflated */
|
||||
#define ZIP_CM_DEFLATE64 9 /* deflate64 */
|
||||
#define ZIP_CM_PKWARE_IMPLODE 10 /* PKWARE imploding */
|
||||
/* 11 - Reserved by PKWARE */
|
||||
#define ZIP_CM_BZIP2 12 /* compressed using BZIP2 algorithm */
|
||||
/* 13 - Reserved by PKWARE */
|
||||
#define ZIP_CM_LZMA 14 /* LZMA (EFS) */
|
||||
/* 15-17 - Reserved by PKWARE */
|
||||
#define ZIP_CM_TERSE 18 /* compressed using IBM TERSE (new) */
|
||||
#define ZIP_CM_LZ77 19 /* IBM LZ77 z Architecture (PFS) */
|
||||
#define ZIP_CM_WAVPACK 97 /* WavPack compressed data */
|
||||
#define ZIP_CM_PPMD 98 /* PPMd version I, Rev 1 */
|
||||
|
||||
/* encryption methods */
|
||||
|
||||
#define ZIP_EM_NONE 0 /* not encrypted */
|
||||
#define ZIP_EM_TRAD_PKWARE 1 /* traditional PKWARE encryption */
|
||||
#if 0 /* Strong Encryption Header not parsed yet */
|
||||
#define ZIP_EM_DES 0x6601 /* strong encryption: DES */
|
||||
#define ZIP_EM_RC2_OLD 0x6602 /* strong encryption: RC2, version < 5.2 */
|
||||
#define ZIP_EM_3DES_168 0x6603
|
||||
#define ZIP_EM_3DES_112 0x6609
|
||||
#define ZIP_EM_AES_128 0x660e
|
||||
#define ZIP_EM_AES_192 0x660f
|
||||
#define ZIP_EM_AES_256 0x6610
|
||||
#define ZIP_EM_RC2 0x6702 /* strong encryption: RC2, version >= 5.2 */
|
||||
#define ZIP_EM_RC4 0x6801
|
||||
#endif
|
||||
#define ZIP_EM_UNKNOWN 0xffff /* unknown algorithm */
|
||||
|
||||
|
||||
|
||||
enum zip_source_cmd {
|
||||
ZIP_SOURCE_OPEN, /* prepare for reading */
|
||||
ZIP_SOURCE_READ, /* read data */
|
||||
ZIP_SOURCE_CLOSE, /* reading is done */
|
||||
ZIP_SOURCE_STAT, /* get meta information */
|
||||
ZIP_SOURCE_ERROR, /* get error information */
|
||||
ZIP_SOURCE_FREE /* cleanup and free resources */
|
||||
};
|
||||
|
||||
typedef ssize_t (*zip_source_callback)(void *state, void *data,
|
||||
size_t len, enum zip_source_cmd cmd);
|
||||
|
||||
struct zip_stat {
|
||||
const char *name; /* name of the file */
|
||||
int index; /* index within archive */
|
||||
unsigned int crc; /* crc of file data */
|
||||
time_t mtime; /* modification time */
|
||||
off_t size; /* size of file (uncompressed) */
|
||||
off_t comp_size; /* size of file (compressed) */
|
||||
unsigned short comp_method; /* compression method used */
|
||||
unsigned short encryption_method; /* encryption method used */
|
||||
};
|
||||
|
||||
struct zip;
|
||||
struct zip_file;
|
||||
struct zip_source;
|
||||
|
||||
|
||||
|
||||
ZIP_EXTERN int zip_add(struct zip *, const char *, struct zip_source *);
|
||||
ZIP_EXTERN int zip_add_dir(struct zip *, const char *);
|
||||
ZIP_EXTERN int zip_close(struct zip *);
|
||||
ZIP_EXTERN int zip_delete(struct zip *, int);
|
||||
ZIP_EXTERN void zip_error_clear(struct zip *);
|
||||
ZIP_EXTERN void zip_error_get(struct zip *, int *, int *);
|
||||
ZIP_EXTERN int zip_error_get_sys_type(int);
|
||||
ZIP_EXTERN int zip_error_to_str(char *, size_t, int, int);
|
||||
ZIP_EXTERN int zip_fclose(struct zip_file *);
|
||||
ZIP_EXTERN void zip_file_error_clear(struct zip_file *);
|
||||
ZIP_EXTERN void zip_file_error_get(struct zip_file *, int *, int *);
|
||||
ZIP_EXTERN const char *zip_file_strerror(struct zip_file *);
|
||||
ZIP_EXTERN struct zip_file *zip_fopen(struct zip *, const char *, int);
|
||||
ZIP_EXTERN struct zip_file *zip_fopen_index(struct zip *, int, int);
|
||||
ZIP_EXTERN ssize_t zip_fread(struct zip_file *, void *, size_t);
|
||||
ZIP_EXTERN const char *zip_get_archive_comment(struct zip *, int *, int);
|
||||
ZIP_EXTERN int zip_get_archive_flag(struct zip *, int, int);
|
||||
ZIP_EXTERN const char *zip_get_file_comment(struct zip *, int, int *, int);
|
||||
ZIP_EXTERN const char *zip_get_name(struct zip *, int, int);
|
||||
ZIP_EXTERN int zip_get_num_files(struct zip *);
|
||||
ZIP_EXTERN int zip_name_locate(struct zip *, const char *, int);
|
||||
ZIP_EXTERN struct zip *zip_open(const char *, int, int *);
|
||||
ZIP_EXTERN int zip_rename(struct zip *, int, const char *);
|
||||
ZIP_EXTERN int zip_replace(struct zip *, int, struct zip_source *);
|
||||
ZIP_EXTERN int zip_set_archive_comment(struct zip *, const char *, int);
|
||||
ZIP_EXTERN int zip_set_archive_flag(struct zip *, int, int);
|
||||
ZIP_EXTERN int zip_set_file_comment(struct zip *, int, const char *, int);
|
||||
ZIP_EXTERN struct zip_source *zip_source_buffer(struct zip *, const void *,
|
||||
off_t, int);
|
||||
ZIP_EXTERN struct zip_source *zip_source_file(struct zip *, const char *,
|
||||
off_t, off_t);
|
||||
ZIP_EXTERN struct zip_source *zip_source_filep(struct zip *, FILE *,
|
||||
off_t, off_t);
|
||||
ZIP_EXTERN void zip_source_free(struct zip_source *);
|
||||
ZIP_EXTERN struct zip_source *zip_source_function(struct zip *,
|
||||
zip_source_callback, void *);
|
||||
ZIP_EXTERN struct zip_source *zip_source_zip(struct zip *, struct zip *,
|
||||
int, int, off_t, off_t);
|
||||
ZIP_EXTERN int zip_stat(struct zip *, const char *, int, struct zip_stat *);
|
||||
ZIP_EXTERN int zip_stat_index(struct zip *, int, int, struct zip_stat *);
|
||||
ZIP_EXTERN void zip_stat_init(struct zip_stat *);
|
||||
ZIP_EXTERN const char *zip_strerror(struct zip *);
|
||||
ZIP_EXTERN int zip_unchange(struct zip *, int);
|
||||
ZIP_EXTERN int zip_unchange_all(struct zip *);
|
||||
ZIP_EXTERN int zip_unchange_archive(struct zip *);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _HAD_ZIP_H */
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
zip_add.c -- add file via callback function
|
||||
Copyright (C) 1999-2007 Dieter Baron and Thomas Klausner
|
||||
|
||||
This file is part of libzip, a library to manipulate ZIP archives.
|
||||
The authors can be contacted at <libzip@nih.at>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. The names of the authors may not be used to endorse or promote
|
||||
products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include "zipint.h"
|
||||
|
||||
|
||||
|
||||
ZIP_EXTERN int
|
||||
zip_add(struct zip *za, const char *name, struct zip_source *source)
|
||||
{
|
||||
if (name == NULL || source == NULL) {
|
||||
_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return _zip_replace(za, -1, name, source);
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
zip_add_dir.c -- add directory
|
||||
Copyright (C) 1999-2007 Dieter Baron and Thomas Klausner
|
||||
|
||||
This file is part of libzip, a library to manipulate ZIP archives.
|
||||
The authors can be contacted at <libzip@nih.at>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. The names of the authors may not be used to endorse or promote
|
||||
products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "zipint.h"
|
||||
|
||||
|
||||
|
||||
ZIP_EXTERN int
|
||||
zip_add_dir(struct zip *za, const char *name)
|
||||
{
|
||||
int len, ret;
|
||||
char *s;
|
||||
struct zip_source *source;
|
||||
|
||||
if (name == NULL) {
|
||||
_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
|
||||
return -1;
|
||||
}
|
||||
|
||||
s = NULL;
|
||||
len = strlen(name);
|
||||
|
||||
if (name[len-1] != '/') {
|
||||
if ((s=(char *)malloc(len+2)) == NULL) {
|
||||
_zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
|
||||
return -1;
|
||||
}
|
||||
strcpy(s, name);
|
||||
s[len] = '/';
|
||||
s[len+1] = '\0';
|
||||
}
|
||||
|
||||
if ((source=zip_source_buffer(za, NULL, 0, 0)) == NULL) {
|
||||
free(s);
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = _zip_replace(za, -1, s ? s : name, source);
|
||||
|
||||
free(s);
|
||||
if (ret < 0)
|
||||
zip_source_free(source);
|
||||
|
||||
return ret;
|
||||
}
|
||||
@@ -0,0 +1,679 @@
|
||||
/*
|
||||
zip_close.c -- close zip archive and update changes
|
||||
Copyright (C) 1999-2008 Dieter Baron and Thomas Klausner
|
||||
|
||||
This file is part of libzip, a library to manipulate ZIP archives.
|
||||
The authors can be contacted at <libzip@nih.at>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. The names of the authors may not be used to endorse or promote
|
||||
products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "zipint.h"
|
||||
|
||||
static int add_data(struct zip *, struct zip_source *, struct zip_dirent *,
|
||||
FILE *);
|
||||
static int add_data_comp(zip_source_callback, void *, struct zip_stat *,
|
||||
FILE *, struct zip_error *);
|
||||
static int add_data_uncomp(struct zip *, zip_source_callback, void *,
|
||||
struct zip_stat *, FILE *);
|
||||
static void ch_set_error(struct zip_error *, zip_source_callback, void *);
|
||||
static int copy_data(FILE *, off_t, FILE *, struct zip_error *);
|
||||
static int write_cdir(struct zip *, struct zip_cdir *, FILE *);
|
||||
static int _zip_cdir_set_comment(struct zip_cdir *, struct zip *);
|
||||
static int _zip_changed(struct zip *, int *);
|
||||
static char *_zip_create_temp_output(struct zip *, FILE **);
|
||||
static int _zip_torrentzip_cmp(const void *, const void *);
|
||||
|
||||
|
||||
|
||||
struct filelist {
|
||||
int idx;
|
||||
const char *name;
|
||||
};
|
||||
|
||||
|
||||
|
||||
ZIP_EXTERN int
|
||||
zip_close(struct zip *za)
|
||||
{
|
||||
int survivors;
|
||||
int i, j, error;
|
||||
char *temp;
|
||||
FILE *out;
|
||||
mode_t mask;
|
||||
struct zip_cdir *cd;
|
||||
struct zip_dirent de;
|
||||
struct filelist *filelist;
|
||||
int reopen_on_error;
|
||||
int new_torrentzip;
|
||||
|
||||
reopen_on_error = 0;
|
||||
|
||||
if (za == NULL)
|
||||
return -1;
|
||||
|
||||
if (!_zip_changed(za, &survivors)) {
|
||||
_zip_free(za);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* don't create zip files with no entries */
|
||||
if (survivors == 0) {
|
||||
if (za->zn && za->zp) {
|
||||
if (remove(za->zn) != 0) {
|
||||
_zip_error_set(&za->error, ZIP_ER_REMOVE, errno);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
_zip_free(za);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if ((filelist=(struct filelist *)malloc(sizeof(filelist[0])*survivors))
|
||||
== NULL)
|
||||
return -1;
|
||||
|
||||
if ((cd=_zip_cdir_new(survivors, &za->error)) == NULL) {
|
||||
free(filelist);
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (i=0; i<survivors; i++)
|
||||
_zip_dirent_init(&cd->entry[i]);
|
||||
|
||||
/* archive comment is special for torrentzip */
|
||||
if (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0)) {
|
||||
cd->comment = _zip_memdup(TORRENT_SIG "XXXXXXXX",
|
||||
TORRENT_SIG_LEN + TORRENT_CRC_LEN,
|
||||
&za->error);
|
||||
if (cd->comment == NULL) {
|
||||
_zip_cdir_free(cd);
|
||||
free(filelist);
|
||||
return -1;
|
||||
}
|
||||
cd->comment_len = TORRENT_SIG_LEN + TORRENT_CRC_LEN;
|
||||
}
|
||||
else if (zip_get_archive_flag(za, ZIP_AFL_TORRENT, ZIP_FL_UNCHANGED) == 0) {
|
||||
if (_zip_cdir_set_comment(cd, za) == -1) {
|
||||
_zip_cdir_free(cd);
|
||||
free(filelist);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
if ((temp=_zip_create_temp_output(za, &out)) == NULL) {
|
||||
_zip_cdir_free(cd);
|
||||
free(filelist);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
/* create list of files with index into original archive */
|
||||
for (i=j=0; i<za->nentry; i++) {
|
||||
if (za->entry[i].state == ZIP_ST_DELETED)
|
||||
continue;
|
||||
|
||||
filelist[j].idx = i;
|
||||
filelist[j].name = zip_get_name(za, i, 0);
|
||||
j++;
|
||||
}
|
||||
if (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0))
|
||||
qsort(filelist, survivors, sizeof(filelist[0]),
|
||||
_zip_torrentzip_cmp);
|
||||
|
||||
new_torrentzip = (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0) == 1
|
||||
&& zip_get_archive_flag(za, ZIP_AFL_TORRENT,
|
||||
ZIP_FL_UNCHANGED) == 0);
|
||||
error = 0;
|
||||
for (j=0; j<survivors; j++) {
|
||||
i = filelist[j].idx;
|
||||
|
||||
/* create new local directory entry */
|
||||
if (ZIP_ENTRY_DATA_CHANGED(za->entry+i) || new_torrentzip) {
|
||||
_zip_dirent_init(&de);
|
||||
|
||||
if (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0))
|
||||
_zip_dirent_torrent_normalize(&de);
|
||||
|
||||
/* use it as central directory entry */
|
||||
memcpy(cd->entry+j, &de, sizeof(cd->entry[j]));
|
||||
|
||||
/* set/update file name */
|
||||
if (za->entry[i].ch_filename == NULL) {
|
||||
if (za->entry[i].state == ZIP_ST_ADDED) {
|
||||
de.filename = strdup("-");
|
||||
de.filename_len = 1;
|
||||
cd->entry[j].filename = "-";
|
||||
cd->entry[j].filename_len = 1;
|
||||
}
|
||||
else {
|
||||
de.filename = strdup(za->cdir->entry[i].filename);
|
||||
de.filename_len = strlen(de.filename);
|
||||
cd->entry[j].filename = za->cdir->entry[i].filename;
|
||||
cd->entry[j].filename_len = de.filename_len;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
/* copy existing directory entries */
|
||||
if (fseeko(za->zp, za->cdir->entry[i].offset, SEEK_SET) != 0) {
|
||||
_zip_error_set(&za->error, ZIP_ER_SEEK, errno);
|
||||
error = 1;
|
||||
break;
|
||||
}
|
||||
if (_zip_dirent_read(&de, za->zp, NULL, NULL, 1,
|
||||
&za->error) != 0) {
|
||||
error = 1;
|
||||
break;
|
||||
}
|
||||
memcpy(cd->entry+j, za->cdir->entry+i, sizeof(cd->entry[j]));
|
||||
if (de.bitflags & ZIP_GPBF_DATA_DESCRIPTOR) {
|
||||
de.crc = za->cdir->entry[i].crc;
|
||||
de.comp_size = za->cdir->entry[i].comp_size;
|
||||
de.uncomp_size = za->cdir->entry[i].uncomp_size;
|
||||
de.bitflags &= ~ZIP_GPBF_DATA_DESCRIPTOR;
|
||||
cd->entry[j].bitflags &= ~ZIP_GPBF_DATA_DESCRIPTOR;
|
||||
}
|
||||
}
|
||||
|
||||
if (za->entry[i].ch_filename) {
|
||||
free(de.filename);
|
||||
if ((de.filename=strdup(za->entry[i].ch_filename)) == NULL) {
|
||||
error = 1;
|
||||
break;
|
||||
}
|
||||
de.filename_len = strlen(de.filename);
|
||||
cd->entry[j].filename = za->entry[i].ch_filename;
|
||||
cd->entry[j].filename_len = de.filename_len;
|
||||
}
|
||||
|
||||
if (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0) == 0
|
||||
&& za->entry[i].ch_comment_len != -1) {
|
||||
/* as the rest of cd entries, its malloc/free is done by za */
|
||||
cd->entry[j].comment = za->entry[i].ch_comment;
|
||||
cd->entry[j].comment_len = za->entry[i].ch_comment_len;
|
||||
}
|
||||
|
||||
cd->entry[j].offset = ftello(out);
|
||||
|
||||
if (ZIP_ENTRY_DATA_CHANGED(za->entry+i) || new_torrentzip) {
|
||||
struct zip_source *zs;
|
||||
|
||||
zs = NULL;
|
||||
if (!ZIP_ENTRY_DATA_CHANGED(za->entry+i)) {
|
||||
if ((zs=zip_source_zip(za, za, i, ZIP_FL_RECOMPRESS, 0, -1))
|
||||
== NULL) {
|
||||
error = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (add_data(za, zs ? zs : za->entry[i].source, &de, out) < 0) {
|
||||
error = 1;
|
||||
break;
|
||||
}
|
||||
cd->entry[j].last_mod = de.last_mod;
|
||||
cd->entry[j].comp_method = de.comp_method;
|
||||
cd->entry[j].comp_size = de.comp_size;
|
||||
cd->entry[j].uncomp_size = de.uncomp_size;
|
||||
cd->entry[j].crc = de.crc;
|
||||
}
|
||||
else {
|
||||
if (_zip_dirent_write(&de, out, 1, &za->error) < 0) {
|
||||
error = 1;
|
||||
break;
|
||||
}
|
||||
/* we just read the local dirent, file is at correct position */
|
||||
if (copy_data(za->zp, cd->entry[j].comp_size, out,
|
||||
&za->error) < 0) {
|
||||
error = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
_zip_dirent_finalize(&de);
|
||||
}
|
||||
|
||||
free(filelist);
|
||||
|
||||
if (!error) {
|
||||
if (write_cdir(za, cd, out) < 0)
|
||||
error = 1;
|
||||
}
|
||||
|
||||
/* pointers in cd entries are owned by za */
|
||||
cd->nentry = 0;
|
||||
_zip_cdir_free(cd);
|
||||
|
||||
if (error) {
|
||||
_zip_dirent_finalize(&de);
|
||||
fclose(out);
|
||||
remove(temp);
|
||||
free(temp);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (fclose(out) != 0) {
|
||||
_zip_error_set(&za->error, ZIP_ER_CLOSE, errno);
|
||||
remove(temp);
|
||||
free(temp);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (za->zp) {
|
||||
fclose(za->zp);
|
||||
za->zp = NULL;
|
||||
reopen_on_error = 1;
|
||||
}
|
||||
if (_zip_rename(temp, za->zn) != 0) {
|
||||
_zip_error_set(&za->error, ZIP_ER_RENAME, errno);
|
||||
remove(temp);
|
||||
free(temp);
|
||||
if (reopen_on_error) {
|
||||
/* ignore errors, since we're already in an error case */
|
||||
za->zp = fopen(za->zn, "rb");
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
mask = umask(0);
|
||||
umask(mask);
|
||||
chmod(za->zn, 0666&~mask);
|
||||
|
||||
_zip_free(za);
|
||||
free(temp);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static int
|
||||
add_data(struct zip *za, struct zip_source *zs, struct zip_dirent *de, FILE *ft)
|
||||
{
|
||||
off_t offstart, offend;
|
||||
zip_source_callback cb;
|
||||
void *ud;
|
||||
struct zip_stat st;
|
||||
|
||||
cb = zs->f;
|
||||
ud = zs->ud;
|
||||
|
||||
if (cb(ud, &st, sizeof(st), ZIP_SOURCE_STAT) < (ssize_t)sizeof(st)) {
|
||||
ch_set_error(&za->error, cb, ud);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (cb(ud, NULL, 0, ZIP_SOURCE_OPEN) < 0) {
|
||||
ch_set_error(&za->error, cb, ud);
|
||||
return -1;
|
||||
}
|
||||
|
||||
offstart = ftello(ft);
|
||||
|
||||
if (_zip_dirent_write(de, ft, 1, &za->error) < 0)
|
||||
return -1;
|
||||
|
||||
if (st.comp_method != ZIP_CM_STORE) {
|
||||
if (add_data_comp(cb, ud, &st, ft, &za->error) < 0)
|
||||
return -1;
|
||||
}
|
||||
else {
|
||||
if (add_data_uncomp(za, cb, ud, &st, ft) < 0)
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (cb(ud, NULL, 0, ZIP_SOURCE_CLOSE) < 0) {
|
||||
ch_set_error(&za->error, cb, ud);
|
||||
return -1;
|
||||
}
|
||||
|
||||
offend = ftello(ft);
|
||||
|
||||
if (fseeko(ft, offstart, SEEK_SET) < 0) {
|
||||
_zip_error_set(&za->error, ZIP_ER_SEEK, errno);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
de->last_mod = st.mtime;
|
||||
de->comp_method = st.comp_method;
|
||||
de->crc = st.crc;
|
||||
de->uncomp_size = st.size;
|
||||
de->comp_size = st.comp_size;
|
||||
|
||||
if (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0))
|
||||
_zip_dirent_torrent_normalize(de);
|
||||
|
||||
if (_zip_dirent_write(de, ft, 1, &za->error) < 0)
|
||||
return -1;
|
||||
|
||||
if (fseeko(ft, offend, SEEK_SET) < 0) {
|
||||
_zip_error_set(&za->error, ZIP_ER_SEEK, errno);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static int
|
||||
add_data_comp(zip_source_callback cb, void *ud, struct zip_stat *st,FILE *ft,
|
||||
struct zip_error *error)
|
||||
{
|
||||
char buf[BUFSIZE];
|
||||
ssize_t n;
|
||||
|
||||
st->comp_size = 0;
|
||||
while ((n=cb(ud, buf, sizeof(buf), ZIP_SOURCE_READ)) > 0) {
|
||||
if (fwrite(buf, 1, n, ft) != (size_t)n) {
|
||||
_zip_error_set(error, ZIP_ER_WRITE, errno);
|
||||
return -1;
|
||||
}
|
||||
|
||||
st->comp_size += n;
|
||||
}
|
||||
if (n < 0) {
|
||||
ch_set_error(error, cb, ud);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static int
|
||||
add_data_uncomp(struct zip *za, zip_source_callback cb, void *ud,
|
||||
struct zip_stat *st, FILE *ft)
|
||||
{
|
||||
char b1[BUFSIZE], b2[BUFSIZE];
|
||||
int end, flush, ret;
|
||||
ssize_t n;
|
||||
size_t n2;
|
||||
z_stream zstr;
|
||||
int mem_level;
|
||||
|
||||
st->comp_method = ZIP_CM_DEFLATE;
|
||||
st->comp_size = st->size = 0;
|
||||
st->crc = crc32(0, NULL, 0);
|
||||
|
||||
zstr.zalloc = Z_NULL;
|
||||
zstr.zfree = Z_NULL;
|
||||
zstr.opaque = NULL;
|
||||
zstr.avail_in = 0;
|
||||
zstr.avail_out = 0;
|
||||
|
||||
if (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0))
|
||||
mem_level = TORRENT_MEM_LEVEL;
|
||||
else
|
||||
mem_level = MAX_MEM_LEVEL;
|
||||
|
||||
/* -MAX_WBITS: undocumented feature of zlib to _not_ write a zlib header */
|
||||
deflateInit2(&zstr, Z_BEST_COMPRESSION, Z_DEFLATED, -MAX_WBITS, mem_level,
|
||||
Z_DEFAULT_STRATEGY);
|
||||
|
||||
zstr.next_out = (Bytef *)b2;
|
||||
zstr.avail_out = sizeof(b2);
|
||||
zstr.next_in = NULL;
|
||||
zstr.avail_in = 0;
|
||||
|
||||
flush = 0;
|
||||
end = 0;
|
||||
while (!end) {
|
||||
if (zstr.avail_in == 0 && !flush) {
|
||||
if ((n=cb(ud, b1, sizeof(b1), ZIP_SOURCE_READ)) < 0) {
|
||||
ch_set_error(&za->error, cb, ud);
|
||||
deflateEnd(&zstr);
|
||||
return -1;
|
||||
}
|
||||
if (n > 0) {
|
||||
zstr.avail_in = n;
|
||||
zstr.next_in = (Bytef *)b1;
|
||||
st->size += n;
|
||||
st->crc = crc32(st->crc, (Bytef *)b1, n);
|
||||
}
|
||||
else
|
||||
flush = Z_FINISH;
|
||||
}
|
||||
|
||||
ret = deflate(&zstr, flush);
|
||||
if (ret != Z_OK && ret != Z_STREAM_END) {
|
||||
_zip_error_set(&za->error, ZIP_ER_ZLIB, ret);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (zstr.avail_out != sizeof(b2)) {
|
||||
n2 = sizeof(b2) - zstr.avail_out;
|
||||
|
||||
if (fwrite(b2, 1, n2, ft) != n2) {
|
||||
_zip_error_set(&za->error, ZIP_ER_WRITE, errno);
|
||||
return -1;
|
||||
}
|
||||
|
||||
zstr.next_out = (Bytef *)b2;
|
||||
zstr.avail_out = sizeof(b2);
|
||||
st->comp_size += n2;
|
||||
}
|
||||
|
||||
if (ret == Z_STREAM_END) {
|
||||
deflateEnd(&zstr);
|
||||
end = 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void
|
||||
ch_set_error(struct zip_error *error, zip_source_callback cb, void *ud)
|
||||
{
|
||||
int e[2];
|
||||
|
||||
if ((cb(ud, e, sizeof(e), ZIP_SOURCE_ERROR)) < (ssize_t)sizeof(e)) {
|
||||
error->zip_err = ZIP_ER_INTERNAL;
|
||||
error->sys_err = 0;
|
||||
}
|
||||
else {
|
||||
error->zip_err = e[0];
|
||||
error->sys_err = e[1];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
static int
|
||||
copy_data(FILE *fs, off_t len, FILE *ft, struct zip_error *error)
|
||||
{
|
||||
char buf[BUFSIZE];
|
||||
int n, nn;
|
||||
|
||||
if (len == 0)
|
||||
return 0;
|
||||
|
||||
while (len > 0) {
|
||||
nn = len > sizeof(buf) ? sizeof(buf) : len;
|
||||
if ((n=fread(buf, 1, nn, fs)) < 0) {
|
||||
_zip_error_set(error, ZIP_ER_READ, errno);
|
||||
return -1;
|
||||
}
|
||||
else if (n == 0) {
|
||||
_zip_error_set(error, ZIP_ER_EOF, 0);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (fwrite(buf, 1, n, ft) != (size_t)n) {
|
||||
_zip_error_set(error, ZIP_ER_WRITE, errno);
|
||||
return -1;
|
||||
}
|
||||
|
||||
len -= n;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static int
|
||||
write_cdir(struct zip *za, struct zip_cdir *cd, FILE *out)
|
||||
{
|
||||
off_t offset;
|
||||
uLong crc;
|
||||
char buf[TORRENT_CRC_LEN+1];
|
||||
|
||||
if (_zip_cdir_write(cd, out, &za->error) < 0)
|
||||
return -1;
|
||||
|
||||
if (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0) == 0)
|
||||
return 0;
|
||||
|
||||
|
||||
/* fix up torrentzip comment */
|
||||
|
||||
offset = ftello(out);
|
||||
|
||||
if (_zip_filerange_crc(out, cd->offset, cd->size, &crc, &za->error) < 0)
|
||||
return -1;
|
||||
|
||||
snprintf(buf, sizeof(buf), "%08lX", (long)crc);
|
||||
|
||||
if (fseeko(out, offset-TORRENT_CRC_LEN, SEEK_SET) < 0) {
|
||||
_zip_error_set(&za->error, ZIP_ER_SEEK, errno);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (fwrite(buf, TORRENT_CRC_LEN, 1, out) != 1) {
|
||||
_zip_error_set(&za->error, ZIP_ER_WRITE, errno);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static int
|
||||
_zip_cdir_set_comment(struct zip_cdir *dest, struct zip *src)
|
||||
{
|
||||
if (src->ch_comment_len != -1) {
|
||||
dest->comment = _zip_memdup(src->ch_comment,
|
||||
src->ch_comment_len, &src->error);
|
||||
if (dest->comment == NULL)
|
||||
return -1;
|
||||
dest->comment_len = src->ch_comment_len;
|
||||
} else {
|
||||
if (src->cdir && src->cdir->comment) {
|
||||
dest->comment = _zip_memdup(src->cdir->comment,
|
||||
src->cdir->comment_len, &src->error);
|
||||
if (dest->comment == NULL)
|
||||
return -1;
|
||||
dest->comment_len = src->cdir->comment_len;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static int
|
||||
_zip_changed(struct zip *za, int *survivorsp)
|
||||
{
|
||||
int changed, i, survivors;
|
||||
|
||||
changed = survivors = 0;
|
||||
|
||||
if (za->ch_comment_len != -1
|
||||
|| za->ch_flags != za->flags)
|
||||
changed = 1;
|
||||
|
||||
for (i=0; i<za->nentry; i++) {
|
||||
if ((za->entry[i].state != ZIP_ST_UNCHANGED)
|
||||
|| (za->entry[i].ch_comment_len != -1))
|
||||
changed = 1;
|
||||
if (za->entry[i].state != ZIP_ST_DELETED)
|
||||
survivors++;
|
||||
}
|
||||
|
||||
*survivorsp = survivors;
|
||||
|
||||
return changed;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static char *
|
||||
_zip_create_temp_output(struct zip *za, FILE **outp)
|
||||
{
|
||||
char *temp;
|
||||
int tfd;
|
||||
FILE *tfp;
|
||||
|
||||
if ((temp=(char *)malloc(strlen(za->zn)+8)) == NULL) {
|
||||
_zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
sprintf(temp, "%s.XXXXXX", za->zn);
|
||||
|
||||
if ((tfd=mkstemp(temp)) == -1) {
|
||||
_zip_error_set(&za->error, ZIP_ER_TMPOPEN, errno);
|
||||
free(temp);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if ((tfp=fdopen(tfd, "r+b")) == NULL) {
|
||||
_zip_error_set(&za->error, ZIP_ER_TMPOPEN, errno);
|
||||
close(tfd);
|
||||
remove(temp);
|
||||
free(temp);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
*outp = tfp;
|
||||
return temp;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static int
|
||||
_zip_torrentzip_cmp(const void *a, const void *b)
|
||||
{
|
||||
return strcasecmp(((const struct filelist *)a)->name,
|
||||
((const struct filelist *)b)->name);
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
zip_delete.c -- delete file from zip archive
|
||||
Copyright (C) 1999-2007 Dieter Baron and Thomas Klausner
|
||||
|
||||
This file is part of libzip, a library to manipulate ZIP archives.
|
||||
The authors can be contacted at <libzip@nih.at>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. The names of the authors may not be used to endorse or promote
|
||||
products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include "zipint.h"
|
||||
|
||||
|
||||
|
||||
ZIP_EXTERN int
|
||||
zip_delete(struct zip *za, int idx)
|
||||
{
|
||||
if (idx < 0 || idx >= za->nentry) {
|
||||
_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* allow duplicate file names, because the file will
|
||||
* be removed directly afterwards */
|
||||
if (_zip_unchange(za, idx, 1) != 0)
|
||||
return -1;
|
||||
|
||||
za->entry[idx].state = ZIP_ST_DELETED;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,615 @@
|
||||
/*
|
||||
zip_dirent.c -- read directory entry (local or central), clean dirent
|
||||
Copyright (C) 1999-2008 Dieter Baron and Thomas Klausner
|
||||
|
||||
This file is part of libzip, a library to manipulate ZIP archives.
|
||||
The authors can be contacted at <libzip@nih.at>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. The names of the authors may not be used to endorse or promote
|
||||
products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "zipint.h"
|
||||
|
||||
static time_t _zip_d2u_time(int, int);
|
||||
static char *_zip_readfpstr(FILE *, unsigned int, int, struct zip_error *);
|
||||
static char *_zip_readstr(unsigned char **, int, int, struct zip_error *);
|
||||
static void _zip_u2d_time(time_t, unsigned short *, unsigned short *);
|
||||
static void _zip_write2(unsigned short, FILE *);
|
||||
static void _zip_write4(unsigned int, FILE *);
|
||||
|
||||
|
||||
|
||||
void
|
||||
_zip_cdir_free(struct zip_cdir *cd)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (!cd)
|
||||
return;
|
||||
|
||||
for (i=0; i<cd->nentry; i++)
|
||||
_zip_dirent_finalize(cd->entry+i);
|
||||
free(cd->comment);
|
||||
free(cd->entry);
|
||||
free(cd);
|
||||
}
|
||||
|
||||
|
||||
|
||||
int
|
||||
_zip_cdir_grow(struct zip_cdir *cd, int nentry, struct zip_error *error)
|
||||
{
|
||||
struct zip_dirent *entry;
|
||||
|
||||
if (nentry < cd->nentry) {
|
||||
_zip_error_set(error, ZIP_ER_INTERNAL, 0);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((entry=((struct zip_dirent *)
|
||||
realloc(cd->entry, sizeof(*(cd->entry))*nentry))) == NULL) {
|
||||
_zip_error_set(error, ZIP_ER_MEMORY, 0);
|
||||
return -1;
|
||||
}
|
||||
|
||||
cd->nentry = nentry;
|
||||
cd->entry = entry;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
struct zip_cdir *
|
||||
_zip_cdir_new(int nentry, struct zip_error *error)
|
||||
{
|
||||
struct zip_cdir *cd;
|
||||
|
||||
if ((cd=(struct zip_cdir *)malloc(sizeof(*cd))) == NULL) {
|
||||
_zip_error_set(error, ZIP_ER_MEMORY, 0);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if ((cd->entry=(struct zip_dirent *)malloc(sizeof(*(cd->entry))*nentry))
|
||||
== NULL) {
|
||||
_zip_error_set(error, ZIP_ER_MEMORY, 0);
|
||||
free(cd);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* entries must be initialized by caller */
|
||||
|
||||
cd->nentry = nentry;
|
||||
cd->size = cd->offset = 0;
|
||||
cd->comment = NULL;
|
||||
cd->comment_len = 0;
|
||||
|
||||
return cd;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int
|
||||
_zip_cdir_write(struct zip_cdir *cd, FILE *fp, struct zip_error *error)
|
||||
{
|
||||
int i;
|
||||
|
||||
cd->offset = ftello(fp);
|
||||
|
||||
for (i=0; i<cd->nentry; i++) {
|
||||
if (_zip_dirent_write(cd->entry+i, fp, 0, error) != 0)
|
||||
return -1;
|
||||
}
|
||||
|
||||
cd->size = ftello(fp) - cd->offset;
|
||||
|
||||
/* clearerr(fp); */
|
||||
fwrite(EOCD_MAGIC, 1, 4, fp);
|
||||
_zip_write4(0, fp);
|
||||
_zip_write2((unsigned short)cd->nentry, fp);
|
||||
_zip_write2((unsigned short)cd->nentry, fp);
|
||||
_zip_write4(cd->size, fp);
|
||||
_zip_write4(cd->offset, fp);
|
||||
_zip_write2(cd->comment_len, fp);
|
||||
fwrite(cd->comment, 1, cd->comment_len, fp);
|
||||
|
||||
if (ferror(fp)) {
|
||||
_zip_error_set(error, ZIP_ER_WRITE, errno);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void
|
||||
_zip_dirent_finalize(struct zip_dirent *zde)
|
||||
{
|
||||
free(zde->filename);
|
||||
zde->filename = NULL;
|
||||
free(zde->extrafield);
|
||||
zde->extrafield = NULL;
|
||||
free(zde->comment);
|
||||
zde->comment = NULL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void
|
||||
_zip_dirent_init(struct zip_dirent *de)
|
||||
{
|
||||
de->version_madeby = 0;
|
||||
de->version_needed = 20; /* 2.0 */
|
||||
de->bitflags = 0;
|
||||
de->comp_method = 0;
|
||||
de->last_mod = 0;
|
||||
de->crc = 0;
|
||||
de->comp_size = 0;
|
||||
de->uncomp_size = 0;
|
||||
de->filename = NULL;
|
||||
de->filename_len = 0;
|
||||
de->extrafield = NULL;
|
||||
de->extrafield_len = 0;
|
||||
de->comment = NULL;
|
||||
de->comment_len = 0;
|
||||
de->disk_number = 0;
|
||||
de->int_attrib = 0;
|
||||
de->ext_attrib = 0;
|
||||
de->offset = 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* _zip_dirent_read(zde, fp, bufp, left, localp, error):
|
||||
Fills the zip directory entry zde.
|
||||
|
||||
If bufp is non-NULL, data is taken from there and bufp is advanced
|
||||
by the amount of data used; otherwise data is read from fp as needed.
|
||||
|
||||
if leftp is non-NULL, no more bytes than specified by it are used,
|
||||
and *leftp is reduced by the number of bytes used.
|
||||
|
||||
If local != 0, it reads a local header instead of a central
|
||||
directory entry.
|
||||
|
||||
Returns 0 if successful. On error, error is filled in and -1 is
|
||||
returned.
|
||||
|
||||
XXX: leftp and file position undefined on error.
|
||||
*/
|
||||
|
||||
int
|
||||
_zip_dirent_read(struct zip_dirent *zde, FILE *fp,
|
||||
unsigned char **bufp, unsigned int *leftp, int local,
|
||||
struct zip_error *error)
|
||||
{
|
||||
unsigned char buf[CDENTRYSIZE];
|
||||
unsigned char *cur;
|
||||
unsigned short dostime, dosdate;
|
||||
unsigned int size;
|
||||
|
||||
if (local)
|
||||
size = LENTRYSIZE;
|
||||
else
|
||||
size = CDENTRYSIZE;
|
||||
|
||||
if (leftp && (*leftp < size)) {
|
||||
_zip_error_set(error, ZIP_ER_NOZIP, 0);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (bufp) {
|
||||
/* use data from buffer */
|
||||
cur = *bufp;
|
||||
}
|
||||
else {
|
||||
/* read entry from disk */
|
||||
if ((fread(buf, 1, size, fp)<size)) {
|
||||
_zip_error_set(error, ZIP_ER_READ, errno);
|
||||
return -1;
|
||||
}
|
||||
cur = buf;
|
||||
}
|
||||
|
||||
if (memcmp(cur, (local ? LOCAL_MAGIC : CENTRAL_MAGIC), 4) != 0) {
|
||||
_zip_error_set(error, ZIP_ER_NOZIP, 0);
|
||||
return -1;
|
||||
}
|
||||
cur += 4;
|
||||
|
||||
|
||||
/* convert buffercontents to zip_dirent */
|
||||
|
||||
if (!local)
|
||||
zde->version_madeby = _zip_read2(&cur);
|
||||
else
|
||||
zde->version_madeby = 0;
|
||||
zde->version_needed = _zip_read2(&cur);
|
||||
zde->bitflags = _zip_read2(&cur);
|
||||
zde->comp_method = _zip_read2(&cur);
|
||||
|
||||
/* convert to time_t */
|
||||
dostime = _zip_read2(&cur);
|
||||
dosdate = _zip_read2(&cur);
|
||||
zde->last_mod = _zip_d2u_time(dostime, dosdate);
|
||||
|
||||
zde->crc = _zip_read4(&cur);
|
||||
zde->comp_size = _zip_read4(&cur);
|
||||
zde->uncomp_size = _zip_read4(&cur);
|
||||
|
||||
zde->filename_len = _zip_read2(&cur);
|
||||
zde->extrafield_len = _zip_read2(&cur);
|
||||
|
||||
if (local) {
|
||||
zde->comment_len = 0;
|
||||
zde->disk_number = 0;
|
||||
zde->int_attrib = 0;
|
||||
zde->ext_attrib = 0;
|
||||
zde->offset = 0;
|
||||
} else {
|
||||
zde->comment_len = _zip_read2(&cur);
|
||||
zde->disk_number = _zip_read2(&cur);
|
||||
zde->int_attrib = _zip_read2(&cur);
|
||||
zde->ext_attrib = _zip_read4(&cur);
|
||||
zde->offset = _zip_read4(&cur);
|
||||
}
|
||||
|
||||
zde->filename = NULL;
|
||||
zde->extrafield = NULL;
|
||||
zde->comment = NULL;
|
||||
|
||||
size += zde->filename_len+zde->extrafield_len+zde->comment_len;
|
||||
|
||||
if (leftp && (*leftp < size)) {
|
||||
_zip_error_set(error, ZIP_ER_NOZIP, 0);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (bufp) {
|
||||
if (zde->filename_len) {
|
||||
zde->filename = _zip_readstr(&cur, zde->filename_len, 1, error);
|
||||
if (!zde->filename)
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (zde->extrafield_len) {
|
||||
zde->extrafield = _zip_readstr(&cur, zde->extrafield_len, 0,
|
||||
error);
|
||||
if (!zde->extrafield)
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (zde->comment_len) {
|
||||
zde->comment = _zip_readstr(&cur, zde->comment_len, 0, error);
|
||||
if (!zde->comment)
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (zde->filename_len) {
|
||||
zde->filename = _zip_readfpstr(fp, zde->filename_len, 1, error);
|
||||
if (!zde->filename)
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (zde->extrafield_len) {
|
||||
zde->extrafield = _zip_readfpstr(fp, zde->extrafield_len, 0,
|
||||
error);
|
||||
if (!zde->extrafield)
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (zde->comment_len) {
|
||||
zde->comment = _zip_readfpstr(fp, zde->comment_len, 0, error);
|
||||
if (!zde->comment)
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (bufp)
|
||||
*bufp = cur;
|
||||
if (leftp)
|
||||
*leftp -= size;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* _zip_dirent_torrent_normalize(de);
|
||||
Set values suitable for torrentzip.
|
||||
*/
|
||||
|
||||
void
|
||||
_zip_dirent_torrent_normalize(struct zip_dirent *de)
|
||||
{
|
||||
static struct tm torrenttime;
|
||||
static time_t last_mod = 0;
|
||||
|
||||
if (last_mod == 0) {
|
||||
#ifdef HAVE_STRUCT_TM_TM_ZONE
|
||||
time_t now;
|
||||
struct tm *l;
|
||||
#endif
|
||||
|
||||
torrenttime.tm_sec = 0;
|
||||
torrenttime.tm_min = 32;
|
||||
torrenttime.tm_hour = 23;
|
||||
torrenttime.tm_mday = 24;
|
||||
torrenttime.tm_mon = 11;
|
||||
torrenttime.tm_year = 96;
|
||||
torrenttime.tm_wday = 0;
|
||||
torrenttime.tm_yday = 0;
|
||||
torrenttime.tm_isdst = 0;
|
||||
|
||||
#ifdef HAVE_STRUCT_TM_TM_ZONE
|
||||
time(&now);
|
||||
l = localtime(&now);
|
||||
torrenttime.tm_gmtoff = l->tm_gmtoff;
|
||||
torrenttime.tm_zone = l->tm_zone;
|
||||
#endif
|
||||
|
||||
last_mod = mktime(&torrenttime);
|
||||
}
|
||||
|
||||
de->version_madeby = 0;
|
||||
de->version_needed = 20; /* 2.0 */
|
||||
de->bitflags = 2; /* maximum compression */
|
||||
de->comp_method = ZIP_CM_DEFLATE;
|
||||
de->last_mod = last_mod;
|
||||
|
||||
de->disk_number = 0;
|
||||
de->int_attrib = 0;
|
||||
de->ext_attrib = 0;
|
||||
de->offset = 0;
|
||||
|
||||
free(de->extrafield);
|
||||
de->extrafield = NULL;
|
||||
de->extrafield_len = 0;
|
||||
free(de->comment);
|
||||
de->comment = NULL;
|
||||
de->comment_len = 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* _zip_dirent_write(zde, fp, localp, error):
|
||||
Writes zip directory entry zde to file fp.
|
||||
|
||||
If localp != 0, it writes a local header instead of a central
|
||||
directory entry.
|
||||
|
||||
Returns 0 if successful. On error, error is filled in and -1 is
|
||||
returned.
|
||||
*/
|
||||
|
||||
int
|
||||
_zip_dirent_write(struct zip_dirent *zde, FILE *fp, int localp,
|
||||
struct zip_error *error)
|
||||
{
|
||||
unsigned short dostime, dosdate;
|
||||
|
||||
fwrite(localp ? LOCAL_MAGIC : CENTRAL_MAGIC, 1, 4, fp);
|
||||
|
||||
if (!localp)
|
||||
_zip_write2(zde->version_madeby, fp);
|
||||
_zip_write2(zde->version_needed, fp);
|
||||
_zip_write2(zde->bitflags, fp);
|
||||
_zip_write2(zde->comp_method, fp);
|
||||
|
||||
_zip_u2d_time(zde->last_mod, &dostime, &dosdate);
|
||||
_zip_write2(dostime, fp);
|
||||
_zip_write2(dosdate, fp);
|
||||
|
||||
_zip_write4(zde->crc, fp);
|
||||
_zip_write4(zde->comp_size, fp);
|
||||
_zip_write4(zde->uncomp_size, fp);
|
||||
|
||||
_zip_write2(zde->filename_len, fp);
|
||||
_zip_write2(zde->extrafield_len, fp);
|
||||
|
||||
if (!localp) {
|
||||
_zip_write2(zde->comment_len, fp);
|
||||
_zip_write2(zde->disk_number, fp);
|
||||
_zip_write2(zde->int_attrib, fp);
|
||||
_zip_write4(zde->ext_attrib, fp);
|
||||
_zip_write4(zde->offset, fp);
|
||||
}
|
||||
|
||||
if (zde->filename_len)
|
||||
fwrite(zde->filename, 1, zde->filename_len, fp);
|
||||
|
||||
if (zde->extrafield_len)
|
||||
fwrite(zde->extrafield, 1, zde->extrafield_len, fp);
|
||||
|
||||
if (!localp) {
|
||||
if (zde->comment_len)
|
||||
fwrite(zde->comment, 1, zde->comment_len, fp);
|
||||
}
|
||||
|
||||
if (ferror(fp)) {
|
||||
_zip_error_set(error, ZIP_ER_WRITE, errno);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static time_t
|
||||
_zip_d2u_time(int dtime, int ddate)
|
||||
{
|
||||
struct tm tm;
|
||||
|
||||
memset(&tm, 0, sizeof(tm));
|
||||
|
||||
/* let mktime decide if DST is in effect */
|
||||
tm.tm_isdst = -1;
|
||||
|
||||
tm.tm_year = ((ddate>>9)&127) + 1980 - 1900;
|
||||
tm.tm_mon = ((ddate>>5)&15) - 1;
|
||||
tm.tm_mday = ddate&31;
|
||||
|
||||
tm.tm_hour = (dtime>>11)&31;
|
||||
tm.tm_min = (dtime>>5)&63;
|
||||
tm.tm_sec = (dtime<<1)&62;
|
||||
|
||||
return mktime(&tm);
|
||||
}
|
||||
|
||||
|
||||
|
||||
unsigned short
|
||||
_zip_read2(unsigned char **a)
|
||||
{
|
||||
unsigned short ret;
|
||||
|
||||
ret = (*a)[0]+((*a)[1]<<8);
|
||||
*a += 2;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
unsigned int
|
||||
_zip_read4(unsigned char **a)
|
||||
{
|
||||
unsigned int ret;
|
||||
|
||||
ret = ((((((*a)[3]<<8)+(*a)[2])<<8)+(*a)[1])<<8)+(*a)[0];
|
||||
*a += 4;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static char *
|
||||
_zip_readfpstr(FILE *fp, unsigned int len, int nulp, struct zip_error *error)
|
||||
{
|
||||
char *r, *o;
|
||||
|
||||
r = (char *)malloc(nulp ? len+1 : len);
|
||||
if (!r) {
|
||||
_zip_error_set(error, ZIP_ER_MEMORY, 0);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (fread(r, 1, len, fp)<len) {
|
||||
free(r);
|
||||
_zip_error_set(error, ZIP_ER_READ, errno);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (nulp) {
|
||||
/* replace any in-string NUL characters with spaces */
|
||||
r[len] = 0;
|
||||
for (o=r; o<r+len; o++)
|
||||
if (*o == '\0')
|
||||
*o = ' ';
|
||||
}
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static char *
|
||||
_zip_readstr(unsigned char **buf, int len, int nulp, struct zip_error *error)
|
||||
{
|
||||
char *r, *o;
|
||||
|
||||
r = (char *)malloc(nulp ? len+1 : len);
|
||||
if (!r) {
|
||||
_zip_error_set(error, ZIP_ER_MEMORY, 0);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memcpy(r, *buf, len);
|
||||
*buf += len;
|
||||
|
||||
if (nulp) {
|
||||
/* replace any in-string NUL characters with spaces */
|
||||
r[len] = 0;
|
||||
for (o=r; o<r+len; o++)
|
||||
if (*o == '\0')
|
||||
*o = ' ';
|
||||
}
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void
|
||||
_zip_write2(unsigned short i, FILE *fp)
|
||||
{
|
||||
putc(i&0xff, fp);
|
||||
putc((i>>8)&0xff, fp);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void
|
||||
_zip_write4(unsigned int i, FILE *fp)
|
||||
{
|
||||
putc(i&0xff, fp);
|
||||
putc((i>>8)&0xff, fp);
|
||||
putc((i>>16)&0xff, fp);
|
||||
putc((i>>24)&0xff, fp);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void
|
||||
_zip_u2d_time(time_t time, unsigned short *dtime, unsigned short *ddate)
|
||||
{
|
||||
struct tm *tm;
|
||||
|
||||
tm = localtime(&time);
|
||||
*ddate = ((tm->tm_year+1900-1980)<<9) + ((tm->tm_mon+1)<<5)
|
||||
+ tm->tm_mday;
|
||||
*dtime = ((tm->tm_hour)<<11) + ((tm->tm_min)<<5)
|
||||
+ ((tm->tm_sec)>>1);
|
||||
|
||||
return;
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
zip_entry_free.c -- free struct zip_entry
|
||||
Copyright (C) 1999-2007 Dieter Baron and Thomas Klausner
|
||||
|
||||
This file is part of libzip, a library to manipulate ZIP archives.
|
||||
The authors can be contacted at <libzip@nih.at>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. The names of the authors may not be used to endorse or promote
|
||||
products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "zipint.h"
|
||||
|
||||
|
||||
|
||||
void
|
||||
_zip_entry_free(struct zip_entry *ze)
|
||||
{
|
||||
free(ze->ch_filename);
|
||||
ze->ch_filename = NULL;
|
||||
free(ze->ch_comment);
|
||||
ze->ch_comment = NULL;
|
||||
ze->ch_comment_len = -1;
|
||||
|
||||
_zip_unchange_data(ze);
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
zip_entry_new.c -- create and init struct zip_entry
|
||||
Copyright (C) 1999-2007 Dieter Baron and Thomas Klausner
|
||||
|
||||
This file is part of libzip, a library to manipulate ZIP archives.
|
||||
The authors can be contacted at <libzip@nih.at>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. The names of the authors may not be used to endorse or promote
|
||||
products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "zipint.h"
|
||||
|
||||
|
||||
|
||||
struct zip_entry *
|
||||
_zip_entry_new(struct zip *za)
|
||||
{
|
||||
struct zip_entry *ze;
|
||||
if (!za) {
|
||||
ze = (struct zip_entry *)malloc(sizeof(struct zip_entry));
|
||||
if (!ze) {
|
||||
_zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (za->nentry >= za->nentry_alloc-1) {
|
||||
za->nentry_alloc += 16;
|
||||
za->entry = (struct zip_entry *)realloc(za->entry,
|
||||
sizeof(struct zip_entry)
|
||||
* za->nentry_alloc);
|
||||
if (!za->entry) {
|
||||
_zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
ze = za->entry+za->nentry;
|
||||
}
|
||||
|
||||
ze->state = ZIP_ST_UNCHANGED;
|
||||
|
||||
ze->ch_filename = NULL;
|
||||
ze->ch_comment = NULL;
|
||||
ze->ch_comment_len = -1;
|
||||
ze->source = NULL;
|
||||
|
||||
if (za)
|
||||
za->nentry++;
|
||||
|
||||
return ze;
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
This file was generated automatically by ./make_zip_err_str.sh
|
||||
from ./zip.h; make changes there.
|
||||
*/
|
||||
|
||||
#include "zipint.h"
|
||||
|
||||
|
||||
|
||||
const char * const _zip_err_str[] = {
|
||||
"No error",
|
||||
"Multi-disk zip archives not supported",
|
||||
"Renaming temporary file failed",
|
||||
"Closing zip archive failed",
|
||||
"Seek error",
|
||||
"Read error",
|
||||
"Write error",
|
||||
"CRC error",
|
||||
"Containing zip archive was closed",
|
||||
"No such file",
|
||||
"File already exists",
|
||||
"Can't open file",
|
||||
"Failure to create temporary file",
|
||||
"Zlib error",
|
||||
"Malloc failure",
|
||||
"Entry has been changed",
|
||||
"Compression method not supported",
|
||||
"Premature EOF",
|
||||
"Invalid argument",
|
||||
"Not a zip archive",
|
||||
"Internal error",
|
||||
"Zip archive inconsistent",
|
||||
"Can't remove file",
|
||||
"Entry has been deleted",
|
||||
};
|
||||
|
||||
const int _zip_nerr_str = sizeof(_zip_err_str)/sizeof(_zip_err_str[0]);
|
||||
|
||||
#define N ZIP_ET_NONE
|
||||
#define S ZIP_ET_SYS
|
||||
#define Z ZIP_ET_ZLIB
|
||||
|
||||
const int _zip_err_type[] = {
|
||||
N,
|
||||
N,
|
||||
S,
|
||||
S,
|
||||
S,
|
||||
S,
|
||||
S,
|
||||
N,
|
||||
N,
|
||||
N,
|
||||
N,
|
||||
S,
|
||||
S,
|
||||
Z,
|
||||
N,
|
||||
N,
|
||||
N,
|
||||
N,
|
||||
N,
|
||||
N,
|
||||
N,
|
||||
N,
|
||||
S,
|
||||
N,
|
||||
};
|
||||
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
zip_error.c -- struct zip_error helper functions
|
||||
Copyright (C) 1999-2007 Dieter Baron and Thomas Klausner
|
||||
|
||||
This file is part of libzip, a library to manipulate ZIP archives.
|
||||
The authors can be contacted at <libzip@nih.at>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. The names of the authors may not be used to endorse or promote
|
||||
products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "zipint.h"
|
||||
|
||||
|
||||
|
||||
void
|
||||
_zip_error_clear(struct zip_error *err)
|
||||
{
|
||||
err->zip_err = ZIP_ER_OK;
|
||||
err->sys_err = 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void
|
||||
_zip_error_copy(struct zip_error *dst, struct zip_error *src)
|
||||
{
|
||||
dst->zip_err = src->zip_err;
|
||||
dst->sys_err = src->sys_err;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void
|
||||
_zip_error_fini(struct zip_error *err)
|
||||
{
|
||||
free(err->str);
|
||||
err->str = NULL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void
|
||||
_zip_error_get(struct zip_error *err, int *zep, int *sep)
|
||||
{
|
||||
if (zep)
|
||||
*zep = err->zip_err;
|
||||
if (sep) {
|
||||
if (zip_error_get_sys_type(err->zip_err) != ZIP_ET_NONE)
|
||||
*sep = err->sys_err;
|
||||
else
|
||||
*sep = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void
|
||||
_zip_error_init(struct zip_error *err)
|
||||
{
|
||||
err->zip_err = ZIP_ER_OK;
|
||||
err->sys_err = 0;
|
||||
err->str = NULL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void
|
||||
_zip_error_set(struct zip_error *err, int ze, int se)
|
||||
{
|
||||
if (err) {
|
||||
err->zip_err = ze;
|
||||
err->sys_err = se;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
zip_error_clear.c -- clear zip error
|
||||
Copyright (C) 1999-2007 Dieter Baron and Thomas Klausner
|
||||
|
||||
This file is part of libzip, a library to manipulate ZIP archives.
|
||||
The authors can be contacted at <libzip@nih.at>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. The names of the authors may not be used to endorse or promote
|
||||
products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include "zipint.h"
|
||||
|
||||
|
||||
|
||||
ZIP_EXTERN void
|
||||
zip_error_clear(struct zip *za)
|
||||
{
|
||||
_zip_error_clear(&za->error);
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
zip_error_get.c -- get zip error
|
||||
Copyright (C) 1999-2007 Dieter Baron and Thomas Klausner
|
||||
|
||||
This file is part of libzip, a library to manipulate ZIP archives.
|
||||
The authors can be contacted at <libzip@nih.at>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. The names of the authors may not be used to endorse or promote
|
||||
products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include "zipint.h"
|
||||
|
||||
|
||||
|
||||
ZIP_EXTERN void
|
||||
zip_error_get(struct zip *za, int *zep, int *sep)
|
||||
{
|
||||
_zip_error_get(&za->error, zep, sep);
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
zip_error_get_sys_type.c -- return type of system error code
|
||||
Copyright (C) 1999-2007 Dieter Baron and Thomas Klausner
|
||||
|
||||
This file is part of libzip, a library to manipulate ZIP archives.
|
||||
The authors can be contacted at <libzip@nih.at>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. The names of the authors may not be used to endorse or promote
|
||||
products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include "zipint.h"
|
||||
|
||||
|
||||
|
||||
ZIP_EXTERN int
|
||||
zip_error_get_sys_type(int ze)
|
||||
{
|
||||
if (ze < 0 || ze >= _zip_nerr_str)
|
||||
return 0;
|
||||
|
||||
return _zip_err_type[ze];
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
zip_error_sterror.c -- get string representation of struct zip_error
|
||||
Copyright (C) 1999-2007 Dieter Baron and Thomas Klausner
|
||||
|
||||
This file is part of libzip, a library to manipulate ZIP archives.
|
||||
The authors can be contacted at <libzip@nih.at>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. The names of the authors may not be used to endorse or promote
|
||||
products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "zipint.h"
|
||||
|
||||
|
||||
|
||||
const char *
|
||||
_zip_error_strerror(struct zip_error *err)
|
||||
{
|
||||
const char *zs, *ss;
|
||||
char buf[128], *s;
|
||||
|
||||
_zip_error_fini(err);
|
||||
|
||||
if (err->zip_err < 0 || err->zip_err >= _zip_nerr_str) {
|
||||
sprintf(buf, "Unknown error %d", err->zip_err);
|
||||
zs = NULL;
|
||||
ss = buf;
|
||||
}
|
||||
else {
|
||||
zs = _zip_err_str[err->zip_err];
|
||||
|
||||
switch (_zip_err_type[err->zip_err]) {
|
||||
case ZIP_ET_SYS:
|
||||
ss = strerror(err->sys_err);
|
||||
break;
|
||||
|
||||
case ZIP_ET_ZLIB:
|
||||
ss = zError(err->sys_err);
|
||||
break;
|
||||
|
||||
default:
|
||||
ss = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
if (ss == NULL)
|
||||
return zs;
|
||||
else {
|
||||
if ((s=(char *)malloc(strlen(ss)
|
||||
+ (zs ? strlen(zs)+2 : 0) + 1)) == NULL)
|
||||
return _zip_err_str[ZIP_ER_MEMORY];
|
||||
|
||||
sprintf(s, "%s%s%s",
|
||||
(zs ? zs : ""),
|
||||
(zs ? ": " : ""),
|
||||
ss);
|
||||
err->str = s;
|
||||
|
||||
return s;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
zip_error_to_str.c -- get string representation of zip error code
|
||||
Copyright (C) 1999-2007 Dieter Baron and Thomas Klausner
|
||||
|
||||
This file is part of libzip, a library to manipulate ZIP archives.
|
||||
The authors can be contacted at <libzip@nih.at>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. The names of the authors may not be used to endorse or promote
|
||||
products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "zipint.h"
|
||||
|
||||
|
||||
|
||||
ZIP_EXTERN int
|
||||
zip_error_to_str(char *buf, size_t len, int ze, int se)
|
||||
{
|
||||
const char *zs, *ss;
|
||||
|
||||
if (ze < 0 || ze >= _zip_nerr_str)
|
||||
return snprintf(buf, len, "Unknown error %d", ze);
|
||||
|
||||
zs = _zip_err_str[ze];
|
||||
|
||||
switch (_zip_err_type[ze]) {
|
||||
case ZIP_ET_SYS:
|
||||
ss = strerror(se);
|
||||
break;
|
||||
|
||||
case ZIP_ET_ZLIB:
|
||||
ss = zError(se);
|
||||
break;
|
||||
|
||||
default:
|
||||
ss = NULL;
|
||||
}
|
||||
|
||||
return snprintf(buf, len, "%s%s%s",
|
||||
zs, (ss ? ": " : ""), (ss ? ss : ""));
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
zip_fclose.c -- close file in zip archive
|
||||
Copyright (C) 1999-2007 Dieter Baron and Thomas Klausner
|
||||
|
||||
This file is part of libzip, a library to manipulate ZIP archives.
|
||||
The authors can be contacted at <libzip@nih.at>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. The names of the authors may not be used to endorse or promote
|
||||
products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "zipint.h"
|
||||
|
||||
|
||||
|
||||
ZIP_EXTERN int
|
||||
zip_fclose(struct zip_file *zf)
|
||||
{
|
||||
int i, ret;
|
||||
|
||||
if (zf->zstr)
|
||||
inflateEnd(zf->zstr);
|
||||
free(zf->buffer);
|
||||
free(zf->zstr);
|
||||
|
||||
for (i=0; i<zf->za->nfile; i++) {
|
||||
if (zf->za->file[i] == zf) {
|
||||
zf->za->file[i] = zf->za->file[zf->za->nfile-1];
|
||||
zf->za->nfile--;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
ret = 0;
|
||||
if (zf->error.zip_err)
|
||||
ret = zf->error.zip_err;
|
||||
else if ((zf->flags & ZIP_ZF_CRC) && (zf->flags & ZIP_ZF_EOF)) {
|
||||
/* if EOF, compare CRC */
|
||||
if (zf->crc_orig != zf->crc)
|
||||
ret = ZIP_ER_CRC;
|
||||
}
|
||||
|
||||
free(zf);
|
||||
return ret;
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
zip_file_error_clear.c -- clear zip file error
|
||||
Copyright (C) 1999-2007 Dieter Baron and Thomas Klausner
|
||||
|
||||
This file is part of libzip, a library to manipulate ZIP archives.
|
||||
The authors can be contacted at <libzip@nih.at>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. The names of the authors may not be used to endorse or promote
|
||||
products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include "zipint.h"
|
||||
|
||||
|
||||
|
||||
ZIP_EXTERN void
|
||||
zip_file_error_clear(struct zip_file *zf)
|
||||
{
|
||||
_zip_error_clear(&zf->error);
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
zip_file_error_get.c -- get zip file error
|
||||
Copyright (C) 1999-2007 Dieter Baron and Thomas Klausner
|
||||
|
||||
This file is part of libzip, a library to manipulate ZIP archives.
|
||||
The authors can be contacted at <libzip@nih.at>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. The names of the authors may not be used to endorse or promote
|
||||
products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include "zipint.h"
|
||||
|
||||
|
||||
|
||||
ZIP_EXTERN void
|
||||
zip_file_error_get(struct zip_file *zf, int *zep, int *sep)
|
||||
{
|
||||
_zip_error_get(&zf->error, zep, sep);
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
zip_file_get_offset.c -- get offset of file data in archive.
|
||||
Copyright (C) 1999-2007 Dieter Baron and Thomas Klausner
|
||||
|
||||
This file is part of libzip, a library to manipulate ZIP archives.
|
||||
The authors can be contacted at <libzip@nih.at>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. The names of the authors may not be used to endorse or promote
|
||||
products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "zipint.h"
|
||||
|
||||
|
||||
|
||||
/* _zip_file_get_offset(za, ze):
|
||||
Returns the offset of the file data for entry ze.
|
||||
|
||||
On error, fills in za->error and returns 0.
|
||||
*/
|
||||
|
||||
unsigned int
|
||||
_zip_file_get_offset(struct zip *za, int idx)
|
||||
{
|
||||
struct zip_dirent de;
|
||||
unsigned int offset;
|
||||
|
||||
offset = za->cdir->entry[idx].offset;
|
||||
|
||||
if (fseeko(za->zp, offset, SEEK_SET) != 0) {
|
||||
_zip_error_set(&za->error, ZIP_ER_SEEK, errno);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (_zip_dirent_read(&de, za->zp, NULL, NULL, 1, &za->error) != 0)
|
||||
return 0;
|
||||
|
||||
offset += LENTRYSIZE + de.filename_len + de.extrafield_len;
|
||||
|
||||
_zip_dirent_finalize(&de);
|
||||
|
||||
return offset;
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
zip_file_sterror.c -- get string representation of zip file error
|
||||
Copyright (C) 1999-2007 Dieter Baron and Thomas Klausner
|
||||
|
||||
This file is part of libzip, a library to manipulate ZIP archives.
|
||||
The authors can be contacted at <libzip@nih.at>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. The names of the authors may not be used to endorse or promote
|
||||
products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include "zipint.h"
|
||||
|
||||
|
||||
|
||||
ZIP_EXTERN const char *
|
||||
zip_file_strerror(struct zip_file *zf)
|
||||
{
|
||||
return _zip_error_strerror(&zf->error);
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
zip_filerange_crc.c -- compute CRC32 for a range of a file
|
||||
Copyright (C) 2008 Dieter Baron and Thomas Klausner
|
||||
|
||||
This file is part of libzip, a library to manipulate ZIP archives.
|
||||
The authors can be contacted at <libzip@nih.at>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. The names of the authors may not be used to endorse or promote
|
||||
products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "zipint.h"
|
||||
|
||||
|
||||
|
||||
|
||||
int
|
||||
_zip_filerange_crc(FILE *fp, off_t start, off_t len, uLong *crcp,
|
||||
struct zip_error *errp)
|
||||
{
|
||||
Bytef buf[BUFSIZE];
|
||||
size_t n;
|
||||
|
||||
*crcp = crc32(0L, Z_NULL, 0);
|
||||
|
||||
if (fseeko(fp, start, SEEK_SET) != 0) {
|
||||
_zip_error_set(errp, ZIP_ER_SEEK, errno);
|
||||
return -1;
|
||||
}
|
||||
|
||||
while (len > 0) {
|
||||
n = len > BUFSIZE ? BUFSIZE : len;
|
||||
if ((n=fread(buf, 1, n, fp)) <= 0) {
|
||||
_zip_error_set(errp, ZIP_ER_READ, errno);
|
||||
return -1;
|
||||
}
|
||||
|
||||
*crcp = crc32(*crcp, buf, n);
|
||||
|
||||
len-= n;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
zip_fopen.c -- open file in zip archive for reading
|
||||
Copyright (C) 1999-2007 Dieter Baron and Thomas Klausner
|
||||
|
||||
This file is part of libzip, a library to manipulate ZIP archives.
|
||||
The authors can be contacted at <libzip@nih.at>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. The names of the authors may not be used to endorse or promote
|
||||
products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include "zipint.h"
|
||||
|
||||
|
||||
|
||||
ZIP_EXTERN struct zip_file *
|
||||
zip_fopen(struct zip *za, const char *fname, int flags)
|
||||
{
|
||||
int idx;
|
||||
|
||||
if ((idx=zip_name_locate(za, fname, flags)) < 0)
|
||||
return NULL;
|
||||
|
||||
return zip_fopen_index(za, idx, flags);
|
||||
}
|
||||
@@ -0,0 +1,216 @@
|
||||
/*
|
||||
zip_fopen_index.c -- open file in zip archive for reading by index
|
||||
Copyright (C) 1999-2007 Dieter Baron and Thomas Klausner
|
||||
|
||||
This file is part of libzip, a library to manipulate ZIP archives.
|
||||
The authors can be contacted at <libzip@nih.at>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. The names of the authors may not be used to endorse or promote
|
||||
products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "zipint.h"
|
||||
|
||||
static struct zip_file *_zip_file_new(struct zip *za);
|
||||
|
||||
|
||||
|
||||
ZIP_EXTERN struct zip_file *
|
||||
zip_fopen_index(struct zip *za, int fileno, int flags)
|
||||
{
|
||||
int len, ret;
|
||||
int zfflags;
|
||||
struct zip_file *zf;
|
||||
|
||||
if ((fileno < 0) || (fileno >= za->nentry)) {
|
||||
_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if ((flags & ZIP_FL_UNCHANGED) == 0
|
||||
&& ZIP_ENTRY_DATA_CHANGED(za->entry+fileno)) {
|
||||
_zip_error_set(&za->error, ZIP_ER_CHANGED, 0);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (fileno >= za->cdir->nentry) {
|
||||
_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
zfflags = 0;
|
||||
switch (za->cdir->entry[fileno].comp_method) {
|
||||
case ZIP_CM_STORE:
|
||||
zfflags |= ZIP_ZF_CRC;
|
||||
break;
|
||||
|
||||
case ZIP_CM_DEFLATE:
|
||||
if ((flags & ZIP_FL_COMPRESSED) == 0)
|
||||
zfflags |= ZIP_ZF_CRC | ZIP_ZF_DECOMP;
|
||||
break;
|
||||
default:
|
||||
if ((flags & ZIP_FL_COMPRESSED) == 0) {
|
||||
_zip_error_set(&za->error, ZIP_ER_COMPNOTSUPP, 0);
|
||||
return NULL;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
zf = _zip_file_new(za);
|
||||
|
||||
zf->flags = zfflags;
|
||||
/* zf->name = za->cdir->entry[fileno].filename; */
|
||||
zf->method = za->cdir->entry[fileno].comp_method;
|
||||
zf->bytes_left = za->cdir->entry[fileno].uncomp_size;
|
||||
zf->cbytes_left = za->cdir->entry[fileno].comp_size;
|
||||
zf->crc_orig = za->cdir->entry[fileno].crc;
|
||||
|
||||
if ((zf->fpos=_zip_file_get_offset(za, fileno)) == 0) {
|
||||
zip_fclose(zf);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if ((zf->flags & ZIP_ZF_DECOMP) == 0)
|
||||
zf->bytes_left = zf->cbytes_left;
|
||||
else {
|
||||
if ((zf->buffer=(char *)malloc(BUFSIZE)) == NULL) {
|
||||
_zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
|
||||
zip_fclose(zf);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
len = _zip_file_fillbuf(zf->buffer, BUFSIZE, zf);
|
||||
if (len <= 0) {
|
||||
_zip_error_copy(&za->error, &zf->error);
|
||||
zip_fclose(zf);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if ((zf->zstr = (z_stream *)malloc(sizeof(z_stream))) == NULL) {
|
||||
_zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
|
||||
zip_fclose(zf);
|
||||
return NULL;
|
||||
}
|
||||
zf->zstr->zalloc = Z_NULL;
|
||||
zf->zstr->zfree = Z_NULL;
|
||||
zf->zstr->opaque = NULL;
|
||||
zf->zstr->next_in = (Bytef *)zf->buffer;
|
||||
zf->zstr->avail_in = len;
|
||||
|
||||
/* negative value to tell zlib that there is no header */
|
||||
if ((ret=inflateInit2(zf->zstr, -MAX_WBITS)) != Z_OK) {
|
||||
_zip_error_set(&za->error, ZIP_ER_ZLIB, ret);
|
||||
zip_fclose(zf);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
return zf;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int
|
||||
_zip_file_fillbuf(void *buf, size_t buflen, struct zip_file *zf)
|
||||
{
|
||||
int i, j;
|
||||
|
||||
if (zf->error.zip_err != ZIP_ER_OK)
|
||||
return -1;
|
||||
|
||||
if ((zf->flags & ZIP_ZF_EOF) || zf->cbytes_left <= 0 || buflen <= 0)
|
||||
return 0;
|
||||
|
||||
if (fseeko(zf->za->zp, zf->fpos, SEEK_SET) < 0) {
|
||||
_zip_error_set(&zf->error, ZIP_ER_SEEK, errno);
|
||||
return -1;
|
||||
}
|
||||
if (buflen < zf->cbytes_left)
|
||||
i = buflen;
|
||||
else
|
||||
i = zf->cbytes_left;
|
||||
|
||||
j = fread(buf, 1, i, zf->za->zp);
|
||||
if (j == 0) {
|
||||
_zip_error_set(&zf->error, ZIP_ER_EOF, 0);
|
||||
j = -1;
|
||||
}
|
||||
else if (j < 0)
|
||||
_zip_error_set(&zf->error, ZIP_ER_READ, errno);
|
||||
else {
|
||||
zf->fpos += j;
|
||||
zf->cbytes_left -= j;
|
||||
}
|
||||
|
||||
return j;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static struct zip_file *
|
||||
_zip_file_new(struct zip *za)
|
||||
{
|
||||
struct zip_file *zf, **file;
|
||||
int n;
|
||||
|
||||
if ((zf=(struct zip_file *)malloc(sizeof(struct zip_file))) == NULL) {
|
||||
_zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (za->nfile >= za->nfile_alloc-1) {
|
||||
n = za->nfile_alloc + 10;
|
||||
file = (struct zip_file **)realloc(za->file,
|
||||
n*sizeof(struct zip_file *));
|
||||
if (file == NULL) {
|
||||
_zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
|
||||
free(zf);
|
||||
return NULL;
|
||||
}
|
||||
za->nfile_alloc = n;
|
||||
za->file = file;
|
||||
}
|
||||
|
||||
za->file[za->nfile++] = zf;
|
||||
|
||||
zf->za = za;
|
||||
_zip_error_init(&zf->error);
|
||||
zf->flags = 0;
|
||||
zf->crc = crc32(0L, Z_NULL, 0);
|
||||
zf->crc_orig = 0;
|
||||
zf->method = -1;
|
||||
zf->bytes_left = zf->cbytes_left = 0;
|
||||
zf->fpos = 0;
|
||||
zf->buffer = NULL;
|
||||
zf->zstr = NULL;
|
||||
|
||||
return zf;
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
/*
|
||||
zip_fread.c -- read from file
|
||||
Copyright (C) 1999-2009 Dieter Baron and Thomas Klausner
|
||||
|
||||
This file is part of libzip, a library to manipulate ZIP archives.
|
||||
The authors can be contacted at <libzip@nih.at>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. The names of the authors may not be used to endorse or promote
|
||||
products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include "zipint.h"
|
||||
|
||||
|
||||
|
||||
ZIP_EXTERN ssize_t
|
||||
zip_fread(struct zip_file *zf, void *outbuf, size_t toread)
|
||||
{
|
||||
int ret;
|
||||
size_t out_before, len;
|
||||
int i;
|
||||
|
||||
if (!zf)
|
||||
return -1;
|
||||
|
||||
if (zf->error.zip_err != 0)
|
||||
return -1;
|
||||
|
||||
if ((zf->flags & ZIP_ZF_EOF) || (toread == 0))
|
||||
return 0;
|
||||
|
||||
if (zf->bytes_left == 0) {
|
||||
zf->flags |= ZIP_ZF_EOF;
|
||||
if (zf->flags & ZIP_ZF_CRC) {
|
||||
if (zf->crc != zf->crc_orig) {
|
||||
_zip_error_set(&zf->error, ZIP_ER_CRC, 0);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
if ((zf->flags & ZIP_ZF_DECOMP) == 0) {
|
||||
ret = _zip_file_fillbuf(outbuf, toread, zf);
|
||||
if (ret > 0) {
|
||||
if (zf->flags & ZIP_ZF_CRC)
|
||||
zf->crc = crc32(zf->crc, (Bytef *)outbuf, ret);
|
||||
zf->bytes_left -= ret;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
zf->zstr->next_out = (Bytef *)outbuf;
|
||||
zf->zstr->avail_out = toread;
|
||||
out_before = zf->zstr->total_out;
|
||||
|
||||
/* endless loop until something has been accomplished */
|
||||
for (;;) {
|
||||
ret = inflate(zf->zstr, Z_SYNC_FLUSH);
|
||||
|
||||
switch (ret) {
|
||||
case Z_STREAM_END:
|
||||
if (zf->zstr->total_out == out_before) {
|
||||
if (zf->crc != zf->crc_orig) {
|
||||
_zip_error_set(&zf->error, ZIP_ER_CRC, 0);
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* fallthrough */
|
||||
|
||||
case Z_OK:
|
||||
len = zf->zstr->total_out - out_before;
|
||||
if (len >= zf->bytes_left || len >= toread) {
|
||||
if (zf->flags & ZIP_ZF_CRC)
|
||||
zf->crc = crc32(zf->crc, (Bytef *)outbuf, len);
|
||||
zf->bytes_left -= len;
|
||||
return len;
|
||||
}
|
||||
break;
|
||||
|
||||
case Z_BUF_ERROR:
|
||||
if (zf->zstr->avail_in == 0) {
|
||||
i = _zip_file_fillbuf(zf->buffer, BUFSIZE, zf);
|
||||
if (i == 0) {
|
||||
_zip_error_set(&zf->error, ZIP_ER_INCONS, 0);
|
||||
return -1;
|
||||
}
|
||||
else if (i < 0)
|
||||
return -1;
|
||||
zf->zstr->next_in = (Bytef *)zf->buffer;
|
||||
zf->zstr->avail_in = i;
|
||||
continue;
|
||||
}
|
||||
/* fallthrough */
|
||||
case Z_NEED_DICT:
|
||||
case Z_DATA_ERROR:
|
||||
case Z_STREAM_ERROR:
|
||||
case Z_MEM_ERROR:
|
||||
_zip_error_set(&zf->error, ZIP_ER_ZLIB, ret);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
zip_free.c -- free struct zip
|
||||
Copyright (C) 1999-2007 Dieter Baron and Thomas Klausner
|
||||
|
||||
This file is part of libzip, a library to manipulate ZIP archives.
|
||||
The authors can be contacted at <libzip@nih.at>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. The names of the authors may not be used to endorse or promote
|
||||
products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "zipint.h"
|
||||
|
||||
|
||||
|
||||
/* _zip_free:
|
||||
frees the space allocated to a zipfile struct, and closes the
|
||||
corresponding file. */
|
||||
|
||||
void
|
||||
_zip_free(struct zip *za)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (za == NULL)
|
||||
return;
|
||||
|
||||
if (za->zn)
|
||||
free(za->zn);
|
||||
|
||||
if (za->zp)
|
||||
fclose(za->zp);
|
||||
|
||||
_zip_cdir_free(za->cdir);
|
||||
|
||||
if (za->entry) {
|
||||
for (i=0; i<za->nentry; i++) {
|
||||
_zip_entry_free(za->entry+i);
|
||||
}
|
||||
free(za->entry);
|
||||
}
|
||||
|
||||
for (i=0; i<za->nfile; i++) {
|
||||
if (za->file[i]->error.zip_err == ZIP_ER_OK) {
|
||||
_zip_error_set(&za->file[i]->error, ZIP_ER_ZIPCLOSED, 0);
|
||||
za->file[i]->za = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
free(za->file);
|
||||
|
||||
free(za);
|
||||
|
||||
return;
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
zip_get_archive_comment.c -- get archive comment
|
||||
Copyright (C) 2006-2007 Dieter Baron and Thomas Klausner
|
||||
|
||||
This file is part of libzip, a library to manipulate ZIP archives.
|
||||
The authors can be contacted at <libzip@nih.at>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. The names of the authors may not be used to endorse or promote
|
||||
products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include "zipint.h"
|
||||
|
||||
|
||||
|
||||
ZIP_EXTERN const char *
|
||||
zip_get_archive_comment(struct zip *za, int *lenp, int flags)
|
||||
{
|
||||
if ((flags & ZIP_FL_UNCHANGED)
|
||||
|| (za->ch_comment_len == -1)) {
|
||||
if (za->cdir) {
|
||||
if (lenp != NULL)
|
||||
*lenp = za->cdir->comment_len;
|
||||
return za->cdir->comment;
|
||||
}
|
||||
else {
|
||||
if (lenp != NULL)
|
||||
*lenp = -1;
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
if (lenp != NULL)
|
||||
*lenp = za->ch_comment_len;
|
||||
return za->ch_comment;
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
zip_get_archive_flag.c -- get archive global flag
|
||||
Copyright (C) 2008 Dieter Baron and Thomas Klausner
|
||||
|
||||
This file is part of libzip, a library to manipulate ZIP archives.
|
||||
The authors can be contacted at <libzip@nih.at>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. The names of the authors may not be used to endorse or promote
|
||||
products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include "zipint.h"
|
||||
|
||||
|
||||
|
||||
ZIP_EXTERN int
|
||||
zip_get_archive_flag(struct zip *za, int flag, int flags)
|
||||
{
|
||||
int fl;
|
||||
|
||||
fl = (flags & ZIP_FL_UNCHANGED) ? za->flags : za->ch_flags;
|
||||
|
||||
return (fl & flag) ? 1 : 0;
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
zip_get_file_comment.c -- get file comment
|
||||
Copyright (C) 2006-2007 Dieter Baron and Thomas Klausner
|
||||
|
||||
This file is part of libzip, a library to manipulate ZIP archives.
|
||||
The authors can be contacted at <libzip@nih.at>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. The names of the authors may not be used to endorse or promote
|
||||
products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include "zipint.h"
|
||||
|
||||
|
||||
|
||||
ZIP_EXTERN const char *
|
||||
zip_get_file_comment(struct zip *za, int idx, int *lenp, int flags)
|
||||
{
|
||||
if (idx < 0 || idx >= za->nentry) {
|
||||
_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if ((flags & ZIP_FL_UNCHANGED)
|
||||
|| (za->entry[idx].ch_comment_len == -1)) {
|
||||
if (lenp != NULL)
|
||||
*lenp = za->cdir->entry[idx].comment_len;
|
||||
return za->cdir->entry[idx].comment;
|
||||
}
|
||||
|
||||
if (lenp != NULL)
|
||||
*lenp = za->entry[idx].ch_comment_len;
|
||||
return za->entry[idx].ch_comment;
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
zip_get_name.c -- get filename for a file in zip file
|
||||
Copyright (C) 1999-2007 Dieter Baron and Thomas Klausner
|
||||
|
||||
This file is part of libzip, a library to manipulate ZIP archives.
|
||||
The authors can be contacted at <libzip@nih.at>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. The names of the authors may not be used to endorse or promote
|
||||
products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include "zipint.h"
|
||||
|
||||
|
||||
|
||||
ZIP_EXTERN const char *
|
||||
zip_get_name(struct zip *za, int idx, int flags)
|
||||
{
|
||||
return _zip_get_name(za, idx, flags, &za->error);
|
||||
}
|
||||
|
||||
|
||||
|
||||
const char *
|
||||
_zip_get_name(struct zip *za, int idx, int flags, struct zip_error *error)
|
||||
{
|
||||
if (idx < 0 || idx >= za->nentry) {
|
||||
_zip_error_set(error, ZIP_ER_INVAL, 0);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if ((flags & ZIP_FL_UNCHANGED) == 0) {
|
||||
if (za->entry[idx].state == ZIP_ST_DELETED) {
|
||||
_zip_error_set(error, ZIP_ER_DELETED, 0);
|
||||
return NULL;
|
||||
}
|
||||
if (za->entry[idx].ch_filename)
|
||||
return za->entry[idx].ch_filename;
|
||||
}
|
||||
|
||||
if (za->cdir == NULL || idx >= za->cdir->nentry) {
|
||||
_zip_error_set(error, ZIP_ER_INVAL, 0);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return za->cdir->entry[idx].filename;
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
zip_get_num_files.c -- get number of files in archive
|
||||
Copyright (C) 1999-2007 Dieter Baron and Thomas Klausner
|
||||
|
||||
This file is part of libzip, a library to manipulate ZIP archives.
|
||||
The authors can be contacted at <libzip@nih.at>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. The names of the authors may not be used to endorse or promote
|
||||
products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include "zipint.h"
|
||||
|
||||
|
||||
|
||||
ZIP_EXTERN int
|
||||
zip_get_num_files(struct zip *za)
|
||||
{
|
||||
if (za == NULL)
|
||||
return -1;
|
||||
|
||||
return za->nentry;
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
zip_memdup.c -- internal zip function, "strdup" with len
|
||||
Copyright (C) 1999-2007 Dieter Baron and Thomas Klausner
|
||||
|
||||
This file is part of libzip, a library to manipulate ZIP archives.
|
||||
The authors can be contacted at <libzip@nih.at>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. The names of the authors may not be used to endorse or promote
|
||||
products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "zipint.h"
|
||||
|
||||
|
||||
|
||||
void *
|
||||
_zip_memdup(const void *mem, size_t len, struct zip_error *error)
|
||||
{
|
||||
void *ret;
|
||||
|
||||
ret = malloc(len);
|
||||
if (!ret) {
|
||||
_zip_error_set(error, ZIP_ER_MEMORY, 0);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memcpy(ret, mem, len);
|
||||
|
||||
return ret;
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
zip_name_locate.c -- get index by name
|
||||
Copyright (C) 1999-2007 Dieter Baron and Thomas Klausner
|
||||
|
||||
This file is part of libzip, a library to manipulate ZIP archives.
|
||||
The authors can be contacted at <libzip@nih.at>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. The names of the authors may not be used to endorse or promote
|
||||
products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "zipint.h"
|
||||
|
||||
|
||||
|
||||
ZIP_EXTERN int
|
||||
zip_name_locate(struct zip *za, const char *fname, int flags)
|
||||
{
|
||||
return _zip_name_locate(za, fname, flags, &za->error);
|
||||
}
|
||||
|
||||
|
||||
|
||||
int
|
||||
_zip_name_locate(struct zip *za, const char *fname, int flags,
|
||||
struct zip_error *error)
|
||||
{
|
||||
int (*cmp)(const char *, const char *);
|
||||
const char *fn, *p;
|
||||
int i, n;
|
||||
|
||||
if (fname == NULL) {
|
||||
_zip_error_set(error, ZIP_ER_INVAL, 0);
|
||||
return -1;
|
||||
}
|
||||
|
||||
cmp = (flags & ZIP_FL_NOCASE) ? strcasecmp : strcmp;
|
||||
|
||||
n = (flags & ZIP_FL_UNCHANGED) ? za->cdir->nentry : za->nentry;
|
||||
for (i=0; i<n; i++) {
|
||||
if (flags & ZIP_FL_UNCHANGED)
|
||||
fn = za->cdir->entry[i].filename;
|
||||
else
|
||||
fn = _zip_get_name(za, i, flags, error);
|
||||
|
||||
/* newly added (partially filled) entry */
|
||||
if (fn == NULL)
|
||||
continue;
|
||||
|
||||
if (flags & ZIP_FL_NODIR) {
|
||||
p = strrchr(fn, '/');
|
||||
if (p)
|
||||
fn = p+1;
|
||||
}
|
||||
|
||||
if (cmp(fname, fn) == 0)
|
||||
return i;
|
||||
}
|
||||
|
||||
_zip_error_set(error, ZIP_ER_NOENT, 0);
|
||||
return -1;
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
zip_new.c -- create and init struct zip
|
||||
Copyright (C) 1999-2007 Dieter Baron and Thomas Klausner
|
||||
|
||||
This file is part of libzip, a library to manipulate ZIP archives.
|
||||
The authors can be contacted at <libzip@nih.at>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. The names of the authors may not be used to endorse or promote
|
||||
products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "zipint.h"
|
||||
|
||||
|
||||
|
||||
/* _zip_new:
|
||||
creates a new zipfile struct, and sets the contents to zero; returns
|
||||
the new struct. */
|
||||
|
||||
struct zip *
|
||||
_zip_new(struct zip_error *error)
|
||||
{
|
||||
struct zip *za;
|
||||
|
||||
za = (struct zip *)malloc(sizeof(struct zip));
|
||||
if (!za) {
|
||||
_zip_error_set(error, ZIP_ER_MEMORY, 0);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
za->zn = NULL;
|
||||
za->zp = NULL;
|
||||
_zip_error_init(&za->error);
|
||||
za->cdir = NULL;
|
||||
za->ch_comment = NULL;
|
||||
za->ch_comment_len = -1;
|
||||
za->nentry = za->nentry_alloc = 0;
|
||||
za->entry = NULL;
|
||||
za->nfile = za->nfile_alloc = 0;
|
||||
za->file = NULL;
|
||||
za->flags = za->ch_flags = 0;
|
||||
|
||||
return za;
|
||||
}
|
||||
@@ -0,0 +1,567 @@
|
||||
/*
|
||||
zip_open.c -- open zip archive
|
||||
Copyright (C) 1999-2008 Dieter Baron and Thomas Klausner
|
||||
|
||||
This file is part of libzip, a library to manipulate ZIP archives.
|
||||
The authors can be contacted at <libzip@nih.at>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. The names of the authors may not be used to endorse or promote
|
||||
products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "zipint.h"
|
||||
|
||||
static void set_error(int *, struct zip_error *, int);
|
||||
static struct zip *_zip_allocate_new(const char *, int *);
|
||||
static int _zip_checkcons(FILE *, struct zip_cdir *, struct zip_error *);
|
||||
static void _zip_check_torrentzip(struct zip *);
|
||||
static struct zip_cdir *_zip_find_central_dir(FILE *, int, int *, off_t);
|
||||
static int _zip_file_exists(const char *, int, int *);
|
||||
static int _zip_headercomp(struct zip_dirent *, int,
|
||||
struct zip_dirent *, int);
|
||||
static unsigned char *_zip_memmem(const unsigned char *, int,
|
||||
const unsigned char *, int);
|
||||
static struct zip_cdir *_zip_readcdir(FILE *, unsigned char *, unsigned char *,
|
||||
int, int, struct zip_error *);
|
||||
|
||||
|
||||
|
||||
ZIP_EXTERN struct zip *
|
||||
zip_open(const char *fn, int flags, int *zep)
|
||||
{
|
||||
FILE *fp;
|
||||
struct zip *za;
|
||||
struct zip_cdir *cdir;
|
||||
int i;
|
||||
off_t len;
|
||||
|
||||
switch (_zip_file_exists(fn, flags, zep)) {
|
||||
case -1:
|
||||
return NULL;
|
||||
case 0:
|
||||
return _zip_allocate_new(fn, zep);
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if ((fp=fopen(fn, "rb")) == NULL) {
|
||||
set_error(zep, NULL, ZIP_ER_OPEN);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fseeko(fp, 0, SEEK_END);
|
||||
len = ftello(fp);
|
||||
|
||||
/* treat empty files as empty archives */
|
||||
if (len == 0) {
|
||||
if ((za=_zip_allocate_new(fn, zep)) == NULL)
|
||||
fclose(fp);
|
||||
else
|
||||
za->zp = fp;
|
||||
return za;
|
||||
}
|
||||
|
||||
cdir = _zip_find_central_dir(fp, flags, zep, len);
|
||||
if (cdir == NULL) {
|
||||
fclose(fp);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if ((za=_zip_allocate_new(fn, zep)) == NULL) {
|
||||
_zip_cdir_free(cdir);
|
||||
fclose(fp);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
za->cdir = cdir;
|
||||
za->zp = fp;
|
||||
|
||||
if ((za->entry=(struct zip_entry *)malloc(sizeof(*(za->entry))
|
||||
* cdir->nentry)) == NULL) {
|
||||
set_error(zep, NULL, ZIP_ER_MEMORY);
|
||||
_zip_free(za);
|
||||
return NULL;
|
||||
}
|
||||
for (i=0; i<cdir->nentry; i++)
|
||||
_zip_entry_new(za);
|
||||
|
||||
_zip_check_torrentzip(za);
|
||||
za->ch_flags = za->flags;
|
||||
|
||||
return za;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void
|
||||
set_error(int *zep, struct zip_error *err, int ze)
|
||||
{
|
||||
int se;
|
||||
|
||||
if (err) {
|
||||
_zip_error_get(err, &ze, &se);
|
||||
if (zip_error_get_sys_type(ze) == ZIP_ET_SYS)
|
||||
errno = se;
|
||||
}
|
||||
|
||||
if (zep)
|
||||
*zep = ze;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* _zip_readcdir:
|
||||
tries to find a valid end-of-central-directory at the beginning of
|
||||
buf, and then the corresponding central directory entries.
|
||||
Returns a struct zip_cdir which contains the central directory
|
||||
entries, or NULL if unsuccessful. */
|
||||
|
||||
static struct zip_cdir *
|
||||
_zip_readcdir(FILE *fp, unsigned char *buf, unsigned char *eocd, int buflen,
|
||||
int flags, struct zip_error *error)
|
||||
{
|
||||
struct zip_cdir *cd;
|
||||
unsigned char *cdp, **bufp;
|
||||
int i, comlen, nentry;
|
||||
unsigned int left;
|
||||
|
||||
comlen = buf + buflen - eocd - EOCDLEN;
|
||||
if (comlen < 0) {
|
||||
/* not enough bytes left for comment */
|
||||
_zip_error_set(error, ZIP_ER_NOZIP, 0);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* check for end-of-central-dir magic */
|
||||
if (memcmp(eocd, EOCD_MAGIC, 4) != 0) {
|
||||
_zip_error_set(error, ZIP_ER_NOZIP, 0);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (memcmp(eocd+4, "\0\0\0\0", 4) != 0) {
|
||||
_zip_error_set(error, ZIP_ER_MULTIDISK, 0);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
cdp = eocd + 8;
|
||||
/* number of cdir-entries on this disk */
|
||||
i = _zip_read2(&cdp);
|
||||
/* number of cdir-entries */
|
||||
nentry = _zip_read2(&cdp);
|
||||
|
||||
if ((cd=_zip_cdir_new(nentry, error)) == NULL)
|
||||
return NULL;
|
||||
|
||||
cd->size = _zip_read4(&cdp);
|
||||
cd->offset = _zip_read4(&cdp);
|
||||
cd->comment = NULL;
|
||||
cd->comment_len = _zip_read2(&cdp);
|
||||
|
||||
if ((comlen < cd->comment_len) || (cd->nentry != i)) {
|
||||
_zip_error_set(error, ZIP_ER_NOZIP, 0);
|
||||
free(cd);
|
||||
return NULL;
|
||||
}
|
||||
if ((flags & ZIP_CHECKCONS) && comlen != cd->comment_len) {
|
||||
_zip_error_set(error, ZIP_ER_INCONS, 0);
|
||||
free(cd);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (cd->comment_len) {
|
||||
if ((cd->comment=(char *)_zip_memdup(eocd+EOCDLEN,
|
||||
cd->comment_len, error))
|
||||
== NULL) {
|
||||
free(cd);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
if (cd->size < (unsigned int)(eocd-buf)) {
|
||||
/* if buffer already read in, use it */
|
||||
cdp = eocd - cd->size;
|
||||
bufp = &cdp;
|
||||
}
|
||||
else {
|
||||
/* go to start of cdir and read it entry by entry */
|
||||
bufp = NULL;
|
||||
clearerr(fp);
|
||||
fseeko(fp, cd->offset, SEEK_SET);
|
||||
/* possible consistency check: cd->offset =
|
||||
len-(cd->size+cd->comment_len+EOCDLEN) ? */
|
||||
if (ferror(fp) || ((unsigned long)ftello(fp) != cd->offset)) {
|
||||
/* seek error or offset of cdir wrong */
|
||||
if (ferror(fp))
|
||||
_zip_error_set(error, ZIP_ER_SEEK, errno);
|
||||
else
|
||||
_zip_error_set(error, ZIP_ER_NOZIP, 0);
|
||||
free(cd);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
left = cd->size;
|
||||
i=0;
|
||||
do {
|
||||
if (i == cd->nentry && left > 0) {
|
||||
/* Infozip extension for more than 64k entries:
|
||||
nentries wraps around, size indicates correct EOCD */
|
||||
_zip_cdir_grow(cd, cd->nentry+0x10000, error);
|
||||
}
|
||||
|
||||
if ((_zip_dirent_read(cd->entry+i, fp, bufp, &left, 0, error)) < 0) {
|
||||
cd->nentry = i;
|
||||
_zip_cdir_free(cd);
|
||||
return NULL;
|
||||
}
|
||||
i++;
|
||||
|
||||
} while (i<cd->nentry);
|
||||
|
||||
return cd;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* _zip_checkcons:
|
||||
Checks the consistency of the central directory by comparing central
|
||||
directory entries with local headers and checking for plausible
|
||||
file and header offsets. Returns -1 if not plausible, else the
|
||||
difference between the lowest and the highest fileposition reached */
|
||||
|
||||
static int
|
||||
_zip_checkcons(FILE *fp, struct zip_cdir *cd, struct zip_error *error)
|
||||
{
|
||||
int i;
|
||||
unsigned int min, max, j;
|
||||
struct zip_dirent temp;
|
||||
|
||||
if (cd->nentry) {
|
||||
max = cd->entry[0].offset;
|
||||
min = cd->entry[0].offset;
|
||||
}
|
||||
else
|
||||
min = max = 0;
|
||||
|
||||
for (i=0; i<cd->nentry; i++) {
|
||||
if (cd->entry[i].offset < min)
|
||||
min = cd->entry[i].offset;
|
||||
if (min > cd->offset) {
|
||||
_zip_error_set(error, ZIP_ER_NOZIP, 0);
|
||||
return -1;
|
||||
}
|
||||
|
||||
j = cd->entry[i].offset + cd->entry[i].comp_size
|
||||
+ cd->entry[i].filename_len + LENTRYSIZE;
|
||||
if (j > max)
|
||||
max = j;
|
||||
if (max > cd->offset) {
|
||||
_zip_error_set(error, ZIP_ER_NOZIP, 0);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (fseeko(fp, cd->entry[i].offset, SEEK_SET) != 0) {
|
||||
_zip_error_set(error, ZIP_ER_SEEK, 0);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (_zip_dirent_read(&temp, fp, NULL, NULL, 1, error) == -1)
|
||||
return -1;
|
||||
|
||||
if (_zip_headercomp(cd->entry+i, 0, &temp, 1) != 0) {
|
||||
_zip_error_set(error, ZIP_ER_INCONS, 0);
|
||||
_zip_dirent_finalize(&temp);
|
||||
return -1;
|
||||
}
|
||||
_zip_dirent_finalize(&temp);
|
||||
}
|
||||
|
||||
return max - min;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* _zip_check_torrentzip:
|
||||
check wether ZA has a valid TORRENTZIP comment, i.e. is torrentzipped */
|
||||
|
||||
static void
|
||||
_zip_check_torrentzip(struct zip *za)
|
||||
{
|
||||
uLong crc_got, crc_should;
|
||||
char buf[8+1];
|
||||
char *end;
|
||||
|
||||
if (za->zp == NULL || za->cdir == NULL)
|
||||
return;
|
||||
|
||||
if (za->cdir->comment_len != TORRENT_SIG_LEN+8
|
||||
|| strncmp(za->cdir->comment, TORRENT_SIG, TORRENT_SIG_LEN) != 0)
|
||||
return;
|
||||
|
||||
memcpy(buf, za->cdir->comment+TORRENT_SIG_LEN, 8);
|
||||
buf[8] = '\0';
|
||||
errno = 0;
|
||||
crc_should = strtoul(buf, &end, 16);
|
||||
if ((crc_should == UINT_MAX && errno != 0) || (end && *end))
|
||||
return;
|
||||
|
||||
if (_zip_filerange_crc(za->zp, za->cdir->offset, za->cdir->size,
|
||||
&crc_got, NULL) < 0)
|
||||
return;
|
||||
|
||||
if (crc_got == crc_should)
|
||||
za->flags |= ZIP_AFL_TORRENT;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/* _zip_headercomp:
|
||||
compares two headers h1 and h2; if they are local headers, set
|
||||
local1p or local2p respectively to 1, else 0. Return 0 if they
|
||||
are identical, -1 if not. */
|
||||
|
||||
static int
|
||||
_zip_headercomp(struct zip_dirent *h1, int local1p, struct zip_dirent *h2,
|
||||
int local2p)
|
||||
{
|
||||
if ((h1->version_needed != h2->version_needed)
|
||||
#if 0
|
||||
/* some zip-files have different values in local
|
||||
and global headers for the bitflags */
|
||||
|| (h1->bitflags != h2->bitflags)
|
||||
#endif
|
||||
|| (h1->comp_method != h2->comp_method)
|
||||
|| (h1->last_mod != h2->last_mod)
|
||||
|| (h1->filename_len != h2->filename_len)
|
||||
|| !h1->filename || !h2->filename
|
||||
|| strcmp(h1->filename, h2->filename))
|
||||
return -1;
|
||||
|
||||
/* check that CRC and sizes are zero if data descriptor is used */
|
||||
if ((h1->bitflags & ZIP_GPBF_DATA_DESCRIPTOR) && local1p
|
||||
&& (h1->crc != 0
|
||||
|| h1->comp_size != 0
|
||||
|| h1->uncomp_size != 0))
|
||||
return -1;
|
||||
if ((h2->bitflags & ZIP_GPBF_DATA_DESCRIPTOR) && local2p
|
||||
&& (h2->crc != 0
|
||||
|| h2->comp_size != 0
|
||||
|| h2->uncomp_size != 0))
|
||||
return -1;
|
||||
|
||||
/* check that CRC and sizes are equal if no data descriptor is used */
|
||||
if (((h1->bitflags & ZIP_GPBF_DATA_DESCRIPTOR) == 0 || local1p == 0)
|
||||
&& ((h2->bitflags & ZIP_GPBF_DATA_DESCRIPTOR) == 0 || local2p == 0)) {
|
||||
if ((h1->crc != h2->crc)
|
||||
|| (h1->comp_size != h2->comp_size)
|
||||
|| (h1->uncomp_size != h2->uncomp_size))
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((local1p == local2p)
|
||||
&& ((h1->extrafield_len != h2->extrafield_len)
|
||||
|| (h1->extrafield_len && h2->extrafield
|
||||
&& memcmp(h1->extrafield, h2->extrafield,
|
||||
h1->extrafield_len))))
|
||||
return -1;
|
||||
|
||||
/* if either is local, nothing more to check */
|
||||
if (local1p || local2p)
|
||||
return 0;
|
||||
|
||||
if ((h1->version_madeby != h2->version_madeby)
|
||||
|| (h1->disk_number != h2->disk_number)
|
||||
|| (h1->int_attrib != h2->int_attrib)
|
||||
|| (h1->ext_attrib != h2->ext_attrib)
|
||||
|| (h1->offset != h2->offset)
|
||||
|| (h1->comment_len != h2->comment_len)
|
||||
|| (h1->comment_len && h2->comment
|
||||
&& memcmp(h1->comment, h2->comment, h1->comment_len)))
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static struct zip *
|
||||
_zip_allocate_new(const char *fn, int *zep)
|
||||
{
|
||||
struct zip *za;
|
||||
struct zip_error error;
|
||||
|
||||
if ((za=_zip_new(&error)) == NULL) {
|
||||
set_error(zep, &error, 0);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
za->zn = strdup(fn);
|
||||
if (!za->zn) {
|
||||
_zip_free(za);
|
||||
set_error(zep, NULL, ZIP_ER_MEMORY);
|
||||
return NULL;
|
||||
}
|
||||
return za;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static int
|
||||
_zip_file_exists(const char *fn, int flags, int *zep)
|
||||
{
|
||||
struct stat st;
|
||||
|
||||
if (fn == NULL) {
|
||||
set_error(zep, NULL, ZIP_ER_INVAL);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (stat(fn, &st) != 0) {
|
||||
if (flags & ZIP_CREATE)
|
||||
return 0;
|
||||
else {
|
||||
set_error(zep, NULL, ZIP_ER_OPEN);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else if ((flags & ZIP_EXCL)) {
|
||||
set_error(zep, NULL, ZIP_ER_EXISTS);
|
||||
return -1;
|
||||
}
|
||||
/* ZIP_CREATE gets ignored if file exists and not ZIP_EXCL,
|
||||
just like open() */
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static struct zip_cdir *
|
||||
_zip_find_central_dir(FILE *fp, int flags, int *zep, off_t len)
|
||||
{
|
||||
struct zip_cdir *cdir, *cdirnew;
|
||||
unsigned char *buf, *match;
|
||||
int a, best, buflen, i;
|
||||
struct zip_error zerr;
|
||||
|
||||
i = fseeko(fp, -(len < CDBUFSIZE ? len : CDBUFSIZE), SEEK_END);
|
||||
if (i == -1 && errno != EFBIG) {
|
||||
/* seek before start of file on my machine */
|
||||
set_error(zep, NULL, ZIP_ER_SEEK);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* 64k is too much for stack */
|
||||
if ((buf=(unsigned char *)malloc(CDBUFSIZE)) == NULL) {
|
||||
set_error(zep, NULL, ZIP_ER_MEMORY);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
clearerr(fp);
|
||||
buflen = fread(buf, 1, CDBUFSIZE, fp);
|
||||
|
||||
if (ferror(fp)) {
|
||||
set_error(zep, NULL, ZIP_ER_READ);
|
||||
free(buf);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
best = -1;
|
||||
cdir = NULL;
|
||||
match = buf;
|
||||
_zip_error_set(&zerr, ZIP_ER_NOZIP, 0);
|
||||
|
||||
while ((match=_zip_memmem(match, buflen-(match-buf)-18,
|
||||
(const unsigned char *)EOCD_MAGIC, 4))!=NULL) {
|
||||
/* found match -- check, if good */
|
||||
/* to avoid finding the same match all over again */
|
||||
match++;
|
||||
if ((cdirnew=_zip_readcdir(fp, buf, match-1, buflen, flags,
|
||||
&zerr)) == NULL)
|
||||
continue;
|
||||
|
||||
if (cdir) {
|
||||
if (best <= 0)
|
||||
best = _zip_checkcons(fp, cdir, &zerr);
|
||||
a = _zip_checkcons(fp, cdirnew, &zerr);
|
||||
if (best < a) {
|
||||
_zip_cdir_free(cdir);
|
||||
cdir = cdirnew;
|
||||
best = a;
|
||||
}
|
||||
else
|
||||
_zip_cdir_free(cdirnew);
|
||||
}
|
||||
else {
|
||||
cdir = cdirnew;
|
||||
if (flags & ZIP_CHECKCONS)
|
||||
best = _zip_checkcons(fp, cdir, &zerr);
|
||||
else
|
||||
best = 0;
|
||||
}
|
||||
cdirnew = NULL;
|
||||
}
|
||||
|
||||
free(buf);
|
||||
|
||||
if (best < 0) {
|
||||
set_error(zep, &zerr, 0);
|
||||
_zip_cdir_free(cdir);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return cdir;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static unsigned char *
|
||||
_zip_memmem(const unsigned char *big, int biglen, const unsigned char *little,
|
||||
int littlelen)
|
||||
{
|
||||
const unsigned char *p;
|
||||
|
||||
if ((biglen < littlelen) || (littlelen == 0))
|
||||
return NULL;
|
||||
p = big-1;
|
||||
while ((p=(const unsigned char *)
|
||||
memchr(p+1, little[0], (size_t)(big-(p+1)+biglen-littlelen+1)))
|
||||
!= NULL) {
|
||||
if (memcmp(p+1, little+1, littlelen-1)==0)
|
||||
return (unsigned char *)p;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
zip_rename.c -- rename file in zip archive
|
||||
Copyright (C) 1999-2008 Dieter Baron and Thomas Klausner
|
||||
|
||||
This file is part of libzip, a library to manipulate ZIP archives.
|
||||
The authors can be contacted at <libzip@nih.at>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. The names of the authors may not be used to endorse or promote
|
||||
products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "zipint.h"
|
||||
|
||||
|
||||
|
||||
ZIP_EXTERN int
|
||||
zip_rename(struct zip *za, int idx, const char *name)
|
||||
{
|
||||
const char *old_name;
|
||||
int old_is_dir, new_is_dir;
|
||||
|
||||
if (idx >= za->nentry || idx < 0 || name[0] == '\0') {
|
||||
_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((old_name=zip_get_name(za, idx, 0)) == NULL)
|
||||
return -1;
|
||||
|
||||
new_is_dir = (name[strlen(name)-1] == '/');
|
||||
old_is_dir = (old_name[strlen(old_name)-1] == '/');
|
||||
|
||||
if (new_is_dir != old_is_dir) {
|
||||
_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return _zip_set_name(za, idx, name);
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
zip_replace.c -- replace file via callback function
|
||||
Copyright (C) 1999-2007 Dieter Baron and Thomas Klausner
|
||||
|
||||
This file is part of libzip, a library to manipulate ZIP archives.
|
||||
The authors can be contacted at <libzip@nih.at>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. The names of the authors may not be used to endorse or promote
|
||||
products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include "zipint.h"
|
||||
|
||||
|
||||
|
||||
ZIP_EXTERN int
|
||||
zip_replace(struct zip *za, int idx, struct zip_source *source)
|
||||
{
|
||||
if (idx < 0 || idx >= za->nentry || source == NULL) {
|
||||
_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (_zip_replace(za, idx, NULL, source) == -1)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
int
|
||||
_zip_replace(struct zip *za, int idx, const char *name,
|
||||
struct zip_source *source)
|
||||
{
|
||||
if (idx == -1) {
|
||||
if (_zip_entry_new(za) == NULL)
|
||||
return -1;
|
||||
|
||||
idx = za->nentry - 1;
|
||||
}
|
||||
|
||||
_zip_unchange_data(za->entry+idx);
|
||||
|
||||
if (name && _zip_set_name(za, idx, name) != 0)
|
||||
return -1;
|
||||
|
||||
za->entry[idx].state = ((za->cdir == NULL || idx >= za->cdir->nentry)
|
||||
? ZIP_ST_ADDED : ZIP_ST_REPLACED);
|
||||
za->entry[idx].source = source;
|
||||
|
||||
return idx;
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
zip_set_archive_comment.c -- set archive comment
|
||||
Copyright (C) 2006-2007 Dieter Baron and Thomas Klausner
|
||||
|
||||
This file is part of libzip, a library to manipulate ZIP archives.
|
||||
The authors can be contacted at <libzip@nih.at>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. The names of the authors may not be used to endorse or promote
|
||||
products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "zipint.h"
|
||||
|
||||
|
||||
|
||||
ZIP_EXTERN int
|
||||
zip_set_archive_comment(struct zip *za, const char *comment, int len)
|
||||
{
|
||||
char *tmpcom;
|
||||
|
||||
if (len < 0 || len > MAXCOMLEN
|
||||
|| (len > 0 && comment == NULL)) {
|
||||
_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (len > 0) {
|
||||
if ((tmpcom=(char *)_zip_memdup(comment, len, &za->error)) == NULL)
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
tmpcom = NULL;
|
||||
|
||||
free(za->ch_comment);
|
||||
za->ch_comment = tmpcom;
|
||||
za->ch_comment_len = len;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
zip_get_archive_flag.c -- set archive global flag
|
||||
Copyright (C) 2008 Dieter Baron and Thomas Klausner
|
||||
|
||||
This file is part of libzip, a library to manipulate ZIP archives.
|
||||
The authors can be contacted at <libzip@nih.at>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. The names of the authors may not be used to endorse or promote
|
||||
products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include "zipint.h"
|
||||
|
||||
|
||||
|
||||
ZIP_EXTERN int
|
||||
zip_set_archive_flag(struct zip *za, int flag, int value)
|
||||
{
|
||||
if (value)
|
||||
za->ch_flags |= flag;
|
||||
else
|
||||
za->ch_flags &= ~flag;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
zip_set_file_comment.c -- set comment for file in archive
|
||||
Copyright (C) 2006-2007 Dieter Baron and Thomas Klausner
|
||||
|
||||
This file is part of libzip, a library to manipulate ZIP archives.
|
||||
The authors can be contacted at <libzip@nih.at>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. The names of the authors may not be used to endorse or promote
|
||||
products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "zipint.h"
|
||||
|
||||
|
||||
|
||||
ZIP_EXTERN int
|
||||
zip_set_file_comment(struct zip *za, int idx, const char *comment, int len)
|
||||
{
|
||||
char *tmpcom;
|
||||
|
||||
if (idx < 0 || idx >= za->nentry
|
||||
|| len < 0 || len > MAXCOMLEN
|
||||
|| (len > 0 && comment == NULL)) {
|
||||
_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (len > 0) {
|
||||
if ((tmpcom=(char *)_zip_memdup(comment, len, &za->error)) == NULL)
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
tmpcom = NULL;
|
||||
|
||||
free(za->entry[idx].ch_comment);
|
||||
za->entry[idx].ch_comment = tmpcom;
|
||||
za->entry[idx].ch_comment_len = len;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
zip_set_name.c -- rename helper function
|
||||
Copyright (C) 1999-2007 Dieter Baron and Thomas Klausner
|
||||
|
||||
This file is part of libzip, a library to manipulate ZIP archives.
|
||||
The authors can be contacted at <libzip@nih.at>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. The names of the authors may not be used to endorse or promote
|
||||
products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "zipint.h"
|
||||
|
||||
|
||||
|
||||
int
|
||||
_zip_set_name(struct zip *za, int idx, const char *name)
|
||||
{
|
||||
char *s;
|
||||
int i;
|
||||
|
||||
if (idx < 0 || idx >= za->nentry || name == NULL) {
|
||||
_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((i=_zip_name_locate(za, name, 0, NULL)) != -1 && i != idx) {
|
||||
_zip_error_set(&za->error, ZIP_ER_EXISTS, 0);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* no effective name change */
|
||||
if (i == idx)
|
||||
return 0;
|
||||
|
||||
if ((s=strdup(name)) == NULL) {
|
||||
_zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (za->entry[idx].state == ZIP_ST_UNCHANGED)
|
||||
za->entry[idx].state = ZIP_ST_RENAMED;
|
||||
|
||||
free(za->entry[idx].ch_filename);
|
||||
za->entry[idx].ch_filename = s;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
/*
|
||||
zip_source_buffer.c -- create zip data source from buffer
|
||||
Copyright (C) 1999-2007 Dieter Baron and Thomas Klausner
|
||||
|
||||
This file is part of libzip, a library to manipulate ZIP archives.
|
||||
The authors can be contacted at <libzip@nih.at>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. The names of the authors may not be used to endorse or promote
|
||||
products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "zipint.h"
|
||||
|
||||
struct read_data {
|
||||
const char *buf, *data, *end;
|
||||
time_t mtime;
|
||||
int freep;
|
||||
};
|
||||
|
||||
static ssize_t read_data(void *state, void *data, size_t len,
|
||||
enum zip_source_cmd cmd);
|
||||
|
||||
|
||||
|
||||
ZIP_EXTERN struct zip_source *
|
||||
zip_source_buffer(struct zip *za, const void *data, off_t len, int freep)
|
||||
{
|
||||
struct read_data *f;
|
||||
struct zip_source *zs;
|
||||
|
||||
if (za == NULL)
|
||||
return NULL;
|
||||
|
||||
if (len < 0 || (data == NULL && len > 0)) {
|
||||
_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if ((f=(struct read_data *)malloc(sizeof(*f))) == NULL) {
|
||||
_zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
f->data = (const char *)data;
|
||||
f->end = ((const char *)data)+len;
|
||||
f->freep = freep;
|
||||
f->mtime = time(NULL);
|
||||
|
||||
if ((zs=zip_source_function(za, read_data, f)) == NULL) {
|
||||
free(f);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return zs;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static ssize_t
|
||||
read_data(void *state, void *data, size_t len, enum zip_source_cmd cmd)
|
||||
{
|
||||
struct read_data *z;
|
||||
char *buf;
|
||||
size_t n;
|
||||
|
||||
z = (struct read_data *)state;
|
||||
buf = (char *)data;
|
||||
|
||||
switch (cmd) {
|
||||
case ZIP_SOURCE_OPEN:
|
||||
z->buf = z->data;
|
||||
return 0;
|
||||
|
||||
case ZIP_SOURCE_READ:
|
||||
n = z->end - z->buf;
|
||||
if (n > len)
|
||||
n = len;
|
||||
|
||||
if (n) {
|
||||
memcpy(buf, z->buf, n);
|
||||
z->buf += n;
|
||||
}
|
||||
|
||||
return n;
|
||||
|
||||
case ZIP_SOURCE_CLOSE:
|
||||
return 0;
|
||||
|
||||
case ZIP_SOURCE_STAT:
|
||||
{
|
||||
struct zip_stat *st;
|
||||
|
||||
if (len < sizeof(*st))
|
||||
return -1;
|
||||
|
||||
st = (struct zip_stat *)data;
|
||||
|
||||
zip_stat_init(st);
|
||||
st->mtime = z->mtime;
|
||||
st->size = z->end - z->data;
|
||||
|
||||
return sizeof(*st);
|
||||
}
|
||||
|
||||
case ZIP_SOURCE_ERROR:
|
||||
{
|
||||
int *e;
|
||||
|
||||
if (len < sizeof(int)*2)
|
||||
return -1;
|
||||
|
||||
e = (int *)data;
|
||||
e[0] = e[1] = 0;
|
||||
}
|
||||
return sizeof(int)*2;
|
||||
|
||||
case ZIP_SOURCE_FREE:
|
||||
if (z->freep) {
|
||||
free((void *)z->data);
|
||||
z->data = NULL;
|
||||
}
|
||||
free(z);
|
||||
return 0;
|
||||
|
||||
default:
|
||||
;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
zip_source_file.c -- create data source from file
|
||||
Copyright (C) 1999-2008 Dieter Baron and Thomas Klausner
|
||||
|
||||
This file is part of libzip, a library to manipulate ZIP archives.
|
||||
The authors can be contacted at <libzip@nih.at>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. The names of the authors may not be used to endorse or promote
|
||||
products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "zipint.h"
|
||||
|
||||
|
||||
|
||||
ZIP_EXTERN struct zip_source *
|
||||
zip_source_file(struct zip *za, const char *fname, off_t start, off_t len)
|
||||
{
|
||||
if (za == NULL)
|
||||
return NULL;
|
||||
|
||||
if (fname == NULL || start < 0 || len < -1) {
|
||||
_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return _zip_source_file_or_p(za, fname, NULL, start, len);
|
||||
}
|
||||
@@ -0,0 +1,216 @@
|
||||
/*
|
||||
zip_source_filep.c -- create data source from FILE *
|
||||
Copyright (C) 1999-2008 Dieter Baron and Thomas Klausner
|
||||
|
||||
This file is part of libzip, a library to manipulate ZIP archives.
|
||||
The authors can be contacted at <libzip@nih.at>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. The names of the authors may not be used to endorse or promote
|
||||
products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "zipint.h"
|
||||
|
||||
struct read_file {
|
||||
char *fname; /* name of file to copy from */
|
||||
FILE *f; /* file to copy from */
|
||||
off_t off; /* start offset of */
|
||||
off_t len; /* lengt of data to copy */
|
||||
off_t remain; /* bytes remaining to be copied */
|
||||
int e[2]; /* error codes */
|
||||
};
|
||||
|
||||
static ssize_t read_file(void *state, void *data, size_t len,
|
||||
enum zip_source_cmd cmd);
|
||||
|
||||
|
||||
|
||||
ZIP_EXTERN struct zip_source *
|
||||
zip_source_filep(struct zip *za, FILE *file, off_t start, off_t len)
|
||||
{
|
||||
if (za == NULL)
|
||||
return NULL;
|
||||
|
||||
if (file == NULL || start < 0 || len < -1) {
|
||||
_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return _zip_source_file_or_p(za, NULL, file, start, len);
|
||||
}
|
||||
|
||||
|
||||
|
||||
struct zip_source *
|
||||
_zip_source_file_or_p(struct zip *za, const char *fname, FILE *file,
|
||||
off_t start, off_t len)
|
||||
{
|
||||
struct read_file *f;
|
||||
struct zip_source *zs;
|
||||
|
||||
if (file == NULL && fname == NULL) {
|
||||
_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if ((f=(struct read_file *)malloc(sizeof(struct read_file))) == NULL) {
|
||||
_zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
f->fname = NULL;
|
||||
if (fname) {
|
||||
if ((f->fname=strdup(fname)) == NULL) {
|
||||
_zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
|
||||
free(f);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
f->f = file;
|
||||
f->off = start;
|
||||
f->len = (len ? len : -1);
|
||||
|
||||
if ((zs=zip_source_function(za, read_file, f)) == NULL) {
|
||||
free(f);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return zs;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static ssize_t
|
||||
read_file(void *state, void *data, size_t len, enum zip_source_cmd cmd)
|
||||
{
|
||||
struct read_file *z;
|
||||
char *buf;
|
||||
int i, n;
|
||||
|
||||
z = (struct read_file *)state;
|
||||
buf = (char *)data;
|
||||
|
||||
switch (cmd) {
|
||||
case ZIP_SOURCE_OPEN:
|
||||
if (z->fname) {
|
||||
if ((z->f=fopen(z->fname, "rb")) == NULL) {
|
||||
z->e[0] = ZIP_ER_OPEN;
|
||||
z->e[1] = errno;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (fseeko(z->f, z->off, SEEK_SET) < 0) {
|
||||
z->e[0] = ZIP_ER_SEEK;
|
||||
z->e[1] = errno;
|
||||
return -1;
|
||||
}
|
||||
z->remain = z->len;
|
||||
return 0;
|
||||
|
||||
case ZIP_SOURCE_READ:
|
||||
if (z->remain != -1)
|
||||
n = len > z->remain ? z->remain : len;
|
||||
else
|
||||
n = len;
|
||||
|
||||
if ((i=fread(buf, 1, n, z->f)) < 0) {
|
||||
z->e[0] = ZIP_ER_READ;
|
||||
z->e[1] = errno;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (z->remain != -1)
|
||||
z->remain -= i;
|
||||
|
||||
return i;
|
||||
|
||||
case ZIP_SOURCE_CLOSE:
|
||||
if (z->fname) {
|
||||
fclose(z->f);
|
||||
z->f = NULL;
|
||||
}
|
||||
return 0;
|
||||
|
||||
case ZIP_SOURCE_STAT:
|
||||
{
|
||||
struct zip_stat *st;
|
||||
struct stat fst;
|
||||
int err;
|
||||
|
||||
if (len < sizeof(*st))
|
||||
return -1;
|
||||
|
||||
if (z->f)
|
||||
err = fstat(fileno(z->f), &fst);
|
||||
else
|
||||
err = stat(z->fname, &fst);
|
||||
|
||||
if (err != 0) {
|
||||
z->e[0] = ZIP_ER_READ; /* best match */
|
||||
z->e[1] = errno;
|
||||
return -1;
|
||||
}
|
||||
|
||||
st = (struct zip_stat *)data;
|
||||
|
||||
zip_stat_init(st);
|
||||
st->mtime = fst.st_mtime;
|
||||
if (z->len != -1)
|
||||
st->size = z->len;
|
||||
else if ((fst.st_mode&S_IFMT) == S_IFREG)
|
||||
st->size = fst.st_size;
|
||||
|
||||
return sizeof(*st);
|
||||
}
|
||||
|
||||
case ZIP_SOURCE_ERROR:
|
||||
if (len < sizeof(int)*2)
|
||||
return -1;
|
||||
|
||||
memcpy(data, z->e, sizeof(int)*2);
|
||||
return sizeof(int)*2;
|
||||
|
||||
case ZIP_SOURCE_FREE:
|
||||
free(z->fname);
|
||||
if (z->f)
|
||||
fclose(z->f);
|
||||
free(z);
|
||||
return 0;
|
||||
|
||||
default:
|
||||
;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
zip_source_free.c -- free zip data source
|
||||
Copyright (C) 1999-2007 Dieter Baron and Thomas Klausner
|
||||
|
||||
This file is part of libzip, a library to manipulate ZIP archives.
|
||||
The authors can be contacted at <libzip@nih.at>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. The names of the authors may not be used to endorse or promote
|
||||
products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "zipint.h"
|
||||
|
||||
|
||||
|
||||
ZIP_EXTERN void
|
||||
zip_source_free(struct zip_source *source)
|
||||
{
|
||||
if (source == NULL)
|
||||
return;
|
||||
|
||||
(void)source->f(source->ud, NULL, 0, ZIP_SOURCE_FREE);
|
||||
|
||||
free(source);
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
zip_source_function.c -- create zip data source from callback function
|
||||
Copyright (C) 1999-2007 Dieter Baron and Thomas Klausner
|
||||
|
||||
This file is part of libzip, a library to manipulate ZIP archives.
|
||||
The authors can be contacted at <libzip@nih.at>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. The names of the authors may not be used to endorse or promote
|
||||
products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "zipint.h"
|
||||
|
||||
|
||||
|
||||
ZIP_EXTERN struct zip_source *
|
||||
zip_source_function(struct zip *za, zip_source_callback zcb, void *ud)
|
||||
{
|
||||
struct zip_source *zs;
|
||||
|
||||
if (za == NULL)
|
||||
return NULL;
|
||||
|
||||
if ((zs=(struct zip_source *)malloc(sizeof(*zs))) == NULL) {
|
||||
_zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
zs->f = zcb;
|
||||
zs->ud = ud;
|
||||
|
||||
return zs;
|
||||
}
|
||||
@@ -0,0 +1,188 @@
|
||||
/*
|
||||
zip_source_zip.c -- create data source from zip file
|
||||
Copyright (C) 1999-2008 Dieter Baron and Thomas Klausner
|
||||
|
||||
This file is part of libzip, a library to manipulate ZIP archives.
|
||||
The authors can be contacted at <libzip@nih.at>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. The names of the authors may not be used to endorse or promote
|
||||
products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "zipint.h"
|
||||
|
||||
struct read_zip {
|
||||
struct zip_file *zf;
|
||||
struct zip_stat st;
|
||||
off_t off, len;
|
||||
};
|
||||
|
||||
static ssize_t read_zip(void *st, void *data, size_t len,
|
||||
enum zip_source_cmd cmd);
|
||||
|
||||
|
||||
|
||||
ZIP_EXTERN struct zip_source *
|
||||
zip_source_zip(struct zip *za, struct zip *srcza, int srcidx, int flags,
|
||||
off_t start, off_t len)
|
||||
{
|
||||
struct zip_error error;
|
||||
struct zip_source *zs;
|
||||
struct read_zip *p;
|
||||
|
||||
/* XXX: ZIP_FL_RECOMPRESS */
|
||||
|
||||
if (za == NULL)
|
||||
return NULL;
|
||||
|
||||
if (srcza == NULL || start < 0 || len < -1 || srcidx < 0 || srcidx >= srcza->nentry) {
|
||||
_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if ((flags & ZIP_FL_UNCHANGED) == 0
|
||||
&& ZIP_ENTRY_DATA_CHANGED(srcza->entry+srcidx)) {
|
||||
_zip_error_set(&za->error, ZIP_ER_CHANGED, 0);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (len == 0)
|
||||
len = -1;
|
||||
|
||||
if (start == 0 && len == -1 && (flags & ZIP_FL_RECOMPRESS) == 0)
|
||||
flags |= ZIP_FL_COMPRESSED;
|
||||
else
|
||||
flags &= ~ZIP_FL_COMPRESSED;
|
||||
|
||||
if ((p=(struct read_zip *)malloc(sizeof(*p))) == NULL) {
|
||||
_zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
_zip_error_copy(&error, &srcza->error);
|
||||
|
||||
if (zip_stat_index(srcza, srcidx, flags, &p->st) < 0
|
||||
|| (p->zf=zip_fopen_index(srcza, srcidx, flags)) == NULL) {
|
||||
free(p);
|
||||
_zip_error_copy(&za->error, &srcza->error);
|
||||
_zip_error_copy(&srcza->error, &error);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
p->off = start;
|
||||
p->len = len;
|
||||
|
||||
if ((flags & ZIP_FL_COMPRESSED) == 0) {
|
||||
p->st.size = p->st.comp_size = len;
|
||||
p->st.comp_method = ZIP_CM_STORE;
|
||||
p->st.crc = 0;
|
||||
}
|
||||
|
||||
if ((zs=zip_source_function(za, read_zip, p)) == NULL) {
|
||||
free(p);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return zs;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static ssize_t
|
||||
read_zip(void *state, void *data, size_t len, enum zip_source_cmd cmd)
|
||||
{
|
||||
struct read_zip *z;
|
||||
char b[8192], *buf;
|
||||
int i, n;
|
||||
|
||||
z = (struct read_zip *)state;
|
||||
buf = (char *)data;
|
||||
|
||||
switch (cmd) {
|
||||
case ZIP_SOURCE_OPEN:
|
||||
for (n=0; n<z->off; n+= i) {
|
||||
i = (z->off-n > sizeof(b) ? sizeof(b) : z->off-n);
|
||||
if ((i=zip_fread(z->zf, b, i)) < 0) {
|
||||
zip_fclose(z->zf);
|
||||
z->zf = NULL;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
|
||||
case ZIP_SOURCE_READ:
|
||||
if (z->len != -1)
|
||||
n = len > z->len ? z->len : len;
|
||||
else
|
||||
n = len;
|
||||
|
||||
|
||||
if ((i=zip_fread(z->zf, buf, n)) < 0)
|
||||
return -1;
|
||||
|
||||
if (z->len != -1)
|
||||
z->len -= i;
|
||||
|
||||
return i;
|
||||
|
||||
case ZIP_SOURCE_CLOSE:
|
||||
return 0;
|
||||
|
||||
case ZIP_SOURCE_STAT:
|
||||
if (len < sizeof(z->st))
|
||||
return -1;
|
||||
len = sizeof(z->st);
|
||||
|
||||
memcpy(data, &z->st, len);
|
||||
return len;
|
||||
|
||||
case ZIP_SOURCE_ERROR:
|
||||
{
|
||||
int *e;
|
||||
|
||||
if (len < sizeof(int)*2)
|
||||
return -1;
|
||||
|
||||
e = (int *)data;
|
||||
zip_file_error_get(z->zf, e, e+1);
|
||||
}
|
||||
return sizeof(int)*2;
|
||||
|
||||
case ZIP_SOURCE_FREE:
|
||||
zip_fclose(z->zf);
|
||||
free(z);
|
||||
return 0;
|
||||
|
||||
default:
|
||||
;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
zip_stat.c -- get information about file by name
|
||||
Copyright (C) 1999-2007 Dieter Baron and Thomas Klausner
|
||||
|
||||
This file is part of libzip, a library to manipulate ZIP archives.
|
||||
The authors can be contacted at <libzip@nih.at>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. The names of the authors may not be used to endorse or promote
|
||||
products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include "zipint.h"
|
||||
|
||||
|
||||
|
||||
ZIP_EXTERN int
|
||||
zip_stat(struct zip *za, const char *fname, int flags, struct zip_stat *st)
|
||||
{
|
||||
int idx;
|
||||
|
||||
if ((idx=zip_name_locate(za, fname, flags)) < 0)
|
||||
return -1;
|
||||
|
||||
return zip_stat_index(za, idx, flags, st);
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
zip_stat_index.c -- get information about file by index
|
||||
Copyright (C) 1999-2007 Dieter Baron and Thomas Klausner
|
||||
|
||||
This file is part of libzip, a library to manipulate ZIP archives.
|
||||
The authors can be contacted at <libzip@nih.at>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. The names of the authors may not be used to endorse or promote
|
||||
products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include "zipint.h"
|
||||
|
||||
|
||||
|
||||
ZIP_EXTERN int
|
||||
zip_stat_index(struct zip *za, int index, int flags, struct zip_stat *st)
|
||||
{
|
||||
const char *name;
|
||||
|
||||
if (index < 0 || index >= za->nentry) {
|
||||
_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((name=zip_get_name(za, index, flags)) == NULL)
|
||||
return -1;
|
||||
|
||||
|
||||
if ((flags & ZIP_FL_UNCHANGED) == 0
|
||||
&& ZIP_ENTRY_DATA_CHANGED(za->entry+index)) {
|
||||
if (za->entry[index].source->f(za->entry[index].source->ud,
|
||||
st, sizeof(*st), ZIP_SOURCE_STAT) < 0) {
|
||||
_zip_error_set(&za->error, ZIP_ER_CHANGED, 0);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (za->cdir == NULL || index >= za->cdir->nentry) {
|
||||
_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
|
||||
return -1;
|
||||
}
|
||||
|
||||
st->crc = za->cdir->entry[index].crc;
|
||||
st->size = za->cdir->entry[index].uncomp_size;
|
||||
st->mtime = za->cdir->entry[index].last_mod;
|
||||
st->comp_size = za->cdir->entry[index].comp_size;
|
||||
st->comp_method = za->cdir->entry[index].comp_method;
|
||||
if (za->cdir->entry[index].bitflags & ZIP_GPBF_ENCRYPTED) {
|
||||
if (za->cdir->entry[index].bitflags & ZIP_GPBF_STRONG_ENCRYPTION) {
|
||||
/* XXX */
|
||||
st->encryption_method = ZIP_EM_UNKNOWN;
|
||||
}
|
||||
else
|
||||
st->encryption_method = ZIP_EM_TRAD_PKWARE;
|
||||
}
|
||||
else
|
||||
st->encryption_method = ZIP_EM_NONE;
|
||||
/* st->bitflags = za->cdir->entry[index].bitflags; */
|
||||
}
|
||||
|
||||
st->index = index;
|
||||
st->name = name;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
zip_stat_init.c -- initialize struct zip_stat.
|
||||
Copyright (C) 2006-2007 Dieter Baron and Thomas Klausner
|
||||
|
||||
This file is part of libzip, a library to manipulate ZIP archives.
|
||||
The authors can be contacted at <libzip@nih.at>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. The names of the authors may not be used to endorse or promote
|
||||
products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include "zipint.h"
|
||||
|
||||
|
||||
|
||||
ZIP_EXTERN void
|
||||
zip_stat_init(struct zip_stat *st)
|
||||
{
|
||||
st->name = NULL;
|
||||
st->index = -1;
|
||||
st->crc = 0;
|
||||
st->mtime = (time_t)-1;
|
||||
st->size = -1;
|
||||
st->comp_size = -1;
|
||||
st->comp_method = ZIP_CM_STORE;
|
||||
st->encryption_method = ZIP_EM_NONE;
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
zip_sterror.c -- get string representation of zip error
|
||||
Copyright (C) 1999-2007 Dieter Baron and Thomas Klausner
|
||||
|
||||
This file is part of libzip, a library to manipulate ZIP archives.
|
||||
The authors can be contacted at <libzip@nih.at>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. The names of the authors may not be used to endorse or promote
|
||||
products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include "zipint.h"
|
||||
|
||||
|
||||
|
||||
ZIP_EXTERN const char *
|
||||
zip_strerror(struct zip *za)
|
||||
{
|
||||
return _zip_error_strerror(&za->error);
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
zip_unchange.c -- undo changes to file in zip archive
|
||||
Copyright (C) 1999-2007 Dieter Baron and Thomas Klausner
|
||||
|
||||
This file is part of libzip, a library to manipulate ZIP archives.
|
||||
The authors can be contacted at <libzip@nih.at>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. The names of the authors may not be used to endorse or promote
|
||||
products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "zipint.h"
|
||||
|
||||
|
||||
|
||||
ZIP_EXTERN int
|
||||
zip_unchange(struct zip *za, int idx)
|
||||
{
|
||||
return _zip_unchange(za, idx, 0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
int
|
||||
_zip_unchange(struct zip *za, int idx, int allow_duplicates)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (idx < 0 || idx >= za->nentry) {
|
||||
_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (za->entry[idx].ch_filename) {
|
||||
if (!allow_duplicates) {
|
||||
i = _zip_name_locate(za,
|
||||
_zip_get_name(za, idx, ZIP_FL_UNCHANGED, NULL),
|
||||
0, NULL);
|
||||
if (i != -1 && i != idx) {
|
||||
_zip_error_set(&za->error, ZIP_ER_EXISTS, 0);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
free(za->entry[idx].ch_filename);
|
||||
za->entry[idx].ch_filename = NULL;
|
||||
}
|
||||
|
||||
free(za->entry[idx].ch_comment);
|
||||
za->entry[idx].ch_comment = NULL;
|
||||
za->entry[idx].ch_comment_len = -1;
|
||||
|
||||
_zip_unchange_data(za->entry+idx);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
zip_unchange.c -- undo changes to all files in zip archive
|
||||
Copyright (C) 1999-2007 Dieter Baron and Thomas Klausner
|
||||
|
||||
This file is part of libzip, a library to manipulate ZIP archives.
|
||||
The authors can be contacted at <libzip@nih.at>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. The names of the authors may not be used to endorse or promote
|
||||
products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "zipint.h"
|
||||
|
||||
|
||||
|
||||
ZIP_EXTERN int
|
||||
zip_unchange_all(struct zip *za)
|
||||
{
|
||||
int ret, i;
|
||||
|
||||
ret = 0;
|
||||
for (i=0; i<za->nentry; i++)
|
||||
ret |= _zip_unchange(za, i, 1);
|
||||
|
||||
ret |= zip_unchange_archive(za);
|
||||
|
||||
return ret;
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
zip_unchange_archive.c -- undo global changes to ZIP archive
|
||||
Copyright (C) 2006-2008 Dieter Baron and Thomas Klausner
|
||||
|
||||
This file is part of libzip, a library to manipulate ZIP archives.
|
||||
The authors can be contacted at <libzip@nih.at>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. The names of the authors may not be used to endorse or promote
|
||||
products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "zipint.h"
|
||||
|
||||
|
||||
|
||||
ZIP_EXTERN int
|
||||
zip_unchange_archive(struct zip *za)
|
||||
{
|
||||
free(za->ch_comment);
|
||||
za->ch_comment = NULL;
|
||||
za->ch_comment_len = -1;
|
||||
|
||||
za->ch_flags = za->flags;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
$NiH: zip_unchange_data.c,v 1.14 2004/11/30 23:02:47 wiz Exp $
|
||||
|
||||
zip_unchange_data.c -- undo helper function
|
||||
Copyright (C) 1999, 2004 Dieter Baron and Thomas Klausner
|
||||
|
||||
This file is part of libzip, a library to manipulate ZIP archives.
|
||||
The authors can be contacted at <libzip@nih.at>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. The names of the authors may not be used to endorse or promote
|
||||
products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "zipint.h"
|
||||
|
||||
void
|
||||
_zip_unchange_data(struct zip_entry *ze)
|
||||
{
|
||||
if (ze->source) {
|
||||
(void)ze->source->f(ze->source->ud, NULL, 0, ZIP_SOURCE_FREE);
|
||||
free(ze->source);
|
||||
ze->source = NULL;
|
||||
}
|
||||
|
||||
ze->state = ze->ch_filename ? ZIP_ST_RENAMED : ZIP_ST_UNCHANGED;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,264 @@
|
||||
#ifndef _HAD_ZIPINT_H
|
||||
#define _HAD_ZIPINT_H
|
||||
|
||||
/*
|
||||
zipint.h -- internal declarations.
|
||||
Copyright (C) 1999-2008 Dieter Baron and Thomas Klausner
|
||||
|
||||
This file is part of libzip, a library to manipulate ZIP archives.
|
||||
The authors can be contacted at <libzip@nih.at>
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. The names of the authors may not be used to endorse or promote
|
||||
products derived from this software without specific prior
|
||||
written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
||||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <zlib.h>
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#define ZIP_EXTERN __declspec(dllimport)
|
||||
#endif
|
||||
|
||||
#include "zip.h"
|
||||
#include "config.h"
|
||||
|
||||
#ifndef HAVE_MKSTEMP
|
||||
int _zip_mkstemp(char *);
|
||||
#define mkstemp _zip_mkstemp
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_MOVEFILEEXA
|
||||
#include <windows.h>
|
||||
#define _zip_rename(s, t) \
|
||||
(!MoveFileExA((s), (t), \
|
||||
MOVEFILE_COPY_ALLOWED|MOVEFILE_REPLACE_EXISTING))
|
||||
#else
|
||||
#define _zip_rename rename
|
||||
#endif
|
||||
|
||||
#ifndef HAVE_FSEEKO
|
||||
#define fseeko(s, o, w) (fseek((s), (long int)(o), (w)))
|
||||
#endif
|
||||
#ifndef HAVE_FTELLO
|
||||
#define ftello(s) ((long)ftell((s)))
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#define CENTRAL_MAGIC "PK\1\2"
|
||||
#define LOCAL_MAGIC "PK\3\4"
|
||||
#define EOCD_MAGIC "PK\5\6"
|
||||
#define DATADES_MAGIC "PK\7\8"
|
||||
#define TORRENT_SIG "TORRENTZIPPED-"
|
||||
#define TORRENT_SIG_LEN 14
|
||||
#define TORRENT_CRC_LEN 8
|
||||
#define TORRENT_MEM_LEVEL 8
|
||||
#define CDENTRYSIZE 46u
|
||||
#define LENTRYSIZE 30
|
||||
#define MAXCOMLEN 65536
|
||||
#define EOCDLEN 22
|
||||
#define CDBUFSIZE (MAXCOMLEN+EOCDLEN)
|
||||
#define BUFSIZE 8192
|
||||
|
||||
|
||||
|
||||
/* state of change of a file in zip archive */
|
||||
|
||||
enum zip_state { ZIP_ST_UNCHANGED, ZIP_ST_DELETED, ZIP_ST_REPLACED,
|
||||
ZIP_ST_ADDED, ZIP_ST_RENAMED };
|
||||
|
||||
/* constants for struct zip_file's member flags */
|
||||
|
||||
#define ZIP_ZF_EOF 1 /* EOF reached */
|
||||
#define ZIP_ZF_DECOMP 2 /* decompress data */
|
||||
#define ZIP_ZF_CRC 4 /* compute and compare CRC */
|
||||
|
||||
/* directory entry: general purpose bit flags */
|
||||
|
||||
#define ZIP_GPBF_ENCRYPTED 0x0001 /* is encrypted */
|
||||
#define ZIP_GPBF_DATA_DESCRIPTOR 0x0008 /* crc/size after file data */
|
||||
#define ZIP_GPBF_STRONG_ENCRYPTION 0x0040 /* uses strong encryption */
|
||||
|
||||
/* error information */
|
||||
|
||||
struct zip_error {
|
||||
int zip_err; /* libzip error code (ZIP_ER_*) */
|
||||
int sys_err; /* copy of errno (E*) or zlib error code */
|
||||
char *str; /* string representation or NULL */
|
||||
};
|
||||
|
||||
/* zip archive, part of API */
|
||||
|
||||
struct zip {
|
||||
char *zn; /* file name */
|
||||
FILE *zp; /* file */
|
||||
struct zip_error error; /* error information */
|
||||
|
||||
unsigned int flags; /* archive global flags */
|
||||
unsigned int ch_flags; /* changed archive global flags */
|
||||
|
||||
struct zip_cdir *cdir; /* central directory */
|
||||
char *ch_comment; /* changed archive comment */
|
||||
int ch_comment_len; /* length of changed zip archive
|
||||
* comment, -1 if unchanged */
|
||||
int nentry; /* number of entries */
|
||||
int nentry_alloc; /* number of entries allocated */
|
||||
struct zip_entry *entry; /* entries */
|
||||
int nfile; /* number of opened files within archive */
|
||||
int nfile_alloc; /* number of files allocated */
|
||||
struct zip_file **file; /* opened files within archive */
|
||||
};
|
||||
|
||||
/* file in zip archive, part of API */
|
||||
|
||||
struct zip_file {
|
||||
struct zip *za; /* zip archive containing this file */
|
||||
struct zip_error error; /* error information */
|
||||
int flags; /* -1: eof, >0: error */
|
||||
|
||||
int method; /* compression method */
|
||||
off_t fpos; /* position within zip file (fread/fwrite) */
|
||||
unsigned long bytes_left; /* number of bytes left to read */
|
||||
unsigned long cbytes_left; /* number of bytes of compressed data left */
|
||||
|
||||
unsigned long crc; /* CRC so far */
|
||||
unsigned long crc_orig; /* CRC recorded in archive */
|
||||
|
||||
char *buffer;
|
||||
z_stream *zstr;
|
||||
};
|
||||
|
||||
/* zip archive directory entry (central or local) */
|
||||
|
||||
struct zip_dirent {
|
||||
unsigned short version_madeby; /* (c) version of creator */
|
||||
unsigned short version_needed; /* (cl) version needed to extract */
|
||||
unsigned short bitflags; /* (cl) general purpose bit flag */
|
||||
unsigned short comp_method; /* (cl) compression method used */
|
||||
time_t last_mod; /* (cl) time of last modification */
|
||||
unsigned int crc; /* (cl) CRC-32 of uncompressed data */
|
||||
unsigned int comp_size; /* (cl) size of commpressed data */
|
||||
unsigned int uncomp_size; /* (cl) size of uncommpressed data */
|
||||
char *filename; /* (cl) file name (NUL-terminated) */
|
||||
unsigned short filename_len; /* (cl) length of filename (w/o NUL) */
|
||||
char *extrafield; /* (cl) extra field */
|
||||
unsigned short extrafield_len; /* (cl) length of extra field */
|
||||
char *comment; /* (c) file comment */
|
||||
unsigned short comment_len; /* (c) length of file comment */
|
||||
unsigned short disk_number; /* (c) disk number start */
|
||||
unsigned short int_attrib; /* (c) internal file attributes */
|
||||
unsigned int ext_attrib; /* (c) external file attributes */
|
||||
unsigned int offset; /* (c) offset of local header */
|
||||
};
|
||||
|
||||
/* zip archive central directory */
|
||||
|
||||
struct zip_cdir {
|
||||
struct zip_dirent *entry; /* directory entries */
|
||||
int nentry; /* number of entries */
|
||||
|
||||
unsigned int size; /* size of central direcotry */
|
||||
unsigned int offset; /* offset of central directory in file */
|
||||
char *comment; /* zip archive comment */
|
||||
unsigned short comment_len; /* length of zip archive comment */
|
||||
};
|
||||
|
||||
|
||||
|
||||
struct zip_source {
|
||||
zip_source_callback f;
|
||||
void *ud;
|
||||
};
|
||||
|
||||
/* entry in zip archive directory */
|
||||
|
||||
struct zip_entry {
|
||||
enum zip_state state;
|
||||
struct zip_source *source;
|
||||
char *ch_filename;
|
||||
char *ch_comment;
|
||||
int ch_comment_len;
|
||||
};
|
||||
|
||||
|
||||
|
||||
extern const char * const _zip_err_str[];
|
||||
extern const int _zip_nerr_str;
|
||||
extern const int _zip_err_type[];
|
||||
|
||||
|
||||
|
||||
#define ZIP_ENTRY_DATA_CHANGED(x) \
|
||||
((x)->state == ZIP_ST_REPLACED \
|
||||
|| (x)->state == ZIP_ST_ADDED)
|
||||
|
||||
|
||||
|
||||
int _zip_cdir_compute_crc(struct zip *, uLong *);
|
||||
void _zip_cdir_free(struct zip_cdir *);
|
||||
int _zip_cdir_grow(struct zip_cdir *, int, struct zip_error *);
|
||||
struct zip_cdir *_zip_cdir_new(int, struct zip_error *);
|
||||
int _zip_cdir_write(struct zip_cdir *, FILE *, struct zip_error *);
|
||||
|
||||
void _zip_dirent_finalize(struct zip_dirent *);
|
||||
void _zip_dirent_init(struct zip_dirent *);
|
||||
int _zip_dirent_read(struct zip_dirent *, FILE *, unsigned char **,
|
||||
unsigned int *, int, struct zip_error *);
|
||||
void _zip_dirent_torrent_normalize(struct zip_dirent *);
|
||||
int _zip_dirent_write(struct zip_dirent *, FILE *, int, struct zip_error *);
|
||||
|
||||
void _zip_entry_free(struct zip_entry *);
|
||||
void _zip_entry_init(struct zip *, int);
|
||||
struct zip_entry *_zip_entry_new(struct zip *);
|
||||
|
||||
void _zip_error_clear(struct zip_error *);
|
||||
void _zip_error_copy(struct zip_error *, struct zip_error *);
|
||||
void _zip_error_fini(struct zip_error *);
|
||||
void _zip_error_get(struct zip_error *, int *, int *);
|
||||
void _zip_error_init(struct zip_error *);
|
||||
void _zip_error_set(struct zip_error *, int, int);
|
||||
const char *_zip_error_strerror(struct zip_error *);
|
||||
|
||||
int _zip_file_fillbuf(void *, size_t, struct zip_file *);
|
||||
unsigned int _zip_file_get_offset(struct zip *, int);
|
||||
|
||||
int _zip_filerange_crc(FILE *, off_t, off_t, uLong *, struct zip_error *);
|
||||
|
||||
struct zip_source *_zip_source_file_or_p(struct zip *, const char *, FILE *,
|
||||
off_t, off_t);
|
||||
|
||||
void _zip_free(struct zip *);
|
||||
const char *_zip_get_name(struct zip *, int, int, struct zip_error *);
|
||||
int _zip_local_header_read(struct zip *, int);
|
||||
void *_zip_memdup(const void *, size_t, struct zip_error *);
|
||||
int _zip_name_locate(struct zip *, const char *, int, struct zip_error *);
|
||||
struct zip *_zip_new(struct zip_error *);
|
||||
unsigned short _zip_read2(unsigned char **);
|
||||
unsigned int _zip_read4(unsigned char **);
|
||||
int _zip_replace(struct zip *, int, const char *, struct zip_source *);
|
||||
int _zip_set_name(struct zip *, int, const char *);
|
||||
int _zip_unchange(struct zip *, int, int);
|
||||
void _zip_unchange_data(struct zip_entry *);
|
||||
|
||||
#endif /* zipint.h */
|
||||
@@ -0,0 +1,52 @@
|
||||
Use of this software is granted under one of the following two licenses,
|
||||
to be chosen freely by the user.
|
||||
|
||||
1. Boost Software License - Version 1.0 - August 17th, 2003
|
||||
===============================================================================
|
||||
|
||||
Copyright (c) 2006, 2007 Marcin Kalicinski
|
||||
|
||||
Permission is hereby granted, free of charge, to any person or organization
|
||||
obtaining a copy of the software and accompanying documentation covered by
|
||||
this license (the "Software") to use, reproduce, display, distribute,
|
||||
execute, and transmit the Software, and to prepare derivative works of the
|
||||
Software, and to permit third-parties to whom the Software is furnished to
|
||||
do so, all subject to the following:
|
||||
|
||||
The copyright notices in the Software and this entire statement, including
|
||||
the above license grant, this restriction and the following disclaimer,
|
||||
must be included in all copies of the Software, in whole or in part, and
|
||||
all derivative works of the Software, unless such copies or derivative
|
||||
works are solely in the form of machine-executable object code generated by
|
||||
a source language processor.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
DEALINGS IN THE SOFTWARE.
|
||||
|
||||
2. The MIT License
|
||||
===============================================================================
|
||||
|
||||
Copyright (c) 2006, 2007 Marcin Kalicinski
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
IN THE SOFTWARE.
|
||||
Loaded 100 of 201 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user