static void file_rename_fods(file_recovery_t *file_recovery) { FILE *file; char buffer[4096]; char *tmp; size_t lu; if((file=fopen(file_recovery->filename, "rb"))==NULL) return; if((lu=fread(&buffer, 1, sizeof(buffer)-1, file)) <= 0) { fclose(file); return ; } buffer[lu]='\0'; tmp=strchr(buffer,'<'); while(tmp!=NULL) { if(strncasecmp(tmp, "<office:meta><dc:title>", 23)==0) { const char *title=tmp+23; tmp=strchr(title,'<'); if(tmp!=NULL) *tmp='\0'; file_rename(file_recovery, (const unsigned char*)title, strlen(title), 0, NULL, 1); fclose(file); return ; } tmp++; tmp=strchr(tmp,'<'); } fclose(file); }
static void file_rename_qbb(file_recovery_t *file_recovery) { FILE *file; unsigned int i=0; unsigned char buffer[4096]; size_t lu; if((file=fopen(file_recovery->filename, "rb"))==NULL) return; lu=fread(&buffer, 1, sizeof(buffer), file); fclose(file); while(i+sizeof(struct qbb_header02) < lu) { const struct qbb_header *hdr=(const struct qbb_header*)&buffer[i]; if(le16(hdr->magic)!=0x8645) return ; if(le16(hdr->type)==2) { const struct qbb_header02 *hdr2=(const struct qbb_header02 *)hdr; if(sizeof(struct qbb_header02)+le16(hdr2->title_len) <= sizeof(struct qbb_header)+le16(hdr2->data_len) && i+sizeof(struct qbb_header)+le16(hdr->data_len) < lu) file_rename(file_recovery, hdr2->title, le16(hdr2->title_len), 0, NULL, 1); return ; } i+=sizeof(struct qbb_header)+le16(hdr->data_len); } }
void convert (char *directory, char *filename) { # define BUFMAX 32000 static char fullname [1024], bufin [BUFMAX], bufout [BUFMAX], backup [1024], ch; int insize, outsize, bufptr; FILE *input, *output; xstrcpy (fullname, directory, "/", filename, NULL); if (!file_exists (fullname)) { printf ("`%s' does not exist", fullname); exit (1); } printf ("Converting %s...\n", fullname); /* Check if file contains suspicious binary characters */ input = fopen (fullname, "rb"); insize = fread (bufin, 1, 256, input); for (bufptr = 0; bufptr < insize; bufptr++) { ch = bufin [bufptr]; if (ch < ' ' && !isspace (ch) && ch != 0x1A) { printf ("[0x%X at %d] ", bufin [bufptr], bufptr); printf ("%s is a binary file - skipping\n", fullname); return; } } fclose (input); strcpy (backup, fullname); fixed_extension (backup, fullname, "bak"); file_delete (backup); file_rename (fullname, backup); input = fopen (backup, "rb"); output = fopen (fullname, "wb"); if (input == NULL || output == NULL) { printf ("Error: %s\n", strerror (errno)); exit (1); } while ((insize = fread (bufin, 1, BUFMAX, input)) > 0) { outsize = 0; for (bufptr = 0; bufptr < insize; bufptr++) if (bufin [bufptr] != '\r' && bufin [bufptr] != 0x1A) bufout [outsize++] = bufin [bufptr]; ASSERT (fwrite (bufout, 1, outsize, output)); } fclose (input); fclose (output); file_delete (backup); }
/*\ |*| Rename a file, and move the directory entry with it. \*/ static int rename_file(u16 *src, u16 *dst) { hfile_t *p; int n; fprintf(stderr, " Rename: %s", basename(src)); fprintf(stderr, " -> %s", basename(dst)); n = file_rename(src, dst); if (n) { perror(" "); return n; } fprintf(stderr, "\n"); /*\ Change directory entries \*/ for (p = hfile; p; p = p->next) { if (!unicode_cmp(p->filename, src)) { free(p->filename); p->filename = unicode_copy(dst); } } return 0; }
static void file_rename_gz(const char *old_filename) { unsigned char buffer[512]; FILE *file; int buffer_size; if((file=fopen(old_filename, "rb"))==NULL) return; buffer_size=fread(buffer, 1, sizeof(buffer), file); fclose(file); if(buffer_size<10) return; if(!(buffer[0]==0x1F && buffer[1]==0x8B && buffer[2]==0x08 && (buffer[3]&0xe0)==0)) return ; { const unsigned int flags=buffer[3]; int off=10; if((flags&GZ_FEXTRA)!=0) { off+=2; off+=buffer[10]|(buffer[11]<<8); } if((flags&GZ_FNAME)!=0) { file_rename(old_filename, buffer, buffer_size, off, NULL, 1); } } }
static void file_rename_d2s(const char *old_filename) { unsigned char buffer[512]; FILE *file; int buffer_size; if((file=fopen(old_filename, "rb"))==NULL) return; buffer_size=fread(buffer, 1, sizeof(buffer), file); fclose(file); file_rename(old_filename, buffer, buffer_size, 0x14, NULL, 1); }
static void file_rename_exs(file_recovery_t *file_recovery) { unsigned char buffer[512]; FILE *file; int buffer_size; if((file=fopen(file_recovery->filename, "rb"))==NULL) return; buffer_size=fread(buffer, 1, sizeof(buffer), file); fclose(file); file_rename(file_recovery, buffer, buffer_size, 0x14, "exs", 0); }
static BU_UINT8 media_frename_msg_proc(media_handle_t mhandle, prot_handle_t phandle, void* msg, BU_UINT32 msg_len) { char* sendBuf = NULL; BU_UINT32 ulOffSet = 0; BU_UINT32 src_path_len = 0; char* src_path = NULL; BU_UINT32 dst_path_len = 0; char* dst_path = NULL; pkt_mhead_t mhead; BU_UINT32 ulResult = BU_OK; BU_UINT32 ulFileType = 0; UNPKG_UINT32_MSG(msg, ulOffSet, ulFileType); UNPKG_UINT32_MSG(msg, ulOffSet, src_path_len); if((src_path = (char*)calloc(1, src_path_len)) == NULL){ E_LOG("calloc error\n"); return BU_ERROR; } UNPKG_BYTES_MSG(msg,ulOffSet, src_path_len, src_path); UNPKG_UINT32_MSG(msg, ulOffSet, dst_path_len); if((dst_path = (char*)calloc(1, dst_path_len)) == NULL){ E_LOG("calloc error\n"); free(src_path); return BU_ERROR; } UNPKG_BYTES_MSG(msg, ulOffSet, dst_path_len, dst_path); ulResult = file_rename(src_path, dst_path); /* ****************pkg prot PKG_PROT_MEDIA_FRENAME_ACK************** +4B: result code *********************************************************** */ ulOffSet = 0; mhead.media_handle = mhandle; mhead.prot_handle = phandle; mhead.type = PKG_PROT_MEDIA_FRENAME_ACK; PKG_INIT_BUFF(sendBuf, sizeof(pkt_mhead_t) + 4); PKG_BYTES_MSG(sendBuf, ulOffSet, &mhead, sizeof(pkt_mhead_t)); PKG_UINT32_MSG(sendBuf, ulOffSet, ulResult); msg_list_push(sendBuf, ulOffSet, PROT_TASK_ID, MEDIA_TASK_ID); PKG_FREE_BUFF(sendBuf); free(src_path); free(dst_path); return BU_OK; }
static int file_recover (FILEINFO *file_info) { int rc = 0; char *origin_name, *origin_fullname, *backup_name, *backup_fullname; size_t backup_name_len; backup_name = file_info-> dir. file_name; backup_name_len = strlen (backup_name); if (! streq (& backup_name [backup_name_len - strlen (BACKUP_SUFFIX)], BACKUP_SUFFIX)) return HEAP_OK; origin_name = mem_alloc (backup_name_len - strlen (BACKUP_SUFFIX) + 1); strncpy (origin_name, backup_name, backup_name_len - strlen (BACKUP_SUFFIX)); origin_name [backup_name_len - strlen (BACKUP_SUFFIX)] = '\0'; origin_fullname = NULL; backup_fullname = mem_strdup (heap_filename (backup_name)); if (! file_exists (backup_fullname)) rc = HEAP_MEMORY_ERROR; if (! rc) { origin_fullname = mem_strdup (heap_filename (origin_name)); if (! origin_fullname) rc = HEAP_MEMORY_ERROR; } if (! rc) if (file_exists (origin_fullname)) rc = file_delete (origin_fullname); if (! rc) rc = file_rename (backup_fullname, origin_fullname); if (rc) rc = HEAP_RECOVERY_ERROR; mem_free (origin_name); mem_free (origin_fullname); mem_free (backup_fullname); return rc; }
bool file_move (const std::string& old_filespec, const std::string& new_filespec) { // try to move the file by renaming - if that fails then do a copy and delete the original if (file_rename(old_filespec, new_filespec)) return true; if (!file_copy(old_filespec, new_filespec)) return false; // I'm not sure what to do if the delete fails - is that an error? // I've made it an error and then delete the copy so that the original state is recovered if (file_delete(old_filespec)) return true; file_delete(new_filespec); return false; }
static void file_rename_mov(const char *old_filename) { FILE *file; unsigned char buffer[512]; if((file=fopen(old_filename, "rb"))==NULL) return; if(fread(&buffer,sizeof(buffer),1,file)!=1) { fclose(file); return ; } fclose(file); buffer[8]='\0'; file_rename(old_filename, buffer, sizeof(buffer), 4, NULL, 1); }
static void file_rename_extdir(file_recovery_t *file_recovery) { unsigned char buffer[512]; char buffer_cluster[32]; FILE *file; int buffer_size; const uint32_t *inode=(const uint32_t *)&buffer[0]; if((file=fopen(file_recovery->filename, "rb"))==NULL) return; buffer_size=fread(buffer, 1, sizeof(buffer), file); fclose(file); if(buffer_size!=sizeof(buffer)) return; sprintf(buffer_cluster, "inode_%u", (unsigned int)le32(*inode)); file_rename(file_recovery, buffer_cluster, strlen(buffer_cluster), 0, NULL, 1); }
static void file_rename_mft(const char *old_filename) { unsigned char buffer[512]; char buffer_cluster[32]; FILE *file; int buffer_size; const struct ntfs_mft_record *record=(const struct ntfs_mft_record *)&buffer; if((file=fopen(old_filename, "rb"))==NULL) return; buffer_size=fread(buffer, 1, sizeof(buffer), file); fclose(file); if(buffer_size<54) return; sprintf(buffer_cluster, "record_%u", (unsigned int)le32(record->mft_record_number)); file_rename(old_filename, buffer_cluster, strlen(buffer_cluster), 0, NULL, 1); }
static void file_rename_fatdir(file_recovery_t *file_recovery) { unsigned char buffer[512]; char buffer_cluster[32]; FILE *file; int buffer_size; unsigned int cluster; if((file=fopen(file_recovery->filename, "rb"))==NULL) return; buffer_size=fread(buffer, 1, sizeof(buffer), file); fclose(file); if(buffer_size<32) return; cluster=fat_get_cluster_from_entry((const struct msdos_dir_entry *)&buffer[0]); sprintf(buffer_cluster, "cluster_%u", cluster); file_rename(file_recovery, buffer_cluster, strlen(buffer_cluster), 0, NULL, 1); }
static void file_rename_ext(file_recovery_t *file_recovery) { unsigned char buffer[512]; char buffer_cluster[32]; FILE *file; const struct ext2_super_block *sb=(const struct ext2_super_block *)&buffer; int buffer_size; unsigned long int block_nr; if((file=fopen(file_recovery->filename, "rb"))==NULL) return; buffer_size=fread(buffer, 1, sizeof(buffer), file); fclose(file); if(buffer_size!=sizeof(buffer)) return; block_nr=(le32(sb->s_first_data_block)+le16(sb->s_block_group_nr)*le32(sb->s_blocks_per_group)); sprintf(buffer_cluster, "sb_%lu", block_nr); file_rename(file_recovery, buffer_cluster, strlen(buffer_cluster), 0, NULL, 1); }
int heap_update ( const char *key, const DESCR *data) { int rc = HEAP_OK; char *filename = NULL, *backupname = NULL; ASSERT (key); ASSERT (data); rc = (filename = mem_strdup (heap_filename (key))) ? HEAP_OK : HEAP_MEMORY_ERROR; if (!rc) { if (file_exists (filename)) { backupname = mem_alloc (strlen (filename) + strlen (BACKUP_SUFFIX) + 1); if (!backupname) rc = HEAP_MEMORY_ERROR; else file_rename (filename, backupname); } else rc = HEAP_DATA_NOT_FOUND; } if (!rc) rc = descr2file (data, filename) ? HEAP_IO_FAILED : HEAP_OK; if ((!rc) && backupname) rc = file_delete (backupname); mem_free (filename); mem_free (backupname); return rc; }
int fs_rename(const char *src, const char *dst) { const char *write_dir; char *real_src, *real_dst; int rc = 0; if ((write_dir = fs_get_write_dir())) { real_src = concat_string(write_dir, "/", src, NULL); real_dst = concat_string(write_dir, "/", dst, NULL); rc = file_rename(real_src, real_dst); free(real_src); free(real_dst); } return rc; }
int heap_rename ( const char *src_name, const char *src_key, const char *dst_name, const char *dst_key ) { char *src_file = NULL, *dst_file = NULL; int err = HEAP_OK; ASSERT (src_name && src_key && dst_name && dst_key); if (err == HEAP_OK) { src_file = file_where ('s', src_name, src_key, NULL); err = src_file ? HEAP_OK : HEAP_MEMORY_ERROR; } if (err == HEAP_OK) err = file_exists (src_file) ? HEAP_OK : HEAP_DATA_NOT_FOUND; if (err == HEAP_OK) { dst_file = file_where ('s', dst_name, dst_key, NULL); err = dst_file ? HEAP_OK : HEAP_MEMORY_ERROR; } if (err == HEAP_OK) err = !file_exists (dst_file) ? HEAP_OK : HEAP_DATA_ALREADY_EXISTS; if (err == HEAP_OK) err = file_rename (src_file, dst_file) == 0 ? HEAP_OK : HEAP_IO_FAILED; mem_free (src_file); mem_free (dst_file); return err; }
static void file_rename_ts_192(const char *old_filename) { FILE *file; unsigned char buffer[192]; char buffer_pid[32]; unsigned int pid; if((file=fopen(old_filename, "rb"))==NULL) return; if( #ifdef HAVE_FSEEKO fseeko(file, 0, SEEK_SET) < 0 || #else fseek(file, 0, SEEK_SET) < 0 || #endif fread(&buffer, sizeof(buffer), 1, file) != 1) { fclose(file); return ; } fclose(file); pid=((buffer[5]<<8)|buffer[6])&0x1fff; sprintf(buffer_pid, "pid_%u", pid); file_rename(old_filename, (const unsigned char*)buffer_pid, strlen(buffer_pid), 0, NULL, 1); }
static void file_rename_pdf(const char *old_filename) { char title[512]; const unsigned char pattern[6]={ '/', 'T', 'i', 't', 'l', 'e' }; uint64_t offset; FILE *handle; unsigned char*buffer; unsigned int i; unsigned int j; int bsize; const unsigned char utf16[3]= { 0xfe, 0xff, 0x00}; if((handle=fopen(old_filename, "rb"))==NULL) return; if(fseek(handle, 0, SEEK_END)<0) { fclose(handle); return; } offset=ftell(handle); offset=file_rsearch(handle, offset, pattern, sizeof(pattern)); if(offset==0) { fclose(handle); return; } offset+=sizeof(pattern); if(fseek(handle, offset, SEEK_SET)<0) { fclose(handle); return ; } buffer=(unsigned char*)MALLOC(512); if((bsize=fread(buffer, 1, 512, handle)) <= 0) { free(buffer); fclose(handle); return ; } fclose(handle); /* Skip spaces after /Title */ for(i=0; i<bsize && buffer[i]==' '; i++); if(i==bsize) { /* Too much spaces */ free(buffer); return ; } if(buffer[i]=='<') { int s=i; /* hexa to ascii */ j=s; buffer[j++]='('; for(s++; s+1<bsize && buffer[s]!='>'; s+=2) buffer[j++]=(hex(buffer[s])<<4) | hex(buffer[s+1]); buffer[j]=')'; } j=0; if(buffer[i]=='(') { i++; /* Skip '(' */ if(i+8<bsize && memcmp(&buffer[i], "\\376\\377", 8)==0) { /* escape utf-16 title */ i+=8; while(i<bsize) { if(buffer[i]==')') break; if(i+4<bsize && buffer[i]=='\\' && isdigit(buffer[i+1]) && isdigit(buffer[i+2]) && isdigit(buffer[i+3])) i+=4; else title[j++]=buffer[i++]; } } else if(i+3<bsize && memcmp(&buffer[i], &utf16, 3)==0) { /* utf-16 title */ i+=2; while(i<bsize) { if(buffer[i]==')') break; title[j++]=buffer[i+1]; i+=2; } } else { /* ascii title */ while(i<bsize && buffer[i]!=')') title[j++]=buffer[i++]; } } else { free(buffer); return ; } /* Try to avoid some double-extensions */ if(j>4 && (memcmp(&title[j-4], ".doc", 4)==0 || memcmp(&title[j-4], ".xls", 4)==0)) j-=4; else if(j>5 && (memcmp(&title[j-5], ".docx", 5)==0 || memcmp(&title[j-5], ".xlsx", 5)==0)) j-=5; file_rename(old_filename, title, j, 0, NULL, 1); free(buffer); }
void qlevel_save(STRING *filename) { STRING *file = "#256"; make_path(file, filename); STRING *backup = "#256"; str_cpy(backup, file); str_cat(backup, ".bak"); file_delete(backup); file_rename(file, backup); ini_write_int(file, "Level Information", "EntityCount", 0); ini_write_int(file, "Level Information", "LightCount", 0); ini_write_int(file, "Level Information", "FogCount", 0); int countEntity = 0; int countLight = 0; int countFog = 0; STRING *section = "#64"; for(you = ent_next(NULL); you != NULL; you = ent_next(you)) { switch(you->group) { case GROUP_LEVEL: str_cpy(section, "Entity "); str_cat(section, str_for_int(NULL, countEntity)); ini_write_string(file, section, "Model", you->type); ini_write_int(file, section, "ActionCount", 0); int actionCount = 0; STRING *actionName = "#64"; ActionInformation *entry; for(entry = list_first(you->string1); entry != NULL; entry = list_next(you->string1, entry)) { str_cpy(actionName, "Action"); str_cat(actionName, str_for_int(NULL, actionCount)); ini_write_string(file, section, actionName, _str(entry->actionname)); actionCount++; } ini_write_int(file, section, "ActionCount", actionCount); ini_write_float(file, section, "X", you->x); ini_write_float(file, section, "Y", you->y); ini_write_float(file, section, "Z", you->z); ini_write_float(file, section, "Pan", cycle(you->pan, 0, 360)); ini_write_float(file, section, "Tilt", cycle(you->tilt, 0, 360)); ini_write_float(file, section, "Roll", cycle(you->roll, 0, 360)); ini_write_float(file, section, "ScaleX", you->scale_x); ini_write_float(file, section, "ScaleY", you->scale_y); ini_write_float(file, section, "ScaleZ", you->scale_z); ini_write_float(file, section, "Lightrange", you->lightrange); countEntity++; break; } } ini_write_int(file, "Level Information", "EntityCount", countEntity); ini_write_int(file, "Level Information", "LightCount", countLight); ini_write_int(file, "Level Information", "FogCount", countFog); }
static int diversion_remove(const char *const *argv) { const char *filename = argv[0]; struct fsys_namenode *namenode; struct fsys_diversion *contest, *altname; struct file file_from, file_to; struct pkgset *pkgset; opt_rename_setup(); if (!filename || argv[1]) badusage(_("--%s needs a single argument"), cipaction->olong); diversion_check_filename(filename); namenode = fsys_hash_find_node(filename, FHFF_NONE); if (namenode == NULL || namenode->divert == NULL || namenode->divert->useinstead == NULL) { if (opt_verbose > 0) printf(_("No diversion '%s', none removed.\n"), diversion_current(filename)); return 0; } if (opt_pkgname == NULL) pkgset = NULL; else pkgset = pkg_hash_find_set(opt_pkgname); contest = namenode->divert; altname = contest->useinstead->divert; if (opt_divertto != NULL && strcmp(opt_divertto, contest->useinstead->name) != 0) ohshit(_("mismatch on divert-to\n" " when removing '%s'\n" " found '%s'"), diversion_current(filename), diversion_describe(contest)); if (!opt_pkgname_match_any && pkgset != contest->pkgset) ohshit(_("mismatch on package\n" " when removing '%s'\n" " found '%s'"), diversion_current(filename), diversion_describe(contest)); /* Ignore removal request if the diverted file is still owned * by another package in the same set. */ if (diversion_is_shared(pkgset, namenode)) { if (opt_verbose > 0) printf(_("Ignoring request to remove shared diversion '%s'.\n"), diversion_describe(contest)); return 0; } if (opt_verbose > 0) printf(_("Removing '%s'\n"), diversion_describe(contest)); file_init(&file_from, altname->camefrom->name); file_init(&file_to, contest->useinstead->name); /* Remove entries from database. */ contest->useinstead->divert = NULL; altname->camefrom->divert = NULL; if (opt_rename) opt_rename = check_rename(&file_to, &file_from); if (opt_rename && !opt_test) file_rename(&file_to, &file_from); if (!opt_test) divertdb_write(); return 0; }
static int diversion_add(const char *const *argv) { const char *filename = argv[0]; struct file file_from, file_to; struct fsys_diversion *contest, *altname; struct fsys_namenode *fnn_from, *fnn_to; struct pkgset *pkgset; opt_pkgname_match_any = false; opt_rename_setup(); /* Handle filename. */ if (!filename || argv[1]) badusage(_("--%s needs a single argument"), cipaction->olong); diversion_check_filename(filename); file_init(&file_from, filename); file_stat(&file_from); if (file_from.stat_state == FILE_STAT_VALID && S_ISDIR(file_from.stat.st_mode)) badusage(_("cannot divert directories")); fnn_from = fsys_hash_find_node(filename, 0); /* Handle divertto. */ if (opt_divertto == NULL) opt_divertto = str_fmt("%s.distrib", filename); if (strcmp(filename, opt_divertto) == 0) badusage(_("cannot divert file '%s' to itself"), filename); file_init(&file_to, opt_divertto); fnn_to = fsys_hash_find_node(opt_divertto, 0); /* Handle package name. */ if (opt_pkgname == NULL) pkgset = NULL; else pkgset = pkg_hash_find_set(opt_pkgname); /* Check we are not stomping over an existing diversion. */ if (fnn_from->divert || fnn_to->divert) { if (fnn_to->divert && fnn_to->divert->camefrom && strcmp(fnn_to->divert->camefrom->name, filename) == 0 && fnn_from->divert && fnn_from->divert->useinstead && strcmp(fnn_from->divert->useinstead->name, opt_divertto) == 0 && fnn_from->divert->pkgset == pkgset) { if (opt_verbose > 0) printf(_("Leaving '%s'\n"), diversion_describe(fnn_from->divert)); return 0; } ohshit(_("'%s' clashes with '%s'"), diversion_current(filename), fnn_from->divert ? diversion_describe(fnn_from->divert) : diversion_describe(fnn_to->divert)); } /* Create new diversion. */ contest = nfmalloc(sizeof(*contest)); altname = nfmalloc(sizeof(*altname)); altname->camefrom = fnn_from; altname->camefrom->divert = contest; altname->useinstead = NULL; altname->pkgset = pkgset; contest->useinstead = fnn_to; contest->useinstead->divert = altname; contest->camefrom = NULL; contest->pkgset = pkgset; /* Update database and file system if needed. */ if (opt_verbose > 0) printf(_("Adding '%s'\n"), diversion_describe(contest)); if (opt_rename) opt_rename = check_rename(&file_from, &file_to); /* Check we are not renaming a file owned by the diverting pkgset. */ if (opt_rename && diversion_is_owned_by_self(pkgset, fnn_from)) { if (opt_verbose > 0) printf(_("Ignoring request to rename file '%s' " "owned by diverting package '%s'\n"), filename, pkgset->name); opt_rename = false; } if (opt_rename && diversion_is_essential(fnn_from)) warning(_("diverting file '%s' from an Essential package with " "rename is dangerous, use --no-rename"), filename); if (!opt_test) { divertdb_write(); if (opt_rename) file_rename(&file_from, &file_to); } return 0; }
static void file_rename_zip(const char *old_filename) { const char *ext=NULL; unsigned int file_nbr=0; file_recovery_t fr; reset_file_recovery(&fr); if((fr.handle=fopen(old_filename, "rb"))==NULL) return; fr.file_size = 0; fr.offset_error=0; first_filename[0]='\0'; if(fseek(fr.handle, 0, SEEK_SET) < 0) { fclose(fr.handle); return ; } while (1) { uint32_t header; int status; if (fread(&header, 4, 1, fr.handle)!=1) { #ifdef DEBUG_ZIP log_trace("Failed to read block header\n"); #endif fclose(fr.handle); return; } header = le32(header); #ifdef DEBUG_ZIP log_trace("Header 0x%08X at 0x%llx\n", header, (long long unsigned int)fr.file_size); log_flush(); #endif fr.file_size += 4; switch (header) { case ZIP_CENTRAL_DIR: /* Central dir */ status = zip_parse_central_dir(&fr); break; case ZIP_CENTRAL_DIR64: /* 64b end central dir */ status = zip64_parse_end_central_dir(&fr); break; case ZIP_END_CENTRAL_DIR: /* End central dir */ status = zip_parse_end_central_dir(&fr); break; case ZIP_END_CENTRAL_DIR64: /* 64b end central dir locator */ status = zip64_parse_end_central_dir_locator(&fr); break; case ZIP_DATA_DESCRIPTOR: /* Data descriptor */ status = zip_parse_data_desc(&fr); break; case ZIP_FILE_ENTRY: /* File Entry */ status = zip_parse_file_entry(&fr, &ext, file_nbr); file_nbr++; if(ext!=NULL) { fclose(fr.handle); file_rename(old_filename, NULL, 0, 0, ext, 1); return; } break; case ZIP_SIGNATURE: /* Signature */ status = zip_parse_signature(&fr); break; default: #ifdef DEBUG_ZIP if ((header&0xFFFF) != 0x4B50) log_trace("Not a zip block: 0x%08X\n", header); else log_trace("Unparsable block with ID 0x%04X\n", header>>16); #endif status = -1; break; } /* Verify status */ if (status<0) { fclose(fr.handle); return; } /* Only end of central dir is end of archive, 64b version of it is before */ if (header==ZIP_END_CENTRAL_DIR) { unsigned int len; fclose(fr.handle); for(len=0; len<32 && first_filename[len]!='\0' && first_filename[len]!='.' && first_filename[len]!='/' && first_filename[len]!='\\'; len++); file_rename(old_filename, first_filename, len, 0, "zip", 0); return; } } }
static void file_rename_doc(const char *old_filename) { const char *ext=NULL; char *title=NULL; FILE *file; unsigned char buffer_header[512]; uint32_t *fat; const struct OLE_HDR *header=(const struct OLE_HDR*)&buffer_header; time_t file_time=0; unsigned int fat_entries; if(strstr(old_filename, ".sdd")!=NULL) ext="sdd"; if((file=fopen(old_filename, "rb"))==NULL) return; #ifdef DEBUG_OLE log_info("file_rename_doc(%s)\n", old_filename); #endif /*reads first sector including OLE header */ if(my_fseek(file, 0, SEEK_SET) < 0 || fread(&buffer_header, sizeof(buffer_header), 1, file) != 1) { fclose(file); return ; } /* Sanity check */ if(le32(header->num_FAT_blocks)==0 || le32(header->num_extra_FAT_blocks)>50 || le32(header->num_FAT_blocks)>109+le32(header->num_extra_FAT_blocks)*((1<<le16(header->uSectorShift))-1)) { fclose(file); return ; } if((fat=OLE_load_FAT(file, header))==NULL) { fclose(file); return ; } fat_entries=(le32(header->num_FAT_blocks)==0 ? 109: (le32(header->num_FAT_blocks)<<le16(header->uSectorShift))/4); { unsigned int ministream_block=0; unsigned int ministream_size=0; unsigned int block; unsigned int i; /* FFFFFFFE = ENDOFCHAIN * Use a loop count i to avoid endless loop */ #ifdef DEBUG_OLE log_info("file_rename_doc root_start_block=%u, fat_entries=%u\n", le32(header->root_start_block), fat_entries); #endif for(block=le32(header->root_start_block), i=0; block<fat_entries && block!=0xFFFFFFFE && i<fat_entries; block=le32(fat[block]), i++) { struct OLE_DIR *dir_entries; if(my_fseek(file, (1+block)<<le16(header->uSectorShift), SEEK_SET)<0) { free(fat); fclose(file); free(title); return ; } dir_entries=(struct OLE_DIR *)MALLOC(1<<le16(header->uSectorShift)); if(fread(dir_entries, 1<<le16(header->uSectorShift), 1, file)!=1) { free(fat); free(dir_entries); fclose(file); free(title); return ; } #ifdef DEBUG_OLE log_info("Root Directory block=%u (0x%x)\n", block, block); #endif { unsigned int sid; const struct OLE_DIR *dir_entry=dir_entries; if(i==0) { ministream_block=le32(dir_entry->start_block); ministream_size=le32(dir_entry->size); } for(sid=0, dir_entry=dir_entries; sid<(1<<le16(header->uSectorShift))/sizeof(struct OLE_DIR); sid++,dir_entry++) { if(dir_entry->type!=NO_ENTRY) { const char SummaryInformation[40]= { 0x05, '\0', 'S', '\0', 'u', '\0', 'm', '\0', 'm', '\0', 'a', '\0', 'r', '\0', 'y', '\0', 'I', '\0', 'n', '\0', 'f', '\0', 'o', '\0', 'r', '\0', 'm', '\0', 'a', '\0', 't', '\0', 'i', '\0', 'o', '\0', 'n', '\0', '\0', '\0' }; #ifdef DEBUG_OLE unsigned int j; for(j=0;j<64 && j<le16(dir_entry->namsiz) && dir_entry->name[j]!='\0';j+=2) { log_info("%c",dir_entry->name[j]); } log_info(" type %u", dir_entry->type); log_info(" Flags=%s", (dir_entry->bflags==0?"Red":"Black")); log_info(" sector %u (%u bytes)\n", (unsigned int)le32(dir_entry->start_block), (unsigned int)le32(dir_entry->size)); #endif switch(le16(dir_entry->namsiz)) { case 12: /* 3ds max */ if(memcmp(dir_entry->name, "S\0c\0e\0n\0e\0\0\0",12)==0) ext="max"; /* Licom AlphaCAM */ else if(memcmp(dir_entry->name,"L\0i\0c\0o\0m\0\0\0",12)==0) ext="amb"; break; case 16: if(sid==1 && memcmp(dir_entry->name, "d\0o\0c\0.\0d\0e\0t\0\0\0", 16)==0) ext="psmodel"; /* Windows Sticky Notes */ else if(sid==1 && memcmp(dir_entry->name, "V\0e\0r\0s\0i\0o\0n\0\0\0", 16)==0) ext="snt"; break; case 18: /* MS Excel * Note: Microsoft Works Spreadsheet contains the same signature */ if(ext==NULL && memcmp(dir_entry->name, "W\0o\0r\0k\0b\0o\0o\0k\0\0\0",18)==0) ext="xls"; /* Microsoft Works .wps */ else if(memcmp(dir_entry->name,"C\0O\0N\0T\0E\0N\0T\0S\0\0\0",18)==0) ext="wps"; break; case 20: /* Page Maker */ if(memcmp(&dir_entry->name, "P\0a\0g\0e\0M\0a\0k\0e\0r\0\0\0", 20)==0) ext="p65"; break; case 22: /* SigmaPlot .jnb */ if(memcmp(dir_entry->name, "J\0N\0B\0V\0e\0r\0s\0i\0o\0n\0\0", 22)==0) ext="jnb"; break; case 24: /* HP Photosmart Photo Printing Album */ if(memcmp(dir_entry->name,"I\0m\0a\0g\0e\0s\0S\0t\0o\0r\0e\0\0\0",24)==0) ext="albm"; break; case 28: /* Microsoft Works Spreadsheet or Chart */ if(memcmp(dir_entry->name,"W\0k\0s\0S\0S\0W\0o\0r\0k\0B\0o\0o\0k\0\0\0",28)==0) ext="xlr"; /* Visio */ else if(memcmp(dir_entry->name,"V\0i\0s\0i\0o\0D\0o\0c\0u\0m\0e\0n\0t\0\0\0",28)==0) ext="vsd"; /* SolidWorks */ else if(memcmp(&dir_entry->name, "s\0w\0X\0m\0l\0C\0o\0n\0t\0e\0n\0t\0s\0\0\0", 28)==0) { #ifdef DJGPP ext="sld"; #else ext="sldprt"; #endif } break; case 32: if(memcmp(dir_entry->name, "m\0a\0n\0i\0f\0e\0s\0t\0.\0c\0a\0m\0x\0m\0l\0\0\0",32)==0) ext="camrec"; break; case 34: if(memcmp(dir_entry->name, "S\0t\0a\0r\0C\0a\0l\0c\0D\0o\0c\0u\0m\0e\0n\0t\0\0\0",34)==0) ext="sdc"; break; case 36: /* sda=StarDraw, sdd=StarImpress */ if((ext==NULL || strcmp(ext,"sdd")!=0) && memcmp(dir_entry->name, "S\0t\0a\0r\0D\0r\0a\0w\0D\0o\0c\0u\0m\0e\0n\0t\0003\0\0\0", 36)==0) ext="sda"; else if(memcmp(dir_entry->name, "f\0i\0l\0e\0_\0C\0O\0M\0P\0A\0N\0Y\0_\0F\0I\0L\0E\0\0\0", 36)==0) ext="qbb"; break; case 38: /* Quattro Pro spreadsheet */ if(memcmp(dir_entry->name, "N\0a\0t\0i\0v\0e\0C\0o\0n\0t\0e\0n\0t\0_\0M\0A\0I\0N\0\0\0", 38)==0) ext="qpw"; else if(memcmp(dir_entry->name, "S\0t\0a\0r\0W\0r\0i\0t\0e\0r\0D\0o\0c\0u\0m\0e\0n\0t\0\0\0", 38)==0) ext="sdw"; break; case 40: if(memcmp(dir_entry->name, SummaryInformation, 40)==0) { OLE_parse_summary(file, fat, fat_entries, header, ministream_block, ministream_size, le32(dir_entry->start_block), le32(dir_entry->size), &ext, &title, &file_time); } else if(memcmp(dir_entry->name,"P\0o\0w\0e\0r\0P\0o\0i\0n\0t\0 \0D\0o\0c\0u\0m\0e\0n\0t\0\0\0", 40)==0) ext="ppt"; /* Outlook */ else if(memcmp(dir_entry->name,"_\0_\0n\0a\0m\0e\0i\0d\0_\0v\0e\0r\0s\0i\0o\0n\0001\0.\0000\0\0\0",40)==0) ext="msg"; break; case 46: if(memcmp(dir_entry->name, "I\0S\0o\0l\0i\0d\0W\0o\0r\0k\0s\0I\0n\0f\0o\0r\0m\0a\0t\0i\0o\0n\0\0\0", 46)==0) { #ifdef DJGPP ext="sld"; #else ext="sldprt"; #endif } break; case 56: /* Wilcom ES Software */ if(memcmp(dir_entry->name, WilcomDesignInformationDDD, 56)==0) ext="emb"; break; } if(sid==1 && le16(dir_entry->namsiz) >=6 && memcmp(dir_entry->name, "D\0g\0n", 6)==0) ext="dgn"; #ifdef DEBUG_OLE if(ext!=NULL) log_info("Found %s %u\n", ext, le16(dir_entry->namsiz)); #endif } } } free(dir_entries); } } free(fat); fclose(file); if(file_time!=0 && file_time!=(time_t)-1) set_date(old_filename, file_time, file_time); if(title!=NULL) { file_rename(old_filename, (const unsigned char*)title, strlen(title), 0, ext, 1); free(title); } else file_rename(old_filename, NULL, 0, 0, ext, 1); }
asmlinkage long xcrypt(void *arg) { if (arg == NULL) { printk("User level argument is NULL\n"); return -EINVAL; } else { struct myargs *arguments; int result, paddedresult, padvalue, preambleWritten, temp, paddingWritten, paddingRead; struct file *filp=NULL, *filp1=NULL; struct dentry *dentry=NULL, *olddentry=NULL, *newdentry=NULL; long int inputFileLen=-1, outputFileLen=-1, keyLen=-1; char *srcbuffer, *destbuffer, *padding, *keyFromFile=NULL, *md5_hash=NULL, *tempOutFile; umode_t inputFileMode, outputFileMode; bool outFileCreated=false, renamed=false, success = false; int err=0; arguments = (struct myargs *)kmalloc(sizeof(struct myargs), GFP_KERNEL); if (arguments==NULL) { printk("Failed to allocate kernel memory\n"); err = -ENOMEM; goto out1; } result = copy_from_user((void *)arguments, arg, sizeof(struct myargs)); if(result!=0) { printk("Copying from user failed\n"); err = -EFAULT; goto out2; } if (((struct myargs*)arg)->inputFile == NULL) { /*Checking whether user passed NULL input File*/ printk("user level input file argument is NULL\n"); err = -EINVAL; goto out2; } inputFileLen = strnlen_user(((struct myargs*)arg)->inputFile, 32767); //TODO get the maximum value from getname if (inputFileLen == -1) { printk("Finding User inputFile string length Failed\n"); err = -EFAULT; goto out2; } arguments->inputFile = (char *)kmalloc(inputFileLen*sizeof(char), GFP_KERNEL); if ((arguments->inputFile)==NULL) { printk("Failed to allocate kernel memory for input file\n"); err = -ENOMEM; goto out2; } result = strncpy_from_user(arguments->inputFile, ((struct myargs*)arg)->inputFile, inputFileLen); if(result!=(inputFileLen-1)) { printk("Copying input file string from user failed\n"); err = -EFAULT; goto out3; } if((arguments->inputFile)==NULL) { printk("Copying input file string from user failed\n"); err = -EFAULT; goto out3; } if (((struct myargs*)arg)->outputFile == NULL) { printk("user level output file argument is NULL\n"); err = -EINVAL; goto out3; } outputFileLen = strnlen_user(((struct myargs*)arg)->outputFile, 32767); //TODO get the maximum value from getname if (outputFileLen == -1) { printk("Finding User outputFile string length Failed\n"); err = -EFAULT; goto out3; } arguments->outputFile = (char *)kmalloc(outputFileLen*sizeof(char), GFP_KERNEL); if ((arguments->outputFile)==NULL) { printk("Failed to allocate kernel memory for outputfile\n"); err = -ENOMEM; goto out3; } result = strncpy_from_user(arguments->outputFile, ((struct myargs*)arg)->outputFile, outputFileLen); if(result!=(outputFileLen-1)) { printk("Copying output file string from user failed\n"); err = -EFAULT; goto out4; } if((arguments->outputFile)==NULL) { printk("Copying output file string from user failed\n"); err = -EFAULT; goto out4; } if (((struct myargs*)arg)->keyBuf == NULL) { printk("user level key buffer argument is NULL\n"); err = -EINVAL; goto out4; } keyLen = strnlen_user(((struct myargs*)arg)->keyBuf, 32767); //TODO get the maximum value from getname if (keyLen == -1) { printk("Finding User keyBuf string length Failed\n"); err = -EFAULT; goto out4; } arguments->keyBuf = (char *)kmalloc(keyLen*sizeof(char), GFP_KERNEL); if ((arguments->keyBuf)==NULL) { printk("Failed to allocate kernel memory\n"); err = -ENOMEM; goto out4; } result = strncpy_from_user(arguments->keyBuf, ((struct myargs*)arg)->keyBuf, keyLen); if(result!=(keyLen-1)) { printk("Copying key buf string from user failed\n"); err = -EFAULT; goto out5; } if((arguments->keyBuf)==NULL) { printk("Copying key buf string from user failed\n"); err = -EFAULT; goto out5; } if (strlen(arguments->keyBuf) != arguments->keyLen) { printk("User key buffer length and kernel key buffer lengths differ\n"); err = -EINVAL; goto out5; } if((arguments->flags)!=0 && (arguments->flags)!=1) { printk("Invalid values for flag argument, it should be either 1 or 0\n"); err = -EINVAL; goto out5; } srcbuffer = (char *)kmalloc((PAGE_SIZE+1)*sizeof(char), GFP_KERNEL); if (srcbuffer==NULL) { printk("Failed to allocate kernel memory for srcbuffer\n"); err = -ENOMEM; goto out5; } destbuffer = (char *)kmalloc((PAGE_SIZE+1)*sizeof(char), GFP_KERNEL); if (destbuffer==NULL) { printk("Failed to allocate kernel memory for destbuffer\n"); err = -ENOMEM; goto out6; } padding = (char *)kmalloc(4*sizeof(char), GFP_KERNEL); if (padding==NULL) { printk("Failed to allocate kernel memory for padding\n"); err = -ENOMEM; goto out7; } initialisePadding(padding); /*Initialising the padding array to all 0's */ result=PAGE_SIZE; filp = filp_open(arguments->inputFile, O_RDONLY, 0); if (PTR_ERR(filp)==-ENOENT) { printk("Input File doesnot exists\n"); err = -ENOENT; goto out8; } else { printk("Input File exists\n"); } if (!filp || IS_ERR(filp)) { printk("Read error for input file %d\n", (int) PTR_ERR(filp)); err = PTR_ERR(filp); goto out8; } inputFileMode = filp->f_inode->i_mode; if(!S_ISREG(inputFileMode)) { printk("Input File is not a regular file\n"); err = -EISDIR; goto out9; } if (!filp->f_op->read) { printk("Error in reading input file\n"); err = PTR_ERR(filp); goto out9; } filp1 = filp_open(arguments->outputFile, O_WRONLY, 0); if (PTR_ERR(filp1)==-ENOENT) { printk("Output File doesnot exists, creating it\n"); filp1 = filp_open(arguments->outputFile, O_CREAT, inputFileMode); /*Creating output file if it doesnot exists with input file permissions*/ if(!filp1 || IS_ERR(filp1)) { printk("Error in creating output file\n"); err = PTR_ERR(filp1); goto out8_1; } else { printk("Output File created succesfully\n"); outFileCreated = true; } } else { printk("Output File exists\n"); } if(!outFileCreated) { if(!filp1 || IS_ERR(filp1)) { printk("Error in opening output file\n"); err = PTR_ERR(filp1); goto out8_1; } } if(!outFileCreated) { outputFileMode = filp1->f_inode->i_mode; if(!S_ISREG(outputFileMode)) { printk("Output File is not a regular file\n"); err = -EISDIR; goto out9; } } if(!outFileCreated) { if (filp->f_inode->i_ino == filp1->f_inode->i_ino) { printk("Both input and output files are same, they should be different\n"); err = -EPERM; goto out9; } } if(!outFileCreated) { olddentry = filp1->f_path.dentry; filp_close(filp1, NULL); tempOutFile = (char *)kmalloc((strlen(arguments->outputFile)+5)*sizeof(char), GFP_KERNEL); strncpy(tempOutFile, arguments->outputFile, strlen(arguments->outputFile)); strcat(tempOutFile, ".tmp"); tempOutFile[(strlen(arguments->outputFile)+5)]='\0'; filp1 = filp_open(tempOutFile, O_WRONLY, 0); if (PTR_ERR(filp1)==-ENOENT || IS_ERR(filp1)) { printk("temp Output File doesnot exists, creating it\n"); } else { printk("temp output File exists, truncating and creating new one\n"); dentry = filp1->f_path.dentry; filp_close(filp1, NULL); err = file_unlink(dentry->d_parent->d_inode, dentry); if(err != 0) { printk("unlink of already existing temporary file failed\n"); err = -EBUSY; goto out9; } printk("unlink function returned : %d\n", err); } filp1 = filp_open(tempOutFile, O_CREAT, outputFileMode); if(!filp1 || IS_ERR(filp1)) { printk("Error in creating temp output file\n"); err = PTR_ERR(filp1); goto out8_1; } else { printk("temp output File created succesfully\n"); } } newdentry = filp1->f_path.dentry; if (!filp1 || IS_ERR(filp1)) { printk("Write error for output file %d\n", (int) PTR_ERR(filp1)); err = PTR_ERR(filp1); goto out10; } if (!filp1->f_op->write) { printk("Error in writing to temp output file\n"); err = PTR_ERR(filp1); goto out10; } md5_hash = kmalloc(17*sizeof(char), GFP_KERNEL); if (md5_hash==NULL) { printk("Failed to allocate kernel memory for key from file\n"); err = -ENOMEM; goto out10; } err = generate_md5(arguments->keyBuf, md5_hash, strlen(arguments->keyBuf)); if(err != 0) { printk("kernel MD5 generation failed\n"); goto out11; } md5_hash[strlen(arguments->keyBuf)] = '\0'; if (arguments->flags) { //ENCRYPTION preambleWritten = wrapfs_write_file(filp1, (void *)(md5_hash), strlen(md5_hash)); /*Writing Key hash to the file*/ if(preambleWritten < 0) { printk("Writing preamble failed\n"); err = -EFAULT; goto out11; } paddingWritten = wrapfs_write_file(filp1, (void *)(padding), 4); /*Writing Inital Padding to the file*/ if (paddingWritten < 0) { printk("Writing padding failed\n"); err = -EFAULT; goto out11; } while(result==PAGE_SIZE) { result = wrapfs_read_file(filp, (void *)srcbuffer, PAGE_SIZE); if(result < 0) { printk("Reading from input file failed\n"); err = -EFAULT; goto out11; } err = encrypt_Cipher(arguments->keyBuf, srcbuffer, destbuffer, result, &paddedresult); if (err < 0) { printk("Error occured while encrypting\n"); goto out11; } if(paddedresult!=result) { padvalue = paddedresult - result; buildPadding(padding, padvalue); result = paddedresult; } result = wrapfs_write_file(filp1, (void *)destbuffer, result); if (result < 0) { printk("Writing to output file failed\n"); err = -EFAULT; goto out11; } } paddingWritten = wrapfs_write_file_pos(filp1, (void *)padding, 4, preambleWritten); if (paddingWritten < 0) { printk("Writing padding failed\n"); err = -EFAULT; goto out11; } } else { //DECRYPTION keyFromFile = kmalloc(16*sizeof(char), GFP_KERNEL); if (keyFromFile==NULL) { printk("Failed to allocate kernel memory for key from file\n"); err = -ENOMEM; goto out11; } temp = wrapfs_read_file(filp, (void *)keyFromFile, strlen(md5_hash)); if (temp != strlen(md5_hash)) { printk("reading key from file failed\n"); err = -EFAULT; goto out12; } if (compareKeys(md5_hash, keyFromFile, temp)) { printk("Both Keys Match\n"); } else { printk("Both keys doesnot match\n"); err = -EINVAL; //TODO : Return proper error Value. goto out12; } paddingRead = wrapfs_read_file(filp, (void *)padding, 4); if(paddingRead < 0) { printk("Reading padding failed\n"); err = -EFAULT; goto out12; } padvalue = reconstructPadding(padding); if (padvalue < 0) { printk("Reconstructing padding value failed(negative value not acceptable)\n"); err = -EFAULT; goto out12; } printk("Pad value returned : %d\n", padvalue); while(result==PAGE_SIZE) { result = wrapfs_read_file(filp, (void *)srcbuffer, PAGE_SIZE); if (result < 0) { printk("Reading from input file failed\n"); err = -EFAULT; goto out12; } printk("result read from file : %u\n", result); err = decrypt_Cipher(arguments->keyBuf, srcbuffer, destbuffer, result); if (err < 0) { printk("Error occured while encrypting\n"); goto out12; } if (result<PAGE_SIZE) { result = result - padvalue; } result = wrapfs_write_file(filp1, (void *)destbuffer, result); if(result < 0) { printk("writing to output file failed\n"); err = -EFAULT; //TODO goto goto out12; } } } if(!outFileCreated) { err = file_rename(newdentry->d_parent->d_inode, newdentry, olddentry->d_parent->d_inode, olddentry); if(err!=0) { printk("renaming of tempfile to output file failed\n"); err = -EBUSY; goto out12; } else renamed = true; } else success = true; out12: if(keyFromFile) kfree(keyFromFile); out11: kfree(md5_hash); out10: if(filp1) filp_close(filp1, NULL); if((!renamed && !outFileCreated)||(!success && outFileCreated)) file_unlink(newdentry->d_parent->d_inode, newdentry); out9: if(filp1) filp_close(filp1, NULL); out8_1: if(filp) filp_close(filp, NULL); out8: kfree(padding); out7: kfree(destbuffer); out6: kfree(srcbuffer); out5: kfree(arguments->keyBuf); out4: kfree(arguments->outputFile); out3: kfree(arguments->inputFile); out2: kfree(arguments); out1: return err; } }