[558161]: corelib / Util.h Maximize Restore History

Download this file

Util.h    153 lines (133 with data), 4.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Util.h
//-----------------------------------------------------------------------------
#ifndef Util_h
#define Util_h
#include "MyString.h"
#include "Fish.h"
#include "PwsPlatform.h"
#define SaltLength 20
#define StuffSize 10
#define SaltLengthV3 32
// this is for the undocumented 'command line file encryption'
#define CIPHERTEXT_SUFFIX ".PSF"
//Use non-standard dash (ANSI decimal 173) for separation
#define SPLTCHR _T('\xAD')
#define SPLTSTR _T(" \xAD ")
#define DEFUSERCHR _T('\xA0')
//Version defines
#define V10 0
#define V15 1
#ifdef _WIN32
//Some extra typedefs -- I'm addicted to typedefs
typedef char int8;
typedef short int16;
typedef int int32;
typedef __int64 int64;
typedef unsigned char uint8;
typedef unsigned short uint16;
typedef unsigned int uint32;
typedef unsigned __int64 uint64;
typedef unsigned __int64 ulong64;
typedef unsigned long ulong32;
#else
#include <sys/types.h>
typedef int8_t int8;
typedef int16_t int16;
typedef int32_t int32;
typedef int64_t int64;
typedef u_int8_t uint8;
typedef u_int16_t uint16;
typedef u_int32_t uint32;
typedef u_int64_t uint64;
#endif
extern void trashMemory(void* buffer, long length );
extern void trashMemory( LPTSTR buffer, long length );
extern void burnStack(unsigned long len); // borrowed from libtomcrypt
extern void GenRandhash(const CMyString &passkey,
const unsigned char* m_randstuff,
unsigned char* m_randhash);
// fill this buffer with random data
void GetRandomData( void * const buffer, unsigned long length );
// generate a random number between 0 and len
unsigned int RangeRand(size_t len);
// buffer is allocated by _readcbc, *** delete[] is responsibility of caller ***
extern int _readcbc(FILE *fp, unsigned char* &buffer, unsigned int &buffer_len,
unsigned char &type, Fish *Algorithm,
unsigned char* cbcbuffer,
const unsigned char *TERMINAL_BLOCK = NULL);
extern int _writecbc(FILE *fp, const unsigned char* buffer, int length,
unsigned char type, Fish *Algorithm,
unsigned char* cbcbuffer);
/*
* Get an integer that is stored in little-endian format
*/
inline int getInt32(const unsigned char buf[4])
{
ASSERT(sizeof(int) == 4);
#if defined(PWS_LITTLE_ENDIAN)
#if defined(_DEBUG)
if ( *(int*) buf != (buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24)) )
{
TRACE0( "Warning: PWS_LITTLE_ENDIAN defined but architecture is big endian\n" );
}
#endif
return *(int *) buf;
#elif defined(PWS_BIG_ENDIAN)
#if defined(_DEBUG)
// Folowing code works for big or little endian architectures but we'll warn anyway
// if CPU is really little endian
if ( *(int*) buf == (buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24)) )
{
TRACE0( "Warning: PWS_BIG_ENDIAN defined but architecture is little endian\n" );
}
#endif
return (buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24) );
#else
#error Is the target CPU big or little endian?
#endif
}
/*
* Store an integer that is stored in little-endian format
*/
inline void putInt32(unsigned char buf[4], const int val )
{
ASSERT(sizeof(int) == 4);
#if defined(PWS_LITTLE_ENDIAN)
*(int32 *) buf = val;
#if defined(_DEBUG)
if ( *(int*) buf != (buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24)) )
{
TRACE0( "Warning: PWS_LITTLE_ENDIAN defined but architecture is big endian\n" );
}
#endif
#elif defined(PWS_BIG_ENDIAN)
buf[0] = val & 0xFF;
buf[1] = (val >> 8) & 0xFF;
buf[2] = (val >> 16) & 0xFF;
buf[3] = (val >> 24) & 0xFF;
#if defined(_DEBUG)
// Above code works for big or little endian architectures but we'll warn anyway
// if CPU is really little endian
if ( *(int*) buf == (buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24)) )
{
TRACE0( "Warning: PWS_BIG_ENDIAN defined but architecture is little endian\n" );
}
#endif
#else
#error Is the target CPU big or little endian?
#endif
}
class PWSUtil {
public:
// namespace, really, of common utility functions
// For Windows implementation, hide Unicode abstraction,
// and use secure versions (_s) when available
static LPTSTR strCopy(LPTSTR target, size_t tcount, const LPCTSTR source, size_t scount);
static size_t strLength(const LPCTSTR str);
static long fileLength(FILE *fp);
};
#endif // Util_h
//-----------------------------------------------------------------------------
// Local variables:
// mode: c++
// End: