Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
1.8 KiB

  1. long
  2. EncodeInteger(
  3. unsigned char * pbEncoded,
  4. unsigned char * pbInt,
  5. unsigned long dwLen,
  6. int Writeflag)
  7. {
  8. long count;
  9. unsigned long i;
  10. long j;
  11. if (Writeflag)
  12. pbEncoded[0] = 0x02;
  13. count = 1;
  14. i = dwLen - 1;
  15. // find the most significant non-zero unsigned char
  16. while ((pbInt[i] == 0) && (i > 0))
  17. i--;
  18. if ((i == 0) && (pbInt[i] == 0))
  19. // this means that the integer value is 0
  20. {
  21. if (Writeflag)
  22. {
  23. pbEncoded[1] = 0x01;
  24. pbEncoded[2] = 0x00;
  25. }
  26. count += 2;
  27. }
  28. else
  29. {
  30. // if the most significant bit of the most sig unsigned char is set
  31. // then need to add a 0 unsigned char to the beginning.
  32. if (pbInt[i] > 0x7F)
  33. {
  34. // encode the length
  35. count += EncodeLength (pbEncoded + count, i+2, Writeflag);
  36. if (Writeflag)
  37. {
  38. // set the first unsigned char of the integer to zero and increment count
  39. pbEncoded[count++] = 0x00;
  40. // copy the integer unsigned chars into the encoded buffer
  41. j = i;
  42. while (j >= 0)
  43. pbEncoded[count++] = pbInt[j--];
  44. }
  45. }
  46. else
  47. {
  48. // encode the length
  49. count += EncodeLength (pbEncoded + count, i+1, Writeflag);
  50. // copy the integer unsigned chars into the encoded buffer
  51. if (Writeflag)
  52. {
  53. j = i;
  54. while (j >= 0)
  55. pbEncoded[count++] = pbInt[j--];
  56. }
  57. }
  58. }
  59. return (count);
  60. }