static void read_client_file_line(FILE *file, struct file *f) { if (fscanf(file, "%s\n", f->name) != 1 || !validate_file(f, false)) { cvs_error(CORRUPT_REPO_ERROR); } }
/****************************************************************************** * subroutine: serve_head * * purpose: return response header to client * * parameters: client_fd - client descriptor * * context - a pointer refers to HTTP context * * is_closed - an indicator if the current transaction is closed * * return: none * ******************************************************************************/ void serve_head(int client_fd, HTTPContext *context, int *is_closed) { struct tm tm; struct stat sbuf; time_t now; char buf[BUF_SIZE], filetype[MIN_LINE], tbuf[MIN_LINE], dbuf[MIN_LINE]; if (validate_file(client_fd, context, is_closed) < 0) return; stat(context->filename, &sbuf); get_filetype(context->filename, filetype); // get time string tm = *gmtime(&sbuf.st_mtime); strftime(tbuf, MIN_LINE, "%a, %d %b %Y %H:%M:%S %Z", &tm); now = time(0); tm = *gmtime(&now); strftime(dbuf, MIN_LINE, "%a, %d %b %Y %H:%M:%S %Z", &tm); // send response headers to client sprintf(buf, "HTTP/1.1 200 OK\r\n"); sprintf(buf, "%sDate: %s\r\n", buf, dbuf); sprintf(buf, "%sServer: Liso/1.0\r\n", buf); if (is_closed) sprintf(buf, "%sConnection: close\r\n", buf); sprintf(buf, "%sContent-Length: %ld\r\n", buf, sbuf.st_size); sprintf(buf, "%sContent-Type: %s\r\n", buf, filetype); sprintf(buf, "%sLast-Modified: %s\r\n\r\n", buf, tbuf); send(client_fd, buf, strlen(buf), 0); }
static void read_server_file_line(FILE *file, struct file *f) { if (fscanf(file, "%s %d %d\n", f->name, &f->version, &f->id) != 3 || !validate_file(f, true)) { cvs_error(CORRUPT_REPO_ERROR); } }
// XXX: need comment here static int do_init_reg(cr_location_t *loc, struct file *filp) { int result; result = validate_file(filp, loc->is_write); if (!result) { loc->filp = filp; init_MUTEX(&loc->mutex); } return result; }
// cr_loc_get(loc, shared) // // Returns the filp to write to. // Sets 'shared' to indicate if the file is shared with other threads. // If so, then the caller is responsible for any synchronization. // The loc->mutex field is provided for this purpose. // struct file *cr_loc_get(cr_location_t *loc, int *shared) { struct file *filp = NULL; if (loc->filp) { // Destination is a "regular" file. // Just return the filp. We don't need to fget() // because the fget() done in cr_loc_init is sufficient. filp = loc->filp; if (shared) { *shared = 1; } } else if (loc->fs) { // Destination is a per-process file in a given directory. struct fs_struct *saved_fs; char filename[CR_FILE_MAX]; int error; // Create a filename sprintf(filename, CR_FILE_PATT, current->pid); // Play with current->fs to open() in the destination dir saved_fs = current->fs; current->fs = loc->fs; filp = filp_open(filename, O_NOFOLLOW | (loc->is_write ? (O_WRONLY | O_CREAT | O_TRUNC) : O_RDONLY), CR_FILE_MODE); current->fs = saved_fs; error = validate_file(filp, loc->is_write); if (error) { filp = ERR_PTR(error); } if (shared) { *shared = 0; } } return filp; }
static bool validate_modification(struct modification *m, bool server) { if (!validate_file(&m->file, server)) return false; if (!m->action || !strchr("ADMN", m->action)) return false; if (m->action == MOVE) { m->new_name[MAX_PATH_LENGTH - 1] = 0; int length = strlen(m->new_name); if (length == 0 || length == MAX_PATH_LENGTH -1) return false; } return true; }
static void iterate_files(const char *path, const long *files, long start_at, long end_at, int progress) { uint64_t tot_ok, tot_corrupted, tot_changed, tot_overwritten, tot_size; struct timeval tot_dt = { .tv_sec = 0, .tv_usec = 0 }; double read_speed; const char *unit; int and_read_all = 1; int or_missing_file = 0; int number = start_at; tot_ok = tot_corrupted = tot_changed = tot_overwritten = tot_size = 0; printf(" SECTORS " " ok/corrupted/changed/overwritten\n"); while (*files >= 0) { uint64_t sec_ok, sec_corrupted, sec_changed, sec_overwritten, file_size; int read_all; or_missing_file = or_missing_file || (*files != number); for (; number < *files; number++) { char *full_fn; const char *filename; full_fn = full_fn_from_number(&filename, "", number); assert(full_fn); printf("Missing file %s\n", filename); free(full_fn); } number++; validate_file(path, *files, &sec_ok, &sec_corrupted, &sec_changed, &sec_overwritten, &file_size, &read_all, &tot_dt, progress); tot_ok += sec_ok; tot_corrupted += sec_corrupted; tot_changed += sec_changed; tot_overwritten += sec_overwritten; tot_size += file_size; and_read_all = and_read_all && read_all; files++; } assert(tot_size / SECTOR_SIZE == (tot_ok + tot_corrupted + tot_changed + tot_overwritten)); /* Notice that not reporting `missing' files after the last file * in @files is important since @end_at could be very large. */ report("\n Data OK:", tot_ok); report("Data LOST:", tot_corrupted + tot_changed + tot_overwritten); report("\t Corrupted:", tot_corrupted); report("\tSlightly changed:", tot_changed); report("\t Overwritten:", tot_overwritten); if (or_missing_file) printf("WARNING: Not all F3 files in the range %li to %li are available\n", start_at + 1, end_at + 1); if (!and_read_all) printf("WARNING: Not all data was read due to I/O error(s)\n"); /* Reading speed. */ read_speed = (double)tot_size / dt_to_s(&tot_dt); unit = adjust_unit(&read_speed); printf("Average reading speed: %.2f %s/s\n", read_speed, unit); } int main(int argc, char **argv) { const long *files; struct args args = { /* Defaults. */ .start_at = 0, .end_at = LONG_MAX - 1, /* If stdout isn't a terminal, supress progress. */ .show_progress = isatty(STDOUT_FILENO), }; /* Read parameters. */ argp_parse(&argp, argc, argv, 0, NULL, &args); print_header(stdout, "read"); files = ls_my_files(args.dev_path, args.start_at, args.end_at); iterate_files(args.dev_path, files, args.start_at, args.end_at, args.show_progress); free((void *)files); return 0; }
int main(int argc, char *argv[]) { int rc, option = 0, result = 0, priority = 0, flagk = 0, flage = 0; int flagd = 0, flaga = 0, flagh = 0, flagc = 0, flagz = 0, flagu = 0; int count =1; int flagr = 0, flagR = 0, flagl = 0, flagC = 0, job_id = 0; char *algo = NULL, *key = NULL, *infile = NULL, *outfile = NULL; void *dummy = NULL; struct job my_job; unsigned char *digest = NULL; MD5_CTX md5; MD5_Init(&md5); while ((option = getopt(argc, argv, "edk:a:hiczur:RlC")) != -1) { switch (option) { case 'e': flage++; my_job.job_type = ENCRYPT; break; case 'd': flagd++; my_job.job_type = DECRYPT; break; case 'k': flagk++; key = optarg; if (strlen(key) < 6) { printf ("Key length should be minimum 6 characters.\n"); rc = -EINVAL; goto out; } break; case 'a': flaga++; algo = optarg; break; case 'h': flagh++; my_job.job_type = CHECKSUM; break; case 'c': flagc++; my_job.job_type = CONCAT; break; case 'z': flagz++; my_job.job_type = COMPRESS; break; case 'u': flagu++; my_job.job_type = DECOMPRESS; break; case 'r': flagr++; job_id = atoi(optarg); my_job.job_type = REMOVE; break; case 'R': flagR++; printf("remove all jobs\n"); my_job.job_type = REMOVEALL; break; case 'l': flagl++; my_job.job_type = DISPLAY; break; case 'C': flagC++; my_job.job_type = CHANGEPR; break; default: printf("Invalid option.\n"); goto out; } } if (flage && (flagd || flagc || flagh || flagz || flagu || flagr || flagR || flagl)) { printf("error in parameters passed.\n"); rc = -EINVAL; goto out; } if (flage > 1) { printf("Error in parameters passed.\n"); rc = -EINVAL; goto out; } if (flagd && (flage || flagc || flagh || flagz || flagu || flagr || flagR || flagl)) { printf("error in parameters passed.\n"); rc = -EINVAL; goto out; } if (flagd > 1) { printf("Error in parameters passed.\n"); rc = -EINVAL; goto out; } if (!flagk && (flage || flagd)) { printf("Please provide key.\n"); rc = -EINVAL; goto out; } if (flaga && (flagc || flagz || flagu || flagr || flagR || flagl)) { printf("error in parameters passed.\n"); rc = -EINVAL; goto out; } if (!flaga) { if ((flage) || (flagd)) { algo = "AES"; } else { algo = "MD5"; } } if (flagc && (flagd || flage || flagh || flagz || flagu || flagr || flagR || flagl)) { printf("error in parameters passed.\n"); rc = -EINVAL; goto out; } if (flagc > 1) { printf("Error in parameters passed.\n"); rc = -EINVAL; goto out; } if (flagh && (flagd || flagc || flage || flagz || flagu || flagr || flagR || flagl)) { printf("error in parameters passed.\n"); rc = -EINVAL; goto out; } if (flagh > 1) { printf("Error in parameters passed.\n"); rc = -EINVAL; goto out; } if (flagz && (flagd || flagc || flagh || flage || flagu || flagr || flagR || flagl)) { printf("error in parameters passed.\n"); rc = -EINVAL; goto out; } if (flagz > 1) { printf("Error in parameters passed.\n"); rc = -EINVAL; goto out; } if (flagu && (flagd || flagc || flagh || flagz || flage || flagr || flagR || flagl)) { printf("error in parameters passed.\n"); rc = -EINVAL; goto out; } if (flagu > 1) { printf("Error in parameters passed.\n"); rc = -EINVAL; goto out; } if (flagr > 1) { printf("Error in parameters passed.\n"); rc = -EINVAL; goto out; } if (flagR > 1) { printf("Error in parameters passed.\n"); rc = -EINVAL; goto out; } if (flagl > 1) { printf("Error in parameters passed.\n"); rc = -EINVAL; goto out; } if (flagC > 1) { printf("Error in parameters passed.\n"); rc = -EINVAL; goto out; } if (flagr) goto out_hash; if (flagR) goto out_hash; if (flagl) { goto out_hash; } if (flagC) { if (!argv[optind]) { printf("Please provide job id.\n"); goto out; } my_job.job_id = atoi(argv[optind]); if (!argv[optind + 1]) { printf("Please provide job priority.\n"); goto out; } my_job.priority = atoi(argv[optind + 1]); goto out_hash; } if (!argv[optind]) { printf("Please provide priority of job.\n"); goto out; } my_job.job_id = 0; priority = atoi(argv[optind]); my_job.priority = priority; if (!argv[optind + 1]) { printf("Please provide input file.\n"); goto out; } infile = argv[optind + 1]; result = validate_file(infile); if (result == -1) goto out; my_job.infile = infile; if (argv[optind + 2]) { if (flagh) { printf("Output file not needed.\n"); goto out; } } else if (!argv[optind + 2] && !flagh) { printf("Please provide output file.\n"); goto out; } outfile = argv[optind + 2]; out_hash: my_job.pid = getpid(); if (my_job.job_type == ENCRYPT || my_job.job_type == DECRYPT) { my_job.outfile = outfile; my_job.algo = algo; digest = malloc(sizeof(unsigned char) * 16); if (!digest) goto out; MD5_Update(&md5, key, 16); MD5_Final(digest, &md5); my_job.key = digest; } if (my_job.job_type == CHECKSUM) { my_job.algo = algo; my_job.outfile = NULL; my_job.key = NULL; } if (my_job.job_type == CONCAT || my_job.job_type == COMPRESS || my_job.job_type == DECOMPRESS) { my_job.outfile = outfile; my_job.key = NULL; my_job.algo = NULL; } if (my_job.job_type == DISPLAY) { my_job.outfile = malloc(PAGE_SIZE * 2); my_job.infile = NULL; my_job.algo = NULL; my_job.key = NULL; goto sys_call_returned; } if (my_job.job_type == REMOVEALL) { my_job.infile = NULL; my_job.outfile = NULL; my_job.algo = NULL; my_job.key = NULL; goto sys_call_returned; } if (my_job.job_type == REMOVE) { my_job.job_id = job_id; my_job.infile = NULL; my_job.outfile = NULL; my_job.algo = NULL; my_job.key = NULL; goto sys_call_returned; } if (my_job.job_type == CHANGEPR) { my_job.infile = NULL; my_job.outfile = NULL; my_job.algo = NULL; my_job.key = NULL; goto sys_call_returned; } skt_fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_USER); if (flagh || flage || flagd || flagc || flagz || flagu) { if (pthread_create (&th1, NULL, (void *) &netlink_skt, (void *) skt_fd)) { printf("Error in thread creation\n"); } else printf("Thread Created\n"); } sys_call_returned: dummy = (void *) &my_job; rc = syscall(__NR_submitjob, dummy); if (rc == 0) { if (flagl) { printf("Job_id\t Job_Priority Job Type\tInput File\n"); printf("%s", my_job.outfile); goto out; } if (flagR) { printf("All jobs deleted.\n"); goto out; } if (flagr) { printf("Job deleted.\n"); goto out; } if (flagC) { printf("Job priority changed.\n"); goto out; } printf("syscall returned %d\n", rc); } else { if (flagl) { printf("Error while listing jobs.\n"); goto out_err2; } if (flagR) { printf("Error while deleting jobs.\n"); goto out_err2; } if (flagr) { printf("Job with job id %d doesn't exist.\n", my_job.job_id); goto out_err2; } out_err2: printf("syscall returned %d (errno=%d)\n", rc, errno); perror("Error"); goto out; } printf("count = %d\n",++count); if (pthread_join(th1, NULL)) { printf("Error joining thread\n"); } printf("count = %d\n",++count); out: if (my_job.outfile && flagl) free(my_job.outfile); if (digest) free(digest); exit(rc); }
int cvsrename(int argc, char **argv) { int c; int err = 0; char *repos_file1, *repos_file2; char *root1, *root2; const char *filename1, *filename2, *dir1, *dir2; int rootlen; List *ent,*ent2; Node *node; Entnode *entnode; const char *cwd; if (argc == -1) usage (rename_usage); quiet = 0; optind = 0; while ((c = getopt (argc, argv, "+q")) != -1) { switch (c) { case 'q': quiet = 1; break; case '?': default: usage (rename_usage); break; } } argc -= optind; argv += optind; if(argc!=2) { usage(rename_usage); }; error(0,0,"Warning: rename is still experimental and may not behave as you would expect"); if(current_parsed_root->isremote) { if(!supported_request("Rename")) error(1,0,"Remote server does not support rename"); if(!supported_request("Can-Rename")) error(1,0,"Renames are currently disabled"); } if(!strcmp(argv[0],argv[1])) return 0; rootlen = strlen(current_parsed_root->directory); if(!isfile(argv[0])) error(1,0,"%s does not exist",argv[0]); if(isfile(argv[1]) && fncmp(argv[0],argv[1])) /* We allow case renames (on Unix this is redundant) */ error(1,0,"%s already exists",argv[1]); if(isdir(argv[0])) error(1,0,"Directory renames are not currently supported"); validate_file(argv[0],&root1, &repos_file1, &filename1, &dir1, 1); validate_file(argv[1],&root2, &repos_file2, &filename2, &dir2, 0); if(strcmp(root1,root2) || strcmp(root1,current_parsed_root->original)) error(1,0,"%s and %s are in different repositories",argv[0],argv[1]); xfree(root1); xfree(root2); repos_file1 = (char*)xrealloc(repos_file1, strlen(filename1)+strlen(repos_file1)+rootlen+10); repos_file2 = (char*)xrealloc(repos_file2, strlen(filename2)+strlen(repos_file2)+rootlen+10); memmove(repos_file1+rootlen+1,repos_file1,strlen(repos_file1)+1); memmove(repos_file2+rootlen+1,repos_file2,strlen(repos_file2)+1); strcpy(repos_file1,current_parsed_root->directory); strcpy(repos_file2,current_parsed_root->directory); repos_file1[rootlen]='/'; repos_file2[rootlen]='/'; strcat(repos_file1,"/"); strcat(repos_file2,"/"); strcat(repos_file1,filename1); strcat(repos_file2,filename2); if(fncmp(argv[0],argv[1])) set_mapping(dir2,repos_file2+rootlen+1,""); /* Delete old file */ if(fncmp(dir1,dir2)) set_mapping(dir1,repos_file1+rootlen+1,""); set_mapping(dir2,repos_file1+rootlen+1,repos_file2+rootlen+1); /* Rename to new file */ cwd = xgetwd(); if(CVS_CHDIR(dir1)) error(1,errno,"Couldn't chdir to %s",dir1); ent = Entries_Open(0, NULL); node = findnode_fn(ent, filename1); entnode=(Entnode*)node->data; if(!node) { error(1,0,"%s is not listed in CVS/Entries",filename1); CVS_CHDIR(cwd); xfree(cwd); return 1; } if(!fncmp(dir1,dir2)) Rename_Entry(ent,filename1,filename2); else { if(CVS_CHDIR(cwd)) error(1,errno,"Couldn't chdir to %s",cwd); if(CVS_CHDIR(dir2)) error(1,errno,"Couldn't chdir to %s",dir2); ent2 = Entries_Open(0, NULL); if(entnode->type==ENT_FILE) Register(ent2,(char*)filename2,entnode->version,entnode->timestamp,entnode->options,entnode->tag,entnode->date,entnode->conflict,entnode->merge_from_tag_1,entnode->merge_from_tag_2,entnode->rcs_timestamp,entnode->edit_revision,entnode->edit_tag,entnode->edit_bugid,entnode->md5); else if(entnode->type==ENT_SUBDIR) Subdir_Register(ent2,NULL,filename2); else error(1,0,"Unknown entry type %d in entries file",node->type); Entries_Close(ent2); if(CVS_CHDIR(cwd)) error(1,errno,"Couldn't chdir to %s",cwd); if(CVS_CHDIR(dir1)) error(1,errno,"Couldn't chdir to %s",dir1); if(entnode->type==ENT_SUBDIR) Subdir_Deregister(ent,NULL,filename1); else if(entnode->type==ENT_FILE) Scratch_Entry(ent,filename1); else error(1,0,"Unknown entry type %d in entries file",node->type); } Entries_Close(ent); CVS_RENAME(argv[0],argv[1]); if(isdir(argv[1])) { char *tmp=(char*)xmalloc(strlen(argv[1])+strlen(CVSADM_VIRTREPOS)+10); FILE *fp; sprintf(tmp,"%s/%s",argv[1],CVSADM_VIRTREPOS); fp = fopen(tmp,"w"); if(!fp) error(0,errno,"Couldn't write %s",tmp); fprintf(fp,"%s\n",repos_file2+rootlen+1); fclose(fp); } xfree(repos_file1); xfree(repos_file2); xfree(dir1); xfree(dir2); CVS_CHDIR(cwd); xfree(cwd); return (err); }
uint8_t _main_flight(json_t *data) { json_t *_source, *_destination, *_set_start, *_set_end, *_pilot, *_task; const char *source; char *destination, *pilot; size_t set_start, set_end; _source = json_object_get(data, "source"); _destination = json_object_get(data, "destination"); _set_start = json_object_get(data, "set_start"); _set_end = json_object_get(data, "set_end"); _pilot = json_object_get(data, "pilot"); _task = json_object_get(data, "task"); if (!json_is_string(_source)) { fprintf(stderr, "{\"error\": \"No source file provided\"}"); return 0; } source = json_string_value(_source); if (!json_is_string(_destination)) { destination = calloc(strlen(source) + 1, sizeof(char)); strcpy(destination, source); dirname(destination); } else { destination = (char *) json_string_value(_destination); } if (!json_is_string(_pilot)) { pilot = (char *) json_string_value(_pilot); } char out_file_1[strlen(destination) + 20]; char out_file_2[strlen(destination) + 20]; char out_file_3[strlen(destination) + 20]; char out_file_4[strlen(destination) + 20]; sprintf(out_file_1, "%s%s", destination, "/track.js"); sprintf(out_file_2, "%s%s", destination, "/track.kml"); sprintf(out_file_3, "%s%s", destination, "/track_earth.kml"); sprintf(out_file_4, "%s%s", destination, "/track_split.kml"); char *igc_file = load_file(source); if (igc_file != NULL) { coordinate_set_t *set = NEW(coordinate_set_t, 1); coordinate_set_init(set); task_t *read_task = NULL; coordinate_set_parse_igc(set, igc_file, &read_task); size_t initial_length = set->length; coordinate_set_trim(set); coordinate_set_repair(set); coordinate_set_simplify(set, 1500); coordinate_set_extrema(set); json_t *_section = json_object_get(data, "section"); size_t section; if (_section) { section = json_integer_value(_section); coordinate_set_select_section(set, section, 0); } if (set->subset_count == 1) { task_t *task = parse_task(_task); if (!task) { task = read_task; } distance_map_t *map = NEW(distance_map_t, 1); distance_map_init(map, set); char *ids; printf("{"); printf("\"validated\": %d,", validate_file(source)); printf("\"total_points\": %d,", initial_length); printf("\"sets\": %d,", set->subset_count); printf("\"date\": \"%04d-%02d-%02d\",", set->year, set->month, set->day); printf("\"start_time\": %d,", set->first->timestamp); printf("\"duration\": %d,", set->last->timestamp - set->first->timestamp); printf("\"points\": %d,", set->length); printf("\"stats\": {", set->length); printf("\"height\" : {\"min\": %d, \"max\": %d},", set->first->ele, set->last->ele); printf("\"speed\" : {\"min\": %d, \"max\": %d},", set->first->speed, set->last->speed); printf("\"climb_rate\" : {\"min\": %d, \"max\": %d}", set->first->climb_rate, set->last->climb_rate); printf("},"); printf("\"task\": {", set->length); task_t *od, *or, *tr, *ft; od = distance_map_score_open_distance_3tp(map); or = distance_map_score_out_and_return(map); tr = distance_map_score_triangle(map, 0.28); ft = distance_map_score_triangle(map, 0); format_task(od, "open_distance", OPEN_DISTANCE); printf(","); format_task(or, "out_and_return", OUT_AND_RETURN); printf(","); format_task(tr, "triangle", TRIANGLE); printf(","); format_task(ft, "flat_triangle", FLAT_TRIANGLE); if (task) { printf(","); format_task(task, "declared", task->type); printf(", \"complete\": %d", task_completes_task(task, set)); } printf("}, \"output\": {\"js\": \"%s\",\"kml\": \"%s\",\"earth\": \"%s\"}", out_file_1, out_file_2, out_file_3); formatter_t *formatter; formatter = NEW(formatter_t, 1); formatter_js_init(formatter, set, 1, od, or, tr, ft); formatter_js_output(formatter, out_file_1); free(formatter); formatter = NEW(formatter_t, 1); formatter_kml_init(formatter, set, pilot ?: "N/A", od, or, tr, ft, task); formatter_kml_output(formatter, out_file_2); free(formatter); formatter = NEW(formatter_t, 1); formatter_kml_earth_init(formatter, set, pilot ?: "N/A", od, or, tr, ft, task); formatter_kml_earth_output(formatter, out_file_3); free(formatter); printf("}"); if (od) { task_deinit(od); } if (or) { task_deinit(or); } if (tr) { task_deinit(tr); } distance_map_deinit(map); } else {
int main (int argc, char*argv[]){ int opt; int vflag=0; int eflag=0; char output_encoding[20]=""; const char *input_path=NULL; const char *output_path=NULL; enum encoding output_encoding_enum=INVALIDENCODING; enum encoding input_encoding_enum=INVALIDENCODING; FILE *input_file_pointer=NULL; while((opt = getopt(argc, argv, "hve:")) != -1){ switch (opt){ case'h': print_usage(); exit(EXIT_SUCCESS); break; case'v': vflag++; break; case 'e': eflag=1; strncpy(output_encoding, optarg, 20); break; case '?': default: print_usage(); exit(EXIT_FAILURE); break; } } // output_encoding is something we alreay get here. if ((output_encoding_enum=e_flag_command(eflag, output_encoding))==INVALIDEFLAG){ print_usage(); exit(EXIT_FAILURE); } // check if there is input file and out put file; if( get_file_argument(argc, optind) ==0 ){ print_usage(); exit(EXIT_FAILURE); } // if there is get the input file and output file here input_path = argv[optind++]; output_path = argv[optind++]; //validate the file arguments checek if if ( validate_file(input_path, output_path)==0){ print_usage(); exit(EXIT_FAILURE); } // check what kind of input_encoding it is? if ( (input_encoding_enum =input_type_check(input_path,output_path))==INVALIDENCODING){ print_usage(); exit(EXIT_FAILURE); } // open and write the BOM to the output file FILE *output_file_pointer=NULL; if ( (output_file_pointer = fopen(output_path,"w+"))==NULL){ // file is created if dose not exist print_usage(); exit(EXIT_FAILURE); } else{ printf("the file %s was successfully created\n", output_path); } const char BOM_UTF8[3]={0xEF,0XBB,0XBF}; // UTF8 DO NOT NEED TO CONSIDER ABOUT LE OR BE write directly const char BOM_UTF16LE[2]={0XFF,0XFE}; // const char BOM_UTF16BE[2]={0XFE,0XFF}; // //size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream); if(output_encoding_enum==UTF8){ fwrite(BOM_UTF8,3,1,output_file_pointer); } else if(output_encoding_enum==UTF16LE){ fwrite(BOM_UTF16LE,2,1,output_file_pointer); } else if(output_encoding_enum==UTF16BE){ fwrite(BOM_UTF16BE,2,1,output_file_pointer); } else{ print_usage(); exit(EXIT_FAILURE); } input_file_pointer = fopen(input_path,"r"); if(input_encoding_enum==UTF8){ fseek(input_file_pointer,3,SEEK_CUR); } else if((input_encoding_enum==UTF16LE) || (input_encoding_enum==UTF16BE)){ fseek(input_file_pointer,2,SEEK_CUR); } uint8_t read_buffer[4]=""; auto size_t filebytes_read=0; if(vflag==1){ fprintf(stdout, "+--------------+-----------------+----------------+\n"); fprintf(stdout, "| ASCII | # of bytes | codepoint |\n"); fprintf(stdout, "+--------------+-----------------+----------------+\n"); } else if(vflag==2){ fprintf(stdout,"+--------------+-----------------+-----------------+---------------+\n"); fprintf(stdout,"| ASCII | # of bytes | codepoint | input |\n"); fprintf(stdout,"+--------------+-----------------+-----------------+---------------+\n"); } else if(vflag>=3){ fprintf(stdout,"+--------------+-----------------+-----------------+---------------+---------------+\n"); fprintf(stdout,"| ASCII | # of bytes | codepoint | input | output |\n"); fprintf(stdout,"+--------------+-----------------+-----------------+---------------+---------------+\n"); } while(filebytes_read = fread(read_buffer,1,1,input_file_pointer) == 1){ // if can sucessfully read the data read_buffer[1]=0; read_buffer[2]=0; read_buffer[3]=0; // clear uint8_t write_buffer[4]={0,0,0,0};// clear int moreByteToRead=0; int *moreToRead=&moreByteToRead; // get the pointer int getByteToWrite=0; int *ByteToWrite=&getByteToWrite; int getcodePoint =-1; int *codePoint = &getcodePoint; // char readin_value=-1; char read_in[10] = " ";// // char writeout_value=-1; char write_out[10]=" ";// if(input_encoding_enum==UTF8){ bytesRead_UTF8(read_buffer, moreToRead); //1 check how many bytes do I need to read to the buffer fread((read_buffer+1), (*moreToRead),1,input_file_pointer); //2 read the bytes to buffer; fread(buffer,n,1,pointer_holder); getCodePointFromUFT8(read_buffer,moreToRead, codePoint); //3 convert to code point if(moreByteToRead==0){ print_chars(read_buffer, 1, read_in); } else if(moreByteToRead==1){ print_chars(read_buffer, 2, read_in); } else if(moreByteToRead==2){ print_chars(read_buffer, 3, read_in); } else if(moreByteToRead==3){ print_chars(read_buffer, 4, read_in); } } else if(input_encoding_enum==UTF16LE){ fread((read_buffer+1), 1,1,input_file_pointer); // 1, read another byte checkIfSurrogate16LE (read_buffer,moreToRead); // 2. check if surrogate? if it is read TWO more byte, so also passing the moretoread pointer fread((read_buffer+2), (*moreToRead),1,input_file_pointer); // keep reading, if it is not surrogate then reading ZERO Byte getCodePointFromUFT16LE(read_buffer, moreToRead, codePoint); //convert to code point if(moreByteToRead==0){ print_chars(read_buffer, 2, read_in); } else if(moreByteToRead==2){ print_chars(read_buffer, 4, read_in); } } else if(input_encoding_enum==UTF16BE){ fread((read_buffer+1), 1,1,input_file_pointer); // 1, read another byte checkIfSurrogate16BE (read_buffer,moreToRead); // 2. check if surrogate? if it is read TWO more byte, so also passing the moretoread pointer fread((read_buffer+2), (*moreToRead),1,input_file_pointer); // keep reading, if it is not surrogate then reading ZERO Byte getCodePointFromUFT16BE(read_buffer, moreToRead, codePoint); //convert to code point if(moreByteToRead==0){ print_chars(read_buffer, 2, read_in); } else if(moreByteToRead==2){ print_chars(read_buffer, 4, read_in); } } codePointConvert(getcodePoint, output_encoding_enum,write_buffer,ByteToWrite,write_out); // convert code point to the UTF8/UTF16LE/UTF16BE type output fwrite(write_buffer,getByteToWrite,1,output_file_pointer); if(input_encoding_enum==UTF8){ vflag_print_utf8( vflag, getcodePoint, read_in, write_out, moreByteToRead,read_buffer); } else if(input_encoding_enum == UTF16LE || input_encoding_enum==UTF16BE) vflag_print_utf16le( vflag, getcodePoint, read_in, write_out, moreByteToRead); } // after everything is done close the inputfile and close the inputfile fclose(output_file_pointer); fclose(input_file_pointer); // point_holder is the inputfilepointer #ifdef CSE320 print_info(input_path,output_path, input_encoding_enum, output_encoding_enum); #else #endif }
/** * main: **/ int main (int argc, char *argv[]) { AsValidateToolPrivate *priv; gboolean ret; gboolean verbose = FALSE; gboolean version = FALSE; gboolean no_color = FALSE; GError *error = NULL; guint retval = 1; guint i; const GOptionEntry options[] = { { "verbose", 0, 0, G_OPTION_ARG_NONE, &verbose, _("Show extra debugging information"), NULL }, { "version", 0, 0, G_OPTION_ARG_NONE, &version, _("Show program version"), NULL }, { "no-color", 0, 0, G_OPTION_ARG_NONE, &no_color, _("Show program version"), NULL }, { NULL} }; setlocale (LC_ALL, ""); /* create helper object */ priv = g_new0 (AsValidateToolPrivate, 1); /* get a list of the commands */ priv->context = g_option_context_new (NULL); g_set_application_name (_("AppStream Validation Utility")); g_option_context_add_main_entries (priv->context, options, NULL); ret = g_option_context_parse (priv->context, &argc, &argv, &error); if (!ret) { g_print ("%s: %s\n", _("Failed to parse arguments"), error->message); g_error_free (error); goto out; } /* just a hack, we might need proper message handling later */ if (verbose) { g_setenv ("G_MESSAGES_DEBUG", "all", TRUE); } if (version) { g_print ("Appstream validation tool version: %s\n", VERSION); retval = 0; goto out; } if (argc <= 1) { g_print ("%s\n", _("You need to specify a file to validate!")); goto out; } ret = TRUE; for (i = 1; i < argc; i++) { gboolean tmp_ret; tmp_ret = validate_file (argv[i], !no_color); if (!tmp_ret) ret = FALSE; } if (!ret) { g_print ("%s\n", _("Validation failed.")); retval = 3; } /* success? */ if (ret) retval = 0; out: if (priv != NULL) { g_option_context_free (priv->context); g_free (priv); } return retval; }