Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Data structures for files */
- char inputFile[MAX_FILE_SIZE];
- char outputFile[MAX_FILE_SIZE];
- /*
- * We define some data structures for ACMEBlockchain implementation
- */
- struct block_header_t
- {
- char corporation[4];
- int32_t version_number;
- char genesis_block_hash[16];
- int32_t blocksize;
- int32_t block_transaction_index;
- } header;
- struct simple_transaction
- {
- char account_sender[8];
- char account_receiver[8];
- int32_t amount;
- } transaction;
- struct block
- {
- int32_t index;
- char previous_block_hash[16];
- char block_hash[16];
- int32_t nonce;
- struct simple_transaction block_transactions[5];
- } block;
- // TODO
- puts(" ___ _______ __ __ _______");
- puts(" / | / _____/ / | / | / _____/");
- puts(" / /| | / / / | / | / /____ ");
- puts(" / /_| | / / / /| |/ /| | / _____/ ");
- puts(" / ___ | / /____ / / |___/ | | / /____ ");
- puts(" /_/ |_| /______/ /_/ |_| /______/ n");
- puts(" [BLOCKCHAIN] n");
- if (argc != 3) {
- puts("[*] Usage is: acmeblockchain input_file output_filen");
- exit(EXIT_FAILURE);
- }
- /* Print some user info */
- for (int i = 0; envp[i] != NULL; i++)
- {
- if (strlen(envp[i]) > 3)
- {
- if (memcmp(envp[i],"TMP",3) == 0)
- {
- printf("[*] Temporary folder is %s.n", envp[i]);
- }
- else if (memcmp(envp[i],"USER",4) == 0)
- {
- printf("[*] Software licensed to %s.n", envp[i]);
- }
- }
- }
- /* Set filenames */
- strncpy(inputFile, argv[1], MAX_FILE_SIZE);
- strncpy(outputFile, argv[2], MAX_FILE_SIZE);
- // Open input file
- FILE * fin = fopen(inputFile, "rb");
- if (fin == NULL)
- {
- perror(__FILE__);
- exit(EXIT_FAILURE);
- }
- // Open output file
- FILE * fout = fopen(outputFile, "wb");
- if (fout == NULL)
- {
- perror(__FILE__);
- exit(EXIT_FAILURE);
- }
- /* Read main header */
- fread(&header, sizeof(struct block_header_t), 1, fin);
- if (memcmp(header.corporation, "ACME", 4) != 0
- || memcmp(header.genesis_block_hash, "A3812345BC0112DD", 4) != 0) {
- fprintf(stderr, "[*] Unknown input formatn");
- exit(EXIT_FAILURE);
- }
- /* Read block transaction index to determine if we can store a new transaction in this block */
- if (header.block_transaction_index > 4)
- {
- fprintf(stderr, "[*] This block can't store more transactionsn");
- exit(EXIT_FAILURE);
- }
- /* Read new transaction */
- fread(&transaction, sizeof(struct simple_transaction), 1, fin);
- /* Print some info to user */
- printf("[*] The account %.8s has send %d coins to account %.8sn", transaction.account_sender, transaction.amount, transaction.account_receiver);
- /* Read block and add the new transaction to transactions array */
- fread(&block, header.blocksize, 1, fin);
- block.block_transactions[header.block_transaction_index] = transaction;
- header.block_transaction_index++;
- /* Write main header and block in fout*/
- fwrite(&header, sizeof(struct block_header_t), 1, fout);
- fwrite(&block, sizeof(struct block), 1, fout);
- /* Close files */
- fclose(fin);
- fclose(fout);
- // Done!
- return 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement