/* Library function for scanning an archive file.
Copyright (C) 1987-2020 Free Software Foundation, Inc.
This file is part of GNU Make.

GNU Make 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 3 of the License, or (at your option) any later
version.

GNU Make 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, see <http://www.gnu.org/licenses/>.  */

#include "libc/sysv/consts/o.h"
#include "third_party/make/makeint.inc"
/* clang-format off */

#ifdef TEST
/* Hack, the real error() routine eventually pulls in die from main.c */
#define error(a, b, c, d)
#endif

/* On the sun386i and in System V rel 3, ar.h defines two different archive
   formats depending upon whether you have defined PORTAR (normal) or PORT5AR
   (System V Release 1).  There is no default, one or the other must be defined
   to have a nonzero value.  */

#if (!defined (PORTAR) || PORTAR == 0) && (!defined (PORT5AR) || PORT5AR == 0)
#undef  PORTAR
#ifdef M_XENIX
/* According to Jim Sievert <jas1@rsvl.unisys.com>, for SCO XENIX defining
   PORTAR to 1 gets the wrong archive format, and defining it to 0 gets the
   right one.  */
#define PORTAR 0
#else
#define PORTAR 1
#endif
#endif

/* On AIX, define these symbols to be sure to get both archive formats.
   AIX 4.3 introduced the "big" archive format to support 64-bit object
   files, so on AIX 4.3 systems we need to support both the "normal" and
   "big" archive formats.  An archive's format is indicated in the
   "fl_magic" field of the "FL_HDR" structure.  For a normal archive,
   this field will be the string defined by the AIAMAG symbol.  For a
   "big" archive, it will be the string defined by the AIAMAGBIG symbol
   (at least on AIX it works this way).

   Note: we'll define these symbols regardless of which AIX version
   we're compiling on, but this is okay since we'll use the new symbols
   only if they're present.  */
#ifdef _AIX
# define __AR_SMALL__
# define __AR_BIG__
#endif

#ifndef WINDOWS32
# if 0 && !defined (__ANDROID__) && !defined (__BEOS__)
# else
   /* These platforms don't have <ar.h> but have archives in the same format
    * as many other Unices.  This was taken from GNU binutils for BeOS.
    */
#  define ARMAG "!<arch>\n"     /* String that begins an archive file.  */
#  define SARMAG 8              /* Size of that string.  */
#  define ARFMAG "`\n"          /* String in ar_fmag at end of each header.  */
struct ar_hdr
  {
    char ar_name[16];           /* Member file name, sometimes / terminated. */
    char ar_date[12];           /* File date, decimal seconds since Epoch.  */
    char ar_uid[6], ar_gid[6];  /* User and group IDs, in ASCII decimal.  */
    char ar_mode[8];            /* File mode, in ASCII octal.  */
    char ar_size[10];           /* File size, in ASCII decimal.  */
    char ar_fmag[2];            /* Always contains ARFMAG.  */
  };
# endif
# define TOCHAR(_m)     (_m)
#else
/* These should allow us to read Windows (VC++) libraries (according to Frank
 * Libbrecht <frankl@abzx.belgium.hp.com>)
 */
# define ARMAG      IMAGE_ARCHIVE_START
# define SARMAG     IMAGE_ARCHIVE_START_SIZE
# define ar_hdr     _IMAGE_ARCHIVE_MEMBER_HEADER
# define ar_name    Name
# define ar_mode    Mode
# define ar_size    Size
# define ar_date    Date
# define ar_uid     UserID
# define ar_gid     GroupID
/* In Windows the member names have type BYTE so we must cast them.  */
# define TOCHAR(_m)     ((char *)(_m))
#endif

/* Cray's <ar.h> apparently defines this.  */
#ifndef AR_HDR_SIZE
# define   AR_HDR_SIZE  (sizeof (struct ar_hdr))
#endif

#include "third_party/make/output.h"

