key, valueの連想配列class
struct Person { public: Person(const string &name, int height) : m_name(name) , m_height(height) {} public: string m_name; int m_height; } ... map<Person, int> mp; Person p("aaa", 180); mp[p] = 123;
ソフトウェアエンジニアの技術ブログ:Software engineer tech blog
随机应变 ABCD: Always Be Coding and … : хороший
key, valueの連想配列class
struct Person { public: Person(const string &name, int height) : m_name(name) , m_height(height) {} public: string m_name; int m_height; } ... map<Person, int> mp; Person p("aaa", 180); mp[p] = 123;
符号付き、符号なしで型の指定ができる
int8_t、int16_t、int32_t、int64_t、uint8_t、uint16_t、uint32_t、uint64_t、int_fast8_t、int_fast16_t、int_fast32_t、int_fast64_t、uint_fast8_t、uint_fast16_t、uint_fast32_t、uint_fast64_t、int_least8_t、int_least16_t、int_least32_t、int_least64_t、uint_least8_t、uint_least16_t、uint_least32_t、uint_least64_t、intmax_t、uintmax_t、intptr_t、uintptr_t
それにしても多いですね
Signetは、Bitcoinのテストネットとは異なり、特定のプロジェクトや開発者が制御できる独自のテストネットワーク
cmd, faucet, addr, passwordなどのオプションがある
getcoins.py
import argparse import io import requests import subprocess import sys import xml.etree.ElementTree DEFAULT_GLOBAL_FAUCET = 'https://signetfaucet.com/claim' DEFAULT_GLOBAL_CAPTCHA = 'https://signetfaucet.com/captcha' GLOBAL_FIRST_BLOCK_HASH = '00000086d6b2636cb2a392d45edc4ec544a10024d30141c9adf4bfd9de533b53' # braille unicode block BASE = 0x2800 BIT_PER_PIXEL = [ [0x01, 0x08], [0x02, 0x10], [0x04, 0x20], [0x40, 0x80], ] BW = 2 BH = 4 # imagemagick or compatible fork (used for converting SVG) CONVERT = 'convert' class PPMImage: ''' Load a PPM image (Pillow-ish API). ''' def __init__(self, f): if f.readline() != b'P6\n': raise ValueError('Invalid ppm format: header') line = f.readline() (width, height) = (int(x) for x in line.rstrip().split(b' ')) if f.readline() != b'255\n': raise ValueError('Invalid ppm format: color depth') data = f.read(width * height * 3) stride = width * 3 self.size = (width, height) self._grid = [[tuple(data[stride * y + 3 * x:stride * y + 3 * (x + 1)]) for x in range(width)] for y in range(height)] def getpixel(self, pos): return self._grid[pos[1]][pos[0]] def print_image(img, threshold=128): '''Print black-and-white image to terminal in braille unicode characters.''' x_blocks = (img.size[0] + BW - 1) // BW y_blocks = (img.size[1] + BH - 1) // BH for yb in range(y_blocks): line = [] for xb in range(x_blocks): ch = BASE for y in range(BH): for x in range(BW): try: val = img.getpixel((xb * BW + x, yb * BH + y)) except IndexError: pass else: if val[0] < threshold: ch |= BIT_PER_PIXEL[y][x] line.append(chr(ch)) print(''.join(line)) parser = argparse.ArgumentParser(description='Script to get coins from a faucet.', epilog='You may need to start with double-dash (--) when providing bitcoin-cli arguments.') parser.add_argument('-c', '--cmd', dest='cmd', default='bitcoin-cli', help='bitcoin-cli command to use') parser.add_argument('-f', '--faucet', dest='faucet', default=DEFAULT_GLOBAL_FAUCET, help='URL of the faucet') parser.add_argument('-g', '--captcha', dest='captcha', default=DEFAULT_GLOBAL_CAPTCHA, help='URL of the faucet captcha, or empty if no captcha is needed') parser.add_argument('-a', '--addr', dest='addr', default='', help='Bitcoin address to which the faucet should send') parser.add_argument('-p', '--password', dest='password', default='', help='Faucet password, if any') parser.add_argument('-n', '--amount', dest='amount', default='0.001', help='Amount to request (0.001-0.1, default is 0.001)') parser.add_argument('-i', '--imagemagick', dest='imagemagick', default=CONVERT, help='Path to imagemagick convert utility') parser.add_argument('bitcoin_cli_args', nargs='*', help='Arguments to pass on to bitcoin-cli (default: -signet)') args = parser.parse_args() if args.bitcoin_cli_args == []: args.bitcoin_cli_args = ['-signet'] def bitcoin_cli(rpc_command_and_params): argv = [args.cmd] + args.bitcoin_cli_args + rpc_command_and_params try: return subprocess.check_output(argv).strip().decode() except FileNotFoundError: raise SystemExit(f"The binary {args.cmd} could not be found") except subprocess.CalledProcessError: cmdline = ' '.join(argv) raise SystemExit(f"-----\nError while calling {cmdline} (see output above).") if args.faucet.lower() == DEFAULT_GLOBAL_FAUCET: # Get the hash of the block at height 1 of the currently active signet chain curr_signet_hash = bitcoin_cli(['getblockhash', '1']) if curr_signet_hash != GLOBAL_FIRST_BLOCK_HASH: raise SystemExit('The global faucet cannot be used with a custom Signet network. Please use the global signet or setup your custom faucet to use this functionality.\n') else: # For custom faucets, don't request captcha by default. if args.captcha == DEFAULT_GLOBAL_CAPTCHA: args.captcha = '' if args.addr == '': # get address for receiving coins args.addr = bitcoin_cli(['getnewaddress', 'faucet', 'bech32']) data = {'address': args.addr, 'password': args.password, 'amount': args.amount} # Store cookies # for debugging: print(session.cookies.get_dict()) session = requests.Session() if args.captcha != '': # Retrieve a captcha try: res = session.get(args.captcha) res.raise_for_status() except requests.exceptions.RequestException as e: raise SystemExit(f"Unexpected error when contacting faucet: {e}") # Size limitation svg = xml.etree.ElementTree.fromstring(res.content) if svg.attrib.get('width') != '150' or svg.attrib.get('height') != '50': raise SystemExit("Captcha size doesn't match expected dimensions 150x50") # Convert SVG image to PPM, and load it try: rv = subprocess.run([args.imagemagick, 'svg:-', '-depth', '8', 'ppm:-'], input=res.content, check=True, capture_output=True) except FileNotFoundError: raise SystemExit(f"The binary {args.imagemagick} could not be found. Please make sure ImageMagick (or a compatible fork) is installed and that the correct path is specified.") img = PPMImage(io.BytesIO(rv.stdout)) # Terminal interaction print_image(img) print(f"Captcha from URL {args.captcha}") data['captcha'] = input('Enter captcha: ') try: res = session.post(args.faucet, data=data) except Exception: raise SystemExit(f"Unexpected error when contacting faucet: {sys.exc_info()[0]}") # Display the output as per the returned status code if res: # When the return code is in between 200 and 400 i.e. successful print(res.text) elif res.status_code == 404: print('The specified faucet URL does not exist. Please check for any server issues/typo.') elif res.status_code == 429: print('The script does not allow for repeated transactions as the global faucet is rate-limitied to 1 request/IP/day. You can access the faucet website to get more coins manually') else: print(f'Returned Error Code {res.status_code}\n{res.text}\n') print('Please check the provided arguments for their validity and/or any possible typo.')
good_morning.hpp
#ifndef GOOD_MORNING_H #define GOOD_MORNING_H void good_morning(); #endif
good_morning.cpp
#include <iostream> #include "good_morning.hpp" void good_morning() { std::cout << "Good Monring!" << std::endl; }
main.cpp
#include "hello.hpp" #include "good_morning.hpp" int main(){ hello(); good_morning(); }
$ g++ -c hello.cpp good_morning.cpp
$ ar rvs libgreetings.a hello.o good_morning.o
ar: creating libgreetings.a
a – hello.o
a – good_morning.o
$ g++ main.cpp libgreetings.a
$ ./a.out
Hello!
Good Monring!
main.cpp
#include "hello.hpp" int main(){ hello(); }
hello.hpp
#ifndef HELLO_H #define HELLO_H void hello(); #endif
hello.cpp
#include <iostream> #include "hello.hpp" void hello() { std::cout << "Hello!" << std::endl; }
### g++でビルドする場合
$ g++ -c main.cpp hello.cpp
$ g++ -o a.out main.o hello.o
$ ./a.out
Hello!
### CMake
CMakeLists.txtを作成する
CMakeLists.txt
# CMakeバージョン cmake_minimum_required(VERSION 3.13) # プロジェクト名と使用する言語 project(test_cmake CXX) add_executable(a.out main.cpp hello.cpp)
CMakeを使ってビルドする際は、ビルド専用のディレクトリを作成し、その中で行う
$ ls
CMakeLists.txt hello.cpp hello.hpp main.cpp
$ cmake -S . -B build
$ tree
.
├── build
│ ├── CMakeCache.txt
│ ├── CMakeFiles
│ │ ├── 3.22.1
│ │ │ ├── CMakeCXXCompiler.cmake
│ │ │ ├── CMakeDetermineCompilerABI_CXX.bin
│ │ │ ├── CMakeSystem.cmake
│ │ │ └── CompilerIdCXX
│ │ │ ├── a.out
│ │ │ ├── CMakeCXXCompilerId.cpp
│ │ │ └── tmp
│ │ ├── a.out.dir
│ │ │ ├── build.make
│ │ │ ├── cmake_clean.cmake
│ │ │ ├── compiler_depend.make
│ │ │ ├── compiler_depend.ts
│ │ │ ├── DependInfo.cmake
│ │ │ ├── depend.make
│ │ │ ├── flags.make
│ │ │ ├── link.txt
│ │ │ └── progress.make
│ │ ├── cmake.check_cache
│ │ ├── CMakeDirectoryInformation.cmake
│ │ ├── CMakeOutput.log
│ │ ├── CMakeTmp
│ │ ├── Makefile2
│ │ ├── Makefile.cmake
│ │ ├── progress.marks
│ │ └── TargetDirectories.txt
│ ├── cmake_install.cmake
│ └── Makefile
├── CMakeLists.txt
├── hello.cpp
├── hello.hpp
└── main.cpp
liefはPE、ELFなどの実行ファイルをパースし、一部のレコードの編集を行うためのライブラリ
– 実行ファイルの一部のレコードを編集した上で新しい実行ファイルとして書き出し
– パース可能な実行ファイルフォーマットが多い
– pefile, pyelftoolsなどがあった
### LIEFのインストール(ubutu22.04)
$ git clone https://github.com/lief-project/LIEF.git
$ cd LIEF && mkdir build && cd build
$ sudo apt update
$ sudo apt install cmake
$ sudo apt install ninja-build
※ninjaはbuildを高速化するツール。よくオープンソースのbuildなどに使われる。CPUを効率的に使用しており、複数コアのCPUだと特に速くなる。
※cmakeはコンパイラに依存しない自動化ツール
$ cmake -DLIEF_PYTHON_API=on -DPYTHON_VERSION=3.6 -DCMAKE_BUILD_TYPE=Release -DLIEF_TESTS=on -GNinja ..
$ cd api/python
$ python3
Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] on linux
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import lief
Traceback (most recent call last):
File “
ModuleNotFoundError: No module named ‘lief’
>>> exit()
う〜ん、よくわかりませんね。
symbol-check.py
import sys import lief
set -e とはコマンドの実行結果がエラー(= 終了ステータスが 0 以外)になった時にシェルスクリプトを自動的に中断させる機能
条件文(&& や || を含む)と共に使うシェル関数は set -e の効果が無効になる
contrib/devtools/split-debug.sh.in
set -e if [ $# -ne 3 ]; then echo "usage: $0 <input> <stripped-binary> <debug-binary>" fi
自分の能力を超えて仮想的な記憶領域を得ること
容量不足時にSSD/HDDなどに書き出す
e.g. 使用していないときはSSDに保存しておいて、使用時に取り出すなど
devtools/security-check.py
L R, W, Xの意味がよくわかりませんね。
L ARCH.PPC64 はPowerPCのことのように見えるが何をやっているか不明
def check_ELF_separate_code(binary): ''' Check that sections are appropriately separated in virtual memory, based on their permissions. This checks for missing -Wl,-z,separate-code and potentially other problems. ''' R = lief.ELF.SEGMENT_FLAGS.R W = lief.ELF.SEGMENT_FLAGS.W E = lief.ELF.SEGMENT_FLAGS.X EXPECTED_FLAGS = { # Read + execute '.init': R | E, '.plt': R | E, '.plt.got': R | E, '.plt.sec': R | E, '.text': R | E, '.fini': R | E, # Read-only data '.interp': R, '.note.gnu.property': R, '.note.gnu.build-id': R, '.note.ABI-tag': R, '.gnu.hash': R, '.dynsym': R, '.dynstr': R, '.gnu.version': R, '.gnu.version_r': R, '.rela.dyn': R, '.rela.plt': R, '.rodata': R, '.eh_frame_hdr': R, '.eh_frame': R, '.qtmetadata': R, '.gcc_except_table': R, '.stapsdt.base': R, # Writable data '.init_array': R | W, '.fini_array': R | W, '.dynamic': R | W, '.got': R | W, '.data': R | W, '.bss': R | W, } if binary.header.machine_type == lief.ELF.ARCH.PPC64: # .plt is RW on ppc64 even with separate-code EXPECTED_FLAGS['.plt'] = R | W # For all LOAD program headers get mapping to the list of sections, # and for each section, remember the flags of the associated program header. flags_per_section = {} for segment in binary.segments: if segment.type == lief.ELF.SEGMENT_TYPES.LOAD: for section in segment.sections: flags_per_section[section.name] = segment.flags # Spot-check ELF LOAD program header flags per section # If these sections exist, check them against the expected R/W/E flags for (section, flags) in flags_per_section.items(): if section in EXPECTED_FLAGS: if int(EXPECTED_FLAGS[section]) != int(flags): return False return True
C++におけるファイルの分割はクラス定義単位で行う
### ヘッダファイル
マクロ定義、型定義、プロトタイプ宣言の3つを書くのが基本
C++ではクラスを利用するには、そのクラスが提供するヘッダファイルを取り込む必要がある
以下のようなファイルがあったとする
class POS { private: int x; int y; public: POS(); int setPos(int tmpx, int tmpy); } POS::POS(){ x = 0; y = 0; } int POS::setPos(int tmpx, int tmpy){ x = tmpx; y = tmpy; return 0; }
これを分割する場合
POS.h
#ifndef POS_H #define POS_H class POS { private: int x; int y; public: POS(); int setPos(int tmpx, int tmpy); } #endif
POS.cpp
#include "POS.h" POS::POS(){ x = 0; y = 0; } int POS::setPos(int tmpx, int tmpy){ x = tmpx; y = tmpy; return 0; }
つまり、最初にheader情報を見て、全体の流れを理解してからソースファイルを見ていくのね。※以前はcppを先に見て、.hは付加情報かと思っていました…orz
HeadersSyncState::ProcessingResult HeadersSyncState::ProcessNextHeaders(const
std::vector
で、ProcessingResult ProcessNextHeaders となっているのは、headerssync.hで、ProcessingResultの構造体を示していたので、このProcessingResultの型で返却して欲しい という書き方なのね。
struct ProcessingResult { std::vector<CBlockHeader> pow_validated_headers; bool success{false}; bool request_more{false}; };
Readmeを読むと以下のように書いている
A script to generate optimal parameters for the headerssync module (src/headerssync.cpp). It takes no command-line
options, as all its configuration is set at the top of the file. It runs many times faster inside PyPy. Invocation:
つまり、node間でのヘッダー情報のやり取りのことだ。c++ファイルのヘッダではない。stateで、 PRESYNC、REDOWNLOAD、FINALとある。
src/headerssync.h, src/headerssync.cpp を見てみる。
### headerssync.h
A compressed CBlockHeader, which leaves out the prevhash
HeadersSyncState
We wish to download a peer’s headers chain in a DoS-resistant way.
* – In the first download phase, called pre-synchronization, we can calculate
* the work on the chain as we go (just by checking the nBits value on each
* header, and validating the proof-of-work).
In phase 1 (presync)
In phase 2 (redownload)
#### headerssync.cpp
// The two constants below are computed using the simulation script in
// contrib/devtools/headerssync-params.py.
m_max_commitments = 6*(Ticks
downloadはこれでやってる?
if (m_current_chain_work >= m_minimum_required_work) { m_redownloaded_headers.clear(); m_redownload_buffer_last_height = m_chain_start->nHeight; m_redownload_buffer_first_prev_hash = m_chain_start->GetBlockHash(); m_redownload_buffer_last_hash = m_chain_start->GetBlockHash(); m_redownload_chain_work = m_chain_start->nChainWork; m_download_state = State::REDOWNLOAD; LogPrint(BCLog::NET, "Initial headers sync transition with peer=%d: reached sufficient work at height=%i, redownloading from height=%i\n", m_id, m_current_height, m_redownload_buffer_last_height); }
downloadのstatusに応じて処理しているのはわかるが、getHashの処理とかはheaderssync.cppの中には書いてないように見えるがどこでやってるんだろうか?
あと、基本的に、HeadersSyncState::${処理}と書かれていますね。
なんか凄いな…