1da0c48c4Sopenharmony_ci/* Convert from memory to file representation.
2da0c48c4Sopenharmony_ci   Copyright (C) 1998, 1999, 2000, 2002, 2015 Red Hat, Inc.
3da0c48c4Sopenharmony_ci   This file is part of elfutils.
4da0c48c4Sopenharmony_ci   Written by Ulrich Drepper <drepper@redhat.com>, 1998.
5da0c48c4Sopenharmony_ci
6da0c48c4Sopenharmony_ci   This file is free software; you can redistribute it and/or modify
7da0c48c4Sopenharmony_ci   it under the terms of either
8da0c48c4Sopenharmony_ci
9da0c48c4Sopenharmony_ci     * the GNU Lesser General Public License as published by the Free
10da0c48c4Sopenharmony_ci       Software Foundation; either version 3 of the License, or (at
11da0c48c4Sopenharmony_ci       your option) any later version
12da0c48c4Sopenharmony_ci
13da0c48c4Sopenharmony_ci   or
14da0c48c4Sopenharmony_ci
15da0c48c4Sopenharmony_ci     * the GNU General Public License as published by the Free
16da0c48c4Sopenharmony_ci       Software Foundation; either version 2 of the License, or (at
17da0c48c4Sopenharmony_ci       your option) any later version
18da0c48c4Sopenharmony_ci
19da0c48c4Sopenharmony_ci   or both in parallel, as here.
20da0c48c4Sopenharmony_ci
21da0c48c4Sopenharmony_ci   elfutils is distributed in the hope that it will be useful, but
22da0c48c4Sopenharmony_ci   WITHOUT ANY WARRANTY; without even the implied warranty of
23da0c48c4Sopenharmony_ci   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24da0c48c4Sopenharmony_ci   General Public License for more details.
25da0c48c4Sopenharmony_ci
26da0c48c4Sopenharmony_ci   You should have received copies of the GNU General Public License and
27da0c48c4Sopenharmony_ci   the GNU Lesser General Public License along with this program.  If
28da0c48c4Sopenharmony_ci   not, see <http://www.gnu.org/licenses/>.  */
29da0c48c4Sopenharmony_ci
30da0c48c4Sopenharmony_ci#ifdef HAVE_CONFIG_H
31da0c48c4Sopenharmony_ci# include <config.h>
32da0c48c4Sopenharmony_ci#endif
33da0c48c4Sopenharmony_ci
34da0c48c4Sopenharmony_ci#include <assert.h>
35da0c48c4Sopenharmony_ci#include <string.h>
36da0c48c4Sopenharmony_ci
37da0c48c4Sopenharmony_ci#include "libelfP.h"
38da0c48c4Sopenharmony_ci
39da0c48c4Sopenharmony_ci#ifndef LIBELFBITS
40da0c48c4Sopenharmony_ci# define LIBELFBITS	32
41da0c48c4Sopenharmony_ci#endif
42da0c48c4Sopenharmony_ci
43da0c48c4Sopenharmony_ci
44da0c48c4Sopenharmony_ciElf_Data *
45da0c48c4Sopenharmony_cielfw2(LIBELFBITS, xlatetof) (Elf_Data *dest, const Elf_Data *src,
46da0c48c4Sopenharmony_ci			     unsigned int encode)
47da0c48c4Sopenharmony_ci{
48da0c48c4Sopenharmony_ci  /* First test whether the input data is really suitable for this
49da0c48c4Sopenharmony_ci     type.  This means, whether there is an integer number of records.
50da0c48c4Sopenharmony_ci     Note that for this implementation the memory and file size of the
51da0c48c4Sopenharmony_ci     data types are identical.  */
52da0c48c4Sopenharmony_ci  size_t recsize = __libelf_type_sizes[ELFW(ELFCLASS,LIBELFBITS) - 1][src->d_type];
53da0c48c4Sopenharmony_ci
54da0c48c4Sopenharmony_ci  if (src->d_size % recsize != 0)
55da0c48c4Sopenharmony_ci    {
56da0c48c4Sopenharmony_ci      __libelf_seterrno (ELF_E_INVALID_DATA);
57da0c48c4Sopenharmony_ci      return NULL;
58da0c48c4Sopenharmony_ci    }
59da0c48c4Sopenharmony_ci
60da0c48c4Sopenharmony_ci  /* Next see whether the converted data fits in the output buffer.  */
61da0c48c4Sopenharmony_ci  if (src->d_size > dest->d_size)
62da0c48c4Sopenharmony_ci    {
63da0c48c4Sopenharmony_ci      __libelf_seterrno (ELF_E_DEST_SIZE);
64da0c48c4Sopenharmony_ci      return NULL;
65da0c48c4Sopenharmony_ci    }
66da0c48c4Sopenharmony_ci
67da0c48c4Sopenharmony_ci  /* Test the encode parameter.  */
68da0c48c4Sopenharmony_ci  if (encode != ELFDATA2LSB && encode != ELFDATA2MSB)
69da0c48c4Sopenharmony_ci    {
70da0c48c4Sopenharmony_ci      __libelf_seterrno (ELF_E_INVALID_ENCODING);
71da0c48c4Sopenharmony_ci      return NULL;
72da0c48c4Sopenharmony_ci    }
73da0c48c4Sopenharmony_ci
74da0c48c4Sopenharmony_ci  /* Determine the translation function to use.
75da0c48c4Sopenharmony_ci
76da0c48c4Sopenharmony_ci     At this point we make an assumption which is valid for all
77da0c48c4Sopenharmony_ci     existing implementations so far: the memory and file sizes are
78da0c48c4Sopenharmony_ci     the same.  This has very important consequences:
79da0c48c4Sopenharmony_ci     a) The requirement that the source and destination buffer can
80da0c48c4Sopenharmony_ci	overlap can easily be fulfilled.
81da0c48c4Sopenharmony_ci     b) We need only one function to convert from and memory to file
82da0c48c4Sopenharmony_ci	and vice versa since the function only has to copy and/or
83da0c48c4Sopenharmony_ci	change the byte order.
84da0c48c4Sopenharmony_ci  */
85da0c48c4Sopenharmony_ci  if ((BYTE_ORDER == LITTLE_ENDIAN && encode == ELFDATA2LSB)
86da0c48c4Sopenharmony_ci      || (BYTE_ORDER == BIG_ENDIAN && encode == ELFDATA2MSB))
87da0c48c4Sopenharmony_ci    {
88da0c48c4Sopenharmony_ci      /* We simply have to copy since the byte order is the same.  */
89da0c48c4Sopenharmony_ci      if (src->d_buf != dest->d_buf)
90da0c48c4Sopenharmony_ci	memmove (dest->d_buf, src->d_buf, src->d_size);
91da0c48c4Sopenharmony_ci    }
92da0c48c4Sopenharmony_ci  else
93da0c48c4Sopenharmony_ci    {
94da0c48c4Sopenharmony_ci      xfct_t fctp;
95da0c48c4Sopenharmony_ci      fctp = __elf_xfctstom[ELFW(ELFCLASS, LIBELFBITS) - 1][src->d_type];
96da0c48c4Sopenharmony_ci
97da0c48c4Sopenharmony_ci      /* Do the real work.  */
98da0c48c4Sopenharmony_ci      (*fctp) (dest->d_buf, src->d_buf, src->d_size, 1);
99da0c48c4Sopenharmony_ci    }
100da0c48c4Sopenharmony_ci
101da0c48c4Sopenharmony_ci  /* Now set the real destination type and length since the operation was
102da0c48c4Sopenharmony_ci     successful.  */
103da0c48c4Sopenharmony_ci  dest->d_type = src->d_type;
104da0c48c4Sopenharmony_ci  dest->d_size = src->d_size;
105da0c48c4Sopenharmony_ci
106da0c48c4Sopenharmony_ci  return dest;
107da0c48c4Sopenharmony_ci}
108da0c48c4Sopenharmony_ciINTDEF(elfw2(LIBELFBITS, xlatetof))
109