Guest User

Untitled

a guest
May 21st, 2018
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. /* ヘッダファイルのインクルード */
  2. #include <stdio.h> /* 標準入出力 */
  3. #include <openssl/bio.h> /* BIO */
  4. #include <openssl/err.h> /* エラー */
  5.  
  6. /* main関数の定義 */
  7. int main(void){
  8.  
  9. /* 変数の宣言と初期化. */
  10. BIO *buffer = NULL; /* バッファ用フィルタBIOへのポインタbufferをNULLに初期化. */
  11. BIO *file = NULL; /* ファイル用データシンクBIOへのポインタfileをNULLに初期化. */
  12.  
  13. /* ファイル用データシンクBIOの作成. */
  14. file = BIO_new_file("test.txt", "w"); /* 書き込み先のファイル用BIOポインタfileを作成. */
  15. if (file == NULL){ /* NULLなら */
  16.  
  17. /* エラー処理 */
  18. ERR_print_errors_fp(stderr); /* ERR_print_errors_fpでエラー出力. */
  19. return -1; /* -1を返す. */
  20.  
  21. }
  22.  
  23. /* fileを出力. */
  24. printf("file = %08x\n", file); /* printfでfileを出力. */
  25.  
  26. /* バッファ用フィルタBIOの作成. */
  27. buffer = BIO_new(BIO_f_buffer()); /* BIO_new()にBIO_f_buffer()の戻り値を渡すことでバッファ用フィルタBIOのbufferを作成. */
  28.  
  29. /* bufferを出力. */
  30. printf("buffer = %08x\n", buffer); /* printfでbufferを出力. */
  31.  
  32. /* bufferとfileを連結. */
  33. BIO_push(buffer, file); /* BIO_pushでbufferとfileを連結. */
  34.  
  35. /* bufferに"ABCDE"を書き込む. */
  36. BIO_write(buffer, "ABCDE", 5); /* BIO_writeでbufferに"ABCDE"を書き込む. */
  37.  
  38. /* fileに"XYZ"を書き込む. */
  39. BIO_write(file, "XYZ", 3); /* BIO_writeでfileに"XYZ"を書き込む. */
  40.  
  41. /* バッファをフラッシュしてファイルに書き込まれる. */
  42. BIO_flush(buffer); /* BIO_flushすることでbufferからfileにちゃんと書き込まれる. */
  43.  
  44. /* BIOの解放. */
  45. BIO_free_all(buffer); /* bufferとfileは連結しているのでBIO_free_allで両方解放される. */
  46.  
  47. /* プログラムの終了. */
  48. return 0; /* 0を返す. */
  49.  
  50. }
Add Comment
Please, Sign In to add comment