Subversion Repositories pentevo

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /* be_le.c */
  2. /*****************************************************************************/
  3. /* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only                     */
  4. /*                                                                           */
  5. /* AS-Portierung                                                             */
  6. /*                                                                           */
  7. /* Little/Big-Endian-Routinen                                                */
  8. /*                                                                           */
  9. /*****************************************************************************/
  10.  
  11. #include "stdinc.h"
  12.  
  13. #include <string.h>
  14.  
  15. #include "be_le.h"
  16.  
  17. /*****************************************************************************/
  18.  
  19. Boolean HostBigEndian;
  20.  
  21. const char *Integ16Format, *Integ32Format, *Integ64Format;
  22. const char *IntegerFormat, *LongIntFormat, *QuadIntFormat;
  23. const char *LargeIntFormat, *LargeHIntFormat;
  24.  
  25. /*****************************************************************************/
  26.  
  27. #define SWAP(x,y) \
  28. do \
  29. { \
  30.   Swap = x; x = y; y = Swap; \
  31. } \
  32. while (0)
  33.  
  34. void WSwap(void *Field, int Cnt)
  35. {
  36.   register unsigned char *Run = (unsigned char *) Field, Swap;
  37.   register int z;
  38.  
  39.   for (z = 0; z < Cnt / 2; z++, Run += 2)
  40.   {
  41.     SWAP(Run[0], Run[1]);
  42.   }
  43. }
  44.  
  45. void DSwap(void *Field, int Cnt)
  46. {
  47.   register unsigned char *Run = (unsigned char *) Field, Swap;
  48.   register int z;
  49.  
  50.   for (z = 0; z < Cnt / 4; z++, Run += 4)
  51.   {
  52.     SWAP(Run[0], Run[3]);
  53.     SWAP(Run[1], Run[2]);
  54.   }
  55. }
  56.  
  57. void QSwap(void *Field, int Cnt)
  58. {
  59.   register unsigned char *Run = (unsigned char *) Field, Swap;
  60.   register int z;
  61.  
  62.   for (z = 0; z < Cnt / 8; z++, Run += 8)
  63.   {
  64.     SWAP(Run[0], Run[7]);
  65.     SWAP(Run[1], Run[6]);
  66.     SWAP(Run[2], Run[5]);
  67.     SWAP(Run[3], Run[4]);
  68.   }
  69. }
  70.  
  71. void TSwap(void *Field, int Cnt)
  72. {
  73.   register unsigned char *Run = (unsigned char *) Field, Swap;
  74.   register int z;
  75.  
  76.   for (z = 0; z < Cnt / 10; z++, Run += 10)
  77.   {
  78.     SWAP(Run[0], Run[9]);
  79.     SWAP(Run[1], Run[8]);
  80.     SWAP(Run[2], Run[7]);
  81.     SWAP(Run[3], Run[6]);
  82.     SWAP(Run[4], Run[5]);
  83.   }
  84. }
  85.  
  86. void OSwap(void *Field, int Cnt)
  87. {
  88.   register unsigned char *Run = (unsigned char *) Field, Swap;
  89.   register int z;
  90.  
  91.   for (z = 0; z < Cnt / 16; z++, Run += 16)
  92.   {
  93.     SWAP(Run[0], Run[15]);
  94.     SWAP(Run[1], Run[14]);
  95.     SWAP(Run[2], Run[13]);
  96.     SWAP(Run[3], Run[12]);
  97.     SWAP(Run[4], Run[11]);
  98.     SWAP(Run[5], Run[10]);
  99.     SWAP(Run[6], Run[ 9]);
  100.     SWAP(Run[7], Run[ 8]);
  101.   }
  102. }
  103.  
  104. void DWSwap(void *Field, int Cnt)
  105. {
  106.   register unsigned char *Run = (unsigned char *) Field, Swap;
  107.   register int z;
  108.  
  109.   for (z = 0; z < Cnt / 4; z++, Run += 4)
  110.   {
  111.     SWAP(Run[0], Run[2]);
  112.     SWAP(Run[1], Run[3]);
  113.   }
  114. }
  115.  
  116. void QWSwap(void *Field, int Cnt)
  117. {
  118.   register unsigned char *Run = (unsigned char *) Field, Swap;
  119.   register int z;
  120.  
  121.   for (z = 0; z < Cnt / 8; z++, Run += 8)
  122.   {
  123.     SWAP(Run[0], Run[6]);
  124.     SWAP(Run[1], Run[7]);
  125.     SWAP(Run[2], Run[4]);
  126.     SWAP(Run[3], Run[5]);
  127.   }
  128. }
  129.  
  130. void TWSwap(void *Field, int Cnt)
  131. {
  132.   register unsigned char *Run = (unsigned char *) Field, Swap;
  133.   register int z;
  134.  
  135.   for (z = 0; z < Cnt / 10; z++, Run += 10)
  136.   {
  137.     SWAP(Run[0], Run[8]);
  138.     SWAP(Run[1], Run[9]);
  139.     SWAP(Run[2], Run[6]);
  140.     SWAP(Run[3], Run[7]);
  141.     /* center word needs not be swapped with itself */
  142.   }
  143. }
  144.  
  145. Boolean Read2(FILE *file, void *Ptr)
  146. {
  147.   if (fread(Ptr, 1, 2, file) != 2)
  148.     return False;
  149.   if (HostBigEndian)
  150.     WSwap(Ptr, 2);
  151.   return True;
  152. }
  153.  
  154. Boolean Read4(FILE *file, void *Ptr)
  155. {
  156.   if (fread(Ptr, 1, 4, file) != 4)
  157.     return False;
  158.   if (HostBigEndian)
  159.     DSwap(Ptr, 4);
  160.   return True;
  161. }
  162.  
  163. Boolean Read8(FILE *file, void *Ptr)
  164. {
  165.   if (fread(Ptr, 1, 8, file) != 8)
  166.     return False;
  167.   if (HostBigEndian)
  168.     QSwap(Ptr, 8);
  169.   return True;
  170. }
  171.  
  172.  
  173. Boolean Write2(FILE *file, void *Ptr)
  174. {
  175.   Boolean OK;
  176.  
  177.   if (HostBigEndian)
  178.     WSwap(Ptr, 2);
  179.   OK = (fwrite(Ptr, 1, 2, file) == 2);
  180.   if (HostBigEndian)
  181.     WSwap(Ptr, 2);
  182.   return OK;
  183. }
  184.  
  185. Boolean Write4(FILE *file, void *Ptr)
  186. {
  187.   Boolean OK;
  188.  
  189.   if (HostBigEndian)
  190.     DSwap(Ptr, 4);
  191.   OK = (fwrite(Ptr, 1, 4, file) == 4);
  192.   if (HostBigEndian)
  193.     DSwap(Ptr, 4);
  194.   return OK;
  195. }
  196.  
  197. Boolean Write8(FILE *file, void *Ptr)
  198. {
  199.   Boolean OK;
  200.  
  201.   if (HostBigEndian)
  202.     QSwap(Ptr, 8);
  203.   OK = (fwrite(Ptr, 1, 8, file) == 8);
  204.   if (HostBigEndian)
  205.     QSwap(Ptr, 8);
  206.   return OK;
  207. }
  208.  
  209.  
  210. Word MRead2L(Byte *Buffer)
  211. {
  212.   return (((Word) Buffer[1]) << 8) | Buffer[0];
  213. }
  214.  
  215. Word MRead2B(Byte *Buffer)
  216. {
  217.   return (((Word) Buffer[0]) << 8) | Buffer[1];
  218. }
  219.  
  220. void MWrite2L(Byte *Buffer, Word Value)
  221. {
  222.   Buffer[0] = Value & 0xff;
  223.   Buffer[1] = (Value >> 8) & 0xff;
  224. }
  225.  
  226. void MWrite2B(Byte *Buffer, Word Value)
  227. {
  228.   Buffer[1] = Value & 0xff;
  229.   Buffer[0] = (Value >> 8) & 0xff;
  230. }
  231.  
  232. LongWord MRead4L(Byte *Buffer)
  233. {
  234.   return (((LongWord) Buffer[3]) << 24) |
  235.          (((LongWord) Buffer[2]) << 16) |
  236.          (((LongWord) Buffer[1]) << 8)  | Buffer[0];
  237. }
  238.  
  239. LongWord MRead4B(Byte *Buffer)
  240. {
  241.   return (((LongWord) Buffer[0]) << 24) |
  242.          (((LongWord) Buffer[1]) << 16) |
  243.          (((LongWord) Buffer[2]) << 8) | Buffer[3];
  244. }
  245.  
  246. void MWrite4L(Byte *Buffer, LongWord Value)
  247. {
  248.   Buffer[0] = Value & 0xff;
  249.   Buffer[1] = (Value >> 8) & 0xff;
  250.   Buffer[2] = (Value >> 16) & 0xff;
  251.   Buffer[3] = (Value >> 24) & 0xff;
  252. }
  253.  
  254. void MWrite4B(Byte *Buffer, LongWord Value)
  255. {
  256.   Buffer[3] = Value & 0xff;
  257.   Buffer[2] = (Value >> 8) & 0xff;
  258.   Buffer[1] = (Value >> 16) & 0xff;
  259.   Buffer[0] = (Value >> 24) & 0xff;
  260. }
  261.  
  262. #ifdef HAS64
  263. QuadWord MRead8L(Byte *Buffer)
  264. {
  265.   return (((LargeWord) Buffer[7]) << 56) |
  266.          (((LargeWord) Buffer[6]) << 48) |
  267.          (((LargeWord) Buffer[5]) << 40) |
  268.          (((LargeWord) Buffer[4]) << 32) |
  269.          (((LargeWord) Buffer[3]) << 24) |
  270.          (((LargeWord) Buffer[2]) << 16) |
  271.          (((LargeWord) Buffer[1]) << 8)  |
  272.                        Buffer[0];
  273. }
  274.  
  275. QuadWord MRead8B(Byte *Buffer)
  276. {
  277.   return (((LargeWord) Buffer[0]) << 56) |
  278.          (((LargeWord) Buffer[1]) << 48) |
  279.          (((LargeWord) Buffer[2]) << 40) |
  280.          (((LargeWord) Buffer[3]) << 32) |
  281.          (((LargeWord) Buffer[4]) << 24) |
  282.          (((LargeWord) Buffer[7]) << 16) |
  283.          (((LargeWord) Buffer[6]) << 8)  |
  284.                        Buffer[7];
  285. }
  286.  
  287. void MWrite8L(Byte *Buffer, QuadWord Value)
  288. {
  289.   Buffer[0] = Value & 0xff;
  290.   Buffer[1] = (Value >> 8) & 0xff;
  291.   Buffer[2] = (Value >> 16) & 0xff;
  292.   Buffer[3] = (Value >> 24) & 0xff;
  293.   Buffer[4] = (Value >> 32) & 0xff;
  294.   Buffer[5] = (Value >> 40) & 0xff;
  295.   Buffer[6] = (Value >> 48) & 0xff;
  296.   Buffer[7] = (Value >> 56) & 0xff;
  297. }
  298.  
  299. void MWrite8B(Byte *Buffer, QuadWord Value)
  300. {
  301.   Buffer[7] = Value & 0xff;
  302.   Buffer[6] = (Value >> 8) & 0xff;
  303.   Buffer[5] = (Value >> 16) & 0xff;
  304.   Buffer[4] = (Value >> 24) & 0xff;
  305.   Buffer[3] = (Value >> 32) & 0xff;
  306.   Buffer[2] = (Value >> 40) & 0xff;
  307.   Buffer[1] = (Value >> 48) & 0xff;
  308.   Buffer[0] = (Value >> 56) & 0xff;
  309. }
  310. #endif
  311.  
  312.  
  313. static void CheckSingle(int Is, int Should, const char *Name)
  314. {
  315.   if (Is != Should)
  316.   {
  317.     fprintf(stderr, "Configuration error: Sizeof(%s) is %d, should be %d\n",
  318.             Name, Is, Should);
  319.     exit(255);
  320.   }
  321. }
  322.  
  323. static void CheckDataTypes(void)
  324. {
  325.   int intsize = sizeof(int);
  326.  
  327.   if (intsize < 2)
  328.   {
  329.     fprintf(stderr, "Configuration error: Sizeof(int) is %d, should be >=2\n",
  330.             (int) sizeof(int));
  331.     exit(255);
  332.   }
  333.   CheckSingle(sizeof(Byte),    1, "Byte");
  334.   CheckSingle(sizeof(ShortInt), 1, "ShortInt");
  335. #ifdef HAS16
  336.   CheckSingle(sizeof(Word),    2, "Word");
  337.   CheckSingle(sizeof(Integer), 2, "Integer");
  338. #endif
  339.   CheckSingle(sizeof(LongInt), 4, "LongInt");
  340.   CheckSingle(sizeof(LongWord), 4, "LongWord");
  341. #ifdef HAS64
  342.   CheckSingle(sizeof(QuadInt), 8, "QuadInt");
  343.   CheckSingle(sizeof(QuadWord), 8, "QuadWord");
  344. #endif
  345. #ifdef IEEEFLOAT_8_DOUBLE
  346.   CheckSingle(sizeof(as_float_t),  8, "as_float_t");
  347. #endif
  348. #ifdef IEEEFLOAT_10_10_LONG_DOUBLE
  349.   CheckSingle(sizeof(as_float_t), 10, "as_float_t");
  350. #endif
  351. #ifdef IEEEFLOAT_10_12_LONG_DOUBLE
  352.   CheckSingle(sizeof(as_float_t), 12, "as_float_t");
  353. #endif
  354. #ifdef IEEEFLOAT_10_16_LONG_DOUBLE
  355.   CheckSingle(sizeof(as_float_t), 16, "as_float_t");
  356. #endif
  357. }
  358.  
  359.  
  360. static const char *AssignSingle(int size)
  361. {
  362.   if (size == sizeof(short))
  363.     return "%d";
  364.   else if (size == sizeof(int))
  365.     return "%d";
  366.   else if (size == sizeof(long))
  367.     return "%ld";
  368. #if AS_HAS_LONGLONG
  369.   else if (size == sizeof(long long))
  370.     return "%lld";
  371. #endif
  372.   else
  373.   {
  374.     fprintf(stderr,
  375.             "Configuration error: cannot assign format string for integer of size %d\n", size);
  376.     exit(255);
  377.     return "";
  378.   }
  379. }
  380.  
  381. static const char *AssignHSingle(int size)
  382. {
  383.   if (size == sizeof(short))
  384.     return "%x";
  385.   else if (size == sizeof(int))
  386.     return "%x";
  387.   else if (size == sizeof(long))
  388.     return "%lx";
  389. #if AS_HAS_LONGLONG
  390.   else if (size == sizeof(long long))
  391.     return "%llx";
  392. #endif
  393.   else
  394.   {
  395.     fprintf(stderr,
  396.             "Configuration error: cannot assign format string for integer of size %d\n", size);
  397.     exit(255);
  398.     return "";
  399.   }
  400. }
  401.  
  402. static void AssignFormats(void)
  403. {
  404. #ifdef HAS16
  405.   IntegerFormat = Integ16Format = AssignSingle(2);
  406. #endif
  407.   LongIntFormat = Integ32Format = AssignSingle(4);
  408. #ifdef HAS64
  409.   QuadIntFormat = Integ64Format = AssignSingle(8);
  410. #endif
  411.   LargeIntFormat = AssignSingle(sizeof(LargeInt));
  412.   LargeHIntFormat = AssignHSingle(sizeof(LargeInt));
  413. }
  414.  
  415. void be_le_init(void)
  416. {
  417.   union
  418.   {
  419.     unsigned char field[sizeof(int)];
  420.     int test;
  421.   } TwoFace;
  422.  
  423.   CheckDataTypes();
  424.   AssignFormats();
  425.  
  426.   memset(TwoFace.field, 0, sizeof(int));
  427.   TwoFace.field[0] = 1;
  428.   HostBigEndian = ((TwoFace.test) != 1);
  429. }
  430.