<commit_before>//
//  src/macho_fd_symbols.c
//  tbd
//
//  Created by inoahdev on 11/21/18.
//  Copyright © 2018 inoahdev. All rights reserved.
//

#include <fcntl.h>

#include <stdlib.h>
#include <string.h>

#include <unistd.h>

#include "c_str_utils.h"
#include "mach-o/nlist.h"

#include "arch_info.h"
#include "macho_file_symbols.h"

#include "swap.h"

#include "tbd.h"
#include "yaml.h"

/*
 * For performance, compare strings using the largest byte-size integers
 * possible, instead of iterating with strncmp.
 */

static bool
is_objc_class_symbol(const char *const symbol,
                     const uint64_t length,
                     const char **const symbol_out,
                     uint64_t *const length_out)
{
    if (length < 8) {
        return false;
    }

    const uint64_t first = *(uint64_t *)symbol;

    /*
     * Objc-class symbols can have different prefixes.
     */

    if (first == 5495340712935444319) {
        if (length < 13) {
            return false;
        }

        /*
         * The check here is `if (first == "_OBJC_CL")`, checking of whether
         * the prefix is "_OBJC_CLASS_$".
         * 
         * The check below is `if (second == "ASS_")`.
         */

        const uint32_t second = *(uint32_t *)(symbol + 8);
        if (second != 1599296321) {
            return false;
        }

        if (symbol[12] != '$') {
            return false;
        }

        *symbol_out = symbol + 13;
        *length_out = length - 13;
    } else if (first == 4993752304437055327) {
        if (length < 17) {
            return false;
        }

        /*
         * The check here is `if (first == "_OBJC_ME")`, checking if the prefix
         * is "_OBJC_METACLASS_$".
         * 
         * The check below is `if (second == "TACLASS")`.
         */

        const uint64_t second = *(uint64_t *)(symbol + 8);
        if (second != 6868925396587594068) {
            return false;
        }

        if (symbol[16] != '$') {
            return false;
        }

        *symbol_out = symbol + 17;
        *length_out = length - 17;
    } else if (first == 7810191059381808942) {
        if (length < 16) {
            return false;
        }

        /*
         * The check here is `if (first == ".objc_cl")`, checking if the prefix
         * is ".objc_class_name".
         * 
         * The check below is `if (second == ".ass_name")`.
         */

        const uint64_t second = *(uint64_t *)(symbol + 8);
        if (second != 7308604896967881569) {
            return false;
        }

        *symbol_out = symbol + 16;
        *length_out = length - 16;
    } else {
        return false;
    }

    return true;
}

static int
is_objc_ivar_symbol(const char *const symbol, const uint64_t length) {<commit_msg>Optimize objc symbol-checks for better performance<commit_after>//
//  src/macho_fd_symbols.c
//  tbd
//
//  Created by inoahdev on 11/21/18.
//  Copyright © 2018 inoahdev. All rights reserved.
//

#include <fcntl.h>

#include <stdlib.h>
#include <string.h>

#include <unistd.h>

#include "c_str_utils.h"
#include "mach-o/nlist.h"

#include "arch_info.h"
#include "macho_file_symbols.h"

#include "swap.h"

#include "tbd.h"
#include "yaml.h"

/*
 * For performance, compare strings using the largest byte-size integers
 * possible, instead of iterating with strncmp.
 */

static bool
is_objc_class_symbol(const char *const symbol,
                     const uint64_t length,
                     const char **const symbol_out,
                     uint64_t *const length_out)
{
    if (length < 13) {
        return false;
    }

    const uint64_t first = *(uint64_t *)symbol;

    /*
     * Objc-class symbols can have different prefixes.
     */

    if (first == 5495340712935444319) {
        /*
         * The check here is `if (first == "_OBJC_CL")`, checking of whether
         * the prefix is "_OBJC_CLASS_$".
         *
         * The check below is `if (second == "ASS_")`.
         */

        const uint32_t second = *(uint32_t *)(symbol + 8);
        if (second != 1599296321) {
            return false;
        }

        if (symbol[12] != '$') {
            return false;
        }

        *symbol_out = symbol + 13;
        *length_out = length - 13;
    } else if (first == 4993752304437055327) {
        /*
         * The check here is `if (first == "_OBJC_ME")`, checking if the prefix
         * is "_OBJC_METACLASS_$".
         */

        if (length < 17) {
            return false;
        }

        /* 
         * The check below is `if (second == "TACLASS")`.
         */

        const uint64_t second = *(uint64_t *)(symbol + 8);
        if (second != 6868925396587594068) {
            return false;
        }

        if (symbol[16] != '$') {
            return false;
        }

        *symbol_out = symbol + 17;
        *length_out = length - 17;
    } else if (first == 7810191059381808942) {
        /*
         * The check here is `if (first == ".objc_cl")`, checking if the prefix
         * is ".objc_class_name".
         */

        if (length < 16) {
            return false;
        }

        /* 
         * The check below is `if (second == ".ass_name")`.
         */

        const uint64_t second = *(uint64_t *)(symbol + 8);
        if (second != 7308604896967881569) {
            return false;
        }

        *symbol_out = symbol + 16;
        *length_out = length - 16;
    } else {
        return false;
    }

    return true;
}

static bool 
is_objc_ivar_symbol(const char *const symbol, const uint64_t length) {
    if (length < 12) {
        return false;
    }
    
{'fraction_non_alphanumeric': 0.1239964317573595, 'fraction_numerical': 0.058340767172167705, 'mean_word_length': 1.714769975786925, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6404492', 'n_tokens_mistral': 2096, 'n_tokens_neox': 1816, 'n_words': 493}