github.com/vmware/govmomi@v0.43.0/vim25/types/byte_slice.go (about)

     1  /*
     2  Copyright (c) 2024-2024 VMware, Inc. All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package types
    18  
    19  import (
    20  	"fmt"
    21  	"io"
    22  	"math"
    23  	"strconv"
    24  
    25  	"github.com/vmware/govmomi/vim25/xml"
    26  )
    27  
    28  // ByteSlice implements vCenter compatibile xml encoding and decoding for a byte slice.
    29  // vCenter encodes each byte of the array in its own xml element, whereas
    30  // Go encodes the entire byte array in a single xml element.
    31  type ByteSlice []byte
    32  
    33  // MarshalXML implements xml.Marshaler
    34  func (b ByteSlice) MarshalXML(e *xml.Encoder, field xml.StartElement) error {
    35  	start := xml.StartElement{
    36  		Name: field.Name,
    37  	}
    38  	for i := range b {
    39  		if err := e.EncodeElement(b[i], start); err != nil {
    40  			return err
    41  		}
    42  	}
    43  	return nil
    44  }
    45  
    46  // UnmarshalXML implements xml.Unmarshaler
    47  func (b *ByteSlice) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
    48  	for {
    49  		t, err := d.Token()
    50  		if err == io.EOF {
    51  			break
    52  		}
    53  
    54  		if c, ok := t.(xml.CharData); ok {
    55  			n, err := strconv.ParseInt(string(c), 10, 16)
    56  			if err != nil {
    57  				return err
    58  			}
    59  			if n > math.MaxUint8 {
    60  				return fmt.Errorf("parsing %q: uint8 overflow", start.Name.Local)
    61  			}
    62  			*b = append(*b, byte(n))
    63  		}
    64  	}
    65  
    66  	return nil
    67  }