Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <tigcclib.h>
- #include "arch.h"
- ARCH * afopen (const char *filename)
- {
- FILE * f;
- ARCH * arch = NULL;
- if ((f = fopen (filename, "rb")) == NULL)
- return NULL;
- if ((arch = (ARCH*)malloc(sizeof(ARCH))))
- {
- arch->archfile = f;
- arch->sublvl = 0;
- fseek(f,0,SEEK_SET);
- arch->set = ftell(f);
- fseek(f,0,SEEK_END);
- arch->size = ftell(f) - arch->set;
- arch->apos = 0;
- }
- return arch;
- }
- ARCH * aopen (ARCH * arch, const char * subfilename)
- {
- ARCH *a, *b;
- register unsigned short i, j;
- unsigned char nbfile;
- unsigned char filename[9];
- unsigned short subdiroffset = 0;
- unsigned char subdir[9];
- short apos;
- unsigned short filepos, filesize;
- a = (ARCH*)malloc(sizeof(ARCH));
- if (a == NULL)
- return NULL;
- for (i = 0; i < strlen (subfilename); i++)
- {
- if (subfilename[i] == '\\') {subdiroffset = i; break;}
- }
- if (i > 8)
- {
- free (a);
- return NULL;
- }
- strncpy (subdir, subfilename, i);
- subdir[i] = 0;
- apos = arch->apos;
- aseek (arch, 0, SEEK_SET);
- aread (&nbfile, sizeof(unsigned char), 1, arch);
- for (i = 0; i < nbfile; i++)
- {
- aread (&filepos, sizeof(unsigned short), 1, arch);
- aread (&filesize, sizeof(unsigned short), 1, arch);
- for(j = 0; j < 8; j++)
- {
- aread (&(filename[j]), sizeof(unsigned char), 1, arch);
- if (filename[j] == 0)
- break;
- }
- if (j == 8)
- aseek (arch, 1, SEEK_CUR);
- filename[j] = 0;
- if (strcmp (filename, subdir) == 0)
- {
- a->archfile = arch->archfile;
- a->sublvl = arch->sublvl + 1;
- a->set = filepos + arch->set;
- a->apos = filepos + arch->set;
- a->size = filesize;
- arch->apos = apos;
- if (subdiroffset)
- {
- b = aopen (a, subfilename + subdiroffset + 1);
- free (a);
- a = b;
- }
- return a;
- }
- }
- arch->apos = apos;
- free (a);
- return NULL;
- }
- void aclose (ARCH * arch)
- {
- if (arch->sublvl == 0)
- fclose (arch->archfile);
- free (arch);
- }
- unsigned short aread (void * ptr, unsigned short size, unsigned short n, ARCH * arch)
- {
- unsigned short buffsize;
- unsigned short retval;
- unsigned short maxbuffsize = arch->set + arch->size - arch->apos;
- if ((size * n) <= maxbuffsize)
- {
- buffsize = size * n;
- retval = n;
- }
- else
- {
- buffsize = maxbuffsize;
- retval = maxbuffsize / size;
- }
- if (buffsize)
- {
- fseek (arch->archfile, arch->apos, SEEK_SET);
- fread (ptr, buffsize, 1, arch->archfile);
- arch->apos = arch->apos + buffsize;
- return retval;
- }
- return 0;
- }
- short aseek (ARCH * arch, short offset, short whence)
- {
- unsigned short newpos;
- switch (whence)
- {
- case SEEK_SET:
- newpos = arch->set + offset;
- if ((newpos >= arch->set) && (newpos < (arch->set + arch->size)))
- arch->apos = newpos;
- else
- return 1;
- break;
- case SEEK_CUR:
- newpos = arch->apos + offset;
- if ((newpos >= arch->set) && (newpos < (arch->set + arch->size)))
- arch->apos = newpos;
- else
- return 1;
- break;
- case SEEK_END:
- newpos = arch->set + arch->size + offset;
- if ((newpos >= arch->set) && (newpos < (arch->set + arch->size)))
- arch->apos = newpos;
- else
- return 1;
- break;
- default: return 1;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement