<commit_before>/*
   +----------------------------------------------------------------------+
   | PHP Embedable Ext                                                    |
   +----------------------------------------------------------------------+
   | Copyright (c) 1997-2014 The PHP Group                                |
   +----------------------------------------------------------------------+
   | This source file is subject to version 3.01 of the PHP license,      |
   | that is bundled with this package in the file LICENSE, and is        |
   | available through the world-wide-web at the following url:           |
   | http://www.php.net/license/3_01.txt                                  |
   | If you did not receive a copy of the PHP license and are unable to   |
   | obtain it through the world-wide-web, please send a note to          |
   | <EMAIL> so we can mail you a copy immediately.               |
   +----------------------------------------------------------------------+
   | Authors: <NAME> <<EMAIL>>                                   |
   +----------------------------------------------------------------------+
*/

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>

#include <libelf.h>
#include <gelf.h>

#ifdef __APPLE__
#include <mach-o/getsect.h>
#include <mach-o/dyld.h>
#endif

#include <php.h>
#include <Zend/zend_compile.h>
#include <Zend/zend_ini.h>

#include "php_ext_embed.h"

#ifndef MAX_PATH_LEN
#define MAX_PATH_LEN 256
#endif

static int read_block_data(char *file, size_t offset, size_t length, char *buf)
{
	int fd = open(file, O_RDONLY);

	if (fd < 0) {
		return FAILURE;
	}

	lseek(fd, offset, SEEK_SET);
	read(fd, buf, length);

	return SUCCESS;
}

/* Mostly came from HHVM source */
static zval* get_embed_data(char *so_path, php_ext_lib_entry *entry TSRMLS_DC)
{
	zval* code = NULL;
	size_t offset = 0, length = 0;

#ifdef __APPLE__
	int i, count = _dyld_image_count();
	for (i = 0; i < count; i++) {
		const struct mach_header_64 *head = (struct mach_header_64 *) _dyld_get_image_header(i);
		const struct section_64 *section = getsectbynamefromheader_64(head,"__text", entry->section_name);

		if (section) {
			offset = section->offset;
			length = section->size;
			break; 
		}   
	}   

#else
	GElf_Shdr shdr;<commit_msg>Mac OS X support both 32&64bits<commit_after>/*
   +----------------------------------------------------------------------+
   | PHP Embedable Ext                                                    |
   +----------------------------------------------------------------------+
   | Copyright (c) 1997-2014 The PHP Group                                |
   +----------------------------------------------------------------------+
   | This source file is subject to version 3.01 of the PHP license,      |
   | that is bundled with this package in the file LICENSE, and is        |
   | available through the world-wide-web at the following url:           |
   | http://www.php.net/license/3_01.txt                                  |
   | If you did not receive a copy of the PHP license and are unable to   |
   | obtain it through the world-wide-web, please send a note to          |
   | license@php.net so we can mail you a copy immediately.               |
   +----------------------------------------------------------------------+
   | Authors: <NAME> <<EMAIL>>                                   |
   +----------------------------------------------------------------------+
*/

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>

#include <libelf.h>
#include <gelf.h>

#ifdef __APPLE__
#include <mach-o/getsect.h>
#include <mach-o/dyld.h>
#endif

#include <php.h>
#include <Zend/zend_compile.h>
#include <Zend/zend_ini.h>

#include "php_ext_embed.h"

#ifndef MAX_PATH_LEN
#define MAX_PATH_LEN 256
#endif

static int read_block_data(char *file, size_t offset, size_t length, char *buf)
{
	int fd = open(file, O_RDONLY);

	if (fd < 0) {
		return FAILURE;
	}

	lseek(fd, offset, SEEK_SET);
	read(fd, buf, length);

	return SUCCESS;
}

/* Mostly came from HHVM source */
static zval* get_embed_data(char *so_path, php_ext_lib_entry *entry TSRMLS_DC)
{
	zval* code = NULL;
	size_t offset = 0, length = 0;

#ifdef __APPLE__
	int i, count = _dyld_image_count();
	for (i = 0; i < count; i++) {
#ifndef __LP64__
		const struct mach_header *head = (struct mach_header *) _dyld_get_image_header(i);
		const struct section *section = getsectbynamefromheader(head,"__text", entry->section_name);
#else
		const struct mach_header_64 *head = (struct mach_header_64 *) _dyld_get_image_header(i);
		const struct section_64 *section = getsectbynamefromheader_64(head,"__text", entry->section_name);
#endif

		if (section) {
			offset = section->offset;
			length = section->size;
			break; 
		}   
	}   

#else
	GElf_Shdr shdr;
{'fraction_non_alphanumeric': 0.2424053266749896, 'fraction_numerical': 0.013316687473990845, 'mean_word_length': 1.5596379126730564, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6224768', 'n_tokens_mistral': 1537, 'n_tokens_neox': 1428, 'n_words': 407}