github.com/trim21/go-phpserialize@v0.0.22-0.20240301204449-2fca0319b3f0/internal/decoder/bool.go (about)

     1  package decoder
     2  
     3  import (
     4  	"unsafe"
     5  
     6  	"github.com/trim21/go-phpserialize/internal/errors"
     7  )
     8  
     9  type boolDecoder struct {
    10  	structName string
    11  	fieldName  string
    12  }
    13  
    14  func newBoolDecoder(structName, fieldName string) *boolDecoder {
    15  	return &boolDecoder{structName: structName, fieldName: fieldName}
    16  }
    17  
    18  func (d *boolDecoder) Decode(ctx *RuntimeContext, cursor, depth int64, p unsafe.Pointer) (int64, error) {
    19  	buf := ctx.Buf
    20  	switch buf[cursor] {
    21  	case 'b':
    22  		// b:0;
    23  		// b:1;
    24  
    25  		cursor++
    26  		if buf[cursor] != ':' {
    27  			return 0, errors.ErrUnexpectedEnd("':' before bool value", cursor)
    28  		}
    29  		cursor++
    30  		switch buf[cursor] {
    31  		case '0':
    32  			**(**bool)(unsafe.Pointer(&p)) = false
    33  		case '1':
    34  			**(**bool)(unsafe.Pointer(&p)) = true
    35  		default:
    36  			return 0, errors.ErrInvalidCharacter(buf[cursor], "bool value", cursor)
    37  		}
    38  		cursor++
    39  		if buf[cursor] != ';' {
    40  			return 0, errors.ErrUnexpectedEnd("';' end bool value", cursor)
    41  		}
    42  		cursor++
    43  		return cursor, nil
    44  
    45  	case 'N':
    46  		if err := validateNull(buf, cursor); err != nil {
    47  			return 0, err
    48  		}
    49  		cursor += 2
    50  		return cursor, nil
    51  	}
    52  	return 0, errors.ErrUnexpectedEnd("bool", cursor)
    53  }