📄 bcformat.c
字号:
/********************************************************************* Copyright (c) 1994-1999 Jetico, Inc., Finland* All rights reserved.** File: bcformat.c* Revision: $Id: bcformat.c,v 1.20 2005/05/14 07:58:33 crypt Rel-1.6-3 $* Created:* Description: implementation of format container routine********************************************************************/#include <sys/mount.h>#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <errno.h>#include <bc_types.h>#include <bc_ioctl.h>#include "keygen.h"#include <kg_defs.h>#include <fcntl.h>#include "header.h"#include <kblock.h>#include "alg.h"#include <string.h>#include "config.h"#include "bctool.h"#include "misc.h"#include "error.h"char bcformat_c[]="$Id: bcformat.c,v 1.20 2005/05/14 07:58:33 crypt Rel-1.6-3 $";int bcformat (int argc, char ** argv){ struct HiddenSector hs; char *device; int fd, fdd, flag; long long Offset; ALG_SERV Alg; DWORD KeyHandle,DataSize, _offset, new_offset; DATA_BLOCK db, *pdb; alg_info *alg_inf; char *command[16]; if ( argc < 2 ) { msg(stderr,"Please specify container name\n"); return -1; } else if ( argc > 2 ) { msg(stderr,"Too many parameters\n"); return -1; } if ( o_fstype==NULL ) { msg(stdout,"Filesystem type not specified. Msdos used.\n"); o_fstype="msdos"; } if ( (device=FindFreeDevice())==NULL ) { msg(stderr,"Can not find free device\n"); return -1; } if ( (Alg.bc_fd = open(device, O_RDONLY)) < 0 ) { msg(stderr,"open(\"%s\",O_RDONLY): %s\n",device,strerror(errno)); return -1; } if ( (fd = open(argv[1], O_RDWR|O_LARGEFILE)) < 0 ) { msg(stderr,"open(\"%s\",O_RDWR|O_LARGEFILE): %s\n",argv[1],strerror(errno)); return -1; } if ( read(fd, &hs, sizeof(hs))!=sizeof(hs) ) { msg(stderr,"Can not read container header\n"); return -1; } if ( strncmp(hs.br.OEMid, "LOCOS94 ", sizeof(hs.br.OEMid))!=0 ) { msg(stderr,"%s is not valid BestCrypt container\n",argv[1]); return -1; } if ('B' == hs.br.jmpCode[0]) { msg(stderr, "WARNING! Container \"%s\" is probably in use!\n\n" "Two reasons for the report are possible:\n" " 1. The container is already mounted. In this case do not use the container\n" " until someone dismounts it.\n" " 2. The container was not unmounted properly last time, so a lock mask \n" " for the container was not reset. If so, unlock the container by running the command:\n" " \"bctool unlock %s\" \n\n", argv[1],argv[1]); return -1; } if ( read(fd, &db, sizeof (db))!=sizeof(db) ) { msg(stderr,"Can not read data block\n"); return -1; } if ( (alg_inf=GetInfoById(hs.algorithmId))==NULL ) { msg(stderr,"Unknown algorithm Id: %d\n",hs.algorithmId); return -1; } Alg.alg_id=hs.algorithmId; Alg.alg_module =alg_inf->mod_name; pdb = &db; DataSize = sizeof(db);// Offset=hs.dwDataOffset; if (KGSHA256_ID == hs.keyGenId) { UseSha256(); } CreateKeyHandleEx(Alg,alg_inf->key_size,NULL,"Enter password:",CFLAG_VERIFY_AND_LOAD_KEY,(BYTE **)&pdb,&DataSize,&KeyHandle,&Error,&_offset); if ( Error!=0 ) { print_error(Error); return -1; } if (_offset) Offset = (long long)512 * _offset; else Offset = hs.dwDataOffset; Error=bc_set_fdkh64(Alg, KeyHandle, fd, Offset, 0, 0, hs.version == 0 ? 0 : BC_FLAGS_IV_64BIT); if ( Error!=0 ) { print_error(Error); FreeKeyHandle(Alg,KeyHandle); return -1; }//----------------------------------------------------------------------// BestCrypt for Windows 7.11+ compatibility code//---------------------------------------------------------------------- fdd = open(device, O_RDONLY); // workaround for 2.4.x flag = 0; new_offset = check_msdos_partition(fdd < 0 ? Alg.bc_fd : fdd, &flag); if (fdd >= 0) close(fdd); if (0 != (flag & 0xFF000000)) { // partition table detected msg(stderr, "Warning! %d (%d extended) partition(s) detected inside container.\n", flag >> 24, (flag >> 16) & 0xFF); if (0x0100 != (flag >> 16)) { // non-trivial case if (!ask_y_n("Do you want to continue? (y/[n]):")) { FreeKeyHandle(Alg, KeyHandle); close(Alg.bc_fd); close(fd); return 0; } } bc_clear_fd(Alg); Error=bc_set_fdkh64(Alg, KeyHandle, fd, Offset, (long long)new_offset, 0, (o_ro ? BC_FLAGS_READONLY : 0) | (hs.version == 0 ? 0 : BC_FLAGS_IV_64BIT)); if ( Error!=0 ) { msg(stderr,"bc_set_fd_kh: %s\n",strerror(-Error)); FreeKeyHandle(Alg,KeyHandle); close(Alg.bc_fd); close(fd); return -1; } }//---------------------------------------------------------------------- //---------------------------------------------------------------------- if (0 == strcasecmp(o_fstype,"vfat")) { command[0]="/sbin/mkfs"; command[1]="-t"; command[2]="msdos"; command[3]="-F"; command[4]="32"; command[5]=device; command[6]=0; }else if (0 == strcasecmp(o_fstype,"ext2") || 0 == strcasecmp(o_fstype,"ext3") ) { command[0]="/sbin/mkfs"; command[1]="-t"; command[2]=o_fstype; command[3]="-m"; command[4]="0"; command[5]=device; command[6]=0; }else { command[0]="/sbin/mkfs"; command[1]="-t"; command[2]=o_fstype; command[3]=device; command[4]=0; } if (-1 == run_process(command)) { msg(stderr,"Can not launch %s\n",command[0]); Error=bc_clear_fd(Alg); FreeKeyHandle(Alg,KeyHandle); return -1; } FreeKeyHandle(Alg,KeyHandle); if (0 == _offset) { hs.fileSystemId=getIdByFStype(o_fstype); } Error=bc_clear_fd(Alg); if ( Error!=0 ) { print_error(Error); return -1; } if ( lseek(fd,0,SEEK_SET) < 0 ) { msg(stderr,"lseek(\"%s\",0,SEEK_SET): %s\n",argv[1],strerror(errno)); return -1; } if ( write(fd,&hs,sizeof(hs)) < 0 ) { msg(stderr,"write(\"%s\"...): %s\n",argv[1],strerror(errno)); return -1; } close(fd); close(Alg.bc_fd); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -