message_xdr.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 buf bytes.Buffer
  13. var xw = xdr.NewWriter(&buf)
  14. o.encodeXDR(xw)
  15. return buf.Bytes()
  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) > 100000 {
  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 buf = bytes.NewBuffer(bs)
  37. var xr = xdr.NewReader(buf)
  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 > 100000 {
  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 buf bytes.Buffer
  58. var xw = xdr.NewWriter(&buf)
  59. o.encodeXDR(xw)
  60. return buf.Bytes()
  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) > 100000 {
  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 buf = bytes.NewBuffer(bs)
  85. var xr = xdr.NewReader(buf)
  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 > 100000 {
  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 buf bytes.Buffer
  109. var xw = xdr.NewWriter(&buf)
  110. o.encodeXDR(xw)
  111. return buf.Bytes()
  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 buf = bytes.NewBuffer(bs)
  127. var xr = xdr.NewReader(buf)
  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 buf bytes.Buffer
  141. var xw = xdr.NewWriter(&buf)
  142. o.encodeXDR(xw)
  143. return buf.Bytes()
  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 buf = bytes.NewBuffer(bs)
  164. var xr = xdr.NewReader(buf)
  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 OptionsMessage) EncodeXDR(w io.Writer) (int, error) {
  175. var xw = xdr.NewWriter(w)
  176. return o.encodeXDR(xw)
  177. }
  178. func (o OptionsMessage) MarshalXDR() []byte {
  179. var buf bytes.Buffer
  180. var xw = xdr.NewWriter(&buf)
  181. o.encodeXDR(xw)
  182. return buf.Bytes()
  183. }
  184. func (o OptionsMessage) encodeXDR(xw *xdr.Writer) (int, error) {
  185. if len(o.Options) > 64 {
  186. return xw.Tot(), xdr.ErrElementSizeExceeded
  187. }
  188. xw.WriteUint32(uint32(len(o.Options)))
  189. for i := range o.Options {
  190. o.Options[i].encodeXDR(xw)
  191. }
  192. return xw.Tot(), xw.Error()
  193. }
  194. func (o *OptionsMessage) DecodeXDR(r io.Reader) error {
  195. xr := xdr.NewReader(r)
  196. return o.decodeXDR(xr)
  197. }
  198. func (o *OptionsMessage) UnmarshalXDR(bs []byte) error {
  199. var buf = bytes.NewBuffer(bs)
  200. var xr = xdr.NewReader(buf)
  201. return o.decodeXDR(xr)
  202. }
  203. func (o *OptionsMessage) decodeXDR(xr *xdr.Reader) error {
  204. _OptionsSize := int(xr.ReadUint32())
  205. if _OptionsSize > 64 {
  206. return xdr.ErrElementSizeExceeded
  207. }
  208. o.Options = make([]Option, _OptionsSize)
  209. for i := range o.Options {
  210. (&o.Options[i]).decodeXDR(xr)
  211. }
  212. return xr.Error()
  213. }
  214. func (o Option) EncodeXDR(w io.Writer) (int, error) {
  215. var xw = xdr.NewWriter(w)
  216. return o.encodeXDR(xw)
  217. }
  218. func (o Option) MarshalXDR() []byte {
  219. var buf bytes.Buffer
  220. var xw = xdr.NewWriter(&buf)
  221. o.encodeXDR(xw)
  222. return buf.Bytes()
  223. }
  224. func (o Option) encodeXDR(xw *xdr.Writer) (int, error) {
  225. if len(o.Key) > 64 {
  226. return xw.Tot(), xdr.ErrElementSizeExceeded
  227. }
  228. xw.WriteString(o.Key)
  229. if len(o.Value) > 1024 {
  230. return xw.Tot(), xdr.ErrElementSizeExceeded
  231. }
  232. xw.WriteString(o.Value)
  233. return xw.Tot(), xw.Error()
  234. }
  235. func (o *Option) DecodeXDR(r io.Reader) error {
  236. xr := xdr.NewReader(r)
  237. return o.decodeXDR(xr)
  238. }
  239. func (o *Option) UnmarshalXDR(bs []byte) error {
  240. var buf = bytes.NewBuffer(bs)
  241. var xr = xdr.NewReader(buf)
  242. return o.decodeXDR(xr)
  243. }
  244. func (o *Option) decodeXDR(xr *xdr.Reader) error {
  245. o.Key = xr.ReadStringMax(64)
  246. o.Value = xr.ReadStringMax(1024)
  247. return xr.Error()
  248. }