
API in C
223
int readLen(int fdSock)
{
char cFirstChar; // first character read from socket
char *cLength; // length of next message to read...will be cast to int at the end
int *iLen; // calculated length of next message (Cast to int)
cLength = calloc(sizeof(int), 1);
DEBUG ? printf("start readLen()\n") : 0;
read(fdSock, &cFirstChar, 1);
DEBUG ? printf("byte1 = %#x\n", cFirstChar) : 0;
// read 4 bytes
// this code SHOULD work, but is untested...
if ((cFirstChar & 0xE0) == 0xE0)
{
DEBUG ? printf("4-byte encoded length\n") : 0;
if (iLittleEndian)
{
cLength[3] = cFirstChar;
cLength[3] &= 0x1f; // mask out the 1st 3 bits
read(fdSock, &cLength[2], 1);
read(fdSock, &cLength[1], 1);
read(fdSock, &cLength[0], 1);
}
else
{
cLength[0] = cFirstChar;
cLength[0] &= 0x1f; // mask out the 1st 3 bits
read(fdSock, &cLength[1], 1);
read(fdSock, &cLength[2], 1);
read(fdSock, &cLength[3], 1);
}
iLen = (int *)cLength;
}
// read 3 bytes
else if ((cFirstChar & 0xC0) == 0xC0)
{
DEBUG ? printf("3-byte encoded length\n") : 0;
if (iLittleEndian)
{
Komentarze do niniejszej Instrukcji