get rid of hacky newDoc2()

This commit is contained in:
Ross Kinder
2015-12-23 15:09:37 -05:00
parent 67af552ad1
commit 6113cc3d6f
5 changed files with 7 additions and 29 deletions

View File

@@ -48,7 +48,7 @@ func Decrypt(privateKey []byte, doc []byte) ([]byte, error) {
return nil, popError()
}
parsedDoc, err := newDoc(doc)
parsedDoc, err := newDoc(doc, nil)
if err != nil {
return nil, err
}

View File

@@ -139,7 +139,7 @@ func Encrypt(publicKey, doc []byte, opts EncryptOptions) ([]byte, error) {
return nil, mustPopError()
}
parsedDoc, err := newDoc(doc)
parsedDoc, err := newDoc(doc, nil)
if err != nil {
return nil, err
}

View File

@@ -88,7 +88,7 @@ func (testSuite *EncryptTest) TestEncrypt(c *C) {
actualPlaintext, err := Decrypt(testSuite.Key, encryptedString)
c.Assert(err, IsNil)
plaintextDoc, _ := newDoc(testSuite.Plaintext)
plaintextDoc, _ := newDoc(testSuite.Plaintext, nil)
expectedPlaintext := dumpDoc(plaintextDoc)
// Big blobs of XML are hard to debug. They are easier to handle when

View File

@@ -58,7 +58,7 @@ func Sign(key []byte, doc []byte, opts SignatureOptions) ([]byte, error) {
return nil, errors.New("failed to load pem key")
}
parsedDoc, err := newDoc2(doc, opts)
parsedDoc, err := newDoc(doc, opts.XMLID)
if err != nil {
return nil, err
}
@@ -134,7 +134,7 @@ func Verify(publicKey []byte, doc []byte, opts SignatureOptions) error {
}
defer C.xmlSecDSigCtxDestroy(dsigCtx)
parsedDoc, err := newDoc2(doc, opts)
parsedDoc, err := newDoc(doc, opts.XMLID)
if err != nil {
return err
}

View File

@@ -49,7 +49,7 @@ func init() {
C.xmlSecErrorsSetCallback((C.xmlSecErrorsCallback)(unsafe.Pointer(C.onError_cgo)))
}
func newDoc(buf []byte) (*C.xmlDoc, error) {
func newDoc(buf []byte, idattrs []XMLIDOption) (*C.xmlDoc, error) {
ctx := C.xmlCreateMemoryParserCtxt((*C.char)(unsafe.Pointer(&buf[0])),
C.int(len(buf)))
if ctx == nil {
@@ -68,29 +68,7 @@ func newDoc(buf []byte) (*C.xmlDoc, error) {
return nil, errors.New("parse failed")
}
return doc, nil
}
func newDoc2(buf []byte, opts SignatureOptions) (*C.xmlDoc, error) {
ctx := C.xmlCreateMemoryParserCtxt((*C.char)(unsafe.Pointer(&buf[0])),
C.int(len(buf)))
if ctx == nil {
return nil, errors.New("error creating parser")
}
defer C.xmlFreeParserCtxt(ctx)
C.xmlParseDocument(ctx)
if ctx.wellFormed == C.int(0) {
return nil, errors.New("malformed XML")
}
doc := ctx.myDoc
if doc == nil {
return nil, errors.New("parse failed")
}
for _, idattr := range opts.XMLID {
for _, idattr := range idattrs {
addIDAttr(C.xmlDocGetRootElement(doc),
idattr.AttributeName, idattr.ElementName, idattr.ElementNamespace)
}