mptobe.c 915 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include "os.h"
  2. #include <mp.h>
  3. #include "dat.h"
  4. // convert an mpint into a big endian byte array (most significant byte first)
  5. // return number of bytes converted
  6. // if p == nil, allocate and result array
  7. int
  8. mptobe(mpint *b, uchar *p, uint n, uchar **pp)
  9. {
  10. int i, j, suppress;
  11. mpdigit x;
  12. uchar *e, *s, c;
  13. if(p == nil){
  14. n = (b->top+1)*Dbytes;
  15. p = malloc(n);
  16. setmalloctag(p, getcallerpc(&b));
  17. }
  18. if(p == nil)
  19. return -1;
  20. if(pp != nil)
  21. *pp = p;
  22. memset(p, 0, n);
  23. // special case 0
  24. if(b->top == 0){
  25. if(n < 1)
  26. return -1;
  27. else
  28. return 1;
  29. }
  30. s = p;
  31. e = s+n;
  32. suppress = 1;
  33. for(i = b->top-1; i >= 0; i--){
  34. x = b->p[i];
  35. for(j = Dbits-8; j >= 0; j -= 8){
  36. c = x>>j;
  37. if(c == 0 && suppress)
  38. continue;
  39. if(p >= e)
  40. return -1;
  41. *p++ = c;
  42. suppress = 0;
  43. }
  44. }
  45. // guarantee at least one byte
  46. if(s == p){
  47. if(p >= e)
  48. return -1;
  49. *p++ = 0;
  50. }
  51. return p - s;
  52. }