Add missing keymaps for the c200, h100, and h300 to the manual. Long-record to enter...
[Rockbox.git] / apps / plugins / md5sum.c
blob65cc0c0a5e60ad39359bd874300d57308d4e6c8e
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id: $
10 * Copyright (C) 2008 Antoine Cellerier <dionoea -at- videolan -dot- org>
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include "plugin.h"
21 #include "lib/md5.h"
23 PLUGIN_HEADER
25 static const struct plugin_api *rb;
27 MEM_FUNCTION_WRAPPERS(rb);
29 static int count = 0;
30 static int done = 0;
32 static int hash( char *string, const char *path )
34 char *buffer[512];
35 ssize_t len;
36 struct md5_s md5;
37 int in = rb->open( path, O_RDONLY );
38 if( in < 0 ) return -1;
40 InitMD5( &md5 );
41 while( ( len = rb->read( in, buffer, 512 ) ) > 0 )
42 AddMD5( &md5, buffer, len );
43 EndMD5( &md5 );
45 psz_md5_hash( string, &md5 );
47 rb->close( in );
48 return 0;
51 static void hash_file( int out, const char *path )
53 if( out < 0 )
54 count++;
55 else
57 char string[MD5_STRING_LENGTH+1];
58 done++;
59 rb->splash( 0, "%d / %d : %s", done, count, path );
60 if( hash( string, path ) )
61 rb->write( out, "error", 5 );
62 else
63 rb->write( out, string, MD5_STRING_LENGTH );
64 rb->write( out, " ", 2 );
65 rb->write( out, path, rb->strlen( path ) );
66 rb->write( out, "\n", 1 );
70 static void hash_dir( int out, const char *path );
71 static void hash_dir( int out, const char *path )
73 DIR *dir;
74 struct dirent *entry;
76 dir = rb->opendir( path );
77 if( dir )
79 while( ( entry = rb->readdir( dir ) ) )
81 char childpath[MAX_PATH];
82 rb->snprintf( childpath, MAX_PATH, "%s/%s",
83 path, entry->d_name );
84 if( entry->attribute & ATTR_DIRECTORY )
86 if( rb->strcmp( entry->d_name, "." )
87 && rb->strcmp( entry->d_name, ".." ) )
89 /* Got a sub directory */
90 hash_dir( out, childpath );
93 else
95 /* Got a file */
96 hash_file( out, childpath );
99 rb->closedir( dir );
103 static void hash_list( int out, const char *path )
105 int list = rb->open( path, O_RDONLY );
106 char newpath[MAX_PATH];
107 if( list < 0 ) return;
109 while( rb->read_line( list, newpath, MAX_PATH ) > 0 )
111 DIR *dir = rb->opendir( newpath );
112 if( dir )
114 rb->closedir( dir );
115 hash_dir( out, newpath );
117 else
119 hash_file( out, newpath );
123 rb->close( list );
126 static void hash_check( int out, const char *path )
128 int list = rb->open( path, O_RDONLY );
129 char line[MD5_STRING_LENGTH+1+MAX_PATH+1];
130 int len;
131 if( list < 0 ) return;
133 while( ( len = rb->read_line( list, line, MD5_STRING_LENGTH+1+MAX_PATH+1 ) ) > 0 )
135 if( out < 0 )
136 count++;
137 else
139 const char *filename = rb->strchr( line, ' ' );
140 done++;
141 rb->splash( 0, "%d / %d : %s", done, count, filename );
142 if( !filename || len < MD5_STRING_LENGTH + 2 )
144 const char error[] = "Malformed input line ... skipping";
145 rb->write( out, error, rb->strlen( error ) );
147 else
149 char string[MD5_STRING_LENGTH+1];
150 while( *filename == ' ' )
151 filename++;
152 rb->write( out, filename, rb->strlen( filename ) );
153 rb->write( out, ": ", 2 );
154 if( hash( string, filename ) )
155 rb->write( out, "FAILED open or read", 19 );
156 else if( rb->strncasecmp( line, string, MD5_STRING_LENGTH ) )
157 rb->write( out, "FAILED", 6 );
158 else
159 rb->write( out, "OK", 2 );
161 rb->write( out, "\n", 1 );
165 rb->close( list );
168 enum plugin_status plugin_start(const struct plugin_api* api, const void* parameter)
170 const char *arg = (const char *)parameter; /* input file name, if any */
171 int out = -1; /* output file descriptor */
172 char filename[MAX_PATH]; /* output file name */
174 void (*action)( int, const char * ) = NULL;
176 md5_init( api );
177 rb = api;
178 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
179 rb->cpu_boost( true );
180 #endif
182 if( arg && *arg )
184 const char *ext = rb->strrchr( arg, '.' );
185 DIR *dir;
186 rb->snprintf( filename, MAX_PATH, "%s.md5sum", arg );
188 if( ext )
190 if( !rb->strcmp( ext, ".md5" ) || !rb->strcmp( ext, ".md5sum" ) )
192 rb->snprintf( filename + ( ext - arg ),
193 MAX_PATH + rb->strlen( ext ) - rb->strlen( arg ),
194 ".md5check" );
195 /* Lets check the sums */
196 action = hash_check;
198 else if( !rb->strcmp( ext, ".md5list" ) ) /* ugly */
200 /* Hash listed files */
201 action = hash_list;
205 if( !action )
207 dir = rb->opendir( arg );
208 if( dir )
210 api->closedir( dir );
212 /* Hash the directory's content recursively */
213 action = hash_dir;
215 else
217 /* Hash the file */
218 action = hash_file;
222 else
224 rb->snprintf( filename, MAX_PATH, "/everything.md5sum" );
225 /* Hash the whole filesystem */
226 action = hash_dir;
227 arg = "/";
230 rb->lcd_puts( 0, 1, "Output file:" );
231 rb->lcd_puts( 0, 2, filename );
233 count = 0;
234 done = 0;
235 action( out, arg );
237 out = rb->open( filename, O_WRONLY|O_CREAT );
238 if( out < 0 ) return PLUGIN_ERROR;
239 action( out, arg );
240 rb->close( out );
241 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
242 rb->cpu_boost( false );
243 #endif
244 return PLUGIN_OK;