1260684Skaiw/*-
2260684Skaiw * Copyright (c) 2006,2008 Joseph Koshy
3260684Skaiw * All rights reserved.
4260684Skaiw *
5260684Skaiw * Redistribution and use in source and binary forms, with or without
6260684Skaiw * modification, are permitted provided that the following conditions
7260684Skaiw * are met:
8260684Skaiw * 1. Redistributions of source code must retain the above copyright
9260684Skaiw *    notice, this list of conditions and the following disclaimer.
10260684Skaiw * 2. Redistributions in binary form must reproduce the above copyright
11260684Skaiw *    notice, this list of conditions and the following disclaimer in the
12260684Skaiw *    documentation and/or other materials provided with the distribution.
13260684Skaiw *
14260684Skaiw * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15260684Skaiw * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16260684Skaiw * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17260684Skaiw * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18260684Skaiw * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19260684Skaiw * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20260684Skaiw * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21260684Skaiw * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22260684Skaiw * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23260684Skaiw * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24260684Skaiw * SUCH DAMAGE.
25260684Skaiw */
26260684Skaiw
27260684Skaiw#include <ar.h>
28260684Skaiw#include <libelf.h>
29260684Skaiw
30260684Skaiw#include "_libelf.h"
31260684Skaiw
32367466SdimELFTC_VCSID("$Id: elf_rand.c 3716 2019-03-18 22:01:01Z jkoshy $");
33260684Skaiw
34260684Skaiwoff_t
35260684Skaiwelf_rand(Elf *ar, off_t offset)
36260684Skaiw{
37260684Skaiw	struct ar_hdr *arh;
38367466Sdim	off_t offset_of_member;
39260684Skaiw
40260684Skaiw	if (ar == NULL || ar->e_kind != ELF_K_AR ||
41260684Skaiw	    (offset & 1) || offset < SARMAG ||
42367466Sdim	    offset >= ar->e_rawsize) {
43260684Skaiw		LIBELF_SET_ERROR(ARGUMENT, 0);
44260684Skaiw		return 0;
45260684Skaiw	}
46260684Skaiw
47367466Sdim	offset_of_member = offset + (off_t) sizeof(struct ar_hdr);
48367466Sdim
49367466Sdim	if (offset_of_member <= 0 || /* Numeric overflow. */
50367466Sdim	    offset_of_member >= ar->e_rawsize) {
51367466Sdim		LIBELF_SET_ERROR(ARGUMENT, 0);
52367466Sdim		return 0;
53367466Sdim	}
54367466Sdim
55260684Skaiw	arh = (struct ar_hdr *) (ar->e_rawfile + offset);
56260684Skaiw
57260684Skaiw	/* a too simple sanity check */
58260684Skaiw	if (arh->ar_fmag[0] != '`' || arh->ar_fmag[1] != '\n') {
59260684Skaiw		LIBELF_SET_ERROR(ARCHIVE, 0);
60260684Skaiw		return 0;
61260684Skaiw	}
62260684Skaiw
63260684Skaiw	ar->e_u.e_ar.e_next = offset;
64260684Skaiw
65260684Skaiw	return (offset);
66260684Skaiw}
67