ops_memory_t* ops_write_mem_from_file(const char *filename, int* errnum) { size_t initial_size=1024; int fd=0; unsigned char buf[1024]; ops_memory_t* mem=NULL; *errnum=0; fd=ops_open(filename,O_RDONLY | O_BINARY, 0); if (fd < 0) { *errnum=errno; return ops_false; } mem=ops_memory_new(); ops_memory_init(mem,initial_size); for (;;) { ssize_t n=0; n=read(fd,buf,1024); if (n<0) { *errnum=errno; break; } if (!n) break; ops_memory_add(mem, &buf[0], n); } close(fd); return mem; }
ops_boolean_t ops_write_literal_data_from_file(const char *filename, const ops_literal_data_type_t type, ops_create_info_t *info) { size_t initial_size=1024; int fd=0; ops_boolean_t rtn; unsigned char buf[1024]; ops_memory_t* mem=NULL; size_t len=0; fd=ops_open(filename,O_RDONLY | O_BINARY, 0); if (fd < 0) return ops_false; mem=ops_memory_new(); ops_memory_init(mem,initial_size); for (;;) { ssize_t n=0; n=read(fd,buf,1024); if (!n) break; if (n == -1) { close(fd); ops_memory_free(mem); return ops_false; } ops_memory_add(mem, &buf[0], n); } close(fd); // \todo do we need to check text data for <cr><lf> line endings ? len=ops_memory_get_length(mem); rtn=ops_write_ptag(OPS_PTAG_CT_LITERAL_DATA, info) && ops_write_length(1+1+4+len,info) && ops_write_scalar(type, 1, info) && ops_write_scalar(0, 1, info) // filename && ops_write_scalar(0, 4, info) // date && ops_write(ops_memory_get_data(mem), len, info); ops_memory_free(mem); return rtn; }
ops_boolean_t ops_decrypt_memory(const unsigned char *encrypted_memory,int em_length, unsigned char **decrypted_memory,int *out_length, ops_keyring_t* keyring, const ops_boolean_t use_armour, ops_parse_cb_t* cb_get_passphrase) { int fd_in=0; int fd_out=0; char* myfilename=NULL; // ops_parse_info_t *pinfo=NULL; // setup for reading from given input file ops_memory_t *input_mem = ops_memory_new() ; ops_memory_add(input_mem,encrypted_memory,em_length) ; ops_setup_memory_read(&pinfo, input_mem, NULL, callback_write_parsed, ops_false); if (pinfo == NULL) { perror("cannot create memory read"); return ops_false; } // setup memory chunk ops_memory_t *output_mem; ops_setup_memory_write(&pinfo->cbinfo.cinfo, &output_mem,0) ; if (output_mem == NULL) { perror("Cannot create output memory"); ops_teardown_memory_read(pinfo, input_mem); return ops_false; } // \todo check for suffix matching armour param // setup keyring and passphrase callback pinfo->cbinfo.cryptinfo.keyring=keyring; pinfo->cbinfo.cryptinfo.cb_get_passphrase=cb_get_passphrase; // Set up armour/passphrase options if (use_armour) ops_reader_push_dearmour(pinfo); // Do it ops_boolean_t res = ops_parse_and_print_errors(pinfo); // Unsetup if (use_armour) ops_reader_pop_dearmour(pinfo); // copy output memory to supplied buffer. // *out_length = ops_memory_get_length(output_mem) ; *decrypted_memory = ops_mallocz(*out_length) ; memcpy(*decrypted_memory,ops_memory_get_data(output_mem),*out_length) ; ops_decrypt_memory_ABORT: ops_teardown_memory_write(pinfo->cbinfo.cinfo, output_mem); ops_teardown_memory_read(pinfo, input_mem); return res ; }