#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"

/* set by scan_fat, used by next */
short *fat = NULL;
char *fat_flag_map = NULL;
unsigned int *fat_remap = NULL;
int fat_remap_size;
int type_size, nb_clusters, bad_cluster_value;

void free_all() {
#define FREE(p) if (p) free(p), p = NULL;
  FREE(fat);
  FREE(fat_flag_map);
  FREE(fat_remap);
#undef FREE
}

unsigned int next(unsigned int cluster) {
  short *p = fat + type_size * cluster;
  if (!fat) {
    free_all();
    croak("fat::next: trying to use null pointer");
  }
  if (cluster >= nb_clusters + 2) {
    free_all();
    croak("fat::next: cluster %d outside filesystem", cluster);
  }
  return type_size == 1 ? *p : *((unsigned int *) p);
}

void set_next(unsigned int cluster, unsigned int val) {
  short *p = fat + type_size * cluster;
  if (!fat) {
    free_all();
    croak("fat::set_next: trying to use null pointer");
  }
  if (cluster >= nb_clusters + 2) {
    free_all();
    croak("fat::set_next: cluster %d outside filesystem", cluster);
  }
  if (type_size == 1)
    *p = val;
  else
    *((unsigned int *) p) = val;
}

MODULE = resize_fat::c_rewritten PACKAGE = resize_fat::c_rewritten

PROTOTYPES: DISABLE


void
read_fat(fd, offset, size, magic)
  int fd
  int offset
  int size
  unsigned char magic
  PPCODE:
{
  fat = (short *) malloc(size);
  if (!fat) {
    free_all();
    croak("read_fat: not enough memory");
  }
  if (lseek(fd, offset, SEEK_SET) != offset ||
      read(fd, fat, size) != size) {
    free_all();
    croak("read_fat: reading FAT failed");
  }
  if (magic != *(unsigned char *) fat) {
    free_all();
    croak("read_fat: FAT has invalid signature");
  }
}

void
write_fat(fd, size)
  int fd
  int size
  PPCODE:
{
  if (write(fd, fat, size) != size) {
    free_all();
    croak("write_fat: write failed");
  }
}

void
free_all()
  PPCODE:
  free_all();

void
scan_fat(nb_clusters_, type_size_)
  int nb_clusters_
  int type_size_
  PPCODE:
{
  unsigned int v;  
  int free = 0, bad = 0, used = 0;
  short *p;
  
  type_size = type_size_; nb_clusters = nb_clusters_;
  bad_cluster_value = type_size == 32 ? 0x0ffffff7 : 0xfff7;

  if (type_size % 16) {
    free_all();
    croak("scan_fat: unable to handle FAT%d", type_size);
  }
  type_size /= 16;

  for (p = fat + 2 * type_size; p < fat + type_size * (nb_clusters + 2); p += type_size) {
    v = type_size == 1 ? *p : *((unsigned int *) p);

    if (v == 0) free++;
    else if (v == bad_cluster_value) bad++;
  }
  used = nb_clusters - free - bad;
  EXTEND(SP, 3);
  PUSHs(sv_2mortal(newSViv(free)));
  PUSHs(sv_2mortal(newSViv(bad)));
  PUSHs(sv_2mortal(newSViv(used)));
}

unsigned int
next(unused, cluster)
  void *unused
  unsigned int cluster
  CODE:
  RETVAL = next(cluster);
  OUTPUT:
  RETVAL

void
set_next(unused, cluster, val)
  void *unused
  unsigned int cluster
  unsigned int val
  CODE:
  set_next(cluster, val);

void
allocate_fat_flag(size)
  int size
  CODE:
  fat_flag_map = calloc(size, 1);
  if (!fat_flag_map) {
    free_all();
    croak("allocate_fat_flag: not enough memory");
  }

int
checkFat(cluster, type, name)
  unsigned int cluster
  int type
  char *name
  CODE:
  int nb = 0;

  if (!fat_flag_map) {
    free_all();
    croak("Bad FAT: trying to use null pointer");
  }
  for (; cluster < bad_cluster_value; cluster = next(cluster)) {
    if (cluster == 0) {
      free_all();
      croak("Bad FAT: unterminated chain for %s\n", name);
    }
    if (cluster >= nb_clusters + 2) {
      free_all();
      croak("Bad FAT: chain outside filesystem for %s\n", name);
    }
    if (fat_flag_map[cluster]) {
      free_all();
      croak("Bad FAT: cluster %d is cross-linked for %s\n", cluster, name);
    }
    fat_flag_map[cluster] = type;
    nb++;
  }
  RETVAL = nb;
  OUTPUT:
  RETVAL

