Ver Fonte

Merge pull request #16 from murphyzhao/v301-rtt

【同步升级】源 iotkit-embedded 软件包到 v3.0.1 版本
朱天龙 (Armink) há 6 anos atrás
pai
commit
dbe289b0de
100 ficheiros alterados com 19761 adições e 3135 exclusões
  1. 3 0
      README.md
  2. 284 116
      SConscript
  3. 7 3
      iotkit-embedded/.gitignore
  4. 0 158
      iotkit-embedded/CMakeLists.txt
  5. 1 0
      iotkit-embedded/LICENSE
  6. 10 160
      iotkit-embedded/README.md
  7. 1 0
      iotkit-embedded/certs/iot.mk
  8. 2 28
      iotkit-embedded/certs/root_ca.c
  9. 11 0
      iotkit-embedded/config.bat
  10. 0 0
      iotkit-embedded/external_libs/mbedtls/include/.gitignore
  11. 60 77
      iotkit-embedded/external_libs/mbedtls/include/mbedtls/aes.h
  12. 47 67
      iotkit-embedded/external_libs/mbedtls/include/mbedtls/asn1.h
  13. 10 26
      iotkit-embedded/external_libs/mbedtls/include/mbedtls/base64.h
  14. 96 112
      iotkit-embedded/external_libs/mbedtls/include/mbedtls/bignum.h
  15. 5 21
      iotkit-embedded/external_libs/mbedtls/include/mbedtls/bn_mul.h
  16. 5 21
      iotkit-embedded/external_libs/mbedtls/include/mbedtls/check_config.h
  17. 78 88
      iotkit-embedded/external_libs/mbedtls/include/mbedtls/cipher.h
  18. 5 23
      iotkit-embedded/external_libs/mbedtls/include/mbedtls/cipher_internal.h
  19. 9 32
      iotkit-embedded/external_libs/mbedtls/include/mbedtls/config.h
  20. 45 62
      iotkit-embedded/external_libs/mbedtls/include/mbedtls/ctr_drbg.h
  21. 29 43
      iotkit-embedded/external_libs/mbedtls/include/mbedtls/debug.h
  22. 47 65
      iotkit-embedded/external_libs/mbedtls/include/mbedtls/des.h
  23. 63 84
      iotkit-embedded/external_libs/mbedtls/include/mbedtls/ecp.h
  24. 37 55
      iotkit-embedded/external_libs/mbedtls/include/mbedtls/entropy.h
  25. 19 35
      iotkit-embedded/external_libs/mbedtls/include/mbedtls/entropy_poll.h
  26. 7 22
      iotkit-embedded/external_libs/mbedtls/include/mbedtls/error.h
  27. 35 53
      iotkit-embedded/external_libs/mbedtls/include/mbedtls/md.h
  28. 18 35
      iotkit-embedded/external_libs/mbedtls/include/mbedtls/md5.h
  29. 5 25
      iotkit-embedded/external_libs/mbedtls/include/mbedtls/md_internal.h
  30. 13 0
      iotkit-embedded/external_libs/mbedtls/include/mbedtls/net.h
  31. 22 39
      iotkit-embedded/external_libs/mbedtls/include/mbedtls/net_sockets.h
  32. 32 48
      iotkit-embedded/external_libs/mbedtls/include/mbedtls/oid.h
  33. 15 32
      iotkit-embedded/external_libs/mbedtls/include/mbedtls/pem.h
  34. 82 102
      iotkit-embedded/external_libs/mbedtls/include/mbedtls/pk.h
  35. 5 21
      iotkit-embedded/external_libs/mbedtls/include/mbedtls/pk_internal.h
  36. 30 46
      iotkit-embedded/external_libs/mbedtls/include/mbedtls/platform.h
  37. 132 149
      iotkit-embedded/external_libs/mbedtls/include/mbedtls/rsa.h
  38. 18 35
      iotkit-embedded/external_libs/mbedtls/include/mbedtls/sha1.h
  39. 20 37
      iotkit-embedded/external_libs/mbedtls/include/mbedtls/sha256.h
  40. 197 204
      iotkit-embedded/external_libs/mbedtls/include/mbedtls/ssl.h
  41. 13 30
      iotkit-embedded/external_libs/mbedtls/include/mbedtls/ssl_ciphersuites.h
  42. 14 31
      iotkit-embedded/external_libs/mbedtls/include/mbedtls/ssl_cookie.h
  43. 135 150
      iotkit-embedded/external_libs/mbedtls/include/mbedtls/ssl_internal.h
  44. 15 33
      iotkit-embedded/external_libs/mbedtls/include/mbedtls/timing.h
  45. 58 75
      iotkit-embedded/external_libs/mbedtls/include/mbedtls/x509.h
  46. 21 37
      iotkit-embedded/external_libs/mbedtls/include/mbedtls/x509_crl.h
  47. 75 92
      iotkit-embedded/external_libs/mbedtls/include/mbedtls/x509_crt.h
  48. 6 0
      iotkit-embedded/external_libs/mbedtls/iot.mk
  49. 0 0
      iotkit-embedded/external_libs/mbedtls/library/.gitignore
  50. 0 0
      iotkit-embedded/external_libs/mbedtls/library/Makefile
  51. 6 19
      iotkit-embedded/external_libs/mbedtls/library/aes.c
  52. 4 18
      iotkit-embedded/external_libs/mbedtls/library/asn1parse.c
  53. 7 19
      iotkit-embedded/external_libs/mbedtls/library/base64.c
  54. 8 19
      iotkit-embedded/external_libs/mbedtls/library/bignum.c
  55. 5 28
      iotkit-embedded/external_libs/mbedtls/library/cipher.c
  56. 5 23
      iotkit-embedded/external_libs/mbedtls/library/cipher_wrap.c
  57. 9 20
      iotkit-embedded/external_libs/mbedtls/library/ctr_drbg.c
  58. 352 0
      iotkit-embedded/external_libs/mbedtls/library/debug.c
  59. 7 19
      iotkit-embedded/external_libs/mbedtls/library/entropy.c
  60. 4 18
      iotkit-embedded/external_libs/mbedtls/library/entropy_poll.c
  61. 4 18
      iotkit-embedded/external_libs/mbedtls/library/error.c
  62. 5 23
      iotkit-embedded/external_libs/mbedtls/library/md.c
  63. 5 23
      iotkit-embedded/external_libs/mbedtls/library/md_wrap.c
  64. 4 18
      iotkit-embedded/external_libs/mbedtls/library/net_sockets.c
  65. 5 21
      iotkit-embedded/external_libs/mbedtls/library/oid.c
  66. 4 18
      iotkit-embedded/external_libs/mbedtls/library/pem.c
  67. 4 18
      iotkit-embedded/external_libs/mbedtls/library/pk.c
  68. 4 18
      iotkit-embedded/external_libs/mbedtls/library/pk_wrap.c
  69. 4 18
      iotkit-embedded/external_libs/mbedtls/library/pkparse.c
  70. 4 18
      iotkit-embedded/external_libs/mbedtls/library/platform.c
  71. 4 18
      iotkit-embedded/external_libs/mbedtls/library/rsa.c
  72. 7 19
      iotkit-embedded/external_libs/mbedtls/library/sha1.c
  73. 7 19
      iotkit-embedded/external_libs/mbedtls/library/sha256.c
  74. 5 21
      iotkit-embedded/external_libs/mbedtls/library/ssl_ciphersuites.c
  75. 4 18
      iotkit-embedded/external_libs/mbedtls/library/ssl_cli.c
  76. 4 18
      iotkit-embedded/external_libs/mbedtls/library/ssl_cookie.c
  77. 4 18
      iotkit-embedded/external_libs/mbedtls/library/ssl_tls.c
  78. 14 19
      iotkit-embedded/external_libs/mbedtls/library/timing.c
  79. 4 18
      iotkit-embedded/external_libs/mbedtls/library/x509.c
  80. 5 19
      iotkit-embedded/external_libs/mbedtls/library/x509_crt.c
  81. 2 0
      iotkit-embedded/external_libs/nghttp2/iot.mk
  82. 5363 0
      iotkit-embedded/external_libs/nghttp2/nghttp2.h
  83. 506 0
      iotkit-embedded/external_libs/nghttp2/nghttp2_buf.c
  84. 393 0
      iotkit-embedded/external_libs/nghttp2/nghttp2_buf.h
  85. 172 0
      iotkit-embedded/external_libs/nghttp2/nghttp2_callbacks.c
  86. 106 0
      iotkit-embedded/external_libs/nghttp2/nghttp2_callbacks.h
  87. 41 0
      iotkit-embedded/external_libs/nghttp2/nghttp2_debug.c
  88. 24 0
      iotkit-embedded/external_libs/nghttp2/nghttp2_debug.h
  89. 977 0
      iotkit-embedded/external_libs/nghttp2/nghttp2_frame.c
  90. 556 0
      iotkit-embedded/external_libs/nghttp2/nghttp2_frame.h
  91. 2317 0
      iotkit-embedded/external_libs/nghttp2/nghttp2_hd.c
  92. 413 0
      iotkit-embedded/external_libs/nghttp2/nghttp2_hd.h
  93. 212 0
      iotkit-embedded/external_libs/nghttp2/nghttp2_hd_huffman.c
  94. 58 0
      iotkit-embedded/external_libs/nghttp2/nghttp2_hd_huffman.h
  95. 4942 0
      iotkit-embedded/external_libs/nghttp2/nghttp2_hd_huffman_data.c
  96. 532 0
      iotkit-embedded/external_libs/nghttp2/nghttp2_helper.c
  97. 103 0
      iotkit-embedded/external_libs/nghttp2/nghttp2_helper.h
  98. 577 0
      iotkit-embedded/external_libs/nghttp2/nghttp2_http.c
  99. 78 0
      iotkit-embedded/external_libs/nghttp2/nghttp2_http.h
  100. 39 0
      iotkit-embedded/external_libs/nghttp2/nghttp2_int.h

+ 3 - 0
README.md

@@ -1,5 +1,8 @@
 # Ali IoTKit
 
+> 说明:  
+Ali IoTKit 软件包已经同步更新到 c-sdk-v3.0.1-269691d1b45b15fb9045a8eb178efa54b262aca1c-sdk.git 版本。本软件包中的文档教程未能及时更新,深表歉意,最新文档请移步阿里 [官方 Wikis](https://code.aliyun.com/edward.yangx/public-docs/wikis/user-guide/Linkkit_User_Manual) 和阿里 [官方文档](https://help.aliyun.com/product/93051.html?spm=a2c4g.11186623.6.540.393e492bC6TzC4)。
+
 ## 1. 介绍
 
 **ali-iotkit** 是 RT-Thread 移植的用于连接阿里云 IoT 平台的软件包。基础 SDK 是阿里提供的 [**iotkit-embedded C-SDK**](https://github.com/aliyun/iotkit-embedded)。

+ 284 - 116
SConscript

@@ -2,129 +2,297 @@ import os
 from building import *
 import rtconfig
 
-cwd  = GetCurrentDir()
+cwd     = GetCurrentDir()
 
-src  = []
+src     = []
 CPPPATH = []
-CPPDEFINES = []
-LOCAL_CCFLAGS = ''
 
-#sample
-if GetDepend(['PKG_USING_ALI_IOTKIT_MQTT_SAMPLE']):
-    src += Glob('samples/mqtt/mqtt-example.c')
+#
+# atm
+#
+if GetDepend(['ATM_ENABLED']):
+    src += Glob("iotkit-embedded/src/atm/at_api.c")
+    if GetDepend(['AT_TCP_ENABLED']):
+        src += Split("""
+        iotkit-embedded/src/atm/at_conn_mbox.c
+        iotkit-embedded/src/atm/at_conn_mgmt.c
+        iotkit-embedded/src/atm/at_tcp.c
+        """)
+    if GetDepend(['AT_MQTT_ENABLED']):
+        src += Glob("iotkit-embedded/src/atm/at_mqtt.c")
+    if GetDepend(['AT_PARSER_ENABLED']):
+        src += Glob("iotkit-embedded/src/atm/at_parser.c")
+
+    CPPPATH += [cwd + '/iotkit-embedded/src/atm']
+#### atm end ####
+
+#
+# infra
+#
+src += Glob('iotkit-embedded/src/infra/infra_defs.c')
+
+if GetDepend(['INFRA_AES']):
+    src += Glob('iotkit-embedded/src/infra/infra_aes.c')
+if GetDepend(['INFRA_CJSON']):
+    src += Glob('iotkit-embedded/src/infra/infra_cjson.c')
+if GetDepend(['INFRA_COMPAT']):
+    src += Glob('iotkit-embedded/src/infra/infra_compat.c')
+if GetDepend(['INFRA_HTTPC']):
+    src += Glob('iotkit-embedded/src/infra/infra_httpc.c')
+if GetDepend(['INFRA_JSON_PARSER']):
+    src += Glob('iotkit-embedded/src/infra/infra_json_parser.c')
+if GetDepend(['INFRA_LOG']):
+    src += Glob('iotkit-embedded/src/infra/infra_log.c')
+if GetDepend(['INFRA_MD5']):
+    src += Glob('iotkit-embedded/src/infra/infra_md5.c')
+if GetDepend(['INFRA_MEM_STATS']):
+    src += Glob('iotkit-embedded/src/infra/infra_mem_stats.c')
+if GetDepend(['INFRA_NET']):
+    src += Glob('iotkit-embedded/src/infra/infra_net.c')
+if GetDepend(['INFRA_PREAUTH']):
+    src += Glob('iotkit-embedded/src/infra/infra_preauth.c')
+if GetDepend(['INFRA_LOG_NETWORK_PAYLOAD']):
+    src += Glob('iotkit-embedded/src/infra/infra_prt_nwk_payload.c')
+if GetDepend(['INFRA_REPORT']):
+    src += Glob('iotkit-embedded/src/infra/infra_report.c')
+if GetDepend(['INFRA_SHA1']):
+    src += Glob('iotkit-embedded/src/infra/infra_sha1.c')
+if GetDepend(['INFRA_SHA256']):
+    src += Glob('iotkit-embedded/src/infra/infra_sha256.c')
+if GetDepend(['INFRA_STRING']):
+    src += Glob('iotkit-embedded/src/infra/infra_string.c')
+if GetDepend(['INFRA_TIMER']):
+    src += Glob('iotkit-embedded/src/infra/infra_timer.c')
+
+CPPPATH += [cwd + '/iotkit-embedded/src/infra']
+#### infra end ####
+
+#
+# mqtt
+#
+if GetDepend(['MQTT_COMM_ENABLED']):
+    src += Glob('iotkit-embedded/src/mqtt/*.c')
+    if GetDepend(['MQTT_DEFAULT_IMPL']):
+        src += Glob('iotkit-embedded/src/mqtt/impl/*.c')
+        CPPPATH += [cwd + '/iotkit-embedded/src/mqtt/impl']
+
+    CPPPATH += [cwd + '/iotkit-embedded/src/mqtt']
+#### mqtt end ####
+
+#
+# coap
+#
+if GetDepend(['COAP_COMM_ENABLED']):
+    if GetDepend(['COAP_CLIENT']):
+        src += Glob('iotkit-embedded/src/coap/client/*.c')
+        CPPPATH += [cwd + '/iotkit-embedded/src/coap/client']
+    if GetDepend(['COAP_PACKET']):
+        src += Glob('iotkit-embedded/src/coap/CoAPPacket/*.c')
+        CPPPATH += [cwd + '/iotkit-embedded/src/coap/CoAPPacket']
+    if GetDepend(['COAP_SERVER']):
+        src += Glob('iotkit-embedded/src/coap/server/*.c')
+        CPPPATH += [cwd + '/iotkit-embedded/src/coap/server']
+    CPPPATH += [cwd + '/iotkit-embedded/src/coap']
+#### coap end ####
+
+
+#
+# device bind
+#
+if GetDepend(['DEV_BIND_ENABLED']):
+    src += Glob("iotkit-embedded/src/dev_bind/impl/*.c")
+    src += Glob("iotkit-embedded/src/dev_bind/impl/awss_reset/*.c")
+    src += Glob("iotkit-embedded/src/dev_bind/impl/os/*.c")
+    CPPPATH += [cwd + '/iotkit-embedded/src/dev_bind']
+    CPPPATH += [cwd + '/iotkit-embedded/src/dev_bind/awss_reset']
+    CPPPATH += [cwd + '/iotkit-embedded/src/dev_bind/os']
+#### device bind ####
+
+
+#
+# device model
+#
+if GetDepend(['DEVICE_MODEL_ENABLED']):
+    src += Split("""
+    iotkit-embedded/src/dev_model/client/dm_client.c
+    iotkit-embedded/src/dev_model/client/dm_client_adapter.c
+    iotkit-embedded/src/dev_model/dm_api.c
+    iotkit-embedded/src/dev_model/dm_cota.c
+    iotkit-embedded/src/dev_model/dm_fota.c
+    iotkit-embedded/src/dev_model/dm_ipc.c
+    iotkit-embedded/src/dev_model/dm_manager.c
+    iotkit-embedded/src/dev_model/dm_message_cache.c
+    iotkit-embedded/src/dev_model/dm_message.c
+    iotkit-embedded/src/dev_model/dm_opt.c
+    iotkit-embedded/src/dev_model/dm_ota.c
+    iotkit-embedded/src/dev_model/dm_msg_process.c
+    iotkit-embedded/src/dev_model/dm_utils.c
+    iotkit-embedded/src/dev_model/iotx_cm_mqtt.c
+    iotkit-embedded/src/dev_model/impl_linkkit.c
+    iotkit-embedded/src/dev_model/iotx_cm.c
+    """)
+
+    if GetDepend(['LOG_REPORT_TO_CLOUD']):
+        src += Split("""
+        iotkit-embedded/src/dev_model/dm_log_report.c
+        """)
+
+    # ALCS(alink local communication service) is a communication between phone and device
+    if GetDepend(['ALCS_ENABLED']):
+        src += Glob("iotkit-embedded/src/dev_model/alcs/*.c")
+        CPPPATH += [cwd + '/iotkit-embedded/src/dev_model/alcs']
+
+    if GetDepend(['COAP_COMM_ENABLED']):
+        src += Glob("iotkit-embedded/src/dev_model/iotx_cm_coap.c")
+
+    CPPPATH += [cwd + '/iotkit-embedded/src/dev_model']
+    CPPPATH += [cwd + '/iotkit-embedded/src/dev_model/client']
+    CPPPATH += [cwd + '/iotkit-embedded/src/dev_model/server']
+#### device model end ####
+
+#
+# device sign
+#
+if GetDepend(['DEV_SIGN']):
+    src += Glob('iotkit-embedded/src/dev_sign/dev_sign_mqtt.c')
+    CPPPATH += [cwd + '/iotkit-embedded/src/dev_sign']
+#### device sign end ####
+
+#
+# device reset
+#
+if GetDepend(['DEV_RESET']):
+    src += Glob('iotkit-embedded/src/dev_reset/dev_reset.c')
+    CPPPATH += [cwd + '/iotkit-embedded/src/dev_reset']
+#### device reset end ####
+
+#
+# dynamic register
+#
+if GetDepend(['DYNAMIC_REGISTER']):
+    src += Glob("iotkit-embedded/src/dynamic_register/*.c")
+    CPPPATH += [cwd + '/iotkit-embedded/src/dynamic_register']
+#### dynamic register end ####
+
+#
+# http
+#
+if GetDepend(['HTTP_COMM_ENABLED']):
+    src += Glob("iotkit-embedded/src/http/*.c")
+    CPPPATH += [cwd + '/iotkit-embedded/src/http']
+#### http end ####
+
+#
+# http2
+#
+if GetDepend(['HTTP2_COMM_ENABLED']):
+    src += Glob("iotkit-embedded/src/http2/http2_api.c")
+    src += Glob("iotkit-embedded/src/http2/iotx_http2.c")
+    
+    if GetDepend(['FS_ENABLED']):
+        src += Glob("iotkit-embedded/src/http2/http2_upload_api.c")
+    CPPPATH += [cwd + '/iotkit-embedded/src/http2']
+#### http end ####
+
+#
+# ota
+#
+if GetDepend(['OTA_ENABLED']):
+    src += Split("""
+    iotkit-embedded/src/ota/iotx_ota.c
+    iotkit-embedded/src/ota/ota_fetch.c
+    iotkit-embedded/src/ota/ota_lib.c
+    """)
+    CPPPATH += [cwd + '/iotkit-embedded/src/ota']
+#### ota end ####
+
+#
+# wifi provision
+#
+if GetDepend(['WIFI_PROVISION_ENABLED']):
+    if GetDepend(['AWSS_SUPPORT_SMARTCONFIG']):
+        src += Glob("iotkit-embedded/src/wifi_provision/smartconfig/*.c")
+        CPPPATH += [cwd + '/iotkit-embedded/src/wifi_provision/smartconfig']
+    if GetDepend(['AWSS_SUPPORT_SMARTCONFIG_WPS']):
+        src += Glob("iotkit-embedded/src/wifi_provision/p2p/*.c")
+        CPPPATH += [cwd + '/iotkit-embedded/src/wifi_provision/p2p']
+
+    if GetDepend(['AWSS_SUPPORT_ZEROCONFIG']):
+        src += Glob("iotkit-embedded/src/wifi_provision/zero_config/*.c")
+        CPPPATH += [cwd + '/iotkit-embedded/src/wifi_provision/zero_config']
+
+    if GetDepend(['AWSS_SUPPORT_AHA']):
+        src += Glob("iotkit-embedded/src/wifi_provision/phone_ap/*.c")
+        CPPPATH += [cwd + '/iotkit-embedded/src/wifi_provision/phone_ap']
+    if GetDepend(['AWSS_SUPPORT_ADHA']):
+        src += Glob("iotkit-embedded/src/wifi_provision/router_ap/*.c")
+        CPPPATH += [cwd + '/iotkit-embedded/src/wifi_provision/router_ap']
+
+    if GetDepend(['AWSS_SUPPORT_DEV_AP']):
+        src += Glob("iotkit-embedded/src/wifi_provision/dev_ap/*.c")
+        CPPPATH += [cwd + '/iotkit-embedded/src/wifi_provision/dev_ap']
+
+    if GetDepend(['AWSS_FRAMEWORKS']):
+        src += Glob("iotkit-embedded/src/wifi_provision/frameworks/*.c")
+        src += Glob("iotkit-embedded/src/wifi_provision/frameworks/*/*.c")
+        CPPPATH += [cwd + '/iotkit-embedded/src/wifi_provision/frameworks']
+        CPPPATH += [cwd + '/iotkit-embedded/src/wifi_provision/frameworks/aplist']
+        CPPPATH += [cwd + '/iotkit-embedded/src/wifi_provision/frameworks/ieee80211']
+        CPPPATH += [cwd + '/iotkit-embedded/src/wifi_provision/frameworks/statics']
+        CPPPATH += [cwd + '/iotkit-embedded/src/wifi_provision/frameworks/utils']
+
+    CPPPATH += [cwd + '/iotkit-embedded/src/wifi_provision']
+#### wifi provision end ####
+
+#
+# wrappers | port
+#
+src += Split("""
+ports/rtthread/HAL_OS_rtthread.c
+ports/rtthread/HAL_TCP_rtthread.c
+ports/rtthread/HAL_UDP_rtthread.c
+ports/wrapper.c
+""")
+
+if GetDepend(['SUPPORT_TLS']) or GetDepend(['COAP_DTLS_SUPPORT']):
+    src += Glob('iotkit-embedded/certs/root_ca.c')
+if GetDepend(['SUPPORT_TLS']):
+    src += Glob('ports/tls/mbedtls/HAL_TLS_mbedtls.c')
+if GetDepend(['COAP_DTLS_SUPPORT']):
+    src += Glob('ports/tls/mbedtls/HAL_DTLS_mbedtls.c')
+    
+if GetDepend(['HAL_CRYPTO']):
+    src += Glob('ports/rtthread/HAL_Crypt_rtthread.c')
+
+CPPPATH += [cwd + '/iotkit-embedded/wrappers']
+#### wrappers | port end ####
+
+#
+# samples
+#
+if GetDepend(['PKG_USING_ALI_IOTKIT_DEV_MODEL_SAMPLE']):
+    src += Split("""
+    samples/dev_model/cJSON.c
+    samples/dev_model/linkkit_example_solo.c
+    """)
+    CPPPATH += [cwd + '/iotkit-embedded/samples/dev_model']
 
-if GetDepend(['PKG_USING_ALI_IOTKIT_OTA']):
-	src += Glob('samples/ota/ota_mqtt-example.c')
+if GetDepend(['PKG_USING_ALI_IOTKIT_MQTT_SAMPLE']):
+    src += Split("""
+    samples/mqtt/mqtt-example.c
+    """)
 
 if GetDepend(['PKG_USING_ALI_IOTKIT_COAP_SAMPLE']):
-	src += Glob('samples/coap/coap-example.c')
-
-#src/cmp, need to enable CMP_ENABLED
-#src += Glob('iotkit-embedded/src/cmp/Link-CMP/src/*.c')
-#CPPPATH += [cwd + '/iotkit-embedded/src/cmp/Link-CMP']
-#CPPPATH += [cwd + '/iotkit-embedded/src/cmp/Link-CMP/inc']
-
-#src/coap
-src += Glob('iotkit-embedded/src/coap/*.c')
-
-#packages/iot-coap-c
-src += Glob('iotkit-embedded/src/packages/iot-coap-c/*.c')
-CPPPATH += [cwd + '/iotkit-embedded/src/packages/iot-coap-c']
-
-#src/dm
-
-#src/http, need to enable HTTP_COMM_ENABLED
-#src += Glob('iotkit-embedded/src/http/*.c')
-
-#src/import
-
-#src/log
-src += Glob('iotkit-embedded/src/log/LITE-log/*.c')
-CPPPATH += [cwd + '/iotkit-embedded/src/log/LITE-log']
-
-#src/mqtt
-if GetDepend(['PKG_USING_ALI_IOTKIT_MQTT']):
-	src += Glob('iotkit-embedded/src/mqtt/Link-MQTT/*.c')
-	src += Glob('iotkit-embedded/src/mqtt/Link-MQTT/MQTTPacket/*.c')
-	CPPPATH += [cwd + '/iotkit-embedded/src/mqtt/Link-MQTT']
-	CPPPATH += [cwd + '/iotkit-embedded/src/mqtt/Link-MQTT/MQTTPacket']
-
-#src/ota
-if GetDepend(['PKG_USING_ALI_IOTKIT_OTA']):
-    src += Glob('iotkit-embedded/src/ota/Link-OTA/src/*.c')
-    CPPPATH += [cwd + '/iotkit-embedded/src/ota/Link-OTA']
-
-SrcRemove(src, 'iotkit-embedded/src/ota/Link-OTA/src/ota_lib.c')   # have been include by ota.c
-SrcRemove(src, 'iotkit-embedded/src/ota/Link-OTA/src/ota_mqtt.c')  # have been include by ota.c
-SrcRemove(src, 'iotkit-embedded/src/ota/Link-OTA/src/ota_coap.c')  # have been include by ota.c
-SrcRemove(src, 'iotkit-embedded/src/ota/Link-OTA/src/ota_fetch.c') # have been include by ota.c
-
-#src/cota
-#src += Glob('iotkit-embedded/src/cota/*.c')
-
-#src/fota
-#src += Glob('iotkit-embedded/src/fota/*.c')
-
-#src/packages
-src += Glob('iotkit-embedded/src/packages/LITE-utils/*.c')
-CPPPATH += [cwd + '/iotkit-embedded/src/packages/LITE-utils']
-
-SrcRemove(src, 'iotkit-embedded/src/packages/LITE-utils/lite-utils_prog.c')
-
-#src/platform
-#src/scripts
-
-#src/sdk-tests
-#src/shadow
-#src/subdev
-
-#src/system
-src += Glob('iotkit-embedded/src/system/iotkit-system/src/*.c')
-CPPPATH += [cwd + '/iotkit-embedded/src/system/iotkit-system']
-
-#src/tfs
-#src/tls
-
-#src/utils
-src += Glob('iotkit-embedded/src/utils/misc/*.c')
-src += Glob('iotkit-embedded/src/utils/digest/*.c')
-CPPPATH += [cwd + '/iotkit-embedded/src/utils/misc']
-CPPPATH += [cwd + '/iotkit-embedded/src/utils/digest']
-
-#ports
-src += Glob('ports/rtthread/*.c')
-
-if GetDepend(['PKG_USING_ALI_IOTKIT_MQTT_TLS']):
-	src += Glob('ports/ssl/mbedtls/*.c')
-
-#src/sdk-impl
-CPPPATH += [cwd + '/iotkit-embedded/src/sdk-impl']
-CPPPATH += [cwd + '/iotkit-embedded/src/sdk-impl/exports']
-CPPPATH += [cwd + '/iotkit-embedded/src/sdk-impl/imports']
-
-if GetDepend(['PKG_USING_ALI_IOTKIT_MQTT']):
-	CPPDEFINES += ['MQTT_COMM_ENABLED']
-	if GetDepend(['PKG_USING_ALI_IOTKIT_MQTT_DIRECT']):
-		CPPDEFINES += ['MQTT_DIRECT']
-	if not GetDepend(['PKG_USING_ALI_IOTKIT_MQTT_TLS']):
-		CPPDEFINES += ['IOTX_WITHOUT_TLS']
-
-if GetDepend(['PKG_USING_ALI_IOTKIT_COAP']):
-	CPPDEFINES += ['COAP_COMM_ENABLED']
-	if GetDepend(['PKG_USING_ALI_IOTKIT_COAP_DTLS']):
-		CPPDEFINES += ['COAP_DTLS_SUPPORT']
-
-# OTA_SIGNAL_CHANNEL: 1-mqtt; 2:coap; 4:http
-if GetDepend(['PKG_USING_ALI_IOTKIT_MQTT_OTA']):
-	CPPDEFINES += ['SERVICE_OTA_ENABLED', 'OTA_SIGNAL_CHANNEL=1']
-
-if GetDepend(['PKG_USING_ALI_IOTKIT_COAP_OTA']):
-	CPPDEFINES += ['SERVICE_OTA_ENABLED', 'OTA_SIGNAL_CHANNEL=2']
+    src += Split("""
+    samples/coap/coap_example.c
+    """)
 
-CPPDEFINES += ['IOTX_NET_INIT_WITH_PK_EXT', '_PLATFORM_IS_RTTHREAD_', 'IOTX_WITHOUT_ITLS']
+if GetDepend(['PKG_USING_ALI_IOTKIT_OTA_SAMPLE']):
+    src += Split("""
+    samples/ota/ota_mqtt-example.c
+    """)
+#### samples end ####
 
-if rtconfig.CROSS_TOOL == 'gcc' :
-	CPPDEFINES += ['IOTX_PRJ_VERSION=\\"V2.10\\"']
+group = DefineGroup('ali-iotkit', src, depend = ['PKG_USING_ALI_IOTKIT'], CPPPATH = CPPPATH)
 
-group = DefineGroup('ali-iotkit', src, depend = ['PKG_USING_ALI_IOTKIT'], CPPPATH = CPPPATH, LOCAL_CCFLAGS = LOCAL_CCFLAGS, CPPDEFINES = CPPDEFINES)
 Return('group')

+ 7 - 3
iotkit-embedded/.gitignore

@@ -1,5 +1,3 @@
-*.so
-*.exe
 .settings/
 *.cproject
 *.project
@@ -20,9 +18,15 @@ ltmain.sh
 .config
 output/
 ota.bin
+src/packages/*.git/hooks/*.sample
 vs_build/
 .vs/
-html/
+doc/html/
 compile.log
 CMakeLists.txt.user
 qt_build/
+.vscode/
+GPATH
+GRTAGS
+GTAGS
+iotx-sdk-c-test/

+ 0 - 158
iotkit-embedded/CMakeLists.txt

@@ -1,158 +0,0 @@
-########################################################################
-# prevent in-tree builds
-########################################################################
-if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR})
-    message(FATAL_ERROR "not allowded in-tree build")
-endif(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR})
-
-########################################################################
-# project setup
-########################################################################
-cmake_minimum_required(VERSION 2.8)
-project(iotx-sdk-c)
-
-list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/modules")
-include(iotx-sdk-version)
-
-########################################################################
-# options
-########################################################################
-option(FEATURE_GIT_CLONE_BEFORE_BUILD     "git clone repos in packages folder when cmake configs"   OFF)
-option(FEATURE_MQTT_COMM_ENABLED          "MQTT communication enabled or not"                        ON)
-option(FEATURE_MQTT_DIRECT                "MQTT direct connection enabled or not"                    ON)
-option(FEATURE_MQTT_DIRECT_NOTLS          "MQTT direct connection w/o tls enabled or not"           OFF)
-option(FEATURE_COAP_COMM_ENABLED          "coap communication enabled or not"                        ON)
-option(FEATURE_HTTP_COMM_ENABLED          "HTTP communication enabled or not"                        ON)
-option(FEATURE_MQTT_SHADOW                "MQTT shadow enabled or not"     ${FEATURE_MQTT_COMM_ENABLED})
-option(FEATURE_COAP_DTLS_SUPPORT          "coap w/ dtls support or not"    ${FEATURE_COAP_COMM_ENABLED})
-option(FEATURE_SUBDEVICE_ENABLED          "subdev enabled or not"                                   OFF)
-option(FEATURE_CLOUD_CONN_ENABLED         "cloud connection enabled or not"                         OFF)
-option(FEATURE_CMP_ENABLED                "cmp enabled or not"                                       ON)
-option(FEATURE_DM_ENABLED                 "dm & linkkit enabled or not"                              ON)
-option(FEATURE_SERVICE_OTA_ENABLED        "ota enabled or not"                                       ON)
-option(FEATURE_SERVICE_COTA_ENABLED       "config ota enabled or not"                               OFF)
-option(FEATURE_SUPPORT_PRODUCT_SECRET     "support via product_secret get device_secret"            OFF)
-#option(FEATURE_OTA_FETCH_CHANNEL         "specify ota fetch channel"                                ON)HTTP
-#option(FEATURE_OTA_SIGNAL_CHANNEL        "specify ota signal channel"                               ON)MQTT
-set(GCC_ARCH native CACHE STRING "GCC compile for specific architecture.")
-
-########################################################################
-# Compiler specific setup
-########################################################################
-add_definitions(-DOTA_SIGNAL_CHANNEL=1)
-add_definitions(-DFORCE_SSL_VERIFY)
-add_definitions(-DUSING_UTILS_JSON)
-add_definitions(-DLITE_THING_MODEL)
-
-if(FEATURE_MQTT_DIRECT)
-    add_definitions(-DMQTT_DIRECT)
-endif(FEATURE_MQTT_DIRECT)
-
-add_definitions(-DUSING_SHA1_IN_HMAC)
-
-if(FEATURE_MQTT_COMM_ENABLED)
-    add_definitions(-DMQTT_COMM_ENABLED)
-endif(FEATURE_MQTT_COMM_ENABLED)
-
-if(FEATURE_SUBDEVICE_ENABLED)
-    add_definitions(-DSUBDEVICE_ENABLED)
-endif(FEATURE_SUBDEVICE_ENABLED)
-
-if(FEATURE_CMP_ENABLED)
-    add_definitions(-DCMP_ENABLED)
-endif(FEATURE_CMP_ENABLED)
-add_definitions(-DCMP_SUPPORT_TOPIC_DISPATCH)
-
-if(FEATURE_DM_ENABLED)
-    add_definitions(-DDM_ENABLED)
-    add_definitions(-DDEVICEINFO_ENABLED)
-endif(FEATURE_DM_ENABLED)
-
-if(FEATURE_SERVICE_OTA_ENABLED)
-    add_definitions(-DSERVICE_OTA_ENABLED)
-    if(FEATURE_SERVICE_COTA_ENABLED)
-        add_definitions(-DSERVICE_COTA_ENABLED)
-    endif(FEATURE_SERVICE_COTA_ENABLED)
-endif(FEATURE_SERVICE_OTA_ENABLED)
-
-if(FEATURE_SUPPORT_PRODUCT_SECRET)
-    add_definitions(-DSUPPORT_PRODUCT_SECRET)
-endif(FEATURE_SUPPORT_PRODUCT_SECRET)
-
-add_definitions(-DIOTX_WITHOUT_ITLS)
-add_definitions(-DIOTX_NET_INIT_WITH_PK_EXT)
-
-set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=${GCC_ARCH} -Wall -Wno-comment -Wno-write-strings -Wno-format-extra-args -Winline -Wno-unused-result -Wno-format")
-########################################################################
-# add -fPIC property to all targets
-########################################################################
-set(CMAKE_POSITION_INDEPENDENT_CODE ON)
-
-########################################################################
-# print project summary
-########################################################################
-message(STATUS "---------------------------------------------")
-message(STATUS "project name:\t" ${PROJECT_NAME})
-message(STATUS "source dir:\t" ${PROJECT_SOURCE_DIR})
-message(STATUS "binary dir:\t" ${PROJECT_BINARY_DIR})
-message(STATUS "system processor:\t" ${CMAKE_SYSTEM_PROCESSOR})
-message(STATUS "c compiler:\t" ${CMAKE_C_COMPILER})
-message(STATUS "system platform:\t" ${CMAKE_SYSTEM})
-message(STATUS "c compiler options:\t" ${CMAKE_C_FLAGS})
-
-if(WIN32)
-    message(STATUS "windows compiling...")
-    add_definitions(-D_PLATFORM_IS_WINDOWS_)
-else(WIN32)
-    message(STATUS "linux compiling...")
-    add_definitions( -D_PLATFORM_IS_LINUX_)
-endif(WIN32)
-message(STATUS "iotx sdk version:\t" ${iotx_sdk_version})
-message(STATUS "---------------------------------------------")
-
-########################################################################
-# git clone integrated repos
-########################################################################
-if(FEATURE_GIT_CLONE_BEFORE_BUILD)
-file(REMOVE_RECURSE ${PROJECT_SOURCE_DIR}/src/packages/LITE-log)
-file(REMOVE_RECURSE ${PROJECT_SOURCE_DIR}/src/packages/mbedtls-in-iotkit)
-file(REMOVE_RECURSE ${PROJECT_SOURCE_DIR}/src/packages/iotkit-system)
-file(REMOVE_RECURSE ${PROJECT_SOURCE_DIR}/src/packages/Link-MQTT)
-file(REMOVE_RECURSE ${PROJECT_SOURCE_DIR}/src/packages/Link-OTA)
-file(REMOVE_RECURSE ${PROJECT_SOURCE_DIR}/src/packages/Link-CMP)
-execute_process(COMMAND git clone ${PROJECT_SOURCE_DIR}/src/packages/LITE-log.git ${PROJECT_SOURCE_DIR}/src/packages/LITE-log)
-execute_process(COMMAND git clone ${PROJECT_SOURCE_DIR}/src/packages/mbedtls-in-iotkit.git ${PROJECT_SOURCE_DIR}/src/packages/mbedtls-in-iotkit)
-execute_process(COMMAND git clone ${PROJECT_SOURCE_DIR}/src/packages/iotkit-system.git ${PROJECT_SOURCE_DIR}/src/packages/iotkit-system)
-execute_process(COMMAND git clone ${PROJECT_SOURCE_DIR}/src/packages/Link-MQTT.git ${PROJECT_SOURCE_DIR}/src/packages/Link-MQTT)
-execute_process(COMMAND git clone ${PROJECT_SOURCE_DIR}/src/packages/Link-OTA.git ${PROJECT_SOURCE_DIR}/src/packages/Link-OTA)
-execute_process(COMMAND git clone ${PROJECT_SOURCE_DIR}/src/packages/Link-CMP.git ${PROJECT_SOURCE_DIR}/src/packages/Link-CMP)
-endif(FEATURE_GIT_CLONE_BEFORE_BUILD)
-
-include_directories(${PROJECT_SOURCE_DIR}/src/sdk-impl)
-include_directories(${PROJECT_SOURCE_DIR}/src/sdk-impl/imports)
-include_directories(${PROJECT_SOURCE_DIR}/src/sdk-impl/exports)
-include_directories(${PROJECT_SOURCE_DIR}/src/packages/mbedtls-in-iotkit/include)
-include_directories(${PROJECT_SOURCE_DIR}/src/packages/LITE-log)
-include_directories(${PROJECT_SOURCE_DIR}/src/packages/LITE-utils)
-include_directories(${PROJECT_SOURCE_DIR}/src/packages/Link-MQTT)
-include_directories(${PROJECT_SOURCE_DIR}/src/packages/Link-OTA)
-include_directories(${PROJECT_SOURCE_DIR}/src/packages/Link-CMP/inc)
-include_directories(${PROJECT_SOURCE_DIR}/src/packages/iot-coap-c)
-include_directories(${PROJECT_SOURCE_DIR}/src/packages/iotkit-system)
-include_directories(${PROJECT_SOURCE_DIR}/src/packages/iot-coap-c)
-include_directories(${PROJECT_SOURCE_DIR}/src/utils/digest)
-include_directories(${PROJECT_SOURCE_DIR}/src/utils/misc)
-include_directories(${PROJECT_SOURCE_DIR}/src/tfs)
-if(FEATURE_SUBDEVICE_ENABLED)
-include_directories(${PROJECT_SOURCE_DIR}/src/subdev)
-endif(FEATURE_SUBDEVICE_ENABLED)
-if(FEATURE_DM_ENABLED)
-include_directories(${PROJECT_SOURCE_DIR}/src/dm/include)
-endif(FEATURE_DM_ENABLED)
-include_directories(${PROJECT_SOURCE_DIR}/src/import/linux/include)
-
-########################################################################
-# Add the subdirectories
-########################################################################
-add_subdirectory(src)
-add_subdirectory(sample)

+ 1 - 0
iotkit-embedded/LICENSE

@@ -199,3 +199,4 @@
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
+

+ 10 - 160
iotkit-embedded/README.md

@@ -1,172 +1,22 @@
-# 阿里云物联网套件
+# C-SDK 简介
 
-物联网套件是阿里云专门为物联网领域的开发人员推出的, 其目的是帮助开发者搭建安全且性能强大的数据通道, 方便终端(如传感器, 执行器, 嵌入式设备或智能家电等等)和云端的双向通信.
+设备厂商在设备上集成 `C-SDK` 后, 可以将设备安全的接入到阿里云IoT物联网平台, 从而使设备可以被阿里云IoT物联网平台进行管理
 
--   **[官方代码首页](https://github.com/aliyun/iotkit-embedded)**
--   **[官方维基首页](https://github.com/aliyun/iotkit-embedded/wiki)**
+设备需要支持TCP/IP协议栈或串口通信, 以及C99标准的C库才能集成SDK, zigbee/433/KNX这样的非IP设备需要通过网关设备接入到阿里云IoT物联网平台, 网关设备需要集成C-SDK
 
-# 快速开始
-
-本节描述如何申请自己的设备, 并结合本SDK快速体验该设备通过`MQTT`+`TLS/SSL`协议连接到阿里云, 上报和接收业务报文. 关于SDK的更多使用方式, 请访问[官方WiKi](https://github.com/aliyun/iotkit-embedded/wiki)
-
-> 实现原理:
->
-> `MQTT协议`(`Message Queuing Telemetry Transport`, 消息队列遥测传输)是IBM开发的一个即时通讯协议, 是为大量计算能力有限, 且工作在低带宽, 不可靠的网络的远程传感器和控制设备通讯而设计的协议
->
-> 利用MQTT协议是一种基于二进制消息的发布/订阅编程模式的消息协议, 下面的应用程序先在阿里云IoT平台订阅(`Subscribe`)一个`Topic`成功, 然后自己向该`Topic`做发布(`Publish`)动作
-> 阿里云IoT平台收到之后, 就会原样推送回这个应用程序, 因为该程序之前已经通过订阅(`Subscribe`)动作成为该`Topic`的一个接收者, 发布到这个`Topic`上的任何消息, 都会被推送到已订阅该`Topic`的所有终端上
-
-## 一. 开发环境准备
-
-#### **1. 安装Ubuntu16.04**
-
-本SDK的编译环境是`64位`的`Ubuntu16.04`, 在其它Linux上尚未测试过, 所以推荐安装与阿里开发者一致的发行版
-
-如果您使用`Windows`操作系统, 建议安装虚拟机软件`Virtualbox`, 下载地址: [https://www.virtualbox.org/wiki/Downloads](https://www.virtualbox.org/wiki/Downloads)
-
-然后安装64位的desktop版本`Ubuntu 16.04.x LTS`, 下载地址: [https://www.ubuntu.com/download/desktop](https://www.ubuntu.com/download/desktop)
-
-#### **2. 安装必备软件**
-
-本SDK的开发编译环境使用如下软件: `make-4.1`, `git-2.7.4`, `gcc-5.4.0`, `gcov-5.4.0`, `lcov-1.12`, `bash-4.3.48`, `tar-1.28`, `mingw-5.3.1`
-
-可使用如下命令行安装必要的软件:
-
-    apt-get install -y build-essential make git gcc
-
-## 二. 在控制台创建设备
-
-#### **1. 注册/登录阿里云账号**
-
-访问阿里云[登录页面](https://account.aliyun.com/login/login.htm), 点击[免费注册](https://account.aliyun.com/register/register.htm), 免费获得一个阿里云账号. 若您已有账号, 可直接登录
-
-#### **2. 访问物联网套件控制台**
-
-登入之后, 鼠标悬停在**产品**上, 弹出层叠菜单
-
-![image](https://raw.githubusercontent.com/wiki/aliyun/iotkit-embedded/pics/iotconsole-product.png)
-
-然后向下滚动页面, 点击**物联网套件**
-
-![image](https://raw.githubusercontent.com/wiki/aliyun/iotkit-embedded/pics/iotconsole-iotkit.png)
-
-或直接访问[https://www.aliyun.com/product/iot](https://www.aliyun.com/product/iot), 之后点击**立即开通**, 或者**管理控制台**, 登入[控制台主界面](https://iot.console.aliyun.com/)
-
-#### **3. 创建产品和设备**
-
-点击页面右上角的**创建产品**按钮, 创建一个品类, 然后在左侧导航栏点击**设备管理**, 再到页面右侧点**添加设备**, 创建该品类下的一个设备, 如下图则得到创建成功后的**设备标识三元组**
 
-- `productKey`: 标识产品的品类
-- `deviceName`: 标识品类下的具体设备
-- `deviceSecret`: 该设备的密钥, 需填写到SDK中, 用于连接阿里云服务器时完成认证
-
-![image](https://raw.githubusercontent.com/wiki/aliyun/iotkit-embedded/pics/iotconsole-devinfo.png)
-
-#### **4. 创建可订阅可发布的Topic**
-
-点击左侧导航栏的**消息通信**, 再到页面右侧点**定义Topic类**, 创建一个新的`/${productKey}/${deviceName}/data`, 并设置为**可订阅可发布**权限
-
-![image](https://raw.githubusercontent.com/wiki/aliyun/iotkit-embedded/pics/iotconsole-subpub.png)
-
-## 三. 编译样例程序
-
-#### **1. 下载SDK**
-
-登录Linux, 运行如下命令从github克隆代码, 或者访问最新地址[下载页面](https://github.com/aliyun/iotkit-embedded/releases/latest), **将下载到的压缩包在Linux上解压缩**
-
-    $ git clone https://github.com/aliyun/iotkit-embedded
-
-#### **2. 填入设备信息**
-
-编辑文件`sample/mqtt/mqtt-example.c`, 编辑如下代码段, 填入之前**创建产品和设备**步骤中得到的**设备标识三元组**:
-
-![image](https://raw.githubusercontent.com/wiki/aliyun/iotkit-embedded/pics/sdk-devinfo.png)
-
-#### **3. 编译SDK产生样例程序**
-
-运行如下命令:
-
-    $ make distclean
-    $ make
-
-编译成功完成后, 生成的样例程序在当前目录的`output/release/bin`目录下:
-
-    $ tree output/release
-    output/release
-    ├── bin
-    │   ├── coap-example
-    │   ├── http-example
-    │   ├── mqtt-example
-    │   ├── mqtt_rrpc-example
-    │   ├── ota_mqtt-example
-    │   ├── sdk-testsuites
-    │   ├── shadow-testsuites
-    │   └── subdev-example
-    ...
-    ...
-
-## 四. 运行样例程序
-
-#### **1. 执行样例程序**
-
-    $ ./output/release/bin/mqtt-example
-    [inf] iotx_device_info_init(40): device_info created successfully!
-    [dbg] iotx_device_info_set(50): start to set device info!
-    [dbg] iotx_device_info_set(64): device_info set successfully!
-    [dbg] _calc_hmac_signature(57): | source: clientId2UCRZpAbCGC.ExampleDevdeviceNameExampleDevproductKey2UCRZpAbCGCtimestamp2524608000000 (93)
-    [dbg] _calc_hmac_signature(58): | secret: fbh47lGBSayncmTHEjF1E5x4CZdeJTO9 (32)
-    [dbg] _calc_hmac_signature(61): | method: hmacsha1
-    [dbg] _calc_hmac_signature(74): | signature: 326a4a6ed38b1bd5ddb6a5d11d27928bfb5a62d0 (40)
-    [dbg] guider_print_dev_guider_info(236): ....................................................
-    [dbg] guider_print_dev_guider_info(237):           ProductKey : 2UCRZpAbCGC
-    [dbg] guider_print_dev_guider_info(238):           DeviceName : ExampleDev
-    [dbg] guider_print_dev_guider_info(239):             DeviceID : 2UCRZpAbCGC.ExampleDev
-    [dbg] guider_print_dev_guider_info(240):         DeviceSecret : fbh47lGBSayncmTHEjF1E5x4CZdeJTO9
-    [dbg] guider_print_dev_guider_info(241): ....................................................
-    ...
-    ...
-    _demo_message_arrive|136 :: ----
-    _demo_message_arrive|140 :: Topic: '/2UCRZpAbCGC/ExampleDev/data' (Length: 28)
-    _demo_message_arrive|144 :: Payload: '{"attr_name":"temperature", "attr_value":"1"}' (Length: 45)
-    _demo_message_arrive|145 :: ----
-    [inf] iotx_mc_unsubscribe(1416): mqtt unsubscribe success,topic = /2UCRZpAbCGC/ExampleDev/data!
-    [dbg] iotx_mc_disconnect(2106): rc = MQTTDisconnect() = 0
-    [inf] _network_ssl_disconnect(413): ssl_disconnect
-    [inf] iotx_mc_disconnect(2114): mqtt disconnect!
-    [inf] iotx_mc_release(2160): mqtt release!
-
-    ---------------------------------------------------
-    . bytes_total_allocated:    1292
-    . bytes_total_freed:        1292
-    . bytes_total_in_use:       0
-    . bytes_max_allocated:      560
-    . bytes_max_in_use:         1066
-    . iterations_allocated:     20
-    . iterations_freed:         20
-    . iterations_in_use:        0
-    . iterations_max_in_use:    11
-    ---------------------------------------------------
-    main|441 :: out of sample!
-
-#### **2. 观察消息上报**
-
-如下日志信息显示样例程序正在通过`MQTT`的`Publish`类型消息, 上报业务数据到`/${prodcutKey}/${deviceName}/data`
+# 快速开始
 
-    mqtt_client|256 :: packet-id=3, publish topic msg={"attr_name":"temperature", "attr_value":"1"}
+用户可以通过[快速的体验C-SDK](https://code.aliyun.com/edward.yangx/public-docs/wikis/user-guide/linkkit/Quick_Start)感受如何将设备连接到阿里云物联网平台, 并如何将设备的数据发送到平台/以及如何从物联网平台接收数据
 
-#### **3. 观察消息下推**
 
-如下日志信息显示该消息因为是到达已被订阅的`Topic`, 所以又被服务器原样推送到样例程序, 并进入相应的回调函数
+# 移植说明
+C-SDK与OS/硬件平台无关, 全部部分用C编写, 它定义了HAL层来对接与硬件相关的功能, 因此在使用C-SDK时用户需要去实现相关的HAL函数
 
-    _demo_message_arrive|136 :: ----
-    _demo_message_arrive|140 :: Topic: '/2UCRZpAbCGC/ExampleDev/data' (Length: 28)
-    _demo_message_arrive|144 :: Payload: '{"attr_name":"temperature", "attr_value":"1"}' (Length: 45)
-    _demo_message_arrive|145 :: ----
+目前C-SDK已实现了在Linux/Windows/AliOS上HAL的实现, 同时对于一些常见的OS或者模组也进行了适配, 可以[访问此处](https://code.aliyun.com/edward.yangx/public-docs/wikis/user-guide/linkkit/Port_Guide/Porting_Overview)查看如何在相应平台上进行SDK的编译与集成
 
-#### **4. 观察控制台日志**
 
-可以登录物联网套件控制台, 到[设备页面](https://iot.console.aliyun.com/#/product/detail), 找到刚才填写在SDK中的设备并点击进入, 点左边导航栏的**日志服务**, 可以看到刚才被上报的消息
+# 编程文档
 
-![image](https://raw.githubusercontent.com/wiki/aliyun/iotkit-embedded/pics/iotconsole-publog.png)
+SDK提供了一系列的编程文档来描述如何使用SDK提供的软件功能, 请[访问此处](https://code.aliyun.com/edward.yangx/public-docs/wikis/user-guide/Linkkit_User_Manual)进行了解
 
-# 关于SDK的更多使用方式, 请访问[官方WiKi](https://github.com/aliyun/iotkit-embedded/wiki)

+ 1 - 0
iotkit-embedded/certs/iot.mk

@@ -0,0 +1 @@
+LIBA_TARGET := libiot_cert.a

+ 2 - 28
iotkit-embedded/src/system/iotkit-system/src/ca.c → iotkit-embedded/certs/root_ca.c

@@ -1,26 +1,10 @@
 /*
- * Copyright (c) 2014-2016 Alibaba Group. All rights reserved.
- * License-Identifier: Apache-2.0
- *
- * Licensed under the Apache License, Version 2.0 (the "License"); you may
- * not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *    http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
 
-
 #include <stdlib.h>
 
-#ifndef IOTX_WITHOUT_TLS
-static const char *iotx_ca_crt = \
+const char *iotx_ca_crt = \
 {
     \
     "-----BEGIN CERTIFICATE-----\r\n"
@@ -45,13 +29,3 @@ static const char *iotx_ca_crt = \
     "HMUfpIBvFSDJ3gyICh3WZlXi/EjJKSZp4A==\r\n" \
     "-----END CERTIFICATE-----"
 };
-#endif  /* #ifndef IOTX_WITHOUT_TLS */
-
-const char *iotx_ca_get(void)
-{
-#ifdef IOTX_WITHOUT_TLS
-    return NULL;
-#else
-    return iotx_ca_crt;
-#endif
-}

+ 11 - 0
iotkit-embedded/config.bat

@@ -0,0 +1,11 @@
+@echo off
+chcp 437
+
+set CONFIG_=FEATURE_
+
+.\tools\prebuilt\windows\kconfig-frontends-3.12.0-windows\kconfig-mconf.exe .\tools\Config.in
+
+if exist .config (
+	copy /y .config make.settings
+	del /F .config
+)

+ 0 - 0
iotkit-embedded/src/tls/mbedtls-in-iotkit/include/.gitignore → iotkit-embedded/external_libs/mbedtls/include/.gitignore


+ 60 - 77
iotkit-embedded/src/tls/mbedtls-in-iotkit/include/mbedtls/aes.h → iotkit-embedded/external_libs/mbedtls/include/mbedtls/aes.h

@@ -1,32 +1,16 @@
-/**
- * \file aes.h
- *
- * \brief AES block cipher
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
+
+
+
 #ifndef MBEDTLS_AES_H
 #define MBEDTLS_AES_H
 
 #if !defined(MBEDTLS_CONFIG_FILE)
-#include "config.h"
+    #include "config.h"
 #else
-#include MBEDTLS_CONFIG_FILE
+    #include MBEDTLS_CONFIG_FILE
 #endif
 
 #include <stddef.h>
@@ -41,7 +25,7 @@
 
 #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
     !defined(inline) && !defined(__cplusplus)
-#define inline __inline
+    #define inline __inline
 #endif
 
 #if !defined(MBEDTLS_AES_ALT)
@@ -60,8 +44,7 @@ extern "C" {
  *                 - to simplify key expansion in the 256-bit case by
  *                 generating an extra round key
  */
-typedef struct
-{
+typedef struct {
     int nr;                     /*!<  number of rounds  */
     uint32_t *rk;               /*!<  AES round keys    */
     uint32_t buf[68];           /*!<  unaligned data    */
@@ -73,14 +56,14 @@ mbedtls_aes_context;
  *
  * \param ctx      AES context to be initialized
  */
-void mbedtls_aes_init( mbedtls_aes_context *ctx );
+void mbedtls_aes_init(mbedtls_aes_context *ctx);
 
 /**
  * \brief          Clear AES context
  *
  * \param ctx      AES context to be cleared
  */
-void mbedtls_aes_free( mbedtls_aes_context *ctx );
+void mbedtls_aes_free(mbedtls_aes_context *ctx);
 
 /**
  * \brief          AES key schedule (encryption)
@@ -91,8 +74,8 @@ void mbedtls_aes_free( mbedtls_aes_context *ctx );
  *
  * \return         0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
  */
-int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key,
-                    unsigned int keybits );
+int mbedtls_aes_setkey_enc(mbedtls_aes_context *ctx, const unsigned char *key,
+                                       unsigned int keybits);
 
 /**
  * \brief          AES key schedule (decryption)
@@ -103,8 +86,8 @@ int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key,
  *
  * \return         0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
  */
-int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key,
-                    unsigned int keybits );
+int mbedtls_aes_setkey_dec(mbedtls_aes_context *ctx, const unsigned char *key,
+                                       unsigned int keybits);
 
 /**
  * \brief          AES-ECB block encryption/decryption
@@ -116,10 +99,10 @@ int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key,
  *
  * \return         0 if successful
  */
-int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
-                    int mode,
-                    const unsigned char input[16],
-                    unsigned char output[16] );
+int mbedtls_aes_crypt_ecb(mbedtls_aes_context *ctx,
+                                      int mode,
+                                      const unsigned char input[16],
+                                      unsigned char output[16]);
 
 #if defined(MBEDTLS_CIPHER_MODE_CBC)
 /**
@@ -144,12 +127,12 @@ int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
  *
  * \return         0 if successful, or MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH
  */
-int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
-                    int mode,
-                    size_t length,
-                    unsigned char iv[16],
-                    const unsigned char *input,
-                    unsigned char *output );
+int mbedtls_aes_crypt_cbc(mbedtls_aes_context *ctx,
+                                      int mode,
+                                      size_t length,
+                                      unsigned char iv[16],
+                                      const unsigned char *input,
+                                      unsigned char *output);
 #endif /* MBEDTLS_CIPHER_MODE_CBC */
 
 #if defined(MBEDTLS_CIPHER_MODE_CFB)
@@ -178,13 +161,13 @@ int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
  *
  * \return         0 if successful
  */
-int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
-                       int mode,
-                       size_t length,
-                       size_t *iv_off,
-                       unsigned char iv[16],
-                       const unsigned char *input,
-                       unsigned char *output );
+int mbedtls_aes_crypt_cfb128(mbedtls_aes_context *ctx,
+        int mode,
+        size_t length,
+        size_t *iv_off,
+        unsigned char iv[16],
+        const unsigned char *input,
+        unsigned char *output);
 
 /**
  * \brief          AES-CFB8 buffer encryption/decryption.
@@ -210,12 +193,12 @@ int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
  *
  * \return         0 if successful
  */
-int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
-                    int mode,
-                    size_t length,
-                    unsigned char iv[16],
-                    const unsigned char *input,
-                    unsigned char *output );
+int mbedtls_aes_crypt_cfb8(mbedtls_aes_context *ctx,
+                                       int mode,
+                                       size_t length,
+                                       unsigned char iv[16],
+                                       const unsigned char *input,
+                                       unsigned char *output);
 #endif /*MBEDTLS_CIPHER_MODE_CFB */
 
 #if defined(MBEDTLS_CIPHER_MODE_CTR)
@@ -241,13 +224,13 @@ int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
  *
  * \return         0 if successful
  */
-int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
-                       size_t length,
-                       size_t *nc_off,
-                       unsigned char nonce_counter[16],
-                       unsigned char stream_block[16],
-                       const unsigned char *input,
-                       unsigned char *output );
+int mbedtls_aes_crypt_ctr(mbedtls_aes_context *ctx,
+                                      size_t length,
+                                      size_t *nc_off,
+                                      unsigned char nonce_counter[16],
+                                      unsigned char stream_block[16],
+                                      const unsigned char *input,
+                                      unsigned char *output);
 #endif /* MBEDTLS_CIPHER_MODE_CTR */
 
 /**
@@ -261,9 +244,9 @@ int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
  *
  * \return          0 if successful
  */
-int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx,
-                                  const unsigned char input[16],
-                                  unsigned char output[16] );
+int mbedtls_internal_aes_encrypt(mbedtls_aes_context *ctx,
+        const unsigned char input[16],
+        unsigned char output[16]);
 
 /**
  * \brief           Internal AES block decryption function
@@ -276,9 +259,9 @@ int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx,
  *
  * \return          0 if successful
  */
-int mbedtls_internal_aes_decrypt( mbedtls_aes_context *ctx,
-                                  const unsigned char input[16],
-                                  unsigned char output[16] );
+int mbedtls_internal_aes_decrypt(mbedtls_aes_context *ctx,
+        const unsigned char input[16],
+        unsigned char output[16]);
 
 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
 #if defined(MBEDTLS_DEPRECATED_WARNING)
@@ -298,11 +281,11 @@ int mbedtls_internal_aes_decrypt( mbedtls_aes_context *ctx,
  * \param output    Output (ciphertext) block
  */
 MBEDTLS_DEPRECATED static inline void mbedtls_aes_encrypt(
-                                                mbedtls_aes_context *ctx,
-                                                const unsigned char input[16],
-                                                unsigned char output[16] )
+            mbedtls_aes_context *ctx,
+            const unsigned char input[16],
+            unsigned char output[16])
 {
-    mbedtls_internal_aes_encrypt( ctx, input, output );
+    mbedtls_internal_aes_encrypt(ctx, input, output);
 }
 
 /**
@@ -317,11 +300,11 @@ MBEDTLS_DEPRECATED static inline void mbedtls_aes_encrypt(
  * \param output    Output (plaintext) block
  */
 MBEDTLS_DEPRECATED static inline void mbedtls_aes_decrypt(
-                                                mbedtls_aes_context *ctx,
-                                                const unsigned char input[16],
-                                                unsigned char output[16] )
+            mbedtls_aes_context *ctx,
+            const unsigned char input[16],
+            unsigned char output[16])
 {
-    mbedtls_internal_aes_decrypt( ctx, input, output );
+    mbedtls_internal_aes_decrypt(ctx, input, output);
 }
 
 #undef MBEDTLS_DEPRECATED
@@ -344,7 +327,7 @@ extern "C" {
  *
  * \return         0 if successful, or 1 if the test failed
  */
-int mbedtls_aes_self_test( int verbose );
+int mbedtls_aes_self_test(int verbose);
 
 #ifdef __cplusplus
 }

+ 47 - 67
iotkit-embedded/src/tls/mbedtls-in-iotkit/include/mbedtls/asn1.h → iotkit-embedded/external_libs/mbedtls/include/mbedtls/asn1.h

@@ -1,38 +1,22 @@
-/**
- * \file asn1.h
- *
- * \brief Generic ASN.1 parsing
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
+
+
+
 #ifndef MBEDTLS_ASN1_H
 #define MBEDTLS_ASN1_H
 
 #if !defined(MBEDTLS_CONFIG_FILE)
-#include "config.h"
+    #include "config.h"
 #else
-#include MBEDTLS_CONFIG_FILE
+    #include MBEDTLS_CONFIG_FILE
 #endif
 
 #include <stddef.h>
 
 #if defined(MBEDTLS_BIGNUM_C)
-#include "bignum.h"
+    #include "bignum.h"
 #endif
 
 /**
@@ -100,8 +84,8 @@
  * 'unsigned char *oid' here!
  */
 #define MBEDTLS_OID_CMP(oid_str, oid_buf)                                   \
-        ( ( MBEDTLS_OID_SIZE(oid_str) != (oid_buf)->len ) ||                \
-          memcmp( (oid_str), (oid_buf)->p, (oid_buf)->len) != 0 )
+    ( ( MBEDTLS_OID_SIZE(oid_str) != (oid_buf)->len ) ||                \
+      memcmp( (oid_str), (oid_buf)->p, (oid_buf)->len) != 0 )
 
 #ifdef __cplusplus
 extern "C" {
@@ -115,8 +99,7 @@ extern "C" {
 /**
  * Type-length-value structure that allows for ASN1 using DER.
  */
-typedef struct mbedtls_asn1_buf
-{
+typedef struct mbedtls_asn1_buf {
     int tag;                /**< ASN1 type, e.g. MBEDTLS_ASN1_UTF8_STRING. */
     size_t len;             /**< ASN1 length, in octets. */
     unsigned char *p;       /**< ASN1 data, e.g. in ASCII. */
@@ -126,8 +109,7 @@ mbedtls_asn1_buf;
 /**
  * Container for ASN1 bit strings.
  */
-typedef struct mbedtls_asn1_bitstring
-{
+typedef struct mbedtls_asn1_bitstring {
     size_t len;                 /**< ASN1 length, in octets. */
     unsigned char unused_bits;  /**< Number of unused bits at the end of the string */
     unsigned char *p;           /**< Raw ASN1 data for the bit string */
@@ -137,8 +119,7 @@ mbedtls_asn1_bitstring;
 /**
  * Container for a sequence of ASN.1 items
  */
-typedef struct mbedtls_asn1_sequence
-{
+typedef struct mbedtls_asn1_sequence {
     mbedtls_asn1_buf buf;                   /**< Buffer containing the given ASN.1 item. */
     struct mbedtls_asn1_sequence *next;    /**< The next entry in the sequence. */
 }
@@ -147,8 +128,7 @@ mbedtls_asn1_sequence;
 /**
  * Container for a sequence or list of 'named' ASN.1 data items
  */
-typedef struct mbedtls_asn1_named_data
-{
+typedef struct mbedtls_asn1_named_data {
     mbedtls_asn1_buf oid;                   /**< The object identifier. */
     mbedtls_asn1_buf val;                   /**< The named value. */
     struct mbedtls_asn1_named_data *next;  /**< The next entry in the sequence. */
@@ -168,9 +148,9 @@ mbedtls_asn1_named_data;
  *              end of data, MBEDTLS_ERR_ASN1_INVALID_LENGTH if length is
  *              unparseable.
  */
-int mbedtls_asn1_get_len( unsigned char **p,
-                  const unsigned char *end,
-                  size_t *len );
+int mbedtls_asn1_get_len(unsigned char **p,
+                                     const unsigned char *end,
+                                     size_t *len);
 
 /**
  * \brief       Get the tag and length of the tag. Check for the requested tag.
@@ -184,9 +164,9 @@ int mbedtls_asn1_get_len( unsigned char **p,
  * \return      0 if successful, MBEDTLS_ERR_ASN1_UNEXPECTED_TAG if tag did
  *              not match requested tag, or another specific ASN.1 error code.
  */
-int mbedtls_asn1_get_tag( unsigned char **p,
-                  const unsigned char *end,
-                  size_t *len, int tag );
+int mbedtls_asn1_get_tag(unsigned char **p,
+                                     const unsigned char *end,
+                                     size_t *len, int tag);
 
 /**
  * \brief       Retrieve a boolean ASN.1 tag and its value.
@@ -198,9 +178,9 @@ int mbedtls_asn1_get_tag( unsigned char **p,
  *
  * \return      0 if successful or a specific ASN.1 error code.
  */
-int mbedtls_asn1_get_bool( unsigned char **p,
-                   const unsigned char *end,
-                   int *val );
+int mbedtls_asn1_get_bool(unsigned char **p,
+                                      const unsigned char *end,
+                                      int *val);
 
 /**
  * \brief       Retrieve an integer ASN.1 tag and its value.
@@ -212,9 +192,9 @@ int mbedtls_asn1_get_bool( unsigned char **p,
  *
  * \return      0 if successful or a specific ASN.1 error code.
  */
-int mbedtls_asn1_get_int( unsigned char **p,
-                  const unsigned char *end,
-                  int *val );
+int mbedtls_asn1_get_int(unsigned char **p,
+                                     const unsigned char *end,
+                                     int *val);
 
 /**
  * \brief       Retrieve a bitstring ASN.1 tag and its value.
@@ -226,8 +206,8 @@ int mbedtls_asn1_get_int( unsigned char **p,
  *
  * \return      0 if successful or a specific ASN.1 error code.
  */
-int mbedtls_asn1_get_bitstring( unsigned char **p, const unsigned char *end,
-                        mbedtls_asn1_bitstring *bs);
+int mbedtls_asn1_get_bitstring(unsigned char **p, const unsigned char *end,
+        mbedtls_asn1_bitstring *bs);
 
 /**
  * \brief       Retrieve a bitstring ASN.1 tag without unused bits and its
@@ -240,8 +220,8 @@ int mbedtls_asn1_get_bitstring( unsigned char **p, const unsigned char *end,
  *
  * \return      0 if successful or a specific ASN.1 error code.
  */
-int mbedtls_asn1_get_bitstring_null( unsigned char **p, const unsigned char *end,
-                             size_t *len );
+int mbedtls_asn1_get_bitstring_null(unsigned char **p, const unsigned char *end,
+        size_t *len);
 
 /**
  * \brief       Parses and splits an ASN.1 "SEQUENCE OF <tag>"
@@ -254,10 +234,10 @@ int mbedtls_asn1_get_bitstring_null( unsigned char **p, const unsigned char *end
  *
  * \return      0 if successful or a specific ASN.1 error code.
  */
-int mbedtls_asn1_get_sequence_of( unsigned char **p,
-                          const unsigned char *end,
-                          mbedtls_asn1_sequence *cur,
-                          int tag);
+int mbedtls_asn1_get_sequence_of(unsigned char **p,
+        const unsigned char *end,
+        mbedtls_asn1_sequence *cur,
+        int tag);
 
 #if defined(MBEDTLS_BIGNUM_C)
 /**
@@ -270,9 +250,9 @@ int mbedtls_asn1_get_sequence_of( unsigned char **p,
  *
  * \return      0 if successful or a specific ASN.1 or MPI error code.
  */
-int mbedtls_asn1_get_mpi( unsigned char **p,
-                  const unsigned char *end,
-                  mbedtls_mpi *X );
+int mbedtls_asn1_get_mpi(unsigned char **p,
+                                     const unsigned char *end,
+                                     mbedtls_mpi *X);
 #endif /* MBEDTLS_BIGNUM_C */
 
 /**
@@ -287,9 +267,9 @@ int mbedtls_asn1_get_mpi( unsigned char **p,
  *
  * \return      0 if successful or a specific ASN.1 or MPI error code.
  */
-int mbedtls_asn1_get_alg( unsigned char **p,
-                  const unsigned char *end,
-                  mbedtls_asn1_buf *alg, mbedtls_asn1_buf *params );
+int mbedtls_asn1_get_alg(unsigned char **p,
+                                     const unsigned char *end,
+                                     mbedtls_asn1_buf *alg, mbedtls_asn1_buf *params);
 
 /**
  * \brief       Retrieve an AlgorithmIdentifier ASN.1 sequence with NULL or no
@@ -303,9 +283,9 @@ int mbedtls_asn1_get_alg( unsigned char **p,
  *
  * \return      0 if successful or a specific ASN.1 or MPI error code.
  */
-int mbedtls_asn1_get_alg_null( unsigned char **p,
-                       const unsigned char *end,
-                       mbedtls_asn1_buf *alg );
+int mbedtls_asn1_get_alg_null(unsigned char **p,
+        const unsigned char *end,
+        mbedtls_asn1_buf *alg);
 
 /**
  * \brief       Find a specific named_data entry in a sequence or list based on
@@ -317,15 +297,15 @@ int mbedtls_asn1_get_alg_null( unsigned char **p,
  *
  * \return      NULL if not found, or a pointer to the existing entry.
  */
-mbedtls_asn1_named_data *mbedtls_asn1_find_named_data( mbedtls_asn1_named_data *list,
-                                       const char *oid, size_t len );
+mbedtls_asn1_named_data *mbedtls_asn1_find_named_data(mbedtls_asn1_named_data *list,
+        const char *oid, size_t len);
 
 /**
  * \brief       Free a mbedtls_asn1_named_data entry
  *
  * \param entry The named data entry to free
  */
-void mbedtls_asn1_free_named_data( mbedtls_asn1_named_data *entry );
+void mbedtls_asn1_free_named_data(mbedtls_asn1_named_data *entry);
 
 /**
  * \brief       Free all entries in a mbedtls_asn1_named_data list
@@ -333,7 +313,7 @@ void mbedtls_asn1_free_named_data( mbedtls_asn1_named_data *entry );
  *
  * \param head  Pointer to the head of the list of named data entries to free
  */
-void mbedtls_asn1_free_named_data_list( mbedtls_asn1_named_data **head );
+void mbedtls_asn1_free_named_data_list(mbedtls_asn1_named_data **head);
 
 #ifdef __cplusplus
 }

+ 10 - 26
iotkit-embedded/src/tls/mbedtls-in-iotkit/include/mbedtls/base64.h → iotkit-embedded/external_libs/mbedtls/include/mbedtls/base64.h

@@ -1,25 +1,9 @@
-/**
- * \file base64.h
- *
- * \brief RFC 1521 base64 encoding/decoding
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
+
+
+
 #ifndef MBEDTLS_BASE64_H
 #define MBEDTLS_BASE64_H
 
@@ -51,8 +35,8 @@ extern "C" {
  * \note           Call this function with dlen = 0 to obtain the
  *                 required buffer size in *olen
  */
-int mbedtls_base64_encode( unsigned char *dst, size_t dlen, size_t *olen,
-                   const unsigned char *src, size_t slen );
+int mbedtls_base64_encode(unsigned char *dst, size_t dlen, size_t *olen,
+                                      const unsigned char *src, size_t slen);
 
 /**
  * \brief          Decode a base64-formatted buffer
@@ -71,15 +55,15 @@ int mbedtls_base64_encode( unsigned char *dst, size_t dlen, size_t *olen,
  * \note           Call this function with *dst = NULL or dlen = 0 to obtain
  *                 the required buffer size in *olen
  */
-int mbedtls_base64_decode( unsigned char *dst, size_t dlen, size_t *olen,
-                   const unsigned char *src, size_t slen );
+int mbedtls_base64_decode(unsigned char *dst, size_t dlen, size_t *olen,
+                                      const unsigned char *src, size_t slen);
 
 /**
  * \brief          Checkup routine
  *
  * \return         0 if successful, or 1 if the test failed
  */
-int mbedtls_base64_self_test( int verbose );
+int mbedtls_base64_self_test(int verbose);
 
 #ifdef __cplusplus
 }

+ 96 - 112
iotkit-embedded/src/tls/mbedtls-in-iotkit/include/mbedtls/bignum.h → iotkit-embedded/external_libs/mbedtls/include/mbedtls/bignum.h

@@ -1,39 +1,23 @@
-/**
- * \file bignum.h
- *
- * \brief  Multi-precision integer library
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
+
+
+
 #ifndef MBEDTLS_BIGNUM_H
 #define MBEDTLS_BIGNUM_H
 
 #if !defined(MBEDTLS_CONFIG_FILE)
-#include "config.h"
+    #include "config.h"
 #else
-#include MBEDTLS_CONFIG_FILE
+    #include MBEDTLS_CONFIG_FILE
 #endif
 
 #include <stddef.h>
 #include <stdint.h>
 
 #if defined(MBEDTLS_FS_IO)
-#include <stdio.h>
+    #include <stdio.h>
 #endif
 
 #define MBEDTLS_ERR_MPI_FILE_IO_ERROR                     -0x0002  /**< An error occurred while reading from or writing to a file. */
@@ -53,27 +37,27 @@
 #define MBEDTLS_MPI_MAX_LIMBS                             10000
 
 #if !defined(MBEDTLS_MPI_WINDOW_SIZE)
-/*
- * Maximum window size used for modular exponentiation. Default: 6
- * Minimum value: 1. Maximum value: 6.
- *
- * Result is an array of ( 2 << MBEDTLS_MPI_WINDOW_SIZE ) MPIs used
- * for the sliding window calculation. (So 64 by default)
- *
- * Reduction in size, reduces speed.
- */
-#define MBEDTLS_MPI_WINDOW_SIZE                           6        /**< Maximum windows size used. */
+    /*
+    * Maximum window size used for modular exponentiation. Default: 6
+    * Minimum value: 1. Maximum value: 6.
+    *
+    * Result is an array of ( 2 << MBEDTLS_MPI_WINDOW_SIZE ) MPIs used
+    * for the sliding window calculation. (So 64 by default)
+    *
+    * Reduction in size, reduces speed.
+    */
+    #define MBEDTLS_MPI_WINDOW_SIZE                           6        /**< Maximum windows size used. */
 #endif /* !MBEDTLS_MPI_WINDOW_SIZE */
 
 #if !defined(MBEDTLS_MPI_MAX_SIZE)
-/*
- * Maximum size of MPIs allowed in bits and bytes for user-MPIs.
- * ( Default: 512 bytes => 4096 bits, Maximum tested: 2048 bytes => 16384 bits )
- *
- * Note: Calculations can results temporarily in larger MPIs. So the number
- * of limbs required (MBEDTLS_MPI_MAX_LIMBS) is higher.
- */
-#define MBEDTLS_MPI_MAX_SIZE                              1024     /**< Maximum number of bytes for usable MPIs. */
+    /*
+    * Maximum size of MPIs allowed in bits and bytes for user-MPIs.
+    * ( Default: 512 bytes => 4096 bits, Maximum tested: 2048 bytes => 16384 bits )
+    *
+    * Note: Calculations can results temporarily in larger MPIs. So the number
+    * of limbs required (MBEDTLS_MPI_MAX_LIMBS) is higher.
+    */
+    #define MBEDTLS_MPI_MAX_SIZE                              1024     /**< Maximum number of bytes for usable MPIs. */
 #endif /* !MBEDTLS_MPI_MAX_SIZE */
 
 #define MBEDTLS_MPI_MAX_BITS                              ( 8 * MBEDTLS_MPI_MAX_SIZE )    /**< Maximum number of bits for usable MPIs. */
@@ -108,30 +92,30 @@
  */
 #if ( ! defined(MBEDTLS_HAVE_INT32) && \
         defined(_MSC_VER) && defined(_M_AMD64) )
-  #define MBEDTLS_HAVE_INT64
-  typedef  int64_t mbedtls_mpi_sint;
-  typedef uint64_t mbedtls_mpi_uint;
+#define MBEDTLS_HAVE_INT64
+typedef  int64_t mbedtls_mpi_sint;
+typedef uint64_t mbedtls_mpi_uint;
 #else
-  #if ( ! defined(MBEDTLS_HAVE_INT32) &&               \
+#if ( ! defined(MBEDTLS_HAVE_INT32) &&               \
         defined(__GNUC__) && (                          \
         defined(__amd64__) || defined(__x86_64__)    || \
         defined(__ppc64__) || defined(__powerpc64__) || \
         defined(__ia64__)  || defined(__alpha__)     || \
         (defined(__sparc__) && defined(__arch64__))  || \
         defined(__s390x__) || defined(__mips64) ) )
-     #define MBEDTLS_HAVE_INT64
-     typedef  int64_t mbedtls_mpi_sint;
-     typedef uint64_t mbedtls_mpi_uint;
-     /* mbedtls_t_udbl defined as 128-bit unsigned int */
-     typedef unsigned int mbedtls_t_udbl __attribute__((mode(TI)));
-     #define MBEDTLS_HAVE_UDBL
-  #else
-     #define MBEDTLS_HAVE_INT32
-     typedef  int32_t mbedtls_mpi_sint;
-     typedef uint32_t mbedtls_mpi_uint;
-     typedef uint64_t mbedtls_t_udbl;
-     #define MBEDTLS_HAVE_UDBL
-  #endif /* !MBEDTLS_HAVE_INT32 && __GNUC__ && 64-bit platform */
+#define MBEDTLS_HAVE_INT64
+typedef  int64_t mbedtls_mpi_sint;
+typedef uint64_t mbedtls_mpi_uint;
+/* mbedtls_t_udbl defined as 128-bit unsigned int */
+typedef unsigned int mbedtls_t_udbl __attribute__((mode(TI)));
+#define MBEDTLS_HAVE_UDBL
+#else
+#define MBEDTLS_HAVE_INT32
+typedef  int32_t mbedtls_mpi_sint;
+typedef uint32_t mbedtls_mpi_uint;
+typedef uint64_t mbedtls_t_udbl;
+#define MBEDTLS_HAVE_UDBL
+#endif /* !MBEDTLS_HAVE_INT32 && __GNUC__ && 64-bit platform */
 #endif /* !MBEDTLS_HAVE_INT32 && _MSC_VER && _M_AMD64 */
 
 #ifdef __cplusplus
@@ -141,8 +125,7 @@ extern "C" {
 /**
  * \brief          MPI structure
  */
-typedef struct
-{
+typedef struct {
     int s;              /*!<  integer sign      */
     size_t n;           /*!<  total # of limbs  */
     mbedtls_mpi_uint *p;          /*!<  pointer to limbs  */
@@ -156,14 +139,14 @@ mbedtls_mpi;
  *
  * \param X         One MPI to initialize.
  */
-void mbedtls_mpi_init( mbedtls_mpi *X );
+void mbedtls_mpi_init(mbedtls_mpi *X);
 
 /**
  * \brief          Unallocate one MPI
  *
  * \param X        One MPI to unallocate.
  */
-void mbedtls_mpi_free( mbedtls_mpi *X );
+void mbedtls_mpi_free(mbedtls_mpi *X);
 
 /**
  * \brief          Enlarge to the specified number of limbs
@@ -174,7 +157,7 @@ void mbedtls_mpi_free( mbedtls_mpi *X );
  * \return         0 if successful,
  *                 MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  */
-int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs );
+int mbedtls_mpi_grow(mbedtls_mpi *X, size_t nblimbs);
 
 /**
  * \brief          Resize down, keeping at least the specified number of limbs
@@ -185,7 +168,7 @@ int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs );
  * \return         0 if successful,
  *                 MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  */
-int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs );
+int mbedtls_mpi_shrink(mbedtls_mpi *X, size_t nblimbs);
 
 /**
  * \brief          Copy the contents of Y into X
@@ -196,7 +179,7 @@ int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs );
  * \return         0 if successful,
  *                 MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  */
-int mbedtls_mpi_copy( mbedtls_mpi *X, const mbedtls_mpi *Y );
+int mbedtls_mpi_copy(mbedtls_mpi *X, const mbedtls_mpi *Y);
 
 /**
  * \brief          Swap the contents of X and Y
@@ -204,7 +187,7 @@ int mbedtls_mpi_copy( mbedtls_mpi *X, const mbedtls_mpi *Y );
  * \param X        First MPI value
  * \param Y        Second MPI value
  */
-void mbedtls_mpi_swap( mbedtls_mpi *X, mbedtls_mpi *Y );
+void mbedtls_mpi_swap(mbedtls_mpi *X, mbedtls_mpi *Y);
 
 /**
  * \brief          Safe conditional assignement X = Y if assign is 1
@@ -223,7 +206,7 @@ void mbedtls_mpi_swap( mbedtls_mpi *X, mbedtls_mpi *Y );
  *                 information through branch prediction and/or memory access
  *                 patterns analysis).
  */
-int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign );
+int mbedtls_mpi_safe_cond_assign(mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign);
 
 /**
  * \brief          Safe conditional swap X <-> Y if swap is 1
@@ -242,7 +225,7 @@ int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned
  *                 information through branch prediction and/or memory access
  *                 patterns analysis).
  */
-int mbedtls_mpi_safe_cond_swap( mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char assign );
+int mbedtls_mpi_safe_cond_swap(mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char assign);
 
 /**
  * \brief          Set value from integer
@@ -253,7 +236,7 @@ int mbedtls_mpi_safe_cond_swap( mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char as
  * \return         0 if successful,
  *                 MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  */
-int mbedtls_mpi_lset( mbedtls_mpi *X, mbedtls_mpi_sint z );
+int mbedtls_mpi_lset(mbedtls_mpi *X, mbedtls_mpi_sint z);
 
 /**
  * \brief          Get a specific bit from X
@@ -263,7 +246,7 @@ int mbedtls_mpi_lset( mbedtls_mpi *X, mbedtls_mpi_sint z );
  *
  * \return         Either a 0 or a 1
  */
-int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos );
+int mbedtls_mpi_get_bit(const mbedtls_mpi *X, size_t pos);
 
 /**
  * \brief          Set a bit of X to a specific value of 0 or 1
@@ -279,7 +262,7 @@ int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos );
  *                 MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
  *                 MBEDTLS_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1
  */
-int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val );
+int mbedtls_mpi_set_bit(mbedtls_mpi *X, size_t pos, unsigned char val);
 
 /**
  * \brief          Return the number of zero-bits before the least significant
@@ -289,7 +272,7 @@ int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val );
  *
  * \param X        MPI to use
  */
-size_t mbedtls_mpi_lsb( const mbedtls_mpi *X );
+size_t mbedtls_mpi_lsb(const mbedtls_mpi *X);
 
 /**
  * \brief          Return the number of bits up to and including the most
@@ -299,14 +282,14 @@ size_t mbedtls_mpi_lsb( const mbedtls_mpi *X );
  *
  * \param X        MPI to use
  */
-size_t mbedtls_mpi_bitlen( const mbedtls_mpi *X );
+size_t mbedtls_mpi_bitlen(const mbedtls_mpi *X);
 
 /**
  * \brief          Return the total size in bytes
  *
  * \param X        MPI to use
  */
-size_t mbedtls_mpi_size( const mbedtls_mpi *X );
+size_t mbedtls_mpi_size(const mbedtls_mpi *X);
 
 /**
  * \brief          Import from an ASCII string
@@ -317,7 +300,7 @@ size_t mbedtls_mpi_size( const mbedtls_mpi *X );
  *
  * \return         0 if successful, or a MBEDTLS_ERR_MPI_XXX error code
  */
-int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s );
+int mbedtls_mpi_read_string(mbedtls_mpi *X, int radix, const char *s);
 
 /**
  * \brief          Export into an ASCII string
@@ -335,8 +318,8 @@ int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s );
  * \note           Call this function with buflen = 0 to obtain the
  *                 minimum required buffer size in *olen.
  */
-int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix,
-                              char *buf, size_t buflen, size_t *olen );
+int mbedtls_mpi_write_string(const mbedtls_mpi *X, int radix,
+        char *buf, size_t buflen, size_t *olen);
 
 #if defined(MBEDTLS_FS_IO)
 /**
@@ -350,7 +333,7 @@ int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix,
  *                 the file read buffer is too small or a
  *                 MBEDTLS_ERR_MPI_XXX error code
  */
-int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin );
+int mbedtls_mpi_read_file(mbedtls_mpi *X, int radix, FILE *fin);
 
 /**
  * \brief          Write X into an opened file, or stdout if fout is NULL
@@ -364,7 +347,7 @@ int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin );
  *
  * \note           Set fout == NULL to print X on the console.
  */
-int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X, int radix, FILE *fout );
+int mbedtls_mpi_write_file(const char *p, const mbedtls_mpi *X, int radix, FILE *fout);
 #endif /* MBEDTLS_FS_IO */
 
 /**
@@ -377,7 +360,7 @@ int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X, int radix, FILE
  * \return         0 if successful,
  *                 MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  */
-int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf, size_t buflen );
+int mbedtls_mpi_read_binary(mbedtls_mpi *X, const unsigned char *buf, size_t buflen);
 
 /**
  * \brief          Export X into unsigned binary data, big endian.
@@ -391,7 +374,7 @@ int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf, size_t bu
  * \return         0 if successful,
  *                 MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
  */
-int mbedtls_mpi_write_binary( const mbedtls_mpi *X, unsigned char *buf, size_t buflen );
+int mbedtls_mpi_write_binary(const mbedtls_mpi *X, unsigned char *buf, size_t buflen);
 
 /**
  * \brief          Left-shift: X <<= count
@@ -402,7 +385,7 @@ int mbedtls_mpi_write_binary( const mbedtls_mpi *X, unsigned char *buf, size_t b
  * \return         0 if successful,
  *                 MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  */
-int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count );
+int mbedtls_mpi_shift_l(mbedtls_mpi *X, size_t count);
 
 /**
  * \brief          Right-shift: X >>= count
@@ -413,7 +396,7 @@ int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count );
  * \return         0 if successful,
  *                 MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  */
-int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count );
+int mbedtls_mpi_shift_r(mbedtls_mpi *X, size_t count);
 
 /**
  * \brief          Compare unsigned values
@@ -425,7 +408,7 @@ int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count );
  *                -1 if |X| is lesser  than |Y| or
  *                 0 if |X| is equal to |Y|
  */
-int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y );
+int mbedtls_mpi_cmp_abs(const mbedtls_mpi *X, const mbedtls_mpi *Y);
 
 /**
  * \brief          Compare signed values
@@ -437,7 +420,7 @@ int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y );
  *                -1 if X is lesser  than Y or
  *                 0 if X is equal to Y
  */
-int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y );
+int mbedtls_mpi_cmp_mpi(const mbedtls_mpi *X, const mbedtls_mpi *Y);
 
 /**
  * \brief          Compare signed values
@@ -449,7 +432,7 @@ int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y );
  *                -1 if X is lesser  than z or
  *                 0 if X is equal to z
  */
-int mbedtls_mpi_cmp_int( const mbedtls_mpi *X, mbedtls_mpi_sint z );
+int mbedtls_mpi_cmp_int(const mbedtls_mpi *X, mbedtls_mpi_sint z);
 
 /**
  * \brief          Unsigned addition: X = |A| + |B|
@@ -461,7 +444,7 @@ int mbedtls_mpi_cmp_int( const mbedtls_mpi *X, mbedtls_mpi_sint z );
  * \return         0 if successful,
  *                 MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  */
-int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
+int mbedtls_mpi_add_abs(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B);
 
 /**
  * \brief          Unsigned subtraction: X = |A| - |B|
@@ -473,7 +456,7 @@ int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi
  * \return         0 if successful,
  *                 MBEDTLS_ERR_MPI_NEGATIVE_VALUE if B is greater than A
  */
-int mbedtls_mpi_sub_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
+int mbedtls_mpi_sub_abs(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B);
 
 /**
  * \brief          Signed addition: X = A + B
@@ -485,7 +468,7 @@ int mbedtls_mpi_sub_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi
  * \return         0 if successful,
  *                 MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  */
-int mbedtls_mpi_add_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
+int mbedtls_mpi_add_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B);
 
 /**
  * \brief          Signed subtraction: X = A - B
@@ -497,7 +480,7 @@ int mbedtls_mpi_add_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi
  * \return         0 if successful,
  *                 MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  */
-int mbedtls_mpi_sub_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
+int mbedtls_mpi_sub_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B);
 
 /**
  * \brief          Signed addition: X = A + b
@@ -509,7 +492,7 @@ int mbedtls_mpi_sub_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi
  * \return         0 if successful,
  *                 MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  */
-int mbedtls_mpi_add_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b );
+int mbedtls_mpi_add_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b);
 
 /**
  * \brief          Signed subtraction: X = A - b
@@ -521,7 +504,7 @@ int mbedtls_mpi_add_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint
  * \return         0 if successful,
  *                 MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  */
-int mbedtls_mpi_sub_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b );
+int mbedtls_mpi_sub_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b);
 
 /**
  * \brief          Baseline multiplication: X = A * B
@@ -533,7 +516,7 @@ int mbedtls_mpi_sub_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint
  * \return         0 if successful,
  *                 MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  */
-int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
+int mbedtls_mpi_mul_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B);
 
 /**
  * \brief          Baseline multiplication: X = A * b
@@ -547,7 +530,7 @@ int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi
  * \return         0 if successful,
  *                 MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  */
-int mbedtls_mpi_mul_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b );
+int mbedtls_mpi_mul_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b);
 
 /**
  * \brief          Division by mbedtls_mpi: A = Q * B + R
@@ -563,7 +546,7 @@ int mbedtls_mpi_mul_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint
  *
  * \note           Either Q or R can be NULL.
  */
-int mbedtls_mpi_div_mpi( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B );
+int mbedtls_mpi_div_mpi(mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B);
 
 /**
  * \brief          Division by int: A = Q * b + R
@@ -579,7 +562,7 @@ int mbedtls_mpi_div_mpi( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, c
  *
  * \note           Either Q or R can be NULL.
  */
-int mbedtls_mpi_div_int( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, mbedtls_mpi_sint b );
+int mbedtls_mpi_div_int(mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, mbedtls_mpi_sint b);
 
 /**
  * \brief          Modulo: R = A mod B
@@ -593,7 +576,7 @@ int mbedtls_mpi_div_int( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, m
  *                 MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if B == 0,
  *                 MBEDTLS_ERR_MPI_NEGATIVE_VALUE if B < 0
  */
-int mbedtls_mpi_mod_mpi( mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B );
+int mbedtls_mpi_mod_mpi(mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B);
 
 /**
  * \brief          Modulo: r = A mod b
@@ -607,7 +590,7 @@ int mbedtls_mpi_mod_mpi( mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi
  *                 MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if b == 0,
  *                 MBEDTLS_ERR_MPI_NEGATIVE_VALUE if b < 0
  */
-int mbedtls_mpi_mod_int( mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b );
+int mbedtls_mpi_mod_int(mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b);
 
 /**
  * \brief          Sliding-window exponentiation: X = A^E mod N
@@ -627,7 +610,8 @@ int mbedtls_mpi_mod_int( mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_
  *                 multiple calls, which speeds up things a bit. It can
  *                 be set to NULL if the extra performance is unneeded.
  */
-int mbedtls_mpi_exp_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *E, const mbedtls_mpi *N, mbedtls_mpi *_RR );
+int mbedtls_mpi_exp_mod(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *E, const mbedtls_mpi *N,
+                                    mbedtls_mpi *_RR);
 
 /**
  * \brief          Fill an MPI X with size bytes of random
@@ -640,9 +624,9 @@ int mbedtls_mpi_exp_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi
  * \return         0 if successful,
  *                 MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  */
-int mbedtls_mpi_fill_random( mbedtls_mpi *X, size_t size,
-                     int (*f_rng)(void *, unsigned char *, size_t),
-                     void *p_rng );
+int mbedtls_mpi_fill_random(mbedtls_mpi *X, size_t size,
+                                        int (*f_rng)(void *, unsigned char *, size_t),
+                                        void *p_rng);
 
 /**
  * \brief          Greatest common divisor: G = gcd(A, B)
@@ -654,7 +638,7 @@ int mbedtls_mpi_fill_random( mbedtls_mpi *X, size_t size,
  * \return         0 if successful,
  *                 MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  */
-int mbedtls_mpi_gcd( mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B );
+int mbedtls_mpi_gcd(mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B);
 
 /**
  * \brief          Modular inverse: X = A^-1 mod N
@@ -668,7 +652,7 @@ int mbedtls_mpi_gcd( mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B
  *                 MBEDTLS_ERR_MPI_BAD_INPUT_DATA if N is negative or nil
                    MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N
  */
-int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N );
+int mbedtls_mpi_inv_mod(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N);
 
 /**
  * \brief          Miller-Rabin primality test
@@ -681,9 +665,9 @@ int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi
  *                 MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
  *                 MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if X is not prime
  */
-int mbedtls_mpi_is_prime( const mbedtls_mpi *X,
-                  int (*f_rng)(void *, unsigned char *, size_t),
-                  void *p_rng );
+int mbedtls_mpi_is_prime(const mbedtls_mpi *X,
+                                     int (*f_rng)(void *, unsigned char *, size_t),
+                                     void *p_rng);
 
 /**
  * \brief          Prime number generation
@@ -699,16 +683,16 @@ int mbedtls_mpi_is_prime( const mbedtls_mpi *X,
  *                 MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
  *                 MBEDTLS_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
  */
-int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int dh_flag,
-                   int (*f_rng)(void *, unsigned char *, size_t),
-                   void *p_rng );
+int mbedtls_mpi_gen_prime(mbedtls_mpi *X, size_t nbits, int dh_flag,
+                                      int (*f_rng)(void *, unsigned char *, size_t),
+                                      void *p_rng);
 
 /**
  * \brief          Checkup routine
  *
  * \return         0 if successful, or 1 if the test failed
  */
-int mbedtls_mpi_self_test( int verbose );
+int mbedtls_mpi_self_test(int verbose);
 
 #ifdef __cplusplus
 }

+ 5 - 21
iotkit-embedded/src/tls/mbedtls-in-iotkit/include/mbedtls/bn_mul.h → iotkit-embedded/external_libs/mbedtls/include/mbedtls/bn_mul.h

@@ -1,25 +1,9 @@
-/**
- * \file bn_mul.h
- *
- * \brief  Multi-precision integer library
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
+
+
+
 /*
  *      Multiply source vector [s] with b, add result
  *       to destination vector [d] and set carry c.

+ 5 - 21
iotkit-embedded/src/tls/mbedtls-in-iotkit/include/mbedtls/check_config.h → iotkit-embedded/external_libs/mbedtls/include/mbedtls/check_config.h

@@ -1,26 +1,10 @@
-/**
- * \file check_config.h
- *
- * \brief Consistency checks for configuration options
- *
- *  Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
 
+
+
+
 /*
  * It is recommended to include this file from your config.h
  * in order to catch dependency issues early.

+ 78 - 88
iotkit-embedded/src/tls/mbedtls-in-iotkit/include/mbedtls/cipher.h → iotkit-embedded/external_libs/mbedtls/include/mbedtls/cipher.h

@@ -1,54 +1,36 @@
-/**
- * \file cipher.h
- *
- * \brief Generic cipher wrapper.
- *
- * \author Adriaan de Jong <dejong@fox-it.com>
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
 
+
+
+
 #ifndef MBEDTLS_CIPHER_H
 #define MBEDTLS_CIPHER_H
 
 #if !defined(MBEDTLS_CONFIG_FILE)
-#include "config.h"
+    #include "config.h"
 #else
-#include MBEDTLS_CONFIG_FILE
+    #include MBEDTLS_CONFIG_FILE
 #endif
 
 #include <stddef.h>
 
 #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
-#define MBEDTLS_CIPHER_MODE_AEAD
+    #define MBEDTLS_CIPHER_MODE_AEAD
 #endif
 
 #if defined(MBEDTLS_CIPHER_MODE_CBC)
-#define MBEDTLS_CIPHER_MODE_WITH_PADDING
+    #define MBEDTLS_CIPHER_MODE_WITH_PADDING
 #endif
 
 #if defined(MBEDTLS_ARC4_C)
-#define MBEDTLS_CIPHER_MODE_STREAM
+    #define MBEDTLS_CIPHER_MODE_STREAM
 #endif
 
 #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
     !defined(inline) && !defined(__cplusplus)
-#define inline __inline
+    #define inline __inline
 #endif
 
 #define MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE            -0x6080  /**< The selected feature is not available. */
@@ -196,7 +178,7 @@ typedef struct {
     unsigned int key_bitlen;
 
     /** Name of the cipher */
-    const char * name;
+    const char *name;
 
     /** IV/NONCE size, in bytes.
      *  For cipher that accept many sizes: recommended size */
@@ -228,8 +210,8 @@ typedef struct {
 
 #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
     /** Padding functions to use, if relevant for cipher mode */
-    void (*add_padding)( unsigned char *output, size_t olen, size_t data_len );
-    int (*get_padding)( unsigned char *input, size_t ilen, size_t *data_len );
+    void (*add_padding)(unsigned char *output, size_t olen, size_t data_len);
+    int (*get_padding)(unsigned char *input, size_t ilen, size_t *data_len);
 #endif
 
     /** Buffer for data that hasn't been encrypted yet */
@@ -259,7 +241,7 @@ typedef struct {
  * \return              a statically allocated array of ciphers, the last entry
  *                      is 0.
  */
-const int *mbedtls_cipher_list( void );
+const int *mbedtls_cipher_list(void);
 
 /**
  * \brief               Returns the cipher information structure associated
@@ -270,7 +252,7 @@ const int *mbedtls_cipher_list( void );
  * \return              the cipher information structure associated with the
  *                      given cipher_name, or NULL if not found.
  */
-const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string( const char *cipher_name );
+const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string(const char *cipher_name);
 
 /**
  * \brief               Returns the cipher information structure associated
@@ -281,7 +263,7 @@ const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string( const char *cipher
  * \return              the cipher information structure associated with the
  *                      given cipher_type, or NULL if not found.
  */
-const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type( const mbedtls_cipher_type_t cipher_type );
+const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type(const mbedtls_cipher_type_t cipher_type);
 
 /**
  * \brief               Returns the cipher information structure associated
@@ -295,21 +277,21 @@ const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type( const mbedtls_cipher
  * \return              the cipher information structure associated with the
  *                      given cipher_type, or NULL if not found.
  */
-const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values( const mbedtls_cipher_id_t cipher_id,
-                                              int key_bitlen,
-                                              const mbedtls_cipher_mode_t mode );
+const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values(const mbedtls_cipher_id_t cipher_id,
+        int key_bitlen,
+        const mbedtls_cipher_mode_t mode);
 
 /**
  * \brief               Initialize a cipher_context (as NONE)
  */
-void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx );
+void mbedtls_cipher_init(mbedtls_cipher_context_t *ctx);
 
 /**
  * \brief               Free and clear the cipher-specific context of ctx.
  *                      Freeing ctx itself remains the responsibility of the
  *                      caller.
  */
-void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx );
+void mbedtls_cipher_free(mbedtls_cipher_context_t *ctx);
 
 /**
  * \brief               Initialises and fills the cipher context structure with
@@ -327,7 +309,7 @@ void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx );
  *                      MBEDTLS_ERR_CIPHER_ALLOC_FAILED if allocation of the
  *                      cipher-specific context failed.
  */
-int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info );
+int mbedtls_cipher_setup(mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info);
 
 /**
  * \brief               Returns the block size of the given cipher.
@@ -337,10 +319,11 @@ int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx, const mbedtls_cipher_in
  * \return              size of the cipher's blocks, or 0 if ctx has not been
  *                      initialised.
  */
-static inline unsigned int mbedtls_cipher_get_block_size( const mbedtls_cipher_context_t *ctx )
+static inline unsigned int mbedtls_cipher_get_block_size(const mbedtls_cipher_context_t *ctx)
 {
-    if( NULL == ctx || NULL == ctx->cipher_info )
+    if (NULL == ctx || NULL == ctx->cipher_info) {
         return 0;
+    }
 
     return ctx->cipher_info->block_size;
 }
@@ -354,10 +337,11 @@ static inline unsigned int mbedtls_cipher_get_block_size( const mbedtls_cipher_c
  * \return              mode of operation, or MBEDTLS_MODE_NONE if ctx
  *                      has not been initialised.
  */
-static inline mbedtls_cipher_mode_t mbedtls_cipher_get_cipher_mode( const mbedtls_cipher_context_t *ctx )
+static inline mbedtls_cipher_mode_t mbedtls_cipher_get_cipher_mode(const mbedtls_cipher_context_t *ctx)
 {
-    if( NULL == ctx || NULL == ctx->cipher_info )
+    if (NULL == ctx || NULL == ctx->cipher_info) {
         return MBEDTLS_MODE_NONE;
+    }
 
     return ctx->cipher_info->mode;
 }
@@ -371,13 +355,15 @@ static inline mbedtls_cipher_mode_t mbedtls_cipher_get_cipher_mode( const mbedtl
  *                      (0 for ciphers not using IV/NONCE).
  *                      If IV has already been set: actual size.
  */
-static inline int mbedtls_cipher_get_iv_size( const mbedtls_cipher_context_t *ctx )
+static inline int mbedtls_cipher_get_iv_size(const mbedtls_cipher_context_t *ctx)
 {
-    if( NULL == ctx || NULL == ctx->cipher_info )
+    if (NULL == ctx || NULL == ctx->cipher_info) {
         return 0;
+    }
 
-    if( ctx->iv_size != 0 )
+    if (ctx->iv_size != 0) {
         return (int) ctx->iv_size;
+    }
 
     return (int) ctx->cipher_info->iv_size;
 }
@@ -390,10 +376,11 @@ static inline int mbedtls_cipher_get_iv_size( const mbedtls_cipher_context_t *ct
  * \return              type of the cipher, or MBEDTLS_CIPHER_NONE if ctx has
  *                      not been initialised.
  */
-static inline mbedtls_cipher_type_t mbedtls_cipher_get_type( const mbedtls_cipher_context_t *ctx )
+static inline mbedtls_cipher_type_t mbedtls_cipher_get_type(const mbedtls_cipher_context_t *ctx)
 {
-    if( NULL == ctx || NULL == ctx->cipher_info )
+    if (NULL == ctx || NULL == ctx->cipher_info) {
         return MBEDTLS_CIPHER_NONE;
+    }
 
     return ctx->cipher_info->type;
 }
@@ -405,10 +392,11 @@ static inline mbedtls_cipher_type_t mbedtls_cipher_get_type( const mbedtls_ciphe
  *
  * \return              name of the cipher, or NULL if ctx was not initialised.
  */
-static inline const char *mbedtls_cipher_get_name( const mbedtls_cipher_context_t *ctx )
+static inline const char *mbedtls_cipher_get_name(const mbedtls_cipher_context_t *ctx)
 {
-    if( NULL == ctx || NULL == ctx->cipher_info )
+    if (NULL == ctx || NULL == ctx->cipher_info) {
         return 0;
+    }
 
     return ctx->cipher_info->name;
 }
@@ -422,10 +410,11 @@ static inline const char *mbedtls_cipher_get_name( const mbedtls_cipher_context_
  *                      MBEDTLS_KEY_LENGTH_NONE if ctx has not been
  *                      initialised.
  */
-static inline int mbedtls_cipher_get_key_bitlen( const mbedtls_cipher_context_t *ctx )
+static inline int mbedtls_cipher_get_key_bitlen(const mbedtls_cipher_context_t *ctx)
 {
-    if( NULL == ctx || NULL == ctx->cipher_info )
+    if (NULL == ctx || NULL == ctx->cipher_info) {
         return MBEDTLS_KEY_LENGTH_NONE;
+    }
 
     return (int) ctx->cipher_info->key_bitlen;
 }
@@ -439,10 +428,11 @@ static inline int mbedtls_cipher_get_key_bitlen( const mbedtls_cipher_context_t
  *                      or MBEDTLS_OPERATION_NONE if ctx has not been
  *                      initialised.
  */
-static inline mbedtls_operation_t mbedtls_cipher_get_operation( const mbedtls_cipher_context_t *ctx )
+static inline mbedtls_operation_t mbedtls_cipher_get_operation(const mbedtls_cipher_context_t *ctx)
 {
-    if( NULL == ctx || NULL == ctx->cipher_info )
+    if (NULL == ctx || NULL == ctx->cipher_info) {
         return MBEDTLS_OPERATION_NONE;
+    }
 
     return ctx->operation;
 }
@@ -462,8 +452,8 @@ static inline mbedtls_operation_t mbedtls_cipher_get_operation( const mbedtls_ci
  *                      parameter verification fails or a cipher specific
  *                      error code.
  */
-int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx, const unsigned char *key,
-                   int key_bitlen, const mbedtls_operation_t operation );
+int mbedtls_cipher_setkey(mbedtls_cipher_context_t *ctx, const unsigned char *key,
+                                      int key_bitlen, const mbedtls_operation_t operation);
 
 #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
 /**
@@ -478,7 +468,7 @@ int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx, const unsigned char *k
  *                      MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA if the cipher mode
  *                      does not support padding.
  */
-int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode );
+int mbedtls_cipher_set_padding_mode(mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode);
 #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
 
 /**
@@ -494,8 +484,8 @@ int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx, mbedtls_ciph
  * \note                Some ciphers don't use IVs nor NONCE. For these
  *                      ciphers, this function has no effect.
  */
-int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
-                   const unsigned char *iv, size_t iv_len );
+int mbedtls_cipher_set_iv(mbedtls_cipher_context_t *ctx,
+                                      const unsigned char *iv, size_t iv_len);
 
 /**
  * \brief               Finish preparation of the given context
@@ -505,7 +495,7 @@ int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
  * \returns             0 on success, MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA
  *                      if parameter verification fails.
  */
-int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx );
+int mbedtls_cipher_reset(mbedtls_cipher_context_t *ctx);
 
 #if defined(MBEDTLS_GCM_C)
 /**
@@ -519,8 +509,8 @@ int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx );
  *
  * \return              0 on success, or a specific error code.
  */
-int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
-                      const unsigned char *ad, size_t ad_len );
+int mbedtls_cipher_update_ad(mbedtls_cipher_context_t *ctx,
+        const unsigned char *ad, size_t ad_len);
 #endif /* MBEDTLS_GCM_C */
 
 /**
@@ -552,8 +542,8 @@ int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
  *                      function, except the last one before mbedtls_cipher_finish(),
  *                      must have ilen a multiple of the block size.
  */
-int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
-                   size_t ilen, unsigned char *output, size_t *olen );
+int mbedtls_cipher_update(mbedtls_cipher_context_t *ctx, const unsigned char *input,
+                                      size_t ilen, unsigned char *output, size_t *olen);
 
 /**
  * \brief               Generic cipher finalisation function. If data still
@@ -572,8 +562,8 @@ int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *i
  *                      MBEDTLS_ERR_CIPHER_INVALID_PADDING on invalid padding
  *                      while decrypting or a cipher specific error code.
  */
-int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
-                   unsigned char *output, size_t *olen );
+int mbedtls_cipher_finish(mbedtls_cipher_context_t *ctx,
+                                      unsigned char *output, size_t *olen);
 
 #if defined(MBEDTLS_GCM_C)
 /**
@@ -587,8 +577,8 @@ int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
  *
  * \return              0 on success, or a specific error code.
  */
-int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
-                      unsigned char *tag, size_t tag_len );
+int mbedtls_cipher_write_tag(mbedtls_cipher_context_t *ctx,
+        unsigned char *tag, size_t tag_len);
 
 /**
  * \brief               Check tag for AEAD ciphers.
@@ -601,8 +591,8 @@ int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
  *
  * \return              0 on success, or a specific error code.
  */
-int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
-                      const unsigned char *tag, size_t tag_len );
+int mbedtls_cipher_check_tag(mbedtls_cipher_context_t *ctx,
+        const unsigned char *tag, size_t tag_len);
 #endif /* MBEDTLS_GCM_C */
 
 /**
@@ -632,10 +622,10 @@ int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
  *                      while decrypting, or
  *                      a cipher specific error code.
  */
-int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
-                  const unsigned char *iv, size_t iv_len,
-                  const unsigned char *input, size_t ilen,
-                  unsigned char *output, size_t *olen );
+int mbedtls_cipher_crypt(mbedtls_cipher_context_t *ctx,
+                                     const unsigned char *iv, size_t iv_len,
+                                     const unsigned char *input, size_t ilen,
+                                     unsigned char *output, size_t *olen);
 
 #if defined(MBEDTLS_CIPHER_MODE_AEAD)
 /**
@@ -660,12 +650,12 @@ int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
  *                      MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, or
  *                      a cipher specific error code.
  */
-int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
-                         const unsigned char *iv, size_t iv_len,
-                         const unsigned char *ad, size_t ad_len,
-                         const unsigned char *input, size_t ilen,
-                         unsigned char *output, size_t *olen,
-                         unsigned char *tag, size_t tag_len );
+int mbedtls_cipher_auth_encrypt(mbedtls_cipher_context_t *ctx,
+        const unsigned char *iv, size_t iv_len,
+        const unsigned char *ad, size_t ad_len,
+        const unsigned char *input, size_t ilen,
+        unsigned char *output, size_t *olen,
+        unsigned char *tag, size_t tag_len);
 
 /**
  * \brief               Generic autenticated decryption (AEAD ciphers).
@@ -694,12 +684,12 @@ int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
  *                      is zeroed out to prevent the unauthentic plaintext to
  *                      be used by mistake, making this interface safer.
  */
-int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
-                         const unsigned char *iv, size_t iv_len,
-                         const unsigned char *ad, size_t ad_len,
-                         const unsigned char *input, size_t ilen,
-                         unsigned char *output, size_t *olen,
-                         const unsigned char *tag, size_t tag_len );
+int mbedtls_cipher_auth_decrypt(mbedtls_cipher_context_t *ctx,
+        const unsigned char *iv, size_t iv_len,
+        const unsigned char *ad, size_t ad_len,
+        const unsigned char *input, size_t ilen,
+        unsigned char *output, size_t *olen,
+        const unsigned char *tag, size_t tag_len);
 #endif /* MBEDTLS_CIPHER_MODE_AEAD */
 
 #ifdef __cplusplus

+ 5 - 23
iotkit-embedded/src/tls/mbedtls-in-iotkit/include/mbedtls/cipher_internal.h → iotkit-embedded/external_libs/mbedtls/include/mbedtls/cipher_internal.h

@@ -1,27 +1,9 @@
-/**
- * \file cipher_internal.h
- *
- * \brief Cipher wrappers.
- *
- * \author Adriaan de Jong <dejong@fox-it.com>
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
+
+
+
 #ifndef MBEDTLS_CIPHER_WRAP_H
 #define MBEDTLS_CIPHER_WRAP_H
 

+ 9 - 32
iotkit-embedded/src/tls/mbedtls-in-iotkit/include/mbedtls/config.h → iotkit-embedded/external_libs/mbedtls/include/mbedtls/config.h

@@ -1,35 +1,12 @@
-/**
- * \file config.h
- *
- * \brief Configuration options (set of defines)
- *
- *  This set of compile-time options may be used to enable
- *  or disable features selectively, and reduce the global
- *  memory footprint.
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
 
 #ifndef MBEDTLS_CONFIG_H
 #define MBEDTLS_CONFIG_H
 
 #if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_DEPRECATE)
-#define _CRT_SECURE_NO_DEPRECATE 1
+    #define _CRT_SECURE_NO_DEPRECATE 1
 #endif
 
 /**
@@ -408,7 +385,7 @@
  *
  * Enable Cipher Feedback mode (CFB) for symmetric ciphers.
  */
-//#define MBEDTLS_CIPHER_MODE_CFB
+#define MBEDTLS_CIPHER_MODE_CFB
 
 /**
  * \def MBEDTLS_CIPHER_MODE_CTR
@@ -1150,7 +1127,7 @@
  *
  * Comment this macro to disable support for the max_fragment_length extension
  */
-//#define MBEDTLS_SSL_MAX_FRAGMENT_LENGTH
+#define MBEDTLS_SSL_MAX_FRAGMENT_LENGTH
 
 /**
  * \def MBEDTLS_SSL_PROTO_SSL3
@@ -1993,7 +1970,7 @@
  * This module is required for SSL/TLS and X.509.
  * PEM_PARSE uses MD5 for decrypting encrypted keys.
  */
-//#define MBEDTLS_MD5_C
+// #define MBEDTLS_MD5_C
 
 /**
  * \def MBEDTLS_MEMORY_BUFFER_ALLOC_C
@@ -2642,7 +2619,7 @@
 //#define YOTTA_CFG_MBEDTLS_TARGET_CONFIG_FILE "mbedtls/target_config.h"
 
 #if defined(TARGET_LIKE_MBED) && defined(YOTTA_CFG_MBEDTLS_TARGET_CONFIG_FILE)
-#include YOTTA_CFG_MBEDTLS_TARGET_CONFIG_FILE
+    #include YOTTA_CFG_MBEDTLS_TARGET_CONFIG_FILE
 #endif
 
 /*
@@ -2653,9 +2630,9 @@
  * - without yotta is looks weird to have a YOTTA prefix.
  */
 #if defined(YOTTA_CFG_MBEDTLS_USER_CONFIG_FILE)
-#include YOTTA_CFG_MBEDTLS_USER_CONFIG_FILE
+    #include YOTTA_CFG_MBEDTLS_USER_CONFIG_FILE
 #elif defined(MBEDTLS_USER_CONFIG_FILE)
-#include MBEDTLS_USER_CONFIG_FILE
+    #include MBEDTLS_USER_CONFIG_FILE
 #endif
 
 #include "check_config.h"

+ 45 - 62
iotkit-embedded/src/tls/mbedtls-in-iotkit/include/mbedtls/ctr_drbg.h → iotkit-embedded/external_libs/mbedtls/include/mbedtls/ctr_drbg.h

@@ -1,32 +1,16 @@
-/**
- * \file ctr_drbg.h
- *
- * \brief CTR_DRBG based on AES-256 (NIST SP 800-90)
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
+
+
+
 #ifndef MBEDTLS_CTR_DRBG_H
 #define MBEDTLS_CTR_DRBG_H
 
 #include "aes.h"
 
 #if defined(MBEDTLS_THREADING_C)
-#include "mbedtls/threading.h"
+    #include "mbedtls/threading.h"
 #endif
 
 #define MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED        -0x0034  /**< The entropy source failed. */
@@ -38,7 +22,7 @@
 #define MBEDTLS_CTR_DRBG_KEYSIZE            32      /**< Key size used by the cipher                    */
 #define MBEDTLS_CTR_DRBG_KEYBITS            ( MBEDTLS_CTR_DRBG_KEYSIZE * 8 )
 #define MBEDTLS_CTR_DRBG_SEEDLEN            ( MBEDTLS_CTR_DRBG_KEYSIZE + MBEDTLS_CTR_DRBG_BLOCKSIZE )
-                                            /**< The seed length (counter + AES key)            */
+/**< The seed length (counter + AES key)            */
 
 /**
  * \name SECTION: Module settings
@@ -49,27 +33,27 @@
  */
 
 #if !defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN)
-#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256)
-#define MBEDTLS_CTR_DRBG_ENTROPY_LEN        48      /**< Amount of entropy used per seed by default (48 with SHA-512, 32 with SHA-256) */
-#else
-#define MBEDTLS_CTR_DRBG_ENTROPY_LEN        32      /**< Amount of entropy used per seed by default (48 with SHA-512, 32 with SHA-256) */
-#endif
+    #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256)
+        #define MBEDTLS_CTR_DRBG_ENTROPY_LEN        48      /**< Amount of entropy used per seed by default (48 with SHA-512, 32 with SHA-256) */
+    #else
+        #define MBEDTLS_CTR_DRBG_ENTROPY_LEN        32      /**< Amount of entropy used per seed by default (48 with SHA-512, 32 with SHA-256) */
+    #endif
 #endif
 
 #if !defined(MBEDTLS_CTR_DRBG_RESEED_INTERVAL)
-#define MBEDTLS_CTR_DRBG_RESEED_INTERVAL    10000   /**< Interval before reseed is performed by default */
+    #define MBEDTLS_CTR_DRBG_RESEED_INTERVAL    10000   /**< Interval before reseed is performed by default */
 #endif
 
 #if !defined(MBEDTLS_CTR_DRBG_MAX_INPUT)
-#define MBEDTLS_CTR_DRBG_MAX_INPUT          256     /**< Maximum number of additional input bytes */
+    #define MBEDTLS_CTR_DRBG_MAX_INPUT          256     /**< Maximum number of additional input bytes */
 #endif
 
 #if !defined(MBEDTLS_CTR_DRBG_MAX_REQUEST)
-#define MBEDTLS_CTR_DRBG_MAX_REQUEST        1024    /**< Maximum number of requested bytes per call */
+    #define MBEDTLS_CTR_DRBG_MAX_REQUEST        1024    /**< Maximum number of requested bytes per call */
 #endif
 
 #if !defined(MBEDTLS_CTR_DRBG_MAX_SEED_INPUT)
-#define MBEDTLS_CTR_DRBG_MAX_SEED_INPUT     384     /**< Maximum size of (re)seed buffer */
+    #define MBEDTLS_CTR_DRBG_MAX_SEED_INPUT     384     /**< Maximum size of (re)seed buffer */
 #endif
 
 /* \} name SECTION: Module settings */
@@ -84,8 +68,7 @@ extern "C" {
 /**
  * \brief          CTR_DRBG context structure
  */
-typedef struct
-{
+typedef struct {
     unsigned char counter[16];  /*!<  counter (V)       */
     int reseed_counter;         /*!<  reseed counter    */
     int prediction_resistance;  /*!<  enable prediction resistance (Automatic
@@ -116,7 +99,7 @@ mbedtls_ctr_drbg_context;
  *
  * \param ctx           CTR_DRBG context to be initialized
  */
-void mbedtls_ctr_drbg_init( mbedtls_ctr_drbg_context *ctx );
+void mbedtls_ctr_drbg_init(mbedtls_ctr_drbg_context *ctx);
 
 /**
  * \brief               CTR_DRBG initial seeding
@@ -136,18 +119,18 @@ void mbedtls_ctr_drbg_init( mbedtls_ctr_drbg_context *ctx );
  * \return              0 if successful, or
  *                      MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED
  */
-int mbedtls_ctr_drbg_seed( mbedtls_ctr_drbg_context *ctx,
-                   int (*f_entropy)(void *, unsigned char *, size_t),
-                   void *p_entropy,
-                   const unsigned char *custom,
-                   size_t len );
+int mbedtls_ctr_drbg_seed(mbedtls_ctr_drbg_context *ctx,
+                                      int (*f_entropy)(void *, unsigned char *, size_t),
+                                      void *p_entropy,
+                                      const unsigned char *custom,
+                                      size_t len);
 
 /**
  * \brief               Clear CTR_CRBG context data
  *
  * \param ctx           CTR_DRBG context to clear
  */
-void mbedtls_ctr_drbg_free( mbedtls_ctr_drbg_context *ctx );
+void mbedtls_ctr_drbg_free(mbedtls_ctr_drbg_context *ctx);
 
 /**
  * \brief               Enable / disable prediction resistance (Default: Off)
@@ -158,8 +141,8 @@ void mbedtls_ctr_drbg_free( mbedtls_ctr_drbg_context *ctx );
  * \param ctx           CTR_DRBG context
  * \param resistance    MBEDTLS_CTR_DRBG_PR_ON or MBEDTLS_CTR_DRBG_PR_OFF
  */
-void mbedtls_ctr_drbg_set_prediction_resistance( mbedtls_ctr_drbg_context *ctx,
-                                         int resistance );
+void mbedtls_ctr_drbg_set_prediction_resistance(mbedtls_ctr_drbg_context *ctx,
+        int resistance);
 
 /**
  * \brief               Set the amount of entropy grabbed on each (re)seed
@@ -168,8 +151,8 @@ void mbedtls_ctr_drbg_set_prediction_resistance( mbedtls_ctr_drbg_context *ctx,
  * \param ctx           CTR_DRBG context
  * \param len           Amount of entropy to grab
  */
-void mbedtls_ctr_drbg_set_entropy_len( mbedtls_ctr_drbg_context *ctx,
-                               size_t len );
+void mbedtls_ctr_drbg_set_entropy_len(mbedtls_ctr_drbg_context *ctx,
+        size_t len);
 
 /**
  * \brief               Set the reseed interval
@@ -178,8 +161,8 @@ void mbedtls_ctr_drbg_set_entropy_len( mbedtls_ctr_drbg_context *ctx,
  * \param ctx           CTR_DRBG context
  * \param interval      Reseed interval
  */
-void mbedtls_ctr_drbg_set_reseed_interval( mbedtls_ctr_drbg_context *ctx,
-                                   int interval );
+void mbedtls_ctr_drbg_set_reseed_interval(mbedtls_ctr_drbg_context *ctx,
+        int interval);
 
 /**
  * \brief               CTR_DRBG reseeding (extracts data from entropy source)
@@ -191,8 +174,8 @@ void mbedtls_ctr_drbg_set_reseed_interval( mbedtls_ctr_drbg_context *ctx,
  * \return              0 if successful, or
  *                      MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED
  */
-int mbedtls_ctr_drbg_reseed( mbedtls_ctr_drbg_context *ctx,
-                     const unsigned char *additional, size_t len );
+int mbedtls_ctr_drbg_reseed(mbedtls_ctr_drbg_context *ctx,
+                                        const unsigned char *additional, size_t len);
 
 /**
  * \brief               CTR_DRBG update state
@@ -205,8 +188,8 @@ int mbedtls_ctr_drbg_reseed( mbedtls_ctr_drbg_context *ctx,
  *                      only the first MBEDTLS_CTR_DRBG_MAX_SEED_INPUT bytes are used,
  *                      the remaining ones are silently discarded.
  */
-void mbedtls_ctr_drbg_update( mbedtls_ctr_drbg_context *ctx,
-                      const unsigned char *additional, size_t add_len );
+void mbedtls_ctr_drbg_update(mbedtls_ctr_drbg_context *ctx,
+        const unsigned char *additional, size_t add_len);
 
 /**
  * \brief               CTR_DRBG generate random with additional update input
@@ -223,9 +206,9 @@ void mbedtls_ctr_drbg_update( mbedtls_ctr_drbg_context *ctx,
  *                      MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED, or
  *                      MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG
  */
-int mbedtls_ctr_drbg_random_with_add( void *p_rng,
-                              unsigned char *output, size_t output_len,
-                              const unsigned char *additional, size_t add_len );
+int mbedtls_ctr_drbg_random_with_add(void *p_rng,
+        unsigned char *output, size_t output_len,
+        const unsigned char *additional, size_t add_len);
 
 /**
  * \brief               CTR_DRBG generate random
@@ -240,8 +223,8 @@ int mbedtls_ctr_drbg_random_with_add( void *p_rng,
  *                      MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED, or
  *                      MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG
  */
-int mbedtls_ctr_drbg_random( void *p_rng,
-                     unsigned char *output, size_t output_len );
+int mbedtls_ctr_drbg_random(void *p_rng,
+                                        unsigned char *output, size_t output_len);
 
 #if defined(MBEDTLS_FS_IO)
 /**
@@ -254,7 +237,7 @@ int mbedtls_ctr_drbg_random( void *p_rng,
  *                      MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR on file error, or
  *                      MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED
  */
-int mbedtls_ctr_drbg_write_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
+int mbedtls_ctr_drbg_write_seed_file(mbedtls_ctr_drbg_context *ctx, const char *path);
 
 /**
  * \brief               Read and update a seed file. Seed is added to this
@@ -268,7 +251,7 @@ int mbedtls_ctr_drbg_write_seed_file( mbedtls_ctr_drbg_context *ctx, const char
  *                      MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or
  *                      MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG
  */
-int mbedtls_ctr_drbg_update_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
+int mbedtls_ctr_drbg_update_seed_file(mbedtls_ctr_drbg_context *ctx, const char *path);
 #endif /* MBEDTLS_FS_IO */
 
 /**
@@ -276,12 +259,12 @@ int mbedtls_ctr_drbg_update_seed_file( mbedtls_ctr_drbg_context *ctx, const char
  *
  * \return              0 if successful, or 1 if the test failed
  */
-int mbedtls_ctr_drbg_self_test( int verbose );
+int mbedtls_ctr_drbg_self_test(int verbose);
 
 /* Internal functions (do not call directly) */
-int mbedtls_ctr_drbg_seed_entropy_len( mbedtls_ctr_drbg_context *,
-                               int (*)(void *, unsigned char *, size_t), void *,
-                               const unsigned char *, size_t, size_t );
+int mbedtls_ctr_drbg_seed_entropy_len(mbedtls_ctr_drbg_context *,
+        int (*)(void *, unsigned char *, size_t), void *,
+        const unsigned char *, size_t, size_t);
 
 #ifdef __cplusplus
 }

+ 29 - 43
iotkit-embedded/src/tls/mbedtls-in-iotkit/include/mbedtls/debug.h → iotkit-embedded/external_libs/mbedtls/include/mbedtls/debug.h

@@ -1,38 +1,24 @@
-/**
- * \file debug.h
- *
- * \brief Functions for controlling and providing debug output from the library.
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
+
+
+
 #ifndef MBEDTLS_DEBUG_H
 #define MBEDTLS_DEBUG_H
 
 #if !defined(MBEDTLS_CONFIG_FILE)
-#include "config.h"
+    #include "config.h"
 #else
-#include MBEDTLS_CONFIG_FILE
+    #include MBEDTLS_CONFIG_FILE
 #endif
 
 #include "ssl.h"
 
+#define tls_info(...)     log_info("tls", __VA_ARGS__)
+
 #if defined(MBEDTLS_ECP_C)
-#include "ecp.h"
+    #include "ecp.h"
 #endif
 
 #if defined(MBEDTLS_DEBUG_C)
@@ -94,7 +80,7 @@ extern "C" {
  *                              - 3 Informational
  *                              - 4 Verbose
  */
-void mbedtls_debug_set_threshold( int threshold );
+void mbedtls_debug_set_threshold(int threshold);
 
 /**
  * \brief    Print a message to the debug output. This function is always used
@@ -111,9 +97,9 @@ void mbedtls_debug_set_threshold( int threshold );
  * \attention       This function is intended for INTERNAL usage within the
  *                  library only.
  */
-void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level,
-                              const char *file, int line,
-                              const char *format, ... );
+void mbedtls_debug_print_msg(const mbedtls_ssl_context *ssl, int level,
+        const char *file, int line,
+        const char *format, ...);
 
 /**
  * \brief   Print the return value of a function to the debug output. This
@@ -130,9 +116,9 @@ void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level,
  * \attention       This function is intended for INTERNAL usage within the
  *                  library only.
  */
-void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level,
-                      const char *file, int line,
-                      const char *text, int ret );
+void mbedtls_debug_print_ret(const mbedtls_ssl_context *ssl, int level,
+        const char *file, int line,
+        const char *text, int ret);
 
 /**
  * \brief   Output a buffer of size len bytes to the debug output. This function
@@ -151,9 +137,9 @@ void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level,
  * \attention       This function is intended for INTERNAL usage within the
  *                  library only.
  */
-void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level,
-                      const char *file, int line, const char *text,
-                      const unsigned char *buf, size_t len );
+void mbedtls_debug_print_buf(const mbedtls_ssl_context *ssl, int level,
+        const char *file, int line, const char *text,
+        const unsigned char *buf, size_t len);
 
 #if defined(MBEDTLS_BIGNUM_C)
 /**
@@ -172,9 +158,9 @@ void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level,
  * \attention       This function is intended for INTERNAL usage within the
  *                  library only.
  */
-void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level,
-                      const char *file, int line,
-                      const char *text, const mbedtls_mpi *X );
+void mbedtls_debug_print_mpi(const mbedtls_ssl_context *ssl, int level,
+        const char *file, int line,
+        const char *text, const mbedtls_mpi *X);
 #endif
 
 #if defined(MBEDTLS_ECP_C)
@@ -194,9 +180,9 @@ void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level,
  * \attention       This function is intended for INTERNAL usage within the
  *                  library only.
  */
-void mbedtls_debug_print_ecp( const mbedtls_ssl_context *ssl, int level,
-                      const char *file, int line,
-                      const char *text, const mbedtls_ecp_point *X );
+void mbedtls_debug_print_ecp(const mbedtls_ssl_context *ssl, int level,
+        const char *file, int line,
+        const char *text, const mbedtls_ecp_point *X);
 #endif
 
 #if defined(MBEDTLS_X509_CRT_PARSE_C)
@@ -215,9 +201,9 @@ void mbedtls_debug_print_ecp( const mbedtls_ssl_context *ssl, int level,
  * \attention       This function is intended for INTERNAL usage within the
  *                  library only.
  */
-void mbedtls_debug_print_crt( const mbedtls_ssl_context *ssl, int level,
-                      const char *file, int line,
-                      const char *text, const mbedtls_x509_crt *crt );
+void mbedtls_debug_print_crt(const mbedtls_ssl_context *ssl, int level,
+        const char *file, int line,
+        const char *text, const mbedtls_x509_crt *crt);
 #endif
 
 #ifdef __cplusplus

+ 47 - 65
iotkit-embedded/src/tls/mbedtls-in-iotkit/include/mbedtls/des.h → iotkit-embedded/external_libs/mbedtls/include/mbedtls/des.h

@@ -1,32 +1,16 @@
-/**
- * \file des.h
- *
- * \brief DES block cipher
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
+
+
+
 #ifndef MBEDTLS_DES_H
 #define MBEDTLS_DES_H
 
 #if !defined(MBEDTLS_CONFIG_FILE)
-#include "config.h"
+    #include "config.h"
 #else
-#include MBEDTLS_CONFIG_FILE
+    #include MBEDTLS_CONFIG_FILE
 #endif
 
 #include <stddef.h>
@@ -50,8 +34,7 @@ extern "C" {
 /**
  * \brief          DES context structure
  */
-typedef struct
-{
+typedef struct {
     uint32_t sk[32];            /*!<  DES subkeys       */
 }
 mbedtls_des_context;
@@ -59,8 +42,7 @@ mbedtls_des_context;
 /**
  * \brief          Triple-DES context structure
  */
-typedef struct
-{
+typedef struct {
     uint32_t sk[96];            /*!<  3DES subkeys      */
 }
 mbedtls_des3_context;
@@ -70,28 +52,28 @@ mbedtls_des3_context;
  *
  * \param ctx      DES context to be initialized
  */
-void mbedtls_des_init( mbedtls_des_context *ctx );
+void mbedtls_des_init(mbedtls_des_context *ctx);
 
 /**
  * \brief          Clear DES context
  *
  * \param ctx      DES context to be cleared
  */
-void mbedtls_des_free( mbedtls_des_context *ctx );
+void mbedtls_des_free(mbedtls_des_context *ctx);
 
 /**
  * \brief          Initialize Triple-DES context
  *
  * \param ctx      DES3 context to be initialized
  */
-void mbedtls_des3_init( mbedtls_des3_context *ctx );
+void mbedtls_des3_init(mbedtls_des3_context *ctx);
 
 /**
  * \brief          Clear Triple-DES context
  *
  * \param ctx      DES3 context to be cleared
  */
-void mbedtls_des3_free( mbedtls_des3_context *ctx );
+void mbedtls_des3_free(mbedtls_des3_context *ctx);
 
 /**
  * \brief          Set key parity on the given key to odd.
@@ -101,7 +83,7 @@ void mbedtls_des3_free( mbedtls_des3_context *ctx );
  *
  * \param key      8-byte secret key
  */
-void mbedtls_des_key_set_parity( unsigned char key[MBEDTLS_DES_KEY_SIZE] );
+void mbedtls_des_key_set_parity(unsigned char key[MBEDTLS_DES_KEY_SIZE]);
 
 /**
  * \brief          Check that key parity on the given key is odd.
@@ -113,7 +95,7 @@ void mbedtls_des_key_set_parity( unsigned char key[MBEDTLS_DES_KEY_SIZE] );
  *
  * \return         0 is parity was ok, 1 if parity was not correct.
  */
-int mbedtls_des_key_check_key_parity( const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
+int mbedtls_des_key_check_key_parity(const unsigned char key[MBEDTLS_DES_KEY_SIZE]);
 
 /**
  * \brief          Check that key is not a weak or semi-weak DES key
@@ -122,7 +104,7 @@ int mbedtls_des_key_check_key_parity( const unsigned char key[MBEDTLS_DES_KEY_SI
  *
  * \return         0 if no weak key was found, 1 if a weak key was identified.
  */
-int mbedtls_des_key_check_weak( const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
+int mbedtls_des_key_check_weak(const unsigned char key[MBEDTLS_DES_KEY_SIZE]);
 
 /**
  * \brief          DES key schedule (56-bit, encryption)
@@ -132,7 +114,7 @@ int mbedtls_des_key_check_weak( const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
  *
  * \return         0
  */
-int mbedtls_des_setkey_enc( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
+int mbedtls_des_setkey_enc(mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE]);
 
 /**
  * \brief          DES key schedule (56-bit, decryption)
@@ -142,7 +124,7 @@ int mbedtls_des_setkey_enc( mbedtls_des_context *ctx, const unsigned char key[MB
  *
  * \return         0
  */
-int mbedtls_des_setkey_dec( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
+int mbedtls_des_setkey_dec(mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE]);
 
 /**
  * \brief          Triple-DES key schedule (112-bit, encryption)
@@ -152,8 +134,8 @@ int mbedtls_des_setkey_dec( mbedtls_des_context *ctx, const unsigned char key[MB
  *
  * \return         0
  */
-int mbedtls_des3_set2key_enc( mbedtls_des3_context *ctx,
-                      const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] );
+int mbedtls_des3_set2key_enc(mbedtls_des3_context *ctx,
+        const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2]);
 
 /**
  * \brief          Triple-DES key schedule (112-bit, decryption)
@@ -163,8 +145,8 @@ int mbedtls_des3_set2key_enc( mbedtls_des3_context *ctx,
  *
  * \return         0
  */
-int mbedtls_des3_set2key_dec( mbedtls_des3_context *ctx,
-                      const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] );
+int mbedtls_des3_set2key_dec(mbedtls_des3_context *ctx,
+        const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2]);
 
 /**
  * \brief          Triple-DES key schedule (168-bit, encryption)
@@ -174,8 +156,8 @@ int mbedtls_des3_set2key_dec( mbedtls_des3_context *ctx,
  *
  * \return         0
  */
-int mbedtls_des3_set3key_enc( mbedtls_des3_context *ctx,
-                      const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] );
+int mbedtls_des3_set3key_enc(mbedtls_des3_context *ctx,
+        const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3]);
 
 /**
  * \brief          Triple-DES key schedule (168-bit, decryption)
@@ -185,8 +167,8 @@ int mbedtls_des3_set3key_enc( mbedtls_des3_context *ctx,
  *
  * \return         0
  */
-int mbedtls_des3_set3key_dec( mbedtls_des3_context *ctx,
-                      const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] );
+int mbedtls_des3_set3key_dec(mbedtls_des3_context *ctx,
+        const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3]);
 
 /**
  * \brief          DES-ECB block encryption/decryption
@@ -197,9 +179,9 @@ int mbedtls_des3_set3key_dec( mbedtls_des3_context *ctx,
  *
  * \return         0 if successful
  */
-int mbedtls_des_crypt_ecb( mbedtls_des_context *ctx,
-                    const unsigned char input[8],
-                    unsigned char output[8] );
+int mbedtls_des_crypt_ecb(mbedtls_des_context *ctx,
+                                      const unsigned char input[8],
+                                      unsigned char output[8]);
 
 #if defined(MBEDTLS_CIPHER_MODE_CBC)
 /**
@@ -220,12 +202,12 @@ int mbedtls_des_crypt_ecb( mbedtls_des_context *ctx,
  * \param input    buffer holding the input data
  * \param output   buffer holding the output data
  */
-int mbedtls_des_crypt_cbc( mbedtls_des_context *ctx,
-                    int mode,
-                    size_t length,
-                    unsigned char iv[8],
-                    const unsigned char *input,
-                    unsigned char *output );
+int mbedtls_des_crypt_cbc(mbedtls_des_context *ctx,
+                                      int mode,
+                                      size_t length,
+                                      unsigned char iv[8],
+                                      const unsigned char *input,
+                                      unsigned char *output);
 #endif /* MBEDTLS_CIPHER_MODE_CBC */
 
 /**
@@ -237,9 +219,9 @@ int mbedtls_des_crypt_cbc( mbedtls_des_context *ctx,
  *
  * \return         0 if successful
  */
-int mbedtls_des3_crypt_ecb( mbedtls_des3_context *ctx,
-                     const unsigned char input[8],
-                     unsigned char output[8] );
+int mbedtls_des3_crypt_ecb(mbedtls_des3_context *ctx,
+                                       const unsigned char input[8],
+                                       unsigned char output[8]);
 
 #if defined(MBEDTLS_CIPHER_MODE_CBC)
 /**
@@ -262,12 +244,12 @@ int mbedtls_des3_crypt_ecb( mbedtls_des3_context *ctx,
  *
  * \return         0 if successful, or MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH
  */
-int mbedtls_des3_crypt_cbc( mbedtls_des3_context *ctx,
-                     int mode,
-                     size_t length,
-                     unsigned char iv[8],
-                     const unsigned char *input,
-                     unsigned char *output );
+int mbedtls_des3_crypt_cbc(mbedtls_des3_context *ctx,
+                                       int mode,
+                                       size_t length,
+                                       unsigned char iv[8],
+                                       const unsigned char *input,
+                                       unsigned char *output);
 #endif /* MBEDTLS_CIPHER_MODE_CBC */
 
 /**
@@ -278,8 +260,8 @@ int mbedtls_des3_crypt_cbc( mbedtls_des3_context *ctx,
  * \param SK       Round keys
  * \param key      Base key
  */
-void mbedtls_des_setkey( uint32_t SK[32],
-                         const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
+void mbedtls_des_setkey(uint32_t SK[32],
+                                    const unsigned char key[MBEDTLS_DES_KEY_SIZE]);
 #ifdef __cplusplus
 }
 #endif
@@ -297,7 +279,7 @@ extern "C" {
  *
  * \return         0 if successful, or 1 if the test failed
  */
-int mbedtls_des_self_test( int verbose );
+int mbedtls_des_self_test(int verbose);
 
 #ifdef __cplusplus
 }

+ 63 - 84
iotkit-embedded/src/tls/mbedtls-in-iotkit/include/mbedtls/ecp.h → iotkit-embedded/external_libs/mbedtls/include/mbedtls/ecp.h

@@ -1,25 +1,9 @@
-/**
- * \file ecp.h
- *
- * \brief Elliptic curves over GF(p)
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
+
+
+
 #ifndef MBEDTLS_ECP_H
 #define MBEDTLS_ECP_H
 
@@ -59,8 +43,7 @@ extern "C" {
  * parameters. Therefore, only well-known domain parameters from trusted
  * sources should be used. See mbedtls_ecp_group_load().
  */
-typedef enum
-{
+typedef enum {
     MBEDTLS_ECP_DP_NONE = 0,
     MBEDTLS_ECP_DP_SECP192R1,      /*!< 192-bits NIST curve  */
     MBEDTLS_ECP_DP_SECP224R1,      /*!< 224-bits NIST curve  */
@@ -86,8 +69,7 @@ typedef enum
 /**
  * Curve information for use by other modules
  */
-typedef struct
-{
+typedef struct {
     mbedtls_ecp_group_id grp_id;    /*!< Internal identifier        */
     uint16_t tls_id;                /*!< TLS NamedCurve identifier  */
     uint16_t bit_size;              /*!< Curve size in bits         */
@@ -103,8 +85,7 @@ typedef struct
  *                  The point is zero, or "at infinity", if Z == 0.
  *                  Otherwise, X and Y are its standard (affine) coordinates.
  */
-typedef struct
-{
+typedef struct {
     mbedtls_mpi X;          /*!<  the point's X coordinate  */
     mbedtls_mpi Y;          /*!<  the point's Y coordinate  */
     mbedtls_mpi Z;          /*!<  the point's Z coordinate  */
@@ -135,8 +116,7 @@ mbedtls_ecp_point;
  * range by a few additions or substractions. It must return 0 on success and
  * non-zero on failure.
  */
-typedef struct
-{
+typedef struct {
     mbedtls_ecp_group_id id;    /*!<  internal group identifier                     */
     mbedtls_mpi P;              /*!<  prime modulus of the base field               */
     mbedtls_mpi A;              /*!<  1. A in the equation, or 2. (A + 2) / 4       */
@@ -162,8 +142,7 @@ mbedtls_ecp_group;
  *
  * \note Members purposefully in the same order as struc mbedtls_ecdsa_context.
  */
-typedef struct
-{
+typedef struct {
     mbedtls_ecp_group grp;      /*!<  Elliptic curve and base point     */
     mbedtls_mpi d;              /*!<  our secret value                  */
     mbedtls_ecp_point Q;        /*!<  our public value                  */
@@ -246,7 +225,7 @@ mbedtls_ecp_keypair;
  *
  * \return          A statically allocated array, the last entry is 0.
  */
-const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list( void );
+const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list(void);
 
 /**
  * \brief           Get the list of supported curves in order of preferrence
@@ -255,7 +234,7 @@ const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list( void );
  * \return          A statically allocated array,
  *                  terminated with MBEDTLS_ECP_DP_NONE.
  */
-const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list( void );
+const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list(void);
 
 /**
  * \brief           Get curve information from an internal group identifier
@@ -264,7 +243,7 @@ const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list( void );
  *
  * \return          The associated curve information or NULL
  */
-const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_grp_id( mbedtls_ecp_group_id grp_id );
+const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_grp_id(mbedtls_ecp_group_id grp_id);
 
 /**
  * \brief           Get curve information from a TLS NamedCurve value
@@ -273,7 +252,7 @@ const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_grp_id( mbedtls_ecp_gr
  *
  * \return          The associated curve information or NULL
  */
-const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id( uint16_t tls_id );
+const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id(uint16_t tls_id);
 
 /**
  * \brief           Get curve information from a human-readable name
@@ -282,37 +261,37 @@ const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id( uint16_t tls_i
  *
  * \return          The associated curve information or NULL
  */
-const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name( const char *name );
+const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name(const char *name);
 
 /**
  * \brief           Initialize a point (as zero)
  */
-void mbedtls_ecp_point_init( mbedtls_ecp_point *pt );
+void mbedtls_ecp_point_init(mbedtls_ecp_point *pt);
 
 /**
  * \brief           Initialize a group (to something meaningless)
  */
-void mbedtls_ecp_group_init( mbedtls_ecp_group *grp );
+void mbedtls_ecp_group_init(mbedtls_ecp_group *grp);
 
 /**
  * \brief           Initialize a key pair (as an invalid one)
  */
-void mbedtls_ecp_keypair_init( mbedtls_ecp_keypair *key );
+void mbedtls_ecp_keypair_init(mbedtls_ecp_keypair *key);
 
 /**
  * \brief           Free the components of a point
  */
-void mbedtls_ecp_point_free( mbedtls_ecp_point *pt );
+void mbedtls_ecp_point_free(mbedtls_ecp_point *pt);
 
 /**
  * \brief           Free the components of an ECP group
  */
-void mbedtls_ecp_group_free( mbedtls_ecp_group *grp );
+void mbedtls_ecp_group_free(mbedtls_ecp_group *grp);
 
 /**
  * \brief           Free the components of a key pair
  */
-void mbedtls_ecp_keypair_free( mbedtls_ecp_keypair *key );
+void mbedtls_ecp_keypair_free(mbedtls_ecp_keypair *key);
 
 /**
  * \brief           Copy the contents of point Q into P
@@ -323,7 +302,7 @@ void mbedtls_ecp_keypair_free( mbedtls_ecp_keypair *key );
  * \return          0 if successful,
  *                  MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  */
-int mbedtls_ecp_copy( mbedtls_ecp_point *P, const mbedtls_ecp_point *Q );
+int mbedtls_ecp_copy(mbedtls_ecp_point *P, const mbedtls_ecp_point *Q);
 
 /**
  * \brief           Copy the contents of a group object
@@ -334,7 +313,7 @@ int mbedtls_ecp_copy( mbedtls_ecp_point *P, const mbedtls_ecp_point *Q );
  * \return          0 if successful,
  *                  MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  */
-int mbedtls_ecp_group_copy( mbedtls_ecp_group *dst, const mbedtls_ecp_group *src );
+int mbedtls_ecp_group_copy(mbedtls_ecp_group *dst, const mbedtls_ecp_group *src);
 
 /**
  * \brief           Set a point to zero
@@ -344,7 +323,7 @@ int mbedtls_ecp_group_copy( mbedtls_ecp_group *dst, const mbedtls_ecp_group *src
  * \return          0 if successful,
  *                  MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  */
-int mbedtls_ecp_set_zero( mbedtls_ecp_point *pt );
+int mbedtls_ecp_set_zero(mbedtls_ecp_point *pt);
 
 /**
  * \brief           Tell if a point is zero
@@ -353,7 +332,7 @@ int mbedtls_ecp_set_zero( mbedtls_ecp_point *pt );
  *
  * \return          1 if point is zero, 0 otherwise
  */
-int mbedtls_ecp_is_zero( mbedtls_ecp_point *pt );
+int mbedtls_ecp_is_zero(mbedtls_ecp_point *pt);
 
 /**
  * \brief           Compare two points
@@ -367,8 +346,8 @@ int mbedtls_ecp_is_zero( mbedtls_ecp_point *pt );
  * \return          0 if the points are equal,
  *                  MBEDTLS_ERR_ECP_BAD_INPUT_DATA otherwise
  */
-int mbedtls_ecp_point_cmp( const mbedtls_ecp_point *P,
-                           const mbedtls_ecp_point *Q );
+int mbedtls_ecp_point_cmp(const mbedtls_ecp_point *P,
+                                      const mbedtls_ecp_point *Q);
 
 /**
  * \brief           Import a non-zero point from two ASCII strings
@@ -380,8 +359,8 @@ int mbedtls_ecp_point_cmp( const mbedtls_ecp_point *P,
  *
  * \return          0 if successful, or a MBEDTLS_ERR_MPI_XXX error code
  */
-int mbedtls_ecp_point_read_string( mbedtls_ecp_point *P, int radix,
-                           const char *x, const char *y );
+int mbedtls_ecp_point_read_string(mbedtls_ecp_point *P, int radix,
+        const char *x, const char *y);
 
 /**
  * \brief           Export a point into unsigned binary data
@@ -397,9 +376,9 @@ int mbedtls_ecp_point_read_string( mbedtls_ecp_point *P, int radix,
  *                  or MBEDTLS_ERR_ECP_BAD_INPUT_DATA
  *                  or MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL
  */
-int mbedtls_ecp_point_write_binary( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *P,
-                            int format, size_t *olen,
-                            unsigned char *buf, size_t buflen );
+int mbedtls_ecp_point_write_binary(const mbedtls_ecp_group *grp, const mbedtls_ecp_point *P,
+        int format, size_t *olen,
+        unsigned char *buf, size_t buflen);
 
 /**
  * \brief           Import a point from unsigned binary data
@@ -419,8 +398,8 @@ int mbedtls_ecp_point_write_binary( const mbedtls_ecp_group *grp, const mbedtls_
  *                  belongs to the given group, see mbedtls_ecp_check_pubkey() for
  *                  that.
  */
-int mbedtls_ecp_point_read_binary( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P,
-                           const unsigned char *buf, size_t ilen );
+int mbedtls_ecp_point_read_binary(const mbedtls_ecp_group *grp, mbedtls_ecp_point *P,
+        const unsigned char *buf, size_t ilen);
 
 /**
  * \brief           Import a point from a TLS ECPoint record
@@ -436,8 +415,8 @@ int mbedtls_ecp_point_read_binary( const mbedtls_ecp_group *grp, mbedtls_ecp_poi
  *                  MBEDTLS_ERR_MPI_XXX if initialization failed
  *                  MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid
  */
-int mbedtls_ecp_tls_read_point( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
-                        const unsigned char **buf, size_t len );
+int mbedtls_ecp_tls_read_point(const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
+        const unsigned char **buf, size_t len);
 
 /**
  * \brief           Export a point as a TLS ECPoint record
@@ -453,9 +432,9 @@ int mbedtls_ecp_tls_read_point( const mbedtls_ecp_group *grp, mbedtls_ecp_point
  *                  or MBEDTLS_ERR_ECP_BAD_INPUT_DATA
  *                  or MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL
  */
-int mbedtls_ecp_tls_write_point( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt,
-                         int format, size_t *olen,
-                         unsigned char *buf, size_t blen );
+int mbedtls_ecp_tls_write_point(const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt,
+        int format, size_t *olen,
+        unsigned char *buf, size_t blen);
 
 /**
  * \brief           Set a group using well-known domain parameters
@@ -470,7 +449,7 @@ int mbedtls_ecp_tls_write_point( const mbedtls_ecp_group *grp, const mbedtls_ecp
  * \note            Index should be a value of RFC 4492's enum NamedCurve,
  *                  usually in the form of a MBEDTLS_ECP_DP_XXX macro.
  */
-int mbedtls_ecp_group_load( mbedtls_ecp_group *grp, mbedtls_ecp_group_id index );
+int mbedtls_ecp_group_load(mbedtls_ecp_group *grp, mbedtls_ecp_group_id index);
 
 /**
  * \brief           Set a group from a TLS ECParameters record
@@ -485,7 +464,7 @@ int mbedtls_ecp_group_load( mbedtls_ecp_group *grp, mbedtls_ecp_group_id index )
  *                  MBEDTLS_ERR_MPI_XXX if initialization failed
  *                  MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid
  */
-int mbedtls_ecp_tls_read_group( mbedtls_ecp_group *grp, const unsigned char **buf, size_t len );
+int mbedtls_ecp_tls_read_group(mbedtls_ecp_group *grp, const unsigned char **buf, size_t len);
 
 /**
  * \brief           Write the TLS ECParameters record for a group
@@ -498,8 +477,8 @@ int mbedtls_ecp_tls_read_group( mbedtls_ecp_group *grp, const unsigned char **bu
  * \return          0 if successful,
  *                  or MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL
  */
-int mbedtls_ecp_tls_write_group( const mbedtls_ecp_group *grp, size_t *olen,
-                         unsigned char *buf, size_t blen );
+int mbedtls_ecp_tls_write_group(const mbedtls_ecp_group *grp, size_t *olen,
+        unsigned char *buf, size_t blen);
 
 /**
  * \brief           Multiplication by an integer: R = m * P
@@ -527,9 +506,9 @@ int mbedtls_ecp_tls_write_group( const mbedtls_ecp_group *grp, size_t *olen,
  *                  or P is not a valid pubkey,
  *                  MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  */
-int mbedtls_ecp_mul( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
-             const mbedtls_mpi *m, const mbedtls_ecp_point *P,
-             int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
+int mbedtls_ecp_mul(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
+                                const mbedtls_mpi *m, const mbedtls_ecp_point *P,
+                                int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
 
 /**
  * \brief           Multiplication and addition of two points by integers:
@@ -551,9 +530,9 @@ int mbedtls_ecp_mul( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
  *                  or P or Q is not a valid pubkey,
  *                  MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  */
-int mbedtls_ecp_muladd( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
-             const mbedtls_mpi *m, const mbedtls_ecp_point *P,
-             const mbedtls_mpi *n, const mbedtls_ecp_point *Q );
+int mbedtls_ecp_muladd(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
+                                   const mbedtls_mpi *m, const mbedtls_ecp_point *P,
+                                   const mbedtls_mpi *n, const mbedtls_ecp_point *Q);
 
 /**
  * \brief           Check that a point is a valid public key on this curve
@@ -576,7 +555,7 @@ int mbedtls_ecp_muladd( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
  *                  in order to ease use with other structures such as
  *                  mbedtls_ecdh_context of mbedtls_ecdsa_context.
  */
-int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt );
+int mbedtls_ecp_check_pubkey(const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt);
 
 /**
  * \brief           Check that an mbedtls_mpi is a valid private key for this curve
@@ -591,7 +570,7 @@ int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp, const mbedtls_ecp_po
  *                  in order to ease use with other structures such as
  *                  mbedtls_ecdh_context of mbedtls_ecdsa_context.
  */
-int mbedtls_ecp_check_privkey( const mbedtls_ecp_group *grp, const mbedtls_mpi *d );
+int mbedtls_ecp_check_privkey(const mbedtls_ecp_group *grp, const mbedtls_mpi *d);
 
 /**
  * \brief           Generate a keypair with configurable base point
@@ -610,11 +589,11 @@ int mbedtls_ecp_check_privkey( const mbedtls_ecp_group *grp, const mbedtls_mpi *
  *                  in order to ease use with other structures such as
  *                  mbedtls_ecdh_context of mbedtls_ecdsa_context.
  */
-int mbedtls_ecp_gen_keypair_base( mbedtls_ecp_group *grp,
-                     const mbedtls_ecp_point *G,
-                     mbedtls_mpi *d, mbedtls_ecp_point *Q,
-                     int (*f_rng)(void *, unsigned char *, size_t),
-                     void *p_rng );
+int mbedtls_ecp_gen_keypair_base(mbedtls_ecp_group *grp,
+        const mbedtls_ecp_point *G,
+        mbedtls_mpi *d, mbedtls_ecp_point *Q,
+        int (*f_rng)(void *, unsigned char *, size_t),
+        void *p_rng);
 
 /**
  * \brief           Generate a keypair
@@ -632,9 +611,9 @@ int mbedtls_ecp_gen_keypair_base( mbedtls_ecp_group *grp,
  *                  in order to ease use with other structures such as
  *                  mbedtls_ecdh_context of mbedtls_ecdsa_context.
  */
-int mbedtls_ecp_gen_keypair( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
-                     int (*f_rng)(void *, unsigned char *, size_t),
-                     void *p_rng );
+int mbedtls_ecp_gen_keypair(mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
+                                        int (*f_rng)(void *, unsigned char *, size_t),
+                                        void *p_rng);
 
 /**
  * \brief           Generate a keypair
@@ -647,8 +626,8 @@ int mbedtls_ecp_gen_keypair( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp
  * \return          0 if successful,
  *                  or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
  */
-int mbedtls_ecp_gen_key( mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
-                int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
+int mbedtls_ecp_gen_key(mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
+                                    int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
 
 /**
  * \brief           Check a public-private key pair
@@ -660,7 +639,7 @@ int mbedtls_ecp_gen_key( mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
  *                  MBEDTLS_ERR_ECP_BAD_INPUT_DATA, or
  *                  a MBEDTLS_ERR_ECP_XXX or MBEDTLS_ERR_MPI_XXX code.
  */
-int mbedtls_ecp_check_pub_priv( const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv );
+int mbedtls_ecp_check_pub_priv(const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv);
 
 #if defined(MBEDTLS_SELF_TEST)
 
@@ -669,7 +648,7 @@ int mbedtls_ecp_check_pub_priv( const mbedtls_ecp_keypair *pub, const mbedtls_ec
  *
  * \return         0 if successful, or 1 if a test failed
  */
-int mbedtls_ecp_self_test( int verbose );
+int mbedtls_ecp_self_test(int verbose);
 
 #endif /* MBEDTLS_SELF_TEST */
 

+ 37 - 55
iotkit-embedded/src/tls/mbedtls-in-iotkit/include/mbedtls/entropy.h → iotkit-embedded/external_libs/mbedtls/include/mbedtls/entropy.h

@@ -1,52 +1,36 @@
-/**
- * \file entropy.h
- *
- * \brief Entropy accumulator implementation
- *
- *  Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
+
+
+
 #ifndef MBEDTLS_ENTROPY_H
 #define MBEDTLS_ENTROPY_H
 
 #if !defined(MBEDTLS_CONFIG_FILE)
-#include "config.h"
+    #include "config.h"
 #else
-#include MBEDTLS_CONFIG_FILE
+    #include MBEDTLS_CONFIG_FILE
 #endif
 
 #include <stddef.h>
 
 #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256)
-#include "sha512.h"
-#define MBEDTLS_ENTROPY_SHA512_ACCUMULATOR
+    #include "sha512.h"
+    #define MBEDTLS_ENTROPY_SHA512_ACCUMULATOR
 #else
-#if defined(MBEDTLS_SHA256_C)
-#define MBEDTLS_ENTROPY_SHA256_ACCUMULATOR
-#include "sha256.h"
-#endif
+    #if defined(MBEDTLS_SHA256_C)
+        #define MBEDTLS_ENTROPY_SHA256_ACCUMULATOR
+        #include "sha256.h"
+    #endif
 #endif
 
 #if defined(MBEDTLS_THREADING_C)
-#include "threading.h"
+    #include "threading.h"
 #endif
 
 #if defined(MBEDTLS_HAVEGE_C)
-#include "havege.h"
+    #include "havege.h"
 #endif
 
 #define MBEDTLS_ERR_ENTROPY_SOURCE_FAILED                 -0x003C  /**< Critical entropy source failure. */
@@ -64,19 +48,19 @@
  */
 
 #if !defined(MBEDTLS_ENTROPY_MAX_SOURCES)
-#define MBEDTLS_ENTROPY_MAX_SOURCES     20      /**< Maximum number of sources supported */
+    #define MBEDTLS_ENTROPY_MAX_SOURCES     20      /**< Maximum number of sources supported */
 #endif
 
 #if !defined(MBEDTLS_ENTROPY_MAX_GATHER)
-#define MBEDTLS_ENTROPY_MAX_GATHER      128     /**< Maximum amount requested from entropy sources */
+    #define MBEDTLS_ENTROPY_MAX_GATHER      128     /**< Maximum amount requested from entropy sources */
 #endif
 
 /* \} name SECTION: Module settings */
 
 #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
-#define MBEDTLS_ENTROPY_BLOCK_SIZE      64      /**< Block size of entropy accumulator (SHA-512) */
+    #define MBEDTLS_ENTROPY_BLOCK_SIZE      64      /**< Block size of entropy accumulator (SHA-512) */
 #else
-#define MBEDTLS_ENTROPY_BLOCK_SIZE      32      /**< Block size of entropy accumulator (SHA-256) */
+    #define MBEDTLS_ENTROPY_BLOCK_SIZE      32      /**< Block size of entropy accumulator (SHA-256) */
 #endif
 
 #define MBEDTLS_ENTROPY_MAX_SEED_SIZE   1024    /**< Maximum size of seed we read from seed file */
@@ -101,15 +85,14 @@ extern "C" {
  *                  MBEDTLS_ERR_ENTROPY_SOURCE_FAILED otherwise
  */
 typedef int (*mbedtls_entropy_f_source_ptr)(void *data, unsigned char *output, size_t len,
-                            size_t *olen);
+        size_t *olen);
 
 /**
  * \brief           Entropy source state
  */
-typedef struct
-{
+typedef struct {
     mbedtls_entropy_f_source_ptr    f_source;   /**< The entropy source callback */
-    void *          p_source;   /**< The callback data pointer */
+    void           *p_source;   /**< The callback data pointer */
     size_t          size;       /**< Amount received in bytes */
     size_t          threshold;  /**< Minimum bytes required before release */
     int             strong;     /**< Is the source strong? */
@@ -119,8 +102,7 @@ mbedtls_entropy_source_state;
 /**
  * \brief           Entropy context structure
  */
-typedef struct
-{
+typedef struct {
 #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
     mbedtls_sha512_context  accumulator;
 #else
@@ -145,14 +127,14 @@ mbedtls_entropy_context;
  *
  * \param ctx       Entropy context to initialize
  */
-void mbedtls_entropy_init( mbedtls_entropy_context *ctx );
+void mbedtls_entropy_init(mbedtls_entropy_context *ctx);
 
 /**
  * \brief           Free the data in the context
  *
  * \param ctx       Entropy context to free
  */
-void mbedtls_entropy_free( mbedtls_entropy_context *ctx );
+void mbedtls_entropy_free(mbedtls_entropy_context *ctx);
 
 /**
  * \brief           Adds an entropy source to poll
@@ -171,9 +153,9 @@ void mbedtls_entropy_free( mbedtls_entropy_context *ctx );
  *
  * \return          0 if successful or MBEDTLS_ERR_ENTROPY_MAX_SOURCES
  */
-int mbedtls_entropy_add_source( mbedtls_entropy_context *ctx,
-                        mbedtls_entropy_f_source_ptr f_source, void *p_source,
-                        size_t threshold, int strong );
+int mbedtls_entropy_add_source(mbedtls_entropy_context *ctx,
+        mbedtls_entropy_f_source_ptr f_source, void *p_source,
+        size_t threshold, int strong);
 
 /**
  * \brief           Trigger an extra gather poll for the accumulator
@@ -183,7 +165,7 @@ int mbedtls_entropy_add_source( mbedtls_entropy_context *ctx,
  *
  * \return          0 if successful, or MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
  */
-int mbedtls_entropy_gather( mbedtls_entropy_context *ctx );
+int mbedtls_entropy_gather(mbedtls_entropy_context *ctx);
 
 /**
  * \brief           Retrieve entropy from the accumulator
@@ -196,7 +178,7 @@ int mbedtls_entropy_gather( mbedtls_entropy_context *ctx );
  *
  * \return          0 if successful, or MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
  */
-int mbedtls_entropy_func( void *data, unsigned char *output, size_t len );
+int mbedtls_entropy_func(void *data, unsigned char *output, size_t len);
 
 /**
  * \brief           Add data to the accumulator manually
@@ -208,8 +190,8 @@ int mbedtls_entropy_func( void *data, unsigned char *output, size_t len );
  *
  * \return          0 if successful
  */
-int mbedtls_entropy_update_manual( mbedtls_entropy_context *ctx,
-                           const unsigned char *data, size_t len );
+int mbedtls_entropy_update_manual(mbedtls_entropy_context *ctx,
+        const unsigned char *data, size_t len);
 
 #if defined(MBEDTLS_ENTROPY_NV_SEED)
 /**
@@ -220,7 +202,7 @@ int mbedtls_entropy_update_manual( mbedtls_entropy_context *ctx,
  *
  * \return          0 if successful
  */
-int mbedtls_entropy_update_nv_seed( mbedtls_entropy_context *ctx );
+int mbedtls_entropy_update_nv_seed(mbedtls_entropy_context *ctx);
 #endif /* MBEDTLS_ENTROPY_NV_SEED */
 
 #if defined(MBEDTLS_FS_IO)
@@ -234,7 +216,7 @@ int mbedtls_entropy_update_nv_seed( mbedtls_entropy_context *ctx );
  *                      MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR on file error, or
  *                      MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
  */
-int mbedtls_entropy_write_seed_file( mbedtls_entropy_context *ctx, const char *path );
+int mbedtls_entropy_write_seed_file(mbedtls_entropy_context *ctx, const char *path);
 
 /**
  * \brief               Read and update a seed file. Seed is added to this
@@ -248,7 +230,7 @@ int mbedtls_entropy_write_seed_file( mbedtls_entropy_context *ctx, const char *p
  *                      MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR on file error,
  *                      MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
  */
-int mbedtls_entropy_update_seed_file( mbedtls_entropy_context *ctx, const char *path );
+int mbedtls_entropy_update_seed_file(mbedtls_entropy_context *ctx, const char *path);
 #endif /* MBEDTLS_FS_IO */
 
 #if defined(MBEDTLS_SELF_TEST)
@@ -260,7 +242,7 @@ int mbedtls_entropy_update_seed_file( mbedtls_entropy_context *ctx, const char *
  *
  * \return         0 if successful, or 1 if a test failed
  */
-int mbedtls_entropy_self_test( int verbose );
+int mbedtls_entropy_self_test(int verbose);
 
 #if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
 /**
@@ -276,7 +258,7 @@ int mbedtls_entropy_self_test( int verbose );
  *
  * \return         0 if successful, or 1 if a test failed
  */
-int mbedtls_entropy_source_self_test( int verbose );
+int mbedtls_entropy_source_self_test(int verbose);
 #endif /* MBEDTLS_ENTROPY_HARDWARE_ALT */
 #endif /* MBEDTLS_SELF_TEST */
 

+ 19 - 35
iotkit-embedded/src/tls/mbedtls-in-iotkit/include/mbedtls/entropy_poll.h → iotkit-embedded/external_libs/mbedtls/include/mbedtls/entropy_poll.h

@@ -1,32 +1,16 @@
-/**
- * \file entropy_poll.h
- *
- * \brief Platform-specific and custom entropy polling functions
- *
- *  Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
+
+
+
 #ifndef MBEDTLS_ENTROPY_POLL_H
 #define MBEDTLS_ENTROPY_POLL_H
 
 #if !defined(MBEDTLS_CONFIG_FILE)
-#include "config.h"
+    #include "config.h"
 #else
-#include MBEDTLS_CONFIG_FILE
+    #include MBEDTLS_CONFIG_FILE
 #endif
 
 #include <stddef.h>
@@ -49,16 +33,16 @@ extern "C" {
  * \brief           Entropy poll callback that provides 0 entropy.
  */
 #if defined(MBEDTLS_TEST_NULL_ENTROPY)
-    int mbedtls_null_entropy_poll( void *data,
-                                unsigned char *output, size_t len, size_t *olen );
+int mbedtls_null_entropy_poll(void *data,
+                              unsigned char *output, size_t len, size_t *olen);
 #endif
 
 #if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
 /**
  * \brief           Platform-specific entropy poll callback
  */
-int mbedtls_platform_entropy_poll( void *data,
-                           unsigned char *output, size_t len, size_t *olen );
+int mbedtls_platform_entropy_poll(void *data,
+        unsigned char *output, size_t len, size_t *olen);
 #endif
 
 #if defined(MBEDTLS_HAVEGE_C)
@@ -67,16 +51,16 @@ int mbedtls_platform_entropy_poll( void *data,
  *
  * Requires an HAVEGE state as its data pointer.
  */
-int mbedtls_havege_poll( void *data,
-                 unsigned char *output, size_t len, size_t *olen );
+int mbedtls_havege_poll(void *data,
+                                    unsigned char *output, size_t len, size_t *olen);
 #endif
 
 #if defined(MBEDTLS_TIMING_C)
 /**
  * \brief           mbedtls_timing_hardclock-based entropy poll callback
  */
-int mbedtls_hardclock_poll( void *data,
-                    unsigned char *output, size_t len, size_t *olen );
+int mbedtls_hardclock_poll(void *data,
+                                       unsigned char *output, size_t len, size_t *olen);
 #endif
 
 #if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
@@ -88,8 +72,8 @@ int mbedtls_hardclock_poll( void *data,
  *
  * \note            This must accept NULL as its first argument.
  */
-int mbedtls_hardware_poll( void *data,
-                           unsigned char *output, size_t len, size_t *olen );
+int mbedtls_hardware_poll(void *data,
+                                      unsigned char *output, size_t len, size_t *olen);
 #endif
 
 #if defined(MBEDTLS_ENTROPY_NV_SEED)
@@ -98,8 +82,8 @@ int mbedtls_hardware_poll( void *data,
  *
  * \note            This must accept NULL as its first argument.
  */
-int mbedtls_nv_seed_poll( void *data,
-                          unsigned char *output, size_t len, size_t *olen );
+int mbedtls_nv_seed_poll(void *data,
+                                     unsigned char *output, size_t len, size_t *olen);
 #endif
 
 #ifdef __cplusplus

+ 7 - 22
iotkit-embedded/src/tls/mbedtls-in-iotkit/include/mbedtls/error.h → iotkit-embedded/external_libs/mbedtls/include/mbedtls/error.h

@@ -1,29 +1,14 @@
-/**
- * \file error.h
- *
- * \brief Error to string translation
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
+
+
+
 #ifndef MBEDTLS_ERROR_H
 #define MBEDTLS_ERROR_H
 
 #include <stddef.h>
+#include "config.h"
 
 /**
  * Error code layout.
@@ -98,7 +83,7 @@ extern "C" {
  * \param buffer    buffer to place representation in
  * \param buflen    length of the buffer
  */
-void mbedtls_strerror( int errnum, char *buffer, size_t buflen );
+void mbedtls_strerror(int errnum, char *buffer, size_t buflen);
 
 #ifdef __cplusplus
 }

+ 35 - 53
iotkit-embedded/src/tls/mbedtls-in-iotkit/include/mbedtls/md.h → iotkit-embedded/external_libs/mbedtls/include/mbedtls/md.h

@@ -1,27 +1,9 @@
-/**
- * \file md.h
- *
- * \brief Generic message digest wrapper
- *
- * \author Adriaan de Jong <dejong@fox-it.com>
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
+
+
+
 #ifndef MBEDTLS_MD_H
 #define MBEDTLS_MD_H
 
@@ -37,7 +19,7 @@ extern "C" {
 #endif
 
 typedef enum {
-    MBEDTLS_MD_NONE=0,
+    MBEDTLS_MD_NONE = 0,
     MBEDTLS_MD_MD2,
     MBEDTLS_MD_MD4,
     MBEDTLS_MD_MD5,
@@ -80,7 +62,7 @@ typedef struct {
  * \return          a statically allocated array of digests, the last entry
  *                  is 0.
  */
-const int *mbedtls_md_list( void );
+const int *mbedtls_md_list(void);
 
 /**
  * \brief           Returns the message digest information associated with the
@@ -91,7 +73,7 @@ const int *mbedtls_md_list( void );
  * \return          The message digest information associated with md_name or
  *                  NULL if not found.
  */
-const mbedtls_md_info_t *mbedtls_md_info_from_string( const char *md_name );
+const mbedtls_md_info_t *mbedtls_md_info_from_string(const char *md_name);
 
 /**
  * \brief           Returns the message digest information associated with the
@@ -102,21 +84,21 @@ const mbedtls_md_info_t *mbedtls_md_info_from_string( const char *md_name );
  * \return          The message digest information associated with md_type or
  *                  NULL if not found.
  */
-const mbedtls_md_info_t *mbedtls_md_info_from_type( mbedtls_md_type_t md_type );
+const mbedtls_md_info_t *mbedtls_md_info_from_type(mbedtls_md_type_t md_type);
 
 /**
  * \brief           Initialize a md_context (as NONE)
  *                  This should always be called first.
  *                  Prepares the context for mbedtls_md_setup() or mbedtls_md_free().
  */
-void mbedtls_md_init( mbedtls_md_context_t *ctx );
+void mbedtls_md_init(mbedtls_md_context_t *ctx);
 
 /**
  * \brief           Free and clear the internal structures of ctx.
  *                  Can be called at any time after mbedtls_md_init().
  *                  Mandatory once mbedtls_md_setup() has been called.
  */
-void mbedtls_md_free( mbedtls_md_context_t *ctx );
+void mbedtls_md_free(mbedtls_md_context_t *ctx);
 
 #if ! defined(MBEDTLS_DEPRECATED_REMOVED)
 #if defined(MBEDTLS_DEPRECATED_WARNING)
@@ -138,7 +120,7 @@ void mbedtls_md_free( mbedtls_md_context_t *ctx );
  *                  \c MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter failure,
  *                  \c MBEDTLS_ERR_MD_ALLOC_FAILED memory allocation failure.
  */
-int mbedtls_md_init_ctx( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info ) MBEDTLS_DEPRECATED;
+int mbedtls_md_init_ctx(mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info) MBEDTLS_DEPRECATED;
 #undef MBEDTLS_DEPRECATED
 #endif /* MBEDTLS_DEPRECATED_REMOVED */
 
@@ -156,7 +138,7 @@ int mbedtls_md_init_ctx( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_
  *                  \c MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter failure,
  *                  \c MBEDTLS_ERR_MD_ALLOC_FAILED memory allocation failure.
  */
-int mbedtls_md_setup( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac );
+int mbedtls_md_setup(mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac);
 
 /**
  * \brief           Clone the state of an MD context
@@ -172,8 +154,8 @@ int mbedtls_md_setup( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_inf
  * \return          \c 0 on success,
  *                  \c MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter failure.
  */
-int mbedtls_md_clone( mbedtls_md_context_t *dst,
-                      const mbedtls_md_context_t *src );
+int mbedtls_md_clone(mbedtls_md_context_t *dst,
+                                 const mbedtls_md_context_t *src);
 
 /**
  * \brief           Returns the size of the message digest output.
@@ -182,7 +164,7 @@ int mbedtls_md_clone( mbedtls_md_context_t *dst,
  *
  * \return          size of the message digest output in bytes.
  */
-unsigned char mbedtls_md_get_size( const mbedtls_md_info_t *md_info );
+unsigned char mbedtls_md_get_size(const mbedtls_md_info_t *md_info);
 
 /**
  * \brief           Returns the type of the message digest output.
@@ -191,7 +173,7 @@ unsigned char mbedtls_md_get_size( const mbedtls_md_info_t *md_info );
  *
  * \return          type of the message digest output.
  */
-mbedtls_md_type_t mbedtls_md_get_type( const mbedtls_md_info_t *md_info );
+mbedtls_md_type_t mbedtls_md_get_type(const mbedtls_md_info_t *md_info);
 
 /**
  * \brief           Returns the name of the message digest output.
@@ -200,7 +182,7 @@ mbedtls_md_type_t mbedtls_md_get_type( const mbedtls_md_info_t *md_info );
  *
  * \return          name of the message digest output.
  */
-const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info );
+const char *mbedtls_md_get_name(const mbedtls_md_info_t *md_info);
 
 /**
  * \brief           Prepare the context to digest a new message.
@@ -212,7 +194,7 @@ const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info );
  * \returns         0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
  *                  verification fails.
  */
-int mbedtls_md_starts( mbedtls_md_context_t *ctx );
+int mbedtls_md_starts(mbedtls_md_context_t *ctx);
 
 /**
  * \brief           Generic message digest process buffer
@@ -226,7 +208,7 @@ int mbedtls_md_starts( mbedtls_md_context_t *ctx );
  * \returns         0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
  *                  verification fails.
  */
-int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen );
+int mbedtls_md_update(mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen);
 
 /**
  * \brief           Generic message digest final digest
@@ -239,7 +221,7 @@ int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, si
  * \returns         0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
  *                  verification fails.
  */
-int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output );
+int mbedtls_md_finish(mbedtls_md_context_t *ctx, unsigned char *output);
 
 /**
  * \brief          Output = message_digest( input buffer )
@@ -252,8 +234,8 @@ int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output );
  * \returns        0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
  *                 verification fails.
  */
-int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
-        unsigned char *output );
+int mbedtls_md(const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
+                           unsigned char *output);
 
 #if defined(MBEDTLS_FS_IO)
 /**
@@ -267,8 +249,8 @@ int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, si
  *                 MBEDTLS_ERR_MD_FILE_IO_ERROR if file input failed,
  *                 MBEDTLS_ERR_MD_BAD_INPUT_DATA if md_info was NULL.
  */
-int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path,
-                     unsigned char *output );
+int mbedtls_md_file(const mbedtls_md_info_t *md_info, const char *path,
+                                unsigned char *output);
 #endif /* MBEDTLS_FS_IO */
 
 /**
@@ -282,8 +264,8 @@ int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path,
  * \returns         0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
  *                  verification fails.
  */
-int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key,
-                    size_t keylen );
+int mbedtls_md_hmac_starts(mbedtls_md_context_t *ctx, const unsigned char *key,
+                                       size_t keylen);
 
 /**
  * \brief           Generic HMAC process buffer.
@@ -298,8 +280,8 @@ int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key,
  * \returns         0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
  *                  verification fails.
  */
-int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const unsigned char *input,
-                    size_t ilen );
+int mbedtls_md_hmac_update(mbedtls_md_context_t *ctx, const unsigned char *input,
+                                       size_t ilen);
 
 /**
  * \brief           Output HMAC.
@@ -313,7 +295,7 @@ int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const unsigned char *inpu
  * \returns         0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
  *                  verification fails.
  */
-int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output);
+int mbedtls_md_hmac_finish(mbedtls_md_context_t *ctx, unsigned char *output);
 
 /**
  * \brief           Prepare to authenticate a new message with the same key.
@@ -325,7 +307,7 @@ int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output);
  * \returns         0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
  *                  verification fails.
  */
-int mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx );
+int mbedtls_md_hmac_reset(mbedtls_md_context_t *ctx);
 
 /**
  * \brief          Output = Generic_HMAC( hmac key, input buffer )
@@ -340,12 +322,12 @@ int mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx );
  * \returns        0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter
  *                 verification fails.
  */
-int mbedtls_md_hmac( const mbedtls_md_info_t *md_info, const unsigned char *key, size_t keylen,
-                const unsigned char *input, size_t ilen,
-                unsigned char *output );
+int mbedtls_md_hmac(const mbedtls_md_info_t *md_info, const unsigned char *key, size_t keylen,
+                                const unsigned char *input, size_t ilen,
+                                unsigned char *output);
 
 /* Internal use */
-int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data );
+int mbedtls_md_process(mbedtls_md_context_t *ctx, const unsigned char *data);
 
 #ifdef __cplusplus
 }

+ 18 - 35
iotkit-embedded/src/tls/mbedtls-in-iotkit/include/mbedtls/md5.h → iotkit-embedded/external_libs/mbedtls/include/mbedtls/md5.h

@@ -1,32 +1,16 @@
-/**
- * \file md5.h
- *
- * \brief MD5 message digest algorithm (hash function)
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
+
+
+
 #ifndef MBEDTLS_MD5_H
 #define MBEDTLS_MD5_H
 
 #if !defined(MBEDTLS_CONFIG_FILE)
-#include "config.h"
+    #include "config.h"
 #else
-#include MBEDTLS_CONFIG_FILE
+    #include MBEDTLS_CONFIG_FILE
 #endif
 
 #include <stddef.h>
@@ -43,8 +27,7 @@ extern "C" {
 /**
  * \brief          MD5 context structure
  */
-typedef struct
-{
+typedef struct {
     uint32_t total[2];          /*!< number of bytes processed  */
     uint32_t state[4];          /*!< intermediate digest state  */
     unsigned char buffer[64];   /*!< data block being processed */
@@ -56,14 +39,14 @@ mbedtls_md5_context;
  *
  * \param ctx      MD5 context to be initialized
  */
-void mbedtls_md5_init( mbedtls_md5_context *ctx );
+void mbedtls_md5_init(mbedtls_md5_context *ctx);
 
 /**
  * \brief          Clear MD5 context
  *
  * \param ctx      MD5 context to be cleared
  */
-void mbedtls_md5_free( mbedtls_md5_context *ctx );
+void mbedtls_md5_free(mbedtls_md5_context *ctx);
 
 /**
  * \brief          Clone (the state of) an MD5 context
@@ -71,15 +54,15 @@ void mbedtls_md5_free( mbedtls_md5_context *ctx );
  * \param dst      The destination context
  * \param src      The context to be cloned
  */
-void mbedtls_md5_clone( mbedtls_md5_context *dst,
-                        const mbedtls_md5_context *src );
+void mbedtls_md5_clone(mbedtls_md5_context *dst,
+                                   const mbedtls_md5_context *src);
 
 /**
  * \brief          MD5 context setup
  *
  * \param ctx      context to be initialized
  */
-void mbedtls_md5_starts( mbedtls_md5_context *ctx );
+void mbedtls_md5_starts(mbedtls_md5_context *ctx);
 
 /**
  * \brief          MD5 process buffer
@@ -88,7 +71,7 @@ void mbedtls_md5_starts( mbedtls_md5_context *ctx );
  * \param input    buffer holding the  data
  * \param ilen     length of the input data
  */
-void mbedtls_md5_update( mbedtls_md5_context *ctx, const unsigned char *input, size_t ilen );
+void mbedtls_md5_update(mbedtls_md5_context *ctx, const unsigned char *input, size_t ilen);
 
 /**
  * \brief          MD5 final digest
@@ -96,10 +79,10 @@ void mbedtls_md5_update( mbedtls_md5_context *ctx, const unsigned char *input, s
  * \param ctx      MD5 context
  * \param output   MD5 checksum result
  */
-void mbedtls_md5_finish( mbedtls_md5_context *ctx, unsigned char output[16] );
+void mbedtls_md5_finish(mbedtls_md5_context *ctx, unsigned char output[16]);
 
 /* Internal use */
-void mbedtls_md5_process( mbedtls_md5_context *ctx, const unsigned char data[64] );
+void mbedtls_md5_process(mbedtls_md5_context *ctx, const unsigned char data[64]);
 
 #ifdef __cplusplus
 }
@@ -120,14 +103,14 @@ extern "C" {
  * \param ilen     length of the input data
  * \param output   MD5 checksum result
  */
-void mbedtls_md5( const unsigned char *input, size_t ilen, unsigned char output[16] );
+void mbedtls_md5(const unsigned char *input, size_t ilen, unsigned char output[16]);
 
 /**
  * \brief          Checkup routine
  *
  * \return         0 if successful, or 1 if the test failed
  */
-int mbedtls_md5_self_test( int verbose );
+int mbedtls_md5_self_test(int verbose);
 
 #ifdef __cplusplus
 }

+ 5 - 25
iotkit-embedded/src/tls/mbedtls-in-iotkit/include/mbedtls/md_internal.h → iotkit-embedded/external_libs/mbedtls/include/mbedtls/md_internal.h

@@ -1,29 +1,9 @@
-/**
- * \file md_internal.h
- *
- * \brief Message digest wrappers.
- *
- * \warning This in an internal header. Do not include directly.
- *
- * \author Adriaan de Jong <dejong@fox-it.com>
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
+
+
+
 #ifndef MBEDTLS_MD_WRAP_H
 #define MBEDTLS_MD_WRAP_H
 

+ 13 - 0
iotkit-embedded/external_libs/mbedtls/include/mbedtls/net.h

@@ -0,0 +1,13 @@
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
+ */
+
+
+
+
+#if !defined(MBEDTLS_DEPRECATED_REMOVED)
+#include "mbedtls/net_sockets.h"
+#if defined(MBEDTLS_DEPRECATED_WARNING)
+#warning "Deprecated header file: Superseded by mbedtls/net_sockets.h"
+#endif /* MBEDTLS_DEPRECATED_WARNING */
+#endif /* !MBEDTLS_DEPRECATED_REMOVED */

+ 22 - 39
iotkit-embedded/src/tls/mbedtls-in-iotkit/include/mbedtls/net_sockets.h → iotkit-embedded/external_libs/mbedtls/include/mbedtls/net_sockets.h

@@ -1,32 +1,16 @@
-/**
- * \file net_sockets.h
- *
- * \brief Network communication functions
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
+
+
+
 #ifndef MBEDTLS_NET_SOCKETS_H
 #define MBEDTLS_NET_SOCKETS_H
 
 #if !defined(MBEDTLS_CONFIG_FILE)
-#include "config.h"
+    #include "config.h"
 #else
-#include MBEDTLS_CONFIG_FILE
+    #include MBEDTLS_CONFIG_FILE
 #endif
 
 #include "ssl.h"
@@ -62,8 +46,7 @@ extern "C" {
  * (eg two file descriptors for combined IPv4 + IPv6 support, or additional
  * structures for hand-made UDP demultiplexing).
  */
-typedef struct
-{
+typedef struct {
     int fd;             /**< The underlying file descriptor                 */
 }
 mbedtls_net_context;
@@ -74,7 +57,7 @@ mbedtls_net_context;
  *
  * \param ctx      Context to initialize
  */
-void mbedtls_net_init( mbedtls_net_context *ctx );
+void mbedtls_net_init(mbedtls_net_context *ctx);
 
 /**
  * \brief          Initiate a connection with host:port in the given protocol
@@ -91,7 +74,7 @@ void mbedtls_net_init( mbedtls_net_context *ctx );
  *
  * \note           Sets the socket in connected mode even with UDP.
  */
-int mbedtls_net_connect( mbedtls_net_context *ctx, const char *host, const char *port, int proto );
+int mbedtls_net_connect(mbedtls_net_context *ctx, const char *host, const char *port, int proto);
 
 /**
  * \brief          Create a receiving socket on bind_ip:port in the chosen
@@ -110,7 +93,7 @@ int mbedtls_net_connect( mbedtls_net_context *ctx, const char *host, const char
  * \note           Regardless of the protocol, opens the sockets and binds it.
  *                 In addition, make the socket listening if protocol is TCP.
  */
-int mbedtls_net_bind( mbedtls_net_context *ctx, const char *bind_ip, const char *port, int proto );
+int mbedtls_net_bind(mbedtls_net_context *ctx, const char *bind_ip, const char *port, int proto);
 
 /**
  * \brief           Accept a connection from a remote client
@@ -127,9 +110,9 @@ int mbedtls_net_bind( mbedtls_net_context *ctx, const char *bind_ip, const char
  *                  MBEDTLS_ERR_SSL_WANT_READ if bind_fd was set to
  *                  non-blocking and accept() would block.
  */
-int mbedtls_net_accept( mbedtls_net_context *bind_ctx,
-                        mbedtls_net_context *client_ctx,
-                        void *client_ip, size_t buf_size, size_t *ip_len );
+int mbedtls_net_accept(mbedtls_net_context *bind_ctx,
+                                   mbedtls_net_context *client_ctx,
+                                   void *client_ip, size_t buf_size, size_t *ip_len);
 
 /**
  * \brief          Set the socket blocking
@@ -138,7 +121,7 @@ int mbedtls_net_accept( mbedtls_net_context *bind_ctx,
  *
  * \return         0 if successful, or a non-zero error code
  */
-int mbedtls_net_set_block( mbedtls_net_context *ctx );
+int mbedtls_net_set_block(mbedtls_net_context *ctx);
 
 /**
  * \brief          Set the socket non-blocking
@@ -147,7 +130,7 @@ int mbedtls_net_set_block( mbedtls_net_context *ctx );
  *
  * \return         0 if successful, or a non-zero error code
  */
-int mbedtls_net_set_nonblock( mbedtls_net_context *ctx );
+int mbedtls_net_set_nonblock(mbedtls_net_context *ctx);
 
 /**
  * \brief          Portable usleep helper
@@ -157,7 +140,7 @@ int mbedtls_net_set_nonblock( mbedtls_net_context *ctx );
  * \note           Real amount of time slept will not be less than
  *                 select()'s timeout granularity (typically, 10ms).
  */
-void mbedtls_net_usleep( unsigned long usec );
+void mbedtls_net_usleep(unsigned long usec);
 
 /**
  * \brief          Read at most 'len' characters. If no error occurs,
@@ -171,7 +154,7 @@ void mbedtls_net_usleep( unsigned long usec );
  *                 or a non-zero error code; with a non-blocking socket,
  *                 MBEDTLS_ERR_SSL_WANT_READ indicates read() would block.
  */
-int mbedtls_net_recv( void *ctx, unsigned char *buf, size_t len );
+int mbedtls_net_recv(void *ctx, unsigned char *buf, size_t len);
 
 /**
  * \brief          Write at most 'len' characters. If no error occurs,
@@ -185,7 +168,7 @@ int mbedtls_net_recv( void *ctx, unsigned char *buf, size_t len );
  *                 or a non-zero error code; with a non-blocking socket,
  *                 MBEDTLS_ERR_SSL_WANT_WRITE indicates write() would block.
  */
-int mbedtls_net_send( void *ctx, const unsigned char *buf, size_t len );
+int mbedtls_net_send(void *ctx, const unsigned char *buf, size_t len);
 
 /**
  * \brief          Read at most 'len' characters, blocking for at most
@@ -208,15 +191,15 @@ int mbedtls_net_send( void *ctx, const unsigned char *buf, size_t len );
  *                 non-blocking. Handling timeouts with non-blocking reads
  *                 requires a different strategy.
  */
-int mbedtls_net_recv_timeout( void *ctx, unsigned char *buf, size_t len,
-                      uint32_t timeout );
+int mbedtls_net_recv_timeout(void *ctx, unsigned char *buf, size_t len,
+        uint32_t timeout);
 
 /**
  * \brief          Gracefully shutdown the connection and free associated data
  *
  * \param ctx      The context to free
  */
-void mbedtls_net_free( mbedtls_net_context *ctx );
+void mbedtls_net_free(mbedtls_net_context *ctx);
 
 #ifdef __cplusplus
 }

+ 32 - 48
iotkit-embedded/src/tls/mbedtls-in-iotkit/include/mbedtls/oid.h → iotkit-embedded/external_libs/mbedtls/include/mbedtls/oid.h

@@ -1,32 +1,16 @@
-/**
- * \file oid.h
- *
- * \brief Object Identifier (OID) database
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
+
+
+
 #ifndef MBEDTLS_OID_H
 #define MBEDTLS_OID_H
 
 #if !defined(MBEDTLS_CONFIG_FILE)
-#include "config.h"
+    #include "config.h"
 #else
-#include MBEDTLS_CONFIG_FILE
+    #include MBEDTLS_CONFIG_FILE
 #endif
 
 #include "asn1.h"
@@ -35,15 +19,15 @@
 #include <stddef.h>
 
 #if defined(MBEDTLS_CIPHER_C)
-#include "cipher.h"
+    #include "cipher.h"
 #endif
 
 #if defined(MBEDTLS_MD_C)
-#include "md.h"
+    #include "md.h"
 #endif
 
 #if defined(MBEDTLS_X509_USE_C) || defined(MBEDTLS_X509_CREATE_C)
-#include "x509.h"
+    #include "x509.h"
 #endif
 
 #define MBEDTLS_ERR_OID_NOT_FOUND                         -0x002E  /**< OID is not found. */
@@ -63,10 +47,10 @@
 #define MBEDTLS_OID_COUNTRY_US                  "\x86\x48"      /* {us(840)} */
 #define MBEDTLS_OID_ORG_RSA_DATA_SECURITY       "\x86\xf7\x0d"  /* {rsadsi(113549)} */
 #define MBEDTLS_OID_RSA_COMPANY                 MBEDTLS_OID_ISO_MEMBER_BODIES MBEDTLS_OID_COUNTRY_US \
-                                        MBEDTLS_OID_ORG_RSA_DATA_SECURITY /* {iso(1) member-body(2) us(840) rsadsi(113549)} */
+    MBEDTLS_OID_ORG_RSA_DATA_SECURITY /* {iso(1) member-body(2) us(840) rsadsi(113549)} */
 #define MBEDTLS_OID_ORG_ANSI_X9_62              "\xce\x3d" /* ansi-X9-62(10045) */
 #define MBEDTLS_OID_ANSI_X9_62                  MBEDTLS_OID_ISO_MEMBER_BODIES MBEDTLS_OID_COUNTRY_US \
-                                        MBEDTLS_OID_ORG_ANSI_X9_62
+    MBEDTLS_OID_ORG_ANSI_X9_62
 
 /*
  * ISO Identified organization OID parts
@@ -397,7 +381,7 @@ typedef struct {
  * \return          Length of the string written (excluding final NULL) or
  *                  MBEDTLS_ERR_OID_BUF_TOO_SMALL in case of error
  */
-int mbedtls_oid_get_numeric_string( char *buf, size_t size, const mbedtls_asn1_buf *oid );
+int mbedtls_oid_get_numeric_string(char *buf, size_t size, const mbedtls_asn1_buf *oid);
 
 #if defined(MBEDTLS_X509_USE_C) || defined(MBEDTLS_X509_CREATE_C)
 /**
@@ -408,7 +392,7 @@ int mbedtls_oid_get_numeric_string( char *buf, size_t size, const mbedtls_asn1_b
  *
  * \return         0 if successful, or MBEDTLS_ERR_OID_NOT_FOUND
  */
-int mbedtls_oid_get_x509_ext_type( const mbedtls_asn1_buf *oid, int *ext_type );
+int mbedtls_oid_get_x509_ext_type(const mbedtls_asn1_buf *oid, int *ext_type);
 #endif
 
 /**
@@ -420,7 +404,7 @@ int mbedtls_oid_get_x509_ext_type( const mbedtls_asn1_buf *oid, int *ext_type );
  *
  * \return         0 if successful, or MBEDTLS_ERR_OID_NOT_FOUND
  */
-int mbedtls_oid_get_attr_short_name( const mbedtls_asn1_buf *oid, const char **short_name );
+int mbedtls_oid_get_attr_short_name(const mbedtls_asn1_buf *oid, const char **short_name);
 
 /**
  * \brief          Translate PublicKeyAlgorithm OID into pk_type
@@ -430,7 +414,7 @@ int mbedtls_oid_get_attr_short_name( const mbedtls_asn1_buf *oid, const char **s
  *
  * \return         0 if successful, or MBEDTLS_ERR_OID_NOT_FOUND
  */
-int mbedtls_oid_get_pk_alg( const mbedtls_asn1_buf *oid, mbedtls_pk_type_t *pk_alg );
+int mbedtls_oid_get_pk_alg(const mbedtls_asn1_buf *oid, mbedtls_pk_type_t *pk_alg);
 
 /**
  * \brief          Translate pk_type into PublicKeyAlgorithm OID
@@ -441,8 +425,8 @@ int mbedtls_oid_get_pk_alg( const mbedtls_asn1_buf *oid, mbedtls_pk_type_t *pk_a
  *
  * \return         0 if successful, or MBEDTLS_ERR_OID_NOT_FOUND
  */
-int mbedtls_oid_get_oid_by_pk_alg( mbedtls_pk_type_t pk_alg,
-                           const char **oid, size_t *olen );
+int mbedtls_oid_get_oid_by_pk_alg(mbedtls_pk_type_t pk_alg,
+        const char **oid, size_t *olen);
 
 #if defined(MBEDTLS_ECP_C)
 /**
@@ -453,7 +437,7 @@ int mbedtls_oid_get_oid_by_pk_alg( mbedtls_pk_type_t pk_alg,
  *
  * \return         0 if successful, or MBEDTLS_ERR_OID_NOT_FOUND
  */
-int mbedtls_oid_get_ec_grp( const mbedtls_asn1_buf *oid, mbedtls_ecp_group_id *grp_id );
+int mbedtls_oid_get_ec_grp(const mbedtls_asn1_buf *oid, mbedtls_ecp_group_id *grp_id);
 
 /**
  * \brief          Translate EC group identifier into NamedCurve OID
@@ -464,8 +448,8 @@ int mbedtls_oid_get_ec_grp( const mbedtls_asn1_buf *oid, mbedtls_ecp_group_id *g
  *
  * \return         0 if successful, or MBEDTLS_ERR_OID_NOT_FOUND
  */
-int mbedtls_oid_get_oid_by_ec_grp( mbedtls_ecp_group_id grp_id,
-                           const char **oid, size_t *olen );
+int mbedtls_oid_get_oid_by_ec_grp(mbedtls_ecp_group_id grp_id,
+        const char **oid, size_t *olen);
 #endif /* MBEDTLS_ECP_C */
 
 #if defined(MBEDTLS_MD_C)
@@ -478,8 +462,8 @@ int mbedtls_oid_get_oid_by_ec_grp( mbedtls_ecp_group_id grp_id,
  *
  * \return         0 if successful, or MBEDTLS_ERR_OID_NOT_FOUND
  */
-int mbedtls_oid_get_sig_alg( const mbedtls_asn1_buf *oid,
-                     mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg );
+int mbedtls_oid_get_sig_alg(const mbedtls_asn1_buf *oid,
+                                        mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg);
 
 /**
  * \brief          Translate SignatureAlgorithm OID into description
@@ -489,7 +473,7 @@ int mbedtls_oid_get_sig_alg( const mbedtls_asn1_buf *oid,
  *
  * \return         0 if successful, or MBEDTLS_ERR_OID_NOT_FOUND
  */
-int mbedtls_oid_get_sig_alg_desc( const mbedtls_asn1_buf *oid, const char **desc );
+int mbedtls_oid_get_sig_alg_desc(const mbedtls_asn1_buf *oid, const char **desc);
 
 /**
  * \brief          Translate md_type and pk_type into SignatureAlgorithm OID
@@ -501,8 +485,8 @@ int mbedtls_oid_get_sig_alg_desc( const mbedtls_asn1_buf *oid, const char **desc
  *
  * \return         0 if successful, or MBEDTLS_ERR_OID_NOT_FOUND
  */
-int mbedtls_oid_get_oid_by_sig_alg( mbedtls_pk_type_t pk_alg, mbedtls_md_type_t md_alg,
-                            const char **oid, size_t *olen );
+int mbedtls_oid_get_oid_by_sig_alg(mbedtls_pk_type_t pk_alg, mbedtls_md_type_t md_alg,
+        const char **oid, size_t *olen);
 
 /**
  * \brief          Translate hash algorithm OID into md_type
@@ -512,7 +496,7 @@ int mbedtls_oid_get_oid_by_sig_alg( mbedtls_pk_type_t pk_alg, mbedtls_md_type_t
  *
  * \return         0 if successful, or MBEDTLS_ERR_OID_NOT_FOUND
  */
-int mbedtls_oid_get_md_alg( const mbedtls_asn1_buf *oid, mbedtls_md_type_t *md_alg );
+int mbedtls_oid_get_md_alg(const mbedtls_asn1_buf *oid, mbedtls_md_type_t *md_alg);
 #endif /* MBEDTLS_MD_C */
 
 /**
@@ -523,7 +507,7 @@ int mbedtls_oid_get_md_alg( const mbedtls_asn1_buf *oid, mbedtls_md_type_t *md_a
  *
  * \return         0 if successful, or MBEDTLS_ERR_OID_NOT_FOUND
  */
-int mbedtls_oid_get_extended_key_usage( const mbedtls_asn1_buf *oid, const char **desc );
+int mbedtls_oid_get_extended_key_usage(const mbedtls_asn1_buf *oid, const char **desc);
 
 /**
  * \brief          Translate md_type into hash algorithm OID
@@ -534,7 +518,7 @@ int mbedtls_oid_get_extended_key_usage( const mbedtls_asn1_buf *oid, const char
  *
  * \return         0 if successful, or MBEDTLS_ERR_OID_NOT_FOUND
  */
-int mbedtls_oid_get_oid_by_md( mbedtls_md_type_t md_alg, const char **oid, size_t *olen );
+int mbedtls_oid_get_oid_by_md(mbedtls_md_type_t md_alg, const char **oid, size_t *olen);
 
 #if defined(MBEDTLS_CIPHER_C)
 /**
@@ -545,7 +529,7 @@ int mbedtls_oid_get_oid_by_md( mbedtls_md_type_t md_alg, const char **oid, size_
  *
  * \return         0 if successful, or MBEDTLS_ERR_OID_NOT_FOUND
  */
-int mbedtls_oid_get_cipher_alg( const mbedtls_asn1_buf *oid, mbedtls_cipher_type_t *cipher_alg );
+int mbedtls_oid_get_cipher_alg(const mbedtls_asn1_buf *oid, mbedtls_cipher_type_t *cipher_alg);
 #endif /* MBEDTLS_CIPHER_C */
 
 #if defined(MBEDTLS_PKCS12_C)
@@ -559,8 +543,8 @@ int mbedtls_oid_get_cipher_alg( const mbedtls_asn1_buf *oid, mbedtls_cipher_type
  *
  * \return         0 if successful, or MBEDTLS_ERR_OID_NOT_FOUND
  */
-int mbedtls_oid_get_pkcs12_pbe_alg( const mbedtls_asn1_buf *oid, mbedtls_md_type_t *md_alg,
-                            mbedtls_cipher_type_t *cipher_alg );
+int mbedtls_oid_get_pkcs12_pbe_alg(const mbedtls_asn1_buf *oid, mbedtls_md_type_t *md_alg,
+        mbedtls_cipher_type_t *cipher_alg);
 #endif /* MBEDTLS_PKCS12_C */
 
 #ifdef __cplusplus

+ 15 - 32
iotkit-embedded/src/tls/mbedtls-in-iotkit/include/mbedtls/pem.h → iotkit-embedded/external_libs/mbedtls/include/mbedtls/pem.h

@@ -1,25 +1,9 @@
-/**
- * \file pem.h
- *
- * \brief Privacy Enhanced Mail (PEM) decoding
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
+
+
+
 #ifndef MBEDTLS_PEM_H
 #define MBEDTLS_PEM_H
 
@@ -50,8 +34,7 @@ extern "C" {
 /**
  * \brief       PEM context structure
  */
-typedef struct
-{
+typedef struct {
     unsigned char *buf;     /*!< buffer for decoded data             */
     size_t buflen;          /*!< length of the buffer                */
     unsigned char *info;    /*!< buffer for extra header information */
@@ -63,7 +46,7 @@ mbedtls_pem_context;
  *
  * \param ctx   context to be initialized
  */
-void mbedtls_pem_init( mbedtls_pem_context *ctx );
+void mbedtls_pem_init(mbedtls_pem_context *ctx);
 
 /**
  * \brief       Read a buffer for PEM information and store the resulting
@@ -87,17 +70,17 @@ void mbedtls_pem_init( mbedtls_pem_context *ctx );
  *
  * \return          0 on success, or a specific PEM error code
  */
-int mbedtls_pem_read_buffer( mbedtls_pem_context *ctx, const char *header, const char *footer,
-                     const unsigned char *data,
-                     const unsigned char *pwd,
-                     size_t pwdlen, size_t *use_len );
+int mbedtls_pem_read_buffer(mbedtls_pem_context *ctx, const char *header, const char *footer,
+                                        const unsigned char *data,
+                                        const unsigned char *pwd,
+                                        size_t pwdlen, size_t *use_len);
 
 /**
  * \brief       PEM context memory freeing
  *
  * \param ctx   context to be freed
  */
-void mbedtls_pem_free( mbedtls_pem_context *ctx );
+void mbedtls_pem_free(mbedtls_pem_context *ctx);
 #endif /* MBEDTLS_PEM_PARSE_C */
 
 #if defined(MBEDTLS_PEM_WRITE_C)
@@ -117,9 +100,9 @@ void mbedtls_pem_free( mbedtls_pem_context *ctx );
  *                  MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL olen is the required
  *                  size.
  */
-int mbedtls_pem_write_buffer( const char *header, const char *footer,
-                      const unsigned char *der_data, size_t der_len,
-                      unsigned char *buf, size_t buf_len, size_t *olen );
+int mbedtls_pem_write_buffer(const char *header, const char *footer,
+        const unsigned char *der_data, size_t der_len,
+        unsigned char *buf, size_t buf_len, size_t *olen);
 #endif /* MBEDTLS_PEM_WRITE_C */
 
 #ifdef __cplusplus

+ 82 - 102
iotkit-embedded/src/tls/mbedtls-in-iotkit/include/mbedtls/pk.h → iotkit-embedded/external_libs/mbedtls/include/mbedtls/pk.h

@@ -1,52 +1,36 @@
-/**
- * \file pk.h
- *
- * \brief Public Key abstraction layer
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
 
+
+
+
 #ifndef MBEDTLS_PK_H
 #define MBEDTLS_PK_H
 
 #if !defined(MBEDTLS_CONFIG_FILE)
-#include "config.h"
+    #include "config.h"
 #else
-#include MBEDTLS_CONFIG_FILE
+    #include MBEDTLS_CONFIG_FILE
 #endif
 
 #include "md.h"
 
 #if defined(MBEDTLS_RSA_C)
-#include "rsa.h"
+    #include "rsa.h"
 #endif
 
 #if defined(MBEDTLS_ECP_C)
-#include "ecp.h"
+    #include "ecp.h"
 #endif
 
 #if defined(MBEDTLS_ECDSA_C)
-#include "ecdsa.h"
+    #include "ecdsa.h"
 #endif
 
 #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
     !defined(inline) && !defined(__cplusplus)
-#define inline __inline
+    #define inline __inline
 #endif
 
 #define MBEDTLS_ERR_PK_ALLOC_FAILED        -0x3F80  /**< Memory allocation failed. */
@@ -72,7 +56,7 @@ extern "C" {
  * \brief          Public key types
  */
 typedef enum {
-    MBEDTLS_PK_NONE=0,
+    MBEDTLS_PK_NONE = 0,
     MBEDTLS_PK_RSA,
     MBEDTLS_PK_ECKEY,
     MBEDTLS_PK_ECKEY_DH,
@@ -85,8 +69,7 @@ typedef enum {
  * \brief           Options for RSASSA-PSS signature verification.
  *                  See \c mbedtls_rsa_rsassa_pss_verify_ext()
  */
-typedef struct
-{
+typedef struct {
     mbedtls_md_type_t mgf1_hash_id;
     int expected_salt_len;
 
@@ -95,8 +78,7 @@ typedef struct
 /**
  * \brief           Types for interfacing with the debug module
  */
-typedef enum
-{
+typedef enum {
     MBEDTLS_PK_DEBUG_NONE = 0,
     MBEDTLS_PK_DEBUG_MPI,
     MBEDTLS_PK_DEBUG_ECP,
@@ -105,8 +87,7 @@ typedef enum
 /**
  * \brief           Item to send to the debug module
  */
-typedef struct
-{
+typedef struct {
     mbedtls_pk_debug_type type;
     const char *name;
     void *value;
@@ -123,10 +104,9 @@ typedef struct mbedtls_pk_info_t mbedtls_pk_info_t;
 /**
  * \brief           Public key container
  */
-typedef struct
-{
-    const mbedtls_pk_info_t *   pk_info; /**< Public key informations        */
-    void *                      pk_ctx;  /**< Underlying public key context  */
+typedef struct {
+    const mbedtls_pk_info_t    *pk_info; /**< Public key informations        */
+    void                       *pk_ctx;  /**< Underlying public key context  */
 } mbedtls_pk_context;
 
 #if defined(MBEDTLS_RSA_C)
@@ -136,9 +116,9 @@ typedef struct
  * \warning You must make sure the PK context actually holds an RSA context
  * before using this function!
  */
-static inline mbedtls_rsa_context *mbedtls_pk_rsa( const mbedtls_pk_context pk )
+static inline mbedtls_rsa_context *mbedtls_pk_rsa(const mbedtls_pk_context pk)
 {
-    return( (mbedtls_rsa_context *) (pk).pk_ctx );
+    return ((mbedtls_rsa_context *)(pk).pk_ctx);
 }
 #endif /* MBEDTLS_RSA_C */
 
@@ -149,9 +129,9 @@ static inline mbedtls_rsa_context *mbedtls_pk_rsa( const mbedtls_pk_context pk )
  * \warning You must make sure the PK context actually holds an EC context
  * before using this function!
  */
-static inline mbedtls_ecp_keypair *mbedtls_pk_ec( const mbedtls_pk_context pk )
+static inline mbedtls_ecp_keypair *mbedtls_pk_ec(const mbedtls_pk_context pk)
 {
-    return( (mbedtls_ecp_keypair *) (pk).pk_ctx );
+    return ((mbedtls_ecp_keypair *)(pk).pk_ctx);
 }
 #endif /* MBEDTLS_ECP_C */
 
@@ -159,14 +139,14 @@ static inline mbedtls_ecp_keypair *mbedtls_pk_ec( const mbedtls_pk_context pk )
 /**
  * \brief           Types for RSA-alt abstraction
  */
-typedef int (*mbedtls_pk_rsa_alt_decrypt_func)( void *ctx, int mode, size_t *olen,
-                    const unsigned char *input, unsigned char *output,
-                    size_t output_max_len );
-typedef int (*mbedtls_pk_rsa_alt_sign_func)( void *ctx,
-                    int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
-                    int mode, mbedtls_md_type_t md_alg, unsigned int hashlen,
-                    const unsigned char *hash, unsigned char *sig );
-typedef size_t (*mbedtls_pk_rsa_alt_key_len_func)( void *ctx );
+typedef int (*mbedtls_pk_rsa_alt_decrypt_func)(void *ctx, int mode, size_t *olen,
+        const unsigned char *input, unsigned char *output,
+        size_t output_max_len);
+typedef int (*mbedtls_pk_rsa_alt_sign_func)(void *ctx,
+        int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
+        int mode, mbedtls_md_type_t md_alg, unsigned int hashlen,
+        const unsigned char *hash, unsigned char *sig);
+typedef size_t (*mbedtls_pk_rsa_alt_key_len_func)(void *ctx);
 #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
 
 /**
@@ -176,17 +156,17 @@ typedef size_t (*mbedtls_pk_rsa_alt_key_len_func)( void *ctx );
  *
  * \return          The PK info associated with the type or NULL if not found.
  */
-const mbedtls_pk_info_t *mbedtls_pk_info_from_type( mbedtls_pk_type_t pk_type );
+const mbedtls_pk_info_t *mbedtls_pk_info_from_type(mbedtls_pk_type_t pk_type);
 
 /**
  * \brief           Initialize a mbedtls_pk_context (as NONE)
  */
-void mbedtls_pk_init( mbedtls_pk_context *ctx );
+void mbedtls_pk_init(mbedtls_pk_context *ctx);
 
 /**
  * \brief           Free a mbedtls_pk_context
  */
-void mbedtls_pk_free( mbedtls_pk_context *ctx );
+void mbedtls_pk_free(mbedtls_pk_context *ctx);
 
 /**
  * \brief           Initialize a PK context with the information given
@@ -202,7 +182,7 @@ void mbedtls_pk_free( mbedtls_pk_context *ctx );
  * \note            For contexts holding an RSA-alt key, use
  *                  \c mbedtls_pk_setup_rsa_alt() instead.
  */
-int mbedtls_pk_setup( mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info );
+int mbedtls_pk_setup(mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info);
 
 #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
 /**
@@ -219,10 +199,10 @@ int mbedtls_pk_setup( mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info );
  *
  * \note            This function replaces \c mbedtls_pk_setup() for RSA-alt.
  */
-int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key,
-                         mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
-                         mbedtls_pk_rsa_alt_sign_func sign_func,
-                         mbedtls_pk_rsa_alt_key_len_func key_len_func );
+int mbedtls_pk_setup_rsa_alt(mbedtls_pk_context *ctx, void *key,
+        mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
+        mbedtls_pk_rsa_alt_sign_func sign_func,
+        mbedtls_pk_rsa_alt_key_len_func key_len_func);
 #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
 
 /**
@@ -232,7 +212,7 @@ int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key,
  *
  * \return          Key size in bits, or 0 on error
  */
-size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx );
+size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context *ctx);
 
 /**
  * \brief           Get the length in bytes of the underlying key
@@ -240,9 +220,9 @@ size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx );
  *
  * \return          Key length in bytes, or 0 on error
  */
-static inline size_t mbedtls_pk_get_len( const mbedtls_pk_context *ctx )
+static inline size_t mbedtls_pk_get_len(const mbedtls_pk_context *ctx)
 {
-    return( ( mbedtls_pk_get_bitlen( ctx ) + 7 ) / 8 );
+    return ((mbedtls_pk_get_bitlen(ctx) + 7) / 8);
 }
 
 /**
@@ -254,7 +234,7 @@ static inline size_t mbedtls_pk_get_len( const mbedtls_pk_context *ctx )
  * \return          0 if context can't do the operations,
  *                  1 otherwise.
  */
-int mbedtls_pk_can_do( const mbedtls_pk_context *ctx, mbedtls_pk_type_t type );
+int mbedtls_pk_can_do(const mbedtls_pk_context *ctx, mbedtls_pk_type_t type);
 
 /**
  * \brief           Verify signature (including padding if relevant).
@@ -280,9 +260,9 @@ int mbedtls_pk_can_do( const mbedtls_pk_context *ctx, mbedtls_pk_type_t type );
  *
  * \note            md_alg may be MBEDTLS_MD_NONE, only if hash_len != 0
  */
-int mbedtls_pk_verify( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
-               const unsigned char *hash, size_t hash_len,
-               const unsigned char *sig, size_t sig_len );
+int mbedtls_pk_verify(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
+                                  const unsigned char *hash, size_t hash_len,
+                                  const unsigned char *sig, size_t sig_len);
 
 /**
  * \brief           Verify signature, with options.
@@ -313,10 +293,10 @@ int mbedtls_pk_verify( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
  *                  to a mbedtls_pk_rsassa_pss_options structure,
  *                  otherwise it must be NULL.
  */
-int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
-                   mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
-                   const unsigned char *hash, size_t hash_len,
-                   const unsigned char *sig, size_t sig_len );
+int mbedtls_pk_verify_ext(mbedtls_pk_type_t type, const void *options,
+                                      mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
+                                      const unsigned char *hash, size_t hash_len,
+                                      const unsigned char *sig, size_t sig_len);
 
 /**
  * \brief           Make signature, including padding if relevant.
@@ -342,10 +322,10 @@ int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
  * \note            For RSA, md_alg may be MBEDTLS_MD_NONE if hash_len != 0.
  *                  For ECDSA, md_alg may never be MBEDTLS_MD_NONE.
  */
-int mbedtls_pk_sign( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
-             const unsigned char *hash, size_t hash_len,
-             unsigned char *sig, size_t *sig_len,
-             int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
+int mbedtls_pk_sign(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
+                                const unsigned char *hash, size_t hash_len,
+                                unsigned char *sig, size_t *sig_len,
+                                int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
 
 /**
  * \brief           Decrypt message (including padding if relevant).
@@ -363,10 +343,10 @@ int mbedtls_pk_sign( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
  *
  * \return          0 on success, or a specific error code.
  */
-int mbedtls_pk_decrypt( mbedtls_pk_context *ctx,
-                const unsigned char *input, size_t ilen,
-                unsigned char *output, size_t *olen, size_t osize,
-                int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
+int mbedtls_pk_decrypt(mbedtls_pk_context *ctx,
+                                   const unsigned char *input, size_t ilen,
+                                   unsigned char *output, size_t *olen, size_t osize,
+                                   int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
 
 /**
  * \brief           Encrypt message (including padding if relevant).
@@ -384,10 +364,10 @@ int mbedtls_pk_decrypt( mbedtls_pk_context *ctx,
  *
  * \return          0 on success, or a specific error code.
  */
-int mbedtls_pk_encrypt( mbedtls_pk_context *ctx,
-                const unsigned char *input, size_t ilen,
-                unsigned char *output, size_t *olen, size_t osize,
-                int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
+int mbedtls_pk_encrypt(mbedtls_pk_context *ctx,
+                                   const unsigned char *input, size_t ilen,
+                                   unsigned char *output, size_t *olen, size_t osize,
+                                   int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
 
 /**
  * \brief           Check if a public-private pair of keys matches.
@@ -397,7 +377,7 @@ int mbedtls_pk_encrypt( mbedtls_pk_context *ctx,
  *
  * \return          0 on success or MBEDTLS_ERR_PK_BAD_INPUT_DATA
  */
-int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv );
+int mbedtls_pk_check_pair(const mbedtls_pk_context *pub, const mbedtls_pk_context *prv);
 
 /**
  * \brief           Export debug information
@@ -407,7 +387,7 @@ int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_conte
  *
  * \return          0 on success or MBEDTLS_ERR_PK_BAD_INPUT_DATA
  */
-int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items );
+int mbedtls_pk_debug(const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items);
 
 /**
  * \brief           Access the type name
@@ -416,7 +396,7 @@ int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *item
  *
  * \return          Type name on success, or "invalid PK"
  */
-const char * mbedtls_pk_get_name( const mbedtls_pk_context *ctx );
+const char *mbedtls_pk_get_name(const mbedtls_pk_context *ctx);
 
 /**
  * \brief           Get the key type
@@ -425,7 +405,7 @@ const char * mbedtls_pk_get_name( const mbedtls_pk_context *ctx );
  *
  * \return          Type on success, or MBEDTLS_PK_NONE
  */
-mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx );
+mbedtls_pk_type_t mbedtls_pk_get_type(const mbedtls_pk_context *ctx);
 
 #if defined(MBEDTLS_PK_PARSE_C)
 /** \ingroup pk_module */
@@ -447,9 +427,9 @@ mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx );
  *
  * \return          0 if successful, or a specific PK or PEM error code
  */
-int mbedtls_pk_parse_key( mbedtls_pk_context *ctx,
-                  const unsigned char *key, size_t keylen,
-                  const unsigned char *pwd, size_t pwdlen );
+int mbedtls_pk_parse_key(mbedtls_pk_context *ctx,
+                                     const unsigned char *key, size_t keylen,
+                                     const unsigned char *pwd, size_t pwdlen);
 
 /** \ingroup pk_module */
 /**
@@ -468,8 +448,8 @@ int mbedtls_pk_parse_key( mbedtls_pk_context *ctx,
  *
  * \return          0 if successful, or a specific PK or PEM error code
  */
-int mbedtls_pk_parse_public_key( mbedtls_pk_context *ctx,
-                         const unsigned char *key, size_t keylen );
+int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
+        const unsigned char *key, size_t keylen);
 
 #if defined(MBEDTLS_FS_IO)
 /** \ingroup pk_module */
@@ -488,8 +468,8 @@ int mbedtls_pk_parse_public_key( mbedtls_pk_context *ctx,
  *
  * \return          0 if successful, or a specific PK or PEM error code
  */
-int mbedtls_pk_parse_keyfile( mbedtls_pk_context *ctx,
-                      const char *path, const char *password );
+int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx,
+        const char *path, const char *password);
 
 /** \ingroup pk_module */
 /**
@@ -507,7 +487,7 @@ int mbedtls_pk_parse_keyfile( mbedtls_pk_context *ctx,
  *
  * \return          0 if successful, or a specific PK or PEM error code
  */
-int mbedtls_pk_parse_public_keyfile( mbedtls_pk_context *ctx, const char *path );
+int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path);
 #endif /* MBEDTLS_FS_IO */
 #endif /* MBEDTLS_PK_PARSE_C */
 
@@ -525,7 +505,7 @@ int mbedtls_pk_parse_public_keyfile( mbedtls_pk_context *ctx, const char *path )
  * \return          length of data written if successful, or a specific
  *                  error code
  */
-int mbedtls_pk_write_key_der( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
+int mbedtls_pk_write_key_der(mbedtls_pk_context *ctx, unsigned char *buf, size_t size);
 
 /**
  * \brief           Write a public key to a SubjectPublicKeyInfo DER structure
@@ -540,7 +520,7 @@ int mbedtls_pk_write_key_der( mbedtls_pk_context *ctx, unsigned char *buf, size_
  * \return          length of data written if successful, or a specific
  *                  error code
  */
-int mbedtls_pk_write_pubkey_der( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
+int mbedtls_pk_write_pubkey_der(mbedtls_pk_context *ctx, unsigned char *buf, size_t size);
 
 #if defined(MBEDTLS_PEM_WRITE_C)
 /**
@@ -552,7 +532,7 @@ int mbedtls_pk_write_pubkey_der( mbedtls_pk_context *ctx, unsigned char *buf, si
  *
  * \return          0 if successful, or a specific error code
  */
-int mbedtls_pk_write_pubkey_pem( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
+int mbedtls_pk_write_pubkey_pem(mbedtls_pk_context *ctx, unsigned char *buf, size_t size);
 
 /**
  * \brief           Write a private key to a PKCS#1 or SEC1 PEM string
@@ -563,7 +543,7 @@ int mbedtls_pk_write_pubkey_pem( mbedtls_pk_context *ctx, unsigned char *buf, si
  *
  * \return          0 if successful, or a specific error code
  */
-int mbedtls_pk_write_key_pem( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
+int mbedtls_pk_write_key_pem(mbedtls_pk_context *ctx, unsigned char *buf, size_t size);
 #endif /* MBEDTLS_PEM_WRITE_C */
 #endif /* MBEDTLS_PK_WRITE_C */
 
@@ -582,8 +562,8 @@ int mbedtls_pk_write_key_pem( mbedtls_pk_context *ctx, unsigned char *buf, size_
  *
  * \return          0 if successful, or a specific PK error code
  */
-int mbedtls_pk_parse_subpubkey( unsigned char **p, const unsigned char *end,
-                        mbedtls_pk_context *pk );
+int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
+        mbedtls_pk_context *pk);
 #endif /* MBEDTLS_PK_PARSE_C */
 
 #if defined(MBEDTLS_PK_WRITE_C)
@@ -597,8 +577,8 @@ int mbedtls_pk_parse_subpubkey( unsigned char **p, const unsigned char *end,
  *
  * \return          the length written or a negative error code
  */
-int mbedtls_pk_write_pubkey( unsigned char **p, unsigned char *start,
-                     const mbedtls_pk_context *key );
+int mbedtls_pk_write_pubkey(unsigned char **p, unsigned char *start,
+                                        const mbedtls_pk_context *key);
 #endif /* MBEDTLS_PK_WRITE_C */
 
 /*
@@ -606,7 +586,7 @@ int mbedtls_pk_write_pubkey( unsigned char **p, unsigned char *start,
  * know you do.
  */
 #if defined(MBEDTLS_FS_IO)
-int mbedtls_pk_load_file( const char *path, unsigned char **buf, size_t *n );
+int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n);
 #endif
 
 #ifdef __cplusplus

+ 5 - 21
iotkit-embedded/src/tls/mbedtls-in-iotkit/include/mbedtls/pk_internal.h → iotkit-embedded/external_libs/mbedtls/include/mbedtls/pk_internal.h

@@ -1,26 +1,10 @@
-/**
- * \file pk.h
- *
- * \brief Public Key abstraction layer: wrapper functions
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
 
+
+
+
 #ifndef MBEDTLS_PK_WRAP_H
 #define MBEDTLS_PK_WRAP_H
 

+ 30 - 46
iotkit-embedded/src/tls/mbedtls-in-iotkit/include/mbedtls/platform.h → iotkit-embedded/external_libs/mbedtls/include/mbedtls/platform.h

@@ -1,36 +1,20 @@
-/**
- * \file platform.h
- *
- * \brief mbed TLS Platform abstraction layer
- *
- *  Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
+
+
+
 #ifndef MBEDTLS_PLATFORM_H
 #define MBEDTLS_PLATFORM_H
 
 #if !defined(MBEDTLS_CONFIG_FILE)
-#include "config.h"
+    #include "config.h"
 #else
-#include MBEDTLS_CONFIG_FILE
+    #include MBEDTLS_CONFIG_FILE
 #endif
 
 #if defined(MBEDTLS_HAVE_TIME)
-#include "mbedtls/platform_time.h"
+    #include "mbedtls/platform_time.h"
 #endif
 
 #ifdef __cplusplus
@@ -111,8 +95,8 @@ extern "C" {
 #else
 /* For size_t */
 #include <stddef.h>
-extern void * (*mbedtls_calloc)( size_t n, size_t size );
-extern void (*mbedtls_free)( void *ptr );
+extern void *(*mbedtls_calloc)(size_t n, size_t size);
+extern void (*mbedtls_free)(void *ptr);
 
 /**
  * \brief   Set your own memory implementation function pointers
@@ -122,8 +106,8 @@ extern void (*mbedtls_free)( void *ptr );
  *
  * \return              0 if successful
  */
-int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
-                              void (*free_func)( void * ) );
+int mbedtls_platform_set_calloc_free(void *(*calloc_func)(size_t, size_t),
+        void (*free_func)(void *));
 #endif /* MBEDTLS_PLATFORM_FREE_MACRO && MBEDTLS_PLATFORM_CALLOC_MACRO */
 #else /* !MBEDTLS_PLATFORM_MEMORY */
 #define mbedtls_free       free
@@ -136,7 +120,7 @@ int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
 #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
 /* We need FILE * */
 #include <stdio.h>
-extern int (*mbedtls_fprintf)( FILE *stream, const char *format, ... );
+extern int (*mbedtls_fprintf)(FILE *stream, const char *format, ...);
 
 /**
  * \brief   Set your own fprintf function pointer
@@ -145,8 +129,8 @@ extern int (*mbedtls_fprintf)( FILE *stream, const char *format, ... );
  *
  * \return              0
  */
-int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *stream, const char *,
-                                               ... ) );
+int mbedtls_platform_set_fprintf(int (*fprintf_func)(FILE *stream, const char *,
+        ...));
 #else
 #if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO)
 #define mbedtls_fprintf    MBEDTLS_PLATFORM_FPRINTF_MACRO
@@ -159,7 +143,7 @@ int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *stream, const char
  * The function pointers for printf
  */
 #if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
-extern int (*mbedtls_printf)( const char *format, ... );
+extern int (*mbedtls_printf)(const char *format, ...);
 
 /**
  * \brief   Set your own printf function pointer
@@ -168,7 +152,7 @@ extern int (*mbedtls_printf)( const char *format, ... );
  *
  * \return              0
  */
-int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) );
+int mbedtls_platform_set_printf(int (*printf_func)(const char *, ...));
 #else /* !MBEDTLS_PLATFORM_PRINTF_ALT */
 #if defined(MBEDTLS_PLATFORM_PRINTF_MACRO)
 #define mbedtls_printf     MBEDTLS_PLATFORM_PRINTF_MACRO
@@ -188,11 +172,11 @@ int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) );
  */
 #if defined(_WIN32)
 /* For Windows (inc. MSYS2), we provide our own fixed implementation */
-int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... );
+int mbedtls_platform_win32_snprintf(char *s, size_t n, const char *fmt, ...);
 #endif
 
 #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
-extern int (*mbedtls_snprintf)( char * s, size_t n, const char * format, ... );
+extern int (*mbedtls_snprintf)(char *s, size_t n, const char *format, ...);
 
 /**
  * \brief   Set your own snprintf function pointer
@@ -201,8 +185,8 @@ extern int (*mbedtls_snprintf)( char * s, size_t n, const char * format, ... );
  *
  * \return              0
  */
-int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
-                                                 const char * format, ... ) );
+int mbedtls_platform_set_snprintf(int (*snprintf_func)(char *s, size_t n,
+        const char *format, ...));
 #else /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
 #if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO)
 #define mbedtls_snprintf   MBEDTLS_PLATFORM_SNPRINTF_MACRO
@@ -215,7 +199,7 @@ int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
  * The function pointers for exit
  */
 #if defined(MBEDTLS_PLATFORM_EXIT_ALT)
-extern void (*mbedtls_exit)( int status );
+extern void (*mbedtls_exit)(int status);
 
 /**
  * \brief   Set your own exit function pointer
@@ -224,7 +208,7 @@ extern void (*mbedtls_exit)( int status );
  *
  * \return              0
  */
-int mbedtls_platform_set_exit( void (*exit_func)( int status ) );
+int mbedtls_platform_set_exit(void (*exit_func)(int status));
 #else
 #if defined(MBEDTLS_PLATFORM_EXIT_MACRO)
 #define mbedtls_exit   MBEDTLS_PLATFORM_EXIT_MACRO
@@ -256,13 +240,13 @@ int mbedtls_platform_set_exit( void (*exit_func)( int status ) );
 #if defined(MBEDTLS_ENTROPY_NV_SEED)
 #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
 /* Internal standard platform definitions */
-int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len );
-int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len );
+int mbedtls_platform_std_nv_seed_read(unsigned char *buf, size_t buf_len);
+int mbedtls_platform_std_nv_seed_write(unsigned char *buf, size_t buf_len);
 #endif
 
 #if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
-extern int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len );
-extern int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len );
+extern int (*mbedtls_nv_seed_read)(unsigned char *buf, size_t buf_len);
+extern int (*mbedtls_nv_seed_write)(unsigned char *buf, size_t buf_len);
 
 /**
  * \brief   Set your own seed file writing/reading functions
@@ -273,9 +257,9 @@ extern int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len );
  * \return              0
  */
 int mbedtls_platform_set_nv_seed(
-            int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ),
-            int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len )
-            );
+            int (*nv_seed_read_func)(unsigned char *buf, size_t buf_len),
+            int (*nv_seed_write_func)(unsigned char *buf, size_t buf_len)
+);
 #else
 #if defined(MBEDTLS_PLATFORM_NV_SEED_READ_MACRO) && \
     defined(MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO)

+ 132 - 149
iotkit-embedded/src/tls/mbedtls-in-iotkit/include/mbedtls/rsa.h → iotkit-embedded/external_libs/mbedtls/include/mbedtls/rsa.h

@@ -1,39 +1,23 @@
-/**
- * \file rsa.h
- *
- * \brief The RSA public-key cryptosystem
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
+
+
+
 #ifndef MBEDTLS_RSA_H
 #define MBEDTLS_RSA_H
 
 #if !defined(MBEDTLS_CONFIG_FILE)
-#include "config.h"
+    #include "config.h"
 #else
-#include MBEDTLS_CONFIG_FILE
+    #include MBEDTLS_CONFIG_FILE
 #endif
 
 #include "bignum.h"
 #include "md.h"
 
 #if defined(MBEDTLS_THREADING_C)
-#include "threading.h"
+    #include "threading.h"
 #endif
 
 /*
@@ -76,8 +60,7 @@ extern "C" {
 /**
  * \brief          RSA context structure
  */
-typedef struct
-{
+typedef struct {
     int ver;                    /*!<  always 0          */
     size_t len;                 /*!<  size(N) in chars  */
 
@@ -134,9 +117,9 @@ mbedtls_rsa_context;
  *                 but can be overriden (and always is, if set to
  *                 MBEDTLS_MD_NONE) for verifying them.
  */
-void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
-               int padding,
-               int hash_id);
+void mbedtls_rsa_init(mbedtls_rsa_context *ctx,
+                                  int padding,
+                                  int hash_id);
 
 /**
  * \brief          Set padding for an already initialized RSA context
@@ -146,7 +129,7 @@ void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
  * \param padding  MBEDTLS_RSA_PKCS_V15 or MBEDTLS_RSA_PKCS_V21
  * \param hash_id  MBEDTLS_RSA_PKCS_V21 hash identifier
  */
-void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id);
+void mbedtls_rsa_set_padding(mbedtls_rsa_context *ctx, int padding, int hash_id);
 
 /**
  * \brief          Generate an RSA keypair
@@ -162,10 +145,10 @@ void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id
  *
  * \return         0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
  */
-int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
-                 int (*f_rng)(void *, unsigned char *, size_t),
-                 void *p_rng,
-                 unsigned int nbits, int exponent );
+int mbedtls_rsa_gen_key(mbedtls_rsa_context *ctx,
+                                    int (*f_rng)(void *, unsigned char *, size_t),
+                                    void *p_rng,
+                                    unsigned int nbits, int exponent);
 
 /**
  * \brief          Check a public RSA key
@@ -174,7 +157,7 @@ int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
  *
  * \return         0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
  */
-int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx );
+int mbedtls_rsa_check_pubkey(const mbedtls_rsa_context *ctx);
 
 /**
  * \brief          Check a private RSA key
@@ -183,7 +166,7 @@ int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx );
  *
  * \return         0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
  */
-int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
+int mbedtls_rsa_check_privkey(const mbedtls_rsa_context *ctx);
 
 /**
  * \brief          Check a public-private RSA key pair.
@@ -194,7 +177,7 @@ int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
  *
  * \return         0 if successful, or an MBEDTLS_ERR_RSA_XXX error code
  */
-int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv );
+int mbedtls_rsa_check_pub_priv(const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv);
 
 /**
  * \brief          Do an RSA public key operation
@@ -212,9 +195,9 @@ int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, const mbedtls_rs
  * \note           The input and output buffers must be large
  *                 enough (eg. 128 bytes if RSA-1024 is used).
  */
-int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
-                const unsigned char *input,
-                unsigned char *output );
+int mbedtls_rsa_public(mbedtls_rsa_context *ctx,
+                                   const unsigned char *input,
+                                   unsigned char *output);
 
 /**
  * \brief          Do an RSA private key operation
@@ -230,11 +213,11 @@ int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
  * \note           The input and output buffers must be large
  *                 enough (eg. 128 bytes if RSA-1024 is used).
  */
-int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
-                 int (*f_rng)(void *, unsigned char *, size_t),
-                 void *p_rng,
-                 const unsigned char *input,
-                 unsigned char *output );
+int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
+                                    int (*f_rng)(void *, unsigned char *, size_t),
+                                    void *p_rng,
+                                    const unsigned char *input,
+                                    unsigned char *output);
 
 /**
  * \brief          Generic wrapper to perform a PKCS#1 encryption using the
@@ -255,12 +238,12 @@ int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
  * \note           The output buffer must be as large as the size
  *                 of ctx->N (eg. 128 bytes if RSA-1024 is used).
  */
-int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
-                       int (*f_rng)(void *, unsigned char *, size_t),
-                       void *p_rng,
-                       int mode, size_t ilen,
-                       const unsigned char *input,
-                       unsigned char *output );
+int mbedtls_rsa_pkcs1_encrypt(mbedtls_rsa_context *ctx,
+        int (*f_rng)(void *, unsigned char *, size_t),
+        void *p_rng,
+        int mode, size_t ilen,
+        const unsigned char *input,
+        unsigned char *output);
 
 /**
  * \brief          Perform a PKCS#1 v1.5 encryption (RSAES-PKCS1-v1_5-ENCRYPT)
@@ -278,12 +261,12 @@ int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
  * \note           The output buffer must be as large as the size
  *                 of ctx->N (eg. 128 bytes if RSA-1024 is used).
  */
-int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
-                                 int (*f_rng)(void *, unsigned char *, size_t),
-                                 void *p_rng,
-                                 int mode, size_t ilen,
-                                 const unsigned char *input,
-                                 unsigned char *output );
+int mbedtls_rsa_rsaes_pkcs1_v15_encrypt(mbedtls_rsa_context *ctx,
+        int (*f_rng)(void *, unsigned char *, size_t),
+        void *p_rng,
+        int mode, size_t ilen,
+        const unsigned char *input,
+        unsigned char *output);
 
 /**
  * \brief          Perform a PKCS#1 v2.1 OAEP encryption (RSAES-OAEP-ENCRYPT)
@@ -304,14 +287,14 @@ int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
  * \note           The output buffer must be as large as the size
  *                 of ctx->N (eg. 128 bytes if RSA-1024 is used).
  */
-int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
-                            int (*f_rng)(void *, unsigned char *, size_t),
-                            void *p_rng,
-                            int mode,
-                            const unsigned char *label, size_t label_len,
-                            size_t ilen,
-                            const unsigned char *input,
-                            unsigned char *output );
+int mbedtls_rsa_rsaes_oaep_encrypt(mbedtls_rsa_context *ctx,
+        int (*f_rng)(void *, unsigned char *, size_t),
+        void *p_rng,
+        int mode,
+        const unsigned char *label, size_t label_len,
+        size_t ilen,
+        const unsigned char *input,
+        unsigned char *output);
 
 /**
  * \brief          Generic wrapper to perform a PKCS#1 decryption using the
@@ -333,13 +316,13 @@ int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
  *                 of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
  *                 an error is thrown.
  */
-int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
-                       int (*f_rng)(void *, unsigned char *, size_t),
-                       void *p_rng,
-                       int mode, size_t *olen,
-                       const unsigned char *input,
-                       unsigned char *output,
-                       size_t output_max_len );
+int mbedtls_rsa_pkcs1_decrypt(mbedtls_rsa_context *ctx,
+        int (*f_rng)(void *, unsigned char *, size_t),
+        void *p_rng,
+        int mode, size_t *olen,
+        const unsigned char *input,
+        unsigned char *output,
+        size_t output_max_len);
 
 /**
  * \brief          Perform a PKCS#1 v1.5 decryption (RSAES-PKCS1-v1_5-DECRYPT)
@@ -359,13 +342,13 @@ int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
  *                 of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
  *                 an error is thrown.
  */
-int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
-                                 int (*f_rng)(void *, unsigned char *, size_t),
-                                 void *p_rng,
-                                 int mode, size_t *olen,
-                                 const unsigned char *input,
-                                 unsigned char *output,
-                                 size_t output_max_len );
+int mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_rsa_context *ctx,
+        int (*f_rng)(void *, unsigned char *, size_t),
+        void *p_rng,
+        int mode, size_t *olen,
+        const unsigned char *input,
+        unsigned char *output,
+        size_t output_max_len);
 
 /**
  * \brief          Perform a PKCS#1 v2.1 OAEP decryption (RSAES-OAEP-DECRYPT)
@@ -387,15 +370,15 @@ int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
  *                 of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
  *                 an error is thrown.
  */
-int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
-                            int (*f_rng)(void *, unsigned char *, size_t),
-                            void *p_rng,
-                            int mode,
-                            const unsigned char *label, size_t label_len,
-                            size_t *olen,
-                            const unsigned char *input,
-                            unsigned char *output,
-                            size_t output_max_len );
+int mbedtls_rsa_rsaes_oaep_decrypt(mbedtls_rsa_context *ctx,
+        int (*f_rng)(void *, unsigned char *, size_t),
+        void *p_rng,
+        int mode,
+        const unsigned char *label, size_t label_len,
+        size_t *olen,
+        const unsigned char *input,
+        unsigned char *output,
+        size_t output_max_len);
 
 /**
  * \brief          Generic wrapper to perform a PKCS#1 signature using the
@@ -421,14 +404,14 @@ int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
  * \note           In case of PKCS#1 v2.1 encoding, see comments on
  * \note           \c mbedtls_rsa_rsassa_pss_sign() for details on md_alg and hash_id.
  */
-int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
-                    int (*f_rng)(void *, unsigned char *, size_t),
-                    void *p_rng,
-                    int mode,
-                    mbedtls_md_type_t md_alg,
-                    unsigned int hashlen,
-                    const unsigned char *hash,
-                    unsigned char *sig );
+int mbedtls_rsa_pkcs1_sign(mbedtls_rsa_context *ctx,
+                                       int (*f_rng)(void *, unsigned char *, size_t),
+                                       void *p_rng,
+                                       int mode,
+                                       mbedtls_md_type_t md_alg,
+                                       unsigned int hashlen,
+                                       const unsigned char *hash,
+                                       unsigned char *sig);
 
 /**
  * \brief          Perform a PKCS#1 v1.5 signature (RSASSA-PKCS1-v1_5-SIGN)
@@ -448,14 +431,14 @@ int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
  * \note           The "sig" buffer must be as large as the size
  *                 of ctx->N (eg. 128 bytes if RSA-1024 is used).
  */
-int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
-                               int (*f_rng)(void *, unsigned char *, size_t),
-                               void *p_rng,
-                               int mode,
-                               mbedtls_md_type_t md_alg,
-                               unsigned int hashlen,
-                               const unsigned char *hash,
-                               unsigned char *sig );
+int mbedtls_rsa_rsassa_pkcs1_v15_sign(mbedtls_rsa_context *ctx,
+        int (*f_rng)(void *, unsigned char *, size_t),
+        void *p_rng,
+        int mode,
+        mbedtls_md_type_t md_alg,
+        unsigned int hashlen,
+        const unsigned char *hash,
+        unsigned char *sig);
 
 /**
  * \brief          Perform a PKCS#1 v2.1 PSS signature (RSASSA-PSS-SIGN)
@@ -481,14 +464,14 @@ int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
  *                 that is encoded. According to RFC 3447 it is advised to
  *                 keep both hashes the same.
  */
-int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
-                         int (*f_rng)(void *, unsigned char *, size_t),
-                         void *p_rng,
-                         int mode,
-                         mbedtls_md_type_t md_alg,
-                         unsigned int hashlen,
-                         const unsigned char *hash,
-                         unsigned char *sig );
+int mbedtls_rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
+        int (*f_rng)(void *, unsigned char *, size_t),
+        void *p_rng,
+        int mode,
+        mbedtls_md_type_t md_alg,
+        unsigned int hashlen,
+        const unsigned char *hash,
+        unsigned char *sig);
 
 /**
  * \brief          Generic wrapper to perform a PKCS#1 verification using the
@@ -513,14 +496,14 @@ int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
  * \note           In case of PKCS#1 v2.1 encoding, see comments on
  *                 \c mbedtls_rsa_rsassa_pss_verify() about md_alg and hash_id.
  */
-int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
-                      int (*f_rng)(void *, unsigned char *, size_t),
-                      void *p_rng,
-                      int mode,
-                      mbedtls_md_type_t md_alg,
-                      unsigned int hashlen,
-                      const unsigned char *hash,
-                      const unsigned char *sig );
+int mbedtls_rsa_pkcs1_verify(mbedtls_rsa_context *ctx,
+        int (*f_rng)(void *, unsigned char *, size_t),
+        void *p_rng,
+        int mode,
+        mbedtls_md_type_t md_alg,
+        unsigned int hashlen,
+        const unsigned char *hash,
+        const unsigned char *sig);
 
 /**
  * \brief          Perform a PKCS#1 v1.5 verification (RSASSA-PKCS1-v1_5-VERIFY)
@@ -540,14 +523,14 @@ int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
  * \note           The "sig" buffer must be as large as the size
  *                 of ctx->N (eg. 128 bytes if RSA-1024 is used).
  */
-int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
-                                 int (*f_rng)(void *, unsigned char *, size_t),
-                                 void *p_rng,
-                                 int mode,
-                                 mbedtls_md_type_t md_alg,
-                                 unsigned int hashlen,
-                                 const unsigned char *hash,
-                                 const unsigned char *sig );
+int mbedtls_rsa_rsassa_pkcs1_v15_verify(mbedtls_rsa_context *ctx,
+        int (*f_rng)(void *, unsigned char *, size_t),
+        void *p_rng,
+        int mode,
+        mbedtls_md_type_t md_alg,
+        unsigned int hashlen,
+        const unsigned char *hash,
+        const unsigned char *sig);
 
 /**
  * \brief          Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
@@ -574,14 +557,14 @@ int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
  *                 keep both hashes the same. If hash_id in the RSA context is
  *                 unset, the md_alg from the function call is used.
  */
-int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
-                           int (*f_rng)(void *, unsigned char *, size_t),
-                           void *p_rng,
-                           int mode,
-                           mbedtls_md_type_t md_alg,
-                           unsigned int hashlen,
-                           const unsigned char *hash,
-                           const unsigned char *sig );
+int mbedtls_rsa_rsassa_pss_verify(mbedtls_rsa_context *ctx,
+        int (*f_rng)(void *, unsigned char *, size_t),
+        void *p_rng,
+        int mode,
+        mbedtls_md_type_t md_alg,
+        unsigned int hashlen,
+        const unsigned char *hash,
+        const unsigned char *sig);
 
 /**
  * \brief          Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
@@ -607,16 +590,16 @@ int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
  *
  * \note           The hash_id in the RSA context is ignored.
  */
-int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
-                               int (*f_rng)(void *, unsigned char *, size_t),
-                               void *p_rng,
-                               int mode,
-                               mbedtls_md_type_t md_alg,
-                               unsigned int hashlen,
-                               const unsigned char *hash,
-                               mbedtls_md_type_t mgf1_hash_id,
-                               int expected_salt_len,
-                               const unsigned char *sig );
+int mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_rsa_context *ctx,
+        int (*f_rng)(void *, unsigned char *, size_t),
+        void *p_rng,
+        int mode,
+        mbedtls_md_type_t md_alg,
+        unsigned int hashlen,
+        const unsigned char *hash,
+        mbedtls_md_type_t mgf1_hash_id,
+        int expected_salt_len,
+        const unsigned char *sig);
 
 /**
  * \brief          Copy the components of an RSA context
@@ -627,21 +610,21 @@ int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
  * \return         0 on success,
  *                 MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure
  */
-int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
+int mbedtls_rsa_copy(mbedtls_rsa_context *dst, const mbedtls_rsa_context *src);
 
 /**
  * \brief          Free the components of an RSA key
  *
  * \param ctx      RSA Context to free
  */
-void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
+void mbedtls_rsa_free(mbedtls_rsa_context *ctx);
 
 /**
  * \brief          Checkup routine
  *
  * \return         0 if successful, or 1 if the test failed
  */
-int mbedtls_rsa_self_test( int verbose );
+int mbedtls_rsa_self_test(int verbose);
 
 #ifdef __cplusplus
 }

+ 18 - 35
iotkit-embedded/src/tls/mbedtls-in-iotkit/include/mbedtls/sha1.h → iotkit-embedded/external_libs/mbedtls/include/mbedtls/sha1.h

@@ -1,32 +1,16 @@
-/**
- * \file sha1.h
- *
- * \brief SHA-1 cryptographic hash function
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
+
+
+
 #ifndef MBEDTLS_SHA1_H
 #define MBEDTLS_SHA1_H
 
 #if !defined(MBEDTLS_CONFIG_FILE)
-#include "config.h"
+    #include "config.h"
 #else
-#include MBEDTLS_CONFIG_FILE
+    #include MBEDTLS_CONFIG_FILE
 #endif
 
 #include <stddef.h>
@@ -43,8 +27,7 @@ extern "C" {
 /**
  * \brief          SHA-1 context structure
  */
-typedef struct
-{
+typedef struct {
     uint32_t total[2];          /*!< number of bytes processed  */
     uint32_t state[5];          /*!< intermediate digest state  */
     unsigned char buffer[64];   /*!< data block being processed */
@@ -56,14 +39,14 @@ mbedtls_sha1_context;
  *
  * \param ctx      SHA-1 context to be initialized
  */
-void mbedtls_sha1_init( mbedtls_sha1_context *ctx );
+void mbedtls_sha1_init(mbedtls_sha1_context *ctx);
 
 /**
  * \brief          Clear SHA-1 context
  *
  * \param ctx      SHA-1 context to be cleared
  */
-void mbedtls_sha1_free( mbedtls_sha1_context *ctx );
+void mbedtls_sha1_free(mbedtls_sha1_context *ctx);
 
 /**
  * \brief          Clone (the state of) a SHA-1 context
@@ -71,15 +54,15 @@ void mbedtls_sha1_free( mbedtls_sha1_context *ctx );
  * \param dst      The destination context
  * \param src      The context to be cloned
  */
-void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
-                         const mbedtls_sha1_context *src );
+void mbedtls_sha1_clone(mbedtls_sha1_context *dst,
+                                    const mbedtls_sha1_context *src);
 
 /**
  * \brief          SHA-1 context setup
  *
  * \param ctx      context to be initialized
  */
-void mbedtls_sha1_starts( mbedtls_sha1_context *ctx );
+void mbedtls_sha1_starts(mbedtls_sha1_context *ctx);
 
 /**
  * \brief          SHA-1 process buffer
@@ -88,7 +71,7 @@ void mbedtls_sha1_starts( mbedtls_sha1_context *ctx );
  * \param input    buffer holding the  data
  * \param ilen     length of the input data
  */
-void mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen );
+void mbedtls_sha1_update(mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen);
 
 /**
  * \brief          SHA-1 final digest
@@ -96,10 +79,10 @@ void mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input,
  * \param ctx      SHA-1 context
  * \param output   SHA-1 checksum result
  */
-void mbedtls_sha1_finish( mbedtls_sha1_context *ctx, unsigned char output[20] );
+void mbedtls_sha1_finish(mbedtls_sha1_context *ctx, unsigned char output[20]);
 
 /* Internal use */
-void mbedtls_sha1_process( mbedtls_sha1_context *ctx, const unsigned char data[64] );
+void mbedtls_sha1_process(mbedtls_sha1_context *ctx, const unsigned char data[64]);
 
 #ifdef __cplusplus
 }
@@ -120,14 +103,14 @@ extern "C" {
  * \param ilen     length of the input data
  * \param output   SHA-1 checksum result
  */
-void mbedtls_sha1( const unsigned char *input, size_t ilen, unsigned char output[20] );
+void mbedtls_sha1(const unsigned char *input, size_t ilen, unsigned char output[20]);
 
 /**
  * \brief          Checkup routine
  *
  * \return         0 if successful, or 1 if the test failed
  */
-int mbedtls_sha1_self_test( int verbose );
+int mbedtls_sha1_self_test(int verbose);
 
 #ifdef __cplusplus
 }

+ 20 - 37
iotkit-embedded/src/tls/mbedtls-in-iotkit/include/mbedtls/sha256.h → iotkit-embedded/external_libs/mbedtls/include/mbedtls/sha256.h

@@ -1,32 +1,16 @@
-/**
- * \file sha256.h
- *
- * \brief SHA-224 and SHA-256 cryptographic hash function
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
+
+
+
 #ifndef MBEDTLS_SHA256_H
 #define MBEDTLS_SHA256_H
 
 #if !defined(MBEDTLS_CONFIG_FILE)
-#include "config.h"
+    #include "config.h"
 #else
-#include MBEDTLS_CONFIG_FILE
+    #include MBEDTLS_CONFIG_FILE
 #endif
 
 #include <stddef.h>
@@ -43,8 +27,7 @@ extern "C" {
 /**
  * \brief          SHA-256 context structure
  */
-typedef struct
-{
+typedef struct {
     uint32_t total[2];          /*!< number of bytes processed  */
     uint32_t state[8];          /*!< intermediate digest state  */
     unsigned char buffer[64];   /*!< data block being processed */
@@ -57,14 +40,14 @@ mbedtls_sha256_context;
  *
  * \param ctx      SHA-256 context to be initialized
  */
-void mbedtls_sha256_init( mbedtls_sha256_context *ctx );
+void mbedtls_sha256_init(mbedtls_sha256_context *ctx);
 
 /**
  * \brief          Clear SHA-256 context
  *
  * \param ctx      SHA-256 context to be cleared
  */
-void mbedtls_sha256_free( mbedtls_sha256_context *ctx );
+void mbedtls_sha256_free(mbedtls_sha256_context *ctx);
 
 /**
  * \brief          Clone (the state of) a SHA-256 context
@@ -72,8 +55,8 @@ void mbedtls_sha256_free( mbedtls_sha256_context *ctx );
  * \param dst      The destination context
  * \param src      The context to be cloned
  */
-void mbedtls_sha256_clone( mbedtls_sha256_context *dst,
-                           const mbedtls_sha256_context *src );
+void mbedtls_sha256_clone(mbedtls_sha256_context *dst,
+                                      const mbedtls_sha256_context *src);
 
 /**
  * \brief          SHA-256 context setup
@@ -81,7 +64,7 @@ void mbedtls_sha256_clone( mbedtls_sha256_context *dst,
  * \param ctx      context to be initialized
  * \param is224    0 = use SHA256, 1 = use SHA224
  */
-void mbedtls_sha256_starts( mbedtls_sha256_context *ctx, int is224 );
+void mbedtls_sha256_starts(mbedtls_sha256_context *ctx, int is224);
 
 /**
  * \brief          SHA-256 process buffer
@@ -90,8 +73,8 @@ void mbedtls_sha256_starts( mbedtls_sha256_context *ctx, int is224 );
  * \param input    buffer holding the  data
  * \param ilen     length of the input data
  */
-void mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *input,
-                    size_t ilen );
+void mbedtls_sha256_update(mbedtls_sha256_context *ctx, const unsigned char *input,
+                                       size_t ilen);
 
 /**
  * \brief          SHA-256 final digest
@@ -99,10 +82,10 @@ void mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *in
  * \param ctx      SHA-256 context
  * \param output   SHA-224/256 checksum result
  */
-void mbedtls_sha256_finish( mbedtls_sha256_context *ctx, unsigned char output[32] );
+void mbedtls_sha256_finish(mbedtls_sha256_context *ctx, unsigned char output[32]);
 
 /* Internal use */
-void mbedtls_sha256_process( mbedtls_sha256_context *ctx, const unsigned char data[64] );
+void mbedtls_sha256_process(mbedtls_sha256_context *ctx, const unsigned char data[64]);
 
 #ifdef __cplusplus
 }
@@ -124,15 +107,15 @@ extern "C" {
  * \param output   SHA-224/256 checksum result
  * \param is224    0 = use SHA256, 1 = use SHA224
  */
-void mbedtls_sha256( const unsigned char *input, size_t ilen,
-           unsigned char output[32], int is224 );
+void mbedtls_sha256(const unsigned char *input, size_t ilen,
+                                unsigned char output[32], int is224);
 
 /**
  * \brief          Checkup routine
  *
  * \return         0 if successful, or 1 if the test failed
  */
-int mbedtls_sha256_self_test( int verbose );
+int mbedtls_sha256_self_test(int verbose);
 
 #ifdef __cplusplus
 }

Diff do ficheiro suprimidas por serem muito extensas
+ 197 - 204
iotkit-embedded/external_libs/mbedtls/include/mbedtls/ssl.h


+ 13 - 30
iotkit-embedded/src/tls/mbedtls-in-iotkit/include/mbedtls/ssl_ciphersuites.h → iotkit-embedded/external_libs/mbedtls/include/mbedtls/ssl_ciphersuites.h

@@ -1,25 +1,9 @@
-/**
- * \file ssl_ciphersuites.h
- *
- * \brief SSL Ciphersuites for mbed TLS
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
+
+
+
 #ifndef MBEDTLS_SSL_CIPHERSUITES_H
 #define MBEDTLS_SSL_CIPHERSUITES_H
 
@@ -285,10 +269,9 @@ typedef struct mbedtls_ssl_ciphersuite_t mbedtls_ssl_ciphersuite_t;
 /**
  * \brief   This structure is used for storing ciphersuite information
  */
-struct mbedtls_ssl_ciphersuite_t
-{
+struct mbedtls_ssl_ciphersuite_t {
     int id;
-    const char * name;
+    const char *name;
 
     mbedtls_cipher_type_t cipher;
     mbedtls_md_type_t mac;
@@ -302,17 +285,17 @@ struct mbedtls_ssl_ciphersuite_t
     unsigned char flags;
 };
 
-const int *mbedtls_ssl_list_ciphersuites( void );
+const int *mbedtls_ssl_list_ciphersuites(void);
 
-const mbedtls_ssl_ciphersuite_t *mbedtls_ssl_ciphersuite_from_string( const char *ciphersuite_name );
-const mbedtls_ssl_ciphersuite_t *mbedtls_ssl_ciphersuite_from_id( int ciphersuite_id );
+const mbedtls_ssl_ciphersuite_t *mbedtls_ssl_ciphersuite_from_string(const char *ciphersuite_name);
+const mbedtls_ssl_ciphersuite_t *mbedtls_ssl_ciphersuite_from_id(int ciphersuite_id);
 
 #if defined(MBEDTLS_PK_C)
-mbedtls_pk_type_t mbedtls_ssl_get_ciphersuite_sig_pk_alg( const mbedtls_ssl_ciphersuite_t *info );
+mbedtls_pk_type_t mbedtls_ssl_get_ciphersuite_sig_pk_alg(const mbedtls_ssl_ciphersuite_t *info);
 #endif
 
-int mbedtls_ssl_ciphersuite_uses_ec( const mbedtls_ssl_ciphersuite_t *info );
-int mbedtls_ssl_ciphersuite_uses_psk( const mbedtls_ssl_ciphersuite_t *info );
+int mbedtls_ssl_ciphersuite_uses_ec(const mbedtls_ssl_ciphersuite_t *info);
+int mbedtls_ssl_ciphersuite_uses_psk(const mbedtls_ssl_ciphersuite_t *info);
 
 #ifdef __cplusplus
 }

+ 14 - 31
iotkit-embedded/src/tls/mbedtls-in-iotkit/include/mbedtls/ssl_cookie.h → iotkit-embedded/external_libs/mbedtls/include/mbedtls/ssl_cookie.h

@@ -1,32 +1,16 @@
-/**
- * \file ssl_cookie.h
- *
- * \brief DTLS cookie callbacks implementation
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
+
+
+
 #ifndef MBEDTLS_SSL_COOKIE_H
 #define MBEDTLS_SSL_COOKIE_H
 
 #include "ssl.h"
 
 #if defined(MBEDTLS_THREADING_C)
-#include "threading.h"
+    #include "threading.h"
 #endif
 
 /**
@@ -37,7 +21,7 @@
  * \{
  */
 #ifndef MBEDTLS_SSL_COOKIE_TIMEOUT
-#define MBEDTLS_SSL_COOKIE_TIMEOUT     60 /**< Default expiration delay of DTLS cookies, in seconds if HAVE_TIME, or in number of cookies issued */
+    #define MBEDTLS_SSL_COOKIE_TIMEOUT     60 /**< Default expiration delay of DTLS cookies, in seconds if HAVE_TIME, or in number of cookies issued */
 #endif
 
 /* \} name SECTION: Module settings */
@@ -49,8 +33,7 @@ extern "C" {
 /**
  * \brief          Context for the default cookie functions.
  */
-typedef struct
-{
+typedef struct {
     mbedtls_md_context_t    hmac_ctx;   /*!< context for the HMAC portion   */
 #if !defined(MBEDTLS_HAVE_TIME)
     unsigned long   serial;     /*!< serial number for expiration   */
@@ -66,14 +49,14 @@ typedef struct
 /**
  * \brief          Initialize cookie context
  */
-void mbedtls_ssl_cookie_init( mbedtls_ssl_cookie_ctx *ctx );
+void mbedtls_ssl_cookie_init(mbedtls_ssl_cookie_ctx *ctx);
 
 /**
  * \brief          Setup cookie context (generate keys)
  */
-int mbedtls_ssl_cookie_setup( mbedtls_ssl_cookie_ctx *ctx,
-                      int (*f_rng)(void *, unsigned char *, size_t),
-                      void *p_rng );
+int mbedtls_ssl_cookie_setup(mbedtls_ssl_cookie_ctx *ctx,
+        int (*f_rng)(void *, unsigned char *, size_t),
+        void *p_rng);
 
 /**
  * \brief          Set expiration delay for cookies
@@ -84,12 +67,12 @@ int mbedtls_ssl_cookie_setup( mbedtls_ssl_cookie_ctx *ctx,
  *                 issued in the meantime.
  *                 0 to disable expiration (NOT recommended)
  */
-void mbedtls_ssl_cookie_set_timeout( mbedtls_ssl_cookie_ctx *ctx, unsigned long delay );
+void mbedtls_ssl_cookie_set_timeout(mbedtls_ssl_cookie_ctx *ctx, unsigned long delay);
 
 /**
  * \brief          Free cookie context
  */
-void mbedtls_ssl_cookie_free( mbedtls_ssl_cookie_ctx *ctx );
+void mbedtls_ssl_cookie_free(mbedtls_ssl_cookie_ctx *ctx);
 
 /**
  * \brief          Generate cookie, see \c mbedtls_ssl_cookie_write_t

+ 135 - 150
iotkit-embedded/src/tls/mbedtls-in-iotkit/include/mbedtls/ssl_internal.h → iotkit-embedded/external_libs/mbedtls/include/mbedtls/ssl_internal.h

@@ -1,91 +1,75 @@
-/**
- * \file ssl_ticket.h
- *
- * \brief Internal functions shared by the SSL modules
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
+
+
+
 #ifndef MBEDTLS_SSL_INTERNAL_H
 #define MBEDTLS_SSL_INTERNAL_H
 
 #include "ssl.h"
 
 #if defined(MBEDTLS_MD5_C)
-#include "md5.h"
+    #include "md5.h"
 #endif
 
 #if defined(MBEDTLS_SHA1_C)
-#include "sha1.h"
+    #include "sha1.h"
 #endif
 
 #if defined(MBEDTLS_SHA256_C)
-#include "sha256.h"
+    #include "sha256.h"
 #endif
 
 #if defined(MBEDTLS_SHA512_C)
-#include "sha512.h"
+    #include "sha512.h"
 #endif
 
 #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
-#include "ecjpake.h"
+    #include "ecjpake.h"
 #endif
 
 #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
     !defined(inline) && !defined(__cplusplus)
-#define inline __inline
+    #define inline __inline
 #endif
 
 /* Determine minimum supported version */
 #define MBEDTLS_SSL_MIN_MAJOR_VERSION           MBEDTLS_SSL_MAJOR_VERSION_3
 
 #if defined(MBEDTLS_SSL_PROTO_SSL3)
-#define MBEDTLS_SSL_MIN_MINOR_VERSION           MBEDTLS_SSL_MINOR_VERSION_0
-#else
-#if defined(MBEDTLS_SSL_PROTO_TLS1)
-#define MBEDTLS_SSL_MIN_MINOR_VERSION           MBEDTLS_SSL_MINOR_VERSION_1
-#else
-#if defined(MBEDTLS_SSL_PROTO_TLS1_1)
-#define MBEDTLS_SSL_MIN_MINOR_VERSION           MBEDTLS_SSL_MINOR_VERSION_2
+    #define MBEDTLS_SSL_MIN_MINOR_VERSION           MBEDTLS_SSL_MINOR_VERSION_0
 #else
-#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
-#define MBEDTLS_SSL_MIN_MINOR_VERSION           MBEDTLS_SSL_MINOR_VERSION_3
-#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
-#endif /* MBEDTLS_SSL_PROTO_TLS1_1 */
-#endif /* MBEDTLS_SSL_PROTO_TLS1   */
+    #if defined(MBEDTLS_SSL_PROTO_TLS1)
+        #define MBEDTLS_SSL_MIN_MINOR_VERSION           MBEDTLS_SSL_MINOR_VERSION_1
+    #else
+        #if defined(MBEDTLS_SSL_PROTO_TLS1_1)
+            #define MBEDTLS_SSL_MIN_MINOR_VERSION           MBEDTLS_SSL_MINOR_VERSION_2
+        #else
+            #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
+                #define MBEDTLS_SSL_MIN_MINOR_VERSION           MBEDTLS_SSL_MINOR_VERSION_3
+            #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
+        #endif /* MBEDTLS_SSL_PROTO_TLS1_1 */
+    #endif /* MBEDTLS_SSL_PROTO_TLS1   */
 #endif /* MBEDTLS_SSL_PROTO_SSL3   */
 
 /* Determine maximum supported version */
 #define MBEDTLS_SSL_MAX_MAJOR_VERSION           MBEDTLS_SSL_MAJOR_VERSION_3
 
 #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
-#define MBEDTLS_SSL_MAX_MINOR_VERSION           MBEDTLS_SSL_MINOR_VERSION_3
-#else
-#if defined(MBEDTLS_SSL_PROTO_TLS1_1)
-#define MBEDTLS_SSL_MAX_MINOR_VERSION           MBEDTLS_SSL_MINOR_VERSION_2
-#else
-#if defined(MBEDTLS_SSL_PROTO_TLS1)
-#define MBEDTLS_SSL_MAX_MINOR_VERSION           MBEDTLS_SSL_MINOR_VERSION_1
+    #define MBEDTLS_SSL_MAX_MINOR_VERSION           MBEDTLS_SSL_MINOR_VERSION_3
 #else
-#if defined(MBEDTLS_SSL_PROTO_SSL3)
-#define MBEDTLS_SSL_MAX_MINOR_VERSION           MBEDTLS_SSL_MINOR_VERSION_0
-#endif /* MBEDTLS_SSL_PROTO_SSL3   */
-#endif /* MBEDTLS_SSL_PROTO_TLS1   */
-#endif /* MBEDTLS_SSL_PROTO_TLS1_1 */
+    #if defined(MBEDTLS_SSL_PROTO_TLS1_1)
+        #define MBEDTLS_SSL_MAX_MINOR_VERSION           MBEDTLS_SSL_MINOR_VERSION_2
+    #else
+        #if defined(MBEDTLS_SSL_PROTO_TLS1)
+            #define MBEDTLS_SSL_MAX_MINOR_VERSION           MBEDTLS_SSL_MINOR_VERSION_1
+        #else
+            #if defined(MBEDTLS_SSL_PROTO_SSL3)
+                #define MBEDTLS_SSL_MAX_MINOR_VERSION           MBEDTLS_SSL_MINOR_VERSION_0
+            #endif /* MBEDTLS_SSL_PROTO_SSL3   */
+        #endif /* MBEDTLS_SSL_PROTO_TLS1   */
+    #endif /* MBEDTLS_SSL_PROTO_TLS1_1 */
 #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
 
 #define MBEDTLS_SSL_INITIAL_HANDSHAKE           0
@@ -113,37 +97,37 @@
  * enabled.
  */
 #if defined(MBEDTLS_ZLIB_SUPPORT)
-#define MBEDTLS_SSL_COMPRESSION_ADD          1024
+    #define MBEDTLS_SSL_COMPRESSION_ADD          1024
 #else
-#define MBEDTLS_SSL_COMPRESSION_ADD             0
+    #define MBEDTLS_SSL_COMPRESSION_ADD             0
 #endif
 
 #if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_MODE_CBC)
-/* Ciphersuites using HMAC */
-#if defined(MBEDTLS_SHA512_C)
-#define MBEDTLS_SSL_MAC_ADD                 48  /* SHA-384 used for HMAC */
-#elif defined(MBEDTLS_SHA256_C)
-#define MBEDTLS_SSL_MAC_ADD                 32  /* SHA-256 used for HMAC */
+    /* Ciphersuites using HMAC */
+    #if defined(MBEDTLS_SHA512_C)
+        #define MBEDTLS_SSL_MAC_ADD                 48  /* SHA-384 used for HMAC */
+    #elif defined(MBEDTLS_SHA256_C)
+        #define MBEDTLS_SSL_MAC_ADD                 32  /* SHA-256 used for HMAC */
+    #else
+        #define MBEDTLS_SSL_MAC_ADD                 20  /* SHA-1   used for HMAC */
+    #endif
 #else
-#define MBEDTLS_SSL_MAC_ADD                 20  /* SHA-1   used for HMAC */
-#endif
-#else
-/* AEAD ciphersuites: GCM and CCM use a 128 bits tag */
-#define MBEDTLS_SSL_MAC_ADD                 16
+    /* AEAD ciphersuites: GCM and CCM use a 128 bits tag */
+    #define MBEDTLS_SSL_MAC_ADD                 16
 #endif
 
 #if defined(MBEDTLS_CIPHER_MODE_CBC)
-#define MBEDTLS_SSL_PADDING_ADD            256
+    #define MBEDTLS_SSL_PADDING_ADD            256
 #else
-#define MBEDTLS_SSL_PADDING_ADD              0
+    #define MBEDTLS_SSL_PADDING_ADD              0
 #endif
 
 #define MBEDTLS_SSL_BUFFER_LEN  ( MBEDTLS_SSL_MAX_CONTENT_LEN               \
-                        + MBEDTLS_SSL_COMPRESSION_ADD               \
-                        + 29 /* counter + header + IV */    \
-                        + MBEDTLS_SSL_MAC_ADD                       \
-                        + MBEDTLS_SSL_PADDING_ADD                   \
-                        )
+                                  + MBEDTLS_SSL_COMPRESSION_ADD               \
+                                  + 29 /* counter + header + IV */    \
+                                  + MBEDTLS_SSL_MAC_ADD                       \
+                                  + MBEDTLS_SSL_PADDING_ADD                   \
+                                )
 
 /*
  * TLS extension flags (for extensions with outgoing ServerHello content
@@ -160,8 +144,7 @@ extern "C" {
 /*
  * This structure contains the parameters only needed during handshake.
  */
-struct mbedtls_ssl_handshake_params
-{
+struct mbedtls_ssl_handshake_params {
     /*
      * Handshake specific crypto variables
      */
@@ -225,8 +208,8 @@ struct mbedtls_ssl_handshake_params
      */
 #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
     defined(MBEDTLS_SSL_PROTO_TLS1_1)
-       mbedtls_md5_context fin_md5;
-      mbedtls_sha1_context fin_sha1;
+    mbedtls_md5_context fin_md5;
+    mbedtls_sha1_context fin_sha1;
 #endif
 #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
 #if defined(MBEDTLS_SHA256_C)
@@ -240,15 +223,15 @@ struct mbedtls_ssl_handshake_params
     void (*update_checksum)(mbedtls_ssl_context *, const unsigned char *, size_t);
     void (*calc_verify)(mbedtls_ssl_context *, unsigned char *);
     void (*calc_finished)(mbedtls_ssl_context *, unsigned char *, int);
-    int  (*tls_prf)(const unsigned char *, size_t, const char *,
-                    const unsigned char *, size_t,
-                    unsigned char *, size_t);
+    int (*tls_prf)(const unsigned char *, size_t, const char *,
+                   const unsigned char *, size_t,
+                   unsigned char *, size_t);
 
     size_t pmslen;                      /*!<  premaster length        */
 
     unsigned char randbytes[64];        /*!<  random bytes            */
     unsigned char premaster[MBEDTLS_PREMASTER_SIZE];
-                                        /*!<  premaster secret        */
+    /*!<  premaster secret        */
 
     int resume;                         /*!<  session resume indicator*/
     int max_major_ver;                  /*!< max. major version client*/
@@ -267,13 +250,12 @@ struct mbedtls_ssl_handshake_params
  * This structure contains a full set of runtime transform parameters
  * either in negotiation or active.
  */
-struct mbedtls_ssl_transform
-{
+struct mbedtls_ssl_transform {
     /*
      * Session specific crypto layer
      */
     const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
-                                        /*!<  Chosen cipersuite_info  */
+    /*!<  Chosen cipersuite_info  */
     unsigned int keylen;                /*!<  symmetric key length (bytes)  */
     size_t minlen;                      /*!<  min. ciphertext length  */
     size_t ivlen;                       /*!<  IV length               */
@@ -308,8 +290,7 @@ struct mbedtls_ssl_transform
 /*
  * List of certificate + private key pairs
  */
-struct mbedtls_ssl_key_cert
-{
+struct mbedtls_ssl_key_cert {
     mbedtls_x509_crt *cert;                 /*!< cert                       */
     mbedtls_pk_context *key;                /*!< private key                */
     mbedtls_ssl_key_cert *next;             /*!< next key/cert pair         */
@@ -320,8 +301,7 @@ struct mbedtls_ssl_key_cert
 /*
  * List of handshake messages kept around for resending
  */
-struct mbedtls_ssl_flight_item
-{
+struct mbedtls_ssl_flight_item {
     unsigned char *p;       /*!< message, including handshake headers   */
     size_t len;             /*!< length of p                            */
     unsigned char type;     /*!< type of the message: handshake or CCS  */
@@ -336,7 +316,7 @@ struct mbedtls_ssl_flight_item
  *
  * \param transform SSL transform context
  */
-void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform );
+void mbedtls_ssl_transform_free(mbedtls_ssl_transform *transform);
 
 /**
  * \brief           Free referenced items in an SSL handshake context and clear
@@ -344,85 +324,87 @@ void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform );
  *
  * \param handshake SSL handshake context
  */
-void mbedtls_ssl_handshake_free( mbedtls_ssl_handshake_params *handshake );
+void mbedtls_ssl_handshake_free(mbedtls_ssl_handshake_params *handshake);
 
-int mbedtls_ssl_handshake_client_step( mbedtls_ssl_context *ssl );
-int mbedtls_ssl_handshake_server_step( mbedtls_ssl_context *ssl );
-void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl );
+int mbedtls_ssl_handshake_client_step(mbedtls_ssl_context *ssl);
+int mbedtls_ssl_handshake_server_step(mbedtls_ssl_context *ssl);
+void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl);
 
-int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl );
+int mbedtls_ssl_send_fatal_handshake_failure(mbedtls_ssl_context *ssl);
 
-void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl );
-int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl );
+void mbedtls_ssl_reset_checksum(mbedtls_ssl_context *ssl);
+int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl);
 
-int mbedtls_ssl_read_record_layer( mbedtls_ssl_context *ssl );
-int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl );
-int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl );
-void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl );
+int mbedtls_ssl_read_record_layer(mbedtls_ssl_context *ssl);
+int mbedtls_ssl_handle_message_type(mbedtls_ssl_context *ssl);
+int mbedtls_ssl_prepare_handshake_record(mbedtls_ssl_context *ssl);
+void mbedtls_ssl_update_handshake_status(mbedtls_ssl_context *ssl);
 
-int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl );
-int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want );
+int mbedtls_ssl_read_record(mbedtls_ssl_context *ssl);
+int mbedtls_ssl_fetch_input(mbedtls_ssl_context *ssl, size_t nb_want);
 
-int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl );
-int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl );
+int mbedtls_ssl_write_record(mbedtls_ssl_context *ssl);
+int mbedtls_ssl_flush_output(mbedtls_ssl_context *ssl);
 
-int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl );
-int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl );
+int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl);
+int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl);
 
-int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl );
-int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl );
+int mbedtls_ssl_parse_change_cipher_spec(mbedtls_ssl_context *ssl);
+int mbedtls_ssl_write_change_cipher_spec(mbedtls_ssl_context *ssl);
 
-int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl );
-int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl );
+int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl);
+int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl);
 
-void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
-                            const mbedtls_ssl_ciphersuite_t *ciphersuite_info );
+void mbedtls_ssl_optimize_checksum(mbedtls_ssl_context *ssl,
+        const mbedtls_ssl_ciphersuite_t *ciphersuite_info);
 
 #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
-int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex );
+int mbedtls_ssl_psk_derive_premaster(mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex);
 #endif
 
 #if defined(MBEDTLS_PK_C)
-unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk );
-mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig );
+unsigned char mbedtls_ssl_sig_from_pk(mbedtls_pk_context *pk);
+mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig(unsigned char sig);
 #endif
 
-mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash );
-unsigned char mbedtls_ssl_hash_from_md_alg( int md );
-int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md );
+mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash(unsigned char hash);
+unsigned char mbedtls_ssl_hash_from_md_alg(int md);
+int mbedtls_ssl_set_calc_verify_md(mbedtls_ssl_context *ssl, int md);
 
 #if defined(MBEDTLS_ECP_C)
-int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id );
+int mbedtls_ssl_check_curve(const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id);
 #endif
 
 #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
-int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
-                                mbedtls_md_type_t md );
+int mbedtls_ssl_check_sig_hash(const mbedtls_ssl_context *ssl,
+        mbedtls_md_type_t md);
 #endif
 
 #if defined(MBEDTLS_X509_CRT_PARSE_C)
-static inline mbedtls_pk_context *mbedtls_ssl_own_key( mbedtls_ssl_context *ssl )
+static inline mbedtls_pk_context *mbedtls_ssl_own_key(mbedtls_ssl_context *ssl)
 {
     mbedtls_ssl_key_cert *key_cert;
 
-    if( ssl->handshake != NULL && ssl->handshake->key_cert != NULL )
+    if (ssl->handshake != NULL && ssl->handshake->key_cert != NULL) {
         key_cert = ssl->handshake->key_cert;
-    else
+    } else {
         key_cert = ssl->conf->key_cert;
+    }
 
-    return( key_cert == NULL ? NULL : key_cert->key );
+    return (key_cert == NULL ? NULL : key_cert->key);
 }
 
-static inline mbedtls_x509_crt *mbedtls_ssl_own_cert( mbedtls_ssl_context *ssl )
+static inline mbedtls_x509_crt *mbedtls_ssl_own_cert(mbedtls_ssl_context *ssl)
 {
     mbedtls_ssl_key_cert *key_cert;
 
-    if( ssl->handshake != NULL && ssl->handshake->key_cert != NULL )
+    if (ssl->handshake != NULL && ssl->handshake->key_cert != NULL) {
         key_cert = ssl->handshake->key_cert;
-    else
+    } else {
         key_cert = ssl->conf->key_cert;
+    }
 
-    return( key_cert == NULL ? NULL : key_cert->cert );
+    return (key_cert == NULL ? NULL : key_cert->cert);
 }
 
 /*
@@ -434,63 +416,66 @@ static inline mbedtls_x509_crt *mbedtls_ssl_own_cert( mbedtls_ssl_context *ssl )
  *
  * Return 0 if everything is OK, -1 if not.
  */
-int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
-                          const mbedtls_ssl_ciphersuite_t *ciphersuite,
-                          int cert_endpoint,
-                          uint32_t *flags );
+int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert,
+        const mbedtls_ssl_ciphersuite_t *ciphersuite,
+        int cert_endpoint,
+        uint32_t *flags);
 #endif /* MBEDTLS_X509_CRT_PARSE_C */
 
-void mbedtls_ssl_write_version( int major, int minor, int transport,
-                        unsigned char ver[2] );
-void mbedtls_ssl_read_version( int *major, int *minor, int transport,
-                       const unsigned char ver[2] );
+void mbedtls_ssl_write_version(int major, int minor, int transport,
+        unsigned char ver[2]);
+void mbedtls_ssl_read_version(int *major, int *minor, int transport,
+        const unsigned char ver[2]);
 
-static inline size_t mbedtls_ssl_hdr_len( const mbedtls_ssl_context *ssl )
+static inline size_t mbedtls_ssl_hdr_len(const mbedtls_ssl_context *ssl)
 {
 #if defined(MBEDTLS_SSL_PROTO_DTLS)
-    if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
-        return( 13 );
+    if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
+        return (13);
+    }
 #else
     ((void) ssl);
 #endif
-    return( 5 );
+    return (5);
 }
 
-static inline size_t mbedtls_ssl_hs_hdr_len( const mbedtls_ssl_context *ssl )
+static inline size_t mbedtls_ssl_hs_hdr_len(const mbedtls_ssl_context *ssl)
 {
 #if defined(MBEDTLS_SSL_PROTO_DTLS)
-    if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
-        return( 12 );
+    if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
+        return (12);
+    }
 #else
     ((void) ssl);
 #endif
-    return( 4 );
+    return (4);
 }
 
 #if defined(MBEDTLS_SSL_PROTO_DTLS)
-void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl );
-void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl );
-int mbedtls_ssl_resend( mbedtls_ssl_context *ssl );
+void mbedtls_ssl_send_flight_completed(mbedtls_ssl_context *ssl);
+void mbedtls_ssl_recv_flight_completed(mbedtls_ssl_context *ssl);
+int mbedtls_ssl_resend(mbedtls_ssl_context *ssl);
 #endif
 
 /* Visible for testing purposes only */
 #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
-int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl );
-void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl );
+int mbedtls_ssl_dtls_replay_check(mbedtls_ssl_context *ssl);
+void mbedtls_ssl_dtls_replay_update(mbedtls_ssl_context *ssl);
 #endif
 
 /* constant-time buffer comparison */
-static inline int mbedtls_ssl_safer_memcmp( const void *a, const void *b, size_t n )
+static inline int mbedtls_ssl_safer_memcmp(const void *a, const void *b, size_t n)
 {
     size_t i;
     const unsigned char *A = (const unsigned char *) a;
     const unsigned char *B = (const unsigned char *) b;
     unsigned char diff = 0;
 
-    for( i = 0; i < n; i++ )
+    for (i = 0; i < n; i++) {
         diff |= A[i] ^ B[i];
+    }
 
-    return( diff );
+    return (diff);
 }
 
 #ifdef __cplusplus

+ 15 - 33
iotkit-embedded/src/tls/mbedtls-in-iotkit/include/mbedtls/timing.h → iotkit-embedded/external_libs/mbedtls/include/mbedtls/timing.h

@@ -1,32 +1,16 @@
-/**
- * \file timing.h
- *
- * \brief Portable interface to the CPU cycle counter
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
+
+
+
 #ifndef MBEDTLS_TIMING_H
 #define MBEDTLS_TIMING_H
 
 #if !defined(MBEDTLS_CONFIG_FILE)
-#include "config.h"
+    #include "config.h"
 #else
-#include MBEDTLS_CONFIG_FILE
+    #include MBEDTLS_CONFIG_FILE
 #endif
 
 #if !defined(MBEDTLS_TIMING_ALT)
@@ -42,16 +26,14 @@ extern "C" {
 /**
  * \brief          timer structure
  */
-struct mbedtls_timing_hr_time
-{
+struct mbedtls_timing_hr_time {
     unsigned char opaque[32];
 };
 
 /**
  * \brief          Context for mbedtls_timing_set/get_delay()
  */
-typedef struct
-{
+typedef struct {
     struct mbedtls_timing_hr_time   timer;
     uint32_t                        int_ms;
     uint32_t                        fin_ms;
@@ -66,7 +48,7 @@ extern volatile int mbedtls_timing_alarmed;
  *                 In particular, it is known to be unreliable on virtual
  *                 machines.
  */
-unsigned long mbedtls_timing_hardclock( void );
+unsigned long mbedtls_timing_hardclock(void);
 
 /**
  * \brief          Return the elapsed time in milliseconds
@@ -74,7 +56,7 @@ unsigned long mbedtls_timing_hardclock( void );
  * \param val      points to a timer structure
  * \param reset    if set to 1, the timer is restarted
  */
-unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset );
+unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset);
 
 /**
  * \brief          Setup an alarm clock
@@ -85,7 +67,7 @@ unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int
  *                 context, this means one for the whole process, not one per
  *                 thread.
  */
-void mbedtls_set_alarm( int seconds );
+void mbedtls_set_alarm(int seconds);
 
 /**
  * \brief          Set a pair of delays to watch
@@ -97,7 +79,7 @@ void mbedtls_set_alarm( int seconds );
  * \param fin_ms   Second (final) delay in milliseconds.
  *                 Pass 0 to cancel the current delay.
  */
-void mbedtls_timing_set_delay( void *data, uint32_t int_ms, uint32_t fin_ms );
+void mbedtls_timing_set_delay(void *data, uint32_t int_ms, uint32_t fin_ms);
 
 /**
  * \brief          Get the status of delays
@@ -111,7 +93,7 @@ void mbedtls_timing_set_delay( void *data, uint32_t int_ms, uint32_t fin_ms );
  *                  1 if only the intermediate delay is passed,
  *                  2 if the final delay is passed.
  */
-int mbedtls_timing_get_delay( void *data );
+int mbedtls_timing_get_delay(void *data);
 
 #ifdef __cplusplus
 }
@@ -131,7 +113,7 @@ extern "C" {
  *
  * \return         0 if successful, or 1 if a test failed
  */
-int mbedtls_timing_self_test( int verbose );
+int mbedtls_timing_self_test(int verbose);
 #endif
 
 #ifdef __cplusplus

+ 58 - 75
iotkit-embedded/src/tls/mbedtls-in-iotkit/include/mbedtls/x509.h → iotkit-embedded/external_libs/mbedtls/include/mbedtls/x509.h

@@ -1,39 +1,23 @@
-/**
- * \file x509.h
- *
- * \brief X.509 generic defines and structures
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
+
+
+
 #ifndef MBEDTLS_X509_H
 #define MBEDTLS_X509_H
 
 #if !defined(MBEDTLS_CONFIG_FILE)
-#include "config.h"
+    #include "config.h"
 #else
-#include MBEDTLS_CONFIG_FILE
+    #include MBEDTLS_CONFIG_FILE
 #endif
 
 #include "asn1.h"
 #include "pk.h"
 
 #if defined(MBEDTLS_RSA_C)
-#include "rsa.h"
+    #include "rsa.h"
 #endif
 
 /**
@@ -42,15 +26,15 @@
  */
 
 #if !defined(MBEDTLS_X509_MAX_INTERMEDIATE_CA)
-/**
- * Maximum number of intermediate CAs in a verification chain.
- * That is, maximum length of the chain, excluding the end-entity certificate
- * and the trusted root certificate.
- *
- * Set this to a low value to prevent an adversary from making you waste
- * resources verifying an overlong certificate chain.
- */
-#define MBEDTLS_X509_MAX_INTERMEDIATE_CA   8
+    /**
+    * Maximum number of intermediate CAs in a verification chain.
+    * That is, maximum length of the chain, excluding the end-entity certificate
+    * and the trusted root certificate.
+    *
+    * Set this to a low value to prevent an adversary from making you waste
+    * resources verifying an overlong certificate chain.
+    */
+    #define MBEDTLS_X509_MAX_INTERMEDIATE_CA   8
 #endif
 
 /**
@@ -203,8 +187,7 @@ typedef mbedtls_asn1_named_data mbedtls_x509_name;
 typedef mbedtls_asn1_sequence mbedtls_x509_sequence;
 
 /** Container for date and time (precision in seconds). */
-typedef struct mbedtls_x509_time
-{
+typedef struct mbedtls_x509_time {
     int year, mon, day;         /**< Date. */
     int hour, min, sec;         /**< Time. */
 }
@@ -224,7 +207,7 @@ mbedtls_x509_time;
  * \return         The length of the string written (not including the
  *                 terminated nul byte), or a negative error code.
  */
-int mbedtls_x509_dn_gets( char *buf, size_t size, const mbedtls_x509_name *dn );
+int mbedtls_x509_dn_gets(char *buf, size_t size, const mbedtls_x509_name *dn);
 
 /**
  * \brief          Store the certificate serial in printable form into buf;
@@ -237,7 +220,7 @@ int mbedtls_x509_dn_gets( char *buf, size_t size, const mbedtls_x509_name *dn );
  * \return         The length of the string written (not including the
  *                 terminated nul byte), or a negative error code.
  */
-int mbedtls_x509_serial_gets( char *buf, size_t size, const mbedtls_x509_buf *serial );
+int mbedtls_x509_serial_gets(char *buf, size_t size, const mbedtls_x509_buf *serial);
 
 /**
  * \brief          Check a given mbedtls_x509_time against the system time
@@ -251,7 +234,7 @@ int mbedtls_x509_serial_gets( char *buf, size_t size, const mbedtls_x509_buf *se
  * \return         1 if the given time is in the past or an error occured,
  *                 0 otherwise.
  */
-int mbedtls_x509_time_is_past( const mbedtls_x509_time *time );
+int mbedtls_x509_time_is_past(const mbedtls_x509_time *time);
 
 /**
  * \brief          Check a given mbedtls_x509_time against the system time
@@ -265,61 +248,61 @@ int mbedtls_x509_time_is_past( const mbedtls_x509_time *time );
  * \return         1 if the given time is in the future or an error occured,
  *                 0 otherwise.
  */
-int mbedtls_x509_time_is_future( const mbedtls_x509_time *time );
+int mbedtls_x509_time_is_future(const mbedtls_x509_time *time);
 
 /**
  * \brief          Checkup routine
  *
  * \return         0 if successful, or 1 if the test failed
  */
-int mbedtls_x509_self_test( int verbose );
+int mbedtls_x509_self_test(int verbose);
 
 /*
  * Internal module functions. You probably do not want to use these unless you
  * know you do.
  */
-int mbedtls_x509_get_name( unsigned char **p, const unsigned char *end,
-                   mbedtls_x509_name *cur );
-int mbedtls_x509_get_alg_null( unsigned char **p, const unsigned char *end,
-                       mbedtls_x509_buf *alg );
-int mbedtls_x509_get_alg( unsigned char **p, const unsigned char *end,
-                  mbedtls_x509_buf *alg, mbedtls_x509_buf *params );
+int mbedtls_x509_get_name(unsigned char **p, const unsigned char *end,
+                                      mbedtls_x509_name *cur);
+int mbedtls_x509_get_alg_null(unsigned char **p, const unsigned char *end,
+        mbedtls_x509_buf *alg);
+int mbedtls_x509_get_alg(unsigned char **p, const unsigned char *end,
+                                     mbedtls_x509_buf *alg, mbedtls_x509_buf *params);
 #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
-int mbedtls_x509_get_rsassa_pss_params( const mbedtls_x509_buf *params,
-                                mbedtls_md_type_t *md_alg, mbedtls_md_type_t *mgf_md,
-                                int *salt_len );
+int mbedtls_x509_get_rsassa_pss_params(const mbedtls_x509_buf *params,
+        mbedtls_md_type_t *md_alg, mbedtls_md_type_t *mgf_md,
+        int *salt_len);
 #endif
-int mbedtls_x509_get_sig( unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig );
-int mbedtls_x509_get_sig_alg( const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params,
-                      mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg,
-                      void **sig_opts );
-int mbedtls_x509_get_time( unsigned char **p, const unsigned char *end,
-                   mbedtls_x509_time *time );
-int mbedtls_x509_get_serial( unsigned char **p, const unsigned char *end,
-                     mbedtls_x509_buf *serial );
-int mbedtls_x509_get_ext( unsigned char **p, const unsigned char *end,
-                  mbedtls_x509_buf *ext, int tag );
-int mbedtls_x509_sig_alg_gets( char *buf, size_t size, const mbedtls_x509_buf *sig_oid,
-                       mbedtls_pk_type_t pk_alg, mbedtls_md_type_t md_alg,
-                       const void *sig_opts );
-int mbedtls_x509_key_size_helper( char *buf, size_t buf_size, const char *name );
-int mbedtls_x509_string_to_names( mbedtls_asn1_named_data **head, const char *name );
-int mbedtls_x509_set_extension( mbedtls_asn1_named_data **head, const char *oid, size_t oid_len,
-                        int critical, const unsigned char *val,
-                        size_t val_len );
-int mbedtls_x509_write_extensions( unsigned char **p, unsigned char *start,
-                           mbedtls_asn1_named_data *first );
-int mbedtls_x509_write_names( unsigned char **p, unsigned char *start,
-                      mbedtls_asn1_named_data *first );
-int mbedtls_x509_write_sig( unsigned char **p, unsigned char *start,
-                    const char *oid, size_t oid_len,
-                    unsigned char *sig, size_t size );
+int mbedtls_x509_get_sig(unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig);
+int mbedtls_x509_get_sig_alg(const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params,
+        mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg,
+        void **sig_opts);
+int mbedtls_x509_get_time(unsigned char **p, const unsigned char *end,
+                                      mbedtls_x509_time *time);
+int mbedtls_x509_get_serial(unsigned char **p, const unsigned char *end,
+                                        mbedtls_x509_buf *serial);
+int mbedtls_x509_get_ext(unsigned char **p, const unsigned char *end,
+                                     mbedtls_x509_buf *ext, int tag);
+int mbedtls_x509_sig_alg_gets(char *buf, size_t size, const mbedtls_x509_buf *sig_oid,
+        mbedtls_pk_type_t pk_alg, mbedtls_md_type_t md_alg,
+        const void *sig_opts);
+int mbedtls_x509_key_size_helper(char *buf, size_t buf_size, const char *name);
+int mbedtls_x509_string_to_names(mbedtls_asn1_named_data **head, const char *name);
+int mbedtls_x509_set_extension(mbedtls_asn1_named_data **head, const char *oid, size_t oid_len,
+        int critical, const unsigned char *val,
+        size_t val_len);
+int mbedtls_x509_write_extensions(unsigned char **p, unsigned char *start,
+        mbedtls_asn1_named_data *first);
+int mbedtls_x509_write_names(unsigned char **p, unsigned char *start,
+        mbedtls_asn1_named_data *first);
+int mbedtls_x509_write_sig(unsigned char **p, unsigned char *start,
+                                       const char *oid, size_t oid_len,
+                                       unsigned char *sig, size_t size);
 
 #define MBEDTLS_X509_SAFE_SNPRINTF                          \
     do {                                                    \
         if( ret < 0 || (size_t) ret >= n )                  \
             return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );    \
-                                                            \
+        \
         n -= (size_t) ret;                                  \
         p += (size_t) ret;                                  \
     } while( 0 )

+ 21 - 37
iotkit-embedded/src/tls/mbedtls-in-iotkit/include/mbedtls/x509_crl.h → iotkit-embedded/external_libs/mbedtls/include/mbedtls/x509_crl.h

@@ -1,32 +1,16 @@
-/**
- * \file x509_crl.h
- *
- * \brief X.509 certificate revocation list parsing
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
+
+
+
 #ifndef MBEDTLS_X509_CRL_H
 #define MBEDTLS_X509_CRL_H
 
 #if !defined(MBEDTLS_CONFIG_FILE)
-#include "config.h"
+    #include "config.h"
 #else
-#include MBEDTLS_CONFIG_FILE
+    #include MBEDTLS_CONFIG_FILE
 #endif
 
 #include "x509.h"
@@ -48,8 +32,7 @@ extern "C" {
  * Certificate revocation list entry.
  * Contains the CA-specific serial numbers and revocation dates.
  */
-typedef struct mbedtls_x509_crl_entry
-{
+typedef struct mbedtls_x509_crl_entry {
     mbedtls_x509_buf raw;
 
     mbedtls_x509_buf serial;
@@ -66,8 +49,7 @@ mbedtls_x509_crl_entry;
  * Certificate revocation list structure.
  * Every CRL may have multiple entries.
  */
-typedef struct mbedtls_x509_crl
-{
+typedef struct mbedtls_x509_crl {
     mbedtls_x509_buf raw;           /**< The raw certificate data (DER). */
     mbedtls_x509_buf tbs;           /**< The raw certificate body (DER). The part that is To Be Signed. */
 
@@ -87,8 +69,10 @@ typedef struct mbedtls_x509_crl
 
     mbedtls_x509_buf sig_oid2;
     mbedtls_x509_buf sig;
-    mbedtls_md_type_t sig_md;           /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */
-    mbedtls_pk_type_t sig_pk;           /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */
+    mbedtls_md_type_t
+    sig_md;           /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */
+    mbedtls_pk_type_t
+    sig_pk;           /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */
     void *sig_opts;             /**< Signature options to be passed to mbedtls_pk_verify_ext(), e.g. for RSASSA-PSS */
 
     struct mbedtls_x509_crl *next;
@@ -105,8 +89,8 @@ mbedtls_x509_crl;
  *
  * \return         0 if successful, or a specific X509 or PEM error code
  */
-int mbedtls_x509_crl_parse_der( mbedtls_x509_crl *chain,
-                        const unsigned char *buf, size_t buflen );
+int mbedtls_x509_crl_parse_der(mbedtls_x509_crl *chain,
+        const unsigned char *buf, size_t buflen);
 /**
  * \brief          Parse one or more CRLs and append them to the chained list
  *
@@ -119,7 +103,7 @@ int mbedtls_x509_crl_parse_der( mbedtls_x509_crl *chain,
  *
  * \return         0 if successful, or a specific X509 or PEM error code
  */
-int mbedtls_x509_crl_parse( mbedtls_x509_crl *chain, const unsigned char *buf, size_t buflen );
+int mbedtls_x509_crl_parse(mbedtls_x509_crl *chain, const unsigned char *buf, size_t buflen);
 
 #if defined(MBEDTLS_FS_IO)
 /**
@@ -132,7 +116,7 @@ int mbedtls_x509_crl_parse( mbedtls_x509_crl *chain, const unsigned char *buf, s
  *
  * \return         0 if successful, or a specific X509 or PEM error code
  */
-int mbedtls_x509_crl_parse_file( mbedtls_x509_crl *chain, const char *path );
+int mbedtls_x509_crl_parse_file(mbedtls_x509_crl *chain, const char *path);
 #endif /* MBEDTLS_FS_IO */
 
 /**
@@ -146,22 +130,22 @@ int mbedtls_x509_crl_parse_file( mbedtls_x509_crl *chain, const char *path );
  * \return         The length of the string written (not including the
  *                 terminated nul byte), or a negative error code.
  */
-int mbedtls_x509_crl_info( char *buf, size_t size, const char *prefix,
-                   const mbedtls_x509_crl *crl );
+int mbedtls_x509_crl_info(char *buf, size_t size, const char *prefix,
+                                      const mbedtls_x509_crl *crl);
 
 /**
  * \brief          Initialize a CRL (chain)
  *
  * \param crl      CRL chain to initialize
  */
-void mbedtls_x509_crl_init( mbedtls_x509_crl *crl );
+void mbedtls_x509_crl_init(mbedtls_x509_crl *crl);
 
 /**
  * \brief          Unallocate all CRL data
  *
  * \param crl      CRL chain to free
  */
-void mbedtls_x509_crl_free( mbedtls_x509_crl *crl );
+void mbedtls_x509_crl_free(mbedtls_x509_crl *crl);
 
 /* \} name */
 /* \} addtogroup x509_module */

+ 75 - 92
iotkit-embedded/src/tls/mbedtls-in-iotkit/include/mbedtls/x509_crt.h → iotkit-embedded/external_libs/mbedtls/include/mbedtls/x509_crt.h

@@ -1,32 +1,16 @@
-/**
- * \file x509_crt.h
- *
- * \brief X.509 certificate parsing and writing
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
+
+
+
 #ifndef MBEDTLS_X509_CRT_H
 #define MBEDTLS_X509_CRT_H
 
 #if !defined(MBEDTLS_CONFIG_FILE)
-#include "config.h"
+    #include "config.h"
 #else
-#include MBEDTLS_CONFIG_FILE
+    #include MBEDTLS_CONFIG_FILE
 #endif
 
 #include "x509.h"
@@ -49,8 +33,7 @@ extern "C" {
 /**
  * Container for an X.509 certificate. The certificate may be chained.
  */
-typedef struct mbedtls_x509_crt
-{
+typedef struct mbedtls_x509_crt {
     mbedtls_x509_buf raw;               /**< The raw certificate data (DER). */
     mbedtls_x509_buf tbs;               /**< The raw certificate body (DER). The part that is To Be Signed. */
 
@@ -85,8 +68,10 @@ typedef struct mbedtls_x509_crt
     unsigned char ns_cert_type; /**< Optional Netscape certificate type extension value: See the values in x509.h */
 
     mbedtls_x509_buf sig;               /**< Signature: hash of the tbs part signed with the private key. */
-    mbedtls_md_type_t sig_md;           /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */
-    mbedtls_pk_type_t sig_pk;           /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */
+    mbedtls_md_type_t
+    sig_md;           /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */
+    mbedtls_pk_type_t
+    sig_pk;           /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */
     void *sig_opts;             /**< Signature options to be passed to mbedtls_pk_verify_ext(), e.g. for RSASSA-PSS */
 
     struct mbedtls_x509_crt *next;     /**< Next certificate in the CA-chain. */
@@ -104,8 +89,7 @@ mbedtls_x509_crt;
  *
  * All lists are bitfields, built by ORing flags from MBEDTLS_X509_ID_FLAG().
  */
-typedef struct
-{
+typedef struct {
     uint32_t allowed_mds;       /**< MDs for signatures         */
     uint32_t allowed_pks;       /**< PK algs for signatures     */
     uint32_t allowed_curves;    /**< Elliptic curves for ECDSA  */
@@ -127,8 +111,7 @@ mbedtls_x509_crt_profile;
 /**
  * Container for writing a certificate (CRT)
  */
-typedef struct mbedtls_x509write_cert
-{
+typedef struct mbedtls_x509write_cert {
     int version;
     mbedtls_mpi serial;
     mbedtls_pk_context *subject_key;
@@ -170,8 +153,8 @@ extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb;
  *
  * \return         0 if successful, or a specific X509 or PEM error code
  */
-int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain, const unsigned char *buf,
-                        size_t buflen );
+int mbedtls_x509_crt_parse_der(mbedtls_x509_crt *chain, const unsigned char *buf,
+        size_t buflen);
 
 /**
  * \brief          Parse one or more certificates and add them
@@ -188,7 +171,7 @@ int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain, const unsigned char *bu
  * \return         0 if all certificates parsed successfully, a positive number
  *                 if partly successful or a specific X509 or PEM error code
  */
-int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen );
+int mbedtls_x509_crt_parse(mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen);
 
 #if defined(MBEDTLS_FS_IO)
 /**
@@ -204,7 +187,7 @@ int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, s
  * \return         0 if all certificates parsed successfully, a positive number
  *                 if partly successful or a specific X509 or PEM error code
  */
-int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path );
+int mbedtls_x509_crt_parse_file(mbedtls_x509_crt *chain, const char *path);
 
 /**
  * \brief          Load one or more certificate files from a path and add them
@@ -219,7 +202,7 @@ int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path );
  * \return         0 if all certificates parsed successfully, a positive number
  *                 if partly successful or a specific X509 or PEM error code
  */
-int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path );
+int mbedtls_x509_crt_parse_path(mbedtls_x509_crt *chain, const char *path);
 #endif /* MBEDTLS_FS_IO */
 
 /**
@@ -234,8 +217,8 @@ int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path );
  * \return         The length of the string written (not including the
  *                 terminated nul byte), or a negative error code.
  */
-int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
-                   const mbedtls_x509_crt *crt );
+int mbedtls_x509_crt_info(char *buf, size_t size, const char *prefix,
+                                      const mbedtls_x509_crt *crt);
 
 /**
  * \brief          Returns an informational string about the
@@ -249,8 +232,8 @@ int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
  * \return         The length of the string written (not including the
  *                 terminated nul byte), or a negative error code.
  */
-int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
-                          uint32_t flags );
+int mbedtls_x509_crt_verify_info(char *buf, size_t size, const char *prefix,
+        uint32_t flags);
 
 /**
  * \brief          Verify the certificate signature
@@ -296,12 +279,12 @@ int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
  *                 or another error in case of a fatal error encountered
  *                 during the verification process.
  */
-int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
-                     mbedtls_x509_crt *trust_ca,
-                     mbedtls_x509_crl *ca_crl,
-                     const char *cn, uint32_t *flags,
-                     int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
-                     void *p_vrfy );
+int mbedtls_x509_crt_verify(mbedtls_x509_crt *crt,
+                                        mbedtls_x509_crt *trust_ca,
+                                        mbedtls_x509_crl *ca_crl,
+                                        const char *cn, uint32_t *flags,
+                                        int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
+                                        void *p_vrfy);
 
 /**
  * \brief          Verify the certificate signature according to profile
@@ -330,13 +313,13 @@ int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
  *                 or another error in case of a fatal error encountered
  *                 during the verification process.
  */
-int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
-                     mbedtls_x509_crt *trust_ca,
-                     mbedtls_x509_crl *ca_crl,
-                     const mbedtls_x509_crt_profile *profile,
-                     const char *cn, uint32_t *flags,
-                     int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
-                     void *p_vrfy );
+int mbedtls_x509_crt_verify_with_profile(mbedtls_x509_crt *crt,
+        mbedtls_x509_crt *trust_ca,
+        mbedtls_x509_crl *ca_crl,
+        const mbedtls_x509_crt_profile *profile,
+        const char *cn, uint32_t *flags,
+        int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
+        void *p_vrfy);
 
 #if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
 /**
@@ -360,8 +343,8 @@ int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
  *                 (intermediate) CAs the keyUsage extension is automatically
  *                 checked by \c mbedtls_x509_crt_verify().
  */
-int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
-                                      unsigned int usage );
+int mbedtls_x509_crt_check_key_usage(const mbedtls_x509_crt *crt,
+        unsigned int usage);
 #endif /* MBEDTLS_X509_CHECK_KEY_USAGE) */
 
 #if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
@@ -377,9 +360,9 @@ int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
  *
  * \note           Usually only makes sense on leaf certificates.
  */
-int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
-                                       const char *usage_oid,
-                                       size_t usage_len );
+int mbedtls_x509_crt_check_extended_key_usage(const mbedtls_x509_crt *crt,
+        const char *usage_oid,
+        size_t usage_len);
 #endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) */
 
 #if defined(MBEDTLS_X509_CRL_PARSE_C)
@@ -392,7 +375,7 @@ int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
  * \return         1 if the certificate is revoked, 0 otherwise
  *
  */
-int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl );
+int mbedtls_x509_crt_is_revoked(const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl);
 #endif /* MBEDTLS_X509_CRL_PARSE_C */
 
 /**
@@ -400,14 +383,14 @@ int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509
  *
  * \param crt      Certificate chain to initialize
  */
-void mbedtls_x509_crt_init( mbedtls_x509_crt *crt );
+void mbedtls_x509_crt_init(mbedtls_x509_crt *crt);
 
 /**
  * \brief          Unallocate all certificate data
  *
  * \param crt      Certificate chain to free
  */
-void mbedtls_x509_crt_free( mbedtls_x509_crt *crt );
+void mbedtls_x509_crt_free(mbedtls_x509_crt *crt);
 #endif /* MBEDTLS_X509_CRT_PARSE_C */
 
 /* \} name */
@@ -419,7 +402,7 @@ void mbedtls_x509_crt_free( mbedtls_x509_crt *crt );
  *
  * \param ctx       CRT context to initialize
  */
-void mbedtls_x509write_crt_init( mbedtls_x509write_cert *ctx );
+void mbedtls_x509write_crt_init(mbedtls_x509write_cert *ctx);
 
 /**
  * \brief           Set the verion for a Certificate
@@ -429,7 +412,7 @@ void mbedtls_x509write_crt_init( mbedtls_x509write_cert *ctx );
  * \param version   version to set (MBEDTLS_X509_CRT_VERSION_1, MBEDTLS_X509_CRT_VERSION_2 or
  *                                  MBEDTLS_X509_CRT_VERSION_3)
  */
-void mbedtls_x509write_crt_set_version( mbedtls_x509write_cert *ctx, int version );
+void mbedtls_x509write_crt_set_version(mbedtls_x509write_cert *ctx, int version);
 
 /**
  * \brief           Set the serial number for a Certificate.
@@ -439,7 +422,7 @@ void mbedtls_x509write_crt_set_version( mbedtls_x509write_cert *ctx, int version
  *
  * \return          0 if successful
  */
-int mbedtls_x509write_crt_set_serial( mbedtls_x509write_cert *ctx, const mbedtls_mpi *serial );
+int mbedtls_x509write_crt_set_serial(mbedtls_x509write_cert *ctx, const mbedtls_mpi *serial);
 
 /**
  * \brief           Set the validity period for a Certificate
@@ -455,8 +438,8 @@ int mbedtls_x509write_crt_set_serial( mbedtls_x509write_cert *ctx, const mbedtls
  * \return          0 if timestamp was parsed successfully, or
  *                  a specific error code
  */
-int mbedtls_x509write_crt_set_validity( mbedtls_x509write_cert *ctx, const char *not_before,
-                                const char *not_after );
+int mbedtls_x509write_crt_set_validity(mbedtls_x509write_cert *ctx, const char *not_before,
+        const char *not_after);
 
 /**
  * \brief           Set the issuer name for a Certificate
@@ -470,8 +453,8 @@ int mbedtls_x509write_crt_set_validity( mbedtls_x509write_cert *ctx, const char
  * \return          0 if issuer name was parsed successfully, or
  *                  a specific error code
  */
-int mbedtls_x509write_crt_set_issuer_name( mbedtls_x509write_cert *ctx,
-                                   const char *issuer_name );
+int mbedtls_x509write_crt_set_issuer_name(mbedtls_x509write_cert *ctx,
+        const char *issuer_name);
 
 /**
  * \brief           Set the subject name for a Certificate
@@ -485,8 +468,8 @@ int mbedtls_x509write_crt_set_issuer_name( mbedtls_x509write_cert *ctx,
  * \return          0 if subject name was parsed successfully, or
  *                  a specific error code
  */
-int mbedtls_x509write_crt_set_subject_name( mbedtls_x509write_cert *ctx,
-                                    const char *subject_name );
+int mbedtls_x509write_crt_set_subject_name(mbedtls_x509write_cert *ctx,
+        const char *subject_name);
 
 /**
  * \brief           Set the subject public key for the certificate
@@ -494,7 +477,7 @@ int mbedtls_x509write_crt_set_subject_name( mbedtls_x509write_cert *ctx,
  * \param ctx       CRT context to use
  * \param key       public key to include
  */
-void mbedtls_x509write_crt_set_subject_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
+void mbedtls_x509write_crt_set_subject_key(mbedtls_x509write_cert *ctx, mbedtls_pk_context *key);
 
 /**
  * \brief           Set the issuer key used for signing the certificate
@@ -502,7 +485,7 @@ void mbedtls_x509write_crt_set_subject_key( mbedtls_x509write_cert *ctx, mbedtls
  * \param ctx       CRT context to use
  * \param key       private key to sign with
  */
-void mbedtls_x509write_crt_set_issuer_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
+void mbedtls_x509write_crt_set_issuer_key(mbedtls_x509write_cert *ctx, mbedtls_pk_context *key);
 
 /**
  * \brief           Set the MD algorithm to use for the signature
@@ -511,7 +494,7 @@ void mbedtls_x509write_crt_set_issuer_key( mbedtls_x509write_cert *ctx, mbedtls_
  * \param ctx       CRT context to use
  * \param md_alg    MD algorithm to use
  */
-void mbedtls_x509write_crt_set_md_alg( mbedtls_x509write_cert *ctx, mbedtls_md_type_t md_alg );
+void mbedtls_x509write_crt_set_md_alg(mbedtls_x509write_cert *ctx, mbedtls_md_type_t md_alg);
 
 /**
  * \brief           Generic function to add to or replace an extension in the
@@ -526,10 +509,10 @@ void mbedtls_x509write_crt_set_md_alg( mbedtls_x509write_cert *ctx, mbedtls_md_t
  *
  * \return          0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
  */
-int mbedtls_x509write_crt_set_extension( mbedtls_x509write_cert *ctx,
-                                 const char *oid, size_t oid_len,
-                                 int critical,
-                                 const unsigned char *val, size_t val_len );
+int mbedtls_x509write_crt_set_extension(mbedtls_x509write_cert *ctx,
+        const char *oid, size_t oid_len,
+        int critical,
+        const unsigned char *val, size_t val_len);
 
 /**
  * \brief           Set the basicConstraints extension for a CRT
@@ -542,8 +525,8 @@ int mbedtls_x509write_crt_set_extension( mbedtls_x509write_cert *ctx,
  *
  * \return          0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
  */
-int mbedtls_x509write_crt_set_basic_constraints( mbedtls_x509write_cert *ctx,
-                                         int is_ca, int max_pathlen );
+int mbedtls_x509write_crt_set_basic_constraints(mbedtls_x509write_cert *ctx,
+        int is_ca, int max_pathlen);
 
 #if defined(MBEDTLS_SHA1_C)
 /**
@@ -555,7 +538,7 @@ int mbedtls_x509write_crt_set_basic_constraints( mbedtls_x509write_cert *ctx,
  *
  * \return          0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
  */
-int mbedtls_x509write_crt_set_subject_key_identifier( mbedtls_x509write_cert *ctx );
+int mbedtls_x509write_crt_set_subject_key_identifier(mbedtls_x509write_cert *ctx);
 
 /**
  * \brief           Set the authorityKeyIdentifier extension for a CRT
@@ -566,7 +549,7 @@ int mbedtls_x509write_crt_set_subject_key_identifier( mbedtls_x509write_cert *ct
  *
  * \return          0 if successful, or a MBEDTLS_ERR_X509_ALLOC_FAILED
  */
-int mbedtls_x509write_crt_set_authority_key_identifier( mbedtls_x509write_cert *ctx );
+int mbedtls_x509write_crt_set_authority_key_identifier(mbedtls_x509write_cert *ctx);
 #endif /* MBEDTLS_SHA1_C */
 
 /**
@@ -578,8 +561,8 @@ int mbedtls_x509write_crt_set_authority_key_identifier( mbedtls_x509write_cert *
  *
  * \return          0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
  */
-int mbedtls_x509write_crt_set_key_usage( mbedtls_x509write_cert *ctx,
-                                         unsigned int key_usage );
+int mbedtls_x509write_crt_set_key_usage(mbedtls_x509write_cert *ctx,
+        unsigned int key_usage);
 
 /**
  * \brief           Set the Netscape Cert Type flags
@@ -590,15 +573,15 @@ int mbedtls_x509write_crt_set_key_usage( mbedtls_x509write_cert *ctx,
  *
  * \return          0 if successful, or MBEDTLS_ERR_X509_ALLOC_FAILED
  */
-int mbedtls_x509write_crt_set_ns_cert_type( mbedtls_x509write_cert *ctx,
-                                    unsigned char ns_cert_type );
+int mbedtls_x509write_crt_set_ns_cert_type(mbedtls_x509write_cert *ctx,
+        unsigned char ns_cert_type);
 
 /**
  * \brief           Free the contents of a CRT write context
  *
  * \param ctx       CRT context to free
  */
-void mbedtls_x509write_crt_free( mbedtls_x509write_cert *ctx );
+void mbedtls_x509write_crt_free(mbedtls_x509write_cert *ctx);
 
 /**
  * \brief           Write a built up certificate to a X509 DER structure
@@ -620,9 +603,9 @@ void mbedtls_x509write_crt_free( mbedtls_x509write_cert *ctx );
  *                  for countermeasures against timing attacks).
  *                  ECDSA signatures always require a non-NULL f_rng.
  */
-int mbedtls_x509write_crt_der( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
-                       int (*f_rng)(void *, unsigned char *, size_t),
-                       void *p_rng );
+int mbedtls_x509write_crt_der(mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
+        int (*f_rng)(void *, unsigned char *, size_t),
+        void *p_rng);
 
 #if defined(MBEDTLS_PEM_WRITE_C)
 /**
@@ -641,9 +624,9 @@ int mbedtls_x509write_crt_der( mbedtls_x509write_cert *ctx, unsigned char *buf,
  *                  for countermeasures against timing attacks).
  *                  ECDSA signatures always require a non-NULL f_rng.
  */
-int mbedtls_x509write_crt_pem( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
-                       int (*f_rng)(void *, unsigned char *, size_t),
-                       void *p_rng );
+int mbedtls_x509write_crt_pem(mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
+        int (*f_rng)(void *, unsigned char *, size_t),
+        void *p_rng);
 #endif /* MBEDTLS_PEM_WRITE_C */
 #endif /* MBEDTLS_X509_CRT_WRITE_C */
 

+ 6 - 0
iotkit-embedded/external_libs/mbedtls/iot.mk

@@ -0,0 +1,6 @@
+LIBA_TARGET := libiot_tls.a
+
+HDR_REFS    += src/infra
+
+CFLAGS := $(filter-out -Wdeclaration-after-statement,$(CFLAGS))
+CFLAGS := $(filter-out -ansi,$(CFLAGS))

+ 0 - 0
iotkit-embedded/src/tls/mbedtls-in-iotkit/library/.gitignore → iotkit-embedded/external_libs/mbedtls/library/.gitignore


+ 0 - 0
iotkit-embedded/src/tls/mbedtls-in-iotkit/library/Makefile → iotkit-embedded/external_libs/mbedtls/library/Makefile


+ 6 - 19
iotkit-embedded/src/tls/mbedtls-in-iotkit/library/aes.c → iotkit-embedded/external_libs/mbedtls/library/aes.c

@@ -1,23 +1,9 @@
 /*
- *  FIPS-197 compliant AES implementation
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
+
+
+
 /*
  *  The AES block cipher was designed by Vincent Rijmen and Joan Daemen.
  *
@@ -48,7 +34,8 @@
 #include "mbedtls/platform.h"
 #else
 #include <stdio.h>
-#define mbedtls_printf printf
+#include "mbedtls/debug.h"
+#define mbedtls_printf tls_info
 #endif /* MBEDTLS_PLATFORM_C */
 #endif /* MBEDTLS_SELF_TEST */
 

+ 4 - 18
iotkit-embedded/src/tls/mbedtls-in-iotkit/library/asn1parse.c → iotkit-embedded/external_libs/mbedtls/library/asn1parse.c

@@ -1,24 +1,10 @@
 /*
- *  Generic ASN.1 parsing
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
 
+
+
+
 #if !defined(MBEDTLS_CONFIG_FILE)
 #include "mbedtls/config.h"
 #else

+ 7 - 19
iotkit-embedded/src/tls/mbedtls-in-iotkit/library/base64.c → iotkit-embedded/external_libs/mbedtls/library/base64.c

@@ -1,24 +1,10 @@
 /*
- *  RFC 1521 base64 encoding/decoding
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
 
+
+
+
 #if !defined(MBEDTLS_CONFIG_FILE)
 #include "mbedtls/config.h"
 #else
@@ -37,7 +23,9 @@
 #include "mbedtls/platform.h"
 #else
 #include <stdio.h>
-#define mbedtls_printf printf
+#include "mbedtls/debug.h"
+
+#define mbedtls_printf tls_info
 #endif /* MBEDTLS_PLATFORM_C */
 #endif /* MBEDTLS_SELF_TEST */
 

+ 8 - 19
iotkit-embedded/src/tls/mbedtls-in-iotkit/library/bignum.c → iotkit-embedded/external_libs/mbedtls/library/bignum.c

@@ -1,24 +1,10 @@
 /*
- *  Multi-precision integer library
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
 
+
+
+
 /*
  *  The following sources were referenced in the design of this Multi-precision
  *  Integer library:
@@ -53,7 +39,9 @@
 #else
 #include <stdio.h>
 #include <stdlib.h>
-#define mbedtls_printf     printf
+#include "mbedtls/debug.h"
+
+#define mbedtls_printf     tls_info
 #define mbedtls_calloc    calloc
 #define mbedtls_free       free
 #endif
@@ -292,6 +280,7 @@ int mbedtls_mpi_lset( mbedtls_mpi *X, mbedtls_mpi_sint z )
     int ret;
 
     MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, 1 ) );
+    MBEDTLS_MPI_CHK( (X->p == NULL) );
     memset( X->p, 0, X->n * ciL );
 
     X->p[0] = ( z < 0 ) ? -z : z;

+ 5 - 28
iotkit-embedded/src/tls/mbedtls-in-iotkit/library/cipher.c → iotkit-embedded/external_libs/mbedtls/library/cipher.c

@@ -1,28 +1,10 @@
-/**
- * \file cipher.c
- *
- * \brief Generic cipher wrapper for mbed TLS
- *
- * \author Adriaan de Jong <dejong@fox-it.com>
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
 
+
+
+
 #if !defined(MBEDTLS_CONFIG_FILE)
 #include "mbedtls/config.h"
 #else
@@ -367,11 +349,6 @@ int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *i
          */
         if( 0 != ilen )
         {
-            if( 0 == block_size )
-            {
-                return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
-            }
-
             copy_len = ilen % block_size;
             if( copy_len == 0 && ctx->operation == MBEDTLS_DECRYPT )
                 copy_len = block_size;

+ 5 - 23
iotkit-embedded/src/tls/mbedtls-in-iotkit/library/cipher_wrap.c → iotkit-embedded/external_libs/mbedtls/library/cipher_wrap.c

@@ -1,28 +1,10 @@
-/**
- * \file cipher_wrap.c
- *
- * \brief Generic cipher wrapper for mbed TLS
- *
- * \author Adriaan de Jong <dejong@fox-it.com>
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
 
+
+
+
 #if !defined(MBEDTLS_CONFIG_FILE)
 #include "mbedtls/config.h"
 #else

+ 9 - 20
iotkit-embedded/src/tls/mbedtls-in-iotkit/library/ctr_drbg.c → iotkit-embedded/external_libs/mbedtls/library/ctr_drbg.c

@@ -1,23 +1,9 @@
 /*
- *  CTR_DRBG implementation based on AES-256 (NIST SP 800-90)
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
+
+
+
 /*
  *  The NIST SP 800-90 DRBGs are described in the following publucation.
  *
@@ -45,7 +31,9 @@
 #include "mbedtls/platform.h"
 #else
 #include <stdio.h>
-#define mbedtls_printf printf
+#include "mbedtls/debug.h"
+
+#define mbedtls_printf tls_info
 #endif /* MBEDTLS_PLATFORM_C */
 #endif /* MBEDTLS_SELF_TEST */
 
@@ -291,7 +279,8 @@ int mbedtls_ctr_drbg_reseed( mbedtls_ctr_drbg_context *ctx,
     size_t seedlen = 0;
 
     if( ctx->entropy_len > MBEDTLS_CTR_DRBG_MAX_SEED_INPUT ||
-        len > MBEDTLS_CTR_DRBG_MAX_SEED_INPUT - ctx->entropy_len )
+        len > MBEDTLS_CTR_DRBG_MAX_SEED_INPUT - ctx->entropy_len ||
+        len < 0)
         return( MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG );
 
     memset( seed, 0, MBEDTLS_CTR_DRBG_MAX_SEED_INPUT );

+ 352 - 0
iotkit-embedded/external_libs/mbedtls/library/debug.c

@@ -0,0 +1,352 @@
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
+ */
+
+#if !defined(MBEDTLS_CONFIG_FILE)
+    #include "mbedtls/config.h"
+#else
+    #include MBEDTLS_CONFIG_FILE
+#endif
+
+#if defined(MBEDTLS_DEBUG_C)
+
+#if defined(MBEDTLS_PLATFORM_C)
+    #include "mbedtls/platform.h"
+#else
+    #include <stdlib.h>
+    #define mbedtls_calloc      calloc
+    #define mbedtls_free        free
+    #define mbedtls_time_t      time_t
+    #define mbedtls_snprintf    snprintf
+#endif
+
+#include "mbedtls/debug.h"
+
+#include <stdarg.h>
+#include <stdio.h>
+#include <string.h>
+
+#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
+    !defined(inline) && !defined(__cplusplus)
+    #define inline __inline
+#endif
+
+#define DEBUG_BUF_SIZE      512
+
+static int debug_threshold = 0;
+
+void mbedtls_debug_set_threshold(int threshold)
+{
+    debug_threshold = threshold;
+}
+
+/*
+ * All calls to f_dbg must be made via this function
+ */
+static inline void debug_send_line(const mbedtls_ssl_context *ssl, int level,
+                                   const char *file, int line,
+                                   const char *str)
+{
+    /*
+     * If in a threaded environment, we need a thread identifier.
+     * Since there is no portable way to get one, use the address of the ssl
+     * context instead, as it shouldn't be shared between threads.
+     */
+#if defined(MBEDTLS_THREADING_C)
+    char idstr[20 + DEBUG_BUF_SIZE]; /* 0x + 16 nibbles + ': ' */
+    mbedtls_snprintf(idstr, sizeof(idstr), "%p: %s", (void *)ssl, str);
+    ssl->conf->f_dbg(ssl->conf->p_dbg, level, file, line, idstr);
+#else
+    ssl->conf->f_dbg(ssl->conf->p_dbg, level, file, line, str);
+#endif
+}
+
+void mbedtls_debug_print_msg(const mbedtls_ssl_context *ssl, int level,
+                             const char *file, int line,
+                             const char *format, ...)
+{
+    va_list argp;
+    char str[DEBUG_BUF_SIZE];
+    int ret;
+
+    if (NULL == ssl || NULL == ssl->conf || NULL == ssl->conf->f_dbg || level > debug_threshold) {
+        return;
+    }
+
+    va_start(argp, format);
+#if defined(_WIN32)
+#if defined(_TRUNCATE)
+    ret = _vsnprintf_s(str, DEBUG_BUF_SIZE, _TRUNCATE, format, argp);
+#else
+    ret = _vsnprintf(str, DEBUG_BUF_SIZE, format, argp);
+    if (ret < 0 || (size_t) ret == DEBUG_BUF_SIZE) {
+        str[DEBUG_BUF_SIZE - 1] = '\0';
+        ret = -1;
+    }
+#endif
+#else
+    ret = vsnprintf(str, DEBUG_BUF_SIZE, format, argp);
+#endif
+    va_end(argp);
+
+    if (ret >= 0 && ret < DEBUG_BUF_SIZE - 1) {
+        //str[ret]     = '\n';
+        str[ret] = '\0';
+    }
+
+    debug_send_line(ssl, level, file, line, str);
+}
+
+void mbedtls_debug_print_ret(const mbedtls_ssl_context *ssl, int level,
+                             const char *file, int line,
+                             const char *text, int ret)
+{
+    char str[DEBUG_BUF_SIZE];
+
+    if (ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold) {
+        return;
+    }
+
+    /*
+     * With non-blocking I/O and examples that just retry immediately,
+     * the logs would be quickly flooded with WANT_READ, so ignore that.
+     * Don't ignore WANT_WRITE however, since is is usually rare.
+     */
+    if (ret == MBEDTLS_ERR_SSL_WANT_READ) {
+        return;
+    }
+
+    mbedtls_snprintf(str, sizeof(str), "%s() returned %d (-0x%04x)",
+                     text, ret, -ret);
+
+    debug_send_line(ssl, level, file, line, str);
+}
+
+void mbedtls_debug_print_buf(const mbedtls_ssl_context *ssl, int level,
+                             const char *file, int line, const char *text,
+                             const unsigned char *buf, size_t len)
+{
+    char str[DEBUG_BUF_SIZE];
+    char txt[17];
+    size_t i, idx = 0;
+
+    if (ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold) {
+        return;
+    }
+
+    mbedtls_snprintf(str + idx, sizeof(str) - idx, "dumping '%s' (%u bytes)",
+                     text, (unsigned int) len);
+
+    debug_send_line(ssl, level, file, line, str);
+
+    idx = 0;
+    memset(txt, 0, sizeof(txt));
+    for (i = 0; i < len; i++) {
+        if (i >= 4096) {
+            break;
+        }
+
+        if (i % 16 == 0) {
+            if (i > 0) {
+                mbedtls_snprintf(str + idx, sizeof(str) - idx, "  %s", txt);
+                debug_send_line(ssl, level, file, line, str);
+
+                idx = 0;
+                memset(txt, 0, sizeof(txt));
+            }
+
+            idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, "%04x: ",
+                                    (unsigned int) i);
+
+        }
+
+        idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " %02x",
+                                (unsigned int) buf[i]);
+        txt[i % 16] = (buf[i] > 31 && buf[i] < 127) ? buf[i] : '.' ;
+    }
+
+    if (len > 0) {
+        for (/* i = i */; i % 16 != 0; i++) {
+            idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, "   ");
+        }
+
+        mbedtls_snprintf(str + idx, sizeof(str) - idx, "  %s", txt);
+        debug_send_line(ssl, level, file, line, str);
+    }
+}
+
+#if defined(MBEDTLS_ECP_C)
+void mbedtls_debug_print_ecp(const mbedtls_ssl_context *ssl, int level,
+                             const char *file, int line,
+                             const char *text, const mbedtls_ecp_point *X)
+{
+    char str[DEBUG_BUF_SIZE];
+
+    if (ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold) {
+        return;
+    }
+
+    mbedtls_snprintf(str, sizeof(str), "%s(X)", text);
+    mbedtls_debug_print_mpi(ssl, level, file, line, str, &X->X);
+
+    mbedtls_snprintf(str, sizeof(str), "%s(Y)", text);
+    mbedtls_debug_print_mpi(ssl, level, file, line, str, &X->Y);
+}
+#endif /* MBEDTLS_ECP_C */
+
+#if defined(MBEDTLS_BIGNUM_C)
+void mbedtls_debug_print_mpi(const mbedtls_ssl_context *ssl, int level,
+                             const char *file, int line,
+                             const char *text, const mbedtls_mpi *X)
+{
+    char str[DEBUG_BUF_SIZE];
+    int j, k, zeros = 1;
+    size_t i, n, idx = 0;
+
+    if (ssl->conf == NULL || ssl->conf->f_dbg == NULL || X == NULL || level > debug_threshold) {
+        return;
+    }
+
+    for (n = X->n - 1; n > 0; n--)
+        if (X->p[n] != 0) {
+            break;
+        }
+
+    for (j = (sizeof(mbedtls_mpi_uint) << 3) - 1; j >= 0; j--)
+        if (((X->p[n] >> j) & 1) != 0) {
+            break;
+        }
+
+    mbedtls_snprintf(str + idx, sizeof(str) - idx, "value of '%s' (%d bits) is:\n",
+                     text, (int)((n * (sizeof(mbedtls_mpi_uint) << 3)) + j + 1));
+
+    debug_send_line(ssl, level, file, line, str);
+
+    idx = 0;
+    for (i = n + 1, j = 0; i > 0; i--) {
+        if (zeros && X->p[i - 1] == 0) {
+            continue;
+        }
+
+        for (k = sizeof(mbedtls_mpi_uint) - 1; k >= 0; k--) {
+            if (zeros && ((X->p[i - 1] >> (k << 3)) & 0xFF) == 0) {
+                continue;
+            } else {
+                zeros = 0;
+            }
+
+            if (j % 16 == 0) {
+                if (j > 0) {
+                    mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n");
+                    debug_send_line(ssl, level, file, line, str);
+                    idx = 0;
+                }
+            }
+
+            idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " %02x", (unsigned int)
+                                    (X->p[i - 1] >> (k << 3)) & 0xFF);
+
+            j++;
+        }
+
+    }
+
+    if (zeros == 1) {
+        idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " 00");
+    }
+
+    mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n");
+    debug_send_line(ssl, level, file, line, str);
+}
+#endif /* MBEDTLS_BIGNUM_C */
+
+#if defined(MBEDTLS_X509_CRT_PARSE_C)
+static void debug_print_pk(const mbedtls_ssl_context *ssl, int level,
+                           const char *file, int line,
+                           const char *text, const mbedtls_pk_context *pk)
+{
+    size_t i;
+    mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS];
+    char name[16];
+
+    memset(items, 0, sizeof(items));
+
+    if (mbedtls_pk_debug(pk, items) != 0) {
+        debug_send_line(ssl, level, file, line,
+                        "invalid PK context\n");
+        return;
+    }
+
+    for (i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++) {
+        if (items[i].type == MBEDTLS_PK_DEBUG_NONE) {
+            return;
+        }
+
+        mbedtls_snprintf(name, sizeof(name), "%s%s", text, items[i].name);
+        name[sizeof(name) - 1] = '\0';
+
+        if (items[i].type == MBEDTLS_PK_DEBUG_MPI) {
+            mbedtls_debug_print_mpi(ssl, level, file, line, name, items[i].value);
+        } else
+#if defined(MBEDTLS_ECP_C)
+            if (items[i].type == MBEDTLS_PK_DEBUG_ECP) {
+                mbedtls_debug_print_ecp(ssl, level, file, line, name, items[i].value);
+            } else
+#endif
+                debug_send_line(ssl, level, file, line,
+                                "should not happen\n");
+    }
+}
+
+static void debug_print_line_by_line(const mbedtls_ssl_context *ssl, int level,
+                                     const char *file, int line, const char *text)
+{
+    char str[DEBUG_BUF_SIZE];
+    const char *start, *cur;
+
+    start = text;
+    for (cur = text; *cur != '\0'; cur++) {
+        if (*cur == '\n') {
+            size_t len = cur - start + 1;
+            if (len > DEBUG_BUF_SIZE - 1) {
+                len = DEBUG_BUF_SIZE - 1;
+            }
+
+            memcpy(str, start, len);
+            str[len] = '\0';
+
+            debug_send_line(ssl, level, file, line, str);
+
+            start = cur + 1;
+        }
+    }
+}
+
+void mbedtls_debug_print_crt(const mbedtls_ssl_context *ssl, int level,
+                             const char *file, int line,
+                             const char *text, const mbedtls_x509_crt *crt)
+{
+    char str[DEBUG_BUF_SIZE];
+    int i = 0;
+
+    if (ssl->conf == NULL || ssl->conf->f_dbg == NULL || crt == NULL || level > debug_threshold) {
+        return;
+    }
+
+    while (crt != NULL) {
+        char buf[1024];
+
+        mbedtls_snprintf(str, sizeof(str), "%s #%d:\n", text, ++i);
+        debug_send_line(ssl, level, file, line, str);
+
+        mbedtls_x509_crt_info(buf, sizeof(buf) - 1, "", crt);
+        debug_print_line_by_line(ssl, level, file, line, buf);
+
+        debug_print_pk(ssl, level, file, line, "crt->", &crt->pk);
+
+        crt = crt->next;
+    }
+}
+#endif /* MBEDTLS_X509_CRT_PARSE_C */
+
+#endif /* MBEDTLS_DEBUG_C */

+ 7 - 19
iotkit-embedded/src/tls/mbedtls-in-iotkit/library/entropy.c → iotkit-embedded/external_libs/mbedtls/library/entropy.c

@@ -1,24 +1,10 @@
 /*
- *  Entropy accumulator implementation
- *
- *  Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
 
+
+
+
 #if !defined(MBEDTLS_CONFIG_FILE)
 #include "mbedtls/config.h"
 #else
@@ -51,7 +37,9 @@
 #include "mbedtls/platform.h"
 #else
 #include <stdio.h>
-#define mbedtls_printf     printf
+#include "mbedtls/debug.h"
+
+#define mbedtls_printf     tls_info
 #endif /* MBEDTLS_PLATFORM_C */
 #endif /* MBEDTLS_SELF_TEST */
 

+ 4 - 18
iotkit-embedded/src/tls/mbedtls-in-iotkit/library/entropy_poll.c → iotkit-embedded/external_libs/mbedtls/library/entropy_poll.c

@@ -1,24 +1,10 @@
 /*
- *  Platform-specific and custom entropy polling functions
- *
- *  Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
 
+
+
+
 #if !defined(MBEDTLS_CONFIG_FILE)
 #include "mbedtls/config.h"
 #else

+ 4 - 18
iotkit-embedded/src/tls/mbedtls-in-iotkit/library/error.c → iotkit-embedded/external_libs/mbedtls/library/error.c

@@ -1,24 +1,10 @@
 /*
- *  Error message information
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
 
+
+
+
 #if !defined(MBEDTLS_CONFIG_FILE)
 #include "mbedtls/config.h"
 #else

+ 5 - 23
iotkit-embedded/src/tls/mbedtls-in-iotkit/library/md.c → iotkit-embedded/external_libs/mbedtls/library/md.c

@@ -1,28 +1,10 @@
-/**
- * \file mbedtls_md.c
- *
- * \brief Generic message digest wrapper for mbed TLS
- *
- * \author Adriaan de Jong <dejong@fox-it.com>
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
 
+
+
+
 #if !defined(MBEDTLS_CONFIG_FILE)
 #include "mbedtls/config.h"
 #else

+ 5 - 23
iotkit-embedded/src/tls/mbedtls-in-iotkit/library/md_wrap.c → iotkit-embedded/external_libs/mbedtls/library/md_wrap.c

@@ -1,28 +1,10 @@
-/**
- * \file md_wrap.c
- *
- * \brief Generic message digest wrapper for mbed TLS
- *
- * \author Adriaan de Jong <dejong@fox-it.com>
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
 
+
+
+
 #if !defined(MBEDTLS_CONFIG_FILE)
 #include "mbedtls/config.h"
 #else

+ 4 - 18
iotkit-embedded/src/tls/mbedtls-in-iotkit/library/net_sockets.c → iotkit-embedded/external_libs/mbedtls/library/net_sockets.c

@@ -1,24 +1,10 @@
 /*
- *  TCP/IP or UDP/IP networking functions
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
 
+
+
+
 #if !defined(MBEDTLS_CONFIG_FILE)
 #include "mbedtls/config.h"
 #else

+ 5 - 21
iotkit-embedded/src/tls/mbedtls-in-iotkit/library/oid.c → iotkit-embedded/external_libs/mbedtls/library/oid.c

@@ -1,26 +1,10 @@
-/**
- * \file oid.c
- *
- * \brief Object Identifier (OID) database
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
 
+
+
+
 #if !defined(MBEDTLS_CONFIG_FILE)
 #include "mbedtls/config.h"
 #else

+ 4 - 18
iotkit-embedded/src/tls/mbedtls-in-iotkit/library/pem.c → iotkit-embedded/external_libs/mbedtls/library/pem.c

@@ -1,24 +1,10 @@
 /*
- *  Privacy Enhanced Mail (PEM) decoding
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
 
+
+
+
 #if !defined(MBEDTLS_CONFIG_FILE)
 #include "mbedtls/config.h"
 #else

+ 4 - 18
iotkit-embedded/src/tls/mbedtls-in-iotkit/library/pk.c → iotkit-embedded/external_libs/mbedtls/library/pk.c

@@ -1,24 +1,10 @@
 /*
- *  Public Key abstraction layer
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
 
+
+
+
 #if !defined(MBEDTLS_CONFIG_FILE)
 #include "mbedtls/config.h"
 #else

+ 4 - 18
iotkit-embedded/src/tls/mbedtls-in-iotkit/library/pk_wrap.c → iotkit-embedded/external_libs/mbedtls/library/pk_wrap.c

@@ -1,24 +1,10 @@
 /*
- *  Public Key abstraction layer: wrapper functions
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
 
+
+
+
 #if !defined(MBEDTLS_CONFIG_FILE)
 #include "mbedtls/config.h"
 #else

+ 4 - 18
iotkit-embedded/src/tls/mbedtls-in-iotkit/library/pkparse.c → iotkit-embedded/external_libs/mbedtls/library/pkparse.c

@@ -1,24 +1,10 @@
 /*
- *  Public Key layer for parsing key files and structures
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
 
+
+
+
 #if !defined(MBEDTLS_CONFIG_FILE)
 #include "mbedtls/config.h"
 #else

+ 4 - 18
iotkit-embedded/src/tls/mbedtls-in-iotkit/library/platform.c → iotkit-embedded/external_libs/mbedtls/library/platform.c

@@ -1,24 +1,10 @@
 /*
- *  Platform abstraction layer
- *
- *  Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
 
+
+
+
 #if !defined(MBEDTLS_CONFIG_FILE)
 #include "mbedtls/config.h"
 #else

+ 4 - 18
iotkit-embedded/src/tls/mbedtls-in-iotkit/library/rsa.c → iotkit-embedded/external_libs/mbedtls/library/rsa.c

@@ -1,23 +1,9 @@
 /*
- *  The RSA public-key cryptosystem
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
+
+
+
 /*
  *  The following sources were referenced in the design of this implementation
  *  of the RSA algorithm:

+ 7 - 19
iotkit-embedded/src/tls/mbedtls-in-iotkit/library/sha1.c → iotkit-embedded/external_libs/mbedtls/library/sha1.c

@@ -1,23 +1,9 @@
 /*
- *  FIPS-180-1 compliant SHA-1 implementation
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
+
+
+
 /*
  *  The SHA-1 standard was published by NIST in 1993.
  *
@@ -41,7 +27,9 @@
 #include "mbedtls/platform.h"
 #else
 #include <stdio.h>
-#define mbedtls_printf printf
+#include "mbedtls/debug.h"
+
+#define mbedtls_printf tls_info
 #endif /* MBEDTLS_PLATFORM_C */
 #endif /* MBEDTLS_SELF_TEST */
 

+ 7 - 19
iotkit-embedded/src/tls/mbedtls-in-iotkit/library/sha256.c → iotkit-embedded/external_libs/mbedtls/library/sha256.c

@@ -1,23 +1,9 @@
 /*
- *  FIPS-180-2 compliant SHA-256 implementation
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
+
+
+
 /*
  *  The SHA-256 Secure Hash Standard was published by NIST in 2002.
  *
@@ -42,7 +28,9 @@
 #else
 #include <stdio.h>
 #include <stdlib.h>
-#define mbedtls_printf printf
+#include "mbedtls/debug.h"
+
+#define mbedtls_printf   tls_info
 #define mbedtls_calloc    calloc
 #define mbedtls_free       free
 #endif /* MBEDTLS_PLATFORM_C */

+ 5 - 21
iotkit-embedded/src/tls/mbedtls-in-iotkit/library/ssl_ciphersuites.c → iotkit-embedded/external_libs/mbedtls/library/ssl_ciphersuites.c

@@ -1,26 +1,10 @@
-/**
- * \file ssl_ciphersuites.c
- *
- * \brief SSL ciphersuites for mbed TLS
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
 
+
+
+
 #if !defined(MBEDTLS_CONFIG_FILE)
 #include "mbedtls/config.h"
 #else

+ 4 - 18
iotkit-embedded/src/tls/mbedtls-in-iotkit/library/ssl_cli.c → iotkit-embedded/external_libs/mbedtls/library/ssl_cli.c

@@ -1,24 +1,10 @@
 /*
- *  SSLv3/TLSv1 client-side functions
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
 
+
+
+
 #if !defined(MBEDTLS_CONFIG_FILE)
 #include "mbedtls/config.h"
 #else

+ 4 - 18
iotkit-embedded/src/tls/mbedtls-in-iotkit/library/ssl_cookie.c → iotkit-embedded/external_libs/mbedtls/library/ssl_cookie.c

@@ -1,23 +1,9 @@
 /*
- *  DTLS cookie callbacks implementation
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
+
+
+
 /*
  * These session callbacks use a simple chained list
  * to store and retrieve the session information.

+ 4 - 18
iotkit-embedded/src/tls/mbedtls-in-iotkit/library/ssl_tls.c → iotkit-embedded/external_libs/mbedtls/library/ssl_tls.c

@@ -1,23 +1,9 @@
 /*
- *  SSLv3/TLSv1 shared functions
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
+
+
+
 /*
  *  The SSL 3.0 specification was drafted by Netscape in 1996,
  *  and became an IETF standard in 1999.

+ 14 - 19
iotkit-embedded/src/tls/mbedtls-in-iotkit/library/timing.c → iotkit-embedded/external_libs/mbedtls/library/timing.c

@@ -1,24 +1,10 @@
 /*
- *  Portable interface to the CPU cycle counter
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
 
+
+
+
 #if !defined(MBEDTLS_CONFIG_FILE)
 #include "mbedtls/config.h"
 #else
@@ -29,7 +15,8 @@
 #include "mbedtls/platform.h"
 #else
 #include <stdio.h>
-#define mbedtls_printf     printf
+#include "mbedtls/debug.h"
+#define mbedtls_printf     tls_info
 #endif
 
 #if defined(MBEDTLS_TIMING_C)
@@ -232,6 +219,14 @@ static struct timeval tv_init;
   #define DELTA_EPOCH_IN_MICROSECS  11644473600000000ULL
 #endif
 
+#ifndef _TIMEZONE_DEFINED
+struct timezone
+{
+    int  tz_minuteswest; /* minutes W of Greenwich */
+    int  tz_dsttime;     /* type of dst correction */
+};
+#endif
+
 int mbedtls_gettimeofday(struct timeval *tv, struct timezone *tz)
 {
     unsigned __int64  tmpres = 0;

+ 4 - 18
iotkit-embedded/src/tls/mbedtls-in-iotkit/library/x509.c → iotkit-embedded/external_libs/mbedtls/library/x509.c

@@ -1,23 +1,9 @@
 /*
- *  X.509 common functions for parsing and verification
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
+
+
+
 /*
  *  The ITU-T X.509 standard defines a certificate format for PKI.
  *

+ 5 - 19
iotkit-embedded/src/tls/mbedtls-in-iotkit/library/x509_crt.c → iotkit-embedded/external_libs/mbedtls/library/x509_crt.c

@@ -1,23 +1,9 @@
 /*
- *  X.509 certificate parsing and verification
- *
- *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
- *  SPDX-License-Identifier: Apache-2.0
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may
- *  not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- *  This file is part of mbed TLS (https://tls.mbed.org)
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  */
+
+
+
 /*
  *  The ITU-T X.509 standard defines a certificate format for PKI.
  *
@@ -711,7 +697,7 @@ static int x509_crt_parse_der_core( mbedtls_x509_crt *crt, const unsigned char *
 
     memcpy( p, buf, crt->raw.len );
 
-    // Direct pointers to the new buffer 
+    // Direct pointers to the new buffer
     p += crt->raw.len - len;
     end = crt_end = p + len;
 

+ 2 - 0
iotkit-embedded/external_libs/nghttp2/iot.mk

@@ -0,0 +1,2 @@
+LIBA_TARGET := libiot_nghttp2.a
+HDR_REFS    += src/infra

+ 5363 - 0
iotkit-embedded/external_libs/nghttp2/nghttp2.h

@@ -0,0 +1,5363 @@
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
+ */
+
+
+#ifndef NGHTTP2_H
+#define NGHTTP2_H
+
+
+#ifdef IOTX_HTTP2_DEBUG
+#define DEBUGBUILD
+#endif
+/* Define WIN32 when build target is Win32 API (borrowed from
+   libcurl) */
+#if (defined(_WIN32) || defined(__WIN32__)) && !defined(WIN32)
+#define WIN32
+#define ssize_t unsigned int
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <stdlib.h>
+#if defined(_MSC_VER) && (_MSC_VER < 1800)
+/* MSVC < 2013 does not have inttypes.h because it is not C99
+   compliant.  See compiler macros and version number in
+   https://sourceforge.net/p/predef/wiki/Compilers/ */
+#include <stdint.h>
+#else /* !defined(_MSC_VER) || (_MSC_VER >= 1800) */
+#include <inttypes.h>
+#endif /* !defined(_MSC_VER) || (_MSC_VER >= 1800) */
+#include <sys/types.h>
+#include <stdarg.h>
+
+#include <nghttp2ver.h>
+
+#ifdef NGHTTP2_STATICLIB
+#define NGHTTP2_EXTERN
+#elif defined(WIN32__)
+#ifdef BUILDING_NGHTTP2
+#define NGHTTP2_EXTERN __declspec(dllexport)
+#else /* !BUILDING_NGHTTP2 */
+#define NGHTTP2_EXTERN __declspec(dllimport)
+#endif /* !BUILDING_NGHTTP2 */
+#else  /* !defined(WIN32) */
+#ifdef BUILDING_NGHTTP2
+#define NGHTTP2_EXTERN __attribute__((visibility("default")))
+#else /* !BUILDING_NGHTTP2 */
+#define NGHTTP2_EXTERN
+#endif /* !BUILDING_NGHTTP2 */
+#endif /* !defined(WIN32) */
+
+/**
+ * @macro
+ *
+ * The protocol version identification string of this library
+ * supports.  This identifier is used if HTTP/2 is used over TLS.
+ */
+#define NGHTTP2_PROTO_VERSION_ID "h2"
+/**
+ * @macro
+ *
+ * The length of :macro:`NGHTTP2_PROTO_VERSION_ID`.
+ */
+#define NGHTTP2_PROTO_VERSION_ID_LEN 2
+
+/**
+ * @macro
+ *
+ * The serialized form of ALPN protocol identifier this library
+ * supports.  Notice that first byte is the length of following
+ * protocol identifier.  This is the same wire format of `TLS ALPN
+ * extension <https://tools.ietf.org/html/rfc7301>`_.  This is useful
+ * to process incoming ALPN tokens in wire format.
+ */
+#define NGHTTP2_PROTO_ALPN "\x2h2"
+
+/**
+ * @macro
+ *
+ * The length of :macro:`NGHTTP2_PROTO_ALPN`.
+ */
+#define NGHTTP2_PROTO_ALPN_LEN (sizeof(NGHTTP2_PROTO_ALPN) - 1)
+
+/**
+ * @macro
+ *
+ * The protocol version identification string of this library
+ * supports.  This identifier is used if HTTP/2 is used over cleartext
+ * TCP.
+ */
+#define NGHTTP2_CLEARTEXT_PROTO_VERSION_ID "h2c"
+
+/**
+ * @macro
+ *
+ * The length of :macro:`NGHTTP2_CLEARTEXT_PROTO_VERSION_ID`.
+ */
+#define NGHTTP2_CLEARTEXT_PROTO_VERSION_ID_LEN 3
+
+struct nghttp2_session;
+/**
+ * @struct
+ *
+ * The primary structure to hold the resources needed for a HTTP/2
+ * session.  The details of this structure are intentionally hidden
+ * from the public API.
+ */
+typedef struct nghttp2_session nghttp2_session;
+
+/**
+ * @macro
+ *
+ * The age of :type:`nghttp2_info`
+ */
+#define NGHTTP2_VERSION_AGE 1
+
+#ifndef HTTP2_RECV_BUFFER_LENGHT
+#define HTTP2_RECV_BUFFER_LENGHT 16384
+#endif
+/**
+ * @struct
+ *
+ * This struct is what `nghttp2_version()` returns.  It holds
+ * information about the particular nghttp2 version.
+ */
+typedef struct {
+  /**
+   * Age of this struct.  This instance of nghttp2 sets it to
+   * :macro:`NGHTTP2_VERSION_AGE` but a future version may bump it and
+   * add more struct fields at the bottom
+   */
+  int age;
+  /**
+   * the :macro:`NGHTTP2_VERSION_NUM` number (since age ==1)
+   */
+  int version_num;
+  /**
+   * points to the :macro:`NGHTTP2_VERSION` string (since age ==1)
+   */
+  const char *version_str;
+  /**
+   * points to the :macro:`NGHTTP2_PROTO_VERSION_ID` string this
+   * instance implements (since age ==1)
+   */
+  const char *proto_str;
+  /* -------- the above fields all exist when age == 1 */
+} nghttp2_info;
+
+/**
+ * @macro
+ *
+ * The default weight of stream dependency.
+ */
+#define NGHTTP2_DEFAULT_WEIGHT 16
+
+/**
+ * @macro
+ *
+ * The maximum weight of stream dependency.
+ */
+#define NGHTTP2_MAX_WEIGHT 256
+
+/**
+ * @macro
+ *
+ * The minimum weight of stream dependency.
+ */
+#define NGHTTP2_MIN_WEIGHT 1
+
+/**
+ * @macro
+ *
+ * The maximum window size
+ */
+#define NGHTTP2_MAX_WINDOW_SIZE ((int32_t)((1U << 31) - 1))
+
+/**
+ * @macro
+ *
+ * The initial window size for stream level flow control.
+ */
+#define NGHTTP2_INITIAL_WINDOW_SIZE ((1 << 24) - 1)
+/**
+ * @macro
+ *
+ * The initial window size for connection level flow control.
+ */
+#define NGHTTP2_INITIAL_CONNECTION_WINDOW_SIZE ((1 << 24) - 1)
+
+/**
+ * @macro
+ *
+ * The default header table size.
+ */
+#define NGHTTP2_DEFAULT_HEADER_TABLE_SIZE (1 << 12)
+
+/**
+ * @macro
+ *
+ * The client magic string, which is the first 24 bytes byte string of
+ * client connection preface.
+ */
+#define NGHTTP2_CLIENT_MAGIC "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
+
+/**
+ * @macro
+ *
+ * The length of :macro:`NGHTTP2_CLIENT_MAGIC`.
+ */
+#define NGHTTP2_CLIENT_MAGIC_LEN 24
+
+/**
+ * @enum
+ *
+ * Error codes used in this library.  The code range is [-999, -500],
+ * inclusive. The following values are defined:
+ */
+typedef enum {
+  /**
+   * Invalid argument passed.
+   */
+  NGHTTP2_ERR_INVALID_ARGUMENT = -501,
+  /**
+   * Out of buffer space.
+   */
+  NGHTTP2_ERR_BUFFER_ERROR = -502,
+  /**
+   * The specified protocol version is not supported.
+   */
+  NGHTTP2_ERR_UNSUPPORTED_VERSION = -503,
+  /**
+   * Used as a return value from :type:`nghttp2_send_callback`,
+   * :type:`nghttp2_recv_callback` and
+   * :type:`nghttp2_send_data_callback` to indicate that the operation
+   * would block.
+   */
+  NGHTTP2_ERR_WOULDBLOCK = -504,
+  /**
+   * General protocol error
+   */
+  NGHTTP2_ERR_PROTO = -505,
+  /**
+   * The frame is invalid.
+   */
+  NGHTTP2_ERR_INVALID_FRAME = -506,
+  /**
+   * The peer performed a shutdown on the connection.
+   */
+  NGHTTP2_ERR_EOF = -507,
+  /**
+   * Used as a return value from
+   * :func:`nghttp2_data_source_read_callback` to indicate that data
+   * transfer is postponed.  See
+   * :func:`nghttp2_data_source_read_callback` for details.
+   */
+  NGHTTP2_ERR_DEFERRED = -508,
+  /**
+   * Stream ID has reached the maximum value.  Therefore no stream ID
+   * is available.
+   */
+  NGHTTP2_ERR_STREAM_ID_NOT_AVAILABLE = -509,
+  /**
+   * The stream is already closed; or the stream ID is invalid.
+   */
+  NGHTTP2_ERR_STREAM_CLOSED = -510,
+  /**
+   * RST_STREAM has been added to the outbound queue.  The stream is
+   * in closing state.
+   */
+  NGHTTP2_ERR_STREAM_CLOSING = -511,
+  /**
+   * The transmission is not allowed for this stream (e.g., a frame
+   * with END_STREAM flag set has already sent).
+   */
+  NGHTTP2_ERR_STREAM_SHUT_WR = -512,
+  /**
+   * The stream ID is invalid.
+   */
+  NGHTTP2_ERR_INVALID_STREAM_ID = -513,
+  /**
+   * The state of the stream is not valid (e.g., DATA cannot be sent
+   * to the stream if response HEADERS has not been sent).
+   */
+  NGHTTP2_ERR_INVALID_STREAM_STATE = -514,
+  /**
+   * Another DATA frame has already been deferred.
+   */
+  NGHTTP2_ERR_DEFERRED_DATA_EXIST = -515,
+  /**
+   * Starting new stream is not allowed (e.g., GOAWAY has been sent
+   * and/or received).
+   */
+  NGHTTP2_ERR_START_STREAM_NOT_ALLOWED = -516,
+  /**
+   * GOAWAY has already been sent.
+   */
+  NGHTTP2_ERR_GOAWAY_ALREADY_SENT = -517,
+  /**
+   * The received frame contains the invalid header block (e.g., There
+   * are duplicate header names; or the header names are not encoded
+   * in US-ASCII character set and not lower cased; or the header name
+   * is zero-length string; or the header value contains multiple
+   * in-sequence NUL bytes).
+   */
+  NGHTTP2_ERR_INVALID_HEADER_BLOCK = -518,
+  /**
+   * Indicates that the context is not suitable to perform the
+   * requested operation.
+   */
+  NGHTTP2_ERR_INVALID_STATE = -519,
+  /**
+   * The user callback function failed due to the temporal error.
+   */
+  NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE = -521,
+  /**
+   * The length of the frame is invalid, either too large or too small.
+   */
+  NGHTTP2_ERR_FRAME_SIZE_ERROR = -522,
+  /**
+   * Header block inflate/deflate error.
+   */
+  NGHTTP2_ERR_HEADER_COMP = -523,
+  /**
+   * Flow control error
+   */
+  NGHTTP2_ERR_FLOW_CONTROL = -524,
+  /**
+   * Insufficient buffer size given to function.
+   */
+  NGHTTP2_ERR_INSUFF_BUFSIZE = -525,
+  /**
+   * Callback was paused by the application
+   */
+  NGHTTP2_ERR_PAUSE = -526,
+  /**
+   * There are too many in-flight SETTING frame and no more
+   * transmission of SETTINGS is allowed.
+   */
+  NGHTTP2_ERR_TOO_MANY_INFLIGHT_SETTINGS = -527,
+  /**
+   * The server push is disabled.
+   */
+  NGHTTP2_ERR_PUSH_DISABLED = -528,
+  /**
+   * DATA or HEADERS frame for a given stream has been already
+   * submitted and has not been fully processed yet.  Application
+   * should wait for the transmission of the previously submitted
+   * frame before submitting another.
+   */
+  NGHTTP2_ERR_DATA_EXIST = -529,
+  /**
+   * The current session is closing due to a connection error or
+   * `nghttp2_session_terminate_session()` is called.
+   */
+  NGHTTP2_ERR_SESSION_CLOSING = -530,
+  /**
+   * Invalid HTTP header field was received and stream is going to be
+   * closed.
+   */
+  NGHTTP2_ERR_HTTP_HEADER = -531,
+  /**
+   * Violation in HTTP messaging rule.
+   */
+  NGHTTP2_ERR_HTTP_MESSAGING = -532,
+  /**
+   * Stream was refused.
+   */
+  NGHTTP2_ERR_REFUSED_STREAM = -533,
+  /**
+   * Unexpected internal error, but recovered.
+   */
+  NGHTTP2_ERR_INTERNAL = -534,
+  /**
+   * Indicates that a processing was canceled.
+   */
+  NGHTTP2_ERR_CANCEL = -535,
+  /**
+   * When a local endpoint expects to receive SETTINGS frame, it
+   * receives an other type of frame.
+   */
+  NGHTTP2_ERR_SETTINGS_EXPECTED = -536,
+  /**
+   * The errors < :enum:`NGHTTP2_ERR_FATAL` mean that the library is
+   * under unexpected condition and processing was terminated (e.g.,
+   * out of memory).  If application receives this error code, it must
+   * stop using that :type:`nghttp2_session` object and only allowed
+   * operation for that object is deallocate it using
+   * `nghttp2_session_del()`.
+   */
+  NGHTTP2_ERR_FATAL = -900,
+  /**
+   * Out of memory.  This is a fatal error.
+   */
+  NGHTTP2_ERR_NOMEM = -901,
+  /**
+   * The user callback function failed.  This is a fatal error.
+   */
+  NGHTTP2_ERR_CALLBACK_FAILURE = -902,
+  /**
+   * Invalid client magic (see :macro:`NGHTTP2_CLIENT_MAGIC`) was
+   * received and further processing is not possible.
+   */
+  NGHTTP2_ERR_BAD_CLIENT_MAGIC = -903,
+  /**
+   * Possible flooding by peer was detected in this HTTP/2 session.
+   * Flooding is measured by how many PING and SETTINGS frames with
+   * ACK flag set are queued for transmission.  These frames are
+   * response for the peer initiated frames, and peer can cause memory
+   * exhaustion on server side to send these frames forever and does
+   * not read network.
+   */
+  NGHTTP2_ERR_FLOODED = -904
+} nghttp2_error;
+
+/**
+ * @struct
+ *
+ * The object representing single contiguous buffer.
+ */
+typedef struct {
+  /**
+   * The pointer to the buffer.
+   */
+  uint8_t *base;
+  /**
+   * The length of the buffer.
+   */
+  size_t len;
+} nghttp2_vec;
+
+struct nghttp2_rcbuf;
+
+/**
+ * @struct
+ *
+ * The object representing reference counted buffer.  The details of
+ * this structure are intentionally hidden from the public API.
+ */
+typedef struct nghttp2_rcbuf nghttp2_rcbuf;
+
+/**
+ * @function
+ *
+ * Increments the reference count of |rcbuf| by 1.
+ */
+NGHTTP2_EXTERN void nghttp2_rcbuf_incref(nghttp2_rcbuf *rcbuf);
+
+/**
+ * @function
+ *
+ * Decrements the reference count of |rcbuf| by 1.  If the reference
+ * count becomes zero, the object pointed by |rcbuf| will be freed.
+ * In this case, application must not use |rcbuf| again.
+ */
+NGHTTP2_EXTERN void nghttp2_rcbuf_decref(nghttp2_rcbuf *rcbuf);
+
+/**
+ * @function
+ *
+ * Returns the underlying buffer managed by |rcbuf|.
+ */
+NGHTTP2_EXTERN nghttp2_vec nghttp2_rcbuf_get_buf(nghttp2_rcbuf *rcbuf);
+
+/**
+ * @function
+ *
+ * Returns nonzero if the underlying buffer is statically allocated,
+ * and 0 otherwise. This can be useful for language bindings that wish
+ * to avoid creating duplicate strings for these buffers.
+ */
+NGHTTP2_EXTERN int nghttp2_rcbuf_is_static(const nghttp2_rcbuf *rcbuf);
+
+/**
+ * @enum
+ *
+ * The flags for header field name/value pair.
+ */
+typedef enum {
+  /**
+   * No flag set.
+   */
+  NGHTTP2_NV_FLAG_NONE = 0,
+  /**
+   * Indicates that this name/value pair must not be indexed ("Literal
+   * Header Field never Indexed" representation must be used in HPACK
+   * encoding).  Other implementation calls this bit as "sensitive".
+   */
+  NGHTTP2_NV_FLAG_NO_INDEX = 0x01,
+  /**
+   * This flag is set solely by application.  If this flag is set, the
+   * library does not make a copy of header field name.  This could
+   * improve performance.
+   */
+  NGHTTP2_NV_FLAG_NO_COPY_NAME = 0x02,
+  /**
+   * This flag is set solely by application.  If this flag is set, the
+   * library does not make a copy of header field value.  This could
+   * improve performance.
+   */
+  NGHTTP2_NV_FLAG_NO_COPY_VALUE = 0x04
+} nghttp2_nv_flag;
+
+/**
+ * @struct
+ *
+ * The name/value pair, which mainly used to represent header fields.
+ */
+typedef struct {
+  /**
+   * The |name| byte string.  If this struct is presented from library
+   * (e.g., :type:`nghttp2_on_frame_recv_callback`), |name| is
+   * guaranteed to be NULL-terminated.  For some callbacks
+   * (:type:`nghttp2_before_frame_send_callback`,
+   * :type:`nghttp2_on_frame_send_callback`, and
+   * :type:`nghttp2_on_frame_not_send_callback`), it may not be
+   * NULL-terminated if header field is passed from application with
+   * the flag :enum:`NGHTTP2_NV_FLAG_NO_COPY_NAME`).  When application
+   * is constructing this struct, |name| is not required to be
+   * NULL-terminated.
+   */
+  uint8_t *name;
+  /**
+   * The |value| byte string.  If this struct is presented from
+   * library (e.g., :type:`nghttp2_on_frame_recv_callback`), |value|
+   * is guaranteed to be NULL-terminated.  For some callbacks
+   * (:type:`nghttp2_before_frame_send_callback`,
+   * :type:`nghttp2_on_frame_send_callback`, and
+   * :type:`nghttp2_on_frame_not_send_callback`), it may not be
+   * NULL-terminated if header field is passed from application with
+   * the flag :enum:`NGHTTP2_NV_FLAG_NO_COPY_VALUE`).  When
+   * application is constructing this struct, |value| is not required
+   * to be NULL-terminated.
+   */
+  uint8_t *value;
+  /**
+   * The length of the |name|, excluding terminating NULL.
+   */
+  size_t namelen;
+  /**
+   * The length of the |value|, excluding terminating NULL.
+   */
+  size_t valuelen;
+  /**
+   * Bitwise OR of one or more of :type:`nghttp2_nv_flag`.
+   */
+  uint8_t flags;
+} nghttp2_nv;
+
+/**
+ * @enum
+ *
+ * The frame types in HTTP/2 specification.
+ */
+typedef enum {
+  /**
+   * The DATA frame.
+   */
+  NGHTTP2_DATA = 0,
+  /**
+   * The HEADERS frame.
+   */
+  NGHTTP2_HEADERS = 0x01,
+  /**
+   * The PRIORITY frame.
+   */
+  NGHTTP2_PRIORITY = 0x02,
+  /**
+   * The RST_STREAM frame.
+   */
+  NGHTTP2_RST_STREAM = 0x03,
+  /**
+   * The SETTINGS frame.
+   */
+  NGHTTP2_SETTINGS = 0x04,
+  /**
+   * The PUSH_PROMISE frame.
+   */
+  NGHTTP2_PUSH_PROMISE = 0x05,
+  /**
+   * The PING frame.
+   */
+  NGHTTP2_PING = 0x06,
+  /**
+   * The GOAWAY frame.
+   */
+  NGHTTP2_GOAWAY = 0x07,
+  /**
+   * The WINDOW_UPDATE frame.
+   */
+  NGHTTP2_WINDOW_UPDATE = 0x08,
+  /**
+   * The CONTINUATION frame.  This frame type won't be passed to any
+   * callbacks because the library processes this frame type and its
+   * preceding HEADERS/PUSH_PROMISE as a single frame.
+   */
+  NGHTTP2_CONTINUATION = 0x09,
+  /**
+   * The ALTSVC frame, which is defined in `RFC 7383
+   * <https://tools.ietf.org/html/rfc7838#section-4>`_.
+   */
+  NGHTTP2_ALTSVC = 0x0a
+} nghttp2_frame_type;
+
+/**
+ * @enum
+ *
+ * The flags for HTTP/2 frames.  This enum defines all flags for all
+ * frames.
+ */
+typedef enum {
+  /**
+   * No flag set.
+   */
+  NGHTTP2_FLAG_NONE = 0,
+  /**
+   * The END_STREAM flag.
+   */
+  NGHTTP2_FLAG_END_STREAM = 0x01,
+  /**
+   * The END_HEADERS flag.
+   */
+  NGHTTP2_FLAG_END_HEADERS = 0x04,
+  /**
+   * The ACK flag.
+   */
+  NGHTTP2_FLAG_ACK = 0x01,
+  /**
+   * The PADDED flag.
+   */
+  NGHTTP2_FLAG_PADDED = 0x08,
+  /**
+   * The PRIORITY flag.
+   */
+  NGHTTP2_FLAG_PRIORITY = 0x20
+} nghttp2_flag;
+
+/**
+ * @enum
+ * The SETTINGS ID.
+ */
+typedef enum {
+  /**
+   * SETTINGS_HEADER_TABLE_SIZE
+   */
+  NGHTTP2_SETTINGS_HEADER_TABLE_SIZE = 0x01,
+  /**
+   * SETTINGS_ENABLE_PUSH
+   */
+  NGHTTP2_SETTINGS_ENABLE_PUSH = 0x02,
+  /**
+   * SETTINGS_MAX_CONCURRENT_STREAMS
+   */
+  NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS = 0x03,
+  /**
+   * SETTINGS_INITIAL_WINDOW_SIZE
+   */
+  NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE = 0x04,
+  /**
+   * SETTINGS_MAX_FRAME_SIZE
+   */
+  NGHTTP2_SETTINGS_MAX_FRAME_SIZE = 0x05,
+  /**
+   * SETTINGS_MAX_HEADER_LIST_SIZE
+   */
+  NGHTTP2_SETTINGS_MAX_HEADER_LIST_SIZE = 0x06
+} nghttp2_settings_id;
+/* Note: If we add SETTINGS, update the capacity of
+   NGHTTP2_INBOUND_NUM_IV as well */
+
+/**
+ * @macro
+ *
+ * .. warning::
+ *
+ *   Deprecated.  The initial max concurrent streams is 0xffffffffu.
+ *
+ * Default maximum number of incoming concurrent streams.  Use
+ * `nghttp2_submit_settings()` with
+ * :enum:`NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS` to change the
+ * maximum number of incoming concurrent streams.
+ *
+ * .. note::
+ *
+ *   The maximum number of outgoing concurrent streams is 100 by
+ *   default.
+ */
+#define NGHTTP2_INITIAL_MAX_CONCURRENT_STREAMS ((1U << 31) - 1)
+
+/**
+ * @enum
+ * The status codes for the RST_STREAM and GOAWAY frames.
+ */
+typedef enum {
+  /**
+   * No errors.
+   */
+  NGHTTP2_NO_ERROR = 0x00,
+  /**
+   * PROTOCOL_ERROR
+   */
+  NGHTTP2_PROTOCOL_ERROR = 0x01,
+  /**
+   * INTERNAL_ERROR
+   */
+  NGHTTP2_INTERNAL_ERROR = 0x02,
+  /**
+   * FLOW_CONTROL_ERROR
+   */
+  NGHTTP2_FLOW_CONTROL_ERROR = 0x03,
+  /**
+   * SETTINGS_TIMEOUT
+   */
+  NGHTTP2_SETTINGS_TIMEOUT = 0x04,
+  /**
+   * STREAM_CLOSED
+   */
+  NGHTTP2_STREAM_CLOSED = 0x05,
+  /**
+   * FRAME_SIZE_ERROR
+   */
+  NGHTTP2_FRAME_SIZE_ERROR = 0x06,
+  /**
+   * REFUSED_STREAM
+   */
+  NGHTTP2_REFUSED_STREAM = 0x07,
+  /**
+   * CANCEL
+   */
+  NGHTTP2_CANCEL = 0x08,
+  /**
+   * COMPRESSION_ERROR
+   */
+  NGHTTP2_COMPRESSION_ERROR = 0x09,
+  /**
+   * CONNECT_ERROR
+   */
+  NGHTTP2_CONNECT_ERROR = 0x0a,
+  /**
+   * ENHANCE_YOUR_CALM
+   */
+  NGHTTP2_ENHANCE_YOUR_CALM = 0x0b,
+  /**
+   * INADEQUATE_SECURITY
+   */
+  NGHTTP2_INADEQUATE_SECURITY = 0x0c,
+  /**
+   * HTTP_1_1_REQUIRED
+   */
+  NGHTTP2_HTTP_1_1_REQUIRED = 0x0d
+} nghttp2_error_code;
+
+/**
+ * @struct
+ * The frame header.
+ */
+typedef struct {
+  /**
+   * The length field of this frame, excluding frame header.
+   */
+  size_t length;
+  /**
+   * The stream identifier (aka, stream ID)
+   */
+  int32_t stream_id;
+  /**
+   * The type of this frame.  See `nghttp2_frame_type`.
+   */
+  uint8_t type;
+  /**
+   * The flags.
+   */
+  uint8_t flags;
+  /**
+   * Reserved bit in frame header.  Currently, this is always set to 0
+   * and application should not expect something useful in here.
+   */
+  uint8_t reserved;
+} nghttp2_frame_hd;
+
+/**
+ * @union
+ *
+ * This union represents the some kind of data source passed to
+ * :type:`nghttp2_data_source_read_callback`.
+ */
+typedef struct {
+  /**
+   * The integer field, suitable for a file descriptor.
+   */
+  int fd;
+  /**
+   * data length.
+   */
+  int len;
+  /**
+   * The pointer to an arbitrary object.
+   */
+  void *ptr;
+} nghttp2_data_source;
+
+/**
+ * @enum
+ *
+ * The flags used to set in |data_flags| output parameter in
+ * :type:`nghttp2_data_source_read_callback`.
+ */
+typedef enum {
+  /**
+   * No flag set.
+   */
+  NGHTTP2_DATA_FLAG_NONE = 0,
+  /**
+   * Indicates EOF was sensed.
+   */
+  NGHTTP2_DATA_FLAG_EOF = 0x01,
+  /**
+   * Indicates that END_STREAM flag must not be set even if
+   * NGHTTP2_DATA_FLAG_EOF is set.  Usually this flag is used to send
+   * trailer fields with `nghttp2_submit_request()` or
+   * `nghttp2_submit_response()`.
+   */
+  NGHTTP2_DATA_FLAG_NO_END_STREAM = 0x02,
+  /**
+   * Indicates that application will send complete DATA frame in
+   * :type:`nghttp2_send_data_callback`.
+   */
+  NGHTTP2_DATA_FLAG_NO_COPY = 0x04
+} nghttp2_data_flag;
+
+/**
+ * @functypedef
+ *
+ * Callback function invoked when the library wants to read data from
+ * the |source|.  The read data is sent in the stream |stream_id|.
+ * The implementation of this function must read at most |length|
+ * bytes of data from |source| (or possibly other places) and store
+ * them in |buf| and return number of data stored in |buf|.  If EOF is
+ * reached, set :enum:`NGHTTP2_DATA_FLAG_EOF` flag in |*data_flags|.
+ *
+ * Sometime it is desirable to avoid copying data into |buf| and let
+ * application to send data directly.  To achieve this, set
+ * :enum:`NGHTTP2_DATA_FLAG_NO_COPY` to |*data_flags| (and possibly
+ * other flags, just like when we do copy), and return the number of
+ * bytes to send without copying data into |buf|.  The library, seeing
+ * :enum:`NGHTTP2_DATA_FLAG_NO_COPY`, will invoke
+ * :type:`nghttp2_send_data_callback`.  The application must send
+ * complete DATA frame in that callback.
+ *
+ * If this callback is set by `nghttp2_submit_request()`,
+ * `nghttp2_submit_response()` or `nghttp2_submit_headers()` and
+ * `nghttp2_submit_data()` with flag parameter
+ * :enum:`NGHTTP2_FLAG_END_STREAM` set, and
+ * :enum:`NGHTTP2_DATA_FLAG_EOF` flag is set to |*data_flags|, DATA
+ * frame will have END_STREAM flag set.  Usually, this is expected
+ * behaviour and all are fine.  One exception is send trailer fields.
+ * You cannot send trailer fields after sending frame with END_STREAM
+ * set.  To avoid this problem, one can set
+ * :enum:`NGHTTP2_DATA_FLAG_NO_END_STREAM` along with
+ * :enum:`NGHTTP2_DATA_FLAG_EOF` to signal the library not to set
+ * END_STREAM in DATA frame.  Then application can use
+ * `nghttp2_submit_trailer()` to send trailer fields.
+ * `nghttp2_submit_trailer()` can be called inside this callback.
+ *
+ * If the application wants to postpone DATA frames (e.g.,
+ * asynchronous I/O, or reading data blocks for long time), it is
+ * achieved by returning :enum:`NGHTTP2_ERR_DEFERRED` without reading
+ * any data in this invocation.  The library removes DATA frame from
+ * the outgoing queue temporarily.  To move back deferred DATA frame
+ * to outgoing queue, call `nghttp2_session_resume_data()`.
+ *
+ * By default, |length| is limited to 16KiB at maximum.  If peer
+ * allows larger frames, application can enlarge transmission buffer
+ * size.  See :type:`nghttp2_data_source_read_length_callback` for
+ * more details.
+ *
+ * If the application just wants to return from
+ * `nghttp2_session_send()` or `nghttp2_session_mem_send()` without
+ * sending anything, return :enum:`NGHTTP2_ERR_PAUSE`.
+ *
+ * In case of error, there are 2 choices. Returning
+ * :enum:`NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE` will close the stream
+ * by issuing RST_STREAM with :enum:`NGHTTP2_INTERNAL_ERROR`.  If a
+ * different error code is desirable, use
+ * `nghttp2_submit_rst_stream()` with a desired error code and then
+ * return :enum:`NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE`.  Returning
+ * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE` will signal the entire session
+ * failure.
+ */
+typedef ssize_t (*nghttp2_data_source_read_callback)(
+    nghttp2_session *session, int32_t stream_id, uint8_t *buf, size_t length,
+    uint32_t *data_flags, nghttp2_data_source *source, void *user_data);
+
+/**
+ * @struct
+ *
+ * This struct represents the data source and the way to read a chunk
+ * of data from it.
+ */
+typedef struct {
+  /**
+   * The data source.
+   */
+  nghttp2_data_source source;
+  /**
+   * The callback function to read a chunk of data from the |source|.
+   */
+  nghttp2_data_source_read_callback read_callback;
+} nghttp2_data_provider;
+
+/**
+ * @struct
+ *
+ * The DATA frame.  The received data is delivered via
+ * :type:`nghttp2_on_data_chunk_recv_callback`.
+ */
+typedef struct {
+  nghttp2_frame_hd hd;
+  /**
+   * The length of the padding in this frame.  This includes PAD_HIGH
+   * and PAD_LOW.
+   */
+  size_t padlen;
+} nghttp2_data;
+
+/**
+ * @enum
+ *
+ * The category of HEADERS, which indicates the role of the frame.  In
+ * HTTP/2 spec, request, response, push response and other arbitrary
+ * headers (e.g., trailer fields) are all called just HEADERS.  To
+ * give the application the role of incoming HEADERS frame, we define
+ * several categories.
+ */
+typedef enum {
+  /**
+   * The HEADERS frame is opening new stream, which is analogous to
+   * SYN_STREAM in SPDY.
+   */
+  NGHTTP2_HCAT_REQUEST = 0,
+  /**
+   * The HEADERS frame is the first response headers, which is
+   * analogous to SYN_REPLY in SPDY.
+   */
+  NGHTTP2_HCAT_RESPONSE = 1,
+  /**
+   * The HEADERS frame is the first headers sent against reserved
+   * stream.
+   */
+  NGHTTP2_HCAT_PUSH_RESPONSE = 2,
+  /**
+   * The HEADERS frame which does not apply for the above categories,
+   * which is analogous to HEADERS in SPDY.  If non-final response
+   * (e.g., status 1xx) is used, final response HEADERS frame will be
+   * categorized here.
+   */
+  NGHTTP2_HCAT_HEADERS = 3
+} nghttp2_headers_category;
+
+/**
+ * @struct
+ *
+ * The structure to specify stream dependency.
+ */
+typedef struct {
+  /**
+   * The stream ID of the stream to depend on.  Specifying 0 makes
+   * stream not depend any other stream.
+   */
+  int32_t stream_id;
+  /**
+   * The weight of this dependency.
+   */
+  int32_t weight;
+  /**
+   * nonzero means exclusive dependency
+   */
+  uint8_t exclusive;
+} nghttp2_priority_spec;
+
+/**
+ * @struct
+ *
+ * The HEADERS frame.  It has the following members:
+ */
+typedef struct {
+  /**
+   * The frame header.
+   */
+  nghttp2_frame_hd hd;
+  /**
+   * The length of the padding in this frame.  This includes PAD_HIGH
+   * and PAD_LOW.
+   */
+  size_t padlen;
+  /**
+   * The priority specification
+   */
+  nghttp2_priority_spec pri_spec;
+  /**
+   * The name/value pairs.
+   */
+  nghttp2_nv *nva;
+  /**
+   * The number of name/value pairs in |nva|.
+   */
+  size_t nvlen;
+  /**
+   * The category of this HEADERS frame.
+   */
+  nghttp2_headers_category cat;
+} nghttp2_headers;
+
+/**
+ * @struct
+ *
+ * The PRIORITY frame.  It has the following members:
+ */
+typedef struct {
+  /**
+   * The frame header.
+   */
+  nghttp2_frame_hd hd;
+  /**
+   * The priority specification.
+   */
+  nghttp2_priority_spec pri_spec;
+} nghttp2_priority;
+
+/**
+ * @struct
+ *
+ * The RST_STREAM frame.  It has the following members:
+ */
+typedef struct {
+  /**
+   * The frame header.
+   */
+  nghttp2_frame_hd hd;
+  /**
+   * The error code.  See :type:`nghttp2_error_code`.
+   */
+  uint32_t error_code;
+} nghttp2_rst_stream;
+
+/**
+ * @struct
+ *
+ * The SETTINGS ID/Value pair.  It has the following members:
+ */
+typedef struct {
+  /**
+   * The SETTINGS ID.  See :type:`nghttp2_settings_id`.
+   */
+  int32_t settings_id;
+  /**
+   * The value of this entry.
+   */
+  uint32_t value;
+} nghttp2_settings_entry;
+
+/**
+ * @struct
+ *
+ * The SETTINGS frame.  It has the following members:
+ */
+typedef struct {
+  /**
+   * The frame header.
+   */
+  nghttp2_frame_hd hd;
+  /**
+   * The number of SETTINGS ID/Value pairs in |iv|.
+   */
+  size_t niv;
+  /**
+   * The pointer to the array of SETTINGS ID/Value pair.
+   */
+  nghttp2_settings_entry *iv;
+} nghttp2_settings;
+
+/**
+ * @struct
+ *
+ * The PUSH_PROMISE frame.  It has the following members:
+ */
+typedef struct {
+  /**
+   * The frame header.
+   */
+  nghttp2_frame_hd hd;
+  /**
+   * The length of the padding in this frame.  This includes PAD_HIGH
+   * and PAD_LOW.
+   */
+  size_t padlen;
+  /**
+   * The name/value pairs.
+   */
+  nghttp2_nv *nva;
+  /**
+   * The number of name/value pairs in |nva|.
+   */
+  size_t nvlen;
+  /**
+   * The promised stream ID
+   */
+  int32_t promised_stream_id;
+  /**
+   * Reserved bit.  Currently this is always set to 0 and application
+   * should not expect something useful in here.
+   */
+  uint8_t reserved;
+} nghttp2_push_promise;
+
+/**
+ * @struct
+ *
+ * The PING frame.  It has the following members:
+ */
+typedef struct {
+  /**
+   * The frame header.
+   */
+  nghttp2_frame_hd hd;
+  /**
+   * The opaque data
+   */
+  uint8_t opaque_data[8];
+} nghttp2_ping;
+
+/**
+ * @struct
+ *
+ * The GOAWAY frame.  It has the following members:
+ */
+typedef struct {
+  /**
+   * The frame header.
+   */
+  nghttp2_frame_hd hd;
+  /**
+   * The last stream stream ID.
+   */
+  int32_t last_stream_id;
+  /**
+   * The error code.  See :type:`nghttp2_error_code`.
+   */
+  uint32_t error_code;
+  /**
+   * The additional debug data
+   */
+  uint8_t *opaque_data;
+  /**
+   * The length of |opaque_data| member.
+   */
+  size_t opaque_data_len;
+  /**
+   * Reserved bit.  Currently this is always set to 0 and application
+   * should not expect something useful in here.
+   */
+  uint8_t reserved;
+} nghttp2_goaway;
+
+/**
+ * @struct
+ *
+ * The WINDOW_UPDATE frame.  It has the following members:
+ */
+typedef struct {
+  /**
+   * The frame header.
+   */
+  nghttp2_frame_hd hd;
+  /**
+   * The window size increment.
+   */
+  int32_t window_size_increment;
+  /**
+   * Reserved bit.  Currently this is always set to 0 and application
+   * should not expect something useful in here.
+   */
+  uint8_t reserved;
+} nghttp2_window_update;
+
+/**
+ * @struct
+ *
+ * The extension frame.  It has following members:
+ */
+typedef struct {
+  /**
+   * The frame header.
+   */
+  nghttp2_frame_hd hd;
+  /**
+   * The pointer to extension payload.  The exact pointer type is
+   * determined by hd.type.
+   *
+   * Currently, no extension is supported.  This is a place holder for
+   * the future extensions.
+   */
+  void *payload;
+} nghttp2_extension;
+
+/**
+ * @union
+ *
+ * This union includes all frames to pass them to various function
+ * calls as nghttp2_frame type.  The CONTINUATION frame is omitted
+ * from here because the library deals with it internally.
+ */
+typedef union {
+  /**
+   * The frame header, which is convenient to inspect frame header.
+   */
+  nghttp2_frame_hd hd;
+  /**
+   * The DATA frame.
+   */
+  nghttp2_data data;
+  /**
+   * The HEADERS frame.
+   */
+  nghttp2_headers headers;
+  /**
+   * The PRIORITY frame.
+   */
+  nghttp2_priority priority;
+  /**
+   * The RST_STREAM frame.
+   */
+  nghttp2_rst_stream rst_stream;
+  /**
+   * The SETTINGS frame.
+   */
+  nghttp2_settings settings;
+  /**
+   * The PUSH_PROMISE frame.
+   */
+  nghttp2_push_promise push_promise;
+  /**
+   * The PING frame.
+   */
+  nghttp2_ping ping;
+  /**
+   * The GOAWAY frame.
+   */
+  nghttp2_goaway goaway;
+  /**
+   * The WINDOW_UPDATE frame.
+   */
+  nghttp2_window_update window_update;
+  /**
+   * The extension frame.
+   */
+  nghttp2_extension ext;
+} nghttp2_frame;
+
+/**
+ * @functypedef
+ *
+ * Callback function invoked when |session| wants to send data to the
+ * remote peer.  The implementation of this function must send at most
+ * |length| bytes of data stored in |data|.  The |flags| is currently
+ * not used and always 0. It must return the number of bytes sent if
+ * it succeeds.  If it cannot send any single byte without blocking,
+ * it must return :enum:`NGHTTP2_ERR_WOULDBLOCK`.  For other errors,
+ * it must return :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`.  The
+ * |user_data| pointer is the third argument passed in to the call to
+ * `nghttp2_session_client_new()` or `nghttp2_session_server_new()`.
+ *
+ * This callback is required if the application uses
+ * `nghttp2_session_send()` to send data to the remote endpoint.  If
+ * the application uses solely `nghttp2_session_mem_send()` instead,
+ * this callback function is unnecessary.
+ *
+ * To set this callback to :type:`nghttp2_session_callbacks`, use
+ * `nghttp2_session_callbacks_set_send_callback()`.
+ *
+ * .. note::
+ *
+ *   The |length| may be very small.  If that is the case, and
+ *   application disables Nagle algorithm (``TCP_NODELAY``), then just
+ *   writing |data| to the network stack leads to very small packet,
+ *   and it is very inefficient.  An application should be responsible
+ *   to buffer up small chunks of data as necessary to avoid this
+ *   situation.
+ */
+typedef ssize_t (*nghttp2_send_callback)(nghttp2_session *session,
+                                         const uint8_t *data, size_t length,
+                                         int flags, void *user_data);
+
+/**
+ * @functypedef
+ *
+ * Callback function invoked when :enum:`NGHTTP2_DATA_FLAG_NO_COPY` is
+ * used in :type:`nghttp2_data_source_read_callback` to send complete
+ * DATA frame.
+ *
+ * The |frame| is a DATA frame to send.  The |framehd| is the
+ * serialized frame header (9 bytes). The |length| is the length of
+ * application data to send (this does not include padding).  The
+ * |source| is the same pointer passed to
+ * :type:`nghttp2_data_source_read_callback`.
+ *
+ * The application first must send frame header |framehd| of length 9
+ * bytes.  If ``frame->data.padlen > 0``, send 1 byte of value
+ * ``frame->data.padlen - 1``.  Then send exactly |length| bytes of
+ * application data.  Finally, if ``frame->data.padlen > 1``, send
+ * ``frame->data.padlen - 1`` bytes of zero as padding.
+ *
+ * The application has to send complete DATA frame in this callback.
+ * If all data were written successfully, return 0.
+ *
+ * If it cannot send any data at all, just return
+ * :enum:`NGHTTP2_ERR_WOULDBLOCK`; the library will call this callback
+ * with the same parameters later (It is recommended to send complete
+ * DATA frame at once in this function to deal with error; if partial
+ * frame data has already sent, it is impossible to send another data
+ * in that state, and all we can do is tear down connection).  When
+ * data is fully processed, but application wants to make
+ * `nghttp2_session_mem_send()` or `nghttp2_session_send()` return
+ * immediately without processing next frames, return
+ * :enum:`NGHTTP2_ERR_PAUSE`.  If application decided to reset this
+ * stream, return :enum:`NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE`, then
+ * the library will send RST_STREAM with INTERNAL_ERROR as error code.
+ * The application can also return
+ * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`, which will result in
+ * connection closure.  Returning any other value is treated as
+ * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE` is returned.
+ */
+typedef int (*nghttp2_send_data_callback)(nghttp2_session *session,
+                                          nghttp2_frame *frame,
+                                          const uint8_t *framehd, size_t length,
+                                          nghttp2_data_source *source,
+                                          void *user_data);
+
+/**
+ * @functypedef
+ *
+ * Callback function invoked when |session| wants to receive data from
+ * the remote peer.  The implementation of this function must read at
+ * most |length| bytes of data and store it in |buf|.  The |flags| is
+ * currently not used and always 0.  It must return the number of
+ * bytes written in |buf| if it succeeds.  If it cannot read any
+ * single byte without blocking, it must return
+ * :enum:`NGHTTP2_ERR_WOULDBLOCK`.  If it gets EOF before it reads any
+ * single byte, it must return :enum:`NGHTTP2_ERR_EOF`.  For other
+ * errors, it must return :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`.
+ * Returning 0 is treated as :enum:`NGHTTP2_ERR_WOULDBLOCK`.  The
+ * |user_data| pointer is the third argument passed in to the call to
+ * `nghttp2_session_client_new()` or `nghttp2_session_server_new()`.
+ *
+ * This callback is required if the application uses
+ * `nghttp2_session_recv()` to receive data from the remote endpoint.
+ * If the application uses solely `nghttp2_session_mem_recv()`
+ * instead, this callback function is unnecessary.
+ *
+ * To set this callback to :type:`nghttp2_session_callbacks`, use
+ * `nghttp2_session_callbacks_set_recv_callback()`.
+ */
+typedef ssize_t (*nghttp2_recv_callback)(nghttp2_session *session, uint8_t *buf,
+                                         size_t length, int flags,
+                                         void *user_data);
+
+/**
+ * @functypedef
+ *
+ * Callback function invoked by `nghttp2_session_recv()` and
+ * `nghttp2_session_mem_recv()` when a frame is received.  The
+ * |user_data| pointer is the third argument passed in to the call to
+ * `nghttp2_session_client_new()` or `nghttp2_session_server_new()`.
+ *
+ * If frame is HEADERS or PUSH_PROMISE, the ``nva`` and ``nvlen``
+ * member of their data structure are always ``NULL`` and 0
+ * respectively.  The header name/value pairs are emitted via
+ * :type:`nghttp2_on_header_callback`.
+ *
+ * For HEADERS, PUSH_PROMISE and DATA frames, this callback may be
+ * called after stream is closed (see
+ * :type:`nghttp2_on_stream_close_callback`).  The application should
+ * check that stream is still alive using its own stream management or
+ * :func:`nghttp2_session_get_stream_user_data()`.
+ *
+ * Only HEADERS and DATA frame can signal the end of incoming data.
+ * If ``frame->hd.flags & NGHTTP2_FLAG_END_STREAM`` is nonzero, the
+ * |frame| is the last frame from the remote peer in this stream.
+ *
+ * This callback won't be called for CONTINUATION frames.
+ * HEADERS/PUSH_PROMISE + CONTINUATIONs are treated as single frame.
+ *
+ * The implementation of this function must return 0 if it succeeds.
+ * If nonzero value is returned, it is treated as fatal error and
+ * `nghttp2_session_recv()` and `nghttp2_session_mem_recv()` functions
+ * immediately return :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`.
+ *
+ * To set this callback to :type:`nghttp2_session_callbacks`, use
+ * `nghttp2_session_callbacks_set_on_frame_recv_callback()`.
+ */
+typedef int (*nghttp2_on_frame_recv_callback)(nghttp2_session *session,
+                                              const nghttp2_frame *frame,
+                                              void *user_data);
+
+/**
+ * @functypedef
+ *
+ * Callback function invoked by `nghttp2_session_recv()` and
+ * `nghttp2_session_mem_recv()` when an invalid non-DATA frame is
+ * received.  The error is indicated by the |lib_error_code|, which is
+ * one of the values defined in :type:`nghttp2_error`.  When this
+ * callback function is invoked, the library automatically submits
+ * either RST_STREAM or GOAWAY frame.  The |user_data| pointer is the
+ * third argument passed in to the call to
+ * `nghttp2_session_client_new()` or `nghttp2_session_server_new()`.
+ *
+ * If frame is HEADERS or PUSH_PROMISE, the ``nva`` and ``nvlen``
+ * member of their data structure are always ``NULL`` and 0
+ * respectively.
+ *
+ * The implementation of this function must return 0 if it succeeds.
+ * If nonzero is returned, it is treated as fatal error and
+ * `nghttp2_session_recv()` and `nghttp2_session_mem_recv()` functions
+ * immediately return :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`.
+ *
+ * To set this callback to :type:`nghttp2_session_callbacks`, use
+ * `nghttp2_session_callbacks_set_on_invalid_frame_recv_callback()`.
+ */
+typedef int (*nghttp2_on_invalid_frame_recv_callback)(
+    nghttp2_session *session, const nghttp2_frame *frame, int lib_error_code,
+    void *user_data);
+
+/**
+ * @functypedef
+ *
+ * Callback function invoked when a chunk of data in DATA frame is
+ * received.  The |stream_id| is the stream ID this DATA frame belongs
+ * to.  The |flags| is the flags of DATA frame which this data chunk
+ * is contained.  ``(flags & NGHTTP2_FLAG_END_STREAM) != 0`` does not
+ * necessarily mean this chunk of data is the last one in the stream.
+ * You should use :type:`nghttp2_on_frame_recv_callback` to know all
+ * data frames are received.  The |user_data| pointer is the third
+ * argument passed in to the call to `nghttp2_session_client_new()` or
+ * `nghttp2_session_server_new()`.
+ *
+ * If the application uses `nghttp2_session_mem_recv()`, it can return
+ * :enum:`NGHTTP2_ERR_PAUSE` to make `nghttp2_session_mem_recv()`
+ * return without processing further input bytes.  The memory by
+ * pointed by the |data| is retained until
+ * `nghttp2_session_mem_recv()` or `nghttp2_session_recv()` is called.
+ * The application must retain the input bytes which was used to
+ * produce the |data| parameter, because it may refer to the memory
+ * region included in the input bytes.
+ *
+ * The implementation of this function must return 0 if it succeeds.
+ * If nonzero is returned, it is treated as fatal error, and
+ * `nghttp2_session_recv()` and `nghttp2_session_mem_recv()` functions
+ * immediately return :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`.
+ *
+ * To set this callback to :type:`nghttp2_session_callbacks`, use
+ * `nghttp2_session_callbacks_set_on_data_chunk_recv_callback()`.
+ */
+typedef int (*nghttp2_on_data_chunk_recv_callback)(nghttp2_session *session,
+                                                   uint8_t flags,
+                                                   int32_t stream_id,
+                                                   const uint8_t *data,
+                                                   size_t len, void *user_data);
+
+/**
+ * @functypedef
+ *
+ * Callback function invoked just before the non-DATA frame |frame| is
+ * sent.  The |user_data| pointer is the third argument passed in to
+ * the call to `nghttp2_session_client_new()` or
+ * `nghttp2_session_server_new()`.
+ *
+ * The implementation of this function must return 0 if it succeeds.
+ * It can also return :enum:`NGHTTP2_ERR_CANCEL` to cancel the
+ * transmission of the given frame.
+ *
+ * If there is a fatal error while executing this callback, the
+ * implementation should return :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`,
+ * which makes `nghttp2_session_send()` and
+ * `nghttp2_session_mem_send()` functions immediately return
+ * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`.
+ *
+ * If the other value is returned, it is treated as if
+ * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE` is returned.  But the
+ * implementation should not rely on this since the library may define
+ * new return value to extend its capability.
+ *
+ * To set this callback to :type:`nghttp2_session_callbacks`, use
+ * `nghttp2_session_callbacks_set_before_frame_send_callback()`.
+ */
+typedef int (*nghttp2_before_frame_send_callback)(nghttp2_session *session,
+                                                  const nghttp2_frame *frame,
+                                                  void *user_data);
+
+/**
+ * @functypedef
+ *
+ * Callback function invoked after the frame |frame| is sent.  The
+ * |user_data| pointer is the third argument passed in to the call to
+ * `nghttp2_session_client_new()` or `nghttp2_session_server_new()`.
+ *
+ * The implementation of this function must return 0 if it succeeds.
+ * If nonzero is returned, it is treated as fatal error and
+ * `nghttp2_session_send()` and `nghttp2_session_mem_send()` functions
+ * immediately return :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`.
+ *
+ * To set this callback to :type:`nghttp2_session_callbacks`, use
+ * `nghttp2_session_callbacks_set_on_frame_send_callback()`.
+ */
+typedef int (*nghttp2_on_frame_send_callback)(nghttp2_session *session,
+                                              const nghttp2_frame *frame,
+                                              void *user_data);
+
+/**
+ * @functypedef
+ *
+ * Callback function invoked after the non-DATA frame |frame| is not
+ * sent because of the error.  The error is indicated by the
+ * |lib_error_code|, which is one of the values defined in
+ * :type:`nghttp2_error`.  The |user_data| pointer is the third
+ * argument passed in to the call to `nghttp2_session_client_new()` or
+ * `nghttp2_session_server_new()`.
+ *
+ * The implementation of this function must return 0 if it succeeds.
+ * If nonzero is returned, it is treated as fatal error and
+ * `nghttp2_session_send()` and `nghttp2_session_mem_send()` functions
+ * immediately return :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`.
+ *
+ * `nghttp2_session_get_stream_user_data()` can be used to get
+ * associated data.
+ *
+ * To set this callback to :type:`nghttp2_session_callbacks`, use
+ * `nghttp2_session_callbacks_set_on_frame_not_send_callback()`.
+ */
+typedef int (*nghttp2_on_frame_not_send_callback)(nghttp2_session *session,
+                                                  const nghttp2_frame *frame,
+                                                  int lib_error_code,
+                                                  void *user_data);
+
+/**
+ * @functypedef
+ *
+ * Callback function invoked when the stream |stream_id| is closed.
+ * The reason of closure is indicated by the |error_code|.  The
+ * |error_code| is usually one of :enum:`nghttp2_error_code`, but that
+ * is not guaranteed.  The stream_user_data, which was specified in
+ * `nghttp2_submit_request()` or `nghttp2_submit_headers()`, is still
+ * available in this function.  The |user_data| pointer is the third
+ * argument passed in to the call to `nghttp2_session_client_new()` or
+ * `nghttp2_session_server_new()`.
+ *
+ * This function is also called for a stream in reserved state.
+ *
+ * The implementation of this function must return 0 if it succeeds.
+ * If nonzero is returned, it is treated as fatal error and
+ * `nghttp2_session_recv()`, `nghttp2_session_mem_recv()`,
+ * `nghttp2_session_send()`, and `nghttp2_session_mem_send()`
+ * functions immediately return :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`.
+ *
+ * To set this callback to :type:`nghttp2_session_callbacks`, use
+ * `nghttp2_session_callbacks_set_on_stream_close_callback()`.
+ */
+typedef int (*nghttp2_on_stream_close_callback)(nghttp2_session *session,
+                                                int32_t stream_id,
+                                                uint32_t error_code,
+                                                void *user_data);
+
+/**
+ * @functypedef
+ *
+ * Callback function invoked when the reception of header block in
+ * HEADERS or PUSH_PROMISE is started.  Each header name/value pair
+ * will be emitted by :type:`nghttp2_on_header_callback`.
+ *
+ * The ``frame->hd.flags`` may not have
+ * :enum:`NGHTTP2_FLAG_END_HEADERS` flag set, which indicates that one
+ * or more CONTINUATION frames are involved.  But the application does
+ * not need to care about that because the header name/value pairs are
+ * emitted transparently regardless of CONTINUATION frames.
+ *
+ * The server applications probably create an object to store
+ * information about new stream if ``frame->hd.type ==
+ * NGHTTP2_HEADERS`` and ``frame->headers.cat ==
+ * NGHTTP2_HCAT_REQUEST``.  If |session| is configured as server side,
+ * ``frame->headers.cat`` is either ``NGHTTP2_HCAT_REQUEST``
+ * containing request headers or ``NGHTTP2_HCAT_HEADERS`` containing
+ * trailer fields and never get PUSH_PROMISE in this callback.
+ *
+ * For the client applications, ``frame->hd.type`` is either
+ * ``NGHTTP2_HEADERS`` or ``NGHTTP2_PUSH_PROMISE``.  In case of
+ * ``NGHTTP2_HEADERS``, ``frame->headers.cat ==
+ * NGHTTP2_HCAT_RESPONSE`` means that it is the first response
+ * headers, but it may be non-final response which is indicated by 1xx
+ * status code.  In this case, there may be zero or more HEADERS frame
+ * with ``frame->headers.cat == NGHTTP2_HCAT_HEADERS`` which has
+ * non-final response code and finally client gets exactly one HEADERS
+ * frame with ``frame->headers.cat == NGHTTP2_HCAT_HEADERS``
+ * containing final response headers (non-1xx status code).  The
+ * trailer fields also has ``frame->headers.cat ==
+ * NGHTTP2_HCAT_HEADERS`` which does not contain any status code.
+ *
+ * Returning :enum:`NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE` will close
+ * the stream (promised stream if frame is PUSH_PROMISE) by issuing
+ * RST_STREAM with :enum:`NGHTTP2_INTERNAL_ERROR`.  In this case,
+ * :type:`nghttp2_on_header_callback` and
+ * :type:`nghttp2_on_frame_recv_callback` will not be invoked.  If a
+ * different error code is desirable, use
+ * `nghttp2_submit_rst_stream()` with a desired error code and then
+ * return :enum:`NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE`.  Again, use
+ * ``frame->push_promise.promised_stream_id`` as stream_id parameter
+ * in `nghttp2_submit_rst_stream()` if frame is PUSH_PROMISE.
+ *
+ * The implementation of this function must return 0 if it succeeds.
+ * It can return :enum:`NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE` to
+ * reset the stream (promised stream if frame is PUSH_PROMISE).  For
+ * critical errors, it must return
+ * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`.  If the other value is
+ * returned, it is treated as if :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`
+ * is returned.  If :enum:`NGHTTP2_ERR_CALLBACK_FAILURE` is returned,
+ * `nghttp2_session_mem_recv()` function will immediately return
+ * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`.
+ *
+ * To set this callback to :type:`nghttp2_session_callbacks`, use
+ * `nghttp2_session_callbacks_set_on_begin_headers_callback()`.
+ */
+typedef int (*nghttp2_on_begin_headers_callback)(nghttp2_session *session,
+                                                 const nghttp2_frame *frame,
+                                                 void *user_data);
+
+/**
+ * @functypedef
+ *
+ * Callback function invoked when a header name/value pair is received
+ * for the |frame|.  The |name| of length |namelen| is header name.
+ * The |value| of length |valuelen| is header value.  The |flags| is
+ * bitwise OR of one or more of :type:`nghttp2_nv_flag`.
+ *
+ * If :enum:`NGHTTP2_NV_FLAG_NO_INDEX` is set in |flags|, the receiver
+ * must not index this name/value pair when forwarding it to the next
+ * hop.  More specifically, "Literal Header Field never Indexed"
+ * representation must be used in HPACK encoding.
+ *
+ * When this callback is invoked, ``frame->hd.type`` is either
+ * :enum:`NGHTTP2_HEADERS` or :enum:`NGHTTP2_PUSH_PROMISE`.  After all
+ * header name/value pairs are processed with this callback, and no
+ * error has been detected, :type:`nghttp2_on_frame_recv_callback`
+ * will be invoked.  If there is an error in decompression,
+ * :type:`nghttp2_on_frame_recv_callback` for the |frame| will not be
+ * invoked.
+ *
+ * Both |name| and |value| are guaranteed to be NULL-terminated.  The
+ * |namelen| and |valuelen| do not include terminal NULL.  If
+ * `nghttp2_option_set_no_http_messaging()` is used with nonzero
+ * value, NULL character may be included in |name| or |value| before
+ * terminating NULL.
+ *
+ * Please note that unless `nghttp2_option_set_no_http_messaging()` is
+ * used, nghttp2 library does perform validation against the |name|
+ * and the |value| using `nghttp2_check_header_name()` and
+ * `nghttp2_check_header_value()`.  In addition to this, nghttp2
+ * performs validation based on HTTP Messaging rule, which is briefly
+ * explained in :ref:`http-messaging` section.
+ *
+ * If the application uses `nghttp2_session_mem_recv()`, it can return
+ * :enum:`NGHTTP2_ERR_PAUSE` to make `nghttp2_session_mem_recv()`
+ * return without processing further input bytes.  The memory pointed
+ * by |frame|, |name| and |value| parameters are retained until
+ * `nghttp2_session_mem_recv()` or `nghttp2_session_recv()` is called.
+ * The application must retain the input bytes which was used to
+ * produce these parameters, because it may refer to the memory region
+ * included in the input bytes.
+ *
+ * Returning :enum:`NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE` will close
+ * the stream (promised stream if frame is PUSH_PROMISE) by issuing
+ * RST_STREAM with :enum:`NGHTTP2_INTERNAL_ERROR`.  In this case,
+ * :type:`nghttp2_on_header_callback` and
+ * :type:`nghttp2_on_frame_recv_callback` will not be invoked.  If a
+ * different error code is desirable, use
+ * `nghttp2_submit_rst_stream()` with a desired error code and then
+ * return :enum:`NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE`.  Again, use
+ * ``frame->push_promise.promised_stream_id`` as stream_id parameter
+ * in `nghttp2_submit_rst_stream()` if frame is PUSH_PROMISE.
+ *
+ * The implementation of this function must return 0 if it succeeds.
+ * It may return :enum:`NGHTTP2_ERR_PAUSE` or
+ * :enum:`NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE`.  For other critical
+ * failures, it must return :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`.  If
+ * the other nonzero value is returned, it is treated as
+ * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`.  If
+ * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE` is returned,
+ * `nghttp2_session_recv()` and `nghttp2_session_mem_recv()` functions
+ * immediately return :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`.
+ *
+ * To set this callback to :type:`nghttp2_session_callbacks`, use
+ * `nghttp2_session_callbacks_set_on_header_callback()`.
+ *
+ * .. warning::
+ *
+ *   Application should properly limit the total buffer size to store
+ *   incoming header fields.  Without it, peer may send large number
+ *   of header fields or large header fields to cause out of memory in
+ *   local endpoint.  Due to how HPACK works, peer can do this
+ *   effectively without using much memory on their own.
+ */
+typedef int (*nghttp2_on_header_callback)(nghttp2_session *session,
+                                          const nghttp2_frame *frame,
+                                          const uint8_t *name, size_t namelen,
+                                          const uint8_t *value, size_t valuelen,
+                                          uint8_t flags, void *user_data);
+
+/**
+ * @functypedef
+ *
+ * Callback function invoked when a header name/value pair is received
+ * for the |frame|.  The |name| is header name.  The |value| is header
+ * value.  The |flags| is bitwise OR of one or more of
+ * :type:`nghttp2_nv_flag`.
+ *
+ * This callback behaves like :type:`nghttp2_on_header_callback`,
+ * except that |name| and |value| are stored in reference counted
+ * buffer.  If application wishes to keep these references without
+ * copying them, use `nghttp2_rcbuf_incref()` to increment their
+ * reference count.  It is the application's responsibility to call
+ * `nghttp2_rcbuf_decref()` if they called `nghttp2_rcbuf_incref()` so
+ * as not to leak memory.  If the |session| is created by
+ * `nghttp2_session_server_new3()` or `nghttp2_session_client_new3()`,
+ * the function to free memory is the one belongs to the mem
+ * parameter.  As long as this free function alives, |name| and
+ * |value| can live after |session| was destroyed.
+ */
+typedef int (*nghttp2_on_header_callback2)(nghttp2_session *session,
+                                           const nghttp2_frame *frame,
+                                           nghttp2_rcbuf *name,
+                                           nghttp2_rcbuf *value, uint8_t flags,
+                                           void *user_data);
+
+/**
+ * @functypedef
+ *
+ * Callback function invoked when a invalid header name/value pair is
+ * received for the |frame|.
+ *
+ * The parameter and behaviour are similar to
+ * :type:`nghttp2_on_header_callback`.  The difference is that this
+ * callback is only invoked when a invalid header name/value pair is
+ * received which is treated as stream error if this callback is not
+ * set.  Only invalid regular header field are passed to this
+ * callback.  In other words, invalid pseudo header field is not
+ * passed to this callback.  Also header fields which includes upper
+ * cased latter are also treated as error without passing them to this
+ * callback.
+ *
+ * This callback is only considered if HTTP messaging validation is
+ * turned on (which is on by default, see
+ * `nghttp2_option_set_no_http_messaging()`).
+ *
+ * With this callback, application inspects the incoming invalid
+ * field, and it also can reset stream from this callback by returning
+ * :enum:`NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE`.  By default, the
+ * error code is :enum:`NGHTTP2_PROTOCOL_ERROR`.  To change the error
+ * code, call `nghttp2_submit_rst_stream()` with the error code of
+ * choice in addition to returning
+ * :enum:`NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE`.
+ *
+ * If 0 is returned, the header field is ignored, and the stream is
+ * not reset.
+ */
+typedef int (*nghttp2_on_invalid_header_callback)(
+    nghttp2_session *session, const nghttp2_frame *frame, const uint8_t *name,
+    size_t namelen, const uint8_t *value, size_t valuelen, uint8_t flags,
+    void *user_data);
+
+/**
+ * @functypedef
+ *
+ * Callback function invoked when a invalid header name/value pair is
+ * received for the |frame|.
+ *
+ * The parameter and behaviour are similar to
+ * :type:`nghttp2_on_header_callback2`.  The difference is that this
+ * callback is only invoked when a invalid header name/value pair is
+ * received which is silently ignored if this callback is not set.
+ * Only invalid regular header field are passed to this callback.  In
+ * other words, invalid pseudo header field is not passed to this
+ * callback.  Also header fields which includes upper cased latter are
+ * also treated as error without passing them to this callback.
+ *
+ * This callback is only considered if HTTP messaging validation is
+ * turned on (which is on by default, see
+ * `nghttp2_option_set_no_http_messaging()`).
+ *
+ * With this callback, application inspects the incoming invalid
+ * field, and it also can reset stream from this callback by returning
+ * :enum:`NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE`.  By default, the
+ * error code is :enum:`NGHTTP2_INTERNAL_ERROR`.  To change the error
+ * code, call `nghttp2_submit_rst_stream()` with the error code of
+ * choice in addition to returning
+ * :enum:`NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE`.
+ */
+typedef int (*nghttp2_on_invalid_header_callback2)(
+    nghttp2_session *session, const nghttp2_frame *frame, nghttp2_rcbuf *name,
+    nghttp2_rcbuf *value, uint8_t flags, void *user_data);
+
+/**
+ * @functypedef
+ *
+ * Callback function invoked when the library asks application how
+ * many padding bytes are required for the transmission of the
+ * |frame|.  The application must choose the total length of payload
+ * including padded bytes in range [frame->hd.length, max_payloadlen],
+ * inclusive.  Choosing number not in this range will be treated as
+ * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`.  Returning
+ * ``frame->hd.length`` means no padding is added.  Returning
+ * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE` will make
+ * `nghttp2_session_send()` and `nghttp2_session_mem_send()` functions
+ * immediately return :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`.
+ *
+ * To set this callback to :type:`nghttp2_session_callbacks`, use
+ * `nghttp2_session_callbacks_set_select_padding_callback()`.
+ */
+typedef ssize_t (*nghttp2_select_padding_callback)(nghttp2_session *session,
+                                                   const nghttp2_frame *frame,
+                                                   size_t max_payloadlen,
+                                                   void *user_data);
+
+/**
+ * @functypedef
+ *
+ * Callback function invoked when library wants to get max length of
+ * data to send data to the remote peer.  The implementation of this
+ * function should return a value in the following range.  [1,
+ * min(|session_remote_window_size|, |stream_remote_window_size|,
+ * |remote_max_frame_size|)].  If a value greater than this range is
+ * returned than the max allow value will be used.  Returning a value
+ * smaller than this range is treated as
+ * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`.  The |frame_type| is provided
+ * for future extensibility and identifies the type of frame (see
+ * :type:`nghttp2_frame_type`) for which to get the length for.
+ * Currently supported frame types are: :enum:`NGHTTP2_DATA`.
+ *
+ * This callback can be used to control the length in bytes for which
+ * :type:`nghttp2_data_source_read_callback` is allowed to send to the
+ * remote endpoint.  This callback is optional.  Returning
+ * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE` will signal the entire session
+ * failure.
+ *
+ * To set this callback to :type:`nghttp2_session_callbacks`, use
+ * `nghttp2_session_callbacks_set_data_source_read_length_callback()`.
+ */
+typedef ssize_t (*nghttp2_data_source_read_length_callback)(
+    nghttp2_session *session, uint8_t frame_type, int32_t stream_id,
+    int32_t session_remote_window_size, int32_t stream_remote_window_size,
+    uint32_t remote_max_frame_size, void *user_data);
+
+/**
+ * @functypedef
+ *
+ * Callback function invoked when a frame header is received.  The
+ * |hd| points to received frame header.
+ *
+ * Unlike :type:`nghttp2_on_frame_recv_callback`, this callback will
+ * also be called when frame header of CONTINUATION frame is received.
+ *
+ * If both :type:`nghttp2_on_begin_frame_callback` and
+ * :type:`nghttp2_on_begin_headers_callback` are set and HEADERS or
+ * PUSH_PROMISE is received, :type:`nghttp2_on_begin_frame_callback`
+ * will be called first.
+ *
+ * The implementation of this function must return 0 if it succeeds.
+ * If nonzero value is returned, it is treated as fatal error and
+ * `nghttp2_session_recv()` and `nghttp2_session_mem_recv()` functions
+ * immediately return :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`.
+ *
+ * To set this callback to :type:`nghttp2_session_callbacks`, use
+ * `nghttp2_session_callbacks_set_on_begin_frame_callback()`.
+ */
+typedef int (*nghttp2_on_begin_frame_callback)(nghttp2_session *session,
+                                               const nghttp2_frame_hd *hd,
+                                               void *user_data);
+
+/**
+ * @functypedef
+ *
+ * Callback function invoked when chunk of extension frame payload is
+ * received.  The |hd| points to frame header.  The received
+ * chunk is |data| of length |len|.
+ *
+ * The implementation of this function must return 0 if it succeeds.
+ *
+ * To abort processing this extension frame, return
+ * :enum:`NGHTTP2_ERR_CANCEL`.
+ *
+ * If fatal error occurred, application should return
+ * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`.  In this case,
+ * `nghttp2_session_recv()` and `nghttp2_session_mem_recv()` functions
+ * immediately return :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`.  If the
+ * other values are returned, currently they are treated as
+ * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`.
+ */
+typedef int (*nghttp2_on_extension_chunk_recv_callback)(
+    nghttp2_session *session, const nghttp2_frame_hd *hd, const uint8_t *data,
+    size_t len, void *user_data);
+
+/**
+ * @functypedef
+ *
+ * Callback function invoked when library asks the application to
+ * unpack extension payload from its wire format.  The extension
+ * payload has been passed to the application using
+ * :type:`nghttp2_on_extension_chunk_recv_callback`.  The frame header
+ * is already unpacked by the library and provided as |hd|.
+ *
+ * To receive extension frames, the application must tell desired
+ * extension frame type to the library using
+ * `nghttp2_option_set_user_recv_extension_type()`.
+ *
+ * The implementation of this function may store the pointer to the
+ * created object as a result of unpacking in |*payload|, and returns
+ * 0.  The pointer stored in |*payload| is opaque to the library, and
+ * the library does not own its pointer.  |*payload| is initialized as
+ * ``NULL``.  The |*payload| is available as ``frame->ext.payload`` in
+ * :type:`nghttp2_on_frame_recv_callback`.  Therefore if application
+ * can free that memory inside :type:`nghttp2_on_frame_recv_callback`
+ * callback.  Of course, application has a liberty not ot use
+ * |*payload|, and do its own mechanism to process extension frames.
+ *
+ * To abort processing this extension frame, return
+ * :enum:`NGHTTP2_ERR_CANCEL`.
+ *
+ * If fatal error occurred, application should return
+ * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`.  In this case,
+ * `nghttp2_session_recv()` and `nghttp2_session_mem_recv()` functions
+ * immediately return :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`.  If the
+ * other values are returned, currently they are treated as
+ * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`.
+ */
+typedef int (*nghttp2_unpack_extension_callback)(nghttp2_session *session,
+                                                 void **payload,
+                                                 const nghttp2_frame_hd *hd,
+                                                 void *user_data);
+
+/**
+ * @functypedef
+ *
+ * Callback function invoked when library asks the application to pack
+ * extension payload in its wire format.  The frame header will be
+ * packed by library.  Application must pack payload only.
+ * ``frame->ext.payload`` is the object passed to
+ * `nghttp2_submit_extension()` as payload parameter.  Application
+ * must pack extension payload to the |buf| of its capacity |len|
+ * bytes.  The |len| is at least 16KiB.
+ *
+ * The implementation of this function should return the number of
+ * bytes written into |buf| when it succeeds.
+ *
+ * To abort processing this extension frame, return
+ * :enum:`NGHTTP2_ERR_CANCEL`, and
+ * :type:`nghttp2_on_frame_not_send_callback` will be invoked.
+ *
+ * If fatal error occurred, application should return
+ * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`.  In this case,
+ * `nghttp2_session_send()` and `nghttp2_session_mem_send()` functions
+ * immediately return :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`.  If the
+ * other values are returned, currently they are treated as
+ * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`.  If the return value is
+ * strictly larger than |len|, it is treated as
+ * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`.
+ */
+typedef ssize_t (*nghttp2_pack_extension_callback)(nghttp2_session *session,
+                                                   uint8_t *buf, size_t len,
+                                                   const nghttp2_frame *frame,
+                                                   void *user_data);
+
+/**
+ * @functypedef
+ *
+ * Callback function invoked when library provides the error message
+ * intended for human consumption.  This callback is solely for
+ * debugging purpose.  The |msg| is typically NULL-terminated string
+ * of length |len|.  |len| does not include the sentinel NULL
+ * character.
+ *
+ * This function is deprecated.  The new application should use
+ * :type:`nghttp2_error_callback2`.
+ *
+ * The format of error message may change between nghttp2 library
+ * versions.  The application should not depend on the particular
+ * format.
+ *
+ * Normally, application should return 0 from this callback.  If fatal
+ * error occurred while doing something in this callback, application
+ * should return :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`.  In this case,
+ * library will return immediately with return value
+ * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`.  Currently, if nonzero value
+ * is returned from this callback, they are treated as
+ * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`, but application should not
+ * rely on this details.
+ */
+typedef int (*nghttp2_error_callback)(nghttp2_session *session, const char *msg,
+                                      size_t len, void *user_data);
+
+/**
+ * @functypedef
+ *
+ * Callback function invoked when library provides the error code, and
+ * message.  This callback is solely for debugging purpose.
+ * |lib_error_code| is one of error code defined in
+ * :enum:`nghttp2_error`.  The |msg| is typically NULL-terminated
+ * string of length |len|, and intended for human consumption.  |len|
+ * does not include the sentinel NULL character.
+ *
+ * The format of error message may change between nghttp2 library
+ * versions.  The application should not depend on the particular
+ * format.
+ *
+ * Normally, application should return 0 from this callback.  If fatal
+ * error occurred while doing something in this callback, application
+ * should return :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`.  In this case,
+ * library will return immediately with return value
+ * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`.  Currently, if nonzero value
+ * is returned from this callback, they are treated as
+ * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`, but application should not
+ * rely on this details.
+ */
+typedef int (*nghttp2_error_callback2)(nghttp2_session *session,
+                                       int lib_error_code, const char *msg,
+                                       size_t len, void *user_data);
+
+struct nghttp2_session_callbacks;
+
+/**
+ * @struct
+ *
+ * Callback functions for :type:`nghttp2_session`.  The details of
+ * this structure are intentionally hidden from the public API.
+ */
+typedef struct nghttp2_session_callbacks nghttp2_session_callbacks;
+
+/**
+ * @function
+ *
+ * Initializes |*callbacks_ptr| with NULL values.
+ *
+ * The initialized object can be used when initializing multiple
+ * :type:`nghttp2_session` objects.
+ *
+ * When the application finished using this object, it can use
+ * `nghttp2_session_callbacks_del()` to free its memory.
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * :enum:`NGHTTP2_ERR_NOMEM`
+ *     Out of memory.
+ */
+NGHTTP2_EXTERN int
+nghttp2_session_callbacks_new(nghttp2_session_callbacks **callbacks_ptr);
+
+/**
+ * @function
+ *
+ * Frees any resources allocated for |callbacks|.  If |callbacks| is
+ * ``NULL``, this function does nothing.
+ */
+NGHTTP2_EXTERN void
+nghttp2_session_callbacks_del(nghttp2_session_callbacks *callbacks);
+
+/**
+ * @function
+ *
+ * Sets callback function invoked when a session wants to send data to
+ * the remote peer.  This callback is not necessary if the application
+ * uses solely `nghttp2_session_mem_send()` to serialize data to
+ * transmit.
+ */
+NGHTTP2_EXTERN void nghttp2_session_callbacks_set_send_callback(
+    nghttp2_session_callbacks *cbs, nghttp2_send_callback send_callback);
+
+/**
+ * @function
+ *
+ * Sets callback function invoked when the a session wants to receive
+ * data from the remote peer.  This callback is not necessary if the
+ * application uses solely `nghttp2_session_mem_recv()` to process
+ * received data.
+ */
+NGHTTP2_EXTERN void nghttp2_session_callbacks_set_recv_callback(
+    nghttp2_session_callbacks *cbs, nghttp2_recv_callback recv_callback);
+
+/**
+ * @function
+ *
+ * Sets callback function invoked by `nghttp2_session_recv()` and
+ * `nghttp2_session_mem_recv()` when a frame is received.
+ */
+NGHTTP2_EXTERN void nghttp2_session_callbacks_set_on_frame_recv_callback(
+    nghttp2_session_callbacks *cbs,
+    nghttp2_on_frame_recv_callback on_frame_recv_callback);
+
+/**
+ * @function
+ *
+ * Sets callback function invoked by `nghttp2_session_recv()` and
+ * `nghttp2_session_mem_recv()` when an invalid non-DATA frame is
+ * received.
+ */
+NGHTTP2_EXTERN void
+nghttp2_session_callbacks_set_on_invalid_frame_recv_callback(
+    nghttp2_session_callbacks *cbs,
+    nghttp2_on_invalid_frame_recv_callback on_invalid_frame_recv_callback);
+
+/**
+ * @function
+ *
+ * Sets callback function invoked when a chunk of data in DATA frame
+ * is received.
+ */
+NGHTTP2_EXTERN void nghttp2_session_callbacks_set_on_data_chunk_recv_callback(
+    nghttp2_session_callbacks *cbs,
+    nghttp2_on_data_chunk_recv_callback on_data_chunk_recv_callback);
+
+/**
+ * @function
+ *
+ * Sets callback function invoked before a non-DATA frame is sent.
+ */
+NGHTTP2_EXTERN void nghttp2_session_callbacks_set_before_frame_send_callback(
+    nghttp2_session_callbacks *cbs,
+    nghttp2_before_frame_send_callback before_frame_send_callback);
+
+/**
+ * @function
+ *
+ * Sets callback function invoked after a frame is sent.
+ */
+NGHTTP2_EXTERN void nghttp2_session_callbacks_set_on_frame_send_callback(
+    nghttp2_session_callbacks *cbs,
+    nghttp2_on_frame_send_callback on_frame_send_callback);
+
+/**
+ * @function
+ *
+ * Sets callback function invoked when a non-DATA frame is not sent
+ * because of an error.
+ */
+NGHTTP2_EXTERN void nghttp2_session_callbacks_set_on_frame_not_send_callback(
+    nghttp2_session_callbacks *cbs,
+    nghttp2_on_frame_not_send_callback on_frame_not_send_callback);
+
+/**
+ * @function
+ *
+ * Sets callback function invoked when the stream is closed.
+ */
+NGHTTP2_EXTERN void nghttp2_session_callbacks_set_on_stream_close_callback(
+    nghttp2_session_callbacks *cbs,
+    nghttp2_on_stream_close_callback on_stream_close_callback);
+
+/**
+ * @function
+ *
+ * Sets callback function invoked when the reception of header block
+ * in HEADERS or PUSH_PROMISE is started.
+ */
+NGHTTP2_EXTERN void nghttp2_session_callbacks_set_on_begin_headers_callback(
+    nghttp2_session_callbacks *cbs,
+    nghttp2_on_begin_headers_callback on_begin_headers_callback);
+
+/**
+ * @function
+ *
+ * Sets callback function invoked when a header name/value pair is
+ * received.  If both
+ * `nghttp2_session_callbacks_set_on_header_callback()` and
+ * `nghttp2_session_callbacks_set_on_header_callback2()` are used to
+ * set callbacks, the latter has the precedence.
+ */
+NGHTTP2_EXTERN void nghttp2_session_callbacks_set_on_header_callback(
+    nghttp2_session_callbacks *cbs,
+    nghttp2_on_header_callback on_header_callback);
+
+/**
+ * @function
+ *
+ * Sets callback function invoked when a header name/value pair is
+ * received.
+ */
+NGHTTP2_EXTERN void nghttp2_session_callbacks_set_on_header_callback2(
+    nghttp2_session_callbacks *cbs,
+    nghttp2_on_header_callback2 on_header_callback2);
+
+/**
+ * @function
+ *
+ * Sets callback function invoked when a invalid header name/value
+ * pair is received.  If both
+ * `nghttp2_session_callbacks_set_on_invalid_header_callback()` and
+ * `nghttp2_session_callbacks_set_on_invalid_header_callback2()` are
+ * used to set callbacks, the latter takes the precedence.
+ */
+NGHTTP2_EXTERN void nghttp2_session_callbacks_set_on_invalid_header_callback(
+    nghttp2_session_callbacks *cbs,
+    nghttp2_on_invalid_header_callback on_invalid_header_callback);
+
+/**
+ * @function
+ *
+ * Sets callback function invoked when a invalid header name/value
+ * pair is received.
+ */
+NGHTTP2_EXTERN void nghttp2_session_callbacks_set_on_invalid_header_callback2(
+    nghttp2_session_callbacks *cbs,
+    nghttp2_on_invalid_header_callback2 on_invalid_header_callback2);
+
+/**
+ * @function
+ *
+ * Sets callback function invoked when the library asks application
+ * how many padding bytes are required for the transmission of the
+ * given frame.
+ */
+NGHTTP2_EXTERN void nghttp2_session_callbacks_set_select_padding_callback(
+    nghttp2_session_callbacks *cbs,
+    nghttp2_select_padding_callback select_padding_callback);
+
+/**
+ * @function
+ *
+ * Sets callback function determine the length allowed in
+ * :type:`nghttp2_data_source_read_callback`.
+ */
+NGHTTP2_EXTERN void
+nghttp2_session_callbacks_set_data_source_read_length_callback(
+    nghttp2_session_callbacks *cbs,
+    nghttp2_data_source_read_length_callback data_source_read_length_callback);
+
+/**
+ * @function
+ *
+ * Sets callback function invoked when a frame header is received.
+ */
+NGHTTP2_EXTERN void nghttp2_session_callbacks_set_on_begin_frame_callback(
+    nghttp2_session_callbacks *cbs,
+    nghttp2_on_begin_frame_callback on_begin_frame_callback);
+
+/**
+ * @function
+ *
+ * Sets callback function invoked when
+ * :enum:`NGHTTP2_DATA_FLAG_NO_COPY` is used in
+ * :type:`nghttp2_data_source_read_callback` to avoid data copy.
+ */
+NGHTTP2_EXTERN void nghttp2_session_callbacks_set_send_data_callback(
+    nghttp2_session_callbacks *cbs,
+    nghttp2_send_data_callback send_data_callback);
+
+/**
+ * @function
+ *
+ * Sets callback function invoked when the library asks the
+ * application to pack extension frame payload in wire format.
+ */
+NGHTTP2_EXTERN void nghttp2_session_callbacks_set_pack_extension_callback(
+    nghttp2_session_callbacks *cbs,
+    nghttp2_pack_extension_callback pack_extension_callback);
+
+/**
+ * @function
+ *
+ * Sets callback function invoked when the library asks the
+ * application to unpack extension frame payload from wire format.
+ */
+NGHTTP2_EXTERN void nghttp2_session_callbacks_set_unpack_extension_callback(
+    nghttp2_session_callbacks *cbs,
+    nghttp2_unpack_extension_callback unpack_extension_callback);
+
+/**
+ * @function
+ *
+ * Sets callback function invoked when chunk of extension frame
+ * payload is received.
+ */
+NGHTTP2_EXTERN void
+nghttp2_session_callbacks_set_on_extension_chunk_recv_callback(
+    nghttp2_session_callbacks *cbs,
+    nghttp2_on_extension_chunk_recv_callback on_extension_chunk_recv_callback);
+
+/**
+ * @function
+ *
+ * Sets callback function invoked when library tells error message to
+ * the application.
+ *
+ * This function is deprecated.  The new application should use
+ * `nghttp2_session_callbacks_set_error_callback2()`.
+ *
+ * If both :type:`nghttp2_error_callback` and
+ * :type:`nghttp2_error_callback2` are set, the latter takes
+ * precedence.
+ */
+NGHTTP2_EXTERN void nghttp2_session_callbacks_set_error_callback(
+    nghttp2_session_callbacks *cbs, nghttp2_error_callback error_callback);
+
+/**
+ * @function
+ *
+ * Sets callback function invoked when library tells error code, and
+ * message to the application.
+ *
+ * If both :type:`nghttp2_error_callback` and
+ * :type:`nghttp2_error_callback2` are set, the latter takes
+ * precedence.
+ */
+NGHTTP2_EXTERN void nghttp2_session_callbacks_set_error_callback2(
+    nghttp2_session_callbacks *cbs, nghttp2_error_callback2 error_callback2);
+
+/**
+ * @functypedef
+ *
+ * Custom memory allocator to replace malloc().  The |mem_user_data|
+ * is the mem_user_data member of :type:`nghttp2_mem` structure.
+ */
+typedef void *(*nghttp2_malloc)(size_t size, void *mem_user_data);
+
+/**
+ * @functypedef
+ *
+ * Custom memory allocator to replace free().  The |mem_user_data| is
+ * the mem_user_data member of :type:`nghttp2_mem` structure.
+ */
+typedef void (*nghttp2_free)(void *ptr, void *mem_user_data);
+
+/**
+ * @functypedef
+ *
+ * Custom memory allocator to replace calloc().  The |mem_user_data|
+ * is the mem_user_data member of :type:`nghttp2_mem` structure.
+ */
+typedef void *(*nghttp2_calloc)(size_t nmemb, size_t size, void *mem_user_data);
+
+/**
+ * @functypedef
+ *
+ * Custom memory allocator to replace realloc().  The |mem_user_data|
+ * is the mem_user_data member of :type:`nghttp2_mem` structure.
+ */
+typedef void *(*nghttp2_realloc)(void *ptr, size_t size, void *mem_user_data);
+
+/**
+ * @struct
+ *
+ * Custom memory allocator functions and user defined pointer.  The
+ * |mem_user_data| member is passed to each allocator function.  This
+ * can be used, for example, to achieve per-session memory pool.
+ *
+ * In the following example code, ``my_malloc``, ``my_free``,
+ * ``my_calloc`` and ``my_realloc`` are the replacement of the
+ * standard allocators ``malloc``, ``free``, ``calloc`` and
+ * ``realloc`` respectively::
+ *
+ *     void *my_malloc_cb(size_t size, void *mem_user_data) {
+ *       return my_malloc(size);
+ *     }
+ *
+ *     void my_free_cb(void *ptr, void *mem_user_data) { my_free(ptr); }
+ *
+ *     void *my_calloc_cb(size_t nmemb, size_t size, void *mem_user_data) {
+ *       return my_calloc(nmemb, size);
+ *     }
+ *
+ *     void *my_realloc_cb(void *ptr, size_t size, void *mem_user_data) {
+ *       return my_realloc(ptr, size);
+ *     }
+ *
+ *     void session_new() {
+ *       nghttp2_session *session;
+ *       nghttp2_session_callbacks *callbacks;
+ *       nghttp2_mem mem = {NULL, my_malloc_cb, my_free_cb, my_calloc_cb,
+ *                          my_realloc_cb};
+ *
+ *       ...
+ *
+ *       nghttp2_session_client_new3(&session, callbacks, NULL, NULL, &mem);
+ *
+ *       ...
+ *     }
+ */
+typedef struct {
+  /**
+   * An arbitrary user supplied data.  This is passed to each
+   * allocator function.
+   */
+  void *mem_user_data;
+  /**
+   * Custom allocator function to replace malloc().
+   */
+  nghttp2_malloc malloc;
+  /**
+   * Custom allocator function to replace free().
+   */
+  nghttp2_free free;
+  /**
+   * Custom allocator function to replace calloc().
+   */
+  nghttp2_calloc calloc;
+  /**
+   * Custom allocator function to replace realloc().
+   */
+  nghttp2_realloc realloc;
+} nghttp2_mem;
+
+struct nghttp2_option;
+
+/**
+ * @struct
+ *
+ * Configuration options for :type:`nghttp2_session`.  The details of
+ * this structure are intentionally hidden from the public API.
+ */
+typedef struct nghttp2_option nghttp2_option;
+
+/**
+ * @function
+ *
+ * Initializes |*option_ptr| with default values.
+ *
+ * When the application finished using this object, it can use
+ * `nghttp2_option_del()` to free its memory.
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * :enum:`NGHTTP2_ERR_NOMEM`
+ *     Out of memory.
+ */
+NGHTTP2_EXTERN int nghttp2_option_new(nghttp2_option **option_ptr);
+
+/**
+ * @function
+ *
+ * Frees any resources allocated for |option|.  If |option| is
+ * ``NULL``, this function does nothing.
+ */
+NGHTTP2_EXTERN void nghttp2_option_del(nghttp2_option *option);
+
+/**
+ * @function
+ *
+ * This option prevents the library from sending WINDOW_UPDATE for a
+ * connection automatically.  If this option is set to nonzero, the
+ * library won't send WINDOW_UPDATE for DATA until application calls
+ * `nghttp2_session_consume()` to indicate the consumed amount of
+ * data.  Don't use `nghttp2_submit_window_update()` for this purpose.
+ * By default, this option is set to zero.
+ */
+NGHTTP2_EXTERN void
+nghttp2_option_set_no_auto_window_update(nghttp2_option *option, int val);
+
+/**
+ * @function
+ *
+ * This option sets the SETTINGS_MAX_CONCURRENT_STREAMS value of
+ * remote endpoint as if it is received in SETTINGS frame.  Without
+ * specifying this option, before the local endpoint receives
+ * SETTINGS_MAX_CONCURRENT_STREAMS in SETTINGS frame from remote
+ * endpoint, SETTINGS_MAX_CONCURRENT_STREAMS is unlimited.  This may
+ * cause problem if local endpoint submits lots of requests initially
+ * and sending them at once to the remote peer may lead to the
+ * rejection of some requests.  Specifying this option to the sensible
+ * value, say 100, may avoid this kind of issue. This value will be
+ * overwritten if the local endpoint receives
+ * SETTINGS_MAX_CONCURRENT_STREAMS from the remote endpoint.
+ */
+NGHTTP2_EXTERN void
+nghttp2_option_set_peer_max_concurrent_streams(nghttp2_option *option,
+                                               uint32_t val);
+
+/**
+ * @function
+ *
+ * By default, nghttp2 library, if configured as server, requires
+ * first 24 bytes of client magic byte string (MAGIC).  In most cases,
+ * this will simplify the implementation of server.  But sometimes
+ * server may want to detect the application protocol based on first
+ * few bytes on clear text communication.
+ *
+ * If this option is used with nonzero |val|, nghttp2 library does not
+ * handle MAGIC.  It still checks following SETTINGS frame.  This
+ * means that applications should deal with MAGIC by themselves.
+ *
+ * If this option is not used or used with zero value, if MAGIC does
+ * not match :macro:`NGHTTP2_CLIENT_MAGIC`, `nghttp2_session_recv()`
+ * and `nghttp2_session_mem_recv()` will return error
+ * :enum:`NGHTTP2_ERR_BAD_CLIENT_MAGIC`, which is fatal error.
+ */
+NGHTTP2_EXTERN void
+nghttp2_option_set_no_recv_client_magic(nghttp2_option *option, int val);
+
+/**
+ * @function
+ *
+ * By default, nghttp2 library enforces subset of HTTP Messaging rules
+ * described in `HTTP/2 specification, section 8
+ * <https://tools.ietf.org/html/rfc7540#section-8>`_.  See
+ * :ref:`http-messaging` section for details.  For those applications
+ * who use nghttp2 library as non-HTTP use, give nonzero to |val| to
+ * disable this enforcement.  Please note that disabling this feature
+ * does not change the fundamental client and server model of HTTP.
+ * That is, even if the validation is disabled, only client can send
+ * requests.
+ */
+NGHTTP2_EXTERN void nghttp2_option_set_no_http_messaging(nghttp2_option *option,
+                                                         int val);
+
+/**
+ * @function
+ *
+ * RFC 7540 does not enforce any limit on the number of incoming
+ * reserved streams (in RFC 7540 terms, streams in reserved (remote)
+ * state).  This only affects client side, since only server can push
+ * streams.  Malicious server can push arbitrary number of streams,
+ * and make client's memory exhausted.  This option can set the
+ * maximum number of such incoming streams to avoid possible memory
+ * exhaustion.  If this option is set, and pushed streams are
+ * automatically closed on reception, without calling user provided
+ * callback, if they exceed the given limit.  The default value is
+ * 200.  If session is configured as server side, this option has no
+ * effect.  Server can control the number of streams to push.
+ */
+NGHTTP2_EXTERN void
+nghttp2_option_set_max_reserved_remote_streams(nghttp2_option *option,
+                                               uint32_t val);
+
+/**
+ * @function
+ *
+ * Sets extension frame type the application is willing to handle with
+ * user defined callbacks (see
+ * :type:`nghttp2_on_extension_chunk_recv_callback` and
+ * :type:`nghttp2_unpack_extension_callback`).  The |type| is
+ * extension frame type, and must be strictly greater than 0x9.
+ * Otherwise, this function does nothing.  The application can call
+ * this function multiple times to set more than one frame type to
+ * receive.  The application does not have to call this function if it
+ * just sends extension frames.
+ */
+NGHTTP2_EXTERN void
+nghttp2_option_set_user_recv_extension_type(nghttp2_option *option,
+                                            uint8_t type);
+
+/**
+ * @function
+ *
+ * Sets extension frame type the application is willing to receive
+ * using builtin handler.  The |type| is the extension frame type to
+ * receive, and must be strictly greater than 0x9.  Otherwise, this
+ * function does nothing.  The application can call this function
+ * multiple times to set more than one frame type to receive.  The
+ * application does not have to call this function if it just sends
+ * extension frames.
+ *
+ * If same frame type is passed to both
+ * `nghttp2_option_set_builtin_recv_extension_type()` and
+ * `nghttp2_option_set_user_recv_extension_type()`, the latter takes
+ * precedence.
+ */
+NGHTTP2_EXTERN void
+nghttp2_option_set_builtin_recv_extension_type(nghttp2_option *option,
+                                               uint8_t type);
+
+/**
+ * @function
+ *
+ * This option prevents the library from sending PING frame with ACK
+ * flag set automatically when PING frame without ACK flag set is
+ * received.  If this option is set to nonzero, the library won't send
+ * PING frame with ACK flag set in the response for incoming PING
+ * frame.  The application can send PING frame with ACK flag set using
+ * `nghttp2_submit_ping()` with :enum:`NGHTTP2_FLAG_ACK` as flags
+ * parameter.
+ */
+NGHTTP2_EXTERN void nghttp2_option_set_no_auto_ping_ack(nghttp2_option *option,
+                                                        int val);
+
+/**
+ * @function
+ *
+ * This option sets the maximum length of header block (a set of
+ * header fields per one HEADERS frame) to send.  The length of a
+ * given set of header fields is calculated using
+ * `nghttp2_hd_deflate_bound()`.  The default value is 64KiB.  If
+ * application attempts to send header fields larger than this limit,
+ * the transmission of the frame fails with error code
+ * :enum:`NGHTTP2_ERR_FRAME_SIZE_ERROR`.
+ */
+NGHTTP2_EXTERN void
+nghttp2_option_set_max_send_header_block_length(nghttp2_option *option,
+                                                size_t val);
+
+/**
+ * @function
+ *
+ * This option sets the maximum dynamic table size for deflating
+ * header fields.  The default value is 4KiB.  In HTTP/2, receiver of
+ * deflated header block can specify maximum dynamic table size.  The
+ * actual maximum size is the minimum of the size receiver specified
+ * and this option value.
+ */
+NGHTTP2_EXTERN void
+nghttp2_option_set_max_deflate_dynamic_table_size(nghttp2_option *option,
+                                                  size_t val);
+
+/**
+ * @function
+ *
+ * This option prevents the library from retaining closed streams to
+ * maintain the priority tree.  If this option is set to nonzero,
+ * applications can discard closed stream completely to save memory.
+ */
+NGHTTP2_EXTERN void nghttp2_option_set_no_closed_streams(nghttp2_option *option,
+                                                         int val);
+
+/**
+ * @function
+ *
+ * Initializes |*session_ptr| for client use.  The all members of
+ * |callbacks| are copied to |*session_ptr|.  Therefore |*session_ptr|
+ * does not store |callbacks|.  The |user_data| is an arbitrary user
+ * supplied data, which will be passed to the callback functions.
+ *
+ * The :type:`nghttp2_send_callback` must be specified.  If the
+ * application code uses `nghttp2_session_recv()`, the
+ * :type:`nghttp2_recv_callback` must be specified.  The other members
+ * of |callbacks| can be ``NULL``.
+ *
+ * If this function fails, |*session_ptr| is left untouched.
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * :enum:`NGHTTP2_ERR_NOMEM`
+ *     Out of memory.
+ */
+NGHTTP2_EXTERN int
+nghttp2_session_client_new(nghttp2_session **session_ptr,
+                           const nghttp2_session_callbacks *callbacks,
+                           void *user_data);
+
+/**
+ * @function
+ *
+ * Initializes |*session_ptr| for server use.  The all members of
+ * |callbacks| are copied to |*session_ptr|. Therefore |*session_ptr|
+ * does not store |callbacks|.  The |user_data| is an arbitrary user
+ * supplied data, which will be passed to the callback functions.
+ *
+ * The :type:`nghttp2_send_callback` must be specified.  If the
+ * application code uses `nghttp2_session_recv()`, the
+ * :type:`nghttp2_recv_callback` must be specified.  The other members
+ * of |callbacks| can be ``NULL``.
+ *
+ * If this function fails, |*session_ptr| is left untouched.
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * :enum:`NGHTTP2_ERR_NOMEM`
+ *     Out of memory.
+ */
+NGHTTP2_EXTERN int
+nghttp2_session_server_new(nghttp2_session **session_ptr,
+                           const nghttp2_session_callbacks *callbacks,
+                           void *user_data);
+
+/**
+ * @function
+ *
+ * Like `nghttp2_session_client_new()`, but with additional options
+ * specified in the |option|.
+ *
+ * The |option| can be ``NULL`` and the call is equivalent to
+ * `nghttp2_session_client_new()`.
+ *
+ * This function does not take ownership |option|.  The application is
+ * responsible for freeing |option| if it finishes using the object.
+ *
+ * The library code does not refer to |option| after this function
+ * returns.
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * :enum:`NGHTTP2_ERR_NOMEM`
+ *     Out of memory.
+ */
+NGHTTP2_EXTERN int
+nghttp2_session_client_new2(nghttp2_session **session_ptr,
+                            const nghttp2_session_callbacks *callbacks,
+                            void *user_data, const nghttp2_option *option);
+
+/**
+ * @function
+ *
+ * Like `nghttp2_session_server_new()`, but with additional options
+ * specified in the |option|.
+ *
+ * The |option| can be ``NULL`` and the call is equivalent to
+ * `nghttp2_session_server_new()`.
+ *
+ * This function does not take ownership |option|.  The application is
+ * responsible for freeing |option| if it finishes using the object.
+ *
+ * The library code does not refer to |option| after this function
+ * returns.
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * :enum:`NGHTTP2_ERR_NOMEM`
+ *     Out of memory.
+ */
+NGHTTP2_EXTERN int
+nghttp2_session_server_new2(nghttp2_session **session_ptr,
+                            const nghttp2_session_callbacks *callbacks,
+                            void *user_data, const nghttp2_option *option);
+
+/**
+ * @function
+ *
+ * Like `nghttp2_session_client_new2()`, but with additional custom
+ * memory allocator specified in the |mem|.
+ *
+ * The |mem| can be ``NULL`` and the call is equivalent to
+ * `nghttp2_session_client_new2()`.
+ *
+ * This function does not take ownership |mem|.  The application is
+ * responsible for freeing |mem|.
+ *
+ * The library code does not refer to |mem| pointer after this
+ * function returns, so the application can safely free it.
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * :enum:`NGHTTP2_ERR_NOMEM`
+ *     Out of memory.
+ */
+NGHTTP2_EXTERN int nghttp2_session_client_new3(
+    nghttp2_session **session_ptr, const nghttp2_session_callbacks *callbacks,
+    void *user_data, const nghttp2_option *option, nghttp2_mem *mem);
+
+/**
+ * @function
+ *
+ * Like `nghttp2_session_server_new2()`, but with additional custom
+ * memory allocator specified in the |mem|.
+ *
+ * The |mem| can be ``NULL`` and the call is equivalent to
+ * `nghttp2_session_server_new2()`.
+ *
+ * This function does not take ownership |mem|.  The application is
+ * responsible for freeing |mem|.
+ *
+ * The library code does not refer to |mem| pointer after this
+ * function returns, so the application can safely free it.
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * :enum:`NGHTTP2_ERR_NOMEM`
+ *     Out of memory.
+ */
+NGHTTP2_EXTERN int nghttp2_session_server_new3(
+    nghttp2_session **session_ptr, const nghttp2_session_callbacks *callbacks,
+    void *user_data, const nghttp2_option *option, nghttp2_mem *mem);
+
+/**
+ * @function
+ *
+ * Frees any resources allocated for |session|.  If |session| is
+ * ``NULL``, this function does nothing.
+ */
+NGHTTP2_EXTERN void nghttp2_session_del(nghttp2_session *session);
+
+/**
+ * @function
+ *
+ * Sends pending frames to the remote peer.
+ *
+ * This function retrieves the highest prioritized frame from the
+ * outbound queue and sends it to the remote peer.  It does this as
+ * many as possible until the user callback
+ * :type:`nghttp2_send_callback` returns
+ * :enum:`NGHTTP2_ERR_WOULDBLOCK` or the outbound queue becomes empty.
+ * This function calls several callback functions which are passed
+ * when initializing the |session|.  Here is the simple time chart
+ * which tells when each callback is invoked:
+ *
+ * 1. Get the next frame to send from outbound queue.
+ *
+ * 2. Prepare transmission of the frame.
+ *
+ * 3. If the control frame cannot be sent because some preconditions
+ *    are not met (e.g., request HEADERS cannot be sent after GOAWAY),
+ *    :type:`nghttp2_on_frame_not_send_callback` is invoked.  Abort
+ *    the following steps.
+ *
+ * 4. If the frame is HEADERS, PUSH_PROMISE or DATA,
+ *    :type:`nghttp2_select_padding_callback` is invoked.
+ *
+ * 5. If the frame is request HEADERS, the stream is opened here.
+ *
+ * 6. :type:`nghttp2_before_frame_send_callback` is invoked.
+ *
+ * 7. If :enum:`NGHTTP2_ERR_CANCEL` is returned from
+ *    :type:`nghttp2_before_frame_send_callback`, the current frame
+ *    transmission is canceled, and
+ *    :type:`nghttp2_on_frame_not_send_callback` is invoked.  Abort
+ *    the following steps.
+ *
+ * 8. :type:`nghttp2_send_callback` is invoked one or more times to
+ *    send the frame.
+ *
+ * 9. :type:`nghttp2_on_frame_send_callback` is invoked.
+ *
+ * 10. If the transmission of the frame triggers closure of the
+ *     stream, the stream is closed and
+ *     :type:`nghttp2_on_stream_close_callback` is invoked.
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * :enum:`NGHTTP2_ERR_NOMEM`
+ *     Out of memory.
+ * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`
+ *     The callback function failed.
+ */
+NGHTTP2_EXTERN int nghttp2_session_send(nghttp2_session *session);
+
+/**
+ * @function
+ *
+ * Returns the serialized data to send.
+ *
+ * This function behaves like `nghttp2_session_send()` except that it
+ * does not use :type:`nghttp2_send_callback` to transmit data.
+ * Instead, it assigns the pointer to the serialized data to the
+ * |*data_ptr| and returns its length.  The other callbacks are called
+ * in the same way as they are in `nghttp2_session_send()`.
+ *
+ * If no data is available to send, this function returns 0.
+ *
+ * This function may not return all serialized data in one invocation.
+ * To get all data, call this function repeatedly until it returns 0
+ * or one of negative error codes.
+ *
+ * The assigned |*data_ptr| is valid until the next call of
+ * `nghttp2_session_mem_send()` or `nghttp2_session_send()`.
+ *
+ * The caller must send all data before sending the next chunk of
+ * data.
+ *
+ * This function returns the length of the data pointed by the
+ * |*data_ptr| if it succeeds, or one of the following negative error
+ * codes:
+ *
+ * :enum:`NGHTTP2_ERR_NOMEM`
+ *     Out of memory.
+ *
+ * .. note::
+ *
+ *   This function may produce very small byte string.  If that is the
+ *   case, and application disables Nagle algorithm (``TCP_NODELAY``),
+ *   then writing this small chunk leads to very small packet, and it
+ *   is very inefficient.  An application should be responsible to
+ *   buffer up small chunks of data as necessary to avoid this
+ *   situation.
+ */
+NGHTTP2_EXTERN ssize_t nghttp2_session_mem_send(nghttp2_session *session,
+                                                const uint8_t **data_ptr);
+
+/**
+ * @function
+ *
+ * Receives frames from the remote peer.
+ *
+ * This function receives as many frames as possible until the user
+ * callback :type:`nghttp2_recv_callback` returns
+ * :enum:`NGHTTP2_ERR_WOULDBLOCK`.  This function calls several
+ * callback functions which are passed when initializing the
+ * |session|.  Here is the simple time chart which tells when each
+ * callback is invoked:
+ *
+ * 1. :type:`nghttp2_recv_callback` is invoked one or more times to
+ *    receive frame header.
+ *
+ * 2. When frame header is received,
+ *    :type:`nghttp2_on_begin_frame_callback` is invoked.
+ *
+ * 3. If the frame is DATA frame:
+ *
+ *    1. :type:`nghttp2_recv_callback` is invoked to receive DATA
+ *       payload. For each chunk of data,
+ *       :type:`nghttp2_on_data_chunk_recv_callback` is invoked.
+ *
+ *    2. If one DATA frame is completely received,
+ *       :type:`nghttp2_on_frame_recv_callback` is invoked.  If the
+ *       reception of the frame triggers the closure of the stream,
+ *       :type:`nghttp2_on_stream_close_callback` is invoked.
+ *
+ * 4. If the frame is the control frame:
+ *
+ *    1. :type:`nghttp2_recv_callback` is invoked one or more times to
+ *       receive whole frame.
+ *
+ *    2. If the received frame is valid, then following actions are
+ *       taken.  If the frame is either HEADERS or PUSH_PROMISE,
+ *       :type:`nghttp2_on_begin_headers_callback` is invoked.  Then
+ *       :type:`nghttp2_on_header_callback` is invoked for each header
+ *       name/value pair.  For invalid header field,
+ *       :type:`nghttp2_on_invalid_header_callback` is called.  After
+ *       all name/value pairs are emitted successfully,
+ *       :type:`nghttp2_on_frame_recv_callback` is invoked.  For other
+ *       frames, :type:`nghttp2_on_frame_recv_callback` is invoked.
+ *       If the reception of the frame triggers the closure of the
+ *       stream, :type:`nghttp2_on_stream_close_callback` is invoked.
+ *
+ *    3. If the received frame is unpacked but is interpreted as
+ *       invalid, :type:`nghttp2_on_invalid_frame_recv_callback` is
+ *       invoked.
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * :enum:`NGHTTP2_ERR_EOF`
+ *     The remote peer did shutdown on the connection.
+ * :enum:`NGHTTP2_ERR_NOMEM`
+ *     Out of memory.
+ * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`
+ *     The callback function failed.
+ * :enum:`NGHTTP2_ERR_BAD_CLIENT_MAGIC`
+ *     Invalid client magic was detected.  This error only returns
+ *     when |session| was configured as server and
+ *     `nghttp2_option_set_no_recv_client_magic()` is not used with
+ *     nonzero value.
+ * :enum:`NGHTTP2_ERR_FLOODED`
+ *     Flooding was detected in this HTTP/2 session, and it must be
+ *     closed.  This is most likely caused by misbehaviour of peer.
+ */
+NGHTTP2_EXTERN int nghttp2_session_recv(nghttp2_session *session);
+
+/**
+ * @function
+ *
+ * Processes data |in| as an input from the remote endpoint.  The
+ * |inlen| indicates the number of bytes in the |in|.
+ *
+ * This function behaves like `nghttp2_session_recv()` except that it
+ * does not use :type:`nghttp2_recv_callback` to receive data; the
+ * |in| is the only data for the invocation of this function.  If all
+ * bytes are processed, this function returns.  The other callbacks
+ * are called in the same way as they are in `nghttp2_session_recv()`.
+ *
+ * In the current implementation, this function always tries to
+ * processes all input data unless either an error occurs or
+ * :enum:`NGHTTP2_ERR_PAUSE` is returned from
+ * :type:`nghttp2_on_header_callback` or
+ * :type:`nghttp2_on_data_chunk_recv_callback`.  If
+ * :enum:`NGHTTP2_ERR_PAUSE` is used, the return value includes the
+ * number of bytes which was used to produce the data or frame for the
+ * callback.
+ *
+ * This function returns the number of processed bytes, or one of the
+ * following negative error codes:
+ *
+ * :enum:`NGHTTP2_ERR_NOMEM`
+ *     Out of memory.
+ * :enum:`NGHTTP2_ERR_CALLBACK_FAILURE`
+ *     The callback function failed.
+ * :enum:`NGHTTP2_ERR_BAD_CLIENT_MAGIC`
+ *     Invalid client magic was detected.  This error only returns
+ *     when |session| was configured as server and
+ *     `nghttp2_option_set_no_recv_client_magic()` is not used with
+ *     nonzero value.
+ * :enum:`NGHTTP2_ERR_FLOODED`
+ *     Flooding was detected in this HTTP/2 session, and it must be
+ *     closed.  This is most likely caused by misbehaviour of peer.
+ */
+NGHTTP2_EXTERN ssize_t nghttp2_session_mem_recv(nghttp2_session *session,
+                                                const uint8_t *in,
+                                                size_t inlen);
+
+/**
+ * @function
+ *
+ * Puts back previously deferred DATA frame in the stream |stream_id|
+ * to the outbound queue.
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * :enum:`NGHTTP2_ERR_INVALID_ARGUMENT`
+ *     The stream does not exist; or no deferred data exist.
+ * :enum:`NGHTTP2_ERR_NOMEM`
+ *     Out of memory.
+ */
+NGHTTP2_EXTERN int nghttp2_session_resume_data(nghttp2_session *session,
+                                               int32_t stream_id);
+
+/**
+ * @function
+ *
+ * Returns nonzero value if |session| wants to receive data from the
+ * remote peer.
+ *
+ * If both `nghttp2_session_want_read()` and
+ * `nghttp2_session_want_write()` return 0, the application should
+ * drop the connection.
+ */
+NGHTTP2_EXTERN int nghttp2_session_want_read(nghttp2_session *session);
+
+/**
+ * @function
+ *
+ * Returns nonzero value if |session| wants to send data to the remote
+ * peer.
+ *
+ * If both `nghttp2_session_want_read()` and
+ * `nghttp2_session_want_write()` return 0, the application should
+ * drop the connection.
+ */
+NGHTTP2_EXTERN int nghttp2_session_want_write(nghttp2_session *session);
+
+/**
+ * @function
+ *
+ * Returns stream_user_data for the stream |stream_id|.  The
+ * stream_user_data is provided by `nghttp2_submit_request()`,
+ * `nghttp2_submit_headers()` or
+ * `nghttp2_session_set_stream_user_data()`.  Unless it is set using
+ * `nghttp2_session_set_stream_user_data()`, if the stream is
+ * initiated by the remote endpoint, stream_user_data is always
+ * ``NULL``.  If the stream does not exist, this function returns
+ * ``NULL``.
+ */
+NGHTTP2_EXTERN void *
+nghttp2_session_get_stream_user_data(nghttp2_session *session,
+                                     int32_t stream_id);
+
+/**
+ * @function
+ *
+ * Sets the |stream_user_data| to the stream denoted by the
+ * |stream_id|.  If a stream user data is already set to the stream,
+ * it is replaced with the |stream_user_data|.  It is valid to specify
+ * ``NULL`` in the |stream_user_data|, which nullifies the associated
+ * data pointer.
+ *
+ * It is valid to set the |stream_user_data| to the stream reserved by
+ * PUSH_PROMISE frame.
+ *
+ * This function returns 0 if it succeeds, or one of following
+ * negative error codes:
+ *
+ * :enum:`NGHTTP2_ERR_INVALID_ARGUMENT`
+ *     The stream does not exist
+ */
+NGHTTP2_EXTERN int
+nghttp2_session_set_stream_user_data(nghttp2_session *session,
+                                     int32_t stream_id, void *stream_user_data);
+
+/**
+ * @function
+ *
+ * Sets |user_data| to |session|, overwriting the existing user data
+ * specified in `nghttp2_session_client_new()`, or
+ * `nghttp2_session_server_new()`.
+ */
+NGHTTP2_EXTERN void nghttp2_session_set_user_data(nghttp2_session *session,
+                                                  void *user_data);
+
+/**
+ * @function
+ *
+ * Returns the number of frames in the outbound queue.  This does not
+ * include the deferred DATA frames.
+ */
+NGHTTP2_EXTERN size_t
+nghttp2_session_get_outbound_queue_size(nghttp2_session *session);
+
+/**
+ * @function
+ *
+ * Returns the number of DATA payload in bytes received without
+ * WINDOW_UPDATE transmission for the stream |stream_id|.  The local
+ * (receive) window size can be adjusted by
+ * `nghttp2_submit_window_update()`.  This function takes into account
+ * that and returns effective data length.  In particular, if the
+ * local window size is reduced by submitting negative
+ * window_size_increment with `nghttp2_submit_window_update()`, this
+ * function returns the number of bytes less than actually received.
+ *
+ * This function returns -1 if it fails.
+ */
+NGHTTP2_EXTERN int32_t nghttp2_session_get_stream_effective_recv_data_length(
+    nghttp2_session *session, int32_t stream_id);
+
+/**
+ * @function
+ *
+ * Returns the local (receive) window size for the stream |stream_id|.
+ * The local window size can be adjusted by
+ * `nghttp2_submit_window_update()`.  This function takes into account
+ * that and returns effective window size.
+ *
+ * This function does not take into account the amount of received
+ * data from the remote endpoint.  Use
+ * `nghttp2_session_get_stream_local_window_size()` to know the amount
+ * of data the remote endpoint can send without receiving stream level
+ * WINDOW_UPDATE frame.  Note that each stream is still subject to the
+ * connection level flow control.
+ *
+ * This function returns -1 if it fails.
+ */
+NGHTTP2_EXTERN int32_t nghttp2_session_get_stream_effective_local_window_size(
+    nghttp2_session *session, int32_t stream_id);
+
+/**
+ * @function
+ *
+ * Returns the amount of flow-controlled payload (e.g., DATA) that the
+ * remote endpoint can send without receiving stream level
+ * WINDOW_UPDATE frame.  It is also subject to the connection level
+ * flow control.  So the actual amount of data to send is
+ * min(`nghttp2_session_get_stream_local_window_size()`,
+ * `nghttp2_session_get_local_window_size()`).
+ *
+ * This function returns -1 if it fails.
+ */
+NGHTTP2_EXTERN int32_t nghttp2_session_get_stream_local_window_size(
+    nghttp2_session *session, int32_t stream_id);
+
+/**
+ * @function
+ *
+ * Returns the number of DATA payload in bytes received without
+ * WINDOW_UPDATE transmission for a connection.  The local (receive)
+ * window size can be adjusted by `nghttp2_submit_window_update()`.
+ * This function takes into account that and returns effective data
+ * length.  In particular, if the local window size is reduced by
+ * submitting negative window_size_increment with
+ * `nghttp2_submit_window_update()`, this function returns the number
+ * of bytes less than actually received.
+ *
+ * This function returns -1 if it fails.
+ */
+NGHTTP2_EXTERN int32_t
+nghttp2_session_get_effective_recv_data_length(nghttp2_session *session);
+
+/**
+ * @function
+ *
+ * Returns the local (receive) window size for a connection.  The
+ * local window size can be adjusted by
+ * `nghttp2_submit_window_update()`.  This function takes into account
+ * that and returns effective window size.
+ *
+ * This function does not take into account the amount of received
+ * data from the remote endpoint.  Use
+ * `nghttp2_session_get_local_window_size()` to know the amount of
+ * data the remote endpoint can send without receiving
+ * connection-level WINDOW_UPDATE frame.  Note that each stream is
+ * still subject to the stream level flow control.
+ *
+ * This function returns -1 if it fails.
+ */
+NGHTTP2_EXTERN int32_t
+nghttp2_session_get_effective_local_window_size(nghttp2_session *session);
+
+/**
+ * @function
+ *
+ * Returns the amount of flow-controlled payload (e.g., DATA) that the
+ * remote endpoint can send without receiving connection level
+ * WINDOW_UPDATE frame.  Note that each stream is still subject to the
+ * stream level flow control (see
+ * `nghttp2_session_get_stream_local_window_size()`).
+ *
+ * This function returns -1 if it fails.
+ */
+NGHTTP2_EXTERN int32_t
+nghttp2_session_get_local_window_size(nghttp2_session *session);
+
+/**
+ * @function
+ *
+ * Returns the remote window size for a given stream |stream_id|.
+ *
+ * This is the amount of flow-controlled payload (e.g., DATA) that the
+ * local endpoint can send without stream level WINDOW_UPDATE.  There
+ * is also connection level flow control, so the effective size of
+ * payload that the local endpoint can actually send is
+ * min(`nghttp2_session_get_stream_remote_window_size()`,
+ * `nghttp2_session_get_remote_window_size()`).
+ *
+ * This function returns -1 if it fails.
+ */
+NGHTTP2_EXTERN int32_t nghttp2_session_get_stream_remote_window_size(
+    nghttp2_session *session, int32_t stream_id);
+
+/**
+ * @function
+ *
+ * Returns the remote window size for a connection.
+ *
+ * This function always succeeds.
+ */
+NGHTTP2_EXTERN int32_t
+nghttp2_session_get_remote_window_size(nghttp2_session *session);
+
+/**
+ * @function
+ *
+ * Returns 1 if local peer half closed the given stream |stream_id|.
+ * Returns 0 if it did not.  Returns -1 if no such stream exists.
+ */
+NGHTTP2_EXTERN int
+nghttp2_session_get_stream_local_close(nghttp2_session *session,
+                                       int32_t stream_id);
+
+/**
+ * @function
+ *
+ * Returns 1 if remote peer half closed the given stream |stream_id|.
+ * Returns 0 if it did not.  Returns -1 if no such stream exists.
+ */
+NGHTTP2_EXTERN int
+nghttp2_session_get_stream_remote_close(nghttp2_session *session,
+                                        int32_t stream_id);
+
+/**
+ * @function
+ *
+ * Returns the current dynamic table size of HPACK inflater, including
+ * the overhead 32 bytes per entry described in RFC 7541.
+ */
+NGHTTP2_EXTERN size_t
+nghttp2_session_get_hd_inflate_dynamic_table_size(nghttp2_session *session);
+
+/**
+ * @function
+ *
+ * Returns the current dynamic table size of HPACK deflater including
+ * the overhead 32 bytes per entry described in RFC 7541.
+ */
+NGHTTP2_EXTERN size_t
+nghttp2_session_get_hd_deflate_dynamic_table_size(nghttp2_session *session);
+
+/**
+ * @function
+ *
+ * Signals the session so that the connection should be terminated.
+ *
+ * The last stream ID is the minimum value between the stream ID of a
+ * stream for which :type:`nghttp2_on_frame_recv_callback` was called
+ * most recently and the last stream ID we have sent to the peer
+ * previously.
+ *
+ * The |error_code| is the error code of this GOAWAY frame.  The
+ * pre-defined error code is one of :enum:`nghttp2_error_code`.
+ *
+ * After the transmission, both `nghttp2_session_want_read()` and
+ * `nghttp2_session_want_write()` return 0.
+ *
+ * This function should be called when the connection should be
+ * terminated after sending GOAWAY.  If the remaining streams should
+ * be processed after GOAWAY, use `nghttp2_submit_goaway()` instead.
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * :enum:`NGHTTP2_ERR_NOMEM`
+ *     Out of memory.
+ */
+NGHTTP2_EXTERN int nghttp2_session_terminate_session(nghttp2_session *session,
+                                                     uint32_t error_code);
+
+/**
+ * @function
+ *
+ * Signals the session so that the connection should be terminated.
+ *
+ * This function behaves like `nghttp2_session_terminate_session()`,
+ * but the last stream ID can be specified by the application for fine
+ * grained control of stream.  The HTTP/2 specification does not allow
+ * last_stream_id to be increased.  So the actual value sent as
+ * last_stream_id is the minimum value between the given
+ * |last_stream_id| and the last_stream_id we have previously sent to
+ * the peer.
+ *
+ * The |last_stream_id| is peer's stream ID or 0.  So if |session| is
+ * initialized as client, |last_stream_id| must be even or 0.  If
+ * |session| is initialized as server, |last_stream_id| must be odd or
+ * 0.
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * :enum:`NGHTTP2_ERR_NOMEM`
+ *     Out of memory.
+ * :enum:`NGHTTP2_ERR_INVALID_ARGUMENT`
+ *     The |last_stream_id| is invalid.
+ */
+NGHTTP2_EXTERN int nghttp2_session_terminate_session2(nghttp2_session *session,
+                                                      int32_t last_stream_id,
+                                                      uint32_t error_code);
+
+/**
+ * @function
+ *
+ * Signals to the client that the server started graceful shutdown
+ * procedure.
+ *
+ * This function is only usable for server.  If this function is
+ * called with client side session, this function returns
+ * :enum:`NGHTTP2_ERR_INVALID_STATE`.
+ *
+ * To gracefully shutdown HTTP/2 session, server should call this
+ * function to send GOAWAY with last_stream_id (1u << 31) - 1.  And
+ * after some delay (e.g., 1 RTT), send another GOAWAY with the stream
+ * ID that the server has some processing using
+ * `nghttp2_submit_goaway()`.  See also
+ * `nghttp2_session_get_last_proc_stream_id()`.
+ *
+ * Unlike `nghttp2_submit_goaway()`, this function just sends GOAWAY
+ * and does nothing more.  This is a mere indication to the client
+ * that session shutdown is imminent.  The application should call
+ * `nghttp2_submit_goaway()` with appropriate last_stream_id after
+ * this call.
+ *
+ * If one or more GOAWAY frame have been already sent by either
+ * `nghttp2_submit_goaway()` or `nghttp2_session_terminate_session()`,
+ * this function has no effect.
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * :enum:`NGHTTP2_ERR_NOMEM`
+ *     Out of memory.
+ * :enum:`NGHTTP2_ERR_INVALID_STATE`
+ *     The |session| is initialized as client.
+ */
+NGHTTP2_EXTERN int nghttp2_submit_shutdown_notice(nghttp2_session *session);
+
+/**
+ * @function
+ *
+ * Returns the value of SETTINGS |id| notified by a remote endpoint.
+ * The |id| must be one of values defined in
+ * :enum:`nghttp2_settings_id`.
+ */
+NGHTTP2_EXTERN uint32_t nghttp2_session_get_remote_settings(
+    nghttp2_session *session, nghttp2_settings_id id);
+
+/**
+ * @function
+ *
+ * Returns the value of SETTINGS |id| of local endpoint acknowledged
+ * by the remote endpoint.  The |id| must be one of the values defined
+ * in :enum:`nghttp2_settings_id`.
+ */
+NGHTTP2_EXTERN uint32_t nghttp2_session_get_local_settings(
+    nghttp2_session *session, nghttp2_settings_id id);
+
+/**
+ * @function
+ *
+ * Tells the |session| that next stream ID is |next_stream_id|.  The
+ * |next_stream_id| must be equal or greater than the value returned
+ * by `nghttp2_session_get_next_stream_id()`.
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * :enum:`NGHTTP2_ERR_INVALID_ARGUMENT`
+ *     The |next_stream_id| is strictly less than the value
+ *     `nghttp2_session_get_next_stream_id()` returns; or
+ *     |next_stream_id| is invalid (e.g., even integer for client, or
+ *     odd integer for server).
+ */
+NGHTTP2_EXTERN int nghttp2_session_set_next_stream_id(nghttp2_session *session,
+                                                      int32_t next_stream_id);
+
+/**
+ * @function
+ *
+ * Returns the next outgoing stream ID.  Notice that return type is
+ * uint32_t.  If we run out of stream ID for this session, this
+ * function returns 1 << 31.
+ */
+NGHTTP2_EXTERN uint32_t
+nghttp2_session_get_next_stream_id(nghttp2_session *session);
+
+/**
+ * @function
+ *
+ * Tells the |session| that |size| bytes for a stream denoted by
+ * |stream_id| were consumed by application and are ready to
+ * WINDOW_UPDATE.  The consumed bytes are counted towards both
+ * connection and stream level WINDOW_UPDATE (see
+ * `nghttp2_session_consume_connection()` and
+ * `nghttp2_session_consume_stream()` to update consumption
+ * independently).  This function is intended to be used without
+ * automatic window update (see
+ * `nghttp2_option_set_no_auto_window_update()`).
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * :enum:`NGHTTP2_ERR_NOMEM`
+ *     Out of memory.
+ * :enum:`NGHTTP2_ERR_INVALID_ARGUMENT`
+ *     The |stream_id| is 0.
+ * :enum:`NGHTTP2_ERR_INVALID_STATE`
+ *     Automatic WINDOW_UPDATE is not disabled.
+ */
+NGHTTP2_EXTERN int nghttp2_session_consume(nghttp2_session *session,
+                                           int32_t stream_id, size_t size);
+
+/**
+ * @function
+ *
+ * Like `nghttp2_session_consume()`, but this only tells library that
+ * |size| bytes were consumed only for connection level.  Note that
+ * HTTP/2 maintains connection and stream level flow control windows
+ * independently.
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * :enum:`NGHTTP2_ERR_NOMEM`
+ *     Out of memory.
+ * :enum:`NGHTTP2_ERR_INVALID_STATE`
+ *     Automatic WINDOW_UPDATE is not disabled.
+ */
+NGHTTP2_EXTERN int nghttp2_session_consume_connection(nghttp2_session *session,
+                                                      size_t size);
+
+/**
+ * @function
+ *
+ * Like `nghttp2_session_consume()`, but this only tells library that
+ * |size| bytes were consumed only for stream denoted by |stream_id|.
+ * Note that HTTP/2 maintains connection and stream level flow control
+ * windows independently.
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * :enum:`NGHTTP2_ERR_NOMEM`
+ *     Out of memory.
+ * :enum:`NGHTTP2_ERR_INVALID_ARGUMENT`
+ *     The |stream_id| is 0.
+ * :enum:`NGHTTP2_ERR_INVALID_STATE`
+ *     Automatic WINDOW_UPDATE is not disabled.
+ */
+NGHTTP2_EXTERN int nghttp2_session_consume_stream(nghttp2_session *session,
+                                                  int32_t stream_id,
+                                                  size_t size);
+
+/**
+ * @function
+ *
+ * Changes priority of existing stream denoted by |stream_id|.  The
+ * new priority specification is |pri_spec|.
+ *
+ * The priority is changed silently and instantly, and no PRIORITY
+ * frame will be sent to notify the peer of this change.  This
+ * function may be useful for server to change the priority of pushed
+ * stream.
+ *
+ * If |session| is initialized as server, and ``pri_spec->stream_id``
+ * points to the idle stream, the idle stream is created if it does
+ * not exist.  The created idle stream will depend on root stream
+ * (stream 0) with weight 16.
+ *
+ * Otherwise, if stream denoted by ``pri_spec->stream_id`` is not
+ * found, we use default priority instead of given |pri_spec|.  That
+ * is make stream depend on root stream with weight 16.
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * :enum:`NGHTTP2_ERR_NOMEM`
+ *     Out of memory.
+ * :enum:`NGHTTP2_ERR_INVALID_ARGUMENT`
+ *     Attempted to depend on itself; or no stream exist for the given
+ *     |stream_id|; or |stream_id| is 0
+ */
+NGHTTP2_EXTERN int
+nghttp2_session_change_stream_priority(nghttp2_session *session,
+                                       int32_t stream_id,
+                                       const nghttp2_priority_spec *pri_spec);
+
+/**
+ * @function
+ *
+ * Creates idle stream with the given |stream_id|, and priority
+ * |pri_spec|.
+ *
+ * The stream creation is done without sending PRIORITY frame, which
+ * means that peer does not know about the existence of this idle
+ * stream in the local endpoint.
+ *
+ * RFC 7540 does not disallow the use of creation of idle stream with
+ * odd or even stream ID regardless of client or server.  So this
+ * function can create odd or even stream ID regardless of client or
+ * server.  But probably it is a bit safer to use the stream ID the
+ * local endpoint can initiate (in other words, use odd stream ID for
+ * client, and even stream ID for server), to avoid potential
+ * collision from peer's instruction.  Also we can use
+ * `nghttp2_session_set_next_stream_id()` to avoid to open created
+ * idle streams accidentally if we follow this recommendation.
+ *
+ * If |session| is initialized as server, and ``pri_spec->stream_id``
+ * points to the idle stream, the idle stream is created if it does
+ * not exist.  The created idle stream will depend on root stream
+ * (stream 0) with weight 16.
+ *
+ * Otherwise, if stream denoted by ``pri_spec->stream_id`` is not
+ * found, we use default priority instead of given |pri_spec|.  That
+ * is make stream depend on root stream with weight 16.
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * :enum:`NGHTTP2_ERR_NOMEM`
+ *     Out of memory.
+ * :enum:`NGHTTP2_ERR_INVALID_ARGUMENT`
+ *     Attempted to depend on itself; or stream denoted by |stream_id|
+ *     already exists; or |stream_id| cannot be used to create idle
+ *     stream (in other words, local endpoint has already opened
+ *     stream ID greater than or equal to the given stream ID; or
+ *     |stream_id| is 0
+ */
+NGHTTP2_EXTERN int
+nghttp2_session_create_idle_stream(nghttp2_session *session, int32_t stream_id,
+                                   const nghttp2_priority_spec *pri_spec);
+
+/**
+ * @function
+ *
+ * Performs post-process of HTTP Upgrade request.  This function can
+ * be called from both client and server, but the behavior is very
+ * different in each other.
+ *
+ * .. warning::
+ *
+ *   This function is deprecated in favor of
+ *   `nghttp2_session_upgrade2()`, because this function lacks the
+ *   parameter to tell the library the request method used in the
+ *   original HTTP request.  This information is required for client
+ *   to validate actual response body length against content-length
+ *   header field (see `nghttp2_option_set_no_http_messaging()`).  If
+ *   HEAD is used in request, the length of response body must be 0
+ *   regardless of value included in content-length header field.
+ *
+ * If called from client side, the |settings_payload| must be the
+ * value sent in ``HTTP2-Settings`` header field and must be decoded
+ * by base64url decoder.  The |settings_payloadlen| is the length of
+ * |settings_payload|.  The |settings_payload| is unpacked and its
+ * setting values will be submitted using `nghttp2_submit_settings()`.
+ * This means that the client application code does not need to submit
+ * SETTINGS by itself.  The stream with stream ID=1 is opened and the
+ * |stream_user_data| is used for its stream_user_data.  The opened
+ * stream becomes half-closed (local) state.
+ *
+ * If called from server side, the |settings_payload| must be the
+ * value received in ``HTTP2-Settings`` header field and must be
+ * decoded by base64url decoder.  The |settings_payloadlen| is the
+ * length of |settings_payload|.  It is treated as if the SETTINGS
+ * frame with that payload is received.  Thus, callback functions for
+ * the reception of SETTINGS frame will be invoked.  The stream with
+ * stream ID=1 is opened.  The |stream_user_data| is ignored.  The
+ * opened stream becomes half-closed (remote).
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * :enum:`NGHTTP2_ERR_NOMEM`
+ *     Out of memory.
+ * :enum:`NGHTTP2_ERR_INVALID_ARGUMENT`
+ *     The |settings_payload| is badly formed.
+ * :enum:`NGHTTP2_ERR_PROTO`
+ *     The stream ID 1 is already used or closed; or is not available.
+ */
+NGHTTP2_EXTERN int nghttp2_session_upgrade(nghttp2_session *session,
+                                           const uint8_t *settings_payload,
+                                           size_t settings_payloadlen,
+                                           void *stream_user_data);
+
+/**
+ * @function
+ *
+ * Performs post-process of HTTP Upgrade request.  This function can
+ * be called from both client and server, but the behavior is very
+ * different in each other.
+ *
+ * If called from client side, the |settings_payload| must be the
+ * value sent in ``HTTP2-Settings`` header field and must be decoded
+ * by base64url decoder.  The |settings_payloadlen| is the length of
+ * |settings_payload|.  The |settings_payload| is unpacked and its
+ * setting values will be submitted using `nghttp2_submit_settings()`.
+ * This means that the client application code does not need to submit
+ * SETTINGS by itself.  The stream with stream ID=1 is opened and the
+ * |stream_user_data| is used for its stream_user_data.  The opened
+ * stream becomes half-closed (local) state.
+ *
+ * If called from server side, the |settings_payload| must be the
+ * value received in ``HTTP2-Settings`` header field and must be
+ * decoded by base64url decoder.  The |settings_payloadlen| is the
+ * length of |settings_payload|.  It is treated as if the SETTINGS
+ * frame with that payload is received.  Thus, callback functions for
+ * the reception of SETTINGS frame will be invoked.  The stream with
+ * stream ID=1 is opened.  The |stream_user_data| is ignored.  The
+ * opened stream becomes half-closed (remote).
+ *
+ * If the request method is HEAD, pass nonzero value to
+ * |head_request|.  Otherwise, pass 0.
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * :enum:`NGHTTP2_ERR_NOMEM`
+ *     Out of memory.
+ * :enum:`NGHTTP2_ERR_INVALID_ARGUMENT`
+ *     The |settings_payload| is badly formed.
+ * :enum:`NGHTTP2_ERR_PROTO`
+ *     The stream ID 1 is already used or closed; or is not available.
+ */
+NGHTTP2_EXTERN int nghttp2_session_upgrade2(nghttp2_session *session,
+                                            const uint8_t *settings_payload,
+                                            size_t settings_payloadlen,
+                                            int head_request,
+                                            void *stream_user_data);
+
+/**
+ * @function
+ *
+ * Serializes the SETTINGS values |iv| in the |buf|.  The size of the
+ * |buf| is specified by |buflen|.  The number of entries in the |iv|
+ * array is given by |niv|.  The required space in |buf| for the |niv|
+ * entries is ``6*niv`` bytes and if the given buffer is too small, an
+ * error is returned.  This function is used mainly for creating a
+ * SETTINGS payload to be sent with the ``HTTP2-Settings`` header
+ * field in an HTTP Upgrade request.  The data written in |buf| is NOT
+ * base64url encoded and the application is responsible for encoding.
+ *
+ * This function returns the number of bytes written in |buf|, or one
+ * of the following negative error codes:
+ *
+ * :enum:`NGHTTP2_ERR_INVALID_ARGUMENT`
+ *     The |iv| contains duplicate settings ID or invalid value.
+ *
+ * :enum:`NGHTTP2_ERR_INSUFF_BUFSIZE`
+ *     The provided |buflen| size is too small to hold the output.
+ */
+NGHTTP2_EXTERN ssize_t nghttp2_pack_settings_payload(
+    uint8_t *buf, size_t buflen, const nghttp2_settings_entry *iv, size_t niv);
+
+/**
+ * @function
+ *
+ * Returns string describing the |lib_error_code|.  The
+ * |lib_error_code| must be one of the :enum:`nghttp2_error`.
+ */
+NGHTTP2_EXTERN const char *nghttp2_strerror(int lib_error_code);
+
+/**
+ * @function
+ *
+ * Returns string representation of HTTP/2 error code |error_code|
+ * (e.g., ``PROTOCOL_ERROR`` is returned if ``error_code ==
+ * NGHTTP2_PROTOCOL_ERROR``).  If string representation is unknown for
+ * given |error_code|, this function returns string ``unknown``.
+ */
+NGHTTP2_EXTERN const char *nghttp2_http2_strerror(uint32_t error_code);
+
+/**
+ * @function
+ *
+ * Initializes |pri_spec| with the |stream_id| of the stream to depend
+ * on with |weight| and its exclusive flag.  If |exclusive| is
+ * nonzero, exclusive flag is set.
+ *
+ * The |weight| must be in [:enum:`NGHTTP2_MIN_WEIGHT`,
+ * :enum:`NGHTTP2_MAX_WEIGHT`], inclusive.
+ */
+NGHTTP2_EXTERN void nghttp2_priority_spec_init(nghttp2_priority_spec *pri_spec,
+                                               int32_t stream_id,
+                                               int32_t weight, int exclusive);
+
+/**
+ * @function
+ *
+ * Initializes |pri_spec| with the default values.  The default values
+ * are: stream_id = 0, weight = :macro:`NGHTTP2_DEFAULT_WEIGHT` and
+ * exclusive = 0.
+ */
+NGHTTP2_EXTERN void
+nghttp2_priority_spec_default_init(nghttp2_priority_spec *pri_spec);
+
+/**
+ * @function
+ *
+ * Returns nonzero if the |pri_spec| is filled with default values.
+ */
+NGHTTP2_EXTERN int
+nghttp2_priority_spec_check_default(const nghttp2_priority_spec *pri_spec);
+
+/**
+ * @function
+ *
+ * Submits HEADERS frame and optionally one or more DATA frames.
+ *
+ * The |pri_spec| is priority specification of this request.  ``NULL``
+ * means the default priority (see
+ * `nghttp2_priority_spec_default_init()`).  To specify the priority,
+ * use `nghttp2_priority_spec_init()`.  If |pri_spec| is not ``NULL``,
+ * this function will copy its data members.
+ *
+ * The ``pri_spec->weight`` must be in [:enum:`NGHTTP2_MIN_WEIGHT`,
+ * :enum:`NGHTTP2_MAX_WEIGHT`], inclusive.  If ``pri_spec->weight`` is
+ * strictly less than :enum:`NGHTTP2_MIN_WEIGHT`, it becomes
+ * :enum:`NGHTTP2_MIN_WEIGHT`.  If it is strictly greater than
+ * :enum:`NGHTTP2_MAX_WEIGHT`, it becomes :enum:`NGHTTP2_MAX_WEIGHT`.
+ *
+ * The |nva| is an array of name/value pair :type:`nghttp2_nv` with
+ * |nvlen| elements.  The application is responsible to include
+ * required pseudo-header fields (header field whose name starts with
+ * ":") in |nva| and must place pseudo-headers before regular header
+ * fields.
+ *
+ * This function creates copies of all name/value pairs in |nva|.  It
+ * also lower-cases all names in |nva|.  The order of elements in
+ * |nva| is preserved.  For header fields with
+ * :enum:`NGHTTP2_NV_FLAG_NO_COPY_NAME` and
+ * :enum:`NGHTTP2_NV_FLAG_NO_COPY_VALUE` are set, header field name
+ * and value are not copied respectively.  With
+ * :enum:`NGHTTP2_NV_FLAG_NO_COPY_NAME`, application is responsible to
+ * pass header field name in lowercase.  The application should
+ * maintain the references to them until
+ * :type:`nghttp2_on_frame_send_callback` or
+ * :type:`nghttp2_on_frame_not_send_callback` is called.
+ *
+ * HTTP/2 specification has requirement about header fields in the
+ * request HEADERS.  See the specification for more details.
+ *
+ * If |data_prd| is not ``NULL``, it provides data which will be sent
+ * in subsequent DATA frames.  In this case, a method that allows
+ * request message bodies
+ * (https://tools.ietf.org/html/rfc7231#section-4) must be specified
+ * with ``:method`` key in |nva| (e.g. ``POST``).  This function does
+ * not take ownership of the |data_prd|.  The function copies the
+ * members of the |data_prd|.  If |data_prd| is ``NULL``, HEADERS have
+ * END_STREAM set.  The |stream_user_data| is data associated to the
+ * stream opened by this request and can be an arbitrary pointer,
+ * which can be retrieved later by
+ * `nghttp2_session_get_stream_user_data()`.
+ *
+ * This function returns assigned stream ID if it succeeds, or one of
+ * the following negative error codes:
+ *
+ * :enum:`NGHTTP2_ERR_NOMEM`
+ *     Out of memory.
+ * :enum:`NGHTTP2_ERR_STREAM_ID_NOT_AVAILABLE`
+ *     No stream ID is available because maximum stream ID was
+ *     reached.
+ * :enum:`NGHTTP2_ERR_INVALID_ARGUMENT`
+ *     Trying to depend on itself (new stream ID equals
+ *     ``pri_spec->stream_id``).
+ * :enum:`NGHTTP2_ERR_PROTO`
+ *     The |session| is server session.
+ *
+ * .. warning::
+ *
+ *   This function returns assigned stream ID if it succeeds.  But
+ *   that stream is not opened yet.  The application must not submit
+ *   frame to that stream ID before
+ *   :type:`nghttp2_before_frame_send_callback` is called for this
+ *   frame.
+ *
+ */
+NGHTTP2_EXTERN int32_t nghttp2_submit_request(
+    nghttp2_session *session, const nghttp2_priority_spec *pri_spec,
+    const nghttp2_nv *nva, size_t nvlen, const nghttp2_data_provider *data_prd,
+    void *stream_user_data);
+
+/**
+ * @function
+ *
+ * Submits response HEADERS frame and optionally one or more DATA
+ * frames against the stream |stream_id|.
+ *
+ * The |nva| is an array of name/value pair :type:`nghttp2_nv` with
+ * |nvlen| elements.  The application is responsible to include
+ * required pseudo-header fields (header field whose name starts with
+ * ":") in |nva| and must place pseudo-headers before regular header
+ * fields.
+ *
+ * This function creates copies of all name/value pairs in |nva|.  It
+ * also lower-cases all names in |nva|.  The order of elements in
+ * |nva| is preserved.  For header fields with
+ * :enum:`NGHTTP2_NV_FLAG_NO_COPY_NAME` and
+ * :enum:`NGHTTP2_NV_FLAG_NO_COPY_VALUE` are set, header field name
+ * and value are not copied respectively.  With
+ * :enum:`NGHTTP2_NV_FLAG_NO_COPY_NAME`, application is responsible to
+ * pass header field name in lowercase.  The application should
+ * maintain the references to them until
+ * :type:`nghttp2_on_frame_send_callback` or
+ * :type:`nghttp2_on_frame_not_send_callback` is called.
+ *
+ * HTTP/2 specification has requirement about header fields in the
+ * response HEADERS.  See the specification for more details.
+ *
+ * If |data_prd| is not ``NULL``, it provides data which will be sent
+ * in subsequent DATA frames.  This function does not take ownership
+ * of the |data_prd|.  The function copies the members of the
+ * |data_prd|.  If |data_prd| is ``NULL``, HEADERS will have
+ * END_STREAM flag set.
+ *
+ * This method can be used as normal HTTP response and push response.
+ * When pushing a resource using this function, the |session| must be
+ * configured using `nghttp2_session_server_new()` or its variants and
+ * the target stream denoted by the |stream_id| must be reserved using
+ * `nghttp2_submit_push_promise()`.
+ *
+ * To send non-final response headers (e.g., HTTP status 101), don't
+ * use this function because this function half-closes the outbound
+ * stream.  Instead, use `nghttp2_submit_headers()` for this purpose.
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * :enum:`NGHTTP2_ERR_NOMEM`
+ *     Out of memory.
+ * :enum:`NGHTTP2_ERR_INVALID_ARGUMENT`
+ *     The |stream_id| is 0.
+ * :enum:`NGHTTP2_ERR_DATA_EXIST`
+ *     DATA or HEADERS has been already submitted and not fully
+ *     processed yet.  Normally, this does not happen, but when
+ *     application wrongly calls `nghttp2_submit_response()` twice,
+ *     this may happen.
+ * :enum:`NGHTTP2_ERR_PROTO`
+ *     The |session| is client session.
+ *
+ * .. warning::
+ *
+ *   Calling this function twice for the same stream ID may lead to
+ *   program crash.  It is generally considered to a programming error
+ *   to commit response twice.
+ */
+NGHTTP2_EXTERN int
+nghttp2_submit_response(nghttp2_session *session, int32_t stream_id,
+                        const nghttp2_nv *nva, size_t nvlen,
+                        const nghttp2_data_provider *data_prd);
+
+/**
+ * @function
+ *
+ * Submits trailer fields HEADERS against the stream |stream_id|.
+ *
+ * The |nva| is an array of name/value pair :type:`nghttp2_nv` with
+ * |nvlen| elements.  The application must not include pseudo-header
+ * fields (headers whose names starts with ":") in |nva|.
+ *
+ * This function creates copies of all name/value pairs in |nva|.  It
+ * also lower-cases all names in |nva|.  The order of elements in
+ * |nva| is preserved.  For header fields with
+ * :enum:`NGHTTP2_NV_FLAG_NO_COPY_NAME` and
+ * :enum:`NGHTTP2_NV_FLAG_NO_COPY_VALUE` are set, header field name
+ * and value are not copied respectively.  With
+ * :enum:`NGHTTP2_NV_FLAG_NO_COPY_NAME`, application is responsible to
+ * pass header field name in lowercase.  The application should
+ * maintain the references to them until
+ * :type:`nghttp2_on_frame_send_callback` or
+ * :type:`nghttp2_on_frame_not_send_callback` is called.
+ *
+ * For server, trailer fields must follow response HEADERS or response
+ * DATA without END_STREAM flat set.  The library does not enforce
+ * this requirement, and applications should do this for themselves.
+ * If `nghttp2_submit_trailer()` is called before any response HEADERS
+ * submission (usually by `nghttp2_submit_response()`), the content of
+ * |nva| will be sent as response headers, which will result in error.
+ *
+ * This function has the same effect with `nghttp2_submit_headers()`,
+ * with flags = :enum:`NGHTTP2_FLAG_END_STREAM` and both pri_spec and
+ * stream_user_data to NULL.
+ *
+ * To submit trailer fields after `nghttp2_submit_response()` is
+ * called, the application has to specify
+ * :type:`nghttp2_data_provider` to `nghttp2_submit_response()`.
+ * Inside of :type:`nghttp2_data_source_read_callback`, when setting
+ * :enum:`NGHTTP2_DATA_FLAG_EOF`, also set
+ * :enum:`NGHTTP2_DATA_FLAG_NO_END_STREAM`.  After that, the
+ * application can send trailer fields using
+ * `nghttp2_submit_trailer()`.  `nghttp2_submit_trailer()` can be used
+ * inside :type:`nghttp2_data_source_read_callback`.
+ *
+ * This function returns 0 if it succeeds and |stream_id| is -1.
+ * Otherwise, this function returns 0 if it succeeds, or one of the
+ * following negative error codes:
+ *
+ * :enum:`NGHTTP2_ERR_NOMEM`
+ *     Out of memory.
+ * :enum:`NGHTTP2_ERR_INVALID_ARGUMENT`
+ *     The |stream_id| is 0.
+ */
+NGHTTP2_EXTERN int nghttp2_submit_trailer(nghttp2_session *session,
+                                          int32_t stream_id,
+                                          const nghttp2_nv *nva, size_t nvlen);
+
+/**
+ * @function
+ *
+ * Submits HEADERS frame. The |flags| is bitwise OR of the
+ * following values:
+ *
+ * * :enum:`NGHTTP2_FLAG_END_STREAM`
+ *
+ * If |flags| includes :enum:`NGHTTP2_FLAG_END_STREAM`, this frame has
+ * END_STREAM flag set.
+ *
+ * The library handles the CONTINUATION frame internally and it
+ * correctly sets END_HEADERS to the last sequence of the PUSH_PROMISE
+ * or CONTINUATION frame.
+ *
+ * If the |stream_id| is -1, this frame is assumed as request (i.e.,
+ * request HEADERS frame which opens new stream).  In this case, the
+ * assigned stream ID will be returned.  Otherwise, specify stream ID
+ * in |stream_id|.
+ *
+ * The |pri_spec| is priority specification of this request.  ``NULL``
+ * means the default priority (see
+ * `nghttp2_priority_spec_default_init()`).  To specify the priority,
+ * use `nghttp2_priority_spec_init()`.  If |pri_spec| is not ``NULL``,
+ * this function will copy its data members.
+ *
+ * The ``pri_spec->weight`` must be in [:enum:`NGHTTP2_MIN_WEIGHT`,
+ * :enum:`NGHTTP2_MAX_WEIGHT`], inclusive.  If ``pri_spec->weight`` is
+ * strictly less than :enum:`NGHTTP2_MIN_WEIGHT`, it becomes
+ * :enum:`NGHTTP2_MIN_WEIGHT`.  If it is strictly greater than
+ * :enum:`NGHTTP2_MAX_WEIGHT`, it becomes :enum:`NGHTTP2_MAX_WEIGHT`.
+ *
+ * The |nva| is an array of name/value pair :type:`nghttp2_nv` with
+ * |nvlen| elements.  The application is responsible to include
+ * required pseudo-header fields (header field whose name starts with
+ * ":") in |nva| and must place pseudo-headers before regular header
+ * fields.
+ *
+ * This function creates copies of all name/value pairs in |nva|.  It
+ * also lower-cases all names in |nva|.  The order of elements in
+ * |nva| is preserved.  For header fields with
+ * :enum:`NGHTTP2_NV_FLAG_NO_COPY_NAME` and
+ * :enum:`NGHTTP2_NV_FLAG_NO_COPY_VALUE` are set, header field name
+ * and value are not copied respectively.  With
+ * :enum:`NGHTTP2_NV_FLAG_NO_COPY_NAME`, application is responsible to
+ * pass header field name in lowercase.  The application should
+ * maintain the references to them until
+ * :type:`nghttp2_on_frame_send_callback` or
+ * :type:`nghttp2_on_frame_not_send_callback` is called.
+ *
+ * The |stream_user_data| is a pointer to an arbitrary data which is
+ * associated to the stream this frame will open.  Therefore it is
+ * only used if this frame opens streams, in other words, it changes
+ * stream state from idle or reserved to open.
+ *
+ * This function is low-level in a sense that the application code can
+ * specify flags directly.  For usual HTTP request,
+ * `nghttp2_submit_request()` is useful.  Likewise, for HTTP response,
+ * prefer `nghttp2_submit_response()`.
+ *
+ * This function returns newly assigned stream ID if it succeeds and
+ * |stream_id| is -1.  Otherwise, this function returns 0 if it
+ * succeeds, or one of the following negative error codes:
+ *
+ * :enum:`NGHTTP2_ERR_NOMEM`
+ *     Out of memory.
+ * :enum:`NGHTTP2_ERR_STREAM_ID_NOT_AVAILABLE`
+ *     No stream ID is available because maximum stream ID was
+ *     reached.
+ * :enum:`NGHTTP2_ERR_INVALID_ARGUMENT`
+ *     The |stream_id| is 0; or trying to depend on itself (stream ID
+ *     equals ``pri_spec->stream_id``).
+ * :enum:`NGHTTP2_ERR_DATA_EXIST`
+ *     DATA or HEADERS has been already submitted and not fully
+ *     processed yet.  This happens if stream denoted by |stream_id|
+ *     is in reserved state.
+ * :enum:`NGHTTP2_ERR_PROTO`
+ *     The |stream_id| is -1, and |session| is server session.
+ *
+ * .. warning::
+ *
+ *   This function returns assigned stream ID if it succeeds and
+ *   |stream_id| is -1.  But that stream is not opened yet.  The
+ *   application must not submit frame to that stream ID before
+ *   :type:`nghttp2_before_frame_send_callback` is called for this
+ *   frame.
+ *
+ */
+NGHTTP2_EXTERN int32_t nghttp2_submit_headers(
+    nghttp2_session *session, uint8_t flags, int32_t stream_id,
+    const nghttp2_priority_spec *pri_spec, const nghttp2_nv *nva, size_t nvlen,
+    void *stream_user_data);
+
+/**
+ * @function
+ *
+ * Submits one or more DATA frames to the stream |stream_id|.  The
+ * data to be sent are provided by |data_prd|.  If |flags| contains
+ * :enum:`NGHTTP2_FLAG_END_STREAM`, the last DATA frame has END_STREAM
+ * flag set.
+ *
+ * This function does not take ownership of the |data_prd|.  The
+ * function copies the members of the |data_prd|.
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * :enum:`NGHTTP2_ERR_NOMEM`
+ *     Out of memory.
+ * :enum:`NGHTTP2_ERR_DATA_EXIST`
+ *     DATA or HEADERS has been already submitted and not fully
+ *     processed yet.
+ * :enum:`NGHTTP2_ERR_INVALID_ARGUMENT`
+ *     The |stream_id| is 0.
+ * :enum:`NGHTTP2_ERR_STREAM_CLOSED`
+ *     The stream was already closed; or the |stream_id| is invalid.
+ *
+ * .. note::
+ *
+ *   Currently, only one DATA or HEADERS is allowed for a stream at a
+ *   time.  Submitting these frames more than once before first DATA
+ *   or HEADERS is finished results in :enum:`NGHTTP2_ERR_DATA_EXIST`
+ *   error code.  The earliest callback which tells that previous
+ *   frame is done is :type:`nghttp2_on_frame_send_callback`.  In side
+ *   that callback, new data can be submitted using
+ *   `nghttp2_submit_data()`.  Of course, all data except for last one
+ *   must not have :enum:`NGHTTP2_FLAG_END_STREAM` flag set in
+ *   |flags|.  This sounds a bit complicated, and we recommend to use
+ *   `nghttp2_submit_request()` and `nghttp2_submit_response()` to
+ *   avoid this cascading issue.  The experience shows that for HTTP
+ *   use, these two functions are enough to implement both client and
+ *   server.
+ */
+NGHTTP2_EXTERN int nghttp2_submit_data(nghttp2_session *session, uint8_t flags,
+                                       int32_t stream_id,
+                                       const nghttp2_data_provider *data_prd);
+
+/**
+ * @function
+ *
+ * Submits PRIORITY frame to change the priority of stream |stream_id|
+ * to the priority specification |pri_spec|.
+ *
+ * The |flags| is currently ignored and should be
+ * :enum:`NGHTTP2_FLAG_NONE`.
+ *
+ * The |pri_spec| is priority specification of this request.  ``NULL``
+ * is not allowed for this function. To specify the priority, use
+ * `nghttp2_priority_spec_init()`.  This function will copy its data
+ * members.
+ *
+ * The ``pri_spec->weight`` must be in [:enum:`NGHTTP2_MIN_WEIGHT`,
+ * :enum:`NGHTTP2_MAX_WEIGHT`], inclusive.  If ``pri_spec->weight`` is
+ * strictly less than :enum:`NGHTTP2_MIN_WEIGHT`, it becomes
+ * :enum:`NGHTTP2_MIN_WEIGHT`.  If it is strictly greater than
+ * :enum:`NGHTTP2_MAX_WEIGHT`, it becomes :enum:`NGHTTP2_MAX_WEIGHT`.
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * :enum:`NGHTTP2_ERR_NOMEM`
+ *     Out of memory.
+ * :enum:`NGHTTP2_ERR_INVALID_ARGUMENT`
+ *     The |stream_id| is 0; or the |pri_spec| is NULL; or trying to
+ *     depend on itself.
+ */
+NGHTTP2_EXTERN int
+nghttp2_submit_priority(nghttp2_session *session, uint8_t flags,
+                        int32_t stream_id,
+                        const nghttp2_priority_spec *pri_spec);
+
+/**
+ * @function
+ *
+ * Submits RST_STREAM frame to cancel/reject the stream |stream_id|
+ * with the error code |error_code|.
+ *
+ * The pre-defined error code is one of :enum:`nghttp2_error_code`.
+ *
+ * The |flags| is currently ignored and should be
+ * :enum:`NGHTTP2_FLAG_NONE`.
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * :enum:`NGHTTP2_ERR_NOMEM`
+ *     Out of memory.
+ * :enum:`NGHTTP2_ERR_INVALID_ARGUMENT`
+ *     The |stream_id| is 0.
+ */
+NGHTTP2_EXTERN int nghttp2_submit_rst_stream(nghttp2_session *session,
+                                             uint8_t flags, int32_t stream_id,
+                                             uint32_t error_code);
+
+/**
+ * @function
+ *
+ * Stores local settings and submits SETTINGS frame.  The |iv| is the
+ * pointer to the array of :type:`nghttp2_settings_entry`.  The |niv|
+ * indicates the number of :type:`nghttp2_settings_entry`.
+ *
+ * The |flags| is currently ignored and should be
+ * :enum:`NGHTTP2_FLAG_NONE`.
+ *
+ * This function does not take ownership of the |iv|.  This function
+ * copies all the elements in the |iv|.
+ *
+ * While updating individual stream's local window size, if the window
+ * size becomes strictly larger than NGHTTP2_MAX_WINDOW_SIZE,
+ * RST_STREAM is issued against such a stream.
+ *
+ * SETTINGS with :enum:`NGHTTP2_FLAG_ACK` is automatically submitted
+ * by the library and application could not send it at its will.
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * :enum:`NGHTTP2_ERR_INVALID_ARGUMENT`
+ *     The |iv| contains invalid value (e.g., initial window size
+ *     strictly greater than (1 << 31) - 1.
+ * :enum:`NGHTTP2_ERR_NOMEM`
+ *     Out of memory.
+ */
+NGHTTP2_EXTERN int nghttp2_submit_settings(nghttp2_session *session,
+                                           uint8_t flags,
+                                           const nghttp2_settings_entry *iv,
+                                           size_t niv);
+
+/**
+ * @function
+ *
+ * Submits PUSH_PROMISE frame.
+ *
+ * The |flags| is currently ignored.  The library handles the
+ * CONTINUATION frame internally and it correctly sets END_HEADERS to
+ * the last sequence of the PUSH_PROMISE or CONTINUATION frame.
+ *
+ * The |stream_id| must be client initiated stream ID.
+ *
+ * The |nva| is an array of name/value pair :type:`nghttp2_nv` with
+ * |nvlen| elements.  The application is responsible to include
+ * required pseudo-header fields (header field whose name starts with
+ * ":") in |nva| and must place pseudo-headers before regular header
+ * fields.
+ *
+ * This function creates copies of all name/value pairs in |nva|.  It
+ * also lower-cases all names in |nva|.  The order of elements in
+ * |nva| is preserved.  For header fields with
+ * :enum:`NGHTTP2_NV_FLAG_NO_COPY_NAME` and
+ * :enum:`NGHTTP2_NV_FLAG_NO_COPY_VALUE` are set, header field name
+ * and value are not copied respectively.  With
+ * :enum:`NGHTTP2_NV_FLAG_NO_COPY_NAME`, application is responsible to
+ * pass header field name in lowercase.  The application should
+ * maintain the references to them until
+ * :type:`nghttp2_on_frame_send_callback` or
+ * :type:`nghttp2_on_frame_not_send_callback` is called.
+ *
+ * The |promised_stream_user_data| is a pointer to an arbitrary data
+ * which is associated to the promised stream this frame will open and
+ * make it in reserved state.  It is available using
+ * `nghttp2_session_get_stream_user_data()`.  The application can
+ * access it in :type:`nghttp2_before_frame_send_callback` and
+ * :type:`nghttp2_on_frame_send_callback` of this frame.
+ *
+ * The client side is not allowed to use this function.
+ *
+ * To submit response headers and data, use
+ * `nghttp2_submit_response()`.
+ *
+ * This function returns assigned promised stream ID if it succeeds,
+ * or one of the following negative error codes:
+ *
+ * :enum:`NGHTTP2_ERR_NOMEM`
+ *     Out of memory.
+ * :enum:`NGHTTP2_ERR_PROTO`
+ *     This function was invoked when |session| is initialized as
+ *     client.
+ * :enum:`NGHTTP2_ERR_STREAM_ID_NOT_AVAILABLE`
+ *     No stream ID is available because maximum stream ID was
+ *     reached.
+ * :enum:`NGHTTP2_ERR_INVALID_ARGUMENT`
+ *     The |stream_id| is 0; The |stream_id| does not designate stream
+ *     that peer initiated.
+ * :enum:`NGHTTP2_ERR_STREAM_CLOSED`
+ *     The stream was already closed; or the |stream_id| is invalid.
+ *
+ * .. warning::
+ *
+ *   This function returns assigned promised stream ID if it succeeds.
+ *   As of 1.16.0, stream object for pushed resource is created when
+ *   this function succeeds.  In that case, the application can submit
+ *   push response for the promised frame.
+ *
+ *   In 1.15.0 or prior versions, pushed stream is not opened yet when
+ *   this function succeeds.  The application must not submit frame to
+ *   that stream ID before :type:`nghttp2_before_frame_send_callback`
+ *   is called for this frame.
+ *
+ */
+NGHTTP2_EXTERN int32_t nghttp2_submit_push_promise(
+    nghttp2_session *session, uint8_t flags, int32_t stream_id,
+    const nghttp2_nv *nva, size_t nvlen, void *promised_stream_user_data);
+
+/**
+ * @function
+ *
+ * Submits PING frame.  You don't have to send PING back when you
+ * received PING frame.  The library automatically submits PING frame
+ * in this case.
+ *
+ * The |flags| is bitwise OR of 0 or more of the following value.
+ *
+ * * :enum:`NGHTTP2_FLAG_ACK`
+ *
+ * Unless `nghttp2_option_set_no_auto_ping_ack()` is used, the |flags|
+ * should be :enum:`NGHTTP2_FLAG_NONE`.
+ *
+ * If the |opaque_data| is non ``NULL``, then it should point to the 8
+ * bytes array of memory to specify opaque data to send with PING
+ * frame.  If the |opaque_data| is ``NULL``, zero-cleared 8 bytes will
+ * be sent as opaque data.
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * :enum:`NGHTTP2_ERR_NOMEM`
+ *     Out of memory.
+ */
+NGHTTP2_EXTERN int nghttp2_submit_ping(nghttp2_session *session, uint8_t flags,
+                                       const uint8_t *opaque_data);
+
+/**
+ * @function
+ *
+ * Submits GOAWAY frame with the last stream ID |last_stream_id| and
+ * the error code |error_code|.
+ *
+ * The pre-defined error code is one of :enum:`nghttp2_error_code`.
+ *
+ * The |flags| is currently ignored and should be
+ * :enum:`NGHTTP2_FLAG_NONE`.
+ *
+ * The |last_stream_id| is peer's stream ID or 0.  So if |session| is
+ * initialized as client, |last_stream_id| must be even or 0.  If
+ * |session| is initialized as server, |last_stream_id| must be odd or
+ * 0.
+ *
+ * The HTTP/2 specification says last_stream_id must not be increased
+ * from the value previously sent.  So the actual value sent as
+ * last_stream_id is the minimum value between the given
+ * |last_stream_id| and the last_stream_id previously sent to the
+ * peer.
+ *
+ * If the |opaque_data| is not ``NULL`` and |opaque_data_len| is not
+ * zero, those data will be sent as additional debug data.  The
+ * library makes a copy of the memory region pointed by |opaque_data|
+ * with the length |opaque_data_len|, so the caller does not need to
+ * keep this memory after the return of this function.  If the
+ * |opaque_data_len| is 0, the |opaque_data| could be ``NULL``.
+ *
+ * After successful transmission of GOAWAY, following things happen.
+ * All incoming streams having strictly more than |last_stream_id| are
+ * closed.  All incoming HEADERS which starts new stream are simply
+ * ignored.  After all active streams are handled, both
+ * `nghttp2_session_want_read()` and `nghttp2_session_want_write()`
+ * return 0 and the application can close session.
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * :enum:`NGHTTP2_ERR_NOMEM`
+ *     Out of memory.
+ * :enum:`NGHTTP2_ERR_INVALID_ARGUMENT`
+ *     The |opaque_data_len| is too large; the |last_stream_id| is
+ *     invalid.
+ */
+NGHTTP2_EXTERN int nghttp2_submit_goaway(nghttp2_session *session,
+                                         uint8_t flags, int32_t last_stream_id,
+                                         uint32_t error_code,
+                                         const uint8_t *opaque_data,
+                                         size_t opaque_data_len);
+
+/**
+ * @function
+ *
+ * Returns the last stream ID of a stream for which
+ * :type:`nghttp2_on_frame_recv_callback` was invoked most recently.
+ * The returned value can be used as last_stream_id parameter for
+ * `nghttp2_submit_goaway()` and
+ * `nghttp2_session_terminate_session2()`.
+ *
+ * This function always succeeds.
+ */
+NGHTTP2_EXTERN int32_t
+nghttp2_session_get_last_proc_stream_id(nghttp2_session *session);
+
+/**
+ * @function
+ *
+ * Returns nonzero if new request can be sent from local endpoint.
+ *
+ * This function return 0 if request is not allowed for this session.
+ * There are several reasons why request is not allowed.  Some of the
+ * reasons are: session is server; stream ID has been spent; GOAWAY
+ * has been sent or received.
+ *
+ * The application can call `nghttp2_submit_request()` without
+ * consulting this function.  In that case, `nghttp2_submit_request()`
+ * may return error.  Or, request is failed to sent, and
+ * :type:`nghttp2_on_stream_close_callback` is called.
+ */
+NGHTTP2_EXTERN int
+nghttp2_session_check_request_allowed(nghttp2_session *session);
+
+/**
+ * @function
+ *
+ * Returns nonzero if |session| is initialized as server side session.
+ */
+NGHTTP2_EXTERN int
+nghttp2_session_check_server_session(nghttp2_session *session);
+
+/**
+ * @function
+ *
+ * Submits WINDOW_UPDATE frame.
+ *
+ * The |flags| is currently ignored and should be
+ * :enum:`NGHTTP2_FLAG_NONE`.
+ *
+ * The |stream_id| is the stream ID to send this WINDOW_UPDATE.  To
+ * send connection level WINDOW_UPDATE, specify 0 to |stream_id|.
+ *
+ * If the |window_size_increment| is positive, the WINDOW_UPDATE with
+ * that value as window_size_increment is queued.  If the
+ * |window_size_increment| is larger than the received bytes from the
+ * remote endpoint, the local window size is increased by that
+ * difference.  If the sole purpose is to increase the local window
+ * size, consider to use `nghttp2_session_set_local_window_size()`.
+ *
+ * If the |window_size_increment| is negative, the local window size
+ * is decreased by -|window_size_increment|.  If automatic
+ * WINDOW_UPDATE is enabled
+ * (`nghttp2_option_set_no_auto_window_update()`), and the library
+ * decided that the WINDOW_UPDATE should be submitted, then
+ * WINDOW_UPDATE is queued with the current received bytes count.  If
+ * the sole purpose is to decrease the local window size, consider to
+ * use `nghttp2_session_set_local_window_size()`.
+ *
+ * If the |window_size_increment| is 0, the function does nothing and
+ * returns 0.
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * :enum:`NGHTTP2_ERR_FLOW_CONTROL`
+ *     The local window size overflow or gets negative.
+ * :enum:`NGHTTP2_ERR_NOMEM`
+ *     Out of memory.
+ */
+NGHTTP2_EXTERN int nghttp2_submit_window_update(nghttp2_session *session,
+                                                uint8_t flags,
+                                                int32_t stream_id,
+                                                int32_t window_size_increment);
+
+/**
+ * @function
+ *
+ * Set local window size (local endpoints's window size) to the given
+ * |window_size| for the given stream denoted by |stream_id|.  To
+ * change connection level window size, specify 0 to |stream_id|.  To
+ * increase window size, this function may submit WINDOW_UPDATE frame
+ * to transmission queue.
+ *
+ * The |flags| is currently ignored and should be
+ * :enum:`NGHTTP2_FLAG_NONE`.
+ *
+ * This sounds similar to `nghttp2_submit_window_update()`, but there
+ * are 2 differences.  The first difference is that this function
+ * takes the absolute value of window size to set, rather than the
+ * delta.  To change the window size, this may be easier to use since
+ * the application just declares the intended window size, rather than
+ * calculating delta.  The second difference is that
+ * `nghttp2_submit_window_update()` affects the received bytes count
+ * which has not acked yet.  By the specification of
+ * `nghttp2_submit_window_update()`, to strictly increase the local
+ * window size, we have to submit delta including all received bytes
+ * count, which might not be desirable in some cases.  On the other
+ * hand, this function does not affect the received bytes count.  It
+ * just sets the local window size to the given value.
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * :enum:`NGHTTP2_ERR_INVALID_ARGUMENT`
+ *     The |stream_id| is negative.
+ * :enum:`NGHTTP2_ERR_NOMEM`
+ *     Out of memory.
+ */
+NGHTTP2_EXTERN int
+nghttp2_session_set_local_window_size(nghttp2_session *session, uint8_t flags,
+                                      int32_t stream_id, int32_t window_size);
+
+/**
+ * @function
+ *
+ * Submits extension frame.
+ *
+ * Application can pass arbitrary frame flags and stream ID in |flags|
+ * and |stream_id| respectively.  The |payload| is opaque pointer, and
+ * it can be accessible though ``frame->ext.payload`` in
+ * :type:`nghttp2_pack_extension_callback`.  The library will not own
+ * passed |payload| pointer.
+ *
+ * The application must set :type:`nghttp2_pack_extension_callback`
+ * using `nghttp2_session_callbacks_set_pack_extension_callback()`.
+ *
+ * The application should retain the memory pointed by |payload| until
+ * the transmission of extension frame is done (which is indicated by
+ * :type:`nghttp2_on_frame_send_callback`), or transmission fails
+ * (which is indicated by :type:`nghttp2_on_frame_not_send_callback`).
+ * If application does not touch this memory region after packing it
+ * into a wire format, application can free it inside
+ * :type:`nghttp2_pack_extension_callback`.
+ *
+ * The standard HTTP/2 frame cannot be sent with this function, so
+ * |type| must be strictly grater than 0x9.  Otherwise, this function
+ * will fail with error code :enum:`NGHTTP2_ERR_INVALID_ARGUMENT`.
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * :enum:`NGHTTP2_ERR_INVALID_STATE`
+ *     If :type:`nghttp2_pack_extension_callback` is not set.
+ * :enum:`NGHTTP2_ERR_INVALID_ARGUMENT`
+ *     If  |type| specifies  standard  HTTP/2 frame  type.  The  frame
+ *     types  in the  rage [0x0,  0x9], both  inclusive, are  standard
+ *     HTTP/2 frame type, and cannot be sent using this function.
+ * :enum:`NGHTTP2_ERR_NOMEM`
+ *     Out of memory
+ */
+NGHTTP2_EXTERN int nghttp2_submit_extension(nghttp2_session *session,
+                                            uint8_t type, uint8_t flags,
+                                            int32_t stream_id, void *payload);
+
+/**
+ * @struct
+ *
+ * The payload of ALTSVC frame.  ALTSVC frame is a non-critical
+ * extension to HTTP/2.  If this frame is received, and
+ * `nghttp2_option_set_user_recv_extension_type()` is not set, and
+ * `nghttp2_option_set_builtin_recv_extension_type()` is set for
+ * :enum:`NGHTTP2_ALTSVC`, ``nghttp2_extension.payload`` will point to
+ * this struct.
+ *
+ * It has the following members:
+ */
+typedef struct {
+  /**
+   * The pointer to origin which this alternative service is
+   * associated with.  This is not necessarily NULL-terminated.
+   */
+  uint8_t *origin;
+  /**
+   * The length of the |origin|.
+   */
+  size_t origin_len;
+  /**
+   * The pointer to Alt-Svc field value contained in ALTSVC frame.
+   * This is not necessarily NULL-terminated.
+   */
+  uint8_t *field_value;
+  /**
+   * The length of the |field_value|.
+   */
+  size_t field_value_len;
+} nghttp2_ext_altsvc;
+
+/**
+ * @function
+ *
+ * Submits ALTSVC frame.
+ *
+ * ALTSVC frame is a non-critical extension to HTTP/2, and defined in
+ * is defined in `RFC 7383
+ * <https://tools.ietf.org/html/rfc7838#section-4>`_.
+ *
+ * The |flags| is currently ignored and should be
+ * :enum:`NGHTTP2_FLAG_NONE`.
+ *
+ * The |origin| points to the origin this alternative service is
+ * associated with.  The |origin_len| is the length of the origin.  If
+ * |stream_id| is 0, the origin must be specified.  If |stream_id| is
+ * not zero, the origin must be empty (in other words, |origin_len|
+ * must be 0).
+ *
+ * The ALTSVC frame is only usable from server side.  If this function
+ * is invoked with client side session, this function returns
+ * :enum:`NGHTTP2_ERR_INVALID_STATE`.
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * :enum:`NGHTTP2_ERR_NOMEM`
+ *     Out of memory
+ * :enum:`NGHTTP2_ERR_INVALID_STATE`
+ *     The function is called from client side session
+ * :enum:`NGHTTP2_ERR_INVALID_ARGUMENT`
+ *     The sum of |origin_len| and |field_value_len| is larger than
+ *     16382; or |origin_len| is 0 while |stream_id| is 0; or
+ *     |origin_len| is not 0 while |stream_id| is not 0.
+ */
+NGHTTP2_EXTERN int nghttp2_submit_altsvc(nghttp2_session *session,
+                                         uint8_t flags, int32_t stream_id,
+                                         const uint8_t *origin,
+                                         size_t origin_len,
+                                         const uint8_t *field_value,
+                                         size_t field_value_len);
+
+/**
+ * @function
+ *
+ * Compares ``lhs->name`` of length ``lhs->namelen`` bytes and
+ * ``rhs->name`` of length ``rhs->namelen`` bytes.  Returns negative
+ * integer if ``lhs->name`` is found to be less than ``rhs->name``; or
+ * returns positive integer if ``lhs->name`` is found to be greater
+ * than ``rhs->name``; or returns 0 otherwise.
+ */
+NGHTTP2_EXTERN int nghttp2_nv_compare_name(const nghttp2_nv *lhs,
+                                           const nghttp2_nv *rhs);
+
+/**
+ * @function
+ *
+ * A helper function for dealing with NPN in client side or ALPN in
+ * server side.  The |in| contains peer's protocol list in preferable
+ * order.  The format of |in| is length-prefixed and not
+ * null-terminated.  For example, ``h2`` and
+ * ``http/1.1`` stored in |in| like this::
+ *
+ *     in[0] = 2
+ *     in[1..2] = "h2"
+ *     in[3] = 8
+ *     in[4..11] = "http/1.1"
+ *     inlen = 12
+ *
+ * The selection algorithm is as follows:
+ *
+ * 1. If peer's list contains HTTP/2 protocol the library supports,
+ *    it is selected and returns 1. The following step is not taken.
+ *
+ * 2. If peer's list contains ``http/1.1``, this function selects
+ *    ``http/1.1`` and returns 0.  The following step is not taken.
+ *
+ * 3. This function selects nothing and returns -1 (So called
+ *    non-overlap case).  In this case, |out| and |outlen| are left
+ *    untouched.
+ *
+ * Selecting ``h2`` means that ``h2`` is written into |*out| and its
+ * length (which is 2) is assigned to |*outlen|.
+ *
+ * For ALPN, refer to https://tools.ietf.org/html/rfc7301
+ *
+ * See http://technotes.googlecode.com/git/nextprotoneg.html for more
+ * details about NPN.
+ *
+ * For NPN, to use this method you should do something like::
+ *
+ *     static int select_next_proto_cb(SSL* ssl,
+ *                                     unsigned char **out,
+ *                                     unsigned char *outlen,
+ *                                     const unsigned char *in,
+ *                                     unsigned int inlen,
+ *                                     void *arg)
+ *     {
+ *         int rv;
+ *         rv = nghttp2_select_next_protocol(out, outlen, in, inlen);
+ *         if (rv == -1) {
+ *             return SSL_TLSEXT_ERR_NOACK;
+ *         }
+ *         if (rv == 1) {
+ *             ((MyType*)arg)->http2_selected = 1;
+ *         }
+ *         return SSL_TLSEXT_ERR_OK;
+ *     }
+ *     ...
+ *     SSL_CTX_set_next_proto_select_cb(ssl_ctx, select_next_proto_cb, my_obj);
+ *
+ */
+NGHTTP2_EXTERN int nghttp2_select_next_protocol(unsigned char **out,
+                                                unsigned char *outlen,
+                                                const unsigned char *in,
+                                                unsigned int inlen);
+
+/**
+ * @function
+ *
+ * Returns a pointer to a nghttp2_info struct with version information
+ * about the run-time library in use.  The |least_version| argument
+ * can be set to a 24 bit numerical value for the least accepted
+ * version number and if the condition is not met, this function will
+ * return a ``NULL``.  Pass in 0 to skip the version checking.
+ */
+NGHTTP2_EXTERN nghttp2_info *nghttp2_version(int least_version);
+
+/**
+ * @function
+ *
+ * Returns nonzero if the :type:`nghttp2_error` library error code
+ * |lib_error| is fatal.
+ */
+NGHTTP2_EXTERN int nghttp2_is_fatal(int lib_error_code);
+
+/**
+ * @function
+ *
+ * Returns nonzero if HTTP header field name |name| of length |len| is
+ * valid according to http://tools.ietf.org/html/rfc7230#section-3.2
+ *
+ * Because this is a header field name in HTTP2, the upper cased alphabet
+ * is treated as error.
+ */
+NGHTTP2_EXTERN int nghttp2_check_header_name(const uint8_t *name, size_t len);
+
+/**
+ * @function
+ *
+ * Returns nonzero if HTTP header field value |value| of length |len|
+ * is valid according to
+ * http://tools.ietf.org/html/rfc7230#section-3.2
+ */
+NGHTTP2_EXTERN int nghttp2_check_header_value(const uint8_t *value, size_t len);
+
+/* HPACK API */
+
+struct nghttp2_hd_deflater;
+
+/**
+ * @struct
+ *
+ * HPACK deflater object.
+ */
+typedef struct nghttp2_hd_deflater nghttp2_hd_deflater;
+
+/**
+ * @function
+ *
+ * Initializes |*deflater_ptr| for deflating name/values pairs.
+ *
+ * The |max_deflate_dynamic_table_size| is the upper bound of header
+ * table size the deflater will use.
+ *
+ * If this function fails, |*deflater_ptr| is left untouched.
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * :enum:`NGHTTP2_ERR_NOMEM`
+ *     Out of memory.
+ */
+NGHTTP2_EXTERN int
+nghttp2_hd_deflate_new(nghttp2_hd_deflater **deflater_ptr,
+                       size_t max_deflate_dynamic_table_size);
+
+/**
+ * @function
+ *
+ * Like `nghttp2_hd_deflate_new()`, but with additional custom memory
+ * allocator specified in the |mem|.
+ *
+ * The |mem| can be ``NULL`` and the call is equivalent to
+ * `nghttp2_hd_deflate_new()`.
+ *
+ * This function does not take ownership |mem|.  The application is
+ * responsible for freeing |mem|.
+ *
+ * The library code does not refer to |mem| pointer after this
+ * function returns, so the application can safely free it.
+ */
+NGHTTP2_EXTERN int
+nghttp2_hd_deflate_new2(nghttp2_hd_deflater **deflater_ptr,
+                        size_t max_deflate_dynamic_table_size,
+                        nghttp2_mem *mem);
+
+/**
+ * @function
+ *
+ * Deallocates any resources allocated for |deflater|.
+ */
+NGHTTP2_EXTERN void nghttp2_hd_deflate_del(nghttp2_hd_deflater *deflater);
+
+/**
+ * @function
+ *
+ * Changes header table size of the |deflater| to
+ * |settings_max_dynamic_table_size| bytes.  This may trigger eviction
+ * in the dynamic table.
+ *
+ * The |settings_max_dynamic_table_size| should be the value received
+ * in SETTINGS_HEADER_TABLE_SIZE.
+ *
+ * The deflater never uses more memory than
+ * ``max_deflate_dynamic_table_size`` bytes specified in
+ * `nghttp2_hd_deflate_new()`.  Therefore, if
+ * |settings_max_dynamic_table_size| >
+ * ``max_deflate_dynamic_table_size``, resulting maximum table size
+ * becomes ``max_deflate_dynamic_table_size``.
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * :enum:`NGHTTP2_ERR_NOMEM`
+ *     Out of memory.
+ */
+NGHTTP2_EXTERN int
+nghttp2_hd_deflate_change_table_size(nghttp2_hd_deflater *deflater,
+                                     size_t settings_max_dynamic_table_size);
+
+/**
+ * @function
+ *
+ * Deflates the |nva|, which has the |nvlen| name/value pairs, into
+ * the |buf| of length |buflen|.
+ *
+ * If |buf| is not large enough to store the deflated header block,
+ * this function fails with :enum:`NGHTTP2_ERR_INSUFF_BUFSIZE`.  The
+ * caller should use `nghttp2_hd_deflate_bound()` to know the upper
+ * bound of buffer size required to deflate given header name/value
+ * pairs.
+ *
+ * Once this function fails, subsequent call of this function always
+ * returns :enum:`NGHTTP2_ERR_HEADER_COMP`.
+ *
+ * After this function returns, it is safe to delete the |nva|.
+ *
+ * This function returns the number of bytes written to |buf| if it
+ * succeeds, or one of the following negative error codes:
+ *
+ * :enum:`NGHTTP2_ERR_NOMEM`
+ *     Out of memory.
+ * :enum:`NGHTTP2_ERR_HEADER_COMP`
+ *     Deflation process has failed.
+ * :enum:`NGHTTP2_ERR_INSUFF_BUFSIZE`
+ *     The provided |buflen| size is too small to hold the output.
+ */
+NGHTTP2_EXTERN ssize_t nghttp2_hd_deflate_hd(nghttp2_hd_deflater *deflater,
+                                             uint8_t *buf, size_t buflen,
+                                             const nghttp2_nv *nva,
+                                             size_t nvlen);
+
+/**
+ * @function
+ *
+ * Deflates the |nva|, which has the |nvlen| name/value pairs, into
+ * the |veclen| size of buf vector |vec|.  The each size of buffer
+ * must be set in len field of :type:`nghttp2_vec`.  If and only if
+ * one chunk is filled up completely, next chunk will be used.  If
+ * |vec| is not large enough to store the deflated header block, this
+ * function fails with :enum:`NGHTTP2_ERR_INSUFF_BUFSIZE`.  The caller
+ * should use `nghttp2_hd_deflate_bound()` to know the upper bound of
+ * buffer size required to deflate given header name/value pairs.
+ *
+ * Once this function fails, subsequent call of this function always
+ * returns :enum:`NGHTTP2_ERR_HEADER_COMP`.
+ *
+ * After this function returns, it is safe to delete the |nva|.
+ *
+ * This function returns the number of bytes written to |vec| if it
+ * succeeds, or one of the following negative error codes:
+ *
+ * :enum:`NGHTTP2_ERR_NOMEM`
+ *     Out of memory.
+ * :enum:`NGHTTP2_ERR_HEADER_COMP`
+ *     Deflation process has failed.
+ * :enum:`NGHTTP2_ERR_INSUFF_BUFSIZE`
+ *     The provided |buflen| size is too small to hold the output.
+ */
+NGHTTP2_EXTERN ssize_t nghttp2_hd_deflate_hd_vec(nghttp2_hd_deflater *deflater,
+                                                 const nghttp2_vec *vec,
+                                                 size_t veclen,
+                                                 const nghttp2_nv *nva,
+                                                 size_t nvlen);
+
+/**
+ * @function
+ *
+ * Returns an upper bound on the compressed size after deflation of
+ * |nva| of length |nvlen|.
+ */
+NGHTTP2_EXTERN size_t nghttp2_hd_deflate_bound(nghttp2_hd_deflater *deflater,
+                                               const nghttp2_nv *nva,
+                                               size_t nvlen);
+
+/**
+ * @function
+ *
+ * Returns the number of entries that header table of |deflater|
+ * contains.  This is the sum of the number of static table and
+ * dynamic table, so the return value is at least 61.
+ */
+NGHTTP2_EXTERN
+size_t nghttp2_hd_deflate_get_num_table_entries(nghttp2_hd_deflater *deflater);
+
+/**
+ * @function
+ *
+ * Returns the table entry denoted by |idx| from header table of
+ * |deflater|.  The |idx| is 1-based, and idx=1 returns first entry of
+ * static table.  idx=62 returns first entry of dynamic table if it
+ * exists.  Specifying idx=0 is error, and this function returns NULL.
+ * If |idx| is strictly greater than the number of entries the tables
+ * contain, this function returns NULL.
+ */
+NGHTTP2_EXTERN
+const nghttp2_nv *
+nghttp2_hd_deflate_get_table_entry(nghttp2_hd_deflater *deflater, size_t idx);
+
+/**
+ * @function
+ *
+ * Returns the used dynamic table size, including the overhead 32
+ * bytes per entry described in RFC 7541.
+ */
+NGHTTP2_EXTERN
+size_t nghttp2_hd_deflate_get_dynamic_table_size(nghttp2_hd_deflater *deflater);
+
+/**
+ * @function
+ *
+ * Returns the maximum dynamic table size.
+ */
+NGHTTP2_EXTERN
+size_t
+nghttp2_hd_deflate_get_max_dynamic_table_size(nghttp2_hd_deflater *deflater);
+
+struct nghttp2_hd_inflater;
+
+/**
+ * @struct
+ *
+ * HPACK inflater object.
+ */
+typedef struct nghttp2_hd_inflater nghttp2_hd_inflater;
+
+/**
+ * @function
+ *
+ * Initializes |*inflater_ptr| for inflating name/values pairs.
+ *
+ * If this function fails, |*inflater_ptr| is left untouched.
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * :enum:`NGHTTP2_ERR_NOMEM`
+ *     Out of memory.
+ */
+NGHTTP2_EXTERN int nghttp2_hd_inflate_new(nghttp2_hd_inflater **inflater_ptr);
+
+/**
+ * @function
+ *
+ * Like `nghttp2_hd_inflate_new()`, but with additional custom memory
+ * allocator specified in the |mem|.
+ *
+ * The |mem| can be ``NULL`` and the call is equivalent to
+ * `nghttp2_hd_inflate_new()`.
+ *
+ * This function does not take ownership |mem|.  The application is
+ * responsible for freeing |mem|.
+ *
+ * The library code does not refer to |mem| pointer after this
+ * function returns, so the application can safely free it.
+ */
+NGHTTP2_EXTERN int nghttp2_hd_inflate_new2(nghttp2_hd_inflater **inflater_ptr,
+                                           nghttp2_mem *mem);
+
+/**
+ * @function
+ *
+ * Deallocates any resources allocated for |inflater|.
+ */
+NGHTTP2_EXTERN void nghttp2_hd_inflate_del(nghttp2_hd_inflater *inflater);
+
+/**
+ * @function
+ *
+ * Changes header table size in the |inflater|.  This may trigger
+ * eviction in the dynamic table.
+ *
+ * The |settings_max_dynamic_table_size| should be the value
+ * transmitted in SETTINGS_HEADER_TABLE_SIZE.
+ *
+ * This function must not be called while header block is being
+ * inflated.  In other words, this function must be called after
+ * initialization of |inflater|, but before calling
+ * `nghttp2_hd_inflate_hd2()`, or after
+ * `nghttp2_hd_inflate_end_headers()`.  Otherwise,
+ * `NGHTTP2_ERR_INVALID_STATE` was returned.
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * :enum:`NGHTTP2_ERR_NOMEM`
+ *     Out of memory.
+ * :enum:`NGHTTP2_ERR_INVALID_STATE`
+ *     The function is called while header block is being inflated.
+ *     Probably, application missed to call
+ *     `nghttp2_hd_inflate_end_headers()`.
+ */
+NGHTTP2_EXTERN int
+nghttp2_hd_inflate_change_table_size(nghttp2_hd_inflater *inflater,
+                                     size_t settings_max_dynamic_table_size);
+
+/**
+ * @enum
+ *
+ * The flags for header inflation.
+ */
+typedef enum {
+  /**
+   * No flag set.
+   */
+  NGHTTP2_HD_INFLATE_NONE = 0,
+  /**
+   * Indicates all headers were inflated.
+   */
+  NGHTTP2_HD_INFLATE_FINAL = 0x01,
+  /**
+   * Indicates a header was emitted.
+   */
+  NGHTTP2_HD_INFLATE_EMIT = 0x02
+} nghttp2_hd_inflate_flag;
+
+/**
+ * @function
+ *
+ * .. warning::
+ *
+ *   Deprecated.  Use `nghttp2_hd_inflate_hd2()` instead.
+ *
+ * Inflates name/value block stored in |in| with length |inlen|.  This
+ * function performs decompression.  For each successful emission of
+ * header name/value pair, :enum:`NGHTTP2_HD_INFLATE_EMIT` is set in
+ * |*inflate_flags| and name/value pair is assigned to the |nv_out|
+ * and the function returns.  The caller must not free the members of
+ * |nv_out|.
+ *
+ * The |nv_out| may include pointers to the memory region in the |in|.
+ * The caller must retain the |in| while the |nv_out| is used.
+ *
+ * The application should call this function repeatedly until the
+ * ``(*inflate_flags) & NGHTTP2_HD_INFLATE_FINAL`` is nonzero and
+ * return value is non-negative.  This means the all input values are
+ * processed successfully.  Then the application must call
+ * `nghttp2_hd_inflate_end_headers()` to prepare for the next header
+ * block input.
+ *
+ * The caller can feed complete compressed header block.  It also can
+ * feed it in several chunks.  The caller must set |in_final| to
+ * nonzero if the given input is the last block of the compressed
+ * header.
+ *
+ * This function returns the number of bytes processed if it succeeds,
+ * or one of the following negative error codes:
+ *
+ * :enum:`NGHTTP2_ERR_NOMEM`
+ *     Out of memory.
+ * :enum:`NGHTTP2_ERR_HEADER_COMP`
+ *     Inflation process has failed.
+ * :enum:`NGHTTP2_ERR_BUFFER_ERROR`
+ *     The header field name or value is too large.
+ *
+ * Example follows::
+ *
+ *     int inflate_header_block(nghttp2_hd_inflater *hd_inflater,
+ *                              uint8_t *in, size_t inlen, int final)
+ *     {
+ *         ssize_t rv;
+ *
+ *         for(;;) {
+ *             nghttp2_nv nv;
+ *             int inflate_flags = 0;
+ *
+ *             rv = nghttp2_hd_inflate_hd(hd_inflater, &nv, &inflate_flags,
+ *                                        in, inlen, final);
+ *
+ *             if(rv < 0) {
+ *                 fprintf(stderr, "inflate failed with error code %zd", rv);
+ *                 return -1;
+ *             }
+ *
+ *             in += rv;
+ *             inlen -= rv;
+ *
+ *             if(inflate_flags & NGHTTP2_HD_INFLATE_EMIT) {
+ *                 fwrite(nv.name, nv.namelen, 1, stderr);
+ *                 fprintf(stderr, ": ");
+ *                 fwrite(nv.value, nv.valuelen, 1, stderr);
+ *                 fprintf(stderr, "\n");
+ *             }
+ *             if(inflate_flags & NGHTTP2_HD_INFLATE_FINAL) {
+ *                 nghttp2_hd_inflate_end_headers(hd_inflater);
+ *                 break;
+ *             }
+ *             if((inflate_flags & NGHTTP2_HD_INFLATE_EMIT) == 0 &&
+ *                inlen == 0) {
+ *                break;
+ *             }
+ *         }
+ *
+ *         return 0;
+ *     }
+ *
+ */
+NGHTTP2_EXTERN ssize_t nghttp2_hd_inflate_hd(nghttp2_hd_inflater *inflater,
+                                             nghttp2_nv *nv_out,
+                                             int *inflate_flags, uint8_t *in,
+                                             size_t inlen, int in_final);
+
+/**
+ * @function
+ *
+ * Inflates name/value block stored in |in| with length |inlen|.  This
+ * function performs decompression.  For each successful emission of
+ * header name/value pair, :enum:`NGHTTP2_HD_INFLATE_EMIT` is set in
+ * |*inflate_flags| and name/value pair is assigned to the |nv_out|
+ * and the function returns.  The caller must not free the members of
+ * |nv_out|.
+ *
+ * The |nv_out| may include pointers to the memory region in the |in|.
+ * The caller must retain the |in| while the |nv_out| is used.
+ *
+ * The application should call this function repeatedly until the
+ * ``(*inflate_flags) & NGHTTP2_HD_INFLATE_FINAL`` is nonzero and
+ * return value is non-negative.  If that happens, all given input
+ * data (|inlen| bytes) are processed successfully.  Then the
+ * application must call `nghttp2_hd_inflate_end_headers()` to prepare
+ * for the next header block input.
+ *
+ * In other words, if |in_final| is nonzero, and this function returns
+ * |inlen|, you can assert that :enum:`NGHTTP2_HD_INFLATE_FINAL` is
+ * set in |*inflate_flags|.
+ *
+ * The caller can feed complete compressed header block.  It also can
+ * feed it in several chunks.  The caller must set |in_final| to
+ * nonzero if the given input is the last block of the compressed
+ * header.
+ *
+ * This function returns the number of bytes processed if it succeeds,
+ * or one of the following negative error codes:
+ *
+ * :enum:`NGHTTP2_ERR_NOMEM`
+ *     Out of memory.
+ * :enum:`NGHTTP2_ERR_HEADER_COMP`
+ *     Inflation process has failed.
+ * :enum:`NGHTTP2_ERR_BUFFER_ERROR`
+ *     The header field name or value is too large.
+ *
+ * Example follows::
+ *
+ *     int inflate_header_block(nghttp2_hd_inflater *hd_inflater,
+ *                              uint8_t *in, size_t inlen, int final)
+ *     {
+ *         ssize_t rv;
+ *
+ *         for(;;) {
+ *             nghttp2_nv nv;
+ *             int inflate_flags = 0;
+ *
+ *             rv = nghttp2_hd_inflate_hd2(hd_inflater, &nv, &inflate_flags,
+ *                                         in, inlen, final);
+ *
+ *             if(rv < 0) {
+ *                 fprintf(stderr, "inflate failed with error code %zd", rv);
+ *                 return -1;
+ *             }
+ *
+ *             in += rv;
+ *             inlen -= rv;
+ *
+ *             if(inflate_flags & NGHTTP2_HD_INFLATE_EMIT) {
+ *                 fwrite(nv.name, nv.namelen, 1, stderr);
+ *                 fprintf(stderr, ": ");
+ *                 fwrite(nv.value, nv.valuelen, 1, stderr);
+ *                 fprintf(stderr, "\n");
+ *             }
+ *             if(inflate_flags & NGHTTP2_HD_INFLATE_FINAL) {
+ *                 nghttp2_hd_inflate_end_headers(hd_inflater);
+ *                 break;
+ *             }
+ *             if((inflate_flags & NGHTTP2_HD_INFLATE_EMIT) == 0 &&
+ *                inlen == 0) {
+ *                break;
+ *             }
+ *         }
+ *
+ *         return 0;
+ *     }
+ *
+ */
+NGHTTP2_EXTERN ssize_t nghttp2_hd_inflate_hd2(nghttp2_hd_inflater *inflater,
+                                              nghttp2_nv *nv_out,
+                                              int *inflate_flags,
+                                              const uint8_t *in, size_t inlen,
+                                              int in_final);
+
+/**
+ * @function
+ *
+ * Signals the end of decompression for one header block.
+ *
+ * This function returns 0 if it succeeds. Currently this function
+ * always succeeds.
+ */
+NGHTTP2_EXTERN int
+nghttp2_hd_inflate_end_headers(nghttp2_hd_inflater *inflater);
+
+/**
+ * @function
+ *
+ * Returns the number of entries that header table of |inflater|
+ * contains.  This is the sum of the number of static table and
+ * dynamic table, so the return value is at least 61.
+ */
+NGHTTP2_EXTERN
+size_t nghttp2_hd_inflate_get_num_table_entries(nghttp2_hd_inflater *inflater);
+
+/**
+ * @function
+ *
+ * Returns the table entry denoted by |idx| from header table of
+ * |inflater|.  The |idx| is 1-based, and idx=1 returns first entry of
+ * static table.  idx=62 returns first entry of dynamic table if it
+ * exists.  Specifying idx=0 is error, and this function returns NULL.
+ * If |idx| is strictly greater than the number of entries the tables
+ * contain, this function returns NULL.
+ */
+NGHTTP2_EXTERN
+const nghttp2_nv *
+nghttp2_hd_inflate_get_table_entry(nghttp2_hd_inflater *inflater, size_t idx);
+
+/**
+ * @function
+ *
+ * Returns the used dynamic table size, including the overhead 32
+ * bytes per entry described in RFC 7541.
+ */
+NGHTTP2_EXTERN
+size_t nghttp2_hd_inflate_get_dynamic_table_size(nghttp2_hd_inflater *inflater);
+
+/**
+ * @function
+ *
+ * Returns the maximum dynamic table size.
+ */
+NGHTTP2_EXTERN
+size_t
+nghttp2_hd_inflate_get_max_dynamic_table_size(nghttp2_hd_inflater *inflater);
+
+struct nghttp2_stream;
+
+/**
+ * @struct
+ *
+ * The structure to represent HTTP/2 stream.  The details of this
+ * structure are intentionally hidden from the public API.
+ */
+typedef struct nghttp2_stream nghttp2_stream;
+
+/**
+ * @function
+ *
+ * Returns pointer to :type:`nghttp2_stream` object denoted by
+ * |stream_id|.  If stream was not found, returns NULL.
+ *
+ * Returns imaginary root stream (see
+ * `nghttp2_session_get_root_stream()`) if 0 is given in |stream_id|.
+ *
+ * Unless |stream_id| == 0, the returned pointer is valid until next
+ * call of `nghttp2_session_send()`, `nghttp2_session_mem_send()`,
+ * `nghttp2_session_recv()`, and `nghttp2_session_mem_recv()`.
+ */
+NGHTTP2_EXTERN nghttp2_stream *
+nghttp2_session_find_stream(nghttp2_session *session, int32_t stream_id);
+
+/**
+ * @enum
+ *
+ * State of stream as described in RFC 7540.
+ */
+typedef enum {
+  /**
+   * idle state.
+   */
+  NGHTTP2_STREAM_STATE_IDLE = 1,
+  /**
+   * open state.
+   */
+  NGHTTP2_STREAM_STATE_OPEN,
+  /**
+   * reserved (local) state.
+   */
+  NGHTTP2_STREAM_STATE_RESERVED_LOCAL,
+  /**
+   * reserved (remote) state.
+   */
+  NGHTTP2_STREAM_STATE_RESERVED_REMOTE,
+  /**
+   * half closed (local) state.
+   */
+  NGHTTP2_STREAM_STATE_HALF_CLOSED_LOCAL,
+  /**
+   * half closed (remote) state.
+   */
+  NGHTTP2_STREAM_STATE_HALF_CLOSED_REMOTE,
+  /**
+   * closed state.
+   */
+  NGHTTP2_STREAM_STATE_CLOSED
+} nghttp2_stream_proto_state;
+
+/**
+ * @function
+ *
+ * Returns state of |stream|.  The root stream retrieved by
+ * `nghttp2_session_get_root_stream()` will have stream state
+ * :enum:`NGHTTP2_STREAM_STATE_IDLE`.
+ */
+NGHTTP2_EXTERN nghttp2_stream_proto_state
+nghttp2_stream_get_state(nghttp2_stream *stream);
+
+/**
+ * @function
+ *
+ * Returns root of dependency tree, which is imaginary stream with
+ * stream ID 0.  The returned pointer is valid until |session| is
+ * freed by `nghttp2_session_del()`.
+ */
+NGHTTP2_EXTERN nghttp2_stream *
+nghttp2_session_get_root_stream(nghttp2_session *session);
+
+/**
+ * @function
+ *
+ * Returns the parent stream of |stream| in dependency tree.  Returns
+ * NULL if there is no such stream.
+ */
+NGHTTP2_EXTERN nghttp2_stream *
+nghttp2_stream_get_parent(nghttp2_stream *stream);
+
+NGHTTP2_EXTERN int32_t nghttp2_stream_get_stream_id(nghttp2_stream *stream);
+
+/**
+ * @function
+ *
+ * Returns the next sibling stream of |stream| in dependency tree.
+ * Returns NULL if there is no such stream.
+ */
+NGHTTP2_EXTERN nghttp2_stream *
+nghttp2_stream_get_next_sibling(nghttp2_stream *stream);
+
+/**
+ * @function
+ *
+ * Returns the previous sibling stream of |stream| in dependency tree.
+ * Returns NULL if there is no such stream.
+ */
+NGHTTP2_EXTERN nghttp2_stream *
+nghttp2_stream_get_previous_sibling(nghttp2_stream *stream);
+
+/**
+ * @function
+ *
+ * Returns the first child stream of |stream| in dependency tree.
+ * Returns NULL if there is no such stream.
+ */
+NGHTTP2_EXTERN nghttp2_stream *
+nghttp2_stream_get_first_child(nghttp2_stream *stream);
+
+/**
+ * @function
+ *
+ * Returns dependency weight to the parent stream of |stream|.
+ */
+NGHTTP2_EXTERN int32_t nghttp2_stream_get_weight(nghttp2_stream *stream);
+
+/**
+ * @function
+ *
+ * Returns the sum of the weight for |stream|'s children.
+ */
+NGHTTP2_EXTERN int32_t
+nghttp2_stream_get_sum_dependency_weight(nghttp2_stream *stream);
+
+/**
+ * @functypedef
+ *
+ * Callback function invoked when the library outputs debug logging.
+ * The function is called with arguments suitable for ``vfprintf(3)``
+ *
+ * The debug output is only enabled if the library is built with
+ * ``DEBUGBUILD`` macro defined.
+ */
+typedef void (*nghttp2_debug_vprintf_callback)(const char *format,
+                                               va_list args);
+
+/**
+ * @function
+ *
+ * Sets a debug output callback called by the library when built with
+ * ``DEBUGBUILD`` macro defined.  If this option is not used, debug
+ * log is written into standard error output.
+ *
+ * For builds without ``DEBUGBUILD`` macro defined, this function is
+ * noop.
+ *
+ * Note that building with ``DEBUGBUILD`` may cause significant
+ * performance penalty to libnghttp2 because of extra processing.  It
+ * should be used for debugging purpose only.
+ *
+ * .. Warning::
+ *
+ *   Building with ``DEBUGBUILD`` may cause significant performance
+ *   penalty to libnghttp2 because of extra processing.  It should be
+ *   used for debugging purpose only.  We write this two times because
+ *   this is important.
+ */
+NGHTTP2_EXTERN void nghttp2_set_debug_vprintf_callback(
+    nghttp2_debug_vprintf_callback debug_vprintf_callback);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* NGHTTP2_H */

+ 506 - 0
iotkit-embedded/external_libs/nghttp2/nghttp2_buf.c

@@ -0,0 +1,506 @@
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
+ */
+
+
+#include "nghttp2_buf.h"
+
+#include <stdio.h>
+
+#include "nghttp2_helper.h"
+#include "nghttp2_debug.h"
+
+void nghttp2_buf_init(nghttp2_buf *buf) {
+  buf->begin = NULL;
+  buf->end = NULL;
+  buf->pos = NULL;
+  buf->last = NULL;
+  buf->mark = NULL;
+}
+
+int nghttp2_buf_init2(nghttp2_buf *buf, size_t initial, nghttp2_mem *mem) {
+  nghttp2_buf_init(buf);
+  return nghttp2_buf_reserve(buf, initial, mem);
+}
+
+void nghttp2_buf_free(nghttp2_buf *buf, nghttp2_mem *mem) {
+  if (buf == NULL) {
+    return;
+  }
+
+  nghttp2_mem_free(mem, buf->begin);
+  buf->begin = NULL;
+}
+
+int nghttp2_buf_reserve(nghttp2_buf *buf, size_t new_cap, nghttp2_mem *mem) {
+  uint8_t *ptr;
+  size_t cap;
+
+  cap = nghttp2_buf_cap(buf);
+
+  if (cap >= new_cap) {
+    return 0;
+  }
+
+  new_cap = nghttp2_max(new_cap, cap * 2);
+
+  ptr = nghttp2_mem_realloc(mem, buf->begin, new_cap);
+  if (ptr == NULL) {
+    return NGHTTP2_ERR_NOMEM;
+  }
+
+  buf->pos = ptr + (buf->pos - buf->begin);
+  buf->last = ptr + (buf->last - buf->begin);
+  buf->mark = ptr + (buf->mark - buf->begin);
+  buf->begin = ptr;
+  buf->end = ptr + new_cap;
+
+  return 0;
+}
+
+void nghttp2_buf_reset(nghttp2_buf *buf) {
+  buf->pos = buf->last = buf->mark = buf->begin;
+}
+
+void nghttp2_buf_wrap_init(nghttp2_buf *buf, uint8_t *begin, size_t len) {
+  buf->begin = buf->pos = buf->last = buf->mark = begin;
+  buf->end = begin + len;
+}
+
+static int buf_chain_new(nghttp2_buf_chain **chain, size_t chunk_length,
+                         nghttp2_mem *mem) {
+  int rv;
+
+  *chain = nghttp2_mem_malloc(mem, sizeof(nghttp2_buf_chain));
+  if (*chain == NULL) {
+    return NGHTTP2_ERR_NOMEM;
+  }
+
+  (*chain)->next = NULL;
+
+  rv = nghttp2_buf_init2(&(*chain)->buf, chunk_length, mem);
+  if (rv != 0) {
+    nghttp2_mem_free(mem, *chain);
+    return NGHTTP2_ERR_NOMEM;
+  }
+
+  return 0;
+}
+
+static void buf_chain_del(nghttp2_buf_chain *chain, nghttp2_mem *mem) {
+  nghttp2_buf_free(&chain->buf, mem);
+  nghttp2_mem_free(mem, chain);
+}
+
+int nghttp2_bufs_init(nghttp2_bufs *bufs, size_t chunk_length, size_t max_chunk,
+                      nghttp2_mem *mem) {
+  return nghttp2_bufs_init2(bufs, chunk_length, max_chunk, 0, mem);
+}
+
+int nghttp2_bufs_init2(nghttp2_bufs *bufs, size_t chunk_length,
+                       size_t max_chunk, size_t offset, nghttp2_mem *mem) {
+  return nghttp2_bufs_init3(bufs, chunk_length, max_chunk, max_chunk, offset,
+                            mem);
+}
+
+int nghttp2_bufs_init3(nghttp2_bufs *bufs, size_t chunk_length,
+                       size_t max_chunk, size_t chunk_keep, size_t offset,
+                       nghttp2_mem *mem) {
+  int rv;
+  nghttp2_buf_chain *chain;
+
+  if (chunk_keep == 0 || max_chunk < chunk_keep || chunk_length < offset) {
+    return NGHTTP2_ERR_INVALID_ARGUMENT;
+  }
+
+  rv = buf_chain_new(&chain, chunk_length, mem);
+  if (rv != 0) {
+    return rv;
+  }
+
+  bufs->mem = mem;
+  bufs->offset = offset;
+
+  bufs->head = chain;
+  bufs->cur = bufs->head;
+
+  nghttp2_buf_shift_right(&bufs->cur->buf, offset);
+
+  bufs->chunk_length = chunk_length;
+  bufs->chunk_used = 1;
+  bufs->max_chunk = max_chunk;
+  bufs->chunk_keep = chunk_keep;
+
+  return 0;
+}
+
+int nghttp2_bufs_realloc(nghttp2_bufs *bufs, size_t chunk_length) {
+  int rv;
+  nghttp2_buf_chain *chain;
+
+  if (chunk_length < bufs->offset) {
+    return NGHTTP2_ERR_INVALID_ARGUMENT;
+  }
+
+  rv = buf_chain_new(&chain, chunk_length, bufs->mem);
+  if (rv != 0) {
+    return rv;
+  }
+
+  nghttp2_bufs_free(bufs);
+
+  bufs->head = chain;
+  bufs->cur = bufs->head;
+
+  nghttp2_buf_shift_right(&bufs->cur->buf, bufs->offset);
+
+  bufs->chunk_length = chunk_length;
+  bufs->chunk_used = 1;
+
+  return 0;
+}
+
+void nghttp2_bufs_free(nghttp2_bufs *bufs) {
+  nghttp2_buf_chain *chain, *next_chain;
+
+  if (bufs == NULL) {
+    return;
+  }
+
+  for (chain = bufs->head; chain;) {
+    next_chain = chain->next;
+
+    buf_chain_del(chain, bufs->mem);
+
+    chain = next_chain;
+  }
+
+  bufs->head = NULL;
+}
+
+int nghttp2_bufs_wrap_init(nghttp2_bufs *bufs, uint8_t *begin, size_t len,
+                           nghttp2_mem *mem) {
+  nghttp2_buf_chain *chain;
+
+  chain = nghttp2_mem_malloc(mem, sizeof(nghttp2_buf_chain));
+  if (chain == NULL) {
+    return NGHTTP2_ERR_NOMEM;
+  }
+
+  chain->next = NULL;
+
+  nghttp2_buf_wrap_init(&chain->buf, begin, len);
+
+  bufs->mem = mem;
+  bufs->offset = 0;
+
+  bufs->head = chain;
+  bufs->cur = bufs->head;
+
+  bufs->chunk_length = len;
+  bufs->chunk_used = 1;
+  bufs->max_chunk = 1;
+  bufs->chunk_keep = 1;
+
+  return 0;
+}
+
+int nghttp2_bufs_wrap_init2(nghttp2_bufs *bufs, const nghttp2_vec *vec,
+                            size_t veclen, nghttp2_mem *mem) {
+  size_t i = 0;
+  nghttp2_buf_chain *cur_chain;
+  nghttp2_buf_chain *head_chain;
+  nghttp2_buf_chain **dst_chain = &head_chain;
+
+  if (veclen == 0) {
+    return nghttp2_bufs_wrap_init(bufs, NULL, 0, mem);
+  }
+
+  head_chain = nghttp2_mem_malloc(mem, sizeof(nghttp2_buf_chain) * veclen);
+  if (head_chain == NULL) {
+    return NGHTTP2_ERR_NOMEM;
+  }
+
+  for (i = 0; i < veclen; ++i) {
+    cur_chain = &head_chain[i];
+    cur_chain->next = NULL;
+    nghttp2_buf_wrap_init(&cur_chain->buf, vec[i].base, vec[i].len);
+
+    *dst_chain = cur_chain;
+    dst_chain = &cur_chain->next;
+  }
+
+  bufs->mem = mem;
+  bufs->offset = 0;
+
+  bufs->head = head_chain;
+  bufs->cur = bufs->head;
+
+  /* We don't use chunk_length since no allocation is expected. */
+  bufs->chunk_length = 0;
+  bufs->chunk_used = veclen;
+  bufs->max_chunk = veclen;
+  bufs->chunk_keep = veclen;
+
+  return 0;
+}
+
+void nghttp2_bufs_wrap_free(nghttp2_bufs *bufs) {
+  if (bufs == NULL) {
+    return;
+  }
+
+  if (bufs->head) {
+    nghttp2_mem_free(bufs->mem, bufs->head);
+  }
+}
+
+void nghttp2_bufs_seek_last_present(nghttp2_bufs *bufs) {
+  nghttp2_buf_chain *ci;
+
+  for (ci = bufs->cur; ci; ci = ci->next) {
+    if (nghttp2_buf_len(&ci->buf) == 0) {
+      return;
+    } else {
+      bufs->cur = ci;
+    }
+  }
+}
+
+size_t nghttp2_bufs_len(nghttp2_bufs *bufs) {
+  nghttp2_buf_chain *ci;
+  size_t len;
+
+  len = 0;
+  for (ci = bufs->head; ci; ci = ci->next) {
+    len += nghttp2_buf_len(&ci->buf);
+  }
+
+  return len;
+}
+
+static int bufs_alloc_chain(nghttp2_bufs *bufs) {
+  int rv;
+  nghttp2_buf_chain *chain;
+
+  if (bufs->cur->next) {
+    bufs->cur = bufs->cur->next;
+
+    return 0;
+  }
+
+  if (bufs->max_chunk == bufs->chunk_used) {
+    return NGHTTP2_ERR_BUFFER_ERROR;
+  }
+
+  rv = buf_chain_new(&chain, bufs->chunk_length, bufs->mem);
+  if (rv != 0) {
+    return rv;
+  }
+
+  DEBUGF("new buffer %zu bytes allocated for bufs %p, used %zu\n",
+         bufs->chunk_length, bufs, bufs->chunk_used);
+
+  ++bufs->chunk_used;
+
+  bufs->cur->next = chain;
+  bufs->cur = chain;
+
+  nghttp2_buf_shift_right(&bufs->cur->buf, bufs->offset);
+
+  return 0;
+}
+
+int nghttp2_bufs_add(nghttp2_bufs *bufs, const void *data, size_t len) {
+  int rv;
+  size_t nwrite;
+  nghttp2_buf *buf;
+  const uint8_t *p;
+
+  p = data;
+
+  while (len) {
+    buf = &bufs->cur->buf;
+
+    nwrite = nghttp2_min(nghttp2_buf_avail(buf), len);
+    if (nwrite == 0) {
+      rv = bufs_alloc_chain(bufs);
+      if (rv != 0) {
+        return rv;
+      }
+      continue;
+    }
+
+    buf->last = nghttp2_cpymem(buf->last, p, nwrite);
+    p += nwrite;
+    len -= nwrite;
+  }
+
+  return 0;
+}
+
+static int bufs_ensure_addb(nghttp2_bufs *bufs) {
+  int rv;
+  nghttp2_buf *buf;
+
+  buf = &bufs->cur->buf;
+
+  if (nghttp2_buf_avail(buf) > 0) {
+    return 0;
+  }
+
+  rv = bufs_alloc_chain(bufs);
+  if (rv != 0) {
+    return rv;
+  }
+
+  return 0;
+}
+
+int nghttp2_bufs_addb(nghttp2_bufs *bufs, uint8_t b) {
+  int rv;
+
+  rv = bufs_ensure_addb(bufs);
+  if (rv != 0) {
+    return rv;
+  }
+
+  *bufs->cur->buf.last++ = b;
+
+  return 0;
+}
+
+int nghttp2_bufs_addb_hold(nghttp2_bufs *bufs, uint8_t b) {
+  int rv;
+
+  rv = bufs_ensure_addb(bufs);
+  if (rv != 0) {
+    return rv;
+  }
+
+  *bufs->cur->buf.last = b;
+
+  return 0;
+}
+
+int nghttp2_bufs_orb(nghttp2_bufs *bufs, uint8_t b) {
+  int rv;
+
+  rv = bufs_ensure_addb(bufs);
+  if (rv != 0) {
+    return rv;
+  }
+
+  *bufs->cur->buf.last++ |= b;
+
+  return 0;
+}
+
+int nghttp2_bufs_orb_hold(nghttp2_bufs *bufs, uint8_t b) {
+  int rv;
+
+  rv = bufs_ensure_addb(bufs);
+  if (rv != 0) {
+    return rv;
+  }
+
+  *bufs->cur->buf.last |= b;
+
+  return 0;
+}
+
+ssize_t nghttp2_bufs_remove(nghttp2_bufs *bufs, uint8_t **out) {
+  size_t len;
+  nghttp2_buf_chain *chain;
+  nghttp2_buf *buf;
+  uint8_t *res;
+  nghttp2_buf resbuf;
+
+  len = 0;
+
+  for (chain = bufs->head; chain; chain = chain->next) {
+    len += nghttp2_buf_len(&chain->buf);
+  }
+
+  if (len == 0) {
+    res = NULL;
+    return 0;
+  }
+
+  res = nghttp2_mem_malloc(bufs->mem, len);
+  if (res == NULL) {
+    return NGHTTP2_ERR_NOMEM;
+  }
+
+  nghttp2_buf_wrap_init(&resbuf, res, len);
+
+  for (chain = bufs->head; chain; chain = chain->next) {
+    buf = &chain->buf;
+    resbuf.last = nghttp2_cpymem(resbuf.last, buf->pos, nghttp2_buf_len(buf));
+  }
+
+  *out = res;
+
+  return (ssize_t)len;
+}
+
+size_t nghttp2_bufs_remove_copy(nghttp2_bufs *bufs, uint8_t *out) {
+  size_t len;
+  nghttp2_buf_chain *chain;
+  nghttp2_buf *buf;
+  nghttp2_buf resbuf;
+
+  len = nghttp2_bufs_len(bufs);
+
+  nghttp2_buf_wrap_init(&resbuf, out, len);
+
+  for (chain = bufs->head; chain; chain = chain->next) {
+    buf = &chain->buf;
+    resbuf.last = nghttp2_cpymem(resbuf.last, buf->pos, nghttp2_buf_len(buf));
+  }
+
+  return len;
+}
+
+void nghttp2_bufs_reset(nghttp2_bufs *bufs) {
+  nghttp2_buf_chain *chain, *ci;
+  size_t k;
+
+  k = bufs->chunk_keep;
+
+  for (ci = bufs->head; ci; ci = ci->next) {
+    nghttp2_buf_reset(&ci->buf);
+    nghttp2_buf_shift_right(&ci->buf, bufs->offset);
+
+    if (--k == 0) {
+      break;
+    }
+  }
+
+  if (ci) {
+    chain = ci->next;
+    ci->next = NULL;
+
+    for (ci = chain; ci;) {
+      chain = ci->next;
+
+      buf_chain_del(ci, bufs->mem);
+
+      ci = chain;
+    }
+
+    bufs->chunk_used = bufs->chunk_keep;
+  }
+
+  bufs->cur = bufs->head;
+}
+
+int nghttp2_bufs_advance(nghttp2_bufs *bufs) { return bufs_alloc_chain(bufs); }
+
+int nghttp2_bufs_next_present(nghttp2_bufs *bufs) {
+  nghttp2_buf_chain *chain;
+
+  chain = bufs->cur->next;
+
+  return chain && nghttp2_buf_len(&chain->buf);
+}

+ 393 - 0
iotkit-embedded/external_libs/nghttp2/nghttp2_buf.h

@@ -0,0 +1,393 @@
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
+ */
+
+
+#ifndef NGHTTP2_BUF_H
+#define NGHTTP2_BUF_H
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif /* HAVE_CONFIG_H */
+
+#include "nghttp2.h"
+
+#include "nghttp2_int.h"
+#include "nghttp2_mem.h"
+
+typedef struct {
+  /* This points to the beginning of the buffer. The effective range
+     of buffer is [begin, end). */
+  uint8_t *begin;
+  /* This points to the memory one byte beyond the end of the
+     buffer. */
+  uint8_t *end;
+  /* The position indicator for effective start of the buffer. pos <=
+     last must be hold. */
+  uint8_t *pos;
+  /* The position indicator for effective one beyond of the end of the
+     buffer. last <= end must be hold. */
+  uint8_t *last;
+  /* Mark arbitrary position in buffer [begin, end) */
+  uint8_t *mark;
+} nghttp2_buf;
+
+#define nghttp2_buf_len(BUF) ((size_t)((BUF)->last - (BUF)->pos))
+#define nghttp2_buf_avail(BUF) ((size_t)((BUF)->end - (BUF)->last))
+#define nghttp2_buf_mark_avail(BUF) ((size_t)((BUF)->mark - (BUF)->last))
+#define nghttp2_buf_cap(BUF) ((size_t)((BUF)->end - (BUF)->begin))
+
+#define nghttp2_buf_pos_offset(BUF) ((size_t)((BUF)->pos - (BUF)->begin))
+#define nghttp2_buf_last_offset(BUF) ((size_t)((BUF)->last - (BUF)->begin))
+
+#define nghttp2_buf_shift_right(BUF, AMT)                                      \
+  do {                                                                         \
+    (BUF)->pos += AMT;                                                         \
+    (BUF)->last += AMT;                                                        \
+  } while (0)
+
+#define nghttp2_buf_shift_left(BUF, AMT)                                       \
+  do {                                                                         \
+    (BUF)->pos -= AMT;                                                         \
+    (BUF)->last -= AMT;                                                        \
+  } while (0)
+
+/*
+ * Initializes the |buf|. No memory is allocated in this function. Use
+ * nghttp2_buf_reserve() to allocate memory.
+ */
+void nghttp2_buf_init(nghttp2_buf *buf);
+
+/*
+ * Initializes the |buf| and allocates at least |initial| bytes of
+ * memory.
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * NGHTTP2_ERR_NOMEM
+ *     Out of memory
+ */
+int nghttp2_buf_init2(nghttp2_buf *buf, size_t initial, nghttp2_mem *mem);
+
+/*
+ * Frees buffer in |buf|.
+ */
+void nghttp2_buf_free(nghttp2_buf *buf, nghttp2_mem *mem);
+
+/*
+ * Extends buffer so that nghttp2_buf_cap() returns at least
+ * |new_cap|. If extensions took place, buffer pointers in |buf| will
+ * change.
+ *
+ * This function returns 0 if it succeeds, or one of the followings
+ * negative error codes:
+ *
+ * NGHTTP2_ERR_NOMEM
+ *     Out of memory
+ */
+int nghttp2_buf_reserve(nghttp2_buf *buf, size_t new_cap, nghttp2_mem *mem);
+
+/*
+ * Resets pos, last, mark member of |buf| to buf->begin.
+ */
+void nghttp2_buf_reset(nghttp2_buf *buf);
+
+/*
+ * Initializes |buf| using supplied buffer |begin| of length
+ * |len|. Semantically, the application should not call *_reserve() or
+ * nghttp2_free() functions for |buf|.
+ */
+void nghttp2_buf_wrap_init(nghttp2_buf *buf, uint8_t *begin, size_t len);
+
+struct nghttp2_buf_chain;
+
+typedef struct nghttp2_buf_chain nghttp2_buf_chain;
+
+/* Chains 2 buffers */
+struct nghttp2_buf_chain {
+  /* Points to the subsequent buffer. NULL if there is no such
+     buffer. */
+  nghttp2_buf_chain *next;
+  nghttp2_buf buf;
+};
+
+typedef struct {
+  /* Points to the first buffer */
+  nghttp2_buf_chain *head;
+  /* Buffer pointer where write occurs. */
+  nghttp2_buf_chain *cur;
+  /* Memory allocator */
+  nghttp2_mem *mem;
+  /* The buffer capacity of each buf.  This field may be 0 if
+     nghttp2_bufs is initialized by nghttp2_bufs_wrap_init* family
+     functions. */
+  size_t chunk_length;
+  /* The maximum number of nghttp2_buf_chain */
+  size_t max_chunk;
+  /* The number of nghttp2_buf_chain allocated */
+  size_t chunk_used;
+  /* The number of nghttp2_buf_chain to keep on reset */
+  size_t chunk_keep;
+  /* pos offset from begin in each buffers. On initialization and
+     reset, buf->pos and buf->last are positioned at buf->begin +
+     offset. */
+  size_t offset;
+} nghttp2_bufs;
+
+/*
+ * This is the same as calling nghttp2_bufs_init2 with the given
+ * arguments and offset = 0.
+ */
+int nghttp2_bufs_init(nghttp2_bufs *bufs, size_t chunk_length, size_t max_chunk,
+                      nghttp2_mem *mem);
+
+/*
+ * This is the same as calling nghttp2_bufs_init3 with the given
+ * arguments and chunk_keep = max_chunk.
+ */
+int nghttp2_bufs_init2(nghttp2_bufs *bufs, size_t chunk_length,
+                       size_t max_chunk, size_t offset, nghttp2_mem *mem);
+
+/*
+ * Initializes |bufs|. Each buffer size is given in the
+ * |chunk_length|.  The maximum number of buffers is given in the
+ * |max_chunk|.  On reset, first |chunk_keep| buffers are kept and
+ * remaining buffers are deleted.  Each buffer will have bufs->pos and
+ * bufs->last shifted to left by |offset| bytes on creation and reset.
+ *
+ * This function allocates first buffer.  bufs->head and bufs->cur
+ * will point to the first buffer after this call.
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * NGHTTP2_ERR_NOMEM
+ *     Out of memory.
+ * NGHTTP2_ERR_INVALID_ARGUMENT
+ *     chunk_keep is 0; or max_chunk < chunk_keep; or offset is too
+ *     long.
+ */
+int nghttp2_bufs_init3(nghttp2_bufs *bufs, size_t chunk_length,
+                       size_t max_chunk, size_t chunk_keep, size_t offset,
+                       nghttp2_mem *mem);
+
+/*
+ * Frees any related resources to the |bufs|.
+ */
+void nghttp2_bufs_free(nghttp2_bufs *bufs);
+
+/*
+ * Initializes |bufs| using supplied buffer |begin| of length |len|.
+ * The first buffer bufs->head uses buffer |begin|.  The buffer size
+ * is fixed and no extra chunk buffer is allocated.  In other
+ * words, max_chunk = chunk_keep = 1.  To free the resource allocated
+ * for |bufs|, use nghttp2_bufs_wrap_free().
+ *
+ * Don't use the function which performs allocation, such as
+ * nghttp2_bufs_realloc().
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * NGHTTP2_ERR_NOMEM
+ *     Out of memory.
+ */
+int nghttp2_bufs_wrap_init(nghttp2_bufs *bufs, uint8_t *begin, size_t len,
+                           nghttp2_mem *mem);
+
+/*
+ * Initializes |bufs| using supplied |veclen| size of buf vector
+ * |vec|.  The number of buffers is fixed and no extra chunk buffer is
+ * allocated.  In other words, max_chunk = chunk_keep = |in_len|.  To
+ * free the resource allocated for |bufs|, use
+ * nghttp2_bufs_wrap_free().
+ *
+ * Don't use the function which performs allocation, such as
+ * nghttp2_bufs_realloc().
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * NGHTTP2_ERR_NOMEM
+ *     Out of memory.
+ */
+int nghttp2_bufs_wrap_init2(nghttp2_bufs *bufs, const nghttp2_vec *vec,
+                            size_t veclen, nghttp2_mem *mem);
+
+/*
+ * Frees any related resource to the |bufs|.  This function does not
+ * free supplied buffer provided in nghttp2_bufs_wrap_init().
+ */
+void nghttp2_bufs_wrap_free(nghttp2_bufs *bufs);
+
+/*
+ * Reallocates internal buffer using |chunk_length|.  The max_chunk,
+ * chunk_keep and offset do not change.  After successful allocation
+ * of new buffer, previous buffers are deallocated without copying
+ * anything into new buffers.  chunk_used is reset to 1.
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * NGHTTP2_ERR_NOMEM
+ *     Out of memory.
+ * NGHTTP2_ERR_INVALID_ARGUMENT
+ *     chunk_length < offset
+ */
+int nghttp2_bufs_realloc(nghttp2_bufs *bufs, size_t chunk_length);
+
+/*
+ * Appends the |data| of length |len| to the |bufs|. The write starts
+ * at bufs->cur->buf.last. A new buffers will be allocated to store
+ * all data.
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * NGHTTP2_ERR_NOMEM
+ *     Out of memory.
+ * NGHTTP2_ERR_BUFFER_ERROR
+ *     Out of buffer space.
+ */
+int nghttp2_bufs_add(nghttp2_bufs *bufs, const void *data, size_t len);
+
+/*
+ * Appends a single byte |b| to the |bufs|. The write starts at
+ * bufs->cur->buf.last. A new buffers will be allocated to store all
+ * data.
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * NGHTTP2_ERR_NOMEM
+ *     Out of memory.
+ * NGHTTP2_ERR_BUFFER_ERROR
+ *     Out of buffer space.
+ */
+int nghttp2_bufs_addb(nghttp2_bufs *bufs, uint8_t b);
+
+/*
+ * Behaves like nghttp2_bufs_addb(), but this does not update
+ * buf->last pointer.
+ */
+int nghttp2_bufs_addb_hold(nghttp2_bufs *bufs, uint8_t b);
+
+#define nghttp2_bufs_fast_addb(BUFS, B)                                        \
+  do {                                                                         \
+    *(BUFS)->cur->buf.last++ = B;                                              \
+  } while (0)
+
+#define nghttp2_bufs_fast_addb_hold(BUFS, B)                                   \
+  do {                                                                         \
+    *(BUFS)->cur->buf.last = B;                                                \
+  } while (0)
+
+/*
+ * Performs bitwise-OR of |b| at bufs->cur->buf.last. A new buffers
+ * will be allocated if necessary.
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * NGHTTP2_ERR_NOMEM
+ *     Out of memory.
+ * NGHTTP2_ERR_BUFFER_ERROR
+ *     Out of buffer space.
+ */
+int nghttp2_bufs_orb(nghttp2_bufs *bufs, uint8_t b);
+
+/*
+ * Behaves like nghttp2_bufs_orb(), but does not update buf->last
+ * pointer.
+ */
+int nghttp2_bufs_orb_hold(nghttp2_bufs *bufs, uint8_t b);
+
+#define nghttp2_bufs_fast_orb(BUFS, B)                                         \
+  do {                                                                         \
+    uint8_t **p = &(BUFS)->cur->buf.last;                                      \
+    **p = (uint8_t)(**p | (B));                                                \
+    ++(*p);                                                                    \
+  } while (0)
+
+#define nghttp2_bufs_fast_orb_hold(BUFS, B)                                    \
+  do {                                                                         \
+    uint8_t *p = (BUFS)->cur->buf.last;                                        \
+    *p = (uint8_t)(*p | (B));                                                  \
+  } while (0)
+
+/*
+ * Copies all data stored in |bufs| to the contiguous buffer.  This
+ * function allocates the contiguous memory to store all data in
+ * |bufs| and assigns it to |*out|.
+ *
+ * The contents of |bufs| is left unchanged.
+ *
+ * This function returns the length of copied data and assigns the
+ * pointer to copied data to |*out| if it succeeds, or one of the
+ * following negative error codes:
+ *
+ * NGHTTP2_ERR_NOMEM
+ *     Out of memory
+ */
+ssize_t nghttp2_bufs_remove(nghttp2_bufs *bufs, uint8_t **out);
+
+/*
+ * Copies all data stored in |bufs| to |out|.  This function assumes
+ * that the buffer space pointed by |out| has at least
+ * nghttp2_bufs(bufs) bytes.
+ *
+ * The contents of |bufs| is left unchanged.
+ *
+ * This function returns the length of copied data.
+ */
+size_t nghttp2_bufs_remove_copy(nghttp2_bufs *bufs, uint8_t *out);
+
+/*
+ * Resets |bufs| and makes the buffers empty.
+ */
+void nghttp2_bufs_reset(nghttp2_bufs *bufs);
+
+/*
+ * Moves bufs->cur to bufs->cur->next.  If resulting bufs->cur is
+ * NULL, this function allocates new buffers and bufs->cur points to
+ * it.
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * NGHTTP2_ERR_NOMEM
+ *     Out of memory
+ * NGHTTP2_ERR_BUFFER_ERROR
+ *     Out of buffer space.
+ */
+int nghttp2_bufs_advance(nghttp2_bufs *bufs);
+
+/* Sets bufs->cur to bufs->head */
+#define nghttp2_bufs_rewind(BUFS)                                              \
+  do {                                                                         \
+    (BUFS)->cur = (BUFS)->head;                                                \
+  } while (0)
+
+/*
+ * Move bufs->cur, from the current position, using next member, to
+ * the last buf which has nghttp2_buf_len(buf) > 0 without seeing buf
+ * which satisfies nghttp2_buf_len(buf) == 0.  If
+ * nghttp2_buf_len(&bufs->cur->buf) == 0 or bufs->cur->next is NULL,
+ * bufs->cur is unchanged.
+ */
+void nghttp2_bufs_seek_last_present(nghttp2_bufs *bufs);
+
+/*
+ * Returns nonzero if bufs->cur->next is not empty.
+ */
+int nghttp2_bufs_next_present(nghttp2_bufs *bufs);
+
+#define nghttp2_bufs_cur_avail(BUFS) nghttp2_buf_avail(&(BUFS)->cur->buf)
+
+/*
+ * Returns the total buffer length of |bufs|.
+ */
+size_t nghttp2_bufs_len(nghttp2_bufs *bufs);
+
+#endif /* NGHTTP2_BUF_H */

+ 172 - 0
iotkit-embedded/external_libs/nghttp2/nghttp2_callbacks.c

@@ -0,0 +1,172 @@
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
+ */
+
+
+#include "nghttp2_callbacks.h"
+#include <string.h>
+#include <stdlib.h>
+#ifdef INFRA_MEM_STATS
+#include "infra_mem_stats.h"
+#endif
+
+extern void *HAL_Malloc(uint32_t size);
+extern void *HAL_Realloc(void *ptr, uint32_t size);
+extern void HAL_Free(void *ptr);
+
+#if INFRA_MEM_STATS
+#define NGHTTP2_CB_MALLOC(size)         LITE_malloc(size, MEM_MAGIC, "nghttp2.cb")
+#define NGHTTP2_CB_FREE(ptr)            LITE_free(ptr)
+#else
+#define NGHTTP2_CB_MALLOC(size)         HAL_Malloc(size)
+#define NGHTTP2_CB_FREE(ptr)            {HAL_Free((void *)ptr);ptr = NULL;}
+#endif
+
+int nghttp2_session_callbacks_new(nghttp2_session_callbacks **callbacks_ptr) {
+  *callbacks_ptr = NGHTTP2_CB_MALLOC(sizeof(nghttp2_session_callbacks));
+
+  if (*callbacks_ptr == NULL) {
+    return NGHTTP2_ERR_NOMEM;
+  }
+
+  memset(*callbacks_ptr, 0, sizeof(nghttp2_session_callbacks));
+  return 0;
+}
+
+void nghttp2_session_callbacks_del(nghttp2_session_callbacks *callbacks) {
+  NGHTTP2_CB_FREE(callbacks);
+}
+
+void nghttp2_session_callbacks_set_send_callback(
+    nghttp2_session_callbacks *cbs, nghttp2_send_callback send_callback) {
+  cbs->send_callback = send_callback;
+}
+
+void nghttp2_session_callbacks_set_recv_callback(
+    nghttp2_session_callbacks *cbs, nghttp2_recv_callback recv_callback) {
+  cbs->recv_callback = recv_callback;
+}
+
+void nghttp2_session_callbacks_set_on_frame_recv_callback(
+    nghttp2_session_callbacks *cbs,
+    nghttp2_on_frame_recv_callback on_frame_recv_callback) {
+  cbs->on_frame_recv_callback = on_frame_recv_callback;
+}
+
+void nghttp2_session_callbacks_set_on_invalid_frame_recv_callback(
+    nghttp2_session_callbacks *cbs,
+    nghttp2_on_invalid_frame_recv_callback on_invalid_frame_recv_callback) {
+  cbs->on_invalid_frame_recv_callback = on_invalid_frame_recv_callback;
+}
+
+void nghttp2_session_callbacks_set_on_data_chunk_recv_callback(
+    nghttp2_session_callbacks *cbs,
+    nghttp2_on_data_chunk_recv_callback on_data_chunk_recv_callback) {
+  cbs->on_data_chunk_recv_callback = on_data_chunk_recv_callback;
+}
+
+void nghttp2_session_callbacks_set_before_frame_send_callback(
+    nghttp2_session_callbacks *cbs,
+    nghttp2_before_frame_send_callback before_frame_send_callback) {
+  cbs->before_frame_send_callback = before_frame_send_callback;
+}
+
+void nghttp2_session_callbacks_set_on_frame_send_callback(
+    nghttp2_session_callbacks *cbs,
+    nghttp2_on_frame_send_callback on_frame_send_callback) {
+  cbs->on_frame_send_callback = on_frame_send_callback;
+}
+
+void nghttp2_session_callbacks_set_on_frame_not_send_callback(
+    nghttp2_session_callbacks *cbs,
+    nghttp2_on_frame_not_send_callback on_frame_not_send_callback) {
+  cbs->on_frame_not_send_callback = on_frame_not_send_callback;
+}
+
+void nghttp2_session_callbacks_set_on_stream_close_callback(
+    nghttp2_session_callbacks *cbs,
+    nghttp2_on_stream_close_callback on_stream_close_callback) {
+  cbs->on_stream_close_callback = on_stream_close_callback;
+}
+
+void nghttp2_session_callbacks_set_on_begin_headers_callback(
+    nghttp2_session_callbacks *cbs,
+    nghttp2_on_begin_headers_callback on_begin_headers_callback) {
+  cbs->on_begin_headers_callback = on_begin_headers_callback;
+}
+
+void nghttp2_session_callbacks_set_on_header_callback(
+    nghttp2_session_callbacks *cbs,
+    nghttp2_on_header_callback on_header_callback) {
+  cbs->on_header_callback = on_header_callback;
+}
+
+void nghttp2_session_callbacks_set_on_header_callback2(
+    nghttp2_session_callbacks *cbs,
+    nghttp2_on_header_callback2 on_header_callback2) {
+  cbs->on_header_callback2 = on_header_callback2;
+}
+
+void nghttp2_session_callbacks_set_on_invalid_header_callback(
+    nghttp2_session_callbacks *cbs,
+    nghttp2_on_invalid_header_callback on_invalid_header_callback) {
+  cbs->on_invalid_header_callback = on_invalid_header_callback;
+}
+
+void nghttp2_session_callbacks_set_on_invalid_header_callback2(
+    nghttp2_session_callbacks *cbs,
+    nghttp2_on_invalid_header_callback2 on_invalid_header_callback2) {
+  cbs->on_invalid_header_callback2 = on_invalid_header_callback2;
+}
+
+void nghttp2_session_callbacks_set_select_padding_callback(
+    nghttp2_session_callbacks *cbs,
+    nghttp2_select_padding_callback select_padding_callback) {
+  cbs->select_padding_callback = select_padding_callback;
+}
+
+void nghttp2_session_callbacks_set_data_source_read_length_callback(
+    nghttp2_session_callbacks *cbs,
+    nghttp2_data_source_read_length_callback data_source_read_length_callback) {
+  cbs->read_length_callback = data_source_read_length_callback;
+}
+
+void nghttp2_session_callbacks_set_on_begin_frame_callback(
+    nghttp2_session_callbacks *cbs,
+    nghttp2_on_begin_frame_callback on_begin_frame_callback) {
+  cbs->on_begin_frame_callback = on_begin_frame_callback;
+}
+
+void nghttp2_session_callbacks_set_send_data_callback(
+    nghttp2_session_callbacks *cbs,
+    nghttp2_send_data_callback send_data_callback) {
+  cbs->send_data_callback = send_data_callback;
+}
+
+void nghttp2_session_callbacks_set_pack_extension_callback(
+    nghttp2_session_callbacks *cbs,
+    nghttp2_pack_extension_callback pack_extension_callback) {
+  cbs->pack_extension_callback = pack_extension_callback;
+}
+
+void nghttp2_session_callbacks_set_unpack_extension_callback(
+    nghttp2_session_callbacks *cbs,
+    nghttp2_unpack_extension_callback unpack_extension_callback) {
+  cbs->unpack_extension_callback = unpack_extension_callback;
+}
+
+void nghttp2_session_callbacks_set_on_extension_chunk_recv_callback(
+    nghttp2_session_callbacks *cbs,
+    nghttp2_on_extension_chunk_recv_callback on_extension_chunk_recv_callback) {
+  cbs->on_extension_chunk_recv_callback = on_extension_chunk_recv_callback;
+}
+
+void nghttp2_session_callbacks_set_error_callback(
+    nghttp2_session_callbacks *cbs, nghttp2_error_callback error_callback) {
+  cbs->error_callback = error_callback;
+}
+
+void nghttp2_session_callbacks_set_error_callback2(
+    nghttp2_session_callbacks *cbs, nghttp2_error_callback2 error_callback2) {
+  cbs->error_callback2 = error_callback2;
+}

+ 106 - 0
iotkit-embedded/external_libs/nghttp2/nghttp2_callbacks.h

@@ -0,0 +1,106 @@
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
+ */
+
+
+#ifndef NGHTTP2_CALLBACKS_H
+#define NGHTTP2_CALLBACKS_H
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif /* HAVE_CONFIG_H */
+
+#include "nghttp2.h"
+
+/*
+ * Callback functions.
+ */
+struct nghttp2_session_callbacks {
+  /**
+   * Callback function invoked when the session wants to send data to
+   * the remote peer.  This callback is not necessary if the
+   * application uses solely `nghttp2_session_mem_send()` to serialize
+   * data to transmit.
+   */
+  nghttp2_send_callback send_callback;
+  /**
+   * Callback function invoked when the session wants to receive data
+   * from the remote peer.  This callback is not necessary if the
+   * application uses solely `nghttp2_session_mem_recv()` to process
+   * received data.
+   */
+  nghttp2_recv_callback recv_callback;
+  /**
+   * Callback function invoked by `nghttp2_session_recv()` when a
+   * frame is received.
+   */
+  nghttp2_on_frame_recv_callback on_frame_recv_callback;
+  /**
+   * Callback function invoked by `nghttp2_session_recv()` when an
+   * invalid non-DATA frame is received.
+   */
+  nghttp2_on_invalid_frame_recv_callback on_invalid_frame_recv_callback;
+  /**
+   * Callback function invoked when a chunk of data in DATA frame is
+   * received.
+   */
+  nghttp2_on_data_chunk_recv_callback on_data_chunk_recv_callback;
+  /**
+   * Callback function invoked before a non-DATA frame is sent.
+   */
+  nghttp2_before_frame_send_callback before_frame_send_callback;
+  /**
+   * Callback function invoked after a frame is sent.
+   */
+  nghttp2_on_frame_send_callback on_frame_send_callback;
+  /**
+   * The callback function invoked when a non-DATA frame is not sent
+   * because of an error.
+   */
+  nghttp2_on_frame_not_send_callback on_frame_not_send_callback;
+  /**
+   * Callback function invoked when the stream is closed.
+   */
+  nghttp2_on_stream_close_callback on_stream_close_callback;
+  /**
+   * Callback function invoked when the reception of header block in
+   * HEADERS or PUSH_PROMISE is started.
+   */
+  nghttp2_on_begin_headers_callback on_begin_headers_callback;
+  /**
+   * Callback function invoked when a header name/value pair is
+   * received.
+   */
+  nghttp2_on_header_callback on_header_callback;
+  nghttp2_on_header_callback2 on_header_callback2;
+  /**
+   * Callback function invoked when a invalid header name/value pair
+   * is received which is silently ignored if these callbacks are not
+   * set.
+   */
+  nghttp2_on_invalid_header_callback on_invalid_header_callback;
+  nghttp2_on_invalid_header_callback2 on_invalid_header_callback2;
+  /**
+   * Callback function invoked when the library asks application how
+   * many padding bytes are required for the transmission of the given
+   * frame.
+   */
+  nghttp2_select_padding_callback select_padding_callback;
+  /**
+   * The callback function used to determine the length allowed in
+   * `nghttp2_data_source_read_callback()`
+   */
+  nghttp2_data_source_read_length_callback read_length_callback;
+  /**
+   * Sets callback function invoked when a frame header is received.
+   */
+  nghttp2_on_begin_frame_callback on_begin_frame_callback;
+  nghttp2_send_data_callback send_data_callback;
+  nghttp2_pack_extension_callback pack_extension_callback;
+  nghttp2_unpack_extension_callback unpack_extension_callback;
+  nghttp2_on_extension_chunk_recv_callback on_extension_chunk_recv_callback;
+  nghttp2_error_callback error_callback;
+  nghttp2_error_callback2 error_callback2;
+};
+
+#endif /* NGHTTP2_CALLBACKS_H */

+ 41 - 0
iotkit-embedded/external_libs/nghttp2/nghttp2_debug.c

@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
+ */
+
+
+#include "nghttp2_debug.h"
+
+#include <stdio.h>
+
+#ifdef DEBUGBUILD
+
+static void nghttp2_default_debug_vfprintf_callback(const char *fmt,
+                                                    va_list args) {
+  vfprintf(stderr, fmt, args);
+}
+
+static nghttp2_debug_vprintf_callback static_debug_vprintf_callback =
+    nghttp2_default_debug_vfprintf_callback;
+
+void nghttp2_debug_vprintf(const char *format, ...) {
+  if (static_debug_vprintf_callback) {
+    va_list args;
+    va_start(args, format);
+    static_debug_vprintf_callback(format, args);
+    va_end(args);
+  }
+}
+
+void nghttp2_set_debug_vprintf_callback(
+    nghttp2_debug_vprintf_callback debug_vprintf_callback) {
+  static_debug_vprintf_callback = debug_vprintf_callback;
+}
+
+#else /* !DEBUGBUILD */
+
+void nghttp2_set_debug_vprintf_callback(
+    nghttp2_debug_vprintf_callback debug_vprintf_callback) {
+  (void)debug_vprintf_callback;
+}
+
+#endif /* !DEBUGBUILD */

+ 24 - 0
iotkit-embedded/external_libs/nghttp2/nghttp2_debug.h

@@ -0,0 +1,24 @@
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
+ */
+
+
+#ifndef NGHTTP2_DEBUG_H
+#define NGHTTP2_DEBUG_H
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif /* HAVE_CONFIG_H */
+
+#include "nghttp2.h"
+
+#ifdef DEBUGBUILD
+#define DEBUGF(...) nghttp2_debug_vprintf(__VA_ARGS__)
+void nghttp2_debug_vprintf(const char *format, ...);
+#else
+#define DEBUGF(...)                                                            \
+  do {                                                                         \
+  } while (0)
+#endif
+
+#endif /* NGHTTP2_DEBUG_H */

+ 977 - 0
iotkit-embedded/external_libs/nghttp2/nghttp2_frame.c

@@ -0,0 +1,977 @@
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
+ */
+
+
+#include "nghttp2_frame.h"
+
+#include <string.h>
+#include <assert.h>
+#include <stdio.h>
+#include <errno.h>
+
+#include "nghttp2_helper.h"
+#include "nghttp2_net.h"
+#include "nghttp2_priority_spec.h"
+#include "nghttp2_debug.h"
+
+void nghttp2_frame_pack_frame_hd(uint8_t *buf, const nghttp2_frame_hd *hd) {
+  nghttp2_put_uint32be(&buf[0], (uint32_t)(hd->length << 8));
+  buf[3] = hd->type;
+  buf[4] = hd->flags;
+  nghttp2_put_uint32be(&buf[5], (uint32_t)hd->stream_id);
+  /* ignore hd->reserved for now */
+}
+
+void nghttp2_frame_unpack_frame_hd(nghttp2_frame_hd *hd, const uint8_t *buf) {
+  hd->length = nghttp2_get_uint32(&buf[0]) >> 8;
+  hd->type = buf[3];
+  hd->flags = buf[4];
+  hd->stream_id = nghttp2_get_uint32(&buf[5]) & NGHTTP2_STREAM_ID_MASK;
+  hd->reserved = 0;
+}
+
+void nghttp2_frame_hd_init(nghttp2_frame_hd *hd, size_t length, uint8_t type,
+                           uint8_t flags, int32_t stream_id) {
+  hd->length = length;
+  hd->type = type;
+  hd->flags = flags;
+  hd->stream_id = stream_id;
+  hd->reserved = 0;
+}
+
+void nghttp2_frame_headers_init(nghttp2_headers *frame, uint8_t flags,
+                                int32_t stream_id, nghttp2_headers_category cat,
+                                const nghttp2_priority_spec *pri_spec,
+                                nghttp2_nv *nva, size_t nvlen) {
+  nghttp2_frame_hd_init(&frame->hd, 0, NGHTTP2_HEADERS, flags, stream_id);
+  frame->padlen = 0;
+  frame->nva = nva;
+  frame->nvlen = nvlen;
+  frame->cat = cat;
+
+  if (pri_spec) {
+    frame->pri_spec = *pri_spec;
+  } else {
+    nghttp2_priority_spec_default_init(&frame->pri_spec);
+  }
+}
+
+void nghttp2_frame_headers_free(nghttp2_headers *frame, nghttp2_mem *mem) {
+  nghttp2_nv_array_del(frame->nva, mem);
+}
+
+void nghttp2_frame_priority_init(nghttp2_priority *frame, int32_t stream_id,
+                                 const nghttp2_priority_spec *pri_spec) {
+  nghttp2_frame_hd_init(&frame->hd, NGHTTP2_PRIORITY_SPECLEN, NGHTTP2_PRIORITY,
+                        NGHTTP2_FLAG_NONE, stream_id);
+  frame->pri_spec = *pri_spec;
+}
+
+void nghttp2_frame_priority_free(nghttp2_priority *frame) { (void)frame; }
+
+void nghttp2_frame_rst_stream_init(nghttp2_rst_stream *frame, int32_t stream_id,
+                                   uint32_t error_code) {
+  nghttp2_frame_hd_init(&frame->hd, 4, NGHTTP2_RST_STREAM, NGHTTP2_FLAG_NONE,
+                        stream_id);
+  frame->error_code = error_code;
+}
+
+void nghttp2_frame_rst_stream_free(nghttp2_rst_stream *frame) { (void)frame; }
+
+void nghttp2_frame_settings_init(nghttp2_settings *frame, uint8_t flags,
+                                 nghttp2_settings_entry *iv, size_t niv) {
+  nghttp2_frame_hd_init(&frame->hd, niv * NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH,
+                        NGHTTP2_SETTINGS, flags, 0);
+  frame->niv = niv;
+  frame->iv = iv;
+}
+
+void nghttp2_frame_settings_free(nghttp2_settings *frame, nghttp2_mem *mem) {
+  nghttp2_mem_free(mem, frame->iv);
+}
+
+void nghttp2_frame_push_promise_init(nghttp2_push_promise *frame, uint8_t flags,
+                                     int32_t stream_id,
+                                     int32_t promised_stream_id,
+                                     nghttp2_nv *nva, size_t nvlen) {
+  nghttp2_frame_hd_init(&frame->hd, 0, NGHTTP2_PUSH_PROMISE, flags, stream_id);
+  frame->padlen = 0;
+  frame->nva = nva;
+  frame->nvlen = nvlen;
+  frame->promised_stream_id = promised_stream_id;
+  frame->reserved = 0;
+}
+
+void nghttp2_frame_push_promise_free(nghttp2_push_promise *frame,
+                                     nghttp2_mem *mem) {
+  nghttp2_nv_array_del(frame->nva, mem);
+}
+
+void nghttp2_frame_ping_init(nghttp2_ping *frame, uint8_t flags,
+                             const uint8_t *opaque_data) {
+  nghttp2_frame_hd_init(&frame->hd, 8, NGHTTP2_PING, flags, 0);
+  if (opaque_data) {
+    memcpy(frame->opaque_data, opaque_data, sizeof(frame->opaque_data));
+  } else {
+    memset(frame->opaque_data, 0, sizeof(frame->opaque_data));
+  }
+}
+
+void nghttp2_frame_ping_free(nghttp2_ping *frame) { (void)frame; }
+
+void nghttp2_frame_goaway_init(nghttp2_goaway *frame, int32_t last_stream_id,
+                               uint32_t error_code, uint8_t *opaque_data,
+                               size_t opaque_data_len) {
+  nghttp2_frame_hd_init(&frame->hd, 8 + opaque_data_len, NGHTTP2_GOAWAY,
+                        NGHTTP2_FLAG_NONE, 0);
+  frame->last_stream_id = last_stream_id;
+  frame->error_code = error_code;
+  frame->opaque_data = opaque_data;
+  frame->opaque_data_len = opaque_data_len;
+  frame->reserved = 0;
+}
+
+void nghttp2_frame_goaway_free(nghttp2_goaway *frame, nghttp2_mem *mem) {
+  nghttp2_mem_free(mem, frame->opaque_data);
+}
+
+void nghttp2_frame_window_update_init(nghttp2_window_update *frame,
+                                      uint8_t flags, int32_t stream_id,
+                                      int32_t window_size_increment) {
+  nghttp2_frame_hd_init(&frame->hd, 4, NGHTTP2_WINDOW_UPDATE, flags, stream_id);
+  frame->window_size_increment = window_size_increment;
+  frame->reserved = 0;
+}
+
+void nghttp2_frame_window_update_free(nghttp2_window_update *frame) {
+  (void)frame;
+}
+
+size_t nghttp2_frame_trail_padlen(nghttp2_frame *frame, size_t padlen) {
+  /* We have iframe->padlen == 0, but iframe->frame.hd.flags may have
+     NGHTTP2_FLAG_PADDED set.  This happens when receiving
+     CONTINUATION frame, since we don't reset flags after HEADERS was
+     received. */
+  if (padlen == 0) {
+    return 0;
+  }
+  return padlen - ((frame->hd.flags & NGHTTP2_FLAG_PADDED) > 0);
+}
+
+void nghttp2_frame_data_init(nghttp2_data *frame, uint8_t flags,
+                             int32_t stream_id) {
+  /* At this moment, the length of DATA frame is unknown */
+  nghttp2_frame_hd_init(&frame->hd, 0, NGHTTP2_DATA, flags, stream_id);
+  frame->padlen = 0;
+}
+
+void nghttp2_frame_data_free(nghttp2_data *frame) { (void)frame; }
+
+void nghttp2_frame_extension_init(nghttp2_extension *frame, uint8_t type,
+                                  uint8_t flags, int32_t stream_id,
+                                  void *payload) {
+  nghttp2_frame_hd_init(&frame->hd, 0, type, flags, stream_id);
+  frame->payload = payload;
+}
+
+void nghttp2_frame_extension_free(nghttp2_extension *frame) { (void)frame; }
+
+void nghttp2_frame_altsvc_init(nghttp2_extension *frame, int32_t stream_id,
+                               uint8_t *origin, size_t origin_len,
+                               uint8_t *field_value, size_t field_value_len) {
+  nghttp2_ext_altsvc *altsvc;
+
+  nghttp2_frame_hd_init(&frame->hd, 2 + origin_len + field_value_len,
+                        NGHTTP2_ALTSVC, NGHTTP2_FLAG_NONE, stream_id);
+
+  altsvc = frame->payload;
+  altsvc->origin = origin;
+  altsvc->origin_len = origin_len;
+  altsvc->field_value = field_value;
+  altsvc->field_value_len = field_value_len;
+}
+
+void nghttp2_frame_altsvc_free(nghttp2_extension *frame, nghttp2_mem *mem) {
+  nghttp2_ext_altsvc *altsvc;
+
+  altsvc = frame->payload;
+  /* We use the same buffer for altsvc->origin and
+     altsvc->field_value. */
+  nghttp2_mem_free(mem, altsvc->origin);
+}
+
+size_t nghttp2_frame_priority_len(uint8_t flags) {
+  if (flags & NGHTTP2_FLAG_PRIORITY) {
+    return NGHTTP2_PRIORITY_SPECLEN;
+  }
+
+  return 0;
+}
+
+size_t nghttp2_frame_headers_payload_nv_offset(nghttp2_headers *frame) {
+  return nghttp2_frame_priority_len(frame->hd.flags);
+}
+
+/*
+ * Call this function after payload was serialized, but not before
+ * changing buf->pos and serializing frame header.
+ *
+ * This function assumes bufs->cur points to the last buf chain of the
+ * frame(s).
+ *
+ * This function serializes frame header for HEADERS/PUSH_PROMISE and
+ * handles their successive CONTINUATION frames.
+ *
+ * We don't process any padding here.
+ */
+static int frame_pack_headers_shared(nghttp2_bufs *bufs,
+                                     nghttp2_frame_hd *frame_hd) {
+  nghttp2_buf *buf;
+  nghttp2_buf_chain *ci, *ce;
+  nghttp2_frame_hd hd;
+
+  buf = &bufs->head->buf;
+
+  hd = *frame_hd;
+  hd.length = nghttp2_buf_len(buf);
+
+  DEBUGF("send: HEADERS/PUSH_PROMISE, payloadlen=%zu\n", hd.length);
+
+  /* We have multiple frame buffers, which means one or more
+     CONTINUATION frame is involved. Remove END_HEADERS flag from the
+     first frame. */
+  if (bufs->head != bufs->cur) {
+    hd.flags = (uint8_t)(hd.flags & ~NGHTTP2_FLAG_END_HEADERS);
+  }
+
+  buf->pos -= NGHTTP2_FRAME_HDLEN;
+  nghttp2_frame_pack_frame_hd(buf->pos, &hd);
+
+  if (bufs->head != bufs->cur) {
+    /* 2nd and later frames are CONTINUATION frames. */
+    hd.type = NGHTTP2_CONTINUATION;
+    /* We don't have no flags except for last CONTINUATION */
+    hd.flags = NGHTTP2_FLAG_NONE;
+
+    ce = bufs->cur;
+
+    for (ci = bufs->head->next; ci != ce; ci = ci->next) {
+      buf = &ci->buf;
+
+      hd.length = nghttp2_buf_len(buf);
+
+      DEBUGF("send: int CONTINUATION, payloadlen=%zu\n", hd.length);
+
+      buf->pos -= NGHTTP2_FRAME_HDLEN;
+      nghttp2_frame_pack_frame_hd(buf->pos, &hd);
+    }
+
+    buf = &ci->buf;
+    hd.length = nghttp2_buf_len(buf);
+    /* Set END_HEADERS flag for last CONTINUATION */
+    hd.flags = NGHTTP2_FLAG_END_HEADERS;
+
+    DEBUGF("send: last CONTINUATION, payloadlen=%zu\n", hd.length);
+
+    buf->pos -= NGHTTP2_FRAME_HDLEN;
+    nghttp2_frame_pack_frame_hd(buf->pos, &hd);
+  }
+
+  return 0;
+}
+
+int nghttp2_frame_pack_headers(nghttp2_bufs *bufs, nghttp2_headers *frame,
+                               nghttp2_hd_deflater *deflater) {
+  size_t nv_offset;
+  int rv;
+  nghttp2_buf *buf;
+
+  assert(bufs->head == bufs->cur);
+
+  nv_offset = nghttp2_frame_headers_payload_nv_offset(frame);
+
+  buf = &bufs->cur->buf;
+
+  buf->pos += nv_offset;
+  buf->last = buf->pos;
+
+  /* This call will adjust buf->last to the correct position */
+  rv = nghttp2_hd_deflate_hd_bufs(deflater, bufs, frame->nva, frame->nvlen);
+
+  if (rv == NGHTTP2_ERR_BUFFER_ERROR) {
+    rv = NGHTTP2_ERR_HEADER_COMP;
+  }
+
+  buf->pos -= nv_offset;
+
+  if (rv != 0) {
+    return rv;
+  }
+
+  if (frame->hd.flags & NGHTTP2_FLAG_PRIORITY) {
+    nghttp2_frame_pack_priority_spec(buf->pos, &frame->pri_spec);
+  }
+
+  frame->padlen = 0;
+  frame->hd.length = nghttp2_bufs_len(bufs);
+
+  return frame_pack_headers_shared(bufs, &frame->hd);
+}
+
+void nghttp2_frame_pack_priority_spec(uint8_t *buf,
+                                      const nghttp2_priority_spec *pri_spec) {
+  nghttp2_put_uint32be(buf, (uint32_t)pri_spec->stream_id);
+  if (pri_spec->exclusive) {
+    buf[0] |= 0x80;
+  }
+  buf[4] = (uint8_t)(pri_spec->weight - 1);
+}
+
+void nghttp2_frame_unpack_priority_spec(nghttp2_priority_spec *pri_spec,
+                                        const uint8_t *payload) {
+  int32_t dep_stream_id;
+  uint8_t exclusive;
+  int32_t weight;
+
+  dep_stream_id = nghttp2_get_uint32(payload) & NGHTTP2_STREAM_ID_MASK;
+  exclusive = (payload[0] & 0x80) > 0;
+  weight = payload[4] + 1;
+
+  nghttp2_priority_spec_init(pri_spec, dep_stream_id, weight, exclusive);
+}
+
+int nghttp2_frame_unpack_headers_payload(nghttp2_headers *frame,
+                                         const uint8_t *payload) {
+  if (frame->hd.flags & NGHTTP2_FLAG_PRIORITY) {
+    nghttp2_frame_unpack_priority_spec(&frame->pri_spec, payload);
+  } else {
+    nghttp2_priority_spec_default_init(&frame->pri_spec);
+  }
+
+  frame->nva = NULL;
+  frame->nvlen = 0;
+
+  return 0;
+}
+
+int nghttp2_frame_pack_priority(nghttp2_bufs *bufs, nghttp2_priority *frame) {
+  nghttp2_buf *buf;
+
+  assert(bufs->head == bufs->cur);
+
+  buf = &bufs->head->buf;
+
+  assert(nghttp2_buf_avail(buf) >= NGHTTP2_PRIORITY_SPECLEN);
+
+  buf->pos -= NGHTTP2_FRAME_HDLEN;
+
+  nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
+
+  nghttp2_frame_pack_priority_spec(buf->last, &frame->pri_spec);
+
+  buf->last += NGHTTP2_PRIORITY_SPECLEN;
+
+  return 0;
+}
+
+void nghttp2_frame_unpack_priority_payload(nghttp2_priority *frame,
+                                           const uint8_t *payload) {
+  nghttp2_frame_unpack_priority_spec(&frame->pri_spec, payload);
+}
+
+int nghttp2_frame_pack_rst_stream(nghttp2_bufs *bufs,
+                                  nghttp2_rst_stream *frame) {
+  nghttp2_buf *buf;
+
+  assert(bufs->head == bufs->cur);
+
+  buf = &bufs->head->buf;
+
+  assert(nghttp2_buf_avail(buf) >= 4);
+
+  buf->pos -= NGHTTP2_FRAME_HDLEN;
+
+  nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
+
+  nghttp2_put_uint32be(buf->last, frame->error_code);
+  buf->last += 4;
+
+  return 0;
+}
+
+void nghttp2_frame_unpack_rst_stream_payload(nghttp2_rst_stream *frame,
+                                             const uint8_t *payload) {
+  frame->error_code = nghttp2_get_uint32(payload);
+}
+
+int nghttp2_frame_pack_settings(nghttp2_bufs *bufs, nghttp2_settings *frame) {
+  nghttp2_buf *buf;
+
+  assert(bufs->head == bufs->cur);
+
+  buf = &bufs->head->buf;
+
+  if (nghttp2_buf_avail(buf) < frame->hd.length) {
+    return NGHTTP2_ERR_FRAME_SIZE_ERROR;
+  }
+
+  buf->pos -= NGHTTP2_FRAME_HDLEN;
+
+  nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
+
+  buf->last +=
+      nghttp2_frame_pack_settings_payload(buf->last, frame->iv, frame->niv);
+
+  return 0;
+}
+
+size_t nghttp2_frame_pack_settings_payload(uint8_t *buf,
+                                           const nghttp2_settings_entry *iv,
+                                           size_t niv) {
+  size_t i;
+  for (i = 0; i < niv; ++i, buf += NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH) {
+    nghttp2_put_uint16be(buf, (uint16_t)iv[i].settings_id);
+    nghttp2_put_uint32be(buf + 2, iv[i].value);
+  }
+  return NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH * niv;
+}
+
+void nghttp2_frame_unpack_settings_payload(nghttp2_settings *frame,
+                                           nghttp2_settings_entry *iv,
+                                           size_t niv) {
+  frame->iv = iv;
+  frame->niv = niv;
+}
+
+void nghttp2_frame_unpack_settings_entry(nghttp2_settings_entry *iv,
+                                         const uint8_t *payload) {
+  iv->settings_id = nghttp2_get_uint16(&payload[0]);
+  iv->value = nghttp2_get_uint32(&payload[2]);
+}
+
+int nghttp2_frame_unpack_settings_payload2(nghttp2_settings_entry **iv_ptr,
+                                           size_t *niv_ptr,
+                                           const uint8_t *payload,
+                                           size_t payloadlen,
+                                           nghttp2_mem *mem) {
+  size_t i;
+
+  *niv_ptr = payloadlen / NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH;
+
+  if (*niv_ptr == 0) {
+    *iv_ptr = NULL;
+
+    return 0;
+  }
+
+  *iv_ptr =
+      nghttp2_mem_malloc(mem, (*niv_ptr) * sizeof(nghttp2_settings_entry));
+
+  if (*iv_ptr == NULL) {
+    return NGHTTP2_ERR_NOMEM;
+  }
+
+  for (i = 0; i < *niv_ptr; ++i) {
+    size_t off = i * NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH;
+    nghttp2_frame_unpack_settings_entry(&(*iv_ptr)[i], &payload[off]);
+  }
+
+  return 0;
+}
+
+int nghttp2_frame_pack_push_promise(nghttp2_bufs *bufs,
+                                    nghttp2_push_promise *frame,
+                                    nghttp2_hd_deflater *deflater) {
+  size_t nv_offset = 4;
+  int rv;
+  nghttp2_buf *buf;
+
+  assert(bufs->head == bufs->cur);
+
+  buf = &bufs->cur->buf;
+
+  buf->pos += nv_offset;
+  buf->last = buf->pos;
+
+  /* This call will adjust buf->last to the correct position */
+  rv = nghttp2_hd_deflate_hd_bufs(deflater, bufs, frame->nva, frame->nvlen);
+
+  if (rv == NGHTTP2_ERR_BUFFER_ERROR) {
+    rv = NGHTTP2_ERR_HEADER_COMP;
+  }
+
+  buf->pos -= nv_offset;
+
+  if (rv != 0) {
+    return rv;
+  }
+
+  nghttp2_put_uint32be(buf->pos, (uint32_t)frame->promised_stream_id);
+
+  frame->padlen = 0;
+  frame->hd.length = nghttp2_bufs_len(bufs);
+
+  return frame_pack_headers_shared(bufs, &frame->hd);
+}
+
+int nghttp2_frame_unpack_push_promise_payload(nghttp2_push_promise *frame,
+                                              const uint8_t *payload) {
+  frame->promised_stream_id =
+      nghttp2_get_uint32(payload) & NGHTTP2_STREAM_ID_MASK;
+  frame->nva = NULL;
+  frame->nvlen = 0;
+  return 0;
+}
+
+int nghttp2_frame_pack_ping(nghttp2_bufs *bufs, nghttp2_ping *frame) {
+  nghttp2_buf *buf;
+
+  assert(bufs->head == bufs->cur);
+
+  buf = &bufs->head->buf;
+
+  assert(nghttp2_buf_avail(buf) >= 8);
+
+  buf->pos -= NGHTTP2_FRAME_HDLEN;
+
+  nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
+
+  buf->last =
+      nghttp2_cpymem(buf->last, frame->opaque_data, sizeof(frame->opaque_data));
+
+  return 0;
+}
+
+void nghttp2_frame_unpack_ping_payload(nghttp2_ping *frame,
+                                       const uint8_t *payload) {
+  memcpy(frame->opaque_data, payload, sizeof(frame->opaque_data));
+}
+
+int nghttp2_frame_pack_goaway(nghttp2_bufs *bufs, nghttp2_goaway *frame) {
+  int rv;
+  nghttp2_buf *buf;
+
+  assert(bufs->head == bufs->cur);
+
+  buf = &bufs->head->buf;
+
+  buf->pos -= NGHTTP2_FRAME_HDLEN;
+
+  nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
+
+  nghttp2_put_uint32be(buf->last, (uint32_t)frame->last_stream_id);
+  buf->last += 4;
+
+  nghttp2_put_uint32be(buf->last, frame->error_code);
+  buf->last += 4;
+
+  rv = nghttp2_bufs_add(bufs, frame->opaque_data, frame->opaque_data_len);
+
+  if (rv == NGHTTP2_ERR_BUFFER_ERROR) {
+    return NGHTTP2_ERR_FRAME_SIZE_ERROR;
+  }
+
+  if (rv != 0) {
+    return rv;
+  }
+
+  return 0;
+}
+
+void nghttp2_frame_unpack_goaway_payload(nghttp2_goaway *frame,
+                                         const uint8_t *payload,
+                                         uint8_t *var_gift_payload,
+                                         size_t var_gift_payloadlen) {
+  frame->last_stream_id = nghttp2_get_uint32(payload) & NGHTTP2_STREAM_ID_MASK;
+  frame->error_code = nghttp2_get_uint32(payload + 4);
+
+  frame->opaque_data = var_gift_payload;
+  frame->opaque_data_len = var_gift_payloadlen;
+}
+
+int nghttp2_frame_unpack_goaway_payload2(nghttp2_goaway *frame,
+                                         const uint8_t *payload,
+                                         size_t payloadlen, nghttp2_mem *mem) {
+  uint8_t *var_gift_payload;
+  size_t var_gift_payloadlen;
+
+  if (payloadlen > 8) {
+    var_gift_payloadlen = payloadlen - 8;
+  } else {
+    var_gift_payloadlen = 0;
+  }
+
+  payloadlen -= var_gift_payloadlen;
+
+  if (!var_gift_payloadlen) {
+    var_gift_payload = NULL;
+  } else {
+    var_gift_payload = nghttp2_mem_malloc(mem, var_gift_payloadlen);
+
+    if (var_gift_payload == NULL) {
+      return NGHTTP2_ERR_NOMEM;
+    }
+
+    memcpy(var_gift_payload, payload + 8, var_gift_payloadlen);
+  }
+
+  nghttp2_frame_unpack_goaway_payload(frame, payload, var_gift_payload,
+                                      var_gift_payloadlen);
+
+  return 0;
+}
+
+int nghttp2_frame_pack_window_update(nghttp2_bufs *bufs,
+                                     nghttp2_window_update *frame) {
+  nghttp2_buf *buf;
+
+  assert(bufs->head == bufs->cur);
+
+  buf = &bufs->head->buf;
+
+  assert(nghttp2_buf_avail(buf) >= 4);
+
+  buf->pos -= NGHTTP2_FRAME_HDLEN;
+
+  nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
+
+  nghttp2_put_uint32be(buf->last, (uint32_t)frame->window_size_increment);
+  buf->last += 4;
+
+  return 0;
+}
+
+void nghttp2_frame_unpack_window_update_payload(nghttp2_window_update *frame,
+                                                const uint8_t *payload) {
+  frame->window_size_increment =
+      nghttp2_get_uint32(payload) & NGHTTP2_WINDOW_SIZE_INCREMENT_MASK;
+}
+
+int nghttp2_frame_pack_altsvc(nghttp2_bufs *bufs, nghttp2_extension *frame) {
+  int rv;
+  nghttp2_buf *buf;
+  nghttp2_ext_altsvc *altsvc;
+
+  /* This is required with --disable-assert. */
+  (void)rv;
+
+  altsvc = frame->payload;
+
+  buf = &bufs->head->buf;
+
+  assert(nghttp2_buf_avail(buf) >=
+         2 + altsvc->origin_len + altsvc->field_value_len);
+
+  buf->pos -= NGHTTP2_FRAME_HDLEN;
+
+  nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
+
+  nghttp2_put_uint16be(buf->last, (uint16_t)altsvc->origin_len);
+  buf->last += 2;
+
+  rv = nghttp2_bufs_add(bufs, altsvc->origin, altsvc->origin_len);
+
+  assert(rv == 0);
+
+  rv = nghttp2_bufs_add(bufs, altsvc->field_value, altsvc->field_value_len);
+
+  assert(rv == 0);
+
+  return 0;
+}
+
+void nghttp2_frame_unpack_altsvc_payload(nghttp2_extension *frame,
+                                         size_t origin_len, uint8_t *payload,
+                                         size_t payloadlen) {
+  nghttp2_ext_altsvc *altsvc;
+  uint8_t *p;
+
+  altsvc = frame->payload;
+  p = payload;
+
+  altsvc->origin = p;
+
+  p += origin_len;
+
+  altsvc->origin_len = origin_len;
+
+  altsvc->field_value = p;
+  altsvc->field_value_len = (size_t)(payload + payloadlen - p);
+}
+
+int nghttp2_frame_unpack_altsvc_payload2(nghttp2_extension *frame,
+                                         const uint8_t *payload,
+                                         size_t payloadlen, nghttp2_mem *mem) {
+  uint8_t *buf;
+  size_t origin_len;
+
+  if (payloadlen < 2) {
+    return NGHTTP2_FRAME_SIZE_ERROR;
+  }
+
+  origin_len = nghttp2_get_uint16(payload);
+
+  buf = nghttp2_mem_malloc(mem, payloadlen - 2);
+  if (!buf) {
+    return NGHTTP2_ERR_NOMEM;
+  }
+
+  nghttp2_cpymem(buf, payload + 2, payloadlen - 2);
+
+  nghttp2_frame_unpack_altsvc_payload(frame, origin_len, buf, payloadlen - 2);
+
+  return 0;
+}
+
+nghttp2_settings_entry *nghttp2_frame_iv_copy(const nghttp2_settings_entry *iv,
+                                              size_t niv, nghttp2_mem *mem) {
+  nghttp2_settings_entry *iv_copy;
+  size_t len = niv * sizeof(nghttp2_settings_entry);
+
+  if (len == 0) {
+    return NULL;
+  }
+
+  iv_copy = nghttp2_mem_malloc(mem, len);
+
+  if (iv_copy == NULL) {
+    return NULL;
+  }
+
+  memcpy(iv_copy, iv, len);
+
+  return iv_copy;
+}
+
+int nghttp2_nv_equal(const nghttp2_nv *a, const nghttp2_nv *b) {
+  return a->namelen == b->namelen && a->valuelen == b->valuelen &&
+         memcmp(a->name, b->name, a->namelen) == 0 &&
+         memcmp(a->value, b->value, a->valuelen) == 0;
+}
+
+void nghttp2_nv_array_del(nghttp2_nv *nva, nghttp2_mem *mem) {
+  nghttp2_mem_free(mem, nva);
+}
+
+static int bytes_compar(const uint8_t *a, size_t alen, const uint8_t *b,
+                        size_t blen) {
+  int rv;
+
+  if (alen == blen) {
+    return memcmp(a, b, alen);
+  }
+
+  if (alen < blen) {
+    rv = memcmp(a, b, alen);
+
+    if (rv == 0) {
+      return -1;
+    }
+
+    return rv;
+  }
+
+  rv = memcmp(a, b, blen);
+
+  if (rv == 0) {
+    return 1;
+  }
+
+  return rv;
+}
+
+int nghttp2_nv_compare_name(const nghttp2_nv *lhs, const nghttp2_nv *rhs) {
+  return bytes_compar(lhs->name, lhs->namelen, rhs->name, rhs->namelen);
+}
+
+static int nv_compar(const void *lhs, const void *rhs) {
+  const nghttp2_nv *a = (const nghttp2_nv *)lhs;
+  const nghttp2_nv *b = (const nghttp2_nv *)rhs;
+  int rv;
+
+  rv = bytes_compar(a->name, a->namelen, b->name, b->namelen);
+
+  if (rv == 0) {
+    return bytes_compar(a->value, a->valuelen, b->value, b->valuelen);
+  }
+
+  return rv;
+}
+
+void nghttp2_nv_array_sort(nghttp2_nv *nva, size_t nvlen) {
+  qsort(nva, nvlen, sizeof(nghttp2_nv), nv_compar);
+}
+
+int nghttp2_nv_array_copy(nghttp2_nv **nva_ptr, const nghttp2_nv *nva,
+                          size_t nvlen, nghttp2_mem *mem) {
+  size_t i;
+  uint8_t *data = NULL;
+  size_t buflen = 0;
+  nghttp2_nv *p;
+
+  if (nvlen == 0) {
+    *nva_ptr = NULL;
+
+    return 0;
+  }
+
+  for (i = 0; i < nvlen; ++i) {
+    /* + 1 for null-termination */
+    if ((nva[i].flags & NGHTTP2_NV_FLAG_NO_COPY_NAME) == 0) {
+      buflen += nva[i].namelen + 1;
+    }
+    if ((nva[i].flags & NGHTTP2_NV_FLAG_NO_COPY_VALUE) == 0) {
+      buflen += nva[i].valuelen + 1;
+    }
+  }
+
+  buflen += sizeof(nghttp2_nv) * nvlen;
+
+  *nva_ptr = nghttp2_mem_malloc(mem, buflen);
+
+  if (*nva_ptr == NULL) {
+    return NGHTTP2_ERR_NOMEM;
+  }
+
+  p = *nva_ptr;
+  data = (uint8_t *)(*nva_ptr) + sizeof(nghttp2_nv) * nvlen;
+
+  for (i = 0; i < nvlen; ++i) {
+    p->flags = nva[i].flags;
+
+    if (nva[i].flags & NGHTTP2_NV_FLAG_NO_COPY_NAME) {
+      p->name = nva[i].name;
+      p->namelen = nva[i].namelen;
+    } else {
+      if (nva[i].namelen) {
+        memcpy(data, nva[i].name, nva[i].namelen);
+      }
+      p->name = data;
+      p->namelen = nva[i].namelen;
+      data[p->namelen] = '\0';
+      nghttp2_downcase(p->name, p->namelen);
+      data += nva[i].namelen + 1;
+    }
+
+    if (nva[i].flags & NGHTTP2_NV_FLAG_NO_COPY_VALUE) {
+      p->value = nva[i].value;
+      p->valuelen = nva[i].valuelen;
+    } else {
+      if (nva[i].valuelen) {
+        memcpy(data, nva[i].value, nva[i].valuelen);
+      }
+      p->value = data;
+      p->valuelen = nva[i].valuelen;
+      data[p->valuelen] = '\0';
+      data += nva[i].valuelen + 1;
+    }
+
+    ++p;
+  }
+  return 0;
+}
+
+int nghttp2_iv_check(const nghttp2_settings_entry *iv, size_t niv) {
+  size_t i;
+  for (i = 0; i < niv; ++i) {
+    switch (iv[i].settings_id) {
+    case NGHTTP2_SETTINGS_HEADER_TABLE_SIZE:
+      break;
+    case NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS:
+      break;
+    case NGHTTP2_SETTINGS_ENABLE_PUSH:
+      if (iv[i].value != 0 && iv[i].value != 1) {
+        return 0;
+      }
+      break;
+    case NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE:
+      if (iv[i].value > (uint32_t)NGHTTP2_MAX_WINDOW_SIZE) {
+        return 0;
+      }
+      break;
+    case NGHTTP2_SETTINGS_MAX_FRAME_SIZE:
+      if (iv[i].value < NGHTTP2_MAX_FRAME_SIZE_MIN ||
+          iv[i].value > NGHTTP2_MAX_FRAME_SIZE_MAX) {
+        return 0;
+      }
+      break;
+    case NGHTTP2_SETTINGS_MAX_HEADER_LIST_SIZE:
+      break;
+    }
+  }
+  return 1;
+}
+
+static void frame_set_pad(nghttp2_buf *buf, size_t padlen, int framehd_only) {
+  size_t trail_padlen;
+  size_t newlen;
+
+  DEBUGF("send: padlen=%zu, shift left 1 bytes\n", padlen);
+
+  memmove(buf->pos - 1, buf->pos, NGHTTP2_FRAME_HDLEN);
+
+  --buf->pos;
+
+  buf->pos[4] |= NGHTTP2_FLAG_PADDED;
+
+  newlen = (nghttp2_get_uint32(buf->pos) >> 8) + padlen;
+  nghttp2_put_uint32be(buf->pos, (uint32_t)((newlen << 8) + buf->pos[3]));
+
+  if (framehd_only) {
+    return;
+  }
+
+  trail_padlen = padlen - 1;
+  buf->pos[NGHTTP2_FRAME_HDLEN] = (uint8_t)trail_padlen;
+
+  /* zero out padding */
+  memset(buf->last, 0, trail_padlen);
+  /* extend buffers trail_padlen bytes, since we ate previous padlen -
+     trail_padlen byte(s) */
+  buf->last += trail_padlen;
+}
+
+int nghttp2_frame_add_pad(nghttp2_bufs *bufs, nghttp2_frame_hd *hd,
+                          size_t padlen, int framehd_only) {
+  nghttp2_buf *buf;
+
+  if (padlen == 0) {
+    DEBUGF("send: padlen = 0, nothing to do\n");
+
+    return 0;
+  }
+
+  /*
+   * We have arranged bufs like this:
+   *
+   *  0                   1                   2                   3
+   *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+   * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+   * | |Frame header     | Frame payload...                          :
+   * +-+-----------------+-------------------------------------------+
+   * | |Frame header     | Frame payload...                          :
+   * +-+-----------------+-------------------------------------------+
+   * | |Frame header     | Frame payload...                          :
+   * +-+-----------------+-------------------------------------------+
+   *
+   * We arranged padding so that it is included in the first frame
+   * completely.  For padded frame, we are going to adjust buf->pos of
+   * frame which includes padding and serialize (memmove) frame header
+   * in the correct position.  Also extends buf->last to include
+   * padding.
+   */
+
+  buf = &bufs->head->buf;
+
+  assert(nghttp2_buf_avail(buf) >= padlen - 1);
+
+  frame_set_pad(buf, padlen, framehd_only);
+
+  hd->length += padlen;
+  hd->flags |= NGHTTP2_FLAG_PADDED;
+
+  DEBUGF("send: final payloadlen=%zu, padlen=%zu\n", hd->length, padlen);
+
+  return 0;
+}

+ 556 - 0
iotkit-embedded/external_libs/nghttp2/nghttp2_frame.h

@@ -0,0 +1,556 @@
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
+ */
+
+
+#ifndef NGHTTP2_FRAME_H
+#define NGHTTP2_FRAME_H
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif /* HAVE_CONFIG_H */
+
+#include "nghttp2.h"
+#include "nghttp2_hd.h"
+#include "nghttp2_buf.h"
+
+#define NGHTTP2_STREAM_ID_MASK ((1u << 31) - 1)
+#define NGHTTP2_PRI_GROUP_ID_MASK ((1u << 31) - 1)
+#define NGHTTP2_PRIORITY_MASK ((1u << 31) - 1)
+#define NGHTTP2_WINDOW_SIZE_INCREMENT_MASK ((1u << 31) - 1)
+#define NGHTTP2_SETTINGS_ID_MASK ((1 << 24) - 1)
+
+/* The number of bytes of frame header. */
+#define NGHTTP2_FRAME_HDLEN 9
+
+#define NGHTTP2_MAX_FRAME_SIZE_MAX ((1 << 24) - 1)
+#define NGHTTP2_MAX_FRAME_SIZE_MIN (1 << 14)
+
+#define NGHTTP2_MAX_PAYLOADLEN 16384
+/* The one frame buffer length for tranmission.  We may use several of
+   them to support CONTINUATION.  To account for Pad Length field, we
+   allocate extra 1 byte, which saves extra large memcopying. */
+#define NGHTTP2_FRAMEBUF_CHUNKLEN                                              \
+  (NGHTTP2_FRAME_HDLEN + 1 + NGHTTP2_MAX_PAYLOADLEN)
+
+/* The default length of DATA frame payload. */
+#define NGHTTP2_DATA_PAYLOADLEN NGHTTP2_MAX_FRAME_SIZE_MIN
+
+/* Maximum headers block size to send, calculated using
+   nghttp2_hd_deflate_bound().  This is the default value, and can be
+   overridden by nghttp2_option_set_max_send_header_block_size(). */
+#define NGHTTP2_MAX_HEADERSLEN 65536
+
+/* The number of bytes for each SETTINGS entry */
+#define NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH 6
+
+/* Length of priority related fields in HEADERS/PRIORITY frames */
+#define NGHTTP2_PRIORITY_SPECLEN 5
+
+/* Maximum length of padding in bytes. */
+#define NGHTTP2_MAX_PADLEN 256
+
+/* Union of extension frame payload */
+typedef union {
+  nghttp2_ext_altsvc altsvc;
+} nghttp2_ext_frame_payload;
+
+void nghttp2_frame_pack_frame_hd(uint8_t *buf, const nghttp2_frame_hd *hd);
+
+void nghttp2_frame_unpack_frame_hd(nghttp2_frame_hd *hd, const uint8_t *buf);
+
+/**
+ * Initializes frame header |hd| with given parameters.  Reserved bit
+ * is set to 0.
+ */
+void nghttp2_frame_hd_init(nghttp2_frame_hd *hd, size_t length, uint8_t type,
+                           uint8_t flags, int32_t stream_id);
+
+/**
+ * Returns the number of priority field depending on the |flags|.  If
+ * |flags| has neither NGHTTP2_FLAG_PRIORITY_GROUP nor
+ * NGHTTP2_FLAG_PRIORITY_DEPENDENCY set, return 0.
+ */
+size_t nghttp2_frame_priority_len(uint8_t flags);
+
+/**
+ * Packs the |pri_spec| in |buf|.  This function assumes |buf| has
+ * enough space for serialization.
+ */
+void nghttp2_frame_pack_priority_spec(uint8_t *buf,
+                                      const nghttp2_priority_spec *pri_spec);
+
+/**
+ * Unpacks the priority specification from payload |payload| of length
+ * |payloadlen| to |pri_spec|.  The |flags| is used to determine what
+ * kind of priority specification is in |payload|.  This function
+ * assumes the |payload| contains whole priority specification.
+ */
+void nghttp2_frame_unpack_priority_spec(nghttp2_priority_spec *pri_spec,
+                                        const uint8_t *payload);
+
+/*
+ * Returns the offset from the HEADERS frame payload where the
+ * compressed header block starts. The frame payload does not include
+ * frame header.
+ */
+size_t nghttp2_frame_headers_payload_nv_offset(nghttp2_headers *frame);
+
+/*
+ * Packs HEADERS frame |frame| in wire format and store it in |bufs|.
+ * This function expands |bufs| as necessary to store frame.
+ *
+ * The caller must make sure that nghttp2_bufs_reset(bufs) is called
+ * before calling this function.
+ *
+ * frame->hd.length is assigned after length is determined during
+ * packing process.  CONTINUATION frames are also serialized in this
+ * function. This function does not handle padding.
+ *
+ * This function returns 0 if it succeeds, or returns one of the
+ * following negative error codes:
+ *
+ * NGHTTP2_ERR_HEADER_COMP
+ *     The deflate operation failed.
+ * NGHTTP2_ERR_NOMEM
+ *     Out of memory.
+ */
+int nghttp2_frame_pack_headers(nghttp2_bufs *bufs, nghttp2_headers *frame,
+                               nghttp2_hd_deflater *deflater);
+
+/*
+ * Unpacks HEADERS frame byte sequence into |frame|.  This function
+ * only unapcks bytes that come before name/value header block and
+ * after possible Pad Length field.
+ *
+ * This function always succeeds and returns 0.
+ */
+int nghttp2_frame_unpack_headers_payload(nghttp2_headers *frame,
+                                         const uint8_t *payload);
+
+/*
+ * Packs PRIORITY frame |frame| in wire format and store it in
+ * |bufs|.
+ *
+ * The caller must make sure that nghttp2_bufs_reset(bufs) is called
+ * before calling this function.
+ *
+ * This function always succeeds and returns 0.
+ */
+int nghttp2_frame_pack_priority(nghttp2_bufs *bufs, nghttp2_priority *frame);
+
+/*
+ * Unpacks PRIORITY wire format into |frame|.
+ */
+void nghttp2_frame_unpack_priority_payload(nghttp2_priority *frame,
+                                           const uint8_t *payload);
+
+/*
+ * Packs RST_STREAM frame |frame| in wire frame format and store it in
+ * |bufs|.
+ *
+ * The caller must make sure that nghttp2_bufs_reset(bufs) is called
+ * before calling this function.
+ *
+ * This function always succeeds and returns 0.
+ */
+int nghttp2_frame_pack_rst_stream(nghttp2_bufs *bufs,
+                                  nghttp2_rst_stream *frame);
+
+/*
+ * Unpacks RST_STREAM frame byte sequence into |frame|.
+ */
+void nghttp2_frame_unpack_rst_stream_payload(nghttp2_rst_stream *frame,
+                                             const uint8_t *payload);
+
+/*
+ * Packs SETTINGS frame |frame| in wire format and store it in
+ * |bufs|.
+ *
+ * The caller must make sure that nghttp2_bufs_reset(bufs) is called
+ * before calling this function.
+ *
+ * This function returns 0 if it succeeds, or returns one of the
+ * following negative error codes:
+ *
+ * NGHTTP2_ERR_FRAME_SIZE_ERROR
+ *     The length of the frame is too large.
+ */
+int nghttp2_frame_pack_settings(nghttp2_bufs *bufs, nghttp2_settings *frame);
+
+/*
+ * Packs the |iv|, which includes |niv| entries, in the |buf|,
+ * assuming the |buf| has at least 8 * |niv| bytes.
+ *
+ * Returns the number of bytes written into the |buf|.
+ */
+size_t nghttp2_frame_pack_settings_payload(uint8_t *buf,
+                                           const nghttp2_settings_entry *iv,
+                                           size_t niv);
+
+void nghttp2_frame_unpack_settings_entry(nghttp2_settings_entry *iv,
+                                         const uint8_t *payload);
+
+/*
+ * Initializes payload of frame->settings.  The |frame| takes
+ * ownership of |iv|.
+ */
+void nghttp2_frame_unpack_settings_payload(nghttp2_settings *frame,
+                                           nghttp2_settings_entry *iv,
+                                           size_t niv);
+
+/*
+ * Unpacks SETTINGS payload into |*iv_ptr|. The number of entries are
+ * assigned to the |*niv_ptr|. This function allocates enough memory
+ * to store the result in |*iv_ptr|. The caller is responsible to free
+ * |*iv_ptr| after its use.
+ *
+ * This function returns 0 if it succeeds or one of the following
+ * negative error codes:
+ *
+ * NGHTTP2_ERR_NOMEM
+ *     Out of memory.
+ */
+int nghttp2_frame_unpack_settings_payload2(nghttp2_settings_entry **iv_ptr,
+                                           size_t *niv_ptr,
+                                           const uint8_t *payload,
+                                           size_t payloadlen, nghttp2_mem *mem);
+
+/*
+ * Packs PUSH_PROMISE frame |frame| in wire format and store it in
+ * |bufs|.  This function expands |bufs| as necessary to store
+ * frame.
+ *
+ * The caller must make sure that nghttp2_bufs_reset(bufs) is called
+ * before calling this function.
+ *
+ * frame->hd.length is assigned after length is determined during
+ * packing process.  CONTINUATION frames are also serialized in this
+ * function. This function does not handle padding.
+ *
+ * This function returns 0 if it succeeds, or returns one of the
+ * following negative error codes:
+ *
+ * NGHTTP2_ERR_HEADER_COMP
+ *     The deflate operation failed.
+ * NGHTTP2_ERR_NOMEM
+ *     Out of memory.
+ */
+int nghttp2_frame_pack_push_promise(nghttp2_bufs *bufs,
+                                    nghttp2_push_promise *frame,
+                                    nghttp2_hd_deflater *deflater);
+
+/*
+ * Unpacks PUSH_PROMISE frame byte sequence into |frame|.  This
+ * function only unapcks bytes that come before name/value header
+ * block and after possible Pad Length field.
+ *
+ * This function returns 0 if it succeeds or one of the following
+ * negative error codes:
+ *
+ * NGHTTP2_ERR_PROTO
+ *     TODO END_HEADERS flag is not set
+ */
+int nghttp2_frame_unpack_push_promise_payload(nghttp2_push_promise *frame,
+                                              const uint8_t *payload);
+
+/*
+ * Packs PING frame |frame| in wire format and store it in
+ * |bufs|.
+ *
+ * The caller must make sure that nghttp2_bufs_reset(bufs) is called
+ * before calling this function.
+ *
+ * This function always succeeds and returns 0.
+ */
+int nghttp2_frame_pack_ping(nghttp2_bufs *bufs, nghttp2_ping *frame);
+
+/*
+ * Unpacks PING wire format into |frame|.
+ */
+void nghttp2_frame_unpack_ping_payload(nghttp2_ping *frame,
+                                       const uint8_t *payload);
+
+/*
+ * Packs GOAWAY frame |frame| in wire format and store it in |bufs|.
+ * This function expands |bufs| as necessary to store frame.
+ *
+ * The caller must make sure that nghttp2_bufs_reset(bufs) is called
+ * before calling this function.
+ *
+ * This function returns 0 if it succeeds or one of the following
+ * negative error codes:
+ *
+ * NGHTTP2_ERR_NOMEM
+ *     Out of memory.
+ * NGHTTP2_ERR_FRAME_SIZE_ERROR
+ *     The length of the frame is too large.
+ */
+int nghttp2_frame_pack_goaway(nghttp2_bufs *bufs, nghttp2_goaway *frame);
+
+/*
+ * Unpacks GOAWAY wire format into |frame|.  The |payload| of length
+ * |payloadlen| contains first 8 bytes of payload.  The
+ * |var_gift_payload| of length |var_gift_payloadlen| contains
+ * remaining payload and its buffer is gifted to the function and then
+ * |frame|.  The |var_gift_payloadlen| must be freed by
+ * nghttp2_frame_goaway_free().
+ */
+void nghttp2_frame_unpack_goaway_payload(nghttp2_goaway *frame,
+                                         const uint8_t *payload,
+                                         uint8_t *var_gift_payload,
+                                         size_t var_gift_payloadlen);
+
+/*
+ * Unpacks GOAWAY wire format into |frame|.  This function only exists
+ * for unit test.  After allocating buffer for debug data, this
+ * function internally calls nghttp2_frame_unpack_goaway_payload().
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * NGHTTP2_ERR_NOMEM
+ *     Out of memory.
+ */
+int nghttp2_frame_unpack_goaway_payload2(nghttp2_goaway *frame,
+                                         const uint8_t *payload,
+                                         size_t payloadlen, nghttp2_mem *mem);
+
+/*
+ * Packs WINDOW_UPDATE frame |frame| in wire frame format and store it
+ * in |bufs|.
+ *
+ * The caller must make sure that nghttp2_bufs_reset(bufs) is called
+ * before calling this function.
+ *
+ * This function always succeeds and returns 0.
+ */
+int nghttp2_frame_pack_window_update(nghttp2_bufs *bufs,
+                                     nghttp2_window_update *frame);
+
+/*
+ * Unpacks WINDOW_UPDATE frame byte sequence into |frame|.
+ */
+void nghttp2_frame_unpack_window_update_payload(nghttp2_window_update *frame,
+                                                const uint8_t *payload);
+
+/*
+ * Packs ALTSVC frame |frame| in wire frame format and store it in
+ * |bufs|.
+ *
+ * The caller must make sure that nghttp2_bufs_reset(bufs) is called
+ * before calling this function.
+ *
+ * This function always succeeds and returns 0.
+ */
+int nghttp2_frame_pack_altsvc(nghttp2_bufs *bufs, nghttp2_extension *ext);
+
+/*
+ * Unpacks ALTSVC wire format into |frame|.  The |payload| of
+ * |payloadlen| bytes contains frame payload.  This function assumes
+ * that frame->payload points to the nghttp2_ext_altsvc object.
+ *
+ * This function always succeeds and returns 0.
+ */
+void nghttp2_frame_unpack_altsvc_payload(nghttp2_extension *frame,
+                                         size_t origin_len, uint8_t *payload,
+                                         size_t payloadlen);
+
+/*
+ * Unpacks ALTSVC wire format into |frame|.  This function only exists
+ * for unit test.  After allocating buffer for fields, this function
+ * internally calls nghttp2_frame_unpack_altsvc_payload().
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * NGHTTP2_ERR_NOMEM
+ *     Out of memory.
+ * NGHTTP2_ERR_FRAME_SIZE_ERROR
+ *     The payload is too small.
+ */
+int nghttp2_frame_unpack_altsvc_payload2(nghttp2_extension *frame,
+                                         const uint8_t *payload,
+                                         size_t payloadlen, nghttp2_mem *mem);
+
+/*
+ * Initializes HEADERS frame |frame| with given values.  |frame| takes
+ * ownership of |nva|, so caller must not free it. If |stream_id| is
+ * not assigned yet, it must be -1.
+ */
+void nghttp2_frame_headers_init(nghttp2_headers *frame, uint8_t flags,
+                                int32_t stream_id, nghttp2_headers_category cat,
+                                const nghttp2_priority_spec *pri_spec,
+                                nghttp2_nv *nva, size_t nvlen);
+
+void nghttp2_frame_headers_free(nghttp2_headers *frame, nghttp2_mem *mem);
+
+void nghttp2_frame_priority_init(nghttp2_priority *frame, int32_t stream_id,
+                                 const nghttp2_priority_spec *pri_spec);
+
+void nghttp2_frame_priority_free(nghttp2_priority *frame);
+
+void nghttp2_frame_rst_stream_init(nghttp2_rst_stream *frame, int32_t stream_id,
+                                   uint32_t error_code);
+
+void nghttp2_frame_rst_stream_free(nghttp2_rst_stream *frame);
+
+/*
+ * Initializes PUSH_PROMISE frame |frame| with given values.  |frame|
+ * takes ownership of |nva|, so caller must not free it.
+ */
+void nghttp2_frame_push_promise_init(nghttp2_push_promise *frame, uint8_t flags,
+                                     int32_t stream_id,
+                                     int32_t promised_stream_id,
+                                     nghttp2_nv *nva, size_t nvlen);
+
+void nghttp2_frame_push_promise_free(nghttp2_push_promise *frame,
+                                     nghttp2_mem *mem);
+
+/*
+ * Initializes SETTINGS frame |frame| with given values. |frame| takes
+ * ownership of |iv|, so caller must not free it. The |flags| are
+ * bitwise-OR of one or more of nghttp2_settings_flag.
+ */
+void nghttp2_frame_settings_init(nghttp2_settings *frame, uint8_t flags,
+                                 nghttp2_settings_entry *iv, size_t niv);
+
+void nghttp2_frame_settings_free(nghttp2_settings *frame, nghttp2_mem *mem);
+
+/*
+ * Initializes PING frame |frame| with given values. If the
+ * |opqeue_data| is not NULL, it must point to 8 bytes memory region
+ * of data. The data pointed by |opaque_data| is copied. It can be
+ * NULL. In this case, 8 bytes NULL is used.
+ */
+void nghttp2_frame_ping_init(nghttp2_ping *frame, uint8_t flags,
+                             const uint8_t *opque_data);
+
+void nghttp2_frame_ping_free(nghttp2_ping *frame);
+
+/*
+ * Initializes GOAWAY frame |frame| with given values. On success,
+ * this function takes ownership of |opaque_data|, so caller must not
+ * free it. If the |opaque_data_len| is 0, opaque_data could be NULL.
+ */
+void nghttp2_frame_goaway_init(nghttp2_goaway *frame, int32_t last_stream_id,
+                               uint32_t error_code, uint8_t *opaque_data,
+                               size_t opaque_data_len);
+
+void nghttp2_frame_goaway_free(nghttp2_goaway *frame, nghttp2_mem *mem);
+
+void nghttp2_frame_window_update_init(nghttp2_window_update *frame,
+                                      uint8_t flags, int32_t stream_id,
+                                      int32_t window_size_increment);
+
+void nghttp2_frame_window_update_free(nghttp2_window_update *frame);
+
+void nghttp2_frame_extension_init(nghttp2_extension *frame, uint8_t type,
+                                  uint8_t flags, int32_t stream_id,
+                                  void *payload);
+
+void nghttp2_frame_extension_free(nghttp2_extension *frame);
+
+/*
+ * Initializes ALTSVC frame |frame| with given values.  This function
+ * assumes that frame->payload points to nghttp2_ext_altsvc object.
+ * Also |origin| and |field_value| are allocated in single buffer,
+ * starting |origin|.  On success, this function takes ownership of
+ * |origin|, so caller must not free it.
+ */
+void nghttp2_frame_altsvc_init(nghttp2_extension *frame, int32_t stream_id,
+                               uint8_t *origin, size_t origin_len,
+                               uint8_t *field_value, size_t field_value_len);
+
+/*
+ * Frees up resources under |frame|.  This function does not free
+ * nghttp2_ext_altsvc object pointed by frame->payload.  This function
+ * only frees origin pointed by nghttp2_ext_altsvc.origin.  Therefore,
+ * other fields must be allocated in the same buffer with origin.
+ */
+void nghttp2_frame_altsvc_free(nghttp2_extension *frame, nghttp2_mem *mem);
+
+/*
+ * Returns the number of padding bytes after payload.  The total
+ * padding length is given in the |padlen|.  The returned value does
+ * not include the Pad Length field.  If |padlen| is 0, this function
+ * returns 0, regardless of frame->hd.flags.
+ */
+size_t nghttp2_frame_trail_padlen(nghttp2_frame *frame, size_t padlen);
+
+void nghttp2_frame_data_init(nghttp2_data *frame, uint8_t flags,
+                             int32_t stream_id);
+
+void nghttp2_frame_data_free(nghttp2_data *frame);
+
+/*
+ * Makes copy of |iv| and return the copy. The |niv| is the number of
+ * entries in |iv|. This function returns the pointer to the copy if
+ * it succeeds, or NULL.
+ */
+nghttp2_settings_entry *nghttp2_frame_iv_copy(const nghttp2_settings_entry *iv,
+                                              size_t niv, nghttp2_mem *mem);
+
+/*
+ * Sorts the |nva| in ascending order of name and value. If names are
+ * equivalent, sort them by value.
+ */
+void nghttp2_nv_array_sort(nghttp2_nv *nva, size_t nvlen);
+
+/*
+ * Copies name/value pairs from |nva|, which contains |nvlen| pairs,
+ * to |*nva_ptr|, which is dynamically allocated so that all items can
+ * be stored.  The resultant name and value in nghttp2_nv are
+ * guaranteed to be NULL-terminated even if the input is not
+ * null-terminated.
+ *
+ * The |*nva_ptr| must be freed using nghttp2_nv_array_del().
+ *
+ * This function returns 0 if it succeeds or one of the following
+ * negative error codes:
+ *
+ * NGHTTP2_ERR_NOMEM
+ *     Out of memory.
+ */
+int nghttp2_nv_array_copy(nghttp2_nv **nva_ptr, const nghttp2_nv *nva,
+                          size_t nvlen, nghttp2_mem *mem);
+
+/*
+ * Returns nonzero if the name/value pair |a| equals to |b|. The name
+ * is compared in case-sensitive, because we ensure that this function
+ * is called after the name is lower-cased.
+ */
+int nghttp2_nv_equal(const nghttp2_nv *a, const nghttp2_nv *b);
+
+/*
+ * Frees |nva|.
+ */
+void nghttp2_nv_array_del(nghttp2_nv *nva, nghttp2_mem *mem);
+
+/*
+ * Checks that the |iv|, which includes |niv| entries, does not have
+ * invalid values.
+ *
+ * This function returns nonzero if it succeeds, or 0.
+ */
+int nghttp2_iv_check(const nghttp2_settings_entry *iv, size_t niv);
+
+/*
+ * Sets Pad Length field and flags and adjusts frame header position
+ * of each buffers in |bufs|.  The number of padding is given in the
+ * |padlen| including Pad Length field.  The |hd| is the frame header
+ * for the serialized data.  This function fills zeros padding region
+ * unless framehd_only is nonzero.
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * NGHTTP2_ERR_NOMEM
+ *     Out of memory.
+ * NGHTTP2_ERR_FRAME_SIZE_ERROR
+ *     The length of the resulting frame is too large.
+ */
+int nghttp2_frame_add_pad(nghttp2_bufs *bufs, nghttp2_frame_hd *hd,
+                          size_t padlen, int framehd_only);
+
+#endif /* NGHTTP2_FRAME_H */

+ 2317 - 0
iotkit-embedded/external_libs/nghttp2/nghttp2_hd.c

@@ -0,0 +1,2317 @@
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
+ */
+
+
+#include "nghttp2_hd.h"
+
+#include <string.h>
+#include <assert.h>
+#include <stdio.h>
+
+#include "nghttp2_helper.h"
+#include "nghttp2_int.h"
+#include "nghttp2_debug.h"
+
+/* Make scalar initialization form of nghttp2_hd_entry */
+#define MAKE_STATIC_ENT(N, V, T, H)                                            \
+  {                                                                            \
+    {NULL, NULL, (uint8_t *)(N), sizeof((N)) - 1, -1},                         \
+        {NULL, NULL, (uint8_t *)(V), sizeof((V)) - 1, -1},                     \
+        {(uint8_t *)(N), (uint8_t *)(V), sizeof((N)) - 1, sizeof((V)) - 1, 0}, \
+        T, H                                                                   \
+  }
+
+/* Generated by mkstatictbl.py */
+/* 3rd parameter is nghttp2_token value for header field name.  We use
+   first enum value if same header names are repeated (e.g.,
+   :status). */
+static nghttp2_hd_static_entry static_table[] = {
+    MAKE_STATIC_ENT(":authority", "", 0, 3153725150u),
+    MAKE_STATIC_ENT(":method", "GET", 1, 695666056u),
+    MAKE_STATIC_ENT(":method", "POST", 1, 695666056u),
+    MAKE_STATIC_ENT(":path", "/", 3, 3292848686u),
+    MAKE_STATIC_ENT(":path", "/index.html", 3, 3292848686u),
+    MAKE_STATIC_ENT(":scheme", "http", 5, 2510477674u),
+    MAKE_STATIC_ENT(":scheme", "https", 5, 2510477674u),
+    MAKE_STATIC_ENT(":status", "200", 7, 4000288983u),
+    MAKE_STATIC_ENT(":status", "204", 7, 4000288983u),
+    MAKE_STATIC_ENT(":status", "206", 7, 4000288983u),
+    MAKE_STATIC_ENT(":status", "304", 7, 4000288983u),
+    MAKE_STATIC_ENT(":status", "400", 7, 4000288983u),
+    MAKE_STATIC_ENT(":status", "404", 7, 4000288983u),
+    MAKE_STATIC_ENT(":status", "500", 7, 4000288983u),
+    MAKE_STATIC_ENT("accept-charset", "", 14, 3664010344u),
+    MAKE_STATIC_ENT("accept-encoding", "gzip, deflate", 15, 3379649177u),
+    MAKE_STATIC_ENT("accept-language", "", 16, 1979086614u),
+    MAKE_STATIC_ENT("accept-ranges", "", 17, 1713753958u),
+    MAKE_STATIC_ENT("accept", "", 18, 136609321u),
+    MAKE_STATIC_ENT("access-control-allow-origin", "", 19, 2710797292u),
+    MAKE_STATIC_ENT("age", "", 20, 742476188u),
+    MAKE_STATIC_ENT("allow", "", 21, 2930878514u),
+    MAKE_STATIC_ENT("authorization", "", 22, 2436257726u),
+    MAKE_STATIC_ENT("cache-control", "", 23, 1355326669u),
+    MAKE_STATIC_ENT("content-disposition", "", 24, 3889184348u),
+    MAKE_STATIC_ENT("content-encoding", "", 25, 65203592u),
+    MAKE_STATIC_ENT("content-language", "", 26, 24973587u),
+    MAKE_STATIC_ENT("content-length", "", 27, 1308181789u),
+    MAKE_STATIC_ENT("content-location", "", 28, 2302364718u),
+    MAKE_STATIC_ENT("content-range", "", 29, 3555523146u),
+    MAKE_STATIC_ENT("content-type", "", 30, 4244048277u),
+    MAKE_STATIC_ENT("cookie", "", 31, 2007449791u),
+    MAKE_STATIC_ENT("date", "", 32, 3564297305u),
+    MAKE_STATIC_ENT("etag", "", 33, 113792960u),
+    MAKE_STATIC_ENT("expect", "", 34, 2530896728u),
+    MAKE_STATIC_ENT("expires", "", 35, 1049544579u),
+    MAKE_STATIC_ENT("from", "", 36, 2513272949u),
+    MAKE_STATIC_ENT("host", "", 37, 2952701295u),
+    MAKE_STATIC_ENT("if-match", "", 38, 3597694698u),
+    MAKE_STATIC_ENT("if-modified-since", "", 39, 2213050793u),
+    MAKE_STATIC_ENT("if-none-match", "", 40, 2536202615u),
+    MAKE_STATIC_ENT("if-range", "", 41, 2340978238u),
+    MAKE_STATIC_ENT("if-unmodified-since", "", 42, 3794814858u),
+    MAKE_STATIC_ENT("last-modified", "", 43, 3226950251u),
+    MAKE_STATIC_ENT("link", "", 44, 232457833u),
+    MAKE_STATIC_ENT("location", "", 45, 200649126u),
+    MAKE_STATIC_ENT("max-forwards", "", 46, 1826162134u),
+    MAKE_STATIC_ENT("proxy-authenticate", "", 47, 2709445359u),
+    MAKE_STATIC_ENT("proxy-authorization", "", 48, 2686392507u),
+    MAKE_STATIC_ENT("range", "", 49, 4208725202u),
+    MAKE_STATIC_ENT("referer", "", 50, 3969579366u),
+    MAKE_STATIC_ENT("refresh", "", 51, 3572655668u),
+    MAKE_STATIC_ENT("retry-after", "", 52, 3336180598u),
+    MAKE_STATIC_ENT("server", "", 53, 1085029842u),
+    MAKE_STATIC_ENT("set-cookie", "", 54, 1848371000u),
+    MAKE_STATIC_ENT("strict-transport-security", "", 55, 4138147361u),
+    MAKE_STATIC_ENT("transfer-encoding", "", 56, 3719590988u),
+    MAKE_STATIC_ENT("user-agent", "", 57, 606444526u),
+    MAKE_STATIC_ENT("vary", "", 58, 1085005381u),
+    MAKE_STATIC_ENT("via", "", 59, 1762798611u),
+    MAKE_STATIC_ENT("www-authenticate", "", 60, 779865858u),
+};
+
+static int memeq(const void *s1, const void *s2, size_t n) {
+  return memcmp(s1, s2, n) == 0;
+}
+
+/*
+ * This function was generated by genlibtokenlookup.py.  Inspired by
+ * h2o header lookup.  https://github.com/h2o/h2o
+ */
+static int32_t lookup_token(const uint8_t *name, size_t namelen) {
+  switch (namelen) {
+  case 2:
+    switch (name[1]) {
+    case 'e':
+      if (memeq("t", name, 1)) {
+        return NGHTTP2_TOKEN_TE;
+      }
+      break;
+    }
+    break;
+  case 3:
+    switch (name[2]) {
+    case 'a':
+      if (memeq("vi", name, 2)) {
+        return NGHTTP2_TOKEN_VIA;
+      }
+      break;
+    case 'e':
+      if (memeq("ag", name, 2)) {
+        return NGHTTP2_TOKEN_AGE;
+      }
+      break;
+    }
+    break;
+  case 4:
+    switch (name[3]) {
+    case 'e':
+      if (memeq("dat", name, 3)) {
+        return NGHTTP2_TOKEN_DATE;
+      }
+      break;
+    case 'g':
+      if (memeq("eta", name, 3)) {
+        return NGHTTP2_TOKEN_ETAG;
+      }
+      break;
+    case 'k':
+      if (memeq("lin", name, 3)) {
+        return NGHTTP2_TOKEN_LINK;
+      }
+      break;
+    case 'm':
+      if (memeq("fro", name, 3)) {
+        return NGHTTP2_TOKEN_FROM;
+      }
+      break;
+    case 't':
+      if (memeq("hos", name, 3)) {
+        return NGHTTP2_TOKEN_HOST;
+      }
+      break;
+    case 'y':
+      if (memeq("var", name, 3)) {
+        return NGHTTP2_TOKEN_VARY;
+      }
+      break;
+    }
+    break;
+  case 5:
+    switch (name[4]) {
+    case 'e':
+      if (memeq("rang", name, 4)) {
+        return NGHTTP2_TOKEN_RANGE;
+      }
+      break;
+    case 'h':
+      if (memeq(":pat", name, 4)) {
+        return NGHTTP2_TOKEN__PATH;
+      }
+      break;
+    case 'w':
+      if (memeq("allo", name, 4)) {
+        return NGHTTP2_TOKEN_ALLOW;
+      }
+      break;
+    }
+    break;
+  case 6:
+    switch (name[5]) {
+    case 'e':
+      if (memeq("cooki", name, 5)) {
+        return NGHTTP2_TOKEN_COOKIE;
+      }
+      break;
+    case 'r':
+      if (memeq("serve", name, 5)) {
+        return NGHTTP2_TOKEN_SERVER;
+      }
+      break;
+    case 't':
+      if (memeq("accep", name, 5)) {
+        return NGHTTP2_TOKEN_ACCEPT;
+      }
+      if (memeq("expec", name, 5)) {
+        return NGHTTP2_TOKEN_EXPECT;
+      }
+      break;
+    }
+    break;
+  case 7:
+    switch (name[6]) {
+    case 'd':
+      if (memeq(":metho", name, 6)) {
+        return NGHTTP2_TOKEN__METHOD;
+      }
+      break;
+    case 'e':
+      if (memeq(":schem", name, 6)) {
+        return NGHTTP2_TOKEN__SCHEME;
+      }
+      if (memeq("upgrad", name, 6)) {
+        return NGHTTP2_TOKEN_UPGRADE;
+      }
+      break;
+    case 'h':
+      if (memeq("refres", name, 6)) {
+        return NGHTTP2_TOKEN_REFRESH;
+      }
+      break;
+    case 'r':
+      if (memeq("refere", name, 6)) {
+        return NGHTTP2_TOKEN_REFERER;
+      }
+      break;
+    case 's':
+      if (memeq(":statu", name, 6)) {
+        return NGHTTP2_TOKEN__STATUS;
+      }
+      if (memeq("expire", name, 6)) {
+        return NGHTTP2_TOKEN_EXPIRES;
+      }
+      break;
+    }
+    break;
+  case 8:
+    switch (name[7]) {
+    case 'e':
+      if (memeq("if-rang", name, 7)) {
+        return NGHTTP2_TOKEN_IF_RANGE;
+      }
+      break;
+    case 'h':
+      if (memeq("if-matc", name, 7)) {
+        return NGHTTP2_TOKEN_IF_MATCH;
+      }
+      break;
+    case 'n':
+      if (memeq("locatio", name, 7)) {
+        return NGHTTP2_TOKEN_LOCATION;
+      }
+      break;
+    }
+    break;
+  case 10:
+    switch (name[9]) {
+    case 'e':
+      if (memeq("keep-aliv", name, 9)) {
+        return NGHTTP2_TOKEN_KEEP_ALIVE;
+      }
+      if (memeq("set-cooki", name, 9)) {
+        return NGHTTP2_TOKEN_SET_COOKIE;
+      }
+      break;
+    case 'n':
+      if (memeq("connectio", name, 9)) {
+        return NGHTTP2_TOKEN_CONNECTION;
+      }
+      break;
+    case 't':
+      if (memeq("user-agen", name, 9)) {
+        return NGHTTP2_TOKEN_USER_AGENT;
+      }
+      break;
+    case 'y':
+      if (memeq(":authorit", name, 9)) {
+        return NGHTTP2_TOKEN__AUTHORITY;
+      }
+      break;
+    }
+    break;
+  case 11:
+    switch (name[10]) {
+    case 'r':
+      if (memeq("retry-afte", name, 10)) {
+        return NGHTTP2_TOKEN_RETRY_AFTER;
+      }
+      break;
+    }
+    break;
+  case 12:
+    switch (name[11]) {
+    case 'e':
+      if (memeq("content-typ", name, 11)) {
+        return NGHTTP2_TOKEN_CONTENT_TYPE;
+      }
+      break;
+    case 's':
+      if (memeq("max-forward", name, 11)) {
+        return NGHTTP2_TOKEN_MAX_FORWARDS;
+      }
+      break;
+    }
+    break;
+  case 13:
+    switch (name[12]) {
+    case 'd':
+      if (memeq("last-modifie", name, 12)) {
+        return NGHTTP2_TOKEN_LAST_MODIFIED;
+      }
+      break;
+    case 'e':
+      if (memeq("content-rang", name, 12)) {
+        return NGHTTP2_TOKEN_CONTENT_RANGE;
+      }
+      break;
+    case 'h':
+      if (memeq("if-none-matc", name, 12)) {
+        return NGHTTP2_TOKEN_IF_NONE_MATCH;
+      }
+      break;
+    case 'l':
+      if (memeq("cache-contro", name, 12)) {
+        return NGHTTP2_TOKEN_CACHE_CONTROL;
+      }
+      break;
+    case 'n':
+      if (memeq("authorizatio", name, 12)) {
+        return NGHTTP2_TOKEN_AUTHORIZATION;
+      }
+      break;
+    case 's':
+      if (memeq("accept-range", name, 12)) {
+        return NGHTTP2_TOKEN_ACCEPT_RANGES;
+      }
+      break;
+    }
+    break;
+  case 14:
+    switch (name[13]) {
+    case 'h':
+      if (memeq("content-lengt", name, 13)) {
+        return NGHTTP2_TOKEN_CONTENT_LENGTH;
+      }
+      break;
+    case 't':
+      if (memeq("accept-charse", name, 13)) {
+        return NGHTTP2_TOKEN_ACCEPT_CHARSET;
+      }
+      break;
+    }
+    break;
+  case 15:
+    switch (name[14]) {
+    case 'e':
+      if (memeq("accept-languag", name, 14)) {
+        return NGHTTP2_TOKEN_ACCEPT_LANGUAGE;
+      }
+      break;
+    case 'g':
+      if (memeq("accept-encodin", name, 14)) {
+        return NGHTTP2_TOKEN_ACCEPT_ENCODING;
+      }
+      break;
+    }
+    break;
+  case 16:
+    switch (name[15]) {
+    case 'e':
+      if (memeq("content-languag", name, 15)) {
+        return NGHTTP2_TOKEN_CONTENT_LANGUAGE;
+      }
+      if (memeq("www-authenticat", name, 15)) {
+        return NGHTTP2_TOKEN_WWW_AUTHENTICATE;
+      }
+      break;
+    case 'g':
+      if (memeq("content-encodin", name, 15)) {
+        return NGHTTP2_TOKEN_CONTENT_ENCODING;
+      }
+      break;
+    case 'n':
+      if (memeq("content-locatio", name, 15)) {
+        return NGHTTP2_TOKEN_CONTENT_LOCATION;
+      }
+      if (memeq("proxy-connectio", name, 15)) {
+        return NGHTTP2_TOKEN_PROXY_CONNECTION;
+      }
+      break;
+    }
+    break;
+  case 17:
+    switch (name[16]) {
+    case 'e':
+      if (memeq("if-modified-sinc", name, 16)) {
+        return NGHTTP2_TOKEN_IF_MODIFIED_SINCE;
+      }
+      break;
+    case 'g':
+      if (memeq("transfer-encodin", name, 16)) {
+        return NGHTTP2_TOKEN_TRANSFER_ENCODING;
+      }
+      break;
+    }
+    break;
+  case 18:
+    switch (name[17]) {
+    case 'e':
+      if (memeq("proxy-authenticat", name, 17)) {
+        return NGHTTP2_TOKEN_PROXY_AUTHENTICATE;
+      }
+      break;
+    }
+    break;
+  case 19:
+    switch (name[18]) {
+    case 'e':
+      if (memeq("if-unmodified-sinc", name, 18)) {
+        return NGHTTP2_TOKEN_IF_UNMODIFIED_SINCE;
+      }
+      break;
+    case 'n':
+      if (memeq("content-dispositio", name, 18)) {
+        return NGHTTP2_TOKEN_CONTENT_DISPOSITION;
+      }
+      if (memeq("proxy-authorizatio", name, 18)) {
+        return NGHTTP2_TOKEN_PROXY_AUTHORIZATION;
+      }
+      break;
+    }
+    break;
+  case 25:
+    switch (name[24]) {
+    case 'y':
+      if (memeq("strict-transport-securit", name, 24)) {
+        return NGHTTP2_TOKEN_STRICT_TRANSPORT_SECURITY;
+      }
+      break;
+    }
+    break;
+  case 27:
+    switch (name[26]) {
+    case 'n':
+      if (memeq("access-control-allow-origi", name, 26)) {
+        return NGHTTP2_TOKEN_ACCESS_CONTROL_ALLOW_ORIGIN;
+      }
+      break;
+    }
+    break;
+  }
+  return -1;
+}
+
+void nghttp2_hd_entry_init(nghttp2_hd_entry *ent, nghttp2_hd_nv *nv) {
+  ent->nv = *nv;
+  ent->cnv.name = nv->name->base;
+  ent->cnv.namelen = nv->name->len;
+  ent->cnv.value = nv->value->base;
+  ent->cnv.valuelen = nv->value->len;
+  ent->cnv.flags = nv->flags;
+  ent->next = NULL;
+  ent->hash = 0;
+
+  nghttp2_rcbuf_incref(ent->nv.name);
+  nghttp2_rcbuf_incref(ent->nv.value);
+}
+
+void nghttp2_hd_entry_free(nghttp2_hd_entry *ent) {
+  nghttp2_rcbuf_decref(ent->nv.value);
+  nghttp2_rcbuf_decref(ent->nv.name);
+}
+
+static int name_eq(const nghttp2_hd_nv *a, const nghttp2_nv *b) {
+  return a->name->len == b->namelen &&
+         memeq(a->name->base, b->name, b->namelen);
+}
+
+static int value_eq(const nghttp2_hd_nv *a, const nghttp2_nv *b) {
+  return a->value->len == b->valuelen &&
+         memeq(a->value->base, b->value, b->valuelen);
+}
+
+static uint32_t name_hash(const nghttp2_nv *nv) {
+  /* 32 bit FNV-1a: http://isthe.com/chongo/tech/comp/fnv/ */
+  uint32_t h = 2166136261u;
+  size_t i;
+
+  for (i = 0; i < nv->namelen; ++i) {
+    h ^= nv->name[i];
+    h += (h << 1) + (h << 4) + (h << 7) + (h << 8) + (h << 24);
+  }
+
+  return h;
+}
+
+static void hd_map_init(nghttp2_hd_map *map) {
+  memset(map, 0, sizeof(nghttp2_hd_map));
+}
+
+static void hd_map_insert(nghttp2_hd_map *map, nghttp2_hd_entry *ent) {
+  nghttp2_hd_entry **bucket;
+
+  bucket = &map->table[ent->hash & (HD_MAP_SIZE - 1)];
+
+  if (*bucket == NULL) {
+    *bucket = ent;
+    return;
+  }
+
+  /* lower index is linked near the root */
+  ent->next = *bucket;
+  *bucket = ent;
+}
+
+static nghttp2_hd_entry *hd_map_find(nghttp2_hd_map *map, int *exact_match,
+                                     const nghttp2_nv *nv, int32_t token,
+                                     uint32_t hash, int name_only) {
+  nghttp2_hd_entry *p;
+  nghttp2_hd_entry *res = NULL;
+
+  *exact_match = 0;
+
+  for (p = map->table[hash & (HD_MAP_SIZE - 1)]; p; p = p->next) {
+    if (token != p->nv.token ||
+        (token == -1 && (hash != p->hash || !name_eq(&p->nv, nv)))) {
+      continue;
+    }
+    if (!res) {
+      res = p;
+      if (name_only) {
+        break;
+      }
+    }
+    if (value_eq(&p->nv, nv)) {
+      res = p;
+      *exact_match = 1;
+      break;
+    }
+  }
+
+  return res;
+}
+
+static void hd_map_remove(nghttp2_hd_map *map, nghttp2_hd_entry *ent) {
+  nghttp2_hd_entry **dst;
+
+  dst = &map->table[ent->hash & (HD_MAP_SIZE - 1)];
+
+  for (; *dst; dst = &(*dst)->next) {
+    if (*dst != ent) {
+      continue;
+    }
+
+    *dst = ent->next;
+    ent->next = NULL;
+    return;
+  }
+}
+
+static int hd_ringbuf_init(nghttp2_hd_ringbuf *ringbuf, size_t bufsize,
+                           nghttp2_mem *mem) {
+  size_t size;
+  for (size = 1; size < bufsize; size <<= 1)
+    ;
+  ringbuf->buffer = nghttp2_mem_malloc(mem, sizeof(nghttp2_hd_entry *) * size);
+  if (ringbuf->buffer == NULL) {
+    return NGHTTP2_ERR_NOMEM;
+  }
+  ringbuf->mask = size - 1;
+  ringbuf->first = 0;
+  ringbuf->len = 0;
+  return 0;
+}
+
+static nghttp2_hd_entry *hd_ringbuf_get(nghttp2_hd_ringbuf *ringbuf,
+                                        size_t idx) {
+  assert(idx < ringbuf->len);
+  return ringbuf->buffer[(ringbuf->first + idx) & ringbuf->mask];
+}
+
+static int hd_ringbuf_reserve(nghttp2_hd_ringbuf *ringbuf, size_t bufsize,
+                              nghttp2_mem *mem) {
+  size_t i;
+  size_t size;
+  nghttp2_hd_entry **buffer;
+
+  if (ringbuf->mask + 1 >= bufsize) {
+    return 0;
+  }
+  for (size = 1; size < bufsize; size <<= 1)
+    ;
+  buffer = nghttp2_mem_malloc(mem, sizeof(nghttp2_hd_entry *) * size);
+  if (buffer == NULL) {
+    return NGHTTP2_ERR_NOMEM;
+  }
+  for (i = 0; i < ringbuf->len; ++i) {
+    buffer[i] = hd_ringbuf_get(ringbuf, i);
+  }
+  nghttp2_mem_free(mem, ringbuf->buffer);
+  ringbuf->buffer = buffer;
+  ringbuf->mask = size - 1;
+  ringbuf->first = 0;
+  return 0;
+}
+
+static void hd_ringbuf_free(nghttp2_hd_ringbuf *ringbuf, nghttp2_mem *mem) {
+  size_t i;
+  if (ringbuf == NULL) {
+    return;
+  }
+  for (i = 0; i < ringbuf->len; ++i) {
+    nghttp2_hd_entry *ent = hd_ringbuf_get(ringbuf, i);
+
+    nghttp2_hd_entry_free(ent);
+    nghttp2_mem_free(mem, ent);
+  }
+  nghttp2_mem_free(mem, ringbuf->buffer);
+}
+
+static int hd_ringbuf_push_front(nghttp2_hd_ringbuf *ringbuf,
+                                 nghttp2_hd_entry *ent, nghttp2_mem *mem) {
+  int rv;
+
+  rv = hd_ringbuf_reserve(ringbuf, ringbuf->len + 1, mem);
+
+  if (rv != 0) {
+    return rv;
+  }
+
+  ringbuf->buffer[--ringbuf->first & ringbuf->mask] = ent;
+  ++ringbuf->len;
+
+  return 0;
+}
+
+static void hd_ringbuf_pop_back(nghttp2_hd_ringbuf *ringbuf) {
+  assert(ringbuf->len > 0);
+  --ringbuf->len;
+}
+
+static int hd_context_init(nghttp2_hd_context *context, nghttp2_mem *mem) {
+  int rv;
+  context->mem = mem;
+  context->bad = 0;
+  context->hd_table_bufsize_max = NGHTTP2_HD_DEFAULT_MAX_BUFFER_SIZE;
+  rv = hd_ringbuf_init(
+      &context->hd_table,
+      context->hd_table_bufsize_max / NGHTTP2_HD_ENTRY_OVERHEAD, mem);
+  if (rv != 0) {
+    return rv;
+  }
+
+  context->hd_table_bufsize = 0;
+  context->next_seq = 0;
+
+  return 0;
+}
+
+static void hd_context_free(nghttp2_hd_context *context) {
+  hd_ringbuf_free(&context->hd_table, context->mem);
+}
+
+int nghttp2_hd_deflate_init(nghttp2_hd_deflater *deflater, nghttp2_mem *mem) {
+  return nghttp2_hd_deflate_init2(
+      deflater, NGHTTP2_HD_DEFAULT_MAX_DEFLATE_BUFFER_SIZE, mem);
+}
+
+int nghttp2_hd_deflate_init2(nghttp2_hd_deflater *deflater,
+                             size_t max_deflate_dynamic_table_size,
+                             nghttp2_mem *mem) {
+  int rv;
+  rv = hd_context_init(&deflater->ctx, mem);
+  if (rv != 0) {
+    return rv;
+  }
+
+  hd_map_init(&deflater->map);
+
+  if (max_deflate_dynamic_table_size < NGHTTP2_HD_DEFAULT_MAX_BUFFER_SIZE) {
+    deflater->notify_table_size_change = 1;
+    deflater->ctx.hd_table_bufsize_max = max_deflate_dynamic_table_size;
+  } else {
+    deflater->notify_table_size_change = 0;
+  }
+
+  deflater->deflate_hd_table_bufsize_max = max_deflate_dynamic_table_size;
+  deflater->min_hd_table_bufsize_max = UINT32_MAX;
+
+  return 0;
+}
+
+int nghttp2_hd_inflate_init(nghttp2_hd_inflater *inflater, nghttp2_mem *mem) {
+  int rv;
+
+  rv = hd_context_init(&inflater->ctx, mem);
+  if (rv != 0) {
+    goto fail;
+  }
+
+  inflater->settings_hd_table_bufsize_max = NGHTTP2_HD_DEFAULT_MAX_BUFFER_SIZE;
+  inflater->min_hd_table_bufsize_max = UINT32_MAX;
+
+  inflater->nv_name_keep = NULL;
+  inflater->nv_value_keep = NULL;
+
+  inflater->opcode = NGHTTP2_HD_OPCODE_NONE;
+  inflater->state = NGHTTP2_HD_STATE_INFLATE_START;
+
+  nghttp2_buf_init(&inflater->namebuf);
+  nghttp2_buf_init(&inflater->valuebuf);
+
+  inflater->namercbuf = NULL;
+  inflater->valuercbuf = NULL;
+
+  inflater->huffman_encoded = 0;
+  inflater->index = 0;
+  inflater->left = 0;
+  inflater->shift = 0;
+  inflater->index_required = 0;
+  inflater->no_index = 0;
+
+  return 0;
+
+fail:
+  return rv;
+}
+
+static void hd_inflate_keep_free(nghttp2_hd_inflater *inflater) {
+  nghttp2_rcbuf_decref(inflater->nv_value_keep);
+  nghttp2_rcbuf_decref(inflater->nv_name_keep);
+
+  inflater->nv_value_keep = NULL;
+  inflater->nv_name_keep = NULL;
+}
+
+void nghttp2_hd_deflate_free(nghttp2_hd_deflater *deflater) {
+  hd_context_free(&deflater->ctx);
+}
+
+void nghttp2_hd_inflate_free(nghttp2_hd_inflater *inflater) {
+  hd_inflate_keep_free(inflater);
+
+  nghttp2_rcbuf_decref(inflater->valuercbuf);
+  nghttp2_rcbuf_decref(inflater->namercbuf);
+
+  hd_context_free(&inflater->ctx);
+}
+
+static size_t entry_room(size_t namelen, size_t valuelen) {
+  return NGHTTP2_HD_ENTRY_OVERHEAD + namelen + valuelen;
+}
+
+static void emit_header(nghttp2_hd_nv *nv_out, nghttp2_hd_nv *nv) {
+  DEBUGF("inflatehd: header emission: %s: %s\n", nv->name->base,
+         nv->value->base);
+  /* ent->ref may be 0. This happens if the encoder emits literal
+     block larger than header table capacity with indexing. */
+  *nv_out = *nv;
+}
+
+static size_t count_encoded_length(size_t n, size_t prefix) {
+  size_t k = (size_t)((1 << prefix) - 1);
+  size_t len = 0;
+
+  if (n < k) {
+    return 1;
+  }
+
+  n -= k;
+  ++len;
+
+  for (; n >= 128; n >>= 7, ++len)
+    ;
+
+  return len + 1;
+}
+
+static size_t encode_length(uint8_t *buf, size_t n, size_t prefix) {
+  size_t k = (size_t)((1 << prefix) - 1);
+  uint8_t *begin = buf;
+
+  *buf = (uint8_t)(*buf & ~k);
+
+  if (n < k) {
+    *buf = (uint8_t)(*buf | n);
+    return 1;
+  }
+
+  *buf = (uint8_t)(*buf | k);
+  ++buf;
+
+  n -= k;
+
+  for (; n >= 128; n >>= 7) {
+    *buf++ = (uint8_t)((1 << 7) | (n & 0x7f));
+  }
+
+  *buf++ = (uint8_t)n;
+
+  return (size_t)(buf - begin);
+}
+
+/*
+ * Decodes |prefix| prefixed integer stored from |in|.  The |last|
+ * represents the 1 beyond the last of the valid contiguous memory
+ * region from |in|.  The decoded integer must be less than or equal
+ * to UINT32_MAX.
+ *
+ * If the |initial| is nonzero, it is used as a initial value, this
+ * function assumes the |in| starts with intermediate data.
+ *
+ * An entire integer is decoded successfully, decoded, the |*fin| is
+ * set to nonzero.
+ *
+ * This function stores the decoded integer in |*res| if it succeed,
+ * including partial decoding (in this case, number of shift to make
+ * in the next call will be stored in |*shift_ptr|) and returns number
+ * of bytes processed, or returns -1, indicating decoding error.
+ */
+static ssize_t decode_length(uint32_t *res, size_t *shift_ptr, int *fin,
+                             uint32_t initial, size_t shift, const uint8_t *in,
+                             const uint8_t *last, size_t prefix) {
+  uint32_t k = (uint8_t)((1 << prefix) - 1);
+  uint32_t n = initial;
+  const uint8_t *start = in;
+
+  *shift_ptr = 0;
+  *fin = 0;
+
+  if (n == 0) {
+    if ((*in & k) != k) {
+      *res = (*in) & k;
+      *fin = 1;
+      return 1;
+    }
+
+    n = k;
+
+    if (++in == last) {
+      *res = n;
+      return (ssize_t)(in - start);
+    }
+  }
+
+  for (; in != last; ++in, shift += 7) {
+    uint32_t add = *in & 0x7f;
+
+    if (shift >= 32) {
+      DEBUGF("inflate: shift exponent overflow\n");
+      return -1;
+    }
+
+    if ((UINT32_MAX >> shift) < add) {
+      DEBUGF("inflate: integer overflow on shift\n");
+      return -1;
+    }
+
+    add <<= shift;
+
+    if (UINT32_MAX - add < n) {
+      DEBUGF("inflate: integer overflow on addition\n");
+      return -1;
+    }
+
+    n += add;
+
+    if ((*in & (1 << 7)) == 0) {
+      break;
+    }
+  }
+
+  *shift_ptr = shift;
+
+  if (in == last) {
+    *res = n;
+    return (ssize_t)(in - start);
+  }
+
+  *res = n;
+  *fin = 1;
+  return (ssize_t)(in + 1 - start);
+}
+
+static int emit_table_size(nghttp2_bufs *bufs, size_t table_size) {
+  int rv;
+  uint8_t *bufp;
+  size_t blocklen;
+  uint8_t sb[16];
+
+  DEBUGF("deflatehd: emit table_size=%zu\n", table_size);
+
+  blocklen = count_encoded_length(table_size, 5);
+
+  if (sizeof(sb) < blocklen) {
+    return NGHTTP2_ERR_HEADER_COMP;
+  }
+
+  bufp = sb;
+
+  *bufp = 0x20u;
+
+  encode_length(bufp, table_size, 5);
+
+  rv = nghttp2_bufs_add(bufs, sb, blocklen);
+  if (rv != 0) {
+    return rv;
+  }
+
+  return 0;
+}
+
+static int emit_indexed_block(nghttp2_bufs *bufs, size_t idx) {
+  int rv;
+  size_t blocklen;
+  uint8_t sb[16];
+  uint8_t *bufp;
+
+  blocklen = count_encoded_length(idx + 1, 7);
+
+  DEBUGF("deflatehd: emit indexed index=%zu, %zu bytes\n", idx, blocklen);
+
+  if (sizeof(sb) < blocklen) {
+    return NGHTTP2_ERR_HEADER_COMP;
+  }
+
+  bufp = sb;
+  *bufp = 0x80u;
+  encode_length(bufp, idx + 1, 7);
+
+  rv = nghttp2_bufs_add(bufs, sb, blocklen);
+  if (rv != 0) {
+    return rv;
+  }
+
+  return 0;
+}
+
+static int emit_string(nghttp2_bufs *bufs, const uint8_t *str, size_t len) {
+  int rv;
+  uint8_t sb[16];
+  uint8_t *bufp;
+  size_t blocklen;
+  size_t enclen;
+  int huffman = 0;
+
+  enclen = nghttp2_hd_huff_encode_count(str, len);
+
+  if (enclen < len) {
+    huffman = 1;
+  } else {
+    enclen = len;
+  }
+
+  blocklen = count_encoded_length(enclen, 7);
+
+  DEBUGF("deflatehd: emit string str=%.*s, length=%zu, huffman=%d, "
+         "encoded_length=%zu\n",
+         (int)len, (const char *)str, len, huffman, enclen);
+
+  if (sizeof(sb) < blocklen) {
+    return NGHTTP2_ERR_HEADER_COMP;
+  }
+
+  bufp = sb;
+  *bufp = huffman ? 1 << 7 : 0;
+  encode_length(bufp, enclen, 7);
+
+  rv = nghttp2_bufs_add(bufs, sb, blocklen);
+  if (rv != 0) {
+    return rv;
+  }
+
+  if (huffman) {
+    rv = nghttp2_hd_huff_encode(bufs, str, len);
+  } else {
+    assert(enclen == len);
+    rv = nghttp2_bufs_add(bufs, str, len);
+  }
+
+  return rv;
+}
+
+static uint8_t pack_first_byte(int indexing_mode) {
+  switch (indexing_mode) {
+  case NGHTTP2_HD_WITH_INDEXING:
+    return 0x40u;
+  case NGHTTP2_HD_WITHOUT_INDEXING:
+    return 0;
+  case NGHTTP2_HD_NEVER_INDEXING:
+    return 0x10u;
+  default:
+    assert(0);
+  }
+  /* This is required to compile with android NDK r10d +
+     --enable-werror */
+  return 0;
+}
+
+static int emit_indname_block(nghttp2_bufs *bufs, size_t idx,
+                              const nghttp2_nv *nv, int indexing_mode) {
+  int rv;
+  uint8_t *bufp;
+  size_t blocklen;
+  uint8_t sb[16];
+  size_t prefixlen;
+
+  if (indexing_mode == NGHTTP2_HD_WITH_INDEXING) {
+    prefixlen = 6;
+  } else {
+    prefixlen = 4;
+  }
+
+  DEBUGF("deflatehd: emit indname index=%zu, valuelen=%zu, indexing_mode=%d\n",
+         idx, nv->valuelen, indexing_mode);
+
+  blocklen = count_encoded_length(idx + 1, prefixlen);
+
+  if (sizeof(sb) < blocklen) {
+    return NGHTTP2_ERR_HEADER_COMP;
+  }
+
+  bufp = sb;
+
+  *bufp = pack_first_byte(indexing_mode);
+
+  encode_length(bufp, idx + 1, prefixlen);
+
+  rv = nghttp2_bufs_add(bufs, sb, blocklen);
+  if (rv != 0) {
+    return rv;
+  }
+
+  rv = emit_string(bufs, nv->value, nv->valuelen);
+  if (rv != 0) {
+    return rv;
+  }
+
+  return 0;
+}
+
+static int emit_newname_block(nghttp2_bufs *bufs, const nghttp2_nv *nv,
+                              int indexing_mode) {
+  int rv;
+
+  DEBUGF(
+      "deflatehd: emit newname namelen=%zu, valuelen=%zu, indexing_mode=%d\n",
+      nv->namelen, nv->valuelen, indexing_mode);
+
+  rv = nghttp2_bufs_addb(bufs, pack_first_byte(indexing_mode));
+  if (rv != 0) {
+    return rv;
+  }
+
+  rv = emit_string(bufs, nv->name, nv->namelen);
+  if (rv != 0) {
+    return rv;
+  }
+
+  rv = emit_string(bufs, nv->value, nv->valuelen);
+  if (rv != 0) {
+    return rv;
+  }
+
+  return 0;
+}
+
+static int add_hd_table_incremental(nghttp2_hd_context *context,
+                                    nghttp2_hd_nv *nv, nghttp2_hd_map *map,
+                                    uint32_t hash) {
+  int rv;
+  nghttp2_hd_entry *new_ent;
+  size_t room;
+  nghttp2_mem *mem;
+
+  mem = context->mem;
+  room = entry_room(nv->name->len, nv->value->len);
+
+  while (context->hd_table_bufsize + room > context->hd_table_bufsize_max &&
+         context->hd_table.len > 0) {
+
+    size_t idx = context->hd_table.len - 1;
+    nghttp2_hd_entry *ent = hd_ringbuf_get(&context->hd_table, idx);
+
+    context->hd_table_bufsize -=
+        entry_room(ent->nv.name->len, ent->nv.value->len);
+
+    DEBUGF("hpack: remove item from header table: %s: %s\n",
+           (char *)ent->nv.name->base, (char *)ent->nv.value->base);
+
+    hd_ringbuf_pop_back(&context->hd_table);
+    if (map) {
+      hd_map_remove(map, ent);
+    }
+
+    nghttp2_hd_entry_free(ent);
+    nghttp2_mem_free(mem, ent);
+  }
+
+  if (room > context->hd_table_bufsize_max) {
+    /* The entry taking more than NGHTTP2_HD_MAX_BUFFER_SIZE is
+       immediately evicted.  So we don't allocate memory for it. */
+    return 0;
+  }
+
+  new_ent = nghttp2_mem_malloc(mem, sizeof(nghttp2_hd_entry));
+  if (new_ent == NULL) {
+    return NGHTTP2_ERR_NOMEM;
+  }
+
+  nghttp2_hd_entry_init(new_ent, nv);
+
+  rv = hd_ringbuf_push_front(&context->hd_table, new_ent, mem);
+
+  if (rv != 0) {
+    nghttp2_hd_entry_free(new_ent);
+    nghttp2_mem_free(mem, new_ent);
+
+    return rv;
+  }
+
+  new_ent->seq = context->next_seq++;
+  new_ent->hash = hash;
+
+  if (map) {
+    hd_map_insert(map, new_ent);
+  }
+
+  context->hd_table_bufsize += room;
+
+  return 0;
+}
+
+typedef struct {
+  ssize_t index;
+  /* Nonzero if both name and value are matched. */
+  int name_value_match;
+} search_result;
+
+static search_result search_static_table(const nghttp2_nv *nv, int32_t token,
+                                         int name_only) {
+  search_result res = {token, 0};
+  int i;
+  nghttp2_hd_static_entry *ent;
+
+  if (name_only) {
+    return res;
+  }
+
+  for (i = token;
+       i <= NGHTTP2_TOKEN_WWW_AUTHENTICATE && static_table[i].token == token;
+       ++i) {
+    ent = &static_table[i];
+    if (ent->value.len == nv->valuelen &&
+        memcmp(ent->value.base, nv->value, nv->valuelen) == 0) {
+      res.index = i;
+      res.name_value_match = 1;
+      return res;
+    }
+  }
+  return res;
+}
+
+static search_result search_hd_table(nghttp2_hd_context *context,
+                                     const nghttp2_nv *nv, int32_t token,
+                                     int indexing_mode, nghttp2_hd_map *map,
+                                     uint32_t hash) {
+  search_result res = {-1, 0};
+  nghttp2_hd_entry *ent;
+  int exact_match;
+  int name_only = indexing_mode == NGHTTP2_HD_NEVER_INDEXING;
+
+  exact_match = 0;
+  ent = hd_map_find(map, &exact_match, nv, token, hash, name_only);
+
+  if (!exact_match && token >= 0 && token <= NGHTTP2_TOKEN_WWW_AUTHENTICATE) {
+    return search_static_table(nv, token, name_only);
+  }
+
+  if (ent == NULL) {
+    return res;
+  }
+
+  res.index =
+      (ssize_t)(context->next_seq - 1 - ent->seq + NGHTTP2_STATIC_TABLE_LENGTH);
+  res.name_value_match = exact_match;
+
+  return res;
+}
+
+static void hd_context_shrink_table_size(nghttp2_hd_context *context,
+                                         nghttp2_hd_map *map) {
+  nghttp2_mem *mem;
+
+  mem = context->mem;
+
+  while (context->hd_table_bufsize > context->hd_table_bufsize_max &&
+         context->hd_table.len > 0) {
+    size_t idx = context->hd_table.len - 1;
+    nghttp2_hd_entry *ent = hd_ringbuf_get(&context->hd_table, idx);
+    context->hd_table_bufsize -=
+        entry_room(ent->nv.name->len, ent->nv.value->len);
+    hd_ringbuf_pop_back(&context->hd_table);
+    if (map) {
+      hd_map_remove(map, ent);
+    }
+
+    nghttp2_hd_entry_free(ent);
+    nghttp2_mem_free(mem, ent);
+  }
+}
+
+int nghttp2_hd_deflate_change_table_size(
+    nghttp2_hd_deflater *deflater, size_t settings_max_dynamic_table_size) {
+  size_t next_bufsize = nghttp2_min(settings_max_dynamic_table_size,
+                                    deflater->deflate_hd_table_bufsize_max);
+
+  deflater->ctx.hd_table_bufsize_max = next_bufsize;
+
+  deflater->min_hd_table_bufsize_max =
+      nghttp2_min(deflater->min_hd_table_bufsize_max, next_bufsize);
+
+  deflater->notify_table_size_change = 1;
+
+  hd_context_shrink_table_size(&deflater->ctx, &deflater->map);
+  return 0;
+}
+
+int nghttp2_hd_inflate_change_table_size(
+    nghttp2_hd_inflater *inflater, size_t settings_max_dynamic_table_size) {
+  switch (inflater->state) {
+  case NGHTTP2_HD_STATE_EXPECT_TABLE_SIZE:
+  case NGHTTP2_HD_STATE_INFLATE_START:
+    break;
+  default:
+    return NGHTTP2_ERR_INVALID_STATE;
+  }
+
+  /* It seems that encoder is not required to send dynamic table size
+     update if the table size is not changed after applying
+     SETTINGS_HEADER_TABLE_SIZE.  RFC 7541 is ambiguous here, but this
+     is the intention of the editor.  If new maximum table size is
+     strictly smaller than the current negotiated maximum size,
+     encoder must send dynamic table size update.  In other cases, we
+     cannot expect it to do so. */
+  if (inflater->ctx.hd_table_bufsize_max > settings_max_dynamic_table_size) {
+    inflater->state = NGHTTP2_HD_STATE_EXPECT_TABLE_SIZE;
+    /* Remember minimum value, and validate that encoder sends the
+       value less than or equal to this. */
+    inflater->min_hd_table_bufsize_max = settings_max_dynamic_table_size;
+  }
+
+  inflater->settings_hd_table_bufsize_max = settings_max_dynamic_table_size;
+
+  inflater->ctx.hd_table_bufsize_max = settings_max_dynamic_table_size;
+
+  hd_context_shrink_table_size(&inflater->ctx, NULL);
+  return 0;
+}
+
+#define INDEX_RANGE_VALID(context, idx)                                        \
+  ((idx) < (context)->hd_table.len + NGHTTP2_STATIC_TABLE_LENGTH)
+
+static size_t get_max_index(nghttp2_hd_context *context) {
+  return context->hd_table.len + NGHTTP2_STATIC_TABLE_LENGTH;
+}
+
+nghttp2_hd_nv nghttp2_hd_table_get(nghttp2_hd_context *context, size_t idx) {
+  assert(INDEX_RANGE_VALID(context, idx));
+  if (idx >= NGHTTP2_STATIC_TABLE_LENGTH) {
+    return hd_ringbuf_get(&context->hd_table, idx - NGHTTP2_STATIC_TABLE_LENGTH)
+        ->nv;
+  } else {
+    nghttp2_hd_static_entry *ent = &static_table[idx];
+    nghttp2_hd_nv nv = {&ent->name, &ent->value, ent->token,
+                        NGHTTP2_NV_FLAG_NONE};
+    return nv;
+  }
+}
+
+static const nghttp2_nv *nghttp2_hd_table_get2(nghttp2_hd_context *context,
+                                               size_t idx) {
+  assert(INDEX_RANGE_VALID(context, idx));
+  if (idx >= NGHTTP2_STATIC_TABLE_LENGTH) {
+    return &hd_ringbuf_get(&context->hd_table,
+                           idx - NGHTTP2_STATIC_TABLE_LENGTH)
+                ->cnv;
+  }
+
+  return &static_table[idx].cnv;
+}
+
+static int hd_deflate_decide_indexing(nghttp2_hd_deflater *deflater,
+                                      const nghttp2_nv *nv, int32_t token) {
+  if (token == NGHTTP2_TOKEN__PATH || token == NGHTTP2_TOKEN_AGE ||
+      token == NGHTTP2_TOKEN_CONTENT_LENGTH || token == NGHTTP2_TOKEN_ETAG ||
+      token == NGHTTP2_TOKEN_IF_MODIFIED_SINCE ||
+      token == NGHTTP2_TOKEN_IF_NONE_MATCH || token == NGHTTP2_TOKEN_LOCATION ||
+      token == NGHTTP2_TOKEN_SET_COOKIE ||
+      entry_room(nv->namelen, nv->valuelen) >
+          deflater->ctx.hd_table_bufsize_max * 3 / 4) {
+    return NGHTTP2_HD_WITHOUT_INDEXING;
+  }
+
+  return NGHTTP2_HD_WITH_INDEXING;
+}
+
+static int deflate_nv(nghttp2_hd_deflater *deflater, nghttp2_bufs *bufs,
+                      const nghttp2_nv *nv) {
+  int rv;
+  search_result res;
+  ssize_t idx;
+  int indexing_mode;
+  int32_t token;
+  nghttp2_mem *mem;
+  uint32_t hash = 0;
+
+  DEBUGF("deflatehd: deflating %.*s: %.*s\n", (int)nv->namelen, nv->name,
+         (int)nv->valuelen, nv->value);
+
+  mem = deflater->ctx.mem;
+
+  token = lookup_token(nv->name, nv->namelen);
+  if (token == -1) {
+    hash = name_hash(nv);
+  } else if (token <= NGHTTP2_TOKEN_WWW_AUTHENTICATE) {
+    hash = static_table[token].hash;
+  }
+
+  /* Don't index authorization header field since it may contain low
+     entropy secret data (e.g., id/password).  Also cookie header
+     field with less than 20 bytes value is also never indexed.  This
+     is the same criteria used in Firefox codebase. */
+  indexing_mode =
+      token == NGHTTP2_TOKEN_AUTHORIZATION ||
+              (token == NGHTTP2_TOKEN_COOKIE && nv->valuelen < 20) ||
+              (nv->flags & NGHTTP2_NV_FLAG_NO_INDEX)
+          ? NGHTTP2_HD_NEVER_INDEXING
+          : hd_deflate_decide_indexing(deflater, nv, token);
+
+  res = search_hd_table(&deflater->ctx, nv, token, indexing_mode,
+                        &deflater->map, hash);
+
+  idx = res.index;
+
+  if (res.name_value_match) {
+
+    DEBUGF("deflatehd: name/value match index=%zd\n", idx);
+
+    rv = emit_indexed_block(bufs, (size_t)idx);
+    if (rv != 0) {
+      return rv;
+    }
+
+    return 0;
+  }
+
+  if (res.index != -1) {
+    DEBUGF("deflatehd: name match index=%zd\n", res.index);
+  }
+
+  if (indexing_mode == NGHTTP2_HD_WITH_INDEXING) {
+    nghttp2_hd_nv hd_nv;
+
+    if (idx != -1 && idx < (ssize_t)NGHTTP2_STATIC_TABLE_LENGTH) {
+      hd_nv.name = nghttp2_hd_table_get(&deflater->ctx, (size_t)idx).name;
+      nghttp2_rcbuf_incref(hd_nv.name);
+    } else {
+      rv = nghttp2_rcbuf_new2(&hd_nv.name, nv->name, nv->namelen, mem);
+      if (rv != 0) {
+        return rv;
+      }
+    }
+
+    rv = nghttp2_rcbuf_new2(&hd_nv.value, nv->value, nv->valuelen, mem);
+
+    if (rv != 0) {
+      nghttp2_rcbuf_decref(hd_nv.name);
+      return rv;
+    }
+
+    hd_nv.token = token;
+    hd_nv.flags = NGHTTP2_NV_FLAG_NONE;
+
+    rv = add_hd_table_incremental(&deflater->ctx, &hd_nv, &deflater->map, hash);
+
+    nghttp2_rcbuf_decref(hd_nv.value);
+    nghttp2_rcbuf_decref(hd_nv.name);
+
+    if (rv != 0) {
+      return NGHTTP2_ERR_HEADER_COMP;
+    }
+  }
+  if (idx == -1) {
+    rv = emit_newname_block(bufs, nv, indexing_mode);
+  } else {
+    rv = emit_indname_block(bufs, (size_t)idx, nv, indexing_mode);
+  }
+  if (rv != 0) {
+    return rv;
+  }
+
+  return 0;
+}
+
+int nghttp2_hd_deflate_hd_bufs(nghttp2_hd_deflater *deflater,
+                               nghttp2_bufs *bufs, const nghttp2_nv *nv,
+                               size_t nvlen) {
+  size_t i;
+  int rv = 0;
+
+  if (deflater->ctx.bad) {
+    return NGHTTP2_ERR_HEADER_COMP;
+  }
+
+  if (deflater->notify_table_size_change) {
+    size_t min_hd_table_bufsize_max;
+
+    min_hd_table_bufsize_max = deflater->min_hd_table_bufsize_max;
+
+    deflater->notify_table_size_change = 0;
+    deflater->min_hd_table_bufsize_max = UINT32_MAX;
+
+    if (deflater->ctx.hd_table_bufsize_max > min_hd_table_bufsize_max) {
+
+      rv = emit_table_size(bufs, min_hd_table_bufsize_max);
+
+      if (rv != 0) {
+        goto fail;
+      }
+    }
+
+    rv = emit_table_size(bufs, deflater->ctx.hd_table_bufsize_max);
+
+    if (rv != 0) {
+      goto fail;
+    }
+  }
+
+  for (i = 0; i < nvlen; ++i) {
+    rv = deflate_nv(deflater, bufs, &nv[i]);
+    if (rv != 0) {
+      goto fail;
+    }
+  }
+
+  DEBUGF("deflatehd: all input name/value pairs were deflated\n");
+
+  return 0;
+fail:
+  DEBUGF("deflatehd: error return %d\n", rv);
+
+  deflater->ctx.bad = 1;
+  return rv;
+}
+
+ssize_t nghttp2_hd_deflate_hd(nghttp2_hd_deflater *deflater, uint8_t *buf,
+                              size_t buflen, const nghttp2_nv *nv,
+                              size_t nvlen) {
+  nghttp2_bufs bufs;
+  int rv;
+  nghttp2_mem *mem;
+
+  mem = deflater->ctx.mem;
+
+  rv = nghttp2_bufs_wrap_init(&bufs, buf, buflen, mem);
+
+  if (rv != 0) {
+    return rv;
+  }
+
+  rv = nghttp2_hd_deflate_hd_bufs(deflater, &bufs, nv, nvlen);
+
+  buflen = nghttp2_bufs_len(&bufs);
+
+  nghttp2_bufs_wrap_free(&bufs);
+
+  if (rv == NGHTTP2_ERR_BUFFER_ERROR) {
+    return NGHTTP2_ERR_INSUFF_BUFSIZE;
+  }
+
+  if (rv != 0) {
+    return rv;
+  }
+
+  return (ssize_t)buflen;
+}
+
+ssize_t nghttp2_hd_deflate_hd_vec(nghttp2_hd_deflater *deflater,
+                                  const nghttp2_vec *vec, size_t veclen,
+                                  const nghttp2_nv *nv, size_t nvlen) {
+  nghttp2_bufs bufs;
+  int rv;
+  nghttp2_mem *mem;
+  size_t buflen;
+
+  mem = deflater->ctx.mem;
+
+  rv = nghttp2_bufs_wrap_init2(&bufs, vec, veclen, mem);
+
+  if (rv != 0) {
+    return rv;
+  }
+
+  rv = nghttp2_hd_deflate_hd_bufs(deflater, &bufs, nv, nvlen);
+
+  buflen = nghttp2_bufs_len(&bufs);
+
+  nghttp2_bufs_wrap_free(&bufs);
+
+  if (rv == NGHTTP2_ERR_BUFFER_ERROR) {
+    return NGHTTP2_ERR_INSUFF_BUFSIZE;
+  }
+
+  if (rv != 0) {
+    return rv;
+  }
+
+  return (ssize_t)buflen;
+}
+
+size_t nghttp2_hd_deflate_bound(nghttp2_hd_deflater *deflater,
+                                const nghttp2_nv *nva, size_t nvlen) {
+  size_t n = 0;
+  size_t i;
+  (void)deflater;
+
+  /* Possible Maximum Header Table Size Change.  Encoding (1u << 31) -
+     1 using 4 bit prefix requires 6 bytes.  We may emit this at most
+     twice. */
+  n += 12;
+
+  /* Use Literal Header Field without indexing - New Name, since it is
+     most space consuming format.  Also we choose the less one between
+     non-huffman and huffman, so using literal byte count is
+     sufficient for upper bound.
+
+     Encoding (1u << 31) - 1 using 7 bit prefix requires 6 bytes.  We
+     need 2 of this for |nvlen| header fields. */
+  n += 6 * 2 * nvlen;
+
+  for (i = 0; i < nvlen; ++i) {
+    n += nva[i].namelen + nva[i].valuelen;
+  }
+
+  return n;
+}
+
+int nghttp2_hd_deflate_new(nghttp2_hd_deflater **deflater_ptr,
+                           size_t deflate_hd_table_bufsize_max) {
+  return nghttp2_hd_deflate_new2(deflater_ptr, deflate_hd_table_bufsize_max,
+                                 NULL);
+}
+
+int nghttp2_hd_deflate_new2(nghttp2_hd_deflater **deflater_ptr,
+                            size_t deflate_hd_table_bufsize_max,
+                            nghttp2_mem *mem) {
+  int rv;
+  nghttp2_hd_deflater *deflater;
+
+  if (mem == NULL) {
+    mem = nghttp2_mem_default();
+  }
+
+  deflater = nghttp2_mem_malloc(mem, sizeof(nghttp2_hd_deflater));
+
+  if (deflater == NULL) {
+    return NGHTTP2_ERR_NOMEM;
+  }
+
+  rv = nghttp2_hd_deflate_init2(deflater, deflate_hd_table_bufsize_max, mem);
+
+  if (rv != 0) {
+    nghttp2_mem_free(mem, deflater);
+
+    return rv;
+  }
+
+  *deflater_ptr = deflater;
+
+  return 0;
+}
+
+void nghttp2_hd_deflate_del(nghttp2_hd_deflater *deflater) {
+  nghttp2_mem *mem;
+
+  mem = deflater->ctx.mem;
+
+  nghttp2_hd_deflate_free(deflater);
+
+  nghttp2_mem_free(mem, deflater);
+}
+
+static void hd_inflate_set_huffman_encoded(nghttp2_hd_inflater *inflater,
+                                           const uint8_t *in) {
+  inflater->huffman_encoded = (*in & (1 << 7)) != 0;
+}
+
+/*
+ * Decodes the integer from the range [in, last).  The result is
+ * assigned to |inflater->left|.  If the |inflater->left| is 0, then
+ * it performs variable integer decoding from scratch. Otherwise, it
+ * uses the |inflater->left| as the initial value and continues to
+ * decode assuming that [in, last) begins with intermediary sequence.
+ *
+ * This function returns the number of bytes read if it succeeds, or
+ * one of the following negative error codes:
+ *
+ * NGHTTP2_ERR_HEADER_COMP
+ *   Integer decoding failed
+ */
+static ssize_t hd_inflate_read_len(nghttp2_hd_inflater *inflater, int *rfin,
+                                   const uint8_t *in, const uint8_t *last,
+                                   size_t prefix, size_t maxlen) {
+  ssize_t rv;
+  uint32_t out;
+
+  *rfin = 0;
+
+  rv = decode_length(&out, &inflater->shift, rfin, (uint32_t)inflater->left,
+                     inflater->shift, in, last, prefix);
+
+  if (rv == -1) {
+    DEBUGF("inflatehd: integer decoding failed\n");
+    return NGHTTP2_ERR_HEADER_COMP;
+  }
+
+  if (out > maxlen) {
+    DEBUGF("inflatehd: integer exceeded the maximum value %zu\n", maxlen);
+    return NGHTTP2_ERR_HEADER_COMP;
+  }
+
+  inflater->left = out;
+
+  DEBUGF("inflatehd: decoded integer is %u\n", out);
+
+  return rv;
+}
+
+/*
+ * Reads |inflater->left| bytes from the range [in, last) and performs
+ * huffman decoding against them and pushes the result into the
+ * |buffer|.
+ *
+ * This function returns the number of bytes read if it succeeds, or
+ * one of the following negative error codes:
+ *
+ * NGHTTP2_ERR_NOMEM
+ *   Out of memory
+ * NGHTTP2_ERR_HEADER_COMP
+ *   Huffman decoding failed
+ */
+static ssize_t hd_inflate_read_huff(nghttp2_hd_inflater *inflater,
+                                    nghttp2_buf *buf, const uint8_t *in,
+                                    const uint8_t *last) {
+  ssize_t readlen;
+  int fin = 0;
+  if ((size_t)(last - in) >= inflater->left) {
+    last = in + inflater->left;
+    fin = 1;
+  }
+  readlen = nghttp2_hd_huff_decode(&inflater->huff_decode_ctx, buf, in,
+                                   (size_t)(last - in), fin);
+
+  if (readlen < 0) {
+    DEBUGF("inflatehd: huffman decoding failed\n");
+    return readlen;
+  }
+  inflater->left -= (size_t)readlen;
+  return readlen;
+}
+
+/*
+ * Reads |inflater->left| bytes from the range [in, last) and copies
+ * them into the |buffer|.
+ *
+ * This function returns the number of bytes read if it succeeds, or
+ * one of the following negative error codes:
+ *
+ * NGHTTP2_ERR_NOMEM
+ *   Out of memory
+ * NGHTTP2_ERR_HEADER_COMP
+ *   Header decompression failed
+ */
+static ssize_t hd_inflate_read(nghttp2_hd_inflater *inflater, nghttp2_buf *buf,
+                               const uint8_t *in, const uint8_t *last) {
+  size_t len = nghttp2_min((size_t)(last - in), inflater->left);
+
+  buf->last = nghttp2_cpymem(buf->last, in, len);
+
+  inflater->left -= len;
+  return (ssize_t)len;
+}
+
+/*
+ * Finalize indexed header representation reception.  The referenced
+ * header is always emitted, and |*nv_out| is filled with that value.
+ */
+static void hd_inflate_commit_indexed(nghttp2_hd_inflater *inflater,
+                                      nghttp2_hd_nv *nv_out) {
+  nghttp2_hd_nv nv = nghttp2_hd_table_get(&inflater->ctx, inflater->index);
+
+  emit_header(nv_out, &nv);
+}
+
+/*
+ * Finalize literal header representation - new name- reception. If
+ * header is emitted, |*nv_out| is filled with that value and 0 is
+ * returned.
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * NGHTTP2_ERR_NOMEM
+ *   Out of memory
+ */
+static int hd_inflate_commit_newname(nghttp2_hd_inflater *inflater,
+                                     nghttp2_hd_nv *nv_out) {
+  nghttp2_hd_nv nv;
+  int rv;
+
+  if (inflater->no_index) {
+    nv.flags = NGHTTP2_NV_FLAG_NO_INDEX;
+  } else {
+    nv.flags = NGHTTP2_NV_FLAG_NONE;
+  }
+
+  nv.name = inflater->namercbuf;
+  nv.value = inflater->valuercbuf;
+  nv.token = lookup_token(inflater->namercbuf->base, inflater->namercbuf->len);
+
+  if (inflater->index_required) {
+    rv = add_hd_table_incremental(&inflater->ctx, &nv, NULL, 0);
+
+    if (rv != 0) {
+      return rv;
+    }
+  }
+
+  emit_header(nv_out, &nv);
+
+  inflater->nv_name_keep = nv.name;
+  inflater->nv_value_keep = nv.value;
+
+  inflater->namercbuf = NULL;
+  inflater->valuercbuf = NULL;
+
+  return 0;
+}
+
+/*
+ * Finalize literal header representation - indexed name-
+ * reception. If header is emitted, |*nv_out| is filled with that
+ * value and 0 is returned.
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * NGHTTP2_ERR_NOMEM
+ *   Out of memory
+ */
+static int hd_inflate_commit_indname(nghttp2_hd_inflater *inflater,
+                                     nghttp2_hd_nv *nv_out) {
+  nghttp2_hd_nv nv;
+  int rv;
+
+  nv = nghttp2_hd_table_get(&inflater->ctx, inflater->index);
+
+  if (inflater->no_index) {
+    nv.flags = NGHTTP2_NV_FLAG_NO_INDEX;
+  } else {
+    nv.flags = NGHTTP2_NV_FLAG_NONE;
+  }
+
+  nghttp2_rcbuf_incref(nv.name);
+
+  nv.value = inflater->valuercbuf;
+
+  if (inflater->index_required) {
+    rv = add_hd_table_incremental(&inflater->ctx, &nv, NULL, 0);
+    if (rv != 0) {
+      nghttp2_rcbuf_decref(nv.name);
+      return NGHTTP2_ERR_NOMEM;
+    }
+  }
+
+  emit_header(nv_out, &nv);
+
+  inflater->nv_name_keep = nv.name;
+  inflater->nv_value_keep = nv.value;
+
+  inflater->valuercbuf = NULL;
+
+  return 0;
+}
+
+ssize_t nghttp2_hd_inflate_hd(nghttp2_hd_inflater *inflater, nghttp2_nv *nv_out,
+                              int *inflate_flags, uint8_t *in, size_t inlen,
+                              int in_final) {
+  return nghttp2_hd_inflate_hd2(inflater, nv_out, inflate_flags, in, inlen,
+                                in_final);
+}
+
+ssize_t nghttp2_hd_inflate_hd2(nghttp2_hd_inflater *inflater,
+                               nghttp2_nv *nv_out, int *inflate_flags,
+                               const uint8_t *in, size_t inlen, int in_final) {
+  ssize_t rv;
+  nghttp2_hd_nv hd_nv;
+
+  rv = nghttp2_hd_inflate_hd_nv(inflater, &hd_nv, inflate_flags, in, inlen,
+                                in_final);
+
+  if (rv < 0) {
+    return rv;
+  }
+
+  if (*inflate_flags & NGHTTP2_HD_INFLATE_EMIT) {
+    nv_out->name = hd_nv.name->base;
+    nv_out->namelen = hd_nv.name->len;
+
+    nv_out->value = hd_nv.value->base;
+    nv_out->valuelen = hd_nv.value->len;
+
+    nv_out->flags = hd_nv.flags;
+  }
+
+  return rv;
+}
+
+ssize_t nghttp2_hd_inflate_hd_nv(nghttp2_hd_inflater *inflater,
+                                 nghttp2_hd_nv *nv_out, int *inflate_flags,
+                                 const uint8_t *in, size_t inlen,
+                                 int in_final) {
+  ssize_t rv = 0;
+  const uint8_t *first = in;
+  const uint8_t *last = in + inlen;
+  int rfin = 0;
+  int busy = 0;
+  nghttp2_mem *mem;
+
+  mem = inflater->ctx.mem;
+
+  if (inflater->ctx.bad) {
+    return NGHTTP2_ERR_HEADER_COMP;
+  }
+
+  DEBUGF("inflatehd: start state=%d\n", inflater->state);
+  hd_inflate_keep_free(inflater);
+  *inflate_flags = NGHTTP2_HD_INFLATE_NONE;
+  for (; in != last || busy;) {
+    busy = 0;
+    switch (inflater->state) {
+    case NGHTTP2_HD_STATE_EXPECT_TABLE_SIZE:
+      if ((*in & 0xe0u) != 0x20u) {
+        DEBUGF("inflatehd: header table size change was expected, but saw "
+               "0x%02x as first byte",
+               *in);
+        rv = NGHTTP2_ERR_HEADER_COMP;
+        goto fail;
+      }
+    /* fall through */
+    case NGHTTP2_HD_STATE_INFLATE_START:
+    case NGHTTP2_HD_STATE_OPCODE:
+      if ((*in & 0xe0u) == 0x20u) {
+        DEBUGF("inflatehd: header table size change\n");
+        if (inflater->state == NGHTTP2_HD_STATE_OPCODE) {
+          DEBUGF("inflatehd: header table size change must appear at the head "
+                 "of header block\n");
+          rv = NGHTTP2_ERR_HEADER_COMP;
+          goto fail;
+        }
+        inflater->opcode = NGHTTP2_HD_OPCODE_INDEXED;
+        inflater->state = NGHTTP2_HD_STATE_READ_TABLE_SIZE;
+      } else if (*in & 0x80u) {
+        DEBUGF("inflatehd: indexed repr\n");
+        inflater->opcode = NGHTTP2_HD_OPCODE_INDEXED;
+        inflater->state = NGHTTP2_HD_STATE_READ_INDEX;
+      } else {
+        if (*in == 0x40u || *in == 0 || *in == 0x10u) {
+          DEBUGF("inflatehd: literal header repr - new name\n");
+          inflater->opcode = NGHTTP2_HD_OPCODE_NEWNAME;
+          inflater->state = NGHTTP2_HD_STATE_NEWNAME_CHECK_NAMELEN;
+        } else {
+          DEBUGF("inflatehd: literal header repr - indexed name\n");
+          inflater->opcode = NGHTTP2_HD_OPCODE_INDNAME;
+          inflater->state = NGHTTP2_HD_STATE_READ_INDEX;
+        }
+        inflater->index_required = (*in & 0x40) != 0;
+        inflater->no_index = (*in & 0xf0u) == 0x10u;
+        DEBUGF("inflatehd: indexing required=%d, no_index=%d\n",
+               inflater->index_required, inflater->no_index);
+        if (inflater->opcode == NGHTTP2_HD_OPCODE_NEWNAME) {
+          ++in;
+        }
+      }
+      inflater->left = 0;
+      inflater->shift = 0;
+      break;
+    case NGHTTP2_HD_STATE_READ_TABLE_SIZE:
+      rfin = 0;
+      rv = hd_inflate_read_len(
+          inflater, &rfin, in, last, 5,
+          nghttp2_min(inflater->min_hd_table_bufsize_max,
+                      inflater->settings_hd_table_bufsize_max));
+      if (rv < 0) {
+        goto fail;
+      }
+      in += rv;
+      if (!rfin) {
+        goto almost_ok;
+      }
+      DEBUGF("inflatehd: table_size=%zu\n", inflater->left);
+      inflater->min_hd_table_bufsize_max = UINT32_MAX;
+      inflater->ctx.hd_table_bufsize_max = inflater->left;
+      hd_context_shrink_table_size(&inflater->ctx, NULL);
+      inflater->state = NGHTTP2_HD_STATE_INFLATE_START;
+      break;
+    case NGHTTP2_HD_STATE_READ_INDEX: {
+      size_t prefixlen;
+
+      if (inflater->opcode == NGHTTP2_HD_OPCODE_INDEXED) {
+        prefixlen = 7;
+      } else if (inflater->index_required) {
+        prefixlen = 6;
+      } else {
+        prefixlen = 4;
+      }
+
+      rfin = 0;
+      rv = hd_inflate_read_len(inflater, &rfin, in, last, prefixlen,
+                               get_max_index(&inflater->ctx));
+      if (rv < 0) {
+        goto fail;
+      }
+
+      in += rv;
+
+      if (!rfin) {
+        goto almost_ok;
+      }
+
+      if (inflater->left == 0) {
+        rv = NGHTTP2_ERR_HEADER_COMP;
+        goto fail;
+      }
+
+      DEBUGF("inflatehd: index=%zu\n", inflater->left);
+      if (inflater->opcode == NGHTTP2_HD_OPCODE_INDEXED) {
+        inflater->index = inflater->left;
+        --inflater->index;
+
+        hd_inflate_commit_indexed(inflater, nv_out);
+
+        inflater->state = NGHTTP2_HD_STATE_OPCODE;
+        *inflate_flags |= NGHTTP2_HD_INFLATE_EMIT;
+        return (ssize_t)(in - first);
+      } else {
+        inflater->index = inflater->left;
+        --inflater->index;
+
+        inflater->state = NGHTTP2_HD_STATE_CHECK_VALUELEN;
+      }
+      break;
+    }
+    case NGHTTP2_HD_STATE_NEWNAME_CHECK_NAMELEN:
+      hd_inflate_set_huffman_encoded(inflater, in);
+      inflater->state = NGHTTP2_HD_STATE_NEWNAME_READ_NAMELEN;
+      inflater->left = 0;
+      inflater->shift = 0;
+      DEBUGF("inflatehd: huffman encoded=%d\n", inflater->huffman_encoded != 0);
+    /* Fall through */
+    case NGHTTP2_HD_STATE_NEWNAME_READ_NAMELEN:
+      rfin = 0;
+      rv = hd_inflate_read_len(inflater, &rfin, in, last, 7, NGHTTP2_HD_MAX_NV);
+      if (rv < 0) {
+        goto fail;
+      }
+      in += rv;
+      if (!rfin) {
+        DEBUGF("inflatehd: integer not fully decoded. current=%zu\n",
+               inflater->left);
+
+        goto almost_ok;
+      }
+
+      if (inflater->huffman_encoded) {
+        nghttp2_hd_huff_decode_context_init(&inflater->huff_decode_ctx);
+
+        inflater->state = NGHTTP2_HD_STATE_NEWNAME_READ_NAMEHUFF;
+
+        rv = nghttp2_rcbuf_new(&inflater->namercbuf, inflater->left * 2 + 1,
+                               mem);
+      } else {
+        inflater->state = NGHTTP2_HD_STATE_NEWNAME_READ_NAME;
+        rv = nghttp2_rcbuf_new(&inflater->namercbuf, inflater->left + 1, mem);
+      }
+
+      if (rv != 0) {
+        goto fail;
+      }
+
+      nghttp2_buf_wrap_init(&inflater->namebuf, inflater->namercbuf->base,
+                            inflater->namercbuf->len);
+
+      break;
+    case NGHTTP2_HD_STATE_NEWNAME_READ_NAMEHUFF:
+      rv = hd_inflate_read_huff(inflater, &inflater->namebuf, in, last);
+      if (rv < 0) {
+        goto fail;
+      }
+
+      in += rv;
+
+      DEBUGF("inflatehd: %zd bytes read\n", rv);
+
+      if (inflater->left) {
+        DEBUGF("inflatehd: still %zu bytes to go\n", inflater->left);
+
+        goto almost_ok;
+      }
+
+      *inflater->namebuf.last = '\0';
+      inflater->namercbuf->len = nghttp2_buf_len(&inflater->namebuf);
+
+      inflater->state = NGHTTP2_HD_STATE_CHECK_VALUELEN;
+
+      break;
+    case NGHTTP2_HD_STATE_NEWNAME_READ_NAME:
+      rv = hd_inflate_read(inflater, &inflater->namebuf, in, last);
+      if (rv < 0) {
+        goto fail;
+      }
+
+      in += rv;
+
+      DEBUGF("inflatehd: %zd bytes read\n", rv);
+      if (inflater->left) {
+        DEBUGF("inflatehd: still %zu bytes to go\n", inflater->left);
+
+        goto almost_ok;
+      }
+
+      *inflater->namebuf.last = '\0';
+      inflater->namercbuf->len = nghttp2_buf_len(&inflater->namebuf);
+
+      inflater->state = NGHTTP2_HD_STATE_CHECK_VALUELEN;
+
+      break;
+    case NGHTTP2_HD_STATE_CHECK_VALUELEN:
+      hd_inflate_set_huffman_encoded(inflater, in);
+      inflater->state = NGHTTP2_HD_STATE_READ_VALUELEN;
+      inflater->left = 0;
+      inflater->shift = 0;
+      DEBUGF("inflatehd: huffman encoded=%d\n", inflater->huffman_encoded != 0);
+    /* Fall through */
+    case NGHTTP2_HD_STATE_READ_VALUELEN:
+      rfin = 0;
+      rv = hd_inflate_read_len(inflater, &rfin, in, last, 7, NGHTTP2_HD_MAX_NV);
+      if (rv < 0) {
+        goto fail;
+      }
+
+      in += rv;
+
+      if (!rfin) {
+        goto almost_ok;
+      }
+
+      DEBUGF("inflatehd: valuelen=%zu\n", inflater->left);
+
+      if (inflater->huffman_encoded) {
+        nghttp2_hd_huff_decode_context_init(&inflater->huff_decode_ctx);
+
+        inflater->state = NGHTTP2_HD_STATE_READ_VALUEHUFF;
+
+        rv = nghttp2_rcbuf_new(&inflater->valuercbuf, inflater->left * 2 + 1,
+                               mem);
+      } else {
+        inflater->state = NGHTTP2_HD_STATE_READ_VALUE;
+
+        rv = nghttp2_rcbuf_new(&inflater->valuercbuf, inflater->left + 1, mem);
+      }
+
+      if (rv != 0) {
+        goto fail;
+      }
+
+      nghttp2_buf_wrap_init(&inflater->valuebuf, inflater->valuercbuf->base,
+                            inflater->valuercbuf->len);
+
+      busy = 1;
+
+      break;
+    case NGHTTP2_HD_STATE_READ_VALUEHUFF:
+      rv = hd_inflate_read_huff(inflater, &inflater->valuebuf, in, last);
+      if (rv < 0) {
+        goto fail;
+      }
+
+      in += rv;
+
+      DEBUGF("inflatehd: %zd bytes read\n", rv);
+
+      if (inflater->left) {
+        DEBUGF("inflatehd: still %zu bytes to go\n", inflater->left);
+
+        goto almost_ok;
+      }
+
+      *inflater->valuebuf.last = '\0';
+      inflater->valuercbuf->len = nghttp2_buf_len(&inflater->valuebuf);
+
+      if (inflater->opcode == NGHTTP2_HD_OPCODE_NEWNAME) {
+        rv = hd_inflate_commit_newname(inflater, nv_out);
+      } else {
+        rv = hd_inflate_commit_indname(inflater, nv_out);
+      }
+
+      if (rv != 0) {
+        goto fail;
+      }
+
+      inflater->state = NGHTTP2_HD_STATE_OPCODE;
+      *inflate_flags |= NGHTTP2_HD_INFLATE_EMIT;
+
+      return (ssize_t)(in - first);
+    case NGHTTP2_HD_STATE_READ_VALUE:
+      rv = hd_inflate_read(inflater, &inflater->valuebuf, in, last);
+      if (rv < 0) {
+        DEBUGF("inflatehd: value read failure %zd: %s\n", rv,
+               nghttp2_strerror((int)rv));
+        goto fail;
+      }
+
+      in += rv;
+
+      DEBUGF("inflatehd: %zd bytes read\n", rv);
+
+      if (inflater->left) {
+        DEBUGF("inflatehd: still %zu bytes to go\n", inflater->left);
+        goto almost_ok;
+      }
+
+      *inflater->valuebuf.last = '\0';
+      inflater->valuercbuf->len = nghttp2_buf_len(&inflater->valuebuf);
+
+      if (inflater->opcode == NGHTTP2_HD_OPCODE_NEWNAME) {
+        rv = hd_inflate_commit_newname(inflater, nv_out);
+      } else {
+        rv = hd_inflate_commit_indname(inflater, nv_out);
+      }
+
+      if (rv != 0) {
+        goto fail;
+      }
+
+      inflater->state = NGHTTP2_HD_STATE_OPCODE;
+      *inflate_flags |= NGHTTP2_HD_INFLATE_EMIT;
+
+      return (ssize_t)(in - first);
+    }
+  }
+
+  assert(in == last);
+
+  DEBUGF("inflatehd: all input bytes were processed\n");
+
+  if (in_final) {
+    DEBUGF("inflatehd: in_final set\n");
+
+    if (inflater->state != NGHTTP2_HD_STATE_OPCODE &&
+        inflater->state != NGHTTP2_HD_STATE_INFLATE_START) {
+      DEBUGF("inflatehd: unacceptable state=%d\n", inflater->state);
+      rv = NGHTTP2_ERR_HEADER_COMP;
+
+      goto fail;
+    }
+    *inflate_flags |= NGHTTP2_HD_INFLATE_FINAL;
+  }
+  return (ssize_t)(in - first);
+
+almost_ok:
+  if (in_final) {
+    DEBUGF("inflatehd: input ended prematurely\n");
+
+    rv = NGHTTP2_ERR_HEADER_COMP;
+
+    goto fail;
+  }
+  return (ssize_t)(in - first);
+
+fail:
+  DEBUGF("inflatehd: error return %zd\n", rv);
+
+  inflater->ctx.bad = 1;
+  return rv;
+}
+
+int nghttp2_hd_inflate_end_headers(nghttp2_hd_inflater *inflater) {
+  hd_inflate_keep_free(inflater);
+  inflater->state = NGHTTP2_HD_STATE_INFLATE_START;
+  return 0;
+}
+
+int nghttp2_hd_inflate_new(nghttp2_hd_inflater **inflater_ptr) {
+  return nghttp2_hd_inflate_new2(inflater_ptr, NULL);
+}
+
+int nghttp2_hd_inflate_new2(nghttp2_hd_inflater **inflater_ptr,
+                            nghttp2_mem *mem) {
+  int rv;
+  nghttp2_hd_inflater *inflater;
+
+  if (mem == NULL) {
+    mem = nghttp2_mem_default();
+  }
+
+  inflater = nghttp2_mem_malloc(mem, sizeof(nghttp2_hd_inflater));
+
+  if (inflater == NULL) {
+    return NGHTTP2_ERR_NOMEM;
+  }
+
+  rv = nghttp2_hd_inflate_init(inflater, mem);
+
+  if (rv != 0) {
+    nghttp2_mem_free(mem, inflater);
+
+    return rv;
+  }
+
+  *inflater_ptr = inflater;
+
+  return 0;
+}
+
+void nghttp2_hd_inflate_del(nghttp2_hd_inflater *inflater) {
+  nghttp2_mem *mem;
+
+  mem = inflater->ctx.mem;
+  nghttp2_hd_inflate_free(inflater);
+
+  nghttp2_mem_free(mem, inflater);
+}
+
+int nghttp2_hd_emit_indname_block(nghttp2_bufs *bufs, size_t idx,
+                                  nghttp2_nv *nv, int indexing_mode) {
+
+  return emit_indname_block(bufs, idx, nv, indexing_mode);
+}
+
+int nghttp2_hd_emit_newname_block(nghttp2_bufs *bufs, nghttp2_nv *nv,
+                                  int indexing_mode) {
+  return emit_newname_block(bufs, nv, indexing_mode);
+}
+
+int nghttp2_hd_emit_table_size(nghttp2_bufs *bufs, size_t table_size) {
+  return emit_table_size(bufs, table_size);
+}
+
+ssize_t nghttp2_hd_decode_length(uint32_t *res, size_t *shift_ptr, int *fin,
+                                 uint32_t initial, size_t shift, uint8_t *in,
+                                 uint8_t *last, size_t prefix) {
+  return decode_length(res, shift_ptr, fin, initial, shift, in, last, prefix);
+}
+
+static const nghttp2_nv *hd_get_table_entry(nghttp2_hd_context *context,
+                                            size_t idx) {
+  if (idx == 0) {
+    return NULL;
+  }
+
+  --idx;
+
+  if (!INDEX_RANGE_VALID(context, idx)) {
+    return NULL;
+  }
+
+  return nghttp2_hd_table_get2(context, idx);
+}
+
+size_t nghttp2_hd_deflate_get_num_table_entries(nghttp2_hd_deflater *deflater) {
+  return get_max_index(&deflater->ctx);
+}
+
+const nghttp2_nv *
+nghttp2_hd_deflate_get_table_entry(nghttp2_hd_deflater *deflater, size_t idx) {
+  return hd_get_table_entry(&deflater->ctx, idx);
+}
+
+size_t
+nghttp2_hd_deflate_get_dynamic_table_size(nghttp2_hd_deflater *deflater) {
+  return deflater->ctx.hd_table_bufsize;
+}
+
+size_t
+nghttp2_hd_deflate_get_max_dynamic_table_size(nghttp2_hd_deflater *deflater) {
+  return deflater->ctx.hd_table_bufsize_max;
+}
+
+size_t nghttp2_hd_inflate_get_num_table_entries(nghttp2_hd_inflater *inflater) {
+  return get_max_index(&inflater->ctx);
+}
+
+const nghttp2_nv *
+nghttp2_hd_inflate_get_table_entry(nghttp2_hd_inflater *inflater, size_t idx) {
+  return hd_get_table_entry(&inflater->ctx, idx);
+}
+
+size_t
+nghttp2_hd_inflate_get_dynamic_table_size(nghttp2_hd_inflater *inflater) {
+  return inflater->ctx.hd_table_bufsize;
+}
+
+size_t
+nghttp2_hd_inflate_get_max_dynamic_table_size(nghttp2_hd_inflater *inflater) {
+  return inflater->ctx.hd_table_bufsize_max;
+}

+ 413 - 0
iotkit-embedded/external_libs/nghttp2/nghttp2_hd.h

@@ -0,0 +1,413 @@
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
+ */
+
+
+#ifndef NGHTTP2_HD_H
+#define NGHTTP2_HD_H
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif /* HAVE_CONFIG_H */
+
+#include "nghttp2.h"
+
+#include "nghttp2_hd_huffman.h"
+#include "nghttp2_buf.h"
+#include "nghttp2_mem.h"
+#include "nghttp2_rcbuf.h"
+
+#define NGHTTP2_HD_DEFAULT_MAX_BUFFER_SIZE NGHTTP2_DEFAULT_HEADER_TABLE_SIZE
+#define NGHTTP2_HD_ENTRY_OVERHEAD 32
+
+/* The maximum length of one name/value pair.  This is the sum of the
+   length of name and value.  This is not specified by the spec. We
+   just chose the arbitrary size */
+#define NGHTTP2_HD_MAX_NV 65536
+
+/* Default size of maximum table buffer size for encoder. Even if
+   remote decoder notifies larger buffer size for its decoding,
+   encoder only uses the memory up to this value. */
+#define NGHTTP2_HD_DEFAULT_MAX_DEFLATE_BUFFER_SIZE (1 << 12)
+
+/* Exported for unit test */
+#define NGHTTP2_STATIC_TABLE_LENGTH 61
+
+/* Generated by genlibtokenlookup.py */
+typedef enum {
+  NGHTTP2_TOKEN__AUTHORITY = 0,
+  NGHTTP2_TOKEN__METHOD = 1,
+  NGHTTP2_TOKEN__PATH = 3,
+  NGHTTP2_TOKEN__SCHEME = 5,
+  NGHTTP2_TOKEN__STATUS = 7,
+  NGHTTP2_TOKEN_ACCEPT_CHARSET = 14,
+  NGHTTP2_TOKEN_ACCEPT_ENCODING = 15,
+  NGHTTP2_TOKEN_ACCEPT_LANGUAGE = 16,
+  NGHTTP2_TOKEN_ACCEPT_RANGES = 17,
+  NGHTTP2_TOKEN_ACCEPT = 18,
+  NGHTTP2_TOKEN_ACCESS_CONTROL_ALLOW_ORIGIN = 19,
+  NGHTTP2_TOKEN_AGE = 20,
+  NGHTTP2_TOKEN_ALLOW = 21,
+  NGHTTP2_TOKEN_AUTHORIZATION = 22,
+  NGHTTP2_TOKEN_CACHE_CONTROL = 23,
+  NGHTTP2_TOKEN_CONTENT_DISPOSITION = 24,
+  NGHTTP2_TOKEN_CONTENT_ENCODING = 25,
+  NGHTTP2_TOKEN_CONTENT_LANGUAGE = 26,
+  NGHTTP2_TOKEN_CONTENT_LENGTH = 27,
+  NGHTTP2_TOKEN_CONTENT_LOCATION = 28,
+  NGHTTP2_TOKEN_CONTENT_RANGE = 29,
+  NGHTTP2_TOKEN_CONTENT_TYPE = 30,
+  NGHTTP2_TOKEN_COOKIE = 31,
+  NGHTTP2_TOKEN_DATE = 32,
+  NGHTTP2_TOKEN_ETAG = 33,
+  NGHTTP2_TOKEN_EXPECT = 34,
+  NGHTTP2_TOKEN_EXPIRES = 35,
+  NGHTTP2_TOKEN_FROM = 36,
+  NGHTTP2_TOKEN_HOST = 37,
+  NGHTTP2_TOKEN_IF_MATCH = 38,
+  NGHTTP2_TOKEN_IF_MODIFIED_SINCE = 39,
+  NGHTTP2_TOKEN_IF_NONE_MATCH = 40,
+  NGHTTP2_TOKEN_IF_RANGE = 41,
+  NGHTTP2_TOKEN_IF_UNMODIFIED_SINCE = 42,
+  NGHTTP2_TOKEN_LAST_MODIFIED = 43,
+  NGHTTP2_TOKEN_LINK = 44,
+  NGHTTP2_TOKEN_LOCATION = 45,
+  NGHTTP2_TOKEN_MAX_FORWARDS = 46,
+  NGHTTP2_TOKEN_PROXY_AUTHENTICATE = 47,
+  NGHTTP2_TOKEN_PROXY_AUTHORIZATION = 48,
+  NGHTTP2_TOKEN_RANGE = 49,
+  NGHTTP2_TOKEN_REFERER = 50,
+  NGHTTP2_TOKEN_REFRESH = 51,
+  NGHTTP2_TOKEN_RETRY_AFTER = 52,
+  NGHTTP2_TOKEN_SERVER = 53,
+  NGHTTP2_TOKEN_SET_COOKIE = 54,
+  NGHTTP2_TOKEN_STRICT_TRANSPORT_SECURITY = 55,
+  NGHTTP2_TOKEN_TRANSFER_ENCODING = 56,
+  NGHTTP2_TOKEN_USER_AGENT = 57,
+  NGHTTP2_TOKEN_VARY = 58,
+  NGHTTP2_TOKEN_VIA = 59,
+  NGHTTP2_TOKEN_WWW_AUTHENTICATE = 60,
+  NGHTTP2_TOKEN_TE,
+  NGHTTP2_TOKEN_CONNECTION,
+  NGHTTP2_TOKEN_KEEP_ALIVE,
+  NGHTTP2_TOKEN_PROXY_CONNECTION,
+  NGHTTP2_TOKEN_UPGRADE,
+} nghttp2_token;
+
+struct nghttp2_hd_entry;
+typedef struct nghttp2_hd_entry nghttp2_hd_entry;
+
+typedef struct {
+  /* The buffer containing header field name.  NULL-termination is
+     guaranteed. */
+  nghttp2_rcbuf *name;
+  /* The buffer containing header field value.  NULL-termination is
+     guaranteed. */
+  nghttp2_rcbuf *value;
+  /* nghttp2_token value for name.  It could be -1 if we have no token
+     for that header field name. */
+  int32_t token;
+  /* Bitwise OR of one or more of nghttp2_nv_flag. */
+  uint8_t flags;
+} nghttp2_hd_nv;
+
+struct nghttp2_hd_entry {
+  /* The header field name/value pair */
+  nghttp2_hd_nv nv;
+  /* This is solely for nghttp2_hd_{deflate,inflate}_get_table_entry
+     APIs to keep backward compatibility. */
+  nghttp2_nv cnv;
+  /* The next entry which shares same bucket in hash table. */
+  nghttp2_hd_entry *next;
+  /* The sequence number.  We will increment it by one whenever we
+     store nghttp2_hd_entry to dynamic header table. */
+  uint32_t seq;
+  /* The hash value for header name (nv.name). */
+  uint32_t hash;
+};
+
+/* The entry used for static header table. */
+typedef struct {
+  nghttp2_rcbuf name;
+  nghttp2_rcbuf value;
+  nghttp2_nv cnv;
+  int32_t token;
+  uint32_t hash;
+} nghttp2_hd_static_entry;
+
+typedef struct {
+  nghttp2_hd_entry **buffer;
+  size_t mask;
+  size_t first;
+  size_t len;
+} nghttp2_hd_ringbuf;
+
+typedef enum {
+  NGHTTP2_HD_OPCODE_NONE,
+  NGHTTP2_HD_OPCODE_INDEXED,
+  NGHTTP2_HD_OPCODE_NEWNAME,
+  NGHTTP2_HD_OPCODE_INDNAME
+} nghttp2_hd_opcode;
+
+typedef enum {
+  NGHTTP2_HD_STATE_EXPECT_TABLE_SIZE,
+  NGHTTP2_HD_STATE_INFLATE_START,
+  NGHTTP2_HD_STATE_OPCODE,
+  NGHTTP2_HD_STATE_READ_TABLE_SIZE,
+  NGHTTP2_HD_STATE_READ_INDEX,
+  NGHTTP2_HD_STATE_NEWNAME_CHECK_NAMELEN,
+  NGHTTP2_HD_STATE_NEWNAME_READ_NAMELEN,
+  NGHTTP2_HD_STATE_NEWNAME_READ_NAMEHUFF,
+  NGHTTP2_HD_STATE_NEWNAME_READ_NAME,
+  NGHTTP2_HD_STATE_CHECK_VALUELEN,
+  NGHTTP2_HD_STATE_READ_VALUELEN,
+  NGHTTP2_HD_STATE_READ_VALUEHUFF,
+  NGHTTP2_HD_STATE_READ_VALUE
+} nghttp2_hd_inflate_state;
+
+typedef enum {
+  NGHTTP2_HD_WITH_INDEXING,
+  NGHTTP2_HD_WITHOUT_INDEXING,
+  NGHTTP2_HD_NEVER_INDEXING
+} nghttp2_hd_indexing_mode;
+
+typedef struct {
+  /* dynamic header table */
+  nghttp2_hd_ringbuf hd_table;
+  /* Memory allocator */
+  nghttp2_mem *mem;
+  /* Abstract buffer size of hd_table as described in the spec. This
+     is the sum of length of name/value in hd_table +
+     NGHTTP2_HD_ENTRY_OVERHEAD bytes overhead per each entry. */
+  size_t hd_table_bufsize;
+  /* The effective header table size. */
+  size_t hd_table_bufsize_max;
+  /* Next sequence number for nghttp2_hd_entry */
+  uint32_t next_seq;
+  /* If inflate/deflate error occurred, this value is set to 1 and
+     further invocation of inflate/deflate will fail with
+     NGHTTP2_ERR_HEADER_COMP. */
+  uint8_t bad;
+} nghttp2_hd_context;
+
+#define HD_MAP_SIZE 128
+
+typedef struct {
+  nghttp2_hd_entry *table[HD_MAP_SIZE];
+} nghttp2_hd_map;
+
+struct nghttp2_hd_deflater {
+  nghttp2_hd_context ctx;
+  nghttp2_hd_map map;
+  /* The upper limit of the header table size the deflater accepts. */
+  size_t deflate_hd_table_bufsize_max;
+  /* Minimum header table size notified in the next context update */
+  size_t min_hd_table_bufsize_max;
+  /* If nonzero, send header table size using encoding context update
+     in the next deflate process */
+  uint8_t notify_table_size_change;
+};
+
+struct nghttp2_hd_inflater {
+  nghttp2_hd_context ctx;
+  /* Stores current state of huffman decoding */
+  nghttp2_hd_huff_decode_context huff_decode_ctx;
+  /* header buffer */
+  nghttp2_buf namebuf, valuebuf;
+  nghttp2_rcbuf *namercbuf, *valuercbuf;
+  /* Pointer to the name/value pair which are used in the current
+     header emission. */
+  nghttp2_rcbuf *nv_name_keep, *nv_value_keep;
+  /* The number of bytes to read */
+  size_t left;
+  /* The index in indexed repr or indexed name */
+  size_t index;
+  /* The maximum header table size the inflater supports. This is the
+     same value transmitted in SETTINGS_HEADER_TABLE_SIZE */
+  size_t settings_hd_table_bufsize_max;
+  /* Minimum header table size set by nghttp2_hd_inflate_change_table_size */
+  size_t min_hd_table_bufsize_max;
+  /* The number of next shift to decode integer */
+  size_t shift;
+  nghttp2_hd_opcode opcode;
+  nghttp2_hd_inflate_state state;
+  /* nonzero if string is huffman encoded */
+  uint8_t huffman_encoded;
+  /* nonzero if deflater requires that current entry is indexed */
+  uint8_t index_required;
+  /* nonzero if deflater requires that current entry must not be
+     indexed */
+  uint8_t no_index;
+};
+
+/*
+ * Initializes the |ent| members.  The reference counts of nv->name
+ * and nv->value are increased by one for each.
+ */
+void nghttp2_hd_entry_init(nghttp2_hd_entry *ent, nghttp2_hd_nv *nv);
+
+/*
+ * This function decreases the reference counts of nv->name and
+ * nv->value.
+ */
+void nghttp2_hd_entry_free(nghttp2_hd_entry *ent);
+
+/*
+ * Initializes |deflater| for deflating name/values pairs.
+ *
+ * The encoder only uses up to
+ * NGHTTP2_HD_DEFAULT_MAX_DEFLATE_BUFFER_SIZE bytes for header table
+ * even if the larger value is specified later in
+ * nghttp2_hd_change_table_size().
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * NGHTTP2_ERR_NOMEM
+ *     Out of memory.
+ */
+int nghttp2_hd_deflate_init(nghttp2_hd_deflater *deflater, nghttp2_mem *mem);
+
+/*
+ * Initializes |deflater| for deflating name/values pairs.
+ *
+ * The encoder only uses up to |max_deflate_dynamic_table_size| bytes
+ * for header table even if the larger value is specified later in
+ * nghttp2_hd_change_table_size().
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * NGHTTP2_ERR_NOMEM
+ *     Out of memory.
+ */
+int nghttp2_hd_deflate_init2(nghttp2_hd_deflater *deflater,
+                             size_t max_deflate_dynamic_table_size,
+                             nghttp2_mem *mem);
+
+/*
+ * Deallocates any resources allocated for |deflater|.
+ */
+void nghttp2_hd_deflate_free(nghttp2_hd_deflater *deflater);
+
+/*
+ * Deflates the |nva|, which has the |nvlen| name/value pairs, into
+ * the |bufs|.
+ *
+ * This function expands |bufs| as necessary to store the result. If
+ * buffers is full and the process still requires more space, this
+ * function fails and returns NGHTTP2_ERR_HEADER_COMP.
+ *
+ * After this function returns, it is safe to delete the |nva|.
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * NGHTTP2_ERR_NOMEM
+ *     Out of memory.
+ * NGHTTP2_ERR_HEADER_COMP
+ *     Deflation process has failed.
+ * NGHTTP2_ERR_BUFFER_ERROR
+ *     Out of buffer space.
+ */
+int nghttp2_hd_deflate_hd_bufs(nghttp2_hd_deflater *deflater,
+                               nghttp2_bufs *bufs, const nghttp2_nv *nva,
+                               size_t nvlen);
+
+/*
+ * Initializes |inflater| for inflating name/values pairs.
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * :enum:`NGHTTP2_ERR_NOMEM`
+ *     Out of memory.
+ */
+int nghttp2_hd_inflate_init(nghttp2_hd_inflater *inflater, nghttp2_mem *mem);
+
+/*
+ * Deallocates any resources allocated for |inflater|.
+ */
+void nghttp2_hd_inflate_free(nghttp2_hd_inflater *inflater);
+
+/*
+ * Similar to nghttp2_hd_inflate_hd(), but this takes nghttp2_hd_nv
+ * instead of nghttp2_nv as output parameter |nv_out|.  Other than
+ * that return values and semantics are the same as
+ * nghttp2_hd_inflate_hd().
+ */
+ssize_t nghttp2_hd_inflate_hd_nv(nghttp2_hd_inflater *inflater,
+                                 nghttp2_hd_nv *nv_out, int *inflate_flags,
+                                 const uint8_t *in, size_t inlen, int in_final);
+
+/* For unittesting purpose */
+int nghttp2_hd_emit_indname_block(nghttp2_bufs *bufs, size_t index,
+                                  nghttp2_nv *nv, int indexing_mode);
+
+/* For unittesting purpose */
+int nghttp2_hd_emit_newname_block(nghttp2_bufs *bufs, nghttp2_nv *nv,
+                                  int indexing_mode);
+
+/* For unittesting purpose */
+int nghttp2_hd_emit_table_size(nghttp2_bufs *bufs, size_t table_size);
+
+/* For unittesting purpose */
+nghttp2_hd_nv nghttp2_hd_table_get(nghttp2_hd_context *context, size_t index);
+
+/* For unittesting purpose */
+ssize_t nghttp2_hd_decode_length(uint32_t *res, size_t *shift_ptr, int *fin,
+                                 uint32_t initial, size_t shift, uint8_t *in,
+                                 uint8_t *last, size_t prefix);
+
+/* Huffman encoding/decoding functions */
+
+/*
+ * Counts the required bytes to encode |src| with length |len|.
+ *
+ * This function returns the number of required bytes to encode given
+ * data, including padding of prefix of terminal symbol code. This
+ * function always succeeds.
+ */
+size_t nghttp2_hd_huff_encode_count(const uint8_t *src, size_t len);
+
+/*
+ * Encodes the given data |src| with length |srclen| to the |bufs|.
+ * This function expands extra buffers in |bufs| if necessary.
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * NGHTTP2_ERR_NOMEM
+ *     Out of memory.
+ * NGHTTP2_ERR_BUFFER_ERROR
+ *     Out of buffer space.
+ */
+int nghttp2_hd_huff_encode(nghttp2_bufs *bufs, const uint8_t *src,
+                           size_t srclen);
+
+void nghttp2_hd_huff_decode_context_init(nghttp2_hd_huff_decode_context *ctx);
+
+/*
+ * Decodes the given data |src| with length |srclen|.  The |ctx| must
+ * be initialized by nghttp2_hd_huff_decode_context_init(). The result
+ * will be written to |buf|.  This function assumes that |buf| has the
+ * enough room to store the decoded byte string.
+ *
+ * The caller must set the |fin| to nonzero if the given input is the
+ * final block.
+ *
+ * This function returns the number of read bytes from the |in|.
+ *
+ * If this function fails, it returns one of the following negative
+ * return codes:
+ *
+ * NGHTTP2_ERR_NOMEM
+ *     Out of memory.
+ * NGHTTP2_ERR_HEADER_COMP
+ *     Decoding process has failed.
+ */
+ssize_t nghttp2_hd_huff_decode(nghttp2_hd_huff_decode_context *ctx,
+                               nghttp2_buf *buf, const uint8_t *src,
+                               size_t srclen, int fin);
+
+#endif /* NGHTTP2_HD_H */

+ 212 - 0
iotkit-embedded/external_libs/nghttp2/nghttp2_hd_huffman.c

@@ -0,0 +1,212 @@
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
+ */
+
+
+#include "nghttp2_hd_huffman.h"
+
+#include <string.h>
+#include <assert.h>
+#include <stdio.h>
+
+#include "nghttp2_hd.h"
+
+/*
+ * Encodes huffman code |sym| into |*dest_ptr|, whose least |rembits|
+ * bits are not filled yet.  The |rembits| must be in range [1, 8],
+ * inclusive.  At the end of the process, the |*dest_ptr| is updated
+ * and points where next output should be placed. The number of
+ * unfilled bits in the pointed location is returned.
+ */
+static ssize_t huff_encode_sym(nghttp2_bufs *bufs, size_t *avail_ptr,
+                               size_t rembits, const nghttp2_huff_sym *sym) {
+  int rv;
+  size_t nbits = sym->nbits;
+  uint32_t code = sym->code;
+
+  /* We assume that sym->nbits <= 32 */
+  if (rembits > nbits) {
+    nghttp2_bufs_fast_orb_hold(bufs, (uint8_t)(code << (rembits - nbits)));
+    return (ssize_t)(rembits - nbits);
+  }
+
+  if (rembits == nbits) {
+    nghttp2_bufs_fast_orb(bufs, (uint8_t)code);
+    --*avail_ptr;
+    return 8;
+  }
+
+  nghttp2_bufs_fast_orb(bufs, (uint8_t)(code >> (nbits - rembits)));
+  --*avail_ptr;
+
+  nbits -= rembits;
+  if (nbits & 0x7) {
+    /* align code to MSB byte boundary */
+    code <<= 8 - (nbits & 0x7);
+  }
+
+  if (*avail_ptr < (nbits + 7) / 8) {
+    /* slow path */
+    if (nbits > 24) {
+      rv = nghttp2_bufs_addb(bufs, (uint8_t)(code >> 24));
+      if (rv != 0) {
+        return rv;
+      }
+      nbits -= 8;
+    }
+    if (nbits > 16) {
+      rv = nghttp2_bufs_addb(bufs, (uint8_t)(code >> 16));
+      if (rv != 0) {
+        return rv;
+      }
+      nbits -= 8;
+    }
+    if (nbits > 8) {
+      rv = nghttp2_bufs_addb(bufs, (uint8_t)(code >> 8));
+      if (rv != 0) {
+        return rv;
+      }
+      nbits -= 8;
+    }
+    if (nbits == 8) {
+      rv = nghttp2_bufs_addb(bufs, (uint8_t)code);
+      if (rv != 0) {
+        return rv;
+      }
+      *avail_ptr = nghttp2_bufs_cur_avail(bufs);
+      return 8;
+    }
+
+    rv = nghttp2_bufs_addb_hold(bufs, (uint8_t)code);
+    if (rv != 0) {
+      return rv;
+    }
+    *avail_ptr = nghttp2_bufs_cur_avail(bufs);
+    return (ssize_t)(8 - nbits);
+  }
+
+  /* fast path, since most code is less than 8 */
+  if (nbits < 8) {
+    nghttp2_bufs_fast_addb_hold(bufs, (uint8_t)code);
+    *avail_ptr = nghttp2_bufs_cur_avail(bufs);
+    return (ssize_t)(8 - nbits);
+  }
+
+  /* handle longer code path */
+  if (nbits > 24) {
+    nghttp2_bufs_fast_addb(bufs, (uint8_t)(code >> 24));
+    nbits -= 8;
+  }
+
+  if (nbits > 16) {
+    nghttp2_bufs_fast_addb(bufs, (uint8_t)(code >> 16));
+    nbits -= 8;
+  }
+
+  if (nbits > 8) {
+    nghttp2_bufs_fast_addb(bufs, (uint8_t)(code >> 8));
+    nbits -= 8;
+  }
+
+  if (nbits == 8) {
+    nghttp2_bufs_fast_addb(bufs, (uint8_t)code);
+    *avail_ptr = nghttp2_bufs_cur_avail(bufs);
+    return 8;
+  }
+
+  nghttp2_bufs_fast_addb_hold(bufs, (uint8_t)code);
+  *avail_ptr = nghttp2_bufs_cur_avail(bufs);
+  return (ssize_t)(8 - nbits);
+}
+
+size_t nghttp2_hd_huff_encode_count(const uint8_t *src, size_t len) {
+  size_t i;
+  size_t nbits = 0;
+
+  for (i = 0; i < len; ++i) {
+    nbits += huff_sym_table[src[i]].nbits;
+  }
+  /* pad the prefix of EOS (256) */
+  return (nbits + 7) / 8;
+}
+
+int nghttp2_hd_huff_encode(nghttp2_bufs *bufs, const uint8_t *src,
+                           size_t srclen) {
+  int rv;
+  ssize_t rembits = 8;
+  size_t i;
+  size_t avail;
+
+  avail = nghttp2_bufs_cur_avail(bufs);
+
+  for (i = 0; i < srclen; ++i) {
+    const nghttp2_huff_sym *sym = &huff_sym_table[src[i]];
+    if (rembits == 8) {
+      if (avail) {
+        nghttp2_bufs_fast_addb_hold(bufs, 0);
+      } else {
+        rv = nghttp2_bufs_addb_hold(bufs, 0);
+        if (rv != 0) {
+          return rv;
+        }
+        avail = nghttp2_bufs_cur_avail(bufs);
+      }
+    }
+    rembits = huff_encode_sym(bufs, &avail, (size_t)rembits, sym);
+    if (rembits < 0) {
+      return (int)rembits;
+    }
+  }
+  /* 256 is special terminal symbol, pad with its prefix */
+  if (rembits < 8) {
+    /* if rembits < 8, we should have at least 1 buffer space
+       available */
+    const nghttp2_huff_sym *sym = &huff_sym_table[256];
+    assert(avail);
+    /* Caution we no longer adjust avail here */
+    nghttp2_bufs_fast_orb(
+        bufs, (uint8_t)(sym->code >> (sym->nbits - (size_t)rembits)));
+  }
+
+  return 0;
+}
+
+void nghttp2_hd_huff_decode_context_init(nghttp2_hd_huff_decode_context *ctx) {
+  ctx->state = 0;
+  ctx->accept = 1;
+}
+
+ssize_t nghttp2_hd_huff_decode(nghttp2_hd_huff_decode_context *ctx,
+                               nghttp2_buf *buf, const uint8_t *src,
+                               size_t srclen, int final) {
+  size_t i;
+
+  /* We use the decoding algorithm described in
+     http://graphics.ics.uci.edu/pub/Prefix.pdf */
+  for (i = 0; i < srclen; ++i) {
+    const nghttp2_huff_decode *t;
+
+    t = &huff_decode_table[ctx->state][src[i] >> 4];
+    if (t->flags & NGHTTP2_HUFF_FAIL) {
+      return NGHTTP2_ERR_HEADER_COMP;
+    }
+    if (t->flags & NGHTTP2_HUFF_SYM) {
+      *buf->last++ = t->sym;
+    }
+
+    t = &huff_decode_table[t->state][src[i] & 0xf];
+    if (t->flags & NGHTTP2_HUFF_FAIL) {
+      return NGHTTP2_ERR_HEADER_COMP;
+    }
+    if (t->flags & NGHTTP2_HUFF_SYM) {
+      *buf->last++ = t->sym;
+    }
+
+    ctx->state = t->state;
+    ctx->accept = (t->flags & NGHTTP2_HUFF_ACCEPTED) != 0;
+  }
+  if (final && !ctx->accept) {
+    return NGHTTP2_ERR_HEADER_COMP;
+  }
+  return (ssize_t)i;
+}

+ 58 - 0
iotkit-embedded/external_libs/nghttp2/nghttp2_hd_huffman.h

@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
+ */
+
+
+#ifndef NGHTTP2_HD_HUFFMAN_H
+#define NGHTTP2_HD_HUFFMAN_H
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif /* HAVE_CONFIG_H */
+
+#include "nghttp2.h"
+
+typedef enum {
+  /* FSA accepts this state as the end of huffman encoding
+     sequence. */
+  NGHTTP2_HUFF_ACCEPTED = 1,
+  /* This state emits symbol */
+  NGHTTP2_HUFF_SYM = (1 << 1),
+  /* If state machine reaches this state, decoding fails. */
+  NGHTTP2_HUFF_FAIL = (1 << 2)
+} nghttp2_huff_decode_flag;
+
+typedef struct {
+  /* huffman decoding state, which is actually the node ID of internal
+     huffman tree.  We have 257 leaf nodes, but they are identical to
+     root node other than emitting a symbol, so we have 256 internal
+     nodes [1..255], inclusive. */
+  uint8_t state;
+  /* bitwise OR of zero or more of the nghttp2_huff_decode_flag */
+  uint8_t flags;
+  /* symbol if NGHTTP2_HUFF_SYM flag set */
+  uint8_t sym;
+} nghttp2_huff_decode;
+
+typedef nghttp2_huff_decode huff_decode_table_type[16];
+
+typedef struct {
+  /* Current huffman decoding state. We stripped leaf nodes, so the
+     value range is [0..255], inclusive. */
+  uint8_t state;
+  /* nonzero if we can say that the decoding process succeeds at this
+     state */
+  uint8_t accept;
+} nghttp2_hd_huff_decode_context;
+
+typedef struct {
+  /* The number of bits in this code */
+  uint32_t nbits;
+  /* Huffman code aligned to LSB */
+  uint32_t code;
+} nghttp2_huff_sym;
+
+extern const nghttp2_huff_sym huff_sym_table[];
+extern const nghttp2_huff_decode huff_decode_table[][16];
+
+#endif /* NGHTTP2_HD_HUFFMAN_H */

+ 4942 - 0
iotkit-embedded/external_libs/nghttp2/nghttp2_hd_huffman_data.c

@@ -0,0 +1,4942 @@
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
+ */
+
+
+#include "nghttp2_hd_huffman.h"
+
+/* Generated by mkhufftbl.py */
+
+const nghttp2_huff_sym huff_sym_table[] = {
+    {13, 0x1ff8u},    {23, 0x7fffd8u},   {28, 0xfffffe2u},  {28, 0xfffffe3u},
+    {28, 0xfffffe4u}, {28, 0xfffffe5u},  {28, 0xfffffe6u},  {28, 0xfffffe7u},
+    {28, 0xfffffe8u}, {24, 0xffffeau},   {30, 0x3ffffffcu}, {28, 0xfffffe9u},
+    {28, 0xfffffeau}, {30, 0x3ffffffdu}, {28, 0xfffffebu},  {28, 0xfffffecu},
+    {28, 0xfffffedu}, {28, 0xfffffeeu},  {28, 0xfffffefu},  {28, 0xffffff0u},
+    {28, 0xffffff1u}, {28, 0xffffff2u},  {30, 0x3ffffffeu}, {28, 0xffffff3u},
+    {28, 0xffffff4u}, {28, 0xffffff5u},  {28, 0xffffff6u},  {28, 0xffffff7u},
+    {28, 0xffffff8u}, {28, 0xffffff9u},  {28, 0xffffffau},  {28, 0xffffffbu},
+    {6, 0x14u},       {10, 0x3f8u},      {10, 0x3f9u},      {12, 0xffau},
+    {13, 0x1ff9u},    {6, 0x15u},        {8, 0xf8u},        {11, 0x7fau},
+    {10, 0x3fau},     {10, 0x3fbu},      {8, 0xf9u},        {11, 0x7fbu},
+    {8, 0xfau},       {6, 0x16u},        {6, 0x17u},        {6, 0x18u},
+    {5, 0x0u},        {5, 0x1u},         {5, 0x2u},         {6, 0x19u},
+    {6, 0x1au},       {6, 0x1bu},        {6, 0x1cu},        {6, 0x1du},
+    {6, 0x1eu},       {6, 0x1fu},        {7, 0x5cu},        {8, 0xfbu},
+    {15, 0x7ffcu},    {6, 0x20u},        {12, 0xffbu},      {10, 0x3fcu},
+    {13, 0x1ffau},    {6, 0x21u},        {7, 0x5du},        {7, 0x5eu},
+    {7, 0x5fu},       {7, 0x60u},        {7, 0x61u},        {7, 0x62u},
+    {7, 0x63u},       {7, 0x64u},        {7, 0x65u},        {7, 0x66u},
+    {7, 0x67u},       {7, 0x68u},        {7, 0x69u},        {7, 0x6au},
+    {7, 0x6bu},       {7, 0x6cu},        {7, 0x6du},        {7, 0x6eu},
+    {7, 0x6fu},       {7, 0x70u},        {7, 0x71u},        {7, 0x72u},
+    {8, 0xfcu},       {7, 0x73u},        {8, 0xfdu},        {13, 0x1ffbu},
+    {19, 0x7fff0u},   {13, 0x1ffcu},     {14, 0x3ffcu},     {6, 0x22u},
+    {15, 0x7ffdu},    {5, 0x3u},         {6, 0x23u},        {5, 0x4u},
+    {6, 0x24u},       {5, 0x5u},         {6, 0x25u},        {6, 0x26u},
+    {6, 0x27u},       {5, 0x6u},         {7, 0x74u},        {7, 0x75u},
+    {6, 0x28u},       {6, 0x29u},        {6, 0x2au},        {5, 0x7u},
+    {6, 0x2bu},       {7, 0x76u},        {6, 0x2cu},        {5, 0x8u},
+    {5, 0x9u},        {6, 0x2du},        {7, 0x77u},        {7, 0x78u},
+    {7, 0x79u},       {7, 0x7au},        {7, 0x7bu},        {15, 0x7ffeu},
+    {11, 0x7fcu},     {14, 0x3ffdu},     {13, 0x1ffdu},     {28, 0xffffffcu},
+    {20, 0xfffe6u},   {22, 0x3fffd2u},   {20, 0xfffe7u},    {20, 0xfffe8u},
+    {22, 0x3fffd3u},  {22, 0x3fffd4u},   {22, 0x3fffd5u},   {23, 0x7fffd9u},
+    {22, 0x3fffd6u},  {23, 0x7fffdau},   {23, 0x7fffdbu},   {23, 0x7fffdcu},
+    {23, 0x7fffddu},  {23, 0x7fffdeu},   {24, 0xffffebu},   {23, 0x7fffdfu},
+    {24, 0xffffecu},  {24, 0xffffedu},   {22, 0x3fffd7u},   {23, 0x7fffe0u},
+    {24, 0xffffeeu},  {23, 0x7fffe1u},   {23, 0x7fffe2u},   {23, 0x7fffe3u},
+    {23, 0x7fffe4u},  {21, 0x1fffdcu},   {22, 0x3fffd8u},   {23, 0x7fffe5u},
+    {22, 0x3fffd9u},  {23, 0x7fffe6u},   {23, 0x7fffe7u},   {24, 0xffffefu},
+    {22, 0x3fffdau},  {21, 0x1fffddu},   {20, 0xfffe9u},    {22, 0x3fffdbu},
+    {22, 0x3fffdcu},  {23, 0x7fffe8u},   {23, 0x7fffe9u},   {21, 0x1fffdeu},
+    {23, 0x7fffeau},  {22, 0x3fffddu},   {22, 0x3fffdeu},   {24, 0xfffff0u},
+    {21, 0x1fffdfu},  {22, 0x3fffdfu},   {23, 0x7fffebu},   {23, 0x7fffecu},
+    {21, 0x1fffe0u},  {21, 0x1fffe1u},   {22, 0x3fffe0u},   {21, 0x1fffe2u},
+    {23, 0x7fffedu},  {22, 0x3fffe1u},   {23, 0x7fffeeu},   {23, 0x7fffefu},
+    {20, 0xfffeau},   {22, 0x3fffe2u},   {22, 0x3fffe3u},   {22, 0x3fffe4u},
+    {23, 0x7ffff0u},  {22, 0x3fffe5u},   {22, 0x3fffe6u},   {23, 0x7ffff1u},
+    {26, 0x3ffffe0u}, {26, 0x3ffffe1u},  {20, 0xfffebu},    {19, 0x7fff1u},
+    {22, 0x3fffe7u},  {23, 0x7ffff2u},   {22, 0x3fffe8u},   {25, 0x1ffffecu},
+    {26, 0x3ffffe2u}, {26, 0x3ffffe3u},  {26, 0x3ffffe4u},  {27, 0x7ffffdeu},
+    {27, 0x7ffffdfu}, {26, 0x3ffffe5u},  {24, 0xfffff1u},   {25, 0x1ffffedu},
+    {19, 0x7fff2u},   {21, 0x1fffe3u},   {26, 0x3ffffe6u},  {27, 0x7ffffe0u},
+    {27, 0x7ffffe1u}, {26, 0x3ffffe7u},  {27, 0x7ffffe2u},  {24, 0xfffff2u},
+    {21, 0x1fffe4u},  {21, 0x1fffe5u},   {26, 0x3ffffe8u},  {26, 0x3ffffe9u},
+    {28, 0xffffffdu}, {27, 0x7ffffe3u},  {27, 0x7ffffe4u},  {27, 0x7ffffe5u},
+    {20, 0xfffecu},   {24, 0xfffff3u},   {20, 0xfffedu},    {21, 0x1fffe6u},
+    {22, 0x3fffe9u},  {21, 0x1fffe7u},   {21, 0x1fffe8u},   {23, 0x7ffff3u},
+    {22, 0x3fffeau},  {22, 0x3fffebu},   {25, 0x1ffffeeu},  {25, 0x1ffffefu},
+    {24, 0xfffff4u},  {24, 0xfffff5u},   {26, 0x3ffffeau},  {23, 0x7ffff4u},
+    {26, 0x3ffffebu}, {27, 0x7ffffe6u},  {26, 0x3ffffecu},  {26, 0x3ffffedu},
+    {27, 0x7ffffe7u}, {27, 0x7ffffe8u},  {27, 0x7ffffe9u},  {27, 0x7ffffeau},
+    {27, 0x7ffffebu}, {28, 0xffffffeu},  {27, 0x7ffffecu},  {27, 0x7ffffedu},
+    {27, 0x7ffffeeu}, {27, 0x7ffffefu},  {27, 0x7fffff0u},  {26, 0x3ffffeeu},
+    {30, 0x3fffffffu}};
+
+const nghttp2_huff_decode huff_decode_table[][16] = {
+    /* 0 */
+    {
+        {4, 0x00, 0},
+        {5, 0x00, 0},
+        {7, 0x00, 0},
+        {8, 0x00, 0},
+        {11, 0x00, 0},
+        {12, 0x00, 0},
+        {16, 0x00, 0},
+        {19, 0x00, 0},
+        {25, 0x00, 0},
+        {28, 0x00, 0},
+        {32, 0x00, 0},
+        {35, 0x00, 0},
+        {42, 0x00, 0},
+        {49, 0x00, 0},
+        {57, 0x00, 0},
+        {64, 0x01, 0},
+    },
+    /* 1 */
+    {
+        {0, 0x03, 48},
+        {0, 0x03, 49},
+        {0, 0x03, 50},
+        {0, 0x03, 97},
+        {0, 0x03, 99},
+        {0, 0x03, 101},
+        {0, 0x03, 105},
+        {0, 0x03, 111},
+        {0, 0x03, 115},
+        {0, 0x03, 116},
+        {13, 0x00, 0},
+        {14, 0x00, 0},
+        {17, 0x00, 0},
+        {18, 0x00, 0},
+        {20, 0x00, 0},
+        {21, 0x00, 0},
+    },
+    /* 2 */
+    {
+        {1, 0x02, 48},
+        {22, 0x03, 48},
+        {1, 0x02, 49},
+        {22, 0x03, 49},
+        {1, 0x02, 50},
+        {22, 0x03, 50},
+        {1, 0x02, 97},
+        {22, 0x03, 97},
+        {1, 0x02, 99},
+        {22, 0x03, 99},
+        {1, 0x02, 101},
+        {22, 0x03, 101},
+        {1, 0x02, 105},
+        {22, 0x03, 105},
+        {1, 0x02, 111},
+        {22, 0x03, 111},
+    },
+    /* 3 */
+    {
+        {2, 0x02, 48},
+        {9, 0x02, 48},
+        {23, 0x02, 48},
+        {40, 0x03, 48},
+        {2, 0x02, 49},
+        {9, 0x02, 49},
+        {23, 0x02, 49},
+        {40, 0x03, 49},
+        {2, 0x02, 50},
+        {9, 0x02, 50},
+        {23, 0x02, 50},
+        {40, 0x03, 50},
+        {2, 0x02, 97},
+        {9, 0x02, 97},
+        {23, 0x02, 97},
+        {40, 0x03, 97},
+    },
+    /* 4 */
+    {
+        {3, 0x02, 48},
+        {6, 0x02, 48},
+        {10, 0x02, 48},
+        {15, 0x02, 48},
+        {24, 0x02, 48},
+        {31, 0x02, 48},
+        {41, 0x02, 48},
+        {56, 0x03, 48},
+        {3, 0x02, 49},
+        {6, 0x02, 49},
+        {10, 0x02, 49},
+        {15, 0x02, 49},
+        {24, 0x02, 49},
+        {31, 0x02, 49},
+        {41, 0x02, 49},
+        {56, 0x03, 49},
+    },
+    /* 5 */
+    {
+        {3, 0x02, 50},
+        {6, 0x02, 50},
+        {10, 0x02, 50},
+        {15, 0x02, 50},
+        {24, 0x02, 50},
+        {31, 0x02, 50},
+        {41, 0x02, 50},
+        {56, 0x03, 50},
+        {3, 0x02, 97},
+        {6, 0x02, 97},
+        {10, 0x02, 97},
+        {15, 0x02, 97},
+        {24, 0x02, 97},
+        {31, 0x02, 97},
+        {41, 0x02, 97},
+        {56, 0x03, 97},
+    },
+    /* 6 */
+    {
+        {2, 0x02, 99},
+        {9, 0x02, 99},
+        {23, 0x02, 99},
+        {40, 0x03, 99},
+        {2, 0x02, 101},
+        {9, 0x02, 101},
+        {23, 0x02, 101},
+        {40, 0x03, 101},
+        {2, 0x02, 105},
+        {9, 0x02, 105},
+        {23, 0x02, 105},
+        {40, 0x03, 105},
+        {2, 0x02, 111},
+        {9, 0x02, 111},
+        {23, 0x02, 111},
+        {40, 0x03, 111},
+    },
+    /* 7 */
+    {
+        {3, 0x02, 99},
+        {6, 0x02, 99},
+        {10, 0x02, 99},
+        {15, 0x02, 99},
+        {24, 0x02, 99},
+        {31, 0x02, 99},
+        {41, 0x02, 99},
+        {56, 0x03, 99},
+        {3, 0x02, 101},
+        {6, 0x02, 101},
+        {10, 0x02, 101},
+        {15, 0x02, 101},
+        {24, 0x02, 101},
+        {31, 0x02, 101},
+        {41, 0x02, 101},
+        {56, 0x03, 101},
+    },
+    /* 8 */
+    {
+        {3, 0x02, 105},
+        {6, 0x02, 105},
+        {10, 0x02, 105},
+        {15, 0x02, 105},
+        {24, 0x02, 105},
+        {31, 0x02, 105},
+        {41, 0x02, 105},
+        {56, 0x03, 105},
+        {3, 0x02, 111},
+        {6, 0x02, 111},
+        {10, 0x02, 111},
+        {15, 0x02, 111},
+        {24, 0x02, 111},
+        {31, 0x02, 111},
+        {41, 0x02, 111},
+        {56, 0x03, 111},
+    },
+    /* 9 */
+    {
+        {1, 0x02, 115},
+        {22, 0x03, 115},
+        {1, 0x02, 116},
+        {22, 0x03, 116},
+        {0, 0x03, 32},
+        {0, 0x03, 37},
+        {0, 0x03, 45},
+        {0, 0x03, 46},
+        {0, 0x03, 47},
+        {0, 0x03, 51},
+        {0, 0x03, 52},
+        {0, 0x03, 53},
+        {0, 0x03, 54},
+        {0, 0x03, 55},
+        {0, 0x03, 56},
+        {0, 0x03, 57},
+    },
+    /* 10 */
+    {
+        {2, 0x02, 115},
+        {9, 0x02, 115},
+        {23, 0x02, 115},
+        {40, 0x03, 115},
+        {2, 0x02, 116},
+        {9, 0x02, 116},
+        {23, 0x02, 116},
+        {40, 0x03, 116},
+        {1, 0x02, 32},
+        {22, 0x03, 32},
+        {1, 0x02, 37},
+        {22, 0x03, 37},
+        {1, 0x02, 45},
+        {22, 0x03, 45},
+        {1, 0x02, 46},
+        {22, 0x03, 46},
+    },
+    /* 11 */
+    {
+        {3, 0x02, 115},
+        {6, 0x02, 115},
+        {10, 0x02, 115},
+        {15, 0x02, 115},
+        {24, 0x02, 115},
+        {31, 0x02, 115},
+        {41, 0x02, 115},
+        {56, 0x03, 115},
+        {3, 0x02, 116},
+        {6, 0x02, 116},
+        {10, 0x02, 116},
+        {15, 0x02, 116},
+        {24, 0x02, 116},
+        {31, 0x02, 116},
+        {41, 0x02, 116},
+        {56, 0x03, 116},
+    },
+    /* 12 */
+    {
+        {2, 0x02, 32},
+        {9, 0x02, 32},
+        {23, 0x02, 32},
+        {40, 0x03, 32},
+        {2, 0x02, 37},
+        {9, 0x02, 37},
+        {23, 0x02, 37},
+        {40, 0x03, 37},
+        {2, 0x02, 45},
+        {9, 0x02, 45},
+        {23, 0x02, 45},
+        {40, 0x03, 45},
+        {2, 0x02, 46},
+        {9, 0x02, 46},
+        {23, 0x02, 46},
+        {40, 0x03, 46},
+    },
+    /* 13 */
+    {
+        {3, 0x02, 32},
+        {6, 0x02, 32},
+        {10, 0x02, 32},
+        {15, 0x02, 32},
+        {24, 0x02, 32},
+        {31, 0x02, 32},
+        {41, 0x02, 32},
+        {56, 0x03, 32},
+        {3, 0x02, 37},
+        {6, 0x02, 37},
+        {10, 0x02, 37},
+        {15, 0x02, 37},
+        {24, 0x02, 37},
+        {31, 0x02, 37},
+        {41, 0x02, 37},
+        {56, 0x03, 37},
+    },
+    /* 14 */
+    {
+        {3, 0x02, 45},
+        {6, 0x02, 45},
+        {10, 0x02, 45},
+        {15, 0x02, 45},
+        {24, 0x02, 45},
+        {31, 0x02, 45},
+        {41, 0x02, 45},
+        {56, 0x03, 45},
+        {3, 0x02, 46},
+        {6, 0x02, 46},
+        {10, 0x02, 46},
+        {15, 0x02, 46},
+        {24, 0x02, 46},
+        {31, 0x02, 46},
+        {41, 0x02, 46},
+        {56, 0x03, 46},
+    },
+    /* 15 */
+    {
+        {1, 0x02, 47},
+        {22, 0x03, 47},
+        {1, 0x02, 51},
+        {22, 0x03, 51},
+        {1, 0x02, 52},
+        {22, 0x03, 52},
+        {1, 0x02, 53},
+        {22, 0x03, 53},
+        {1, 0x02, 54},
+        {22, 0x03, 54},
+        {1, 0x02, 55},
+        {22, 0x03, 55},
+        {1, 0x02, 56},
+        {22, 0x03, 56},
+        {1, 0x02, 57},
+        {22, 0x03, 57},
+    },
+    /* 16 */
+    {
+        {2, 0x02, 47},
+        {9, 0x02, 47},
+        {23, 0x02, 47},
+        {40, 0x03, 47},
+        {2, 0x02, 51},
+        {9, 0x02, 51},
+        {23, 0x02, 51},
+        {40, 0x03, 51},
+        {2, 0x02, 52},
+        {9, 0x02, 52},
+        {23, 0x02, 52},
+        {40, 0x03, 52},
+        {2, 0x02, 53},
+        {9, 0x02, 53},
+        {23, 0x02, 53},
+        {40, 0x03, 53},
+    },
+    /* 17 */
+    {
+        {3, 0x02, 47},
+        {6, 0x02, 47},
+        {10, 0x02, 47},
+        {15, 0x02, 47},
+        {24, 0x02, 47},
+        {31, 0x02, 47},
+        {41, 0x02, 47},
+        {56, 0x03, 47},
+        {3, 0x02, 51},
+        {6, 0x02, 51},
+        {10, 0x02, 51},
+        {15, 0x02, 51},
+        {24, 0x02, 51},
+        {31, 0x02, 51},
+        {41, 0x02, 51},
+        {56, 0x03, 51},
+    },
+    /* 18 */
+    {
+        {3, 0x02, 52},
+        {6, 0x02, 52},
+        {10, 0x02, 52},
+        {15, 0x02, 52},
+        {24, 0x02, 52},
+        {31, 0x02, 52},
+        {41, 0x02, 52},
+        {56, 0x03, 52},
+        {3, 0x02, 53},
+        {6, 0x02, 53},
+        {10, 0x02, 53},
+        {15, 0x02, 53},
+        {24, 0x02, 53},
+        {31, 0x02, 53},
+        {41, 0x02, 53},
+        {56, 0x03, 53},
+    },
+    /* 19 */
+    {
+        {2, 0x02, 54},
+        {9, 0x02, 54},
+        {23, 0x02, 54},
+        {40, 0x03, 54},
+        {2, 0x02, 55},
+        {9, 0x02, 55},
+        {23, 0x02, 55},
+        {40, 0x03, 55},
+        {2, 0x02, 56},
+        {9, 0x02, 56},
+        {23, 0x02, 56},
+        {40, 0x03, 56},
+        {2, 0x02, 57},
+        {9, 0x02, 57},
+        {23, 0x02, 57},
+        {40, 0x03, 57},
+    },
+    /* 20 */
+    {
+        {3, 0x02, 54},
+        {6, 0x02, 54},
+        {10, 0x02, 54},
+        {15, 0x02, 54},
+        {24, 0x02, 54},
+        {31, 0x02, 54},
+        {41, 0x02, 54},
+        {56, 0x03, 54},
+        {3, 0x02, 55},
+        {6, 0x02, 55},
+        {10, 0x02, 55},
+        {15, 0x02, 55},
+        {24, 0x02, 55},
+        {31, 0x02, 55},
+        {41, 0x02, 55},
+        {56, 0x03, 55},
+    },
+    /* 21 */
+    {
+        {3, 0x02, 56},
+        {6, 0x02, 56},
+        {10, 0x02, 56},
+        {15, 0x02, 56},
+        {24, 0x02, 56},
+        {31, 0x02, 56},
+        {41, 0x02, 56},
+        {56, 0x03, 56},
+        {3, 0x02, 57},
+        {6, 0x02, 57},
+        {10, 0x02, 57},
+        {15, 0x02, 57},
+        {24, 0x02, 57},
+        {31, 0x02, 57},
+        {41, 0x02, 57},
+        {56, 0x03, 57},
+    },
+    /* 22 */
+    {
+        {26, 0x00, 0},
+        {27, 0x00, 0},
+        {29, 0x00, 0},
+        {30, 0x00, 0},
+        {33, 0x00, 0},
+        {34, 0x00, 0},
+        {36, 0x00, 0},
+        {37, 0x00, 0},
+        {43, 0x00, 0},
+        {46, 0x00, 0},
+        {50, 0x00, 0},
+        {53, 0x00, 0},
+        {58, 0x00, 0},
+        {61, 0x00, 0},
+        {65, 0x00, 0},
+        {68, 0x01, 0},
+    },
+    /* 23 */
+    {
+        {0, 0x03, 61},
+        {0, 0x03, 65},
+        {0, 0x03, 95},
+        {0, 0x03, 98},
+        {0, 0x03, 100},
+        {0, 0x03, 102},
+        {0, 0x03, 103},
+        {0, 0x03, 104},
+        {0, 0x03, 108},
+        {0, 0x03, 109},
+        {0, 0x03, 110},
+        {0, 0x03, 112},
+        {0, 0x03, 114},
+        {0, 0x03, 117},
+        {38, 0x00, 0},
+        {39, 0x00, 0},
+    },
+    /* 24 */
+    {
+        {1, 0x02, 61},
+        {22, 0x03, 61},
+        {1, 0x02, 65},
+        {22, 0x03, 65},
+        {1, 0x02, 95},
+        {22, 0x03, 95},
+        {1, 0x02, 98},
+        {22, 0x03, 98},
+        {1, 0x02, 100},
+        {22, 0x03, 100},
+        {1, 0x02, 102},
+        {22, 0x03, 102},
+        {1, 0x02, 103},
+        {22, 0x03, 103},
+        {1, 0x02, 104},
+        {22, 0x03, 104},
+    },
+    /* 25 */
+    {
+        {2, 0x02, 61},
+        {9, 0x02, 61},
+        {23, 0x02, 61},
+        {40, 0x03, 61},
+        {2, 0x02, 65},
+        {9, 0x02, 65},
+        {23, 0x02, 65},
+        {40, 0x03, 65},
+        {2, 0x02, 95},
+        {9, 0x02, 95},
+        {23, 0x02, 95},
+        {40, 0x03, 95},
+        {2, 0x02, 98},
+        {9, 0x02, 98},
+        {23, 0x02, 98},
+        {40, 0x03, 98},
+    },
+    /* 26 */
+    {
+        {3, 0x02, 61},
+        {6, 0x02, 61},
+        {10, 0x02, 61},
+        {15, 0x02, 61},
+        {24, 0x02, 61},
+        {31, 0x02, 61},
+        {41, 0x02, 61},
+        {56, 0x03, 61},
+        {3, 0x02, 65},
+        {6, 0x02, 65},
+        {10, 0x02, 65},
+        {15, 0x02, 65},
+        {24, 0x02, 65},
+        {31, 0x02, 65},
+        {41, 0x02, 65},
+        {56, 0x03, 65},
+    },
+    /* 27 */
+    {
+        {3, 0x02, 95},
+        {6, 0x02, 95},
+        {10, 0x02, 95},
+        {15, 0x02, 95},
+        {24, 0x02, 95},
+        {31, 0x02, 95},
+        {41, 0x02, 95},
+        {56, 0x03, 95},
+        {3, 0x02, 98},
+        {6, 0x02, 98},
+        {10, 0x02, 98},
+        {15, 0x02, 98},
+        {24, 0x02, 98},
+        {31, 0x02, 98},
+        {41, 0x02, 98},
+        {56, 0x03, 98},
+    },
+    /* 28 */
+    {
+        {2, 0x02, 100},
+        {9, 0x02, 100},
+        {23, 0x02, 100},
+        {40, 0x03, 100},
+        {2, 0x02, 102},
+        {9, 0x02, 102},
+        {23, 0x02, 102},
+        {40, 0x03, 102},
+        {2, 0x02, 103},
+        {9, 0x02, 103},
+        {23, 0x02, 103},
+        {40, 0x03, 103},
+        {2, 0x02, 104},
+        {9, 0x02, 104},
+        {23, 0x02, 104},
+        {40, 0x03, 104},
+    },
+    /* 29 */
+    {
+        {3, 0x02, 100},
+        {6, 0x02, 100},
+        {10, 0x02, 100},
+        {15, 0x02, 100},
+        {24, 0x02, 100},
+        {31, 0x02, 100},
+        {41, 0x02, 100},
+        {56, 0x03, 100},
+        {3, 0x02, 102},
+        {6, 0x02, 102},
+        {10, 0x02, 102},
+        {15, 0x02, 102},
+        {24, 0x02, 102},
+        {31, 0x02, 102},
+        {41, 0x02, 102},
+        {56, 0x03, 102},
+    },
+    /* 30 */
+    {
+        {3, 0x02, 103},
+        {6, 0x02, 103},
+        {10, 0x02, 103},
+        {15, 0x02, 103},
+        {24, 0x02, 103},
+        {31, 0x02, 103},
+        {41, 0x02, 103},
+        {56, 0x03, 103},
+        {3, 0x02, 104},
+        {6, 0x02, 104},
+        {10, 0x02, 104},
+        {15, 0x02, 104},
+        {24, 0x02, 104},
+        {31, 0x02, 104},
+        {41, 0x02, 104},
+        {56, 0x03, 104},
+    },
+    /* 31 */
+    {
+        {1, 0x02, 108},
+        {22, 0x03, 108},
+        {1, 0x02, 109},
+        {22, 0x03, 109},
+        {1, 0x02, 110},
+        {22, 0x03, 110},
+        {1, 0x02, 112},
+        {22, 0x03, 112},
+        {1, 0x02, 114},
+        {22, 0x03, 114},
+        {1, 0x02, 117},
+        {22, 0x03, 117},
+        {0, 0x03, 58},
+        {0, 0x03, 66},
+        {0, 0x03, 67},
+        {0, 0x03, 68},
+    },
+    /* 32 */
+    {
+        {2, 0x02, 108},
+        {9, 0x02, 108},
+        {23, 0x02, 108},
+        {40, 0x03, 108},
+        {2, 0x02, 109},
+        {9, 0x02, 109},
+        {23, 0x02, 109},
+        {40, 0x03, 109},
+        {2, 0x02, 110},
+        {9, 0x02, 110},
+        {23, 0x02, 110},
+        {40, 0x03, 110},
+        {2, 0x02, 112},
+        {9, 0x02, 112},
+        {23, 0x02, 112},
+        {40, 0x03, 112},
+    },
+    /* 33 */
+    {
+        {3, 0x02, 108},
+        {6, 0x02, 108},
+        {10, 0x02, 108},
+        {15, 0x02, 108},
+        {24, 0x02, 108},
+        {31, 0x02, 108},
+        {41, 0x02, 108},
+        {56, 0x03, 108},
+        {3, 0x02, 109},
+        {6, 0x02, 109},
+        {10, 0x02, 109},
+        {15, 0x02, 109},
+        {24, 0x02, 109},
+        {31, 0x02, 109},
+        {41, 0x02, 109},
+        {56, 0x03, 109},
+    },
+    /* 34 */
+    {
+        {3, 0x02, 110},
+        {6, 0x02, 110},
+        {10, 0x02, 110},
+        {15, 0x02, 110},
+        {24, 0x02, 110},
+        {31, 0x02, 110},
+        {41, 0x02, 110},
+        {56, 0x03, 110},
+        {3, 0x02, 112},
+        {6, 0x02, 112},
+        {10, 0x02, 112},
+        {15, 0x02, 112},
+        {24, 0x02, 112},
+        {31, 0x02, 112},
+        {41, 0x02, 112},
+        {56, 0x03, 112},
+    },
+    /* 35 */
+    {
+        {2, 0x02, 114},
+        {9, 0x02, 114},
+        {23, 0x02, 114},
+        {40, 0x03, 114},
+        {2, 0x02, 117},
+        {9, 0x02, 117},
+        {23, 0x02, 117},
+        {40, 0x03, 117},
+        {1, 0x02, 58},
+        {22, 0x03, 58},
+        {1, 0x02, 66},
+        {22, 0x03, 66},
+        {1, 0x02, 67},
+        {22, 0x03, 67},
+        {1, 0x02, 68},
+        {22, 0x03, 68},
+    },
+    /* 36 */
+    {
+        {3, 0x02, 114},
+        {6, 0x02, 114},
+        {10, 0x02, 114},
+        {15, 0x02, 114},
+        {24, 0x02, 114},
+        {31, 0x02, 114},
+        {41, 0x02, 114},
+        {56, 0x03, 114},
+        {3, 0x02, 117},
+        {6, 0x02, 117},
+        {10, 0x02, 117},
+        {15, 0x02, 117},
+        {24, 0x02, 117},
+        {31, 0x02, 117},
+        {41, 0x02, 117},
+        {56, 0x03, 117},
+    },
+    /* 37 */
+    {
+        {2, 0x02, 58},
+        {9, 0x02, 58},
+        {23, 0x02, 58},
+        {40, 0x03, 58},
+        {2, 0x02, 66},
+        {9, 0x02, 66},
+        {23, 0x02, 66},
+        {40, 0x03, 66},
+        {2, 0x02, 67},
+        {9, 0x02, 67},
+        {23, 0x02, 67},
+        {40, 0x03, 67},
+        {2, 0x02, 68},
+        {9, 0x02, 68},
+        {23, 0x02, 68},
+        {40, 0x03, 68},
+    },
+    /* 38 */
+    {
+        {3, 0x02, 58},
+        {6, 0x02, 58},
+        {10, 0x02, 58},
+        {15, 0x02, 58},
+        {24, 0x02, 58},
+        {31, 0x02, 58},
+        {41, 0x02, 58},
+        {56, 0x03, 58},
+        {3, 0x02, 66},
+        {6, 0x02, 66},
+        {10, 0x02, 66},
+        {15, 0x02, 66},
+        {24, 0x02, 66},
+        {31, 0x02, 66},
+        {41, 0x02, 66},
+        {56, 0x03, 66},
+    },
+    /* 39 */
+    {
+        {3, 0x02, 67},
+        {6, 0x02, 67},
+        {10, 0x02, 67},
+        {15, 0x02, 67},
+        {24, 0x02, 67},
+        {31, 0x02, 67},
+        {41, 0x02, 67},
+        {56, 0x03, 67},
+        {3, 0x02, 68},
+        {6, 0x02, 68},
+        {10, 0x02, 68},
+        {15, 0x02, 68},
+        {24, 0x02, 68},
+        {31, 0x02, 68},
+        {41, 0x02, 68},
+        {56, 0x03, 68},
+    },
+    /* 40 */
+    {
+        {44, 0x00, 0},
+        {45, 0x00, 0},
+        {47, 0x00, 0},
+        {48, 0x00, 0},
+        {51, 0x00, 0},
+        {52, 0x00, 0},
+        {54, 0x00, 0},
+        {55, 0x00, 0},
+        {59, 0x00, 0},
+        {60, 0x00, 0},
+        {62, 0x00, 0},
+        {63, 0x00, 0},
+        {66, 0x00, 0},
+        {67, 0x00, 0},
+        {69, 0x00, 0},
+        {72, 0x01, 0},
+    },
+    /* 41 */
+    {
+        {0, 0x03, 69},
+        {0, 0x03, 70},
+        {0, 0x03, 71},
+        {0, 0x03, 72},
+        {0, 0x03, 73},
+        {0, 0x03, 74},
+        {0, 0x03, 75},
+        {0, 0x03, 76},
+        {0, 0x03, 77},
+        {0, 0x03, 78},
+        {0, 0x03, 79},
+        {0, 0x03, 80},
+        {0, 0x03, 81},
+        {0, 0x03, 82},
+        {0, 0x03, 83},
+        {0, 0x03, 84},
+    },
+    /* 42 */
+    {
+        {1, 0x02, 69},
+        {22, 0x03, 69},
+        {1, 0x02, 70},
+        {22, 0x03, 70},
+        {1, 0x02, 71},
+        {22, 0x03, 71},
+        {1, 0x02, 72},
+        {22, 0x03, 72},
+        {1, 0x02, 73},
+        {22, 0x03, 73},
+        {1, 0x02, 74},
+        {22, 0x03, 74},
+        {1, 0x02, 75},
+        {22, 0x03, 75},
+        {1, 0x02, 76},
+        {22, 0x03, 76},
+    },
+    /* 43 */
+    {
+        {2, 0x02, 69},
+        {9, 0x02, 69},
+        {23, 0x02, 69},
+        {40, 0x03, 69},
+        {2, 0x02, 70},
+        {9, 0x02, 70},
+        {23, 0x02, 70},
+        {40, 0x03, 70},
+        {2, 0x02, 71},
+        {9, 0x02, 71},
+        {23, 0x02, 71},
+        {40, 0x03, 71},
+        {2, 0x02, 72},
+        {9, 0x02, 72},
+        {23, 0x02, 72},
+        {40, 0x03, 72},
+    },
+    /* 44 */
+    {
+        {3, 0x02, 69},
+        {6, 0x02, 69},
+        {10, 0x02, 69},
+        {15, 0x02, 69},
+        {24, 0x02, 69},
+        {31, 0x02, 69},
+        {41, 0x02, 69},
+        {56, 0x03, 69},
+        {3, 0x02, 70},
+        {6, 0x02, 70},
+        {10, 0x02, 70},
+        {15, 0x02, 70},
+        {24, 0x02, 70},
+        {31, 0x02, 70},
+        {41, 0x02, 70},
+        {56, 0x03, 70},
+    },
+    /* 45 */
+    {
+        {3, 0x02, 71},
+        {6, 0x02, 71},
+        {10, 0x02, 71},
+        {15, 0x02, 71},
+        {24, 0x02, 71},
+        {31, 0x02, 71},
+        {41, 0x02, 71},
+        {56, 0x03, 71},
+        {3, 0x02, 72},
+        {6, 0x02, 72},
+        {10, 0x02, 72},
+        {15, 0x02, 72},
+        {24, 0x02, 72},
+        {31, 0x02, 72},
+        {41, 0x02, 72},
+        {56, 0x03, 72},
+    },
+    /* 46 */
+    {
+        {2, 0x02, 73},
+        {9, 0x02, 73},
+        {23, 0x02, 73},
+        {40, 0x03, 73},
+        {2, 0x02, 74},
+        {9, 0x02, 74},
+        {23, 0x02, 74},
+        {40, 0x03, 74},
+        {2, 0x02, 75},
+        {9, 0x02, 75},
+        {23, 0x02, 75},
+        {40, 0x03, 75},
+        {2, 0x02, 76},
+        {9, 0x02, 76},
+        {23, 0x02, 76},
+        {40, 0x03, 76},
+    },
+    /* 47 */
+    {
+        {3, 0x02, 73},
+        {6, 0x02, 73},
+        {10, 0x02, 73},
+        {15, 0x02, 73},
+        {24, 0x02, 73},
+        {31, 0x02, 73},
+        {41, 0x02, 73},
+        {56, 0x03, 73},
+        {3, 0x02, 74},
+        {6, 0x02, 74},
+        {10, 0x02, 74},
+        {15, 0x02, 74},
+        {24, 0x02, 74},
+        {31, 0x02, 74},
+        {41, 0x02, 74},
+        {56, 0x03, 74},
+    },
+    /* 48 */
+    {
+        {3, 0x02, 75},
+        {6, 0x02, 75},
+        {10, 0x02, 75},
+        {15, 0x02, 75},
+        {24, 0x02, 75},
+        {31, 0x02, 75},
+        {41, 0x02, 75},
+        {56, 0x03, 75},
+        {3, 0x02, 76},
+        {6, 0x02, 76},
+        {10, 0x02, 76},
+        {15, 0x02, 76},
+        {24, 0x02, 76},
+        {31, 0x02, 76},
+        {41, 0x02, 76},
+        {56, 0x03, 76},
+    },
+    /* 49 */
+    {
+        {1, 0x02, 77},
+        {22, 0x03, 77},
+        {1, 0x02, 78},
+        {22, 0x03, 78},
+        {1, 0x02, 79},
+        {22, 0x03, 79},
+        {1, 0x02, 80},
+        {22, 0x03, 80},
+        {1, 0x02, 81},
+        {22, 0x03, 81},
+        {1, 0x02, 82},
+        {22, 0x03, 82},
+        {1, 0x02, 83},
+        {22, 0x03, 83},
+        {1, 0x02, 84},
+        {22, 0x03, 84},
+    },
+    /* 50 */
+    {
+        {2, 0x02, 77},
+        {9, 0x02, 77},
+        {23, 0x02, 77},
+        {40, 0x03, 77},
+        {2, 0x02, 78},
+        {9, 0x02, 78},
+        {23, 0x02, 78},
+        {40, 0x03, 78},
+        {2, 0x02, 79},
+        {9, 0x02, 79},
+        {23, 0x02, 79},
+        {40, 0x03, 79},
+        {2, 0x02, 80},
+        {9, 0x02, 80},
+        {23, 0x02, 80},
+        {40, 0x03, 80},
+    },
+    /* 51 */
+    {
+        {3, 0x02, 77},
+        {6, 0x02, 77},
+        {10, 0x02, 77},
+        {15, 0x02, 77},
+        {24, 0x02, 77},
+        {31, 0x02, 77},
+        {41, 0x02, 77},
+        {56, 0x03, 77},
+        {3, 0x02, 78},
+        {6, 0x02, 78},
+        {10, 0x02, 78},
+        {15, 0x02, 78},
+        {24, 0x02, 78},
+        {31, 0x02, 78},
+        {41, 0x02, 78},
+        {56, 0x03, 78},
+    },
+    /* 52 */
+    {
+        {3, 0x02, 79},
+        {6, 0x02, 79},
+        {10, 0x02, 79},
+        {15, 0x02, 79},
+        {24, 0x02, 79},
+        {31, 0x02, 79},
+        {41, 0x02, 79},
+        {56, 0x03, 79},
+        {3, 0x02, 80},
+        {6, 0x02, 80},
+        {10, 0x02, 80},
+        {15, 0x02, 80},
+        {24, 0x02, 80},
+        {31, 0x02, 80},
+        {41, 0x02, 80},
+        {56, 0x03, 80},
+    },
+    /* 53 */
+    {
+        {2, 0x02, 81},
+        {9, 0x02, 81},
+        {23, 0x02, 81},
+        {40, 0x03, 81},
+        {2, 0x02, 82},
+        {9, 0x02, 82},
+        {23, 0x02, 82},
+        {40, 0x03, 82},
+        {2, 0x02, 83},
+        {9, 0x02, 83},
+        {23, 0x02, 83},
+        {40, 0x03, 83},
+        {2, 0x02, 84},
+        {9, 0x02, 84},
+        {23, 0x02, 84},
+        {40, 0x03, 84},
+    },
+    /* 54 */
+    {
+        {3, 0x02, 81},
+        {6, 0x02, 81},
+        {10, 0x02, 81},
+        {15, 0x02, 81},
+        {24, 0x02, 81},
+        {31, 0x02, 81},
+        {41, 0x02, 81},
+        {56, 0x03, 81},
+        {3, 0x02, 82},
+        {6, 0x02, 82},
+        {10, 0x02, 82},
+        {15, 0x02, 82},
+        {24, 0x02, 82},
+        {31, 0x02, 82},
+        {41, 0x02, 82},
+        {56, 0x03, 82},
+    },
+    /* 55 */
+    {
+        {3, 0x02, 83},
+        {6, 0x02, 83},
+        {10, 0x02, 83},
+        {15, 0x02, 83},
+        {24, 0x02, 83},
+        {31, 0x02, 83},
+        {41, 0x02, 83},
+        {56, 0x03, 83},
+        {3, 0x02, 84},
+        {6, 0x02, 84},
+        {10, 0x02, 84},
+        {15, 0x02, 84},
+        {24, 0x02, 84},
+        {31, 0x02, 84},
+        {41, 0x02, 84},
+        {56, 0x03, 84},
+    },
+    /* 56 */
+    {
+        {0, 0x03, 85},
+        {0, 0x03, 86},
+        {0, 0x03, 87},
+        {0, 0x03, 89},
+        {0, 0x03, 106},
+        {0, 0x03, 107},
+        {0, 0x03, 113},
+        {0, 0x03, 118},
+        {0, 0x03, 119},
+        {0, 0x03, 120},
+        {0, 0x03, 121},
+        {0, 0x03, 122},
+        {70, 0x00, 0},
+        {71, 0x00, 0},
+        {73, 0x00, 0},
+        {74, 0x01, 0},
+    },
+    /* 57 */
+    {
+        {1, 0x02, 85},
+        {22, 0x03, 85},
+        {1, 0x02, 86},
+        {22, 0x03, 86},
+        {1, 0x02, 87},
+        {22, 0x03, 87},
+        {1, 0x02, 89},
+        {22, 0x03, 89},
+        {1, 0x02, 106},
+        {22, 0x03, 106},
+        {1, 0x02, 107},
+        {22, 0x03, 107},
+        {1, 0x02, 113},
+        {22, 0x03, 113},
+        {1, 0x02, 118},
+        {22, 0x03, 118},
+    },
+    /* 58 */
+    {
+        {2, 0x02, 85},
+        {9, 0x02, 85},
+        {23, 0x02, 85},
+        {40, 0x03, 85},
+        {2, 0x02, 86},
+        {9, 0x02, 86},
+        {23, 0x02, 86},
+        {40, 0x03, 86},
+        {2, 0x02, 87},
+        {9, 0x02, 87},
+        {23, 0x02, 87},
+        {40, 0x03, 87},
+        {2, 0x02, 89},
+        {9, 0x02, 89},
+        {23, 0x02, 89},
+        {40, 0x03, 89},
+    },
+    /* 59 */
+    {
+        {3, 0x02, 85},
+        {6, 0x02, 85},
+        {10, 0x02, 85},
+        {15, 0x02, 85},
+        {24, 0x02, 85},
+        {31, 0x02, 85},
+        {41, 0x02, 85},
+        {56, 0x03, 85},
+        {3, 0x02, 86},
+        {6, 0x02, 86},
+        {10, 0x02, 86},
+        {15, 0x02, 86},
+        {24, 0x02, 86},
+        {31, 0x02, 86},
+        {41, 0x02, 86},
+        {56, 0x03, 86},
+    },
+    /* 60 */
+    {
+        {3, 0x02, 87},
+        {6, 0x02, 87},
+        {10, 0x02, 87},
+        {15, 0x02, 87},
+        {24, 0x02, 87},
+        {31, 0x02, 87},
+        {41, 0x02, 87},
+        {56, 0x03, 87},
+        {3, 0x02, 89},
+        {6, 0x02, 89},
+        {10, 0x02, 89},
+        {15, 0x02, 89},
+        {24, 0x02, 89},
+        {31, 0x02, 89},
+        {41, 0x02, 89},
+        {56, 0x03, 89},
+    },
+    /* 61 */
+    {
+        {2, 0x02, 106},
+        {9, 0x02, 106},
+        {23, 0x02, 106},
+        {40, 0x03, 106},
+        {2, 0x02, 107},
+        {9, 0x02, 107},
+        {23, 0x02, 107},
+        {40, 0x03, 107},
+        {2, 0x02, 113},
+        {9, 0x02, 113},
+        {23, 0x02, 113},
+        {40, 0x03, 113},
+        {2, 0x02, 118},
+        {9, 0x02, 118},
+        {23, 0x02, 118},
+        {40, 0x03, 118},
+    },
+    /* 62 */
+    {
+        {3, 0x02, 106},
+        {6, 0x02, 106},
+        {10, 0x02, 106},
+        {15, 0x02, 106},
+        {24, 0x02, 106},
+        {31, 0x02, 106},
+        {41, 0x02, 106},
+        {56, 0x03, 106},
+        {3, 0x02, 107},
+        {6, 0x02, 107},
+        {10, 0x02, 107},
+        {15, 0x02, 107},
+        {24, 0x02, 107},
+        {31, 0x02, 107},
+        {41, 0x02, 107},
+        {56, 0x03, 107},
+    },
+    /* 63 */
+    {
+        {3, 0x02, 113},
+        {6, 0x02, 113},
+        {10, 0x02, 113},
+        {15, 0x02, 113},
+        {24, 0x02, 113},
+        {31, 0x02, 113},
+        {41, 0x02, 113},
+        {56, 0x03, 113},
+        {3, 0x02, 118},
+        {6, 0x02, 118},
+        {10, 0x02, 118},
+        {15, 0x02, 118},
+        {24, 0x02, 118},
+        {31, 0x02, 118},
+        {41, 0x02, 118},
+        {56, 0x03, 118},
+    },
+    /* 64 */
+    {
+        {1, 0x02, 119},
+        {22, 0x03, 119},
+        {1, 0x02, 120},
+        {22, 0x03, 120},
+        {1, 0x02, 121},
+        {22, 0x03, 121},
+        {1, 0x02, 122},
+        {22, 0x03, 122},
+        {0, 0x03, 38},
+        {0, 0x03, 42},
+        {0, 0x03, 44},
+        {0, 0x03, 59},
+        {0, 0x03, 88},
+        {0, 0x03, 90},
+        {75, 0x00, 0},
+        {78, 0x00, 0},
+    },
+    /* 65 */
+    {
+        {2, 0x02, 119},
+        {9, 0x02, 119},
+        {23, 0x02, 119},
+        {40, 0x03, 119},
+        {2, 0x02, 120},
+        {9, 0x02, 120},
+        {23, 0x02, 120},
+        {40, 0x03, 120},
+        {2, 0x02, 121},
+        {9, 0x02, 121},
+        {23, 0x02, 121},
+        {40, 0x03, 121},
+        {2, 0x02, 122},
+        {9, 0x02, 122},
+        {23, 0x02, 122},
+        {40, 0x03, 122},
+    },
+    /* 66 */
+    {
+        {3, 0x02, 119},
+        {6, 0x02, 119},
+        {10, 0x02, 119},
+        {15, 0x02, 119},
+        {24, 0x02, 119},
+        {31, 0x02, 119},
+        {41, 0x02, 119},
+        {56, 0x03, 119},
+        {3, 0x02, 120},
+        {6, 0x02, 120},
+        {10, 0x02, 120},
+        {15, 0x02, 120},
+        {24, 0x02, 120},
+        {31, 0x02, 120},
+        {41, 0x02, 120},
+        {56, 0x03, 120},
+    },
+    /* 67 */
+    {
+        {3, 0x02, 121},
+        {6, 0x02, 121},
+        {10, 0x02, 121},
+        {15, 0x02, 121},
+        {24, 0x02, 121},
+        {31, 0x02, 121},
+        {41, 0x02, 121},
+        {56, 0x03, 121},
+        {3, 0x02, 122},
+        {6, 0x02, 122},
+        {10, 0x02, 122},
+        {15, 0x02, 122},
+        {24, 0x02, 122},
+        {31, 0x02, 122},
+        {41, 0x02, 122},
+        {56, 0x03, 122},
+    },
+    /* 68 */
+    {
+        {1, 0x02, 38},
+        {22, 0x03, 38},
+        {1, 0x02, 42},
+        {22, 0x03, 42},
+        {1, 0x02, 44},
+        {22, 0x03, 44},
+        {1, 0x02, 59},
+        {22, 0x03, 59},
+        {1, 0x02, 88},
+        {22, 0x03, 88},
+        {1, 0x02, 90},
+        {22, 0x03, 90},
+        {76, 0x00, 0},
+        {77, 0x00, 0},
+        {79, 0x00, 0},
+        {81, 0x00, 0},
+    },
+    /* 69 */
+    {
+        {2, 0x02, 38},
+        {9, 0x02, 38},
+        {23, 0x02, 38},
+        {40, 0x03, 38},
+        {2, 0x02, 42},
+        {9, 0x02, 42},
+        {23, 0x02, 42},
+        {40, 0x03, 42},
+        {2, 0x02, 44},
+        {9, 0x02, 44},
+        {23, 0x02, 44},
+        {40, 0x03, 44},
+        {2, 0x02, 59},
+        {9, 0x02, 59},
+        {23, 0x02, 59},
+        {40, 0x03, 59},
+    },
+    /* 70 */
+    {
+        {3, 0x02, 38},
+        {6, 0x02, 38},
+        {10, 0x02, 38},
+        {15, 0x02, 38},
+        {24, 0x02, 38},
+        {31, 0x02, 38},
+        {41, 0x02, 38},
+        {56, 0x03, 38},
+        {3, 0x02, 42},
+        {6, 0x02, 42},
+        {10, 0x02, 42},
+        {15, 0x02, 42},
+        {24, 0x02, 42},
+        {31, 0x02, 42},
+        {41, 0x02, 42},
+        {56, 0x03, 42},
+    },
+    /* 71 */
+    {
+        {3, 0x02, 44},
+        {6, 0x02, 44},
+        {10, 0x02, 44},
+        {15, 0x02, 44},
+        {24, 0x02, 44},
+        {31, 0x02, 44},
+        {41, 0x02, 44},
+        {56, 0x03, 44},
+        {3, 0x02, 59},
+        {6, 0x02, 59},
+        {10, 0x02, 59},
+        {15, 0x02, 59},
+        {24, 0x02, 59},
+        {31, 0x02, 59},
+        {41, 0x02, 59},
+        {56, 0x03, 59},
+    },
+    /* 72 */
+    {
+        {2, 0x02, 88},
+        {9, 0x02, 88},
+        {23, 0x02, 88},
+        {40, 0x03, 88},
+        {2, 0x02, 90},
+        {9, 0x02, 90},
+        {23, 0x02, 90},
+        {40, 0x03, 90},
+        {0, 0x03, 33},
+        {0, 0x03, 34},
+        {0, 0x03, 40},
+        {0, 0x03, 41},
+        {0, 0x03, 63},
+        {80, 0x00, 0},
+        {82, 0x00, 0},
+        {84, 0x00, 0},
+    },
+    /* 73 */
+    {
+        {3, 0x02, 88},
+        {6, 0x02, 88},
+        {10, 0x02, 88},
+        {15, 0x02, 88},
+        {24, 0x02, 88},
+        {31, 0x02, 88},
+        {41, 0x02, 88},
+        {56, 0x03, 88},
+        {3, 0x02, 90},
+        {6, 0x02, 90},
+        {10, 0x02, 90},
+        {15, 0x02, 90},
+        {24, 0x02, 90},
+        {31, 0x02, 90},
+        {41, 0x02, 90},
+        {56, 0x03, 90},
+    },
+    /* 74 */
+    {
+        {1, 0x02, 33},
+        {22, 0x03, 33},
+        {1, 0x02, 34},
+        {22, 0x03, 34},
+        {1, 0x02, 40},
+        {22, 0x03, 40},
+        {1, 0x02, 41},
+        {22, 0x03, 41},
+        {1, 0x02, 63},
+        {22, 0x03, 63},
+        {0, 0x03, 39},
+        {0, 0x03, 43},
+        {0, 0x03, 124},
+        {83, 0x00, 0},
+        {85, 0x00, 0},
+        {88, 0x00, 0},
+    },
+    /* 75 */
+    {
+        {2, 0x02, 33},
+        {9, 0x02, 33},
+        {23, 0x02, 33},
+        {40, 0x03, 33},
+        {2, 0x02, 34},
+        {9, 0x02, 34},
+        {23, 0x02, 34},
+        {40, 0x03, 34},
+        {2, 0x02, 40},
+        {9, 0x02, 40},
+        {23, 0x02, 40},
+        {40, 0x03, 40},
+        {2, 0x02, 41},
+        {9, 0x02, 41},
+        {23, 0x02, 41},
+        {40, 0x03, 41},
+    },
+    /* 76 */
+    {
+        {3, 0x02, 33},
+        {6, 0x02, 33},
+        {10, 0x02, 33},
+        {15, 0x02, 33},
+        {24, 0x02, 33},
+        {31, 0x02, 33},
+        {41, 0x02, 33},
+        {56, 0x03, 33},
+        {3, 0x02, 34},
+        {6, 0x02, 34},
+        {10, 0x02, 34},
+        {15, 0x02, 34},
+        {24, 0x02, 34},
+        {31, 0x02, 34},
+        {41, 0x02, 34},
+        {56, 0x03, 34},
+    },
+    /* 77 */
+    {
+        {3, 0x02, 40},
+        {6, 0x02, 40},
+        {10, 0x02, 40},
+        {15, 0x02, 40},
+        {24, 0x02, 40},
+        {31, 0x02, 40},
+        {41, 0x02, 40},
+        {56, 0x03, 40},
+        {3, 0x02, 41},
+        {6, 0x02, 41},
+        {10, 0x02, 41},
+        {15, 0x02, 41},
+        {24, 0x02, 41},
+        {31, 0x02, 41},
+        {41, 0x02, 41},
+        {56, 0x03, 41},
+    },
+    /* 78 */
+    {
+        {2, 0x02, 63},
+        {9, 0x02, 63},
+        {23, 0x02, 63},
+        {40, 0x03, 63},
+        {1, 0x02, 39},
+        {22, 0x03, 39},
+        {1, 0x02, 43},
+        {22, 0x03, 43},
+        {1, 0x02, 124},
+        {22, 0x03, 124},
+        {0, 0x03, 35},
+        {0, 0x03, 62},
+        {86, 0x00, 0},
+        {87, 0x00, 0},
+        {89, 0x00, 0},
+        {90, 0x00, 0},
+    },
+    /* 79 */
+    {
+        {3, 0x02, 63},
+        {6, 0x02, 63},
+        {10, 0x02, 63},
+        {15, 0x02, 63},
+        {24, 0x02, 63},
+        {31, 0x02, 63},
+        {41, 0x02, 63},
+        {56, 0x03, 63},
+        {2, 0x02, 39},
+        {9, 0x02, 39},
+        {23, 0x02, 39},
+        {40, 0x03, 39},
+        {2, 0x02, 43},
+        {9, 0x02, 43},
+        {23, 0x02, 43},
+        {40, 0x03, 43},
+    },
+    /* 80 */
+    {
+        {3, 0x02, 39},
+        {6, 0x02, 39},
+        {10, 0x02, 39},
+        {15, 0x02, 39},
+        {24, 0x02, 39},
+        {31, 0x02, 39},
+        {41, 0x02, 39},
+        {56, 0x03, 39},
+        {3, 0x02, 43},
+        {6, 0x02, 43},
+        {10, 0x02, 43},
+        {15, 0x02, 43},
+        {24, 0x02, 43},
+        {31, 0x02, 43},
+        {41, 0x02, 43},
+        {56, 0x03, 43},
+    },
+    /* 81 */
+    {
+        {2, 0x02, 124},
+        {9, 0x02, 124},
+        {23, 0x02, 124},
+        {40, 0x03, 124},
+        {1, 0x02, 35},
+        {22, 0x03, 35},
+        {1, 0x02, 62},
+        {22, 0x03, 62},
+        {0, 0x03, 0},
+        {0, 0x03, 36},
+        {0, 0x03, 64},
+        {0, 0x03, 91},
+        {0, 0x03, 93},
+        {0, 0x03, 126},
+        {91, 0x00, 0},
+        {92, 0x00, 0},
+    },
+    /* 82 */
+    {
+        {3, 0x02, 124},
+        {6, 0x02, 124},
+        {10, 0x02, 124},
+        {15, 0x02, 124},
+        {24, 0x02, 124},
+        {31, 0x02, 124},
+        {41, 0x02, 124},
+        {56, 0x03, 124},
+        {2, 0x02, 35},
+        {9, 0x02, 35},
+        {23, 0x02, 35},
+        {40, 0x03, 35},
+        {2, 0x02, 62},
+        {9, 0x02, 62},
+        {23, 0x02, 62},
+        {40, 0x03, 62},
+    },
+    /* 83 */
+    {
+        {3, 0x02, 35},
+        {6, 0x02, 35},
+        {10, 0x02, 35},
+        {15, 0x02, 35},
+        {24, 0x02, 35},
+        {31, 0x02, 35},
+        {41, 0x02, 35},
+        {56, 0x03, 35},
+        {3, 0x02, 62},
+        {6, 0x02, 62},
+        {10, 0x02, 62},
+        {15, 0x02, 62},
+        {24, 0x02, 62},
+        {31, 0x02, 62},
+        {41, 0x02, 62},
+        {56, 0x03, 62},
+    },
+    /* 84 */
+    {
+        {1, 0x02, 0},
+        {22, 0x03, 0},
+        {1, 0x02, 36},
+        {22, 0x03, 36},
+        {1, 0x02, 64},
+        {22, 0x03, 64},
+        {1, 0x02, 91},
+        {22, 0x03, 91},
+        {1, 0x02, 93},
+        {22, 0x03, 93},
+        {1, 0x02, 126},
+        {22, 0x03, 126},
+        {0, 0x03, 94},
+        {0, 0x03, 125},
+        {93, 0x00, 0},
+        {94, 0x00, 0},
+    },
+    /* 85 */
+    {
+        {2, 0x02, 0},
+        {9, 0x02, 0},
+        {23, 0x02, 0},
+        {40, 0x03, 0},
+        {2, 0x02, 36},
+        {9, 0x02, 36},
+        {23, 0x02, 36},
+        {40, 0x03, 36},
+        {2, 0x02, 64},
+        {9, 0x02, 64},
+        {23, 0x02, 64},
+        {40, 0x03, 64},
+        {2, 0x02, 91},
+        {9, 0x02, 91},
+        {23, 0x02, 91},
+        {40, 0x03, 91},
+    },
+    /* 86 */
+    {
+        {3, 0x02, 0},
+        {6, 0x02, 0},
+        {10, 0x02, 0},
+        {15, 0x02, 0},
+        {24, 0x02, 0},
+        {31, 0x02, 0},
+        {41, 0x02, 0},
+        {56, 0x03, 0},
+        {3, 0x02, 36},
+        {6, 0x02, 36},
+        {10, 0x02, 36},
+        {15, 0x02, 36},
+        {24, 0x02, 36},
+        {31, 0x02, 36},
+        {41, 0x02, 36},
+        {56, 0x03, 36},
+    },
+    /* 87 */
+    {
+        {3, 0x02, 64},
+        {6, 0x02, 64},
+        {10, 0x02, 64},
+        {15, 0x02, 64},
+        {24, 0x02, 64},
+        {31, 0x02, 64},
+        {41, 0x02, 64},
+        {56, 0x03, 64},
+        {3, 0x02, 91},
+        {6, 0x02, 91},
+        {10, 0x02, 91},
+        {15, 0x02, 91},
+        {24, 0x02, 91},
+        {31, 0x02, 91},
+        {41, 0x02, 91},
+        {56, 0x03, 91},
+    },
+    /* 88 */
+    {
+        {2, 0x02, 93},
+        {9, 0x02, 93},
+        {23, 0x02, 93},
+        {40, 0x03, 93},
+        {2, 0x02, 126},
+        {9, 0x02, 126},
+        {23, 0x02, 126},
+        {40, 0x03, 126},
+        {1, 0x02, 94},
+        {22, 0x03, 94},
+        {1, 0x02, 125},
+        {22, 0x03, 125},
+        {0, 0x03, 60},
+        {0, 0x03, 96},
+        {0, 0x03, 123},
+        {95, 0x00, 0},
+    },
+    /* 89 */
+    {
+        {3, 0x02, 93},
+        {6, 0x02, 93},
+        {10, 0x02, 93},
+        {15, 0x02, 93},
+        {24, 0x02, 93},
+        {31, 0x02, 93},
+        {41, 0x02, 93},
+        {56, 0x03, 93},
+        {3, 0x02, 126},
+        {6, 0x02, 126},
+        {10, 0x02, 126},
+        {15, 0x02, 126},
+        {24, 0x02, 126},
+        {31, 0x02, 126},
+        {41, 0x02, 126},
+        {56, 0x03, 126},
+    },
+    /* 90 */
+    {
+        {2, 0x02, 94},
+        {9, 0x02, 94},
+        {23, 0x02, 94},
+        {40, 0x03, 94},
+        {2, 0x02, 125},
+        {9, 0x02, 125},
+        {23, 0x02, 125},
+        {40, 0x03, 125},
+        {1, 0x02, 60},
+        {22, 0x03, 60},
+        {1, 0x02, 96},
+        {22, 0x03, 96},
+        {1, 0x02, 123},
+        {22, 0x03, 123},
+        {96, 0x00, 0},
+        {110, 0x00, 0},
+    },
+    /* 91 */
+    {
+        {3, 0x02, 94},
+        {6, 0x02, 94},
+        {10, 0x02, 94},
+        {15, 0x02, 94},
+        {24, 0x02, 94},
+        {31, 0x02, 94},
+        {41, 0x02, 94},
+        {56, 0x03, 94},
+        {3, 0x02, 125},
+        {6, 0x02, 125},
+        {10, 0x02, 125},
+        {15, 0x02, 125},
+        {24, 0x02, 125},
+        {31, 0x02, 125},
+        {41, 0x02, 125},
+        {56, 0x03, 125},
+    },
+    /* 92 */
+    {
+        {2, 0x02, 60},
+        {9, 0x02, 60},
+        {23, 0x02, 60},
+        {40, 0x03, 60},
+        {2, 0x02, 96},
+        {9, 0x02, 96},
+        {23, 0x02, 96},
+        {40, 0x03, 96},
+        {2, 0x02, 123},
+        {9, 0x02, 123},
+        {23, 0x02, 123},
+        {40, 0x03, 123},
+        {97, 0x00, 0},
+        {101, 0x00, 0},
+        {111, 0x00, 0},
+        {133, 0x00, 0},
+    },
+    /* 93 */
+    {
+        {3, 0x02, 60},
+        {6, 0x02, 60},
+        {10, 0x02, 60},
+        {15, 0x02, 60},
+        {24, 0x02, 60},
+        {31, 0x02, 60},
+        {41, 0x02, 60},
+        {56, 0x03, 60},
+        {3, 0x02, 96},
+        {6, 0x02, 96},
+        {10, 0x02, 96},
+        {15, 0x02, 96},
+        {24, 0x02, 96},
+        {31, 0x02, 96},
+        {41, 0x02, 96},
+        {56, 0x03, 96},
+    },
+    /* 94 */
+    {
+        {3, 0x02, 123},
+        {6, 0x02, 123},
+        {10, 0x02, 123},
+        {15, 0x02, 123},
+        {24, 0x02, 123},
+        {31, 0x02, 123},
+        {41, 0x02, 123},
+        {56, 0x03, 123},
+        {98, 0x00, 0},
+        {99, 0x00, 0},
+        {102, 0x00, 0},
+        {105, 0x00, 0},
+        {112, 0x00, 0},
+        {119, 0x00, 0},
+        {134, 0x00, 0},
+        {153, 0x00, 0},
+    },
+    /* 95 */
+    {
+        {0, 0x03, 92},
+        {0, 0x03, 195},
+        {0, 0x03, 208},
+        {100, 0x00, 0},
+        {103, 0x00, 0},
+        {104, 0x00, 0},
+        {106, 0x00, 0},
+        {107, 0x00, 0},
+        {113, 0x00, 0},
+        {116, 0x00, 0},
+        {120, 0x00, 0},
+        {126, 0x00, 0},
+        {135, 0x00, 0},
+        {142, 0x00, 0},
+        {154, 0x00, 0},
+        {169, 0x00, 0},
+    },
+    /* 96 */
+    {
+        {1, 0x02, 92},
+        {22, 0x03, 92},
+        {1, 0x02, 195},
+        {22, 0x03, 195},
+        {1, 0x02, 208},
+        {22, 0x03, 208},
+        {0, 0x03, 128},
+        {0, 0x03, 130},
+        {0, 0x03, 131},
+        {0, 0x03, 162},
+        {0, 0x03, 184},
+        {0, 0x03, 194},
+        {0, 0x03, 224},
+        {0, 0x03, 226},
+        {108, 0x00, 0},
+        {109, 0x00, 0},
+    },
+    /* 97 */
+    {
+        {2, 0x02, 92},
+        {9, 0x02, 92},
+        {23, 0x02, 92},
+        {40, 0x03, 92},
+        {2, 0x02, 195},
+        {9, 0x02, 195},
+        {23, 0x02, 195},
+        {40, 0x03, 195},
+        {2, 0x02, 208},
+        {9, 0x02, 208},
+        {23, 0x02, 208},
+        {40, 0x03, 208},
+        {1, 0x02, 128},
+        {22, 0x03, 128},
+        {1, 0x02, 130},
+        {22, 0x03, 130},
+    },
+    /* 98 */
+    {
+        {3, 0x02, 92},
+        {6, 0x02, 92},
+        {10, 0x02, 92},
+        {15, 0x02, 92},
+        {24, 0x02, 92},
+        {31, 0x02, 92},
+        {41, 0x02, 92},
+        {56, 0x03, 92},
+        {3, 0x02, 195},
+        {6, 0x02, 195},
+        {10, 0x02, 195},
+        {15, 0x02, 195},
+        {24, 0x02, 195},
+        {31, 0x02, 195},
+        {41, 0x02, 195},
+        {56, 0x03, 195},
+    },
+    /* 99 */
+    {
+        {3, 0x02, 208},
+        {6, 0x02, 208},
+        {10, 0x02, 208},
+        {15, 0x02, 208},
+        {24, 0x02, 208},
+        {31, 0x02, 208},
+        {41, 0x02, 208},
+        {56, 0x03, 208},
+        {2, 0x02, 128},
+        {9, 0x02, 128},
+        {23, 0x02, 128},
+        {40, 0x03, 128},
+        {2, 0x02, 130},
+        {9, 0x02, 130},
+        {23, 0x02, 130},
+        {40, 0x03, 130},
+    },
+    /* 100 */
+    {
+        {3, 0x02, 128},
+        {6, 0x02, 128},
+        {10, 0x02, 128},
+        {15, 0x02, 128},
+        {24, 0x02, 128},
+        {31, 0x02, 128},
+        {41, 0x02, 128},
+        {56, 0x03, 128},
+        {3, 0x02, 130},
+        {6, 0x02, 130},
+        {10, 0x02, 130},
+        {15, 0x02, 130},
+        {24, 0x02, 130},
+        {31, 0x02, 130},
+        {41, 0x02, 130},
+        {56, 0x03, 130},
+    },
+    /* 101 */
+    {
+        {1, 0x02, 131},
+        {22, 0x03, 131},
+        {1, 0x02, 162},
+        {22, 0x03, 162},
+        {1, 0x02, 184},
+        {22, 0x03, 184},
+        {1, 0x02, 194},
+        {22, 0x03, 194},
+        {1, 0x02, 224},
+        {22, 0x03, 224},
+        {1, 0x02, 226},
+        {22, 0x03, 226},
+        {0, 0x03, 153},
+        {0, 0x03, 161},
+        {0, 0x03, 167},
+        {0, 0x03, 172},
+    },
+    /* 102 */
+    {
+        {2, 0x02, 131},
+        {9, 0x02, 131},
+        {23, 0x02, 131},
+        {40, 0x03, 131},
+        {2, 0x02, 162},
+        {9, 0x02, 162},
+        {23, 0x02, 162},
+        {40, 0x03, 162},
+        {2, 0x02, 184},
+        {9, 0x02, 184},
+        {23, 0x02, 184},
+        {40, 0x03, 184},
+        {2, 0x02, 194},
+        {9, 0x02, 194},
+        {23, 0x02, 194},
+        {40, 0x03, 194},
+    },
+    /* 103 */
+    {
+        {3, 0x02, 131},
+        {6, 0x02, 131},
+        {10, 0x02, 131},
+        {15, 0x02, 131},
+        {24, 0x02, 131},
+        {31, 0x02, 131},
+        {41, 0x02, 131},
+        {56, 0x03, 131},
+        {3, 0x02, 162},
+        {6, 0x02, 162},
+        {10, 0x02, 162},
+        {15, 0x02, 162},
+        {24, 0x02, 162},
+        {31, 0x02, 162},
+        {41, 0x02, 162},
+        {56, 0x03, 162},
+    },
+    /* 104 */
+    {
+        {3, 0x02, 184},
+        {6, 0x02, 184},
+        {10, 0x02, 184},
+        {15, 0x02, 184},
+        {24, 0x02, 184},
+        {31, 0x02, 184},
+        {41, 0x02, 184},
+        {56, 0x03, 184},
+        {3, 0x02, 194},
+        {6, 0x02, 194},
+        {10, 0x02, 194},
+        {15, 0x02, 194},
+        {24, 0x02, 194},
+        {31, 0x02, 194},
+        {41, 0x02, 194},
+        {56, 0x03, 194},
+    },
+    /* 105 */
+    {
+        {2, 0x02, 224},
+        {9, 0x02, 224},
+        {23, 0x02, 224},
+        {40, 0x03, 224},
+        {2, 0x02, 226},
+        {9, 0x02, 226},
+        {23, 0x02, 226},
+        {40, 0x03, 226},
+        {1, 0x02, 153},
+        {22, 0x03, 153},
+        {1, 0x02, 161},
+        {22, 0x03, 161},
+        {1, 0x02, 167},
+        {22, 0x03, 167},
+        {1, 0x02, 172},
+        {22, 0x03, 172},
+    },
+    /* 106 */
+    {
+        {3, 0x02, 224},
+        {6, 0x02, 224},
+        {10, 0x02, 224},
+        {15, 0x02, 224},
+        {24, 0x02, 224},
+        {31, 0x02, 224},
+        {41, 0x02, 224},
+        {56, 0x03, 224},
+        {3, 0x02, 226},
+        {6, 0x02, 226},
+        {10, 0x02, 226},
+        {15, 0x02, 226},
+        {24, 0x02, 226},
+        {31, 0x02, 226},
+        {41, 0x02, 226},
+        {56, 0x03, 226},
+    },
+    /* 107 */
+    {
+        {2, 0x02, 153},
+        {9, 0x02, 153},
+        {23, 0x02, 153},
+        {40, 0x03, 153},
+        {2, 0x02, 161},
+        {9, 0x02, 161},
+        {23, 0x02, 161},
+        {40, 0x03, 161},
+        {2, 0x02, 167},
+        {9, 0x02, 167},
+        {23, 0x02, 167},
+        {40, 0x03, 167},
+        {2, 0x02, 172},
+        {9, 0x02, 172},
+        {23, 0x02, 172},
+        {40, 0x03, 172},
+    },
+    /* 108 */
+    {
+        {3, 0x02, 153},
+        {6, 0x02, 153},
+        {10, 0x02, 153},
+        {15, 0x02, 153},
+        {24, 0x02, 153},
+        {31, 0x02, 153},
+        {41, 0x02, 153},
+        {56, 0x03, 153},
+        {3, 0x02, 161},
+        {6, 0x02, 161},
+        {10, 0x02, 161},
+        {15, 0x02, 161},
+        {24, 0x02, 161},
+        {31, 0x02, 161},
+        {41, 0x02, 161},
+        {56, 0x03, 161},
+    },
+    /* 109 */
+    {
+        {3, 0x02, 167},
+        {6, 0x02, 167},
+        {10, 0x02, 167},
+        {15, 0x02, 167},
+        {24, 0x02, 167},
+        {31, 0x02, 167},
+        {41, 0x02, 167},
+        {56, 0x03, 167},
+        {3, 0x02, 172},
+        {6, 0x02, 172},
+        {10, 0x02, 172},
+        {15, 0x02, 172},
+        {24, 0x02, 172},
+        {31, 0x02, 172},
+        {41, 0x02, 172},
+        {56, 0x03, 172},
+    },
+    /* 110 */
+    {
+        {114, 0x00, 0},
+        {115, 0x00, 0},
+        {117, 0x00, 0},
+        {118, 0x00, 0},
+        {121, 0x00, 0},
+        {123, 0x00, 0},
+        {127, 0x00, 0},
+        {130, 0x00, 0},
+        {136, 0x00, 0},
+        {139, 0x00, 0},
+        {143, 0x00, 0},
+        {146, 0x00, 0},
+        {155, 0x00, 0},
+        {162, 0x00, 0},
+        {170, 0x00, 0},
+        {180, 0x00, 0},
+    },
+    /* 111 */
+    {
+        {0, 0x03, 176},
+        {0, 0x03, 177},
+        {0, 0x03, 179},
+        {0, 0x03, 209},
+        {0, 0x03, 216},
+        {0, 0x03, 217},
+        {0, 0x03, 227},
+        {0, 0x03, 229},
+        {0, 0x03, 230},
+        {122, 0x00, 0},
+        {124, 0x00, 0},
+        {125, 0x00, 0},
+        {128, 0x00, 0},
+        {129, 0x00, 0},
+        {131, 0x00, 0},
+        {132, 0x00, 0},
+    },
+    /* 112 */
+    {
+        {1, 0x02, 176},
+        {22, 0x03, 176},
+        {1, 0x02, 177},
+        {22, 0x03, 177},
+        {1, 0x02, 179},
+        {22, 0x03, 179},
+        {1, 0x02, 209},
+        {22, 0x03, 209},
+        {1, 0x02, 216},
+        {22, 0x03, 216},
+        {1, 0x02, 217},
+        {22, 0x03, 217},
+        {1, 0x02, 227},
+        {22, 0x03, 227},
+        {1, 0x02, 229},
+        {22, 0x03, 229},
+    },
+    /* 113 */
+    {
+        {2, 0x02, 176},
+        {9, 0x02, 176},
+        {23, 0x02, 176},
+        {40, 0x03, 176},
+        {2, 0x02, 177},
+        {9, 0x02, 177},
+        {23, 0x02, 177},
+        {40, 0x03, 177},
+        {2, 0x02, 179},
+        {9, 0x02, 179},
+        {23, 0x02, 179},
+        {40, 0x03, 179},
+        {2, 0x02, 209},
+        {9, 0x02, 209},
+        {23, 0x02, 209},
+        {40, 0x03, 209},
+    },
+    /* 114 */
+    {
+        {3, 0x02, 176},
+        {6, 0x02, 176},
+        {10, 0x02, 176},
+        {15, 0x02, 176},
+        {24, 0x02, 176},
+        {31, 0x02, 176},
+        {41, 0x02, 176},
+        {56, 0x03, 176},
+        {3, 0x02, 177},
+        {6, 0x02, 177},
+        {10, 0x02, 177},
+        {15, 0x02, 177},
+        {24, 0x02, 177},
+        {31, 0x02, 177},
+        {41, 0x02, 177},
+        {56, 0x03, 177},
+    },
+    /* 115 */
+    {
+        {3, 0x02, 179},
+        {6, 0x02, 179},
+        {10, 0x02, 179},
+        {15, 0x02, 179},
+        {24, 0x02, 179},
+        {31, 0x02, 179},
+        {41, 0x02, 179},
+        {56, 0x03, 179},
+        {3, 0x02, 209},
+        {6, 0x02, 209},
+        {10, 0x02, 209},
+        {15, 0x02, 209},
+        {24, 0x02, 209},
+        {31, 0x02, 209},
+        {41, 0x02, 209},
+        {56, 0x03, 209},
+    },
+    /* 116 */
+    {
+        {2, 0x02, 216},
+        {9, 0x02, 216},
+        {23, 0x02, 216},
+        {40, 0x03, 216},
+        {2, 0x02, 217},
+        {9, 0x02, 217},
+        {23, 0x02, 217},
+        {40, 0x03, 217},
+        {2, 0x02, 227},
+        {9, 0x02, 227},
+        {23, 0x02, 227},
+        {40, 0x03, 227},
+        {2, 0x02, 229},
+        {9, 0x02, 229},
+        {23, 0x02, 229},
+        {40, 0x03, 229},
+    },
+    /* 117 */
+    {
+        {3, 0x02, 216},
+        {6, 0x02, 216},
+        {10, 0x02, 216},
+        {15, 0x02, 216},
+        {24, 0x02, 216},
+        {31, 0x02, 216},
+        {41, 0x02, 216},
+        {56, 0x03, 216},
+        {3, 0x02, 217},
+        {6, 0x02, 217},
+        {10, 0x02, 217},
+        {15, 0x02, 217},
+        {24, 0x02, 217},
+        {31, 0x02, 217},
+        {41, 0x02, 217},
+        {56, 0x03, 217},
+    },
+    /* 118 */
+    {
+        {3, 0x02, 227},
+        {6, 0x02, 227},
+        {10, 0x02, 227},
+        {15, 0x02, 227},
+        {24, 0x02, 227},
+        {31, 0x02, 227},
+        {41, 0x02, 227},
+        {56, 0x03, 227},
+        {3, 0x02, 229},
+        {6, 0x02, 229},
+        {10, 0x02, 229},
+        {15, 0x02, 229},
+        {24, 0x02, 229},
+        {31, 0x02, 229},
+        {41, 0x02, 229},
+        {56, 0x03, 229},
+    },
+    /* 119 */
+    {
+        {1, 0x02, 230},
+        {22, 0x03, 230},
+        {0, 0x03, 129},
+        {0, 0x03, 132},
+        {0, 0x03, 133},
+        {0, 0x03, 134},
+        {0, 0x03, 136},
+        {0, 0x03, 146},
+        {0, 0x03, 154},
+        {0, 0x03, 156},
+        {0, 0x03, 160},
+        {0, 0x03, 163},
+        {0, 0x03, 164},
+        {0, 0x03, 169},
+        {0, 0x03, 170},
+        {0, 0x03, 173},
+    },
+    /* 120 */
+    {
+        {2, 0x02, 230},
+        {9, 0x02, 230},
+        {23, 0x02, 230},
+        {40, 0x03, 230},
+        {1, 0x02, 129},
+        {22, 0x03, 129},
+        {1, 0x02, 132},
+        {22, 0x03, 132},
+        {1, 0x02, 133},
+        {22, 0x03, 133},
+        {1, 0x02, 134},
+        {22, 0x03, 134},
+        {1, 0x02, 136},
+        {22, 0x03, 136},
+        {1, 0x02, 146},
+        {22, 0x03, 146},
+    },
+    /* 121 */
+    {
+        {3, 0x02, 230},
+        {6, 0x02, 230},
+        {10, 0x02, 230},
+        {15, 0x02, 230},
+        {24, 0x02, 230},
+        {31, 0x02, 230},
+        {41, 0x02, 230},
+        {56, 0x03, 230},
+        {2, 0x02, 129},
+        {9, 0x02, 129},
+        {23, 0x02, 129},
+        {40, 0x03, 129},
+        {2, 0x02, 132},
+        {9, 0x02, 132},
+        {23, 0x02, 132},
+        {40, 0x03, 132},
+    },
+    /* 122 */
+    {
+        {3, 0x02, 129},
+        {6, 0x02, 129},
+        {10, 0x02, 129},
+        {15, 0x02, 129},
+        {24, 0x02, 129},
+        {31, 0x02, 129},
+        {41, 0x02, 129},
+        {56, 0x03, 129},
+        {3, 0x02, 132},
+        {6, 0x02, 132},
+        {10, 0x02, 132},
+        {15, 0x02, 132},
+        {24, 0x02, 132},
+        {31, 0x02, 132},
+        {41, 0x02, 132},
+        {56, 0x03, 132},
+    },
+    /* 123 */
+    {
+        {2, 0x02, 133},
+        {9, 0x02, 133},
+        {23, 0x02, 133},
+        {40, 0x03, 133},
+        {2, 0x02, 134},
+        {9, 0x02, 134},
+        {23, 0x02, 134},
+        {40, 0x03, 134},
+        {2, 0x02, 136},
+        {9, 0x02, 136},
+        {23, 0x02, 136},
+        {40, 0x03, 136},
+        {2, 0x02, 146},
+        {9, 0x02, 146},
+        {23, 0x02, 146},
+        {40, 0x03, 146},
+    },
+    /* 124 */
+    {
+        {3, 0x02, 133},
+        {6, 0x02, 133},
+        {10, 0x02, 133},
+        {15, 0x02, 133},
+        {24, 0x02, 133},
+        {31, 0x02, 133},
+        {41, 0x02, 133},
+        {56, 0x03, 133},
+        {3, 0x02, 134},
+        {6, 0x02, 134},
+        {10, 0x02, 134},
+        {15, 0x02, 134},
+        {24, 0x02, 134},
+        {31, 0x02, 134},
+        {41, 0x02, 134},
+        {56, 0x03, 134},
+    },
+    /* 125 */
+    {
+        {3, 0x02, 136},
+        {6, 0x02, 136},
+        {10, 0x02, 136},
+        {15, 0x02, 136},
+        {24, 0x02, 136},
+        {31, 0x02, 136},
+        {41, 0x02, 136},
+        {56, 0x03, 136},
+        {3, 0x02, 146},
+        {6, 0x02, 146},
+        {10, 0x02, 146},
+        {15, 0x02, 146},
+        {24, 0x02, 146},
+        {31, 0x02, 146},
+        {41, 0x02, 146},
+        {56, 0x03, 146},
+    },
+    /* 126 */
+    {
+        {1, 0x02, 154},
+        {22, 0x03, 154},
+        {1, 0x02, 156},
+        {22, 0x03, 156},
+        {1, 0x02, 160},
+        {22, 0x03, 160},
+        {1, 0x02, 163},
+        {22, 0x03, 163},
+        {1, 0x02, 164},
+        {22, 0x03, 164},
+        {1, 0x02, 169},
+        {22, 0x03, 169},
+        {1, 0x02, 170},
+        {22, 0x03, 170},
+        {1, 0x02, 173},
+        {22, 0x03, 173},
+    },
+    /* 127 */
+    {
+        {2, 0x02, 154},
+        {9, 0x02, 154},
+        {23, 0x02, 154},
+        {40, 0x03, 154},
+        {2, 0x02, 156},
+        {9, 0x02, 156},
+        {23, 0x02, 156},
+        {40, 0x03, 156},
+        {2, 0x02, 160},
+        {9, 0x02, 160},
+        {23, 0x02, 160},
+        {40, 0x03, 160},
+        {2, 0x02, 163},
+        {9, 0x02, 163},
+        {23, 0x02, 163},
+        {40, 0x03, 163},
+    },
+    /* 128 */
+    {
+        {3, 0x02, 154},
+        {6, 0x02, 154},
+        {10, 0x02, 154},
+        {15, 0x02, 154},
+        {24, 0x02, 154},
+        {31, 0x02, 154},
+        {41, 0x02, 154},
+        {56, 0x03, 154},
+        {3, 0x02, 156},
+        {6, 0x02, 156},
+        {10, 0x02, 156},
+        {15, 0x02, 156},
+        {24, 0x02, 156},
+        {31, 0x02, 156},
+        {41, 0x02, 156},
+        {56, 0x03, 156},
+    },
+    /* 129 */
+    {
+        {3, 0x02, 160},
+        {6, 0x02, 160},
+        {10, 0x02, 160},
+        {15, 0x02, 160},
+        {24, 0x02, 160},
+        {31, 0x02, 160},
+        {41, 0x02, 160},
+        {56, 0x03, 160},
+        {3, 0x02, 163},
+        {6, 0x02, 163},
+        {10, 0x02, 163},
+        {15, 0x02, 163},
+        {24, 0x02, 163},
+        {31, 0x02, 163},
+        {41, 0x02, 163},
+        {56, 0x03, 163},
+    },
+    /* 130 */
+    {
+        {2, 0x02, 164},
+        {9, 0x02, 164},
+        {23, 0x02, 164},
+        {40, 0x03, 164},
+        {2, 0x02, 169},
+        {9, 0x02, 169},
+        {23, 0x02, 169},
+        {40, 0x03, 169},
+        {2, 0x02, 170},
+        {9, 0x02, 170},
+        {23, 0x02, 170},
+        {40, 0x03, 170},
+        {2, 0x02, 173},
+        {9, 0x02, 173},
+        {23, 0x02, 173},
+        {40, 0x03, 173},
+    },
+    /* 131 */
+    {
+        {3, 0x02, 164},
+        {6, 0x02, 164},
+        {10, 0x02, 164},
+        {15, 0x02, 164},
+        {24, 0x02, 164},
+        {31, 0x02, 164},
+        {41, 0x02, 164},
+        {56, 0x03, 164},
+        {3, 0x02, 169},
+        {6, 0x02, 169},
+        {10, 0x02, 169},
+        {15, 0x02, 169},
+        {24, 0x02, 169},
+        {31, 0x02, 169},
+        {41, 0x02, 169},
+        {56, 0x03, 169},
+    },
+    /* 132 */
+    {
+        {3, 0x02, 170},
+        {6, 0x02, 170},
+        {10, 0x02, 170},
+        {15, 0x02, 170},
+        {24, 0x02, 170},
+        {31, 0x02, 170},
+        {41, 0x02, 170},
+        {56, 0x03, 170},
+        {3, 0x02, 173},
+        {6, 0x02, 173},
+        {10, 0x02, 173},
+        {15, 0x02, 173},
+        {24, 0x02, 173},
+        {31, 0x02, 173},
+        {41, 0x02, 173},
+        {56, 0x03, 173},
+    },
+    /* 133 */
+    {
+        {137, 0x00, 0},
+        {138, 0x00, 0},
+        {140, 0x00, 0},
+        {141, 0x00, 0},
+        {144, 0x00, 0},
+        {145, 0x00, 0},
+        {147, 0x00, 0},
+        {150, 0x00, 0},
+        {156, 0x00, 0},
+        {159, 0x00, 0},
+        {163, 0x00, 0},
+        {166, 0x00, 0},
+        {171, 0x00, 0},
+        {174, 0x00, 0},
+        {181, 0x00, 0},
+        {190, 0x00, 0},
+    },
+    /* 134 */
+    {
+        {0, 0x03, 178},
+        {0, 0x03, 181},
+        {0, 0x03, 185},
+        {0, 0x03, 186},
+        {0, 0x03, 187},
+        {0, 0x03, 189},
+        {0, 0x03, 190},
+        {0, 0x03, 196},
+        {0, 0x03, 198},
+        {0, 0x03, 228},
+        {0, 0x03, 232},
+        {0, 0x03, 233},
+        {148, 0x00, 0},
+        {149, 0x00, 0},
+        {151, 0x00, 0},
+        {152, 0x00, 0},
+    },
+    /* 135 */
+    {
+        {1, 0x02, 178},
+        {22, 0x03, 178},
+        {1, 0x02, 181},
+        {22, 0x03, 181},
+        {1, 0x02, 185},
+        {22, 0x03, 185},
+        {1, 0x02, 186},
+        {22, 0x03, 186},
+        {1, 0x02, 187},
+        {22, 0x03, 187},
+        {1, 0x02, 189},
+        {22, 0x03, 189},
+        {1, 0x02, 190},
+        {22, 0x03, 190},
+        {1, 0x02, 196},
+        {22, 0x03, 196},
+    },
+    /* 136 */
+    {
+        {2, 0x02, 178},
+        {9, 0x02, 178},
+        {23, 0x02, 178},
+        {40, 0x03, 178},
+        {2, 0x02, 181},
+        {9, 0x02, 181},
+        {23, 0x02, 181},
+        {40, 0x03, 181},
+        {2, 0x02, 185},
+        {9, 0x02, 185},
+        {23, 0x02, 185},
+        {40, 0x03, 185},
+        {2, 0x02, 186},
+        {9, 0x02, 186},
+        {23, 0x02, 186},
+        {40, 0x03, 186},
+    },
+    /* 137 */
+    {
+        {3, 0x02, 178},
+        {6, 0x02, 178},
+        {10, 0x02, 178},
+        {15, 0x02, 178},
+        {24, 0x02, 178},
+        {31, 0x02, 178},
+        {41, 0x02, 178},
+        {56, 0x03, 178},
+        {3, 0x02, 181},
+        {6, 0x02, 181},
+        {10, 0x02, 181},
+        {15, 0x02, 181},
+        {24, 0x02, 181},
+        {31, 0x02, 181},
+        {41, 0x02, 181},
+        {56, 0x03, 181},
+    },
+    /* 138 */
+    {
+        {3, 0x02, 185},
+        {6, 0x02, 185},
+        {10, 0x02, 185},
+        {15, 0x02, 185},
+        {24, 0x02, 185},
+        {31, 0x02, 185},
+        {41, 0x02, 185},
+        {56, 0x03, 185},
+        {3, 0x02, 186},
+        {6, 0x02, 186},
+        {10, 0x02, 186},
+        {15, 0x02, 186},
+        {24, 0x02, 186},
+        {31, 0x02, 186},
+        {41, 0x02, 186},
+        {56, 0x03, 186},
+    },
+    /* 139 */
+    {
+        {2, 0x02, 187},
+        {9, 0x02, 187},
+        {23, 0x02, 187},
+        {40, 0x03, 187},
+        {2, 0x02, 189},
+        {9, 0x02, 189},
+        {23, 0x02, 189},
+        {40, 0x03, 189},
+        {2, 0x02, 190},
+        {9, 0x02, 190},
+        {23, 0x02, 190},
+        {40, 0x03, 190},
+        {2, 0x02, 196},
+        {9, 0x02, 196},
+        {23, 0x02, 196},
+        {40, 0x03, 196},
+    },
+    /* 140 */
+    {
+        {3, 0x02, 187},
+        {6, 0x02, 187},
+        {10, 0x02, 187},
+        {15, 0x02, 187},
+        {24, 0x02, 187},
+        {31, 0x02, 187},
+        {41, 0x02, 187},
+        {56, 0x03, 187},
+        {3, 0x02, 189},
+        {6, 0x02, 189},
+        {10, 0x02, 189},
+        {15, 0x02, 189},
+        {24, 0x02, 189},
+        {31, 0x02, 189},
+        {41, 0x02, 189},
+        {56, 0x03, 189},
+    },
+    /* 141 */
+    {
+        {3, 0x02, 190},
+        {6, 0x02, 190},
+        {10, 0x02, 190},
+        {15, 0x02, 190},
+        {24, 0x02, 190},
+        {31, 0x02, 190},
+        {41, 0x02, 190},
+        {56, 0x03, 190},
+        {3, 0x02, 196},
+        {6, 0x02, 196},
+        {10, 0x02, 196},
+        {15, 0x02, 196},
+        {24, 0x02, 196},
+        {31, 0x02, 196},
+        {41, 0x02, 196},
+        {56, 0x03, 196},
+    },
+    /* 142 */
+    {
+        {1, 0x02, 198},
+        {22, 0x03, 198},
+        {1, 0x02, 228},
+        {22, 0x03, 228},
+        {1, 0x02, 232},
+        {22, 0x03, 232},
+        {1, 0x02, 233},
+        {22, 0x03, 233},
+        {0, 0x03, 1},
+        {0, 0x03, 135},
+        {0, 0x03, 137},
+        {0, 0x03, 138},
+        {0, 0x03, 139},
+        {0, 0x03, 140},
+        {0, 0x03, 141},
+        {0, 0x03, 143},
+    },
+    /* 143 */
+    {
+        {2, 0x02, 198},
+        {9, 0x02, 198},
+        {23, 0x02, 198},
+        {40, 0x03, 198},
+        {2, 0x02, 228},
+        {9, 0x02, 228},
+        {23, 0x02, 228},
+        {40, 0x03, 228},
+        {2, 0x02, 232},
+        {9, 0x02, 232},
+        {23, 0x02, 232},
+        {40, 0x03, 232},
+        {2, 0x02, 233},
+        {9, 0x02, 233},
+        {23, 0x02, 233},
+        {40, 0x03, 233},
+    },
+    /* 144 */
+    {
+        {3, 0x02, 198},
+        {6, 0x02, 198},
+        {10, 0x02, 198},
+        {15, 0x02, 198},
+        {24, 0x02, 198},
+        {31, 0x02, 198},
+        {41, 0x02, 198},
+        {56, 0x03, 198},
+        {3, 0x02, 228},
+        {6, 0x02, 228},
+        {10, 0x02, 228},
+        {15, 0x02, 228},
+        {24, 0x02, 228},
+        {31, 0x02, 228},
+        {41, 0x02, 228},
+        {56, 0x03, 228},
+    },
+    /* 145 */
+    {
+        {3, 0x02, 232},
+        {6, 0x02, 232},
+        {10, 0x02, 232},
+        {15, 0x02, 232},
+        {24, 0x02, 232},
+        {31, 0x02, 232},
+        {41, 0x02, 232},
+        {56, 0x03, 232},
+        {3, 0x02, 233},
+        {6, 0x02, 233},
+        {10, 0x02, 233},
+        {15, 0x02, 233},
+        {24, 0x02, 233},
+        {31, 0x02, 233},
+        {41, 0x02, 233},
+        {56, 0x03, 233},
+    },
+    /* 146 */
+    {
+        {1, 0x02, 1},
+        {22, 0x03, 1},
+        {1, 0x02, 135},
+        {22, 0x03, 135},
+        {1, 0x02, 137},
+        {22, 0x03, 137},
+        {1, 0x02, 138},
+        {22, 0x03, 138},
+        {1, 0x02, 139},
+        {22, 0x03, 139},
+        {1, 0x02, 140},
+        {22, 0x03, 140},
+        {1, 0x02, 141},
+        {22, 0x03, 141},
+        {1, 0x02, 143},
+        {22, 0x03, 143},
+    },
+    /* 147 */
+    {
+        {2, 0x02, 1},
+        {9, 0x02, 1},
+        {23, 0x02, 1},
+        {40, 0x03, 1},
+        {2, 0x02, 135},
+        {9, 0x02, 135},
+        {23, 0x02, 135},
+        {40, 0x03, 135},
+        {2, 0x02, 137},
+        {9, 0x02, 137},
+        {23, 0x02, 137},
+        {40, 0x03, 137},
+        {2, 0x02, 138},
+        {9, 0x02, 138},
+        {23, 0x02, 138},
+        {40, 0x03, 138},
+    },
+    /* 148 */
+    {
+        {3, 0x02, 1},
+        {6, 0x02, 1},
+        {10, 0x02, 1},
+        {15, 0x02, 1},
+        {24, 0x02, 1},
+        {31, 0x02, 1},
+        {41, 0x02, 1},
+        {56, 0x03, 1},
+        {3, 0x02, 135},
+        {6, 0x02, 135},
+        {10, 0x02, 135},
+        {15, 0x02, 135},
+        {24, 0x02, 135},
+        {31, 0x02, 135},
+        {41, 0x02, 135},
+        {56, 0x03, 135},
+    },
+    /* 149 */
+    {
+        {3, 0x02, 137},
+        {6, 0x02, 137},
+        {10, 0x02, 137},
+        {15, 0x02, 137},
+        {24, 0x02, 137},
+        {31, 0x02, 137},
+        {41, 0x02, 137},
+        {56, 0x03, 137},
+        {3, 0x02, 138},
+        {6, 0x02, 138},
+        {10, 0x02, 138},
+        {15, 0x02, 138},
+        {24, 0x02, 138},
+        {31, 0x02, 138},
+        {41, 0x02, 138},
+        {56, 0x03, 138},
+    },
+    /* 150 */
+    {
+        {2, 0x02, 139},
+        {9, 0x02, 139},
+        {23, 0x02, 139},
+        {40, 0x03, 139},
+        {2, 0x02, 140},
+        {9, 0x02, 140},
+        {23, 0x02, 140},
+        {40, 0x03, 140},
+        {2, 0x02, 141},
+        {9, 0x02, 141},
+        {23, 0x02, 141},
+        {40, 0x03, 141},
+        {2, 0x02, 143},
+        {9, 0x02, 143},
+        {23, 0x02, 143},
+        {40, 0x03, 143},
+    },
+    /* 151 */
+    {
+        {3, 0x02, 139},
+        {6, 0x02, 139},
+        {10, 0x02, 139},
+        {15, 0x02, 139},
+        {24, 0x02, 139},
+        {31, 0x02, 139},
+        {41, 0x02, 139},
+        {56, 0x03, 139},
+        {3, 0x02, 140},
+        {6, 0x02, 140},
+        {10, 0x02, 140},
+        {15, 0x02, 140},
+        {24, 0x02, 140},
+        {31, 0x02, 140},
+        {41, 0x02, 140},
+        {56, 0x03, 140},
+    },
+    /* 152 */
+    {
+        {3, 0x02, 141},
+        {6, 0x02, 141},
+        {10, 0x02, 141},
+        {15, 0x02, 141},
+        {24, 0x02, 141},
+        {31, 0x02, 141},
+        {41, 0x02, 141},
+        {56, 0x03, 141},
+        {3, 0x02, 143},
+        {6, 0x02, 143},
+        {10, 0x02, 143},
+        {15, 0x02, 143},
+        {24, 0x02, 143},
+        {31, 0x02, 143},
+        {41, 0x02, 143},
+        {56, 0x03, 143},
+    },
+    /* 153 */
+    {
+        {157, 0x00, 0},
+        {158, 0x00, 0},
+        {160, 0x00, 0},
+        {161, 0x00, 0},
+        {164, 0x00, 0},
+        {165, 0x00, 0},
+        {167, 0x00, 0},
+        {168, 0x00, 0},
+        {172, 0x00, 0},
+        {173, 0x00, 0},
+        {175, 0x00, 0},
+        {177, 0x00, 0},
+        {182, 0x00, 0},
+        {185, 0x00, 0},
+        {191, 0x00, 0},
+        {207, 0x00, 0},
+    },
+    /* 154 */
+    {
+        {0, 0x03, 147},
+        {0, 0x03, 149},
+        {0, 0x03, 150},
+        {0, 0x03, 151},
+        {0, 0x03, 152},
+        {0, 0x03, 155},
+        {0, 0x03, 157},
+        {0, 0x03, 158},
+        {0, 0x03, 165},
+        {0, 0x03, 166},
+        {0, 0x03, 168},
+        {0, 0x03, 174},
+        {0, 0x03, 175},
+        {0, 0x03, 180},
+        {0, 0x03, 182},
+        {0, 0x03, 183},
+    },
+    /* 155 */
+    {
+        {1, 0x02, 147},
+        {22, 0x03, 147},
+        {1, 0x02, 149},
+        {22, 0x03, 149},
+        {1, 0x02, 150},
+        {22, 0x03, 150},
+        {1, 0x02, 151},
+        {22, 0x03, 151},
+        {1, 0x02, 152},
+        {22, 0x03, 152},
+        {1, 0x02, 155},
+        {22, 0x03, 155},
+        {1, 0x02, 157},
+        {22, 0x03, 157},
+        {1, 0x02, 158},
+        {22, 0x03, 158},
+    },
+    /* 156 */
+    {
+        {2, 0x02, 147},
+        {9, 0x02, 147},
+        {23, 0x02, 147},
+        {40, 0x03, 147},
+        {2, 0x02, 149},
+        {9, 0x02, 149},
+        {23, 0x02, 149},
+        {40, 0x03, 149},
+        {2, 0x02, 150},
+        {9, 0x02, 150},
+        {23, 0x02, 150},
+        {40, 0x03, 150},
+        {2, 0x02, 151},
+        {9, 0x02, 151},
+        {23, 0x02, 151},
+        {40, 0x03, 151},
+    },
+    /* 157 */
+    {
+        {3, 0x02, 147},
+        {6, 0x02, 147},
+        {10, 0x02, 147},
+        {15, 0x02, 147},
+        {24, 0x02, 147},
+        {31, 0x02, 147},
+        {41, 0x02, 147},
+        {56, 0x03, 147},
+        {3, 0x02, 149},
+        {6, 0x02, 149},
+        {10, 0x02, 149},
+        {15, 0x02, 149},
+        {24, 0x02, 149},
+        {31, 0x02, 149},
+        {41, 0x02, 149},
+        {56, 0x03, 149},
+    },
+    /* 158 */
+    {
+        {3, 0x02, 150},
+        {6, 0x02, 150},
+        {10, 0x02, 150},
+        {15, 0x02, 150},
+        {24, 0x02, 150},
+        {31, 0x02, 150},
+        {41, 0x02, 150},
+        {56, 0x03, 150},
+        {3, 0x02, 151},
+        {6, 0x02, 151},
+        {10, 0x02, 151},
+        {15, 0x02, 151},
+        {24, 0x02, 151},
+        {31, 0x02, 151},
+        {41, 0x02, 151},
+        {56, 0x03, 151},
+    },
+    /* 159 */
+    {
+        {2, 0x02, 152},
+        {9, 0x02, 152},
+        {23, 0x02, 152},
+        {40, 0x03, 152},
+        {2, 0x02, 155},
+        {9, 0x02, 155},
+        {23, 0x02, 155},
+        {40, 0x03, 155},
+        {2, 0x02, 157},
+        {9, 0x02, 157},
+        {23, 0x02, 157},
+        {40, 0x03, 157},
+        {2, 0x02, 158},
+        {9, 0x02, 158},
+        {23, 0x02, 158},
+        {40, 0x03, 158},
+    },
+    /* 160 */
+    {
+        {3, 0x02, 152},
+        {6, 0x02, 152},
+        {10, 0x02, 152},
+        {15, 0x02, 152},
+        {24, 0x02, 152},
+        {31, 0x02, 152},
+        {41, 0x02, 152},
+        {56, 0x03, 152},
+        {3, 0x02, 155},
+        {6, 0x02, 155},
+        {10, 0x02, 155},
+        {15, 0x02, 155},
+        {24, 0x02, 155},
+        {31, 0x02, 155},
+        {41, 0x02, 155},
+        {56, 0x03, 155},
+    },
+    /* 161 */
+    {
+        {3, 0x02, 157},
+        {6, 0x02, 157},
+        {10, 0x02, 157},
+        {15, 0x02, 157},
+        {24, 0x02, 157},
+        {31, 0x02, 157},
+        {41, 0x02, 157},
+        {56, 0x03, 157},
+        {3, 0x02, 158},
+        {6, 0x02, 158},
+        {10, 0x02, 158},
+        {15, 0x02, 158},
+        {24, 0x02, 158},
+        {31, 0x02, 158},
+        {41, 0x02, 158},
+        {56, 0x03, 158},
+    },
+    /* 162 */
+    {
+        {1, 0x02, 165},
+        {22, 0x03, 165},
+        {1, 0x02, 166},
+        {22, 0x03, 166},
+        {1, 0x02, 168},
+        {22, 0x03, 168},
+        {1, 0x02, 174},
+        {22, 0x03, 174},
+        {1, 0x02, 175},
+        {22, 0x03, 175},
+        {1, 0x02, 180},
+        {22, 0x03, 180},
+        {1, 0x02, 182},
+        {22, 0x03, 182},
+        {1, 0x02, 183},
+        {22, 0x03, 183},
+    },
+    /* 163 */
+    {
+        {2, 0x02, 165},
+        {9, 0x02, 165},
+        {23, 0x02, 165},
+        {40, 0x03, 165},
+        {2, 0x02, 166},
+        {9, 0x02, 166},
+        {23, 0x02, 166},
+        {40, 0x03, 166},
+        {2, 0x02, 168},
+        {9, 0x02, 168},
+        {23, 0x02, 168},
+        {40, 0x03, 168},
+        {2, 0x02, 174},
+        {9, 0x02, 174},
+        {23, 0x02, 174},
+        {40, 0x03, 174},
+    },
+    /* 164 */
+    {
+        {3, 0x02, 165},
+        {6, 0x02, 165},
+        {10, 0x02, 165},
+        {15, 0x02, 165},
+        {24, 0x02, 165},
+        {31, 0x02, 165},
+        {41, 0x02, 165},
+        {56, 0x03, 165},
+        {3, 0x02, 166},
+        {6, 0x02, 166},
+        {10, 0x02, 166},
+        {15, 0x02, 166},
+        {24, 0x02, 166},
+        {31, 0x02, 166},
+        {41, 0x02, 166},
+        {56, 0x03, 166},
+    },
+    /* 165 */
+    {
+        {3, 0x02, 168},
+        {6, 0x02, 168},
+        {10, 0x02, 168},
+        {15, 0x02, 168},
+        {24, 0x02, 168},
+        {31, 0x02, 168},
+        {41, 0x02, 168},
+        {56, 0x03, 168},
+        {3, 0x02, 174},
+        {6, 0x02, 174},
+        {10, 0x02, 174},
+        {15, 0x02, 174},
+        {24, 0x02, 174},
+        {31, 0x02, 174},
+        {41, 0x02, 174},
+        {56, 0x03, 174},
+    },
+    /* 166 */
+    {
+        {2, 0x02, 175},
+        {9, 0x02, 175},
+        {23, 0x02, 175},
+        {40, 0x03, 175},
+        {2, 0x02, 180},
+        {9, 0x02, 180},
+        {23, 0x02, 180},
+        {40, 0x03, 180},
+        {2, 0x02, 182},
+        {9, 0x02, 182},
+        {23, 0x02, 182},
+        {40, 0x03, 182},
+        {2, 0x02, 183},
+        {9, 0x02, 183},
+        {23, 0x02, 183},
+        {40, 0x03, 183},
+    },
+    /* 167 */
+    {
+        {3, 0x02, 175},
+        {6, 0x02, 175},
+        {10, 0x02, 175},
+        {15, 0x02, 175},
+        {24, 0x02, 175},
+        {31, 0x02, 175},
+        {41, 0x02, 175},
+        {56, 0x03, 175},
+        {3, 0x02, 180},
+        {6, 0x02, 180},
+        {10, 0x02, 180},
+        {15, 0x02, 180},
+        {24, 0x02, 180},
+        {31, 0x02, 180},
+        {41, 0x02, 180},
+        {56, 0x03, 180},
+    },
+    /* 168 */
+    {
+        {3, 0x02, 182},
+        {6, 0x02, 182},
+        {10, 0x02, 182},
+        {15, 0x02, 182},
+        {24, 0x02, 182},
+        {31, 0x02, 182},
+        {41, 0x02, 182},
+        {56, 0x03, 182},
+        {3, 0x02, 183},
+        {6, 0x02, 183},
+        {10, 0x02, 183},
+        {15, 0x02, 183},
+        {24, 0x02, 183},
+        {31, 0x02, 183},
+        {41, 0x02, 183},
+        {56, 0x03, 183},
+    },
+    /* 169 */
+    {
+        {0, 0x03, 188},
+        {0, 0x03, 191},
+        {0, 0x03, 197},
+        {0, 0x03, 231},
+        {0, 0x03, 239},
+        {176, 0x00, 0},
+        {178, 0x00, 0},
+        {179, 0x00, 0},
+        {183, 0x00, 0},
+        {184, 0x00, 0},
+        {186, 0x00, 0},
+        {187, 0x00, 0},
+        {192, 0x00, 0},
+        {199, 0x00, 0},
+        {208, 0x00, 0},
+        {223, 0x00, 0},
+    },
+    /* 170 */
+    {
+        {1, 0x02, 188},
+        {22, 0x03, 188},
+        {1, 0x02, 191},
+        {22, 0x03, 191},
+        {1, 0x02, 197},
+        {22, 0x03, 197},
+        {1, 0x02, 231},
+        {22, 0x03, 231},
+        {1, 0x02, 239},
+        {22, 0x03, 239},
+        {0, 0x03, 9},
+        {0, 0x03, 142},
+        {0, 0x03, 144},
+        {0, 0x03, 145},
+        {0, 0x03, 148},
+        {0, 0x03, 159},
+    },
+    /* 171 */
+    {
+        {2, 0x02, 188},
+        {9, 0x02, 188},
+        {23, 0x02, 188},
+        {40, 0x03, 188},
+        {2, 0x02, 191},
+        {9, 0x02, 191},
+        {23, 0x02, 191},
+        {40, 0x03, 191},
+        {2, 0x02, 197},
+        {9, 0x02, 197},
+        {23, 0x02, 197},
+        {40, 0x03, 197},
+        {2, 0x02, 231},
+        {9, 0x02, 231},
+        {23, 0x02, 231},
+        {40, 0x03, 231},
+    },
+    /* 172 */
+    {
+        {3, 0x02, 188},
+        {6, 0x02, 188},
+        {10, 0x02, 188},
+        {15, 0x02, 188},
+        {24, 0x02, 188},
+        {31, 0x02, 188},
+        {41, 0x02, 188},
+        {56, 0x03, 188},
+        {3, 0x02, 191},
+        {6, 0x02, 191},
+        {10, 0x02, 191},
+        {15, 0x02, 191},
+        {24, 0x02, 191},
+        {31, 0x02, 191},
+        {41, 0x02, 191},
+        {56, 0x03, 191},
+    },
+    /* 173 */
+    {
+        {3, 0x02, 197},
+        {6, 0x02, 197},
+        {10, 0x02, 197},
+        {15, 0x02, 197},
+        {24, 0x02, 197},
+        {31, 0x02, 197},
+        {41, 0x02, 197},
+        {56, 0x03, 197},
+        {3, 0x02, 231},
+        {6, 0x02, 231},
+        {10, 0x02, 231},
+        {15, 0x02, 231},
+        {24, 0x02, 231},
+        {31, 0x02, 231},
+        {41, 0x02, 231},
+        {56, 0x03, 231},
+    },
+    /* 174 */
+    {
+        {2, 0x02, 239},
+        {9, 0x02, 239},
+        {23, 0x02, 239},
+        {40, 0x03, 239},
+        {1, 0x02, 9},
+        {22, 0x03, 9},
+        {1, 0x02, 142},
+        {22, 0x03, 142},
+        {1, 0x02, 144},
+        {22, 0x03, 144},
+        {1, 0x02, 145},
+        {22, 0x03, 145},
+        {1, 0x02, 148},
+        {22, 0x03, 148},
+        {1, 0x02, 159},
+        {22, 0x03, 159},
+    },
+    /* 175 */
+    {
+        {3, 0x02, 239},
+        {6, 0x02, 239},
+        {10, 0x02, 239},
+        {15, 0x02, 239},
+        {24, 0x02, 239},
+        {31, 0x02, 239},
+        {41, 0x02, 239},
+        {56, 0x03, 239},
+        {2, 0x02, 9},
+        {9, 0x02, 9},
+        {23, 0x02, 9},
+        {40, 0x03, 9},
+        {2, 0x02, 142},
+        {9, 0x02, 142},
+        {23, 0x02, 142},
+        {40, 0x03, 142},
+    },
+    /* 176 */
+    {
+        {3, 0x02, 9},
+        {6, 0x02, 9},
+        {10, 0x02, 9},
+        {15, 0x02, 9},
+        {24, 0x02, 9},
+        {31, 0x02, 9},
+        {41, 0x02, 9},
+        {56, 0x03, 9},
+        {3, 0x02, 142},
+        {6, 0x02, 142},
+        {10, 0x02, 142},
+        {15, 0x02, 142},
+        {24, 0x02, 142},
+        {31, 0x02, 142},
+        {41, 0x02, 142},
+        {56, 0x03, 142},
+    },
+    /* 177 */
+    {
+        {2, 0x02, 144},
+        {9, 0x02, 144},
+        {23, 0x02, 144},
+        {40, 0x03, 144},
+        {2, 0x02, 145},
+        {9, 0x02, 145},
+        {23, 0x02, 145},
+        {40, 0x03, 145},
+        {2, 0x02, 148},
+        {9, 0x02, 148},
+        {23, 0x02, 148},
+        {40, 0x03, 148},
+        {2, 0x02, 159},
+        {9, 0x02, 159},
+        {23, 0x02, 159},
+        {40, 0x03, 159},
+    },
+    /* 178 */
+    {
+        {3, 0x02, 144},
+        {6, 0x02, 144},
+        {10, 0x02, 144},
+        {15, 0x02, 144},
+        {24, 0x02, 144},
+        {31, 0x02, 144},
+        {41, 0x02, 144},
+        {56, 0x03, 144},
+        {3, 0x02, 145},
+        {6, 0x02, 145},
+        {10, 0x02, 145},
+        {15, 0x02, 145},
+        {24, 0x02, 145},
+        {31, 0x02, 145},
+        {41, 0x02, 145},
+        {56, 0x03, 145},
+    },
+    /* 179 */
+    {
+        {3, 0x02, 148},
+        {6, 0x02, 148},
+        {10, 0x02, 148},
+        {15, 0x02, 148},
+        {24, 0x02, 148},
+        {31, 0x02, 148},
+        {41, 0x02, 148},
+        {56, 0x03, 148},
+        {3, 0x02, 159},
+        {6, 0x02, 159},
+        {10, 0x02, 159},
+        {15, 0x02, 159},
+        {24, 0x02, 159},
+        {31, 0x02, 159},
+        {41, 0x02, 159},
+        {56, 0x03, 159},
+    },
+    /* 180 */
+    {
+        {0, 0x03, 171},
+        {0, 0x03, 206},
+        {0, 0x03, 215},
+        {0, 0x03, 225},
+        {0, 0x03, 236},
+        {0, 0x03, 237},
+        {188, 0x00, 0},
+        {189, 0x00, 0},
+        {193, 0x00, 0},
+        {196, 0x00, 0},
+        {200, 0x00, 0},
+        {203, 0x00, 0},
+        {209, 0x00, 0},
+        {216, 0x00, 0},
+        {224, 0x00, 0},
+        {238, 0x00, 0},
+    },
+    /* 181 */
+    {
+        {1, 0x02, 171},
+        {22, 0x03, 171},
+        {1, 0x02, 206},
+        {22, 0x03, 206},
+        {1, 0x02, 215},
+        {22, 0x03, 215},
+        {1, 0x02, 225},
+        {22, 0x03, 225},
+        {1, 0x02, 236},
+        {22, 0x03, 236},
+        {1, 0x02, 237},
+        {22, 0x03, 237},
+        {0, 0x03, 199},
+        {0, 0x03, 207},
+        {0, 0x03, 234},
+        {0, 0x03, 235},
+    },
+    /* 182 */
+    {
+        {2, 0x02, 171},
+        {9, 0x02, 171},
+        {23, 0x02, 171},
+        {40, 0x03, 171},
+        {2, 0x02, 206},
+        {9, 0x02, 206},
+        {23, 0x02, 206},
+        {40, 0x03, 206},
+        {2, 0x02, 215},
+        {9, 0x02, 215},
+        {23, 0x02, 215},
+        {40, 0x03, 215},
+        {2, 0x02, 225},
+        {9, 0x02, 225},
+        {23, 0x02, 225},
+        {40, 0x03, 225},
+    },
+    /* 183 */
+    {
+        {3, 0x02, 171},
+        {6, 0x02, 171},
+        {10, 0x02, 171},
+        {15, 0x02, 171},
+        {24, 0x02, 171},
+        {31, 0x02, 171},
+        {41, 0x02, 171},
+        {56, 0x03, 171},
+        {3, 0x02, 206},
+        {6, 0x02, 206},
+        {10, 0x02, 206},
+        {15, 0x02, 206},
+        {24, 0x02, 206},
+        {31, 0x02, 206},
+        {41, 0x02, 206},
+        {56, 0x03, 206},
+    },
+    /* 184 */
+    {
+        {3, 0x02, 215},
+        {6, 0x02, 215},
+        {10, 0x02, 215},
+        {15, 0x02, 215},
+        {24, 0x02, 215},
+        {31, 0x02, 215},
+        {41, 0x02, 215},
+        {56, 0x03, 215},
+        {3, 0x02, 225},
+        {6, 0x02, 225},
+        {10, 0x02, 225},
+        {15, 0x02, 225},
+        {24, 0x02, 225},
+        {31, 0x02, 225},
+        {41, 0x02, 225},
+        {56, 0x03, 225},
+    },
+    /* 185 */
+    {
+        {2, 0x02, 236},
+        {9, 0x02, 236},
+        {23, 0x02, 236},
+        {40, 0x03, 236},
+        {2, 0x02, 237},
+        {9, 0x02, 237},
+        {23, 0x02, 237},
+        {40, 0x03, 237},
+        {1, 0x02, 199},
+        {22, 0x03, 199},
+        {1, 0x02, 207},
+        {22, 0x03, 207},
+        {1, 0x02, 234},
+        {22, 0x03, 234},
+        {1, 0x02, 235},
+        {22, 0x03, 235},
+    },
+    /* 186 */
+    {
+        {3, 0x02, 236},
+        {6, 0x02, 236},
+        {10, 0x02, 236},
+        {15, 0x02, 236},
+        {24, 0x02, 236},
+        {31, 0x02, 236},
+        {41, 0x02, 236},
+        {56, 0x03, 236},
+        {3, 0x02, 237},
+        {6, 0x02, 237},
+        {10, 0x02, 237},
+        {15, 0x02, 237},
+        {24, 0x02, 237},
+        {31, 0x02, 237},
+        {41, 0x02, 237},
+        {56, 0x03, 237},
+    },
+    /* 187 */
+    {
+        {2, 0x02, 199},
+        {9, 0x02, 199},
+        {23, 0x02, 199},
+        {40, 0x03, 199},
+        {2, 0x02, 207},
+        {9, 0x02, 207},
+        {23, 0x02, 207},
+        {40, 0x03, 207},
+        {2, 0x02, 234},
+        {9, 0x02, 234},
+        {23, 0x02, 234},
+        {40, 0x03, 234},
+        {2, 0x02, 235},
+        {9, 0x02, 235},
+        {23, 0x02, 235},
+        {40, 0x03, 235},
+    },
+    /* 188 */
+    {
+        {3, 0x02, 199},
+        {6, 0x02, 199},
+        {10, 0x02, 199},
+        {15, 0x02, 199},
+        {24, 0x02, 199},
+        {31, 0x02, 199},
+        {41, 0x02, 199},
+        {56, 0x03, 199},
+        {3, 0x02, 207},
+        {6, 0x02, 207},
+        {10, 0x02, 207},
+        {15, 0x02, 207},
+        {24, 0x02, 207},
+        {31, 0x02, 207},
+        {41, 0x02, 207},
+        {56, 0x03, 207},
+    },
+    /* 189 */
+    {
+        {3, 0x02, 234},
+        {6, 0x02, 234},
+        {10, 0x02, 234},
+        {15, 0x02, 234},
+        {24, 0x02, 234},
+        {31, 0x02, 234},
+        {41, 0x02, 234},
+        {56, 0x03, 234},
+        {3, 0x02, 235},
+        {6, 0x02, 235},
+        {10, 0x02, 235},
+        {15, 0x02, 235},
+        {24, 0x02, 235},
+        {31, 0x02, 235},
+        {41, 0x02, 235},
+        {56, 0x03, 235},
+    },
+    /* 190 */
+    {
+        {194, 0x00, 0},
+        {195, 0x00, 0},
+        {197, 0x00, 0},
+        {198, 0x00, 0},
+        {201, 0x00, 0},
+        {202, 0x00, 0},
+        {204, 0x00, 0},
+        {205, 0x00, 0},
+        {210, 0x00, 0},
+        {213, 0x00, 0},
+        {217, 0x00, 0},
+        {220, 0x00, 0},
+        {225, 0x00, 0},
+        {231, 0x00, 0},
+        {239, 0x00, 0},
+        {246, 0x00, 0},
+    },
+    /* 191 */
+    {
+        {0, 0x03, 192},
+        {0, 0x03, 193},
+        {0, 0x03, 200},
+        {0, 0x03, 201},
+        {0, 0x03, 202},
+        {0, 0x03, 205},
+        {0, 0x03, 210},
+        {0, 0x03, 213},
+        {0, 0x03, 218},
+        {0, 0x03, 219},
+        {0, 0x03, 238},
+        {0, 0x03, 240},
+        {0, 0x03, 242},
+        {0, 0x03, 243},
+        {0, 0x03, 255},
+        {206, 0x00, 0},
+    },
+    /* 192 */
+    {
+        {1, 0x02, 192},
+        {22, 0x03, 192},
+        {1, 0x02, 193},
+        {22, 0x03, 193},
+        {1, 0x02, 200},
+        {22, 0x03, 200},
+        {1, 0x02, 201},
+        {22, 0x03, 201},
+        {1, 0x02, 202},
+        {22, 0x03, 202},
+        {1, 0x02, 205},
+        {22, 0x03, 205},
+        {1, 0x02, 210},
+        {22, 0x03, 210},
+        {1, 0x02, 213},
+        {22, 0x03, 213},
+    },
+    /* 193 */
+    {
+        {2, 0x02, 192},
+        {9, 0x02, 192},
+        {23, 0x02, 192},
+        {40, 0x03, 192},
+        {2, 0x02, 193},
+        {9, 0x02, 193},
+        {23, 0x02, 193},
+        {40, 0x03, 193},
+        {2, 0x02, 200},
+        {9, 0x02, 200},
+        {23, 0x02, 200},
+        {40, 0x03, 200},
+        {2, 0x02, 201},
+        {9, 0x02, 201},
+        {23, 0x02, 201},
+        {40, 0x03, 201},
+    },
+    /* 194 */
+    {
+        {3, 0x02, 192},
+        {6, 0x02, 192},
+        {10, 0x02, 192},
+        {15, 0x02, 192},
+        {24, 0x02, 192},
+        {31, 0x02, 192},
+        {41, 0x02, 192},
+        {56, 0x03, 192},
+        {3, 0x02, 193},
+        {6, 0x02, 193},
+        {10, 0x02, 193},
+        {15, 0x02, 193},
+        {24, 0x02, 193},
+        {31, 0x02, 193},
+        {41, 0x02, 193},
+        {56, 0x03, 193},
+    },
+    /* 195 */
+    {
+        {3, 0x02, 200},
+        {6, 0x02, 200},
+        {10, 0x02, 200},
+        {15, 0x02, 200},
+        {24, 0x02, 200},
+        {31, 0x02, 200},
+        {41, 0x02, 200},
+        {56, 0x03, 200},
+        {3, 0x02, 201},
+        {6, 0x02, 201},
+        {10, 0x02, 201},
+        {15, 0x02, 201},
+        {24, 0x02, 201},
+        {31, 0x02, 201},
+        {41, 0x02, 201},
+        {56, 0x03, 201},
+    },
+    /* 196 */
+    {
+        {2, 0x02, 202},
+        {9, 0x02, 202},
+        {23, 0x02, 202},
+        {40, 0x03, 202},
+        {2, 0x02, 205},
+        {9, 0x02, 205},
+        {23, 0x02, 205},
+        {40, 0x03, 205},
+        {2, 0x02, 210},
+        {9, 0x02, 210},
+        {23, 0x02, 210},
+        {40, 0x03, 210},
+        {2, 0x02, 213},
+        {9, 0x02, 213},
+        {23, 0x02, 213},
+        {40, 0x03, 213},
+    },
+    /* 197 */
+    {
+        {3, 0x02, 202},
+        {6, 0x02, 202},
+        {10, 0x02, 202},
+        {15, 0x02, 202},
+        {24, 0x02, 202},
+        {31, 0x02, 202},
+        {41, 0x02, 202},
+        {56, 0x03, 202},
+        {3, 0x02, 205},
+        {6, 0x02, 205},
+        {10, 0x02, 205},
+        {15, 0x02, 205},
+        {24, 0x02, 205},
+        {31, 0x02, 205},
+        {41, 0x02, 205},
+        {56, 0x03, 205},
+    },
+    /* 198 */
+    {
+        {3, 0x02, 210},
+        {6, 0x02, 210},
+        {10, 0x02, 210},
+        {15, 0x02, 210},
+        {24, 0x02, 210},
+        {31, 0x02, 210},
+        {41, 0x02, 210},
+        {56, 0x03, 210},
+        {3, 0x02, 213},
+        {6, 0x02, 213},
+        {10, 0x02, 213},
+        {15, 0x02, 213},
+        {24, 0x02, 213},
+        {31, 0x02, 213},
+        {41, 0x02, 213},
+        {56, 0x03, 213},
+    },
+    /* 199 */
+    {
+        {1, 0x02, 218},
+        {22, 0x03, 218},
+        {1, 0x02, 219},
+        {22, 0x03, 219},
+        {1, 0x02, 238},
+        {22, 0x03, 238},
+        {1, 0x02, 240},
+        {22, 0x03, 240},
+        {1, 0x02, 242},
+        {22, 0x03, 242},
+        {1, 0x02, 243},
+        {22, 0x03, 243},
+        {1, 0x02, 255},
+        {22, 0x03, 255},
+        {0, 0x03, 203},
+        {0, 0x03, 204},
+    },
+    /* 200 */
+    {
+        {2, 0x02, 218},
+        {9, 0x02, 218},
+        {23, 0x02, 218},
+        {40, 0x03, 218},
+        {2, 0x02, 219},
+        {9, 0x02, 219},
+        {23, 0x02, 219},
+        {40, 0x03, 219},
+        {2, 0x02, 238},
+        {9, 0x02, 238},
+        {23, 0x02, 238},
+        {40, 0x03, 238},
+        {2, 0x02, 240},
+        {9, 0x02, 240},
+        {23, 0x02, 240},
+        {40, 0x03, 240},
+    },
+    /* 201 */
+    {
+        {3, 0x02, 218},
+        {6, 0x02, 218},
+        {10, 0x02, 218},
+        {15, 0x02, 218},
+        {24, 0x02, 218},
+        {31, 0x02, 218},
+        {41, 0x02, 218},
+        {56, 0x03, 218},
+        {3, 0x02, 219},
+        {6, 0x02, 219},
+        {10, 0x02, 219},
+        {15, 0x02, 219},
+        {24, 0x02, 219},
+        {31, 0x02, 219},
+        {41, 0x02, 219},
+        {56, 0x03, 219},
+    },
+    /* 202 */
+    {
+        {3, 0x02, 238},
+        {6, 0x02, 238},
+        {10, 0x02, 238},
+        {15, 0x02, 238},
+        {24, 0x02, 238},
+        {31, 0x02, 238},
+        {41, 0x02, 238},
+        {56, 0x03, 238},
+        {3, 0x02, 240},
+        {6, 0x02, 240},
+        {10, 0x02, 240},
+        {15, 0x02, 240},
+        {24, 0x02, 240},
+        {31, 0x02, 240},
+        {41, 0x02, 240},
+        {56, 0x03, 240},
+    },
+    /* 203 */
+    {
+        {2, 0x02, 242},
+        {9, 0x02, 242},
+        {23, 0x02, 242},
+        {40, 0x03, 242},
+        {2, 0x02, 243},
+        {9, 0x02, 243},
+        {23, 0x02, 243},
+        {40, 0x03, 243},
+        {2, 0x02, 255},
+        {9, 0x02, 255},
+        {23, 0x02, 255},
+        {40, 0x03, 255},
+        {1, 0x02, 203},
+        {22, 0x03, 203},
+        {1, 0x02, 204},
+        {22, 0x03, 204},
+    },
+    /* 204 */
+    {
+        {3, 0x02, 242},
+        {6, 0x02, 242},
+        {10, 0x02, 242},
+        {15, 0x02, 242},
+        {24, 0x02, 242},
+        {31, 0x02, 242},
+        {41, 0x02, 242},
+        {56, 0x03, 242},
+        {3, 0x02, 243},
+        {6, 0x02, 243},
+        {10, 0x02, 243},
+        {15, 0x02, 243},
+        {24, 0x02, 243},
+        {31, 0x02, 243},
+        {41, 0x02, 243},
+        {56, 0x03, 243},
+    },
+    /* 205 */
+    {
+        {3, 0x02, 255},
+        {6, 0x02, 255},
+        {10, 0x02, 255},
+        {15, 0x02, 255},
+        {24, 0x02, 255},
+        {31, 0x02, 255},
+        {41, 0x02, 255},
+        {56, 0x03, 255},
+        {2, 0x02, 203},
+        {9, 0x02, 203},
+        {23, 0x02, 203},
+        {40, 0x03, 203},
+        {2, 0x02, 204},
+        {9, 0x02, 204},
+        {23, 0x02, 204},
+        {40, 0x03, 204},
+    },
+    /* 206 */
+    {
+        {3, 0x02, 203},
+        {6, 0x02, 203},
+        {10, 0x02, 203},
+        {15, 0x02, 203},
+        {24, 0x02, 203},
+        {31, 0x02, 203},
+        {41, 0x02, 203},
+        {56, 0x03, 203},
+        {3, 0x02, 204},
+        {6, 0x02, 204},
+        {10, 0x02, 204},
+        {15, 0x02, 204},
+        {24, 0x02, 204},
+        {31, 0x02, 204},
+        {41, 0x02, 204},
+        {56, 0x03, 204},
+    },
+    /* 207 */
+    {
+        {211, 0x00, 0},
+        {212, 0x00, 0},
+        {214, 0x00, 0},
+        {215, 0x00, 0},
+        {218, 0x00, 0},
+        {219, 0x00, 0},
+        {221, 0x00, 0},
+        {222, 0x00, 0},
+        {226, 0x00, 0},
+        {228, 0x00, 0},
+        {232, 0x00, 0},
+        {235, 0x00, 0},
+        {240, 0x00, 0},
+        {243, 0x00, 0},
+        {247, 0x00, 0},
+        {250, 0x00, 0},
+    },
+    /* 208 */
+    {
+        {0, 0x03, 211},
+        {0, 0x03, 212},
+        {0, 0x03, 214},
+        {0, 0x03, 221},
+        {0, 0x03, 222},
+        {0, 0x03, 223},
+        {0, 0x03, 241},
+        {0, 0x03, 244},
+        {0, 0x03, 245},
+        {0, 0x03, 246},
+        {0, 0x03, 247},
+        {0, 0x03, 248},
+        {0, 0x03, 250},
+        {0, 0x03, 251},
+        {0, 0x03, 252},
+        {0, 0x03, 253},
+    },
+    /* 209 */
+    {
+        {1, 0x02, 211},
+        {22, 0x03, 211},
+        {1, 0x02, 212},
+        {22, 0x03, 212},
+        {1, 0x02, 214},
+        {22, 0x03, 214},
+        {1, 0x02, 221},
+        {22, 0x03, 221},
+        {1, 0x02, 222},
+        {22, 0x03, 222},
+        {1, 0x02, 223},
+        {22, 0x03, 223},
+        {1, 0x02, 241},
+        {22, 0x03, 241},
+        {1, 0x02, 244},
+        {22, 0x03, 244},
+    },
+    /* 210 */
+    {
+        {2, 0x02, 211},
+        {9, 0x02, 211},
+        {23, 0x02, 211},
+        {40, 0x03, 211},
+        {2, 0x02, 212},
+        {9, 0x02, 212},
+        {23, 0x02, 212},
+        {40, 0x03, 212},
+        {2, 0x02, 214},
+        {9, 0x02, 214},
+        {23, 0x02, 214},
+        {40, 0x03, 214},
+        {2, 0x02, 221},
+        {9, 0x02, 221},
+        {23, 0x02, 221},
+        {40, 0x03, 221},
+    },
+    /* 211 */
+    {
+        {3, 0x02, 211},
+        {6, 0x02, 211},
+        {10, 0x02, 211},
+        {15, 0x02, 211},
+        {24, 0x02, 211},
+        {31, 0x02, 211},
+        {41, 0x02, 211},
+        {56, 0x03, 211},
+        {3, 0x02, 212},
+        {6, 0x02, 212},
+        {10, 0x02, 212},
+        {15, 0x02, 212},
+        {24, 0x02, 212},
+        {31, 0x02, 212},
+        {41, 0x02, 212},
+        {56, 0x03, 212},
+    },
+    /* 212 */
+    {
+        {3, 0x02, 214},
+        {6, 0x02, 214},
+        {10, 0x02, 214},
+        {15, 0x02, 214},
+        {24, 0x02, 214},
+        {31, 0x02, 214},
+        {41, 0x02, 214},
+        {56, 0x03, 214},
+        {3, 0x02, 221},
+        {6, 0x02, 221},
+        {10, 0x02, 221},
+        {15, 0x02, 221},
+        {24, 0x02, 221},
+        {31, 0x02, 221},
+        {41, 0x02, 221},
+        {56, 0x03, 221},
+    },
+    /* 213 */
+    {
+        {2, 0x02, 222},
+        {9, 0x02, 222},
+        {23, 0x02, 222},
+        {40, 0x03, 222},
+        {2, 0x02, 223},
+        {9, 0x02, 223},
+        {23, 0x02, 223},
+        {40, 0x03, 223},
+        {2, 0x02, 241},
+        {9, 0x02, 241},
+        {23, 0x02, 241},
+        {40, 0x03, 241},
+        {2, 0x02, 244},
+        {9, 0x02, 244},
+        {23, 0x02, 244},
+        {40, 0x03, 244},
+    },
+    /* 214 */
+    {
+        {3, 0x02, 222},
+        {6, 0x02, 222},
+        {10, 0x02, 222},
+        {15, 0x02, 222},
+        {24, 0x02, 222},
+        {31, 0x02, 222},
+        {41, 0x02, 222},
+        {56, 0x03, 222},
+        {3, 0x02, 223},
+        {6, 0x02, 223},
+        {10, 0x02, 223},
+        {15, 0x02, 223},
+        {24, 0x02, 223},
+        {31, 0x02, 223},
+        {41, 0x02, 223},
+        {56, 0x03, 223},
+    },
+    /* 215 */
+    {
+        {3, 0x02, 241},
+        {6, 0x02, 241},
+        {10, 0x02, 241},
+        {15, 0x02, 241},
+        {24, 0x02, 241},
+        {31, 0x02, 241},
+        {41, 0x02, 241},
+        {56, 0x03, 241},
+        {3, 0x02, 244},
+        {6, 0x02, 244},
+        {10, 0x02, 244},
+        {15, 0x02, 244},
+        {24, 0x02, 244},
+        {31, 0x02, 244},
+        {41, 0x02, 244},
+        {56, 0x03, 244},
+    },
+    /* 216 */
+    {
+        {1, 0x02, 245},
+        {22, 0x03, 245},
+        {1, 0x02, 246},
+        {22, 0x03, 246},
+        {1, 0x02, 247},
+        {22, 0x03, 247},
+        {1, 0x02, 248},
+        {22, 0x03, 248},
+        {1, 0x02, 250},
+        {22, 0x03, 250},
+        {1, 0x02, 251},
+        {22, 0x03, 251},
+        {1, 0x02, 252},
+        {22, 0x03, 252},
+        {1, 0x02, 253},
+        {22, 0x03, 253},
+    },
+    /* 217 */
+    {
+        {2, 0x02, 245},
+        {9, 0x02, 245},
+        {23, 0x02, 245},
+        {40, 0x03, 245},
+        {2, 0x02, 246},
+        {9, 0x02, 246},
+        {23, 0x02, 246},
+        {40, 0x03, 246},
+        {2, 0x02, 247},
+        {9, 0x02, 247},
+        {23, 0x02, 247},
+        {40, 0x03, 247},
+        {2, 0x02, 248},
+        {9, 0x02, 248},
+        {23, 0x02, 248},
+        {40, 0x03, 248},
+    },
+    /* 218 */
+    {
+        {3, 0x02, 245},
+        {6, 0x02, 245},
+        {10, 0x02, 245},
+        {15, 0x02, 245},
+        {24, 0x02, 245},
+        {31, 0x02, 245},
+        {41, 0x02, 245},
+        {56, 0x03, 245},
+        {3, 0x02, 246},
+        {6, 0x02, 246},
+        {10, 0x02, 246},
+        {15, 0x02, 246},
+        {24, 0x02, 246},
+        {31, 0x02, 246},
+        {41, 0x02, 246},
+        {56, 0x03, 246},
+    },
+    /* 219 */
+    {
+        {3, 0x02, 247},
+        {6, 0x02, 247},
+        {10, 0x02, 247},
+        {15, 0x02, 247},
+        {24, 0x02, 247},
+        {31, 0x02, 247},
+        {41, 0x02, 247},
+        {56, 0x03, 247},
+        {3, 0x02, 248},
+        {6, 0x02, 248},
+        {10, 0x02, 248},
+        {15, 0x02, 248},
+        {24, 0x02, 248},
+        {31, 0x02, 248},
+        {41, 0x02, 248},
+        {56, 0x03, 248},
+    },
+    /* 220 */
+    {
+        {2, 0x02, 250},
+        {9, 0x02, 250},
+        {23, 0x02, 250},
+        {40, 0x03, 250},
+        {2, 0x02, 251},
+        {9, 0x02, 251},
+        {23, 0x02, 251},
+        {40, 0x03, 251},
+        {2, 0x02, 252},
+        {9, 0x02, 252},
+        {23, 0x02, 252},
+        {40, 0x03, 252},
+        {2, 0x02, 253},
+        {9, 0x02, 253},
+        {23, 0x02, 253},
+        {40, 0x03, 253},
+    },
+    /* 221 */
+    {
+        {3, 0x02, 250},
+        {6, 0x02, 250},
+        {10, 0x02, 250},
+        {15, 0x02, 250},
+        {24, 0x02, 250},
+        {31, 0x02, 250},
+        {41, 0x02, 250},
+        {56, 0x03, 250},
+        {3, 0x02, 251},
+        {6, 0x02, 251},
+        {10, 0x02, 251},
+        {15, 0x02, 251},
+        {24, 0x02, 251},
+        {31, 0x02, 251},
+        {41, 0x02, 251},
+        {56, 0x03, 251},
+    },
+    /* 222 */
+    {
+        {3, 0x02, 252},
+        {6, 0x02, 252},
+        {10, 0x02, 252},
+        {15, 0x02, 252},
+        {24, 0x02, 252},
+        {31, 0x02, 252},
+        {41, 0x02, 252},
+        {56, 0x03, 252},
+        {3, 0x02, 253},
+        {6, 0x02, 253},
+        {10, 0x02, 253},
+        {15, 0x02, 253},
+        {24, 0x02, 253},
+        {31, 0x02, 253},
+        {41, 0x02, 253},
+        {56, 0x03, 253},
+    },
+    /* 223 */
+    {
+        {0, 0x03, 254},
+        {227, 0x00, 0},
+        {229, 0x00, 0},
+        {230, 0x00, 0},
+        {233, 0x00, 0},
+        {234, 0x00, 0},
+        {236, 0x00, 0},
+        {237, 0x00, 0},
+        {241, 0x00, 0},
+        {242, 0x00, 0},
+        {244, 0x00, 0},
+        {245, 0x00, 0},
+        {248, 0x00, 0},
+        {249, 0x00, 0},
+        {251, 0x00, 0},
+        {252, 0x00, 0},
+    },
+    /* 224 */
+    {
+        {1, 0x02, 254},
+        {22, 0x03, 254},
+        {0, 0x03, 2},
+        {0, 0x03, 3},
+        {0, 0x03, 4},
+        {0, 0x03, 5},
+        {0, 0x03, 6},
+        {0, 0x03, 7},
+        {0, 0x03, 8},
+        {0, 0x03, 11},
+        {0, 0x03, 12},
+        {0, 0x03, 14},
+        {0, 0x03, 15},
+        {0, 0x03, 16},
+        {0, 0x03, 17},
+        {0, 0x03, 18},
+    },
+    /* 225 */
+    {
+        {2, 0x02, 254},
+        {9, 0x02, 254},
+        {23, 0x02, 254},
+        {40, 0x03, 254},
+        {1, 0x02, 2},
+        {22, 0x03, 2},
+        {1, 0x02, 3},
+        {22, 0x03, 3},
+        {1, 0x02, 4},
+        {22, 0x03, 4},
+        {1, 0x02, 5},
+        {22, 0x03, 5},
+        {1, 0x02, 6},
+        {22, 0x03, 6},
+        {1, 0x02, 7},
+        {22, 0x03, 7},
+    },
+    /* 226 */
+    {
+        {3, 0x02, 254},
+        {6, 0x02, 254},
+        {10, 0x02, 254},
+        {15, 0x02, 254},
+        {24, 0x02, 254},
+        {31, 0x02, 254},
+        {41, 0x02, 254},
+        {56, 0x03, 254},
+        {2, 0x02, 2},
+        {9, 0x02, 2},
+        {23, 0x02, 2},
+        {40, 0x03, 2},
+        {2, 0x02, 3},
+        {9, 0x02, 3},
+        {23, 0x02, 3},
+        {40, 0x03, 3},
+    },
+    /* 227 */
+    {
+        {3, 0x02, 2},
+        {6, 0x02, 2},
+        {10, 0x02, 2},
+        {15, 0x02, 2},
+        {24, 0x02, 2},
+        {31, 0x02, 2},
+        {41, 0x02, 2},
+        {56, 0x03, 2},
+        {3, 0x02, 3},
+        {6, 0x02, 3},
+        {10, 0x02, 3},
+        {15, 0x02, 3},
+        {24, 0x02, 3},
+        {31, 0x02, 3},
+        {41, 0x02, 3},
+        {56, 0x03, 3},
+    },
+    /* 228 */
+    {
+        {2, 0x02, 4},
+        {9, 0x02, 4},
+        {23, 0x02, 4},
+        {40, 0x03, 4},
+        {2, 0x02, 5},
+        {9, 0x02, 5},
+        {23, 0x02, 5},
+        {40, 0x03, 5},
+        {2, 0x02, 6},
+        {9, 0x02, 6},
+        {23, 0x02, 6},
+        {40, 0x03, 6},
+        {2, 0x02, 7},
+        {9, 0x02, 7},
+        {23, 0x02, 7},
+        {40, 0x03, 7},
+    },
+    /* 229 */
+    {
+        {3, 0x02, 4},
+        {6, 0x02, 4},
+        {10, 0x02, 4},
+        {15, 0x02, 4},
+        {24, 0x02, 4},
+        {31, 0x02, 4},
+        {41, 0x02, 4},
+        {56, 0x03, 4},
+        {3, 0x02, 5},
+        {6, 0x02, 5},
+        {10, 0x02, 5},
+        {15, 0x02, 5},
+        {24, 0x02, 5},
+        {31, 0x02, 5},
+        {41, 0x02, 5},
+        {56, 0x03, 5},
+    },
+    /* 230 */
+    {
+        {3, 0x02, 6},
+        {6, 0x02, 6},
+        {10, 0x02, 6},
+        {15, 0x02, 6},
+        {24, 0x02, 6},
+        {31, 0x02, 6},
+        {41, 0x02, 6},
+        {56, 0x03, 6},
+        {3, 0x02, 7},
+        {6, 0x02, 7},
+        {10, 0x02, 7},
+        {15, 0x02, 7},
+        {24, 0x02, 7},
+        {31, 0x02, 7},
+        {41, 0x02, 7},
+        {56, 0x03, 7},
+    },
+    /* 231 */
+    {
+        {1, 0x02, 8},
+        {22, 0x03, 8},
+        {1, 0x02, 11},
+        {22, 0x03, 11},
+        {1, 0x02, 12},
+        {22, 0x03, 12},
+        {1, 0x02, 14},
+        {22, 0x03, 14},
+        {1, 0x02, 15},
+        {22, 0x03, 15},
+        {1, 0x02, 16},
+        {22, 0x03, 16},
+        {1, 0x02, 17},
+        {22, 0x03, 17},
+        {1, 0x02, 18},
+        {22, 0x03, 18},
+    },
+    /* 232 */
+    {
+        {2, 0x02, 8},
+        {9, 0x02, 8},
+        {23, 0x02, 8},
+        {40, 0x03, 8},
+        {2, 0x02, 11},
+        {9, 0x02, 11},
+        {23, 0x02, 11},
+        {40, 0x03, 11},
+        {2, 0x02, 12},
+        {9, 0x02, 12},
+        {23, 0x02, 12},
+        {40, 0x03, 12},
+        {2, 0x02, 14},
+        {9, 0x02, 14},
+        {23, 0x02, 14},
+        {40, 0x03, 14},
+    },
+    /* 233 */
+    {
+        {3, 0x02, 8},
+        {6, 0x02, 8},
+        {10, 0x02, 8},
+        {15, 0x02, 8},
+        {24, 0x02, 8},
+        {31, 0x02, 8},
+        {41, 0x02, 8},
+        {56, 0x03, 8},
+        {3, 0x02, 11},
+        {6, 0x02, 11},
+        {10, 0x02, 11},
+        {15, 0x02, 11},
+        {24, 0x02, 11},
+        {31, 0x02, 11},
+        {41, 0x02, 11},
+        {56, 0x03, 11},
+    },
+    /* 234 */
+    {
+        {3, 0x02, 12},
+        {6, 0x02, 12},
+        {10, 0x02, 12},
+        {15, 0x02, 12},
+        {24, 0x02, 12},
+        {31, 0x02, 12},
+        {41, 0x02, 12},
+        {56, 0x03, 12},
+        {3, 0x02, 14},
+        {6, 0x02, 14},
+        {10, 0x02, 14},
+        {15, 0x02, 14},
+        {24, 0x02, 14},
+        {31, 0x02, 14},
+        {41, 0x02, 14},
+        {56, 0x03, 14},
+    },
+    /* 235 */
+    {
+        {2, 0x02, 15},
+        {9, 0x02, 15},
+        {23, 0x02, 15},
+        {40, 0x03, 15},
+        {2, 0x02, 16},
+        {9, 0x02, 16},
+        {23, 0x02, 16},
+        {40, 0x03, 16},
+        {2, 0x02, 17},
+        {9, 0x02, 17},
+        {23, 0x02, 17},
+        {40, 0x03, 17},
+        {2, 0x02, 18},
+        {9, 0x02, 18},
+        {23, 0x02, 18},
+        {40, 0x03, 18},
+    },
+    /* 236 */
+    {
+        {3, 0x02, 15},
+        {6, 0x02, 15},
+        {10, 0x02, 15},
+        {15, 0x02, 15},
+        {24, 0x02, 15},
+        {31, 0x02, 15},
+        {41, 0x02, 15},
+        {56, 0x03, 15},
+        {3, 0x02, 16},
+        {6, 0x02, 16},
+        {10, 0x02, 16},
+        {15, 0x02, 16},
+        {24, 0x02, 16},
+        {31, 0x02, 16},
+        {41, 0x02, 16},
+        {56, 0x03, 16},
+    },
+    /* 237 */
+    {
+        {3, 0x02, 17},
+        {6, 0x02, 17},
+        {10, 0x02, 17},
+        {15, 0x02, 17},
+        {24, 0x02, 17},
+        {31, 0x02, 17},
+        {41, 0x02, 17},
+        {56, 0x03, 17},
+        {3, 0x02, 18},
+        {6, 0x02, 18},
+        {10, 0x02, 18},
+        {15, 0x02, 18},
+        {24, 0x02, 18},
+        {31, 0x02, 18},
+        {41, 0x02, 18},
+        {56, 0x03, 18},
+    },
+    /* 238 */
+    {
+        {0, 0x03, 19},
+        {0, 0x03, 20},
+        {0, 0x03, 21},
+        {0, 0x03, 23},
+        {0, 0x03, 24},
+        {0, 0x03, 25},
+        {0, 0x03, 26},
+        {0, 0x03, 27},
+        {0, 0x03, 28},
+        {0, 0x03, 29},
+        {0, 0x03, 30},
+        {0, 0x03, 31},
+        {0, 0x03, 127},
+        {0, 0x03, 220},
+        {0, 0x03, 249},
+        {253, 0x00, 0},
+    },
+    /* 239 */
+    {
+        {1, 0x02, 19},
+        {22, 0x03, 19},
+        {1, 0x02, 20},
+        {22, 0x03, 20},
+        {1, 0x02, 21},
+        {22, 0x03, 21},
+        {1, 0x02, 23},
+        {22, 0x03, 23},
+        {1, 0x02, 24},
+        {22, 0x03, 24},
+        {1, 0x02, 25},
+        {22, 0x03, 25},
+        {1, 0x02, 26},
+        {22, 0x03, 26},
+        {1, 0x02, 27},
+        {22, 0x03, 27},
+    },
+    /* 240 */
+    {
+        {2, 0x02, 19},
+        {9, 0x02, 19},
+        {23, 0x02, 19},
+        {40, 0x03, 19},
+        {2, 0x02, 20},
+        {9, 0x02, 20},
+        {23, 0x02, 20},
+        {40, 0x03, 20},
+        {2, 0x02, 21},
+        {9, 0x02, 21},
+        {23, 0x02, 21},
+        {40, 0x03, 21},
+        {2, 0x02, 23},
+        {9, 0x02, 23},
+        {23, 0x02, 23},
+        {40, 0x03, 23},
+    },
+    /* 241 */
+    {
+        {3, 0x02, 19},
+        {6, 0x02, 19},
+        {10, 0x02, 19},
+        {15, 0x02, 19},
+        {24, 0x02, 19},
+        {31, 0x02, 19},
+        {41, 0x02, 19},
+        {56, 0x03, 19},
+        {3, 0x02, 20},
+        {6, 0x02, 20},
+        {10, 0x02, 20},
+        {15, 0x02, 20},
+        {24, 0x02, 20},
+        {31, 0x02, 20},
+        {41, 0x02, 20},
+        {56, 0x03, 20},
+    },
+    /* 242 */
+    {
+        {3, 0x02, 21},
+        {6, 0x02, 21},
+        {10, 0x02, 21},
+        {15, 0x02, 21},
+        {24, 0x02, 21},
+        {31, 0x02, 21},
+        {41, 0x02, 21},
+        {56, 0x03, 21},
+        {3, 0x02, 23},
+        {6, 0x02, 23},
+        {10, 0x02, 23},
+        {15, 0x02, 23},
+        {24, 0x02, 23},
+        {31, 0x02, 23},
+        {41, 0x02, 23},
+        {56, 0x03, 23},
+    },
+    /* 243 */
+    {
+        {2, 0x02, 24},
+        {9, 0x02, 24},
+        {23, 0x02, 24},
+        {40, 0x03, 24},
+        {2, 0x02, 25},
+        {9, 0x02, 25},
+        {23, 0x02, 25},
+        {40, 0x03, 25},
+        {2, 0x02, 26},
+        {9, 0x02, 26},
+        {23, 0x02, 26},
+        {40, 0x03, 26},
+        {2, 0x02, 27},
+        {9, 0x02, 27},
+        {23, 0x02, 27},
+        {40, 0x03, 27},
+    },
+    /* 244 */
+    {
+        {3, 0x02, 24},
+        {6, 0x02, 24},
+        {10, 0x02, 24},
+        {15, 0x02, 24},
+        {24, 0x02, 24},
+        {31, 0x02, 24},
+        {41, 0x02, 24},
+        {56, 0x03, 24},
+        {3, 0x02, 25},
+        {6, 0x02, 25},
+        {10, 0x02, 25},
+        {15, 0x02, 25},
+        {24, 0x02, 25},
+        {31, 0x02, 25},
+        {41, 0x02, 25},
+        {56, 0x03, 25},
+    },
+    /* 245 */
+    {
+        {3, 0x02, 26},
+        {6, 0x02, 26},
+        {10, 0x02, 26},
+        {15, 0x02, 26},
+        {24, 0x02, 26},
+        {31, 0x02, 26},
+        {41, 0x02, 26},
+        {56, 0x03, 26},
+        {3, 0x02, 27},
+        {6, 0x02, 27},
+        {10, 0x02, 27},
+        {15, 0x02, 27},
+        {24, 0x02, 27},
+        {31, 0x02, 27},
+        {41, 0x02, 27},
+        {56, 0x03, 27},
+    },
+    /* 246 */
+    {
+        {1, 0x02, 28},
+        {22, 0x03, 28},
+        {1, 0x02, 29},
+        {22, 0x03, 29},
+        {1, 0x02, 30},
+        {22, 0x03, 30},
+        {1, 0x02, 31},
+        {22, 0x03, 31},
+        {1, 0x02, 127},
+        {22, 0x03, 127},
+        {1, 0x02, 220},
+        {22, 0x03, 220},
+        {1, 0x02, 249},
+        {22, 0x03, 249},
+        {254, 0x00, 0},
+        {255, 0x00, 0},
+    },
+    /* 247 */
+    {
+        {2, 0x02, 28},
+        {9, 0x02, 28},
+        {23, 0x02, 28},
+        {40, 0x03, 28},
+        {2, 0x02, 29},
+        {9, 0x02, 29},
+        {23, 0x02, 29},
+        {40, 0x03, 29},
+        {2, 0x02, 30},
+        {9, 0x02, 30},
+        {23, 0x02, 30},
+        {40, 0x03, 30},
+        {2, 0x02, 31},
+        {9, 0x02, 31},
+        {23, 0x02, 31},
+        {40, 0x03, 31},
+    },
+    /* 248 */
+    {
+        {3, 0x02, 28},
+        {6, 0x02, 28},
+        {10, 0x02, 28},
+        {15, 0x02, 28},
+        {24, 0x02, 28},
+        {31, 0x02, 28},
+        {41, 0x02, 28},
+        {56, 0x03, 28},
+        {3, 0x02, 29},
+        {6, 0x02, 29},
+        {10, 0x02, 29},
+        {15, 0x02, 29},
+        {24, 0x02, 29},
+        {31, 0x02, 29},
+        {41, 0x02, 29},
+        {56, 0x03, 29},
+    },
+    /* 249 */
+    {
+        {3, 0x02, 30},
+        {6, 0x02, 30},
+        {10, 0x02, 30},
+        {15, 0x02, 30},
+        {24, 0x02, 30},
+        {31, 0x02, 30},
+        {41, 0x02, 30},
+        {56, 0x03, 30},
+        {3, 0x02, 31},
+        {6, 0x02, 31},
+        {10, 0x02, 31},
+        {15, 0x02, 31},
+        {24, 0x02, 31},
+        {31, 0x02, 31},
+        {41, 0x02, 31},
+        {56, 0x03, 31},
+    },
+    /* 250 */
+    {
+        {2, 0x02, 127},
+        {9, 0x02, 127},
+        {23, 0x02, 127},
+        {40, 0x03, 127},
+        {2, 0x02, 220},
+        {9, 0x02, 220},
+        {23, 0x02, 220},
+        {40, 0x03, 220},
+        {2, 0x02, 249},
+        {9, 0x02, 249},
+        {23, 0x02, 249},
+        {40, 0x03, 249},
+        {0, 0x03, 10},
+        {0, 0x03, 13},
+        {0, 0x03, 22},
+        {0, 0x04, 0},
+    },
+    /* 251 */
+    {
+        {3, 0x02, 127},
+        {6, 0x02, 127},
+        {10, 0x02, 127},
+        {15, 0x02, 127},
+        {24, 0x02, 127},
+        {31, 0x02, 127},
+        {41, 0x02, 127},
+        {56, 0x03, 127},
+        {3, 0x02, 220},
+        {6, 0x02, 220},
+        {10, 0x02, 220},
+        {15, 0x02, 220},
+        {24, 0x02, 220},
+        {31, 0x02, 220},
+        {41, 0x02, 220},
+        {56, 0x03, 220},
+    },
+    /* 252 */
+    {
+        {3, 0x02, 249},
+        {6, 0x02, 249},
+        {10, 0x02, 249},
+        {15, 0x02, 249},
+        {24, 0x02, 249},
+        {31, 0x02, 249},
+        {41, 0x02, 249},
+        {56, 0x03, 249},
+        {1, 0x02, 10},
+        {22, 0x03, 10},
+        {1, 0x02, 13},
+        {22, 0x03, 13},
+        {1, 0x02, 22},
+        {22, 0x03, 22},
+        {0, 0x04, 0},
+        {0, 0x04, 0},
+    },
+    /* 253 */
+    {
+        {2, 0x02, 10},
+        {9, 0x02, 10},
+        {23, 0x02, 10},
+        {40, 0x03, 10},
+        {2, 0x02, 13},
+        {9, 0x02, 13},
+        {23, 0x02, 13},
+        {40, 0x03, 13},
+        {2, 0x02, 22},
+        {9, 0x02, 22},
+        {23, 0x02, 22},
+        {40, 0x03, 22},
+        {0, 0x04, 0},
+        {0, 0x04, 0},
+        {0, 0x04, 0},
+        {0, 0x04, 0},
+    },
+    /* 254 */
+    {
+        {3, 0x02, 10},
+        {6, 0x02, 10},
+        {10, 0x02, 10},
+        {15, 0x02, 10},
+        {24, 0x02, 10},
+        {31, 0x02, 10},
+        {41, 0x02, 10},
+        {56, 0x03, 10},
+        {3, 0x02, 13},
+        {6, 0x02, 13},
+        {10, 0x02, 13},
+        {15, 0x02, 13},
+        {24, 0x02, 13},
+        {31, 0x02, 13},
+        {41, 0x02, 13},
+        {56, 0x03, 13},
+    },
+    /* 255 */
+    {
+        {3, 0x02, 22},
+        {6, 0x02, 22},
+        {10, 0x02, 22},
+        {15, 0x02, 22},
+        {24, 0x02, 22},
+        {31, 0x02, 22},
+        {41, 0x02, 22},
+        {56, 0x03, 22},
+        {0, 0x04, 0},
+        {0, 0x04, 0},
+        {0, 0x04, 0},
+        {0, 0x04, 0},
+        {0, 0x04, 0},
+        {0, 0x04, 0},
+        {0, 0x04, 0},
+        {0, 0x04, 0},
+    },
+};

+ 532 - 0
iotkit-embedded/external_libs/nghttp2/nghttp2_helper.c

@@ -0,0 +1,532 @@
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
+ */
+
+
+#include "nghttp2_helper.h"
+
+#include <assert.h>
+#include <string.h>
+
+#include "nghttp2_net.h"
+
+void nghttp2_put_uint16be(uint8_t *buf, uint16_t n) {
+  uint16_t x = nghttp2_htons(n);
+  memcpy(buf, &x, sizeof(uint16_t));
+}
+
+void nghttp2_put_uint32be(uint8_t *buf, uint32_t n) {
+  uint32_t x = nghttp2_htonl(n);
+  memcpy(buf, &x, sizeof(uint32_t));
+}
+
+uint16_t nghttp2_get_uint16(const uint8_t *data) {
+  uint16_t n;
+  memcpy(&n, data, sizeof(uint16_t));
+  return nghttp2_ntohs(n);
+}
+
+uint32_t nghttp2_get_uint32(const uint8_t *data) {
+  uint32_t n;
+  memcpy(&n, data, sizeof(uint32_t));
+  return nghttp2_ntohl(n);
+}
+
+/* Generated by gendowncasetbl.py */
+static const uint8_t DOWNCASE_TBL[] = {
+    0 /* NUL  */,   1 /* SOH  */,   2 /* STX  */,   3 /* ETX  */,
+    4 /* EOT  */,   5 /* ENQ  */,   6 /* ACK  */,   7 /* BEL  */,
+    8 /* BS   */,   9 /* HT   */,   10 /* LF   */,  11 /* VT   */,
+    12 /* FF   */,  13 /* CR   */,  14 /* SO   */,  15 /* SI   */,
+    16 /* DLE  */,  17 /* DC1  */,  18 /* DC2  */,  19 /* DC3  */,
+    20 /* DC4  */,  21 /* NAK  */,  22 /* SYN  */,  23 /* ETB  */,
+    24 /* CAN  */,  25 /* EM   */,  26 /* SUB  */,  27 /* ESC  */,
+    28 /* FS   */,  29 /* GS   */,  30 /* RS   */,  31 /* US   */,
+    32 /* SPC  */,  33 /* !    */,  34 /* "    */,  35 /* #    */,
+    36 /* $    */,  37 /* %    */,  38 /* &    */,  39 /* '    */,
+    40 /* (    */,  41 /* )    */,  42 /* *    */,  43 /* +    */,
+    44 /* ,    */,  45 /* -    */,  46 /* .    */,  47 /* /    */,
+    48 /* 0    */,  49 /* 1    */,  50 /* 2    */,  51 /* 3    */,
+    52 /* 4    */,  53 /* 5    */,  54 /* 6    */,  55 /* 7    */,
+    56 /* 8    */,  57 /* 9    */,  58 /* :    */,  59 /* ;    */,
+    60 /* <    */,  61 /* =    */,  62 /* >    */,  63 /* ?    */,
+    64 /* @    */,  97 /* A    */,  98 /* B    */,  99 /* C    */,
+    100 /* D    */, 101 /* E    */, 102 /* F    */, 103 /* G    */,
+    104 /* H    */, 105 /* I    */, 106 /* J    */, 107 /* K    */,
+    108 /* L    */, 109 /* M    */, 110 /* N    */, 111 /* O    */,
+    112 /* P    */, 113 /* Q    */, 114 /* R    */, 115 /* S    */,
+    116 /* T    */, 117 /* U    */, 118 /* V    */, 119 /* W    */,
+    120 /* X    */, 121 /* Y    */, 122 /* Z    */, 91 /* [    */,
+    92 /* \    */,  93 /* ]    */,  94 /* ^    */,  95 /* _    */,
+    96 /* `    */,  97 /* a    */,  98 /* b    */,  99 /* c    */,
+    100 /* d    */, 101 /* e    */, 102 /* f    */, 103 /* g    */,
+    104 /* h    */, 105 /* i    */, 106 /* j    */, 107 /* k    */,
+    108 /* l    */, 109 /* m    */, 110 /* n    */, 111 /* o    */,
+    112 /* p    */, 113 /* q    */, 114 /* r    */, 115 /* s    */,
+    116 /* t    */, 117 /* u    */, 118 /* v    */, 119 /* w    */,
+    120 /* x    */, 121 /* y    */, 122 /* z    */, 123 /* {    */,
+    124 /* |    */, 125 /* }    */, 126 /* ~    */, 127 /* DEL  */,
+    128 /* 0x80 */, 129 /* 0x81 */, 130 /* 0x82 */, 131 /* 0x83 */,
+    132 /* 0x84 */, 133 /* 0x85 */, 134 /* 0x86 */, 135 /* 0x87 */,
+    136 /* 0x88 */, 137 /* 0x89 */, 138 /* 0x8a */, 139 /* 0x8b */,
+    140 /* 0x8c */, 141 /* 0x8d */, 142 /* 0x8e */, 143 /* 0x8f */,
+    144 /* 0x90 */, 145 /* 0x91 */, 146 /* 0x92 */, 147 /* 0x93 */,
+    148 /* 0x94 */, 149 /* 0x95 */, 150 /* 0x96 */, 151 /* 0x97 */,
+    152 /* 0x98 */, 153 /* 0x99 */, 154 /* 0x9a */, 155 /* 0x9b */,
+    156 /* 0x9c */, 157 /* 0x9d */, 158 /* 0x9e */, 159 /* 0x9f */,
+    160 /* 0xa0 */, 161 /* 0xa1 */, 162 /* 0xa2 */, 163 /* 0xa3 */,
+    164 /* 0xa4 */, 165 /* 0xa5 */, 166 /* 0xa6 */, 167 /* 0xa7 */,
+    168 /* 0xa8 */, 169 /* 0xa9 */, 170 /* 0xaa */, 171 /* 0xab */,
+    172 /* 0xac */, 173 /* 0xad */, 174 /* 0xae */, 175 /* 0xaf */,
+    176 /* 0xb0 */, 177 /* 0xb1 */, 178 /* 0xb2 */, 179 /* 0xb3 */,
+    180 /* 0xb4 */, 181 /* 0xb5 */, 182 /* 0xb6 */, 183 /* 0xb7 */,
+    184 /* 0xb8 */, 185 /* 0xb9 */, 186 /* 0xba */, 187 /* 0xbb */,
+    188 /* 0xbc */, 189 /* 0xbd */, 190 /* 0xbe */, 191 /* 0xbf */,
+    192 /* 0xc0 */, 193 /* 0xc1 */, 194 /* 0xc2 */, 195 /* 0xc3 */,
+    196 /* 0xc4 */, 197 /* 0xc5 */, 198 /* 0xc6 */, 199 /* 0xc7 */,
+    200 /* 0xc8 */, 201 /* 0xc9 */, 202 /* 0xca */, 203 /* 0xcb */,
+    204 /* 0xcc */, 205 /* 0xcd */, 206 /* 0xce */, 207 /* 0xcf */,
+    208 /* 0xd0 */, 209 /* 0xd1 */, 210 /* 0xd2 */, 211 /* 0xd3 */,
+    212 /* 0xd4 */, 213 /* 0xd5 */, 214 /* 0xd6 */, 215 /* 0xd7 */,
+    216 /* 0xd8 */, 217 /* 0xd9 */, 218 /* 0xda */, 219 /* 0xdb */,
+    220 /* 0xdc */, 221 /* 0xdd */, 222 /* 0xde */, 223 /* 0xdf */,
+    224 /* 0xe0 */, 225 /* 0xe1 */, 226 /* 0xe2 */, 227 /* 0xe3 */,
+    228 /* 0xe4 */, 229 /* 0xe5 */, 230 /* 0xe6 */, 231 /* 0xe7 */,
+    232 /* 0xe8 */, 233 /* 0xe9 */, 234 /* 0xea */, 235 /* 0xeb */,
+    236 /* 0xec */, 237 /* 0xed */, 238 /* 0xee */, 239 /* 0xef */,
+    240 /* 0xf0 */, 241 /* 0xf1 */, 242 /* 0xf2 */, 243 /* 0xf3 */,
+    244 /* 0xf4 */, 245 /* 0xf5 */, 246 /* 0xf6 */, 247 /* 0xf7 */,
+    248 /* 0xf8 */, 249 /* 0xf9 */, 250 /* 0xfa */, 251 /* 0xfb */,
+    252 /* 0xfc */, 253 /* 0xfd */, 254 /* 0xfe */, 255 /* 0xff */,
+};
+
+void nghttp2_downcase(uint8_t *s, size_t len) {
+  size_t i;
+  for (i = 0; i < len; ++i) {
+    s[i] = DOWNCASE_TBL[s[i]];
+  }
+}
+
+/*
+ *   local_window_size
+ *   ^  *
+ *   |  *    recv_window_size
+ *   |  *  * ^
+ *   |  *  * |
+ *  0+++++++++
+ *   |  *  *   \
+ *   |  *  *   | This rage is hidden in flow control.  But it must be
+ *   v  *  *   / kept in order to restore it when window size is enlarged.
+ *   recv_reduction
+ *   (+ for negative direction)
+ *
+ *   recv_window_size could be negative if we decrease
+ *   local_window_size more than recv_window_size:
+ *
+ *   local_window_size
+ *   ^  *
+ *   |  *
+ *   |  *
+ *   0++++++++
+ *   |  *    ^ recv_window_size (negative)
+ *   |  *    |
+ *   v  *  *
+ *   recv_reduction
+ */
+int nghttp2_adjust_local_window_size(int32_t *local_window_size_ptr,
+                                     int32_t *recv_window_size_ptr,
+                                     int32_t *recv_reduction_ptr,
+                                     int32_t *delta_ptr) {
+  if (*delta_ptr > 0) {
+    int32_t recv_reduction_delta;
+    int32_t delta;
+    int32_t new_recv_window_size =
+        nghttp2_max(0, *recv_window_size_ptr) - *delta_ptr;
+
+    if (new_recv_window_size >= 0) {
+      *recv_window_size_ptr = new_recv_window_size;
+      return 0;
+    }
+
+    delta = -new_recv_window_size;
+
+    /* The delta size is strictly more than received bytes. Increase
+       local_window_size by that difference |delta|. */
+    if (*local_window_size_ptr > NGHTTP2_MAX_WINDOW_SIZE - delta) {
+      return NGHTTP2_ERR_FLOW_CONTROL;
+    }
+    *local_window_size_ptr += delta;
+    /* If there is recv_reduction due to earlier window_size
+       reduction, we have to adjust it too. */
+    recv_reduction_delta = nghttp2_min(*recv_reduction_ptr, delta);
+    *recv_reduction_ptr -= recv_reduction_delta;
+    if (*recv_window_size_ptr < 0) {
+      *recv_window_size_ptr += recv_reduction_delta;
+    } else {
+      /* If *recv_window_size_ptr > 0, then those bytes are going to
+         be returned to the remote peer (by WINDOW_UPDATE with the
+         adjusted *delta_ptr), so it is effectively 0 now.  We set to
+         *recv_reduction_delta, because caller does not take into
+         account it in *delta_ptr. */
+      *recv_window_size_ptr = recv_reduction_delta;
+    }
+    /* recv_reduction_delta must be paid from *delta_ptr, since it was
+       added in window size reduction (see below). */
+    *delta_ptr -= recv_reduction_delta;
+
+    return 0;
+  }
+
+  if (*local_window_size_ptr + *delta_ptr < 0 ||
+      *recv_window_size_ptr < INT32_MIN - *delta_ptr ||
+      *recv_reduction_ptr > INT32_MAX + *delta_ptr) {
+    return NGHTTP2_ERR_FLOW_CONTROL;
+  }
+  /* Decreasing local window size. Note that we achieve this without
+     noticing to the remote peer. To do this, we cut
+     recv_window_size by -delta. This means that we don't send
+     WINDOW_UPDATE for -delta bytes. */
+  *local_window_size_ptr += *delta_ptr;
+  *recv_window_size_ptr += *delta_ptr;
+  *recv_reduction_ptr -= *delta_ptr;
+  *delta_ptr = 0;
+
+  return 0;
+}
+
+int nghttp2_increase_local_window_size(int32_t *local_window_size_ptr,
+                                       int32_t *recv_window_size_ptr,
+                                       int32_t *recv_reduction_ptr,
+                                       int32_t *delta_ptr) {
+  int32_t recv_reduction_delta;
+  int32_t delta;
+
+  delta = *delta_ptr;
+
+  assert(delta >= 0);
+
+  /* The delta size is strictly more than received bytes. Increase
+     local_window_size by that difference |delta|. */
+  if (*local_window_size_ptr > NGHTTP2_MAX_WINDOW_SIZE - delta) {
+    return NGHTTP2_ERR_FLOW_CONTROL;
+  }
+
+  *local_window_size_ptr += delta;
+  /* If there is recv_reduction due to earlier window_size
+     reduction, we have to adjust it too. */
+  recv_reduction_delta = nghttp2_min(*recv_reduction_ptr, delta);
+  *recv_reduction_ptr -= recv_reduction_delta;
+
+  *recv_window_size_ptr += recv_reduction_delta;
+
+  /* recv_reduction_delta must be paid from *delta_ptr, since it was
+     added in window size reduction (see below). */
+  *delta_ptr -= recv_reduction_delta;
+
+  return 0;
+}
+
+int nghttp2_should_send_window_update(int32_t local_window_size,
+                                      int32_t recv_window_size) {
+  return recv_window_size > 0 && recv_window_size >= local_window_size / 2;
+}
+
+const char *nghttp2_strerror(int error_code) {
+  switch (error_code) {
+  case 0:
+    return "Success";
+  case NGHTTP2_ERR_INVALID_ARGUMENT:
+    return "Invalid argument";
+  case NGHTTP2_ERR_BUFFER_ERROR:
+    return "Out of buffer space";
+  case NGHTTP2_ERR_UNSUPPORTED_VERSION:
+    return "Unsupported SPDY version";
+  case NGHTTP2_ERR_WOULDBLOCK:
+    return "Operation would block";
+  case NGHTTP2_ERR_PROTO:
+    return "Protocol error";
+  case NGHTTP2_ERR_INVALID_FRAME:
+    return "Invalid frame octets";
+  case NGHTTP2_ERR_EOF:
+    return "EOF";
+  case NGHTTP2_ERR_DEFERRED:
+    return "Data transfer deferred";
+  case NGHTTP2_ERR_STREAM_ID_NOT_AVAILABLE:
+    return "No more Stream ID available";
+  case NGHTTP2_ERR_STREAM_CLOSED:
+    return "Stream was already closed or invalid";
+  case NGHTTP2_ERR_STREAM_CLOSING:
+    return "Stream is closing";
+  case NGHTTP2_ERR_STREAM_SHUT_WR:
+    return "The transmission is not allowed for this stream";
+  case NGHTTP2_ERR_INVALID_STREAM_ID:
+    return "Stream ID is invalid";
+  case NGHTTP2_ERR_INVALID_STREAM_STATE:
+    return "Invalid stream state";
+  case NGHTTP2_ERR_DEFERRED_DATA_EXIST:
+    return "Another DATA frame has already been deferred";
+  case NGHTTP2_ERR_START_STREAM_NOT_ALLOWED:
+    return "request HEADERS is not allowed";
+  case NGHTTP2_ERR_GOAWAY_ALREADY_SENT:
+    return "GOAWAY has already been sent";
+  case NGHTTP2_ERR_INVALID_HEADER_BLOCK:
+    return "Invalid header block";
+  case NGHTTP2_ERR_INVALID_STATE:
+    return "Invalid state";
+  case NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE:
+    return "The user callback function failed due to the temporal error";
+  case NGHTTP2_ERR_FRAME_SIZE_ERROR:
+    return "The length of the frame is invalid";
+  case NGHTTP2_ERR_HEADER_COMP:
+    return "Header compression/decompression error";
+  case NGHTTP2_ERR_FLOW_CONTROL:
+    return "Flow control error";
+  case NGHTTP2_ERR_INSUFF_BUFSIZE:
+    return "Insufficient buffer size given to function";
+  case NGHTTP2_ERR_PAUSE:
+    return "Callback was paused by the application";
+  case NGHTTP2_ERR_TOO_MANY_INFLIGHT_SETTINGS:
+    return "Too many inflight SETTINGS";
+  case NGHTTP2_ERR_PUSH_DISABLED:
+    return "Server push is disabled by peer";
+  case NGHTTP2_ERR_DATA_EXIST:
+    return "DATA or HEADERS frame has already been submitted for the stream";
+  case NGHTTP2_ERR_SESSION_CLOSING:
+    return "The current session is closing";
+  case NGHTTP2_ERR_HTTP_HEADER:
+    return "Invalid HTTP header field was received";
+  case NGHTTP2_ERR_HTTP_MESSAGING:
+    return "Violation in HTTP messaging rule";
+  case NGHTTP2_ERR_REFUSED_STREAM:
+    return "Stream was refused";
+  case NGHTTP2_ERR_INTERNAL:
+    return "Internal error";
+  case NGHTTP2_ERR_CANCEL:
+    return "Cancel";
+  case NGHTTP2_ERR_SETTINGS_EXPECTED:
+    return "When a local endpoint expects to receive SETTINGS frame, it "
+           "receives an other type of frame";
+  case NGHTTP2_ERR_NOMEM:
+    return "Out of memory";
+  case NGHTTP2_ERR_CALLBACK_FAILURE:
+    return "The user callback function failed";
+  case NGHTTP2_ERR_BAD_CLIENT_MAGIC:
+    return "Received bad client magic byte string";
+  case NGHTTP2_ERR_FLOODED:
+    return "Flooding was detected in this HTTP/2 session, and it must be "
+           "closed";
+  default:
+    return "Unknown error code";
+  }
+}
+
+/* Generated by gennmchartbl.py */
+static int VALID_HD_NAME_CHARS[] = {
+    0 /* NUL  */, 0 /* SOH  */, 0 /* STX  */, 0 /* ETX  */,
+    0 /* EOT  */, 0 /* ENQ  */, 0 /* ACK  */, 0 /* BEL  */,
+    0 /* BS   */, 0 /* HT   */, 0 /* LF   */, 0 /* VT   */,
+    0 /* FF   */, 0 /* CR   */, 0 /* SO   */, 0 /* SI   */,
+    0 /* DLE  */, 0 /* DC1  */, 0 /* DC2  */, 0 /* DC3  */,
+    0 /* DC4  */, 0 /* NAK  */, 0 /* SYN  */, 0 /* ETB  */,
+    0 /* CAN  */, 0 /* EM   */, 0 /* SUB  */, 0 /* ESC  */,
+    0 /* FS   */, 0 /* GS   */, 0 /* RS   */, 0 /* US   */,
+    0 /* SPC  */, 1 /* !    */, 0 /* "    */, 1 /* #    */,
+    1 /* $    */, 1 /* %    */, 1 /* &    */, 1 /* '    */,
+    0 /* (    */, 0 /* )    */, 1 /* *    */, 1 /* +    */,
+    0 /* ,    */, 1 /* -    */, 1 /* .    */, 0 /* /    */,
+    1 /* 0    */, 1 /* 1    */, 1 /* 2    */, 1 /* 3    */,
+    1 /* 4    */, 1 /* 5    */, 1 /* 6    */, 1 /* 7    */,
+    1 /* 8    */, 1 /* 9    */, 0 /* :    */, 0 /* ;    */,
+    0 /* <    */, 0 /* =    */, 0 /* >    */, 0 /* ?    */,
+    0 /* @    */, 0 /* A    */, 0 /* B    */, 0 /* C    */,
+    0 /* D    */, 0 /* E    */, 0 /* F    */, 0 /* G    */,
+    0 /* H    */, 0 /* I    */, 0 /* J    */, 0 /* K    */,
+    0 /* L    */, 0 /* M    */, 0 /* N    */, 0 /* O    */,
+    0 /* P    */, 0 /* Q    */, 0 /* R    */, 0 /* S    */,
+    0 /* T    */, 0 /* U    */, 0 /* V    */, 0 /* W    */,
+    0 /* X    */, 0 /* Y    */, 0 /* Z    */, 0 /* [    */,
+    0 /* \    */, 0 /* ]    */, 1 /* ^    */, 1 /* _    */,
+    1 /* `    */, 1 /* a    */, 1 /* b    */, 1 /* c    */,
+    1 /* d    */, 1 /* e    */, 1 /* f    */, 1 /* g    */,
+    1 /* h    */, 1 /* i    */, 1 /* j    */, 1 /* k    */,
+    1 /* l    */, 1 /* m    */, 1 /* n    */, 1 /* o    */,
+    1 /* p    */, 1 /* q    */, 1 /* r    */, 1 /* s    */,
+    1 /* t    */, 1 /* u    */, 1 /* v    */, 1 /* w    */,
+    1 /* x    */, 1 /* y    */, 1 /* z    */, 0 /* {    */,
+    1 /* |    */, 0 /* }    */, 1 /* ~    */, 0 /* DEL  */,
+    0 /* 0x80 */, 0 /* 0x81 */, 0 /* 0x82 */, 0 /* 0x83 */,
+    0 /* 0x84 */, 0 /* 0x85 */, 0 /* 0x86 */, 0 /* 0x87 */,
+    0 /* 0x88 */, 0 /* 0x89 */, 0 /* 0x8a */, 0 /* 0x8b */,
+    0 /* 0x8c */, 0 /* 0x8d */, 0 /* 0x8e */, 0 /* 0x8f */,
+    0 /* 0x90 */, 0 /* 0x91 */, 0 /* 0x92 */, 0 /* 0x93 */,
+    0 /* 0x94 */, 0 /* 0x95 */, 0 /* 0x96 */, 0 /* 0x97 */,
+    0 /* 0x98 */, 0 /* 0x99 */, 0 /* 0x9a */, 0 /* 0x9b */,
+    0 /* 0x9c */, 0 /* 0x9d */, 0 /* 0x9e */, 0 /* 0x9f */,
+    0 /* 0xa0 */, 0 /* 0xa1 */, 0 /* 0xa2 */, 0 /* 0xa3 */,
+    0 /* 0xa4 */, 0 /* 0xa5 */, 0 /* 0xa6 */, 0 /* 0xa7 */,
+    0 /* 0xa8 */, 0 /* 0xa9 */, 0 /* 0xaa */, 0 /* 0xab */,
+    0 /* 0xac */, 0 /* 0xad */, 0 /* 0xae */, 0 /* 0xaf */,
+    0 /* 0xb0 */, 0 /* 0xb1 */, 0 /* 0xb2 */, 0 /* 0xb3 */,
+    0 /* 0xb4 */, 0 /* 0xb5 */, 0 /* 0xb6 */, 0 /* 0xb7 */,
+    0 /* 0xb8 */, 0 /* 0xb9 */, 0 /* 0xba */, 0 /* 0xbb */,
+    0 /* 0xbc */, 0 /* 0xbd */, 0 /* 0xbe */, 0 /* 0xbf */,
+    0 /* 0xc0 */, 0 /* 0xc1 */, 0 /* 0xc2 */, 0 /* 0xc3 */,
+    0 /* 0xc4 */, 0 /* 0xc5 */, 0 /* 0xc6 */, 0 /* 0xc7 */,
+    0 /* 0xc8 */, 0 /* 0xc9 */, 0 /* 0xca */, 0 /* 0xcb */,
+    0 /* 0xcc */, 0 /* 0xcd */, 0 /* 0xce */, 0 /* 0xcf */,
+    0 /* 0xd0 */, 0 /* 0xd1 */, 0 /* 0xd2 */, 0 /* 0xd3 */,
+    0 /* 0xd4 */, 0 /* 0xd5 */, 0 /* 0xd6 */, 0 /* 0xd7 */,
+    0 /* 0xd8 */, 0 /* 0xd9 */, 0 /* 0xda */, 0 /* 0xdb */,
+    0 /* 0xdc */, 0 /* 0xdd */, 0 /* 0xde */, 0 /* 0xdf */,
+    0 /* 0xe0 */, 0 /* 0xe1 */, 0 /* 0xe2 */, 0 /* 0xe3 */,
+    0 /* 0xe4 */, 0 /* 0xe5 */, 0 /* 0xe6 */, 0 /* 0xe7 */,
+    0 /* 0xe8 */, 0 /* 0xe9 */, 0 /* 0xea */, 0 /* 0xeb */,
+    0 /* 0xec */, 0 /* 0xed */, 0 /* 0xee */, 0 /* 0xef */,
+    0 /* 0xf0 */, 0 /* 0xf1 */, 0 /* 0xf2 */, 0 /* 0xf3 */,
+    0 /* 0xf4 */, 0 /* 0xf5 */, 0 /* 0xf6 */, 0 /* 0xf7 */,
+    0 /* 0xf8 */, 0 /* 0xf9 */, 0 /* 0xfa */, 0 /* 0xfb */,
+    0 /* 0xfc */, 0 /* 0xfd */, 0 /* 0xfe */, 0 /* 0xff */
+};
+
+int nghttp2_check_header_name(const uint8_t *name, size_t len) {
+  const uint8_t *last;
+  if (len == 0) {
+    return 0;
+  }
+  if (*name == ':') {
+    if (len == 1) {
+      return 0;
+    }
+    ++name;
+    --len;
+  }
+  for (last = name + len; name != last; ++name) {
+    if (!VALID_HD_NAME_CHARS[*name]) {
+      return 0;
+    }
+  }
+  return 1;
+}
+
+/* Generated by genvchartbl.py */
+static int VALID_HD_VALUE_CHARS[] = {
+    0 /* NUL  */, 0 /* SOH  */, 0 /* STX  */, 0 /* ETX  */,
+    0 /* EOT  */, 0 /* ENQ  */, 0 /* ACK  */, 0 /* BEL  */,
+    0 /* BS   */, 1 /* HT   */, 0 /* LF   */, 0 /* VT   */,
+    0 /* FF   */, 0 /* CR   */, 0 /* SO   */, 0 /* SI   */,
+    0 /* DLE  */, 0 /* DC1  */, 0 /* DC2  */, 0 /* DC3  */,
+    0 /* DC4  */, 0 /* NAK  */, 0 /* SYN  */, 0 /* ETB  */,
+    0 /* CAN  */, 0 /* EM   */, 0 /* SUB  */, 0 /* ESC  */,
+    0 /* FS   */, 0 /* GS   */, 0 /* RS   */, 0 /* US   */,
+    1 /* SPC  */, 1 /* !    */, 1 /* "    */, 1 /* #    */,
+    1 /* $    */, 1 /* %    */, 1 /* &    */, 1 /* '    */,
+    1 /* (    */, 1 /* )    */, 1 /* *    */, 1 /* +    */,
+    1 /* ,    */, 1 /* -    */, 1 /* .    */, 1 /* /    */,
+    1 /* 0    */, 1 /* 1    */, 1 /* 2    */, 1 /* 3    */,
+    1 /* 4    */, 1 /* 5    */, 1 /* 6    */, 1 /* 7    */,
+    1 /* 8    */, 1 /* 9    */, 1 /* :    */, 1 /* ;    */,
+    1 /* <    */, 1 /* =    */, 1 /* >    */, 1 /* ?    */,
+    1 /* @    */, 1 /* A    */, 1 /* B    */, 1 /* C    */,
+    1 /* D    */, 1 /* E    */, 1 /* F    */, 1 /* G    */,
+    1 /* H    */, 1 /* I    */, 1 /* J    */, 1 /* K    */,
+    1 /* L    */, 1 /* M    */, 1 /* N    */, 1 /* O    */,
+    1 /* P    */, 1 /* Q    */, 1 /* R    */, 1 /* S    */,
+    1 /* T    */, 1 /* U    */, 1 /* V    */, 1 /* W    */,
+    1 /* X    */, 1 /* Y    */, 1 /* Z    */, 1 /* [    */,
+    1 /* \    */, 1 /* ]    */, 1 /* ^    */, 1 /* _    */,
+    1 /* `    */, 1 /* a    */, 1 /* b    */, 1 /* c    */,
+    1 /* d    */, 1 /* e    */, 1 /* f    */, 1 /* g    */,
+    1 /* h    */, 1 /* i    */, 1 /* j    */, 1 /* k    */,
+    1 /* l    */, 1 /* m    */, 1 /* n    */, 1 /* o    */,
+    1 /* p    */, 1 /* q    */, 1 /* r    */, 1 /* s    */,
+    1 /* t    */, 1 /* u    */, 1 /* v    */, 1 /* w    */,
+    1 /* x    */, 1 /* y    */, 1 /* z    */, 1 /* {    */,
+    1 /* |    */, 1 /* }    */, 1 /* ~    */, 0 /* DEL  */,
+    1 /* 0x80 */, 1 /* 0x81 */, 1 /* 0x82 */, 1 /* 0x83 */,
+    1 /* 0x84 */, 1 /* 0x85 */, 1 /* 0x86 */, 1 /* 0x87 */,
+    1 /* 0x88 */, 1 /* 0x89 */, 1 /* 0x8a */, 1 /* 0x8b */,
+    1 /* 0x8c */, 1 /* 0x8d */, 1 /* 0x8e */, 1 /* 0x8f */,
+    1 /* 0x90 */, 1 /* 0x91 */, 1 /* 0x92 */, 1 /* 0x93 */,
+    1 /* 0x94 */, 1 /* 0x95 */, 1 /* 0x96 */, 1 /* 0x97 */,
+    1 /* 0x98 */, 1 /* 0x99 */, 1 /* 0x9a */, 1 /* 0x9b */,
+    1 /* 0x9c */, 1 /* 0x9d */, 1 /* 0x9e */, 1 /* 0x9f */,
+    1 /* 0xa0 */, 1 /* 0xa1 */, 1 /* 0xa2 */, 1 /* 0xa3 */,
+    1 /* 0xa4 */, 1 /* 0xa5 */, 1 /* 0xa6 */, 1 /* 0xa7 */,
+    1 /* 0xa8 */, 1 /* 0xa9 */, 1 /* 0xaa */, 1 /* 0xab */,
+    1 /* 0xac */, 1 /* 0xad */, 1 /* 0xae */, 1 /* 0xaf */,
+    1 /* 0xb0 */, 1 /* 0xb1 */, 1 /* 0xb2 */, 1 /* 0xb3 */,
+    1 /* 0xb4 */, 1 /* 0xb5 */, 1 /* 0xb6 */, 1 /* 0xb7 */,
+    1 /* 0xb8 */, 1 /* 0xb9 */, 1 /* 0xba */, 1 /* 0xbb */,
+    1 /* 0xbc */, 1 /* 0xbd */, 1 /* 0xbe */, 1 /* 0xbf */,
+    1 /* 0xc0 */, 1 /* 0xc1 */, 1 /* 0xc2 */, 1 /* 0xc3 */,
+    1 /* 0xc4 */, 1 /* 0xc5 */, 1 /* 0xc6 */, 1 /* 0xc7 */,
+    1 /* 0xc8 */, 1 /* 0xc9 */, 1 /* 0xca */, 1 /* 0xcb */,
+    1 /* 0xcc */, 1 /* 0xcd */, 1 /* 0xce */, 1 /* 0xcf */,
+    1 /* 0xd0 */, 1 /* 0xd1 */, 1 /* 0xd2 */, 1 /* 0xd3 */,
+    1 /* 0xd4 */, 1 /* 0xd5 */, 1 /* 0xd6 */, 1 /* 0xd7 */,
+    1 /* 0xd8 */, 1 /* 0xd9 */, 1 /* 0xda */, 1 /* 0xdb */,
+    1 /* 0xdc */, 1 /* 0xdd */, 1 /* 0xde */, 1 /* 0xdf */,
+    1 /* 0xe0 */, 1 /* 0xe1 */, 1 /* 0xe2 */, 1 /* 0xe3 */,
+    1 /* 0xe4 */, 1 /* 0xe5 */, 1 /* 0xe6 */, 1 /* 0xe7 */,
+    1 /* 0xe8 */, 1 /* 0xe9 */, 1 /* 0xea */, 1 /* 0xeb */,
+    1 /* 0xec */, 1 /* 0xed */, 1 /* 0xee */, 1 /* 0xef */,
+    1 /* 0xf0 */, 1 /* 0xf1 */, 1 /* 0xf2 */, 1 /* 0xf3 */,
+    1 /* 0xf4 */, 1 /* 0xf5 */, 1 /* 0xf6 */, 1 /* 0xf7 */,
+    1 /* 0xf8 */, 1 /* 0xf9 */, 1 /* 0xfa */, 1 /* 0xfb */,
+    1 /* 0xfc */, 1 /* 0xfd */, 1 /* 0xfe */, 1 /* 0xff */
+};
+
+int nghttp2_check_header_value(const uint8_t *value, size_t len) {
+  const uint8_t *last;
+  for (last = value + len; value != last; ++value) {
+    if (!VALID_HD_VALUE_CHARS[*value]) {
+      return 0;
+    }
+  }
+  return 1;
+}
+
+uint8_t *nghttp2_cpymem(uint8_t *dest, const void *src, size_t len) {
+  if (len == 0) {
+    return dest;
+  }
+
+  memcpy(dest, src, len);
+
+  return dest + len;
+}
+
+const char *nghttp2_http2_strerror(uint32_t error_code) {
+  switch (error_code) {
+  case NGHTTP2_NO_ERROR:
+    return "NO_ERROR";
+  case NGHTTP2_PROTOCOL_ERROR:
+    return "PROTOCOL_ERROR";
+  case NGHTTP2_INTERNAL_ERROR:
+    return "INTERNAL_ERROR";
+  case NGHTTP2_FLOW_CONTROL_ERROR:
+    return "FLOW_CONTROL_ERROR";
+  case NGHTTP2_SETTINGS_TIMEOUT:
+    return "SETTINGS_TIMEOUT";
+  case NGHTTP2_STREAM_CLOSED:
+    return "STREAM_CLOSED";
+  case NGHTTP2_FRAME_SIZE_ERROR:
+    return "FRAME_SIZE_ERROR";
+  case NGHTTP2_REFUSED_STREAM:
+    return "REFUSED_STREAM";
+  case NGHTTP2_CANCEL:
+    return "CANCEL";
+  case NGHTTP2_COMPRESSION_ERROR:
+    return "COMPRESSION_ERROR";
+  case NGHTTP2_CONNECT_ERROR:
+    return "CONNECT_ERROR";
+  case NGHTTP2_ENHANCE_YOUR_CALM:
+    return "ENHANCE_YOUR_CALM";
+  case NGHTTP2_INADEQUATE_SECURITY:
+    return "INADEQUATE_SECURITY";
+  case NGHTTP2_HTTP_1_1_REQUIRED:
+    return "HTTP_1_1_REQUIRED";
+  default:
+    return "unknown";
+  }
+}

+ 103 - 0
iotkit-embedded/external_libs/nghttp2/nghttp2_helper.h

@@ -0,0 +1,103 @@
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
+ */
+
+
+#ifndef NGHTTP2_HELPER_H
+#define NGHTTP2_HELPER_H
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif /* HAVE_CONFIG_H */
+
+#include <string.h>
+#include <stddef.h>
+
+#include "nghttp2.h"
+#include "nghttp2_mem.h"
+
+#define nghttp2_min(A, B) ((A) < (B) ? (A) : (B))
+#define nghttp2_max(A, B) ((A) > (B) ? (A) : (B))
+
+#define lstreq(A, B, N) ((sizeof((A)) - 1) == (N) && memcmp((A), (B), (N)) == 0)
+
+#define nghttp2_struct_of(ptr, type, member)                                   \
+  ((type *)(void *)((char *)(ptr)-offsetof(type, member)))
+
+/*
+ * Copies 2 byte unsigned integer |n| in host byte order to |buf| in
+ * network byte order.
+ */
+void nghttp2_put_uint16be(uint8_t *buf, uint16_t n);
+
+/*
+ * Copies 4 byte unsigned integer |n| in host byte order to |buf| in
+ * network byte order.
+ */
+void nghttp2_put_uint32be(uint8_t *buf, uint32_t n);
+
+/*
+ * Retrieves 2 byte unsigned integer stored in |data| in network byte
+ * order and returns it in host byte order.
+ */
+uint16_t nghttp2_get_uint16(const uint8_t *data);
+
+/*
+ * Retrieves 4 byte unsigned integer stored in |data| in network byte
+ * order and returns it in host byte order.
+ */
+uint32_t nghttp2_get_uint32(const uint8_t *data);
+
+void nghttp2_downcase(uint8_t *s, size_t len);
+
+/*
+ * Adjusts |*local_window_size_ptr|, |*recv_window_size_ptr|,
+ * |*recv_reduction_ptr| with |*delta_ptr| which is the
+ * WINDOW_UPDATE's window_size_increment sent from local side. If
+ * |delta| is strictly larger than |*recv_window_size_ptr|,
+ * |*local_window_size_ptr| is increased by delta -
+ * *recv_window_size_ptr. If |delta| is negative,
+ * |*local_window_size_ptr| is decreased by delta.
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * NGHTTP2_ERR_FLOW_CONTROL
+ *     local_window_size overflow or gets negative.
+ */
+int nghttp2_adjust_local_window_size(int32_t *local_window_size_ptr,
+                                     int32_t *recv_window_size_ptr,
+                                     int32_t *recv_reduction_ptr,
+                                     int32_t *delta_ptr);
+
+/*
+ * This function works like nghttp2_adjust_local_window_size().  The
+ * difference is that this function assumes *delta_ptr >= 0, and
+ * *recv_window_size_ptr is not decreased by *delta_ptr.
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * NGHTTP2_ERR_FLOW_CONTROL
+ *     local_window_size overflow or gets negative.
+ */
+int nghttp2_increase_local_window_size(int32_t *local_window_size_ptr,
+                                       int32_t *recv_window_size_ptr,
+                                       int32_t *recv_reduction_ptr,
+                                       int32_t *delta_ptr);
+
+/*
+ * Returns non-zero if the function decided that WINDOW_UPDATE should
+ * be sent.
+ */
+int nghttp2_should_send_window_update(int32_t local_window_size,
+                                      int32_t recv_window_size);
+
+/*
+ * Copies the buffer |src| of length |len| to the destination pointed
+ * by the |dest|, assuming that the |dest| is at lest |len| bytes long
+ * . Returns dest + len.
+ */
+uint8_t *nghttp2_cpymem(uint8_t *dest, const void *src, size_t len);
+
+#endif /* NGHTTP2_HELPER_H */

+ 577 - 0
iotkit-embedded/external_libs/nghttp2/nghttp2_http.c

@@ -0,0 +1,577 @@
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
+ */
+
+
+#include "nghttp2_http.h"
+
+#include <string.h>
+#include <assert.h>
+#include <stdio.h>
+
+#include "nghttp2_hd.h"
+#include "nghttp2_helper.h"
+
+static uint8_t downcase(uint8_t c) {
+  return 'A' <= c && c <= 'Z' ? (uint8_t)(c - 'A' + 'a') : c;
+}
+
+static int memieq(const void *a, const void *b, size_t n) {
+  size_t i;
+  const uint8_t *aa = a, *bb = b;
+
+  for (i = 0; i < n; ++i) {
+    if (downcase(aa[i]) != downcase(bb[i])) {
+      return 0;
+    }
+  }
+  return 1;
+}
+
+#define lstrieq(A, B, N) ((sizeof((A)) - 1) == (N) && memieq((A), (B), (N)))
+
+static int64_t parse_uint(const uint8_t *s, size_t len) {
+  int64_t n = 0;
+  size_t i;
+  if (len == 0) {
+    return -1;
+  }
+  for (i = 0; i < len; ++i) {
+    if ('0' <= s[i] && s[i] <= '9') {
+      if (n > INT64_MAX / 10) {
+        return -1;
+      }
+      n *= 10;
+      if (n > INT64_MAX - (s[i] - '0')) {
+        return -1;
+      }
+      n += s[i] - '0';
+      continue;
+    }
+    return -1;
+  }
+  return n;
+}
+
+static int lws(const uint8_t *s, size_t n) {
+  size_t i;
+  for (i = 0; i < n; ++i) {
+    if (s[i] != ' ' && s[i] != '\t') {
+      return 0;
+    }
+  }
+  return 1;
+}
+
+static int check_pseudo_header(nghttp2_stream *stream, const nghttp2_hd_nv *nv,
+                               int flag) {
+  if (stream->http_flags & flag) {
+    return 0;
+  }
+  if (lws(nv->value->base, nv->value->len)) {
+    return 0;
+  }
+  stream->http_flags = (uint16_t)(stream->http_flags | flag);
+  return 1;
+}
+
+static int expect_response_body(nghttp2_stream *stream) {
+  return (stream->http_flags & NGHTTP2_HTTP_FLAG_METH_HEAD) == 0 &&
+         stream->status_code / 100 != 1 && stream->status_code != 304 &&
+         stream->status_code != 204;
+}
+
+/* For "http" or "https" URIs, OPTIONS request may have "*" in :path
+   header field to represent system-wide OPTIONS request.  Otherwise,
+   :path header field value must start with "/".  This function must
+   be called after ":method" header field was received.  This function
+   returns nonzero if path is valid.*/
+static int check_path(nghttp2_stream *stream) {
+  return (stream->http_flags & NGHTTP2_HTTP_FLAG_SCHEME_HTTP) == 0 ||
+         ((stream->http_flags & NGHTTP2_HTTP_FLAG_PATH_REGULAR) ||
+          ((stream->http_flags & NGHTTP2_HTTP_FLAG_METH_OPTIONS) &&
+           (stream->http_flags & NGHTTP2_HTTP_FLAG_PATH_ASTERISK)));
+}
+
+static int http_request_on_header(nghttp2_stream *stream, nghttp2_hd_nv *nv,
+                                  int trailer) {
+  if (nv->name->base[0] == ':') {
+    if (trailer ||
+        (stream->http_flags & NGHTTP2_HTTP_FLAG_PSEUDO_HEADER_DISALLOWED)) {
+      return NGHTTP2_ERR_HTTP_HEADER;
+    }
+  }
+
+  switch (nv->token) {
+  case NGHTTP2_TOKEN__AUTHORITY:
+    if (!check_pseudo_header(stream, nv, NGHTTP2_HTTP_FLAG__AUTHORITY)) {
+      return NGHTTP2_ERR_HTTP_HEADER;
+    }
+    break;
+  case NGHTTP2_TOKEN__METHOD:
+    if (!check_pseudo_header(stream, nv, NGHTTP2_HTTP_FLAG__METHOD)) {
+      return NGHTTP2_ERR_HTTP_HEADER;
+    }
+    switch (nv->value->len) {
+    case 4:
+      if (lstreq("HEAD", nv->value->base, nv->value->len)) {
+        stream->http_flags |= NGHTTP2_HTTP_FLAG_METH_HEAD;
+      }
+      break;
+    case 7:
+      switch (nv->value->base[6]) {
+      case 'T':
+        if (lstreq("CONNECT", nv->value->base, nv->value->len)) {
+          if (stream->stream_id % 2 == 0) {
+            /* we won't allow CONNECT for push */
+            return NGHTTP2_ERR_HTTP_HEADER;
+          }
+          stream->http_flags |= NGHTTP2_HTTP_FLAG_METH_CONNECT;
+          if (stream->http_flags &
+              (NGHTTP2_HTTP_FLAG__PATH | NGHTTP2_HTTP_FLAG__SCHEME)) {
+            return NGHTTP2_ERR_HTTP_HEADER;
+          }
+        }
+        break;
+      case 'S':
+        if (lstreq("OPTIONS", nv->value->base, nv->value->len)) {
+          stream->http_flags |= NGHTTP2_HTTP_FLAG_METH_OPTIONS;
+        }
+        break;
+      }
+      break;
+    }
+    break;
+  case NGHTTP2_TOKEN__PATH:
+    if (stream->http_flags & NGHTTP2_HTTP_FLAG_METH_CONNECT) {
+      return NGHTTP2_ERR_HTTP_HEADER;
+    }
+    if (!check_pseudo_header(stream, nv, NGHTTP2_HTTP_FLAG__PATH)) {
+      return NGHTTP2_ERR_HTTP_HEADER;
+    }
+    if (nv->value->base[0] == '/') {
+      stream->http_flags |= NGHTTP2_HTTP_FLAG_PATH_REGULAR;
+    } else if (nv->value->len == 1 && nv->value->base[0] == '*') {
+      stream->http_flags |= NGHTTP2_HTTP_FLAG_PATH_ASTERISK;
+    }
+    break;
+  case NGHTTP2_TOKEN__SCHEME:
+    if (stream->http_flags & NGHTTP2_HTTP_FLAG_METH_CONNECT) {
+      return NGHTTP2_ERR_HTTP_HEADER;
+    }
+    if (!check_pseudo_header(stream, nv, NGHTTP2_HTTP_FLAG__SCHEME)) {
+      return NGHTTP2_ERR_HTTP_HEADER;
+    }
+    if ((nv->value->len == 4 && memieq("http", nv->value->base, 4)) ||
+        (nv->value->len == 5 && memieq("https", nv->value->base, 5))) {
+      stream->http_flags |= NGHTTP2_HTTP_FLAG_SCHEME_HTTP;
+    }
+    break;
+  case NGHTTP2_TOKEN_HOST:
+    if (!check_pseudo_header(stream, nv, NGHTTP2_HTTP_FLAG_HOST)) {
+      return NGHTTP2_ERR_HTTP_HEADER;
+    }
+    break;
+  case NGHTTP2_TOKEN_CONTENT_LENGTH: {
+    if (stream->content_length != -1) {
+      return NGHTTP2_ERR_HTTP_HEADER;
+    }
+    stream->content_length = parse_uint(nv->value->base, nv->value->len);
+    if (stream->content_length == -1) {
+      return NGHTTP2_ERR_HTTP_HEADER;
+    }
+    break;
+  }
+  /* disallowed header fields */
+  case NGHTTP2_TOKEN_CONNECTION:
+  case NGHTTP2_TOKEN_KEEP_ALIVE:
+  case NGHTTP2_TOKEN_PROXY_CONNECTION:
+  case NGHTTP2_TOKEN_TRANSFER_ENCODING:
+  case NGHTTP2_TOKEN_UPGRADE:
+    return NGHTTP2_ERR_HTTP_HEADER;
+  case NGHTTP2_TOKEN_TE:
+    if (!lstrieq("trailers", nv->value->base, nv->value->len)) {
+      return NGHTTP2_ERR_HTTP_HEADER;
+    }
+    break;
+  default:
+    if (nv->name->base[0] == ':') {
+      return NGHTTP2_ERR_HTTP_HEADER;
+    }
+  }
+
+  if (nv->name->base[0] != ':') {
+    stream->http_flags |= NGHTTP2_HTTP_FLAG_PSEUDO_HEADER_DISALLOWED;
+  }
+
+  return 0;
+}
+
+static int http_response_on_header(nghttp2_stream *stream, nghttp2_hd_nv *nv,
+                                   int trailer) {
+  if (nv->name->base[0] == ':') {
+    if (trailer ||
+        (stream->http_flags & NGHTTP2_HTTP_FLAG_PSEUDO_HEADER_DISALLOWED)) {
+      return NGHTTP2_ERR_HTTP_HEADER;
+    }
+  }
+
+  switch (nv->token) {
+  case NGHTTP2_TOKEN__STATUS: {
+    if (!check_pseudo_header(stream, nv, NGHTTP2_HTTP_FLAG__STATUS)) {
+      return NGHTTP2_ERR_HTTP_HEADER;
+    }
+    if (nv->value->len != 3) {
+      return NGHTTP2_ERR_HTTP_HEADER;
+    }
+    stream->status_code = (int16_t)parse_uint(nv->value->base, nv->value->len);
+    if (stream->status_code == -1 || stream->status_code == 101) {
+      return NGHTTP2_ERR_HTTP_HEADER;
+    }
+    break;
+  }
+  case NGHTTP2_TOKEN_CONTENT_LENGTH: {
+    if (stream->status_code == 204) {
+      /* content-length header field in 204 response is prohibited by
+         RFC 7230.  But some widely used servers send content-length:
+         0.  Until they get fixed, we ignore it. */
+      if (stream->content_length != -1) {
+        /* Found multiple content-length field */
+        return NGHTTP2_ERR_HTTP_HEADER;
+      }
+      if (!lstrieq("0", nv->value->base, nv->value->len)) {
+        return NGHTTP2_ERR_HTTP_HEADER;
+      }
+      stream->content_length = 0;
+      return NGHTTP2_ERR_REMOVE_HTTP_HEADER;
+    }
+    if (stream->status_code / 100 == 1 ||
+        (stream->status_code == 200 &&
+         (stream->http_flags & NGHTTP2_HTTP_FLAG_METH_CONNECT))) {
+      return NGHTTP2_ERR_HTTP_HEADER;
+    }
+    if (stream->content_length != -1) {
+      return NGHTTP2_ERR_HTTP_HEADER;
+    }
+    stream->content_length = parse_uint(nv->value->base, nv->value->len);
+    if (stream->content_length == -1) {
+      return NGHTTP2_ERR_HTTP_HEADER;
+    }
+    break;
+  }
+  /* disallowed header fields */
+  case NGHTTP2_TOKEN_CONNECTION:
+  case NGHTTP2_TOKEN_KEEP_ALIVE:
+  case NGHTTP2_TOKEN_PROXY_CONNECTION:
+  case NGHTTP2_TOKEN_TRANSFER_ENCODING:
+  case NGHTTP2_TOKEN_UPGRADE:
+    return NGHTTP2_ERR_HTTP_HEADER;
+  case NGHTTP2_TOKEN_TE:
+    if (!lstrieq("trailers", nv->value->base, nv->value->len)) {
+      return NGHTTP2_ERR_HTTP_HEADER;
+    }
+    break;
+  default:
+    if (nv->name->base[0] == ':') {
+      return NGHTTP2_ERR_HTTP_HEADER;
+    }
+  }
+
+  if (nv->name->base[0] != ':') {
+    stream->http_flags |= NGHTTP2_HTTP_FLAG_PSEUDO_HEADER_DISALLOWED;
+  }
+
+  return 0;
+}
+
+/* Generated by genauthroitychartbl.py */
+static char VALID_AUTHORITY_CHARS[] = {
+    0 /* NUL  */, 0 /* SOH  */, 0 /* STX  */, 0 /* ETX  */,
+    0 /* EOT  */, 0 /* ENQ  */, 0 /* ACK  */, 0 /* BEL  */,
+    0 /* BS   */, 0 /* HT   */, 0 /* LF   */, 0 /* VT   */,
+    0 /* FF   */, 0 /* CR   */, 0 /* SO   */, 0 /* SI   */,
+    0 /* DLE  */, 0 /* DC1  */, 0 /* DC2  */, 0 /* DC3  */,
+    0 /* DC4  */, 0 /* NAK  */, 0 /* SYN  */, 0 /* ETB  */,
+    0 /* CAN  */, 0 /* EM   */, 0 /* SUB  */, 0 /* ESC  */,
+    0 /* FS   */, 0 /* GS   */, 0 /* RS   */, 0 /* US   */,
+    0 /* SPC  */, 1 /* !    */, 0 /* "    */, 0 /* #    */,
+    1 /* $    */, 1 /* %    */, 1 /* &    */, 1 /* '    */,
+    1 /* (    */, 1 /* )    */, 1 /* *    */, 1 /* +    */,
+    1 /* ,    */, 1 /* -    */, 1 /* .    */, 0 /* /    */,
+    1 /* 0    */, 1 /* 1    */, 1 /* 2    */, 1 /* 3    */,
+    1 /* 4    */, 1 /* 5    */, 1 /* 6    */, 1 /* 7    */,
+    1 /* 8    */, 1 /* 9    */, 1 /* :    */, 1 /* ;    */,
+    0 /* <    */, 1 /* =    */, 0 /* >    */, 0 /* ?    */,
+    1 /* @    */, 1 /* A    */, 1 /* B    */, 1 /* C    */,
+    1 /* D    */, 1 /* E    */, 1 /* F    */, 1 /* G    */,
+    1 /* H    */, 1 /* I    */, 1 /* J    */, 1 /* K    */,
+    1 /* L    */, 1 /* M    */, 1 /* N    */, 1 /* O    */,
+    1 /* P    */, 1 /* Q    */, 1 /* R    */, 1 /* S    */,
+    1 /* T    */, 1 /* U    */, 1 /* V    */, 1 /* W    */,
+    1 /* X    */, 1 /* Y    */, 1 /* Z    */, 1 /* [    */,
+    0 /* \    */, 1 /* ]    */, 0 /* ^    */, 1 /* _    */,
+    0 /* `    */, 1 /* a    */, 1 /* b    */, 1 /* c    */,
+    1 /* d    */, 1 /* e    */, 1 /* f    */, 1 /* g    */,
+    1 /* h    */, 1 /* i    */, 1 /* j    */, 1 /* k    */,
+    1 /* l    */, 1 /* m    */, 1 /* n    */, 1 /* o    */,
+    1 /* p    */, 1 /* q    */, 1 /* r    */, 1 /* s    */,
+    1 /* t    */, 1 /* u    */, 1 /* v    */, 1 /* w    */,
+    1 /* x    */, 1 /* y    */, 1 /* z    */, 0 /* {    */,
+    0 /* |    */, 0 /* }    */, 1 /* ~    */, 0 /* DEL  */,
+    0 /* 0x80 */, 0 /* 0x81 */, 0 /* 0x82 */, 0 /* 0x83 */,
+    0 /* 0x84 */, 0 /* 0x85 */, 0 /* 0x86 */, 0 /* 0x87 */,
+    0 /* 0x88 */, 0 /* 0x89 */, 0 /* 0x8a */, 0 /* 0x8b */,
+    0 /* 0x8c */, 0 /* 0x8d */, 0 /* 0x8e */, 0 /* 0x8f */,
+    0 /* 0x90 */, 0 /* 0x91 */, 0 /* 0x92 */, 0 /* 0x93 */,
+    0 /* 0x94 */, 0 /* 0x95 */, 0 /* 0x96 */, 0 /* 0x97 */,
+    0 /* 0x98 */, 0 /* 0x99 */, 0 /* 0x9a */, 0 /* 0x9b */,
+    0 /* 0x9c */, 0 /* 0x9d */, 0 /* 0x9e */, 0 /* 0x9f */,
+    0 /* 0xa0 */, 0 /* 0xa1 */, 0 /* 0xa2 */, 0 /* 0xa3 */,
+    0 /* 0xa4 */, 0 /* 0xa5 */, 0 /* 0xa6 */, 0 /* 0xa7 */,
+    0 /* 0xa8 */, 0 /* 0xa9 */, 0 /* 0xaa */, 0 /* 0xab */,
+    0 /* 0xac */, 0 /* 0xad */, 0 /* 0xae */, 0 /* 0xaf */,
+    0 /* 0xb0 */, 0 /* 0xb1 */, 0 /* 0xb2 */, 0 /* 0xb3 */,
+    0 /* 0xb4 */, 0 /* 0xb5 */, 0 /* 0xb6 */, 0 /* 0xb7 */,
+    0 /* 0xb8 */, 0 /* 0xb9 */, 0 /* 0xba */, 0 /* 0xbb */,
+    0 /* 0xbc */, 0 /* 0xbd */, 0 /* 0xbe */, 0 /* 0xbf */,
+    0 /* 0xc0 */, 0 /* 0xc1 */, 0 /* 0xc2 */, 0 /* 0xc3 */,
+    0 /* 0xc4 */, 0 /* 0xc5 */, 0 /* 0xc6 */, 0 /* 0xc7 */,
+    0 /* 0xc8 */, 0 /* 0xc9 */, 0 /* 0xca */, 0 /* 0xcb */,
+    0 /* 0xcc */, 0 /* 0xcd */, 0 /* 0xce */, 0 /* 0xcf */,
+    0 /* 0xd0 */, 0 /* 0xd1 */, 0 /* 0xd2 */, 0 /* 0xd3 */,
+    0 /* 0xd4 */, 0 /* 0xd5 */, 0 /* 0xd6 */, 0 /* 0xd7 */,
+    0 /* 0xd8 */, 0 /* 0xd9 */, 0 /* 0xda */, 0 /* 0xdb */,
+    0 /* 0xdc */, 0 /* 0xdd */, 0 /* 0xde */, 0 /* 0xdf */,
+    0 /* 0xe0 */, 0 /* 0xe1 */, 0 /* 0xe2 */, 0 /* 0xe3 */,
+    0 /* 0xe4 */, 0 /* 0xe5 */, 0 /* 0xe6 */, 0 /* 0xe7 */,
+    0 /* 0xe8 */, 0 /* 0xe9 */, 0 /* 0xea */, 0 /* 0xeb */,
+    0 /* 0xec */, 0 /* 0xed */, 0 /* 0xee */, 0 /* 0xef */,
+    0 /* 0xf0 */, 0 /* 0xf1 */, 0 /* 0xf2 */, 0 /* 0xf3 */,
+    0 /* 0xf4 */, 0 /* 0xf5 */, 0 /* 0xf6 */, 0 /* 0xf7 */,
+    0 /* 0xf8 */, 0 /* 0xf9 */, 0 /* 0xfa */, 0 /* 0xfb */,
+    0 /* 0xfc */, 0 /* 0xfd */, 0 /* 0xfe */, 0 /* 0xff */
+};
+
+static int check_authority(const uint8_t *value, size_t len) {
+  const uint8_t *last;
+  for (last = value + len; value != last; ++value) {
+    if (!VALID_AUTHORITY_CHARS[*value]) {
+      return 0;
+    }
+  }
+  return 1;
+}
+
+static int check_scheme(const uint8_t *value, size_t len) {
+  const uint8_t *last;
+  if (len == 0) {
+    return 0;
+  }
+
+  if (!(('A' <= *value && *value <= 'Z') || ('a' <= *value && *value <= 'z'))) {
+    return 0;
+  }
+
+  last = value + len;
+  ++value;
+
+  for (; value != last; ++value) {
+    if (!(('A' <= *value && *value <= 'Z') ||
+          ('a' <= *value && *value <= 'z') ||
+          ('0' <= *value && *value <= '9') || *value == '+' || *value == '-' ||
+          *value == '.')) {
+      return 0;
+    }
+  }
+  return 1;
+}
+
+int nghttp2_http_on_header(nghttp2_session *session, nghttp2_stream *stream,
+                           nghttp2_frame *frame, nghttp2_hd_nv *nv,
+                           int trailer) {
+  int rv;
+
+  /* We are strict for pseudo header field.  One bad character should
+     lead to fail.  OTOH, we should be a bit forgiving for regular
+     headers, since existing public internet has so much illegal
+     headers floating around and if we kill the stream because of
+     this, we may disrupt many web sites and/or libraries.  So we
+     become conservative here, and just ignore those illegal regular
+     headers. */
+  if (!nghttp2_check_header_name(nv->name->base, nv->name->len)) {
+    size_t i;
+    if (nv->name->len > 0 && nv->name->base[0] == ':') {
+      return NGHTTP2_ERR_HTTP_HEADER;
+    }
+    /* header field name must be lower-cased without exception */
+    for (i = 0; i < nv->name->len; ++i) {
+      uint8_t c = nv->name->base[i];
+      if ('A' <= c && c <= 'Z') {
+        return NGHTTP2_ERR_HTTP_HEADER;
+      }
+    }
+    /* When ignoring regular headers, we set this flag so that we
+       still enforce header field ordering rule for pseudo header
+       fields. */
+    stream->http_flags |= NGHTTP2_HTTP_FLAG_PSEUDO_HEADER_DISALLOWED;
+    return NGHTTP2_ERR_IGN_HTTP_HEADER;
+  }
+
+  if (nv->token == NGHTTP2_TOKEN__AUTHORITY ||
+      nv->token == NGHTTP2_TOKEN_HOST) {
+    rv = check_authority(nv->value->base, nv->value->len);
+  } else if (nv->token == NGHTTP2_TOKEN__SCHEME) {
+    rv = check_scheme(nv->value->base, nv->value->len);
+  } else {
+    rv = nghttp2_check_header_value(nv->value->base, nv->value->len);
+  }
+
+  if (rv == 0) {
+    assert(nv->name->len > 0);
+    if (nv->name->base[0] == ':') {
+      return NGHTTP2_ERR_HTTP_HEADER;
+    }
+    /* When ignoring regular headers, we set this flag so that we
+       still enforce header field ordering rule for pseudo header
+       fields. */
+    stream->http_flags |= NGHTTP2_HTTP_FLAG_PSEUDO_HEADER_DISALLOWED;
+    return NGHTTP2_ERR_IGN_HTTP_HEADER;
+  }
+
+  if (session->server || frame->hd.type == NGHTTP2_PUSH_PROMISE) {
+    return http_request_on_header(stream, nv, trailer);
+  }
+
+  return http_response_on_header(stream, nv, trailer);
+}
+
+int nghttp2_http_on_request_headers(nghttp2_stream *stream,
+                                    nghttp2_frame *frame) {
+  if (stream->http_flags & NGHTTP2_HTTP_FLAG_METH_CONNECT) {
+    if ((stream->http_flags & NGHTTP2_HTTP_FLAG__AUTHORITY) == 0) {
+      return -1;
+    }
+    stream->content_length = -1;
+  } else {
+    if ((stream->http_flags & NGHTTP2_HTTP_FLAG_REQ_HEADERS) !=
+            NGHTTP2_HTTP_FLAG_REQ_HEADERS ||
+        (stream->http_flags &
+         (NGHTTP2_HTTP_FLAG__AUTHORITY | NGHTTP2_HTTP_FLAG_HOST)) == 0) {
+      return -1;
+    }
+    if (!check_path(stream)) {
+      return -1;
+    }
+  }
+
+  if (frame->hd.type == NGHTTP2_PUSH_PROMISE) {
+    /* we are going to reuse data fields for upcoming response.  Clear
+       them now, except for method flags. */
+    stream->http_flags &= NGHTTP2_HTTP_FLAG_METH_ALL;
+    stream->content_length = -1;
+  }
+
+  return 0;
+}
+
+int nghttp2_http_on_response_headers(nghttp2_stream *stream) {
+  if ((stream->http_flags & NGHTTP2_HTTP_FLAG__STATUS) == 0) {
+    return -1;
+  }
+
+  if (stream->status_code / 100 == 1) {
+    /* non-final response */
+    stream->http_flags =
+        (uint16_t)((stream->http_flags & NGHTTP2_HTTP_FLAG_METH_ALL) |
+                   NGHTTP2_HTTP_FLAG_EXPECT_FINAL_RESPONSE);
+    stream->content_length = -1;
+    stream->status_code = -1;
+    return 0;
+  }
+
+  stream->http_flags =
+      (uint16_t)(stream->http_flags & ~NGHTTP2_HTTP_FLAG_EXPECT_FINAL_RESPONSE);
+
+  if (!expect_response_body(stream)) {
+    stream->content_length = 0;
+  } else if (stream->http_flags & (NGHTTP2_HTTP_FLAG_METH_CONNECT |
+                                   NGHTTP2_HTTP_FLAG_METH_UPGRADE_WORKAROUND)) {
+    stream->content_length = -1;
+  }
+
+  return 0;
+}
+
+int nghttp2_http_on_trailer_headers(nghttp2_stream *stream,
+                                    nghttp2_frame *frame) {
+  (void)stream;
+
+  if ((frame->hd.flags & NGHTTP2_FLAG_END_STREAM) == 0) {
+    return -1;
+  }
+
+  return 0;
+}
+
+int nghttp2_http_on_remote_end_stream(nghttp2_stream *stream) {
+  if (stream->http_flags & NGHTTP2_HTTP_FLAG_EXPECT_FINAL_RESPONSE) {
+    return -1;
+  }
+
+  if (stream->content_length != -1 &&
+      stream->content_length != stream->recv_content_length) {
+    return -1;
+  }
+
+  return 0;
+}
+
+int nghttp2_http_on_data_chunk(nghttp2_stream *stream, size_t n) {
+  stream->recv_content_length += (int64_t)n;
+
+  if ((stream->http_flags & NGHTTP2_HTTP_FLAG_EXPECT_FINAL_RESPONSE) ||
+      (stream->content_length != -1 &&
+       stream->recv_content_length > stream->content_length)) {
+    return -1;
+  }
+
+  return 0;
+}
+
+void nghttp2_http_record_request_method(nghttp2_stream *stream,
+                                        nghttp2_frame *frame) {
+  const nghttp2_nv *nva;
+  size_t nvlen;
+  size_t i;
+
+  switch (frame->hd.type) {
+  case NGHTTP2_HEADERS:
+    nva = frame->headers.nva;
+    nvlen = frame->headers.nvlen;
+    break;
+  case NGHTTP2_PUSH_PROMISE:
+    nva = frame->push_promise.nva;
+    nvlen = frame->push_promise.nvlen;
+    break;
+  default:
+    return;
+  }
+
+  /* TODO we should do this strictly. */
+  for (i = 0; i < nvlen; ++i) {
+    const nghttp2_nv *nv = &nva[i];
+    if (!(nv->namelen == 7 && nv->name[6] == 'd' &&
+          memcmp(":metho", nv->name, nv->namelen - 1) == 0)) {
+      continue;
+    }
+    if (lstreq("CONNECT", nv->value, nv->valuelen)) {
+      stream->http_flags |= NGHTTP2_HTTP_FLAG_METH_CONNECT;
+      return;
+    }
+    if (lstreq("HEAD", nv->value, nv->valuelen)) {
+      stream->http_flags |= NGHTTP2_HTTP_FLAG_METH_HEAD;
+      return;
+    }
+    return;
+  }
+}

+ 78 - 0
iotkit-embedded/external_libs/nghttp2/nghttp2_http.h

@@ -0,0 +1,78 @@
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
+ */
+
+
+#ifndef NGHTTP2_HTTP_H
+#define NGHTTP2_HTTP_H
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif /* HAVE_CONFIG_H */
+
+#include "nghttp2.h"
+#include "nghttp2_session.h"
+#include "nghttp2_stream.h"
+
+/*
+ * This function is called when HTTP header field |nv| in |frame| is
+ * received for |stream|.  This function will validate |nv| against
+ * the current state of stream.
+ *
+ * This function returns 0 if it succeeds, or one of the following
+ * negative error codes:
+ *
+ * NGHTTP2_ERR_HTTP_HEADER
+ *     Invalid HTTP header field was received.
+ * NGHTTP2_ERR_IGN_HTTP_HEADER
+ *     Invalid HTTP header field was received but it can be treated as
+ *     if it was not received because of compatibility reasons.
+ */
+int nghttp2_http_on_header(nghttp2_session *session, nghttp2_stream *stream,
+                           nghttp2_frame *frame, nghttp2_hd_nv *nv,
+                           int trailer);
+
+/*
+ * This function is called when request header is received.  This
+ * function performs validation and returns 0 if it succeeds, or -1.
+ */
+int nghttp2_http_on_request_headers(nghttp2_stream *stream,
+                                    nghttp2_frame *frame);
+
+/*
+ * This function is called when response header is received.  This
+ * function performs validation and returns 0 if it succeeds, or -1.
+ */
+int nghttp2_http_on_response_headers(nghttp2_stream *stream);
+
+/*
+ * This function is called trailer header (for both request and
+ * response) is received.  This function performs validation and
+ * returns 0 if it succeeds, or -1.
+ */
+int nghttp2_http_on_trailer_headers(nghttp2_stream *stream,
+                                    nghttp2_frame *frame);
+
+/*
+ * This function is called when END_STREAM flag is seen in incoming
+ * frame.  This function performs validation and returns 0 if it
+ * succeeds, or -1.
+ */
+int nghttp2_http_on_remote_end_stream(nghttp2_stream *stream);
+
+/*
+ * This function is called when chunk of data is received.  This
+ * function performs validation and returns 0 if it succeeds, or -1.
+ */
+int nghttp2_http_on_data_chunk(nghttp2_stream *stream, size_t n);
+
+/*
+ * This function inspects header field in |frame| and records its
+ * method in stream->http_flags.  If frame->hd.type is neither
+ * NGHTTP2_HEADERS nor NGHTTP2_PUSH_PROMISE, this function does
+ * nothing.
+ */
+void nghttp2_http_record_request_method(nghttp2_stream *stream,
+                                        nghttp2_frame *frame);
+
+#endif /* NGHTTP2_HTTP_H */

+ 39 - 0
iotkit-embedded/external_libs/nghttp2/nghttp2_int.h

@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) 2015-2018 Alibaba Group Holding Limited
+ */
+
+
+#ifndef NGHTTP2_INT_H
+#define NGHTTP2_INT_H
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif /* HAVE_CONFIG_H */
+
+#include "nghttp2.h"
+
+/* Macros, types and constants for internal use */
+
+/* "less" function, return nonzero if |lhs| is less than |rhs|. */
+typedef int (*nghttp2_less)(const void *lhs, const void *rhs);
+
+/* Internal error code. They must be in the range [-499, -100],
+   inclusive. */
+typedef enum {
+  NGHTTP2_ERR_CREDENTIAL_PENDING = -101,
+  NGHTTP2_ERR_IGN_HEADER_BLOCK = -103,
+  NGHTTP2_ERR_IGN_PAYLOAD = -104,
+  /*
+   * Invalid HTTP header field was received but it can be treated as
+   * if it was not received because of compatibility reasons.
+   */
+  NGHTTP2_ERR_IGN_HTTP_HEADER = -105,
+  /*
+   * Invalid HTTP header field was received, and it is ignored.
+   * Unlike NGHTTP2_ERR_IGN_HTTP_HEADER, this does not invoke
+   * nghttp2_on_invalid_header_callback.
+   */
+  NGHTTP2_ERR_REMOVE_HTTP_HEADER = -106
+} nghttp2_internal_error;
+
+#endif /* NGHTTP2_INT_H */

Alguns ficheiros não foram mostrados porque muitos ficheiros mudaram neste diff