first commit
This commit is contained in:
commit
5578091190
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
deldify*
|
86
main.go
Normal file
86
main.go
Normal file
@ -0,0 +1,86 @@
|
||||
// de-LDIF'y LDAP query result
|
||||
// join breaked lines, and decode base64
|
||||
//
|
||||
// Copyright (c) 2023 yo000 <johan@nosd.in>
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"fmt"
|
||||
"flag"
|
||||
"bufio"
|
||||
"regexp"
|
||||
"strings"
|
||||
"encoding/base64"
|
||||
)
|
||||
|
||||
var (
|
||||
gVersion = "0.0.1"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var input string
|
||||
|
||||
flag.Parse()
|
||||
if len(flag.Args()) < 1 {
|
||||
fmt.Printf("Usage: deldify filename\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
input = flag.Args()[0]
|
||||
|
||||
// open file
|
||||
f, err := os.Open(input)
|
||||
if err != nil {
|
||||
fmt.Errorf("%v\n", err)
|
||||
}
|
||||
// remember to close the file at the end of the program
|
||||
defer f.Close()
|
||||
|
||||
// read the file line by line using scanner
|
||||
scanner := bufio.NewScanner(f)
|
||||
|
||||
// Make 2 pass:
|
||||
// First concatenate space starting lines, put result into ram
|
||||
// Second to decode base64 lines
|
||||
// Then print result
|
||||
|
||||
// 1st pass
|
||||
var res1 []string
|
||||
var cl string
|
||||
for scanner.Scan() {
|
||||
cl = scanner.Text()
|
||||
if strings.HasPrefix(cl, " ") {
|
||||
res1[len(res1)-1] = fmt.Sprintf("%s%s", res1[len(res1)-1], cl[1:])
|
||||
} else {
|
||||
res1 = append(res1, fmt.Sprintf("%s", cl))
|
||||
}
|
||||
}
|
||||
|
||||
// 2nd pass
|
||||
var res2 []string
|
||||
re := regexp.MustCompile("(\\w+)::\\s(.*)")
|
||||
for _, l := range res1 {
|
||||
m := re.FindStringSubmatch(l)
|
||||
if len(m) > 0 {
|
||||
dec, err := base64.StdEncoding.DecodeString(m[2])
|
||||
if err != nil {
|
||||
fmt.Printf("Error decoding base64: %s: %v\n", m[2], err)
|
||||
continue
|
||||
}
|
||||
res2 = append(res2, fmt.Sprintf("%s: %s", m[1], dec))
|
||||
} else {
|
||||
res2 = append(res2, l)
|
||||
}
|
||||
}
|
||||
|
||||
for _, l := range res2 {
|
||||
fmt.Printf("%s\n", l)
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
fmt.Errorf("%v\n", err)
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user