util.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #include "ctype.h"
  2. #include "util.h"
  3. #include "comdef.h"
  4. #include "encoding.h"
  5. /*===========================================================================
  6. FUNCTION: sys_memcpy
  7. DESCRIPTION:
  8. copy one block memory to another position
  9. INPUT PARAMETER:
  10. void *p_des: destination address
  11. const void * p_src: source address
  12. unsigned long size: length of memory to copy (unit is byte)
  13. OUTPUT PARAMETER:
  14. RETURN VALUE:
  15. ===========================================================================*/
  16. void * sys_memcpy(void *p_des,const void * p_src,unsigned long size)
  17. {
  18. char *tmp = p_des;
  19. const char *s = p_src;
  20. while (size--)
  21. *tmp++ = *s++;
  22. return p_des;
  23. }
  24. int sys_memcmp(const void * cs,const void * ct,unsigned int count)
  25. {
  26. const unsigned char *su1, *su2;
  27. int res = 0;
  28. for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
  29. if ((res = *su1 - *su2) != 0)
  30. break;
  31. return res;
  32. }
  33. void * _memcpy(void * dest,const void *src,unsigned int count)
  34. {
  35. char *tmp = (char *) dest;
  36. const char *s = (char *) src;
  37. while (count--)
  38. *tmp++ = *s++;
  39. return dest;
  40. }
  41. /*===========================================================================
  42. FUNCTION: sys_memcpy_32
  43. DESCRIPTION:
  44. copy one block memory to another position
  45. caller guarantee the src/des address are DWORD allign
  46. INPUT PARAMETER:
  47. void *p_des: destination address
  48. const void * p_src: source address
  49. unsigned long size: length of memory to copy (unit is DWORD)
  50. OUTPUT PARAMETER:
  51. RETURN VALUE:
  52. ===========================================================================*/
  53. void sys_memcpy_32(void *p_des,const void * p_src,unsigned long size)
  54. {
  55. unsigned long i;
  56. for (i=0;i<size;i++)
  57. *((unsigned long*)p_des+i) = *((unsigned long*)p_src+i);
  58. }
  59. /*===========================================================================
  60. FUNCTION: sys_memset
  61. DESCRIPTION:
  62. fill memory with specifed value
  63. INPUT PARAMETER:
  64. void *p_des: destination address
  65. int c: value to set
  66. unsigned long size: length of memory
  67. OUTPUT PARAMETER:
  68. RETURN VALUE:
  69. ===========================================================================*/
  70. void sys_memset(void *p_des,unsigned char c,unsigned long size)
  71. {
  72. unsigned long i;
  73. for (i=0;i<size;i++)
  74. *((char*)p_des+i) = c;
  75. }
  76. /*===========================================================================
  77. FUNCTION: sys_memset32
  78. DESCRIPTION:
  79. fill memory with specifed value
  80. INPUT PARAMETER:
  81. void *p_des: destination address
  82. int c: value to set
  83. unsigned long size: length of memory in word(32bit)
  84. OUTPUT PARAMETER:
  85. RETURN VALUE:
  86. ===========================================================================*/
  87. void sys_memset32(void *p_des,int c,unsigned long size)
  88. {
  89. unsigned long i;
  90. for(i=0; i< size; i++)
  91. ((unsigned long*)p_des)[i] = c;
  92. }
  93. int atoi(const char *nptr)
  94. {
  95. int c; /* current char */
  96. int total; /* current total */
  97. int sign; /* if '-', then negative, otherwise positive */
  98. /* skip whitespace */
  99. while ( isspace((int)(unsigned char)*nptr) )
  100. ++nptr;
  101. c = (int)(unsigned char)*nptr++;
  102. sign = c; /* save sign indication */
  103. if (c == '-' || c == '+')
  104. c = (int)(unsigned char)*nptr++; /* skip sign */
  105. total = 0;
  106. while (isdigit(c)) {
  107. total = 10 * total + (c - '0'); /* accumulate digit */
  108. c = (int)(unsigned char)*nptr++; /* get next char */
  109. }
  110. if (sign == '-')
  111. return -total;
  112. else
  113. return total; /* return result, negated if necessary */
  114. }