clitool.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright 2014 Luke Dashjr
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the standard MIT license. See COPYING for more details.
  6. */
  7. #include <ctype.h>
  8. #include <stdbool.h>
  9. #include <stdint.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <unistd.h>
  14. #include <gcrypt.h>
  15. #include "libbase58.h"
  16. static
  17. bool my_sha256(void *digest, const void *data, size_t datasz)
  18. {
  19. gcry_md_hash_buffer(GCRY_MD_SHA256, digest, data, datasz);
  20. return true;
  21. }
  22. static
  23. void usage(const char *prog)
  24. {
  25. fprintf(stderr, "Usage: %s [-c] [-d] [data]\n", prog);
  26. fprintf(stderr, "\t-c Use base58check (default: raw base58)\n");
  27. fprintf(stderr, "\t-d <size> Decode <size> bytes\n");
  28. exit(1);
  29. }
  30. int main(int argc, char **argv)
  31. {
  32. bool b58c = false;
  33. size_t decode = 0;
  34. int opt;
  35. while ( (opt = getopt(argc, argv, "cd:h")) != -1)
  36. {
  37. switch (opt)
  38. {
  39. case 'c':
  40. b58c = true;
  41. b58_sha256_impl = my_sha256;
  42. break;
  43. case 'd':
  44. {
  45. int i = atoi(optarg);
  46. if (i < 0 || (uintmax_t)i >= SIZE_MAX)
  47. usage(argv[0]);
  48. decode = (size_t)i;
  49. break;
  50. }
  51. default:
  52. usage(argv[0]);
  53. }
  54. }
  55. size_t rt;
  56. union {
  57. uint8_t *b;
  58. char *s;
  59. } r;
  60. if (optind >= argc)
  61. {
  62. rt = 0;
  63. r.b = NULL;
  64. while (!feof(stdin))
  65. {
  66. r.b = realloc(r.b, rt + 0x100);
  67. rt += fread(&r.b[rt], 1, 0x100, stdin);
  68. }
  69. if (decode)
  70. while (isspace(r.s[rt-1]))
  71. --rt;
  72. }
  73. else
  74. {
  75. r.s = argv[optind];
  76. rt = strlen(argv[optind]);
  77. }
  78. if (decode)
  79. {
  80. uint8_t bin[decode];
  81. size_t ssz = decode;
  82. if (!b58tobin(bin, &ssz, r.s, rt))
  83. return 2;
  84. if (b58c)
  85. {
  86. int chk = b58check(bin, decode, r.s, rt);
  87. if (chk < 0)
  88. return chk;
  89. if (fwrite(bin, decode, 1, stdout) != 1)
  90. return 3;
  91. }
  92. else
  93. {
  94. // Raw base58 doesn't check length match
  95. uint8_t cbin[ssz];
  96. if (ssz > decode)
  97. {
  98. size_t zeros = ssz - decode;
  99. memset(cbin, 0, zeros);
  100. memcpy(&cbin[zeros], bin, decode);
  101. }
  102. else
  103. memcpy(cbin, &bin[decode - ssz], ssz);
  104. if (fwrite(cbin, ssz, 1, stdout) != 1)
  105. return 3;
  106. }
  107. }
  108. else
  109. {
  110. size_t ssz = rt * 2;
  111. char s[ssz];
  112. bool rv;
  113. if (b58c)
  114. rv = rt && b58check_enc(s, &ssz, r.b[0], &r.b[1], rt-1);
  115. else
  116. rv = b58enc(s, &ssz, r.b, rt);
  117. if (!rv)
  118. return 2;
  119. puts(s);
  120. }
  121. }