1 /* ====================================================================
2  * Copyright (c) 2002 Johnny Shelley.  All rights reserved.
3  *
4  * Bcrypt is licensed under the BSD software license. See the file
5  * called 'LICENSE' that you should have received with this software
6  * for details
7  * ====================================================================
8  */
9 
10 #include "includes.h"
11 #include "defines.h"
12 #include "functions.h"
13 
getEndian(unsigned char ** e)14 void getEndian(unsigned char **e) {
15   short i = 0x4321;
16   int bigE = (*(char*) &i);
17 
18   if ((*e = realloc(*e, sizeof(char) + 1)) == NULL)
19     memerror();
20 
21   memset(*e, 0, sizeof(char) + 1);
22 
23   if (bigE == 0x43)
24     memset(*e, endianBig, 1);
25   else if (bigE == 0x21)
26     memset(*e, endianLittle, 1);
27 }
28 
swapEndian(uInt32 value)29 uInt32 swapEndian(uInt32 value) {
30   unsigned int b1, b2, b3, b4;
31   uInt32 swapped;
32 
33   b4 = (value>>24) & 0xff;
34   b3 = (value>>16) & 0xff;
35   b2 = (value>>8) & 0xff;
36   b1 = value & 0xff;
37 
38   swapped = (b1<<24) | (b2<<16) | (b3<<8) | b4;
39   return(swapped);
40 }
41 
testEndian(char * input)42 int testEndian(char *input) {
43   unsigned char *myEndian = NULL, *yourEndian = NULL;
44 
45   getEndian(&myEndian);
46   if ((yourEndian = malloc(2)) == NULL)
47     memerror();
48 
49   memset(yourEndian, 0, 2);
50   memcpy(yourEndian, input, 1);
51 
52   if (memcmp(myEndian, yourEndian, 1) == 0)
53     return(0);
54 
55   return(1);
56 }
57 
swapCompressed(char ** input,uLong sz)58 int swapCompressed(char **input, uLong sz) {
59   char c;
60   unsigned int j, swap;
61 
62   memcpy(&c, *input+1, 1);
63 
64   if (c == 0)
65     return(0);
66 
67   j = sizeof(uInt32);
68 
69   memcpy(&swap, *input+(sz - j), j);
70   swap = swapEndian(swap);
71   memcpy(*input+(sz - j), &swap, j);
72 
73   return(1);
74 }
75