-
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from wneessen/37-attachment-length
Fixes bug with attachment encoding
- Loading branch information
Showing
2 changed files
with
68 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// SPDX-FileCopyrightText: 2022 Winni Neessen <[email protected]> | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
package mail | ||
|
||
import "io" | ||
|
||
// Base64LineBreaker is a io.WriteCloser that writes Base64 encoded data streams | ||
// with line breaks at a given line length | ||
type Base64LineBreaker struct { | ||
line [MaxBodyLength]byte | ||
used int | ||
out io.Writer | ||
} | ||
|
||
var nl = []byte(SingleNewLine) | ||
|
||
// Write writes the data stream and inserts a SingleNewLine when the maximum | ||
// line length is reached | ||
func (l *Base64LineBreaker) Write(b []byte) (n int, err error) { | ||
if l.used+len(b) < MaxBodyLength { | ||
copy(l.line[l.used:], b) | ||
l.used += len(b) | ||
return len(b), nil | ||
} | ||
|
||
n, err = l.out.Write(l.line[0:l.used]) | ||
if err != nil { | ||
return | ||
} | ||
excess := MaxBodyLength - l.used | ||
l.used = 0 | ||
|
||
n, err = l.out.Write(b[0:excess]) | ||
if err != nil { | ||
return | ||
} | ||
|
||
n, err = l.out.Write(nl) | ||
if err != nil { | ||
return | ||
} | ||
|
||
return l.Write(b[excess:]) | ||
} | ||
|
||
// Close closes the Base64LineBreaker and writes any access data that is still | ||
// unwritten in memory | ||
func (l *Base64LineBreaker) Close() (err error) { | ||
if l.used > 0 { | ||
_, err = l.out.Write(l.line[0:l.used]) | ||
if err != nil { | ||
return | ||
} | ||
_, err = l.out.Write(nl) | ||
} | ||
|
||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters