writeOptions writes the given options to the file. prepareOptions must be called beforehand.
(options []ngOption)
| 116 | |
| 117 | // writeOptions writes the given options to the file. prepareOptions must be called beforehand. |
| 118 | func (w *NgWriter) writeOptions(options []ngOption) error { |
| 119 | if len(options) == 0 { |
| 120 | return nil |
| 121 | } |
| 122 | |
| 123 | var zero [4]byte |
| 124 | for _, option := range options { |
| 125 | binary.LittleEndian.PutUint16(w.buf[0:2], uint16(option.code)) |
| 126 | binary.LittleEndian.PutUint16(w.buf[2:4], option.length) |
| 127 | if _, err := w.w.Write(w.buf[:4]); err != nil { |
| 128 | return err |
| 129 | } |
| 130 | switch val := option.raw.(type) { |
| 131 | case []byte: |
| 132 | if _, err := w.w.Write(val); err != nil { |
| 133 | return err |
| 134 | } |
| 135 | padding := uint8((4 - option.length&3) & 3) |
| 136 | if padding < 4 { |
| 137 | if _, err := w.w.Write(zero[:padding]); err != nil { |
| 138 | return err |
| 139 | } |
| 140 | } |
| 141 | case string: |
| 142 | if _, err := w.w.Write([]byte(val)); err != nil { |
| 143 | return err |
| 144 | } |
| 145 | padding := uint8((4 - option.length&3) & 3) |
| 146 | if padding < 4 { |
| 147 | if _, err := w.w.Write(zero[:padding]); err != nil { |
| 148 | return err |
| 149 | } |
| 150 | } |
| 151 | case time.Time: |
| 152 | ts := val.UnixNano() |
| 153 | binary.LittleEndian.PutUint32(w.buf[:4], uint32(ts>>32)) |
| 154 | binary.LittleEndian.PutUint32(w.buf[4:8], uint32(ts)) |
| 155 | if _, err := w.w.Write(w.buf[:8]); err != nil { |
| 156 | return err |
| 157 | } |
| 158 | case uint64: |
| 159 | binary.LittleEndian.PutUint64(w.buf[:8], val) |
| 160 | if _, err := w.w.Write(w.buf[:8]); err != nil { |
| 161 | return err |
| 162 | } |
| 163 | case uint32: |
| 164 | binary.LittleEndian.PutUint32(w.buf[:4], val) |
| 165 | if _, err := w.w.Write(w.buf[:4]); err != nil { |
| 166 | return err |
| 167 | } |
| 168 | case uint8: |
| 169 | binary.LittleEndian.PutUint32(w.buf[:4], 0) // padding |
| 170 | w.buf[0] = val |
| 171 | if _, err := w.w.Write(w.buf[:4]); err != nil { |
| 172 | return err |
| 173 | } |
| 174 | default: |
| 175 | panic("This should never happen") |
no test coverage detected