从mysql C连接器到NSData的longblob数据

时间:2011-04-07 18:39:19

标签: mysql objective-c cocoa

我尝试拾取奇怪的文件,这在C连接器的php中工作得很好: 选择是做: 从tablemedia limit 1;

中选择media_ogg

拾取结果的代码是:

           row = mysql_fetch_row(qResult);
           unsigned long *lengths;
           lengths = mysql_fetch_lengths(qResult);
            NSLog(@"%@",[NSNumber numberWithUnsignedInteger:lengths[i]]);

            NSData *data = [NSData dataWithBytes:row[i] length:lengths[i]];*/
            NSString *url = [NSString stringWithFormat:@"/Users/alex/test.ogg"];
            FILE *fp;
            fp = fopen([url UTF8String], "wb");
            fwrite(row[0], lengths[0], 1, fp);
            fclose(fp);
            mysql_free_result(qResult);

文件正在写,但不要完成。 看起来我不明白从MYSQL_ROW封装到字节的规则。可能有人可以建议吗?

1 个答案:

答案 0 :(得分:0)

太难理解,但是数据在base64中被解码了。编码你可以看到下面。测试完成。

            NSString *base64 = [NSString stringWithCString:row[0] encoding:NSASCIIStringEncoding];
            NSData *base64Data = [base64 dataUsingEncoding:NSASCIIStringEncoding];
            const unsigned char *base64Bytes = [base64Data bytes];
            NSMutableData *mutableData = [NSMutableData dataWithCapacity:[base64Data length]];
            NSUInteger lentext = [base64Data length];
            unsigned long ixtext = 0;
            unsigned char ch = 0;
            //const unsigned char *base64Bytes = nil;
            short i = 0, ixinbuf = 0;
            BOOL flignore = NO;
            BOOL flendtext = NO;
            unsigned char inbuf[4], outbuf[3];


            while( YES ) {
                if( ixtext >= lentext ) break;
                ch = base64Bytes[ixtext++];
                flignore = NO;

                if( ( ch >= 'A' ) && ( ch <= 'Z' ) ) ch = ch - 'A';
                else if( ( ch >= 'a' ) && ( ch <= 'z' ) ) ch = ch - 'a' + 26;
                else if( ( ch >= '0' ) && ( ch <= '9' ) ) ch = ch - '0' + 52;
                else if( ch == '+' ) ch = 62;
                else if( ch == '=' ) flendtext = YES;
                else if( ch == '/' ) ch = 63;
                else flignore = YES;

                if( ! flignore ) {
                    short ctcharsinbuf = 3;
                    BOOL flbreak = NO;

                    if( flendtext ) {
                        if( ! ixinbuf ) break;
                        if( ( ixinbuf == 1 ) || ( ixinbuf == 2 ) ) ctcharsinbuf = 1;
                        else ctcharsinbuf = 2;
                        ixinbuf = 3;
                        flbreak = YES;
                    }

                    inbuf [ixinbuf++] = ch;

                    if( ixinbuf == 4 ) {
                        ixinbuf = 0;
                        outbuf [0] = ( inbuf[0] << 2 ) | ( ( inbuf[1] & 0x30) >> 4 );
                        outbuf [1] = ( ( inbuf[1] & 0x0F ) << 4 ) | ( ( inbuf[2] & 0x3C ) >> 2 );
                        outbuf [2] = ( ( inbuf[2] & 0x03 ) << 6 ) | ( inbuf[3] & 0x3F );

                        for( i = 0; i < ctcharsinbuf; i++ )
                            [mutableData appendBytes:&outbuf[i] length:1];
                    }

                    if( flbreak )  break;
                }
            }