diff options
| author | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2023-09-15 23:00:48 +0300 |
|---|---|---|
| committer | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2023-09-15 23:00:48 +0300 |
| commit | 3e30e44284e310ada2e60a2f6397eddc953e8933 (patch) | |
| tree | b04a2cf43dd0d88f1fe88e7c6480b33c0a917d3a | |
| parent | remove debug text (diff) | |
| download | bitreader-3e30e44284e310ada2e60a2f6397eddc953e8933.tar.gz bitreader-3e30e44284e310ada2e60a2f6397eddc953e8933.tar.bz2 bitreader-3e30e44284e310ada2e60a2f6397eddc953e8933.zip | |
prevent negative bit parameters by using uint64v1.4.2
| -rw-r--r-- | bitreader.go | 42 |
1 files changed, 24 insertions, 18 deletions
diff --git a/bitreader.go b/bitreader.go index e7c29d1..e50f433 100644 --- a/bitreader.go +++ b/bitreader.go | |||
| @@ -197,7 +197,7 @@ func (reader *Reader) TryReadFloat64() float64 { | |||
| 197 | // TryReadBits is a wrapper function that returns the value of bits specified in the parameter. | 197 | // TryReadBits is a wrapper function that returns the value of bits specified in the parameter. |
| 198 | // | 198 | // |
| 199 | // Returns uint64. Panics on overflow. | 199 | // Returns uint64. Panics on overflow. |
| 200 | func (reader *Reader) TryReadBits(bits int) uint64 { | 200 | func (reader *Reader) TryReadBits(bits uint64) uint64 { |
| 201 | value, err := reader.ReadBits(bits) | 201 | value, err := reader.ReadBits(bits) |
| 202 | if err != nil { | 202 | if err != nil { |
| 203 | panic(err) | 203 | panic(err) |
| @@ -208,7 +208,7 @@ func (reader *Reader) TryReadBits(bits int) uint64 { | |||
| 208 | // TryReadBytes is a wrapper function that returns the value of bits specified in the parameter. | 208 | // TryReadBytes is a wrapper function that returns the value of bits specified in the parameter. |
| 209 | // | 209 | // |
| 210 | // Returns uint64. Panics on overflow. | 210 | // Returns uint64. Panics on overflow. |
| 211 | func (reader *Reader) TryReadBytes(bytes int) uint64 { | 211 | func (reader *Reader) TryReadBytes(bytes uint64) uint64 { |
| 212 | value, err := reader.ReadBytes(bytes) | 212 | value, err := reader.ReadBytes(bytes) |
| 213 | if err != nil { | 213 | if err != nil { |
| 214 | panic(err) | 214 | panic(err) |
| @@ -232,7 +232,7 @@ func (reader *Reader) TryReadString() string { | |||
| 232 | // that is read until the given length is reached or it is null-terminated. | 232 | // that is read until the given length is reached or it is null-terminated. |
| 233 | // | 233 | // |
| 234 | // Returns string. Panics on overflow. | 234 | // Returns string. Panics on overflow. |
| 235 | func (reader *Reader) TryReadStringLength(length int) string { | 235 | func (reader *Reader) TryReadStringLength(length uint64) string { |
| 236 | text, err := reader.ReadStringLength(length) | 236 | text, err := reader.ReadStringLength(length) |
| 237 | if err != nil { | 237 | if err != nil { |
| 238 | panic(err) | 238 | panic(err) |
| @@ -244,13 +244,14 @@ func (reader *Reader) TryReadStringLength(length int) string { | |||
| 244 | // from the parameter and puts each bit into a slice and returns this slice. | 244 | // from the parameter and puts each bit into a slice and returns this slice. |
| 245 | // | 245 | // |
| 246 | // Returns []byte. Panics on overflow. | 246 | // Returns []byte. Panics on overflow. |
| 247 | func (reader *Reader) TryReadBitsToSlice(bits int) []byte { | 247 | func (reader *Reader) TryReadBitsToSlice(bits uint64) []byte { |
| 248 | bytes := (bits / 8) | 248 | bytes := (bits / 8) |
| 249 | if bits%8 != 0 { | 249 | if bits%8 != 0 { |
| 250 | bytes++ | 250 | bytes++ |
| 251 | } | 251 | } |
| 252 | out := make([]byte, bytes) | 252 | out := make([]byte, bytes) |
| 253 | for i := 0; i < bytes; i++ { | 253 | var i uint64 |
| 254 | for i = 0; i < bytes; i++ { | ||
| 254 | if i == bytes-1 { // Not enough to fill a whole byte | 255 | if i == bytes-1 { // Not enough to fill a whole byte |
| 255 | if bits%8 != 0 { | 256 | if bits%8 != 0 { |
| 256 | val, err := reader.ReadBits(bits % 8) | 257 | val, err := reader.ReadBits(bits % 8) |
| @@ -281,9 +282,10 @@ func (reader *Reader) TryReadBitsToSlice(bits int) []byte { | |||
| 281 | // from the parameter and puts each byte into a slice and returns this slice. | 282 | // from the parameter and puts each byte into a slice and returns this slice. |
| 282 | // | 283 | // |
| 283 | // Returns []byte. Panics on overflow. | 284 | // Returns []byte. Panics on overflow. |
| 284 | func (reader *Reader) TryReadBytesToSlice(bytes int) []byte { | 285 | func (reader *Reader) TryReadBytesToSlice(bytes uint64) []byte { |
| 285 | var out []byte | 286 | var out []byte |
| 286 | for i := 0; i < bytes; i++ { | 287 | var i uint64 |
| 288 | for i = 0; i < bytes; i++ { | ||
| 287 | val, err := reader.ReadBytes(1) | 289 | val, err := reader.ReadBytes(1) |
| 288 | if err != nil { | 290 | if err != nil { |
| 289 | panic(err) | 291 | panic(err) |
| @@ -323,12 +325,13 @@ func (reader *Reader) ReadBool() (bool, error) { | |||
| 323 | // value in type uint64. | 325 | // value in type uint64. |
| 324 | // | 326 | // |
| 325 | // Returns an error if there are no remaining bits. | 327 | // Returns an error if there are no remaining bits. |
| 326 | func (reader *Reader) ReadBits(bits int) (uint64, error) { | 328 | func (reader *Reader) ReadBits(bits uint64) (uint64, error) { |
| 327 | if bits < 1 || bits > 64 { | 329 | if bits < 1 || bits > 64 { |
| 328 | return 0, errors.New("ReadBits(bits) ERROR: Bits number should be between 1 and 64") | 330 | return 0, errors.New("ReadBits(bits) ERROR: Bits number should be between 1 and 64") |
| 329 | } | 331 | } |
| 330 | var val uint64 | 332 | var val uint64 |
| 331 | for i := 0; i < bits; i++ { | 333 | var i uint64 |
| 334 | for i = 0; i < bits; i++ { | ||
| 332 | bit, err := reader.readBit() | 335 | bit, err := reader.readBit() |
| 333 | if err != nil { | 336 | if err != nil { |
| 334 | return 0, err | 337 | return 0, err |
| @@ -348,7 +351,7 @@ func (reader *Reader) ReadBits(bits int) (uint64, error) { | |||
| 348 | // value in type uint64. | 351 | // value in type uint64. |
| 349 | // | 352 | // |
| 350 | // Returns an error if there are no remaining bits. | 353 | // Returns an error if there are no remaining bits. |
| 351 | func (reader *Reader) ReadBytes(bytes int) (uint64, error) { | 354 | func (reader *Reader) ReadBytes(bytes uint64) (uint64, error) { |
| 352 | if bytes < 1 || bytes > 8 { | 355 | if bytes < 1 || bytes > 8 { |
| 353 | return 0, errors.New("ReadBytes(bytes) ERROR: Bytes number should be between 1 and 8") | 356 | return 0, errors.New("ReadBytes(bytes) ERROR: Bytes number should be between 1 and 8") |
| 354 | } | 357 | } |
| @@ -385,9 +388,10 @@ func (reader *Reader) ReadString() (string, error) { | |||
| 385 | // It will skip the remaining bytes if it is null-terminated. | 388 | // It will skip the remaining bytes if it is null-terminated. |
| 386 | // | 389 | // |
| 387 | // Returns an error if there are no remaining bits. | 390 | // Returns an error if there are no remaining bits. |
| 388 | func (reader *Reader) ReadStringLength(length int) (string, error) { | 391 | func (reader *Reader) ReadStringLength(length uint64) (string, error) { |
| 389 | var out string | 392 | var out string |
| 390 | for i := 0; i < length; i++ { | 393 | var i uint64 |
| 394 | for i = 0; i < length; i++ { | ||
| 391 | value, err := reader.ReadBytes(1) | 395 | value, err := reader.ReadBytes(1) |
| 392 | if err != nil { | 396 | if err != nil { |
| 393 | return out, err | 397 | return out, err |
| @@ -405,13 +409,14 @@ func (reader *Reader) ReadStringLength(length int) (string, error) { | |||
| 405 | // from the parameter and puts each bit into a slice and returns this slice. | 409 | // from the parameter and puts each bit into a slice and returns this slice. |
| 406 | // | 410 | // |
| 407 | // Returns an error if there are no remaining bits. | 411 | // Returns an error if there are no remaining bits. |
| 408 | func (reader *Reader) ReadBitsToSlice(bits int) ([]byte, error) { | 412 | func (reader *Reader) ReadBitsToSlice(bits uint64) ([]byte, error) { |
| 409 | bytes := (bits / 8) | 413 | bytes := (bits / 8) |
| 410 | if bits%8 != 0 { | 414 | if bits%8 != 0 { |
| 411 | bytes++ | 415 | bytes++ |
| 412 | } | 416 | } |
| 413 | out := make([]byte, bytes) | 417 | out := make([]byte, bytes) |
| 414 | for i := 0; i < bytes; i++ { | 418 | var i uint64 |
| 419 | for i = 0; i < bytes; i++ { | ||
| 415 | if i == bytes-1 { // Not enough to fill a whole byte | 420 | if i == bytes-1 { // Not enough to fill a whole byte |
| 416 | if bits%8 != 0 { | 421 | if bits%8 != 0 { |
| 417 | val, err := reader.ReadBits(bits % 8) | 422 | val, err := reader.ReadBits(bits % 8) |
| @@ -442,9 +447,10 @@ func (reader *Reader) ReadBitsToSlice(bits int) ([]byte, error) { | |||
| 442 | // from the parameter and puts each byte into a slice and returns this slice. | 447 | // from the parameter and puts each byte into a slice and returns this slice. |
| 443 | // | 448 | // |
| 444 | // Returns an error if there are no remaining bytes. | 449 | // Returns an error if there are no remaining bytes. |
| 445 | func (reader *Reader) ReadBytesToSlice(bytes int) ([]byte, error) { | 450 | func (reader *Reader) ReadBytesToSlice(bytes uint64) ([]byte, error) { |
| 446 | var out []byte | 451 | var out []byte |
| 447 | for i := 0; i < bytes; i++ { | 452 | var i uint64 |
| 453 | for i = 0; i < bytes; i++ { | ||
| 448 | val, err := reader.ReadBytes(1) | 454 | val, err := reader.ReadBytes(1) |
| 449 | if err != nil { | 455 | if err != nil { |
| 450 | return out, err | 456 | return out, err |
| @@ -458,7 +464,7 @@ func (reader *Reader) ReadBytesToSlice(bytes int) ([]byte, error) { | |||
| 458 | // based on given input bits number. | 464 | // based on given input bits number. |
| 459 | // | 465 | // |
| 460 | // Returns an error if there are no remaining bits. | 466 | // Returns an error if there are no remaining bits. |
| 461 | func (reader *Reader) SkipBits(bits int) error { | 467 | func (reader *Reader) SkipBits(bits uint64) error { |
| 462 | // Read as many raw bytes as we can | 468 | // Read as many raw bytes as we can |
| 463 | bytes := bits / 8 | 469 | bytes := bits / 8 |
| 464 | if bytes > 0 { | 470 | if bytes > 0 { |
| @@ -484,7 +490,7 @@ func (reader *Reader) SkipBits(bits int) error { | |||
| 484 | // based on given input bytes number. | 490 | // based on given input bytes number. |
| 485 | // | 491 | // |
| 486 | // Returns an error if there are no remaining bits. | 492 | // Returns an error if there are no remaining bits. |
| 487 | func (reader *Reader) SkipBytes(bytes int) error { | 493 | func (reader *Reader) SkipBytes(bytes uint64) error { |
| 488 | err := reader.SkipBits(bytes * 8) | 494 | err := reader.SkipBits(bytes * 8) |
| 489 | if err != nil { | 495 | if err != nil { |
| 490 | return err | 496 | return err |