static hole_t * hole_new(const char *fname, int flags, zip_error_t *error) { hole_t *ctx = (hole_t *)malloc(sizeof(*ctx)); if (ctx == NULL) { zip_error_set(error, ZIP_ER_MEMORY, 0); return NULL; } if ((ctx->fname = strdup(fname)) == NULL) { free(ctx); zip_error_set(error, ZIP_ER_MEMORY, 0); return NULL; } if ((ctx->in = buffer_from_file(fname, flags, error)) == NULL) { free(ctx); return NULL; } zip_error_init(&ctx->error); ctx->out = NULL; return ctx; }
static void display_cache_file(struct display *dp, const char *filename) /* Does the initial cache of the file. */ { FILE *fp; int ret; dp->filename = filename; if (filename != NULL) { fp = fopen(filename, "rb"); if (fp == NULL) display_log(dp, USER_ERROR, "open failed: %s", strerror(errno)); } else fp = stdin; ret = buffer_from_file(&dp->original_file, fp); fclose(fp); if (ret != 0) display_log(dp, APP_ERROR, "read failed: %s", strerror(ret)); }
int GSI_SOCKET_get_creds(GSI_SOCKET *self, const char *source_credentials) { int return_value = GSI_SOCKET_ERROR; unsigned char *output_buffer = NULL; int output_buffer_length; if (self == NULL) { goto error; } if (self->gss_context == GSS_C_NO_CONTEXT) { GSI_SOCKET_set_error_string(self, "GSI_SOCKET not authenticated"); goto error; } if (buffer_from_file(source_credentials, &output_buffer, &output_buffer_length) < 0) { GSI_SOCKET_set_error_from_verror(self); goto error; } /* * Write the proxy certificate back to user */ myproxy_debug( "Sending credential" ); if (GSI_SOCKET_write_buffer(self, (const char *)output_buffer, output_buffer_length) == GSI_SOCKET_ERROR) { goto error; } /* Success */ return_value = GSI_SOCKET_SUCCESS; error: if (output_buffer != NULL) { free(output_buffer); } return return_value; }
/* * Main entry point. */ int main(int argc, char **argv) { PDS_callbacks callbacks; PDS_payload payload; char *buffer; size_t size; int ret; if(4 != argc) { usage(); return EXIT_FAILURE; } g_image_string = argv[1]; if(!buffer_from_file(argv[2], &buffer, &size)) { return EXIT_FAILURE; } memset(&payload, 0, sizeof(PDS_payload)); PDS_set_error_callback(&callbacks, print_error); PDS_set_scalar_callback(&callbacks, parse_scalar); PDS_set_attribute_callbacks(&callbacks, parse_attribute_begin, parse_attribute_end); PDS_set_pointer_callbacks(&callbacks, parse_pointer_begin, parse_pointer_end); PDS_set_set_callbacks(&callbacks, dummy_begin_end, dummy_begin_end); PDS_set_sequence_callbacks(&callbacks, dummy_begin_end, dummy_begin_end); PDS_set_group_callbacks(&callbacks, group_begin, group_end); PDS_set_object_callbacks(&callbacks, object_begin, object_end); ret = PDS_parse(&callbacks, buffer, (int)size, &payload); if(ret) { ret = write_image(argv[3], buffer, size, (payload.image_record-1) * payload.record_size, &payload.image); } free(buffer); return ret ? EXIT_SUCCESS : EXIT_FAILURE; }
int cbfs_image_from_file(struct cbfs_image *image, const char *filename) { void *header_loc; if (buffer_from_file(&image->buffer, filename) != 0) return -1; DEBUG("read_cbfs_image: %s (%zd bytes)\n", image->buffer.name, image->buffer.size); header_loc = cbfs_find_header(image->buffer.data, image->buffer.size); if (!header_loc) { ERROR("%s does not have CBFS master header.\n", filename); cbfs_image_delete(image); return -1; } if ((image->header = malloc(sizeof(*image->header))) == NULL) return -1; cbfs_get_header(image->header, header_loc); cbfs_fix_legacy_size(image, header_loc); return 0; }
static partitioned_file_t *reopen_flat_file(const char *filename) { assert(filename); struct partitioned_file *file = calloc(1, sizeof(*file)); if (!file) { ERROR("Failed to allocate partitioned file structure\n"); return NULL; } if (buffer_from_file(&file->buffer, filename)) { free(file); return NULL; } file->stream = fopen(filename, "rb+"); if (!file->stream) { perror(filename); partitioned_file_close(file); return NULL; } return file; }
int makeproxy(const char certfile[], const char keyfile[], const char proxyfile[]) { static char BEGINCERT[] = "-----BEGIN CERTIFICATE-----"; static char ENDCERT[] = "-----END CERTIFICATE-----"; static char BEGINKEY1[] = "-----BEGIN RSA PRIVATE KEY-----"; static char BEGINKEY2[] = "-----BEGIN PRIVATE KEY-----"; static char BEGINKEY3[] = "-----BEGIN ENCRYPTED PRIVATE KEY-----"; static char ENDKEY1[] = "-----END RSA PRIVATE KEY-----"; static char ENDKEY2[] = "-----END PRIVATE KEY-----"; static char ENDKEY3[] = "-----END ENCRYPTED PRIVATE KEY-----"; unsigned char *certbuf=NULL, *keybuf=NULL; char *certstart, *certend, *keystart, *keyend; int return_value = -1, size, rval, fd=0; uid_t owner; /* Read the certificate(s) into a buffer. */ if (buffer_from_file(certfile, &certbuf, NULL) < 0) { fprintf(stderr, "Failed to read %s\n", certfile); goto cleanup; } /* Read the key into a buffer. */ if (buffer_from_file(keyfile, &keybuf, NULL) < 0) { fprintf(stderr, "Failed to read %s\n", keyfile); goto cleanup; } /* special case: run as root w/ non-root storage dir */ if (getuid() == 0 && get_storage_dir_owner(&owner) == 0 && owner != 0) { seteuid(0); setuid(owner); } /* Open the output file. */ if ((fd = open(proxyfile, O_CREAT | O_EXCL | O_WRONLY, S_IRUSR | S_IWUSR)) < 0) { fprintf(stderr, "open(%s) failed: %s\n", proxyfile, strerror(errno)); goto cleanup; } /* Write the first certificate. */ if ((certstart = strstr((const char *)certbuf, BEGINCERT)) == NULL) { fprintf(stderr, "%s doesn't contain '%s'.\n", certfile, BEGINCERT); goto cleanup; } if ((certend = strstr((const char *)certstart, ENDCERT)) == NULL) { fprintf(stderr, "%s doesn't contain '%s'.\n", certfile, ENDCERT); goto cleanup; } certend += strlen(ENDCERT); size = certend-certstart; while (size) { if ((rval = write(fd, certstart, size)) < 0) { perror("write"); goto cleanup; } size -= rval; certstart += rval; } if (write(fd, "\n", 1) < 0) { perror("write"); goto cleanup; } /* Write the key. */ if ((keystart = strstr((const char *)keybuf, BEGINKEY1)) == NULL && (keystart = strstr((const char *)keybuf, BEGINKEY2)) == NULL && (keystart = strstr((const char *)keybuf, BEGINKEY3)) == NULL) { fprintf(stderr, "%s doesn't contain '%s' nor '%s' nor '%s'.\n", keyfile, BEGINKEY1, BEGINKEY2, BEGINKEY3); goto cleanup; } if ((keyend = strstr((const char *)keystart, ENDKEY1)) != NULL) keyend += strlen(ENDKEY1); else if ((keyend = strstr((const char *)keystart, ENDKEY2)) != NULL) keyend += strlen(ENDKEY2); else if ((keyend = strstr((const char *)keystart, ENDKEY3)) != NULL) keyend += strlen(ENDKEY3); else { fprintf(stderr, "%s doesn't contain '%s' nor '%s' nor '%s'.\n", keyfile, ENDKEY1, ENDKEY2, ENDKEY3); goto cleanup; } size = keyend-keystart; while (size) { if ((rval = write(fd, keystart, size)) < 0) { perror("write"); goto cleanup; } size -= rval; keystart += rval; } if (write(fd, "\n", 1) < 0) { perror("write"); goto cleanup; } /* Write any remaining certificates. */ while ((certstart = strstr((const char *)certstart, BEGINCERT)) != NULL) { if ((certend = strstr((const char *)certstart, ENDCERT)) == NULL) { fprintf(stderr, "Can't find matching '%s' in %s.\n", ENDCERT, certfile); goto cleanup; } certend += strlen(ENDCERT); size = certend-certstart; while (size) { if ((rval = write(fd, certstart, size)) < 0) { perror("write"); goto cleanup; } size -= rval; certstart += rval; } if (write(fd, "\n", 1) < 0) { perror("write"); goto cleanup; } } return_value = 0; cleanup: if (certbuf) free(certbuf); if (keybuf) free(keybuf); if (fd >= 0) close(fd); return return_value; }
void spectrogram::load_files( const std::string& path, const commands& cmd, i_fft_f* fft_instance, int64_t start_time, int64_t end_time) { data_.clear(); time_.clear(); bunch_pattern_.clear(); try { fs::path open_path(path.c_str()); std::vector<fs::path> list_file; if (fs::exists(open_path) && fs::is_directory(open_path)) { std::copy( fs::directory_iterator(open_path), fs::directory_iterator(), std::back_inserter(list_file)); sort(list_file.begin(), list_file.end()); } else { throw std::runtime_error(path + " is not a directory!"); } std::vector<fs::path>::iterator ite; bunch_buffer_f acc_bb; std::vector<float> temp; unsigned int acc_count = 1; unsigned int file_count = 0; for (ite = list_file.begin(); ite != list_file.end(); ++ite) { std::string full_path = (*ite).string(); long long time_stamp = time_stamp_from_file(*ite); // check boundaries if (time_stamp < start_time || time_stamp > end_time) continue; boost::posix_time::ptime file_time = ptime_from_file(*ite); std::cout << "\rloading (data) : " << ++file_count << "/" << list_file.size() << " " << file_time; std::cout.flush(); bunch_buffer_f bb = buffer_from_file(*ite, fft_instance); if (bb.empty()) { std::cout << std::endl << "empty (data) : " << full_path << " Not saved!" << std::endl; continue; } if (!pitch_) { pitch_ = bb.buffer_size() / 2; } // save the bunch pattern if (bb.get_bunch_pattern() != bunch_pattern_) { bunch_pattern_ = bb.get_bunch_pattern(); std::cout << std::endl; std::cout << "bunch pattern : "; for (size_t i = 0; i < bunch_pattern_.size(); ++i) std::cout << bunch_pattern_[i] << " "; std::cout << std::endl; } time_.push_back(time_stamp); acc_bb += bb; if (!(acc_count % nb_acc_)) { std::vector<float> temp; // here come the computing cmd(acc_bb, temp); pitch_ = acc_bb.buffer_size(); // apply bunch mask! data_.insert(data_.end(), temp.begin(), temp.end()); acc_bb.clear(); } acc_count++; } // if (!data_.empty()) // normalize(data_); std::cout << std::endl; } catch (const fs::filesystem_error& er) { std::cerr << "exception (fs) : " << er.what() << std::endl; } }