simplejsble 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (272) hide show
  1. package/VERSION +1 -0
  2. package/android/CMakeLists.txt +62 -20
  3. package/cmake/epilogue.cmake +81 -0
  4. package/cmake/find/FindDBus1.cmake +20 -0
  5. package/cmake/find/Findfmt.cmake +41 -0
  6. package/cmake/find/Findgoogletest.cmake +18 -0
  7. package/cmake/parse_version.cmake +45 -0
  8. package/cmake/prelude.cmake +8 -0
  9. package/dependencies/README +4 -0
  10. package/dependencies/external/kvn/kvn_bytearray.h +297 -0
  11. package/dependencies/external/kvn/kvn_safe_callback.hpp +66 -0
  12. package/dependencies/external/kvn/logfwd.hpp +35 -0
  13. package/dependencies/internal/include/fmt/LICENSE +27 -0
  14. package/dependencies/internal/include/fmt/args.h +220 -0
  15. package/dependencies/internal/include/fmt/base.h +2962 -0
  16. package/dependencies/internal/include/fmt/chrono.h +2338 -0
  17. package/dependencies/internal/include/fmt/color.h +610 -0
  18. package/dependencies/internal/include/fmt/compile.h +539 -0
  19. package/dependencies/internal/include/fmt/core.h +5 -0
  20. package/dependencies/internal/include/fmt/format-inl.h +1949 -0
  21. package/dependencies/internal/include/fmt/format.h +4244 -0
  22. package/dependencies/internal/include/fmt/os.h +427 -0
  23. package/dependencies/internal/include/fmt/ostream.h +166 -0
  24. package/dependencies/internal/include/fmt/printf.h +633 -0
  25. package/dependencies/internal/include/fmt/ranges.h +850 -0
  26. package/dependencies/internal/include/fmt/std.h +726 -0
  27. package/dependencies/internal/include/fmt/xchar.h +373 -0
  28. package/dependencies/internal/include/nanopb/pb.h +948 -0
  29. package/dependencies/internal/include/nanopb/pb_common.h +49 -0
  30. package/dependencies/internal/include/nanopb/pb_decode.h +204 -0
  31. package/dependencies/internal/include/nanopb/pb_encode.h +195 -0
  32. package/dependencies/internal/include/simplejni/Common.hpp +598 -0
  33. package/dependencies/internal/include/simplejni/References.hpp +151 -0
  34. package/dependencies/internal/include/simplejni/Registry.hpp +249 -0
  35. package/dependencies/internal/include/simplejni/VM.hpp +93 -0
  36. package/dependencies/internal/src/nanopb/pb_common.c +388 -0
  37. package/dependencies/internal/src/nanopb/pb_decode.c +1763 -0
  38. package/dependencies/internal/src/nanopb/pb_encode.c +1009 -0
  39. package/dependencies/internal/src/simplejni/Common.cpp +4 -0
  40. package/package.json +5 -1
  41. package/simpleble/CMakeLists.txt +519 -0
  42. package/simpleble/include/simpleble/Adapter.h +102 -0
  43. package/simpleble/include/simpleble/AdapterSafe.h +58 -0
  44. package/simpleble/include/simpleble/Advanced.h +50 -0
  45. package/simpleble/include/simpleble/Characteristic.h +39 -0
  46. package/simpleble/include/simpleble/Config.h +64 -0
  47. package/simpleble/include/simpleble/Descriptor.h +30 -0
  48. package/simpleble/include/simpleble/Exceptions.h +72 -0
  49. package/simpleble/include/simpleble/Logging.h +73 -0
  50. package/simpleble/include/simpleble/Peripheral.h +82 -0
  51. package/simpleble/include/simpleble/PeripheralSafe.h +64 -0
  52. package/simpleble/include/simpleble/Service.h +34 -0
  53. package/simpleble/include/simpleble/SimpleBLE.h +8 -0
  54. package/simpleble/include/simpleble/Types.h +49 -0
  55. package/simpleble/include/simpleble/Utils.h +13 -0
  56. package/simpleble/include/simpleble_c/DEPRECATED +2 -0
  57. package/simpleble/include/simpleble_c/adapter.h +188 -0
  58. package/simpleble/include/simpleble_c/logging.h +37 -0
  59. package/simpleble/include/simpleble_c/peripheral.h +304 -0
  60. package/simpleble/include/simpleble_c/simpleble.h +21 -0
  61. package/simpleble/include/simpleble_c/types.h +73 -0
  62. package/simpleble/include/simpleble_c/utils.h +27 -0
  63. package/simpleble/src/CommonUtils.h +63 -0
  64. package/simpleble/src/Config.cpp +25 -0
  65. package/simpleble/src/Exceptions.cpp +31 -0
  66. package/simpleble/src/Logging.cpp +136 -0
  67. package/simpleble/src/LoggingInternal.h +85 -0
  68. package/simpleble/src/Utils.cpp +24 -0
  69. package/simpleble/src/backends/android/AdapterAndroid.cpp +101 -0
  70. package/simpleble/src/backends/android/AdapterAndroid.h +67 -0
  71. package/simpleble/src/backends/android/BackendAndroid.cpp +41 -0
  72. package/simpleble/src/backends/android/BackendAndroid.h +26 -0
  73. package/simpleble/src/backends/android/PeripheralAndroid.cpp +365 -0
  74. package/simpleble/src/backends/android/PeripheralAndroid.h +90 -0
  75. package/simpleble/src/backends/android/bridge/BluetoothGattCallback.cpp +432 -0
  76. package/simpleble/src/backends/android/bridge/BluetoothGattCallback.h +102 -0
  77. package/simpleble/src/backends/android/bridge/ScanCallback.cpp +142 -0
  78. package/simpleble/src/backends/android/bridge/ScanCallback.h +55 -0
  79. package/simpleble/src/backends/android/types/android/bluetooth/BluetoothAdapter.cpp +107 -0
  80. package/simpleble/src/backends/android/types/android/bluetooth/BluetoothAdapter.h +55 -0
  81. package/simpleble/src/backends/android/types/android/bluetooth/BluetoothDevice.cpp +68 -0
  82. package/simpleble/src/backends/android/types/android/bluetooth/BluetoothDevice.h +54 -0
  83. package/simpleble/src/backends/android/types/android/bluetooth/BluetoothGatt.cpp +115 -0
  84. package/simpleble/src/backends/android/types/android/bluetooth/BluetoothGatt.h +75 -0
  85. package/simpleble/src/backends/android/types/android/bluetooth/BluetoothGattCharacteristic.cpp +142 -0
  86. package/simpleble/src/backends/android/types/android/bluetooth/BluetoothGattCharacteristic.h +66 -0
  87. package/simpleble/src/backends/android/types/android/bluetooth/BluetoothGattDescriptor.cpp +67 -0
  88. package/simpleble/src/backends/android/types/android/bluetooth/BluetoothGattDescriptor.h +46 -0
  89. package/simpleble/src/backends/android/types/android/bluetooth/BluetoothGattService.cpp +106 -0
  90. package/simpleble/src/backends/android/types/android/bluetooth/BluetoothGattService.h +47 -0
  91. package/simpleble/src/backends/android/types/android/bluetooth/le/BluetoothScanner.cpp +47 -0
  92. package/simpleble/src/backends/android/types/android/bluetooth/le/BluetoothScanner.h +37 -0
  93. package/simpleble/src/backends/android/types/android/bluetooth/le/ScanRecord.cpp +69 -0
  94. package/simpleble/src/backends/android/types/android/bluetooth/le/ScanRecord.h +41 -0
  95. package/simpleble/src/backends/android/types/android/bluetooth/le/ScanResult.cpp +63 -0
  96. package/simpleble/src/backends/android/types/android/bluetooth/le/ScanResult.h +42 -0
  97. package/simpleble/src/backends/android/types/android/os/ParcelUUID.cpp +32 -0
  98. package/simpleble/src/backends/android/types/android/os/ParcelUUID.h +30 -0
  99. package/simpleble/src/backends/android/types/android/util/SparseArray.cpp +54 -0
  100. package/simpleble/src/backends/android/types/android/util/SparseArray.h +37 -0
  101. package/simpleble/src/backends/android/types/java/util/Iterator.cpp +36 -0
  102. package/simpleble/src/backends/android/types/java/util/Iterator.h +28 -0
  103. package/simpleble/src/backends/android/types/java/util/List.cpp +29 -0
  104. package/simpleble/src/backends/android/types/java/util/List.h +27 -0
  105. package/simpleble/src/backends/android/types/java/util/Set.cpp +33 -0
  106. package/simpleble/src/backends/android/types/java/util/Set.h +28 -0
  107. package/simpleble/src/backends/android/types/java/util/UUID.cpp +26 -0
  108. package/simpleble/src/backends/android/types/java/util/UUID.h +29 -0
  109. package/simpleble/src/backends/common/AdapterBase.cpp +53 -0
  110. package/simpleble/src/backends/common/AdapterBase.h +81 -0
  111. package/simpleble/src/backends/common/AdapterBaseTypes.h +22 -0
  112. package/simpleble/src/backends/common/BackendBase.h +20 -0
  113. package/simpleble/src/backends/common/BackendUtils.h +33 -0
  114. package/simpleble/src/backends/common/CharacteristicBase.cpp +28 -0
  115. package/simpleble/src/backends/common/CharacteristicBase.h +38 -0
  116. package/simpleble/src/backends/common/DescriptorBase.cpp +7 -0
  117. package/simpleble/src/backends/common/DescriptorBase.h +19 -0
  118. package/simpleble/src/backends/common/PeripheralBase.h +82 -0
  119. package/simpleble/src/backends/common/ServiceBase.cpp +18 -0
  120. package/simpleble/src/backends/common/ServiceBase.h +28 -0
  121. package/simpleble/src/backends/dongl/AdapterBaseTypes.h +22 -0
  122. package/simpleble/src/backends/dongl/AdapterDongl.cpp +369 -0
  123. package/simpleble/src/backends/dongl/AdapterDongl.h +60 -0
  124. package/simpleble/src/backends/dongl/BackendDongl.cpp +35 -0
  125. package/simpleble/src/backends/dongl/PeripheralDongl.cpp +562 -0
  126. package/simpleble/src/backends/dongl/PeripheralDongl.h +136 -0
  127. package/simpleble/src/backends/dongl/protocol/basic.pb.c +33 -0
  128. package/simpleble/src/backends/dongl/protocol/basic.pb.h +172 -0
  129. package/simpleble/src/backends/dongl/protocol/d2h.pb.c +18 -0
  130. package/simpleble/src/backends/dongl/protocol/d2h.pb.h +118 -0
  131. package/simpleble/src/backends/dongl/protocol/h2d.pb.c +12 -0
  132. package/simpleble/src/backends/dongl/protocol/h2d.pb.h +69 -0
  133. package/simpleble/src/backends/dongl/protocol/simpleble.pb.c +123 -0
  134. package/simpleble/src/backends/dongl/protocol/simpleble.pb.h +908 -0
  135. package/simpleble/src/backends/dongl/protocol/softdevice.pb.c +18 -0
  136. package/simpleble/src/backends/dongl/protocol/softdevice.pb.h +815 -0
  137. package/simpleble/src/backends/dongl/protocol/softdevice_gap.pb.c +339 -0
  138. package/simpleble/src/backends/dongl/protocol/softdevice_gap.pb.h +2086 -0
  139. package/simpleble/src/backends/dongl/protocol/softdevice_gattc.pb.c +114 -0
  140. package/simpleble/src/backends/dongl/protocol/softdevice_gattc.pb.h +772 -0
  141. package/simpleble/src/backends/dongl/protocol/softdevice_gatts.pb.c +117 -0
  142. package/simpleble/src/backends/dongl/protocol/softdevice_gatts.pb.h +766 -0
  143. package/simpleble/src/backends/dongl/protocol/softdevice_types.pb.c +207 -0
  144. package/simpleble/src/backends/dongl/protocol/softdevice_types.pb.h +1686 -0
  145. package/simpleble/src/backends/dongl/serial/Protocol.cpp +887 -0
  146. package/simpleble/src/backends/dongl/serial/Protocol.h +117 -0
  147. package/simpleble/src/backends/dongl/serial/ProtocolBase.cpp +95 -0
  148. package/simpleble/src/backends/dongl/serial/ProtocolBase.h +54 -0
  149. package/simpleble/src/backends/dongl/serial/Wire.cpp +133 -0
  150. package/simpleble/src/backends/dongl/serial/Wire.h +116 -0
  151. package/simpleble/src/backends/dongl/usb/UsbHelper.cpp +53 -0
  152. package/simpleble/src/backends/dongl/usb/UsbHelper.h +32 -0
  153. package/simpleble/src/backends/dongl/usb/UsbHelperApple.cpp +266 -0
  154. package/simpleble/src/backends/dongl/usb/UsbHelperApple.h +34 -0
  155. package/simpleble/src/backends/dongl/usb/UsbHelperImpl.h +29 -0
  156. package/simpleble/src/backends/dongl/usb/UsbHelperLinux.cpp +21 -0
  157. package/simpleble/src/backends/dongl/usb/UsbHelperLinux.h +22 -0
  158. package/simpleble/src/backends/dongl/usb/UsbHelperNull.cpp +21 -0
  159. package/simpleble/src/backends/dongl/usb/UsbHelperNull.h +22 -0
  160. package/simpleble/src/backends/dongl/usb/UsbHelperWindows.cpp +21 -0
  161. package/simpleble/src/backends/dongl/usb/UsbHelperWindows.h +22 -0
  162. package/simpleble/src/backends/linux/AdapterLinux.cpp +102 -0
  163. package/simpleble/src/backends/linux/AdapterLinux.h +55 -0
  164. package/simpleble/src/backends/linux/BackendBluez.cpp +90 -0
  165. package/simpleble/src/backends/linux/PeripheralLinux.cpp +394 -0
  166. package/simpleble/src/backends/linux/PeripheralLinux.h +90 -0
  167. package/simpleble/src/backends/linux_legacy/AdapterLinuxLegacy.cpp +102 -0
  168. package/simpleble/src/backends/linux_legacy/AdapterLinuxLegacy.h +55 -0
  169. package/simpleble/src/backends/linux_legacy/BackendBluezLegacy.cpp +87 -0
  170. package/simpleble/src/backends/linux_legacy/PeripheralLinuxLegacy.cpp +376 -0
  171. package/simpleble/src/backends/linux_legacy/PeripheralLinuxLegacy.h +90 -0
  172. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/Adapter.h +46 -0
  173. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/Agent.h +52 -0
  174. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/Bluez.h +37 -0
  175. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/BluezOrg.h +22 -0
  176. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/BluezOrgBluez.h +26 -0
  177. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/BluezRoot.h +32 -0
  178. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/Characteristic.h +47 -0
  179. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/Descriptor.h +33 -0
  180. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/Device.h +64 -0
  181. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/Exceptions.h +43 -0
  182. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/Service.h +27 -0
  183. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/Types.h +9 -0
  184. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/interfaces/Adapter1.h +49 -0
  185. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/interfaces/Agent1.h +100 -0
  186. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/interfaces/AgentManager1.h +24 -0
  187. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/interfaces/Battery1.h +32 -0
  188. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/interfaces/Device1.h +62 -0
  189. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/interfaces/GattCharacteristic1.h +48 -0
  190. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/interfaces/GattDescriptor1.h +41 -0
  191. package/simpleble/src/backends/linux_legacy/include/simplebluezlegacy/interfaces/GattService1.h +28 -0
  192. package/simpleble/src/backends/linux_legacy/include/simpledbuslegacy/advanced/Interface.h +67 -0
  193. package/simpleble/src/backends/linux_legacy/include/simpledbuslegacy/advanced/InterfaceRegistry.h +62 -0
  194. package/simpleble/src/backends/linux_legacy/include/simpledbuslegacy/advanced/Proxy.h +117 -0
  195. package/simpleble/src/backends/linux_legacy/include/simpledbuslegacy/base/Connection.h +50 -0
  196. package/simpleble/src/backends/linux_legacy/include/simpledbuslegacy/base/Exceptions.h +56 -0
  197. package/simpleble/src/backends/linux_legacy/include/simpledbuslegacy/base/Holder.h +147 -0
  198. package/simpleble/src/backends/linux_legacy/include/simpledbuslegacy/base/Logging.h +57 -0
  199. package/simpleble/src/backends/linux_legacy/include/simpledbuslegacy/base/Message.h +89 -0
  200. package/simpleble/src/backends/linux_legacy/include/simpledbuslegacy/base/Path.h +24 -0
  201. package/simpleble/src/backends/linux_legacy/include/simpledbuslegacy/interfaces/ObjectManager.h +26 -0
  202. package/simpleble/src/backends/linux_legacy/src/Adapter.cpp +78 -0
  203. package/simpleble/src/backends/linux_legacy/src/Agent.cpp +72 -0
  204. package/simpleble/src/backends/linux_legacy/src/Bluez.cpp +45 -0
  205. package/simpleble/src/backends/linux_legacy/src/BluezOrg.cpp +20 -0
  206. package/simpleble/src/backends/linux_legacy/src/BluezOrgBluez.cpp +23 -0
  207. package/simpleble/src/backends/linux_legacy/src/BluezRoot.cpp +44 -0
  208. package/simpleble/src/backends/linux_legacy/src/Characteristic.cpp +64 -0
  209. package/simpleble/src/backends/linux_legacy/src/Descriptor.cpp +27 -0
  210. package/simpleble/src/backends/linux_legacy/src/Device.cpp +104 -0
  211. package/simpleble/src/backends/linux_legacy/src/Exceptions.cpp +29 -0
  212. package/simpleble/src/backends/linux_legacy/src/Service.cpp +33 -0
  213. package/simpleble/src/backends/linux_legacy/src/advanced/Interface.cpp +155 -0
  214. package/simpleble/src/backends/linux_legacy/src/advanced/Proxy.cpp +374 -0
  215. package/simpleble/src/backends/linux_legacy/src/base/Connection.cpp +219 -0
  216. package/simpleble/src/backends/linux_legacy/src/base/Exceptions.cpp +39 -0
  217. package/simpleble/src/backends/linux_legacy/src/base/Holder.cpp +739 -0
  218. package/simpleble/src/backends/linux_legacy/src/base/Message.cpp +622 -0
  219. package/simpleble/src/backends/linux_legacy/src/base/Path.cpp +129 -0
  220. package/simpleble/src/backends/linux_legacy/src/interfaces/Adapter1.cpp +123 -0
  221. package/simpleble/src/backends/linux_legacy/src/interfaces/Agent1.cpp +143 -0
  222. package/simpleble/src/backends/linux_legacy/src/interfaces/AgentManager1.cpp +34 -0
  223. package/simpleble/src/backends/linux_legacy/src/interfaces/Battery1.cpp +30 -0
  224. package/simpleble/src/backends/linux_legacy/src/interfaces/Device1.cpp +170 -0
  225. package/simpleble/src/backends/linux_legacy/src/interfaces/GattCharacteristic1.cpp +118 -0
  226. package/simpleble/src/backends/linux_legacy/src/interfaces/GattDescriptor1.cpp +78 -0
  227. package/simpleble/src/backends/linux_legacy/src/interfaces/GattService1.cpp +28 -0
  228. package/simpleble/src/backends/linux_legacy/src/interfaces/ObjectManager.cpp +58 -0
  229. package/simpleble/src/backends/macos/AdapterBaseMacOS.h +29 -0
  230. package/simpleble/src/backends/macos/AdapterBaseMacOS.mm +235 -0
  231. package/simpleble/src/backends/macos/AdapterMac.h +78 -0
  232. package/simpleble/src/backends/macos/AdapterMac.mm +156 -0
  233. package/simpleble/src/backends/macos/BackendCoreBluetooth.mm +45 -0
  234. package/simpleble/src/backends/macos/PeripheralBaseMacOS.h +49 -0
  235. package/simpleble/src/backends/macos/PeripheralBaseMacOS.mm +714 -0
  236. package/simpleble/src/backends/macos/PeripheralMac.h +81 -0
  237. package/simpleble/src/backends/macos/PeripheralMac.mm +229 -0
  238. package/simpleble/src/backends/macos/Utils.h +9 -0
  239. package/simpleble/src/backends/macos/Utils.mm +23 -0
  240. package/simpleble/src/backends/plain/AdapterPlain.cpp +65 -0
  241. package/simpleble/src/backends/plain/AdapterPlain.h +49 -0
  242. package/simpleble/src/backends/plain/BackendPlain.cpp +30 -0
  243. package/simpleble/src/backends/plain/PeripheralPlain.cpp +159 -0
  244. package/simpleble/src/backends/plain/PeripheralPlain.h +72 -0
  245. package/simpleble/src/backends/windows/AdapterWindows.cpp +338 -0
  246. package/simpleble/src/backends/windows/AdapterWindows.h +89 -0
  247. package/simpleble/src/backends/windows/BackendWinRT.cpp +67 -0
  248. package/simpleble/src/backends/windows/BackendWinRT.h +18 -0
  249. package/simpleble/src/backends/windows/MtaManager.cpp +49 -0
  250. package/simpleble/src/backends/windows/MtaManager.h +90 -0
  251. package/simpleble/src/backends/windows/PeripheralWindows.cpp +487 -0
  252. package/simpleble/src/backends/windows/PeripheralWindows.h +129 -0
  253. package/simpleble/src/backends/windows/Utils.cpp +146 -0
  254. package/simpleble/src/backends/windows/Utils.h +47 -0
  255. package/simpleble/src/builders/BuildVec.h +32 -0
  256. package/simpleble/src/builders/BuilderBase.h +87 -0
  257. package/simpleble/src/external/TaskRunner.hpp +99 -0
  258. package/simpleble/src/external/ThreadRunner.h +52 -0
  259. package/simpleble/src/external/kvn_safe_callback.hpp +66 -0
  260. package/simpleble/src/external/kvn_safe_map.hpp +94 -0
  261. package/simpleble/src/external/kvn_threadrunner.hpp +70 -0
  262. package/simpleble/src/external/logfwd.hpp +35 -0
  263. package/simpleble/src/frontends/base/Adapter.cpp +111 -0
  264. package/simpleble/src/frontends/base/Advanced.cpp +34 -0
  265. package/simpleble/src/frontends/base/Backend.cpp +95 -0
  266. package/simpleble/src/frontends/base/Backend.h +76 -0
  267. package/simpleble/src/frontends/base/Characteristic.cpp +56 -0
  268. package/simpleble/src/frontends/base/Descriptor.cpp +21 -0
  269. package/simpleble/src/frontends/base/Peripheral.cpp +113 -0
  270. package/simpleble/src/frontends/base/Service.cpp +26 -0
  271. package/simpleble/src/frontends/safe/AdapterSafe.cpp +158 -0
  272. package/simpleble/src/frontends/safe/PeripheralSafe.cpp +219 -0
