static void record_ttyrec(FILE *f, void* state, struct timeval *tm, char *buf, int len) { struct ttyrec_header h; h.sec=little_endian(tm->tv_sec); h.usec=little_endian(tm->tv_usec); h.len=little_endian(len); fwrite(&h, 1, sizeof(h), f); fwrite(buf, 1, len, f); }
void init() { char buf[PNG_BYTES_TO_CHECK]; // read in some of the signature bytes io_error_if( fread( buf, 1, PNG_BYTES_TO_CHECK, get() ) != detail::PNG_BYTES_TO_CHECK, "png_check_validity: fail to read file" ); // compare the first PNG_BYTES_TO_CHECK bytes of the signature. io_error_if( png_sig_cmp( (png_bytep)buf, (png_size_t)0, detail::PNG_BYTES_TO_CHECK ) != 0, "png_check_validity: invalid png file" ); _png_ptr = png_create_read_struct( PNG_LIBPNG_VER_STRING, NULL, NULL, NULL ); io_error_if( _png_ptr == NULL, "png_get_file_size: fail to call png_create_write_struct()" ); // allocate/initialize the image information data _info_ptr = png_create_info_struct( _png_ptr ); if( _info_ptr == NULL ) { png_destroy_read_struct( &_png_ptr, png_infopp_NULL, png_infopp_NULL ); io_error( "png_get_file_size: fail to call png_create_info_struct()" ); } if( setjmp( png_jmpbuf( _png_ptr ) ) ) { //free all of the memory associated with the png_ptr and info_ptr png_destroy_read_struct( &_png_ptr, &_info_ptr, png_infopp_NULL ); io_error( "png_get_file_size: fail to call setjmp()" ); } png_init_io( _png_ptr, get() ); png_set_sig_bytes( _png_ptr, PNG_BYTES_TO_CHECK ); png_read_info( _png_ptr, _info_ptr ); if( little_endian() && png_get_bit_depth( _png_ptr, _info_ptr ) > 8 ) png_set_swap( _png_ptr ); }
/*! * Create a new ECAT 6.3 file and return file pointer * or NULL in case of an error. * If file exists, it is renamed as fname% if possible. * Directory list is written in big endian byte order. * * @param fname file name * @param h Ecat 6.3 main header * @return opened file pointer, or NULL in case of failure */ FILE *ecat63Create(const char *fname, ECAT63_mainheader *h) { FILE *fp; char tmp[FILENAME_MAX]; int buf[MatBLKSIZE/4]; if(ECAT63_TEST) printf("ecat63Create()\n"); /* Check the arguments */ if(fname==NULL || h==NULL) return(NULL); /* Check if file exists; backup, if necessary */ if(access(fname, 0) != -1) { strcpy(tmp, fname); strcat(tmp, BACKUP_EXTENSION); if(access(tmp, 0) != -1) remove(tmp); if(ECAT63_TEST) printf("Renaming %s -> %s\n", fname, tmp); rename(fname, tmp); } /* Open file */ fp=fopen(fname, "wb+"); if(fp==NULL) return(fp); /* Write main header */ if(ecat63WriteMainheader(fp, h)) return(NULL); /* Construct an empty matrix list ; convert to little endian if necessary */ memset(buf, 0, MatBLKSIZE); buf[0]=31; buf[1]=2; if(!little_endian()) swawbip(buf, MatBLKSIZE); /* Write data buffer */ fseek(fp, (MatFirstDirBlk-1)*MatBLKSIZE, SEEK_SET); if(ftell(fp)!=(MatFirstDirBlk-1)*MatBLKSIZE) return(NULL); if(fwrite(buf, 4, MatBLKSIZE/4, fp) != MatBLKSIZE/4) return(NULL); /* OK, then return file pointer */ return(fp); }
/*! * Write ECAT 6.3 matrix data to a specified file position. * Data does not need to be allocated for full blocks. * Data must be represented in current machines byte order, and it is * always saved in big endian byte order. * Give also nr of pixels and byte size of one pixel. * * @param fp target file pointer * @param strtblk starting image block [>=1] * @param data pointer to data that is written * @param pxlNr number of items to be written [>=1] * @param pxlSize size of one data item in bytes [>=1] * @return 0 if ok, 1 invalid input, 2 failed to find starting block, * 3 failed to write data */ int ecat63WriteMatdata(FILE *fp, int strtblk, char *data, int pxlNr, int pxlSize) { unsigned char buf[MatBLKSIZE]; char *dptr; int i, blkNr, dataSize, byteNr; if(ECAT63_TEST) printf("ecat63WriteMatdata(fp, %d, data, %d, %d)\n", strtblk, pxlNr, pxlSize); if(fp==NULL || strtblk<1 || data==NULL || pxlNr<1 || pxlSize<1) return(1); memset(buf, 0, MatBLKSIZE); dataSize=pxlNr*pxlSize; if(dataSize<1) return(1); /* block nr taken by all pixels */ blkNr=(dataSize+MatBLKSIZE-1)/MatBLKSIZE; if(blkNr<1) return(1); if(ECAT63_TEST>1) printf(" blkNr=%d\n", blkNr); /* Search the place for writing */ fseek(fp, (strtblk-1)*MatBLKSIZE, SEEK_SET); if(ftell(fp)!=(strtblk-1)*MatBLKSIZE) return(2); /* Save blocks one at a time */ for(i=0, dptr=data; i<blkNr && dataSize>0; i++) { byteNr=(dataSize<MatBLKSIZE)?dataSize:MatBLKSIZE; memcpy(buf, dptr, byteNr); /* Change matrix byte order in big endian platforms */ if(!little_endian()) { if(pxlSize==2) swabip(buf, byteNr); else if(pxlSize==4) swawbip(buf, byteNr); } /* Write block */ if(fwrite(buf, 1, MatBLKSIZE, fp)!=MatBLKSIZE) return(3); /* Prepare for the next block */ dptr+=byteNr; dataSize-=byteNr; } /* next block */ return(0); }
/*! * Write ECAT 6.3 image header * * @param fp target file pointer * @param block block number [>= 3] * @param h Ecat 6.3 image header * @return 0, if ok, 1 invalid input, 2 failed to find block, * 3 failed to write block */ int ecat63WriteImageheader(FILE *fp, int block, ECAT63_imageheader *h) { char buf[MatBLKSIZE]; int i, little, tovax; if(ECAT63_TEST) printf("ecat63WriteImageheader(fp, %d, ih)\n", block); little=little_endian(); /* Clear buf */ memset(buf, 0, MatBLKSIZE); /* Check arguments */ if(fp==NULL || block<3 || h->data_type<1 || h->data_type>7) return(1); if(h->data_type==VAX_I2 || h->data_type==VAX_I4 || h->data_type==VAX_R4) tovax=1; else tovax=0; /* Copy short ints to buf */ memcpy(buf+126, &h->data_type, 2); memcpy(buf+128, &h->num_dimensions, 2); memcpy(buf+132, &h->dimension_1, 2); memcpy(buf+134, &h->dimension_2, 2); memcpy(buf+176, &h->image_min, 2); memcpy(buf+178, &h->image_max, 2); memcpy(buf+200, &h->slice_location, 2); memcpy(buf+202, &h->recon_start_hour, 2); memcpy(buf+204, &h->recon_start_min, 2); memcpy(buf+206, &h->recon_start_sec, 2); memcpy(buf+236, &h->filter_code, 2); memcpy(buf+376, &h->processing_code, 2); memcpy(buf+380, &h->quant_units, 2); memcpy(buf+382, &h->recon_start_day, 2); memcpy(buf+384, &h->recon_start_month, 2); memcpy(buf+386, &h->recon_start_year, 2); memcpy(buf+460, h->fill2, 52); /* big to little endian if necessary */ if(!little) swabip(buf, MatBLKSIZE); /* Copy floats to buf */ ecat63wFloat(&h->x_origin, buf+160, tovax, little); ecat63wFloat(&h->y_origin, buf+164, tovax, little); ecat63wFloat(&h->recon_scale, buf+168, tovax, little); ecat63wFloat(&h->quant_scale, buf+172, tovax, little); ecat63wFloat(&h->pixel_size, buf+184, tovax, little); ecat63wFloat(&h->slice_width, buf+188, tovax, little); ecat63wFloat(&h->image_rotation, buf+296, tovax, little); ecat63wFloat(&h->plane_eff_corr_fctr, buf+300, tovax, little); ecat63wFloat(&h->decay_corr_fctr, buf+304, tovax, little); ecat63wFloat(&h->loss_corr_fctr, buf+308, tovax, little); ecat63wFloat(&h->intrinsic_tilt, buf+312, tovax, little); ecat63wFloat(&h->ecat_calibration_fctr, buf+388, tovax, little); ecat63wFloat(&h->well_counter_cal_fctr, buf+392, tovax, little); for(i=0; i<6; i++) ecat63wFloat(&h->filter_params[i], buf+396+4*i, tovax, little); /* Copy ints to buf */ ecat63wInt(&h->frame_duration, buf+192, tovax, little); ecat63wInt(&h->frame_start_time, buf+196, tovax, little); ecat63wInt(&h->scan_matrix_num, buf+238, tovax, little); ecat63wInt(&h->norm_matrix_num, buf+242, tovax, little); ecat63wInt(&h->atten_cor_mat_num, buf+246, tovax, little); /* Copy chars */ memcpy(buf+0, h->fill1, 126); memcpy(buf+420, h->annotation, 40); /* Write subheader */ fseek(fp, (block-1)*MatBLKSIZE, SEEK_SET); if(ftell(fp)!=(block-1)*MatBLKSIZE) return(2); if(fwrite(buf, 1, 1*MatBLKSIZE, fp) != 1*MatBLKSIZE) return(3); return(0); }
// string 4 bytes length // return integer static int reply_length(lua_State *L) { const char * rawlen_str = luaL_checkstring(L, 1); int rawlen = 0; memcpy(&rawlen, rawlen_str, sizeof(int)); int length = little_endian(rawlen); lua_pushinteger(L, length - 4); return 1; }
/*! * Write ECAT 6.3 scan header * * @param fp target file pointer * @param block block number [>=3] * @param h Ecat 6.3 scan header * @return 0 if ok, 1 invalid input, 2 failed to find block, * 3 failed to write block */ int ecat63WriteScanheader(FILE *fp, int block, ECAT63_scanheader *h) { unsigned char buf[MatBLKSIZE]; int i, little, tovax; if(ECAT63_TEST) printf("ecat63WriteScanheader(fp, %d, ih)\n", block); little=little_endian(); /* Clear buf */ memset(buf, 0, MatBLKSIZE); /* Check arguments */ if(fp==NULL || block<3 || h->data_type<1 || h->data_type>7) return(1); if(h->data_type==VAX_I2 || h->data_type==VAX_I4 || h->data_type==VAX_R4) tovax=1; else tovax=0; /* Copy short ints to buf */ memcpy(buf+126, &h->data_type, 2); memcpy(buf+132, &h->dimension_1, 2); memcpy(buf+134, &h->dimension_2, 2); memcpy(buf+136, &h->smoothing, 2); memcpy(buf+138, &h->processing_code, 2); memcpy(buf+170, &h->frame_duration_sec, 2); memcpy(buf+192, &h->scan_min, 2); memcpy(buf+194, &h->scan_max, 2); memcpy(buf+468, h->fill2, 44); /* big to little endian if necessary */ if(!little) swabip(buf, MatBLKSIZE); /* Copy floats to buf */ ecat63wFloat(&h->sample_distance, buf+146, tovax, little); ecat63wFloat(&h->isotope_halflife, buf+166, tovax, little); ecat63wFloat(&h->scale_factor, buf+182, tovax, little); for(i=0; i<16; i++) { ecat63wFloat(&h->cor_singles[i], buf+316+4*i, tovax, little); ecat63wFloat(&h->uncor_singles[i], buf+380+4*i, tovax, little); } ecat63wFloat(&h->tot_avg_cor, buf+444, tovax, little); ecat63wFloat(&h->tot_avg_uncor, buf+448, tovax, little); ecat63wFloat(&h->loss_correction_fctr, buf+464, tovax, little); /* Copy ints to buf */ ecat63wInt(&h->gate_duration, buf+172, tovax, little); ecat63wInt(&h->r_wave_offset, buf+176, tovax, little); ecat63wInt(&h->prompts, buf+196, tovax, little); ecat63wInt(&h->delayed, buf+200, tovax, little); ecat63wInt(&h->multiples, buf+204, tovax, little); ecat63wInt(&h->net_trues, buf+208, tovax, little); ecat63wInt(&h->total_coin_rate, buf+452, tovax, little); ecat63wInt(&h->frame_start_time, buf+456, tovax, little); ecat63wInt(&h->frame_duration, buf+460, tovax, little); /* Copy chars */ memcpy(buf+0, h->fill1, 126); /* Write subheader */ fseek(fp, (block-1)*MatBLKSIZE, SEEK_SET); if(ftell(fp)!=(block-1)*MatBLKSIZE) return(2); if(fwrite(buf, 1, 1*MatBLKSIZE, fp) != 1*MatBLKSIZE) return(3); return(0); }/*****************************************************************************/
void ide::buffer_uncompress(const char* packed, std::size_t packed_sz, int* values) { int current = 0; unsigned int j = 0; short s; char c; int i; bool le = little_endian(); while (j < packed_sz) { c = packed[j]; j += 1; if (c != -128) { current += c; *values=current; values++; continue; } ((u_s *) & s)[0].b[0] = packed[j]; ((u_s *) & s)[0].b[1] = packed[j + 1]; j += 2; if (!le) { byte_swap_short((char *) &s); } if (s != -32768) { current += s; *values=current; values++; continue; } ((u_i *) & i)[0].b[0] = packed[j]; ((u_i *) & i)[0].b[1] = packed[j + 1]; ((u_i *) & i)[0].b[2] = packed[j + 2]; ((u_i *) & i)[0].b[3] = packed[j + 3]; j += 4; if (!le) { byte_swap_int((char *) &i); } current += i; *values=current; values++; } }
int wsr88d_read_ray(Wsr88d_file *wf, Wsr88d_ray *wsr88d_ray) { int n; n = fread(wsr88d_ray, sizeof(Wsr88d_ray), 1, wf->fptr); /* if (n > 0) print_packet_info(wsr88d_ray); */ if (little_endian()) wsr88d_swap_ray(wsr88d_ray); return n; }
int wsr88d_read_file_header(Wsr88d_file *wf, Wsr88d_file_header *wsr88d_file_header) { int n; n = fread(&wsr88d_file_header->title, sizeof(wsr88d_file_header->title), 1, wf->fptr); if (little_endian()) wsr88d_swap_file_header(wsr88d_file_header); return n; }
/// /// LDR I/O, only available to LDR images void SavePng(const std::string& file_name) { BOOST_STATIC_ASSERT( (boost::is_same<T, UCHAR>::value) ); BOOST_STATIC_ASSERT( channel == 3 ); size_t pos = file_name.find_last_of("."); std::string file_type = file_name.substr(++pos, std::string::npos); boost::to_lower(file_type); assert(file_type == "png"); FILE* fp; if((fp = fopen(file_name.c_str(), "wb")) == NULL) assert(0); png_structp _png_ptr; png_infop _info_ptr; _png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if(!_png_ptr) assert(0); _info_ptr = png_create_info_struct(_png_ptr); if (!_info_ptr) { png_destroy_write_struct(&_png_ptr, png_infopp_NULL); assert(0); } if (setjmp(png_jmpbuf(_png_ptr))) { png_destroy_write_struct(&_png_ptr, &_info_ptr); assert(0); } png_init_io(_png_ptr, fp); png_set_IHDR(_png_ptr, _info_ptr, GetWidth(), GetHeight(), 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); png_write_info(_png_ptr, _info_ptr); if (little_endian() && false /* RGB depth always = 8 */) png_set_swap(_png_ptr); for(UINT y = 0; y < GetHeight(); ++y) png_write_row(_png_ptr, (png_bytep)GetPixelPtr(0, y)); png_write_end(_png_ptr,_info_ptr); fclose(fp); }
static uint16 read_little_16 (uint8 **data) { uint16 x; uint8 a,b; a = **data; b = *(*data + 1); *data = *data + 2; x = little_endian (b<<8 | a); return x; }
int main (int argc, char *argv[]) { int i, fptlen = 128, passes = 3; unsigned char fingerprint[256 >> 3]; if (argc <= 1) { argv[1] = "?"; argc++; } for (i = 1; i < argc; i++) { if ((argv[i][0] == '?') || /* show help info */ (argv[i][0] == '-' && argv[i][1] == '?') || (argv[i][0] == '-' && argv[i][1] == 'h')) { printf (" (none) hash input from stdin\n"); printf (" ?/-?/-h show help menu\n"); printf (" -c hash certification data\n"); printf (" -e test endianity\n"); printf (" -mstring hash message\n"); printf (" -s test speed\n"); printf (" file_name hash file\n"); } else if (argv[i][0] == '-' && argv[i][1] == 'm') { /* hash string */ haval_string (argv[i]+2, fingerprint, fptlen, passes); printf ("HAVAL(\"%s\") = ", argv[i]+2); haval_print (fingerprint, fptlen); printf ("\n"); } else if (strcmp (argv[i], "-c") == 0) { /* hash test set */ haval_cert (); } else if (strcmp (argv[i], "-s") == 0) { /* test speed */ haval_speed (); } else if (strcmp (argv[i], "-e") == 0) { /* test endianity */ if (little_endian()) { printf ("Your machine is little-endian.\n"); printf ("You may define LITTLE_ENDIAN to speed up processing.\n"); } else { printf ("Your machine is NOT little-endian.\n"); printf ("You must NOT define LITTLE_ENDIAN.\n"); } } else { /* hash file */ int fd = open(argv[i], O_RDONLY); if (fd < 0 || haval_fd (fd, -1, fingerprint, fptlen, passes)) { printf ("%s can not be opened !\n= ", argv[i]); } else { printf ("HAVAL(File %s) = ", argv[i]); haval_print (fingerprint, fptlen); printf ("\n"); } } } return (0); }
std::vector<char> file::read_bytes(size_t const max_bytes) { size_t const chunk{ std::min<size_t>(max_bytes, 4096) }; std::vector<char> out(chunk); /* Output param from OV; unused. */ int32_t bit_stream{}; /* We may not read a whole chunk, so keep track of how * far behind we are. */ size_t accumulated_diff{}; size_t total_read{}; for(size_t i{}; ; ++i) { /* Determine how much to read next. */ size_t const next_chunk{ std::min(max_bytes - total_read, chunk) }; if(next_chunk == 0) { out.resize(total_read); break; } if(total_read > max_bytes) { throw std::runtime_error("Read too much ogg"); } auto const read( ::ov_read(m_ogg.get(), out.data() + (chunk * i) - accumulated_diff, next_chunk, !little_endian(), 2, 1, &bit_stream)); if(read < 0) { throw std::runtime_error("Error reading from ogg file"); } else if(!read) { /* Done reading, remove the last unused chunk. */ out.resize(out.size() - chunk); break; } /* We may not've read a full chunk. */ size_t const diff{ chunk - read }; accumulated_diff += diff; total_read += read; /* Size up for another chunk. */ out.resize(out.size() + (chunk - diff)); } return out; }
static int divby1billion(unsigned int *a) { int i; ULONGLONG kk; kk = 0; for (i = MLENGTH(a) - 1; i >= 0; i--) { if (little_endian()) { ((unsigned int *) &kk)[1] = ((unsigned int *) &kk)[0]; ((unsigned int *) &kk)[0] = a[i]; } else { ((unsigned int *) &kk)[0] = ((unsigned int *) &kk)[1]; ((unsigned int *) &kk)[1] = a[i]; } a[i] = (int) (kk / 1000000000); kk -= (ULONGLONG) 1000000000 * a[i]; } // length of quotient for (i = MLENGTH(a) - 1; i > 0; i--) if (a[i]) break; MLENGTH(a) = i + 1; if (little_endian()) return ((unsigned int *) &kk)[0]; else return ((unsigned int *) &kk)[1]; }
static inline void store64_le(uint8_t dest[], uint64_t src) { union t { int64_t dw; uint8_t b[4]; } *result = (void*) &src; if (little_endian()) { memcpy(dest, &src, sizeof src); } else { dest[0] = result->b[3]; dest[1] = result->b[2]; dest[2] = result->b[1]; dest[3] = result->b[0]; } }
EXPORT_SYM uint32_t load_le_uint32(const uint8_t *in) { union { uint32_t w; uint8_t b[4]; } x, y; memcpy(&x, in, 4); y = x; if (!little_endian()) { y.b[0] = x.b[3]; y.b[1] = x.b[2]; y.b[2] = x.b[1]; y.b[3] = x.b[0]; } return y.w; }
/*! * Read ECAT 6.3 matrix data and convert byte order if necessary * Remember to allocate memory for full blocks! * There are differences here when compared to ecat7.c * * @param fp file pointer from where data is read * @param strtblk starting block [>= 1] * @param blkNr number of block to be read [>= 0] * @param data pointer to block where data is read * @param dtype data type code * @return 0 if ok, 1 invalid input, 2 failed to read data, * 9 failed to find starting block from file, */ int ecat63ReadMatdata(FILE *fp, int strtblk, int blkNr, char *data, int dtype) { int i, n, little, err=0; char *cptr; float f; if(ECAT63_TEST) printf("ecat63ReadMatdata(fp, %d, %d, data, %d)\n", strtblk, blkNr, dtype); /* Check the arguments */ if(blkNr<=0 || strtblk<1 || data==NULL) return(1); /* Seek the first data block */ fseek(fp, (strtblk-1)*MatBLKSIZE, SEEK_SET); if(ftell(fp)!=(strtblk-1)*MatBLKSIZE) return(9); /* Read the data blocks */ if(fread(data, MatBLKSIZE, blkNr, fp) < blkNr) return(2); /* Translate data if necessary */ little=little_endian(); switch(dtype) { case BYTE_TYPE: /* byte format...no translation necessary */ break; case VAX_I2: /* byte conversion necessary on big endian platform */ if(!little) {cptr=data; swabip(cptr, blkNr*MatBLKSIZE);} break; case VAX_I4: for(i=0, cptr=data; i<blkNr*MatBLKSIZE; i+=4, cptr+=4) { n=ecat63rInt(cptr, 1, little); memcpy(cptr, &n, 4); } break; case VAX_R4: for(i=0, cptr=data; i<blkNr*MatBLKSIZE; i+=4, cptr+=4) { f=ecat63rFloat(cptr, 1, little); memcpy(cptr, &f, 4); } break; case IEEE_R4: /* IEEE float ; byte conversion necessary on big end platforms */ case SUN_I4: /* SUN int ; byte conversion necessary on big end platforms */ if(!little) swawbip(data, blkNr*MatBLKSIZE); break; case SUN_I2: /* SUN short ; byte conversion necessary on big end platforms */ if(!little) swabip(data, blkNr*MatBLKSIZE); break; default: /* if something else, for now think it as an error */ err=2; break; } return(err); }
/*! * Read ECAT 6.3 attenuation header * * @param fp file pointer * @param blk block number [2..number of blocks] * @param h Ecat 6.3 attenuation header * @return 0 if ok, 1 failed to find block, 3 failed to read block, * 4 invalid data type, 5 invalid scale factor */ int ecat63ReadAttnheader(FILE *fp, int blk, ECAT63_attnheader *h) { unsigned char buf[MatBLKSIZE]; int little; /* 1 if current platform is little endian (i386), else 0 */ int vaxdata=1; /* 1 if data is in VAX format, else 0 */ if(ECAT63_TEST) printf("ecat63ReadAttnheader(fp, %d, ah)\n", blk); if(fp==NULL || blk<2 || h==NULL) return(1); little=little_endian(); /* Seek the subheader block */ fseek(fp, (blk-1)*MatBLKSIZE, SEEK_SET); if(ftell(fp)!=(blk-1)*MatBLKSIZE) return(2); /* Read the subheader block */ if(fread(buf, MatBLKSIZE, 1, fp)<1) return(3); /* Copy short ints */ /* in big endian platform, change byte order temporarily */ if(!little) swabip(buf, MatBLKSIZE); memcpy(&h->data_type, buf+126, 2); if(h->data_type>4) vaxdata=0; /*printf("data_type=%d\n", h->data_type);*/ memcpy(&h->attenuation_type, buf+128, 2); memcpy(&h->dimension_1, buf+132, 2); memcpy(&h->dimension_2, buf+134, 2); /* Change back the byte order */ if(!little) swabip(buf, MatBLKSIZE); /* Copy floats */ h->scale_factor=ecat63rFloat(buf+182, vaxdata, little); h->x_origin=ecat63rFloat(buf+186, vaxdata, little); h->y_origin=ecat63rFloat(buf+190, vaxdata, little); h->x_radius=ecat63rFloat(buf+194, vaxdata, little); h->y_radius=ecat63rFloat(buf+198, vaxdata, little); h->tilt_angle=ecat63rFloat(buf+202, vaxdata, little); h->attenuation_coeff=ecat63rFloat(buf+206, vaxdata, little); h->sample_distance=ecat63rFloat(buf+210, vaxdata, little); /* Check that most important contents are ok */ if(h->data_type<BYTE_TYPE || h->data_type>SUN_I4) { if(ECAT63_TEST) printf("Invalid data types; probable conversion error.\n"); return(4); } if(h->scale_factor<=0.0 || h->scale_factor>1.0e8) { if(ECAT63_TEST) printf("Invalid scale factor; probable conversion error.\n"); return(5); } return(0); }
static inline uint64_t load64_le(const uint8_t p[]) { union { int64_t dw; uint8_t b[4]; } result; if (little_endian()) { memcpy(&result, p, sizeof result); } else { result.b[0] = p[3]; result.b[1] = p[2]; result.b[2] = p[1]; result.b[3] = p[0]; } return result.dw; }
static void output_hinsi (FILE* ofpter) { int i; short k; int little_endianp = little_endian (); /* hinsi ha koko de hikkuri kaesu */ for (i = 0; i < jt.maxserial; i++) { k = jeary[i]->hinsi; if (little_endianp) { rev_short_fun (&k); } fwrite (&k, 1, 2, ofpter); } }
static void play_nh_recorder(FILE *f, void *(synch_init_wait)(struct timeval *ts, void *arg), void *(synch_wait)(struct timeval *tv, void *arg), void *(synch_print)(char *buf, int len, void *arg), void *arg, struct timeval *cont) { char buf[BUFFER_SIZE]; int b,i,i0; struct timeval tv; uint32_t t,tp; t=0; i=b=0; while ((b=fread(buf+b-i, 1, BUFFER_SIZE-(b-i), f))>0) { i0=0; for (i=0;i<b;i++) if (!buf[i]) { if (i0<i) synch_print(buf+i0, i-i0, arg); if (i+4>b) // timestamp happened on a block boundary { if (b<5) // incomplete last timestamp return; memmove(buf+i, buf, b-i); goto block_end; } else { tp=t; t=little_endian(*(uint32_t*)(buf+i+1)); i0=i+=4; tv.tv_sec=(t-tp)/100; tv.tv_usec=(t-tp)%100*10000; synch_wait(&tv, arg); } } if (i0<i) synch_print(buf+i0, i-i0, arg); block_end:; } }
/*! * Read ECAT 6.3 normalization header * * @param fp file pointer * @param blk block number [2..number of blocks] * @param h Ecat 6.3 normalization header * @return 0 if ok, 1 invalid input, 2 failed to find block, * 3 failed to read block, 4 invalid data type, 5 invalid scale factor */ int ecat63ReadNormheader(FILE *fp, int blk, ECAT63_normheader *h) { unsigned char buf[MatBLKSIZE]; int little; /* 1 if current platform is little endian (i386), else 0 */ int vaxdata=1; /* 1 if data is in VAX format, else 0 */ if(ECAT63_TEST) printf("ecat63ReadNormheader(fp, %d, nh)\n", blk); if(fp==NULL || blk<2 || h==NULL) return(1); little=little_endian(); /* Seek the subheader block */ fseek(fp, (blk-1)*MatBLKSIZE, SEEK_SET); if(ftell(fp)!=(blk-1)*MatBLKSIZE) return(2); /* Read the subheader block */ if(fread(buf, MatBLKSIZE, 1, fp)<1) return(3); /* Copy short ints */ /* in big endian platform, change byte order temporarily */ if(!little) swabip(buf, MatBLKSIZE); memcpy(&h->data_type, buf+126, 2); if(h->data_type>4) vaxdata=0; /*printf("data_type=%d\n", h->data_type);*/ memcpy(&h->dimension_1, buf+132, 2); memcpy(&h->dimension_2, buf+134, 2); memcpy(&h->norm_hour, buf+186, 2); memcpy(&h->norm_minute, buf+188, 2); memcpy(&h->norm_second, buf+190, 2); memcpy(&h->norm_day, buf+192, 2); memcpy(&h->norm_month, buf+194, 2); memcpy(&h->norm_year, buf+196, 2); /* Change back the byte order */ if(!little) swabip(buf, MatBLKSIZE); /* Copy floats */ h->scale_factor=ecat63rFloat(buf+182, vaxdata, little); h->fov_source_width=ecat63rFloat(buf+198, vaxdata, little); /* Check that most important contents are ok */ if(h->data_type<BYTE_TYPE && h->data_type>SUN_I4) { if(ECAT63_TEST) printf("Invalid data types; probable conversion error.\n"); return(4); } if(h->scale_factor<=0.0 || h->scale_factor>1.0e8) { if(ECAT63_TEST) printf("Invalid scale factor; probable conversion error.\n"); return(5); } return(0); }
static int otp_md_hash (const char *data, size_t len, const EVP_MD *md, int le, unsigned char *res, size_t ressz) { EVP_MD_CTX *ctx; ctx = EVP_MD_CTX_create(); EVP_DigestInit_ex(ctx, md, NULL); EVP_DigestUpdate(ctx, data, len); EVP_DigestFinal_ex(ctx, res, NULL); EVP_MD_CTX_destroy(ctx); if (le) little_endian(res, ressz); return 0; }
static int otp_md_next (OtpKey key, const EVP_MD *md, int le, unsigned char *res, size_t ressz) { EVP_MD_CTX *ctx; ctx = EVP_MD_CTX_create(); EVP_DigestInit_ex(ctx, md, NULL); EVP_DigestUpdate(ctx, key, OTPKEYSIZE); EVP_DigestFinal_ex(ctx, res, NULL); EVP_MD_CTX_destroy(ctx); if (le) little_endian(res, ressz); compressmd (key, res, ressz); return 0; }
/*! * Write ECAT 6.3 normalization header * * @param fp target file pointer * @param block block number [>=3] * @param h Ecat 6.3 normalization header * @return 0 if ok, 1 invalid input, 2 failed to find block, * 3 failed to write block */ int ecat63WriteNormheader(FILE *fp, int block, ECAT63_normheader *h) { unsigned char buf[MatBLKSIZE]; int little, tovax; if(ECAT63_TEST) printf("ecat63WriteNormheader(fp, %d, nh)\n", block); little=little_endian(); /* Clear buf */ memset(buf, 0, MatBLKSIZE); /* Check arguments */ if(fp==NULL || block<3 || h->data_type<1 || h->data_type>7) return(1); if(h->data_type==VAX_I2 || h->data_type==VAX_I4 || h->data_type==VAX_R4) tovax=1; else tovax=0; /* Copy short ints to buf */ memcpy(buf+126, &h->data_type, 2); memcpy(buf+132, &h->dimension_1, 2); memcpy(buf+134, &h->dimension_2, 2); memcpy(buf+372, &h->norm_hour, 2); memcpy(buf+376, &h->norm_minute, 2); memcpy(buf+380, &h->norm_second, 2); memcpy(buf+384, &h->norm_day, 2); memcpy(buf+388, &h->norm_month, 2); memcpy(buf+392, &h->norm_year, 2); /* big to little endian if necessary */ if(!little) swabip(buf, MatBLKSIZE); /* Copy floats to buf */ ecat63wFloat(&h->scale_factor, buf+182, tovax, little); ecat63wFloat(&h->fov_source_width, buf+198, tovax, little); /* Write subheader */ fseek(fp, (block-1)*MatBLKSIZE, SEEK_SET); if(ftell(fp)!=(block-1)*MatBLKSIZE) return(2); if(fwrite(buf, 1, 1*MatBLKSIZE, fp) != 1*MatBLKSIZE) return(3); return(0); }
static void play_auto(FILE *f, void *(synch_init_wait)(struct timeval *ts, void *arg), void *(synch_wait)(struct timeval *tv, void *arg), void *(synch_print)(char *buf, int len, void *arg), void *arg, struct timeval *cont) { struct ttyrec_header tth; int len, got; char buf[BUFFER_SIZE]; struct timeval tv; // first, grab 12 bytes and see if it looks like a ttyrec header got=0; do { // must use unbuffered I/O here, to not spoil it for play_live() if ((len=read(fileno(f), ((char*)&tth)+got, 12-got))<=0) return; got+=len; } while (got<12); if (little_endian(tth.usec)<1000000 && little_endian(tth.len)>0 && little_endian(tth.len)<65536) { tv.tv_sec=little_endian(tth.sec); tv.tv_usec=little_endian(tth.usec); synch_init_wait(&tv, arg); got=little_endian(tth.len); while (got>0) { if ((len=fread(buf, 1, (got>BUFFER_SIZE)?BUFFER_SIZE:got, f))<=0) return; synch_print(buf, len, arg); got-=len; } play_ttyrec(f, 0, synch_wait, synch_print, arg, &tv); return; } // fall back to "live" gettimeofday(&tv, 0); synch_init_wait(&tv, arg); synch_print((char*)&tth, 12, arg); play_live(f, 0, synch_wait, synch_print, arg, &tv); }
static int otp_md_init (OtpKey key, const char *pwd, const char *seed, const EVP_MD *md, int le, unsigned char *res, size_t ressz) { EVP_MD_CTX *ctx; char *p; int len; ctx = EVP_MD_CTX_create(); len = strlen(pwd) + strlen(seed); p = malloc (len + 1); if (p == NULL) return -1; strlcpy (p, seed, len + 1); strlwr (p); strlcat (p, pwd, len + 1); EVP_DigestInit_ex(ctx, md, NULL); EVP_DigestUpdate(ctx, p, len); EVP_DigestFinal_ex(ctx, res, NULL); EVP_MD_CTX_destroy(ctx); if (le) little_endian(res, ressz); free (p); compressmd (key, res, ressz); return 0; }
void output_dic_data (void) { fprintf (stderr, "%d words are in this dictionary\n", jt.maxserial); if (little_endian ()) { rev_dic_data (); } jt.syurui = which_dict; jt.gosuu = jt.maxserial; output_header (ofpter, &jt, &file_head); /* dummy; Will be rewitten. */ output_comment (ofpter); output_hinsi_list (ofpter); output_hindo (ofpter); output_hinsi (ofpter); #ifdef CONVERT_with_SiSheng if (which_dict == CWNN_REV_DICT) output_sisheng (ofpter); #endif output_kanji (ofpter); }
/*! * Write ECAT 6.3 attenuation header * * @param fp target file pointer * @param block block number [>=3] * @param h Ecat 6.3 attenuation header * @return 0 if ok, 1 invalid input, 2 failed to find block, * 3 failed to write block */ int ecat63WriteAttnheader(FILE *fp, int block, ECAT63_attnheader *h) { unsigned char buf[MatBLKSIZE]; int little, tovax; if(ECAT63_TEST) printf("ecat63WriteAttnheader(fp, %d, ah)\n", block); little=little_endian(); /* Clear buf */ memset(buf, 0, MatBLKSIZE); /* Check arguments */ if(fp==NULL || block<3 || h->data_type<1 || h->data_type>7) return(1); if(h->data_type==VAX_I2 || h->data_type==VAX_I4 || h->data_type==VAX_R4) tovax=1; else tovax=0; /* Copy short ints to buf */ memcpy(buf+126, &h->data_type, 2); memcpy(buf+128, &h->attenuation_type, 2); memcpy(buf+132, &h->dimension_1, 2); memcpy(buf+134, &h->dimension_2, 2); /* big to little endian if necessary */ if(!little) swabip(buf, MatBLKSIZE); /* Copy floats to buf */ ecat63wFloat(&h->scale_factor, buf+182, tovax, little); ecat63wFloat(&h->x_origin, buf+186, tovax, little); ecat63wFloat(&h->y_origin, buf+190, tovax, little); ecat63wFloat(&h->x_radius, buf+194, tovax, little); ecat63wFloat(&h->y_radius, buf+198, tovax, little); ecat63wFloat(&h->tilt_angle, buf+202, tovax, little); ecat63wFloat(&h->attenuation_coeff, buf+206, tovax, little); ecat63wFloat(&h->sample_distance, buf+210, tovax, little); /* Write subheader */ fseek(fp, (block-1)*MatBLKSIZE, SEEK_SET); if(ftell(fp)!=(block-1)*MatBLKSIZE) return(2); if(fwrite(buf, 1, 1*MatBLKSIZE, fp) != 1*MatBLKSIZE) return(3); return(0); }