unsigned int
flag(cluster)
  unsigned int cluster
  CODE:
  if (!fat_flag_map) {
    free_all();
    croak("Bad FAT: trying to use null pointer");
  }
  if (cluster >= nb_clusters + 2) {
    free_all();
    croak("Bad FAT: going outside filesystem");
  }
  RETVAL = fat_flag_map[cluster];
  OUTPUT:
  RETVAL

void
set_flag(cluster, flag)
  unsigned int cluster
  int flag
  CODE:
  if (!fat_flag_map) {
    free_all();
    croak("Bad FAT: trying to use null pointer");
  }
  if (cluster >= nb_clusters + 2) {
    free_all();
    croak("Bad FAT: going outside filesystem");
  }
  fat_flag_map[cluster] = flag;

void
allocate_fat_remap(size)
  int size
  CODE:
  fat_remap_size = size;
  fat_remap = (unsigned int *) calloc(size, sizeof(unsigned int *));
  if (!fat_remap) {
    free_all();
    croak("allocate_fat_remap: not enough memory");
  }

unsigned int
fat_remap(cluster)
  unsigned int cluster
  CODE:
  if (!fat_remap) {
    free_all();
    croak("fat_remap: trying to use null pointer");
  }
  if (cluster >= bad_cluster_value) {
    RETVAL = cluster; /* special cases */
  } else {
    if (cluster >= fat_remap_size) {
      free_all();
      croak("fat_remap: cluster %d >= %d in fat_remap", cluster, fat_remap_size);
    }
    RETVAL = fat_remap[cluster];
  }
  OUTPUT:
  RETVAL

void
set_fat_remap(cluster, val)
  unsigned int cluster
  unsigned int val
  CODE:
  if (!fat_remap) {
    free_all();
    croak("set_fat_remap: trying to use null pointer");
  }
  if (cluster >= fat_remap_size) {
    free_all();
    croak("set_fat_remap: cluster %d >= %d in set_fat_remap", cluster, fat_remap_size);
  }
  if (val < bad_cluster_value && val >= fat_remap_size) {
    free_all();
    croak("set_fat_remap: remapping cluster %d to cluster %d >= %d in set_fat_remap", cluster, val, fat_remap_size);
  }
  fat_remap[cluster] = val;
 size='10' name='q' value=''/>
