<commit_before>// Copyright 2015 flannel authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package ip

import (
	"bytes"
	"errors"
	"fmt"
	"net"
)

type IP4 uint32

func FromBytes(ip []byte) IP4 {
	if NativelyLittle() {
		return IP4(uint32(ip[3]) |
			(uint32(ip[2]) << 8) |
			(uint32(ip[1]) << 16) |
			(uint32(ip[0]) << 24))
	} else {
		return IP4(uint32(ip[0]) |
			(uint32(ip[1]) << 8) |
			(uint32(ip[2]) << 16) |
			(uint32(ip[3]) << 24))
	}
}

func FromIP(ip net.IP) IP4 {
	return FromBytes(ip.To4())
}

func ParseIP4(s string) (IP4, error) {
	ip := net.ParseIP(s)
	if ip == nil {
		return IP4(0), errors.New("Invalid IP address format")
	}
	return FromIP(ip), nil
}

func MustParseIP4(s string) IP4 {
	ip, err := ParseIP4(s)
	if err != nil {
		panic(err)
	}
	return ip
}

func (ip IP4) Octets() (a, b, c, d byte) {
	if NativelyLittle() {
		a, b, c, d = byte(ip>>24), byte(ip>>16), byte(ip>>8), byte(ip)
	} else {
		a, b, c, d = byte(ip), byte(ip>>8), byte(ip>>16), byte(ip>>24)
	}
	return
}

func (ip IP4) ToIP() net.IP {
	return net.IPv4(ip.Octets())
}

func (ip IP4) NetworkOrder() uint32 {
	if NativelyLittle() {
		a, b, c, d := byte(ip>>24), byte(ip>>16), byte(ip>>8), byte(ip)
		return uint32(a) | (uint32(b) << 8) | (uint32(c) << 16) | (uint32(d) << 24)
	} else {
		return uint32(ip)
	}
}

func (ip IP4) String() string {
	return ip.ToIP().String()
}

func (ip IP4) StringSep(sep string) string {
	a, b, c, d := ip.Octets()
	return fmt.Sprintf("%d%s%d%s%d%s%d", a, sep, b, sep, c, sep, d)
}

// json.Marshaler impl
func (ip IP4) MarshalJSON() ([]byte, error) {
	return []byte(fmt.Sprintf(`"%s"`, ip)), nil
}

// json.Unmarshaler impl<commit_msg>      During test case execution, we came across all test case failures due to invalid IP.s generation for s390x system. On checking git issues , we came across the link  https://github.com/coreos/flannel/issues/526, which logs similar issue for bigEndiane during IP.s generation.       On analysis we came across littleEndian check, where code is manipulating IP.s for bigEndianess, which is unnecessary because changes are required only at bit-level not for IP. We are removing the littleEndian check where the IP address are involved.       For example subnetSize := ip.IP4(1 << (32 - cfg.SubnetLen)), assume cfg.SubnetLen=24 (by default) then, value of subnetSize should be same regardless of endianess, which is achieved after endiness check is removed.<commit_after>// Copyright 2015 flannel authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package ip

import (
	"bytes"
	"errors"
	"fmt"
	"net"
)

type IP4 uint32

func FromBytes(ip []byte) IP4 {
	return IP4(uint32(ip[3]) |
		(uint32(ip[2]) << 8) |
		(uint32(ip[1]) << 16) |
		(uint32(ip[0]) << 24))
}

func FromIP(ip net.IP) IP4 {
	return FromBytes(ip.To4())
}

func ParseIP4(s string) (IP4, error) {
	ip := net.ParseIP(s)
	if ip == nil {
		return IP4(0), errors.New("Invalid IP address format")
	}
	return FromIP(ip), nil
}

func MustParseIP4(s string) IP4 {
	ip, err := ParseIP4(s)
	if err != nil {
		panic(err)
	}
	return ip
}

func (ip IP4) Octets() (a, b, c, d byte) {
		a, b, c, d = byte(ip>>24), byte(ip>>16), byte(ip>>8), byte(ip)
	return
}

func (ip IP4) ToIP() net.IP {
	return net.IPv4(ip.Octets())
}

func (ip IP4) NetworkOrder() uint32 {
	a, b, c, d := byte(ip>>24), byte(ip>>16), byte(ip>>8), byte(ip)
	return uint32(a) | (uint32(b) << 8) | (uint32(c) << 16) | (uint32(d) << 24)
}

func (ip IP4) String() string {
	return ip.ToIP().String()
}

func (ip IP4) StringSep(sep string) string {
	a, b, c, d := ip.Octets()
	return fmt.Sprintf("%d%s%d%s%d%s%d", a, sep, b, sep, c, sep, d)
}

// json.Marshaler impl
func (ip IP4) MarshalJSON() ([]byte, error) {
	return []byte(fmt.Sprintf(`"%s"`, ip)), nil
}

// json.Unmarshaler impl
{'fraction_non_alphanumeric': 0.15984974958263773, 'fraction_numerical': 0.03735392320534224, 'mean_word_length': 3.71751968503937, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 33, 'https://': 1, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2173005', 'n_tokens_mistral': 1860, 'n_tokens_neox': 1752, 'n_words': 591}