std::string secret_to_wif(const ec_secret& secret, bool compressed) { auto version = to_byte(payment_address::wif_version); data_chunk data; if (compressed) data = build_data({version, secret, to_byte(0x01)}, checksum_size); else data = build_data({version, secret}, checksum_size); append_checksum(data); return encode_base58(data); }
hd_private_key hd_private_key::generate_private_key(uint32_t i) const { if (!valid_) return hd_private_key(); data_chunk data; if (first_hardened_key <= i) data = build_data({to_byte(0x00), k_, to_big_endian(i)}); else data = build_data({K_, to_big_endian(i)}); const auto I = split(hmac_sha512_hash(data, c_)); // The child key ki is (parse256(IL) + kpar) mod n: ec_secret ki = k_; if (!ec_add(ki, I.L)) return hd_private_key(); hd_key_lineage lineage { lineage_.testnet, static_cast<uint8_t>(lineage_.depth + 1), fingerprint(), i }; return hd_private_key(ki, I.R, lineage); }
std::string hd_private_key::encoded() const { auto prefix = mainnet_private_prefix; if (lineage_.testnet) prefix = testnet_private_prefix; auto data = build_data({ to_big_endian(prefix), to_byte(lineage_.depth), to_little_endian(lineage_.parent_fingerprint), to_big_endian(lineage_.child_number), c_, to_byte(0x00), k_ }, checksum_size); append_checksum(data); return encode_base58(data); }
ec_secret create_nonce(ec_secret secret, hash_digest hash, unsigned index) { init.init(); hash_digest K {{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }}; hash_digest V {{ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 }}; K = hmac_sha256_hash(build_data({V, to_byte(0x00), secret, hash}), K); V = hmac_sha256_hash(V, K); K = hmac_sha256_hash(build_data({V, to_byte(0x01), secret, hash}), K); V = hmac_sha256_hash(V, K); while (true) { V = hmac_sha256_hash(V, K); if (0 == index) return V; --index; K = hmac_sha256_hash(build_data({V, to_byte(0x00)}), K); V = hmac_sha256_hash(V, K); } }
int ts::demuxer::demux_ts_packet(const char* ptr) { u_int32_t timecode=0; if(hdmv) { timecode=to_int32(ptr)&0x3fffffff; ptr+=4; } const char* end_ptr=ptr+188; if(ptr[0]!=0x47) // ts sync byte return -1; u_int16_t pid=to_int(ptr+1); u_int8_t flags=to_byte(ptr+3); bool transport_error=pid&0x8000; bool payload_unit_start_indicator=pid&0x4000; bool adaptation_field_exist=flags&0x20; bool payload_data_exist=flags&0x10; u_int8_t continuity_counter=flags&0x0f; pid&=0x1fff; if(transport_error) return -2; if(pid==0x1fff || !payload_data_exist) return 0; ptr+=4; // skip adaptation field if(adaptation_field_exist) { ptr+=to_byte(ptr)+1; if(ptr>=end_ptr) return -3; } #ifdef VERBOSE if(dump==1) printf("%.4x: [%c%c%c%c] %u.%i\n", pid, transport_error?'e':'-', payload_data_exist?'p':'-', payload_unit_start_indicator?'s':'-', adaptation_field_exist?'a':'-', timecode, continuity_counter ); #endif stream& s=streams[pid]; if(!pid || (s.channel!=0xffff && s.type==0xff)) { // PSI if(payload_unit_start_indicator) { // begin of PSI table ptr++; if(ptr>=end_ptr) return -4; if(*ptr!=0x00 && *ptr!=0x02) return 0; if(end_ptr-ptr<3) return -5; u_int16_t l=to_int(ptr+1); if((l&0x3000)!=0x3000) return -6; l&=0x0fff; ptr+=3; int len=end_ptr-ptr; if(l>len) { if(l>ts::table::max_buf_len) return -7; s.psi.reset(); memcpy(s.psi.buf,ptr,len); s.psi.offset+=len; s.psi.len=l; return 0; }else end_ptr=ptr+l; }else { // next part of PSI if(!s.psi.offset) return -8; int len=end_ptr-ptr; if(len>ts::table::max_buf_len-s.psi.offset) return -9; memcpy(s.psi.buf+s.psi.offset,ptr,len); s.psi.offset+=len; if(s.psi.offset<s.psi.len) return 0; else { ptr=s.psi.buf; end_ptr=ptr+s.psi.len; } } if(!pid) { // PAT ptr+=5; if(ptr>=end_ptr) return -10; int len=end_ptr-ptr-4; if(len<0 || len%4) return -11; int n=len/4; for(int i=0;i<n;i++,ptr+=4) { u_int16_t channel=to_int(ptr); u_int16_t pid=to_int(ptr+2); if((pid&0xe000)!=0xe000) return -12; pid&=0x1fff; if(!demuxer::channel || demuxer::channel==channel) { stream& ss=streams[pid]; ss.channel=channel; ss.type=0xff; } } }else { // PMT ptr+=7; if(ptr>=end_ptr) return -13; u_int16_t info_len=to_int(ptr)&0x0fff; ptr+=info_len+2; end_ptr-=4; if(ptr>=end_ptr) return -14; while(ptr<end_ptr) { if(end_ptr-ptr<5) return -15; u_int8_t type=to_byte(ptr); u_int16_t pid=to_int(ptr+1); if((pid&0xe000)!=0xe000) return -16; pid&=0x1fff; info_len=to_int(ptr+3)&0x0fff; ptr+=5+info_len; // ignore unknown streams if(validate_type(type)) { stream& ss=streams[pid]; if(ss.channel!=s.channel || ss.type!=type) { ss.channel=s.channel; ss.type=type; ss.id=++s.id; if(!parse_only && !ss.file.is_opened()) { if(dst.length()) { ss.file.open(file::out,"%s%c%s%s",dst.c_str(),os_slash,prefix.c_str(),get_stream_ext(get_stream_type(ss.type))); fprintf(stderr,"%s%c%s%s\n",dst.c_str(),os_slash,prefix.c_str(),get_stream_ext(get_stream_type(ss.type))); } else ss.file.open(file::out,"%s%s",prefix.c_str(),get_stream_ext(get_stream_type(ss.type))); } } } } if(ptr!=end_ptr) return -18; } }else { if(s.type!=0xff) { // PES if(payload_unit_start_indicator) { s.psi.reset(); s.psi.len=9; } while(s.psi.offset<s.psi.len) { int len=end_ptr-ptr; if(len<=0) return 0; int n=s.psi.len-s.psi.offset; if(len>n) len=n; memcpy(s.psi.buf+s.psi.offset,ptr,len); s.psi.offset+=len; ptr+=len; if(s.psi.len==9) s.psi.len+=to_byte(s.psi.buf+8); } if(s.psi.len) { if(memcmp(s.psi.buf,"\x00\x00\x01",3)) return -19; s.stream_id=to_byte(s.psi.buf+3); u_int8_t flags=to_byte(s.psi.buf+7); s.frame_num++; switch(flags&0xc0) { case 0x80: // PTS only { u_int64_t pts=decode_pts(s.psi.buf+9); #ifdef VERBOSE if(dump==2) printf("%.4x: %llu\n",pid,pts); else if(dump==3) printf("%.4x: track=%.4x.%.2i, type=%.2x, stream=%.2x, pts=%llums\n",pid,s.channel,s.id,s.type,s.stream_id,pts/90); #endif if(s.dts>0 && pts>s.dts) s.frame_length=(u_int32_t)(pts-s.dts); s.dts=pts; if(pts>s.last_pts) s.last_pts=pts; if(!s.first_pts) s.first_pts=pts; } break; case 0xc0: // PTS,DTS { u_int64_t pts=decode_pts(s.psi.buf+9); u_int64_t dts=decode_pts(s.psi.buf+14); #ifdef VERBOSE if(dump==2) printf("%.4x: %llu %llu\n",pid,pts,dts); else if(dump==3) printf("%.4x: track=%.4x.%.2i, type=%.2x, stream=%.2x, pts=%llums, dts=%llums\n",pid,s.channel,s.id,s.type,s.stream_id,pts/90,dts/90); #endif if(s.dts>0 && dts>s.dts) s.frame_length=(u_int32_t)(dts-s.dts); s.dts=dts; if(pts>s.last_pts) s.last_pts=pts; if(!s.first_dts) s.first_dts=dts; } break; } if(pes_output && s.file.is_opened()) s.file.write(s.psi.buf,s.psi.len); s.psi.reset(); } if(s.frame_num) { int len=end_ptr-ptr; if(es_parse) { switch(s.type) { case 0x1b: s.frame_num_h264.parse(ptr,len); break; case 0x06: case 0x81: case 0x83: s.frame_num_ac3.parse(ptr,len); break; } } if(s.file.is_opened()) s.file.write(ptr,len); } } } return 0; }
int main(int argc, char *const argv[]) { char *map_output_file = NULL; if (argc < 3) { usage(argv[0]); return 1; } opterr = 0; int c; while((c = getopt(argc, argv, "ho:")) != -1) { switch (c) { case 'h': usage(argv[0]); return 0; case 'o': map_output_file = optarg; break; default: fprintf(stderr, "Unknown option\n"); return 1; } } if (optind+1 >= argc) { fprintf(stderr, "You must specify at least 2 files to compare\n"); return 1; } const char *file1 = argv[optind]; png24_image image1 = {}; int retval = read_image(file1, &image1); if (retval) { fprintf(stderr, "Can't read %s\n", file1); return retval; } dssim_image *original = dssim_create_image(image1.row_pointers, DSSIM_RGBA, image1.width, image1.height, image1.gamma); free(image1.row_pointers); free(image1.rgba_data); for (int arg = optind+1; arg < argc; arg++) { const char *file2 = argv[arg]; png24_image image2 = {}; retval = read_image(file2, &image2); if (retval) { fprintf(stderr, "Can't read %s\n", file2); break; } if (image1.width != image2.width || image1.height != image2.height) { fprintf(stderr, "Image %s has different size than %s\n", file2, file1); break; } dssim_image *modified = dssim_create_image(image2.row_pointers, DSSIM_RGBA, image2.width, image2.height, image2.gamma); free(image2.row_pointers); free(image2.rgba_data); float *map = NULL; double dssim = dssim_compare(original, modified, map_output_file ? &map : NULL); dssim_dealloc_image(modified); printf("%.6f\t%s\n", dssim, file2); if (map) { dssim_rgba *out = (dssim_rgba*)map; for(int i=0; i < image2.width*image2.height; i++) { const float max = 1.0 - map[i]; const float maxsq = max * max; out[i] = (dssim_rgba) { .r = to_byte(max * 3.0), .g = to_byte(maxsq * 3.0), .b = to_byte((max-0.5) * 2.0f), .a = 255, }; } if (write_image(map_output_file, out, image2.width, image2.height)) { fprintf(stderr, "Can't write %s\n", map_output_file); free(map); return 1; } free(map); } } dssim_dealloc_image(original); return retval; }
int main(int argc, char *const argv[]) { char *map_output_file = NULL; if (argc < 3) { usage(argv[0]); return 1; } opterr = 0; int c; while((c = getopt(argc, argv, "ho:")) != -1) { switch (c) { case 'h': usage(argv[0]); return 0; case 'o': map_output_file = optarg; break; default: fprintf(stderr, "Unknown option\n"); return 1; } } if (optind+1 >= argc) { fprintf(stderr, "You must specify at least 2 files to compare\n"); return 1; } const char *file1 = argv[optind]; png24_image image1 = {}; int retval = read_image(file1, &image1); if (retval) { fprintf(stderr, "Can't read %s (%d)\n", file1, retval); return retval; } dssim_attr *attr = dssim_create_attr(); dssim_image *original = dssim_create_image(attr, image1.row_pointers, DSSIM_RGBA, image1.width, image1.height, get_gamma(&image1)); free(image1.row_pointers); free(image1.rgba_data); for (int arg = optind+1; arg < argc; arg++) { const char *file2 = argv[arg]; png24_image image2 = {}; retval = read_image(file2, &image2); if (retval) { fprintf(stderr, "Can't read %s (%d)\n", file2, retval); break; } if (image1.width != image2.width || image1.height != image2.height) { fprintf(stderr, "Image %s has different size than %s\n", file2, file1); retval = 4; break; } dssim_image *modified = dssim_create_image(attr, image2.row_pointers, DSSIM_RGBA, image2.width, image2.height, get_gamma(&image2)); free(image2.row_pointers); free(image2.rgba_data); if (!modified) { fprintf(stderr, "Unable to process image %s\n", file2); retval = 4; break; } if (map_output_file) { dssim_set_save_ssim_maps(attr, 1, 1); } double dssim = dssim_compare(attr, original, modified); dssim_dealloc_image(modified); printf("%.8f\t%s\n", dssim, file2); if (map_output_file) { dssim_ssim_map map_meta = dssim_pop_ssim_map(attr, 0, 0); dssim_px_t *map = map_meta.data; dssim_rgba *out = (dssim_rgba*)map; for(int i=0; i < map_meta.width*map_meta.height; i++) { const dssim_px_t max = 1.0 - map[i]; const dssim_px_t maxsq = max * max; out[i] = (dssim_rgba) { .r = to_byte(max * 3.0), .g = to_byte(maxsq * 6.0), .b = to_byte(max / ((1.0 - map_meta.dssim) * 4.0)), .a = 255, }; } if (write_image(map_output_file, out, map_meta.width, map_meta.height)) { fprintf(stderr, "Can't write %s\n", map_output_file); free(map); return 1; } free(map); } } dssim_dealloc_image(original); dssim_dealloc_attr(attr); return retval; }
/*! Match Digit HexaDecimal */ inline pHit cbuf_lmatch_DIGIT_HEX(const char* pbuf, size_t len, bix off, pMode pmm) { return pHit1(off, char_is_DIGIT_HEX(pbuf[to_byte(off)])); }
/*! Match Vowel Swedish-Only - åäö */ inline pHit cbuf_lmatch_VOWEL_SVonly(const char* pbuf, size_t len, bix off, pMode pmm) { return pHit1(off, char_is_VOWEL_SVonly(pbuf[to_byte(off)])); }
inline pHit cbuf_lmatch_LETTER_A_to_F(const char* pbuf, size_t len, bix off, pMode pmm) { return pHit1(off, char_is_LETTER_A_to_F(pbuf[to_byte(off)])); }