Debian Codegraph @

src/lib/libdwarf/dwarf_memcpy_swap.c

raw
1 /*
2 Copyright (C) 2000-2005 Silicon Graphics, Inc. All Rights Reserved.
3 Portions Copyright (C) 2007-2020 David Anderson. All Rights Reserved.
4 Portions Copyright 2012 SN Systems Ltd. All rights reserved.
5 Portions Copyright 2020 Google All rights reserved.
6
7 This program is free software; you can redistribute it
8 and/or modify it under the terms of version 2.1 of the
9 GNU Lesser General Public License as published by the Free
10 Software Foundation.
11
12 This program is distributed in the hope that it would be
13 useful, but WITHOUT ANY WARRANTY; without even the implied
14 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 PURPOSE.
16
17 Further, this software is distributed without any warranty
18 that it is free of the rightful claim of any third person
19 regarding infringement or the like. Any license provided
20 herein, whether implied or otherwise, applies only to this
21 software file. Patent licenses, if any, provided herein
22 do not apply to combinations of this program with other
23 software, or any other product whatsoever.
24
25 You should have received a copy of the GNU Lesser General
26 Public License along with this program; if not, write the
27 Free Software Foundation, Inc., 51 Franklin Street - Fifth
28 Floor, Boston MA 02110-1301, USA.
29
30 */
31
32 #include <config.h>
33
34 #include <stddef.h> /* size_t */
35 #include <string.h> /* memcpy() */
36
37 #include "dwarf_memcpy_swap.h"
38
39 /*
40 A byte-swapping version of memcpy
41 for cross-endian use.
42 Only 2,4,8 should be lengths passed in.
43 */
44 void
45 _dwarf_memcpy_noswap_bytes(void *s1,
46 const void *s2,
47 unsigned long len)
48 {
49 memcpy(s1,s2,(size_t)len);
50 return;
51 }
52
53 void
54 _dwarf_memcpy_swap_bytes(void *s1,
55 const void *s2,
56 unsigned long len)
57 {
58 unsigned char *targ = (unsigned char *) s1;
59 const unsigned char *src = (const unsigned char *) s2;
60 unsigned long i = 0;
61 unsigned long n = (long)(len-1);
62
63 if (len > 8) {
64 /* Really we should not be here!
65 Not writing an integer, we think, so
66 best to not swap bytes! */
67 memcpy(s1,s2,(size_t)len);
68 return;
69 }
70 for ( ; i < len; ++i,--n) {
71 targ[n] = src[i];
72 }
73 return;
74 }