/* Takes three arguments ARCHIVE, FUNCTION and ARG.

   Open the archive named ARCHIVE, find its members one by one,
   and for each one call FUNCTION with the following arguments:
     archive file descriptor for reading the data,
     member name,
     member name might be truncated flag,
     member header position in file,
     member data position in file,
     member data size,
     member date,
     member uid,
     member gid,
     member protection mode,
     ARG.

   The descriptor is poised to read the data of the member
   when FUNCTION is called.  It does not matter how much
   data FUNCTION reads.

   If FUNCTION returns nonzero, we immediately return
   what FUNCTION returned.

   Returns -1 if archive does not exist,
   Returns -2 if archive has invalid format.
   Returns 0 if have scanned successfully.  */

long int
ar_scan (const char *archive, ar_member_func_t function, const void *arg)
{
#ifdef AIAMAG
  FL_HDR fl_header;
# ifdef AIAMAGBIG
  int big_archive = 0;
  FL_HDR_BIG fl_header_big;
# endif
#endif
  char *namemap = 0;
  int namemap_size = 0;
  int desc = open (archive, O_RDONLY, 0);
  if (desc < 0)
    return -1;

#ifdef SARMAG
  {
    char buf[SARMAG];
    int nread;
    nread = readbuf (desc, buf, SARMAG);
    if (nread != SARMAG || memcmp (buf, ARMAG, SARMAG))
      goto invalid;
  }
#else
#ifdef AIAMAG
  {
    int nread;
    nread = readbuf (desc, &fl_header, FL_HSZ);
    if (nread != FL_HSZ)
      goto invalid;

#ifdef AIAMAGBIG
    /* If this is a "big" archive, then set the flag and
       re-read the header into the "big" structure. */
    if (!memcmp (fl_header.fl_magic, AIAMAGBIG, SAIAMAG))
      {
        off_t o;

        big_archive = 1;

        /* seek back to beginning of archive */
        EINTRLOOP (o, lseek (desc, 0, 0));
        if (o < 0)
          goto invalid;

        /* re-read the header into the "big" structure */
        nread = readbuf (desc, &fl_header_big, FL_HSZ_BIG);
        if (nread != FL_HSZ_BIG)
          goto invalid;
      }
    else
#endif
       /* Check to make sure this is a "normal" archive. */
      if (memcmp (fl_header.fl_magic, AIAMAG, SAIAMAG))
        goto invalid;
  }
#else
  {
#ifndef M_XENIX
    int buf;
#else
    unsigned short int buf;
#endif
    int nread;
    nread = readbuf (desc, &buf, sizeof (buf));
    if (nread != sizeof (buf) || buf != ARMAG)
      goto invalid;
  }
#endif
#endif

  /* Now find the members one by one.  */
  {
#ifdef SARMAG
    long int member_offset = SARMAG;
#else
#ifdef AIAMAG
    long int member_offset;
    long int last_member_offset;

#ifdef AIAMAGBIG
    if ( big_archive )
      {
        sscanf (fl_header_big.fl_fstmoff, "%20ld", &member_offset);
        sscanf (fl_header_big.fl_lstmoff, "%20ld", &last_member_offset);
      }
    else
#endif
      {
        sscanf (fl_header.fl_fstmoff, "%12ld", &member_offset);
        sscanf (fl_header.fl_lstmoff, "%12ld", &last_member_offset);
      }

    if (member_offset == 0)
      {
        /* Empty archive.  */
        close (desc);
        return 0;
      }
#else
#ifndef M_XENIX
    long int member_offset = sizeof (int);
#else   /* Xenix.  */
    long int member_offset = sizeof (unsigned short int);
#endif  /* Not Xenix.  */
#endif
#endif

    while (1)
      {
        int nread;
        struct ar_hdr member_header;
#ifdef AIAMAGBIG
        struct ar_hdr_big member_header_big;
#endif
#ifdef AIAMAG
# define ARNAME_MAX 255
        char name[ARNAME_MAX + 1];
        int name_len;
        long int dateval;
        int uidval, gidval;
        long int data_offset;
#else
# define ARNAME_MAX (int)sizeof(member_header.ar_name)
        char namebuf[ARNAME_MAX + 1];
        char *name;
        int is_namemap;         /* Nonzero if this entry maps long names.  */
        int long_name = 0;
#endif
        long int eltsize;
        unsigned int eltmode;
        long int fnval;
        off_t o;

        EINTRLOOP (o, lseek (desc, member_offset, 0));
        if (o < 0)
          goto invalid;

#ifdef AIAMAG
#define       AR_MEMHDR_SZ(x) (sizeof(x) - sizeof (x._ar_name))

#ifdef AIAMAGBIG
        if (big_archive)
          {
            nread = readbuf (desc, &member_header_big,
                             AR_MEMHDR_SZ(member_header_big));

            if (nread != AR_MEMHDR_SZ(member_header_big))
              goto invalid;

            sscanf (member_header_big.ar_namlen, "%4d", &name_len);
            if (name_len < 1 || name_len > ARNAME_MAX)
              goto invalid;

            nread = readbuf (desc, name, name_len);
            if (nread != name_len)
              goto invalid;

            name[name_len] = '\0';

            sscanf (member_header_big.ar_date, "%12ld", &dateval);
            sscanf (member_header_big.ar_uid, "%12d", &uidval);
            sscanf (member_header_big.ar_gid, "%12d", &gidval);
            sscanf (member_header_big.ar_mode, "%12o", &eltmode);
            sscanf (member_header_big.ar_size, "%20ld", &eltsize);

            data_offset = (member_offset + AR_MEMHDR_SZ(member_header_big)
                           + name_len + 2);
          }
        else
#endif
          {
            nread = readbuf (desc, &member_header,
                             AR_MEMHDR_SZ(member_header));

            if (nread != AR_MEMHDR_SZ(member_header))
              goto invalid;

            sscanf (member_header.ar_namlen, "%4d", &name_len);
            if (name_len < 1 || name_len > ARNAME_MAX)
              goto invalid;

            nread = readbuf (desc, name, name_len);
            if (nread != name_len)
              goto invalid;

            name[name_len] = '\0';

            sscanf (member_header.ar_date, "%12ld", &dateval);
            sscanf (member_header.ar_uid, "%12d", &uidval);
            sscanf (member_header.ar_gid, "%12d", &gidval);
            sscanf (member_header.ar_mode, "%12o", &eltmode);
            sscanf (member_header.ar_size, "%12ld", &eltsize);

            data_offset = (member_offset + AR_MEMHDR_SZ(member_header)
                           + name_len + 2);
          }
        data_offset += data_offset % 2;

        fnval =
          (*function) (desc, name, 0,
                       member_offset, data_offset, eltsize,
                       dateval, uidval, gidval,
                       eltmode, arg);

#else   /* Not AIAMAG.  */
        nread = readbuf (desc, &member_header, AR_HDR_SIZE);
        if (nread == 0)
          /* No data left means end of file; that is OK.  */
          break;

        if (nread != AR_HDR_SIZE
#if defined(ARFMAG) || defined(ARFZMAG)
            || (
# ifdef ARFMAG
                memcmp (member_header.ar_fmag, ARFMAG, 2)
# else
                1
# endif
                &&
# ifdef ARFZMAG
                memcmp (member_header.ar_fmag, ARFZMAG, 2)
# else
                1
# endif
               )
#endif
            )
          goto invalid;

        name = namebuf;
        memcpy (name, member_header.ar_name, sizeof member_header.ar_name);
        {
          char *p = name + sizeof member_header.ar_name;
          do
            *p = '\0';
          while (p > name && *--p == ' ');

#ifndef AIAMAG
          /* If the member name is "//" or "ARFILENAMES/" this may be
             a list of file name mappings.  The maximum file name
             length supported by the standard archive format is 14
             characters.  This member will actually always be the
             first or second entry in the archive, but we don't check
             that.  */
          is_namemap = (!strcmp (name, "//")
                        || !strcmp (name, "ARFILENAMES/"));
#endif  /* Not AIAMAG. */

          /* On some systems, there is a slash after each member name.  */
          if (*p == '/')
            *p = '\0';

#ifndef AIAMAG
          /* If the member name starts with a space or a slash, this
             is an index into the file name mappings (used by GNU ar).
             Otherwise if the member name looks like #1/NUMBER the
             real member name appears in the element data (used by
             4.4BSD).  */
          if (! is_namemap
              && (name[0] == ' ' || name[0] == '/')
              && namemap != 0)
            {
              int name_off = atoi (name + 1);
              int name_len;

              if (name_off < 0 || name_off >= namemap_size)
                goto invalid;

              name = namemap + name_off;
              name_len = strlen (name);
              if (name_len < 1)
                goto invalid;
              long_name = 1;
            }
          else if (name[0] == '#'
                   && name[1] == '1'
                   && name[2] == '/')
            {
              int name_len = atoi (name + 3);

              if (name_len < 1)
                goto invalid;

              name = alloca (name_len + 1);
              nread = readbuf (desc, name, name_len);
              if (nread != name_len)
                goto invalid;

              name[name_len] = '\0';

              long_name = 1;
            }
#endif /* Not AIAMAG. */
        }

#ifndef M_XENIX
        sscanf (TOCHAR (member_header.ar_mode), "%8o", &eltmode);
        eltsize = atol (TOCHAR (member_header.ar_size));
#else   /* Xenix.  */
        eltmode = (unsigned short int) member_header.ar_mode;
        eltsize = member_header.ar_size;
#endif  /* Not Xenix.  */

        fnval =
          (*function) (desc, name, ! long_name, member_offset,
                       member_offset + AR_HDR_SIZE, eltsize,
#ifndef M_XENIX
                       atol (TOCHAR (member_header.ar_date)),
                       atoi (TOCHAR (member_header.ar_uid)),
                       atoi (TOCHAR (member_header.ar_gid)),
#else   /* Xenix.  */
                       member_header.ar_date,
                       member_header.ar_uid,
                       member_header.ar_gid,
#endif  /* Not Xenix.  */
                       eltmode, arg);

#endif  /* AIAMAG.  */

        if (fnval)
          {
            (void) close (desc);
            return fnval;
          }

#ifdef AIAMAG
        if (member_offset == last_member_offset)
          /* End of the chain.  */
          break;

#ifdef AIAMAGBIG
        if (big_archive)
         sscanf (member_header_big.ar_nxtmem, "%20ld", &member_offset);
        else
#endif
          sscanf (member_header.ar_nxtmem, "%12ld", &member_offset);

        if (lseek (desc, member_offset, 0) != member_offset)
          goto invalid;
#else

        /* If this member maps archive names, we must read it in.  The
           name map will always precede any members whose names must
           be mapped.  */
        if (is_namemap)
          {
            char *clear;
            char *limit;

            if (eltsize > INT_MAX)
              goto invalid;
            namemap = alloca (eltsize + 1);
            nread = readbuf (desc, namemap, eltsize);
            if (nread != eltsize)
              goto invalid;
            namemap_size = eltsize;

            /* The names are separated by newlines.  Some formats have
               a trailing slash.  Null terminate the strings for
               convenience.  */
            limit = namemap + eltsize;
            for (clear = namemap; clear < limit; clear++)
              {
                if (*clear == '\n')
                  {
                    *clear = '\0';
                    if (clear[-1] == '/')
                      clear[-1] = '\0';
                  }
              }
            *limit = '\0';

            is_namemap = 0;
          }

        member_offset += AR_HDR_SIZE + eltsize;
        if (member_offset % 2 != 0)
          member_offset++;
#endif
      }
  }

  close (desc);
  return 0;

 invalid:
  close (desc);
  return -2;
}


