message_xdr.go 9.1 KB

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