void holding_set_from_driver( char *holding_file, off_t orig_size, crc_t native_crc, crc_t client_crc, crc_t server_crc) { int fd; size_t buflen; char buffer[DISK_BLOCK_BYTES]; char *read_buffer; dumpfile_t file; if((fd = robust_open(holding_file, O_RDWR, 0)) == -1) { dbprintf(_("holding_set_origsize: open of %s failed: %s\n"), holding_file, strerror(errno)); return; } buflen = read_fully(fd, buffer, sizeof(buffer), NULL); if (buflen <= 0) { dbprintf(_("holding_set_origsize: %s: empty file?\n"), holding_file); close(fd); return; } parse_file_header(buffer, &file, (size_t)buflen); lseek(fd, (off_t)0, SEEK_SET); file.orig_size = orig_size; file.native_crc = native_crc; file.client_crc = client_crc; file.server_crc = server_crc; read_buffer = build_header(&file, NULL, DISK_BLOCK_BYTES); full_write(fd, read_buffer, DISK_BLOCK_BYTES); dumpfile_free_data(&file); amfree(read_buffer); close(fd); }
int holding_file_get_dumpfile( char * fname, dumpfile_t *file) { char buffer[DISK_BLOCK_BYTES]; int fd; memset(buffer, 0, sizeof(buffer)); fh_init(file); file->type = F_UNKNOWN; if((fd = robust_open(fname, O_RDONLY, 0)) == -1) return 0; if(read_fully(fd, buffer, sizeof(buffer), NULL) != sizeof(buffer)) { aclose(fd); return 0; } aclose(fd); parse_file_header(buffer, file, sizeof(buffer)); return 1; }
int rename_tmp_holding( char * holding_file, int complete) { int fd; size_t buflen; char buffer[DISK_BLOCK_BYTES]; dumpfile_t file; char *filename; char *filename_tmp = NULL; memset(buffer, 0, sizeof(buffer)); filename = g_strdup(holding_file); while(filename != NULL && filename[0] != '\0') { g_free(filename_tmp); filename_tmp = g_strconcat(filename, ".tmp", NULL); if((fd = robust_open(filename_tmp,O_RDONLY, 0)) == -1) { dbprintf(_("rename_tmp_holding: open of %s failed: %s\n"),filename_tmp,strerror(errno)); amfree(filename); amfree(filename_tmp); return 0; } buflen = read_fully(fd, buffer, sizeof(buffer), NULL); close(fd); if(rename(filename_tmp, filename) != 0) { dbprintf(_("rename_tmp_holding: could not rename \"%s\" to \"%s\": %s"), filename_tmp, filename, strerror(errno)); } if (buflen <= 0) { dbprintf(_("rename_tmp_holding: %s: empty file?\n"), filename); amfree(filename); amfree(filename_tmp); return 0; } parse_file_header(buffer, &file, (size_t)buflen); if(complete == 0 ) { char * header; if((fd = robust_open(filename, O_RDWR, 0)) == -1) { dbprintf(_("rename_tmp_holdingX: open of %s failed: %s\n"), filename, strerror(errno)); dumpfile_free_data(&file); amfree(filename); amfree(filename_tmp); return 0; } file.is_partial = 1; if (debug_holding > 1) dump_dumpfile_t(&file); header = build_header(&file, NULL, DISK_BLOCK_BYTES); if (!header) /* this shouldn't happen */ error(_("header does not fit in %zd bytes"), (size_t)DISK_BLOCK_BYTES); if (full_write(fd, header, DISK_BLOCK_BYTES) != DISK_BLOCK_BYTES) { dbprintf(_("rename_tmp_holding: writing new header failed: %s"), strerror(errno)); dumpfile_free_data(&file); amfree(filename); amfree(filename_tmp); free(header); close(fd); return 0; } free(header); close(fd); } g_free(filename); filename = g_strdup(file.cont_filename); dumpfile_free_data(&file); } amfree(filename); amfree(filename_tmp); return 1; }
FILE * OpenSndFile(lame_global_flags *gfp,const char* inPath, int default_samp, int default_channels) { struct stat sb; void parse_file_header(lame_global_flags *gfp,FILE *sf); /* set the defaults from info incase we cannot determine them from file */ num_samples=MAX_U_32_NUM; samp_freq=default_samp; num_channels = default_channels; if (!strcmp(inPath, "-")) { /* Read from standard input. */ #ifdef __EMX__ _fsetmode(stdin,"b"); #elif (defined __BORLANDC__) setmode(_fileno(stdin), O_BINARY); #elif (defined __CYGWIN__) setmode(fileno(stdin), _O_BINARY); #elif (defined _WIN32) _setmode(_fileno(stdin), _O_BINARY); #endif musicin = stdin; } else { if ((musicin = fopen(inPath, "rb")) == NULL) { fprintf(stderr, "Could not find \"%s\".\n", inPath); exit(1); } } input_bitrate=0; if (gfp->input_format==sf_mp3) { #ifdef AMIGA_MPEGA if (-1==lame_decode_initfile(inPath,&num_channels,&samp_freq,&input_bitrate,&num_samples)) { fprintf(stderr,"Error reading headers in mp3 input file %s.\n", inPath); exit(1); } #endif #ifdef HAVEMPGLIB if (-1==lame_decode_initfile(musicin,&num_channels,&samp_freq,&input_bitrate,&num_samples)) { fprintf(stderr,"Error reading headers in mp3 input file %s.\n", inPath); exit(1); } #endif }else{ if (gfp->input_format != sf_raw) { parse_file_header(gfp,musicin); } if (gfp->input_format==sf_raw) { /* assume raw PCM */ fprintf(stderr, "Assuming raw pcm input file"); if (gfp->swapbytes==TRUE) fprintf(stderr, " : Forcing byte-swapping\n"); else fprintf(stderr, "\n"); } } if (num_samples==MAX_U_32_NUM && musicin != stdin) { /* try to figure out num_samples */ if (0==stat(inPath,&sb)) { /* try file size, assume 2 bytes per sample */ if (gfp->input_format == sf_mp3) { FLOAT totalseconds = (sb.st_size*8.0/(1000.0*GetSndBitrate())); num_samples= totalseconds*GetSndSampleRate(); }else{ num_samples = sb.st_size/(2*GetSndChannels()); } } } return musicin; }
void Restore::restore_next(Signal* signal, FilePtr file_ptr) { Uint32 *data, len= 0; Uint32 status = file_ptr.p->m_status; Uint32 page_count = file_ptr.p->m_pages.getSize(); do { Uint32 left= file_ptr.p->m_bytes_left; if (left < 8) { jam(); /** * Not enough bytes to read header */ break; } Ptr<GlobalPage> page_ptr(0,0), next_page_ptr(0,0); m_global_page_pool.getPtr(page_ptr, file_ptr.p->m_current_page_ptr_i); List::Iterator it; Uint32 pos= file_ptr.p->m_current_page_pos; if(status & File::READING_RECORDS) { jam(); /** * We are reading records */ len= ntohl(* (page_ptr.p->data + pos)) + 1; ndbrequire(len < GLOBAL_PAGE_SIZE_WORDS); } else { jam(); /** * Section length is in 2 word */ if(pos + 1 == GLOBAL_PAGE_SIZE_WORDS) { jam(); /** * But that's stored on next page... * and since we have atleast 8 bytes left in buffer * we can be sure that that's in buffer */ LocalDataBuffer<15> pages(m_databuffer_pool, file_ptr.p->m_pages); Uint32 next_page = file_ptr.p->m_current_page_index + 1; pages.position(it, next_page % page_count); m_global_page_pool.getPtr(next_page_ptr, * it.data); len= ntohl(* next_page_ptr.p->data); } else { jam(); len= ntohl(* (page_ptr.p->data + pos + 1)); } } if (file_ptr.p->m_status & File::FIRST_READ) { jam(); len= 3; file_ptr.p->m_status &= ~(Uint32)File::FIRST_READ; } if (4 * len > left) { jam(); /** * Not enought bytes to read "record" */ if (unlikely((status & File:: FILE_THREAD_RUNNING) == 0)) { crash_during_restore(file_ptr, __LINE__, 0); } len= 0; break; } /** * Entire record is in buffer */ if(pos + len >= GLOBAL_PAGE_SIZE_WORDS) { jam(); /** * But it's split over pages */ if(next_page_ptr.p == 0) { LocalDataBuffer<15> pages(m_databuffer_pool, file_ptr.p->m_pages); Uint32 next_page = file_ptr.p->m_current_page_index + 1; pages.position(it, next_page % page_count); m_global_page_pool.getPtr(next_page_ptr, * it.data); } file_ptr.p->m_current_page_ptr_i = next_page_ptr.i; file_ptr.p->m_current_page_pos = (pos + len) - GLOBAL_PAGE_SIZE_WORDS; file_ptr.p->m_current_page_index = (file_ptr.p->m_current_page_index + 1) % page_count; if (len <= GLOBAL_PAGE_SIZE_WORDS) { jam(); Uint32 first = (GLOBAL_PAGE_SIZE_WORDS - pos); // wl4391_todo removing valgrind overlap warning for now memmove(page_ptr.p, page_ptr.p->data+pos, 4 * first); memcpy(page_ptr.p->data+first, next_page_ptr.p, 4 * (len - first)); data= page_ptr.p->data; } else { jam(); /** * A table definition can be larger than one page... * when that happens copy it out to side buffer * * First copy part belonging to page_ptr * Then copy full middle pages (moving forward in page-list) * Last copy last part */ Uint32 save = len; assert(len <= NDB_ARRAY_SIZE(m_table_buf)); Uint32 * dst = m_table_buf; /** * First */ Uint32 first = (GLOBAL_PAGE_SIZE_WORDS - pos); memcpy(dst, page_ptr.p->data+pos, 4 * first); len -= first; dst += first; /** * Middle */ while (len > GLOBAL_PAGE_SIZE_WORDS) { jam(); memcpy(dst, next_page_ptr.p, 4 * GLOBAL_PAGE_SIZE_WORDS); len -= GLOBAL_PAGE_SIZE_WORDS; dst += GLOBAL_PAGE_SIZE_WORDS; { LocalDataBuffer<15> pages(m_databuffer_pool, file_ptr.p->m_pages); Uint32 next_page = (file_ptr.p->m_current_page_index + 1) % page_count; pages.position(it, next_page % page_count); m_global_page_pool.getPtr(next_page_ptr, * it.data); file_ptr.p->m_current_page_ptr_i = next_page_ptr.i; file_ptr.p->m_current_page_index = next_page; } } /** * last */ memcpy(dst, next_page_ptr.p, 4 * len); file_ptr.p->m_current_page_pos = len; /** * Set pointer and len */ len = save; data = m_table_buf; } } else { file_ptr.p->m_current_page_pos = pos + len; data= page_ptr.p->data+pos; } file_ptr.p->m_bytes_left -= 4*len; if(status & File::READING_RECORDS) { if(len == 1) { file_ptr.p->m_status = status & ~(Uint32)File::READING_RECORDS; } else { parse_record(signal, file_ptr, data, len); } } else { switch(ntohl(* data)){ case BackupFormat::FILE_HEADER: parse_file_header(signal, file_ptr, data-3, len+3); break; case BackupFormat::FRAGMENT_HEADER: file_ptr.p->m_status = status | File::READING_RECORDS; parse_fragment_header(signal, file_ptr, data, len); break; case BackupFormat::FRAGMENT_FOOTER: parse_fragment_footer(signal, file_ptr, data, len); break; case BackupFormat::TABLE_LIST: parse_table_list(signal, file_ptr, data, len); break; case BackupFormat::TABLE_DESCRIPTION: parse_table_description(signal, file_ptr, data, len); break; case BackupFormat::GCP_ENTRY: parse_gcp_entry(signal, file_ptr, data, len); break; case BackupFormat::EMPTY_ENTRY: // skip break; case 0x4e444242: // 'NDBB' if (check_file_version(signal, ntohl(* (data+2))) == 0) { break; } default: parse_error(signal, file_ptr, __LINE__, ntohl(* data)); } } } while(0); if(file_ptr.p->m_bytes_left == 0 && status & File::FILE_EOF) { file_ptr.p->m_status &= ~(Uint32)File::RESTORE_THREAD_RUNNING; /** * File is finished... */ close_file(signal, file_ptr); return; } /** * We send an immediate signal to continue the restore, at times this * could lead to burning some extra CPU since we might still wait for * input from the disk reading. This code is however only executed * as part of restarts, so it should be ok to spend some extra CPU * to ensure that restarts are quick. */ signal->theData[0] = RestoreContinueB::RESTORE_NEXT; signal->theData[1] = file_ptr.i; sendSignal(reference(), GSN_CONTINUEB, signal, 2, JBB); }
static int do_chunk( int infd, struct databuf * db) { size_t nread; char header_buf[DISK_BLOCK_BYTES]; startclock(); dumpsize = dumpbytes = filesize = (off_t)0; headersize = 0; memset(header_buf, 0, sizeof(header_buf)); /* * The first thing we should receive is the file header, which we * need to save into "file", as well as write out. Later, the * chunk code will rewrite it. */ nread = full_read(infd, header_buf, SIZEOF(header_buf)); if (nread != sizeof(header_buf)) { if(errno != 0) { errstr = vstrallocf(_("cannot read header: %s"), strerror(errno)); } else { errstr = vstrallocf(_("cannot read header: got %zd bytes instead of %zd"), nread, sizeof(header_buf)); } return 0; } parse_file_header(header_buf, &file, (size_t)nread); if(write_tapeheader(db->fd, &file)) { int save_errno = errno; char *m = vstrallocf(_("write_tapeheader file %s: %s"), db->filename, strerror(errno)); errstr = quote_string(m); amfree(m); if(save_errno == ENOSPC) { putresult(NO_ROOM, "%s %lld\n", handle, (long long)(db->use+db->split_size-dumpsize)); } return 0; } dumpsize += (off_t)DISK_BLOCK_KB; filesize = (off_t)DISK_BLOCK_KB; headersize += DISK_BLOCK_KB; /* * We've written the file header. Now, just write data until the * end. */ while ((nread = full_read(infd, db->buf, (size_t)(db->datalimit - db->datain))) > 0) { db->datain += nread; while(db->dataout < db->datain) { if(!databuf_flush(db)) { return 0; } } } while(db->dataout < db->datain) { if(!databuf_flush(db)) { return 0; } } if(dumpbytes > (off_t)0) { dumpsize += (off_t)1; /* count partial final KByte */ filesize += (off_t)1; } return 1; }
static void make_stream (HwpHWP5File *file, GError **error) { GsfInput *input = NULL; GsfInfile *ole = GSF_INFILE (file->priv->olefile); gint n_root_entry = gsf_infile_num_children (ole); if (n_root_entry < 1) { g_set_error_literal (error, HWP_FILE_ERROR, HWP_FILE_ERROR_INVALID, "invalid hwp file"); return; } /* 우선 순위에 따라 스트림을 만든다 */ input = gsf_infile_child_by_name (ole, "FileHeader"); if (input && gsf_infile_num_children (GSF_INFILE (input)) == -1) { file->file_header_stream = input; input = NULL; parse_file_header (file); } else { goto FAIL; } input = gsf_infile_child_by_name (ole, "DocInfo"); if (input && gsf_infile_num_children (GSF_INFILE (input)) == -1) { if (file->is_compress) { GInputStream *gis; GZlibDecompressor *zd; GInputStream *cis; gis = (GInputStream *) gsf_input_stream_new (input); zd = g_zlib_decompressor_new (G_ZLIB_COMPRESSOR_FORMAT_RAW); cis = g_converter_input_stream_new (gis, (GConverter *) zd); g_filter_input_stream_set_close_base_stream (G_FILTER_INPUT_STREAM (cis), TRUE); file->doc_info_stream = cis; g_object_unref (zd); g_object_unref (gis); input = NULL; } else { file->doc_info_stream = (GInputStream *) gsf_input_stream_new (input); } } else { goto FAIL; } if (!file->is_distribute) input = gsf_infile_child_by_name (ole, "BodyText"); else input = gsf_infile_child_by_name (ole, "ViewText"); if (input) { for (gint i = 0; i < gsf_infile_num_children (GSF_INFILE (input)); i++) { GsfInput *section = gsf_infile_child_by_name (GSF_INFILE (input), g_strdup_printf("Section%d", i)); if (gsf_infile_num_children (GSF_INFILE (section)) != -1) { if (GSF_IS_INPUT (section)) g_object_unref (section); g_set_error_literal (error, HWP_FILE_ERROR, HWP_FILE_ERROR_INVALID, "invalid hwp file"); return; } if (file->is_distribute) { guint8 *data = g_malloc0 (256); gsf_input_read (section, 4, NULL); gsf_input_read (section, 256, data); guint32 seed = GSF_LE_GET_GUINT32 (data); msvc_srand (seed); gint n = 0, val = 0, offset; for (guint i = 0; i < 256; i++) { if (n == 0) { val = msvc_rand() & 0xff; n = (msvc_rand() & 0xf) + 1; } data[i] ^= val; n--; } offset = 4 + (seed & 0xf); gchar *key = g_malloc0 (16); memcpy (key, (const gchar *) data + offset, 16); #ifdef HWP_ENABLE_DEBUG gchar *sha1 = g_convert ((const gchar *) data + offset, 80, "UTF-8", "UTF-16LE", NULL, NULL, error); printf ("sha1: %s\n", sha1); printf ("key: %s\n", key); g_free (sha1); #endif g_free (data); EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new (); EVP_CIPHER_CTX_init (ctx); EVP_DecryptInit_ex (ctx, EVP_aes_128_ecb(), NULL, (unsigned char *) key, NULL); g_free (key); EVP_CIPHER_CTX_set_padding(ctx, 0); /* no padding */ gsf_off_t encrypted_data_len = gsf_input_remaining (section); guint8 const *encrypted_data = gsf_input_read (section, encrypted_data_len, NULL); guint8 *decrypted_data = g_malloc (encrypted_data_len); int decrypted_data_len, len; EVP_DecryptUpdate (ctx, decrypted_data, &len, encrypted_data, encrypted_data_len); decrypted_data_len = len; EVP_DecryptFinal_ex (ctx, decrypted_data + len, &len); decrypted_data_len += len; EVP_CIPHER_CTX_free (ctx); g_object_unref (section); section = gsf_input_memory_new (decrypted_data, decrypted_data_len, TRUE); } if (file->is_compress) { GInputStream *gis; GZlibDecompressor *zd; GInputStream *cis; gis = (GInputStream *) gsf_input_stream_new (section); zd = g_zlib_decompressor_new (G_ZLIB_COMPRESSOR_FORMAT_RAW); cis = g_converter_input_stream_new (gis, (GConverter *) zd); g_filter_input_stream_set_close_base_stream (G_FILTER_INPUT_STREAM (cis), TRUE); g_ptr_array_add (file->section_streams, cis); g_object_unref (zd); g_object_unref (gis); } else { GInputStream *stream = (GInputStream *) gsf_input_stream_new (section); g_ptr_array_add (file->section_streams, stream); } } /* for */ g_object_unref (input); input = NULL; } else { goto FAIL; } input = gsf_infile_child_by_name (ole, "\005HwpSummaryInformation"); if (input && gsf_infile_num_children (GSF_INFILE (input)) == -1) { file->summary_info_stream = input; input = NULL; } else { goto FAIL; } input = gsf_infile_child_by_name (ole, "BinData"); if (input) { gint n_data = gsf_infile_num_children (GSF_INFILE (input)); for (gint i = 0; i < n_data; i++) { GsfInput *bin_data_input = gsf_infile_child_by_index (GSF_INFILE (input), i); if (gsf_infile_num_children (GSF_INFILE (bin_data_input)) != -1) { if (GSF_IS_INPUT (bin_data_input)) g_object_unref (bin_data_input); g_set_error_literal (error, HWP_FILE_ERROR, HWP_FILE_ERROR_INVALID, "invalid hwp file"); return; } if (file->is_compress) { GInputStream *gis; GZlibDecompressor *zd; GInputStream *cis; gis = (GInputStream *) gsf_input_stream_new (bin_data_input); zd = g_zlib_decompressor_new (G_ZLIB_COMPRESSOR_FORMAT_RAW); cis = g_converter_input_stream_new (gis, (GConverter *) zd); g_filter_input_stream_set_close_base_stream (G_FILTER_INPUT_STREAM (cis), TRUE); g_ptr_array_add (file->bin_data_streams, cis); g_object_unref (zd); g_object_unref (gis); } else { GInputStream *stream = (GInputStream *) gsf_input_stream_new (bin_data_input); g_ptr_array_add (file->bin_data_streams, stream); } } g_object_unref (input); input = NULL; } input = gsf_infile_child_by_name (ole, "PrvText"); if (input && gsf_infile_num_children (GSF_INFILE (input)) == -1) { file->prv_text_stream = input; input = NULL; } else { goto FAIL; } input = gsf_infile_child_by_name (ole, "PrvImage"); if (input && gsf_infile_num_children (GSF_INFILE (input)) == -1) { file->prv_image_stream = input; input = NULL; } else { goto FAIL; } return; FAIL: if (GSF_IS_INPUT (input)) g_object_unref (input); g_set_error_literal (error, HWP_FILE_ERROR, HWP_FILE_ERROR_INVALID, "invalid hwp file"); return; }