2023/04/21

[C] 16進制字串與 byte 陣列戶轉

 Convert between hex string and byte array.

以空間換取時間, 盡量減少計算跟比較.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
static uint8_t HEX_VALUE[256] = {
//  x0 x1 x2 x3 x4 x5 x6 x7   x8 x9 xA xB xC xD xE xF
     0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,  // 0x
     0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,  // 1x
     0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,  // 2x
     0, 1, 2, 3, 4, 5, 6, 7,   8, 9, 0, 0, 0, 0, 0, 0,  // 3x
     0,10,11,12,13,14,15, 0,   0, 0, 0, 0, 0, 0, 0, 0,  // 4x
     0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,  // 5x
     0,10,11,12,13,14,15, 0,   0, 0, 0, 0, 0, 0, 0, 0,  // 6x
     0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,  // 7x
     0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,  // 8x
     0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,  // 9x
     0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,  // Ax
     0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,  // Bx
     0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,  // Cx
     0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,  // Dx
     0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,  // Ex
     0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0   // Fx
};

static uint8_t HEX_CHAR[16] = {
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};

void bytes2hex(uint8_t* src, char* dest, uint8_t srcsize) {
    uint8_t flag;
    uint8_t pos;

    flag = 0;
    pos = 0;
    while (srcsize--) {
if (flag || src[srcsize]) {
dest[pos++] = HEX_CHAR[src[srcsize]>>4];
dest[pos++] = HEX_CHAR[src[srcsize]&0xf];
} } } void hex2bytes(char *src, uint8_t* dest, uint8_t destsize) { uint8_t len; uint8_t pos; uint8_t hilo; uint8_t byte; uint8_t lohi[2]; char *cptr; len = strlen(src); if (len > (destsize* 2)) {
return; } cptr = src + len; pos = 0; ++len; while (len) { memset(lohi, 0, 2); hilo = 2; while (hilo-- && --len) { lohi[hilo] = HEX_VALUE[*--cptr]; } dest[pos++] = lohi[1] | (lohi[0] << 4); } return; }




2023/03/16

Docker 筆記

列出所有 container

    docker ps -a


啟動即停止 container

    docker start container_id/name

    docker stop container_id/name


刪除 container

    docker rm container_id/name


在目前路徑執行 container 指令 (通常用於編譯)

    docker --workdir $PWD container_id/name command

--------------------------------------------------------------------------------

docker --help

Usage: docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Options:
      --config string      Location of client config files (default "/home/jwang/.docker")
  -D, --debug              Enable debug mode
  -H, --host list          Daemon socket(s) to connect to
  -l, --log-level string   Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
      --tls                Use TLS; implied by --tlsverify
      --tlscacert string   Trust certs signed only by this CA (default "/home/jwang/.docker/ca.pem")
      --tlscert string     Path to TLS certificate file (default "/home/jwang/.docker/cert.pem")
      --tlskey string      Path to TLS key file (default "/home/jwang/.docker/key.pem")
      --tlsverify          Use TLS and verify the remote
  -v, --version            Print version information and quit

Management Commands:
  builder     Manage builds
  config      Manage Docker configs
  container   Manage containers
  engine      Manage the docker engine
  image       Manage images
  network     Manage networks
  node        Manage Swarm nodes
  plugin      Manage plugins
  secret      Manage Docker secrets
  service     Manage services
  stack       Manage Docker stacks
  swarm       Manage Swarm
  system      Manage Docker
  trust       Manage trust on Docker images
  volume      Manage volumes

Commands:
  attach      Attach local standard input, output, and error streams to a running container
  build       Build an image from a Dockerfile
  commit      Create a new image from a container's changes
  cp          Copy files/folders between a container and the local filesystem
  create      Create a new container
  diff        Inspect changes to files or directories on a container's filesystem
  events      Get real time events from the server
  exec        Run a command in a running container
  export      Export a container's filesystem as a tar archive
  history     Show the history of an image
  images      List images
  import      Import the contents from a tarball to create a filesystem image
  info        Display system-wide information
  inspect     Return low-level information on Docker objects
  kill        Kill one or more running containers
  load        Load an image from a tar archive or STDIN
  login       Log in to a Docker registry
  logout      Log out from a Docker registry
  logs        Fetch the logs of a container
  pause       Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
  ps          List containers
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  rmi         Remove one or more images
  run         Run a command in a new container
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  search      Search the Docker Hub for images
  start       Start one or more stopped containers
  stats       Display a live stream of container(s) resource usage statistics
  stop        Stop one or more running containers
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
  top         Display the running processes of a container
  unpause     Unpause all processes within one or more containers
  update      Update configuration of one or more containers
  version     Show the Docker version information
  wait        Block until one or more containers stop, then print their exit codes

Run 'docker COMMAND --help' for more information on a command.

2023/02/08

用 gdb 找 Segmentation fault 的位置

編譯 elf 時不要 strip

STRIP := /usr/bin/true

產生 coredump

ulimit -c 設定 user 的 core上限, 記憶體足夠的情況下, 直接設定成無限. 

$ulimit -c unlimited

發生 Segmentation fault

讓程式發生 Segmentation fault, 如果有產生 core 檔, 會在 stdout 看到 Segmentation fault (core dumped).
core 文件會放在 /tmp/process_name.xxxxxx.core

預設存放路徑
UBUNTU 22.04:    /var/lib/apport/coredump

用 gdb 載入 core

$ gdb <elf> <core>

跑到錯誤點

如果一個 page 跑不到, 直接按 c 跑到錯誤點.
--Type <RET> for more, q to quit, c to continue without paging--

用 Back Trace (bt) 查看 call stack

如果 elf 經過 strip 減肥, 會看不到 symbol.

2022/02/14

SVN 筆記

checkout

    從 server 拉來本地, 簡寫 co
    checkout URL[@REV]... [PATH]
    例:
        svn co svn+ssh://repo/home/svn/mybranch mybranch 

    直接 checkout 指定版本
        svn co -r version svn+ssh://repo/home/svn/mybranch mybranch

commit

    將本地的修改推到 server, 簡寫 ci
    commit [PATH...]
    例:
        svn ci file1 file2 path1/file3

copy

    複製成新的 branch, 簡寫 cp
    copy SRC[@REV]... DST
    例:
        svn cp svn+ssh://repo/home/svn/mybranch svn+ssh://repo/home/svn/newbranch

status

    檔案狀態, 簡寫 st
    svn st [PATH...]
    例:
        svn st

merge

    合併,
    svn merge [-c M[,N...] | -r N:M ...] SOURCE[@REV] [TARGET_WCPATH]
    例: 把 trunk 的修改合併進來.
        svn merge svn+ssh://repo/trunk
    例: 退回 oldver. 完成後重新 commit. 可以保留原先變更的版本參考
        svn merge -r 123:115 svn+ssh://repo/develop
    例: 檔案退回 oldver. 完成後重新 commit 可以保留原先變更的版本參考
        svn merge -r 123:115 path/filename

update

    更新,
    svn update [PATH...]
    例:
        svn up
    例:
        svn up -r 115

cherry-pick

    摘櫻桃
    svn merge -r N:M SOURCE

    例: (從 repo/develop 摘取從 r98 到 r99 所做的變更)
        svn merge -r 98:99 svn+ssh://repo/develop

svn merge 出現下列訊息:

    svn local file obstruction incoming file add upon merge
        這是因為本地跟merge源個別 add 檔案造成的, 解決方法:
            先手動比較兩個版本的差異, 並將檔案修改到最終想要的版本. 然後執行
                svn resolve --accept working <file>
            意思是接受 working 的版本來解決衝突.