Pull request: all: add native endianness, imp Makefile
Merge in DNS/adguard-home from fix-some to master Squashed commit of the following: commit 190e9a88d9c0f2bfc597aa61b41dae8b8686158e Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Tue Mar 16 20:50:02 2021 +0300 all: add native endianness, imp Makefile
This commit is contained in:
10
internal/aghos/endian_big.go
Normal file
10
internal/aghos/endian_big.go
Normal file
@@ -0,0 +1,10 @@
|
||||
// +build mips mips64
|
||||
|
||||
// This file is an adapted version of github.com/josharian/native.
|
||||
|
||||
package aghos
|
||||
|
||||
import "encoding/binary"
|
||||
|
||||
// NativeEndian is the native endianness of this system.
|
||||
var NativeEndian = binary.BigEndian
|
||||
10
internal/aghos/endian_little.go
Normal file
10
internal/aghos/endian_little.go
Normal file
@@ -0,0 +1,10 @@
|
||||
// +build amd64 386 arm arm64 mipsle mips64le ppc64le
|
||||
|
||||
// This file is an adapted version of github.com/josharian/native.
|
||||
|
||||
package aghos
|
||||
|
||||
import "encoding/binary"
|
||||
|
||||
// NativeEndian is the native endianness of this system.
|
||||
var NativeEndian = binary.LittleEndian
|
||||
@@ -8,9 +8,9 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
"unsafe"
|
||||
|
||||
"github.com/AdguardTeam/AdGuardHome/internal/aghio"
|
||||
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
|
||||
"github.com/AdguardTeam/golibs/log"
|
||||
)
|
||||
|
||||
@@ -66,16 +66,6 @@ func glCheckToken(sess string) bool {
|
||||
return now <= (tokenDate + glTokenTimeoutSeconds)
|
||||
}
|
||||
|
||||
// TODO(a.garipov): Replace with a smaller version of
|
||||
// https://github.com/josharian/native.
|
||||
func archIsLittleEndian() bool {
|
||||
var i int32 = 0x01020304
|
||||
u := unsafe.Pointer(&i)
|
||||
pb := (*byte)(u)
|
||||
b := *pb
|
||||
return (b == 0x04)
|
||||
}
|
||||
|
||||
// MaxFileSize is a maximum file length in bytes.
|
||||
const MaxFileSize = 1024 * 1024
|
||||
|
||||
@@ -104,12 +94,7 @@ func glGetTokenDate(file string) uint32 {
|
||||
}
|
||||
buf := bytes.NewBuffer(bs)
|
||||
|
||||
var order binary.ByteOrder = binary.BigEndian
|
||||
if archIsLittleEndian() {
|
||||
order = binary.LittleEndian
|
||||
}
|
||||
|
||||
err = binary.Read(buf, order, &dateToken)
|
||||
err = binary.Read(buf, aghos.NativeEndian, &dateToken)
|
||||
if err != nil {
|
||||
log.Error("binary.Read: %s", err)
|
||||
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
package home
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
@@ -20,19 +20,14 @@ func TestAuthGL(t *testing.T) {
|
||||
})
|
||||
glFilePrefix = dir + "/gl_token_"
|
||||
|
||||
putFunc := binary.BigEndian.PutUint32
|
||||
if archIsLittleEndian() {
|
||||
putFunc = binary.LittleEndian.PutUint32
|
||||
}
|
||||
|
||||
data := make([]byte, 4)
|
||||
putFunc(data, 1)
|
||||
aghos.NativeEndian.PutUint32(data, 1)
|
||||
|
||||
require.Nil(t, ioutil.WriteFile(glFilePrefix+"test", data, 0o644))
|
||||
assert.False(t, glCheckToken("test"))
|
||||
|
||||
data = make([]byte, 4)
|
||||
putFunc(data, uint32(time.Now().UTC().Unix()+60))
|
||||
aghos.NativeEndian.PutUint32(data, uint32(time.Now().UTC().Unix()+60))
|
||||
|
||||
require.Nil(t, ioutil.WriteFile(glFilePrefix+"test", data, 0o644))
|
||||
r, _ := http.NewRequest(http.MethodGet, "http://localhost/", nil)
|
||||
|
||||
Reference in New Issue
Block a user