summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/BString.h2
-rw-r--r--include/Handle.h6
-rw-r--r--include/generic.h13
-rw-r--r--lib/Handle.cc54
-rw-r--r--lib/Image.cc3
-rw-r--r--lib/Input.cc7
-rw-r--r--src/paq.cc21
7 files changed, 84 insertions, 22 deletions
diff --git a/include/BString.h b/include/BString.h
index a0aa10b..a57f203 100644
--- a/include/BString.h
+++ b/include/BString.h
@@ -8,7 +8,7 @@
#include <generic.h>
struct ugly_string {
- char * p;
+ const char * p;
};
class String : public Base {
diff --git a/include/Handle.h b/include/Handle.h
index 25b22a7..1a3973d 100644
--- a/include/Handle.h
+++ b/include/Handle.h
@@ -15,6 +15,12 @@ class Handle : public Base {
virtual ~Handle();
virtual ssize_t read(void *buf, size_t count) throw (GeneralException);
virtual ssize_t write(const void *buf, size_t count) throw (GeneralException);
+ Uint8 readU8();
+ Uint16 readU16();
+ Uint32 readU32();
+ void writeU8(Uint8);
+ void writeU16(Uint16);
+ void writeU32(Uint32);
bool IsClosed(void) const;
bool IsNonBlock(void) const;
void SetNonBlock(void);
diff --git a/include/generic.h b/include/generic.h
index 964501f..f20eeb7 100644
--- a/include/generic.h
+++ b/include/generic.h
@@ -74,10 +74,10 @@ typedef unsigned _int64 uint64;
#ifndef PACKED
#if defined __linux__ || defined __CYGWIN32__
#define PACKED __attribute__((packed))
-#else
+#else // PACKED
#define PACKED
#endif
-#endif
+#endif // !PACKED
extern char verbosity;
char ** split(char * s, char t);
@@ -98,7 +98,8 @@ inline T MIN(T a, T b) {
}
#endif
-#else
+#else // cplusplus
+
#ifndef MAX
#define MAX(__a,__b) ((__a)<(__b)?(__b):(__a))
#endif
@@ -107,11 +108,11 @@ inline T MIN(T a, T b) {
#define MIN(__a,__b) ((__a)>(__b)?(__b):(__a))
#endif
-#endif
+#endif // !cplusplus
#define BITCOUNT(x) (((BX_(x)+(BX_(x)>>4)) & 0x0F0F0F0F) % 255)
-#define BX_(x) ((x) - (((x)>>1)&0x77777777) \
- - (((x)>>2)&0x33333333) \
+#define BX_(x) ((x) - (((x)>>1)&0x77777777) \
+ - (((x)>>2)&0x33333333) \
- (((x)>>3)&0x11111111))
#define ISPOT(x) (((x)&(x-1))==0?1:0)
diff --git a/lib/Handle.cc b/lib/Handle.cc
index 030eb2c..d028c47 100644
--- a/lib/Handle.cc
+++ b/lib/Handle.cc
@@ -1,15 +1,19 @@
#include <stdio.h>
#include <string.h>
#include <errno.h>
+#include <fcntl.h>
+#include <byteswap.h>
+
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
+
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#else
#include <io.h>
#endif
-#include <fcntl.h>
+
#include "Handle.h"
#include "gettext.h"
@@ -330,6 +334,54 @@ off_t Handle::seek(off_t offset, int whence) throw(GeneralException) {
}
}
+Uint8 Handle::readU8() {
+ Uint8 r;
+ read(&r, 1);
+ return r;
+}
+
+Uint16 Handle::readU16() {
+ Uint16 r;
+ read(&r, 2);
+#ifdef WORDS_BIGENDIAN
+ return bswap_16(r);
+#else
+ return r;
+#endif
+}
+
+Uint32 Handle::readU32() {
+ Uint32 r;
+ read(&r, 4);
+#ifdef WORDS_BIGENDIAN
+ return bswap_32(r);
+#else
+ return r;
+#endif
+}
+
+void Handle::writeU8(Uint8 v) {
+ write(&v, 1);
+}
+
+void Handle::writeU16(Uint16 v) {
+#ifdef WORDS_BIGENDIAN
+ Uint16 t = bswap_16(v);
+ write(&t, 2);
+#else
+ write(&v, 2);
+#endif
+}
+
+void Handle::writeU32(Uint32 v) {
+#ifdef WORDS_BIGENDIAN
+ Uint32 t = bswap_32(v);
+ write(&t, 4);
+#else
+ write(&v, 4);
+#endif
+}
+
void copy(Handle * s, Handle * d, ssize_t size) {
long i;
unsigned char c;
diff --git a/lib/Image.cc b/lib/Image.cc
index a0ece1b..6b698ae 100644
--- a/lib/Image.cc
+++ b/lib/Image.cc
@@ -44,6 +44,9 @@ void Image::SetPixel(unsigned int px, unsigned int py, Color c) {
#ifndef WORDS_BIGENDIAN
#define WORDS_BIGENDIAN 0
+#else
+#undef WORDS_BIGENDIAN
+#define WORDS_BIGENDIAN 1
#endif
bool Image::Prepare(unsigned int f) {
diff --git a/lib/Input.cc b/lib/Input.cc
index 4648d91..60e17c9 100644
--- a/lib/Input.cc
+++ b/lib/Input.cc
@@ -4,14 +4,17 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
+
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
+
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#else
#include <io.h>
#endif
+
#include "Input.h"
#include "Exceptions.h"
#include "gettext.h"
@@ -89,7 +92,7 @@ Input::Input(const String & no) throw (GeneralException) :
} else {
// size = results.size;
seek(results.ptr, SEEK_SET);
- read(&size, 4);
+ size = readU32();
date_modif = 0;
SetZ();
fromarchive = true;
@@ -201,7 +204,7 @@ Archive::Archive(const String & fname, int atype) throw (GeneralException) :
archive.read(buffer, len);
buffer[len] = 0;
ifname = buffer;
- archive.read(&size, 4);
+ size = archive.readU32();
archive.read(buffer, 1);
#ifdef DEBUG
std::cerr << "Adding file `" << ifname << "' to node `" << p->name << "'\n";
diff --git a/src/paq.cc b/src/paq.cc
index 913e925..2cbddd4 100644
--- a/src/paq.cc
+++ b/src/paq.cc
@@ -57,7 +57,7 @@ void finalize(void) {
cerr << "Finalize file " << *i << endl;
file = new Input(*i);
size = file->GetSize();
- Archive->write(&size, 4);
+ Archive->writeU32(size);
delete file;
file = new Input(*i + ".gz");
copy(file, Archive);
@@ -89,18 +89,18 @@ void process_file(const String & filename) {
size = from->GetSize() + 4;
cerr << old_size << " --> " << from->GetSize() << " (" << 100 * from->GetSize() / old_size << "%)\n";
- Archive->write(&size, 4);
+ Archive->writeU32(size);
delete from;
t = 0;
- Archive->write(&t, 1);
+ Archive->writeU8(t);
}
void process_directory(const String & dirname) throw (GeneralException) {
struct dirent ** namelist;
int n, i;
- char t;
+ Uint32 t;
struct stat fstats;
String fname;
@@ -118,21 +118,18 @@ void process_directory(const String & dirname) throw (GeneralException) {
if (S_ISDIR(fstats.st_mode)) {
if (!Regex("^\\.{1,2}$").Match(namelist[i]->d_name)) {
t = strlen(namelist[i]->d_name);
- Archive->write(&t, 1);
+ Archive->writeU8(t);
Archive->write(namelist[i]->d_name, t);
t = 0;
- Archive->write(&t, 1);
- Archive->write(&t, 1);
- Archive->write(&t, 1);
- Archive->write(&t, 1);
+ Archive->writeU32(t);
t = 1;
- Archive->write(&t, 1);
+ Archive->writeU8(t);
process_directory(dirname + "/" + namelist[i]->d_name);
}
} else {
if (!Regex("\\.gz$").Match(namelist[i]->d_name)) {
t = strlen(namelist[i]->d_name);
- Archive->write(&t, 1);
+ Archive->writeU8(t);
Archive->write(namelist[i]->d_name, t);
process_file(dirname + "/" + namelist[i]->d_name);
}
@@ -143,7 +140,7 @@ void process_directory(const String & dirname) throw (GeneralException) {
free((void *)namelist);
t = 0;
- Archive->write(&t, 1);
+ Archive->writeU8(t);
}
void build_archive(const String & dirname) {