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 <assert.h>
29260684Skaiw#include <libelf.h>
30260684Skaiw
31260684Skaiw#include "_libelf.h"
32260684Skaiw
33367466SdimELFTC_VCSID("$Id: elf_next.c 3710 2019-03-12 09:42:35Z jkoshy $");
34260684Skaiw
35260684SkaiwElf_Cmd
36260684Skaiwelf_next(Elf *e)
37260684Skaiw{
38260684Skaiw	off_t next;
39260684Skaiw	Elf *parent;
40260684Skaiw
41260684Skaiw	if (e == NULL)
42260684Skaiw		return (ELF_C_NULL);
43260684Skaiw
44260684Skaiw	 if ((parent = e->e_parent) == NULL) {
45260684Skaiw		 LIBELF_SET_ERROR(ARGUMENT, 0);
46260684Skaiw		 return (ELF_C_NULL);
47260684Skaiw	 }
48260684Skaiw
49276371Semaste	assert(parent->e_kind == ELF_K_AR);
50276371Semaste	assert(parent->e_cmd == ELF_C_READ);
51260684Skaiw	assert(e->e_rawfile > parent->e_rawfile);
52260684Skaiw
53276371Semaste	next = e->e_rawfile - parent->e_rawfile + (off_t) e->e_rawsize;
54260684Skaiw	next = (next + 1) & ~1;	/* round up to an even boundary */
55260684Skaiw
56276371Semaste	/*
57276371Semaste	 * Setup the 'e_next' field of the archive descriptor for the
58276371Semaste	 * next call to 'elf_begin()'.
59276371Semaste	 */
60260684Skaiw	parent->e_u.e_ar.e_next = (next >= (off_t) parent->e_rawsize) ?
61260684Skaiw	    (off_t) 0 : next;
62260684Skaiw
63367466Sdim	/*
64367466Sdim	 * Return an error if the 'e_next' field falls outside the current
65367466Sdim	 * file.
66367466Sdim	 *
67367466Sdim	 * This check is performed after updating the parent descriptor's
68367466Sdim	 * 'e_next' field so that the next call to elf_begin(3) will terminate
69367466Sdim	 * traversal of a too-small archive even if client code forgets to
70367466Sdim	 * check the return value from elf_next(3).
71367466Sdim	 */
72367466Sdim	if (next > (off_t) parent->e_rawsize) {
73367466Sdim		LIBELF_SET_ERROR(ARGUMENT, 0);
74367466Sdim		return (ELF_C_NULL);
75367466Sdim	}
76367466Sdim
77260684Skaiw	return (ELF_C_READ);
78260684Skaiw}
79