utils.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include "flashfs.h"
  12. char* prog;
  13. uint32_t sectsize;
  14. uint32_t nsects;
  15. uint8_t* sectbuff;
  16. int readonly;
  17. uint32_t delta;
  18. int eparity;
  19. uint8_t magic[] = { MAGIC0, MAGIC1, MAGIC2, FFSVERS };
  20. int
  21. putc3(uint8_t *buff, uint32_t v)
  22. {
  23. if(v < (1 << 7)) {
  24. buff[0] = v;
  25. return 1;
  26. }
  27. if(v < (1 << 13)) {
  28. buff[0] = 0x80 | (v >> 7);
  29. buff[1] = v & ((1 << 7) - 1);
  30. return 2;
  31. }
  32. if(v < (1 << 21)) {
  33. buff[0] = 0x80 | (v >> 15);
  34. buff[1] = 0x80 | ((v >> 8) & ((1 << 7) - 1));
  35. buff[2] = v;
  36. return 3;
  37. }
  38. fprint(2, "%s: putc3 fail 0x%lx, called from %#p\n", prog, v, getcallerpc());
  39. abort();
  40. return -1;
  41. }
  42. int
  43. getc3(uint8_t *buff, uint32_t *p)
  44. {
  45. int c, d;
  46. c = buff[0];
  47. if((c & 0x80) == 0) {
  48. *p = c;
  49. return 1;
  50. }
  51. c &= ~0x80;
  52. d = buff[1];
  53. if((d & 0x80) == 0) {
  54. *p = (c << 7) | d;
  55. return 2;
  56. }
  57. d &= ~0x80;
  58. *p = (c << 15) | (d << 8) | buff[2];
  59. return 3;
  60. }
  61. uint32_t
  62. get4(uint8_t *b)
  63. {
  64. return (b[0] << 0) |
  65. (b[1] << 8) |
  66. (b[2] << 16) |
  67. (b[3] << 24);
  68. }
  69. void
  70. put4(uint8_t *b, uint32_t v)
  71. {
  72. b[0] = v >> 0;
  73. b[1] = v >> 8;
  74. b[2] = v >> 16;
  75. b[3] = v >> 24;
  76. }