mirror of
https://github.com/ipxe/ipxe
synced 2025-12-10 21:41:09 +03:00
[pnm] Remove userptr_t from PNM image parsing
Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
@@ -65,7 +65,8 @@ struct pixel_buffer * alloc_pixbuf ( unsigned int width, unsigned int height ) {
|
|||||||
ref_init ( &pixbuf->refcnt, free_pixbuf );
|
ref_init ( &pixbuf->refcnt, free_pixbuf );
|
||||||
pixbuf->width = width;
|
pixbuf->width = width;
|
||||||
pixbuf->height = height;
|
pixbuf->height = height;
|
||||||
pixbuf->len = ( width * height * sizeof ( uint32_t ) );
|
pixbuf->pixels = ( width * height );
|
||||||
|
pixbuf->len = ( pixbuf->pixels * sizeof ( uint32_t ) );
|
||||||
|
|
||||||
/* Check for multiplication overflow */
|
/* Check for multiplication overflow */
|
||||||
if ( ( width != 0 ) &&
|
if ( ( width != 0 ) &&
|
||||||
|
|||||||
@@ -46,21 +46,21 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
|
|||||||
static int pnm_ascii ( struct image *image, struct pnm_context *pnm ) {
|
static int pnm_ascii ( struct image *image, struct pnm_context *pnm ) {
|
||||||
char buf[ pnm->ascii_len + 1 /* NUL */ ];
|
char buf[ pnm->ascii_len + 1 /* NUL */ ];
|
||||||
char *endp;
|
char *endp;
|
||||||
|
char ch;
|
||||||
size_t len;
|
size_t len;
|
||||||
int value;
|
int value;
|
||||||
int in_comment = 0;
|
int in_comment = 0;
|
||||||
|
|
||||||
/* Skip any leading whitespace and comments */
|
/* Skip any leading whitespace and comments */
|
||||||
for ( ; pnm->offset < image->len ; pnm->offset++ ) {
|
for ( ; pnm->offset < image->len ; pnm->offset++ ) {
|
||||||
copy_from_user ( &buf[0], image->data, pnm->offset,
|
ch = *( ( ( const uint8_t * ) image->data ) + pnm->offset );
|
||||||
sizeof ( buf[0] ) );
|
|
||||||
if ( in_comment ) {
|
if ( in_comment ) {
|
||||||
if ( buf[0] == '\n' )
|
if ( ch == '\n' )
|
||||||
in_comment = 0;
|
in_comment = 0;
|
||||||
} else {
|
} else {
|
||||||
if ( buf[0] == '#' ) {
|
if ( ch == '#' ) {
|
||||||
in_comment = 1;
|
in_comment = 1;
|
||||||
} else if ( ! isspace ( buf[0] ) ) {
|
} else if ( ! isspace ( ch ) ) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -76,7 +76,7 @@ static int pnm_ascii ( struct image *image, struct pnm_context *pnm ) {
|
|||||||
/* Copy ASCII value to buffer and ensure string is NUL-terminated */
|
/* Copy ASCII value to buffer and ensure string is NUL-terminated */
|
||||||
if ( len > ( sizeof ( buf ) - 1 /* NUL */ ) )
|
if ( len > ( sizeof ( buf ) - 1 /* NUL */ ) )
|
||||||
len = ( sizeof ( buf ) - 1 /* NUL */ );
|
len = ( sizeof ( buf ) - 1 /* NUL */ );
|
||||||
copy_from_user ( buf, image->data, pnm->offset, len );
|
memcpy ( buf, ( image->data + pnm->offset ), len );
|
||||||
buf[len] = '\0';
|
buf[len] = '\0';
|
||||||
|
|
||||||
/* Parse value and update offset */
|
/* Parse value and update offset */
|
||||||
@@ -104,7 +104,7 @@ static int pnm_ascii ( struct image *image, struct pnm_context *pnm ) {
|
|||||||
* @ret value Value, or negative error
|
* @ret value Value, or negative error
|
||||||
*/
|
*/
|
||||||
static int pnm_binary ( struct image *image, struct pnm_context *pnm ) {
|
static int pnm_binary ( struct image *image, struct pnm_context *pnm ) {
|
||||||
uint8_t value;
|
const uint8_t *value;
|
||||||
|
|
||||||
/* Sanity check */
|
/* Sanity check */
|
||||||
if ( pnm->offset == image->len ) {
|
if ( pnm->offset == image->len ) {
|
||||||
@@ -114,10 +114,8 @@ static int pnm_binary ( struct image *image, struct pnm_context *pnm ) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Extract value */
|
/* Extract value */
|
||||||
copy_from_user ( &value, image->data, pnm->offset, sizeof ( value ) );
|
value = ( image->data + pnm->offset++ );
|
||||||
pnm->offset++;
|
return *value;
|
||||||
|
|
||||||
return value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -189,15 +187,15 @@ static uint32_t pnm_pixmap ( uint32_t composite, unsigned int index __unused ) {
|
|||||||
static int pnm_data ( struct image *image, struct pnm_context *pnm,
|
static int pnm_data ( struct image *image, struct pnm_context *pnm,
|
||||||
struct pixel_buffer *pixbuf ) {
|
struct pixel_buffer *pixbuf ) {
|
||||||
struct pnm_type *type = pnm->type;
|
struct pnm_type *type = pnm->type;
|
||||||
size_t offset = 0;
|
unsigned int pixels = pixbuf->pixels;
|
||||||
|
unsigned int index = 0;
|
||||||
unsigned int xpos = 0;
|
unsigned int xpos = 0;
|
||||||
int scalar;
|
int scalar;
|
||||||
uint32_t composite;
|
uint32_t composite;
|
||||||
uint32_t rgb;
|
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
|
|
||||||
/* Fill pixel buffer */
|
/* Fill pixel buffer */
|
||||||
while ( offset < pixbuf->len ) {
|
while ( index < pixels ) {
|
||||||
|
|
||||||
/* Extract a scaled composite scalar value from the file */
|
/* Extract a scaled composite scalar value from the file */
|
||||||
composite = 0;
|
composite = 0;
|
||||||
@@ -213,15 +211,12 @@ static int pnm_data ( struct image *image, struct pnm_context *pnm,
|
|||||||
|
|
||||||
/* Extract 24-bit RGB values from composite value */
|
/* Extract 24-bit RGB values from composite value */
|
||||||
for ( i = 0 ; i < type->packing ; i++ ) {
|
for ( i = 0 ; i < type->packing ; i++ ) {
|
||||||
if ( offset >= pixbuf->len ) {
|
if ( index >= pixels ) {
|
||||||
DBGC ( image, "PNM %s has too many pixels\n",
|
DBGC ( image, "PNM %s has too many pixels\n",
|
||||||
image->name );
|
image->name );
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
rgb = type->rgb ( composite, i );
|
pixbuf->data[index++] = type->rgb ( composite, i );
|
||||||
copy_to_user ( pixbuf->data, offset, &rgb,
|
|
||||||
sizeof ( rgb ) );
|
|
||||||
offset += sizeof ( rgb );
|
|
||||||
if ( ++xpos == pixbuf->width ) {
|
if ( ++xpos == pixbuf->width ) {
|
||||||
xpos = 0;
|
xpos = 0;
|
||||||
break;
|
break;
|
||||||
@@ -287,19 +282,19 @@ static struct pnm_type pnm_types[] = {
|
|||||||
* @ret type PNM image type, or NULL if not found
|
* @ret type PNM image type, or NULL if not found
|
||||||
*/
|
*/
|
||||||
static struct pnm_type * pnm_type ( struct image *image ) {
|
static struct pnm_type * pnm_type ( struct image *image ) {
|
||||||
struct pnm_signature signature;
|
const struct pnm_signature *signature;
|
||||||
struct pnm_type *type;
|
struct pnm_type *type;
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
|
|
||||||
/* Extract signature */
|
/* Extract signature */
|
||||||
assert ( image->len >= sizeof ( signature ) );
|
assert ( image->len >= sizeof ( *signature ) );
|
||||||
copy_from_user ( &signature, image->data, 0, sizeof ( signature ) );
|
signature = image->data;
|
||||||
|
|
||||||
/* Check for supported types */
|
/* Check for supported types */
|
||||||
for ( i = 0 ; i < ( sizeof ( pnm_types ) /
|
for ( i = 0 ; i < ( sizeof ( pnm_types ) /
|
||||||
sizeof ( pnm_types[0] ) ) ; i++ ) {
|
sizeof ( pnm_types[0] ) ) ; i++ ) {
|
||||||
type = &pnm_types[i];
|
type = &pnm_types[i];
|
||||||
if ( type->type == signature.type )
|
if ( type->type == signature->type )
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -390,23 +385,23 @@ static int pnm_pixbuf ( struct image *image, struct pixel_buffer **pixbuf ) {
|
|||||||
* @ret rc Return status code
|
* @ret rc Return status code
|
||||||
*/
|
*/
|
||||||
static int pnm_probe ( struct image *image ) {
|
static int pnm_probe ( struct image *image ) {
|
||||||
struct pnm_signature signature;
|
const struct pnm_signature *signature;
|
||||||
|
|
||||||
/* Sanity check */
|
/* Sanity check */
|
||||||
if ( image->len < sizeof ( signature ) ) {
|
if ( image->len < sizeof ( *signature ) ) {
|
||||||
DBGC ( image, "PNM %s is too short\n", image->name );
|
DBGC ( image, "PNM %s is too short\n", image->name );
|
||||||
return -ENOEXEC;
|
return -ENOEXEC;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Check signature */
|
/* Check signature */
|
||||||
copy_from_user ( &signature, image->data, 0, sizeof ( signature ) );
|
signature = image->data;
|
||||||
if ( ! ( ( signature.magic == PNM_MAGIC ) &&
|
if ( ! ( ( signature->magic == PNM_MAGIC ) &&
|
||||||
( isdigit ( signature.type ) ) &&
|
( isdigit ( signature->type ) ) &&
|
||||||
( isspace ( signature.space ) ) ) ) {
|
( isspace ( signature->space ) ) ) ) {
|
||||||
DBGC ( image, "PNM %s has invalid signature\n", image->name );
|
DBGC ( image, "PNM %s has invalid signature\n", image->name );
|
||||||
return -ENOEXEC;
|
return -ENOEXEC;
|
||||||
}
|
}
|
||||||
DBGC ( image, "PNM %s is type %c\n", image->name, signature.type );
|
DBGC ( image, "PNM %s is type %c\n", image->name, signature->type );
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,6 +22,8 @@ struct pixel_buffer {
|
|||||||
unsigned int height;
|
unsigned int height;
|
||||||
/** 32-bit (8:8:8:8) xRGB pixel data, in host-endian order */
|
/** 32-bit (8:8:8:8) xRGB pixel data, in host-endian order */
|
||||||
uint32_t *data;
|
uint32_t *data;
|
||||||
|
/** Total number of pixels */
|
||||||
|
unsigned int pixels;
|
||||||
/** Total length */
|
/** Total length */
|
||||||
size_t len;
|
size_t len;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -10,7 +10,6 @@
|
|||||||
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
|
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <ipxe/uaccess.h>
|
|
||||||
#include <ipxe/image.h>
|
#include <ipxe/image.h>
|
||||||
|
|
||||||
/** PNM signature */
|
/** PNM signature */
|
||||||
|
|||||||
Reference in New Issue
Block a user