@@ -0,0 +1,1763 @@
1
+ /* pb_decode.c -- decode a protobuf using minimal resources
2
+ *
3
+ * 2011 Petteri Aimonen <jpa@kapsi.fi>
4
+ */
5
+
6
+ /* Use the GCC warn_unused_result attribute to check that all return values
7
+ * are propagated correctly. On other compilers, gcc before 3.4.0 and iar
8
+ * before 9.40.1 just ignore the annotation.
9
+ */
10
+ #if (defined(__GNUC__) && ((__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))) || \
11
+ (defined(__IAR_SYSTEMS_ICC__) && (__VER__ >= 9040001))
12
+ #define checkreturn __attribute__((warn_unused_result))
13
+ #else
14
+ #define checkreturn
15
+ #endif
16
+
17
+ #include "nanopb/pb.h" // NOTE: INCLUDE PATH HAS BEEN MANUALLY CHANGED
18
+ #include "nanopb/pb_decode.h" // NOTE: INCLUDE PATH HAS BEEN MANUALLY CHANGED
19
+ #include "nanopb/pb_common.h" // NOTE: INCLUDE PATH HAS BEEN MANUALLY CHANGED
20
+
21
+ /**************************************
22
+ * Declarations internal to this file *
23
+ **************************************/
24
+
25
+ static bool checkreturn buf_read(pb_istream_t *stream, pb_byte_t *buf, size_t count);
26
+ static bool checkreturn read_raw_value(pb_istream_t *stream, pb_wire_type_t wire_type, pb_byte_t *buf, size_t *size);
27
+ static bool checkreturn decode_basic_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *field);
28
+ static bool checkreturn decode_static_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *field);
29
+ static bool checkreturn decode_pointer_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *field);
30
+ static bool checkreturn decode_callback_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *field);
31
+ static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *field);
32
+ static bool checkreturn default_extension_decoder(pb_istream_t *stream, pb_extension_t *extension, uint32_t tag, pb_wire_type_t wire_type);
33
+ static bool checkreturn decode_extension(pb_istream_t *stream, uint32_t tag, pb_wire_type_t wire_type, pb_extension_t *extension);
34
+ static bool pb_field_set_to_default(pb_field_iter_t *field);
35
+ static bool pb_message_set_to_defaults(pb_field_iter_t *iter);
36
+ static bool checkreturn pb_dec_bool(pb_istream_t *stream, const pb_field_iter_t *field);
37
+ static bool checkreturn pb_dec_varint(pb_istream_t *stream, const pb_field_iter_t *field);
38
+ static bool checkreturn pb_dec_bytes(pb_istream_t *stream, const pb_field_iter_t *field);
39
+ static bool checkreturn pb_dec_string(pb_istream_t *stream, const pb_field_iter_t *field);
40
+ static bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_iter_t *field);
41
+ static bool checkreturn pb_dec_fixed_length_bytes(pb_istream_t *stream, const pb_field_iter_t *field);
42
+ static bool checkreturn pb_skip_varint(pb_istream_t *stream);
43
+ static bool checkreturn pb_skip_string(pb_istream_t *stream);
44
+
45
+ #ifdef PB_ENABLE_MALLOC
46
+ static bool checkreturn allocate_field(pb_istream_t *stream, void *pData, size_t data_size, size_t array_size);
47
+ static void initialize_pointer_field(void *pItem, pb_field_iter_t *field);
48
+ static bool checkreturn pb_release_union_field(pb_istream_t *stream, pb_field_iter_t *field);
49
+ static void pb_release_single_field(pb_field_iter_t *field);
50
+ #endif
51
+
52
+ #ifdef PB_WITHOUT_64BIT
53
+ #define pb_int64_t int32_t
54
+ #define pb_uint64_t uint32_t
55
+ #else
56
+ #define pb_int64_t int64_t
57
+ #define pb_uint64_t uint64_t
58
+ #endif
59
+
60
+ typedef struct {
61
+ uint32_t bitfield[(PB_MAX_REQUIRED_FIELDS + 31) / 32];
62
+ } pb_fields_seen_t;
63
+
64
+ /*******************************
65
+ * pb_istream_t implementation *
66
+ *******************************/
67
+
68
+ static bool checkreturn buf_read(pb_istream_t *stream, pb_byte_t *buf, size_t count)
69
+ {
70
+ const pb_byte_t *source = (const pb_byte_t*)stream->state;
71
+ stream->state = (pb_byte_t*)stream->state + count;
72
+
73
+ if (buf != NULL)
74
+ {
75
+ memcpy(buf, source, count * sizeof(pb_byte_t));
76
+ }
77
+
78
+ return true;
79
+ }
80
+
81
+ bool checkreturn pb_read(pb_istream_t *stream, pb_byte_t *buf, size_t count)
82
+ {
83
+ if (count == 0)
84
+ return true;
85
+
86
+ #ifndef PB_BUFFER_ONLY
87
+ if (buf == NULL && stream->callback != buf_read)
88
+ {
89
+ /* Skip input bytes */
90
+ pb_byte_t tmp[16];
91
+ while (count > 16)
92
+ {
93
+ if (!pb_read(stream, tmp, 16))
94
+ return false;
95
+
96
+ count -= 16;
97
+ }
98
+
99
+ return pb_read(stream, tmp, count);
100
+ }
101
+ #endif
102
+
103
+ if (stream->bytes_left < count)
104
+ PB_RETURN_ERROR(stream, "end-of-stream");
105
+
106
+ #ifndef PB_BUFFER_ONLY
107
+ if (!stream->callback(stream, buf, count))
108
+ PB_RETURN_ERROR(stream, "io error");
109
+ #else
110
+ if (!buf_read(stream, buf, count))
111
+ return false;
112
+ #endif
113
+
114
+ if (stream->bytes_left < count)
115
+ stream->bytes_left = 0;
116
+ else
117
+ stream->bytes_left -= count;
118
+
119
+ return true;
120
+ }
121
+
122
+ /* Read a single byte from input stream. buf may not be NULL.
123
+ * This is an optimization for the varint decoding. */
124
+ static bool checkreturn pb_readbyte(pb_istream_t *stream, pb_byte_t *buf)
125
+ {
126
+ if (stream->bytes_left == 0)
127
+ PB_RETURN_ERROR(stream, "end-of-stream");
128
+
129
+ #ifndef PB_BUFFER_ONLY
130
+ if (!stream->callback(stream, buf, 1))
131
+ PB_RETURN_ERROR(stream, "io error");
132
+ #else
133
+ *buf = *(const pb_byte_t*)stream->state;
134
+ stream->state = (pb_byte_t*)stream->state + 1;
135
+ #endif
136
+
137
+ stream->bytes_left--;
138
+
139
+ return true;
140
+ }
141
+
142
+ pb_istream_t pb_istream_from_buffer(const pb_byte_t *buf, size_t msglen)
143
+ {
144
+ pb_istream_t stream;
145
+ /* Cast away the const from buf without a compiler error. We are
146
+ * careful to use it only in a const manner in the callbacks.
147
+ */
148
+ union {
149
+ void *state;
150
+ const void *c_state;
151
+ } state;
152
+ #ifdef PB_BUFFER_ONLY
153
+ stream.callback = NULL;
154
+ #else
155
+ stream.callback = &buf_read;
156
+ #endif
157
+ state.c_state = buf;
158
+ stream.state = state.state;
159
+ stream.bytes_left = msglen;
160
+ #ifndef PB_NO_ERRMSG
161
+ stream.errmsg = NULL;
162
+ #endif
163
+ return stream;
164
+ }
165
+
166
+
167
+ /********************
168
+ * Helper functions *
169
+ ********************/
170
+
171
+ bool checkreturn pb_decode_varint32(pb_istream_t *stream, uint32_t *dest)
172
+ {
173
+ pb_byte_t byte;
174
+ uint32_t result;
175
+
176
+ if (!pb_readbyte(stream, &byte))
177
+ {
178
+ return false;
179
+ }
180
+
181
+ if ((byte & 0x80) == 0)
182
+ {
183
+ /* Quick case, 1 byte value */
184
+ result = byte;
185
+ }
186
+ else
187
+ {
188
+ /* Multibyte case */
189
+ uint_fast8_t bitpos = 7;
190
+ result = byte & 0x7F;
191
+
192
+ do
193
+ {
194
+ if (!pb_readbyte(stream, &byte))
195
+ return false;
196
+
197
+ if (bitpos >= 32)
198
+ {
199
+ /* Note: The varint could have trailing 0x80 bytes, or 0xFF for negative. */
200
+ pb_byte_t sign_extension = (bitpos < 63) ? 0xFF : 0x01;
201
+ bool valid_extension = ((byte & 0x7F) == 0x00 ||
202
+ ((result >> 31) != 0 && byte == sign_extension));
203
+
204
+ if (bitpos >= 64 || !valid_extension)
205
+ {
206
+ PB_RETURN_ERROR(stream, "varint overflow");
207
+ }
208
+ }
209
+ else if (bitpos == 28)
210
+ {
211
+ if ((byte & 0x70) != 0 && (byte & 0x78) != 0x78)
212
+ {
213
+ PB_RETURN_ERROR(stream, "varint overflow");
214
+ }
215
+ result |= (uint32_t)(byte & 0x0F) << bitpos;
216
+ }
217
+ else
218
+ {
219
+ result |= (uint32_t)(byte & 0x7F) << bitpos;
220
+ }
221
+ bitpos = (uint_fast8_t)(bitpos + 7);
222
+ } while (byte & 0x80);
223
+ }
224
+
225
+ *dest = result;
226
+ return true;
227
+ }
228
+
229
+ #ifndef PB_WITHOUT_64BIT
230
+ bool checkreturn pb_decode_varint(pb_istream_t *stream, uint64_t *dest)
231
+ {
232
+ pb_byte_t byte;
233
+ uint_fast8_t bitpos = 0;
234
+ uint64_t result = 0;
235
+
236
+ do
237
+ {
238
+ if (!pb_readbyte(stream, &byte))
239
+ return false;
240
+
241
+ if (bitpos >= 63 && (byte & 0xFE) != 0)
242
+ PB_RETURN_ERROR(stream, "varint overflow");
243
+
244
+ result |= (uint64_t)(byte & 0x7F) << bitpos;
245
+ bitpos = (uint_fast8_t)(bitpos + 7);
246
+ } while (byte & 0x80);
247
+
248
+ *dest = result;
249
+ return true;
250
+ }
251
+ #endif
252
+
253
+ bool checkreturn pb_skip_varint(pb_istream_t *stream)
254
+ {
255
+ pb_byte_t byte;
256
+ do
257
+ {
258
+ if (!pb_read(stream, &byte, 1))
259
+ return false;
260
+ } while (byte & 0x80);
261
+ return true;
262
+ }
263
+
264
+ bool checkreturn pb_skip_string(pb_istream_t *stream)
265
+ {
266
+ uint32_t length;
267
+ if (!pb_decode_varint32(stream, &length))
268
+ return false;
269
+
270
+ if ((size_t)length != length)
271
+ {
272
+ PB_RETURN_ERROR(stream, "size too large");
273
+ }
274
+
275
+ return pb_read(stream, NULL, (size_t)length);
276
+ }
277
+
278
+ bool checkreturn pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, uint32_t *tag, bool *eof)
279
+ {
280
+ uint32_t temp;
281
+ *eof = false;
282
+ *wire_type = (pb_wire_type_t) 0;
283
+ *tag = 0;
284
+
285
+ if (stream->bytes_left == 0)
286
+ {
287
+ *eof = true;
288
+ return false;
289
+ }
290
+
291
+ if (!pb_decode_varint32(stream, &temp))
292
+ {
293
+ #ifndef PB_BUFFER_ONLY
294
+ /* Workaround for issue #1017
295
+ *
296
+ * Callback streams don't set bytes_left to 0 on eof until after being called by pb_decode_varint32,
297
+ * which results in "io error" being raised. This contrasts the behavior of buffer streams who raise
298
+ * no error on eof as bytes_left is already 0 on entry. This causes legitimate errors (e.g. missing
299
+ * required fields) to be incorrectly reported by callback streams.
300
+ */
301
+ if (stream->callback != buf_read && stream->bytes_left == 0)
302
+ {
303
+ #ifndef PB_NO_ERRMSG
304
+ if (strcmp(stream->errmsg, "io error") == 0)
305
+ stream->errmsg = NULL;
306
+ #endif
307
+ *eof = true;
308
+ }
309
+ #endif
310
+ return false;
311
+ }
312
+
313
+ *tag = temp >> 3;
314
+ *wire_type = (pb_wire_type_t)(temp & 7);
315
+ return true;
316
+ }
317
+
318
+ bool checkreturn pb_skip_field(pb_istream_t *stream, pb_wire_type_t wire_type)
319
+ {
320
+ switch (wire_type)
321
+ {
322
+ case PB_WT_VARINT: return pb_skip_varint(stream);
323
+ case PB_WT_64BIT: return pb_read(stream, NULL, 8);
324
+ case PB_WT_STRING: return pb_skip_string(stream);
325
+ case PB_WT_32BIT: return pb_read(stream, NULL, 4);
326
+ case PB_WT_PACKED:
327
+ /* Calling pb_skip_field with a PB_WT_PACKED is an error.
328
+ * Explicitly handle this case and fallthrough to default to avoid
329
+ * compiler warnings.
330
+ */
331
+ default: PB_RETURN_ERROR(stream, "invalid wire_type");
332
+ }
333
+ }
334
+
335
+ /* Read a raw value to buffer, for the purpose of passing it to callback as
336
+ * a substream. Size is maximum size on call, and actual size on return.
337
+ */
338
+ static bool checkreturn read_raw_value(pb_istream_t *stream, pb_wire_type_t wire_type, pb_byte_t *buf, size_t *size)
339
+ {
340
+ size_t max_size = *size;
341
+ switch (wire_type)
342
+ {
343
+ case PB_WT_VARINT:
344
+ *size = 0;
345
+ do
346
+ {
347
+ (*size)++;
348
+ if (*size > max_size)
349
+ PB_RETURN_ERROR(stream, "varint overflow");
350
+
351
+ if (!pb_read(stream, buf, 1))
352
+ return false;
353
+ } while (*buf++ & 0x80);
354
+ return true;
355
+
356
+ case PB_WT_64BIT:
357
+ *size = 8;
358
+ return pb_read(stream, buf, 8);
359
+
360
+ case PB_WT_32BIT:
361
+ *size = 4;
362
+ return pb_read(stream, buf, 4);
363
+
364
+ case PB_WT_STRING:
365
+ /* Calling read_raw_value with a PB_WT_STRING is an error.
366
+ * Explicitly handle this case and fallthrough to default to avoid
367
+ * compiler warnings.
368
+ */
369
+
370
+ case PB_WT_PACKED:
371
+ /* Calling read_raw_value with a PB_WT_PACKED is an error.
372
+ * Explicitly handle this case and fallthrough to default to avoid
373
+ * compiler warnings.
374
+ */
375
+
376
+ default: PB_RETURN_ERROR(stream, "invalid wire_type");
377
+ }
378
+ }
379
+
380
+ /* Decode string length from stream and return a substream with limited length.
381
+ * Remember to close the substream using pb_close_string_substream().
382
+ */
383
+ bool checkreturn pb_make_string_substream(pb_istream_t *stream, pb_istream_t *substream)
384
+ {
385
+ uint32_t size;
386
+ if (!pb_decode_varint32(stream, &size))
387
+ return false;
388
+
389
+ *substream = *stream;
390
+ if (substream->bytes_left < size)
391
+ PB_RETURN_ERROR(stream, "parent stream too short");
392
+
393
+ substream->bytes_left = (size_t)size;
394
+ stream->bytes_left -= (size_t)size;
395
+ return true;
396
+ }
397
+
398
+ bool checkreturn pb_close_string_substream(pb_istream_t *stream, pb_istream_t *substream)
399
+ {
400
+ if (substream->bytes_left) {
401
+ if (!pb_read(substream, NULL, substream->bytes_left))
402
+ return false;
403
+ }
404
+
405
+ stream->state = substream->state;
406
+
407
+ #ifndef PB_NO_ERRMSG
408
+ stream->errmsg = substream->errmsg;
409
+ #endif
410
+ return true;
411
+ }
412
+
413
+ /*************************
414
+ * Decode a single field *
415
+ *************************/
416
+
417
+ static bool checkreturn decode_basic_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *field)
418
+ {
419
+ switch (PB_LTYPE(field->type))
420
+ {
421
+ case PB_LTYPE_BOOL:
422
+ if (wire_type != PB_WT_VARINT && wire_type != PB_WT_PACKED)
423
+ PB_RETURN_ERROR(stream, "wrong wire type");
424
+
425
+ return pb_dec_bool(stream, field);
426
+
427
+ case PB_LTYPE_VARINT:
428
+ case PB_LTYPE_UVARINT:
429
+ case PB_LTYPE_SVARINT:
430
+ if (wire_type != PB_WT_VARINT && wire_type != PB_WT_PACKED)
431
+ PB_RETURN_ERROR(stream, "wrong wire type");
432
+
433
+ return pb_dec_varint(stream, field);
434
+
435
+ case PB_LTYPE_FIXED32:
436
+ if (wire_type != PB_WT_32BIT && wire_type != PB_WT_PACKED)
437
+ PB_RETURN_ERROR(stream, "wrong wire type");
438
+
439
+ return pb_decode_fixed32(stream, field->pData);
440
+
441
+ case PB_LTYPE_FIXED64:
442
+ if (wire_type != PB_WT_64BIT && wire_type != PB_WT_PACKED)
443
+ PB_RETURN_ERROR(stream, "wrong wire type");
444
+
445
+ #ifdef PB_CONVERT_DOUBLE_FLOAT
446
+ if (field->data_size == sizeof(float))
447
+ {
448
+ return pb_decode_double_as_float(stream, (float*)field->pData);
449
+ }
450
+ #endif
451
+
452
+ #ifdef PB_WITHOUT_64BIT
453
+ PB_RETURN_ERROR(stream, "invalid data_size");
454
+ #else
455
+ return pb_decode_fixed64(stream, field->pData);
456
+ #endif
457
+
458
+ case PB_LTYPE_BYTES:
459
+ if (wire_type != PB_WT_STRING)
460
+ PB_RETURN_ERROR(stream, "wrong wire type");
461
+
462
+ return pb_dec_bytes(stream, field);
463
+
464
+ case PB_LTYPE_STRING:
465
+ if (wire_type != PB_WT_STRING)
466
+ PB_RETURN_ERROR(stream, "wrong wire type");
467
+
468
+ return pb_dec_string(stream, field);
469
+
470
+ case PB_LTYPE_SUBMESSAGE:
471
+ case PB_LTYPE_SUBMSG_W_CB:
472
+ if (wire_type != PB_WT_STRING)
473
+ PB_RETURN_ERROR(stream, "wrong wire type");
474
+
475
+ return pb_dec_submessage(stream, field);
476
+
477
+ case PB_LTYPE_FIXED_LENGTH_BYTES:
478
+ if (wire_type != PB_WT_STRING)
479
+ PB_RETURN_ERROR(stream, "wrong wire type");
480
+
481
+ return pb_dec_fixed_length_bytes(stream, field);
482
+
483
+ default:
484
+ PB_RETURN_ERROR(stream, "invalid field type");
485
+ }
486
+ }
487
+
488
+ static bool checkreturn decode_static_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *field)
489
+ {
490
+ switch (PB_HTYPE(field->type))
491
+ {
492
+ case PB_HTYPE_REQUIRED:
493
+ return decode_basic_field(stream, wire_type, field);
494
+
495
+ case PB_HTYPE_OPTIONAL:
496
+ if (field->pSize != NULL)
497
+ *(bool*)field->pSize = true;
498
+ return decode_basic_field(stream, wire_type, field);
499
+
500
+ case PB_HTYPE_REPEATED:
501
+ if (wire_type == PB_WT_STRING
502
+ && PB_LTYPE(field->type) <= PB_LTYPE_LAST_PACKABLE)
503
+ {
504
+ /* Packed array */
505
+ bool status = true;
506
+ pb_istream_t substream;
507
+ pb_size_t *size = (pb_size_t*)field->pSize;
508
+ field->pData = (char*)field->pField + field->data_size * (*size);
509
+
510
+ if (!pb_make_string_substream(stream, &substream))
511
+ return false;
512
+
513
+ while (substream.bytes_left > 0 && *size < field->array_size)
514
+ {
515
+ if (!decode_basic_field(&substream, PB_WT_PACKED, field))
516
+ {
517
+ status = false;
518
+ break;
519
+ }
520
+ (*size)++;
521
+ field->pData = (char*)field->pData + field->data_size;
522
+ }
523
+
524
+ if (substream.bytes_left != 0)
525
+ PB_RETURN_ERROR(stream, "array overflow");
526
+ if (!pb_close_string_substream(stream, &substream))
527
+ return false;
528
+
529
+ return status;
530
+ }
531
+ else
532
+ {
533
+ /* Repeated field */
534
+ pb_size_t *size = (pb_size_t*)field->pSize;
535
+ field->pData = (char*)field->pField + field->data_size * (*size);
536
+
537
+ if ((*size)++ >= field->array_size)
538
+ PB_RETURN_ERROR(stream, "array overflow");
539
+
540
+ return decode_basic_field(stream, wire_type, field);
541
+ }
542
+
543
+ case PB_HTYPE_ONEOF:
544
+ if (PB_LTYPE_IS_SUBMSG(field->type) &&
545
+ *(pb_size_t*)field->pSize != field->tag)
546
+ {
547
+ /* We memset to zero so that any callbacks are set to NULL.
548
+ * This is because the callbacks might otherwise have values
549
+ * from some other union field.
550
+ * If callbacks are needed inside oneof field, use .proto
551
+ * option submsg_callback to have a separate callback function
552
+ * that can set the fields before submessage is decoded.
553
+ * pb_dec_submessage() will set any default values. */
554
+ memset(field->pData, 0, (size_t)field->data_size);
555
+
556
+ /* Set default values for the submessage fields. */
557
+ if (field->submsg_desc->default_value != NULL ||
558
+ field->submsg_desc->field_callback != NULL ||
559
+ field->submsg_desc->submsg_info[0] != NULL)
560
+ {
561
+ pb_field_iter_t submsg_iter;
562
+ if (pb_field_iter_begin(&submsg_iter, field->submsg_desc, field->pData))
563
+ {
564
+ if (!pb_message_set_to_defaults(&submsg_iter))
565
+ PB_RETURN_ERROR(stream, "failed to set defaults");
566
+ }
567
+ }
568
+ }
569
+ *(pb_size_t*)field->pSize = field->tag;
570
+
571
+ return decode_basic_field(stream, wire_type, field);
572
+
573
+ default:
574
+ PB_RETURN_ERROR(stream, "invalid field type");
575
+ }
576
+ }
577
+
578
+ #ifdef PB_ENABLE_MALLOC
579
+ /* Allocate storage for the field and store the pointer at iter->pData.
580
+ * array_size is the number of entries to reserve in an array.
581
+ * Zero size is not allowed, use pb_free() for releasing.
582
+ */
583
+ static bool checkreturn allocate_field(pb_istream_t *stream, void *pData, size_t data_size, size_t array_size)
584
+ {
585
+ void *ptr = *(void**)pData;
586
+
587
+ if (data_size == 0 || array_size == 0)
588
+ PB_RETURN_ERROR(stream, "invalid size");
589
+
590
+ #ifdef __AVR__
591
+ /* Workaround for AVR libc bug 53284: http://savannah.nongnu.org/bugs/?53284
592
+ * Realloc to size of 1 byte can cause corruption of the malloc structures.
593
+ */
594
+ if (data_size == 1 && array_size == 1)
595
+ {
596
+ data_size = 2;
597
+ }
598
+ #endif
599
+
600
+ /* Check for multiplication overflows.
601
+ * This code avoids the costly division if the sizes are small enough.
602
+ * Multiplication is safe as long as only half of bits are set
603
+ * in either multiplicand.
604
+ */
605
+ {
606
+ const size_t check_limit = (size_t)1 << (sizeof(size_t) * 4);
607
+ if (data_size >= check_limit || array_size >= check_limit)
608
+ {
609
+ const size_t size_max = (size_t)-1;
610
+ if (size_max / array_size < data_size)
611
+ {
612
+ PB_RETURN_ERROR(stream, "size too large");
613
+ }
614
+ }
615
+ }
616
+
617
+ /* Allocate new or expand previous allocation */
618
+ /* Note: on failure the old pointer will remain in the structure,
619
+ * the message must be freed by caller also on error return. */
620
+ ptr = pb_realloc(ptr, array_size * data_size);
621
+ if (ptr == NULL)
622
+ PB_RETURN_ERROR(stream, "realloc failed");
623
+
624
+ *(void**)pData = ptr;
625
+ return true;
626
+ }
627
+
628
+ /* Clear a newly allocated item in case it contains a pointer, or is a submessage. */
629
+ static void initialize_pointer_field(void *pItem, pb_field_iter_t *field)
630
+ {
631
+ if (PB_LTYPE(field->type) == PB_LTYPE_STRING ||
632
+ PB_LTYPE(field->type) == PB_LTYPE_BYTES)
633
+ {
634
+ *(void**)pItem = NULL;
635
+ }
636
+ else if (PB_LTYPE_IS_SUBMSG(field->type))
637
+ {
638
+ /* We memset to zero so that any callbacks are set to NULL.
639
+ * Default values will be set by pb_dec_submessage(). */
640
+ memset(pItem, 0, field->data_size);
641
+ }
642
+ }
643
+ #endif
644
+
645
+ static bool checkreturn decode_pointer_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *field)
646
+ {
647
+ #ifndef PB_ENABLE_MALLOC
648
+ PB_UNUSED(wire_type);
649
+ PB_UNUSED(field);
650
+ PB_RETURN_ERROR(stream, "no malloc support");
651
+ #else
652
+ switch (PB_HTYPE(field->type))
653
+ {
654
+ case PB_HTYPE_REQUIRED:
655
+ case PB_HTYPE_OPTIONAL:
656
+ case PB_HTYPE_ONEOF:
657
+ if (PB_LTYPE_IS_SUBMSG(field->type) && *(void**)field->pField != NULL)
658
+ {
659
+ /* Duplicate field, have to release the old allocation first. */
660
+ /* FIXME: Does this work correctly for oneofs? */
661
+ pb_release_single_field(field);
662
+ }
663
+
664
+ if (PB_HTYPE(field->type) == PB_HTYPE_ONEOF)
665
+ {
666
+ *(pb_size_t*)field->pSize = field->tag;
667
+ }
668
+
669
+ if (PB_LTYPE(field->type) == PB_LTYPE_STRING ||
670
+ PB_LTYPE(field->type) == PB_LTYPE_BYTES)
671
+ {
672
+ /* pb_dec_string and pb_dec_bytes handle allocation themselves */
673
+ field->pData = field->pField;
674
+ return decode_basic_field(stream, wire_type, field);
675
+ }
676
+ else
677
+ {
678
+ if (!allocate_field(stream, field->pField, field->data_size, 1))
679
+ return false;
680
+
681
+ field->pData = *(void**)field->pField;
682
+ initialize_pointer_field(field->pData, field);
683
+ return decode_basic_field(stream, wire_type, field);
684
+ }
685
+
686
+ case PB_HTYPE_REPEATED:
687
+ if (wire_type == PB_WT_STRING
688
+ && PB_LTYPE(field->type) <= PB_LTYPE_LAST_PACKABLE)
689
+ {
690
+ /* Packed array, multiple items come in at once. */
691
+ bool status = true;
692
+ pb_size_t *size = (pb_size_t*)field->pSize;
693
+ size_t allocated_size = *size;
694
+ pb_istream_t substream;
695
+
696
+ if (!pb_make_string_substream(stream, &substream))
697
+ return false;
698
+
699
+ while (substream.bytes_left)
700
+ {
701
+ if (*size == PB_SIZE_MAX)
702
+ {
703
+ #ifndef PB_NO_ERRMSG
704
+ stream->errmsg = "too many array entries";
705
+ #endif
706
+ status = false;
707
+ break;
708
+ }
709
+
710
+ if ((size_t)*size + 1 > allocated_size)
711
+ {
712
+ /* Allocate more storage. This tries to guess the
713
+ * number of remaining entries. Round the division
714
+ * upwards. */
715
+ size_t remain = (substream.bytes_left - 1) / field->data_size + 1;
716
+ if (remain < PB_SIZE_MAX - allocated_size)
717
+ allocated_size += remain;
718
+ else
719
+ allocated_size += 1;
720
+
721
+ if (!allocate_field(&substream, field->pField, field->data_size, allocated_size))
722
+ {
723
+ status = false;
724
+ break;
725
+ }
726
+ }
727
+
728
+ /* Decode the array entry */
729
+ field->pData = *(char**)field->pField + field->data_size * (*size);
730
+ if (field->pData == NULL)
731
+ {
732
+ /* Shouldn't happen, but satisfies static analyzers */
733
+ status = false;
734
+ break;
735
+ }
736
+ initialize_pointer_field(field->pData, field);
737
+ if (!decode_basic_field(&substream, PB_WT_PACKED, field))
738
+ {
739
+ status = false;
740
+ break;
741
+ }
742
+
743
+ (*size)++;
744
+ }
745
+ if (!pb_close_string_substream(stream, &substream))
746
+ return false;
747
+
748
+ return status;
749
+ }
750
+ else
751
+ {
752
+ /* Normal repeated field, i.e. only one item at a time. */
753
+ pb_size_t *size = (pb_size_t*)field->pSize;
754
+
755
+ if (*size == PB_SIZE_MAX)
756
+ PB_RETURN_ERROR(stream, "too many array entries");
757
+
758
+ if (!allocate_field(stream, field->pField, field->data_size, (size_t)(*size + 1)))
759
+ return false;
760
+
761
+ field->pData = *(char**)field->pField + field->data_size * (*size);
762
+ (*size)++;
763
+ initialize_pointer_field(field->pData, field);
764
+ return decode_basic_field(stream, wire_type, field);
765
+ }
766
+
767
+ default:
768
+ PB_RETURN_ERROR(stream, "invalid field type");
769
+ }
770
+ #endif
771
+ }
772
+
773
+ static bool checkreturn decode_callback_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *field)
774
+ {
775
+ if (!field->descriptor->field_callback)
776
+ return pb_skip_field(stream, wire_type);
777
+
778
+ if (wire_type == PB_WT_STRING)
779
+ {
780
+ pb_istream_t substream;
781
+ size_t prev_bytes_left;
782
+
783
+ if (!pb_make_string_substream(stream, &substream))
784
+ return false;
785
+
786
+ /* If the callback field is inside a submsg, first call the submsg_callback which
787
+ * should set the decoder for the callback field. */
788
+ if (PB_LTYPE(field->type) == PB_LTYPE_SUBMSG_W_CB && field->pSize != NULL) {
789
+ pb_callback_t* callback;
790
+ *(pb_size_t*)field->pSize = field->tag;
791
+ callback = (pb_callback_t*)field->pSize - 1;
792
+
793
+ if (callback->funcs.decode)
794
+ {
795
+ if (!callback->funcs.decode(&substream, field, &callback->arg)) {
796
+ PB_SET_ERROR(stream, substream.errmsg ? substream.errmsg : "submsg callback failed");
797
+ return false;
798
+ }
799
+ }
800
+ }
801
+
802
+ do
803
+ {
804
+ prev_bytes_left = substream.bytes_left;
805
+ if (!field->descriptor->field_callback(&substream, NULL, field))
806
+ {
807
+ PB_SET_ERROR(stream, substream.errmsg ? substream.errmsg : "callback failed");
808
+ return false;
809
+ }
810
+ } while (substream.bytes_left > 0 && substream.bytes_left < prev_bytes_left);
811
+
812
+ if (!pb_close_string_substream(stream, &substream))
813
+ return false;
814
+
815
+ return true;
816
+ }
817
+ else
818
+ {
819
+ /* Copy the single scalar value to stack.
820
+ * This is required so that we can limit the stream length,
821
+ * which in turn allows to use same callback for packed and
822
+ * not-packed fields. */
823
+ pb_istream_t substream;
824
+ pb_byte_t buffer[10];
825
+ size_t size = sizeof(buffer);
826
+
827
+ if (!read_raw_value(stream, wire_type, buffer, &size))
828
+ return false;
829
+ substream = pb_istream_from_buffer(buffer, size);
830
+
831
+ return field->descriptor->field_callback(&substream, NULL, field);
832
+ }
833
+ }
834
+
835
+ static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *field)
836
+ {
837
+ #ifdef PB_ENABLE_MALLOC
838
+ /* When decoding an oneof field, check if there is old data that must be
839
+ * released first. */
840
+ if (PB_HTYPE(field->type) == PB_HTYPE_ONEOF)
841
+ {
842
+ if (!pb_release_union_field(stream, field))
843
+ return false;
844
+ }
845
+ #endif
846
+
847
+ switch (PB_ATYPE(field->type))
848
+ {
849
+ case PB_ATYPE_STATIC:
850
+ return decode_static_field(stream, wire_type, field);
851
+
852
+ case PB_ATYPE_POINTER:
853
+ return decode_pointer_field(stream, wire_type, field);
854
+
855
+ case PB_ATYPE_CALLBACK:
856
+ return decode_callback_field(stream, wire_type, field);
857
+
858
+ default:
859
+ PB_RETURN_ERROR(stream, "invalid field type");
860
+ }
861
+ }
862
+
863
+ /* Default handler for extension fields. Expects to have a pb_msgdesc_t
864
+ * pointer in the extension->type->arg field, pointing to a message with
865
+ * only one field in it. */
866
+ static bool checkreturn default_extension_decoder(pb_istream_t *stream,
867
+ pb_extension_t *extension, uint32_t tag, pb_wire_type_t wire_type)
868
+ {
869
+ pb_field_iter_t iter;
870
+
871
+ if (!pb_field_iter_begin_extension(&iter, extension))
872
+ PB_RETURN_ERROR(stream, "invalid extension");
873
+
874
+ if (iter.tag != tag || !iter.message)
875
+ return true;
876
+
877
+ extension->found = true;
878
+ return decode_field(stream, wire_type, &iter);
879
+ }
880
+
881
+ /* Try to decode an unknown field as an extension field. Tries each extension
882
+ * decoder in turn, until one of them handles the field or loop ends. */
883
+ static bool checkreturn decode_extension(pb_istream_t *stream,
884
+ uint32_t tag, pb_wire_type_t wire_type, pb_extension_t *extension)
885
+ {
886
+ size_t pos = stream->bytes_left;
887
+
888
+ while (extension != NULL && pos == stream->bytes_left)
889
+ {
890
+ bool status;
891
+ if (extension->type->decode)
892
+ status = extension->type->decode(stream, extension, tag, wire_type);
893
+ else
894
+ status = default_extension_decoder(stream, extension, tag, wire_type);
895
+
896
+ if (!status)
897
+ return false;
898
+
899
+ extension = extension->next;
900
+ }
901
+
902
+ return true;
903
+ }
904
+
905
+ /* Initialize message fields to default values, recursively */
906
+ static bool pb_field_set_to_default(pb_field_iter_t *field)
907
+ {
908
+ pb_type_t type;
909
+ type = field->type;
910
+
911
+ if (PB_LTYPE(type) == PB_LTYPE_EXTENSION)
912
+ {
913
+ pb_extension_t *ext = *(pb_extension_t* const *)field->pData;
914
+ while (ext != NULL)
915
+ {
916
+ pb_field_iter_t ext_iter;
917
+ if (pb_field_iter_begin_extension(&ext_iter, ext))
918
+ {
919
+ ext->found = false;
920
+ if (!pb_message_set_to_defaults(&ext_iter))
921
+ return false;
922
+ }
923
+ ext = ext->next;
924
+ }
925
+ }
926
+ else if (PB_ATYPE(type) == PB_ATYPE_STATIC)
927
+ {
928
+ bool init_data = true;
929
+ if (PB_HTYPE(type) == PB_HTYPE_OPTIONAL && field->pSize != NULL)
930
+ {
931
+ /* Set has_field to false. Still initialize the optional field
932
+ * itself also. */
933
+ *(bool*)field->pSize = false;
934
+ }
935
+ else if (PB_HTYPE(type) == PB_HTYPE_REPEATED ||
936
+ PB_HTYPE(type) == PB_HTYPE_ONEOF)
937
+ {
938
+ /* REPEATED: Set array count to 0, no need to initialize contents.
939
+ ONEOF: Set which_field to 0. */
940
+ *(pb_size_t*)field->pSize = 0;
941
+ init_data = false;
942
+ }
943
+
944
+ if (init_data)
945
+ {
946
+ if (PB_LTYPE_IS_SUBMSG(field->type) &&
947
+ (field->submsg_desc->default_value != NULL ||
948
+ field->submsg_desc->field_callback != NULL ||
949
+ field->submsg_desc->submsg_info[0] != NULL))
950
+ {
951
+ /* Initialize submessage to defaults.
952
+ * Only needed if it has default values
953
+ * or callback/submessage fields. */
954
+ pb_field_iter_t submsg_iter;
955
+ if (pb_field_iter_begin(&submsg_iter, field->submsg_desc, field->pData))
956
+ {
957
+ if (!pb_message_set_to_defaults(&submsg_iter))
958
+ return false;
959
+ }
960
+ }
961
+ else
962
+ {
963
+ /* Initialize to zeros */
964
+ memset(field->pData, 0, (size_t)field->data_size);
965
+ }
966
+ }
967
+ }
968
+ else if (PB_ATYPE(type) == PB_ATYPE_POINTER)
969
+ {
970
+ /* Initialize the pointer to NULL. */
971
+ *(void**)field->pField = NULL;
972
+
973
+ /* Initialize array count to 0. */
974
+ if (PB_HTYPE(type) == PB_HTYPE_REPEATED ||
975
+ PB_HTYPE(type) == PB_HTYPE_ONEOF)
976
+ {
977
+ *(pb_size_t*)field->pSize = 0;
978
+ }
979
+ }
980
+ else if (PB_ATYPE(type) == PB_ATYPE_CALLBACK)
981
+ {
982
+ /* Don't overwrite callback */
983
+ }
984
+
985
+ return true;
986
+ }
987
+
988
+ static bool pb_message_set_to_defaults(pb_field_iter_t *iter)
989
+ {
990
+ pb_istream_t defstream = PB_ISTREAM_EMPTY;
991
+ uint32_t tag = 0;
992
+ pb_wire_type_t wire_type = PB_WT_VARINT;
993
+ bool eof;
994
+
995
+ if (iter->descriptor->default_value)
996
+ {
997
+ defstream = pb_istream_from_buffer(iter->descriptor->default_value, (size_t)-1);
998
+ if (!pb_decode_tag(&defstream, &wire_type, &tag, &eof))
999
+ return false;
1000
+ }
1001
+
1002
+ do
1003
+ {
1004
+ if (!pb_field_set_to_default(iter))
1005
+ return false;
1006
+
1007
+ if (tag != 0 && iter->tag == tag)
1008
+ {
1009
+ /* We have a default value for this field in the defstream */
1010
+ if (!decode_field(&defstream, wire_type, iter))
1011
+ return false;
1012
+ if (!pb_decode_tag(&defstream, &wire_type, &tag, &eof))
1013
+ return false;
1014
+
1015
+ if (iter->pSize)
1016
+ *(bool*)iter->pSize = false;
1017
+ }
1018
+ } while (pb_field_iter_next(iter));
1019
+
1020
+ return true;
1021
+ }
1022
+
1023
+ /*********************
1024
+ * Decode all fields *
1025
+ *********************/
1026
+
1027
+ static bool checkreturn pb_decode_inner(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct, unsigned int flags)
1028
+ {
1029
+ /* If the message contains extension fields, the extension handlers
1030
+ * are called when tag number is >= extension_range_start. This precheck
1031
+ * is just for speed, and the handlers will check for precise match.
1032
+ */
1033
+ uint32_t extension_range_start = 0;
1034
+ pb_extension_t *extensions = NULL;
1035
+
1036
+ /* 'fixed_count_field' and 'fixed_count_size' track position of a repeated fixed
1037
+ * count field. This can only handle _one_ repeated fixed count field that
1038
+ * is unpacked and unordered among other (non repeated fixed count) fields.
1039
+ */
1040
+ pb_size_t fixed_count_field = PB_SIZE_MAX;
1041
+ pb_size_t fixed_count_size = 0;
1042
+ pb_size_t fixed_count_total_size = 0;
1043
+
1044
+ /* Tag and wire type of next field from the input stream */
1045
+ uint32_t tag;
1046
+ pb_wire_type_t wire_type;
1047
+ bool eof;
1048
+
1049
+ /* Track presence of required fields */
1050
+ pb_fields_seen_t fields_seen = {{0, 0}};
1051
+ const uint32_t allbits = ~(uint32_t)0;
1052
+
1053
+ /* Descriptor for the structure field matching the tag decoded from stream */
1054
+ pb_field_iter_t iter;
1055
+
1056
+ if (pb_field_iter_begin(&iter, fields, dest_struct))
1057
+ {
1058
+ if ((flags & PB_DECODE_NOINIT) == 0)
1059
+ {
1060
+ if (!pb_message_set_to_defaults(&iter))
1061
+ PB_RETURN_ERROR(stream, "failed to set defaults");
1062
+ }
1063
+ }
1064
+
1065
+ while (pb_decode_tag(stream, &wire_type, &tag, &eof))
1066
+ {
1067
+ if (tag == 0)
1068
+ {
1069
+ if (flags & PB_DECODE_NULLTERMINATED)
1070
+ {
1071
+ eof = true;
1072
+ break;
1073
+ }
1074
+ else
1075
+ {
1076
+ PB_RETURN_ERROR(stream, "zero tag");
1077
+ }
1078
+ }
1079
+
1080
+ if (!pb_field_iter_find(&iter, tag) || PB_LTYPE(iter.type) == PB_LTYPE_EXTENSION)
1081
+ {
1082
+ /* No match found, check if it matches an extension. */
1083
+ if (extension_range_start == 0)
1084
+ {
1085
+ if (pb_field_iter_find_extension(&iter))
1086
+ {
1087
+ extensions = *(pb_extension_t* const *)iter.pData;
1088
+ extension_range_start = iter.tag;
1089
+ }
1090
+
1091
+ if (!extensions)
1092
+ {
1093
+ extension_range_start = (uint32_t)-1;
1094
+ }
1095
+ }
1096
+
1097
+ if (tag >= extension_range_start)
1098
+ {
1099
+ size_t pos = stream->bytes_left;
1100
+
1101
+ if (!decode_extension(stream, tag, wire_type, extensions))
1102
+ return false;
1103
+
1104
+ if (pos != stream->bytes_left)
1105
+ {
1106
+ /* The field was handled */
1107
+ continue;
1108
+ }
1109
+ }
1110
+
1111
+ /* No match found, skip data */
1112
+ if (!pb_skip_field(stream, wire_type))
1113
+ return false;
1114
+ continue;
1115
+ }
1116
+
1117
+ /* If a repeated fixed count field was found, get size from
1118
+ * 'fixed_count_field' as there is no counter contained in the struct.
1119
+ */
1120
+ if (PB_HTYPE(iter.type) == PB_HTYPE_REPEATED && iter.pSize == &iter.array_size)
1121
+ {
1122
+ if (fixed_count_field != iter.index) {
1123
+ /* If the new fixed count field does not match the previous one,
1124
+ * check that the previous one is NULL or that it finished
1125
+ * receiving all the expected data.
1126
+ */
1127
+ if (fixed_count_field != PB_SIZE_MAX &&
1128
+ fixed_count_size != fixed_count_total_size)
1129
+ {
1130
+ PB_RETURN_ERROR(stream, "wrong size for fixed count field");
1131
+ }
1132
+
1133
+ fixed_count_field = iter.index;
1134
+ fixed_count_size = 0;
1135
+ fixed_count_total_size = iter.array_size;
1136
+ }
1137
+
1138
+ iter.pSize = &fixed_count_size;
1139
+ }
1140
+
1141
+ if (PB_HTYPE(iter.type) == PB_HTYPE_REQUIRED
1142
+ && iter.required_field_index < PB_MAX_REQUIRED_FIELDS)
1143
+ {
1144
+ uint32_t tmp = ((uint32_t)1 << (iter.required_field_index & 31));
1145
+ fields_seen.bitfield[iter.required_field_index >> 5] |= tmp;
1146
+ }
1147
+
1148
+ if (!decode_field(stream, wire_type, &iter))
1149
+ return false;
1150
+ }
1151
+
1152
+ if (!eof)
1153
+ {
1154
+ /* pb_decode_tag() returned error before end of stream */
1155
+ return false;
1156
+ }
1157
+
1158
+ /* Check that all elements of the last decoded fixed count field were present. */
1159
+ if (fixed_count_field != PB_SIZE_MAX &&
1160
+ fixed_count_size != fixed_count_total_size)
1161
+ {
1162
+ PB_RETURN_ERROR(stream, "wrong size for fixed count field");
1163
+ }
1164
+
1165
+ /* Check that all required fields were present. */
1166
+ {
1167
+ pb_size_t req_field_count = iter.descriptor->required_field_count;
1168
+
1169
+ if (req_field_count > 0)
1170
+ {
1171
+ pb_size_t i;
1172
+
1173
+ if (req_field_count > PB_MAX_REQUIRED_FIELDS)
1174
+ req_field_count = PB_MAX_REQUIRED_FIELDS;
1175
+
1176
+ /* Check the whole words */
1177
+ for (i = 0; i < (req_field_count >> 5); i++)
1178
+ {
1179
+ if (fields_seen.bitfield[i] != allbits)
1180
+ PB_RETURN_ERROR(stream, "missing required field");
1181
+ }
1182
+
1183
+ /* Check the remaining bits (if any) */
1184
+ if ((req_field_count & 31) != 0)
1185
+ {
1186
+ if (fields_seen.bitfield[req_field_count >> 5] !=
1187
+ (allbits >> (uint_least8_t)(32 - (req_field_count & 31))))
1188
+ {
1189
+ PB_RETURN_ERROR(stream, "missing required field");
1190
+ }
1191
+ }
1192
+ }
1193
+ }
1194
+
1195
+ return true;
1196
+ }
1197
+
1198
+ bool checkreturn pb_decode_ex(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct, unsigned int flags)
1199
+ {
1200
+ bool status;
1201
+
1202
+ if ((flags & PB_DECODE_DELIMITED) == 0)
1203
+ {
1204
+ status = pb_decode_inner(stream, fields, dest_struct, flags);
1205
+ }
1206
+ else
1207
+ {
1208
+ pb_istream_t substream;
1209
+ if (!pb_make_string_substream(stream, &substream))
1210
+ return false;
1211
+
1212
+ status = pb_decode_inner(&substream, fields, dest_struct, flags);
1213
+
1214
+ if (!pb_close_string_substream(stream, &substream))
1215
+ status = false;
1216
+ }
1217
+
1218
+ #ifdef PB_ENABLE_MALLOC
1219
+ if (!status)
1220
+ pb_release(fields, dest_struct);
1221
+ #endif
1222
+
1223
+ return status;
1224
+ }
1225
+
1226
+ bool checkreturn pb_decode(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct)
1227
+ {
1228
+ return pb_decode_ex(stream, fields, dest_struct, 0);
1229
+ }
1230
+
1231
+ #ifdef PB_ENABLE_MALLOC
1232
+ /* Given an oneof field, if there has already been a field inside this oneof,
1233
+ * release it before overwriting with a different one. */
1234
+ static bool pb_release_union_field(pb_istream_t *stream, pb_field_iter_t *field)
1235
+ {
1236
+ pb_field_iter_t old_field = *field;
1237
+ pb_size_t old_tag = *(pb_size_t*)field->pSize; /* Previous which_ value */
1238
+ pb_size_t new_tag = field->tag; /* New which_ value */
1239
+
1240
+ if (old_tag == 0)
1241
+ return true; /* Ok, no old data in union */
1242
+
1243
+ if (old_tag == new_tag)
1244
+ return true; /* Ok, old data is of same type => merge */
1245
+
1246
+ /* Release old data. The find can fail if the message struct contains
1247
+ * invalid data. */
1248
+ if (!pb_field_iter_find(&old_field, old_tag))
1249
+ PB_RETURN_ERROR(stream, "invalid union tag");
1250
+
1251
+ pb_release_single_field(&old_field);
1252
+
1253
+ if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
1254
+ {
1255
+ /* Initialize the pointer to NULL to make sure it is valid
1256
+ * even in case of error return. */
1257
+ *(void**)field->pField = NULL;
1258
+ field->pData = NULL;
1259
+ }
1260
+
1261
+ return true;
1262
+ }
1263
+
1264
+ static void pb_release_single_field(pb_field_iter_t *field)
1265
+ {
1266
+ pb_type_t type;
1267
+ type = field->type;
1268
+
1269
+ if (PB_HTYPE(type) == PB_HTYPE_ONEOF)
1270
+ {
1271
+ if (*(pb_size_t*)field->pSize != field->tag)
1272
+ return; /* This is not the current field in the union */
1273
+ }
1274
+
1275
+ /* Release anything contained inside an extension or submsg.
1276
+ * This has to be done even if the submsg itself is statically
1277
+ * allocated. */
1278
+ if (PB_LTYPE(type) == PB_LTYPE_EXTENSION)
1279
+ {
1280
+ /* Release fields from all extensions in the linked list */
1281
+ pb_extension_t *ext = *(pb_extension_t**)field->pData;
1282
+ while (ext != NULL)
1283
+ {
1284
+ pb_field_iter_t ext_iter;
1285
+ if (pb_field_iter_begin_extension(&ext_iter, ext))
1286
+ {
1287
+ pb_release_single_field(&ext_iter);
1288
+ }
1289
+ ext = ext->next;
1290
+ }
1291
+ }
1292
+ else if (PB_LTYPE_IS_SUBMSG(type) && PB_ATYPE(type) != PB_ATYPE_CALLBACK)
1293
+ {
1294
+ /* Release fields in submessage or submsg array */
1295
+ pb_size_t count = 1;
1296
+
1297
+ if (PB_ATYPE(type) == PB_ATYPE_POINTER)
1298
+ {
1299
+ field->pData = *(void**)field->pField;
1300
+ }
1301
+ else
1302
+ {
1303
+ field->pData = field->pField;
1304
+ }
1305
+
1306
+ if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
1307
+ {
1308
+ count = *(pb_size_t*)field->pSize;
1309
+
1310
+ if (PB_ATYPE(type) == PB_ATYPE_STATIC && count > field->array_size)
1311
+ {
1312
+ /* Protect against corrupted _count fields */
1313
+ count = field->array_size;
1314
+ }
1315
+ }
1316
+
1317
+ if (field->pData)
1318
+ {
1319
+ for (; count > 0; count--)
1320
+ {
1321
+ pb_release(field->submsg_desc, field->pData);
1322
+ field->pData = (char*)field->pData + field->data_size;
1323
+ }
1324
+ }
1325
+ }
1326
+
1327
+ if (PB_ATYPE(type) == PB_ATYPE_POINTER)
1328
+ {
1329
+ if (PB_HTYPE(type) == PB_HTYPE_REPEATED &&
1330
+ (PB_LTYPE(type) == PB_LTYPE_STRING ||
1331
+ PB_LTYPE(type) == PB_LTYPE_BYTES))
1332
+ {
1333
+ /* Release entries in repeated string or bytes array */
1334
+ void **pItem = *(void***)field->pField;
1335
+ pb_size_t count = *(pb_size_t*)field->pSize;
1336
+ for (; count > 0; count--)
1337
+ {
1338
+ pb_free(*pItem);
1339
+ *pItem++ = NULL;
1340
+ }
1341
+ }
1342
+
1343
+ if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
1344
+ {
1345
+ /* We are going to release the array, so set the size to 0 */
1346
+ *(pb_size_t*)field->pSize = 0;
1347
+ }
1348
+
1349
+ /* Release main pointer */
1350
+ pb_free(*(void**)field->pField);
1351
+ *(void**)field->pField = NULL;
1352
+ }
1353
+ }
1354
+
1355
+ void pb_release(const pb_msgdesc_t *fields, void *dest_struct)
1356
+ {
1357
+ pb_field_iter_t iter;
1358
+
1359
+ if (!dest_struct)
1360
+ return; /* Ignore NULL pointers, similar to free() */
1361
+
1362
+ if (!pb_field_iter_begin(&iter, fields, dest_struct))
1363
+ return; /* Empty message type */
1364
+
1365
+ do
1366
+ {
1367
+ pb_release_single_field(&iter);
1368
+ } while (pb_field_iter_next(&iter));
1369
+ }
1370
+ #else
1371
+ void pb_release(const pb_msgdesc_t *fields, void *dest_struct)
1372
+ {
1373
+ /* Nothing to release without PB_ENABLE_MALLOC. */
1374
+ PB_UNUSED(fields);
1375
+ PB_UNUSED(dest_struct);
1376
+ }
1377
+ #endif
1378
+
1379
+ /* Field decoders */
1380
+
1381
+ bool pb_decode_bool(pb_istream_t *stream, bool *dest)
1382
+ {
1383
+ uint32_t value;
1384
+ if (!pb_decode_varint32(stream, &value))
1385
+ return false;
1386
+
1387
+ *(bool*)dest = (value != 0);
1388
+ return true;
1389
+ }
1390
+
1391
+ bool pb_decode_svarint(pb_istream_t *stream, pb_int64_t *dest)
1392
+ {
1393
+ pb_uint64_t value;
1394
+ if (!pb_decode_varint(stream, &value))
1395
+ return false;
1396
+
1397
+ if (value & 1)
1398
+ *dest = (pb_int64_t)(~(value >> 1));
1399
+ else
1400
+ *dest = (pb_int64_t)(value >> 1);
1401
+
1402
+ return true;
1403
+ }
1404
+
1405
+ bool pb_decode_fixed32(pb_istream_t *stream, void *dest)
1406
+ {
1407
+ union {
1408
+ uint32_t fixed32;
1409
+ pb_byte_t bytes[4];
1410
+ } u;
1411
+
1412
+ if (!pb_read(stream, u.bytes, 4))
1413
+ return false;
1414
+
1415
+ #if defined(PB_LITTLE_ENDIAN_8BIT) && PB_LITTLE_ENDIAN_8BIT == 1
1416
+ /* fast path - if we know that we're on little endian, assign directly */
1417
+ *(uint32_t*)dest = u.fixed32;
1418
+ #else
1419
+ *(uint32_t*)dest = ((uint32_t)u.bytes[0] << 0) |
1420
+ ((uint32_t)u.bytes[1] << 8) |
1421
+ ((uint32_t)u.bytes[2] << 16) |
1422
+ ((uint32_t)u.bytes[3] << 24);
1423
+ #endif
1424
+ return true;
1425
+ }
1426
+
1427
+ #ifndef PB_WITHOUT_64BIT
1428
+ bool pb_decode_fixed64(pb_istream_t *stream, void *dest)
1429
+ {
1430
+ union {
1431
+ uint64_t fixed64;
1432
+ pb_byte_t bytes[8];
1433
+ } u;
1434
+
1435
+ if (!pb_read(stream, u.bytes, 8))
1436
+ return false;
1437
+
1438
+ #if defined(PB_LITTLE_ENDIAN_8BIT) && PB_LITTLE_ENDIAN_8BIT == 1
1439
+ /* fast path - if we know that we're on little endian, assign directly */
1440
+ *(uint64_t*)dest = u.fixed64;
1441
+ #else
1442
+ *(uint64_t*)dest = ((uint64_t)u.bytes[0] << 0) |
1443
+ ((uint64_t)u.bytes[1] << 8) |
1444
+ ((uint64_t)u.bytes[2] << 16) |
1445
+ ((uint64_t)u.bytes[3] << 24) |
1446
+ ((uint64_t)u.bytes[4] << 32) |
1447
+ ((uint64_t)u.bytes[5] << 40) |
1448
+ ((uint64_t)u.bytes[6] << 48) |
1449
+ ((uint64_t)u.bytes[7] << 56);
1450
+ #endif
1451
+ return true;
1452
+ }
1453
+ #endif
1454
+
1455
+ static bool checkreturn pb_dec_bool(pb_istream_t *stream, const pb_field_iter_t *field)
1456
+ {
1457
+ return pb_decode_bool(stream, (bool*)field->pData);
1458
+ }
1459
+
1460
+ static bool checkreturn pb_dec_varint(pb_istream_t *stream, const pb_field_iter_t *field)
1461
+ {
1462
+ if (PB_LTYPE(field->type) == PB_LTYPE_UVARINT)
1463
+ {
1464
+ pb_uint64_t value, clamped;
1465
+ if (!pb_decode_varint(stream, &value))
1466
+ return false;
1467
+
1468
+ /* Cast to the proper field size, while checking for overflows */
1469
+ if (field->data_size == sizeof(pb_uint64_t))
1470
+ clamped = *(pb_uint64_t*)field->pData = value;
1471
+ else if (field->data_size == sizeof(uint32_t))
1472
+ clamped = *(uint32_t*)field->pData = (uint32_t)value;
1473
+ else if (field->data_size == sizeof(uint_least16_t))
1474
+ clamped = *(uint_least16_t*)field->pData = (uint_least16_t)value;
1475
+ else if (field->data_size == sizeof(uint_least8_t))
1476
+ clamped = *(uint_least8_t*)field->pData = (uint_least8_t)value;
1477
+ else
1478
+ PB_RETURN_ERROR(stream, "invalid data_size");
1479
+
1480
+ if (clamped != value)
1481
+ PB_RETURN_ERROR(stream, "integer too large");
1482
+
1483
+ return true;
1484
+ }
1485
+ else
1486
+ {
1487
+ pb_uint64_t value;
1488
+ pb_int64_t svalue;
1489
+ pb_int64_t clamped;
1490
+
1491
+ if (PB_LTYPE(field->type) == PB_LTYPE_SVARINT)
1492
+ {
1493
+ if (!pb_decode_svarint(stream, &svalue))
1494
+ return false;
1495
+ }
1496
+ else
1497
+ {
1498
+ if (!pb_decode_varint(stream, &value))
1499
+ return false;
1500
+
1501
+ /* See issue 97: Google's C++ protobuf allows negative varint values to
1502
+ * be cast as int32_t, instead of the int64_t that should be used when
1503
+ * encoding. Nanopb versions before 0.2.5 had a bug in encoding. In order to
1504
+ * not break decoding of such messages, we cast <=32 bit fields to
1505
+ * int32_t first to get the sign correct.
1506
+ */
1507
+ if (field->data_size == sizeof(pb_int64_t))
1508
+ svalue = (pb_int64_t)value;
1509
+ else
1510
+ svalue = (int32_t)value;
1511
+ }
1512
+
1513
+ /* Cast to the proper field size, while checking for overflows */
1514
+ if (field->data_size == sizeof(pb_int64_t))
1515
+ clamped = *(pb_int64_t*)field->pData = svalue;
1516
+ else if (field->data_size == sizeof(int32_t))
1517
+ clamped = *(int32_t*)field->pData = (int32_t)svalue;
1518
+ else if (field->data_size == sizeof(int_least16_t))
1519
+ clamped = *(int_least16_t*)field->pData = (int_least16_t)svalue;
1520
+ else if (field->data_size == sizeof(int_least8_t))
1521
+ clamped = *(int_least8_t*)field->pData = (int_least8_t)svalue;
1522
+ else
1523
+ PB_RETURN_ERROR(stream, "invalid data_size");
1524
+
1525
+ if (clamped != svalue)
1526
+ PB_RETURN_ERROR(stream, "integer too large");
1527
+
1528
+ return true;
1529
+ }
1530
+ }
1531
+
1532
+ static bool checkreturn pb_dec_bytes(pb_istream_t *stream, const pb_field_iter_t *field)
1533
+ {
1534
+ uint32_t size;
1535
+ size_t alloc_size;
1536
+ pb_bytes_array_t *dest;
1537
+
1538
+ if (!pb_decode_varint32(stream, &size))
1539
+ return false;
1540
+
1541
+ if (size > PB_SIZE_MAX)
1542
+ PB_RETURN_ERROR(stream, "bytes overflow");
1543
+
1544
+ alloc_size = PB_BYTES_ARRAY_T_ALLOCSIZE(size);
1545
+ if (size > alloc_size)
1546
+ PB_RETURN_ERROR(stream, "size too large");
1547
+
1548
+ if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
1549
+ {
1550
+ #ifndef PB_ENABLE_MALLOC
1551
+ PB_RETURN_ERROR(stream, "no malloc support");
1552
+ #else
1553
+ if (stream->bytes_left < size)
1554
+ PB_RETURN_ERROR(stream, "end-of-stream");
1555
+
1556
+ if (!allocate_field(stream, field->pData, alloc_size, 1))
1557
+ return false;
1558
+ dest = *(pb_bytes_array_t**)field->pData;
1559
+ #endif
1560
+ }
1561
+ else
1562
+ {
1563
+ if (alloc_size > field->data_size)
1564
+ PB_RETURN_ERROR(stream, "bytes overflow");
1565
+ dest = (pb_bytes_array_t*)field->pData;
1566
+ }
1567
+
1568
+ dest->size = (pb_size_t)size;
1569
+ return pb_read(stream, dest->bytes, (size_t)size);
1570
+ }
1571
+
1572
+ static bool checkreturn pb_dec_string(pb_istream_t *stream, const pb_field_iter_t *field)
1573
+ {
1574
+ uint32_t size;
1575
+ size_t alloc_size;
1576
+ pb_byte_t *dest = (pb_byte_t*)field->pData;
1577
+
1578
+ if (!pb_decode_varint32(stream, &size))
1579
+ return false;
1580
+
1581
+ if (size == (uint32_t)-1)
1582
+ PB_RETURN_ERROR(stream, "size too large");
1583
+
1584
+ /* Space for null terminator */
1585
+ alloc_size = (size_t)(size + 1);
1586
+
1587
+ if (alloc_size < size)
1588
+ PB_RETURN_ERROR(stream, "size too large");
1589
+
1590
+ if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
1591
+ {
1592
+ #ifndef PB_ENABLE_MALLOC
1593
+ PB_RETURN_ERROR(stream, "no malloc support");
1594
+ #else
1595
+ if (stream->bytes_left < size)
1596
+ PB_RETURN_ERROR(stream, "end-of-stream");
1597
+
1598
+ if (!allocate_field(stream, field->pData, alloc_size, 1))
1599
+ return false;
1600
+ dest = *(pb_byte_t**)field->pData;
1601
+ #endif
1602
+ }
1603
+ else
1604
+ {
1605
+ if (alloc_size > field->data_size)
1606
+ PB_RETURN_ERROR(stream, "string overflow");
1607
+ }
1608
+
1609
+ dest[size] = 0;
1610
+
1611
+ if (!pb_read(stream, dest, (size_t)size))
1612
+ return false;
1613
+
1614
+ #ifdef PB_VALIDATE_UTF8
1615
+ if (!pb_validate_utf8((const char*)dest))
1616
+ PB_RETURN_ERROR(stream, "invalid utf8");
1617
+ #endif
1618
+
1619
+ return true;
1620
+ }
1621
+
1622
+ static bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_iter_t *field)
1623
+ {
1624
+ bool status = true;
1625
+ bool submsg_consumed = false;
1626
+ pb_istream_t substream;
1627
+
1628
+ if (!pb_make_string_substream(stream, &substream))
1629
+ return false;
1630
+
1631
+ if (field->submsg_desc == NULL)
1632
+ PB_RETURN_ERROR(stream, "invalid field descriptor");
1633
+
1634
+ /* Submessages can have a separate message-level callback that is called
1635
+ * before decoding the message. Typically it is used to set callback fields
1636
+ * inside oneofs. */
1637
+ if (PB_LTYPE(field->type) == PB_LTYPE_SUBMSG_W_CB && field->pSize != NULL)
1638
+ {
1639
+ /* Message callback is stored right before pSize. */
1640
+ pb_callback_t *callback = (pb_callback_t*)field->pSize - 1;
1641
+ if (callback->funcs.decode)
1642
+ {
1643
+ status = callback->funcs.decode(&substream, field, &callback->arg);
1644
+
1645
+ if (substream.bytes_left == 0)
1646
+ {
1647
+ submsg_consumed = true;
1648
+ }
1649
+ }
1650
+ }
1651
+
1652
+ /* Now decode the submessage contents */
1653
+ if (status && !submsg_consumed)
1654
+ {
1655
+ unsigned int flags = 0;
1656
+
1657
+ /* Static required/optional fields are already initialized by top-level
1658
+ * pb_decode(), no need to initialize them again. */
1659
+ if (PB_ATYPE(field->type) == PB_ATYPE_STATIC &&
1660
+ PB_HTYPE(field->type) != PB_HTYPE_REPEATED)
1661
+ {
1662
+ flags = PB_DECODE_NOINIT;
1663
+ }
1664
+
1665
+ status = pb_decode_inner(&substream, field->submsg_desc, field->pData, flags);
1666
+ }
1667
+
1668
+ if (!pb_close_string_substream(stream, &substream))
1669
+ return false;
1670
+
1671
+ return status;
1672
+ }
1673
+
1674
+ static bool checkreturn pb_dec_fixed_length_bytes(pb_istream_t *stream, const pb_field_iter_t *field)
1675
+ {
1676
+ uint32_t size;
1677
+
1678
+ if (!pb_decode_varint32(stream, &size))
1679
+ return false;
1680
+
1681
+ if (size > PB_SIZE_MAX)
1682
+ PB_RETURN_ERROR(stream, "bytes overflow");
1683
+
1684
+ if (size == 0)
1685
+ {
1686
+ /* As a special case, treat empty bytes string as all zeros for fixed_length_bytes. */
1687
+ memset(field->pData, 0, (size_t)field->data_size);
1688
+ return true;
1689
+ }
1690
+
1691
+ if (size != field->data_size)
1692
+ PB_RETURN_ERROR(stream, "incorrect fixed length bytes size");
1693
+
1694
+ return pb_read(stream, (pb_byte_t*)field->pData, (size_t)field->data_size);
1695
+ }
1696
+
1697
+ #ifdef PB_CONVERT_DOUBLE_FLOAT
1698
+ bool pb_decode_double_as_float(pb_istream_t *stream, float *dest)
1699
+ {
1700
+ uint_least8_t sign;
1701
+ int exponent;
1702
+ uint32_t mantissa;
1703
+ uint64_t value;
1704
+ union { float f; uint32_t i; } out;
1705
+
1706
+ if (!pb_decode_fixed64(stream, &value))
1707
+ return false;
1708
+
1709
+ /* Decompose input value */
1710
+ sign = (uint_least8_t)((value >> 63) & 1);
1711
+ exponent = (int)((value >> 52) & 0x7FF) - 1023;
1712
+ mantissa = (value >> 28) & 0xFFFFFF; /* Highest 24 bits */
1713
+
1714
+ /* Figure if value is in range representable by floats. */
1715
+ if (exponent == 1024)
1716
+ {
1717
+ /* Special value */
1718
+ exponent = 128;
1719
+ mantissa >>= 1;
1720
+ }
1721
+ else
1722
+ {
1723
+ if (exponent > 127)
1724
+ {
1725
+ /* Too large, convert to infinity */
1726
+ exponent = 128;
1727
+ mantissa = 0;
1728
+ }
1729
+ else if (exponent < -150)
1730
+ {
1731
+ /* Too small, convert to zero */
1732
+ exponent = -127;
1733
+ mantissa = 0;
1734
+ }
1735
+ else if (exponent < -126)
1736
+ {
1737
+ /* Denormalized */
1738
+ mantissa |= 0x1000000;
1739
+ mantissa >>= (-126 - exponent);
1740
+ exponent = -127;
1741
+ }
1742
+
1743
+ /* Round off mantissa */
1744
+ mantissa = (mantissa + 1) >> 1;
1745
+
1746
+ /* Check if mantissa went over 2.0 */
1747
+ if (mantissa & 0x800000)
1748
+ {
1749
+ exponent += 1;
1750
+ mantissa &= 0x7FFFFF;
1751
+ mantissa >>= 1;
1752
+ }
1753
+ }
1754
+
1755
+ /* Combine fields */
1756
+ out.i = mantissa;
1757
+ out.i |= (uint32_t)(exponent + 127) << 23;
1758
+ out.i |= (uint32_t)sign << 31;
1759
+
1760
+ *dest = out.f;
1761
+ return true;
1762
+ }
1763
+ #endif