<input type='submit' value='search'/>
</form>
</td></tr></table>
<div class='path'>path: <a href='/software/drakx/commit/?h=V9_3_15mdk&amp;id=411e0e9d500d91cf111fb93209acd7d461ee2453'>root</a>/<a href='/software/drakx/commit/perl-install?h=V9_3_15mdk&amp;id=411e0e9d500d91cf111fb93209acd7d461ee2453'>perl-install</a>/<a href='/software/drakx/commit/perl-install/share?h=V9_3_15mdk&amp;id=411e0e9d500d91cf111fb93209acd7d461ee2453'>share</a>/<a href='/software/drakx/commit/perl-install/share/po?h=V9_3_15mdk&amp;id=411e0e9d500d91cf111fb93209acd7d461ee2453'>po</a>/<a href='/software/drakx/commit/perl-install/share/po/tg.po?h=V9_3_15mdk&amp;id=411e0e9d500d91cf111fb93209acd7d461ee2453'>tg.po</a></div><div class='content'><div class='cgit-panel'><b>diff options</b><form method='get'><input type='hidden' name='h' value='V9_3_15mdk'/><input type='hidden' name='id' value='411e0e9d500d91cf111fb93209acd7d461ee2453'/><table><tr><td colspan='2'/></tr><tr><td class='label'>context:</td><td class='ctrl'><select name='context' onchange='this.form.submit();'><option value='1'>1</option><option value='2'>2</option><option value='3' selected='selected'>3</option><option value='4'>4</option><option value='5'>5</option><option value='6'>6</option><option value='7'>7</option><option value='8'>8</option><option value='9'>9</option><option value='10'>10</option><option value='15'>15</option><option value='20'>20</option><option value='25'>25</option><option value='30'>30</option><option value='35'>35</option><option value='40'>40</option></select></td></tr><tr><td class='label'>space:</td><td class='ctrl'><select name='ignorews' onchange='this.form.submit();'><option value='0' selected='selected'>include</option><option value='1'>ignore</option></select></td></tr><tr><td class='label'>mode:</td><td class='ctrl'><select name='dt' onchange='this.form.submit();'><option value='0' selected='selected'>unified</option><option value='1'>ssdiff</option><option value='2'>stat only</option></select></td></tr><tr><td/><td class='ctrl'><noscript><input type='submit' value='reload'/></noscript></td></tr></table></form></div><table summary='commit info' class='commit-info'>
<tr><th>author</th><td>Pablo Saratxaga &lt;pablo@mandriva.com&gt;</td><td class='right'>2003-05-16 15:55:38 +0000</td></tr>
<tr><th>committer</th><td>Pablo Saratxaga &lt;pablo@mandriva.com&gt;</td><td class='right'>2003-05-16 15:55:38 +0000</td></tr>
<tr><th>commit</th><td colspan='2' class='sha1'><a href='/software/drakx/commit/perl-install/share/po/tg.po?h=V9_3_15mdk&amp;id=411e0e9d500d91cf111fb93209acd7d461ee2453'>411e0e9d500d91cf111fb93209acd7d461ee2453</a> (<a href='/software/drakx/patch/perl-install/share/po/tg.po?id=411e0e9d500d91cf111fb93209acd7d461ee2453'>patch</a>)</td></tr>
<tr><th>tree</th><td colspan='2' class='sha1'><a href='/software/drakx/tree/?h=V9_3_15mdk&amp;id=411e0e9d500d91cf111fb93209acd7d461ee2453'>2ad2bc9508b90d8be8a0dc571ee75b43be5b0019</a> /<a href='/software/drakx/tree/perl-install/share/po/tg.po?h=V9_3_15mdk&amp;id=411e0e9d500d91cf111fb93209acd7d461ee2453'>perl-install/share/po/tg.po</a></td></tr>
<tr><th>parent</th><td colspan='2' class='sha1'><a href='/software/drakx/commit/perl-install/share/po/tg.po?h=V9_3_15mdk&amp;id=1af0da3b2f30c11993aaefd4a9271768cb2e1bf3'>1af0da3b2f30c11993aaefd4a9271768cb2e1bf3</a> (<a href='/software/drakx/diff/perl-install/share/po/tg.po?h=V9_3_15mdk&amp;id=411e0e9d500d91cf111fb93209acd7d461ee2453&amp;id2=1af0da3b2f30c11993aaefd4a9271768cb2e1bf3'>diff</a>)</td></tr><tr><th>download</th><td colspan='2' class='sha1'><a href='/software/drakx/snapshot/drakx-411e0e9d500d91cf111fb93209acd7d461ee2453.tar'>drakx-411e0e9d500d91cf111fb93209acd7d461ee2453.tar</a><br/><a href='/software/drakx/snapshot/drakx-411e0e9d500d91cf111fb93209acd7d461ee2453.tar.gz'>drakx-411e0e9d500d91cf111fb93209acd7d461ee2453.tar.gz</a><br/><a href='/software/drakx/snapshot/drakx-411e0e9d500d91cf111fb93209acd7d461ee2453.tar.bz2'>drakx-411e0e9d500d91cf111fb93209acd7d461ee2453.tar.bz2</a><br/><a href='/software/drakx/snapshot/drakx-411e0e9d500d91cf111fb93209acd7d461ee2453.tar.xz'>drakx-411e0e9d500d91cf111fb93209acd7d461ee2453.tar.xz</a><br/><a href='/software/drakx/snapshot/drakx-411e0e9d500d91cf111fb93209acd7d461ee2453.zip'>drakx-411e0e9d500d91cf111fb93209acd7d461ee2453.zip</a><br/></td></tr></table>
<div class='commit-subject'>updated pot file</div><div class='commit-msg'></div><div class='diffstat-header'><a href='/software/drakx/diff/?h=V9_3_15mdk&amp;id=411e0e9d500d91cf111fb93209acd7d461ee2453'>Diffstat</a> (limited to 'perl-install/share/po/tg.po')</div><table summary='diffstat' class='diffstat'><tr><td class='mode'>-rw-r--r--</td><td class='upd'><a href='/software/drakx/diff/perl-install/share/po/tg.po?h=V9_3_15mdk&amp;id=411e0e9d500d91cf111fb93209acd7d461ee2453'>perl-install/share/po/tg.po</a></td><td class='right'>19825</td><td class='graph'><table summary='file diffstat' width='100%'><tr><td class='add' style='width: 50.1%;'/><td class='rem' style='width: 49.9%;'/><td class='none' style='width: 0.0%;'/></tr></table></td></tr>
</table><div class='diffstat-summary'>1 files changed, 9926 insertions, 9899 deletions</div><table summary='diff' class='diff'><tr><td><div class='head'>diff --git a/perl-install/share/po/tg.po b/perl-install/share/po/tg.po<br/>index 1212577b8..536451b3f 100644<br/>--- a/<a href='/software/drakx/tree/perl-install/share/po/tg.po?h=V9_3_15mdk&amp;id=1af0da3b2f30c11993aaefd4a9271768cb2e1bf3'>perl-install/share/po/tg.po</a><br/>+++ b/<a href='/software/drakx/tree/perl-install/share/po/tg.po?h=V9_3_15mdk&amp;id=411e0e9d500d91cf111fb93209acd7d461ee2453'>perl-install/share/po/tg.po</a></div>