📄 linux.c
字号:
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ +
+ linux.c - this file has linux API native code +
+ +
++++++++++++++++testConversion+++++++++++++++++++++++++++++++++++++++++++++++++++*/
#include "linux.h"
#include <sys/stat.h>
#include <stdlib.h>
U1 fb[8];
/*-----------------------------------------------------------------*/
/*
This method checks the current platform's endianess
big-endian higher-order bytes first ( are at lower mem )
little endian lower-order bytes first ( are at lower mem )
On Intel Hardware using NT 4.0 we get:
value = 0xDEED1234
machine is LITTLE endian - LOWER order bytes come first
here are the bytes
[0]=0x34 [1]=0x12 [2]=0xED [3]=0xDE
*/
void checkEndian()
{
int i = 0xDEED1234;
int j;
unsigned char *buff;
printf("value = %lx\n", i);
buff = (unsigned char*)&i;
if (buff[0] == 0x34)
{
printf("machine is LITTLE endian - LOWER order bytes come first");
}
else
{
printf("machine is BIG endian - HIGHER order bytes come first");
}
printf("\nhere are the 4 bytes\n");
for (j = 0; j < 4; j++)
{
printf(" byte [%d]=%x ", j, buff[j]);
}
printf("\n");
return;
} /*end checkEndian*/
/*-----------------------------------------------------------------*/
/*
Routines below convert bytecode values to native Intel format
*/
U2 bytecodeToWord(U1 bytes[])
{
U2 word;
U1 *buffer;
buffer = (U1*)&word;
buffer[0] = bytes[1];
buffer[1] = bytes[0];
return word;
} /*end bytecodeToWord*/
U4 bytecodeToDWord(U1 bytes[])
{
U4 dword;
U1 *buffer;
buffer = (U1*)&dword;
buffer[0] = bytes[3];
buffer[1] = bytes[2];
buffer[2] = bytes[1];
buffer[3] = bytes[0];
return dword;
} /*end bytecodeToDWord*/
U8 bytecodeToQWord(U1 bytes[])
{
U8 qword;
U1 *buffer;
buffer = (U1*)&qword;
buffer[0] = bytes[7];
buffer[1] = bytes[6];
buffer[2] = bytes[5];
buffer[3] = bytes[4];
buffer[4] = bytes[3];
buffer[5] = bytes[2];
buffer[6] = bytes[1];
buffer[7] = bytes[0];
return qword;
} /*end bytecodeToQWord*/
F4 bytecodeToFloat(U1 bytes[])
{
F4 flt;
U1 *buffer;
buffer = (U1*)&flt;
buffer[0] = bytes[3];
buffer[1] = bytes[2];
buffer[2] = bytes[1];
buffer[3] = bytes[0];
return flt;
} /*end bytecodeToFloat*/
F8 bytecodeToDouble(U1 bytes[])
{
F8 dbl;
U1 *buffer;
buffer = (U1*)&dbl;
buffer[0] = bytes[7];
buffer[1] = bytes[6];
buffer[2] = bytes[5];
buffer[3] = bytes[4];
buffer[4] = bytes[3];
buffer[5] = bytes[2];
buffer[6] = bytes[1];
buffer[7] = bytes[0];
return dbl;
} /*end bytecodeToDouble*/
/*-----------------------------------------------------------------*/
/*
Routines below convert Intel values to bytecode format
*/
void wordToBytecode(U2 word, U1 arr[])
{
U1 *buffer;
buffer = (U1*)&word;
arr[0] = buffer[1];
arr[1] = buffer[0];
return;
} /*end wordToBytecode*/
void dwordToBytecode(U4 dword, U1 arr[])
{
U1 *buffer;
buffer = (U1*)&dword;
arr[0] = buffer[3];
arr[1] = buffer[2];
arr[2] = buffer[1];
arr[3] = buffer[0];
return;
} /*end dwordToBytecode*/
void qwordToBytecode(U8 qword, U1 arr[])
{
U1 *buffer;
buffer = (U1*)&qword;
arr[0] = buffer[7];
arr[1] = buffer[6];
arr[2] = buffer[5];
arr[3] = buffer[4];
arr[4] = buffer[3];
arr[5] = buffer[2];
arr[6] = buffer[1];
arr[7] = buffer[0];
return;
} /*end qwordToBytecode*/
void floatToBytecode(F4 flt, U1 arr[])
{
U1 *buffer;
buffer = (U1*)&flt;
arr[0] = buffer[3];
arr[1] = buffer[2];
arr[2] = buffer[1];
arr[3] = buffer[0];
return;
} /*end floatToBytecode*/
void doubleToBytecode(F8 dbl, U1 arr[])
{
U1 *buffer;
buffer = (U1*)&dbl;
arr[0] = buffer[7];
arr[1] = buffer[6];
arr[2] = buffer[5];
arr[3] = buffer[4];
arr[4] = buffer[3];
arr[5] = buffer[2];
arr[6] = buffer[1];
arr[7] = buffer[0];
return;
} /*end doubleToBytecode*/
/*-----------------------------------------------------------------*/
/*
Test native->bytecode and bytecode->native conversion
Take a value
i) print out the value
ii) print out the byte pattern
iii) convert to bytecode and print byte pattern
iv) convert back to native and print out byte pattern
v) print out the value ( should match that from i) )
*/
void testConversion()
{
S2 w;
S4 d;
S8 q;
F4 f;
F8 db;
S1 *b;
S1 b2[2];
S1 b4[4];
S1 b8[8];
w = (S2)0xAB12;
d = 0xCAFEBABE;
q = 1234567890123;
f = -2.3e-4;
db = 100e2;
checkEndian();
printf("\n---------------------------------\n");
printf("\nw = %hx\n", w);
b = (S1 *)&w;
printf("S1s before wordToBytecode: ");
printBytes(b, 2);
wordToBytecode(w, (U1 *)b2);
printf("Bytecode w: ");
printBytes(b2, 2);
w = bytecodeToWord((U1 *)b2);
printf("S1s after bytecodeToWord: ");
printBytes(b, 2);
printf("\n---------------------------------\n");
printf("\nd = %x\n", d);
b = (S1 *)&d;
printf("before dwordToBytecode: ");
printBytes(b, 4);
dwordToBytecode(d, (U1*)b4);
printf("Bytecode d: ");
printBytes(b4,4);
d = bytecodeToDWord((U1*)b4);
printf("after bytecodeToDWord: ");
printBytes(b,4);
printf("d = %x\n",d);
printf("\n---------------------------------\n");
printf("\nq = %llx\n", q);
b = (S1 *)&q;
printf("before qwordToBytecode: ");
printBytes(b, 8);
qwordToBytecode(q, (U1*)b8);
printf("byecode q: ");
printBytes(b8, 8);
q = bytecodeToQWord((U1*)b8);
printf("after bytecodeToQWord: ");
printBytes(b, 8);
printf("q = %llx\n",q);
printf("\n---------------------------------\n");
printf("\nf = %e\n", f);
b = (S1 *)&f;
printf("before floatToBytecode: ");
printBytes(b, 4);f= atof("-2.3e-4");
floatToBytecode(f, (U1*)b4);
printf("Bytecode f: ");
printBytes(b4, 4);
f = bytecodeToFloat((U1*)b4);
printf("after floatToBytecode: ");
printBytes(b,4);
printf("\nf = %e\n",f);
printf("\n---------------------------------\n");
printf("\ndb = %e\n",db);
b = (S1 *)&db;
printf("before doubleToBytecode: ");
printBytes(b,8);
doubleToBytecode(db, (U1*)b8);
printf("Bytecode db: ");
printBytes(b8,8);
db = bytecodeToDouble((U1*)b8);
printf("after doubleToBytecode: ");
printBytes(b,8);
printf("db = %e\n",db);
return;
} /*end testConversion*/
/*-----------------------------------------------------------------*/
/*
this prints out the bytes of a datatype
*/
void printBytes(S1 bytes[], int nbytes)
{
int i;
for (i = 0; i < nbytes; i++)
{
printf("byte[%u]=%X ", i, (U1)bytes[i]);
}
printf("\n");
return;
} /*end printBytes*/
/*-----------------------------------------------------------------*/
/* Returns the amount of free physical memory in bytes. Again, Linux didn't seem to provide anything outside of /proc/meminfo, free, etc. So, I simply return the max value ( I apologize for this crude hack )*/
U4 getAvailableMemory()
{
return(0xFFFFFFFF);
} /*end getAvailableMemory*/
/*
Returns size of file in bytes
Note, assume file will not be larger than 4GB
because nFileSizeLow is lower 32-bits of file size
*/
U4 getFileSize(char *name)
{ U4 size; S1 ret; struct stat buffer;
ret = lstat(name, &buffer); if (ret == 0)
return (U4)buffer.st_size; else
return 0; } /*end getFileSize*/
void testNativeFunctions()
{
U4 ram;
U4 mb;
/*234,256*/
ram = getFileSize("Makefile");
mb = ram / 1048576;
printf("size of file = %lu = %dMB\n", ram, mb);
return;
} /*test native functions*/
U8 stringToU8(char *str)
{
return (U8)atoll(str);
} /*end stringToU8*/
void shutDown(U1 code)
{
exit(code);
} /*end shutDown*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -