#include "PalmType.h"  
#include "Palm1Lib.h"

#include <string.h>


 UCHAR P_HIBYTE(USHORT worddata)
{
	return (UCHAR)((worddata & 0xff00) >> 8);
}

 UCHAR P_LOBYTE(USHORT worddata)
{
	return (UCHAR)(worddata & 0x00ff);
}

 USHORT P_LOWORD( longdata)
{
	return (USHORT)(longdata & 0x0000ffff);
}

 USHORT P_HIWORD( longdata)
{
	return (USHORT)((longdata & 0xffff0000) >> 16);
}

 USHORT P_MAKEWORD(UCHAR lowbyte, UCHAR highbyte)
{
	USHORT rt;
	rt = highbyte;
	rt <<= 8;
	rt += lowbyte;
	return rt;
}

  P_MAKELONG(USHORT lowword, USHORT highword)
{
	 rt;
	rt = highword;
	rt <<= 16;
	rt += lowword;
	return rt;
}

  PXCHAR2ULONG(PXCHAR src)
{
	 dst;
	dst = P_MAKELONG(P_MAKEWORD(src[3], src[2]), P_MAKEWORD(src[1], src[0]));
	return dst;
}

 void ULONG2PUCHAR( src, PUCHAR dst)
{
	dst[0] = P_HIBYTE(P_HIWORD(src));
	dst[1] = P_LOBYTE(P_HIWORD(src));
	dst[2] = P_HIBYTE(P_LOWORD(src));
	dst[3] = P_LOBYTE(P_LOWORD(src));
}

		PXCHAR2ULONG_L(PXCHAR src)
{
	 dst;
	dst = P_MAKELONG(P_MAKEWORD(src[0], src[1]), P_MAKEWORD(src[2], src[3]));
	return dst;
}

	void	ULONG2PUCHAR_L( src, PUCHAR dst)
{
	dst[3] = P_HIBYTE(P_HIWORD(src));
	dst[2] = P_LOBYTE(P_HIWORD(src));
	dst[1] = P_HIBYTE(P_LOWORD(src));
	dst[0] = P_LOBYTE(P_LOWORD(src));
}

 USHORT PXCHAR2USHORT(PXCHAR src)
{
	USHORT dst;
	dst = P_MAKEWORD(src[1], src[0]);
	return dst;
}

 void USHORT2PXCHAR(USHORT src, PXCHAR dst)
{
	dst[0] = P_HIBYTE(src);
	dst[1] = P_LOBYTE(src);
}

 USHORT PXCHAR2USHORT_L(PXCHAR src)
{
	USHORT dst;
	dst = P_MAKEWORD(src[0], src[1]);
	return dst;
}

 void USHORT2PXCHAR_L(USHORT src, PXCHAR dst)
{
	dst[1] = P_HIBYTE(src);
	dst[0] = P_LOBYTE(src);
}

 void PXCHAR2PXLONG_L(PXLONG DST, PXCHAR SRC, UCHAR LEN)
{
	memcpy(DST, SRC, LEN*4);
}

 PXCHAR p_memcpy_x2x_0(PXCHAR dst, PXCHAR src, UCHAR count)
{
	PXCHAR pData;

	pData = p_memcpy(dst, src, count);
	if (count == 0)
	{
		dst[256] = 0;
	}
	else
	{
		dst[count] = 0;
	}
	return pData;
}

 PXCHAR p_memcpy_c2x_0(PXCHAR dst, PCCHAR src, UCHAR count)
{
	return p_memcpy_x2x_0(dst, src, count);
}

 PXCHAR p_wmemcpy_x2x(PXCHAR dst, PXCHAR src, USHORT count)	//dst must be less than src
{
	return (PXCHAR)memcpy(dst, src, count);
}

// memory transfer functions
 PUCHAR p_memcpy(PUCHAR dst, PUCHAR src, UCHAR count)
{
	if (count == 0)
	{
		return (PUCHAR)memcpy(dst, src, 256);
	}
	else
	{
		return (PUCHAR)memcpy(dst, src, count);
	}
}

 UCHAR p_memcmp(PXCHAR dst, PCCHAR src, UCHAR count)
{
	if (count == 0)
	{
		return (UCHAR)memcmp(dst, src, 256);
	}
	else
	{
		return (UCHAR)memcmp(dst, src, count);
	}
}

 PUCHAR p_memset(PUCHAR dst, UCHAR val, UCHAR count)
{
	if (count == 0)
	{
		return (PUCHAR)memset(dst, val, 256);
	}
	else
	{
		return (PUCHAR)memset(dst, val, count);
	}
}

 PUCHAR p_strcpy(PXCHAR dst, PCCHAR src)
{
	return (PUCHAR)strcpy((char *)dst, (const char *)src);
}

 UCHAR p_strlen(PUCHAR dst)
{
	return (UCHAR)strlen((const char *)dst);
}

 PUCHAR p_strcat(PXCHAR dst, PCCHAR src)
{
	return (PUCHAR)strcat((char *)dst, (const char *)src);
}

 PUCHAR p_strchr(PCCHAR string, UCHAR ch)
{
	return (PUCHAR)strchr((const char *)string, ch);
}

 UCHAR p_strcmp(PXCHAR dst, PCCHAR src)
{
	return (UCHAR)strcmp((const char *)dst, (const char *)src);
}
