github.com/danil/iso8583@v0.21.0/scan8583/scan8583.go (about)

     1  package scan8583
     2  
     3  import (
     4  	"encoding/binary"
     5  	"errors"
     6  )
     7  
     8  // iso8583Size is the number of bytes in which are encoded in Big-endian,
     9  // unsigned number that determines the size of the ISO 8583 message.
    10  const iso8583Size = 2
    11  
    12  // ScanISO8583Indiscriminately is a split function for the ISO 8583 message format.
    13  // Split function returns hint is a number of bytes hinted to read and
    14  // returns advance is a needed number of bytes by which the carriage is to shift
    15  // and returns a token and an error if occurs.
    16  //
    17  // Each token is an ISO 8583 message size plus message itself.
    18  // The returned token may holds invalid or inconsistent
    19  // or incomplete ISO 8583 message or holds not ISO 8583 at all
    20  // because this split function do not performs any message validation.
    21  func ScanISO8583Indiscriminately(data []byte, _ bool) (int, int, []byte, error) {
    22  	if len(data) == 0 {
    23  		return iso8583Size, 0, nil, nil
    24  	}
    25  
    26  	size := int(binary.BigEndian.Uint16(data[:iso8583Size]))
    27  	if len(data) == iso8583Size {
    28  		return size, 0, nil, nil
    29  	}
    30  
    31  	if len(data) > size+iso8583Size {
    32  		return 0, 0, nil, errors.New("buffer exceeds hinted size of the ISO 8583 token")
    33  	}
    34  
    35  	if len(data) < size+iso8583Size {
    36  		return size + iso8583Size - len(data), 0, nil, nil
    37  	}
    38  
    39  	return 0, len(data), data, nil
    40  }
    41  
    42  // ScanISO8583 is a split function for a Protoscan that returns each
    43  // ISO 8583 message size plus message itself as a token.
    44  // The returned token intends to be valid and consistent
    45  // and complete ISO 8583 message
    46  // but unfortunately this split function is not implemented yet)
    47  func ScanISO8583(data []byte, atEOF bool) (int, int, []byte, error) {
    48  	return 0, 0, nil, errors.New("not implemented")
    49  }