1 #ifdef _WIN322 #define__use_mingw_ansi_stdio 13 #endif4 5#include <stdio.h>6#include <stdlib.h>7#include <string.h>8#include <errno.h>9#include <fcntl.h>Ten #ifdef Has_stdint_h One#include <stdint.h> A #endif - #ifdef UNIX -#include <unistd.h> the#include <sys/types.h> -#include <sys/stat.h> - #else -#include <direct.h> +#include <io.h> - #endif + A #defineMAGIC 0xf97cff8a at - #defineS_ifmt 00170000 - #defineS_ifsock 0140000 - #defineS_iflnk 0120000 - #defineS_ifreg 0100000 - #defineS_ifblk 0060000 in #defineS_ifdir 0040000 - #defineS_IFCHR 0020000 to #defineS_ififo 0010000 + #defineS_isuid 0004000 - #defineS_isgid 0002000 the #defineS_ISVTX 0001000 * $ #defineS_islnk (M) (((m) & s_ifmt) = = S_iflnk)Panax Notoginseng #defineS_isreg (M) (((m) & s_ifmt) = = S_ifreg) - #defineS_isdir (M) (((m) & s_ifmt) = = S_ifdir) the #defineS_ISCHR (M) (((m) & s_ifmt) = = S_IFCHR) + #defineS_ISBLK (M) (((m) & s_ifmt) = = s_ifblk) A #defineS_isfifo (M) (((m) & s_ifmt) = = S_ififo) the #defineS_issock (M) (((m) & s_ifmt) = = S_ifsock) + - Char*mode_to_string (unsignedintmode) $ { $ if(s_isblk (mode)) - return "- b"; - Else if(S_ISCHR (mode)) the return "- C"; - Else if(S_isdir (mode))Wuyi return "- D"; the Else if(S_isfifo (mode)) - return "- P"; Wu Else if(S_islnk (mode)) - return "- L"; About Else if(S_issock (mode)) $ return "- S"; - Else if(S_isreg (mode)) - return "--"; - Else A return ""; + } the - intMainintargcChar*argv[]) $ { theUnsignedintselinux_vers; theUnsignedintNothing ; theUnsignedinti; theUnsignedintsteams; -UnsignedintNum_of_regex; in the Charsteam[ the]; the Charsrc[ +]; About Charcontext[ +]; the theFILE *FP =NULL; theFILE *fpout =NULL; + - if(ARGC! =3) { theprintf"Usage:%s binary_file output_text_file\n", argv[0]);Bayiprintf"e.g:%s file_contexts.bin file_contexts\n", argv[0]); the return 1; the } - - if(fp = fopen (argv[1],"RB")) ==NULL) { theprintf"Unable to open%s:", argv[1]); thePerror (""); the return 1; the } -Fread (¬hing,1,4, FP); the if(Nothing! =MAGIC) { theprintf"%s is not a binary!\n", argv[1]); the if(FP) fclose (FP);94 return 1; the } the theFread (¬hing,1,4, FP);98Selinux_vers =Nothing ; AboutFread (¬hing,1,4, FP); -Fseek (FP, (unsignedint) Ftell (FP) +Nothing , seek_set);101 102Fread (¬hing,1,4, FP);103 //printf ("Num of steams:0x%x\n", nothing);104 the for(i=0; i<nothing; ++i) {106Fread (&steams,1,4, FP);107Fread (Steam,1, steams+1, FP);108 //printf ("%s\n", steam);109 } the 111Fread (&num_of_regex,1,4, FP); the //printf ("Num of regex:0x%x\n", Num_of_regex);113 the if(Fpout = fopen (argv[2],"WB")) ==NULL) { theprintf"Unable to open%s for write!\n", argv[2]); the if(FP) fclose (FP);117 return 1;118 }119 - //printf ("SELinux version:%d\n", selinux_vers);121fprintf (Fpout,"#context version%d\n", selinux_vers);122 123 for(i=0; i<num_of_regex; ++i) {124Fread (¬hing,1,4, FP); theFread (Context,1, nothing, FP);126Fread (¬hing,1,4, FP);127Fread (SRC,1, nothing, FP); - //printf ("%s%s\n", SRC, context);129Fread (¬hing,1,4, FP); thefprintf (Fpout,"%s\t%s\t%s\n", SRC, mode_to_string (Nothing), context);131Fseek (FP, (unsignedint) Ftell (FP) + A, seek_set); theFread (¬hing,1,4, FP);133Fseek (FP, (unsignedint) Ftell (FP) +Nothing , seek_set);134Fread (¬hing,1,4, FP);135Fread (¬hing,1,4, FP);136Fseek (FP, (unsignedint) Ftell (FP) + Nothing-4, seek_set);137 }138 139 if(FP) fclose (FP); $ if(fpout) fclose (fpout);141 142 return 0;143}
Android system file_contexts binary and text conversion tool