static quik_err_t load_image(path_t *path, vaddr_t *where, length_t *len) { quik_err_t err; printk("Loading '%P'\n", path); err = file_len(path, len); if (err != ERR_NONE) { printk("Error fetching size for '%P': %r\n", path, err); return err; } *where = (vaddr_t) prom_claim_chunk((void *) *where, *len); if (*where == (vaddr_t) -1) { printk("Couldn't claim 0x%x bytes to load '%P'\n", *len, path); return ERR_NO_MEM; } err = file_load(path, (void *) *where); if (err != ERR_NONE) { printk("Error loading '%P': %r\n", path, err); prom_release((void *) *where, *len); return err; } return ERR_NONE; }
int main(int argc, char ** argv) { int i; be_node * ben_res; FILE * f; int flen; char * data; f = fopen(argv[1], "r"); if (!f) { printf("file open fail\n"); exit(1); } flen = file_len(f); data = (char *) malloc ( sizeof(char) * flen ); fread(data, sizeof(char), flen, f); fclose(f); ben_res = be_decoden(data, flen); printfile(ben_res, 0); return 0; }
int aes::AES_do_decrypt_from_file(char *infile, char *outfile, unsigned long *CifKey) { BYTE* in = new BYTE[4*Nb]; printf("Decoding...\n"); GenPowerTab(); GenSubBytesTab(); FILE* stream_in; FILE* stream_out; if ( !(stream_in = fopen(infile, "rb"))) { printf("File in: %s cannot be read", infile); return -1; } if ( !(stream_out = fopen(outfile, "wb")) ) { printf("File out: %s cannot be read", outfile); return -1; } fpos_t flen; // fseek(stream_in, 0, SEEK_END); fgetpos(stream_in, &flen); unsigned long rlen = file_len(flen); // fseek(stream_in, 0, SEEK_SET); WORD ExpKey[Nb*(Nr+1)]; //WORD CifKey[Nk] = { 0x00010203, 0x04050607, // 0x08090a0b, 0x0c0d0e0f}; KeyExpansion(CifKey, ExpKey); while(rlen > 0 && !feof(stream_in)) { unsigned long len = (unsigned long)fread(in, 1, 4*Nb, stream_in); if (rlen < 4*Nb) for (int i = rlen; i < 4*Nb; i++) in[i] = 0; rlen -= len; //if (len != 4*Nb) #ifdef LOGit printf("\nNew block\n"); for(int i=0; i<4; i++) { printf("%02x %02x %02x %02x\n", in[i], in[4+i], in[8+i], in[12+i]); } #endif AddRoundKey((WORD*)in, &ExpKey[4*Nr]); InvCipher((WORD*)in, ExpKey); if (rlen == 1) { BYTE* out = new BYTE[1]; fread(out, sizeof(BYTE), 1, stream_in); len = out[0]; rlen = 0; } int nWritten = fwrite(in, sizeof(BYTE), len, stream_out); } fclose(stream_out); }
static quik_err_t load_config(void) { length_t len; char *buf; char *endp; char *p; path_t path; unsigned n = 0; quik_err_t err = ERR_NONE; char *attempts[] = { bi->config_file, "/boot/iquik.conf", "/boot/quik.conf", "/boot/iquik.conf", "/quik.conf", NULL }; err = env_dev_is_valid(&bi->default_dev); if (err != ERR_NONE) { if (err == ERR_ENV_CURRENT_BAD) { err = ERR_ENV_DEFAULT_BAD; } return err; } path.device = bi->default_dev.device; path.part = bi->default_dev.part; while (attempts[n] != NULL) { path.path = attempts[n]; printk("Trying configuration file @ '%P'\n", &path); err = file_len(&path, &len); if (err == ERR_NONE) { break; } n++; } /* Set by file_len. */ if (err != ERR_NONE) { return ERR_CONFIG_NOT_FOUND; } buf = malloc(len); if (buf == NULL) { return ERR_NO_MEM; } err = file_load(&path, buf); if (err != ERR_NONE) { printk("\nCouldn't load '%P': %r\n", &path, err); free(buf); return err; } if (cfg_parse(bi->config_file, buf, len) < 0) { printk ("Syntax error or read error in '%P'\n", &path); } free(buf); bi->flags |= CONFIG_VALID; p = cfg_get_strg(0, "init-code"); if (p) { prom_interpret(p); } p = cfg_get_strg(0, "init-message"); if (p) { printk("%s\n", p); } if(cfg_get_strg(0, "device") != NULL) { bi->default_dev.device = cfg_get_strg(0, "device"); } p = cfg_get_strg(0, "partition"); if (p) { n = strtol(p, &endp, 10); if (endp != p && *endp == 0) { env_dev_set_part(&bi->default_dev, n); } } p = cfg_get_strg(0, "pause-message"); if (p) { bi->pause_message = p; } p = cfg_get_strg(0, "message"); if (p) { file_cmd_cat(p); } return ERR_NONE; }
int decfile(FILE *fin, FILE *fout, aes_ctx *ctx, const char* ifn, const char* ofn) { char buf1[BLOCK_LEN], buf2[BLOCK_LEN], dbuf[2 * BLOCK_LEN]; char *b1, *b2, *bt; fpos_t flen; unsigned long i, len, rlen; // find the file length fseek(fin, 0, SEEK_END); fgetpos(fin, &flen); rlen = file_len(flen); // reset to start fseek(fin, 0, SEEK_SET); if(rlen <= 2 * BLOCK_LEN) { // if the original file length is less than or equal to 16 bytes // read the bytes of the file and verify length len = (unsigned long)fread(dbuf, 1, 2 * BLOCK_LEN, fin); rlen -= len; if(rlen > 0) return READ_ERROR; // set the original file length len -= BLOCK_LEN; // decrypt from position len to position len + BLOCK_LEN aes_dec_blk(dbuf + len, dbuf + BLOCK_LEN, ctx); // undo CBC chaining for(i = 0; i < len; ++i) dbuf[i] ^= dbuf[i + BLOCK_LEN]; // output decrypted bytes if(fwrite(dbuf, 1, len, fout) != len) return WRITE_ERROR; } else { // we need two input buffers because we have to keep the previous // ciphertext block - the pointers b1 and b2 are swapped once per // loop so that b2 points to new ciphertext block and b1 to the // last ciphertext block rlen -= BLOCK_LEN; b1 = buf1; b2 = buf2; // input the IV if(fread(b1, 1, BLOCK_LEN, fin) != BLOCK_LEN) return READ_ERROR; // read the encrypted file a block at a time while(rlen > 0 && !feof(fin)) { // input a block and reduce the remaining byte count len = (unsigned long)fread(b2, 1, BLOCK_LEN, fin); rlen -= len; // verify the length of the read operation if(len != BLOCK_LEN) return READ_ERROR; // decrypt input buffer aes_dec_blk(b2, dbuf, ctx); // if there is only one more block do ciphertext stealing if(rlen > 0 && rlen < BLOCK_LEN) { // read last ciphertext block if(fread(b2, 1, rlen, fin) != rlen) return READ_ERROR; // append high part of last decrypted block for(i = rlen; i < BLOCK_LEN; ++i) b2[i] = dbuf[i]; // decrypt last block of plaintext for(i = 0; i < rlen; ++i) dbuf[i + BLOCK_LEN] = dbuf[i] ^ b2[i]; // decrypt last but one block of plaintext aes_dec_blk(b2, dbuf, ctx); // adjust length of last output block len = rlen + BLOCK_LEN; rlen = 0; } // unchain CBC using the last ciphertext block for(i = 0; i < BLOCK_LEN; ++i) dbuf[i] ^= b1[i]; // write decrypted block if(fwrite(dbuf, 1, len, fout) != len) return WRITE_ERROR; // swap the buffer pointers bt = b1, b1 = b2, b2 = bt; } } return 0; }
int encfile(FILE *fin, FILE *fout, aes_ctx *ctx, const char* ifn, const char* ofn) { char buf[BLOCK_LEN], dbuf[2 * BLOCK_LEN]; fpos_t flen; unsigned long i, len, rlen; // set a random IV fillrand(dbuf, BLOCK_LEN); // find the file length fseek(fin, 0, SEEK_END); fgetpos(fin, &flen); rlen = file_len(flen); // reset to start fseek(fin, 0, SEEK_SET); if(rlen <= BLOCK_LEN) { // if the file length is less than or equal to 16 bytes // read the bytes of the file into the buffer and verify length len = (unsigned long) fread(dbuf + BLOCK_LEN, 1, BLOCK_LEN, fin); rlen -= len; if(rlen > 0) return READ_ERROR; // pad the file bytes with zeroes for(i = len; i < BLOCK_LEN; ++i) dbuf[i + BLOCK_LEN] = 0; // xor the file bytes with the IV bytes for(i = 0; i < BLOCK_LEN; ++i) dbuf[i + BLOCK_LEN] ^= dbuf[i]; // encrypt the top 16 bytes of the buffer aes_enc_blk(dbuf + BLOCK_LEN, dbuf + len, ctx); len += BLOCK_LEN; // write the IV and the encrypted file bytes if(fwrite(dbuf, 1, len, fout) != len) return WRITE_ERROR; } else { // if the file length is more 16 bytes // write the IV if(fwrite(dbuf, 1, BLOCK_LEN, fout) != BLOCK_LEN) return WRITE_ERROR; // read the file a block at a time while(rlen > 0 && !feof(fin)) { // read a block and reduce the remaining byte count len = (unsigned long)fread(buf, 1, BLOCK_LEN, fin); rlen -= len; // verify length of block if(len != BLOCK_LEN) return READ_ERROR; // do CBC chaining prior to encryption for(i = 0; i < BLOCK_LEN; ++i) buf[i] ^= dbuf[i]; // encrypt the block aes_enc_blk(buf, dbuf, ctx); // if there is only one more block do ciphertext stealing if(rlen > 0 && rlen < BLOCK_LEN) { // move the previous ciphertext to top half of double buffer // since rlen bytes of this are output last for(i = 0; i < BLOCK_LEN; ++i) dbuf[i + BLOCK_LEN] = dbuf[i]; // read last part of plaintext into bottom half of buffer if(fread(dbuf, 1, rlen, fin) != rlen) return READ_ERROR; // clear the remainder of the bottom half of buffer for(i = 0; i < BLOCK_LEN - rlen; ++i) dbuf[rlen + i] = 0; // do CBC chaining from previous ciphertext for(i = 0; i < BLOCK_LEN; ++i) dbuf[i] ^= dbuf[i + BLOCK_LEN]; // encrypt the final block aes_enc_blk(dbuf, dbuf, ctx); // set the length of the final write len = rlen + BLOCK_LEN; rlen = 0; } // write the encrypted block if(fwrite(dbuf, 1, len, fout) != len) return WRITE_ERROR; } } return 0; }
int main(int argc, char *argv[]) { FILE *pass1, *pass2; struct symbol *symb; int r,i,pci,sf; unsigned int start,end; fx = stdout;/*select terminal*/ cmdline(argc, argv); if(a_input) { f1=fopen(a_input, "rb"); if(f1==NULL) { msg(0, "Error: Cannot open %s: %s\n", a_input, strerror(errno)); exit(1); } } else { msg(0, "Error: No input file specified\n"); exit(1); } if(a_syminput) { r=symbol_load_file(a_syminput, 0); if(r) { exit(1); } } if(a_symoutput) { f2=fopen(a_symoutput, "w"); if(f2==NULL) { msg(0, "Error: Cannot create %s: %s\n", a_symoutput, strerror(errno)); exit(1); } } if(a_output==NULL) { f3=stdout; } else { f3=fopen(a_output, "w"); if(f3==NULL) { msg(0, "Error: Cannot create %s: %s\n", a_output, strerror(errno)); exit(1); } } start=a_org; end=a_org+file_len(a_input); if(end==start) { msg(0, "Error: Empty input file\n"); exit(1); } if(end>0x10000) { msg(0, "Error: Binary data outside 16-bit address space\n"); exit(1); } msg(1, "Disassembling binary data at 0x%04x - 0x%04x\n", start, end); block_init(start, end); if(a_blockfile) { r=block_load_file(a_blockfile); if(r) { exit(1); } } /* Temporary files to redirect pass1 and pass2 output. * * This is just a dirty hack to remove a lot of console output on * first two passes by diz80() */ pass1=tmpfile(); if(pass1==NULL) { msg(0, "Error: Cannot create temporary file: %s\n", strerror(errno)); exit(1); } pass2=tmpfile(); if(pass2==NULL) { msg(0, "Error: Cannot create temporary file: %s\n", strerror(errno)); exit(1); } /**************************pass 1*******************************/ /*In pass 1 addresses are calculated, arguments are stored in table*/ msg(1, "Starting pass 1\n"); pass=1; fx=pass1; pc=start; blk_reset(); do { for(i=0;i<T_SIZE;i++) shiftin(); while(1) { pci=disassemble(); if(pci==0) break; FP(fx,"\t\t;%04x",pc); FP(fx,"\n"); for(i=0;i<pci;i++) shiftin(); pc+=pci; } } while(blk_iterate()); /******************************pass 2******************************/ /* In pass 2 a check is made if all arguments stored in the table that are within code area range correspond to instruction addresses. If they do not correspond, they must be changed to the instruction address just before that + offset (could be self modifiing code) */ msg(1, "Starting pass 2\n"); pass=2; fx=pass2; pc=start; blk_reset(); do { for(i=0;i<T_SIZE;i++) shiftin(); while(1) { pci=disassemble(); if(pci==0) break; if(a_labels) { symbol_setlabel(pc,pci); } FP(fx,"\n"); for(i=0;i<pci;i++) shiftin(); pc+=pci; } } while(blk_iterate()); /* remove symbols from the symbol table that: * 1) were added automatically * 2) are not labels */ if(a_labels) { symbol_remove_nonlabels(); } if(f2) { msg(2, "Writing symbol file\n"); symbol_export(f2); fclose(f2); } /***************************pass 3*********************************/ /* In pass 3 the instruction length is again calculated, the instruction start addresses are calculated, these are looked up in the table to see if they correspond to any argument, if they correspond a label is printed before the instruction, made of lxxx where xxx is the hex address of the instruction. */ msg(1, "Starting pass 3\n"); msg(2, "Writing assembly file\n"); pass=3; fx=f3; pc=start; blk_reset(); FP(fx,"; %s\n", PACKAGE_STRING); FP(fx,"; command line:"); for(i=0;i<argc;i++) FP(fx," %s", argv[i]); FP(fx,"\n"); FP(fx,"\n\torg\t0%04xh\n",pc); /* Print required definitions for symbols that are not labels into the * assembly file so that it assembles even when symbol file is not * included */ symbol_export_nonlabels(fx); FP(fx,"\n"); /* Ugly but simplest way do get rid of double label prints at * block boundaries */ sf=1; do { for(i=0;i<T_SIZE;i++) shiftin(); while(1) { /* print a label, if one exists at this address */ if(a_labels && sf) { symb=NULL; while(1) { symb=symbol_find_next(pc, symb); if(symb==NULL) break; if(symb->comment!=NULL) { FP(fx, "%s", symb->comment); } FP(fx,"%s:\n",symb->name); } } sf=1; /* print assembly instruction */ pci=disassemble(); if(pci==0) break; /* current address */ if(a_address||a_source) { FP(fx,"\t\t;%04x",pc); } if(a_source) { /* binary in hex... */ FP(fx,"\t"); for(i=0;i<pci;i++) { FP(fx,"%02x ",t[i]); } /* ...and ASCII */ FP(fx,"\t"); for(i=0;i<pci;i++) { if((t[i] >= 32) && (t[i] < 128)) { FP(fx,"%c ",t[i]); } else { FP(fx,". "); } } }/*end if a_source*/ FP(fx,"\n"); for(i=0;i<pci;i++) shiftin(); pc+=pci; } sf=0; } while(blk_iterate()); fclose(f1); if(a_zilog) { FP(fx,"\n\tend\n"); } if(f_z80) { msg(0, "Warning: Code might not be 8080 compatible!\n"); } if(f_smc) { msg(0, "Warning: Self modifying code detected!\n"); } if(a_output) { fclose(f3); } exit(0); }/*end main*/
// 注意: 这个函数只能处理单文件模式torrent torrentmetadata_t* parsetorrentfile(char* filename) { int i; be_node* ben_res; FILE* f; int flen; char* data; torrentmetadata_t* ret; // 打开文件, 获取数据并解码字符串 f = fopen(filename,"r"); if(f==NULL) { printf("torrent not exist!\n"); exit(-1); } flen = file_len(f); data = (char*)malloc(flen*sizeof(char)); fread((void*)data,sizeof(char),flen,f); fclose(f); ben_res = be_decoden(data,flen); // 遍历节点, 检查文件结构并填充相应的字段. if(ben_res->type != BE_DICT) { perror("Torrent file isn't a dictionary"); exit(-13); } ret = (torrentmetadata_t*)malloc(sizeof(torrentmetadata_t)); if(ret == NULL) { perror("Could not allocate torrent meta data"); exit(-13); } // 计算这个torrent的info_hash值 // 注意: SHA1函数返回的哈希值存储在一个整数数组中, 对于小端字节序主机来说, // 在与tracker或其他peer返回的哈希值进行比较时, 需要对本地存储的哈希值 // 进行字节序转换. 当你生成发送给tracker的请求时, 也需要对字节序进行转换. char* info_loc, *info_end; info_loc = strstr(data,"infod"); // 查找info键, 它的值是一个字典 if(info_loc==NULL)printf("info null\n"); info_loc += 4; // 将指针指向值开始的地方 info_end = data+flen-6; int getnode=0; while(*info_end!='\0') { if(*info_end=='n'&&*(info_end+1)=='o'&&*(info_end+2)=='d'&&*(info_end+3)=='e') { getnode=1; break; } info_end--; } if(getnode) { printf("nodes\n"); while(*info_end!='e') --info_end; } else { info_end = data+flen-1; // 去掉结尾的e if(*info_end == 'e') { --info_end; } } printf("end:%c\n",*info_end); char* p; int len = 0; for(p=info_loc; p<=info_end; p++) len++; // 计算上面字符串的SHA1哈希值以获取info_hash SHA1Context sha; SHA1Reset(&sha); SHA1Input(&sha,(const unsigned char*)info_loc,len); if(!SHA1Result(&sha)) { printf("FAILURE\n"); } memcpy(ret->info_hash,sha.Message_Digest,20); printf("SHA1:\n"); for(i=0; i<5; i++) { printf("%08X ",ret->info_hash[i]); } printf("\n"); // 检查键并提取对应的信息 int filled=0; int flag=0; for(i=0; ben_res->val.d[i].val != NULL; i++) { int j; if(!strncmp(ben_res->val.d[i].key,"announce",strlen("announce"))&&flag<1) { ret->announce = (char*)malloc(strlen(ben_res->val.d[i].val->val.s)*sizeof(char)); memcpy(ret->announce,ben_res->val.d[i].val->val.s,strlen(ben_res->val.d[i].val->val.s)); filled++; flag++; } // info是一个字典, 它还有一些其他我们关心的键 if(!strncmp(ben_res->val.d[i].key,"info",strlen("info"))) { be_dict* idict; if(ben_res->val.d[i].val->type != BE_DICT) { perror("Expected dict, got something else"); exit(-3); } idict = ben_res->val.d[i].val->val.d; // 检查这个字典的键 for(j=0; idict[j].key != NULL; j++) { if(!strncmp(idict[j].key,"length",strlen("length"))) { ret->length = idict[j].val->val.i; filled++; } if(!strncmp(idict[j].key,"name",strlen("name"))) { ret->name = (char*)malloc(strlen(idict[j].val->val.s)*sizeof(char)); memcpy(ret->name,idict[j].val->val.s,strlen(idict[j].val->val.s)); filled++; } if(!strncmp(idict[j].key,"piece length",strlen("piece length"))) { ret->piece_len = idict[j].val->val.i; filled++; } if(!strncmp(idict[j].key,"pieces",strlen("pieces"))) { int num_pieces = ret->length/ret->piece_len; if(ret->length % ret->piece_len != 0) num_pieces++; ret->pieces = (char*)malloc(num_pieces*20); memcpy(ret->pieces,idict[j].val->val.s,num_pieces*20); ret->num_pieces = num_pieces; filled++; } } // for循环结束 } // info键处理结束 } // 确认已填充了必要的字段 be_free(ben_res); if(filled < 5) { printf("Did not fill necessary field\n"); return NULL; } else return ret; }