parsescore.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 <venti.h>
  12. int
  13. vtparsescore(char *s, char **prefix, uint8_t score[VtScoreSize])
  14. {
  15. int i, c;
  16. char *buf, *colon;
  17. if((colon = strchr(s, ':')) != nil)
  18. buf = colon+1;
  19. else
  20. buf = s;
  21. if(strlen(buf) != 2*VtScoreSize)
  22. return -1;
  23. memset(score, 0, VtScoreSize);
  24. for(i=0; i<2*VtScoreSize; i++){
  25. if(buf[i] >= '0' && buf[i] <= '9')
  26. c = buf[i] - '0';
  27. else if(buf[i] >= 'a' && buf[i] <= 'z')
  28. c = buf[i] - 'a' + 10;
  29. else if(buf[i] >= 'A' && buf[i] <= 'Z')
  30. c = buf[i] - 'A' + 10;
  31. else
  32. return -1;
  33. if((i & 1) == 0)
  34. c <<= 4;
  35. score[i>>1] |= c;
  36. }
  37. if(colon){
  38. *colon = 0;
  39. if(prefix)
  40. *prefix = s;
  41. }else{
  42. if(prefix)
  43. *prefix = nil;
  44. }
  45. return 0;
  46. }