/* Return nonzero iff NAME matches MEM.
   If TRUNCATED is nonzero, MEM may be truncated to
   sizeof (struct ar_hdr.ar_name) - 1.  */

int
ar_name_equal (const char *name, const char *mem, int truncated)
{
  const char *p;

  p = strrchr (name, '/');
  if (p != 0)
    name = p + 1;

#ifndef VMS
  if (truncated)
    {
#ifdef AIAMAG
      /* TRUNCATED should never be set on this system.  */
      abort ();
#else
      struct ar_hdr hdr;
#if !defined (__hpux) && !defined (cray)
      return strneq (name, mem, sizeof (hdr.ar_name) - 1);
#else
      return strneq (name, mem, sizeof (hdr.ar_name) - 2);
#endif /* !__hpux && !cray */
#endif /* !AIAMAG */
    }

  return !strcmp (name, mem);
#else
  /* VMS members do not have suffixes, but the filenames usually
     have.
     Do we need to strip VMS disk/directory format paths?

     Most VMS compilers etc. by default are case insensitive
     but produce uppercase external names, incl. module names.
     However the VMS librarian (ar) and the linker by default
     are case sensitive: they take what they get, usually
     uppercase names. So for the non-default settings of the
     compilers etc. there is a need to have a case sensitive
     mode. */
  {
    int len;
    len = strlen(mem);
    int match;
    char *dot;
    if ((dot=strrchr(name,'.')))
      match = (len == dot - name) && !strncasecmp(name, mem, len);
    else
      match = !strcasecmp (name, mem);
    return match;
  }
#endif /* !VMS */
}

