mirror of
https://github.com/unidoc/unipdf.git
synced 2025-05-05 19:30:30 +08:00
fixed buffer overrun
This commit is contained in:
parent
5176b0b6b7
commit
a904ca32e7
@ -127,7 +127,7 @@ func (csp *ContentStreamProcessor) getColorspace(name string, resources *PdfPage
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Otherwise unsupported.
|
// Otherwise unsupported.
|
||||||
common.Log.Debug("Unknown colorspace requested: %s", name)
|
common.Log.Error("Unknown colorspace requested: %s", name)
|
||||||
return nil, errors.New("Unsupported colorspace")
|
return nil, errors.New("Unsupported colorspace")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -140,6 +140,10 @@ func (csp *ContentStreamProcessor) getInitialColor(cs PdfColorspace) (PdfColor,
|
|||||||
return NewPdfColorDeviceRGB(0.0, 0.0, 0.0), nil
|
return NewPdfColorDeviceRGB(0.0, 0.0, 0.0), nil
|
||||||
case *PdfColorspaceDeviceCMYK:
|
case *PdfColorspaceDeviceCMYK:
|
||||||
return NewPdfColorDeviceCMYK(0.0, 0.0, 0.0, 1.0), nil
|
return NewPdfColorDeviceCMYK(0.0, 0.0, 0.0, 1.0), nil
|
||||||
|
case *PdfColorspaceCalGray:
|
||||||
|
return NewPdfColorCalGray(0.0), nil
|
||||||
|
case *PdfColorspaceCalRGB:
|
||||||
|
return NewPdfColorCalRGB(0.0, 0.0, 0.0), nil
|
||||||
case *PdfColorspaceLab:
|
case *PdfColorspaceLab:
|
||||||
l := 0.0
|
l := 0.0
|
||||||
a := 0.0
|
a := 0.0
|
||||||
@ -190,7 +194,7 @@ func (csp *ContentStreamProcessor) getInitialColor(cs PdfColorspace) (PdfColor,
|
|||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
common.Log.Debug("Unable to determine initial color for unknown colorspace: %T", cs)
|
common.Log.Error("Unable to determine initial color for unknown colorspace: %T", cs)
|
||||||
return nil, errors.New("Unsupported colorspace")
|
return nil, errors.New("Unsupported colorspace")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -364,8 +368,8 @@ func (this *ContentStreamProcessor) handleCommand_SC(op *ContentStreamOperation,
|
|||||||
|
|
||||||
cs := this.graphicsState.ColorspaceStroking
|
cs := this.graphicsState.ColorspaceStroking
|
||||||
if len(op.Params) != cs.GetNumComponents() {
|
if len(op.Params) != cs.GetNumComponents() {
|
||||||
common.Log.Debug("Invalid number of parameters for SC")
|
common.Log.Error("Invalid number of parameters for SC")
|
||||||
common.Log.Debug("Number %d not matching colorspace %T", len(op.Params), cs)
|
common.Log.Error("Number %d not matching colorspace %T", len(op.Params), cs)
|
||||||
return errors.New("Invalid number of parameters")
|
return errors.New("Invalid number of parameters")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -389,8 +393,8 @@ func (this *ContentStreamProcessor) handleCommand_SCN(op *ContentStreamOperation
|
|||||||
|
|
||||||
if !isPatternCS(cs) {
|
if !isPatternCS(cs) {
|
||||||
if len(op.Params) != cs.GetNumComponents() {
|
if len(op.Params) != cs.GetNumComponents() {
|
||||||
common.Log.Debug("Invalid number of parameters for SC")
|
common.Log.Error("Invalid number of parameters for SC")
|
||||||
common.Log.Debug("Number %d not matching colorspace %T", len(op.Params), cs)
|
common.Log.Error("Number %d not matching colorspace %T", len(op.Params), cs)
|
||||||
return errors.New("Invalid number of parameters")
|
return errors.New("Invalid number of parameters")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -411,8 +415,8 @@ func (this *ContentStreamProcessor) handleCommand_sc(op *ContentStreamOperation,
|
|||||||
|
|
||||||
if !isPatternCS(cs) {
|
if !isPatternCS(cs) {
|
||||||
if len(op.Params) != cs.GetNumComponents() {
|
if len(op.Params) != cs.GetNumComponents() {
|
||||||
common.Log.Debug("Invalid number of parameters for SC")
|
common.Log.Error("Invalid number of parameters for SC")
|
||||||
common.Log.Debug("Number %d not matching colorspace %T", len(op.Params), cs)
|
common.Log.Error("Number %d not matching colorspace %T", len(op.Params), cs)
|
||||||
return errors.New("Invalid number of parameters")
|
return errors.New("Invalid number of parameters")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -433,8 +437,8 @@ func (this *ContentStreamProcessor) handleCommand_scn(op *ContentStreamOperation
|
|||||||
|
|
||||||
if !isPatternCS(cs) {
|
if !isPatternCS(cs) {
|
||||||
if len(op.Params) != cs.GetNumComponents() {
|
if len(op.Params) != cs.GetNumComponents() {
|
||||||
common.Log.Debug("Invalid number of parameters for SC")
|
common.Log.Error("Invalid number of parameters for SC")
|
||||||
common.Log.Debug("Number %d not matching colorspace %T", len(op.Params), cs)
|
common.Log.Error("Number %d not matching colorspace %T", len(op.Params), cs)
|
||||||
return errors.New("Invalid number of parameters")
|
return errors.New("Invalid number of parameters")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -454,8 +458,8 @@ func (this *ContentStreamProcessor) handleCommand_scn(op *ContentStreamOperation
|
|||||||
func (this *ContentStreamProcessor) handleCommand_G(op *ContentStreamOperation, resources *PdfPageResources) error {
|
func (this *ContentStreamProcessor) handleCommand_G(op *ContentStreamOperation, resources *PdfPageResources) error {
|
||||||
cs := NewPdfColorspaceDeviceGray()
|
cs := NewPdfColorspaceDeviceGray()
|
||||||
if len(op.Params) != cs.GetNumComponents() {
|
if len(op.Params) != cs.GetNumComponents() {
|
||||||
common.Log.Debug("Invalid number of parameters for SC")
|
common.Log.Error("Invalid number of parameters for SC")
|
||||||
common.Log.Debug("Number %d not matching colorspace %T", len(op.Params), cs)
|
common.Log.Error("Number %d not matching colorspace %T", len(op.Params), cs)
|
||||||
return errors.New("Invalid number of parameters")
|
return errors.New("Invalid number of parameters")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -475,8 +479,8 @@ func (this *ContentStreamProcessor) handleCommand_G(op *ContentStreamOperation,
|
|||||||
func (this *ContentStreamProcessor) handleCommand_g(op *ContentStreamOperation, resources *PdfPageResources) error {
|
func (this *ContentStreamProcessor) handleCommand_g(op *ContentStreamOperation, resources *PdfPageResources) error {
|
||||||
cs := NewPdfColorspaceDeviceGray()
|
cs := NewPdfColorspaceDeviceGray()
|
||||||
if len(op.Params) != cs.GetNumComponents() {
|
if len(op.Params) != cs.GetNumComponents() {
|
||||||
common.Log.Debug("Invalid number of parameters for SC")
|
common.Log.Error("Invalid number of parameters for SC")
|
||||||
common.Log.Debug("Number %d not matching colorspace %T", len(op.Params), cs)
|
common.Log.Error("Number %d not matching colorspace %T", len(op.Params), cs)
|
||||||
return errors.New("Invalid number of parameters")
|
return errors.New("Invalid number of parameters")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -496,8 +500,8 @@ func (this *ContentStreamProcessor) handleCommand_g(op *ContentStreamOperation,
|
|||||||
func (this *ContentStreamProcessor) handleCommand_RG(op *ContentStreamOperation, resources *PdfPageResources) error {
|
func (this *ContentStreamProcessor) handleCommand_RG(op *ContentStreamOperation, resources *PdfPageResources) error {
|
||||||
cs := NewPdfColorspaceDeviceRGB()
|
cs := NewPdfColorspaceDeviceRGB()
|
||||||
if len(op.Params) != cs.GetNumComponents() {
|
if len(op.Params) != cs.GetNumComponents() {
|
||||||
common.Log.Debug("Invalid number of parameters for SC")
|
common.Log.Error("Invalid number of parameters for SC")
|
||||||
common.Log.Debug("Number %d not matching colorspace %T", len(op.Params), cs)
|
common.Log.Error("Number %d not matching colorspace %T", len(op.Params), cs)
|
||||||
return errors.New("Invalid number of parameters")
|
return errors.New("Invalid number of parameters")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -516,8 +520,8 @@ func (this *ContentStreamProcessor) handleCommand_RG(op *ContentStreamOperation,
|
|||||||
func (this *ContentStreamProcessor) handleCommand_rg(op *ContentStreamOperation, resources *PdfPageResources) error {
|
func (this *ContentStreamProcessor) handleCommand_rg(op *ContentStreamOperation, resources *PdfPageResources) error {
|
||||||
cs := NewPdfColorspaceDeviceRGB()
|
cs := NewPdfColorspaceDeviceRGB()
|
||||||
if len(op.Params) != cs.GetNumComponents() {
|
if len(op.Params) != cs.GetNumComponents() {
|
||||||
common.Log.Debug("Invalid number of parameters for SC")
|
common.Log.Error("Invalid number of parameters for SC")
|
||||||
common.Log.Debug("Number %d not matching colorspace %T", len(op.Params), cs)
|
common.Log.Error("Number %d not matching colorspace %T", len(op.Params), cs)
|
||||||
return errors.New("Invalid number of parameters")
|
return errors.New("Invalid number of parameters")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -537,8 +541,8 @@ func (this *ContentStreamProcessor) handleCommand_rg(op *ContentStreamOperation,
|
|||||||
func (this *ContentStreamProcessor) handleCommand_K(op *ContentStreamOperation, resources *PdfPageResources) error {
|
func (this *ContentStreamProcessor) handleCommand_K(op *ContentStreamOperation, resources *PdfPageResources) error {
|
||||||
cs := NewPdfColorspaceDeviceCMYK()
|
cs := NewPdfColorspaceDeviceCMYK()
|
||||||
if len(op.Params) != cs.GetNumComponents() {
|
if len(op.Params) != cs.GetNumComponents() {
|
||||||
common.Log.Debug("Invalid number of parameters for SC")
|
common.Log.Error("Invalid number of parameters for SC")
|
||||||
common.Log.Debug("Number %d not matching colorspace %T", len(op.Params), cs)
|
common.Log.Error("Number %d not matching colorspace %T", len(op.Params), cs)
|
||||||
return errors.New("Invalid number of parameters")
|
return errors.New("Invalid number of parameters")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -557,8 +561,8 @@ func (this *ContentStreamProcessor) handleCommand_K(op *ContentStreamOperation,
|
|||||||
func (this *ContentStreamProcessor) handleCommand_k(op *ContentStreamOperation, resources *PdfPageResources) error {
|
func (this *ContentStreamProcessor) handleCommand_k(op *ContentStreamOperation, resources *PdfPageResources) error {
|
||||||
cs := NewPdfColorspaceDeviceCMYK()
|
cs := NewPdfColorspaceDeviceCMYK()
|
||||||
if len(op.Params) != cs.GetNumComponents() {
|
if len(op.Params) != cs.GetNumComponents() {
|
||||||
common.Log.Debug("Invalid number of parameters for SC")
|
common.Log.Error("Invalid number of parameters for SC")
|
||||||
common.Log.Debug("Number %d not matching colorspace %T", len(op.Params), cs)
|
common.Log.Error("Number %d not matching colorspace %T", len(op.Params), cs)
|
||||||
return errors.New("Invalid number of parameters")
|
return errors.New("Invalid number of parameters")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -354,6 +354,7 @@ func (this *PdfColorspaceDeviceRGB) ImageToRGB(img Image) (Image, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (this *PdfColorspaceDeviceRGB) ImageToGray(img Image) (Image, error) {
|
func (this *PdfColorspaceDeviceRGB) ImageToGray(img Image) (Image, error) {
|
||||||
|
|
||||||
grayImage := img
|
grayImage := img
|
||||||
|
|
||||||
samples := img.GetSamples()
|
samples := img.GetSamples()
|
||||||
@ -374,7 +375,9 @@ func (this *PdfColorspaceDeviceRGB) ImageToGray(img Image) (Image, error) {
|
|||||||
|
|
||||||
// Convert to uint32
|
// Convert to uint32
|
||||||
val := uint32(grayValue * maxVal)
|
val := uint32(grayValue * maxVal)
|
||||||
|
|
||||||
graySamples = append(graySamples, val)
|
graySamples = append(graySamples, val)
|
||||||
|
|
||||||
}
|
}
|
||||||
grayImage.SetSamples(graySamples)
|
grayImage.SetSamples(graySamples)
|
||||||
grayImage.ColorComponents = 1
|
grayImage.ColorComponents = 1
|
||||||
@ -893,7 +896,7 @@ func (this *PdfColorspaceCalRGB) String() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (this *PdfColorspaceCalRGB) GetNumComponents() int {
|
func (this *PdfColorspaceCalRGB) GetNumComponents() int {
|
||||||
return 1
|
return 3
|
||||||
}
|
}
|
||||||
|
|
||||||
func newPdfColorspaceCalRGBFromPdfObject(obj PdfObject) (*PdfColorspaceCalRGB, error) {
|
func newPdfColorspaceCalRGBFromPdfObject(obj PdfObject) (*PdfColorspaceCalRGB, error) {
|
||||||
@ -1119,7 +1122,7 @@ func (this *PdfColorspaceCalRGB) ImageToRGB(img Image) (Image, error) {
|
|||||||
maxVal := math.Pow(2, float64(img.BitsPerComponent)) - 1
|
maxVal := math.Pow(2, float64(img.BitsPerComponent)) - 1
|
||||||
|
|
||||||
rgbSamples := []uint32{}
|
rgbSamples := []uint32{}
|
||||||
for i := 0; i < len(samples); i++ {
|
for i := 0; i < len(samples)-2; i++ {
|
||||||
// A, B, C in range 0.0 to 1.0
|
// A, B, C in range 0.0 to 1.0
|
||||||
aVal := float64(samples[i]) / maxVal
|
aVal := float64(samples[i]) / maxVal
|
||||||
bVal := float64(samples[i+1]) / maxVal
|
bVal := float64(samples[i+1]) / maxVal
|
||||||
@ -1916,6 +1919,7 @@ func (this *PdfColorspaceSpecialPattern) ColorFromFloats(vals []float64) (PdfCol
|
|||||||
// the name of the pattern.
|
// the name of the pattern.
|
||||||
func (this *PdfColorspaceSpecialPattern) ColorFromPdfObjects(objects []PdfObject) (PdfColor, error) {
|
func (this *PdfColorspaceSpecialPattern) ColorFromPdfObjects(objects []PdfObject) (PdfColor, error) {
|
||||||
if len(objects) < 1 {
|
if len(objects) < 1 {
|
||||||
|
common.Log.Error("ColorFromPdfObjects: len(objects)=%d", len(objects))
|
||||||
return nil, errors.New("Invalid number of parameters")
|
return nil, errors.New("Invalid number of parameters")
|
||||||
}
|
}
|
||||||
patternColor := &PdfColorPattern{}
|
patternColor := &PdfColorPattern{}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user