zoukankan      html  css  js  c++  java
  • Android系统file_contexts二进制与文本转换工具

      1 #ifdef _WIN32
      2     #define __USE_MINGW_ANSI_STDIO 1
      3 #endif
      4 
      5 #include <stdio.h>
      6 #include <stdlib.h>
      7 #include <string.h>
      8 #include <errno.h>
      9 #include <fcntl.h>
     10 #ifdef HAS_STDINT_H
     11     #include <stdint.h>
     12 #endif
     13 #ifdef unix
     14     #include <unistd.h>
     15     #include <sys/types.h>
     16     #include <sys/stat.h>
     17 #else
     18     #include <direct.h>
     19     #include <io.h>
     20 #endif
     21 
     22 #define MAGIC 0xF97CFF8A
     23 
     24 #define S_IFMT      00170000
     25 #define S_IFSOCK    0140000
     26 #define S_IFLNK        0120000
     27 #define S_IFREG        0100000
     28 #define S_IFBLK        0060000
     29 #define S_IFDIR        0040000
     30 #define S_IFCHR        0020000
     31 #define S_IFIFO        0010000
     32 #define S_ISUID        0004000
     33 #define S_ISGID        0002000
     34 #define S_ISVTX        0001000
     35 
     36 #define S_ISLNK(m)    (((m) & S_IFMT) == S_IFLNK)
     37 #define S_ISREG(m)    (((m) & S_IFMT) == S_IFREG)
     38 #define S_ISDIR(m)    (((m) & S_IFMT) == S_IFDIR)
     39 #define S_ISCHR(m)    (((m) & S_IFMT) == S_IFCHR)
     40 #define S_ISBLK(m)    (((m) & S_IFMT) == S_IFBLK)
     41 #define S_ISFIFO(m)    (((m) & S_IFMT) == S_IFIFO)
     42 #define S_ISSOCK(m)    (((m) & S_IFMT) == S_IFSOCK)
     43 
     44 char *mode_to_string(unsigned int mode)
     45 {
     46     if (S_ISBLK(mode))
     47         return "-b";
     48     else if (S_ISCHR(mode))
     49         return "-c";
     50     else if (S_ISDIR(mode))
     51         return "-d";
     52     else if (S_ISFIFO(mode))
     53         return "-p";
     54     else if (S_ISLNK(mode))
     55         return "-l";
     56     else if (S_ISSOCK(mode))
     57         return "-s";
     58     else if (S_ISREG(mode))
     59         return "--";
     60     else
     61         return "";
     62 }
     63 
     64 int main(int argc, char *argv[])
     65 {
     66     unsigned int selinux_vers;
     67     unsigned int nothing;
     68     unsigned int i;
     69     unsigned int steams;
     70     unsigned int num_of_regex;
     71 
     72     char steam[256];
     73     char src[512];
     74     char context[512];
     75 
     76     FILE *fp = NULL;
     77     FILE *fpout = NULL;
     78 
     79     if (argc != 3) {
     80         printf("usage: %s BINARY_FILE OUTPUT_TEXT_FILE
    ", argv[0]);
     81         printf("e.g: %s file_contexts.bin file_contexts
    ", argv[0]);
     82         return 1;
     83     }
     84 
     85     if ((fp = fopen(argv[1], "rb")) == NULL) {
     86         printf("Unable to open %s : ", argv[1]);
     87         perror("");
     88         return 1;
     89     }
     90     fread(&nothing, 1, 4, fp);
     91     if (nothing != MAGIC) {
     92         printf("%s is not a binary!
    ", argv[1]);
     93         if (fp) fclose(fp);
     94         return 1;
     95     }
     96 
     97     fread(&nothing, 1, 4, fp);
     98   selinux_vers = nothing;
     99     fread(&nothing, 1, 4, fp);
    100     fseek(fp, (unsigned int)ftell(fp) + nothing, SEEK_SET);
    101 
    102     fread(&nothing, 1, 4, fp);
    103     //printf("num of steams: 0x%x
    ", nothing);
    104 
    105     for (i=0; i<nothing; ++i) {
    106         fread(&steams, 1, 4, fp);
    107       fread(steam, 1, steams+1, fp);
    108         //printf("%s
    ", steam);
    109     }
    110 
    111     fread(&num_of_regex, 1, 4, fp);
    112     //printf("num of regex: 0x%x
    ", num_of_regex);
    113 
    114     if ((fpout = fopen(argv[2], "wb")) == NULL) {
    115         printf("unable to open %s for write!
    ", argv[2]);
    116         if (fp) fclose(fp);
    117         return 1;
    118     }
    119 
    120   //printf("selinux version: %d
    ", selinux_vers);
    121   fprintf(fpout, "#context version %d
    ", selinux_vers);
    122 
    123     for (i=0; i<num_of_regex; ++i) {
    124         fread(&nothing, 1, 4, fp);
    125       fread(context, 1, nothing, fp);
    126     fread(&nothing, 1, 4, fp);
    127     fread(src, 1, nothing, fp);
    128         //printf("%s %s
    ", src, context);
    129         fread(&nothing, 1, 4, fp);
    130         fprintf(fpout, "%s	%s	%s
    ", src, mode_to_string(nothing), context);
    131       fseek(fp, (unsigned int)ftell(fp) + 12, SEEK_SET);
    132         fread(&nothing, 1, 4, fp);
    133     fseek(fp, (unsigned int)ftell(fp) + nothing, SEEK_SET);
    134         fread(&nothing, 1 , 4, fp);
    135     fread(&nothing, 1 , 4, fp);
    136     fseek(fp, (unsigned int)ftell(fp) + nothing - 4, SEEK_SET);
    137     }
    138 
    139     if (fp) fclose(fp);
    140     if (fpout) fclose(fpout);
    141 
    142     return 0;
    143 }
  • 相关阅读:
    生成操作 嵌入的资源
    用JavaScript操作CSS滤镜实现最近新闻旁边的“new”
    26个ASP.NET常用性能优化方法
    kafka集群搭建(windows环境下)
    AtCoder Beginner Contest 215 F Dist Max 2(二分、尺取)
    CodeForces 1487C Minimum Ties(建图、模拟)
    C#获取 URL参数
    编程给程序员带来哪些坏习惯
    【转载】IIS和服务器安全设置教程
    [转载]在IE8下动易SiteWeaver后台编辑器按钮没有反应的解决方案
  • 原文地址:https://www.cnblogs.com/lyuyangly/p/8586119.html
Copyright © 2011-2022 走看看