2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
2009-08-17 12:41:51 +02:00
|
|
|
gb_arch_temp.h
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-08-17 12:41:51 +02:00
|
|
|
(c) 2000-2009 Benoît Minisini <gambas@users.sourceforge.net>
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
2009-08-17 12:41:51 +02:00
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
2007-12-30 17:41:49 +01:00
|
|
|
any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#define __GBX_ARCH_C
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "gb_common.h"
|
|
|
|
#include "gb_common_swap.h"
|
|
|
|
#include "gb_error.h"
|
|
|
|
#include "gb_alloc.h"
|
|
|
|
#include "gb_arch.h"
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef PROJECT_EXEC
|
|
|
|
#define E_ARCH "Bad archive: &1"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static bool _swap = FALSE;
|
|
|
|
static const char *_path;
|
|
|
|
|
|
|
|
static void arch_error(const char *msg)
|
|
|
|
{
|
|
|
|
const char *name = FILE_get_name(_path);
|
|
|
|
if (msg == NULL)
|
|
|
|
THROW(E_ARCH, name, strerror(errno));
|
|
|
|
else
|
|
|
|
THROW(E_ARCH, name, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void read_at(ARCH *arch, int pos, void *buf, int len)
|
|
|
|
{
|
|
|
|
memcpy(buf, &arch->addr[pos], len);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void load_arch(ARCH *arch, const char *path)
|
|
|
|
{
|
2010-05-27 15:51:09 +02:00
|
|
|
int len, lens;
|
2007-12-30 17:41:49 +01:00
|
|
|
int i;
|
|
|
|
int pos;
|
2010-05-28 21:33:05 +02:00
|
|
|
int pos_sort;
|
2007-12-30 17:41:49 +01:00
|
|
|
struct stat info;
|
2008-01-17 22:39:26 +01:00
|
|
|
#ifdef OS_64BITS
|
|
|
|
ARCH_SYMBOL_32 *sym;
|
|
|
|
#endif
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
_path = path;
|
|
|
|
|
|
|
|
arch->fd = open(path, O_RDONLY);
|
|
|
|
if (arch->fd < 0)
|
|
|
|
THROW(E_OPEN, path, strerror(errno));
|
|
|
|
|
|
|
|
if (fstat(arch->fd, &info) < 0)
|
|
|
|
THROW(E_OPEN, path, strerror(errno));
|
|
|
|
|
|
|
|
arch->length = info.st_size;
|
|
|
|
arch->addr = mmap(NULL, arch->length, PROT_READ, MAP_PRIVATE, arch->fd, 0);
|
|
|
|
if (arch->addr == MAP_FAILED)
|
|
|
|
THROW(E_OPEN, path, strerror(errno));
|
|
|
|
|
|
|
|
//fprintf(stderr, "mmap: %s\n", path);
|
|
|
|
|
|
|
|
/* Header */
|
|
|
|
|
|
|
|
read_at(arch, 32, &arch->header, sizeof(ARCH_HEADER));
|
|
|
|
_swap = arch->header.magic != ARCH_MAGIC;
|
|
|
|
|
|
|
|
if (_swap)
|
2008-01-05 15:07:21 +01:00
|
|
|
SWAP_ints((int *)&arch->header, 6);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-07-12 01:13:11 +02:00
|
|
|
if (arch->header.magic != ARCH_MAGIC)
|
|
|
|
arch_error("not an archive");
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
//if (arch->header.version != ARCH_VERSION)
|
|
|
|
// arch_error("bad version");
|
|
|
|
|
|
|
|
/* Strings */
|
|
|
|
|
|
|
|
len = arch->header.pos_table - arch->header.pos_string;
|
|
|
|
if (len <= 0)
|
|
|
|
arch_error("corrupted header");
|
|
|
|
|
|
|
|
ALLOC(&arch->string, len, "ARCH_init");
|
|
|
|
read_at(arch, arch->header.pos_string, arch->string, len);
|
|
|
|
|
|
|
|
/* File names table */
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
len = arch->header.n_symbol * sizeof(ARCH_SYMBOL);
|
2010-05-27 15:51:09 +02:00
|
|
|
lens = arch->header.n_symbol * sizeof(ushort);
|
|
|
|
if (len <= 0 || lens <= 0)
|
2008-01-17 22:39:26 +01:00
|
|
|
arch_error("corrupted header");
|
|
|
|
|
|
|
|
ALLOC(&arch->symbol, len, "ARCHIVE_load");
|
2010-05-27 15:51:09 +02:00
|
|
|
ALLOC(&arch->sort, lens, "ARCHIVE_load");
|
2008-01-17 22:39:26 +01:00
|
|
|
|
|
|
|
#ifdef OS_64BITS
|
2010-05-28 21:33:05 +02:00
|
|
|
sym = (ARCH_SYMBOL_32 *)&arch->addr[arch->header.pos_table];
|
|
|
|
for (i = 0; i < arch->header.n_symbol; i++, sym++)
|
|
|
|
{
|
|
|
|
//arch->symbol[i].sym.sort = sym->sym.sort;
|
|
|
|
arch->symbol[i].sym.len = sym->sym.len;
|
|
|
|
arch->symbol[i].sym.name = (char *)(intptr_t)sym->sym.name;
|
|
|
|
arch->symbol[i].pos = sym->pos;
|
|
|
|
arch->symbol[i].len = sym->len;
|
|
|
|
}
|
|
|
|
pos_sort = arch->header.pos_table + arch->header.n_symbol * sizeof(ARCH_SYMBOL_32);
|
2008-01-17 22:39:26 +01:00
|
|
|
#else
|
2010-05-28 21:33:05 +02:00
|
|
|
read_at(arch, arch->header.pos_table, arch->symbol, len);
|
|
|
|
pos_sort = arch->header.pos_table + len;
|
2008-01-17 22:39:26 +01:00
|
|
|
#endif
|
|
|
|
|
2010-05-28 21:33:05 +02:00
|
|
|
read_at(arch, pos_sort, arch->sort, lens);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
/* String relocation */
|
|
|
|
|
|
|
|
pos = 0;
|
2009-07-18 17:50:51 +02:00
|
|
|
if (_swap)
|
|
|
|
{
|
|
|
|
for (i = 0; i < arch->header.n_symbol; i++)
|
|
|
|
{
|
2010-05-27 15:51:09 +02:00
|
|
|
SWAP_short((short *)&arch->sort[i]);
|
2009-07-18 17:50:51 +02:00
|
|
|
SWAP_short((short *)&arch->symbol[i].sym.len);
|
2008-01-05 15:07:21 +01:00
|
|
|
SWAP_int(&arch->symbol[i].pos);
|
|
|
|
SWAP_int(&arch->symbol[i].len);
|
2009-07-18 17:50:51 +02:00
|
|
|
arch->symbol[i].sym.name = &arch->string[pos];
|
|
|
|
pos += arch->symbol[i].sym.len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (i = 0; i < arch->header.n_symbol; i++)
|
|
|
|
{
|
|
|
|
arch->symbol[i].sym.name = &arch->string[pos];
|
|
|
|
pos += arch->symbol[i].sym.len;
|
|
|
|
}
|
|
|
|
}
|
2010-05-27 15:51:09 +02:00
|
|
|
|
2010-12-05 21:17:24 +01:00
|
|
|
_path = NULL;
|
|
|
|
|
2010-05-27 15:51:09 +02:00
|
|
|
#if 0
|
|
|
|
for (i = 0; i < arch->header.n_symbol; i++)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%i (%i) %.*s\n", i, arch->sort[i], arch->symbol[i].sym.len, arch->symbol[i].sym.name);
|
|
|
|
}
|
|
|
|
#endif
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-08 21:57:50 +02:00
|
|
|
ARCH *ARCH_open(const char *path)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
ARCH *arch;
|
|
|
|
|
|
|
|
ALLOC_ZERO(&arch, sizeof(ARCH), "ARCH_open");
|
|
|
|
|
|
|
|
load_arch(arch, path);
|
|
|
|
|
|
|
|
return arch;
|
|
|
|
}
|
|
|
|
|
2009-07-08 21:57:50 +02:00
|
|
|
void ARCH_close(ARCH *arch)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
if (arch->fd)
|
|
|
|
{
|
|
|
|
FREE(&arch->string, "ARCH_close");
|
|
|
|
FREE(&arch->symbol, "ARCH_close");
|
2010-05-27 15:51:09 +02:00
|
|
|
FREE(&arch->sort, "ARCH_close");
|
2007-12-30 17:41:49 +01:00
|
|
|
munmap(arch->addr, arch->length);
|
|
|
|
close(arch->fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
FREE(&arch, "ARCH_close");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-09-27 21:34:27 +02:00
|
|
|
static bool get_absolute_path(const char *path, int len_path, char *abs_path, int *len_abs_path)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
const char *p, *lp;
|
2010-09-27 21:34:27 +02:00
|
|
|
char *ap, *apm;
|
2007-12-30 17:41:49 +01:00
|
|
|
char c;
|
|
|
|
int rest;
|
2010-09-27 21:34:27 +02:00
|
|
|
bool err = FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
p = path;
|
|
|
|
lp = p;
|
|
|
|
ap = abs_path;
|
2010-09-27 21:34:27 +02:00
|
|
|
apm = &abs_path[PATH_MAX];
|
2007-12-30 17:41:49 +01:00
|
|
|
*len_abs_path = 0;
|
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
rest = &path[len_path] - p;
|
|
|
|
if (rest <= 0)
|
|
|
|
break;
|
2010-09-27 21:34:27 +02:00
|
|
|
|
|
|
|
if (ap >= apm)
|
|
|
|
{
|
|
|
|
err = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
c = *p;
|
|
|
|
|
|
|
|
if (p == lp && c == '.')
|
|
|
|
{
|
|
|
|
if (rest == 1 || p[1] == '/')
|
|
|
|
{
|
|
|
|
if (rest == 1)
|
|
|
|
p++;
|
|
|
|
else
|
|
|
|
p += 2;
|
|
|
|
|
|
|
|
lp = p;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (rest >= 2 && p[1] == '.' && (rest == 2 || p[2] == '/'))
|
|
|
|
{
|
|
|
|
if (ap > abs_path)
|
|
|
|
{
|
|
|
|
ap--; // Jumps the last '/'
|
|
|
|
while (ap > abs_path)
|
|
|
|
{
|
|
|
|
ap--;
|
|
|
|
if (*ap == '/')
|
|
|
|
{
|
|
|
|
ap++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
p += 3;
|
|
|
|
lp = p;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*ap++ = c;
|
|
|
|
p++;
|
|
|
|
|
|
|
|
if (c == '/')
|
|
|
|
lp = p;
|
|
|
|
}
|
|
|
|
|
|
|
|
*len_abs_path = ap - abs_path;
|
|
|
|
*ap = 0;
|
2010-09-27 21:34:27 +02:00
|
|
|
return err;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2009-07-08 21:57:50 +02:00
|
|
|
bool ARCH_find(ARCH *arch, const char *path, int len_path, ARCH_FIND *find)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
int ind;
|
|
|
|
ARCH_SYMBOL *sym;
|
2010-01-28 11:55:24 +01:00
|
|
|
char tpath[PATH_MAX];
|
2007-12-30 17:41:49 +01:00
|
|
|
int len_tpath;
|
|
|
|
|
|
|
|
if (len_path <= 0)
|
|
|
|
len_path = strlen(path);
|
|
|
|
|
2010-09-27 21:34:27 +02:00
|
|
|
if (get_absolute_path(path, len_path, tpath, &len_tpath))
|
|
|
|
return TRUE;
|
2010-04-05 03:44:43 +02:00
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
if (len_tpath == 0)
|
|
|
|
{
|
|
|
|
find->sym = NULL;
|
2010-07-06 00:17:49 +02:00
|
|
|
find->index = NO_SYMBOL;
|
2007-12-30 17:41:49 +01:00
|
|
|
find->pos = -1;
|
|
|
|
find->len = 0;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2008-11-02 03:40:10 +01:00
|
|
|
//if (arch->header.version == 2)
|
|
|
|
//{
|
2007-12-30 17:41:49 +01:00
|
|
|
char *p;
|
2010-01-28 11:55:24 +01:00
|
|
|
char tpath2[len_tpath + 8];
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
p = index(tpath + 1, '/');
|
|
|
|
if (!p)
|
|
|
|
break;
|
2008-01-06 21:06:44 +01:00
|
|
|
|
2010-06-05 01:48:53 +02:00
|
|
|
ind = SYMBOL_find(arch->symbol, arch->sort, arch->header.n_symbol, sizeof(ARCH_SYMBOL), TF_NORMAL, tpath, p - tpath, 0);
|
2007-12-30 17:41:49 +01:00
|
|
|
if (ind == NO_SYMBOL)
|
|
|
|
break;
|
2008-01-06 21:06:44 +01:00
|
|
|
|
|
|
|
sym = &arch->symbol[ind];
|
2008-01-05 02:18:28 +01:00
|
|
|
len_tpath = snprintf(tpath2, sizeof(tpath2),"/%d:%s", ind, p + 1);
|
2008-02-19 00:07:24 +01:00
|
|
|
strcpy(tpath, tpath2);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2010-06-05 01:48:53 +02:00
|
|
|
ind = SYMBOL_find(arch->symbol, arch->sort, arch->header.n_symbol, sizeof(ARCH_SYMBOL), TF_NORMAL, tpath, len_tpath, 0);
|
2008-11-02 03:40:10 +01:00
|
|
|
//}
|
|
|
|
//else
|
|
|
|
// SYMBOL_find_old(arch->symbol, arch->header.n_symbol, sizeof(ARCH_SYMBOL), TF_NORMAL, tpath, len_tpath, 0, &ind);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
if (ind == NO_SYMBOL)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
sym = &arch->symbol[ind];
|
|
|
|
|
|
|
|
find->sym = sym;
|
|
|
|
find->pos = sym->pos;
|
|
|
|
find->len = sym->len;
|
2008-11-02 03:40:10 +01:00
|
|
|
find->index = ind;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-08 21:57:50 +02:00
|
|
|
bool ARCH_read(ARCH *arch, int pos, void *buffer, int len)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
/*if (lseek(arch->fd, pos, SEEK_SET) < 0)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
if (read(arch->fd, buffer, len) != len)
|
|
|
|
return TRUE;*/
|
|
|
|
|
|
|
|
memcpy(buffer, &arch->addr[pos], len);
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|