#ifndef VMS
/* ARGSUSED */
static long int
ar_member_pos (int desc UNUSED, const char *mem, int truncated,
               long int hdrpos, long int datapos UNUSED, long int size UNUSED,
               long int date UNUSED, int uid UNUSED, int gid UNUSED,
               unsigned int mode UNUSED, const void *name)
{
  if (!ar_name_equal (name, mem, truncated))
    return 0;
  return hdrpos;
}

/* Set date of member MEMNAME in archive ARNAME to current time.
   Returns 0 if successful,
   -1 if file ARNAME does not exist,
   -2 if not a valid archive,
   -3 if other random system call error (including file read-only),
   1 if valid but member MEMNAME does not exist.  */

int
ar_member_touch (const char *arname, const char *memname)
{
  long int pos = ar_scan (arname, ar_member_pos, memname);
  int fd;
  struct ar_hdr ar_hdr;
  off_t o;
  int r;
  unsigned int ui;
  struct stat statbuf;

  if (pos < 0)
    return (int) pos;
  if (!pos)
    return 1;

  EINTRLOOP (fd, open (arname, O_RDWR, 0666));
  if (fd < 0)
    return -3;
  /* Read in this member's header */
  EINTRLOOP (o, lseek (fd, pos, 0));
  if (o < 0)
    goto lose;
  r = readbuf (fd, &ar_hdr, AR_HDR_SIZE);
  if (r != AR_HDR_SIZE)
    goto lose;
  /* The file's mtime is the time we we want.  */
  EINTRLOOP (r, fstat (fd, &statbuf));
  if (r < 0)
    goto lose;
  /* Advance member's time to that time */
#if defined(ARFMAG) || defined(ARFZMAG) || defined(AIAMAG) || defined(WINDOWS32)
  for (ui = 0; ui < sizeof ar_hdr.ar_date; ui++)
    ar_hdr.ar_date[ui] = ' ';
  sprintf (TOCHAR (ar_hdr.ar_date), "%lu", (long unsigned) statbuf.st_mtime);
  ar_hdr.ar_date[strlen ((char *) ar_hdr.ar_date)] = ' ';
#else
  ar_hdr.ar_date = statbuf.st_mtime;
#endif
  /* Write back this member's header */
  EINTRLOOP (o, lseek (fd, pos, 0));
  if (o < 0)
    goto lose;
  r = writebuf (fd, &ar_hdr, AR_HDR_SIZE);
  if (r != AR_HDR_SIZE)
    goto lose;
  close (fd);
  return 0;

 lose:
  r = errno;
  close (fd);
  errno = r;
  return -3;
}
#endif

#ifdef TEST

long int
describe_member (int desc, const char *name, int truncated,
                 long int hdrpos, long int datapos, long int size,
                 long int date, int uid, int gid, unsigned int mode,
                 const void *arg)
{
  extern char *ctime ();

  printf (_("Member '%s'%s: %ld bytes at %ld (%ld).\n"),
          name, truncated ? _(" (name might be truncated)") : "",
          size, hdrpos, datapos);
  printf (_("  Date %s"), ctime (&date));
  printf (_("  uid = %d, gid = %d, mode = 0%o.\n"), uid, gid, mode);

  return 0;
}

int
main (int argc, char **argv)
{
  ar_scan (argv[1], describe_member, NULL);
  return 0;
}

#endif  /* TEST.  */
