Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. /* Data structures for files */
  2. char inputFile[MAX_FILE_SIZE];
  3. char outputFile[MAX_FILE_SIZE];
  4.  
  5. /*
  6. * We define some data structures for ACMEBlockchain implementation
  7. */
  8. struct block_header_t
  9. {
  10. char corporation[4];
  11. int32_t version_number;
  12. char genesis_block_hash[16];
  13. int32_t blocksize;
  14. int32_t block_transaction_index;
  15. } header;
  16.  
  17. struct simple_transaction
  18. {
  19. char account_sender[8];
  20. char account_receiver[8];
  21. int32_t amount;
  22. } transaction;
  23.  
  24. struct block
  25. {
  26. int32_t index;
  27. char previous_block_hash[16];
  28. char block_hash[16];
  29. int32_t nonce;
  30. struct simple_transaction block_transactions[5];
  31. } block;
  32.  
  33. // TODO
  34. puts(" ___ _______ __ __ _______");
  35. puts(" / | / _____/ / | / | / _____/");
  36. puts(" / /| | / / / | / | / /____ ");
  37. puts(" / /_| | / / / /| |/ /| | / _____/ ");
  38. puts(" / ___ | / /____ / / |___/ | | / /____ ");
  39. puts(" /_/ |_| /______/ /_/ |_| /______/ n");
  40. puts(" [BLOCKCHAIN] n");
  41.  
  42. if (argc != 3) {
  43. puts("[*] Usage is: acmeblockchain input_file output_filen");
  44. exit(EXIT_FAILURE);
  45. }
  46.  
  47. /* Print some user info */
  48. for (int i = 0; envp[i] != NULL; i++)
  49. {
  50. if (strlen(envp[i]) > 3)
  51. {
  52. if (memcmp(envp[i],"TMP",3) == 0)
  53. {
  54. printf("[*] Temporary folder is %s.n", envp[i]);
  55. }
  56. else if (memcmp(envp[i],"USER",4) == 0)
  57. {
  58. printf("[*] Software licensed to %s.n", envp[i]);
  59. }
  60. }
  61. }
  62.  
  63. /* Set filenames */
  64. strncpy(inputFile, argv[1], MAX_FILE_SIZE);
  65. strncpy(outputFile, argv[2], MAX_FILE_SIZE);
  66.  
  67. // Open input file
  68. FILE * fin = fopen(inputFile, "rb");
  69.  
  70. if (fin == NULL)
  71. {
  72. perror(__FILE__);
  73. exit(EXIT_FAILURE);
  74. }
  75.  
  76. // Open output file
  77. FILE * fout = fopen(outputFile, "wb");
  78.  
  79. if (fout == NULL)
  80. {
  81. perror(__FILE__);
  82. exit(EXIT_FAILURE);
  83. }
  84.  
  85. /* Read main header */
  86. fread(&header, sizeof(struct block_header_t), 1, fin);
  87.  
  88. if (memcmp(header.corporation, "ACME", 4) != 0
  89. || memcmp(header.genesis_block_hash, "A3812345BC0112DD", 4) != 0) {
  90.  
  91. fprintf(stderr, "[*] Unknown input formatn");
  92. exit(EXIT_FAILURE);
  93. }
  94.  
  95. /* Read block transaction index to determine if we can store a new transaction in this block */
  96. if (header.block_transaction_index > 4)
  97. {
  98. fprintf(stderr, "[*] This block can't store more transactionsn");
  99. exit(EXIT_FAILURE);
  100. }
  101.  
  102.  
  103. /* Read new transaction */
  104. fread(&transaction, sizeof(struct simple_transaction), 1, fin);
  105.  
  106. /* Print some info to user */
  107. printf("[*] The account %.8s has send %d coins to account %.8sn", transaction.account_sender, transaction.amount, transaction.account_receiver);
  108.  
  109. /* Read block and add the new transaction to transactions array */
  110. fread(&block, header.blocksize, 1, fin);
  111. block.block_transactions[header.block_transaction_index] = transaction;
  112. header.block_transaction_index++;
  113.  
  114. /* Write main header and block in fout*/
  115. fwrite(&header, sizeof(struct block_header_t), 1, fout);
  116. fwrite(&block, sizeof(struct block), 1, fout);
  117.  
  118. /* Close files */
  119. fclose(fin);
  120. fclose(fout);
  121.  
  122. // Done!
  123. return 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement