message_xdr.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. package protocol
  2. import (
  3. "bytes"
  4. "io"
  5. "github.com/calmh/syncthing/xdr"
  6. )
  7. func (o IndexMessage) EncodeXDR(w io.Writer) (int, error) {
  8. var xw = xdr.NewWriter(w)
  9. return o.encodeXDR(xw)
  10. }
  11. func (o IndexMessage) MarshalXDR() []byte {
  12. var aw = make(xdr.AppendWriter, 0, 128)
  13. var xw = xdr.NewWriter(&aw)
  14. o.encodeXDR(xw)
  15. return []byte(aw)
  16. }
  17. func (o IndexMessage) encodeXDR(xw *xdr.Writer) (int, error) {
  18. if len(o.Repository) > 64 {
  19. return xw.Tot(), xdr.ErrElementSizeExceeded
  20. }
  21. xw.WriteString(o.Repository)
  22. if len(o.Files) > 10000000 {
  23. return xw.Tot(), xdr.ErrElementSizeExceeded
  24. }
  25. xw.WriteUint32(uint32(len(o.Files)))
  26. for i := range o.Files {
  27. o.Files[i].encodeXDR(xw)
  28. }
  29. return xw.Tot(), xw.Error()
  30. }
  31. func (o *IndexMessage) DecodeXDR(r io.Reader) error {
  32. xr := xdr.NewReader(r)
  33. return o.decodeXDR(xr)
  34. }
  35. func (o *IndexMessage) UnmarshalXDR(bs []byte) error {
  36. var br = bytes.NewReader(bs)
  37. var xr = xdr.NewReader(br)
  38. return o.decodeXDR(xr)
  39. }
  40. func (o *IndexMessage) decodeXDR(xr *xdr.Reader) error {
  41. o.Repository = xr.ReadStringMax(64)
  42. _FilesSize := int(xr.ReadUint32())
  43. if _FilesSize > 10000000 {
  44. return xdr.ErrElementSizeExceeded
  45. }
  46. o.Files = make([]FileInfo, _FilesSize)
  47. for i := range o.Files {
  48. (&o.Files[i]).decodeXDR(xr)
  49. }
  50. return xr.Error()
  51. }
  52. func (o FileInfo) EncodeXDR(w io.Writer) (int, error) {
  53. var xw = xdr.NewWriter(w)
  54. return o.encodeXDR(xw)
  55. }
  56. func (o FileInfo) MarshalXDR() []byte {
  57. var aw = make(xdr.AppendWriter, 0, 128)
  58. var xw = xdr.NewWriter(&aw)
  59. o.encodeXDR(xw)
  60. return []byte(aw)
  61. }
  62. func (o FileInfo) encodeXDR(xw *xdr.Writer) (int, error) {
  63. if len(o.Name) > 1024 {
  64. return xw.Tot(), xdr.ErrElementSizeExceeded
  65. }
  66. xw.WriteString(o.Name)
  67. xw.WriteUint32(o.Flags)
  68. xw.WriteUint64(uint64(o.Modified))
  69. xw.WriteUint64(o.Version)
  70. if len(o.Blocks) > 1000000 {
  71. return xw.Tot(), xdr.ErrElementSizeExceeded
  72. }
  73. xw.WriteUint32(uint32(len(o.Blocks)))
  74. for i := range o.Blocks {
  75. o.Blocks[i].encodeXDR(xw)
  76. }
  77. return xw.Tot(), xw.Error()
  78. }
  79. func (o *FileInfo) DecodeXDR(r io.Reader) error {
  80. xr := xdr.NewReader(r)
  81. return o.decodeXDR(xr)
  82. }
  83. func (o *FileInfo) UnmarshalXDR(bs []byte) error {
  84. var br = bytes.NewReader(bs)
  85. var xr = xdr.NewReader(br)
  86. return o.decodeXDR(xr)
  87. }
  88. func (o *FileInfo) decodeXDR(xr *xdr.Reader) error {
  89. o.Name = xr.ReadStringMax(1024)
  90. o.Flags = xr.ReadUint32()
  91. o.Modified = int64(xr.ReadUint64())
  92. o.Version = xr.ReadUint64()
  93. _BlocksSize := int(xr.ReadUint32())
  94. if _BlocksSize > 1000000 {
  95. return xdr.ErrElementSizeExceeded
  96. }
  97. o.Blocks = make([]BlockInfo, _BlocksSize)
  98. for i := range o.Blocks {
  99. (&o.Blocks[i]).decodeXDR(xr)
  100. }
  101. return xr.Error()
  102. }
  103. func (o BlockInfo) EncodeXDR(w io.Writer) (int, error) {
  104. var xw = xdr.NewWriter(w)
  105. return o.encodeXDR(xw)
  106. }
  107. func (o BlockInfo) MarshalXDR() []byte {
  108. var aw = make(xdr.AppendWriter, 0, 128)
  109. var xw = xdr.NewWriter(&aw)
  110. o.encodeXDR(xw)
  111. return []byte(aw)
  112. }
  113. func (o BlockInfo) encodeXDR(xw *xdr.Writer) (int, error) {
  114. xw.WriteUint32(o.Size)
  115. if len(o.Hash) > 64 {
  116. return xw.Tot(), xdr.ErrElementSizeExceeded
  117. }
  118. xw.WriteBytes(o.Hash)
  119. return xw.Tot(), xw.Error()
  120. }
  121. func (o *BlockInfo) DecodeXDR(r io.Reader) error {
  122. xr := xdr.NewReader(r)
  123. return o.decodeXDR(xr)
  124. }
  125. func (o *BlockInfo) UnmarshalXDR(bs []byte) error {
  126. var br = bytes.NewReader(bs)
  127. var xr = xdr.NewReader(br)
  128. return o.decodeXDR(xr)
  129. }
  130. func (o *BlockInfo) decodeXDR(xr *xdr.Reader) error {
  131. o.Size = xr.ReadUint32()
  132. o.Hash = xr.ReadBytesMax(64)
  133. return xr.Error()
  134. }
  135. func (o RequestMessage) EncodeXDR(w io.Writer) (int, error) {
  136. var xw = xdr.NewWriter(w)
  137. return o.encodeXDR(xw)
  138. }
  139. func (o RequestMessage) MarshalXDR() []byte {
  140. var aw = make(xdr.AppendWriter, 0, 128)
  141. var xw = xdr.NewWriter(&aw)
  142. o.encodeXDR(xw)
  143. return []byte(aw)
  144. }
  145. func (o RequestMessage) encodeXDR(xw *xdr.Writer) (int, error) {
  146. if len(o.Repository) > 64 {
  147. return xw.Tot(), xdr.ErrElementSizeExceeded
  148. }
  149. xw.WriteString(o.Repository)
  150. if len(o.Name) > 1024 {
  151. return xw.Tot(), xdr.ErrElementSizeExceeded
  152. }
  153. xw.WriteString(o.Name)
  154. xw.WriteUint64(o.Offset)
  155. xw.WriteUint32(o.Size)
  156. return xw.Tot(), xw.Error()
  157. }
  158. func (o *RequestMessage) DecodeXDR(r io.Reader) error {
  159. xr := xdr.NewReader(r)
  160. return o.decodeXDR(xr)
  161. }
  162. func (o *RequestMessage) UnmarshalXDR(bs []byte) error {
  163. var br = bytes.NewReader(bs)
  164. var xr = xdr.NewReader(br)
  165. return o.decodeXDR(xr)
  166. }
  167. func (o *RequestMessage) decodeXDR(xr *xdr.Reader) error {
  168. o.Repository = xr.ReadStringMax(64)
  169. o.Name = xr.ReadStringMax(1024)
  170. o.Offset = xr.ReadUint64()
  171. o.Size = xr.ReadUint32()
  172. return xr.Error()
  173. }
  174. func (o ClusterConfigMessage) EncodeXDR(w io.Writer) (int, error) {
  175. var xw = xdr.NewWriter(w)
  176. return o.encodeXDR(xw)
  177. }
  178. func (o ClusterConfigMessage) MarshalXDR() []byte {
  179. var aw = make(xdr.AppendWriter, 0, 128)
  180. var xw = xdr.NewWriter(&aw)
  181. o.encodeXDR(xw)
  182. return []byte(aw)
  183. }
  184. func (o ClusterConfigMessage) encodeXDR(xw *xdr.Writer) (int, error) {
  185. if len(o.ClientName) > 64 {
  186. return xw.Tot(), xdr.ErrElementSizeExceeded
  187. }
  188. xw.WriteString(o.ClientName)
  189. if len(o.ClientVersion) > 64 {
  190. return xw.Tot(), xdr.ErrElementSizeExceeded
  191. }
  192. xw.WriteString(o.ClientVersion)
  193. if len(o.Repositories) > 64 {
  194. return xw.Tot(), xdr.ErrElementSizeExceeded
  195. }
  196. xw.WriteUint32(uint32(len(o.Repositories)))
  197. for i := range o.Repositories {
  198. o.Repositories[i].encodeXDR(xw)
  199. }
  200. if len(o.Options) > 64 {
  201. return xw.Tot(), xdr.ErrElementSizeExceeded
  202. }
  203. xw.WriteUint32(uint32(len(o.Options)))
  204. for i := range o.Options {
  205. o.Options[i].encodeXDR(xw)
  206. }
  207. return xw.Tot(), xw.Error()
  208. }
  209. func (o *ClusterConfigMessage) DecodeXDR(r io.Reader) error {
  210. xr := xdr.NewReader(r)
  211. return o.decodeXDR(xr)
  212. }
  213. func (o *ClusterConfigMessage) UnmarshalXDR(bs []byte) error {
  214. var br = bytes.NewReader(bs)
  215. var xr = xdr.NewReader(br)
  216. return o.decodeXDR(xr)
  217. }
  218. func (o *ClusterConfigMessage) decodeXDR(xr *xdr.Reader) error {
  219. o.ClientName = xr.ReadStringMax(64)
  220. o.ClientVersion = xr.ReadStringMax(64)
  221. _RepositoriesSize := int(xr.ReadUint32())
  222. if _RepositoriesSize > 64 {
  223. return xdr.ErrElementSizeExceeded
  224. }
  225. o.Repositories = make([]Repository, _RepositoriesSize)
  226. for i := range o.Repositories {
  227. (&o.Repositories[i]).decodeXDR(xr)
  228. }
  229. _OptionsSize := int(xr.ReadUint32())
  230. if _OptionsSize > 64 {
  231. return xdr.ErrElementSizeExceeded
  232. }
  233. o.Options = make([]Option, _OptionsSize)
  234. for i := range o.Options {
  235. (&o.Options[i]).decodeXDR(xr)
  236. }
  237. return xr.Error()
  238. }
  239. func (o Repository) EncodeXDR(w io.Writer) (int, error) {
  240. var xw = xdr.NewWriter(w)
  241. return o.encodeXDR(xw)
  242. }
  243. func (o Repository) MarshalXDR() []byte {
  244. var aw = make(xdr.AppendWriter, 0, 128)
  245. var xw = xdr.NewWriter(&aw)
  246. o.encodeXDR(xw)
  247. return []byte(aw)
  248. }
  249. func (o Repository) encodeXDR(xw *xdr.Writer) (int, error) {
  250. if len(o.ID) > 64 {
  251. return xw.Tot(), xdr.ErrElementSizeExceeded
  252. }
  253. xw.WriteString(o.ID)
  254. if len(o.Nodes) > 64 {
  255. return xw.Tot(), xdr.ErrElementSizeExceeded
  256. }
  257. xw.WriteUint32(uint32(len(o.Nodes)))
  258. for i := range o.Nodes {
  259. o.Nodes[i].encodeXDR(xw)
  260. }
  261. return xw.Tot(), xw.Error()
  262. }
  263. func (o *Repository) DecodeXDR(r io.Reader) error {
  264. xr := xdr.NewReader(r)
  265. return o.decodeXDR(xr)
  266. }
  267. func (o *Repository) UnmarshalXDR(bs []byte) error {
  268. var br = bytes.NewReader(bs)
  269. var xr = xdr.NewReader(br)
  270. return o.decodeXDR(xr)
  271. }
  272. func (o *Repository) decodeXDR(xr *xdr.Reader) error {
  273. o.ID = xr.ReadStringMax(64)
  274. _NodesSize := int(xr.ReadUint32())
  275. if _NodesSize > 64 {
  276. return xdr.ErrElementSizeExceeded
  277. }
  278. o.Nodes = make([]Node, _NodesSize)
  279. for i := range o.Nodes {
  280. (&o.Nodes[i]).decodeXDR(xr)
  281. }
  282. return xr.Error()
  283. }
  284. func (o Node) EncodeXDR(w io.Writer) (int, error) {
  285. var xw = xdr.NewWriter(w)
  286. return o.encodeXDR(xw)
  287. }
  288. func (o Node) MarshalXDR() []byte {
  289. var aw = make(xdr.AppendWriter, 0, 128)
  290. var xw = xdr.NewWriter(&aw)
  291. o.encodeXDR(xw)
  292. return []byte(aw)
  293. }
  294. func (o Node) encodeXDR(xw *xdr.Writer) (int, error) {
  295. if len(o.ID) > 32 {
  296. return xw.Tot(), xdr.ErrElementSizeExceeded
  297. }
  298. xw.WriteBytes(o.ID)
  299. xw.WriteUint32(o.Flags)
  300. xw.WriteUint64(o.MaxVersion)
  301. return xw.Tot(), xw.Error()
  302. }
  303. func (o *Node) DecodeXDR(r io.Reader) error {
  304. xr := xdr.NewReader(r)
  305. return o.decodeXDR(xr)
  306. }
  307. func (o *Node) UnmarshalXDR(bs []byte) error {
  308. var br = bytes.NewReader(bs)
  309. var xr = xdr.NewReader(br)
  310. return o.decodeXDR(xr)
  311. }
  312. func (o *Node) decodeXDR(xr *xdr.Reader) error {
  313. o.ID = xr.ReadBytesMax(32)
  314. o.Flags = xr.ReadUint32()
  315. o.MaxVersion = xr.ReadUint64()
  316. return xr.Error()
  317. }
  318. func (o Option) EncodeXDR(w io.Writer) (int, error) {
  319. var xw = xdr.NewWriter(w)
  320. return o.encodeXDR(xw)
  321. }
  322. func (o Option) MarshalXDR() []byte {
  323. var aw = make(xdr.AppendWriter, 0, 128)
  324. var xw = xdr.NewWriter(&aw)
  325. o.encodeXDR(xw)
  326. return []byte(aw)
  327. }
  328. func (o Option) encodeXDR(xw *xdr.Writer) (int, error) {
  329. if len(o.Key) > 64 {
  330. return xw.Tot(), xdr.ErrElementSizeExceeded
  331. }
  332. xw.WriteString(o.Key)
  333. if len(o.Value) > 1024 {
  334. return xw.Tot(), xdr.ErrElementSizeExceeded
  335. }
  336. xw.WriteString(o.Value)
  337. return xw.Tot(), xw.Error()
  338. }
  339. func (o *Option) DecodeXDR(r io.Reader) error {
  340. xr := xdr.NewReader(r)
  341. return o.decodeXDR(xr)
  342. }
  343. func (o *Option) UnmarshalXDR(bs []byte) error {
  344. var br = bytes.NewReader(bs)
  345. var xr = xdr.NewReader(br)
  346. return o.decodeXDR(xr)
  347. }
  348. func (o *Option) decodeXDR(xr *xdr.Reader) error {
  349. o.Key = xr.ReadStringMax(64)
  350. o.Value = xr.ReadStringMax(1024)
  351. return xr.Error()
  352. }