message_xdr.go 9.8 KB

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