ReadString reads a utf-8 string from the reader
()
| 1248 | |
| 1249 | // ReadString reads a utf-8 string from the reader |
| 1250 | func (m *Reader) ReadString() (s string, err error) { |
| 1251 | var read int64 |
| 1252 | lead, err := m.R.PeekByte() |
| 1253 | if err != nil { |
| 1254 | return |
| 1255 | } |
| 1256 | |
| 1257 | var p []byte |
| 1258 | if isfixstr(lead) { |
| 1259 | read = int64(rfixstr(lead)) |
| 1260 | m.R.Skip(1) |
| 1261 | goto fill |
| 1262 | } |
| 1263 | |
| 1264 | switch lead { |
| 1265 | case mstr8: |
| 1266 | p, err = m.R.Next(2) |
| 1267 | if err != nil { |
| 1268 | return |
| 1269 | } |
| 1270 | read = int64(p[1]) |
| 1271 | case mstr16: |
| 1272 | p, err = m.R.Next(3) |
| 1273 | if err != nil { |
| 1274 | return |
| 1275 | } |
| 1276 | read = int64(big.Uint16(p[1:])) |
| 1277 | case mstr32: |
| 1278 | p, err = m.R.Next(5) |
| 1279 | if err != nil { |
| 1280 | return |
| 1281 | } |
| 1282 | read = int64(big.Uint32(p[1:])) |
| 1283 | default: |
| 1284 | err = badPrefix(StrType, lead) |
| 1285 | return |
| 1286 | } |
| 1287 | fill: |
| 1288 | if read == 0 { |
| 1289 | s, err = "", nil |
| 1290 | return |
| 1291 | } |
| 1292 | if uint64(read) > m.GetMaxStringLength() { |
| 1293 | err = ErrLimitExceeded |
| 1294 | return |
| 1295 | } |
| 1296 | |
| 1297 | // reading into the memory |
| 1298 | // that will become the string |
| 1299 | // itself has vastly superior |
| 1300 | // worst-case performance, because |
| 1301 | // the reader buffer doesn't have |
| 1302 | // to be large enough to hold the string. |
| 1303 | // the idea here is to make it more |
| 1304 | // difficult for someone malicious |
| 1305 | // to cause the system to run out of |
| 1306 | // memory by sending very large strings. |
| 1307 | // |