예제 #1
0
func toAscii85String(buf []byte) string {
	out := bytes.NewBuffer(nil)
	enc := ascii85.NewEncoder(out)
	enc.Write(buf)
	enc.Close()
	return out.String()
}
예제 #2
0
파일: add.go 프로젝트: kisom/hockeypuck
// NewUuid creates a new randomly generated, secure unique identifier.
func NewUuid() (string, error) {
	buf := bytes.NewBuffer([]byte{})
	enc := ascii85.NewEncoder(buf)
	n, err := io.CopyN(enc, rand.Reader, UUID_LEN)
	if err != nil {
		return "", err
	}
	if n < UUID_LEN {
		return "", fmt.Errorf("Failed to generate UUID")
	}
	return string(buf.Bytes()), nil
}
예제 #3
0
func main() {
	var buffer bytes.Buffer
	enc := ascii85.NewEncoder(io.MultiWriter(os.Stdout, &buffer))
	log.Println("encoding to stdout")
	_, err := enc.Write(data())
	enc.Close()
	if err != nil {
		log.Fatalf("failed encoding: %s", err)
	}
	println()
	dec := ascii85.NewDecoder(&buffer)
	log.Println("decoding to stdout")
	io.Copy(os.Stdout, dec)
}
예제 #4
0
파일: file.go 프로젝트: phoenixxie/resource
func readFile(filePath string) (string, error) {
	var buf []byte
	var err error
	var n int
	var writer *flate.Writer
	var encoder io.WriteCloser
	var file *os.File
	var data *bytes.Buffer = new(bytes.Buffer)

	if file, err = os.Open(filePath); err != nil {
		return "", err
	}
	defer file.Close()

	encoder = ascii85.NewEncoder(data)
	defer encoder.Close()

	writer, err = flate.NewWriter(encoder, flate.BestCompression)
	defer writer.Close()

	buf = make([]byte, 8192)
	err = nil
	for {
		if n, err = file.Read(buf); err != nil {
			if err == io.EOF {
				break
			} else {
				return "", err
			}
		}

		if n == 8192 {
			_, err = writer.Write(buf)
		} else {
			_, err = writer.Write(buf[:n])
		}
		if err != nil {
			return "", err
		}
	}
	if err = writer.Flush(); err != nil {
		return "", err
	}

	return strconv.Quote(string(data.Bytes())), nil
}
예제 #5
0
파일: main.go 프로젝트: tscholl2/prime
func main() {
	flag.CommandLine.Usage = func() {
		fmt.Println(`prime: generate a prime number and print to stdout
Example: 'prime -f 16 -b 128' prints: 83b19881300529d1fd4dac680415c60f
Example: 'prime -f 64 -b 256' prints: zUwiK96O4sy6pm3LtQM5YtRP9L4RGxsU/zCNliZZXn0=
Example: 'prime -f  0 -b 1024 > p.bytes' saves the raw bytes to the file 'p.bytes'
Options:`)
		flag.CommandLine.PrintDefaults()
	}
	var b, f int
	flag.IntVar(&b, "b", 128, "number of bits [supports: 2,...,128,...]")
	flag.IntVar(&f, "f", 10, "format of output [supports: 0,2-36,64,85]")
	flag.Parse()
	if b <= 1 {
		log.Fatalf("bits must be positive integer > 1, not %d", b)
	}
	p := prime.RandPrime(b)
	var s string
	switch {
	case f == 0:
		os.Stdout.Write(p.Bytes())
		return
	case 2 <= f && f <= 36:
		s = p.Text(f)
	case f == 64:
		s = string(base64.StdEncoding.EncodeToString(p.Bytes()))
	case f == 85:
		buf := bytes.NewBuffer(nil)
		enc := ascii85.NewEncoder(buf)
		enc.Write(p.Bytes())
		enc.Close()
		s = string(buf.Bytes())
	default:
		log.Fatal("unknown base")
	}
	fmt.Println(s)
}
예제 #6
0
파일: pdf.go 프로젝트: Davorak/imp
func (w *PDFWriter) WriteFontEmbedded(id int, f *otf.Font) {
	var (
		fontBase       = id
		fontDescedant  = w.NextID()
		fontDescriptor = w.NextID()
		fontStream     = w.NextID()
		fontUnicode    = w.NextID()
	)

	name := encodeName(f.PostscriptName)
	cff := f.CFF()

	// base font object
	w.WriteObjectf(fontBase, `<<
  /Type /Font
  /Subtype /Type0
  /BaseFont %s
  /Encoding /Identity-H
  /ToUnicode %d 0 R
  /DescendantFonts [%d 0 R]
>>`, name, fontUnicode, fontDescedant)

	// font descedant
	widths := make([]int, f.NumGlyphs())
	for i := 0; i < len(widths); i++ {
		widths[i] = f.Scale(f.HMetric(otf.Index(i)).Width, 1000)
	}
	fontType := 2
	if cff != nil {
		fontType = 0
	}
	w.WriteObjectf(fontDescedant, `<<
  /Type /Font
  /Subtype /CIDFontType%d
  /BaseFont %s
  /CIDSystemInfo
  <<
    /Registry (Adobe)
    /Ordering (Identity)
    /Supplement 0
  >>
  /DW %d
  /W [0 %v]
  /FontDescriptor %d 0 R
>>`, fontType, name, widths[0], widths, fontDescriptor)

	// font descriptor
	fontFile := 2
	if cff != nil {
		fontFile = 3
	}
	flags := 0
	if f.ItalicAngle != 0 {
		flags |= 0x40 // italic
	}
	flags |= 0x20 // non-symbolic font
	w.WriteObjectf(fontDescriptor, `<<
  /Type /FontDescriptor
  /FontName %s
  /Ascent %d
  /Descent %d
  /CapHeight %d
  /FontBBox [%d %d %d %d]
  /ItalicAngle %.4f
  /Flags %d
  /StemV 0
  /FontFile%d %d 0 R
>>`, name, f.Scale(f.Ascender, 1000), f.Scale(f.Descender, 1000),
		f.Scale(f.CapHeight, 1000), f.Scale(f.XMin, 1000),
		f.Scale(f.YMin, 1000), f.Scale(f.XMax, 1000),
		f.Scale(f.YMax, 1000), f.ItalicAngle, flags, fontFile, fontStream)

	// font stream
	w.WriteObjectStart(fontStream)
	streamBuf := &bytes.Buffer{}
	enc := ascii85.NewEncoder(streamBuf)
	enc.Write(cff)
	enc.Close()
	fontStreamBytes := streamBuf.Bytes()

	if cff == nil {
		ttf := f.TTF()
		fmt.Fprintf(w, "<< /Length %d /Length1 %d >>\n", len(ttf), len(ttf))
		fmt.Fprintf(w, "stream\n%s\nendstream\n", ttf)
	} else {
		fmt.Fprintf(w, "<< /Length %d /Length1 %d /Filter /ASCII85Decode /Subtype /CIDFontType0C >>\n", len(fontStreamBytes), len(cff)) // CIDType0C or Type1C depending on the font
		fmt.Fprintf(w, "stream\n%s\nendstream\n", fontStreamBytes)
	}
	w.WriteObjectEnd()

	// to unicode mapping
	w.WriteObjectStart(fontUnicode)
	buf := &bytes.Buffer{}
	fmt.Fprintf(buf, `/CIDInit /ProcSet findresource begin
12 dict begin
begincmap
/CIDSystemInfo << /Registry (FontSpecific) /Ordering (%s) /Supplement 0 >> def
/CMapName /FontSpecific-%s def
/CMapType 2 def
1 begincodespacerange
<0000> <FFFF>
endcodespacerange
`, name[1:], name[1:])
	glyphs := make([]rune, f.NumGlyphs())
	for i := 0; i < math.MaxUint16; i++ {
		glyphs[f.Index(rune(i))] = rune(i)
	}
	total := 0
	for i := 0; i < len(glyphs); i++ {
		if glyphs[i] != 0 {
			total++
		}
	}
	section := 0
	inside := false
	for i := 0; i < len(glyphs); i++ {
		if glyphs[i] == 0 {
			continue
		}
		if section--; section < 0 {
			if section = total; section > 100 {
				section = 100
			}
			total -= section
			if inside {
				fmt.Fprintf(buf, "endbfchar\n")
			}
			fmt.Fprintf(buf, "%d beginbfchar\n", section)
			inside = true
		}
		fmt.Fprintf(buf, "<%04x> <%04x>\n", i, glyphs[i])
	}
	if inside {
		fmt.Fprintf(buf, "endbfchar\n")
	}
	fmt.Fprintf(buf, `endcmap
CMapName currentdict /CMap defineresource pop
end
end`)
	w.WriteStreamPlain(buf.String())
	w.WriteObjectEnd()
}