Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring passwd. #14

Merged
merged 13 commits into from
Nov 13, 2020
32 changes: 16 additions & 16 deletions big5_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ func TestUtf8ToBig5(t *testing.T) {
input string
}
tests := []struct {
name string
args args
want []byte
name string
args args
expected []byte
}{
// TODO: Add test cases.
{
name: "test0",
args: args{input: "新的目錄"},
want: []byte{183, 115, 170, 186, 165, 216, 191, 253},
name: "test0",
args: args{input: "新的目錄"},
expected: []byte{0xb7, 0x73, 0xaa, 0xba, 0xa5, 0xd8, 0xbf, 0xfd},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Utf8ToBig5(tt.args.input); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Utf8ToBig5() = %v, want %v", got, tt.want)
if got := Utf8ToBig5(tt.args.input); !reflect.DeepEqual(got, tt.expected) {
t.Errorf("Utf8ToBig5() = %v, expected %v", got, tt.expected)
}
})
}
Expand All @@ -35,21 +35,21 @@ func TestBig5ToUtf8(t *testing.T) {
input []byte
}
tests := []struct {
name string
args args
want string
name string
args args
expected string
}{
// TODO: Add test cases.
{
name: "test0",
args: args{input: []byte{183, 115, 170, 186, 165, 216, 191, 253}},
want: "新的目錄",
name: "test0",
args: args{input: []byte{0xb7, 0x73, 0xaa, 0xba, 0xa5, 0xd8, 0xbf, 0xfd}},
expected: "新的目錄",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Big5ToUtf8(tt.args.input); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Big5ToUtf8() = %v, want %v", got, tt.want)
if got := Big5ToUtf8(tt.args.input); !reflect.DeepEqual(got, tt.expected) {
t.Errorf("Big5ToUtf8() = %v, expected %v", got, tt.expected)
}
})
}
Expand Down
45 changes: 45 additions & 0 deletions const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package bbs

const (
//////////
//pttstruch.h
//////////
PTT_IDLEN = 12 /* Length of bid/uid */
PTT_IPV4LEN = 15 /* a.b.c.d form */

PTT_PASS_INPUT_LEN = 8 /* Length of valid input password length.
For DES, set to 8. */
PTT_PASSLEN = 14 /* Length of encrypted passwd field */
PTT_REGLEN = 38 /* Length of registration data */

PTT_REALNAMESZ = 20 /* Size of real-name field */
PTT_NICKNAMESZ = 24 /* SIze of nick-name field */
PTT_EMAILSZ = 50 /* Size of email field */
PTT_ADDRESSSZ = 50 /* Size of address field */
PTT_CAREERSZ = 40 /* Size of career field */
PTT_PHONESZ = 20 /* Size of phone field */

PTT_PASSWD_VERSION = 4194 /* passwd version */

PTT_TTLEN = 64 /* Length of title */
PTT_FNLEN = 28 /* Length of filename */
)

const (
//////////
//pttstruch.h: 292
//////////
PTT_FILE_LOCAL = 0x01 /* local saved, non-mail */
PTT_FILE_READ = 0x01 /* already read, mail only */
PTT_FILE_MARKED = 0x02 /* non-mail + mail */
PTT_FILE_DIGEST = 0x04 /* digest, non-mail */
PTT_FILE_REPLIED = 0x04 /* replied, mail only */
PTT_FILE_BOTTOM = 0x08 /* push_bottom, non-mail */
PTT_FILE_MULTI = 0x08 /* multi send, mail only */
PTT_FILE_SOLVED = 0x10 /* problem solved, sysop/BM non-mail only */
PTT_FILE_HIDE = 0x20 /* hide, in announce */
PTT_FILE_BID = 0x20 /* bid, in non-announce */
PTT_FILE_BM = 0x40 /* BM only, in announce */
PTT_FILE_VOTE = 0x40 /* for vote, in non-announce */
PTT_FILE_ANONYMOUS = 0x80 /* anonymous file */
)
49 changes: 49 additions & 0 deletions cstr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package bbs

import "bytes"

//Cstr
//
//[]byte with C String property in that \0 is considered as the end of the bytes/string.
//It is used to convert from fixed-length bytes to string or []byte with no \0.
//
//Naming Cstr instead of CString is to avoid confusion with C.CString
//(C.CString is from string, and should be compatible with string, not with []byte)
//(We also have str(len/cpy/cmp) functions in C)
//
//See tests for more examples of how to use fixed-bytes with Cstr to get no-\0 string / []byte
type Cstr []byte

//CstrToString
//
//Only the bytes until \0 when converting to string.
//See tests for more examples.
//
//Params
// cstr
//
//Return
// string: string
func CstrToString(cstr Cstr) string {
theBytes := CstrToBytes(cstr)
return string(theBytes)
}

//CstrToBytes
//
//Only the bytes until \0.
//See tests for more examples.
//
//Params
// cstr
//
//Return
// []byte: bytes
func CstrToBytes(cstr Cstr) []byte {
len := bytes.IndexByte(cstr, 0x00)
if len == -1 {
return cstr
}

return cstr[:len]
}
107 changes: 107 additions & 0 deletions cstr_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package bbs

import (
"reflect"
"testing"
)

func TestCstrToBytes(t *testing.T) {
setupTest()
defer teardownTest()

str1 := [13]byte{}
str2 := [13]byte{}
copy(str2[:], []byte("123"))
str3 := [10]byte{}
copy(str3[:], []byte("0123456789"))
str4 := [10]byte{}
copy(str4[:], []byte("01234\x006789"))

type args struct {
cstr Cstr
}
tests := []struct {
name string
args args
expected []byte
}{
{
name: "init",
args: args{str1[:]},
expected: []byte{},
},
{
name: "with only 3 letters",
args: args{str2[:]},
expected: []byte("123"),
},
{
name: "with no 0",
args: args{str3[:]},
expected: []byte("0123456789"),
},
{
name: "cutoff at str4[5]",
args: args{str4[:]},
expected: []byte("01234"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := CstrToBytes(tt.args.cstr); !reflect.DeepEqual(got, tt.expected) {
t.Errorf("CstrToBytes() = %v, expected %v", got, tt.expected)
}
})
}
}

func TestCstrToString(t *testing.T) {
setupTest()
defer teardownTest()

str1 := [13]byte{}
str2 := [13]byte{}
copy(str2[:], []byte("123"))
str3 := [10]byte{}
copy(str3[:], []byte("0123456789"))
str4 := [10]byte{}
copy(str4[:], []byte("01234\x006789"))

type args struct {
cstr Cstr
}
tests := []struct {
name string
args args
expected string
}{
// TODO: Add test cases.
{
name: "init",
args: args{str1[:]},
expected: "",
},
{
name: "with only 3 letters",
args: args{str2[:]},
expected: "123",
},
{
name: "with no 0",
args: args{str3[:]},
expected: "0123456789",
},
{
name: "cutoff at str4[5]",
args: args{str4[:]},
expected: "01234",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := CstrToString(tt.args.cstr); got != tt.expected {
t.Errorf("CstrToString() = %v, expected %v", got, tt.expected)
}
})
}
}
22 changes: 0 additions & 22 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,6 @@ import (
"time"
)

const (
PTT_FILE_LOCAL = 0x01 /* local saved, non-mail */
PTT_FILE_READ = 0x01 /* already read, mail only */
PTT_FILE_MARKED = 0x02 /* non-mail + mail */
PTT_FILE_DIGEST = 0x04 /* digest, non-mail */
PTT_FILE_REPLIED = 0x04 /* replied, mail only */
PTT_FILE_BOTTOM = 0x08 /* push_bottom, non-mail */
PTT_FILE_MULTI = 0x08 /* multi send, mail only */
PTT_FILE_SOLVED = 0x10 /* problem solved, sysop/BM non-mail only */
PTT_FILE_HIDE = 0x20 /* hide, in announce */
PTT_FILE_BID = 0x20 /* bid, in non-announce */
PTT_FILE_BM = 0x40 /* BM only, in announce */
PTT_FILE_VOTE = 0x40 /* for vote, in non-announce */
PTT_FILE_ANONYMOUS = 0x80 /* anonymous file */

)
const (
PTT_IDLEN = 12
PTT_TTLEN = 64
PTT_FNLEN = 28
)

const (
PosOfPTTFilename = 0
PosOfPTTModified = PosOfPTTFilename + PTT_FNLEN
Expand Down
Loading