1
0
mirror of https://gitlab.com/80486DX2-66/gists synced 2024-09-19 05:45:36 +05:30

C: fwrite_le.*: update to revision 3acee16

This commit is contained in:
Intel A80486DX2-66 2024-06-25 11:10:59 +03:00
parent 60858de021
commit d1750c19ca
Signed by: 80486DX2-66
GPG Key ID: 83631EF27054609B
2 changed files with 32 additions and 21 deletions

View File

@ -8,19 +8,11 @@
#include "fwrite_le.h"
int detect_endianness(void) {
volatile uint32_t i = 0x01234567;
uint8_t* bytes = (uint8_t*)(&i);
if (bytes[0] == 0x01 &&
bytes[1] == 0x23 &&
bytes[2] == 0x45 &&
bytes[3] == 0x67)
return DETECTED_BIG_ENDIAN;
else if (
bytes[0] == 0x67 &&
bytes[1] == 0x45 &&
bytes[2] == 0x23 &&
bytes[3] == 0x01)
return DETECTED_LITTLE_ENDIAN;
volatile uint32_t native_order_value = ORDER_NATIVE_U32;
uint8_t* as_bytes = (uint8_t*)&native_order_value;
ifeq_b32_ret(as_bytes, ORDER_LITTLE_ENDIAN_S4, DETECTED_LITTLE_ENDIAN);
ifeq_b32_ret(as_bytes, ORDER_BIG_ENDIAN_S4, DETECTED_BIG_ENDIAN );
return UNSUPPORTED_ENDIANNESS;
}
@ -31,6 +23,11 @@ void reorder_le_be(
uint8_t* bytes,
#endif
size_t count, size_t step) {
if (step == 1) {
return;
}
for (size_t i = 0; i < count; i += step) {
const size_t div_size = step / 2;
for (size_t j = 0; j < div_size; j++) {
@ -47,7 +44,11 @@ void reorder_le_be(
}
}
size_t fwrite_le(void* ptr, size_t size, size_t count, FILE* stream) {
size_t fwrite_le(
#if FWRITE_LE_NO_MODIFICATION
const
#endif
void* ptr, size_t size, size_t count, FILE* stream) {
/*
* warning: this function modifies `void* ptr` by default!
* (if FWRITE_LE_NO_MODIFICATION in the header is 0)
@ -56,11 +57,12 @@ size_t fwrite_le(void* ptr, size_t size, size_t count, FILE* stream) {
return 0;
int endianness = detect_endianness();
if (endianness == UNSUPPORTED_ENDIANNESS) {
if (size == 1 || endianness == DETECTED_LITTLE_ENDIAN)
return fwrite(ptr, size, count, stream);
else if (endianness == UNSUPPORTED_ENDIANNESS) {
fprintf(stderr, "Unsupported endianness\n");
exit(EXIT_FAILURE);
} else if (endianness == DETECTED_LITTLE_ENDIAN)
return fwrite(ptr, size, count, stream);
}
// case: big-endian
size_t bytes_count = size * count;

View File

@ -5,22 +5,31 @@
* License: Unlicense
*/
#ifndef _FWRITE_LE_H
#define _FWRITE_LE_H
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef _FWRITE_LE_H
#define _FWRITE_LE_H
#define FWRITE_LE_NO_MODIFICATION 0
#define DETECTED_LITTLE_ENDIAN 0
#define DETECTED_BIG_ENDIAN 1
#define UNSUPPORTED_ENDIANNESS -1
#define ORDER_NATIVE_U32 0x01234567
#define ORDER_LITTLE_ENDIAN_S4 "\x67\x45\x23\x01"
#define ORDER_BIG_ENDIAN_S4 "\x01\x23\x45\x67"
#define ifeq_b32_ret(lhs, rhs, value) if (!memcmp(lhs, rhs, 4)) return value;
int detect_endianness(void);
size_t fwrite_le(void* ptr, size_t size, size_t count, FILE* stream);
size_t fwrite_le(
#if FWRITE_LE_NO_MODIFICATION
const
#endif
void* ptr, size_t size, size_t count, FILE* stream);
void reorder_le_be(
#if FWRITE_LE_NO_MODIFICATION
uint8_t* dest, uint8_t* src,