]>
glassweightruler.freedombox.rocks Git - Ventoy.git/blob - wimboot/wimboot-2.7.3/src/cpio.c
2 * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
36 * Align CPIO length to nearest dword
39 * @ret len Aligned length
41 static size_t cpio_align ( size_t len
) {
42 return ( ( len
+ 0x03 ) & ~0x03 );
46 * Parse CPIO field value
48 * @v field ASCII field
49 * @ret value Field value
51 static unsigned long cpio_value ( const char *field
) {
54 memcpy ( buf
, field
, ( sizeof ( buf
) - 1 ) );
55 buf
[ sizeof ( buf
) - 1 ] = '\0';
56 return strtoul ( buf
, NULL
, 16 );
60 * Extract files from CPIO archive
62 * @v data CPIO archive
63 * @v len Maximum length of CPIO archive
64 * @v file File handler
65 * @ret rc Return status code
67 int cpio_extract ( void *data
, size_t len
,
68 int ( * file
) ( const char *name
, void *data
,
70 const struct cpio_header
*cpio
;
72 const char *file_name
;
81 /* Skip over any padding */
82 while ( len
>= sizeof ( *pad
) ) {
86 data
+= sizeof ( *pad
);
87 len
-= sizeof ( *pad
);
90 /* Stop if we have reached the end of the archive */
95 if ( len
< sizeof ( *cpio
) ) {
96 DBG ( "Truncated CPIO header\n" );
102 if ( memcmp ( cpio
->c_magic
, CPIO_MAGIC
,
103 sizeof ( cpio
->c_magic
) ) != 0 ) {
104 DBG ( "Bad CPIO magic\n" );
108 /* Extract file parameters */
109 file_name
= ( ( void * ) ( cpio
+ 1 ) );
110 file_name_len
= cpio_value ( cpio
->c_namesize
);
111 file_data
= ( data
+ cpio_align ( sizeof ( *cpio
) +
113 file_len
= cpio_value ( cpio
->c_filesize
);
114 cpio_len
= ( file_data
+ file_len
- data
);
115 if ( cpio_len
< len
)
116 cpio_len
= cpio_align ( cpio_len
);
117 if ( cpio_len
> len
) {
118 DBG ( "Truncated CPIO file\n" );
122 /* If we reach the trailer, we're done */
123 if ( strcmp ( file_name
, CPIO_TRAILER
) == 0 )
127 if ( ( rc
= file ( file_name
, file_data
, file_len
) ) != 0 )
130 /* Move to next file */