LCOV - code coverage report
Current view: top level - frmts/fit - gstEndian.h (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 29 37 78.4 %
Date: 2025-01-18 12:42:00 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  FIT Driver
       4             :  * Purpose:  Implement FIT Support - not using the SGI iflFIT library.
       5             :  * Author:   Philip Nemec, nemec@keyholecorp.com
       6             :  *
       7             :  ******************************************************************************
       8             :  * Copyright (c) 2001, Keyhole, Inc.
       9             :  *
      10             :  * SPDX-License-Identifier: MIT
      11             :  ****************************************************************************/
      12             : 
      13             : #ifndef gstEndian_h_
      14             : #define gstEndian_h_
      15             : 
      16             : // endian swapping tools
      17             : 
      18             : #include <stdio.h>
      19             : #include "cpl_port.h"
      20             : 
      21             : #include "gstTypes.h"
      22             : 
      23             : namespace gstEndian
      24             : {
      25             : 
      26             : // have to do swapping on Linux and Windows
      27             : #ifdef CPL_LSB
      28             : #define swapping
      29             : #else
      30             : #endif
      31             : 
      32             : #ifdef swapping
      33             : size_t swapped_fread(void *ptr, size_t size, size_t nitems, FILE *stream);
      34             : size_t swapped_fwrite(const void *ptr, size_t size, size_t nitems,
      35             :                       FILE *stream);
      36             : 
      37         506 : static inline void gst_swap64(void *value)
      38             : {
      39             :     // 0x1122334455667788 --> 0x8877665544332211
      40             : 
      41         506 :     *(uint64 *)(value) = (((*(uint64 *)(value)&0x00000000000000ff) << 56) |
      42         506 :                           ((*(uint64 *)(value)&0x000000000000ff00) << 40) |
      43         506 :                           ((*(uint64 *)(value)&0x0000000000ff0000) << 24) |
      44         506 :                           ((*(uint64 *)(value)&0x00000000ff000000) << 8) |
      45         506 :                           ((*(uint64 *)(value) >> 8) & 0x00000000ff000000) |
      46         506 :                           ((*(uint64 *)(value) >> 24) & 0x0000000000ff0000) |
      47         506 :                           ((*(uint64 *)(value) >> 40) & 0x000000000000ff00) |
      48         506 :                           ((*(uint64 *)(value) >> 56) & 0x00000000000000ff));
      49         506 : }
      50             : 
      51        1905 : static inline void gst_swap32(void *value)
      52             : {
      53             :     // 0x12 34 56 78 --> 0x78 56 34 12
      54             : 
      55        1905 :     *(uint32 *)(value) = (((*(uint32 *)(value)&0x000000ff) << 24) |
      56        1905 :                           ((*(uint32 *)(value)&0x0000ff00) << 8) |
      57        1905 :                           ((*(uint32 *)(value) >> 8) & 0x0000ff00) |
      58        1905 :                           ((*(uint32 *)(value) >> 24) & 0x000000ff));
      59        1905 : }
      60             : 
      61         800 : static inline void gst_swap16(void *value)
      62             : {
      63         800 :     *(uint16 *)(value) = (((*(uint16 *)(value)&0x00ff) << 8) |
      64         800 :                           ((*(uint16 *)(value) >> 8) & 0x00ff));
      65         800 : }
      66             : 
      67         811 : static inline void gst_swapbytes(void *value, int size)
      68             : {
      69         811 :     switch (size)
      70             :     {
      71           0 :         case 1:
      72             :             // do nothing
      73           0 :             break;
      74           0 :         case 2:
      75           0 :             gst_swap16(value);
      76           0 :             break;
      77         705 :         case 4:
      78         705 :             gst_swap32(value);
      79         705 :             break;
      80         106 :         case 8:
      81         106 :             gst_swap64(value);
      82         106 :             break;
      83           0 :         default:
      84           0 :             fprintf(stderr,
      85             :                     "gst_swapbytes unsupported size %i - not swapping\n", size);
      86           0 :             break;
      87             :     }  // switch
      88         811 : }
      89             : 
      90             : #define gst_swapb(value) gst_swapbytes(&value, sizeof(value))
      91             : 
      92             : #else  // swapping
      93             : 
      94             : #define swapped_fread(ptr, size, nitems, stream)                               \
      95             :     fread(ptr, size, nitems, stream)
      96             : #define swapped_fwrite(ptr, size, nitems, stream)                              \
      97             :     fwrite(ptr, size, nitems, stream)
      98             : 
      99             : #define gst_swap64(value)
     100             : #define gst_swap32(value)
     101             : #define gst_swap16(value)
     102             : #define gst_swapbytes(value, size)
     103             : #define gst_swapb(value)
     104             : 
     105             : #endif  // swapping
     106             : 
     107             : }  // namespace gstEndian
     108             : 
     109             : #endif  // ! gstEndian_h_

Generated by: LCOV version 1.14