今まで避けてきた「CMake」に挑戦。動的ライブラリ(Windowsで云うDLL)とそれを呼び出す実行ファイルを作ってみる。
動的ライブラリはリンク時に指定するんじゃなく、dlopenを使う。なんかもう、こっちしかわからんくなってきた。
./study3.cpp
- #include <stdio.h>
- #include <stdint.h>
- #include <dlfcn.h>
- //#include "keeloq/include/keeloq.hpp"
-
- void *hdll;
- typedef uint32_t (*keeloq_enc_t)(uint32_t pr,uint64_t kr);
- typedef uint32_t (*keeloq_dec_t)(uint32_t cr,uint64_t kr);
-
- keeloq_enc_t keeloq_enc;
- keeloq_dec_t keeloq_dec;
-
- int main(int argc,char** argv){
- uint32_t pr=0xf741e2db;
- uint64_t kr=0x5cec6701b79fd949;
- uint32_t cr;
-
- hdll = dlopen("./keeloq/libkeeloq.so", RTLD_LAZY);
- if(hdll==NULL){return 1;}
- keeloq_enc=(keeloq_enc_t)dlsym(hdll,"keeloq_enc");
- keeloq_dec=(keeloq_dec_t)dlsym(hdll,"keeloq_dec");
-
- cr=keeloq_enc(pr,kr);
- printf("%08X\n",cr);
- pr=keeloq_dec(cr,kr);
- printf("%08X\n",pr);
- dlclose(hdll);
- return 0;
- }
./CMakeLists.txt
- cmake_minimum_required(VERSION 3.1)
- project(study3 CXX)
- add_subdirectory(./keeloq)
- add_executable(study3 study3.cpp)
- #target_link_libraries(study3 keeloq)
./keeloq/CMakeLists.txt
- cmake_minimum_required(VERSION 3.1)
- project(keeloq_lib
- VERSION 1.0.0
- DESCRIPTION "Keeloq library"
- LANGUAGES CXX)
- add_library(keeloq SHARED ./src/keeloq.cpp)
- #target_compile_features(keeloq PRIVATE cxx_std_11)
- #target_include_directories(keeloq SHARED ./include)
- set_target_properties(keeloq
- PROPERTIES
- VERSION ${PROJECT_VERSION})
./keeloq/include/keeloq.hpp
- #ifndef _KEELOQ_H_
- #define _KEELOQ_H_
- #include <stdint.h>
- extern "C" uint32_t keeloq_enc(uint32_t pr,uint64_t kr);
- extern "C" uint32_t keeloq_dec(uint32_t cr,uint64_t kr);
- #endif
./keeloq/src/keeloq.cpp
- #include <stdint.h>
- extern "C" uint32_t keeloq_enc(uint32_t pr,uint64_t kr){
- uint32_t nlf_table=0x3a5c742e;
- uint32_t nlf_arg;
- uint32_t nlf;
- uint32_t newz;
- uint32_t i;
- for(i=0;i<528;i++){
- nlf_arg=((pr&0x80000000)>>(31-4))
- |((pr&0x04000000)>>(26-3))
- |((pr&0x00100000)>>(20-2))
- |((pr&0x00000200)>>(9-1))
- |((pr&0x00000002)>>(1-0));
- nlf=((nlf_table>>nlf_arg)&0x00000001);
- newz=nlf^((pr&0x00010000)>>16)^(pr&0x00000001)^(uint32_t)(kr&0x0000000000000001);
- pr=(newz<<31)|(pr>>1);
- kr=((kr&0x0000000000000001)<<63)|(kr>>1);
- }
- return pr;
- }
- extern "C" uint32_t keeloq_dec(uint32_t cr,uint64_t kr){
- uint32_t nlf_table=0x3a5c742e;
- uint32_t nlf_arg;
- uint32_t nlf;
- uint32_t newz;
- uint32_t i;
- for(i=0;i<528;i++){
- nlf_arg=((cr&0x40000000)>>(30-4))
- |((cr&0x02000000)>>(25-3))
- |((cr&0x00080000)>>(19-2))
- |((cr&0x00000100)>>(8-1))
- |((cr&0x00000001)>>(0-0));
- nlf=((nlf_table>>nlf_arg)&0x00000001);
- newz=nlf^((cr&0x00008000)>>15)^((cr&0x80000000)>>31)^(uint32_t)((kr&0x0000000000008000)>>15);
- cr=(cr<<1)|(newz);
- kr=((kr&0x8000000000000000)>>63)|(kr<<1);
- }
- return cr;
- }
いつものいきなりな感じで。
・マングリングをさせないために関数にextern "C"をつけた。Windowsでは__stdcallってのもつけるけど、Linuxではいらんらしい。
・CMakeLists.txtでtarget_link_libraries(study3 keeloq)をコメント化。いらんはずなので。
・keeloq/CMakeLists.txtの"target_include_directories(keeloq SHARED ./include)"はあるとエラーが出るのでコメント化。意味はわかってないが。
、、、でけた。イイネCMake。
ビルド中にLinking sahred library libkeeloq.soって出ているのがあれ?って思うけど、今日は掘らない。
あ、お伝えしていなかったのですが、ちょっと古いPCを入手して、それでLinuxマシンをこさえたのでこの手の挑戦はLinuxでやてます。WSLでも同じようにできると思いますが、やっぱりLinuxマシンのほうが楽しいじゃん。なんとなく。