Changeset 225

Show
Ignore:
Timestamp:
10/08/05 18:25:43
Author:
ben
Message:

New! Improved! Nearly working armour.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • openpgpsdk/trunk/examples/packet-dump.c

    r221 r225  
    2020    { 
    2121    int i=0; 
    22     for(i=0 ; i<indent ; i++) 
     22 
     23    for(i=0 ; i < indent ; i++) 
    2324        printf("  "); 
    2425    } 
     
    116117    } 
    117118 
    118 static void print_hexdump( char *name, 
     119static void print_hexdump(const char *name, 
    119120                          const unsigned char *data, 
    120121                          unsigned int len) 
     
    127128    } 
    128129 
    129 static void print_hexdump_data( char *name, 
    130                           const unsigned char *data, 
    131                           unsigned int len) 
     130static void print_hexdump_data(const char *name, 
     131                               const unsigned char *data, 
     132                               unsigned int len) 
    132133    { 
    133134    print_name(name); 
     
    138139    } 
    139140 
    140 static void print_data( char *name, const ops_data_t *data) 
    141     { 
    142     print_hexdump( name, data->contents, data->len); 
    143     } 
    144  
    145  
    146 static void print_boolean(char *name, unsigned char bool) 
     141static void print_data(const char *name,const ops_data_t *data) 
     142    { 
     143    print_hexdump(name,data->contents,data->len); 
     144    } 
     145 
     146 
     147static void print_boolean(const char *name, unsigned char bool) 
    147148    { 
    148149    print_name(name); 
     
    155156    } 
    156157 
    157 static void print_tagname(char *str) 
     158static void print_tagname(const char *str) 
    158159    { 
    159160    print_indent(); 
     
    175176    } 
    176177 
    177 static void print_unsigned_int( char *name, unsigned int val) 
     178static void print_block(const char *name,const unsigned char *str, 
     179                        size_t length) 
     180    { 
     181    int o=length; 
     182 
     183    print_indent(); 
     184    printf(">>>>> %s >>>>>\n",name); 
     185 
     186    print_indent(); 
     187    for( ; length > 0 ; --length) 
     188        { 
     189        if(*str >= 0x20 && *str < 0x7f && *str != '%') 
     190            putchar(*str); 
     191        else if(*str == '\n') 
     192            { 
     193            putchar(*str); 
     194            print_indent(); 
     195            } 
     196        else 
     197            printf("%%%02x",*str); 
     198        ++str; 
     199        } 
     200    if(o && str[-1] != '\n') 
     201        { 
     202        putchar('\n'); 
     203        print_indent(); 
     204        fputs("[no newline]",stdout); 
     205        } 
     206    else 
     207        print_indent(); 
     208    printf("<<<<< %s <<<<<\n",name); 
     209    } 
     210 
     211static void print_unsigned_int(char *name, unsigned int val) 
    178212    { 
    179213    print_name(name); 
     
    741775    case OPS_PTAG_CT_SIGNED_CLEARTEXT_BODY: 
    742776        print_tagname("SIGNED CLEARTEXT BODY"); 
    743         printf("----------------\n%.*s\n----------------\n", 
    744                (int)content->signed_cleartext_body.length, 
    745                content->signed_cleartext_body.data); 
     777        print_block("signed cleartext",content->signed_cleartext_body.data, 
     778                    content->signed_cleartext_body.length); 
     779        break; 
     780 
     781    case OPS_PTAG_CT_UNARMOURED_TEXT: 
     782        print_tagname("UNARMOURED TEXT"); 
     783        print_block("unarmoured text",content->unarmoured_text.data, 
     784                    content->unarmoured_text.length); 
     785        break; 
     786 
     787    case OPS_PTAG_CT_ARMOUR_TRAILER: 
     788        print_tagname("ARMOUR TRAILER"); 
     789        print_string("type",content->armour_header.type); 
    746790        break; 
    747791 
  • openpgpsdk/trunk/include/packet-parse.h

    r221 r225  
    2626    OPS_R_OK            =0,     /*!< success */ 
    2727    OPS_R_EOF           =1,     /*!< reached end of file, no data has been returned */ 
    28     OPS_R_EARLY_EOF     =2,     /*!< could not read the requested amount of bytes and OPS_RETURN_LENGTH was not set and at least 1 byte was read */ 
     28    OPS_R_EARLY_EOF     =2,     /*!< could not read the requested 
     29                                  number of bytes and either 
     30                                  OPS_RETURN_LENGTH was not set and at 
     31                                  least 1 byte was read, or there was 
     32                                  an abnormal end to the file (or 
     33                                  armoured block) */ 
    2934    OPS_R_PARTIAL_READ  =3,     /*!< if OPS_RETURN_LENGTH is set and the buffer was not filled */ 
     35    _OPS_R_BLOCK_END    =4,     /*!< the end of an armoured block 
     36                                  [interal use only] */ 
    3037    }; 
    3138 
  • openpgpsdk/trunk/include/packet.h

    r221 r225  
    195195    OPS_PTAG_CT_SIGNED_CLEARTEXT_HEADER =0x300+6, 
    196196    OPS_PTAG_CT_SIGNED_CLEARTEXT_BODY   =0x300+7, 
     197    OPS_PTAG_CT_UNARMOURED_TEXT         =0x300+8, 
    197198    }; 
    198199 
     
    690691    unsigned char               data[8192]; 
    691692    } ops_signed_cleartext_body_t; 
     693 
     694typedef struct 
     695    { 
     696    unsigned                    length; 
     697    unsigned char               *data; 
     698    } ops_unarmoured_text_t; 
    692699 
    693700typedef union 
     
    732739    ops_signed_cleartext_header_t signed_cleartext_header; 
    733740    ops_signed_cleartext_body_t signed_cleartext_body; 
     741    ops_unarmoured_text_t       unarmoured_text; 
    734742    } ops_parser_content_union_t; 
    735743 
  • openpgpsdk/trunk/src/armour.c

    r224 r225  
    2727    ops_boolean_t eof64; 
    2828    unsigned long checksum; 
     29    // unarmoured text blocks 
     30    unsigned char unarmoured[8192]; 
     31    size_t num_unarmoured; 
    2932    } dearmour_arg_t; 
    3033 
     
    5659 
    5760    return c[0]; 
    58     }     
     61    } 
     62 
     63static void flush(dearmour_arg_t *arg) 
     64    { 
     65    ops_parser_content_t content; 
     66 
     67    content.content.unarmoured_text.data=arg->unarmoured; 
     68    content.content.unarmoured_text.length=arg->num_unarmoured; 
     69    CB(OPS_PTAG_CT_UNARMOURED_TEXT,&content); 
     70    arg->num_unarmoured=0; 
     71    } 
     72 
     73static int unarmoured_read_char(dearmour_arg_t *arg,ops_boolean_t skip) 
     74    { 
     75    int c; 
     76 
     77    do 
     78        { 
     79        c=read_char(arg,ops_false); 
     80        if(c < 0) 
     81            return c; 
     82        arg->unarmoured[arg->num_unarmoured++]=c; 
     83        if(arg->num_unarmoured == sizeof arg->unarmoured) 
     84            flush(arg); 
     85        } 
     86    while(skip && c == '\r'); 
     87 
     88    return c; 
     89    } 
    5990 
    6091static ops_reader_ret_t process_dash_escaped(dearmour_arg_t *arg) 
     
    70101 
    71102        if((c=read_char(arg,ops_false)) < 0) 
    72             return OPS_R_EOF; 
     103            return OPS_R_EARLY_EOF; 
    73104        if(arg->prev_nl && c == '-') 
    74105            { 
    75106            if((c=read_char(arg,ops_false)) < 0) 
    76                 return OPS_R_EOF; 
     107                return OPS_R_EARLY_EOF; 
    77108            if(c != ' ') 
    78109                { 
     
    83114                    { 
    84115                    if((c=read_char(arg,ops_false)) < 0) 
    85                         return OPS_R_EOF; 
     116                        return OPS_R_EARLY_EOF; 
    86117                    if(c != '-') 
    87118                        ERR("Bad dash-escaping (2)"); 
     
    92123            /* otherwise we read the next character */ 
    93124            if((c=read_char(arg,ops_false)) < 0) 
    94                 return OPS_R_EOF; 
     125                return OPS_R_EARLY_EOF; 
    95126            } 
    96127        body->data[body->length++]=c; 
     
    139170            { 
    140171            arg->eof64=ops_true; 
    141             return OPS_R_EOF; 
     172            return OPS_R_EARLY_EOF; 
    142173            } 
    143174        if(c == '-') 
     
    187218        { 
    188219        arg->buffered=2; 
     220        arg->eof64=ops_true; 
    189221        l >>= 2; 
    190222        } 
     
    228260        if(l != arg->checksum) 
    229261            ERR("Checksum mismatch"); 
     262        c=read_char(arg,ops_true); 
     263        if(c != '\n') 
     264            ERR("Badly terminated checksum"); 
     265        c=read_char(arg,ops_false); 
     266        if(c != '-') 
     267            ERR("Bad base64 trailer (2)"); 
    230268        } 
    231269 
     
    249287 
    250288        arg->checksum ^= arg->buffer[n] << 16; 
    251         for (i = 0; i < 8; i++) 
     289        for(i=0 ; i < 8 ; i++) 
    252290            { 
    253291            arg->checksum <<= 1; 
     
    262300 
    263301// This reader is rather strange in that it can generate callbacks for 
    264 // content - this is because plaintext is not encapsulated in PGP packets... 
     302// content - this is because plaintext is not encapsulated in PGP 
     303// packets... it also calls back for the text between the blocks. 
    265304 
    266305static ops_reader_ret_t armoured_data_reader(unsigned char *dest, 
     
    273312    ops_parser_content_t content; 
    274313    ops_reader_ret_t ret; 
     314    ops_boolean_t first; 
     315 
     316    if(arg->eof64 && !arg->buffered) 
     317        return _OPS_R_BLOCK_END; 
    275318 
    276319    while(length > 0) 
     
    284327            { 
    285328        case OUTSIDE_BLOCK: 
     329            /* This code returns EOF rather than EARLY_EOF because if 
     330               we don't see a header line at all, then it is just an 
     331               EOF (and not a BLOCK_END) */ 
    286332            while(!arg->seen_nl) 
    287                 if((c=read_char(arg,ops_true)) < 0) 
     333                if((c=unarmoured_read_char(arg,ops_true)) < 0) 
    288334                    return OPS_R_EOF; 
    289                  
     335 
     336            /* flush at this point so we definitely have room for the 
     337               header, and so we can easily erase it from the buffer */ 
     338            flush(arg); 
    290339            /* Find and consume the 5 leading '-' */ 
    291             for(count=0 ; count < 5 ;
    292                 { 
    293                 if((c=read_char(arg,ops_false)) < 0) 
     340            for(count=0 ; count < 5 ; ++count
     341                { 
     342                if((c=unarmoured_read_char(arg,ops_false)) < 0) 
    294343                    return OPS_R_EOF; 
    295                 if(c == '-') 
    296                     ++count; 
    297                 else 
    298                     count=0; 
     344                if(c != '-') 
     345                    goto reloop; 
    299346                } 
    300347 
     
    302349            for(n=0 ; n < sizeof buf-1 ; ) 
    303350                { 
    304                 if((c=read_char(arg,ops_false)) < 0) 
     351                if((c=unarmoured_read_char(arg,ops_false)) < 0) 
    305352                    return OPS_R_EOF; 
    306353                if(c == '-') 
     
    317364            for(count=1 ; count < 5 ; ++count) 
    318365                { 
    319                 if((c=read_char(arg,ops_false)) < 0) 
     366                if((c=unarmoured_read_char(arg,ops_false)) < 0) 
    320367                    return OPS_R_EOF; 
    321368                if(c != '-') 
     
    325372 
    326373            /* Consume final NL */ 
    327             if((c=read_char(arg,ops_true)) < 0) 
     374            if((c=unarmoured_read_char(arg,ops_true)) < 0) 
    328375                return OPS_R_EOF; 
    329376            if(c != '\n') 
     
    331378                break; 
    332379 
     380            /* Now we've seen the header, scrub it from the buffer */ 
     381            arg->num_unarmoured=0; 
     382 
     383            /* But now we've seen a header line, then errors are 
     384               EARLY_EOF */ 
    333385            if(!parse_headers(arg)) 
    334                 return OPS_R_EOF; 
     386                return OPS_R_EARLY_EOF; 
    335387 
    336388            if(!strcmp(buf,"BEGIN PGP SIGNED MESSAGE")) 
     
    352404 
    353405        case BASE64: 
     406            first=ops_true; 
    354407            while(length > 0) 
    355408                { 
    356409                if(!arg->buffered) 
    357410                    { 
    358                     if(arg->eof64) 
    359                         return OPS_R_EOF; 
    360                     ret=decode64(arg); 
    361                     if(ret != OPS_R_OK) 
    362                         return ret; 
     411                    if(!arg->eof64) 
     412                        { 
     413                        ret=decode64(arg); 
     414                        if(ret != OPS_R_OK) 
     415                            return ret; 
     416                        } 
    363417                    if(!arg->buffered) 
    364                         return OPS_R_EOF; 
     418                        { 
     419                        assert(arg->eof64); 
     420                        if(first) 
     421                            { 
     422                            arg->state=AT_TRAILER_NAME; 
     423                            break; 
     424                            } 
     425                        return OPS_R_EARLY_EOF; 
     426                        } 
    365427                    } 
    366428                 
     
    369431                ++dest; 
    370432                --length; 
     433                first=ops_false; 
    371434                } 
    372435            break; 
     
    376439                { 
    377440                if((c=read_char(arg,ops_false)) < 0) 
    378                     return OPS_R_EOF; 
     441                    return OPS_R_EARLY_EOF; 
    379442                if(c == '-') 
    380443                    goto got_minus2; 
     
    392455                { 
    393456                if((c=read_char(arg,ops_false)) < 0) 
    394                     return OPS_R_EOF; 
     457                    return OPS_R_EARLY_EOF; 
    395458                if(c != '-') 
    396459                    /* wasn't a trailer after all */ 
     
    400463            /* Consume final NL */ 
    401464            if((c=read_char(arg,ops_true)) < 0) 
    402                 return OPS_R_EOF; 
     465                return OPS_R_EARLY_EOF; 
    403466            if(c != '\n') 
    404467                /* wasn't a trailer line after all */ 
     
    408471                { 
    409472                if(!parse_headers(arg)) 
    410                     return OPS_R_EOF; 
     473                    return OPS_R_EARLY_EOF; 
    411474                content.content.armour_header.type=buf; 
    412475                CB(OPS_PTAG_CT_ARMOUR_HEADER,&content); 
  • openpgpsdk/trunk/src/packet-parse.c

    r221 r225  
    557557    case OPS_PTAG_CT_SIGNED_CLEARTEXT_HEADER: 
    558558    case OPS_PTAG_CT_SIGNED_CLEARTEXT_BODY: 
     559    case OPS_PTAG_CT_UNARMOURED_TEXT: 
     560    case OPS_PTAG_CT_ARMOUR_TRAILER: 
    559561        break; 
    560562