1/* be-flipping.c -- Test for handling different endianness in libsframe. 2 3 Copyright (C) 2022 Free Software Foundation, Inc. 4 5 This program is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published by 7 the Free Software Foundation; either version 3 of the License, or 8 (at your option) any later version. 9 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 17 18#include "config.h" 19 20#include <stdio.h> 21#include <stdlib.h> 22#include <sys/stat.h> 23 24#include "sframe-api.h" 25 26/* DejaGnu should not use gnulib's vsnprintf replacement here. */ 27#undef vsnprintf 28#include <dejagnu.h> 29 30/* SFrame info from the following source (1 fde 5 fres): 31 static int cnt; 32 extern void foo (void); 33 34 int bar() 35 { 36 cnt++; 37 if (cnt == 3) 38 foo(); 39 return (cnt); 40 } 41 gcc -mbig-endian -Wa,--gsframe -c -O3 t.c 42 objcopy --dump-section .sframe=DATA-BE t.o 43 */ 44#define DATA "DATA-BE" 45 46int 47main (void) 48{ 49 sframe_decoder_ctx *dctx = NULL; 50 uint32_t nfres, fsize; 51 int32_t fstart; 52 unsigned char finfo; 53 int err = 0; 54 FILE *fp; 55 struct stat st; 56 char *sf_buf; 57 size_t sf_size; 58 59#define TEST(name, cond) \ 60 do \ 61 { \ 62 if (cond) \ 63 pass (name); \ 64 else \ 65 fail (name); \ 66 } \ 67 while (0) 68 69 /* Test setup. */ 70 fp = fopen (DATA, "r"); 71 if (fp == NULL) 72 goto setup_fail; 73 if (fstat (fileno (fp), &st) < 0) 74 { 75 perror ("fstat"); 76 fclose (fp); 77 goto setup_fail; 78 } 79 sf_buf = malloc (st.st_size); 80 if (sf_buf == NULL) 81 { 82 perror ("malloc"); 83 goto setup_fail; 84 } 85 sf_size = fread (sf_buf, 1, st.st_size, fp); 86 fclose (fp); 87 if (sf_size == 0) 88 { 89 fprintf (stderr, "Decode: Read buffer failed\n"); 90 goto setup_fail; 91 } 92 93 /* Execute tests. */ 94 95 /* Call to sframe_decode will endian flip the input buffer (big-endian) if 96 the host running the test is a little-endian system. This endian-flipped 97 copy of the buffer is kept internally in dctx. */ 98 dctx = sframe_decode (sf_buf, sf_size, &err); 99 TEST ("be-flipping: Decoder setup", dctx != NULL); 100 101 unsigned int fde_cnt = sframe_decoder_get_num_fidx (dctx); 102 TEST ("be-flipping: Decoder FDE count", fde_cnt == 1); 103 104 err = sframe_decoder_get_funcdesc (dctx, 0, &nfres, &fsize, &fstart, &finfo); 105 TEST ("be-flipping: Decoder get FDE", err == 0); 106 TEST ("be-flipping: Decoder FRE count", nfres == 5); 107 108 sframe_decoder_free (&dctx); 109 return 0; 110 111setup_fail: 112 sframe_decoder_free (&dctx); 113 fail ("be-flipping: Test setup"); 114 return 1; 115} 116