Subversion Repositories pentevo

Rev

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

  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 DWSwap(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 / 4; z++, Run += 4)
  92.   {
  93.     SWAP(Run[0], Run[2]);
  94.     SWAP(Run[1], Run[3]);
  95.   }
  96. }
  97.  
  98. void QWSwap(void *Field, int Cnt)
  99. {
  100.   register unsigned char *Run = (unsigned char *) Field, Swap;
  101.   register int z;
  102.  
  103.   for (z = 0; z < Cnt / 8; z++, Run += 8)
  104.   {
  105.     SWAP(Run[0], Run[6]);
  106.     SWAP(Run[1], Run[7]);
  107.     SWAP(Run[2], Run[4]);
  108.     SWAP(Run[3], Run[5]);
  109.   }
  110. }
  111.  
  112. void TWSwap(void *Field, int Cnt)
  113. {
  114.   register unsigned char *Run = (unsigned char *) Field, Swap;
  115.   register int z;
  116.  
  117.   for (z = 0; z < Cnt / 10; z++, Run += 10)
  118.   {
  119.     SWAP(Run[0], Run[8]);
  120.     SWAP(Run[1], Run[9]);
  121.     SWAP(Run[2], Run[6]);
  122.     SWAP(Run[3], Run[7]);
  123.     /* center word needs not be swapped with itself */
  124.   }
  125. }
  126.  
  127. Boolean Read2(FILE *file, void *Ptr)
  128. {
  129.   if (fread(Ptr, 1, 2, file) != 2)
  130.     return False;
  131.   if (HostBigEndian)
  132.     WSwap(Ptr, 2);
  133.   return True;
  134. }
  135.  
  136. Boolean Read4(FILE *file, void *Ptr)
  137. {
  138.   if (fread(Ptr, 1, 4, file) != 4)
  139.     return False;
  140.   if (HostBigEndian)
  141.     DSwap(Ptr, 4);
  142.   return True;
  143. }
  144.  
  145. Boolean Read8(FILE *file, void *Ptr)
  146. {
  147.   if (fread(Ptr, 1, 8, file) != 8)
  148.     return False;
  149.   if (HostBigEndian)
  150.     QSwap(Ptr, 8);
  151.   return True;
  152. }
  153.  
  154.  
  155. Boolean Write2(FILE *file, void *Ptr)
  156. {
  157.   Boolean OK;
  158.  
  159.   if (HostBigEndian)
  160.     WSwap(Ptr, 2);
  161.   OK = (fwrite(Ptr, 1, 2, file) == 2);
  162.   if (HostBigEndian)
  163.     WSwap(Ptr, 2);
  164.   return OK;
  165. }
  166.  
  167. Boolean Write4(FILE *file, void *Ptr)
  168. {
  169.   Boolean OK;
  170.  
  171.   if (HostBigEndian)
  172.     DSwap(Ptr, 4);
  173.   OK = (fwrite(Ptr, 1, 4, file) == 4);
  174.   if (HostBigEndian)
  175.     DSwap(Ptr, 4);
  176.   return OK;
  177. }
  178.  
  179. Boolean Write8(FILE *file, void *Ptr)
  180. {
  181.   Boolean OK;
  182.  
  183.   if (HostBigEndian)
  184.     QSwap(Ptr, 8);
  185.   OK = (fwrite(Ptr, 1, 8, file) == 8);
  186.   if (HostBigEndian)
  187.     QSwap(Ptr, 8);
  188.   return OK;
  189. }
  190.  
  191.  
  192. Word MRead2L(Byte *Buffer)
  193. {
  194.   return (((Word) Buffer[1]) << 8) | Buffer[0];
  195. }
  196.  
  197. Word MRead2B(Byte *Buffer)
  198. {
  199.   return (((Word) Buffer[0]) << 8) | Buffer[1];
  200. }
  201.  
  202. void MWrite2L(Byte *Buffer, Word Value)
  203. {
  204.   Buffer[0] = Value & 0xff;
  205.   Buffer[1] = (Value >> 8) & 0xff;
  206. }
  207.  
  208. void MWrite2B(Byte *Buffer, Word Value)
  209. {
  210.   Buffer[1] = Value & 0xff;
  211.   Buffer[0] = (Value >> 8) & 0xff;
  212. }
  213.  
  214. LongWord MRead4L(Byte *Buffer)
  215. {
  216.   return (((LongWord) Buffer[3]) << 24) |
  217.          (((LongWord) Buffer[2]) << 16) |
  218.          (((LongWord) Buffer[1]) << 8)  | Buffer[0];
  219. }
  220.  
  221. LongWord MRead4B(Byte *Buffer)
  222. {
  223.   return (((LongWord) Buffer[0]) << 24) |
  224.          (((LongWord) Buffer[1]) << 16) |
  225.          (((LongWord) Buffer[2]) << 8) | Buffer[3];
  226. }
  227.  
  228. void MWrite4L(Byte *Buffer, LongWord Value)
  229. {
  230.   Buffer[0] = Value & 0xff;
  231.   Buffer[1] = (Value >> 8) & 0xff;
  232.   Buffer[2] = (Value >> 16) & 0xff;
  233.   Buffer[3] = (Value >> 24) & 0xff;
  234. }
  235.  
  236. void MWrite4B(Byte *Buffer, LongWord Value)
  237. {
  238.   Buffer[3] = Value & 0xff;
  239.   Buffer[2] = (Value >> 8) & 0xff;
  240.   Buffer[1] = (Value >> 16) & 0xff;
  241.   Buffer[0] = (Value >> 24) & 0xff;
  242. }
  243.  
  244. #ifdef HAS64
  245. QuadWord MRead8L(Byte *Buffer)
  246. {
  247.   return (((LargeWord) Buffer[7]) << 56) |
  248.          (((LargeWord) Buffer[6]) << 48) |
  249.          (((LargeWord) Buffer[5]) << 40) |
  250.          (((LargeWord) Buffer[4]) << 32) |
  251.          (((LargeWord) Buffer[3]) << 24) |
  252.          (((LargeWord) Buffer[2]) << 16) |
  253.          (((LargeWord) Buffer[1]) << 8)  |
  254.                        Buffer[0];
  255. }
  256.  
  257. QuadWord MRead8B(Byte *Buffer)
  258. {
  259.   return (((LargeWord) Buffer[0]) << 56) |
  260.          (((LargeWord) Buffer[1]) << 48) |
  261.          (((LargeWord) Buffer[2]) << 40) |
  262.          (((LargeWord) Buffer[3]) << 32) |
  263.          (((LargeWord) Buffer[4]) << 24) |
  264.          (((LargeWord) Buffer[7]) << 16) |
  265.          (((LargeWord) Buffer[6]) << 8)  |
  266.                        Buffer[7];
  267. }
  268.  
  269. void MWrite8L(Byte *Buffer, QuadWord Value)
  270. {
  271.   Buffer[0] = Value & 0xff;
  272.   Buffer[1] = (Value >> 8) & 0xff;
  273.   Buffer[2] = (Value >> 16) & 0xff;
  274.   Buffer[3] = (Value >> 24) & 0xff;
  275.   Buffer[4] = (Value >> 32) & 0xff;
  276.   Buffer[5] = (Value >> 40) & 0xff;
  277.   Buffer[6] = (Value >> 48) & 0xff;
  278.   Buffer[7] = (Value >> 56) & 0xff;
  279. }
  280.  
  281. void MWrite8B(Byte *Buffer, QuadWord Value)
  282. {
  283.   Buffer[7] = Value & 0xff;
  284.   Buffer[6] = (Value >> 8) & 0xff;
  285.   Buffer[5] = (Value >> 16) & 0xff;
  286.   Buffer[4] = (Value >> 24) & 0xff;
  287.   Buffer[3] = (Value >> 32) & 0xff;
  288.   Buffer[2] = (Value >> 40) & 0xff;
  289.   Buffer[1] = (Value >> 48) & 0xff;
  290.   Buffer[0] = (Value >> 56) & 0xff;
  291. }
  292. #endif
  293.  
  294.  
  295. static void CheckSingle(int Is, int Should, const char *Name)
  296. {
  297.   if (Is != Should)
  298.   {
  299.     fprintf(stderr, "Configuration error: Sizeof(%s) is %d, should be %d\n",
  300.             Name, Is, Should);
  301.     exit(255);
  302.   }
  303. }
  304.  
  305. static void CheckDataTypes(void)
  306. {
  307.   int intsize = sizeof(int);
  308.  
  309.   if (intsize < 2)
  310.   {
  311.     fprintf(stderr, "Configuration error: Sizeof(int) is %d, should be >=2\n",
  312.             (int) sizeof(int));
  313.     exit(255);
  314.   }
  315.   CheckSingle(sizeof(Byte),    1, "Byte");
  316.   CheckSingle(sizeof(ShortInt), 1, "ShortInt");
  317. #ifdef HAS16
  318.   CheckSingle(sizeof(Word),    2, "Word");
  319.   CheckSingle(sizeof(Integer), 2, "Integer");
  320. #endif
  321.   CheckSingle(sizeof(LongInt), 4, "LongInt");
  322.   CheckSingle(sizeof(LongWord), 4, "LongWord");
  323. #ifdef HAS64
  324.   CheckSingle(sizeof(QuadInt), 8, "QuadInt");
  325.   CheckSingle(sizeof(QuadWord), 8, "QuadWord");
  326. #endif
  327.   CheckSingle(sizeof(Single),  4, "Single");
  328.   CheckSingle(sizeof(Double),  8, "Double");
  329. }
  330.  
  331.  
  332. static const char *AssignSingle(int size)
  333. {
  334.   if (size == sizeof(short))
  335.     return "%d";
  336.   else if (size == sizeof(int))
  337.     return "%d";
  338.   else if (size == sizeof(long))
  339.     return "%ld";
  340. #ifndef NOLONGLONG
  341.   else if (size == sizeof(long long))
  342.     return "%lld";
  343. #endif
  344.   else
  345.   {
  346.     fprintf(stderr,
  347.             "Configuration error: cannot assign format string for integer of size %d\n", size);
  348.     exit(255);
  349.     return "";
  350.   }
  351. }
  352.  
  353. static const char *AssignHSingle(int size)
  354. {
  355.   if (size == sizeof(short))
  356.     return "%x";
  357.   else if (size == sizeof(int))
  358.     return "%x";
  359.   else if (size == sizeof(long))
  360.     return "%lx";
  361. #ifndef NOLONGLONG
  362.   else if (size == sizeof(long long))
  363.     return "%llx";
  364. #endif
  365.   else
  366.   {
  367.     fprintf(stderr,
  368.             "Configuration error: cannot assign format string for integer of size %d\n", size);
  369.     exit(255);
  370.     return "";
  371.   }
  372. }
  373.  
  374. static void AssignFormats(void)
  375. {
  376. #ifdef HAS16
  377.   IntegerFormat = Integ16Format = AssignSingle(2);
  378. #endif
  379.   LongIntFormat = Integ32Format = AssignSingle(4);
  380. #ifdef HAS64
  381.   QuadIntFormat = Integ64Format = AssignSingle(8);
  382. #endif
  383.   LargeIntFormat = AssignSingle(sizeof(LargeInt));
  384.   LargeHIntFormat = AssignHSingle(sizeof(LargeInt));
  385. }
  386.  
  387. void be_le_init(void)
  388. {
  389.   union
  390.   {
  391.     unsigned char field[sizeof(int)];
  392.     int test;
  393.   } TwoFace;
  394.  
  395.   CheckDataTypes();
  396.   AssignFormats();
  397.  
  398.   memset(TwoFace.field, 0, sizeof(int));
  399.   TwoFace.field[0] = 1;
  400.   HostBigEndian = ((TwoFace.test) != 1);
  401. }
  402.