/*
 *  smoke16/libObj/src/a_out.c - (de-)serialize a_out16_* structures
 *  $Revision: 1.4 $
 *  Copyright 2000, Benjamin Sittler
 *
 *  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
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) 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.
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <smoke16/libObj.h>

/*
 * WARNING:
 * fput16exec() silently promotes toolversion from "v0" to "v1" in some cases!
 */

/* exec headers */

size_t
fget16exec(struct a_out16_exec *exec, size_t nmemb, FILE *stream)
{
  size_t count;

  for (count = 0; count < nmemb; count ++) {
    if (! (fget_u16(&(exec[count] . a_info), 1, stream) &&
	   fget_u16(&(exec[count] . a_magic), 1, stream)))
      break;
    if (SMOKE16_A_TOOLVERSION(exec[count]) == 0) {
      u16 old_text, old_data;

      if (! (fget_u16(&old_text, 1, stream) &&
	     fget_u16(&old_data, 1, stream)))
	break;
      exec[count] . a_text = (u32)old_text;
      exec[count] . a_data = (u32)old_data;
    } else {
      if (! (fget_u32(&(exec[count] . a_text), 1, stream) &&
	     fget_u32(&(exec[count] . a_data), 1, stream)))
	break;
    }
    if (! (fget_u16(&(exec[count] . a_bss), 1, stream) &&
	   fget_u16(&(exec[count] . a_syms), 1, stream) &&
	   fget_u16(&(exec[count] . a_entry), 1, stream) &&
	   fget_u16(&(exec[count] . a_trsize), 1, stream) &&
	   fget_u16(&(exec[count] . a_drsize), 1, stream)))
      break;
  }
  return count;
}

size_t
fput16exec(struct a_out16_exec *exec, size_t nmemb, FILE *stream)
{
  size_t count;

  for (count = 0; count < nmemb; count ++) {
    if ((SMOKE16_A_TOOLVERSION(exec[count]) == 0) &&
	((exec[count] . a_text & 0xFFFF0000UL) ||
	 (exec[count] . a_data & 0xFFFF0000UL)))
      SMOKE16_SET_A_TOOLVERSION(exec[count], 1);
    if (! (fput_u16(&(exec[count] . a_info), 1, stream) &&
	   fput_u16(&(exec[count] . a_magic), 1, stream)))
      break;
    if (SMOKE16_A_TOOLVERSION(exec[count]) == 0) {
      u16 old_text, old_data;

      old_text = (u16)exec[count] . a_text;
      old_data = (u16)exec[count] . a_data;
      if (! (fput_u16(&old_text, 1, stream) &&
	     fput_u16(&old_data, 1, stream)))
	break;
    } else {
      if (! (fput_u32(&(exec[count] . a_text), 1, stream) &&
	     fput_u32(&(exec[count] . a_data), 1, stream)))
	break;
    }
    if (! (fput_u16(&(exec[count] . a_bss), 1, stream) &&
	   fput_u16(&(exec[count] . a_syms), 1, stream) &&
	   fput_u16(&(exec[count] . a_entry), 1, stream) &&
	   fput_u16(&(exec[count] . a_trsize), 1, stream) &&
	   fput_u16(&(exec[count] . a_drsize), 1, stream)))
      break;
  }
  return count;
}

/* LMAGIC directory entries */

size_t
fget16dirent(struct a_out16_dirent *dirent, size_t nmemb, FILE *stream)
{
  size_t count;

  for (count = 0; count < nmemb; count ++)
  {
    if (! (fget_u16(&(dirent[count] . d_strx), 1, stream) &&
	   fget_u16(&(dirent[count] . d_magic), 1, stream) &&
	   fget_u32(&(dirent[count] . d_value), 1, stream) &&
	   fget_u32(&(dirent[count] . d_size), 1, stream) &&
	   fget_s32(&(dirent[count] . d_mtime), 1, stream)))
      break;
  }
  return count;
}

size_t
fput16dirent(struct a_out16_dirent *dirent, size_t nmemb, FILE *stream)
{
  size_t count;

  for (count = 0; count < nmemb; count ++)
  {
    if (! (fput_u16(&(dirent[count] . d_strx), 1, stream) &&
	   fput_u16(&(dirent[count] . d_magic), 1, stream) &&
	   fput_u32(&(dirent[count] . d_value), 1, stream) &&
	   fput_u32(&(dirent[count] . d_size), 1, stream) &&
	   fput_s32(&(dirent[count] . d_mtime), 1, stream)))
      break;
  }
  return count;
}

/* relocations */

size_t
fget16reloc(struct a_out16_reloc *reloc, size_t nmemb, FILE *stream)
{
  size_t count;

  for (count = 0; count < nmemb; count ++)
    if (! (fget_u16(&(reloc[count] . r_addr_high), 1, stream) &&
	   fget_u16(&(reloc[count] . r_addr_low), 1, stream) &&
	   fget_u16(&(reloc[count] . r_index), 1, stream) &&
	   fget_u16(&(reloc[count] . r_info), 1, stream) &&
	   fget_u16(&(reloc[count] . r_value), 1, stream)))
      break;
  return count;
}

size_t
fput16reloc(struct a_out16_reloc *reloc, size_t nmemb, FILE *stream)
{
  size_t count;

  for (count = 0; count < nmemb; count ++)
    if (! (fput_u16(&(reloc[count] . r_addr_high), 1, stream) &&
	   fput_u16(&(reloc[count] . r_addr_low), 1, stream) &&
	   fput_u16(&(reloc[count] . r_index), 1, stream) &&
	   fput_u16(&(reloc[count] . r_info), 1, stream) &&
	   fput_u16(&(reloc[count] . r_value), 1, stream)))
      break;
  return count;
}

/* symbols */

size_t
fget16nlist(struct a_out16_nlist *nlist, size_t nmemb, FILE *stream)
{
  size_t count;

  for (count = 0; count < nmemb; count ++)
    if (! (fget_u16(&(nlist[count] . n_strx), 1, stream) &&
	   fget_u8(&(nlist[count] . n_type), 1, stream) &&
	   fget_s8(&(nlist[count] . n_other), 1, stream) &&
	   fget_s16(&(nlist[count] . n_desc), 1, stream) &&
	   fget_u16(&(nlist[count] . n_value), 1, stream)))
      break;
  return count;
}

size_t
fput16nlist(struct a_out16_nlist *nlist, size_t nmemb, FILE *stream)
{
  size_t count;

  for (count = 0; count < nmemb; count ++)
    if (! (fput_u16(&(nlist[count] . n_strx), 1, stream) &&
	   fput_u8(&(nlist[count] . n_type), 1, stream) &&
	   fput_s8(&(nlist[count] . n_other), 1, stream) &&
	   fput_s16(&(nlist[count] . n_desc), 1, stream) &&
	   fput_u16(&(nlist[count] . n_value), 1, stream)))
      break;
  return count;
}

