mnencode.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <errno.h>
  5. #include "mnemonic.h"
  6. int hex2bytes(const char *in, mn_byte *out) {
  7. int inlen = strlen(in);
  8. char t[3];
  9. int i, j = 0, k = 0;
  10. if(inlen % 2 != 0) {
  11. t[j++] = '0';
  12. }
  13. for(i = 0; i < inlen; i++) {
  14. if(isxdigit(in[i])) {
  15. t[j++] = in[i];
  16. if(j == 2) {
  17. t[2] = '\0';
  18. errno = 0;
  19. out[k++] = strtol(t, NULL, 16);
  20. if(errno != 0) {
  21. return -1;
  22. }
  23. j = 0;
  24. }
  25. }
  26. }
  27. return k;
  28. }
  29. int main(int argc, char **argv) {
  30. mn_byte *buf;
  31. mn_byte cbuf[0x10000];
  32. mn_byte xbuf[0x8000];
  33. char outbuf[0x90000];
  34. int buflen;
  35. int n;
  36. int hex_encoded_input = 0;
  37. if(argc > 1 && strcmp(argv[1],"-x") == 0) {
  38. hex_encoded_input = 1;
  39. }
  40. fprintf (stderr, "%s\n", mn_wordlist_version);
  41. buflen = fread(cbuf, 1, sizeof cbuf, stdin);
  42. if(hex_encoded_input) {
  43. buflen = hex2bytes(cbuf, xbuf);
  44. if(buflen < 0) {
  45. perror("hex2bytes");
  46. return EXIT_FAILURE;
  47. }
  48. buf = xbuf;
  49. } else {
  50. buf = cbuf;
  51. }
  52. n = mn_encode(buf, buflen, outbuf, sizeof outbuf, "x x x. x x x. x x x\n");
  53. if(n == 0) {
  54. fwrite(outbuf, 1, strlen (outbuf), stdout);
  55. } else {
  56. fprintf(stderr, "mn_encode error %d\n", n);
  57. }
  58. putchar('\n');
  59. return EXIT_SUCCESS;
  60. }